\documentclass[12pt,oneside,a4paper,openright]{book}

% ---------------- Packages ----------------
\usepackage[a4paper,margin=2.5cm]{geometry}
\usepackage{titlesec}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{xcolor}
\usepackage{tcolorbox}
\usepackage{tabularx}
\usepackage{longtable}

\usepackage{array}
\usepackage{ragged2e}
\usepackage{wrapfig}
\usepackage{atbegshi}
\usepackage{float}
\usepackage{tabularx}
\usepackage{array}   % for >{\centering\arraybackslash}
\usepackage{algorithm}
\usepackage{algpseudocode}

% ---- Enable Bold + Italic fonts ----
\usepackage{lmodern}

% --- Tighten vertical spacing around floats (figures, tables) ---
\setlength{\textfloatsep}{4pt plus 2pt minus 2pt}  % text <-> float
\setlength{\floatsep}{4pt plus 2pt minus 2pt}      % float <-> float
\setlength{\intextsep}{4pt plus 2pt minus 2pt}     % inline float <-> text


% --- BibLaTeX for per-chapter references ---
\usepackage[
    backend=biber,
    style=numeric,
    citestyle=numeric,
    sorting=none,
    url=false,
    doi=false,
    defernumbers=true
]{biblatex}

% ---------------- Per-Chapter .bib Files ----------------
\addbibresource[label=ch1bib]{chapter-1/references.bib}
\addbibresource[label=ch2bib]{chapter-2/references.bib}
\addbibresource[label=ch3bib]{chapter-3/references.bib}
\addbibresource[label=ch4bib]{chapter-4/references.bib}
\addbibresource[label=ch5bib]{chapter-5/references.bib}
\addbibresource[label=ch6bib]{chapter-6/references.bib}
\addbibresource[label=ch7bib]{chapter-7/references.bib}
\addbibresource[label=ch8bib]{chapter-8/references.bib}
\addbibresource[label=ch9bib]{chapter-9/references.bib}
\addbibresource[label=ch10bib]{chapter-10/references.bib}
\addbibresource[label=ch11bib]{chapter-11/references.bib}
\addbibresource[label=ch12bib]{chapter-12/references.bib}
\addbibresource[label=ch13bib]{chapter-13/references.bib}
\addbibresource[label=ch14bib]{chapter-14/references.bib}
\addbibresource[label=ch15bib]{chapter-15/references.bib}



% Load hyperref AFTER biblatex
\usepackage{hyperref}

\onehalfspacing
\hypersetup{hidelinks}

% =========================================================
% HEADER / FOOTER SETTINGS (Corrected: Page No NOT Italic)
% =========================================================
\setlength{\headheight}{16pt}

\fancypagestyle{bookstyle}{
  \fancyhf{}%

  % HEADER — right aligned, Blue + Bold + Italic
  \fancyhead[R]{\footnotesize\bfseries\itshape\textcolor{blue}{\makebox[0pt][r]{Principles of Hybrid Intelligent Systems}}}


  % FOOTER — Page number Blue + Bold (NOT italic), ISBN Bold-Italic
  \fancyfoot[C]{%
      \begin{minipage}{\textwidth}
          \raggedright \footnotesize\bfseries\itshape\textcolor{blue}{
          ISBN: 978-81-994969-7-2 (Print); 978-81-994969-1-0 (Online)}\\[4pt]
          \centering {\bfseries\thepage}
      \end{minipage}
}


  % Thick blue header line
  \renewcommand{\headrulewidth}{1.2pt}
  \renewcommand{\headrule}{\color{blue}\hrule height 1.2pt}

  \renewcommand{\footrulewidth}{0pt}
}

% PAGE STYLE FOR CHAPTER OPENING PAGES
\fancypagestyle{plain}{
  \fancyhf{}%
  \fancyhead[R]{\small\bfseries\itshape\textcolor{blue}{Principles of Hybrid Intelligent Systems}}

  \fancyfoot[C]{%
      \begin{minipage}{\textwidth}
          \centering {\bfseries\textcolor{blue}{\thepage}} \\[4pt]
          \raggedright \small\bfseries\itshape\textcolor{blue}{
          ISBN: 978-81-994969-7-2 (Print); 978-81-994969-1-0 (Online)}
      \end{minipage}
  }

  \renewcommand{\headrulewidth}{1.2pt}
  \renewcommand{\headrule}{\color{blue}\hrule height 1.2pt}

  \renewcommand{\footrulewidth}{0pt}
}

% =========================================================
% SECTION NUMBERING
% =========================================================
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\thesection.\arabic{subsection}}

\titleformat{\section}
  {\large\bfseries}{\thesection.}{1em}{}

