:root {
  --body-size: 17.5px;
  /* paper (light) */
  --bg: #ece3cd;          /* desk around the page */
  --page: #f8f2e2;        /* paper */
  --text: #3a3226;
  --text-muted: #857961;
  --border: #d9cdad;
  --accent: #1f6a60;      /* deep teal ink */
  --accent-soft: #e2ece6;
  --code-bg: #efe7d1;
  --topbar-bg: #f4ecd9f2;
  --shadow: 0 1px 3px rgba(90, 70, 40, 0.14), 0 8px 28px rgba(90, 70, 40, 0.09);
  --hl-comment: #96896c;
  --hl-keyword: #a8442a;
  --hl-string: #4a7340;
  --hl-number: #9c5d1f;
  --hl-func: #1f6a60;
  --hl-builtin: #6f5aa0;
  --hl-var: #8a5a2b;
  --hl-punct: #6b6353;
}
html.dark {
  --bg: #16130e;
  --page: #201c14;
  --text: #d8cfba;
  --text-muted: #938870;
  --border: #383021;
  --accent: #6bb7ab;
  --accent-soft: #1a241f;
  --code-bg: #191510;
  --topbar-bg: #1b1710f2;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 8px 28px rgba(0, 0, 0, 0.3);
  --hl-comment: #7d7157;
  --hl-keyword: #e08a63;
  --hl-string: #9fc27f;
  --hl-number: #d9a05b;
  --hl-func: #6bb7ab;
  --hl-builtin: #b49ad6;
  --hl-var: #d3a679;
  --hl-punct: #a49a84;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Georgia, "Times New Roman", "Noto Serif SC", "Source Han Serif SC",
    "Songti SC", STSong, SimSun, serif;
  font-size: var(--body-size);
  line-height: 2;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* UI chrome uses sans */
.topbar, .sidebar, .pager, .card, .account, .resume-btn, .cover .meta, .license, .card-pct {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Noto Sans CJK SC", "Microsoft YaHei", sans-serif;
}

.mobile-only { display: none !important; }

/* top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 20px 10px;
  background: var(--topbar-bg);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.topbar .home { color: var(--text); font-weight: 600; }
.topbar .chapter-label { color: var(--text-muted); }
.spacer { flex: 1; }
.topbar button {
  border: 1px solid var(--border);
  background: var(--page);
  color: var(--text);
  border-radius: 6px;
  padding: 3px 10px;
  font-size: 13px;
  cursor: pointer;
}
.topbar button:hover { border-color: var(--accent); color: var(--accent); }

#progress {
  position: fixed;
  top: 0;
  left: 0;
  height: 2px;
  width: 0;
  background: var(--accent);
  z-index: 30;
}

/* layout */
.layout {
  display: flex;
  max-width: 1240px;
  margin: 0 auto;
  align-items: flex-start;
}
.sidebar {
  position: sticky;
  top: 49px;
  width: 272px;
  flex-shrink: 0;
  height: calc(100vh - 49px);
  display: flex;
  flex-direction: column;
  padding: 16px 6px 16px 20px;
  font-size: 13px;
  line-height: 1.7;
}

/* tabs: 本章目录 / 全书目录 */
.side-tabs {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
  padding: 3px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--code-bg);
}
.side-tab {
  flex: 1;
  border: none;
  background: none;
  color: var(--text-muted);
  font: inherit;
  font-size: 12.5px;
  padding: 5px 8px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.side-tab:hover { color: var(--text); }
.side-tab.is-on {
  background: var(--page);
  color: var(--accent);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

/* panes — only the active one shows, and only it scrolls */
.side-pane {
  display: none;
  flex: 1;
  min-height: 0;          /* required, else the flex child refuses to shrink */
}
.side-pane.is-on { display: flex; flex-direction: column; }
#pane-toc { overflow-y: auto; padding-right: 8px; padding-bottom: 24px; display: block !important; }
#pane-toc:not(.is-on) { display: none !important; }
.side-empty { color: var(--text-muted); font-size: 12px; padding: 6px; }

/* the list scrolls; the filter box stays put */
.nav-groups {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding-right: 8px;
  padding-bottom: 24px;
}

/* slim, unobtrusive scrollbar */
.scroller { scrollbar-width: thin; scrollbar-color: transparent transparent; }
.scroller:hover { scrollbar-color: var(--border) transparent; }
.scroller::-webkit-scrollbar { width: 8px; }
.scroller::-webkit-scrollbar-track { background: transparent; }
.scroller::-webkit-scrollbar-thumb {
  background: transparent;
  border-radius: 99px;
  border: 2px solid transparent;
  background-clip: content-box;
}
.scroller:hover::-webkit-scrollbar-thumb { background: var(--border); background-clip: content-box; }
.scroller::-webkit-scrollbar-thumb:hover { background: var(--text-muted); background-clip: content-box; }

/* in-chapter TOC */
.sidebar ul { list-style: none; margin: 0; padding-left: 0; }
.sidebar li { margin: 1px 0; }
#pane-toc > ul > li > a { font-weight: 500; color: var(--text); }
#pane-toc ul ul { padding-left: 12px; border-left: 1px solid var(--border); margin-left: 6px; }
#pane-toc ul ul a { font-size: 12.5px; }
.sidebar a {
  color: var(--text-muted);
  display: block;
  padding: 3px 8px;
  border-radius: 5px;
  transition: background 0.12s, color 0.12s;
}
.sidebar a:hover { color: var(--accent); background: var(--accent-soft); text-decoration: none; }
.sidebar a.active {
  color: var(--accent);
  background: var(--accent-soft);
  font-weight: 600;
}

/* full-book nav: collapsible groups */
.nav-filter { flex-shrink: 0; padding: 0 8px 8px 0; }
.nav-filter input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--page);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 10px;
  font: inherit;
  font-size: 12.5px;
}
.nav-filter input:focus { outline: none; border-color: var(--accent); }
.nav-group { margin-bottom: 4px; }
.nav-group > summary {
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 5px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: 0.02em;
  position: sticky;        /* keep the Part label visible while scrolling it */
  top: 0;
  z-index: 1;
  background: var(--bg);
  border-bottom: 1px solid transparent;
}
.nav-group[open] > summary { border-bottom-color: var(--border); }
.nav-group > summary::-webkit-details-marker { display: none; }
.nav-group > summary::before {
  content: "›";
  display: inline-block;
  width: 10px;
  flex-shrink: 0;
  color: var(--text-muted);
  transition: transform 0.15s;
}
.nav-group[open] > summary::before { transform: rotate(90deg); }
.nav-group > summary:hover { background: var(--accent-soft); color: var(--accent); }
.nav-group .cnt {
  margin-left: auto;
  font-size: 11px;
  font-weight: 400;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}
