% ==============================================================================
% uqo-examen.sty — Style commun pour les examens UQO (IMM1003 / IMM1033)
% Université du Québec en Outaouais — Simon-Pierre Boucher
% Design modernisé : couverture pleine page aux bandes UQO, titres sans-serif,
% bandeaux de questions avec pastilles de points, encadrés et graphiques unifiés.
% Compilation : LuaLaTeX (compatible XeLaTeX)
% ==============================================================================
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{uqo-examen}[2026/08/04 Style examens UQO v2]

% --- Option solutionnaire ---
\newif\ifsolutionnaire
\solutionnairefalse
\DeclareOption{solutionnaire}{\solutionnairetrue}
\ProcessOptions\relax

% --- Polices et langue (LuaLaTeX / XeLaTeX) ---
\RequirePackage{fontspec}
\RequirePackage[french]{babel}
\frenchsetup{StandardLists=true}
\setmainfont{TeX Gyre Termes}
\setsansfont{TeX Gyre Heros}
\RequirePackage{unicode-math}
\setmathfont{TeX Gyre Termes Math}

% --- Mise en page ---
\RequirePackage[letterpaper,margin=2.3cm,top=2.7cm,bottom=2.7cm]{geometry}
\setlength{\headheight}{15pt}
\RequirePackage{graphicx}
\RequirePackage{amsmath}
\RequirePackage{booktabs}
\RequirePackage{array}
\RequirePackage{tabularx}
\RequirePackage{multirow}
\RequirePackage{longtable}
\RequirePackage[table]{xcolor}
\RequirePackage{enumitem}
\RequirePackage{fancyhdr}
\RequirePackage{lastpage}
\RequirePackage{tikz}
\usetikzlibrary{calc}
\RequirePackage{tcolorbox}
\tcbuselibrary{skins,breakable}
\RequirePackage{needspace}
\RequirePackage{xurl}
\urlstyle{same}
\RequirePackage[font=small, labelfont={sf,bf,color=uqoBleu}, textfont={color=uqoGris}, labelsep=period]{caption}
\RequirePackage{siunitx}
\sisetup{
  locale = FR,
  group-separator = {\,},
  group-minimum-digits = 4,
  output-decimal-marker = {,}
}

% --- Couleurs UQO ---
\definecolor{uqoBleu}{RGB}{0, 62, 126}
\definecolor{uqoBleuClair}{RGB}{0, 102, 179}
\definecolor{uqoOr}{RGB}{198, 163, 0}
\definecolor{uqoGris}{RGB}{88, 89, 91}
\definecolor{uqoVert}{RGB}{0, 128, 70}
\definecolor{uqoRouge}{RGB}{180, 35, 24}
\definecolor{uqoBgClair}{RGB}{240, 244, 248}

