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

\newcommand{\DurhamMaxCharsOutline}{35}
\newcommand{\DurhamMaxCharsHeadline}{20}
\newcommand{\DurhamMaxCharsSubOutline}{35}
\newcommand{\DurhamMaxCharsSubHeadline}{55}

\makeatletter
\newcommand{\DurhamTrunc}[2]{%
  \begingroup
    \let\hyperlink\@secondoftwo
    \let\beamer@link\@secondoftwo
    \let\beamer@linkparent\@secondoftwo
    \edef\Durham@tmp{#1}%
    \StrLen{\Durham@tmp}[\Durham@len]%
    \ifnum\Durham@len>#2\relax
      \StrLeft{\Durham@tmp}{#2}[\Durham@out]%
      \Durham@out...\relax
    \else
      \Durham@tmp
    \fi
  \endgroup
}
\makeatother

\newcommand{\DurhamOutline}{%
  \begingroup
  \setbeamertemplate{background canvas}{%
    \begin{tikzpicture}[remember picture,overlay]
      \fill[Dpurple]
        (current page.south west) rectangle
        ($(current page.north west) + (0.22\paperwidth,0)$);
      \node[
        anchor=center,
        text=white,
        align=center
      ] at ($(current page.west) + (0.11\paperwidth,3.5cm)$) {%
        {\usebeamerfont{title}\bfseries Outline}%
      };
    \end{tikzpicture}%
  }%
  \begin{frame}[plain,noframenumbering]
    \begin{tikzpicture}[remember picture,overlay]
      \node[
        anchor=center,
        align=left,
        text width=0.72\paperwidth
      ] at ($(current page.center) + (0.15\paperwidth,0)$) {%
        \begingroup
          \setlength{\parskip}{0pt}%
          \tableofcontents
        \endgroup
      };
    \end{tikzpicture}
  \end{frame}
  \endgroup
}

\makeatletter
\pretocmd\beamer@sectionintoc{\vspace{0.6em}}{}{}
\makeatother

\makeatletter
\setbeamertemplate{section in toc}{%
  \leavevmode\noindent
  \begingroup\bfseries
    \inserttocsectionnumber.\hspace{0.6em}%
    \let\hyperlink\@secondoftwo
    \let\beamer@link\@secondoftwo
    \let\beamer@linkparent\@secondoftwo
    \DurhamTrunc{\inserttocsection}{\DurhamMaxCharsOutline}%
  \endgroup
  \par\nointerlineskip
}
\setbeamertemplate{subsection in toc}{%
  \leavevmode\noindent\hspace*{1.5em}%
  \inserttocsectionnumber.\inserttocsubsectionnumber\hspace{0.6em}%
  \begingroup
    \let\hyperlink\@secondoftwo
    \let\beamer@link\@secondoftwo
    \let\beamer@linkparent\@secondoftwo
    \DurhamTrunc{\inserttocsubsection}{\DurhamMaxCharsSubOutline}%
  \endgroup
  \par
}
\makeatother

\setbeamerfont{title}{size=\LARGE,series=\bfseries}
\setbeamerfont{subtitle}{size=\large}
\setbeamerfont{author}{size=\large,series=\bfseries}
\setbeamerfont{institute}{size=\normalsize}
\setbeamerfont{date}{size=\small,series=\bfseries}

\newcommand{\DurhamTitleRule}{%
  \begin{tikzpicture}
    \draw[Dpurple, line width=1.5pt, line cap=round] (0,0) -- (0.9\linewidth,0);
  \end{tikzpicture}%
}

\setbeamertemplate{title page}{%
  \begin{tikzpicture}[remember picture,overlay]
    \fill[white] (current page.south west) rectangle (current page.north east);
    \fill[Dpurple]
      ($(current page.south east) + (0,0)$) --
      ($(current page.south east) + (-4.5cm,0)$) --
      ($(current page.south east) + (0,4.5cm)$) -- cycle;
  \end{tikzpicture}

  \begin{tikzpicture}[remember picture,overlay]
    \node[
      anchor=north west,
      align=left,
      text width=0.80\paperwidth
    ] at ($(current page.north west) + (1.2cm,-1.5cm)$) {%
      {\usebeamerfont{title}\color{Dpurple}\inserttitle\par}
      \vspace{0.5em}
      {\usebeamerfont{subtitle}\color{black}\insertsubtitle\par}

      \vspace{1.0em}
      \DurhamTitleRule

      \vspace{2.0em}

      {%
        \begingroup
          \makeatletter
          \def\and{\hspace{1.8em}}%
          \def\beamer@and{\hspace{1.8em}}%
          \def\hfill{\hskip0pt}%
          \parbox[t]{\linewidth}{%
            \raggedright
            \usebeamerfont{author}\color{black}\insertauthor
          }%
          \makeatother
        \endgroup
        \par
      }

      \vspace{0.9em}
      {\usebeamerfont{institute}\color{black}\insertinstitute\par}
    };
  \end{tikzpicture}

  \begin{tikzpicture}[remember picture,overlay]
    \node[
      anchor=south east,
      text=white,
      align=right
    ] at ($(current page.south east) + (-0.2cm,0.3cm)$) {%
      {\usebeamerfont{date}\insertdate}%
    };
  \end{tikzpicture}
}

\makeatletter
\renewcommand{\maketitle}{%
  \begin{frame}[plain,noframenumbering]
    \titlepage
  \end{frame}
}
\makeatother

\setbeamerfont{section title}{series=\bfseries}

\newcommand{\DurhamSectionPage}{%
  \begin{frame}[plain,noframenumbering]
    \centering
    \vfill
    {\usebeamerfont{section title}\color{Dpurple}\LARGE\DurhamTrunc{\insertsectionhead}{\DurhamMaxCharsOutline}\par}
    \vfill
    \begin{tikzpicture}[remember picture,overlay]
      \pgfmathsetlengthmacro{\bary}{4cm}
      \pgfmathsetlengthmacro{\barxstart}{0.10\paperwidth}
      \pgfmathsetlengthmacro{\barxend}{0.90\paperwidth}
      \pgfmathsetlengthmacro{\barheight}{0.08cm}
      \pgfmathsetlengthmacro{\barwidth}{\barxend-\barxstart}
      \pgfmathsetmacro{\totalframes}{max(\inserttotalframenumber,1)}
      \pgfmathsetmacro{\ratio}{min((\value{framenumber})/\totalframes,1)}
      \pgfmathsetlengthmacro{\progress}{\ratio*\barwidth}
      \fill[gray!20,rounded corners=1.2pt]
        ($(current page.south west)+(\barxstart,\bary)$) rectangle
        ($(current page.south west)+(\barxend,\bary+\barheight)$);
      \fill[Dpurple,rounded corners=1.2pt]
        ($(current page.south west)+(\barxstart,\bary)$) rectangle
        ($(current page.south west)+(\barxstart+\progress,\bary+\barheight)$);
    \end{tikzpicture}
  \end{frame}%
}
\AtBeginSection[]{\DurhamSectionPage}

\setbeamerfont{frametitle}{size=\large\bfseries}
\setbeamertemplate{frametitle}{%
  \vspace{0.45em}\noindent{\usebeamerfont{frametitle}\insertframetitle\par}\vspace{0.25em}%
}

\newcommand{\Durhamthankyou}{%
  \begin{frame}[plain,noframenumbering]
    \begin{tikzpicture}[remember picture,overlay]
      \fill[Dpurple] (current page.south west) rectangle (current page.north east);
      \fill[white]
        ($(current page.south east) + (0,0)$) --
        ($(current page.south east) + (-4.5cm,0)$) --
        ($(current page.south east) + (0,4.5cm)$) -- cycle;
    \end{tikzpicture}
    \begin{tikzpicture}[remember picture,overlay]
      \node[
        anchor=center,
        align=center,
        text=white
      ] at (current page.center) {%
        {\usebeamerfont{title}\bfseries Thank you!}%
      };
    \end{tikzpicture}
  \end{frame}
}

\usepackage[authoryear]{natbib}
\bibliographystyle{apalike}

% -------------------------------------------------
% Plain references frame (supports allowframebreaks)
% -------------------------------------------------
\newenvironment{DurhamReferences}
{%
  \begingroup
  % Disable headline and footline
  \setbeamertemplate{headline}{}
  \setbeamertemplate{footline}{}
  \setbeamertemplate{navigation symbols}{}
  \begin{frame}[allowframebreaks,noframenumbering]{References}
}
{%
  \end{frame}
  \endgroup
}

