% beamethemeCelestia.sty
%
% copyright (C) 2025 Razik Ikhlef
% razik.ikhlef@csilyon.fr
%
% The newest version of this beamer theme should always be available
% from the following web page: https://apps.edulatex.xyz/celestia/

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{beamerthemeCelestia}[2025/11/02 v1.1.1]

\RequirePackage{iftex}
\RequirePackage{etoolbox}
\RequirePackage{kvoptions}
\RequirePackage{xstring}

\RequirePackage{silence}
\WarningFilter{latexfont}{Font shape}
\renewcommand{\@font@warning}[1]{}

%------------------
% Theme options
%------------------
\SetupKeyvalOptions{
    family=celestia,
    prefix=celestia@
}

% Boolean declarations using kvoptions
\DeclareBoolOption{allserif}
\DeclareBoolOption{unicolor}
\DeclareBoolOption{standout}
\DeclareBoolOption{compacttoc}
\DeclareBoolOption{twocolumntoc}
\DeclareBoolOption{decorative}
\DeclareBoolOption{nodecorative}
\DeclareStringOption{footerstyle}
\DeclareStringOption[center]{titlealign}
\DeclareStringOption{titlebackground}
\DeclareStringOption[none]{decorationstyle}
\DeclareStringOption[0.2]{decorationopacity}
\DeclareStringOption{scheme}
\DeclareBoolOption[true]{codebox}
\DeclareBoolOption{nocodebox}
\DeclareBoolOption{nocodeframe}
\DeclareBoolOption{soberblock}
\DeclareBoolOption{softblock}
\DeclareBoolOption{shadedblock}
\DeclareStringOption[0.35]{shadedblockopacity}
\DeclareStringOption[0.18]{soberblockopacity}
\DeclareStringOption[0.18]{softblockopacity}
\DeclareBoolOption{nobackblock}
\DeclareBoolOption{shownavigation}
\DeclareBoolOption{boldurl}
\DeclareBoolOption[true]{sectionpage}
\DeclareBoolOption[false]{sectionnumber}
\DeclareStringOption[listings]{codehandler}

% String options with defaults
\DeclareStringOption[sffamily]{headstyle}  % rmfamily, sffamily
\DeclareStringOption[normal]{headshape}    % sc, it, normal
\DeclareStringOption[bfseries]{headweight} % bfseries, mdseries
\DeclareStringOption[Large]{titlesize} % bfseries, mdseries
\DeclareStringOption[large]{frametitlesize} % bfseries, mdseries
\DeclareStringOption[LARGE]{sectiontitlesize} % bfseries, mdseries
\DeclareStringOption[Large]{subtitlesize} % bfseries, mdseries

\DeclareStringOption{maincolor}
\DeclareStringOption{accentcolor}
\DeclareStringOption{backgroundcolor}
\DeclareStringOption{codebackgroundcolor}
\DeclareStringOption{blockcolor}
\DeclareStringOption{examplecolor}
\DeclareStringOption{alertcolor}
\DeclareStringOption{custompalette}

\DeclareStringOption[2em]{margin}
\DeclareStringOption[elegant]{frametitle}  % centered, plain, plain centered, shaded

\DeclareStringOption[Literata]{mainface}
\DeclareStringOption{mainfaceoptions}
\DeclareStringOption[Inter]{sansface}
\DeclareStringOption{sansfaceoptions}
\DeclareStringOption[Roboto Mono]{monoface}
\DeclareStringOption{monofaceoptions}

\DeclareStringOption[english]{language}