% --- Métadonnées de l'examen (à définir dans le document) ---
\newcommand{\examCours}{}      \newcommand{\setexamCours}[1]{\renewcommand{\examCours}{#1}}
\newcommand{\examTitreCours}{} \newcommand{\setexamTitreCours}[1]{\renewcommand{\examTitreCours}{#1}}
\newcommand{\examType}{}       \newcommand{\setexamType}[1]{\renewcommand{\examType}{#1}}
\newcommand{\examSession}{}    \newcommand{\setexamSession}[1]{\renewcommand{\examSession}{#1}}
\newcommand{\examDuree}{}      \newcommand{\setexamDuree}[1]{\renewcommand{\examDuree}{#1}}
\newcommand{\examRemise}{}     \newcommand{\setexamRemise}[1]{\renewcommand{\examRemise}{#1}}
\newcommand{\examPonderation}{}\newcommand{\setexamPonderation}[1]{\renewcommand{\examPonderation}{#1}}
\newcommand{\examCouverture}{} \newcommand{\setexamCouverture}[1]{\renewcommand{\examCouverture}{#1}}

% --- En-têtes / pieds de page (filet doré, typographie sans) ---
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{1.2pt}
\renewcommand{\headrule}{\hbox to\headwidth{\color{uqoOr}\leaders\hrule height \headrulewidth\hfill}}
\renewcommand{\footrulewidth}{0.4pt}
\renewcommand{\footrule}{\hbox to\headwidth{\color{uqoBgClair!60!uqoGris!30}\leaders\hrule height \footrulewidth\hfill}}
\fancyhead[L]{\small\sffamily\textcolor{uqoBleu}{\textbf{\examCours}}\;{\color{uqoGris}·\;\examTitreCours}}
\fancyhead[R]{\small\sffamily\color{uqoGris}\examType\ifsolutionnaire\ --- \textcolor{uqoVert}{\textbf{SOLUTIONNAIRE}}\fi}
\fancyfoot[L]{\small\sffamily\color{uqoGris}UQO --- Simon-Pierre Boucher}
\fancyfoot[R]{\small\sffamily\color{uqoGris}Page \textcolor{uqoBleu}{\textbf{\thepage}} de \pageref{LastPage}}

% --- Style global des graphiques (pgfplots) ---
\RequirePackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{dateplot}
\pgfplotsset{every axis/.append style={
  axis line style={uqoGris!55},
  tick style={uqoGris!55},
  ticklabel style={font=\footnotesize\sffamily, /pgf/number format/assume math mode},
  xlabel style={font=\small\sffamily, color=uqoBleu},
  ylabel style={font=\small\sffamily, color=uqoBleu},
  grid=major,
  major grid style={uqoGris!16},
  legend style={font=\footnotesize\sffamily, draw=uqoGris!35, fill=white, rounded corners=2pt},
}}

% --- Pastille de points ---
\newcommand{\uqopastille}[1]{{\sffamily\bfseries\textcolor{uqoBleu}{(#1 points)}}}
\newcommand{\uqopastilleclair}[1]{{\sffamily\bfseries\textcolor{white}{#1 points}}}

% ==============================================================================
% PAGE COUVERTURE — bandes UQO, typographie institutionnelle (numérotation continue)
% ==============================================================================
\newcommand{\pagetitreexamen}{%
\begingroup
\thispagestyle{empty}%
\begin{tikzpicture}[remember picture, overlay]
  \fill[uqoBleu] (current page.north west) rectangle ([yshift=-4.9cm]current page.north east);
  \fill[uqoOr]   ([yshift=-4.9cm]current page.north west) rectangle ([yshift=-5.1cm]current page.north east);
  \node[fill=white, inner xsep=16pt, inner ysep=10pt]
    at ($(current page.north)+(0,-1.75cm)$) {\includegraphics[width=3.1cm]{assets/uqo-logo.png}};
  \node[anchor=north, text=white, font=\sffamily\bfseries\large]
    at ($(current page.north)+(0,-3.45cm)$) {Université du Québec en Outaouais};
  \node[anchor=north, text=uqoBgClair, font=\sffamily\small]
    at ($(current page.north)+(0,-4.15cm)$) {Département des sciences administratives};
  \fill[uqoBleu] (current page.south west) rectangle ([yshift=1.3cm]current page.south east);
  \fill[uqoOr]   ([yshift=1.3cm]current page.south west) rectangle ([yshift=1.5cm]current page.south east);
  \node[anchor=south, text=white, font=\sffamily\footnotesize]
    at ($(current page.south)+(0,0.5cm)$)
    {UQO --- Département des sciences administratives --- \examSession};
\end{tikzpicture}%
\vspace*{3.0cm}
\begin{center}
{\fontsize{23}{27}\selectfont\sffamily\bfseries\color{uqoBleu}\examType\par}
\ifsolutionnaire
  \vspace{8pt}
  {\large\sffamily\bfseries\color{uqoVert}SOLUTIONNAIRE --- Réservé à l'enseignant\par}
\fi
\vspace{10pt}
{\color{uqoOr}\rule{4.2cm}{2pt}}\par
\vspace{10pt}
{\LARGE\sffamily\bfseries\color{uqoBleu}\examCours\par}
\vspace{4pt}
{\Large\sffamily\color{uqoBleu}\examTitreCours\par}
\vspace{7pt}
{\small\itshape\color{uqoGris}\examCouverture\par}

\vspace{18pt}
{\renewcommand{\arraystretch}{1.55}
\begin{tabular}{@{}>{\sffamily\bfseries\color{uqoBleu}}m{3.2cm}>{\raggedright\arraybackslash}m{10.2cm}@{}}
\toprule
Professeur & Simon-Pierre Boucher \\
Session & \examSession \\
Modalité & \examDuree \\
Remise & \examRemise \\
Pondération & \examPonderation \\
Total & 100 points \\
\bottomrule
\end{tabular}\par}

\ifsolutionnaire\else
\vspace{20pt}
{\renewcommand{\arraystretch}{2.1}
\begin{tabular}{@{}>{\sffamily\bfseries\color{uqoBleu}}m{3.2cm}m{10.2cm}@{}}
\toprule
Nom, prénom & \textcolor{uqoGris!55}{\dotfill} \\
Code permanent & \textcolor{uqoGris!55}{\dotfill} \\
Groupe & \textcolor{uqoGris!55}{\dotfill} \\
Signature & \textcolor{uqoGris!55}{\dotfill} \\
\bottomrule
\end{tabular}\par}
\fi

\vspace{14pt}
{\footnotesize\color{uqoGris}Ce questionnaire compte \textbf{\pageref{LastPage}} pages, section \emph{Sources} incluse. Vérifiez qu'il est complet avant de commencer.}
\end{center}
\clearpage
\endgroup
}

% ==============================================================================
% BOÎTES
% ==============================================================================
\newtcolorbox{consignes}{
  enhanced, breakable, sharp corners,
  colback=uqoBgClair, colframe=uqoBleu, boxrule=0.6pt,
  title={Instructions générales},
  fonttitle=\sffamily\bfseries, coltitle=white, colbacktitle=uqoBleu,
  left=9pt, right=9pt, top=6pt, bottom=6pt,
  toptitle=3pt, bottomtitle=3pt
}

\newtcolorbox{miseengarde}[1][Important]{
  enhanced, breakable, sharp corners,
  colback=uqoRouge!4, colframe=uqoRouge, boxrule=0.6pt,
  title={#1},
  fonttitle=\sffamily\bfseries, coltitle=white, colbacktitle=uqoRouge,
  left=9pt, right=9pt, top=6pt, bottom=6pt,
  toptitle=3pt, bottomtitle=3pt
}

\newtcolorbox{donnees}[1][Données]{
  enhanced, breakable, sharp corners,
  colback=white, colframe=uqoBleuClair, boxrule=0.6pt,
  title={#1},
  fonttitle=\sffamily\bfseries, coltitle=white, colbacktitle=uqoBleuClair,
  left=9pt, right=9pt, top=7pt, bottom=7pt,
  toptitle=3pt, bottomtitle=3pt
}

% Boîte solution (solutionnaire seulement)
\newtcolorbox{solution}[1][Solution]{
  enhanced, breakable, sharp corners,
  colback=uqoVert!4, colframe=uqoVert, boxrule=0.6pt,
  title={#1},
  fonttitle=\sffamily\bfseries, coltitle=white, colbacktitle=uqoVert,
  left=9pt, right=9pt, top=6pt, bottom=6pt,
  toptitle=3pt, bottomtitle=3pt
}

% Notes de correction (solutionnaire seulement)
\newtcolorbox{notescorrection}[1][Notes de correction]{
  enhanced, breakable, sharp corners,
  colback=uqoOr!7, colframe=uqoOr, boxrule=0.6pt,
  title={#1},
  fonttitle=\sffamily\bfseries, coltitle=uqoBleu, colbacktitle=uqoOr!45,
  left=9pt, right=9pt, top=6pt, bottom=6pt,
  toptitle=3pt, bottomtitle=3pt
}

% ==============================================================================
% SECTIONS ET QUESTIONS
% ==============================================================================
\newcounter{partie}
\newcommand{\partieexamen}[2]{% {Titre}{points ou vide}
  \stepcounter{partie}%
  \needspace{6\baselineskip}%
  \vspace{16pt}%
  \noindent\begin{tcolorbox}[enhanced, sharp corners, colback=uqoBleu, colframe=uqoBleu, boxrule=0pt,
      left=10pt, right=10pt, top=7pt, bottom=7pt,
      borderline south={2pt}{-3pt}{uqoOr},
      before skip=0pt, after skip=10pt]
    {\sffamily\bfseries\large\textcolor{white}{Partie \Alph{partie} --- #1}%
     \if\relax\detokenize{#2}\relax\else\hfill\uqopastilleclair{#2}\fi}
  \end{tcolorbox}\par
}

\newcounter{questionexam}
\newenvironment{questionexam}[2]{% {Titre}{points}
  \refstepcounter{questionexam}%
  \needspace{9\baselineskip}%
  \vspace{13pt}%
  \noindent{\color{uqoBleu}\rule{\textwidth}{1.6pt}}\par\nopagebreak\vspace{3.5pt}%
  \noindent{\large\sffamily\bfseries\textcolor{uqoBleu}{Question \arabic{questionexam} --- #1}%
    \hfill{\large\sffamily\bfseries\textcolor{uqoBleu}{(#2 points)}}}\par\nopagebreak\vspace{2.5pt}%
  \noindent{\color{uqoOr}\rule{\textwidth}{0.9pt}}\par\nopagebreak\vspace{5pt}%
  \noindent\ignorespaces
}{\par}

% --- Boîte « formule » : équations mises en évidence, rendu net ---
\newtcolorbox{formule}{
  enhanced, sharp corners,
  colback=white, colframe=uqoBleu, boxrule=0.8pt,
  left=10pt, right=10pt, top=7pt, bottom=7pt,
  before skip=8pt, after skip=8pt,
  halign=center
}

% --- Espace de réponse (version étudiante : espace vide encadré) ---
\newcommand{\espacereponse}[1]{% hauteur
  \ifsolutionnaire\else
  \par\vspace{4pt}\noindent
  \begin{tikzpicture}
    \draw[uqoGris!60, rounded corners=2pt] (0,0) rectangle (\textwidth-2pt, #1);
  \end{tikzpicture}\par
  \fi
}

% Lignes de réponse
\newcounter{uqolignes}
\newcommand{\lignesreponse}[1]{% nombre de lignes
  \ifsolutionnaire\else
  \par\vspace{6pt}
  \setcounter{uqolignes}{0}%
  \loop\ifnum\value{uqolignes}<#1
    \noindent\textcolor{uqoGris!55}{\rule{\textwidth}{0.4pt}}\par\vspace{18pt}
    \stepcounter{uqolignes}%
  \repeat
  \fi
}

% --- Divers ---
\newcommand{\pts}[1]{\textbf{(#1~points)}}
\setlength{\parindent}{0pt}
\setlength{\parskip}{5pt}
\renewcommand{\arraystretch}{1.25}
