\usepackage{geometry}
\usepackage{xcolor}
\usepackage{eso-pic}
\usepackage{fancyhdr}
\usepackage{tikz}
\usepackage{fontspec}
\usepackage{sectsty}
\usepackage{titlesec}

% 设置页面布局
\geometry{a4paper, 
          total={170mm,257mm}, 
          left=30mm, 
          top=25mm, 
          bottom=25mm, 
          right=30mm,
          headheight=15pt}

% 定义颜色
\definecolor{light}{HTML}{ECF1EC}
\definecolor{highlight}{HTML}{800080}
\definecolor{dark}{HTML}{330033}
\definecolor{coverbg}{HTML}{F0F0F0} % 封面背景灰

% 封面设计
\AddToShipoutPictureBG*{% 仅作用于封面
    \AtPageLowerLeft{
        \begin{tikzpicture}[remember picture, overlay]
            \fill[color=coverbg] (0,0) rectangle (\paperwidth,\paperheight);
            \node[anchor=north east] at (0.95\paperwidth,0.9\paperheight) 
                {\includegraphics[width=8cm]{logo.png}};
        \end{tikzpicture}
    }
}

% 正文页眉页脚
\fancypagestyle{mainstyle}{
    \fancyhf{}
    \fancyhead[RE]{\normalfont\small\leftmark} % 偶数页章标题
    \fancyhead[LO]{\normalfont\small\rightmark} % 奇数页节标题
    \fancyfoot[C]{\thepage}
    \renewcommand{\headrulewidth}{0.4pt}
    \renewcommand{\footrulewidth}{0pt}
}

% 章节标题样式
\addtokomafont{chapter}{\color{dark}\Huge\bfseries}
\addtokomafont{section}{\color{dark}\Large\bfseries}
\addtokomafont{subsection}{\color{dark}\large\bfseries}

% 标题格式调整
\RedeclareSectionCommand[
  beforeskip=-1sp,
  afterskip=1sp
]{chapter}

% 自定义标题页
\renewcommand{\maketitle}{%
    \begin{titlepage}
        \ThisCenterWallPaper{1.0}{logo.png} % 封面背景图
        \vspace*{3cm}
        \begin{center}
            {\fontsize{28}{36}\selectfont\bfseries\color{dark} \@title}\\[2cm]
            {\LARGE \@subtitle}\\[3cm]
            {\Large \@author}\\[2cm]
            {\large \@date}
        \end{center}
    \end{titlepage}
}
