\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{miextras}[2026/03/13 MIStatlE book and lecture note style]

% ============================================================
%  Engine check
% ============================================================
\RequirePackage{iftex}
\ifPDFTeX
  \PackageError{miextras}{Please compile with XeLaTeX or LuaLaTeX}{This package relies on fontspec and CJK font support.}
\fi

% ============================================================
%  Core packages
% ============================================================
\RequirePackage[fontset=fandol]{ctex}
\RequirePackage{fontspec}
\RequirePackage{geometry}
\RequirePackage{xcolor}
\RequirePackage{hyperref}
\RequirePackage{titlesec}
\RequirePackage{fancyhdr}
\RequirePackage{tikz}
\RequirePackage{eso-pic}
\usetikzlibrary{calc,positioning}
\RequirePackage[most]{tcolorbox}
\RequirePackage{enumitem}
\RequirePackage{mathtools}
\RequirePackage{amsmath,amssymb,bm,amsthm}

% ============================================================
%  Page geometry and fonts
% ============================================================
\geometry{
  paperwidth=6in,
  paperheight=9in,
  inner=0.92in,
  outer=0.78in,
  top=0.92in,
  bottom=0.95in,
  headsep=0.24in,
  footskip=0.42in,
  bindingoffset=0.08in
}

\IfFontExistsTF{Times New Roman}{\setmainfont{Times New Roman}}{\setmainfont{TeX Gyre Termes}}
\IfFontExistsTF{Helvetica Neue}{\setsansfont{Helvetica Neue}}{\setsansfont{TeX Gyre Heros}}
\IfFontExistsTF{JetBrains Mono}{\setmonofont{JetBrains Mono}}{\IfFontExistsTF{Menlo}{\setmonofont{Menlo}}{\setmonofont{Courier New}}}
\IfFontExistsTF{Noto Serif CJK SC}{\setCJKmainfont{Noto Serif CJK SC}}{}
\IfFontExistsTF{Noto Sans CJK SC}{\setCJKsansfont{Noto Sans CJK SC}}{}

\linespread{1.24}
\setlength{\parindent}{1.6em}
\setlength{\parskip}{0pt}
\pagecolor{white}
\color{black}

% ============================================================
%  Palette
% ============================================================
\definecolor{brand}{HTML}{1A9D8F}
\definecolor{brandD}{HTML}{2A7F6F}
\definecolor{keybg}{HTML}{E8F4F1}
\definecolor{keydark}{HTML}{2F5E58}
\definecolor{secblue}{HTML}{2B44C6}
\definecolor{accent}{HTML}{F97352}
\definecolor{accentD}{HTML}{C44536}
\definecolor{plum}{HTML}{6C5CE7}
\definecolor{plumBg}{HTML}{F2EEFF}
\definecolor{soft}{HTML}{F6FBFA}
\definecolor{ink}{HTML}{1A1A1A}

\hypersetup{
  colorlinks=true,
  linkcolor=brandD,
  citecolor=brandD,
  urlcolor=secblue,
  pdfauthor={MIStatlE},
  pdftitle={MIStatlE Book Template}
}

