%% ISO27DIY eBook Template
%% For use with Pandoc + XeLaTeX
%% Usage: pandoc --template=iso27diy.tex --pdf-engine=xelatex

\documentclass[11pt, a4paper, oneside]{book}

%% ─── PACKAGES ────────────────────────────────────────────────────────────────

\usepackage{fontspec}
\usepackage{geometry}
\usepackage{xcolor}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{fancyhdr}
\usepackage{fancyvrb}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{mdframed}
\usepackage{enumitem}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{calc}
\usepackage{colortbl}
\usepackage{hyperref}

%% FIX: LaTeX Error: No counter 'none' defined (Pandoc 3.1.8+ bug)
\newcounter{none}
\usepackage{footnote}
\usepackage{imakeidx}
\usepackage{microtype}
\usepackage{parskip}
\usepackage{etoolbox}
\usepackage{caption}
\usepackage{eso-pic}

%% ─── PANDOC COMPATIBILITY ────────────────────────────────────────────────────

\providecommand{\tightlist}{%
  \setlength{\itemsep}{4pt}\setlength{\parskip}{0pt}}

$if(highlighting-macros)$
$highlighting-macros$
$endif$

%% ─── COLORS ──────────────────────────────────────────────────────────────────

\definecolor{coverBg}{HTML}{ffffff}
\definecolor{navyBg}{HTML}{1a2535}
\definecolor{brandRed}{HTML}{b31c1c}
\definecolor{brandBlue}{HTML}{2d8fd4}
\definecolor{bodyColor}{HTML}{444444}
\definecolor{mutedColor}{HTML}{888888}
\definecolor{ruleColor}{HTML}{dde3ea}
\definecolor{calloutBg}{HTML}{eef4fb}
\definecolor{calloutText}{HTML}{185fa5}
\definecolor{dotColor}{HTML}{dde3ea}
\definecolor{headerBg}{HTML}{f4f4f4}
\definecolor{headerRule}{HTML}{dddddd}

%% Semantic aliases
\colorlet{headingColor}{navyBg}
\colorlet{calloutBorder}{brandBlue}
\colorlet{stepLabelColor}{brandRed}
\colorlet{pageNumColor}{brandBlue}

%% ─── PAGE GEOMETRY ───────────────────────────────────────────────────────────

\geometry{
  a4paper,
  top=25mm,
  bottom=25mm,
  inner=28mm,
  outer=22mm,
  headsep=8mm,
  footskip=12mm
}

%% ─── FONTS ───────────────────────────────────────────────────────────────────

\setmainfont{Inter}[
  UprightFont    = *-Regular,
  BoldFont       = *-Bold,
  ItalicFont     = *-Italic,
  BoldItalicFont = *-BoldItalic
]
\setsansfont{Inter}[
  UprightFont    = *-Regular,
  BoldFont       = *-Bold,
  ItalicFont     = *-Italic,
  BoldItalicFont = *-BoldItalic
]
\setmonofont{Courier New}

%% ─── TEXT COLOR ──────────────────────────────────────────────────────────────

\color{bodyColor}

%% ─── HYPERREF ────────────────────────────────────────────────────────────────

\hypersetup{
  colorlinks=true,
  linkcolor=brandBlue,
  urlcolor=brandBlue,
  citecolor=brandBlue,
  pdfauthor={$author$},
  pdftitle={$title$},
  pdfsubject={$subtitle$},
  hidelinks
}

%% ─── INDEX ───────────────────────────────────────────────────────────────────

\makeindex[title=Index of Terms, intoc]

%% ─── HEADINGS ────────────────────────────────────────────────────────────────

%% Section numbers as plain arabic (book class default would give "0.1")
\renewcommand{\thesection}{\arabic{section}}

%% H1 — number shown as "1. Title"
\titleformat{\section}
  {\color{headingColor}\fontsize{18}{22}\selectfont\bfseries}
  {\thesection.}
  {0.5em}
  {}
  [\vspace{4pt}{\color{brandBlue}\rule{44pt}{3pt}}\vspace{6pt}]