% Color Palettes
% {main} {accent} {block} {example} {alert} {background}
\newcommand{\celestia@set@palette}[6]{%
    \def\celestia@maincolor{#1}%
    \def\celestia@accentcolor{#2}%
    \def\celestia@blockcolor{#3}%
    \def\celestia@examplecolor{#4}%
    \def\celestia@alertcolor{#5}%
    \def\celestia@backgroundcolor{#6}%
}

\newcommand{\celestia@palette@amber}{%
  \celestia@set@palette{D97706}{F59E0B}{B8863D}{9F7D3A}{EA580C}{FFFBF5}
}
\newcommand{\celestia@palette@bnw}{%
  \celestia@set@palette{000000}{000000}{000000}{000000}{000000}{FFFFFF}
}
\newcommand{\celestia@palette@copper}{%
  \celestia@set@palette{B7410E}{D4682A}{9F5A2D}{8B4A1A}{CD7F32}{FBF8F5}
}
\newcommand{\celestia@palette@coral}{%
  \celestia@set@palette{264653}{E76F51}{2A9D8F}{E9C46A}{F4A261}{F1FAEE}
}
\newcommand{\celestia@palette@crimson}{%
  \celestia@set@palette{9A1B2C}{D4A24E}{616161}{507B60}{B54A3D}{FCF8F7}
}
\newcommand{\celestia@palette@earth}{%
  \celestia@set@palette{3E2723}{D4A574}{6D4C41}{7CB342}{E64A19}{FAF6F1}
}
\newcommand{\celestia@palette@emerald}{%
  \celestia@set@palette{0A7A5C}{1A9F7A}{2D8B6F}{3D6B52}{7B4A8D}{F7FBF9}
}
\newcommand{\celestia@palette@forest}{%
  \celestia@set@palette{2D5C3D}{4A6B3D}{3D5C4A}{3D6B3D}{6D4C41}{F7FBF7}
}
\newcommand{\celestia@palette@frost}{%
  \celestia@set@palette{007A85}{004FC0}{5A6B8C}{00845F}{D95C2B}{F7FBFC}
}
\newcommand{\celestia@palette@lavender}{%
  \celestia@set@palette{6B5B95}{8B7AB8}{9A8AAF}{00796B}{8E44AD}{F9F8FC}
}
\newcommand{\celestia@palette@midnight}{%
  \celestia@set@palette{0A1F3C}{B38600}{4A6572}{4A7C59}{A93226}{F8FAFC}
}
\newcommand{\celestia@palette@modern}{%
  \celestia@set@palette{2C3E50}{E74C3C}{3498DB}{27AE60}{E67E22}{ECF0F1}
}
\newcommand{\celestia@palette@neon}{%
  \celestia@set@palette{1A1A2E}{FF2E63}{0F3460}{16C172}{FF6B35}{FFFFFF}
}
\newcommand{\celestia@palette@navy}{%
  \celestia@set@palette{194B7D}{236955}{5C6B73}{236955}{912D2D}{F7F9FB}
}
\newcommand{\celestia@palette@nordic}{%
  \celestia@set@palette{2E3440}{88C0D0}{5E81AC}{A3BE8C}{BF616A}{ECEFF4}
}
\newcommand{\celestia@palette@ocean}{%
  \celestia@set@palette{0D5F7A}{1A8FA6}{4AA699}{3D7A8F}{6C4F70}{F7FAFC}
}
\newcommand{\celestia@palette@pastel}{%
  \celestia@set@palette{5A6F7D}{B4869F}{7A9B76}{9B8AAB}{C77B6A}{F2EFE9}
%
}
\newcommand{\celestia@palette@plum}{%
  \celestia@set@palette{5D3A5F}{6B8E23}{8B5A7D}{6F4A6B}{C75B12}{FBF7FB}
}
\newcommand{\celestia@palette@retro}{%
  \celestia@set@palette{BF360C}{00695C}{A1887F}{00695C}{BF360C}{FDFBF8}
}
\newcommand{\celestia@palette@royal}{%
  \celestia@set@palette{00539C}{D9455F}{6D6D6D}{59886B}{C0392B}{F7F9FB}
}
\newcommand{\celestia@palette@sakura}{%
  \celestia@set@palette{C2185B}{7B1FA2}{512DA8}{00796B}{C2185B}{FFF8FB}
}
\newcommand{\celestia@palette@sepia}{%
  \celestia@set@palette{5D4A3A}{546E7A}{9F7A52}{7D5A3A}{8F3E3E}{FBF9F5}
}
\newcommand{\celestia@palette@slate}{%
  \celestia@set@palette{3D4A5C}{A9714B}{5B6B7D}{6B7A8B}{8F3E3E}{F8F9FA}
}
\newcommand{\celestia@palette@solar}{%
  \celestia@set@palette{F57C00}{FFA000}{1976D2}{388E3C}{D32F2F}{FFFBF0}
}
\newcommand{\celestia@palette@spring}{%
  \celestia@set@palette{D65B7C}{4A9B89}{6B8FCC}{CC8B5C}{D65B7C}{FFF9FB}
}
\newcommand{\celestia@palette@sunset}{%
  \celestia@set@palette{D84A3D}{E67D3D}{795548}{B85B3D}{D84A3D}{FFF9F5}
}
\newcommand{\celestia@palette@terra}{%
  \celestia@set@palette{B85A3D}{A67A52}{9F7D5C}{C77A52}{8F5A3D}{FFF8F3}
}
\newcommand{\celestia@palette@twilight}{%
  \celestia@set@palette{1E3A5F}{F39C12}{34495E}{16A085}{E74C3C}{F8F9FA}
}

\def\celestia@parse@custompalette#1{%
  \celestia@parse@palette@aux#1,,,,,\relax
}

\def\celestia@parse@palette@aux#1,#2,#3,#4,#5,#6,#7\relax{%
  \def\celestia@maincolor{#1}%
  \def\celestia@accentcolor{#2}%
  \def\celestia@blockcolor{#3}%
  \def\celestia@examplecolor{#4}%
  \def\celestia@alertcolor{#5}%
  \def\celestia@backgroundcolor{#6}%
}

\define@key{celestia}{palette}[midnight]{%
  \expandafter\ifx\csname celestia@palette@#1\endcsname\relax
    \PackageError{celestia}{Unknown palette: #1.}{%
      See documentation for available palettes.}%
  \else
    \csname celestia@palette@#1\endcsname
  \fi
}

\define@key{celestia}{custompalette}{%
  \celestia@parse@custompalette{#1}%
}

\celestia@palette@midnight

\newif\ifcelestia@user@frametitle@set
\newif\ifcelestia@user@footerstyle@set
\newif\ifcelestia@user@decorationstyle@set
\newif\ifcelestia@user@blockstyle@set

\let\celestia@orig@frametitle\celestia@frametitle
\let\celestia@orig@footerstyle\celestia@footerstyle
\let\celestia@orig@decorationstyle\celestia@decorationstyle

\define@key{celestia}{frametitle}{%
  \celestia@user@frametitle@settrue
  \def\celestia@frametitle{#1}%
}

\define@key{celestia}{footerstyle}{%
  \celestia@user@footerstyle@settrue
  \def\celestia@footerstyle{#1}%
}

\define@key{celestia}{decorationstyle}{%
  \celestia@user@decorationstyle@settrue
  \def\celestia@decorationstyle{#1}%
}

\define@key{celestia}{soberblock}[true]{%
  \celestia@user@blockstyle@settrue
  \csname celestia@soberblock#1\endcsname
}

\define@key{celestia}{softblock}[true]{%
  \celestia@user@blockstyle@settrue
  \csname celestia@softblock#1\endcsname
}

\define@key{celestia}{shadedblock}[true]{%
  \celestia@user@blockstyle@settrue
  \csname celestia@shadedblock#1\endcsname
}

\define@key{celestia}{nobackblock}[true]{%
  \celestia@user@blockstyle@settrue
  \csname celestia@nobackblock#1\endcsname
}

\define@key{celestia}{defaultblock}[true]{%
  \celestia@user@blockstyle@settrue
  \celestia@soberblockfalse
  \celestia@softblockfalse
  \celestia@shadedblockfalse
  \celestia@nobackblockfalse
}

% Process options
\ProcessKeyvalOptions*

\@ifpackageloaded{babel}{}{
  \RequirePackage[\celestia@language]{babel}
}

\ifcelestia@nocodebox
    \setbool{celestia@codebox}{false}
\fi

\def\celestia@scheme@light{light}
\def\celestia@scheme@academic{academic}
\def\celestia@scheme@creative{creative}
\def\celestia@scheme@zen{zen}
\def\celestia@scheme@simple{simple}

\ifx\celestia@scheme\@empty\else
\ifx\celestia@scheme\celestia@scheme@light
    \ifcelestia@user@decorationstyle@set\else\def\celestia@decorationstyle{sober}\fi
    \ifcelestia@user@footerstyle@set\else\def\celestia@footerstyle{shadedfullbar}\fi
    \ifcelestia@user@frametitle@set\else\def\celestia@frametitle{shaded}\fi
    \ifcelestia@user@blockstyle@set\else\setbool{celestia@nobackblock}{true}\fi
  \else\ifx\celestia@scheme\celestia@scheme@academic
      \ifcelestia@user@decorationstyle@set\else\def\celestia@decorationstyle{none}\fi
      \ifcelestia@user@footerstyle@set\else\def\celestia@footerstyle{quartercircle}\fi
      \ifcelestia@user@frametitle@set\else\def\celestia@frametitle{centered}\fi
      \ifcelestia@user@blockstyle@set\else\setbool{celestia@shadedblock}{true}\fi
  \else\ifx\celestia@scheme\celestia@scheme@creative
      \ifcelestia@user@decorationstyle@set\else\def\celestia@decorationstyle{fancy}\fi
      \ifcelestia@user@footerstyle@set\else\def\celestia@footerstyle{fullbar}\fi
      \ifcelestia@user@frametitle@set\else\def\celestia@frametitle{plain}\fi
      \ifcelestia@user@blockstyle@set\else\setbool{celestia@softblock}{true}\fi
  \else\ifx\celestia@scheme\celestia@scheme@zen
      \ifcelestia@user@decorationstyle@set\else\def\celestia@decorationstyle{minimal}\fi
      \ifcelestia@user@footerstyle@set\else\def\celestia@footerstyle{number}\fi
      \ifcelestia@user@frametitle@set\else\def\celestia@frametitle{plaincentered}\fi
      \ifcelestia@user@blockstyle@set\else\setbool{celestia@nobackblock}{true}\fi
      \def\celestia@headstyle{sffamily}%
      \def\celestia@headshape{normal}%
      \def\celestia@headweight{mdseries}%
  \else\ifx\celestia@scheme\celestia@scheme@simple
      \ifcelestia@user@decorationstyle@set\else\def\celestia@decorationstyle{none}\fi
      \ifcelestia@user@footerstyle@set\else\def\celestia@footerstyle{fraction}\fi
      \ifcelestia@user@frametitle@set\else\def\celestia@frametitle{plain}\fi
      \ifcelestia@user@blockstyle@set\else\setbool{celestia@nobackblock}{true}\fi
  \fi\fi\fi\fi\fi
\fi

\edef\celestia@decorationopacity{\celestia@decorationopacity}

\pgfmathsetmacro{\dopPercent}{\celestia@decorationopacity * 100}
\pgfmathsetmacro{\opaccent}{\celestia@decorationopacity * 0.75}
\pgfmathsetmacro{\celestia@shadedopacity}{\celestia@shadedblockopacity * 100}
\pgfmathsetmacro{\celestia@soberopacity}{\celestia@soberblockopacity * 100}
\pgfmathsetmacro{\celestia@softopacity}{\celestia@softblockopacity * 100}

\newif\ifcelestia@hasdecoration
\edef\celestia@ds@none{none}%
\ifx\celestia@decorationstyle\celestia@ds@none
  \celestia@hasdecorationfalse
\else
  \celestia@hasdecorationtrue
\fi

\RequirePackage{tikz}
\usetikzlibrary{backgrounds,calc,shapes}
\RequirePackage[most]{tcolorbox}
\RequirePackage{multicol}

%------------------
% Engine detection
%------------------

\ifluatex
    \RequirePackage{fontspec}
    \RequirePackage[T1]{fontenc}
    \ifcelestia@allserif\RequirePackage{mathpazo}\else\RequirePackage{arev}\fi

    \defaultfontfeatures{Ligatures=TeX,Renderer=HarfBuzz}

    \IfFontExistsTF{\celestia@mainface}{
        \setmainfont[\celestia@mainfaceoptions]{\celestia@mainface}%
    }{%
      \IfFontExistsTF{Source Serif Pro}{%
        \setmainfont{Source Serif Pro}%
      }{%
        \setmainfont{TeX Gyre Termes}%
      }%
    }

    \IfFontExistsTF{\celestia@sansface}{
        \setsansfont[\celestia@sansfaceoptions]{\celestia@sansface}%
    }{%
      \IfFontExistsTF{Source Sans Pro}{%
        \setsansfont{Source Sans Pro}%
      }{%
        \setsansfont{TeX Gyre Heros}}%
    }

    \IfFontExistsTF{\celestia@monoface}{
        \setmonofont[\celestia@monofaceoptions]{\celestia@monoface}%
    }{
      \IfFontExistsTF{Source Code Pro}{%
        \setmonofont{Source Code Pro}%
      }{%
        \setmonofont{Latin Modern Mono}}%
    }

    \RequirePackage[
        protrusion=true,
        expansion=true
    ]{microtype}
\else
    \ifxetex
      \RequirePackage{fontspec}
      \ifcelestia@allserif\RequirePackage{mathpazo}\else\RequirePackage{arev}\fi
      \RequirePackage{sourceserifpro}
      \RequirePackage{sourcesanspro}
      \RequirePackage[scaled=0.9]{roboto-mono}
    \else
        \RequirePackage[utf8]{inputenc}
        \RequirePackage[T1]{fontenc}
        \RequirePackage{sourceserifpro}
        \RequirePackage{sourcesanspro}
        \RequirePackage[scaled=0.9]{roboto-mono}
        \RequirePackage[
            protrusion=true,
            expansion=true,
            tracking=true,
            kerning=true,
            spacing=true,
            factor=1100,
            stretch=10,
            shrink=10
        ]{microtype}
    \fi
\fi

\ifcelestia@allserif
\usefonttheme{serif}
\else
\usefonttheme[stillsansserifmath,stillsansseriftext]{serif}
\fi

%------------------
% Color definitions
%------------------

% Test if the color is a valid SVG name, otherwise use HTML code
\newcommand{\@testcolor}[2]{%
    \ifcsname\string\color@#1\endcsname
        \colorlet{#2}{#1}%
    \else
        \definecolor{#2}{HTML}{#1}%
    \fi
}

\@testcolor{\celestia@maincolor}{main}

% Main color palette
\@testcolor{\celestia@blockcolor}{blockcolor}
\@testcolor{\celestia@examplecolor}{examplecolor}
\@testcolor{\celestia@alertcolor}{alertcolor}

\colorlet{codeframecolor}{main!60}

\@testcolor{\celestia@accentcolor}{accent}

\ifx\celestia@backgroundcolor\@empty\else
    \@testcolor{\celestia@backgroundcolor}{background}
\fi

\ifx\celestia@codebackgroundcolor\@empty
    \colorlet{codebackground}{background!95!main}
\else
    \@testcolor{\celestia@codebackgroundcolor}{codebackground}
\fi

% Special colors based on options
\setbeamercolor{plain frametitle}{fg=main,bg=background}

%------------------
% Basic settings
%------------------
\setbeamersize{text margin left=\celestia@margin,text margin right=\celestia@margin}

\def\celestia@fs@fraction{fraction}%
\def\celestia@fs@quarter{quartercircle}%
\def\celestia@fs@number{number}%
\def\celestia@fs@fullbar{fullbar}%
\def\celestia@fs@shadedfullbar{shadedfullbar}%

\ifcelestia@shownavigation
  \setbeamercolor{navigation symbols}{fg=main, bg=background}
  \setbeamercolor{navigation symbols dimmed}{fg=main!50!background}

  \ifx\celestia@footerstyle\celestia@fs@fraction
    \setbeamertemplate{navigation symbols}{}

    \BeforeBeginEnvironment{frame}{%
      \ifbeamer@plainframe
        \setbeamertemplate{footline}[plain with nav]%
      \fi
    }
  \else
    \addtobeamertemplate{navigation symbols}{}{%
      \ifbeamer@plainframe\else
        \ifcelestia@standout\else
          \ifx\celestia@footerstyle\celestia@fs@quarter
            \hspace*{1cm}%
          \else
            \ifx\celestia@footerstyle\celestia@fs@fullbar
              \hspace*{1.15cm}%
            \fi
          \fi
        \fi
      \fi
    }
  \fi
\else
  \setbeamertemplate{navigation symbols}{}
\fi

%------------------
% Font settings
%------------------

\setbeamerfont{title}{size=\csname\celestia@titlesize\endcsname,%
    series=\csname\celestia@headweight\endcsname,%
    family=\csname\celestia@headstyle\endcsname,%
    shape=\csname\celestia@headshape\endcsname}
\setbeamerfont{subtitle}{series=\mdseries}
\setbeamerfont{author}{%
    series=\csname\celestia@headweight\endcsname,%
    family=\csname\celestia@headstyle\endcsname,%
    shape=\csname\celestia@headshape\endcsname}
\setbeamerfont{institute}{series=\mdseries,size=\small}
\setbeamerfont{date}{series=\mdseries}
\setbeamerfont{description item}{size=\normalsize,%
    series=\csname\celestia@headweight\endcsname,%
    family=\ifcelestia@allserif\rmfamily\else\sffamily\fi,%
    }
\setbeamerfont{frametitle}{size=\csname\celestia@frametitlesize\endcsname,%
    series=\csname\celestia@headweight\endcsname,%
    family=\csname\celestia@headstyle\endcsname,%
    shape=\csname\celestia@headshape\endcsname,%
    parent=structure}
\setbeamerfont{plain title}{size=\csname\celestia@frametitlesize\endcsname,%
    series=\csname\celestia@headweight\endcsname,%
    family=\csname\celestia@headstyle\endcsname,%
    shape=\csname\celestia@headshape\endcsname,%
    parent=structure}
\setbeamerfont{section title}{size=\csname\celestia@sectiontitlesize\endcsname,%
    series=\csname\celestia@headweight\endcsname,%
    family=\csname\celestia@headstyle\endcsname,%
    shape=\csname\celestia@headshape\endcsname,%
    parent=structure}
\setbeamerfont{section in toc}{size=\large,%
    series=\mdseries,%
    family=\ifcelestia@allserif\rmfamily\else\sffamily\fi,%
    shape=\normalfont,%
    parent=structure}
\setbeamerfont{subsection title}{size=\csname\celestia@subtitlesize\endcsname,%
    series=\csname\celestia@headweight\endcsname,%
    family=\csname\celestia@headstyle\endcsname,%
    shape=\csname\celestia@headshape\endcsname,%
    parent=structure}
\setbeamerfont{subsection in toc}{size=\normalsize,%
    series=\mdseries,%
    family=\ifcelestia@allserif\rmfamily\else\sffamily\fi,%
    shape=\normalfont,%
    parent=structure}
\setbeamerfont{footline text}{size=\tiny,%
    series=\mdseries,%
    family=\csname\celestia@headstyle\endcsname,%
    shape=\csname\celestia@headshape\endcsname,%
    parent=structure}
\setbeamerfont{block title}{size=\normalsize,%
    series=\csname\celestia@headweight\endcsname,%
    family=\ifcelestia@allserif\rmfamily\else\sffamily\fi,%
    parent=structure}

% Special fonts
\setbeamerfont{caption}{size=\small}
\setbeamerfont{footnote}{size=\small}

% Itemize markers
\newcommand{\celestiaCircle}{%
    \raisebox{0.1ex}{\scalebox{1.2}{$\bullet$}}%
}
\newcommand{\celestiaSmallCircle}{%
    \raisebox{0.2ex}{\scalebox{1}{$\circ$}}%
}
\newcommand{\celestiaDiamond}{%
    \raisebox{0.2ex}{\scalebox{0.8}{$\diamond$}}%
}

%------------------
% Color settings
%------------------
% Basic structure colors
\usecolortheme[named=background]{structure}
\setbeamercolor{background canvas}{bg=background!50}
\ifcelestia@unicolor
  \setbeamercolor{normal text}{fg=main}
\else
  \setbeamercolor{normal text}{fg=black}
\fi
\usebeamercolor[fg]{normal text}

% Title and section colors
\setbeamercolor{titlelike}{parent=structure}
\ifcelestia@unicolor
    \setbeamercolor{title}{fg=main}
    \setbeamercolor{subtitle}{fg=main}
    \setbeamercolor{author}{fg=main}
    \setbeamercolor{institute}{fg=main}
    \setbeamercolor{date}{fg=main}
    \setbeamercolor{section title}{fg=main}
    \setbeamercolor{subsection title}{fg=main}
\else
    \setbeamercolor{title}{fg=main}
    \setbeamercolor{subtitle}{fg=black}
    \setbeamercolor{author}{fg=main}
    \setbeamercolor{institute}{fg=black}
    \setbeamercolor{date}{fg=black}
    \setbeamercolor{section title}{fg=main}
    \setbeamercolor{subsection title}{fg=accent}
\fi

% Navigation and structural elements
\setbeamercolor{item}{fg=main}
\setbeamercolor{description}{fg=main}
\setbeamercolor{section in toc}{fg=main}

% Colors for different block types
\ifcelestia@soberblock
  \ifcelestia@unicolor
    \setbeamercolor{block title}{fg=main,bg=}
    \setbeamercolor{block body}{bg=main!\celestia@soberopacity!white}
    \setbeamercolor{block title example}{fg=main,bg=}
    \setbeamercolor{block body example}{bg=main!\celestia@soberopacity!white}
    \setbeamercolor{block title alerted}{fg=main,bg=}
    \setbeamercolor{block body alerted}{bg=main!\celestia@soberopacity!white}
  \else
    \setbeamercolor{block title}{fg=blockcolor,bg=}
    \setbeamercolor{block body}{bg=blockcolor!\celestia@soberopacity!white}
    \setbeamercolor{block title example}{fg=examplecolor,bg=}
    \setbeamercolor{block body example}{bg=examplecolor!\celestia@soberopacity!white}
    \setbeamercolor{block title alerted}{fg=alertcolor,bg=}
    \setbeamercolor{block body alerted}{bg=alertcolor!\celestia@soberopacity!white}
  \fi
\else
  \ifcelestia@softblock
    \ifcelestia@unicolor
      \setbeamercolor{block title}{fg=main,bg=main!\celestia@softopacity!white}
      \setbeamercolor{block body}{bg=main!\celestia@softopacity!white}
      \setbeamercolor{block title example}{fg=main,bg=main!\celestia@softopacity!white}
      \setbeamercolor{block body example}{bg=main!\celestia@softopacity!white}
      \setbeamercolor{block title alerted}{fg=main,bg=main!\celestia@softopacity!white}
      \setbeamercolor{block body alerted}{bg=main!\celestia@softopacity!white}
    \else
      \setbeamercolor{block title}{fg=blockcolor,bg=blockcolor!\celestia@softopacity!white}
      \setbeamercolor{block body}{bg=blockcolor!\celestia@softopacity!white}
      \setbeamercolor{block title example}{fg=examplecolor,bg=examplecolor!\celestia@softopacity!white}
      \setbeamercolor{block body example}{bg=examplecolor!\celestia@softopacity!white}
      \setbeamercolor{block title alerted}{fg=alertcolor,bg=alertcolor!\celestia@softopacity!white}
      \setbeamercolor{block body alerted}{bg=alertcolor!\celestia@softopacity!white}
    \fi
  \else
    \ifcelestia@shadedblock
      \ifcelestia@unicolor
        \setbeamercolor{block title}{fg=main!70!black,bg=main!\celestia@shadedopacity!white}
        \setbeamercolor{block body}{bg=main!15!white}
        \setbeamercolor{block title example}{fg=main!70!black,bg=main!\celestia@shadedopacity!white}
        \setbeamercolor{block body example}{bg=main!15!white}
        \setbeamercolor{block title alerted}{fg=main!70!black,bg=main!\celestia@shadedopacity!white}
        \setbeamercolor{block body alerted}{bg=main!15!white}
      \else
        \setbeamercolor{block title}{fg=blockcolor!70!black,bg=blockcolor!\celestia@shadedopacity!white}
        \setbeamercolor{block body}{bg=blockcolor!15!white}
        \setbeamercolor{block title example}{fg=examplecolor!70!black,bg=examplecolor!\celestia@shadedopacity!white}
        \setbeamercolor{block body example}{bg=examplecolor!15!white}
        \setbeamercolor{block title alerted}{fg=alertcolor!70!black,bg=alertcolor!\celestia@shadedopacity!white}
        \setbeamercolor{block body alerted}{bg=alertcolor!15!white}
      \fi
    \else
      \ifcelestia@nobackblock
        \ifcelestia@unicolor
          \ifx\celestia@scheme\celestia@scheme@zen
            \setbeamercolor{block title}{fg=main!70!black,bg=}
            \setbeamercolor{block body}{bg=}
            \setbeamercolor{block title example}{fg=main!70!black,bg=}
            \setbeamercolor{block body example}{bg=}
            \setbeamercolor{block title alerted}{fg=main!70!black,bg=}
            \setbeamercolor{block body alerted}{bg=}
          \else
            \setbeamercolor{block title}{fg=main,bg=}
            \setbeamercolor{block body}{bg=}
            \setbeamercolor{block title example}{fg=main,bg=}
            \setbeamercolor{block body example}{bg=}
            \setbeamercolor{block title alerted}{fg=main,bg=}
            \setbeamercolor{block body alerted}{bg=}
          \fi
        \else
          \ifx\celestia@scheme\celestia@scheme@zen
            \setbeamercolor{block title}{fg=blockcolor!70!black,bg=}
            \setbeamercolor{block body}{bg=}
            \setbeamercolor{block title example}{fg=examplecolor!70!black,bg=}
            \setbeamercolor{block body example}{bg=}
            \setbeamercolor{block title alerted}{fg=alertcolor!70!black,bg=}
            \setbeamercolor{block body alerted}{bg=}
          \else
            \setbeamercolor{block title}{fg=blockcolor,bg=}
            \setbeamercolor{block body}{bg=}
            \setbeamercolor{block title example}{fg=examplecolor,bg=}
            \setbeamercolor{block body example}{bg=}
            \setbeamercolor{block title alerted}{fg=alertcolor,bg=}
            \setbeamercolor{block body alerted}{bg=}
          \fi
        \fi
      \else
        \ifcelestia@unicolor
          \setbeamercolor{block title}{fg=background,bg=main!90!white}
          \setbeamercolor{block body}{bg=main!15!white}
          \setbeamercolor{block title example}{fg=background,bg=main!90!white}
          \setbeamercolor{block body example}{bg=main!15!white}
          \setbeamercolor{block title alerted}{fg=background,bg=main!90!white}
          \setbeamercolor{block body alerted}{bg=main!15!white}
        \else
          \setbeamercolor{block title}{fg=background,bg=blockcolor!90!white}
          \setbeamercolor{block body}{bg=blockcolor!10!white}
          \setbeamercolor{block title example}{fg=background,bg=examplecolor!90!white}
          \setbeamercolor{block body example}{bg=examplecolor!15!white}
          \setbeamercolor{block title alerted}{fg=background,bg=alertcolor!90!white}
          \setbeamercolor{block body alerted}{bg=alertcolor!15!white}
        \fi
      \fi
    \fi
  \fi
\fi

% Caption colors
\setbeamercolor{caption}{fg=main}
\setbeamercolor{caption name}{parent=caption}

% Bibliography colors
\setbeamercolor{bibliography entry author}{fg=black}
\setbeamercolor{bibliography entry title}{fg=black}
\setbeamercolor{bibliography entry location}{fg=black}
\setbeamercolor{bibliography entry note}{fg=black}

% Button colors
\setbeamercolor{button}{bg=main,fg=background}
\setbeamercolor{button border}{fg=main}

%------------------
% Basic templates
%------------------
% List settings
\setbeamertemplate{itemize/enumerate subbody begin}{\normalsize}

% Itemize markers
\setbeamertemplate{itemize item}{\celestiaCircle}
\setbeamertemplate{itemize subitem}{\celestiaSmallCircle}
\setbeamertemplate{itemize subsubitem}{\celestiaDiamond}

% Enumeration settings
% Level 1: Circled numbers with main color background
\setbeamertemplate{enumerate item}[circle]
\setbeamercolor{enumerate item}{fg=background,bg=main}

% Level 2: Circled letters with a fixed-size background
\setbeamertemplate{enumerate subitem}{%
    \begin{tikzpicture}[baseline=(char.base)]
        \node[
            circle,
            draw=main,
            fill=white,
            minimum size=0.85em,
            inner sep=0pt
        ] (char) {\scriptsize\textcolor{main}{\alph{enumii}}};
    \end{tikzpicture}%
}
\setbeamercolor{enumerate subitem}{fg=main,bg=background}

% Level 3: Numbers with parentheses
\setbeamertemplate{enumerate subsubitem}{%
    (\insertsubsubenumlabel)%
}
\setbeamercolor{enumerate subsubitem}{fg=main}

% Special commands for text formatting
\renewcommand{\texttt}[1]{\textcolor{accent}{{\ttfamily\csname\celestia@headweight\endcsname #1}}}
\newcommand{\celestia@verb}[1]{%
    \lstinline[style=latex,basicstyle=\ttfamily\footnotesize\color{main},keywordstyle=\color{main}\bfseries]|#1|%
}
\renewcommand{\verb}{\celestia@verb}

% Caption template
\setbeamertemplate{caption}{%
    \raggedright%
    \insertcaption\par%
}

% Hyperlink setup
\hypersetup{colorlinks=true, urlcolor=blockcolor}

% Bold urls
\ifcelestia@boldurl
\def\UrlFont{\bfseries}
\fi

%------------------
% Table of contents
%------------------
% Base TOC style
\setbeamertemplate{section in toc}{%
    \leavevmode%
    {\color{main}\inserttocsectionnumber.}%
    \hspace{0.5em}%
    \hypersetup{linkcolor=main}%
    {\color{main}\inserttocsection}\par%
}
\setbeamertemplate{subsection in toc}{%
    \leavevmode\leftskip=3.2em%
    \rlap{\hskip-2em{\color{accent}\inserttocsectionnumber.\inserttocsubsectionnumber}}%
    \hypersetup{linkcolor=accent}%
    \hspace{0.5em}%
    {\color{accent}\inserttocsubsection}\par%
}

% Compact TOC handling
\ifcelestia@compacttoc
    \patchcmd{\beamer@sectionintoc}
        {\vfill}
        {\vskip\itemsep}
        {}
        {}
\fi

% Two-column TOC handling
\NewDocumentCommand{\twocolumntoc}{ O{} O{} O{} }{%
    \ifcelestia@twocolumntoc
        \setlength{\columnsep}{2em}
        \begin{multicols}{2}
            \tableofcontents[sections={#1-\the\numexpr#2-1}]
            \columnbreak
            \tableofcontents[sections={#2-#3}]
        \end{multicols}
    \else
        \tableofcontents
    \fi
}

%------------------
% Frame title
% ------------------

\newdimen\celestia@frametitle@skip

\def\celestia@ft@elegant{elegant}%
\def\celestia@ft@shaded{shaded}%
\def\celestia@ft@plain{plain}%
\def\celestia@ft@centered{centered}%
\def\celestia@ft@plaincentered{plaincentered}%

\ifx\celestia@frametitle\celestia@ft@plain
  \celestia@frametitle@skip=-1.5em%
\else\ifx\celestia@frametitle\celestia@ft@plaincentered
    \celestia@frametitle@skip=-1.5em%
  \else
    \celestia@frametitle@skip=0em%
  \fi
\fi

% Define colors for different frame title styles
\ifx\celestia@scheme\celestia@scheme@creative
  \setbeamercolor{elegant frametitle}{bg=accent,fg=background}
\else
  \setbeamercolor{elegant frametitle}{bg=main,fg=background}
\fi
\ifx\celestia@scheme\celestia@scheme@creative
  \setbeamercolor{plain frametitle}{fg=accent,bg=}
\else
  \setbeamercolor{plain frametitle}{fg=main,bg=}
\fi
\setbeamercolor{shaded frametitle}{bg=accent!\dopPercent,fg=main}
\setbeamercolor{centered frametitle}{bg=main,fg=background}
\setbeamercolor{plaincentered frametitle}{fg=main,bg=}

% Elegant frame title
\defbeamertemplate{frametitle}{elegant}{%
  \nointerlineskip%
  \begin{beamercolorbox}[wd=\paperwidth,sep=0.6em]{elegant frametitle}
    \usebeamerfont{frametitle}\insertframetitle%
    \ifx\insertframesubtitle\@empty\else%
      \par{\usebeamerfont{framesubtitle}\insertframesubtitle}%
    \fi%
  \end{beamercolorbox}%

  \vskip\celestia@frametitle@skip%
}

% Shaded version of elegant frame title
\defbeamertemplate{frametitle}{shaded}{%
  \nointerlineskip%
  \begin{beamercolorbox}[wd=\paperwidth,sep=0.6em]{shaded frametitle}
    \usebeamerfont{frametitle}\insertframetitle%
    \ifx\insertframesubtitle\@empty\else%
      \par{\usebeamerfont{framesubtitle}\insertframesubtitle}%
    \fi%
  \end{beamercolorbox}%

  \vskip\celestia@frametitle@skip%
}

% Plain frame title (no background, left-aligned)
\defbeamertemplate{frametitle}{plain}{%
    \nointerlineskip%
    \begin{beamercolorbox}[wd=\paperwidth,sep=1em]{plain frametitle}
        \usebeamerfont{plain title}\insertframetitle%
        \ifx\insertframesubtitle\@empty\else%
            \par{\usebeamerfont{framesubtitle}\insertframesubtitle}%
        \fi%
      \end{beamercolorbox}%

    \vskip\celestia@frametitle@skip%
}

% Centered frame title (like elegant but centered)
\defbeamertemplate{frametitle}{centered}{%
    \nointerlineskip%
    \begin{beamercolorbox}[wd=\paperwidth,sep=0.6em,center]{centered frametitle}
        \usebeamerfont{frametitle}\insertframetitle%
        \ifx\insertframesubtitle\@empty\else%
            \par{\usebeamerfont{framesubtitle}\insertframesubtitle}%
        \fi%
      \end{beamercolorbox}%

    \vskip\celestia@frametitle@skip%
}

% Plain centered frame title (no background, centered)
\defbeamertemplate{frametitle}{plaincentered}{%
    \nointerlineskip%
    \begin{beamercolorbox}[wd=\paperwidth,sep=1em,center]{plaincentered frametitle}
        \usebeamerfont{plain title}\insertframetitle%
        \ifx\insertframesubtitle\@empty\else%
            \par{\usebeamerfont{framesubtitle}\insertframesubtitle}%
        \fi%
      \end{beamercolorbox}%

    \vskip\celestia@frametitle@skip%
}

% Set the default style to elegant
\setbeamertemplate{frametitle}[\celestia@frametitle]

%------------------
% Frame continuation
%------------------
\setbeamertemplate{frametitle continuation}{\usebeamerfont{frametitle}(\insertcontinuationcount)}

% ------------------
% Footline
% ------------------

\setbeamertemplate{footline}{%
  \ifnum \theframenumber=1
  \else
    \leavevmode%
    \setbeamercolor{current author}{fg=main,bg=background}%
    \setbeamercolor{current title}{fg=background,bg=main}%
    \setbeamercolor{current date}{fg=main,bg=background}%
    \setbeamercolor{current page}{fg=background,bg=main}%

    \ifx\celestia@footerstyle\celestia@fs@fraction
      \hspace*{2em}%
      \hfill%
      \ifcelestia@shownavigation%
        \setbeamercolor{navigation symbols}{fg=main, bg=background}
        \setbeamercolor{navigation symbols dimmed}{fg=main!50!background} %
        \insertslidenavigationsymbol%
        \insertframenavigationsymbol%
        \insertsubsectionnavigationsymbol%
        \insertsectionnavigationsymbol%
        \insertdocnavigationsymbol%
        \insertbackfindforwardnavigationsymbol%
      \fi%
      \hfill\usebeamerfont{footline text}\textcolor{main}{\footnotesize\insertframenumber/\inserttotalframenumber}%
      \hspace*{2em}%
      \vspace*{2em}%

    \else\ifx\celestia@footerstyle\celestia@fs@quarter
        \begin{tikzpicture}[overlay]
          \usebeamercolor[bg]{current title}
          \draw[fill] (\paperwidth,0ex) circle (8ex);
          \usebeamercolor[fg]{current title}
          \node at (\paperwidth-3.25ex,3.25ex) {\footnotesize\insertframenumber};
        \end{tikzpicture}

      \else\ifx\celestia@footerstyle\celestia@fs@number
          \begin{tikzpicture}[overlay]
            \usebeamercolor[bg]{current title}
            \node at (\paperwidth-3.25ex,3.25ex) {\footnotesize\insertframenumber};
          \end{tikzpicture}

        \else\ifx\celestia@footerstyle\celestia@fs@fullbar
            \hbox{%
              \begin{beamercolorbox}[wd=.275\paperwidth,ht=3ex,dp=1.75ex,left]{current title}%
                \usebeamerfont{footline text}\hspace{1em}\insertshortauthor%
              \end{beamercolorbox}%
              \begin{beamercolorbox}[wd=.45\paperwidth,ht=3ex,dp=1.75ex,center]{current title}%
                \usebeamerfont{footline text}{\hypersetup{linkcolor=.}\insertshorttitle}%
              \end{beamercolorbox}%
              \begin{beamercolorbox}[wd=.275\paperwidth,ht=3ex,dp=1.75ex,right]{current title}%
                \usebeamerfont{footline text}\insertshortdate{}\hspace*{6.5em}%
              \end{beamercolorbox}%
            }%
            \begin{tikzpicture}[overlay]
              \usebeamercolor[bg]{current title}
              \draw[fill] (\paperwidth-5ex,3.625ex) circle (6ex);
              \usebeamercolor[fg]{current title}
              \draw[fill] (\paperwidth-5ex,3.625ex) circle (5ex);
              \usebeamercolor[bg]{current title}
              \node at (\paperwidth-5ex,4ex) {{\footnotesize\mathversion{bold}${}^{\insertframenumber}\!/\!_{\inserttotalframenumber}$}};
            \end{tikzpicture}
          \else\ifx\celestia@footerstyle\celestia@fs@shadedfullbar
              \setbeamercolor{current title}{fg=main,bg=accent!\dopPercent}%
              \hbox{%
                \begin{beamercolorbox}[wd=.275\paperwidth,ht=3ex,dp=1.75ex,left]{current title}%
                  \usebeamerfont{footline text}\hspace{1em}\insertshortauthor%
                \end{beamercolorbox}%
                \begin{beamercolorbox}[wd=.45\paperwidth,ht=3ex,dp=1.75ex,center]{current title}%
                  \usebeamerfont{footline text}{\hypersetup{linkcolor=.}\insertshorttitle}%
                \end{beamercolorbox}%
                \begin{beamercolorbox}[wd=.275\paperwidth,ht=3ex,dp=1.75ex,right]{current title}%
                  \usebeamerfont{footline text}\insertshortdate{}\hspace*{6.5em}%
                \end{beamercolorbox}%
              }%
              \begin{tikzpicture}[overlay]
                \usebeamercolor[bg]{current title}
                \draw[fill] (\paperwidth-5ex,3.625ex) circle (6ex);
                \draw[fill=background!5] (\paperwidth-5ex,3.625ex) circle (5ex);
                \usebeamercolor[fg]{current title}
                \node at (\paperwidth-5ex,4ex) {{\footnotesize${}^{\insertframenumber}\!/\!_{\inserttotalframenumber}$}};
              \end{tikzpicture}
            \else
              \hbox{%
                \begin{beamercolorbox}[wd=.3\paperwidth,ht=3ex,dp=1.5ex,center,sep=-1ex]{current author}%
                  \usebeamerfont{footline text}\insertshortauthor%
                \end{beamercolorbox}%
                \begin{beamercolorbox}[wd=.4\paperwidth,ht=3ex,dp=1.5ex,center,sep=-1ex]{current title}%
                  \usebeamerfont{footline text}{\hypersetup{linkcolor=.}\insertshorttitle}%
                \end{beamercolorbox}%
                \begin{beamercolorbox}[wd=.2\paperwidth,ht=3ex,dp=1.5ex,center,sep=-1ex]{current date}%
                  \usebeamerfont{footline text}\insertshortdate%
                \end{beamercolorbox}%
                \begin{beamercolorbox}[wd=.1\paperwidth,ht=3ex,dp=1.5ex,center,sep=-1ex]{current page}%
                  \usebeamerfont{footline text}\insertframenumber/\inserttotalframenumber%
                \end{beamercolorbox}%
              }%
            \fi
          \fi
        \fi
      \fi
    \fi
  \fi%
}

\defbeamertemplate{footline}{plain with nav}{%
    \ifcelestia@shownavigation%
      \def\celestia@fs@fraction{fraction}\ifx\celestia@footerstyle\celestia@fs@fraction
            \leavevmode%
            \hbox{%
                \hbox to \paperwidth{%
                    \hfill%
                    \raise1mm\hbox{%
                        \usebeamerfont{footline text}%
                        \insertslidenavigationsymbol%
                        \insertframenavigationsymbol%
                        \insertsubsectionnavigationsymbol%
                        \insertsectionnavigationsymbol%
                        \insertdocumentnavigationsymbol%
                        \insertbackfindforwardnavigationsymbol%
                    }%
                    \kern2mm%
                }%
            }%
            \vskip1em%
        \fi%
    \fi%
}

%------------------
% Standout frames
%------------------
\define@key{beamerframe}{standout}[true]{%
    \booltrue{celestia@standout}%
    \begingroup
        % Center the frame and remove frame number
        \setkeys{beamerframe}{c}%
        \setkeys{beamerframe}{noframenumbering}%

        % Set the colors for standout frame
        \setbeamercolor{background canvas}{bg=accent!10}%
        \setbeamercolor{frametitle}{fg=background,bg=accent}%
        \setbeamercolor{normal text}{fg=accent}%
        \usebeamercolor[fg]{normal text}%

        \setbeamercolor{block title}{parent={}}%
        \ifcelestia@unicolor
        \setbeamercolor{block title}{fg=main,bg=accent!10}%
        \setbeamercolor{block title example}{parent={}}%
        \setbeamercolor{block title example}{fg=main,bg=accent!10}%
        \setbeamercolor{block title alerted}{parent={}}%
        \setbeamercolor{block title alerted}{fg=main,bg=accent!10}%
        \else
        \setbeamercolor{block title}{fg=blockcolor,bg=accent!10}%
        \setbeamercolor{block title example}{parent={}}%
        \setbeamercolor{block title example}{fg=examplecolor,bg=accent!10}%
        \setbeamercolor{block title alerted}{parent={}}%
        \setbeamercolor{block title alerted}{fg=alertcolor,bg=accent!10}%
        \fi

        \setbeamercolor{button}{bg=accent,fg=background}
        \setbeamercolor{button border}{fg=accent}

        % Remove footline for standout frames
        \setbeamertemplate{footline}{}%
}

% Close the group at the end of the frame
\pretocmd{\beamer@reseteecodes}{%
    \ifbool{celestia@standout}{%
        \endgroup
        \boolfalse{celestia@standout}%
    }{}%
}{}{}

% Format standout content
\AtBeginEnvironment{beamer@frameslide}{%
    \ifbool{celestia@standout}{%
        \centering
        \bfseries
    }{}%
}

%------------------
% Special pages
% ------------------

% Title page
\setbeamertemplate{title page}{%
  \vfill%
  \begingroup

  \newdimen\celestia@baseindent
  \celestia@baseindent=8pt

  \newdimen\celestia@decorpad
  \celestia@decorpad=0pt

  \ifcelestia@hasdecoration
    \def\@sober{sober}%
    \ifx\celestia@decorationstyle\@sober
      \celestia@decorpad=1em
    \fi
  \fi

  \newdimen\celestia@leftskip
  \newdimen\celestia@rightskip
  \celestia@leftskip=\dimexpr \celestia@baseindent + \celestia@decorpad \relax
  \celestia@rightskip=\dimexpr \celestia@baseindent + \celestia@decorpad \relax

  \def\celestia@ta@center{center}
  \def\celestia@ta@right{right}

  \ifx\celestia@titlealign\celestia@ta@center
    \centering
    \leftskip=\celestia@leftskip plus 1fil
    \rightskip=\celestia@rightskip plus 1fil
  \else\ifx\celestia@titlealign\celestia@ta@right
      \raggedleft
      \leftskip=\celestia@leftskip plus 1fil
      \rightskip=\celestia@rightskip
    \else
      \leftskip=\celestia@leftskip
      \rightskip=\celestia@rightskip plus 1fil
    \fi\fi

  {\usebeamercolor[fg]{title}\usebeamerfont{title}\inserttitle\par}
  \ifx\insertsubtitle\@empty\else%
    \vskip0.75em%
    {\usebeamercolor[fg]{subtitle}\usebeamerfont{subtitle}\insertsubtitle\par}%
  \fi%
  \vskip0.5em%
  \tikz[baseline]{\draw[line width=0.8pt, main!65, line cap=round] (0,0) -- (0.5\linewidth,0);}%
  \vskip1.5em
  {\usebeamercolor[fg]{author}\usebeamerfont{author}\insertauthor\par}
  \vskip0.5em
  {\usebeamercolor[fg]{institute}\usebeamerfont{institute}\insertinstitute\par}
  \vskip0.5em
  {\usebeamercolor[fg]{date}\usebeamerfont{date}\insertdate\par}

  \endgroup
  \vfill%
}

\addtobeamertemplate{title page}{%
  \ifx\celestia@titlebackground\@empty\else
    \begin{tikzpicture}[remember picture, overlay]
      \node[opacity=0.15] at (current page.center) {\includegraphics[width=\paperwidth,height=\paperheight]{\celestia@titlebackground}};
    \end{tikzpicture}
  \fi
  \begin{tikzpicture}[remember picture, overlay]
    \ifcelestia@hasdecoration
      \def\@sober{sober}%
      \def\@fancy{fancy}%
      \def\@minimal{minimal}%
      \def\celestia@ta@center{center}
      \def\celestia@ta@right{right}

      \ifx\celestia@decorationstyle\@sober
        \ifx\celestia@titlealign\celestia@ta@center
          \fill[accent, opacity=\opaccent]
          (current page.south west) -- ([xshift=8cm] current page.south west) -- ([yshift=4cm] current page.south west) -- cycle;
          \fill[accent, opacity=\opaccent]
          (current page.south west) -- ([xshift=4cm] current page.south west) -- ([yshift=7cm] current page.south west) -- cycle;
          \fill[accent, opacity=\opaccent]
          (current page.south east) -- ([xshift=-8cm] current page.south east) -- ([yshift=4cm] current page.south east) -- cycle;
          \fill[accent, opacity=\opaccent]
          (current page.south east) -- ([xshift=-4cm] current page.south east) -- ([yshift=7cm] current page.south east) -- cycle;
        \else\ifx\celestia@titlealign\celestia@ta@right
            \fill[accent, opacity=\opaccent]
            (current page.south west) -- ([xshift=7cm] current page.south west) -- ([yshift=4cm] current page.south west) -- cycle;
            \fill[accent, opacity=\opaccent]
            (current page.south west) -- ([xshift=4cm] current page.south west) -- ([yshift=7cm] current page.south west) -- cycle;
          \else
            \fill[accent, opacity=\opaccent]
            (current page.south east) -- ([xshift=-7cm] current page.south east) -- ([yshift=4cm] current page.south east) -- cycle;
            \fill[accent, opacity=\opaccent]
            (current page.south east) -- ([xshift=-4cm] current page.south east) -- ([yshift=7cm] current page.south east) -- cycle;
          \fi\fi
      \else\ifx\celestia@decorationstyle\@fancy
          \ifx\celestia@titlealign\celestia@ta@center
            \fill[accent, opacity=\opaccent]
            (current page.north east) .. controls ++(-4,-9) and ++(0.5,5.5) .. ([xshift=-1.8cm]current page.south east)
            -- (current page.south east) -- (current page.north east) -- cycle;
            \fill[accent, opacity=\opaccent]
            (current page.north east) .. controls ++(-3,-9) and ++(1.5,6.5) .. ([xshift=-1cm]current page.south east)
            -- (current page.south east) -- (current page.north east) -- cycle;
            \fill[accent, opacity=\opaccent]
            (current page.south east) .. controls ++(-4,9) and ++(0.5,-5.5) .. ([xshift=-1.8cm]current page.north east)
            -- (current page.north east) -- (current page.south east) -- cycle;
            \fill[accent, opacity=\opaccent]
            (current page.south east) .. controls ++(-3,9) and ++(1.5,-6.5) .. ([xshift=-1cm]current page.north east)
            -- (current page.north east) -- (current page.south east) -- cycle;
            \fill[accent, opacity=\opaccent]
            (current page.north west) .. controls ++(4.5,-9.5) and ++(-0.6,-6) .. ([xshift=1.8cm]current page.south west)
            -- (current page.south west) -- (current page.north west) -- cycle;
            \fill[accent, opacity=\opaccent]
            (current page.north west) .. controls ++(3.5,-9.5) and ++(-1.5,-7) .. ([xshift=1cm]current page.south west)
            -- (current page.south west) -- (current page.north west) -- cycle;
            \fill[accent, opacity=\opaccent]
            (current page.south west) .. controls ++(4.5,9.5) and ++(-0.6,6) .. ([xshift=1.8cm]current page.north west)
            -- (current page.north west) -- (current page.south west) -- cycle;
            \fill[accent, opacity=\opaccent]
            (current page.south west) .. controls ++(3.5,9.5) and ++(-1.5,7) .. ([xshift=1cm]current page.north west)
            -- (current page.north west) -- (current page.south west) -- cycle;
          \else\ifx\celestia@titlealign\celestia@ta@right
              \fill[accent, opacity=\opaccent]
              (current page.north west) .. controls ++(4.5,-9.5) and ++(-0.6,-6) .. ([xshift=1.8cm]current page.south west)
              -- (current page.south west) -- (current page.north west) -- cycle;
              \fill[accent, opacity=\opaccent]
              (current page.north west) .. controls ++(3.5,-9.5) and ++(-1.5,-7) .. ([xshift=1cm]current page.south west)
              -- (current page.south west) -- (current page.north west) -- cycle;
              \fill[accent, opacity=\opaccent]
              (current page.south west) .. controls ++(4.5,9.5) and ++(-0.6,6) .. ([xshift=1.8cm]current page.north west)
              -- (current page.north west) -- (current page.south west) -- cycle;
              \fill[accent, opacity=\opaccent]
              (current page.south west) .. controls ++(3.5,9.5) and ++(-1.5,7) .. ([xshift=1cm]current page.north west)
              -- (current page.north west) -- (current page.south west) -- cycle;
            \else
              \fill[accent, opacity=\opaccent]
              (current page.north east) .. controls ++(-4,-9) and ++(0.5,5.5) .. ([xshift=-1.8cm]current page.south east)
              -- (current page.south east) -- (current page.north east) -- cycle;
              \fill[accent, opacity=\opaccent]
              (current page.north east) .. controls ++(-3,-9) and ++(1.5,6.5) .. ([xshift=-1cm]current page.south east)
              -- (current page.south east) -- (current page.north east) -- cycle;
              \fill[accent, opacity=\opaccent]
              (current page.south east) .. controls ++(-4,9) and ++(0.5,-5.5) .. ([xshift=-1.8cm]current page.north east)
              -- (current page.north east) -- (current page.south east) -- cycle;
              \fill[accent, opacity=\opaccent]
              (current page.south east) .. controls ++(-3,9) and ++(1.5,-6.5) .. ([xshift=-1cm]current page.north east)
              -- (current page.north east) -- (current page.south east) -- cycle;
            \fi\fi
        \else\ifx\celestia@decorationstyle\@minimal
            \ifx\celestia@titlealign\celestia@ta@center
              \fill[accent, opacity=\opaccent]
              (current page.south east)
              .. controls ++(-2.5,6) and ++(-0.5,-4) ..
              ([xshift=-2cm,yshift=4cm]current page.north east)
              -- (current page.north east) -- (current page.south east) -- cycle;
              \fill[accent, opacity=\opaccent]
              (current page.north east)
              .. controls ++(-2.5,-6) and ++(-0.5,4) ..
              ([xshift=-2cm,yshift=-4cm]current page.south east)
              -- (current page.south east) -- (current page.north east) -- cycle;
              \fill[accent, opacity=\opaccent]
              (current page.south west)
              .. controls ++(2.5,6) and ++(0.5,-4) ..
              ([xshift=2cm,yshift=4cm]current page.north west)
              -- (current page.north west) -- (current page.south west) -- cycle;
              \fill[accent, opacity=\opaccent]
              (current page.north west)
              .. controls ++(2.5,-6) and ++(0.5,4) ..
              ([xshift=2cm,yshift=-4cm]current page.south west)
              -- (current page.south west) -- (current page.north west) -- cycle;
            \else\ifx\celestia@titlealign\celestia@ta@right
                \fill[accent, opacity=\opaccent]
                (current page.south west)
                .. controls ++(2.5,6) and ++(0.5,-4) ..
                ([xshift=2cm,yshift=4cm]current page.north west)
                -- (current page.north west) -- (current page.south west) -- cycle;
                \fill[accent, opacity=\opaccent]
                (current page.north west)
                .. controls ++(2.5,-6) and ++(0.5,4) ..
                ([xshift=2cm,yshift=-4cm]current page.south west)
                -- (current page.south west) -- (current page.north west) -- cycle;
              \else
                \fill[accent, opacity=\opaccent]
                (current page.south east)
                .. controls ++(-2.5,6) and ++(-0.5,-4) ..
                ([xshift=-2cm,yshift=4cm]current page.north east)
                -- (current page.north east) -- (current page.south east) -- cycle;
                \fill[accent, opacity=\opaccent]
                (current page.north east)
                .. controls ++(-2.5,-6) and ++(-0.5,4) ..
                ([xshift=-2cm,yshift=-4cm]current page.south east)
                -- (current page.south east) -- (current page.north east) -- cycle;
              \fi
            \fi
          \fi
        \fi
      \fi
    \fi
  \end{tikzpicture}
}{}

% Section pages
\setbeamertemplate{section page}{%
  \begin{center}
    \vfill
    \parbox{.9\textwidth}{%
      \centering

      \ifcelestia@hasdecoration
        \def\@sober{sober}%
        \def\@fancy{fancy}%
        \def\@minimal{minimal}%

        \ifx\celestia@decorationstyle\@sober
          \hypersetup{linkcolor=main}%
          \ifcelestia@sectionnumber
            \vskip-3em
            \begin{tikzpicture}
              \fill[background] (0,0) circle (2.5ex);
              \node[text=main] at (0,0) {\LARGE\bfseries\thesection};
            \end{tikzpicture}
            \vskip1em
          \fi
          \usebeamerfont{section title}\LARGE\color{main}\insertsectionhead\par%
        \else
          \hypersetup{linkcolor=main}%
          \ifx\celestia@decorationstyle\@fancy
            \begin{tikzpicture}[remember picture,overlay]
              \fill[accent, opacity=\opaccent]
              (current page.south west) .. controls ++(8.5,3) and ++(-6.5,1.5) .. ([yshift=0.8cm]current page.south east)
              -- (current page.south east) -- (current page.south west) -- cycle;
              \fill[accent, opacity=\opaccent]
              ([xshift=1.5cm]current page.south west) .. controls ++(5.5,-0.5) and ++(-8.5,2) .. ([yshift=0.4cm]current page.south east)
              -- (current page.south east) -- (current page.south west) -- cycle;
            \end{tikzpicture}
          \fi
          \ifx\celestia@decorationstyle\@minimal
            \begin{tikzpicture}[remember picture,overlay]
              \draw[main, line width=3pt]
                (current page.north west) -- (current page.north east);
              \draw[main, line width=3pt]
                (current page.south west) -- (current page.south east);
            \end{tikzpicture}
          \fi
          \ifcelestia@sectionnumber
            \vskip-2em
              \begin{tikzpicture}
                \draw[main, line width=1.2pt] (0,0) circle (2.5ex);
                \node[text=main, opacity=\celestia@decorationopacity] at (0,0) {\Large\bfseries\thesection};
              \end{tikzpicture}
            \vskip1em
          \fi
          \usebeamerfont{section title}\huge\color{main}\insertsectionhead\par%
        \fi
      \else
        \hypersetup{linkcolor=main}%
        \ifcelestia@sectionnumber
          \vskip-3em
          \def\@simple{simple}%
          \ifx\celestia@scheme\@simple
            \begin{tikzpicture}
              \draw[main, line width=1.2pt] (0,0) circle (2.5ex);
              \node[text=main] at (0,0) {\LARGE\bfseries\thesection};
            \end{tikzpicture}
          \else
            \begin{tikzpicture}
              \fill[main] (0,0) circle (2.5ex);
              \node[text=background] at (0,0) {\LARGE\bfseries\thesection};
            \end{tikzpicture}
          \fi
          \vskip1em
        \fi
        \usebeamerfont{section title}\insertsectionhead\par%
      \fi
    }
    \vfill
  \end{center}
}

% Subsection Page
\setbeamertemplate{subsection page}{%
  \begin{center}
    \vfill
    \parbox{.9\textwidth}{%
      \centering
      \hypersetup{linkcolor=accent}%
      \ifcelestia@sectionnumber
        \vskip-3em
        \def\@simple{simple}%
        \ifx\celestia@scheme\@simple
          \begin{tikzpicture}
            \draw[accent, line width=1.2pt] (0,0) circle (2.5ex);
            \node[text=accent] at (0,0) {\Large\bfseries\thesection.\thesubsection};
          \end{tikzpicture}
        \else
          \begin{tikzpicture}
            \fill[accent] (0,0) circle (2.5ex);
            \node[text=background] at (0,0) {\Large\bfseries\thesection.\thesubsection};
          \end{tikzpicture}
        \fi
        \vskip1.5em
      \fi
      \usebeamerfont{subsection title}\insertsubsectionhead\par%
    }
    \vfill
  \end{center}
}

\ifcelestia@sectionpage
  \AtBeginSection{%
    \ifcelestia@hasdecoration
      \def\@sober{sober}%
      \ifx\celestia@decorationstyle\@sober
        {\setbeamercolor{background canvas}{bg=accent!\dopPercent}
        \begin{frame}[noframenumbering,plain]
          \sectionpage
        \end{frame}}
      \else
        \begin{frame}[noframenumbering,plain]
          \sectionpage
        \end{frame}
      \fi
    \else
      \begin{frame}[noframenumbering,plain]
        \sectionpage
      \end{frame}
    \fi
  }
  \AtBeginSubsection{%
    \begin{frame}[noframenumbering,plain]
      \subsectionpage
    \end{frame}
  }
\fi

% ------------------
% Math and Theorem Settings
% ------------------

\ifcelestia@soberblock
  \ifcelestia@unicolor
    \tcbset{
      celestia@blockstyle/.style={enhanced jigsaw, opacitybacktitle=0,  opacityframe=0, colback=main!\celestia@soberopacity, colframe=background, fonttitle=\csname\celestia@headweight\endcsname\color{main}, left=3pt, lefttitle=3pt, right=3pt, separator sign=~~---~},
      celestia@exstyle/.style={enhanced jigsaw, opacitybacktitle=0,  opacityframe=0, colback=main!\celestia@soberopacity, colframe=background, fonttitle=\csname\celestia@headweight\endcsname\color{main}, left=3pt, lefttitle=3pt, right=3pt, separator sign=~~---~},
      celestia@alertstyle/.style={enhanced jigsaw, opacitybacktitle=0,  opacityframe=0, colback=main!\celestia@soberopacity, colframe=background, fonttitle=\csname\celestia@headweight\endcsname\color{main}, left=3pt, lefttitle=3pt, right=3pt, separator sign=~~---~}
    }
  \else
    \tcbset{
      celestia@blockstyle/.style={enhanced jigsaw, opacitybacktitle=0, opacityframe=0, colback=blockcolor!\celestia@soberopacity, colframe=background, fonttitle=\csname\celestia@headweight\endcsname\color{blockcolor}, left=3pt, lefttitle=3pt, right=3pt, separator sign=~~---~},
      celestia@exstyle/.style={enhanced jigsaw, opacitybacktitle=0, opacityframe=0, colback=examplecolor!\celestia@soberopacity, colframe=background, fonttitle=\csname\celestia@headweight\endcsname\color{examplecolor}, left=3pt, lefttitle=3pt, right=3pt, separator sign=~~---~},
      celestia@alertstyle/.style={enhanced jigsaw, opacitybacktitle=0, opacityframe=0, colback=alertcolor!\celestia@soberopacity, colframe=background, fonttitle=\csname\celestia@headweight\endcsname\color{alertcolor}, left=3pt, lefttitle=3pt, right=3pt, separator sign=~~---~}
    }
  \fi
\else\ifcelestia@softblock
    \ifcelestia@unicolor
      \tcbset{
        celestia@blockstyle/.style={colback=main!\celestia@softopacity, colframe=main!\celestia@softopacity, fonttitle=\csname\celestia@headweight\endcsname\color{main}, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~},
        celestia@exstyle/.style={colback=main!\celestia@softopacity, colframe=main!\celestia@softopacity, fonttitle=\csname\celestia@headweight\endcsname\color{main}, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~},
        celestia@alertstyle/.style={colback=main!\celestia@softopacity, colframe=main!\celestia@softopacity, fonttitle=\csname\celestia@headweight\endcsname\color{main}, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~}
      }
    \else
      \tcbset{
        celestia@blockstyle/.style={colback=blockcolor!\celestia@softopacity, colframe=blockcolor!\celestia@softopacity, fonttitle=\csname\celestia@headweight\endcsname\color{blockcolor}, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~},
        celestia@exstyle/.style={colback=examplecolor!\celestia@softopacity, colframe=examplecolor!\celestia@softopacity, fonttitle=\csname\celestia@headweight\endcsname\color{examplecolor}, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~},
        celestia@alertstyle/.style={colback=alertcolor!\celestia@softopacity, colframe=alertcolor!\celestia@softopacity, fonttitle=\csname\celestia@headweight\endcsname\color{alertcolor}, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~}
      }
    \fi
  \else\ifcelestia@shadedblock
      \ifcelestia@unicolor
        \tcbset{
          celestia@blockstyle/.style={colback=main!15, colframe=main!15, fonttitle=\csname\celestia@headweight\endcsname\color{main!70!black}, colbacktitle=main!\celestia@shadedopacity!white, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~},
          celestia@exstyle/.style={colback=main!15, colframe=main!15, fonttitle=\csname\celestia@headweight\endcsname\color{main!70!black}, colbacktitle=main!\celestia@shadedopacity!white, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~},
          celestia@alertstyle/.style={colback=main!15, colframe=main!15, fonttitle=\csname\celestia@headweight\endcsname\color{main!70!black}, colbacktitle=main!\celestia@shadedopacity!white, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~}
        }
      \else
        \tcbset{
          celestia@blockstyle/.style={colback=blockcolor!15, colframe=blockcolor!5, fonttitle=\csname\celestia@headweight\endcsname\color{blockcolor!70!black}, colbacktitle=blockcolor!\celestia@shadedopacity!white, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~},
          celestia@exstyle/.style={colback=examplecolor!15, colframe=examplecolor!15, fonttitle=\csname\celestia@headweight\endcsname\color{examplecolor!70!black}, colbacktitle=examplecolor!\celestia@shadedopacity!white, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~},
          celestia@alertstyle/.style={colback=alertcolor!15, colframe=alertcolor!15, fonttitle=\csname\celestia@headweight\endcsname\color{alertcolor!70!black}, colbacktitle=alertcolor!\celestia@shadedopacity!white, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~}
        }
      \fi
    \else\ifcelestia@nobackblock
        \ifx\celestia@scheme\celestia@scheme@zen
          \colorlet{celestia@tcb@blocktitle}{blockcolor!70!black}
          \colorlet{celestia@tcb@exampletitle}{examplecolor!70!black}
          \colorlet{celestia@tcb@alerttitle}{alertcolor!70!black}
          \colorlet{celestia@tcb@maintitle}{main!70!black}
        \else
          \colorlet{celestia@tcb@blocktitle}{blockcolor}
          \colorlet{celestia@tcb@exampletitle}{examplecolor}
          \colorlet{celestia@tcb@alerttitle}{alertcolor}
          \colorlet{celestia@tcb@maintitle}{main}
        \fi
        \ifcelestia@unicolor
          \tcbset{
            celestia@blockstyle/.style={opacityframe=0, opacityback=0, colback=background, colframe=background, fonttitle=\csname\celestia@headweight\endcsname\color{celestia@tcb@maintitle}, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~},
            celestia@exstyle/.style={opacityframe=0, opacityback=0, colback=background, colframe=background, fonttitle=\csname\celestia@headweight\endcsname\color{celestia@tcb@maintitle}, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~},
            celestia@alertstyle/.style={opacityframe=0, opacityback=0, colback=background, colframe=background, fonttitle=\csname\celestia@headweight\endcsname\color{celestia@tcb@maintitle}, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~}
          }
        \else
          \tcbset{
            celestia@blockstyle/.style={opacityframe=0, opacityback=0, colback=background, colframe=background, fonttitle=\csname\celestia@headweight\endcsname\color{celestia@tcb@blocktitle}, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~},
            celestia@exstyle/.style={opacityframe=0, opacityback=0, colback=background, colframe=background, fonttitle=\csname\celestia@headweight\endcsname\color{celestia@tcb@exampletitle}, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~},
            celestia@alertstyle/.style={opacityframe=0, opacityback=0, colback=background, colframe=background, fonttitle=\csname\celestia@headweight\endcsname\color{celestia@tcb@alerttitle}, left=2pt, lefttitle=2pt, right=2pt, separator sign=~~---~}
          }
        \fi
      \else
        \ifcelestia@unicolor
          \tcbset{
            celestia@blockstyle/.style={colback=main!15, colframe=main!90!white, fonttitle=\csname\celestia@headweight\endcsname\color{background}, colbacktitle=main!90!white, left=1ex, lefttitle=1ex, right=1ex, separator sign=~~---~},
            celestia@exstyle/.style={colback=main!15, colframe=main!90!white, fonttitle=\csname\celestia@headweight\endcsname\color{background}, colbacktitle=main!90!white, left=1ex, lefttitle=1ex, right=1ex, separator sign=~~---~},
            celestia@alertstyle/.style={colback=main!15, colframe=main!90!white, fonttitle=\csname\celestia@headweight\endcsname\color{background}, colbacktitle=main!90!white, left=1ex, lefttitle=1ex, right=1ex, separator sign=~~---~}
          }
        \else
          \tcbset{
            celestia@blockstyle/.style={colback=blockcolor!15, colframe=blockcolor!90!white, fonttitle=\csname\celestia@headweight\endcsname\color{background}, colbacktitle=blockcolor!90!white, left=1ex, lefttitle=1ex, right=1ex, separator sign=~~---~},
            celestia@exstyle/.style={colback=examplecolor!15, colframe=examplecolor!90!white, fonttitle=\csname\celestia@headweight\endcsname\color{background}, colbacktitle=examplecolor!90!white, left=1ex, lefttitle=1ex, right=1ex, separator sign=~~---~},
            celestia@alertstyle/.style={colback=alertcolor!15, colframe=alertcolor!90!white, fonttitle=\csname\celestia@headweight\endcsname\color{background}, colbacktitle=alertcolor!90!white, left=1ex, lefttitle=1ex, right=1ex, separator sign=~~---~}
          }
        \fi
      \fi
    \fi
  \fi
\fi

\let\theorem\relax
\let\endtheorem\relax
\let\lemma\relax
\let\endlemma\relax
\let\proposition\relax
\let\endproposition\relax
\let\corollary\relax
\let\endcorollary\relax
\let\definition\relax
\let\enddefinition\relax
\let\example\relax
\let\endexample\relax

\ExplSyntaxOn
\tl_new:N \l__custom_tcb_title_tl
\tl_new:N \l__custom_tcb_label_tl
\tl_new:N \l__custom_tcb_options_tl

\NewDocumentCommand{\celestia@newtcbtheorem}{O{}mmmm}{%
    \newtcbtheorem[#1]{#2inner}{#3}{#4}{#5}

    \NewDocumentEnvironment{#2}{O{}}{%
        \tl_clear:N \l__custom_tcb_title_tl
        \tl_clear:N \l__custom_tcb_label_tl
        \tl_clear:N \l__custom_tcb_options_tl
        \keys_set:nn { custom/tcb } { ##1 }

        \tl_if_empty:NTF \l__custom_tcb_options_tl {%
            \use:x {
                \exp_not:N \begin{#2inner}
                {\exp_not:V \l__custom_tcb_title_tl}
                {\exp_not:V \l__custom_tcb_label_tl}
            }
        }{%
            \use:x {
                \exp_not:N \begin{#2inner}
                [\exp_not:V \l__custom_tcb_options_tl]
                {\exp_not:V \l__custom_tcb_title_tl}
                {\exp_not:V \l__custom_tcb_label_tl}
            }
        }
    }{%
        \end{#2inner}
    }
}

\keys_define:nn { custom/tcb } {
    title .tl_set:N = \l__custom_tcb_title_tl,
    label .tl_set:N = \l__custom_tcb_label_tl,
    colback .code:n = \tl_put_right:Nx \l__custom_tcb_options_tl { colback=#1, },
    colframe .code:n = \tl_put_right:Nx \l__custom_tcb_options_tl { colframe=#1, },
    coltitle .code:n = \tl_put_right:Nx \l__custom_tcb_options_tl { coltitle=#1, },
    fonttitle .code:n = \tl_put_right:Nx \l__custom_tcb_options_tl { fonttitle=#1, },
    sharp~corners .bool_set:N = \l__custom_tcb_sharp_bool,
    sharp~corners .code:n = \tl_put_right:Nn \l__custom_tcb_options_tl { sharp~corners, },
    unknown .code:n = \tl_put_right:Nx \l__custom_tcb_options_tl { \l_keys_key_tl=#1, },
}
\ExplSyntaxOff

\celestia@newtcbtheorem[auto counter]{theorem}{\translate{Theorem}}
    {celestia@blockstyle}{th}

\celestia@newtcbtheorem[auto counter]{lemma}{\translate{Lemma}}
    {celestia@blockstyle}{le}

\celestia@newtcbtheorem[auto counter]{proposition}{\translate{Proposition}}
    {celestia@blockstyle}{prop}

\celestia@newtcbtheorem[auto counter]{corollary}{\translate{Corollary}}
    {celestia@blockstyle}{cor}

\celestia@newtcbtheorem[auto counter]{definition}{\translate{Definition}}
    {celestia@exstyle}{def}

\celestia@newtcbtheorem[auto counter]{example}{\translate{Example}}
    {celestia@exstyle}{ex}

\celestia@newtcbtheorem[auto counter]{remark}{\translate{Remark}}
    {celestia@alertstyle}{rem}

\newcommand{\mathterm}[1]{\textcolor{blockcolor}{\mathbf{#1}}}
\newcommand{\mathdef}[1]{\textcolor{examplecolor}{\mathit{#1}}}
  
%------------------
% Code Settings
%------------------

\def\celestia@ch@listings{listings}%
\def\celestia@ch@minted{minted}%

\ifx\celestia@scheme\celestia@scheme@simple
  \def\celestia@lst@colframe{codebackground}
\else
  \def\celestia@lst@colframe{codeframecolor}
\fi

\ifx\celestia@codehandler\celestia@ch@minted

% Minted
\RequirePackage{minted}

\setminted{
    bgcolor=codebackground,
    fontsize=\footnotesize,
    breaklines=true,
    style=friendly
}

\newtcolorbox{mintedbox}[1][]{
  enhanced,
  colback=codebackground,
  colframe=\celestia@lst@colframe,
  boxrule=0.5pt,
  left=1em,
  right=1em,
  top=2pt,
  bottom=2pt,
  boxsep=0pt,
  left skip=5pt,
  overlay={
    \fill[accent] (frame.north west) rectangle ([xshift=3pt]frame.south west);
  },
  #1
}

\ifcelestia@codebox
    \BeforeBeginEnvironment{minted}{\begin{mintedbox}}
    \AfterEndEnvironment{minted}{\end{mintedbox}}
\fi

\else

% Listings
\RequirePackage{listings}

\AtEndPreamble{
    \colorlet{codeKeyword}{main}
    \colorlet{codeEmph}{accent}
    \colorlet{codeString}{main!70!black}
    \colorlet{codeComment}{main!50!background}
    \colorlet{codeNumber}{accent!80!main}

    \ifcelestia@unicolor
        \colorlet{codeIdentifier}{main}
    \else
        \colorlet{codeIdentifier}{black}
    \fi

    \lstset{
        basicstyle=\ttfamily\footnotesize\color{codeIdentifier},
        keywordstyle=\color{codeKeyword}\csname\celestia@headweight\endcsname,
        stringstyle=\color{codeString},
        commentstyle=\color{codeComment},
        emphstyle=\color{codeEmph}\csname\celestia@headweight\endcsname,
        numberstyle=\color{codeNumber},
        numbersep=20pt,
        columns=fullflexible,
        breaklines=true,
        showstringspaces=false,
        inputencoding=utf8,
        extendedchars=true,
        escapeinside={(*@}{@*)},
        aboveskip=.25\baselineskip,
        belowskip=.25\baselineskip,
        literate={%
            á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
            {Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
            {à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
            {À}{{\`A}}1 {È}{{\`E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
            {ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
            {Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
            {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
            {Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
            {œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
            {ẞ}{{\SS}}1 {ç}{{\c{c}}}1 {Ç}{{\c{C}}}1 {ø}{{\o}}1 {Ø}{{\O}}1
            {å}{{\aa}}1 {Å}{{\AA}}1 {ã}{{\~a}}1 {õ}{{\~o}}1 {Ã}{{\~A}}1
            {Õ}{{\~O}}1 {ñ}{{\~n}}1 {Ñ}{{\~N}}1 {¿}{{?`}}1 {¡}{{!`}}1
            {°}{{\textdegree}}1 {º}{{\textordmasculine}}1 {ª}{{\textordfeminine}}1
            {€}{{\euro}}1 {£}{{\pounds}}1 {©}{{\copyright}}1 {®}{{\textregistered}}1
            {«}{{\guillemotleft}}1 {»}{{\guillemotright}}1 {Ð}{{\DH}}1 {ð}{{\dh}}1
            {Ý}{{\'Y}}1 {ý}{{\'y}}1 {Þ}{{\TH}}1 {þ}{{\th}}1 {Ă}{{\u{A}}}1
            {ă}{{\u{a}}}1 {Ą}{{\k{A}}}1 {ą}{{\k{a}}}1 {Ć}{{\'C}}1 {ć}{{\'c}}1
            {Č}{{\v{C}}}1 {č}{{\v{c}}}1 {Ď}{{\v{D}}}1 {ď}{{\v{d}}}1 {Đ}{{\DJ}}1
            {đ}{{\dj}}1 {Ė}{{\.{E}}}1 {ė}{{\.{e}}}1 {Ę}{{\k{E}}}1 {ę}{{\k{e}}}1
            {Ě}{{\v{E}}}1 {ě}{{\v{e}}}1 {Ğ}{{\u{G}}}1 {ğ}{{\u{g}}}1 {Ĩ}{{\~I}}1
            {ĩ}{{\~\i}}1 {Į}{{\k{I}}}1 {į}{{\k{i}}}1 {İ}{{\.{I}}}1 {ı}{{\i}}1
            {Ĺ}{{\'L}}1 {ĺ}{{\'l}}1 {Ľ}{{\v{L}}}1 {ľ}{{\v{l}}}1 {Ł}{{\L{}}}1
            {ł}{{\l{}}}1 {Ń}{{\'N}}1 {ń}{{\'n}}1 {Ň}{{\v{N}}}1 {ň}{{\v{n}}}1
            {Ő}{{\H{O}}}1 {ő}{{\H{o}}}1 {Ŕ}{{\'{R}}}1 {ŕ}{{\'{r}}}1 {Ř}{{\v{R}}}1
            {ř}{{\v{r}}}1 {Ś}{{\'S}}1 {ś}{{\'s}}1 {Ş}{{\c{S}}}1 {ş}{{\c{s}}}1
            {Š}{{\v{S}}}1 {š}{{\v{s}}}1 {Ť}{{\v{T}}}1 {ť}{{\v{t}}}1 {Ũ}{{\~U}}1
          }

          \lstdefinelanguage{json}{%
            keywords={true,false,null},
            sensitive=false,
            morestring=[b]",
            morecomment=[l]{//},
            morecomment=[s]{/*}{*/},
            literate=
            *{0}{{{\color{codeComment}0}}}{1}
            {1}{{{\color{codeComment}1}}}{1}
            {2}{{{\color{codeComment}2}}}{1}
            {3}{{{\color{codeComment}3}}}{1}
            {4}{{{\color{codeComment}4}}}{1}
            {5}{{{\color{codeComment}5}}}{1}
            {6}{{{\color{codeComment}6}}}{1}
            {7}{{{\color{codeComment}7}}}{1}
            {8}{{{\color{codeComment}8}}}{1}
            {9}{{{\color{codeComment}9}}}{1}
            {:}{{{\color{codeKeyword}{:}}}}{1}
            {,}{{{\color{codeKeyword}{,}}}}{1}
            {\{}{{{\color{codeKeyword}{\{}}}}{1}
            {\}}{{{\color{codeKeyword}{\}}}}}{1}
            {[}{{{\color{codeKeyword}{[}}}}{1}
            {]}{{{\color{codeKeyword}{]}}}}{1},
          }

          % YAML
          \lstdefinelanguage{yaml}{%
            keywords={true,false,null,yes,no,on,off},
            sensitive=false,
            morestring=[b]",
            morestring=[b]',
            morecomment=[l]{\#},
            literate=
            *{:}{{{\color{codeKeyword}{:}}}}{1}
            {-}{{{\color{codeKeyword}{-}}}}{1}
            {>}{{{\color{codeKeyword}{>}}}}{1}
            {|}{{{\color{codeKeyword}{|}}}}{1},
          }

          % TOML
          \lstdefinelanguage{toml}{%
            keywords={true,false},
            sensitive=false,
            morestring=[b]",
            morestring=[b]',
            morecomment=[l]{\#},
            literate=
            *{=}{{{\color{codeKeyword}{=}}}}{1}
            {[}{{{\color{codeKeyword}{[}}}}{1}
            {]}{{{\color{codeKeyword}{]}}}}{1}
            {.}{{{\color{codeKeyword}{.}}}}{1},
          }

          % CSV
          \lstdefinelanguage{csv}{%
            sensitive=false,
            morestring=[b]",
            morecomment=[l]{\#},
            literate=
            *{,}{{{\color{codeKeyword}{,}}}}{1}
            {;}{{{\color{codeKeyword}{;}}}}{1},
          }

          % Markdown
          \lstdefinelanguage{markdown}{%
            sensitive=false,
            morecomment=[l]{\%},
            literate=
            *{\#}{{{\color{codeKeyword}{\#}}}}{1}
            {-}{{{\color{codeKeyword}{-}}}}{1}
            {*}{{{\color{codeKeyword}{*}}}}{1}
            {>}{{{\color{codeKeyword}{>}}}}{1}
            {`}{{{\color{codeKeyword}{`}}}}{1}
            {|}{{{\color{codeKeyword}{|}}}}{1}
            {[}{{{\color{codeKeyword}{[}}}}{1}
            {]}{{{\color{codeKeyword}{]}}}}{1}
            {(}{{{\color{codeKeyword}{(}}}}{1}
            {)}{{{\color{codeKeyword}{)}}}}{1},
          }

          %% Language-Specific Styles
          %% ----------------------------------
          \lstdefinestyle{python}{%
            language=Python,
            morekeywords={%
              @property,@classmethod,@staticmethod,
            },
            emph={%
              range,int,str,list,dict,set,bool,float,
              tuple,super,type,print,len,sum,min,max,
              enumerate,zip,map,filter,any,all,as,assert,
              nonlocal,with,yield,self,True,False,None,
              lambda,raise,await,async,
            },
            morestring=[b]""",
          }

          \lstdefinestyle{java}{%
            language=Java,
            morekeywords={%
              @Override,@Deprecated,@SuppressWarnings,
              @FunctionalInterface,@SafeVarargs,
              var,record,sealed,permits,
              public,private,protected,static,final,
              abstract,interface,extends,implements
            },
            morecomment=[s]{/*}{*/},
            morecomment=[l]//,
            morestring=[b]",
          }

          \lstdefinestyle{cpp}{%
            language=C++,
            morekeywords={%
              nullptr,constexpr,override,final,
              template,typename,concept,requires,
              auto,decltype,noexcept,static_assert,
              thread_local,alignas,alignof
            },
            morecomment=[s]{/*}{*/},
            morecomment=[l]//,
            morestring=[b]",
          }

          \lstdefinestyle{javascript}{%
            language=JavaScript,
            morekeywords={%
              let,const,var,function,class,extends,
              static,get,set,new,this,super,
              import,export,default,from,as,
              async,await,yield,return,
              undefined,null,true,false
            },
            morecomment=[s]{/*}{*/},
            morecomment=[l]//,
            morestring=[b]",
            morestring=[b]',
            morestring=[b]`,
          }

          \lstdefinestyle{sql}{%
            language=SQL,
            morekeywords={%
              CREATE,TABLE,INSERT,INTO,VALUES,
              SELECT,FROM,WHERE,GROUP,BY,HAVING,
              ORDER,LIMIT,JOIN,LEFT,RIGHT,INNER,
              UPDATE,SET,DELETE,ALTER,DROP,
              CONSTRAINT,PRIMARY,KEY,FOREIGN,
              REFERENCES,CASCADE,INDEX
            },
            sensitive=false,
          }

          \lstdefinestyle{latex}{%
            language=[latex]TeX,
            texcsstyle=*\bfseries\color{codeKeyword},
            moretexcs = {usetheme,tableofcontents,index,footnote,sout,part,chapter,subsection,subsubsection,paragraph,maketitle,leqslant,geqslant,varnothing,includegraphics,draw,node,theoremstyle,newtcolorbox,tcbuselibrary,newtcbtheorem,SI,ang,ce,chemfig,norm,abs,deriv,R,N,Z,ProvidesPackage,color,ps,montitre,lstset,lstinline,lstinputlisting,definecolor,textcolor,colorlet,setlength,colorbox,fcolorbox,addplot,pgfplotsset,opadd,opsub,opmul,opdiv,opgcd,metre,second,squared,kelvin,coulomb,volt,per,opprint,legend,tkzDefPoint,tkzInterLL,tkzGetPoint,tkzDrawPolygon,tkzDrawSegments,tkzMarkRightAngles,tkzMarkSegments,tkzLabelPoints,boxed,boldsymbol,boldmath,multirow,addbibresource,printbibliography,bm,dfrac,meter,thead,makecell,euro,cellcolor,rowcolor,columncolor,base,repere,rog,ron,rond,derpart,drv,integrer,nuplet,anuplet,ensemble,E,V,suite,suitar,suitgeo,vect,norme,tr,rank,adj,sgn,im,di,intabfx,integrale,e,moinsinf,plusinf,sisetup,restoregeometry,newgeometry},
            morecomment=[l]{\%},
            morestring=[b]",
            sensitive=true
          }

          \lstdefinestyle{bash}{%
            language=bash,
            morekeywords={%
              source, alias, bg, bind, break, builtin, cd, command, compgen,
              complete, continue, declare, dirs, disown, echo, enable, eval,
              exec, exit, export, fc, fg, getopts, hash, help, history, jobs,
              kill, let, local, logout, mapfile, popd, printf, pushd, pwd,
              read, readarray, readonly, return, set, shift, shopt, suspend,
              test, times, trap, type, typeset, ulimit, umask, unalias,
              unset, wait
            },
            morestring=[b]",
            morestring=[b]',
            morestring=[b]\`,
            morecomment=[l]{\#},
            literate=
            *{\$}{{{\color{codeKeyword}{\$}}}}{1}
            {|}{{{\color{codeKeyword}{|}}}}{1}
            {>}{{{\color{codeKeyword}{>}}}}{1}
            {<}{{{\color{codeKeyword}{<}}}}{1}
            {&}{{{\color{codeKeyword}{\&}}}}{1},
          }

          \lstdefinestyle{assembly}{%
            language=[x86]Assembler,
            morekeywords={%
              section, global, extern
            },
            morecomment=[l]{;},
            morecomment=[l]{\#},
            literate=
            *{,}{{{\color{codeKeyword}{,}}}}{1}
            {:}{{{\color{codeKeyword}{:}}}}{1}
            {[}{{{\color{codeKeyword}{[}}}}{1}
            {]}{{{\color{codeKeyword}{]}}}}{1}
            {\$}{{{\color{codeKeyword}{\$}}}}{1}
            {\%}{{{\color{codeKeyword}{\%}}}}{1},
          }

          \lstdefinestyle{lisp}{%
            language=Lisp,
            morekeywords={%
              setq, setf, loop, do, progn, when, unless
            },
            literate=
            *{(}{{{\color{codeKeyword}{(}}}}{1}
            {)}{{{\color{codeKeyword}{)}}}}{1}
            {'}{{{\color{codeKeyword}{'}}}}{1}
            {`}{{{\color{codeKeyword}{`}}}}{1}
            {,}{{{\color{codeKeyword}{,}}}}{1},
          }

          \lstdefinestyle{json}{%
            language=json,
          }

          \lstdefinestyle{yaml}{%
            language=yaml,
          }

          \lstdefinestyle{toml}{%
            language=toml,
          }

          \lstdefinestyle{csv}{%
            language=csv,
          }

          \lstdefinestyle{markdown}{%
            language=markdown,
          }

}

\newtcolorbox{lstbox}[1][]{
    enhanced,
    opacityframe=0,
    opacityback=0,
    colback=codebackground,
    colframe=\celestia@lst@colframe,
    boxrule=0.5pt,
    left=1em,
    right=1em,
    top=2pt,
    bottom=2pt,
    boxsep=0pt,
    left skip=5pt,
    overlay={
        \fill[accent] (frame.north west) rectangle ([xshift=3pt]frame.south west);
    },
    #1
}

\ifcelestia@codebox
    \BeforeBeginEnvironment{lstlisting}{\begin{lstbox}}
    \AfterEndEnvironment{lstlisting}{\end{lstbox}}

    \let\oldlstinputlisting\lstinputlisting
    \renewcommand{\lstinputlisting}[2][]{%
        \begin{lstbox}%
        \oldlstinputlisting[#1]{#2}%
        \end{lstbox}%
    }
\else
    \BeforeBeginEnvironment{lstlisting}{}
    \AfterEndEnvironment{lstlisting}{}
\fi

\fi

% Bibliography
\AtBeginDocument{%
    \IfPackageLoadedTF{biblatex}{%
        \defbibheading{bibliography}{%
            \frametitle{Références}%
        }%
    }{%
        \IfPackageLoadedTF{natbib}{%
            \setbeamertemplate{bibliography item}{\insertbiblabel}%
            \setbeamercolor{bibliography entry author}{fg=black}%
            \setbeamercolor{bibliography entry title}{fg=black}%
            \setbeamercolor{bibliography entry location}{fg=black}%
            \setbeamercolor{bibliography entry note}{fg=black}%
        }{}%
    }%
}

% Mode declaration
\mode<all>
