% Définition des couleurs utiliées dans le cours
% Définition des styles utilisés (firstchapterstyle, coursstyle)
% Définition des titres de \section, \subsection, \subsubsection
% Définition des styles de:
%	- paragraph 
% 	- remarque
% 	- propriété
%	- définition
% 	- exemple
%	- théorème
% 	- démonstration
%	- attention


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Définition des couleurs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\definecolor{chapterbacknum}{HTML}{2d70a7}
\definecolor{chapterbacktop}{RGB}{18,37,59}
\definecolor{chapterfirstpage}{HTML}{d9ebff}
\definecolor{chaptertitlecolor}{HTML}{ffffff}
\definecolor{minitoc}{HTML}{edecae}
\definecolor{minitoctitle}{HTML}{562222}

\definecolor{backnumpagecours}{HTML}{2d70a7}
\definecolor{backpagecours}{HTML}{ecf2fc}

\definecolor{sectioncolor}{HTML}{1b437c}
\definecolor{subsectioncolor}{HTML}{eb6100}
\definecolor{subsubsectioncolor}{HTML}{038117}
\definecolor{paragraphcolor}{HTML}{a417ba}
\definecolor{prop}{HTML}{1f6dfa}
\definecolor{rem}{HTML}{7ec670}
\definecolor{defin}{HTML}{d076e6}
\definecolor{exemple}{HTML}{f4ce95}
\definecolor{theorem}{HTML}{e71771}
\definecolor{demo}{HTML}{b3b3b3}
\definecolor{attention}{HTML}{fb5858}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Définition des styles
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\fancypagestyle{firstchapterstyle}
{
	\fancyhf{}
}

\fancypagestyle{coursstyle}
{
	\pagecolor{backpagecours}
	\fancyhf{}
	\fancyfoot[C]{\tikz\node[fill=backnumpagecours,text=white,rectangle,rounded corners=5pt,inner sep=2mm]{\fontfamily{lmss}\bfseries\thepage};}
}