%% Show "VOLGENDE VRAAG: <title>" at the bottom of the previous page before
%% breaking to the new section. Skipped for the very first section.
\let\isosection\section
\RenewDocumentCommand{\section}{o m}{%
  \ifnum\value{section}>0
    \par\vspace{12pt}%
    {\color{stepLabelColor}\fontsize{8}{10}\selectfont\bfseries
     VOLGENDE VRAAG: #2}%
    \par\vspace{4pt}%
  \fi
  \clearpage
  \IfValueTF{#1}{\isosection[#1]{#2}}{\isosection{#2}}%
}

%% H2
\titleformat{\subsection}
  {\color{headingColor}\fontsize{14}{18}\selectfont\bfseries}
  {}
  {0em}
  {}

%% H3
\titleformat{\subsubsection}
  {\color{headingColor}\fontsize{12}{16}\selectfont\bfseries}
  {}
  {0em}
  {}

\titlespacing{\section}{0pt}{24pt}{8pt}
\titlespacing{\subsection}{0pt}{18pt}{6pt}
\titlespacing{\subsubsection}{0pt}{14pt}{4pt}

%% ─── CHAPTER STYLE ───────────────────────────────────────────────────────────
%% Dot pattern full-page, white fade at bottom, red left bar

\newif\ifchapterbg
\chapterbgtrue

\renewcommand{\contentsname}{Inhoud}

\titleformat{\chapter}[display]
  {\normalfont}
  {}
  {0pt}
  {%
    \ifchapterbg
    %% White base
    \AddToShipoutPictureBG*{%
      \AtPageLowerLeft{%
        \color{white}\rule{\paperwidth}{\paperheight}%
      }%
    }%
    %% Dot pattern
    \AddToShipoutPictureBG*{%
      \begin{tikzpicture}[remember picture, overlay]
        \foreach \x in {0,0.42,...,21} {
          \foreach \y in {0,0.42,...,29.7} {
            \fill[dotColor] (\x cm, \y cm) circle (0.7pt);
          }
        }
      \end{tikzpicture}%
    }%
    %% White fade at bottom (covers lower ~35% of page)
    \AddToShipoutPictureBG*{%
      \AtPageLowerLeft{%
        \color{white}\rule{\paperwidth}{104mm}%
      }%
    }%
    %% Red left bar
    \AddToShipoutPictureBG*{%
      \AtPageLowerLeft{%
        \color{brandRed}\rule{8pt}{\paperheight}%
      }%
    }%
    \fi
    \vspace*{60mm}%
    \begin{minipage}[t]{0.85\textwidth}
      \color{brandBlue}\fontsize{9}{11}\selectfont\bfseries
      \MakeUppercase{Hoofdstuk \thechapter}\\[8pt]
      \color{navyBg}\fontsize{28}{34}\selectfont\bfseries
  }
  [%
    \end{minipage}%
    \vspace{8mm}%
    {\color{brandRed}\rule{44pt}{3pt}}%
  ]

\titlespacing{\chapter}{8pt}{0pt}{40pt}

%% Unnumbered chapter style (TOC, index) — plain dark text on white
\titleformat{name=\chapter,numberless}[display]
  {\normalfont}
  {}
  {0pt}
  {%
    \vspace*{20mm}%
    \color{headingColor}\fontsize{28}{34}\selectfont\bfseries
  }
  [%
    \vspace{4mm}%
    {\color{brandRed}\rule{44pt}{3pt}}%
  ]

\titlespacing{name=\chapter,numberless}{0pt}{0pt}{20pt}

%% ─── STEP LABEL ──────────────────────────────────────────────────────────────

\newcommand{\steplabel}[1]{%
  \vspace{6pt}%
  {\color{stepLabelColor}\fontsize{8}{10}\selectfont\bfseries\MakeUppercase{#1}}%
  \vspace{4pt}%
}

%% ─── CALLOUT BOX ─────────────────────────────────────────────────────────────
%% Usage: \begin{callout}{Title} ... \end{callout}

\newmdenv[
  topline=false,
  bottomline=false,
  rightline=false,
  leftline=true,
  linewidth=3pt,
  linecolor=calloutBorder,
  backgroundcolor=calloutBg,
  innerleftmargin=12pt,
  innerrightmargin=12pt,
  innertopmargin=10pt,
  innerbottommargin=10pt,
  skipabove=12pt,
  skipbelow=12pt
]{calloutbox}

\newenvironment{callout}[1]{%
  \begin{calloutbox}%
  {\color{calloutText}\fontsize{8.5}{11}\selectfont\bfseries #1}\\[4pt]%
}{%
  \end{calloutbox}%
}

%% ─── LISTS ───────────────────────────────────────────────────────────────────

\setlist[itemize,1]{
  leftmargin=16pt,
  itemsep=4pt,
  parsep=0pt,
  label={\color{brandRed}\rule{4pt}{4pt}}
}
\setlist[itemize,2]{
  leftmargin=14pt,
  itemsep=2pt,
  label={\color{ruleColor}\rule{3pt}{3pt}}
}
\setlist[enumerate,1]{
  leftmargin=18pt,
  itemsep=4pt,
  parsep=0pt,
  label={\color{brandRed}\bfseries\arabic*.}
}
\setlist[enumerate,2]{
  leftmargin=16pt,
  itemsep=2pt,
  label={\color{brandRed}\bfseries\alph*.}
}

%% ─── TABLES ──────────────────────────────────────────────────────────────────

\setlength{\arrayrulewidth}{0.5pt}
\arrayrulecolor{ruleColor}
\renewcommand{\arraystretch}{1.4}

%% Helpers for styled header rows (use \rowcolor{\tableheadcolor} in preamble)
\newcommand{\tableheadcolor}{\rowcolor{navyBg}}
\newcommand{\tableheadtext}{\color{white}\bfseries}

%% ─── RUNNING HEADERS & FOOTERS ───────────────────────────────────────────────

\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

%% Light grey header bar with red left accent — title left, page number right
\fancyhead[RO]{%
  \begin{tikzpicture}[remember picture, overlay]
    \fill[headerBg] (current page.north west) rectangle ++(\paperwidth, -10mm);
    \fill[headerRule] (current page.north west) rectangle ++(\paperwidth, -0.5pt);
    \fill[brandRed] (current page.north west) rectangle ++(3pt, -10mm);
    \node[anchor=west, xshift=14pt, yshift=-5mm] at (current page.north west)
      {\color{mutedColor}\fontsize{8}{10}\selectfont $title$};
    \node[anchor=east, xshift=-16pt, yshift=-5mm] at (current page.north east)
      {\color{brandBlue}\fontsize{9}{11}\selectfont\thepage};
  \end{tikzpicture}%
}
\fancyfoot[L]{%
  \color{mutedColor}\fontsize{8}{10}\selectfont iso27diy.com%
}

%% No header/footer on chapter opener pages
\fancypagestyle{plain}{%
  \fancyhf{}%
  \renewcommand{\headrulewidth}{0pt}%
  \renewcommand{\footrulewidth}{0pt}%
}

%% ─── FOOTNOTES ───────────────────────────────────────────────────────────────

\renewcommand{\footnoterule}{%
  \vspace{4pt}%
  {\color{ruleColor}\hrule height 0.5pt width 40mm}%
  \vspace{4pt}%
}

%% ─── TABLE OF CONTENTS ───────────────────────────────────────────────────────

\setcounter{tocdepth}{2}
\setcounter{secnumdepth}{1}

\titlecontents{chapter}
  [0pt]
  {\vspace{8pt}\color{headingColor}\bfseries}
  {\contentslabel{0pt}}
  {}
  {\hfill\color{pageNumColor}\contentspage}

\titlecontents{section}
  [28pt]
  {\vspace{2pt}\color{bodyColor}}
  {\contentslabel{28pt}}
  {}
  {\hfill\color{mutedColor}\contentspage}

\titlecontents{subsection}
  [28pt]
  {\vspace{1pt}\small\color{mutedColor}}
  {}
  {}
  {\hfill\color{mutedColor}\contentspage}

%% ─── CODE BLOCKS ─────────────────────────────────────────────────────────────

\DefineVerbatimEnvironment{Highlighting}{Verbatim}{
  fontsize=\small,
  xleftmargin=12pt,
  frame=leftline,
  framerule=3pt,
  rulecolor=\color{brandBlue},
  commandchars=\\\{\}
}

%% ─── IMAGES ──────────────────────────────────────────────────────────────────

\captionsetup{
  font={small, color=bodyColor},
  labelfont={bf, color=headingColor},
  labelsep=period
}
\setkeys{Gin}{width=\linewidth, keepaspectratio}

%% ─── COVER PAGE ──────────────────────────────────────────────────────────────
%% Print-friendly: white background with dot pattern, red left bar

\newcommand{\makecover}{%
  \begin{titlepage}%
    %% White base
    \AddToShipoutPictureBG*{%
      \AtPageLowerLeft{%
        \color{coverBg}\rule{\paperwidth}{\paperheight}%
      }%
    }%
    %% Dot pattern via TikZ
    \AddToShipoutPictureBG*{%
      \begin{tikzpicture}[remember picture, overlay]
        \foreach \x in {0,0.42,...,21} {
          \foreach \y in {0,0.42,...,29.7} {
            \fill[dotColor] (\x cm, \y cm) circle (0.7pt);
          }
        }
      \end{tikzpicture}%
    }%
    %% Red left bar
    \AddToShipoutPictureBG*{%
      \AtPageLowerLeft{%
        \color{brandRed}\rule{8pt}{\paperheight}%
      }%
    }%
    \vspace*{10mm}%
    \begin{flushleft}%
      \hspace*{16pt}%
      \begin{minipage}{0.85\textwidth}
        \includegraphics[width=48mm]{$logo$}\par%
        \vspace{32mm}%
        {\hyphenpenalty=10000\exhyphenpenalty=10000\raggedright%
         \color{navyBg}\fontsize{48}{56}\selectfont\bfseries%
         $if(covertitle)$$covertitle$$else$$title$$endif$\par}%
        \vspace{10mm}%
        {\color{brandBlue}\rule{104pt}{6pt}\par}%
        \vspace{8mm}%
        {\color{bodyColor}\fontsize{20}{26}\selectfont $subtitle$\par}%
      \end{minipage}%
    \end{flushleft}%
    \vfill%
    \hspace*{16pt}%
    \begin{minipage}{0.85\textwidth}
      {\color{mutedColor}\fontsize{9}{12}\selectfont iso27diy.com\par}%
      \vspace{3mm}%
      {\color{mutedColor}\fontsize{8}{11}\selectfont $year$ $author$\par}%
    \end{minipage}%
    \vspace*{10mm}%
  \end{titlepage}%
}

%% ─── DOCUMENT ────────────────────────────────────────────────────────────────

\begin{document}

\makecover

\frontmatter

$if(toc)$
\chapterbgfalse
\tableofcontents
\chapterbgtrue
\clearpage
$endif$

\pagenumbering{arabic}
\setcounter{page}{1}

$body$

$if(index)$
\backmatter
\printindex
$endif$

\end{document}