% ============================================================
%  Utility macros
% ============================================================
\newcommand{\padzero}[1]{\ifnum#1<10 0\fi\number#1}
\newcommand{\BrandYMD}{{\the\year·\padzero{\the\month}·\padzero{\the\day}}}
\newcommand{\E}{\mathbb{E}}
\DeclareMathOperator{\Var}{Var}
\DeclareMathOperator{\Cov}{Cov}
\DeclareMathOperator{\diag}{diag}
\DeclarePairedDelimiter{\abs}{\lvert}{\rvert}
\DeclarePairedDelimiter{\norm}{\lVert}{\rVert}
\DeclarePairedDelimiter{\ip}{\langle}{\rangle}
\numberwithin{equation}{chapter}

% ============================================================
%  Left bands
% ============================================================
\newif\ifMIleftbands
\MIleftbandstrue
\newcommand{\EnableLeftBands}{\MIleftbandstrue}
\newcommand{\DisableLeftBands}{\MIleftbandsfalse}
\newcommand{\LeftBandA}{0.12in}
\newcommand{\LeftBandB}{0.28in}
\newcommand{\PutLeftBands}{%
  \ifMIleftbands
    \begin{tikzpicture}[remember picture, overlay]
      \fill[keydark] ([xshift=0cm]current page.north west)
                     rectangle ([xshift=\LeftBandA]current page.south west);
      \fill[keybg]   ([xshift=\LeftBandA]current page.north west)
                     rectangle ([xshift=\LeftBandB]current page.south west);
    \end{tikzpicture}%
  \fi
}
\makeatletter
\@ifundefined{AddToHook}{
  \RequirePackage{everypage}
  \AddEverypageHook{\PutLeftBands}
}{
  \AddToHook{shipout/background}{\PutLeftBands}
}
\makeatother

% ============================================================
%  Header / footer system
% ============================================================
\newcommand{\seriesnum}{I}
\newcommand{\BookShortTitle}{Lecture Notes}
\newcommand{\BookFooterLabel}{@MIStatlE}

\setlength{\headheight}{14pt}
\renewcommand{\chaptermark}[1]{\markboth{第\thechapter 章\ #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}

\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0.4pt}
\fancyhead[LE]{\small\sffamily\textcolor{keydark}{\nouppercase{\leftmark}}}
\fancyhead[RO]{\small\sffamily\textcolor{keydark}{\nouppercase{\rightmark}}}
\fancyfoot[LE,RO]{\textcolor{brandD}{\thepage}}
\fancyfoot[LO,RE]{\scriptsize\sffamily\textcolor{brand}{\BookFooterLabel\ |\ \BookShortTitle\ \seriesnum}}

\fancypagestyle{coverstyle}{
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
  \renewcommand{\footrulewidth}{0pt}
}

\fancypagestyle{plain}{
  \fancyhf{}
  \renewcommand{\headrulewidth}{0.4pt}
  \fancyhead[LE]{\small\sffamily\textcolor{keydark}{\nouppercase{\leftmark}}}
  \fancyhead[RO]{\small\sffamily\textcolor{keydark}{\nouppercase{\rightmark}}}
  \fancyfoot[LE,RO]{\textcolor{brandD}{\thepage}}
  \fancyfoot[LO,RE]{\scriptsize\sffamily\textcolor{brand}{\BookFooterLabel\ |\ \BookShortTitle\ \seriesnum}}
}

\makeatletter
\newcommand{\TOCwithoutHeadFoot}{%
  \begingroup
    \let\saved@thispagestyle\thispagestyle
    \def\thispagestyle##1{\saved@thispagestyle{empty}}%
    \pagestyle{empty}%
    \tableofcontents
    \clearpage
  \endgroup
  \pagestyle{fancy}%
}
\makeatother

% ============================================================
%  Headings
% ============================================================
\titleformat{\part}[display]
  {\filcenter\bfseries\color{keydark}}
  {\Large\sffamily Part \thepart}{0.8em}{\Huge}
  [\vspace{0.8em}{\color{brand}\rule{7cm}{1.1pt}}]
\titlespacing*{\part}{0pt}{\fill}{\fill}

\titleformat{\chapter}[display]
  {\bfseries\color{keydark}}
  {\Large 第\thechapter 章}{0.45em}{\Huge}
  [\vspace{0.45em}{\color{brandD!45}\rule{0.72\linewidth}{0.8pt}}]
\titlespacing*{\chapter}{0pt}{1.1em}{0.8em}

\titleformat{\section}
  {\Large\bfseries\color{keydark}}
  {\thesection}{0.55em}{}
\titlespacing*{\section}{0pt}{0.9em}{0.5em}

\titleformat{\subsection}
  {\large\bfseries\color{brand}}
  {\thesubsection}{0.55em}{}
\titlespacing*{\subsection}{0pt}{0.8em}{0.35em}

% ============================================================
%  Lists
% ============================================================
\setlist[itemize]{leftmargin=1.4em,itemsep=0.2em,topsep=0.45em}
\setlist[enumerate]{leftmargin=1.6em,itemsep=0.2em,topsep=0.45em}

% ============================================================
%  Box system
% ============================================================
\tcbset{
  mi-elegant/.style={
    enhanced,
    breakable,
    colback=white,
    colframe=#1,
    boxrule=0.6pt,
    arc=3pt,
    left=10pt,right=10pt,top=12pt,bottom=10pt,
    before skip=10pt,after skip=10pt,
    borderline west={3pt}{0pt}{#1}
  },
  mi-notion/.style={
    enhanced,
    breakable,
    frame hidden,
    colback=#1!7,
    borderline west={3pt}{0pt}{#1},
    left=9pt,right=9pt,top=8pt,bottom=8pt,
    before skip=8pt,after skip=8pt,
    arc=2pt
  }
}

\newtcolorbox{KeyBox}{
  mi-elegant=brandD,
  title=\textbf{\color{white}Key Idea},
  colbacktitle=brandD,
  coltitle=white,
  fonttitle=\bfseries
}

\newtcolorbox{Takeaway}{
  mi-notion=accentD,
  title=\textbf{Takeaway},
  fonttitle=\bfseries,
  coltitle=accentD
}

\newtcolorbox{SideBar}{
  mi-notion=brand,
  title=\textbf{Sidebar},
  fonttitle=\bfseries,
  coltitle=brandD
}

\newtcolorbox{Example}[1][]{
  mi-elegant=plum,
  title=\textbf{\color{white}Example},
  colbacktitle=plum,
  coltitle=white,
  fonttitle=\bfseries,
  #1
}

% ============================================================
%  Theorem environments with boxed skin
% ============================================================
\theoremstyle{plain}
\newtheorem{theorem}{定理}[chapter]
\newtheorem{lemma}[theorem]{引理}
\newtheorem{proposition}[theorem]{命题}
\newtheorem{corollary}[theorem]{推论}

\theoremstyle{definition}
\newtheorem{definition}[theorem]{定义}

\theoremstyle{remark}
\newtheorem{remark}[theorem]{注}

\tcolorboxenvironment{theorem}{
  mi-elegant=secblue
}
\tcolorboxenvironment{lemma}{
  mi-elegant=secblue
}
\tcolorboxenvironment{proposition}{
  mi-elegant=secblue
}
\tcolorboxenvironment{corollary}{
  mi-elegant=secblue
}
\tcolorboxenvironment{definition}{
  mi-notion=brandD
}
\tcolorboxenvironment{remark}{
  mi-notion=accentD
}

% ============================================================
%  Cover and chapter epigraph
% ============================================================
\newcommand{\MakeBookCoverUltraSimple}[3]{%
  \thispagestyle{coverstyle}%
  \vspace*{\fill}
  \begin{center}
    {\small\sffamily\textcolor{brandD}{MIStatlE Monograph Series}\par}
    \vspace{0.8em}
    {\bfseries\fontsize{30}{36}\selectfont\color{keydark} #1\par}
    \vspace{0.48em}
    {\bfseries\fontsize{15}{21}\selectfont\color{ink!88} #2\par}
    \if\relax\detokenize{#3}\relax\else
      \vspace{1.2em}
      {\small\sffamily\color{brandD}\fboxsep=6pt\colorbox{white}{\textbf{\textcolor{brandD}{#3}}}\par}
    \fi
    \vspace{1.5em}
    {\small\color{keydark!72}\BrandYMD}
  \end{center}
  \vspace*{\fill}
  \clearpage
}

\newcommand{\ChapterEpigraph}[2][]{%
  \par\vspace*{0.55em}
  \begin{flushright}
    \begin{minipage}{0.78\linewidth}\raggedleft
      {\itshape\color{ink!82}#2\par}
      \if\relax\detokenize{#1}\relax\else
        \vspace{0.35em}{\color{ink!65}--- #1}\par
      \fi
    \end{minipage}
  \end{flushright}
  \vspace{0.6em}
}
