% Margins
\usepackage[inner=35mm, outer=20mm, top=30mm, bottom=25mm]{geometry}

% Set line spacing to 1.15
\usepackage{setspace}
\linespread{1.15}

% Typography
\usepackage{iftex}
\ifluatex
\usepackage{libertinus-otf}
\else
\usepackage{mathtools} % Math improvements
\usepackage{libertinus} % Libertinus font for text and math
\usepackage[T1]{fontenc} % Use T1 font encoding
\usepackage{textcomp} % Additional symbols
\usepackage[varqu, varl]{inconsolata} % Better monospace font (with alternative "quote" [varqu] and "l" [varl])
%\usepackage{libertinust1math} % Libertinus math font
\fi

\usepackage[english]{babel} % Typographical rules
\usepackage{microtype} % Micro-typography improvements (slightly more compact, better to read)

% Use sans-serif font for sections
\usepackage{titlesec}
\titleformat{\part}[display]{\normalfont\Huge\sffamily\bfseries\centering}{\partname\ \thepart}{20pt}{}
\titleformat{\chapter}[display]{\normalfont\Huge\sffamily\bfseries}{\chaptertitlename\ \thechapter}{10pt}{}
\titleformat{\section}{\normalfont\Large\sffamily\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}{\normalfont\large\sffamily\bfseries}{\thesubsection}{1em}{}
\titleformat{\subsubsection}{\normalfont\normalsize\sffamily\bfseries}{\thesubsubsection}{1em}{}
\titleformat{\paragraph}[runin]{\normalfont\normalsize\sffamily\bfseries}{\theparagraph}{1em}{}

\setcounter{secnumdepth}{3} % Number subsubsections

% Parts and chapters
\NewCommandCopy{\oldchapter}{\chapter}
\RenewDocumentCommand{\chapter}{s o m}{% #1 = star (no number), #2 = short title, #3 = title
  \IfBooleanTF{#1}{%
    \oldchapter*{#3}% Starred chapter
    \addcontentsline{toc}{chapter}{#3}% Add to the table of contents
  }{%
    \IfValueTF{#2}{%
      \oldchapter[#2]{#3}% Chapter with short title
    }{%
      \oldchapter{#3}% Regular chapter
    }%
    \addcontentsline{lof}{chapter}{\thechapter.\ #3} % Add to the list of figures
    \addcontentsline{lot}{chapter}{\thechapter.\ #3} % Add to the list of tables
  }%
}

\newcommand{\importchapter}[2]{% Load a chapter (#2) in a path (#1)
  \glsresetall% Reset the acronyms
  \graphicspath{{#1/figures/}}% Enable relative paths for images

  \cleardoublepage% Start on a right page
  \includefrom{#1}{#2}% Include with relative paths \input in the chapter
}

% Headers
\usepackage{emptypage} % Remove headers and footers on empty pages
\usepackage{etoolbox} % Patching commands
\usepackage{fancyhdr} % Headers and footers

\pagestyle{fancy} % Use fancy headers and footers
\fancyhf{} % Clear headers and footers
\fancyhead[LO, RE]{}
\fancyfoot[C]{\thepage} % Page number in the center of the footer

\appto\mainmatter{
  \renewcommand{\chaptermark}[1]{\markboth{\chaptername~\thechapter~--\ #1}{}} % Chapter formatting
  \renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}} % Section formatting
  \fancyhead[LO]{\small\nouppercase{\leftmark}} % Chapter in the left on odd pages
  \fancyhead[RE]{\small\nouppercase{\rightmark}} % Section in the right on even pages
}

\setlength{\headheight}{14pt}

% Less hbox/vbox badness messages
\hfuzz=20pt
\vfuzz=20pt
\hbadness=2000
\vbadness=\maxdimen