.book-nav { padding-left: 4px !important; margin-bottom: 8px; }
.book-nav a {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 3px 8px;
}
.book-nav .n {
  flex-shrink: 0;
  min-width: 22px;
  text-align: right;
  font-size: 11px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}
.book-nav .t {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.book-nav a.cur {
  color: var(--accent);
  background: var(--accent-soft);
  font-weight: 600;
}
.book-nav a.cur .n { color: var(--accent); }

/* the "page" */
.content {
  flex: 1;
  min-width: 0;
  padding: 36px 40px 80px;
}
article {
  max-width: 780px;
  margin: 0 auto;
  background: var(--page);
  border: 1px solid var(--border);
  border-radius: 4px;
  box-shadow: var(--shadow);
  padding: 56px 64px 64px;
}

/* typography */
article h1 {
  font-size: 1.65em;
  line-height: 1.5;
  margin: 0 0 1.2em;
  text-align: center;
  letter-spacing: 0.02em;
}
article h1::after {
  content: "";
  display: block;
  width: 48px;
  height: 2px;
  background: var(--accent);
  margin: 0.7em auto 0;
}
article h2 {
  font-size: 1.32em;
  line-height: 1.5;
  margin: 2.4em 0 0.8em;
  padding-top: 0.8em;
  border-top: 1px solid var(--border);
}
article h3 { font-size: 1.12em; margin: 1.8em 0 0.6em; }
article h4 { font-size: 1em; margin: 1.5em 0 0.5em; }
article p { margin: 0.9em 0; text-align: justify; }
article img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 1.6em auto;
}
html.dark article img { background: #f4ecd9; border-radius: 6px; padding: 8px; }
article blockquote {
  margin: 1.2em 0;
  padding: 2px 20px;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 0 6px 6px 0;
  color: var(--text-muted);
}
article code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85em;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.1em 0.35em;
}
article pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 14px 16px;
  overflow-x: auto;
  line-height: 1.6;
}
article pre code { background: none; border: none; padding: 0; font-size: 0.82em; }

