% Copyright (c) 2026 Wang Yang (王阳) and contributors
% SPDX-License-Identifier: CC-BY-SA-4.0
% HUST names and institutional marks are excluded from this license grant.
% See LICENSE and NOTICE.md for details.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{beamerinnerthemehust}[2026/07/20 v0.7.1 HUST inner theme]

\RequirePackage{tikz}

\mode<presentation>

\providecommand{\tikzexternaldisable}{}
\providecommand{\tikzexternalenable}{}

\newcommand{\hust@covergraphic}[2][]{%
  \includegraphics[height=\paperheight,#1]{#2}}

\newcommand{\hust@drawinversebrand}{%
  \node[
    anchor=north west,
    xshift=\hust@margin,
    yshift=-7mm,
    inner sep=0pt
  ] at (current page.north west) {\hustwordmarkinverse[30mm]};
}

\newcommand{\hust@drawinversepagenumber}{%
  \node[
    anchor=south east,
    xshift=-\hust@margin,
    yshift=3mm,
    text=white,
    font=\usebeamerfont{footline}
  ] at (current page.south east) {%
    \bfseries\hust@displaypagenumberinverse};
}

% Cover -----------------------------------------------------------------------

\setbeamertemplate{title page}{%
  \begin{tikzpicture}[remember picture,overlay]
    \fill[HUSTPaper]
      (current page.south west) rectangle (current page.north east);

    % The cover alone uses a full-height identity field. A photograph may
    % replace its base layer, but the blue overlay and all text anchors remain
    % unchanged so image and no-image covers share one composition.
    \ifdefempty{\hust@coverimage}{%
      \fill[HUSTFrameBlue]
        (current page.south west)
        rectangle
        ([xshift=\hust@coverfieldwidth]current page.north west);
    }{%
      \begin{scope}
        \clip
          (current page.south west)
          rectangle
          ([xshift=\hust@coverfieldwidth]current page.north west);
        \node[
          anchor=center,
          xshift=30mm,
          inner sep=0pt
        ] at (current page.west) {%
          \ifdefempty{\hust@coverimageoptions}{%
            \includegraphics[height=\paperheight]{\hust@coverimage}%
          }{%
            \expandafter\hust@covergraphic
              \expandafter[\hust@coverimageoptions]{\hust@coverimage}%
          }};%
      \end{scope}
      \fill[HUSTFrameBlue,fill opacity=.88]
        (current page.south west)
        rectangle
        ([xshift=\hust@coverfieldwidth]current page.north west);
    }%

    % Compact inverse brand lockup inside the identity field.
    \node[
      anchor=north west,
      xshift=\hust@margin,
      yshift=-7mm,
      inner sep=0pt
    ] at (current page.north west) {\hustbrandmarkinverse[9.5mm]};
    \node[
      anchor=north west,
      xshift=25mm,
      yshift=-8.4mm,
      inner sep=0pt
    ] at (current page.north west) {\hustwordmarkinverse[30mm]};
    \node[
      anchor=north west,
      xshift=\hust@margin,
      yshift=-22.5mm,
      inner sep=0pt,
      text=white,
      font=\usebeamerfont{hust cover unit}
    ] at (current page.north west) {%
      \begin{minipage}[t]{44mm}
        \raggedright
        \ifstrempty{\hust@department}{\insertinstitute}{\hust@department}%
        \ifstrempty{\hust@departmenten}{}{%
          \\[.35mm]%
          {\usebeamerfont{hust cover unit english}\color{white!72}%
            \hust@departmenten}}%
        \par
      \end{minipage}%
    };

    % Editorial content field: one shared left anchor and an 80 mm measure.
    \node[
      anchor=north west,
      xshift=70mm,
      yshift=-18.5mm,
      text width=80mm,
      align=left,
      inner sep=0pt
    ] at (current page.north west) {%
      {\usebeamerfont{hust cover event}\color{HUSTFrameBlueSoft}%
        \ifstrempty{\hust@event}{学术报告}{\hust@event}%
        \ifstrempty{\hust@eventen}{}{%
          \hspace{1.4mm}{\color{HUSTFrameRule}/}\hspace{1.4mm}%
          {\color{HUSTMuted}\hust@eventen}}%
        \par}%
    };
    \node[
      anchor=north west,
      xshift=70mm,
      yshift=-26.5mm,
      text width=80mm,
      align=left,
      inner sep=0pt
    ] at (current page.north west) {%
      \raggedright
      {\usebeamerfont{title}\color{HUSTInk}\inserttitle\par}%
      \ifx\insertsubtitle\@empty\else
        \vspace{2mm}%
        {\usebeamerfont{subtitle}\color{HUSTMuted}%
          \hyphenpenalty=10000\exhyphenpenalty=10000%
          \insertsubtitle\par}%
      \fi
    };

    \node[
      anchor=south west,
      xshift=\hust@margin,
      yshift=11.5mm,
      inner sep=0pt,
      text=white,
      font=\usebeamerfont{author}
    ] at (current page.south west) {\insertauthor};
    \node[
      anchor=south east,
      xshift=50mm,
      yshift=11.5mm,
      inner sep=0pt,
      text=HUSTBluePale,
      font=\usebeamerfont{date}
    ] at (current page.south west) {\insertdate};
  \end{tikzpicture}%
}

% Outline ---------------------------------------------------------------------

\newenvironment{hustoutlinepage}[1][汇报结构]{%
  \setbeamercolor{hust outline number}{fg=HUSTFrameBlue}%
  \begin{tikzpicture}[remember picture,overlay]
    \fill[white] (current page.south west) rectangle (current page.north east);
    \node[
      anchor=north west,
      xshift=\hust@margin,
      yshift=-7mm,
      text=HUSTFrameBlueSoft,
      font=\usebeamerfont{hust page label}
    ] at (current page.north west) {OVERVIEW};
    \node[
      anchor=north west,
      xshift=\hust@margin,
      yshift=-12.5mm,
      text width=\hust@contentwidth,
      align=left,
      inner sep=0pt,
      text=HUSTInk,
      font=\usebeamerfont{section title}
    ] at (current page.north west) {#1};
    \hust@drawlightpagenumber
  \end{tikzpicture}%
  \par\vspace*{28mm}%
  \noindent
  \begin{minipage}[t]{\linewidth}
}{%
  \end{minipage}%
}

% Section and closing pages ----------------------------------------------------

\setbeamertemplate{section page}{%
  \begin{tikzpicture}[remember picture,overlay]
    \fill[HUSTFrameBlue]
      (current page.south west) rectangle (current page.north east);
    \hust@drawinversebrand
    \hust@drawinversepagenumber

    \node[
      anchor=north west,
      xshift=\hust@margin,
      yshift=-31mm,
      text=HUSTBluePale,
      font=\usebeamerfont{hust section label}
    ] at (current page.north west) {%
      章节\hspace{1.5mm}/\hspace{1.5mm}%
      \hust@twodigits{\insertsectionnumber}};

    \node[
      anchor=north west,
      xshift=\hust@margin,
      yshift=-38mm,
      text width=115mm,
      align=left,
      inner sep=0pt,
      text=white,
      font=\usebeamerfont{section title}
    ] (hust@sectiontitle) at (current page.north west) {\insertsectionhead};

    \ifstrempty{\hust@sectionnote}{}{%
      \node[
        anchor=north west,
        text width=100mm,
        align=left,
        inner sep=0pt,
        text=HUSTBluePale,
        font=\usebeamerfont{hust section note}
      ] at ([yshift=-8mm]hust@sectiontitle.south west) {\hust@sectionnote};
    }%
  \end{tikzpicture}%
  \global\let\hust@sectionnote\@empty
}

% Standout frames use the same inverse family as section pages. Drawing only
% on standout frames keeps normal background canvases user-customizable.
\setbeamertemplate{background}{%
  \ifbool{moloch@standout}{%
    \tikzexternaldisable
    \begin{tikzpicture}[remember picture,overlay]
      \fill[HUSTFrameBlue]
        (current page.south west) rectangle (current page.north east);
      \hust@drawinversebrand
      \hust@drawinversepagenumber
    \end{tikzpicture}%
    \tikzexternalenable
  }{}%
}

% Lists, contents, and surfaces ------------------------------------------------

\setbeamertemplate{itemize item}{%
  \raise0.1ex\hbox{%
    \usebeamercolor[fg]{itemize item}%
    \fontsize{9.5}{9.5}\selectfont\textbullet}}
\setbeamertemplate{itemize subitem}{%
  \raise0.1ex\hbox{%
    \usebeamercolor[fg]{itemize subitem}%
    \fontsize{7.5}{7.5}\selectfont\textbullet}}
\setbeamertemplate{itemize subsubitem}{%
  \raise0.1ex\hbox{%
    \usebeamercolor[fg]{itemize subsubitem}%
    \fontsize{6}{6}\selectfont\textbullet}}
\setbeamertemplate{enumerate item}{%
  \usebeamerfont*{enumerate item}%
  \usebeamercolor[fg]{enumerate item}%
  \ifnum\value{enumi}<10 0\fi\arabic{enumi}}
\setbeamertemplate{enumerate subitem}{%
  \usebeamerfont*{enumerate subitem}%
  \usebeamercolor[fg]{enumerate subitem}\alph{enumii}}
\setbeamertemplate{enumerate subsubitem}{%
  \usebeamerfont*{enumerate subsubitem}%
  \usebeamercolor[fg]{enumerate subsubitem}\roman{enumiii}}
\setbeamertemplate{description item}{%
  \usebeamerfont*{description item}%
  \usebeamercolor[fg]{description item}\insertdescriptionitem}

\setbeamertemplate{itemize/enumerate body begin}{%
  \setlength{\topsep}{0.5mm}%
  \setlength{\partopsep}{0pt}%
  \setlength{\itemsep}{1.25mm}%
  \setlength{\parsep}{0pt}%
}
\setbeamertemplate{itemize/enumerate subbody begin}{%
  \setlength{\topsep}{0.6mm}%
  \setlength{\partopsep}{0pt}%
  \setlength{\itemsep}{0.65mm}%
  \setlength{\parsep}{0pt}%
}
\setbeamertemplate{itemize/enumerate subsubbody begin}{%
  \setlength{\topsep}{0.4mm}%
  \setlength{\partopsep}{0pt}%
  \setlength{\itemsep}{0.4mm}%
  \setlength{\parsep}{0pt}%
}

\setlength{\leftmargini}{5.5mm}
\setlength{\leftmarginii}{4.2mm}
\setlength{\leftmarginiii}{3.6mm}

\setbeamertemplate{section in toc}{%
  \begin{beamercolorbox}[wd=\linewidth,sep=1.5mm]{section in toc}%
    \makebox[11mm][l]{%
      {\color{HUSTFrameBlue}\bfseries
        \hust@twodigits{\inserttocsectionnumber}}}%
    \inserttocsection
  \end{beamercolorbox}%
  \vspace{1.2mm}%
}

% Split-band content boxes: a narrow semantic title band sits directly on
% a pale reading surface. Both families stay square, flat, and shadow-free.
\newcommand{\hust@blockbegin}[3]{%
  \par
  \setbeamercolor{itemize item}{fg=#3}%
  \setbeamercolor{enumerate item}{fg=#3}%
  \setbeamercolor{itemize subitem}{fg=HUSTMuted}%
  \setbeamercolor{enumerate subitem}{fg=HUSTMuted}%
  \ifdefempty{\insertblocktitle}{}{%
    \begin{beamercolorbox}[
      wd=\linewidth,
      sep=0.9mm,
      leftskip=0.5mm,
      rightskip=0.5mm
    ]{#1}%
      \usebeamerfont*{block title}%
      \usebeamercolor[fg]{#1}%
      \insertblocktitle\par
    \end{beamercolorbox}%
    \nointerlineskip\vskip-0.3pt%
  }%
  \begin{beamercolorbox}[wd=\linewidth,sep=2.4mm]{#2}%
    \usebeamerfont{block body}\usebeamercolor[fg]{#2}%
}
\newcommand{\hust@blockend}{%
  \end{beamercolorbox}%
  \vskip0.8ex%
}

\setbeamertemplate{block begin}{%
  \hust@blockbegin{block title}{block body}{HUSTBoxGold}}
\setbeamertemplate{block end}{\hust@blockend}
\setbeamertemplate{block alerted begin}{%
  \hust@blockbegin{%
    block title alerted}{block body alerted}{HUSTAlertDeep}}
\setbeamertemplate{block alerted end}{\hust@blockend}
\setbeamertemplate{block example begin}{%
  \hust@blockbegin{%
    block title example}{block body example}{HUSTBoxGold}}
\setbeamertemplate{block example end}{\hust@blockend}

\mode<all>
\endinput
