%%%%%%%%%%%% 
% Première page de chapitre
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\chapter{%
  \if@openright
    \cleardoublepage
  \else
    \clearpage
  \fi
  \if@mainmatter
	\pagestyle{main}
  \fi
  \global\@topnum\z@
  \@afterindenttrue
  \secdef\@chapter\@schapter
}

\newsavebox\title@mathweb
\newlength\title@mathweb@length

\def\@makechapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
      \if@mainmatter
		\begin{lrbox}{\title@mathweb}\begin{titreChapitre}\huge\ECFWebster#1\end{titreChapitre}\end{lrbox}
		\settowidth{\title@mathweb@length}{\usebox{\title@mathweb}}
		\begin{center}
			\ifdim\the\title@mathweb@length<0.95\textwidth
				\usebox{\title@mathweb}
			\else
				\begin{titreChapitre}
					\begin{minipage}{0.8\textwidth}
						\begin{center}
						\huge\ECFWebster
						#1
						\end{center}
					\end{minipage}
				\end{titreChapitre}
			\fi
		\end{center}
        \par\nobreak
        \vskip 20\p@
		\begin{minitocbox}
		  \color{titrecolor}
 		  \minitoc
		\end{minitocbox}
	  \else
	    \begin{center}
		\begin{titre}
		\huge\duttfamily
		#1
		\end{titre}
		\end{center}
        \par\nobreak
        \vskip 20\p@
      \fi
	  
    \fi
  }
} 

\newtcolorbox{titreChapitre}{%
	enhanced,
	hbox,
	halign=center,
    valign=center,
	fontupper=\color{titrecolor},
	tikznode,
	left=8mm,
	right=8mm,
	top=5mm,
	bottom=6mm,
	boxrule=0.4pt,
	colframe=titrecolor,
	drop lifted shadow=titrecolor!20,
	after=\par\bigskip,
	interior style={
		draw=main,
		top color=main,
		bottom color=main
	},
	title = \thechapter,
	colframe = titrecolor,
	colback = titrecolor!10,
	colbacktitle = titrecolor!10,
	coltitle = titrecolor,
	fonttitle = \huge\ECFWebster,
	attach boxed title to top center = {
		yshift=-0.25mm-\tcboxedtitleheight/2,
		yshifttext=2mm-\tcboxedtitleheight/2
	},
	boxed title style = {
		boxrule = 0.5mm,
		frame code = {
			\path[tcb fill frame] (frame.center) circle(1.5em);
		},
		interior code = { 
			\path[tcb fill interior] (interior.center) circle(1.4em);
		},
		after=\par\vspace{5mm}
	}
}

\newtcolorbox{titre}{%
	enhanced,
	hbox,
	halign=center,
    valign=center,
	fontupper=\color{toc},
	tikznode,
	boxrule=0.4pt,
	colback=toc,
	colframe=toc,
	drop lifted shadow=toc!20,
	after=\par\bigskip,
	interior style={
		draw=toc,
		top color=toc!10,
		bottom color=toc!10
	}
}


\fancypagestyle{chapterstart}{%
	\fancyhf{}%
	\pagecolor{main}
  	\fancyfoot[RO,RE]{%
		\vskip0.1pt
		\begin{tikzpicture}
			\node[rounded corners = 3pt,fill = titrecolor,inner ysep = 5pt, inner xsep = 2ex,text = white] (numpage) {\thepage};
			\drawshadow{numpage}{titrecolor}
		\end{tikzpicture}
  	}
}

\newcounter{frontmatterchap}
\setcounter{frontmatterchap}{0}