/* syntax highlighting — Pygments tokens, warm palette tuned to the paper theme */
.hl .c, .hl .ch, .hl .cm, .hl .c1, .hl .cs, .hl .cp, .hl .cpf {
  color: var(--hl-comment);
  font-style: italic;
}
.hl .k, .hl .kc, .hl .kd, .hl .kn, .hl .kp, .hl .kr, .hl .kt { color: var(--hl-keyword); }
.hl .s, .hl .sa, .hl .sb, .hl .sc, .hl .dl, .hl .sd, .hl .s2, .hl .se,
.hl .sh, .hl .si, .hl .sx, .hl .sr, .hl .s1, .hl .ss { color: var(--hl-string); }
.hl .m, .hl .mb, .hl .mf, .hl .mh, .hl .mi, .hl .mo, .hl .il { color: var(--hl-number); }
.hl .nf, .hl .fm, .hl .nc, .hl .ne { color: var(--hl-func); }
.hl .nb, .hl .bp, .hl .no, .hl .nn, .hl .nt { color: var(--hl-builtin); }
.hl .nv, .hl .vc, .hl .vg, .hl .vi, .hl .vm, .hl .na { color: var(--hl-var); }
.hl .o, .hl .ow, .hl .p { color: var(--hl-punct); }
.hl .gd { color: #b3403a; }
.hl .gi { color: #3f7a4f; }
.hl .gh, .hl .gu { color: var(--hl-func); font-weight: 600; }
.hl .err { color: inherit; }
.table-wrap { overflow-x: auto; margin: 1.4em 0; }
article table {
  border-collapse: collapse;
  font-size: 0.88em;
  line-height: 1.7;
  min-width: 60%;
}
article th, article td {
  border: 1px solid var(--border);
  padding: 7px 12px;
  text-align: left;
  vertical-align: top;
}
article th { background: var(--code-bg); }
article hr { border: none; border-top: 1px solid var(--border); margin: 2.5em 0; }
.footnote { font-size: 0.85em; color: var(--text-muted); margin-top: 3em; }
.footnote hr { margin: 1em 0; }
.katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }

/* pager */
.pager {
  display: flex;
  max-width: 780px;
  margin: 2.5em auto 0;
  gap: 12px;
}
.nav-btn {
  border: 1px solid var(--border);
  background: var(--page);
  border-radius: 6px;
  padding: 10px 18px;
  color: var(--text);
  font-size: 14px;
}
.nav-btn:hover { border-color: var(--accent); color: var(--accent); text-decoration: none; }

/* cover / index */
.cover { max-width: 920px; margin: 0 auto; padding: 56px 24px 80px; }
.cover h1 { font-size: 2.3em; margin: 0 0 6px; letter-spacing: 0.03em; }
.cover .subtitle { color: var(--text-muted); font-size: 1.05em; margin: 0 0 8px; }
.cover .meta { font-size: 14px; color: var(--text-muted); margin-bottom: 20px; }

/* account box */
.account {
  border: 1px solid var(--border);
  background: var(--page);
  border-radius: 8px;
  padding: 14px 18px;
  margin-bottom: 20px;
  font-size: 14px;
  box-shadow: var(--shadow);
}
.account .hint { color: var(--text-muted); }
.account form { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.account input {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 14px;
  min-width: 0;
}
.account input:focus { outline: none; border-color: var(--accent); }
.account button, .account .linklike {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  padding: 8px 16px;
  font-size: 14px;
  cursor: pointer;
}
.account button.ghost {
  background: transparent;
  color: var(--accent);
}
.account button:disabled { opacity: 0.55; cursor: default; }
.account .msg { margin-top: 8px; font-size: 13px; color: var(--text-muted); }
.account .msg.err { color: #b3403a; }

#resume-slot { margin-bottom: 24px; }
.resume-btn {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  padding: 12px 22px;
  font-size: 15px;
  font-weight: 500;
}
.resume-btn:hover { text-decoration: none; opacity: 0.92; }

.cover .section {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Noto Sans CJK SC", "Microsoft YaHei", sans-serif;
  font-size: 0.95em;
  font-weight: 600;
  letter-spacing: 0.03em;
  color: var(--accent);
  margin: 34px 0 12px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}
.cover .section:first-of-type { margin-top: 26px; }
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(255px, 1fr));
  gap: 12px;
}

/* mermaid diagrams */
.mermaid {
  margin: 1.6em auto;
  text-align: center;
  overflow-x: auto;
}
.card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px 18px;
  color: var(--text);
  background: var(--page);
  box-shadow: var(--shadow);
}
.card:hover { border-color: var(--accent); text-decoration: none; }
.card-label { font-size: 12px; color: var(--text-muted); }
.card-title { font-weight: 600; line-height: 1.5; }
.card-pct { font-size: 12px; color: var(--accent); min-height: 1em; }
.license { margin-top: 40px; font-size: 12px; color: var(--text-muted); }

