\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{beamerouterthemeDurham}[2026/02/04 v1.0 Durham Beamer outer theme]

\RequirePackage{xcolor}
\RequirePackage{tikz}
\usetikzlibrary{calc}
\RequirePackage{xstring}
\RequirePackage{etoolbox}
\RequirePackage{appendixnumberbeamer}

\setbeamercolor{headline}{fg=white,bg=Dpurple}
\setbeamercolor{headline second}{fg=black,bg=DgrayBar}

\setbeamercolor{section in head/foot}{fg=white,bg=Dpurple}
\setbeamercolor{section in head/foot shaded}{fg=DpurpleInactive,bg=Dpurple}

\setbeamerfont{section in head/foot}{size=\fontsize{6.5pt}{7.5pt}\selectfont}
\setbeamerfont{subsection in head/foot}{size=\fontsize{6pt}{7pt}\selectfont}

\makeatletter
\setbeamertemplate{section in head/foot}{%
  \raisebox{0.35ex}{\tikz[baseline=-0.2ex]\fill[fg] (0,0) circle (0.55ex);}%
  \hspace{0.6ex}%
  \usebeamerfont{section in head/foot}%
  \DurhamTrunc{\insertsectionhead}{\DurhamMaxCharsHeadline}%
  \hspace{1.2em}%
}
\setbeamertemplate{section in head/foot shaded}{%
  \raisebox{0.35ex}{\tikz[baseline=-0.2ex]\draw[fg, line width=0.18ex] (0,0) circle (0.55ex);}%
  \hspace{0.6ex}%
  \usebeamerfont{section in head/foot}%
  \DurhamTrunc{\insertsectionhead}{\DurhamMaxCharsHeadline}%
  \hspace{1.2em}%
}

\setbeamertemplate{headline}{%
  \begin{beamercolorbox}[
    wd=\paperwidth,
    ht=3ex,dp=2.2ex,
    leftskip=1.2em,rightskip=1.2em
  ]{headline}
    \insertsectionnavigationhorizontal{\dimexpr\paperwidth-2.4em\relax}{}{}%
  \end{beamercolorbox}%
  \begingroup
    \edef\Durham@subsec{\insertsubsectionhead}%
    \ifx\Durham@subsec\@empty
    \else
      \begin{beamercolorbox}[
        wd=\paperwidth,
        ht=2.8ex,dp=1.0ex,
        leftskip=1.2em,rightskip=1.2em
      ]{headline second}
        \usebeamerfont{subsection in head/foot}\hspace*{1.4em}%
        \DurhamTrunc{\insertsubsectionhead}{\DurhamMaxCharsSubHeadline}%
      \end{beamercolorbox}%
    \fi
  \endgroup
}
\makeatother

\newcommand{\DurhamProgressBar}{%
  \begin{tikzpicture}[remember picture,overlay]
    \def\barx{0.25cm}
    \def\barystart{0.9cm}
    \def\baryend{.85\paperheight}
    \def\barwidth{0.07cm}
    \def\circleradius{0.08cm}

    \pgfmathsetmacro{\TotalFrames}{max(\inserttotalframenumber,1)}
    \pgfmathsetmacro{\barheight}{\baryend - \barystart}
    \pgfmathsetmacro{\progress}{min(\insertframenumber/\TotalFrames,1)*\barheight}

    \fill[gray!20,rounded corners=1.2pt]
      ($(current page.south west)+(\barx,\barystart)$) rectangle
      ($(current page.south west)+(\barx+\barwidth,\baryend)$);

    \fill[Dpurple,rounded corners=1.2pt]
      ($(current page.south west)+(\barx,\baryend-\progress)$) rectangle
      ($(current page.south west)+(\barx+\barwidth,\baryend)$);

    \draw[Dpurple,line width=0.8pt,fill=white]
      ($(current page.south west)+(\barx+0.5*\barwidth,\baryend-\progress)$)
      circle (\circleradius);
  \end{tikzpicture}%
}

\setbeamercolor{footline}{fg=white,bg=Dpurple}
\setbeamerfont{footline}{size=\fontsize{6pt}{7pt}\selectfont}

\setbeamertemplate{footline}{%
  \leavevmode%
  \begin{beamercolorbox}[
    wd=\paperwidth,
    ht=2.5ex,
    dp=1.1ex,
    leftskip=1.2em,
    rightskip=1.2em
  ]{footline}%
    \usebeamerfont{footline}%
    \raisebox{0.25ex}{\insertshorttitle}%
    \hfill%
    \raisebox{0.25ex}{\insertshortauthor}%
  \end{beamercolorbox}%
  \DurhamProgressBar%
}