\fancypagestyle{bbstyle}
{
	\pagecolor{pagebb}
	\fancyhf{}
	\fancyfoot[C]{\tikz\node[fill=bbtitle,text=white,rectangle,rounded corners=5pt,inner sep=2mm]{\fontfamily{lmss}\bfseries\thepage};}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Définition des formats (\chapter, \section, \subsection, \subsubsection, \paragraph)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand\makeminitoc{%
\begin{center}
    \begin{tikzpicture}
      \node[fill=minitoc,rounded corners=3mm,drop shadow] (minitoc) {\begin{minipage}{\textwidth}\minitoc\end{minipage}};
	  \node[decorate, decoration={zigzag, segment length=4mm, amplitude=0.5mm},fill=minitoctitle, text=white,rotate=2] at (minitoc.south) {Dans ce chapitre};
    \end{tikzpicture}
  \end{center}
  \newpage
  \thispagestyle{coursstyle}
}

\newcommand\numchapter{}
\newcommand{\chapicon}{\faList}

\titleformat{\chapter}[display]
	{\normalfont\Huge\bfseries} % style du texte du chapitre
	{} % label, on gère nous-mêmes le numéro avec TikZ
	{0pt} % espacement entre label et titre
	{%
		\if@mainmatter
			\renewcommand\numchapter{\thechapter}
		\else
			\renewcommand\numchapter{\chapicon}
			\if@frontmatter
				\definecolor{chapterbacknum}{HTML}{d59039}
				\definecolor{chapterbacktop}{HTML}{be7635}
				\definecolor{chapterfirstpage}{HTML}{ece0c3}
				\definecolor{backnumpagecours}{HTML}{7c5c08}
				\definecolor{backpagecours}{HTML}{ece0c3}
			\else % donc forcément backmatter
				\definecolor{chapterbacknum}{HTML}{464505}
				\definecolor{chapterbacktop}{HTML}{f2ee5d}
				\definecolor{chapterfirstpage}{HTML}{f0efd5}
				\definecolor{backnumpagecours}{HTML}{a19e2d}
				\definecolor{backpagecours}{HTML}{f0efd5}
				\definecolor{chaptertitlecolor}{HTML}{464505}
			\fi
		\fi
		\begin{tikzpicture}[remember picture,overlay]
		\fill[chapterbacktop] (current page.north west) rectangle ($(current page.north east)+(0,-4cm)$);
		% Ellipse rouge en haut à gauche
		\node[fill=chapterbacknum, text=white, ellipse, minimum height=1cm, minimum width=2cm, anchor=north west, scale=4] 
			at ([xshift=0cm,yshift=0cm]current page.north west) {\fontfamily{lmss}\fontsize{20}{18}\selectfont \bfseries \numchapter};
			% Titre
			\node[anchor=north east, font=\Huge\bfseries\color{chaptertitlecolor}\fontfamily{lmss}\selectfont, text width=0.8\paperwidth, align=right] at ([xshift=-1cm,yshift=-1.5cm]current page.north east) {#1};
    \end{tikzpicture}%
  }
  [%
	\pagecolor{chapterfirstpage}
	\thispagestyle{firstchapterstyle}
	\if@mainmatter
	\makeminitoc
	\fi
  ]

% Suppression du numéro automatique
\renewcommand{\thechapter}{\arabic{chapter}}


%%%%%%%%%%%%% 
% section
%%%%%%%%%%%%%%%%%%

\renewcommand{\thesection}{\arabic{section}}
\titleformat{\section}[block]
{%
	\begin{tikzpicture}
	\node[minimum width=10mm, minimum height=10mm, inner sep=2mm] (num) {\huge\fontfamily{lmss}\selectfont\bfseries\thesection};
	\fill[sectioncolor] (num.south west) arc(180:90:10mm) (num.north east) -- (num.south east) -- (num.south west);
	\node[text=white, left] at (num.east) {\huge\fontfamily{lmss}\selectfont\bfseries\thesection};
	\draw[thick,sectioncolor] ($(num.south east)+(-1pt,0.5pt)$) -- ($(num.south east)+(\dimexpr\textwidth-10mm,0)$);
	\node[right,text=sectioncolor] at (num.east) {\huge\fontfamily{lmss}\selectfont\bfseries #1};
	\end{tikzpicture}
}
{}
{0ex}
{\vspace*{-5mm}}

%%%%%%%%%%%%% 
% subsection
%%%%%%%%%%%%%%%%%%

\renewcommand{\thesubsection}{\arabic{subsection}}
\titleformat{\subsection}[block]
{%
	\begin{tikzpicture}
	\node[minimum width=10mm, minimum height=10mm, inner sep=2mm] (num) {\Large\fontfamily{lmss}\selectfont\bfseries\thesubsection};
	\fill[subsectioncolor] (num.south west) -- (num.north west) -- (num.east) -- (num.south west);
	\node[right,text=white] at (num.west) {\Large\fontfamily{lmss}\selectfont\bfseries\thesubsection};
	\node[right,text=subsectioncolor] at (num.east) {\Large\fontfamily{lmss}\selectfont\bfseries #1};
	\node at ($(num.west)+(-10mm,0)$) {};
	\end{tikzpicture}
}
{}
{0ex}
{}

%%%%%%%%%%%%% 
% subsubsection
%%%%%%%%%%%%%%%%%%

\setcounter{secnumdepth}{3}
\renewcommand{\thesubsubsection}{\alph{subsubsection}}
\titleformat{\subsubsection}[block]
{%
	\begin{tikzpicture}
	\node[inner sep=1mm,circle,text=white,fill=subsubsectioncolor] (num) {\fontfamily{lmss}\selectfont\bfseries\thesubsubsection};
	%\fill[subsubsectioncolor] (num.center) circle (5mm);
	%\node[text=white] at (num.center) {\fontfamily{lmss}\selectfont\bfseries\thesubsubsection};
	\node[right,text=subsubsectioncolor] at (num.east) {\fontfamily{lmss}\selectfont\bfseries #1};
	\node at ($(num.west)+(-20mm,0)$) {};
	\end{tikzpicture}
}
{}
{0ex}
{}


%%%%%%%%%%%%% 
% paragraph
%%%%%%%%%%%%%%%%%%%%%%%%%

\titleformat{\paragraph}[runin]
{%
	\color{paragraphcolor}
	\fontfamily{lmss}\selectfont\bfseries
}{}{0pt}{#1}

%%%%%%%
% Remarque
%%%%%%%%%%%%%%

\newtcbtheorem{tcbrem}{Remarque}
{%
	enhanced,
	breakable,
	label={rem:\thetcbcounter},
	separator sign none,
	before skip=2mm,
	after skip=3mm,
	boxrule=0.4pt,
	left=5mm,
	right=2mm,
	top=1mm,
	bottom=1mm,
	colback = rem!20,
	colframe = rem,
	fonttitle = \fontfamily{lmss}\selectfont\bfseries,
	sharp corners,rounded corners=southeast,arc is angular,arc=3mm,
	drop fuzzy shadow,
	underlay={
		\begin{tcbclipinterior}
			\draw[help lines,step=5mm,rem,shift={(interior.north west)},opacity=0.15] (interior.south west) grid (interior.north east);
		\end{tcbclipinterior},
		\path[fill=rem,draw=rem,shorten <=-0.05mm,shorten >=-0.05mm] ([yshift=3mm]interior.south east)--++(-0.4,-0.1)--++(0.1,-0.2);
	}
}
{rem}

\newenvironment{rem}[1][]
{%
	\itemclass{rem}
	\arrayrulecolor{rem}
	\begin{tcbrem}{\if\@empty#1\else(#1)\fi}{}
}
{%
	\end{tcbrem}
	\itemclass{black}
}


%%%%%%%%
% Propriétés
%%%%%%%%%%%%%%%

\newtcbtheorem{tcbprop}{Propriété}
{%
	enhanced jigsaw,
	breakable,
	arc = 0pt,
	separator sign none,
	label={prop:\thechapter.\thetcbcounter},
	fonttitle = \fontfamily{lmss}\selectfont\bfseries,
	attach boxed title to top left={xshift=-0.25pt},
	colbacktitle = prop,
	colback = prop!30,
	frame style = {color=backpagecours},
	boxrule = 0pt,
	boxed title style = {
		boxrule=0pt,
		sharp corners
	},
	overlay = { \draw[prop,thick] (frame.south east) -- (frame.south west) -- (frame.north west); },
		% enhanced,
}
{prop}

\newenvironment{prop}[1][]
{%
	\itemclass{prop}
	\arrayrulecolor{prop}
	\begin{tcbprop}{\if\@empty#1\else(#1)\fi}{#1}
}
{%
	\end{tcbprop}
	\itemclass{black}
}

%%%%%%%%
% Définitions
%%%%%%%%%%%%%%%%

\newtcbtheorem{tcbdef}{Définition}
{%
	enhanced,
	breakable,
	separator sign none,
	before skip = 2mm,
	after skip = 2mm,
	colback = defin!30,
	colframe = defin,
	boxrule = 0.2mm,
	attach boxed title to top left = { xshift = 1cm , yshift*=1mm-\tcboxedtitleheight },
	varwidth boxed title*=-3cm,
	boxed title style = {
		frame code = {
			\path[fill = defin!30!black] ([yshift=-1mm,xshift=-1mm]frame.north west)
				arc[start angle=0,end angle=180,radius=1mm] ([yshift=-1mm,xshift=1mm]frame.north east)
				arc[start angle=180,end angle=0,radius=1mm];
			\path[left color=defin!60!black,right color=defin!60!black,middle color=defin!80!black] ([xshift=-2mm]frame.north west) 
				-- ([xshift=2mm]frame.north east)[rounded corners=1mm]
				-- ([xshift=1mm,yshift=-1mm]frame.north east)
				-- (frame.south east) -- (frame.south west)-- ([xshift=-1mm,yshift=-1mm]frame.north west)[sharp corners]-- cycle;
		},
		interior engine=empty
	},
	fonttitle = \fontfamily{lmss}\selectfont\bfseries
}
{def}

\newenvironment{defin}[1][]
{%
	\itemclass{defin}
	\arrayrulecolor{defin}
	\begin{tcbdef}{\if\@empty#1\else(#1)\fi}{}
}
{%
	\end{tcbdef}
	\itemclass{black}
}

%%%%%%%
% Exemple
%%%%%%%%%%%%%%

\newtcbtheorem{tcbexemple}{Exemple}
{%
	enhanced,
	label={exemple:\thechapter.\thetcbcounter},
	breakable,
	separator sign none,
	before skip=2mm,
	after skip=3mm,
	boxrule=0.4pt,
	left=5mm,
	right=2mm,
	top=1mm,
	bottom=1mm,
	colback = exemple!20,
	colframe = exemple,
	fonttitle = \fontfamily{lmss}\selectfont\bfseries\color{exemple!60!red},
	sharp corners,rounded corners=southeast,arc is angular,arc=3mm,
	drop fuzzy shadow,
	underlay={
		\begin{tcbclipinterior}
			\draw[help lines,step=5mm,exemple,shift={(interior.north west)},opacity=0.15] (interior.south west) grid (interior.north east);
		\end{tcbclipinterior},
		\path[fill=exemple,draw=exemple,shorten <=-0.05mm,shorten >=-0.05mm] ([yshift=3mm]interior.south east)--++(-0.4,-0.1)--++(0.1,-0.2);
	}
}
{exemple}

\newenvironment{exemple}[1][]
{%
	\itemclass{exemple!60!red}
	\arrayrulecolor{exemple!60!red}
	\begin{tcbexemple}{\if\@empty#1\else(#1)\fi}{#1}
}
{%
	\end{tcbexemple}
	\itemclass{black}
}

%%%%%%%%
% Théorèmes
%%%%%%%%%%%%%%

\newtcbtheorem{tcbtheorem}{Théorème}
{%
	label={thm:\thechapter.\thetcbcounter},
	enhanced,%
	breakable,
	separator sign none,
	skin = enhancedlast jigsaw,
	attach boxed title to top left = 
	{%
		xshift = -4mm,%
		yshift = -0.5mm
	},
	fonttitle = \fontfamily{lmss}\selectfont\bfseries,
	coltitle = white,
	varwidth boxed title = 0.7\linewidth,
	colbacktitle = theorem,
	colframe = theorem,
	interior style = {%
		top color = theorem!10!white,
		bottom color = theorem!30!white
	},
	boxed title style = {%
		empty,arc = 0pt,
		outer arc = 0pt,
		boxrule = 0pt
	},
	underlay boxed title = {%
		\fill[theorem] (title.north west) 
					-- (title.north east)
					-- +(\tcboxedtitleheight-1mm,-\tcboxedtitleheight+1mm)
					-- ([xshift=4mm,yshift=0.5mm]frame.north east) 
					-- +(0mm,-1mm)-- (title.south west) 
					-- cycle;
		\fill[theorem!45!white!50!black] ([yshift=-0.5mm]frame.north west)	-- +(-0.4,0) -- +(0,-0.3) -- cycle;
		\fill[theorem!45!white!50!black] ([yshift=-0.5mm]frame.north east)-- +(0,-0.3) -- +(0.4,0) -- cycle;  
	}
}
{th}

\newenvironment{thm}[1][]
{%
	\itemclass{theorem}
	\arrayrulecolor{theorem}
	\begin{tcbtheorem}{\if\@empty#1\else(#1)\fi}{}
}
{%
	\end{tcbtheorem}
	\itemclass{black}
}

%%%%%%%%%
% Demo
%%%%%%%%%%%%%

\newtcbtheorem{tcbdemo}{Démonstration}
{%
	enhanced,
	breakable,
	separator sign none,
	before skip = 2mm,
	after skip = 3mm,
	boxrule = 0.4pt,
	left = 5mm,
	right = 2mm,
	top = 1mm,
	bottom = 1mm,
	colback = demo!20,
	colframe = demo,
	fonttitle = \fontfamily{lmss}\selectfont\bfseries,
	sharp corners,rounded corners=southeast,arc is angular,arc=3mm,
	drop fuzzy shadow,
	underlay={
		\begin{tcbclipinterior}
			\draw[help lines,step=5mm,demo,shift={(interior.north west)},opacity=0.15] (interior.south west) grid (interior.north east);
		\end{tcbclipinterior},
		\path[fill=demo,draw=demo,shorten <=-0.05mm,shorten >=-0.05mm] ([yshift=3mm]interior.south east)--++(-0.4,-0.1)--++(0.1,-0.2);
	}
}
{demo}

\newenvironment{demo}[1][]
{%
	\itemclass{demo}
	\arrayrulecolor{demo}
	\begin{tcbdemo}{\if\@empty#1\else(#1)\fi}{}
}
{%
	\end{tcbdemo}
	\itemclass{black}
}

%%%%%%%
% Attention
%%%%%%%%%%%%%%

\newtcbtheorem{tcbwarning}{Attention}
{%
	enhanced,	
	breakable,
	separator sign none,
	before skip = 2mm,
	after skip = 3mm,
	boxrule = 0.4pt,
	left = 2mm,
	right = 2mm,
	top = 1mm,
	bottom = 1mm,
	colback = attention!20,
	colframe = attention,
	sharp corners,
	fonttitle = \fontfamily{lmss}\selectfont\bfseries\faWarning~,
	drop fuzzy shadow
}
{warning}

\newenvironment{attention}[1][]
{%
	\itemclass{warning}
	\arrayrulecolor{warning}
	\begin{tcbwarning}{\if\@empty#1\else(#1)\fi}{}
}
{%
	\end{tcbwarning}
	\itemclass{black}
}