/* full-book search overlay */
.search-btn { white-space: nowrap; }
html.search-open { overflow: hidden; }
/* must beat the `display:flex` below, or the hidden overlay eats every click */
#search-overlay[hidden] { display: none; }
#search-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(30, 22, 10, 0.42);
  backdrop-filter: blur(2px);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 8vh 16px 16px;
}
html.dark #search-overlay { background: rgba(0, 0, 0, 0.6); }
.search-panel {
  width: 100%;
  max-width: 660px;
  max-height: 78vh;
  display: flex;
  flex-direction: column;
  background: var(--page);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Noto Sans CJK SC", "Microsoft YaHei", sans-serif;
}
.search-head { display: flex; align-items: center; gap: 8px; padding: 12px 14px; border-bottom: 1px solid var(--border); }
#search-input {
  flex: 1;
  min-width: 0;
  border: none;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 16px;
  padding: 4px 2px;
}
#search-input:focus { outline: none; }
#search-close {
  flex-shrink: 0;
  border: 1px solid var(--border);
  background: var(--code-bg);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 3px 9px;
  font: inherit;
  font-size: 11px;
  cursor: pointer;
}
#search-close:hover { border-color: var(--accent); color: var(--accent); }
.search-meta {
  flex-shrink: 0;
  padding: 7px 14px;
  font-size: 12px;
  color: var(--text-muted);
  background: var(--code-bg);
  border-bottom: 1px solid var(--border);
}
.search-results { flex: 1; min-height: 0; overflow-y: auto; padding: 6px; }
a.sr {
  display: block;
  padding: 9px 11px;
  border-radius: 8px;
  color: var(--text);
  line-height: 1.6;
}
a.sr:hover, a.sr.on { background: var(--accent-soft); text-decoration: none; }
.sr-top { display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: baseline; }
.sr-ch { font-size: 11.5px; color: var(--accent); font-weight: 600; }
.sr-h { font-size: 13px; font-weight: 600; }
.sr-x { display: block; font-size: 12.5px; color: var(--text-muted); margin-top: 3px; }
.sr-x mark, .search-results mark {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
  padding: 0 1px;
  border-radius: 2px;
}
@media (max-width: 900px) {
  #search-overlay { padding: 0; }
  .search-panel { max-width: none; max-height: none; height: 100%; border-radius: 0; border: none; }
  .search-head { padding-top: calc(12px + env(safe-area-inset-top)); }
}

/* drawer backdrop + flip hint (mobile) */
#drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(30, 22, 10, 0.45);
  z-index: 39;
}
#drawer-backdrop.show { display: block; }
.flip-hint {
  position: fixed;
  left: 50%;
  bottom: calc(28px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  font-size: 13px;
  padding: 9px 16px;
  border-radius: 20px;
  z-index: 50;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.4s;
  pointer-events: none;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", sans-serif;
}
.flip-hint.show { opacity: 0.92; }

/* mobile */
@media (max-width: 900px) {
  .mobile-only { display: inline-block !important; }
  /* sidebar becomes a slide-in drawer */
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: 84%;
    max-width: 330px;
    height: auto;            /* top/bottom pinning defines the height */
    max-height: none;
    z-index: 40;
    background: var(--page);
    border-right: 1px solid var(--border);
    box-shadow: var(--shadow);
    padding: calc(14px + env(safe-area-inset-top)) 10px calc(14px + env(safe-area-inset-bottom)) calc(14px + env(safe-area-inset-left));
    transform: translateX(-105%);
    transition: transform 0.25s ease;
    overflow: hidden;        /* the active pane scrolls, not the drawer */
    -webkit-overflow-scrolling: touch;
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar a { padding: 7px 8px; }
  .scroller { -webkit-overflow-scrolling: touch; }
  /* touch devices have no hover — keep the thumb visible */
  .scroller { scrollbar-color: var(--border) transparent; }
  .scroller::-webkit-scrollbar-thumb { background: var(--border); background-clip: content-box; }
  /* drawer sits on --page, so sticky headers must match it */
  .nav-group > summary { background: var(--page); }
  .content { padding: 0 0 calc(70px + env(safe-area-inset-bottom)); }
  article {
    border: none;
    border-radius: 0;
    box-shadow: none;
    padding: 28px 20px 48px;
    max-width: none;
  }
  body.reading { background: var(--page); }
  .pager { padding: 0 16px; }
  .topbar { padding: calc(8px + env(safe-area-inset-top)) 14px 8px; }
  .topbar button { padding: 5px 12px; }
  #toc-btn { font-size: 15px; }
  :root { --body-size: 17px; }
}