\titleformat{\subsection}
  {\normalsize\bfseries}{\thesubsection}{1em}{}

% Chapter Format
\titleformat{\chapter}[display]{\Huge\bfseries}{}{0pt}{}

% =========================================================
% BLUE BIBLIOGRAPHY CITES
% =========================================================
\DeclareFieldFormat{labelnumber}{\textcolor{blue}{#1}}

\DeclareCiteCommand{\cite}
  {\usebibmacro{prenote}}
  {\bibhyperref{\textcolor{blue}{[\printfield{labelnumber}]}}}
  {\multicitedelim}
  {\usebibmacro{postnote}}

\defbibheading{subbibchapter}{%
  \section*{\bfseries References}
}

% =========================================================
% CUSTOM CONTENTS SUPPORT
% =========================================================
\newwrite\ctcfile
\newif\ifctcopen
\ctcopenfalse

\newcommand{\CTCline}[4]{%
  \parbox[t]{0.9cm}{%
    \raggedright\raisebox{\baselineskip}{\textbf{#1.}}%
  } &
  \begin{tabular}[t]{@{}b{\dimexpr\linewidth-2cm\relax}>{\raggedleft\arraybackslash}b{2cm}@{}}
    \textcolor{blue}{\hyperref[#4-start]{\textbf{#2}}}\dotfill &
    \textcolor{blue}{\hyperref[#4-start]{\textbf{\pageref{#4-start}--\pageref{#4-end}}}}\\
    \textit{#3} & \\
  \end{tabular}\\[0.8em]
}

\newcommand{\startchapter}[3]{%
  \ifctcopen\else
    \immediate\openout\ctcfile=\jobname.ctc
    \global\ctcopentrue
  \fi
  \refstepcounter{chapter}%
  \setcounter{section}{0}
  \setcounter{subsection}{0}
  \label{#3-start}%
  \immediate\write\ctcfile{\string\CTCline{\thechapter}{#1}{#2}{#3}}%
}

\newcommand{\chapterend}[1]{%
  \label{#1-end}%
}

\AtEndDocument{%
  \ifctcopen\immediate\closeout\ctcfile\fi
}

% ----------------------------------------------------------
% FIGURE NUMBERING WITHOUT CHAPTER PREFIX (PER CHAPTER)
% ----------------------------------------------------------
\makeatletter
\renewcommand{\thefigure}{\arabic{figure}}
% Reset figure numbering at each chapter start
\@addtoreset{figure}{chapter}
\makeatother

% =========================================================

\begin{document}

\AtBeginShipoutNext{\AtBeginShipoutDiscard}

\pagenumbering{gobble} % no page numbers on covers
\makeatletter\let\cleardoublepage\clearpage\makeatother
% --- PAGE 1 ---
% --- COVERS: two pages back-to-back, no blanks ---
\begingroup
  % silence openright just for these two pages
  \makeatletter\let\cleardoublepage\relax\makeatother

  % full-bleed margins for both pages
  \newgeometry{left=0cm,right=0cm,top=0cm,bottom=0cm}

  % --- PAGE 1 ---
  \thispagestyle{empty}
  \noindent\includegraphics[width=\paperwidth,height=\paperheight]{images/coverimage1.png}
  \newpage

  % --- PAGE 2 ---
  \thispagestyle{empty}
  \noindent\includegraphics[width=\paperwidth,height=\paperheight]{images/coverimage2.png}

  % restore margins AFTER the pair (a clear happens here, which is fine *after* both)
  \restoregeometry
\endgroup


\input{./coverpage/coverpage1}
\clearpage
\input{./coverpage/coverpage2}
\clearpage
\input{./coverpage/coverpage3}
\clearpage
\input{./coverpage/coverpage4}
\clearpage
\begin{spacing}{1.5}
\input{./coverpage/preface}
\newpage
\input{./coverpage/Acknowledgments}
\newpage
\setcounter{page}{1}
\pagenumbering{roman}
\input{./coverpage/aboutthisbook}
\end{spacing}
\newpage

% ---------------- Contents ----------------
% ---------------- Contents ----------------
\clearpage
\begin{center}
{\Huge \textbf{\textcolor{blue}{Contents}}}
\end{center}

\vspace{1cm}

\noindent
\begin{longtable}{@{}p{0.9cm}p{\dimexpr\textwidth-0.9cm\relax}@{}}
\textbf{S.No} &
  \begin{tabular}{@{}p{\dimexpr\linewidth-1.5cm\relax}>{\raggedleft\arraybackslash}p{1.5cm}@{}}
    \centering\textbf{Chapter Name} & \textbf{Pages}\\
  \end{tabular}\\[0.4em]
% ---- table body: generated lines ----
\InputIfFileExists{\jobname.ctc}{}{}
\end{longtable}

\clearpage




% ---------------- Main Matter ----------------
\pagenumbering{arabic}
\pagestyle{bookstyle}

% =================== CHAPTER 1 ===================
\begin{refsection}[ch1bib]
   \input{chapter-1/chapter1}
  {\begin{spacing}{1.5}
     \printbibliography[heading=subbibchapter]
   \end{spacing}}

  \chapterend{ch1}
\end{refsection}

\clearpage

% =================== CHAPTER 2 ===================
\begin{refsection}[ch2bib]
   \input{chapter-2/chapter2}
   \printbibliography[heading=subbibchapter]
   \chapterend{ch2}
\end{refsection}
\clearpage
% =================== CHAPTER 3 ===================
\begin{refsection}[ch3bib]
   \input{chapter-3/chapter3}
  {\begin{spacing}{1.5}
     \printbibliography[heading=subbibchapter]
   \end{spacing}}

  \chapterend{ch3}
\end{refsection}

\clearpage
% =================== CHAPTER 4 ===================
\begin{refsection}[ch4bib]
   \input{chapter-4/chapter4}
  {\begin{spacing}{1.5}
     \printbibliography[heading=subbibchapter]
   \end{spacing}}

  \chapterend{ch4}
\end{refsection}

\clearpage
% =================== CHAPTER 5 ===================
\begin{refsection}[ch5bib]
 \input{chapter-5/chapter5}
  {\begin{spacing}{1.5}
     \printbibliography[heading=subbibchapter]
   \end{spacing}}

  \chapterend{ch5}
\end{refsection}

\clearpage
% =================== CHAPTER 6 ===================
\begin{refsection}[ch6bib]
  %\input{chapter-6/chapter6}
  {\begin{spacing}{1.5}
     \printbibliography[heading=subbibchapter]
   \end{spacing}}

  \chapterend{ch6}
\end{refsection}
\clearpage
% =================== CHAPTER 7 ===================
\begin{refsection}[ch7bib]
%\input{chapter-7/chapter7}

  {\begin{spacing}{1.5}
     \printbibliography[heading=subbibchapter]
   \end{spacing}}

  \chapterend{ch7}
\end{refsection}
\clearpage
% =================== CHAPTER 8 ===================
\begin{refsection}[ch8bib]
 %\input{chapter-8/chapter8}
  {\begin{spacing}{1.5}
     \printbibliography[heading=subbibchapter]
   \end{spacing}}

  \chapterend{ch8}
\end{refsection}
\clearpage
% =================== CHAPTER 9 ===================
\begin{refsection}[ch9bib]
 %\input{chapter-9/chapter9}
  {\begin{spacing}{1.5}
     \printbibliography[heading=subbibchapter]
   \end{spacing}}

  \chapterend{ch9}
\end{refsection}
\clearpage
% =================== CHAPTER 10 ===================
\begin{refsection}[ch10bib]
 %\input{chapter-10/chapter10}
  {\begin{spacing}{1.5}
     \printbibliography[heading=subbibchapter]
   \end{spacing}}

  \chapterend{ch10}
\end{refsection}
\clearpage
% =================== CHAPTER 11 ===================
\begin{refsection}[ch11bib]
 %\input{chapter-11/chapter11}
  {\begin{spacing}{1.5}
     \printbibliography[heading=subbibchapter]
   \end{spacing}}

  \chapterend{ch11}
\end{refsection}
\clearpage
% =================== CHAPTER 12 ===================
\begin{refsection}[ch12bib]
%\input{chapter-12/chapter12}
  {\begin{spacing}{1.5}
     \printbibliography[heading=subbibchapter]
   \end{spacing}}

  \chapterend{ch12}
\end{refsection}
\clearpage
% =================== CHAPTER 13 ===================
\begin{refsection}[ch13bib]
 %\input{chapter-13/chapter13}
  {\begin{spacing}{1.5}
     \printbibliography[heading=subbibchapter]
   \end{spacing}}

  \chapterend{ch13}
\end{refsection}
\clearpage
% =================== CHAPTER 14 ===================
\begin{refsection}[ch14bib]
%\input{chapter-14/chapter14}
  {\begin{spacing}{1.5}
     \printbibliography[heading=subbibchapter]
   \end{spacing}}

  \chapterend{ch14}
\end{refsection}
\clearpage

% =================== CHAPTER 15===================
\begin{refsection}[ch15bib]
%\input{chapter-15/chapter15}
  {\begin{spacing}{1.5}
     \printbibliography[heading=subbibchapter]
   \end{spacing}}

  \chapterend{ch15}
\end{refsection}
\clearpage


\end{document}
