:root {
  --paper: #f6f4ee;
  --surface: #fffcf7;
  --ink: #20332f;
  --muted: #5c6c66;
  --line: #d6dfd6;
  --accent: #146b5c;
  --accent-strong: #0d5146;
  --accent-pale: #e5f0e8;
  --warm: #fff0e3;
  --orange: #a94e2d;
  --code: #142c2a;
  --code-ink: #eaf5ed;
  --shadow: 0 18px 54px rgba(31, 60, 48, .08);
  --radius: 13px;
  --content-width: 78rem;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  min-width: 0;
  margin: 0;
  /* 不要设 overflow-x: hidden —— 它会传播到 viewport，在移动端使嵌套横向滚动容器（公式/表格）无法用手指拖动 */
  background: var(--paper);
  color: var(--ink);
  font-family: "Segoe UI", "Microsoft YaHei", "PingFang SC", "Noto Sans CJK SC", sans-serif;
  font-size: 16px;
  line-height: 1.78;
}
a { color: var(--accent); text-underline-offset: .22em; }
button { font: inherit; }
:focus-visible { outline: 3px solid #d06b36; outline-offset: 3px; }
.skip-link {
  position: fixed; top: -5rem; left: 1rem; z-index: 100;
  padding: .65rem 1rem; border-radius: 0 0 8px 8px; background: #fff;
  box-shadow: var(--shadow); color: var(--ink); font-weight: 700;
}
.skip-link:focus { top: 0; }
.topbar {
  position: relative; z-index: 20; border-bottom: 1px solid var(--line);
  background: rgba(255, 252, 247, .92); backdrop-filter: blur(12px);
}
.topbar-inner, .page, .site-footer-inner { width: min(100%, var(--content-width)); margin: 0 auto; }
.topbar-inner {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; padding: .8rem 1.8rem;
}
.brand {
  display: inline-flex; align-items: center; gap: .6rem; color: var(--ink);
  font-size: .82rem; font-weight: 800; letter-spacing: .1em; text-decoration: none;
}
.brand::before {
  width: .65rem; height: .65rem; border-radius: 2px; background: var(--accent);
  content: ""; transform: rotate(45deg);
}
.topbar-meta { color: var(--muted); font-size: .75rem; letter-spacing: .08em; }
.page { padding: 0 1.8rem 5rem; }
.hero {
  display: grid; grid-template-columns: minmax(0, 1.45fr) minmax(13rem, .55fr);
  gap: 2.5rem; align-items: end; padding: 5rem 0 4rem;
}
.eyebrow {
  margin: 0 0 .9rem; color: var(--accent); font-size: .74rem;
  font-weight: 800; letter-spacing: .18em; text-transform: uppercase;
}
h1, h2, h3, h4, h5, h6 { color: var(--ink); line-height: 1.3; }
.hero h1 { max-width: 15ch; margin: 0; font-size: clamp(2.6rem, 6vw, 5rem); letter-spacing: -.055em; }
.hero-subtitle { max-width: 58rem; margin: 1.3rem 0 0; color: #435650; font-size: clamp(1rem, 1.8vw, 1.2rem); }
.hero-note {
  margin: 0; padding: 1.25rem 1.35rem; border: 1px solid var(--line);
  border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow);
  color: var(--muted); font-size: .86rem;
}
.hero-note strong { display: block; margin-bottom: .25rem; color: var(--accent); }
.site-layout {
  display: grid; grid-template-columns: minmax(12rem, 15rem) minmax(0, 1fr);
  gap: clamp(2rem, 5vw, 4.5rem); align-items: start;
}
.global-nav { position: sticky; top: 1.5rem; min-width: 0; padding: .5rem 0; }
.nav-heading {
  margin: 0 0 .8rem; color: var(--muted); font-size: .7rem;
  font-weight: 800; letter-spacing: .16em; text-transform: uppercase;
}
.chapter-list { margin: 0; padding: 0 0 0 .15rem; border-left: 1px solid var(--line); list-style: none; }
.chapter-list a {
  display: block; margin-left: -1px; padding: .52rem .8rem .52rem 1rem;
  border-left: 2px solid transparent; color: var(--muted); font-size: .84rem;
  line-height: 1.5; text-decoration: none;
}
.chapter-list a:hover, .chapter-list a[aria-current="page"] { border-left-color: var(--accent); color: var(--accent); }
.chapter-list .order {
  display: block; margin-bottom: .05rem; color: #87948e;
  font-family: Consolas, "SFMono-Regular", monospace; font-size: .68rem;
}
.nav-toggle {
  display: none; width: 100%; padding: .65rem .8rem; border: 1px solid var(--line);
  border-radius: 8px; background: var(--surface); color: var(--accent); font-weight: 700;
}
.content { min-width: 0; }
/* 锚点跳转（多页站点的 h2/h3、单文件的 .chapter-block）让开移动端顶部常驻目录条 */
.chapter-block, .article-body h2, .article-body h3 { scroll-margin-top: 4.75rem; }
.content > :first-child { margin-top: 0; }
.chapter-header { padding-bottom: 2.4rem; }
.chapter-kicker {
  margin: 0; color: var(--accent); font-family: Consolas, "SFMono-Regular", monospace;
  font-size: .82rem; font-weight: 700;
}
.chapter-header h1 { max-width: 22ch; margin: .4rem 0 1rem; font-size: clamp(2.15rem, 5vw, 4.2rem); letter-spacing: -.045em; }
.chapter-goal { max-width: 65ch; margin: 0; color: var(--muted); }
.practice {
  margin: 0 0 2.8rem; padding: 1.2rem 1.4rem; border-left: 4px solid var(--accent);
  border-radius: 0 var(--radius) var(--radius) 0; background: var(--accent-pale);
}
.practice h2 { margin: 0 0 .4rem; font-size: 1rem; }
.practice ul { margin: 0; padding-left: 1.25rem; }
.article-body { min-width: 0; }
.article-body h2 {
  margin: 2.8rem 0 1rem; padding-top: .7rem; border-top: 1px solid var(--line);
  font-size: clamp(1.55rem, 3vw, 2.05rem); letter-spacing: -.025em;
}
.article-body h3 { margin: 2rem 0 .65rem; font-size: 1.2rem; }
.article-body h4, .article-body h5, .article-body h6 { margin: 1.65rem 0 .55rem; font-size: 1rem; }
.article-body p, .article-body li { overflow-wrap: anywhere; }
.article-body p { margin: .75rem 0 1.05rem; }
.article-body ul, .article-body ol { margin: .75rem 0 1.1rem; padding-left: 1.55rem; }
.article-body li + li { margin-top: .3rem; }
.article-body blockquote {
  margin: 1.4rem 0; padding: .8rem 1.2rem; border-left: 4px solid var(--orange);
  background: var(--warm); color: #684332;
}
.article-body blockquote > :first-child { margin-top: 0; }
.article-body blockquote > :last-child { margin-bottom: 0; }
.article-body code, .article-body kbd, .article-body samp { font-family: Consolas, "SFMono-Regular", "Courier New", monospace; }
.article-body :not(pre) > code {
  padding: .14em .36em; border-radius: 4px; background: #e8ede5; color: #174f45;
  font-size: .9em; overflow-wrap: anywhere;
}
.article-body pre {
  max-width: 100%; margin: 1.25rem 0; overflow: auto; border-radius: var(--radius);
  background: var(--code); color: var(--code-ink); padding: 1.1rem 1.25rem;
  font-size: .86rem; line-height: 1.72; tab-size: 2;
}
.article-body pre code { color: inherit; }
.table-scroll {
  max-width: 100%; margin: 1.25rem 0; overflow-x: auto; overflow-y: hidden; border: 1px solid var(--line);
  border-radius: 9px; background: var(--surface);
}
.article-body table { width: 100%; min-width: 34rem; border-collapse: collapse; }
.article-body th, .article-body td { padding: .65rem .8rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.article-body th { background: #edf3ec; color: var(--accent-strong); font-size: .84rem; }
.article-body tr:last-child td { border-bottom: 0; }
.article-body hr { margin: 2.5rem 0; border: 0; border-top: 1px solid var(--line); }
.article-body img { display: block; max-width: 100%; height: auto; margin: 1.25rem auto; border-radius: 8px; }
.math, .math-display { max-width: 100%; }
.math-display { margin: 1.25rem 0; overflow-x: auto; overflow-y: hidden; padding: .5rem 0; }
/* 交互演示（{{demo:}}）—— 默认折叠，展开后按需加载，避免手册变成一屏控件 */
.article-body details.demo-fold > summary { background: #e9f0f6; color: #17506b; }
.article-body details.demo-fold > summary:hover { background: #dde8f1; }
.article-body .demo-body { min-width: 0; padding: 0; }
.article-body .demo-body iframe {
  display: block; width: 100%; height: 32rem; border: 0; background: #fff;
}
@media (max-width: 820px) {
  .article-body .demo-body iframe { height: 27rem; }
}
.demo-fallback {
  display: block; padding: .85rem 1rem;
  color: var(--muted); font-size: .82rem;
}
.demo-content { min-width: 0; padding: 1rem 1.15rem; overflow-x: auto; overflow-y: hidden; }
.math-display, .table-scroll, .demo-content, .article-body pre {
  -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain;
}
.chapter-nav {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem;
  margin-top: 3.5rem; padding-top: 1.5rem; border-top: 1px solid var(--line);
}
.chapter-nav a, .chapter-nav span {
  min-width: 0; padding: .9rem 1rem; border: 1px solid var(--line);
  border-radius: 10px; background: var(--surface); color: var(--accent); text-decoration: none;
}
.chapter-nav span { color: #9aa39e; }
.chapter-nav .next { text-align: right; }
.chapter-nav small { display: block; color: var(--muted); font-size: .68rem; font-weight: 700; letter-spacing: .08em; }
.home-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.home-card {
  min-width: 0; padding: 1.35rem 1.4rem; border: 1px solid var(--line);
  border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow);
}
.home-card .order { color: var(--accent); font-family: Consolas, "SFMono-Regular", monospace; font-size: .75rem; font-weight: 700; }
.home-card h2 { margin: .3rem 0 .65rem; font-size: 1.25rem; }
.home-card h2 a { color: var(--ink); text-decoration: none; }
.home-card h2 a:hover { color: var(--accent); }
.home-card p { margin: 0; color: var(--muted); font-size: .9rem; }
.site-footer { border-top: 1px solid var(--line); color: var(--muted); font-size: .75rem; }
.site-footer-inner { padding: 1.5rem 1.8rem 2.2rem; }
.personal-use {
  margin: 0 0 2.5rem; padding: .9rem 1.1rem; border: 1px solid var(--line);
  border-left: 3px solid var(--orange); border-radius: 0 8px 8px 0;
  background: var(--warm); color: var(--muted); font-size: .8rem; line-height: 1.7;
}
.personal-use strong { display: block; margin-bottom: .2rem; color: var(--orange); font-size: .78rem; letter-spacing: .04em; }
.personal-use p { margin: 0; }

@media (max-width: 820px) {
  .hero { grid-template-columns: 1fr; padding: 3.7rem 0 3rem; }
  .hero h1 { max-width: none; }
  .site-layout { display: block; }
  /* 移动端目录常驻：入口固定在视口顶部，面板脱离文档流向下展开（不推动正文） */
  .global-nav {
    position: sticky; top: 0; z-index: 15;
    margin-bottom: 1.25rem; padding: .5rem 0; background: var(--paper);
  }
  /* 小标题仅供 aria-labelledby 引用，移动端入口由按钮承担，视觉隐藏但保留在无障碍树 */
  .nav-heading {
    position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%);
  }
  .nav-toggle { display: block; }
  .chapter-list {
    display: none; position: absolute; left: 0; right: 0; top: 100%;
    margin: 0; padding: .4rem; border: 1px solid var(--line);
    border-radius: 9px; background: var(--surface);
    max-height: min(60vh, 22rem); overflow-y: auto; overscroll-behavior: contain;
    box-shadow: 0 14px 30px rgba(31, 60, 48, .14);
  }
  .chapter-list.is-open { display: block; }
  .chapter-list a { border-left: 0; border-radius: 6px; padding: .55rem .7rem; }
  .chapter-list a[aria-current="page"] { background: var(--accent-pale); }
}

@media (max-width: 620px) {
  body { font-size: 15px; }
  .page, .topbar-inner, .site-footer-inner { padding-left: 1rem; padding-right: 1rem; }
  .personal-use { margin-bottom: 2rem; padding: .8rem .9rem; font-size: .78rem; }
  .topbar-meta { display: none; }
  .hero { padding-top: 2.8rem; }
  .home-grid, .chapter-nav { grid-template-columns: 1fr; }
  .chapter-nav .next { text-align: left; }
  .article-body pre, .demo-content { border-radius: 8px; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: .01ms !important; animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}
/* 折叠块（:::details）—— 用于章末习题的 AI 解答，默认折叠，避免剧透 */
.article-body details.fold {
  margin: 1.25rem 0; border: 1px solid var(--line); border-radius: 9px;
  background: var(--surface); overflow: hidden;
}
.article-body details.fold > summary {
  cursor: pointer; padding: .7rem 1rem; font-weight: 600; color: var(--accent-strong);
  background: #edf3ec; list-style: none; user-select: none;
}
.article-body details.fold > summary::-webkit-details-marker { display: none; }
.article-body details.fold > summary::before {
  content: "▸ "; display: inline-block; transition: transform .15s ease;
}
.article-body details.fold[open] > summary::before { content: "▾ "; }
.article-body details.fold > summary:hover { background: #e3ece1; }
.article-body details.fold > .fold-body { padding: .2rem 1rem .6rem; }
.article-body details.fold > .fold-body > :first-child { margin-top: .6rem; }
.article-body details.fold > .fold-body > :last-child { margin-bottom: .2rem; }

/* 章节头的来源标注：PDF 页码范围 + 内容来源 */
.article-body .chapter-source,
.chapter-header .chapter-source {
  margin: .35rem 0 0; font-size: .82rem; color: var(--muted);
  letter-spacing: .01em;
}
