: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 { min-width: 0; }
.content { min-width: 0; }

/* ---- 目录：浮动圆钮 + 左侧抽屉（参考《看见智能》的移动端阅读体验） ---- */
/* position 必须 fixed，不能用 absolute：隐藏的 checkbox 一旦可聚焦，点 <label> 会把它 focus，
   浏览器随后把「绝对定位在页面顶部」的它滚进视口，整页猛地跳回页首（实测 scrollY 3000→0）。
   fixed 的定位盒不随文档滚动，聚焦也停在视口里，不再触发这跳。 */
.toc-toggle { position: fixed; top: 0; left: 0; opacity: 0; pointer-events: none; }
.toc-btn {
  position: fixed; left: 1.1rem; bottom: 1.4rem; z-index: 40;
  display: inline-flex; align-items: center; gap: .35rem;
  padding: .55rem 1.05rem; border-radius: 999px; cursor: pointer;
  background: var(--accent); color: #fff; font-size: .9rem; font-weight: 700;
  box-shadow: 0 4px 16px rgba(20, 60, 50, .28); user-select: none;
  transition: transform .15s ease, box-shadow .15s ease;
}
.toc-btn:hover { box-shadow: 0 6px 22px rgba(20, 60, 50, .34); }
.toc-btn span[aria-hidden] { font-size: 1.05rem; line-height: 1; }
.global-nav {
  position: fixed; top: 0; left: 0; bottom: 0; z-index: 35;
  width: min(19.5rem, 84vw); padding: 1.4rem .9rem 4rem;
  background: var(--surface); border-right: 1px solid var(--line);
  overflow-y: auto; overscroll-behavior: contain;
  transform: translateX(-102%); transition: transform .26s ease;
  box-shadow: 0 0 0 rgba(0,0,0,0);
}
.toc-toggle:checked ~ .global-nav {
  transform: none; box-shadow: 12px 0 40px rgba(20, 40, 34, .18);
}
.toc-toggle:focus-visible ~ .toc-btn { outline: 3px solid #d06b36; outline-offset: 3px; }
.nav-heading {
  display: flex; align-items: center; justify-content: space-between; gap: .6rem;
  margin: 0 0 1rem; padding: 0 .4rem; color: var(--muted);
  font-size: .8rem; font-weight: 800; letter-spacing: .08em;
}
.toc-close {
  display: none; cursor: pointer; padding: .15rem .45rem; border-radius: 6px;
  color: var(--muted); font-size: .95rem; letter-spacing: 0;
}
.toc-close:hover { background: var(--accent-pale); color: var(--accent-strong); }
.chapter-list, .chapter-list ol { margin: 0; padding: 0; list-style: none; }
.chapter-list { font-size: .88rem; }
.chapter-list > li { margin: .1rem 0; }
.chapter-list a {
  display: block; padding: .42rem .6rem; border-radius: 7px;
  color: var(--ink); text-decoration: none; line-height: 1.45;
}
.chapter-list a:hover { background: var(--accent-pale); color: var(--accent-strong); }
.chapter-list a[aria-current="page"] { background: var(--accent-pale); color: var(--accent-strong); font-weight: 700; }
.chapter-list .toc-chapter { position: relative; }
/* 章标题让出左侧 1.9rem 给折叠按钮；无子目录的章（引言/后记）也留同样缩进，保证左边缘对齐 */
.chapter-list .toc-chapter > a { font-weight: 700; padding-left: 1.9rem; }
.chapter-list .toc-chapter > a[aria-current="page"] { font-weight: 800; }
/* 折叠按钮：每章一个独立开关（纯 CSS :checked 驱动，无 JS）。按钮绝对定位在整行左侧、
   不占布局空间；因为整个目录是 fixed/sticky 容器，聚焦它也不会像 toc-toggle 那样把整页拉回页首。 */
.toc-branch-toggle { position: absolute; top: .5rem; left: .12rem; width: 1.5rem; height: 1.6rem; margin: 0; opacity: 0; pointer-events: none; }
.toc-branch-btn {
  position: absolute; top: .5rem; left: .12rem; width: 1.5rem; height: 1.6rem;
  display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: .72rem; line-height: 1; cursor: pointer;
  border-radius: 6px; user-select: none;
}
.toc-branch-btn:hover { background: var(--accent-pale); color: var(--accent-strong); }
.toc-branch-btn .toc-caret { display: inline-block; transition: transform .15s ease; }
.toc-branch-toggle:checked ~ .toc-branch-btn .toc-caret { transform: rotate(90deg); }
/* 子目录默认折叠，点按钮才展开；当前章在服务端默认展开（见 render_site.py） */
.chapter-list .toc-l2 { display: none; margin: .05rem 0 .35rem .45rem; padding-left: .55rem; border-left: 1px solid var(--line); }
.toc-branch-toggle:checked ~ .toc-l2 { display: block; }
.chapter-list .toc-l2 a { padding: .26rem .5rem; font-size: .82rem; color: #40544d; }
.chapter-list .toc-l3 { margin: .05rem 0 .2rem .35rem; padding-left: .5rem; border-left: 1px dashed var(--line); }
.chapter-list .toc-l3 a { padding: .2rem .45rem; font-size: .78rem; color: var(--muted); }
.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); }
.article-body { min-width: 0; }
/* 页面主标题（正文第一个 # 标题）：全页最大字 */
.article-body h1 {
  margin: 0 0 1.6rem; padding-bottom: 1rem; border-bottom: 2px solid var(--line);
  font-size: clamp(1.95rem, 5.2vw, 2.9rem); font-weight: 800;
  letter-spacing: -.03em; line-height: 1.25; scroll-margin-top: 1rem;
}
.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; text-indent: 2em; }
/* 图/表/提示框里的段落不缩进：缩进只属于正文自然段 */
.article-body li > p, .article-body blockquote p, .article-body blockquote,
.article-body figcaption, .article-body td p, .article-body th p,
.article-body .chapter-source, .article-body .chapter-goal { text-indent: 0; }
.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: 0 auto; border-radius: 8px; }
.article-body figure.book-figure {
  margin: 1.8rem 0 2rem; text-align: center;
}
.article-body figure.book-figure figcaption {
  margin: .7rem auto 0; max-width: 40rem; color: var(--muted);
  font-size: .84rem; line-height: 1.6; text-align: center;
}
.math, .math-display { max-width: 100%; }
.math-display { margin: 1.25rem 0; overflow-x: auto; overflow-y: hidden; padding: .5rem 0; }
/* 行内公式可能比正文列还宽。行内盒无法成为滚动容器（overflow 对 inline 不适用），
   所以由承载它的块级元素自己横滚：既不撑宽整页，也不动公式的基线与换行行为。 */
.article-body p, .article-body li, .article-body dd, .article-body dt,
.article-body td, .article-body th, .article-body summary,
.article-body h2, .article-body h3, .article-body h4 { overflow-x: auto; }
/* 交互演示（{{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 (min-width: 72rem) {
  .global-nav {
    position: sticky; top: 0; align-self: start;
    transform: none; height: 100vh; width: auto;
    border-right: 1px solid var(--line); background: transparent;
    box-shadow: none;
  }
  .toc-btn { display: none; }
  .site-layout {
    display: grid; grid-template-columns: minmax(14rem, 17rem) minmax(0, 1fr);
    gap: clamp(2rem, 5vw, 4.5rem); align-items: start;
    width: min(100%, 84rem); margin: 0 auto; padding: 0 1.8rem;
  }
  .page { width: auto; padding: 0; }
  .hero { width: min(100%, 84rem); margin: 0 auto; padding: 5rem 1.8rem 4rem; }
}

@media (max-width: 1151px) {
  .toc-close { display: inline-block; }
}

@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 { scrollbar-width: thin; }
}

@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;
}
