/* ---------- 主题 ---------- */
:root {
  --bg: #f6f3ec;
  --bg-veil: radial-gradient(1100px 520px at 50% -12%, #fffefb 0%, rgba(255, 254, 251, 0) 62%);
  --card: #fffefb;
  --ink: #26231e;
  --ink-soft: #4b453b;
  --muted: #8b8275;
  --line: #e9e3d7;
  --line-strong: #d5ccbb;
  --accent: #a8442f;
  --accent-ink: #fffdf9;
  --accent-soft: rgba(168, 68, 47, .07);
  --answer-bg: rgba(168, 68, 47, .12);
  --accent-glow: rgba(168, 68, 47, .22);
  --shadow: 0 1px 2px rgba(54, 44, 30, .04), 0 20px 44px -26px rgba(54, 44, 30, .30);
  --serif: "Noto Serif SC", "Source Han Serif SC", "Songti SC", "STSong", "SimSun", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", "Segoe UI", sans-serif;
  color-scheme: light;
}

[data-theme="dark"] {
  --bg: #161512;
  --bg-veil: radial-gradient(1100px 520px at 50% -12%, #221f19 0%, rgba(34, 31, 25, 0) 62%);
  --card: #201e19;
  --ink: #eae5d9;
  --ink-soft: #cfc7b8;
  --muted: #968d7e;
  --line: #322e27;
  --line-strong: #474237;
  --accent: #d98a72;
  --accent-ink: #1a1512;
  --accent-soft: rgba(217, 138, 114, .09);
  --answer-bg: rgba(217, 138, 114, .14);
  --accent-glow: rgba(217, 138, 114, .18);
  --shadow: 0 1px 2px rgba(0, 0, 0, .5), 0 24px 48px -28px rgba(0, 0, 0, .8);
  color-scheme: dark;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background-color: var(--bg);
  background-image: var(--bg-veil);
  background-repeat: no-repeat;
  color: var(--ink);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button { font: inherit; color: inherit; }
button:focus-visible, input:focus-visible, .blk:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

::selection { background: var(--accent-soft); color: var(--ink); }

/* ---------- 顶栏 ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px max(20px, env(safe-area-inset-left)) 14px max(20px, env(safe-area-inset-right));
  background: color-mix(in srgb, var(--bg) 84%, transparent);
  -webkit-backdrop-filter: saturate(140%) blur(14px);
  backdrop-filter: saturate(140%) blur(14px);
  border-bottom: 1px solid transparent;
  transition: border-color .2s ease;
}
body.is-scrolled .topbar { border-bottom-color: var(--line); }

.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }

.seal {
  width: 34px;
  height: 34px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-ink);
  font-family: var(--serif);
  font-size: 19px;
  font-weight: 600;
  letter-spacing: 0;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .18), 0 4px 10px -6px var(--accent-glow);
}

.brand-text { min-width: 0; }
.brand-text h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: .015em;
  white-space: nowrap;
}
.brand-text p {
  margin: 2px 0 0;
  font-size: 11.5px;
  color: var(--muted);
  letter-spacing: .02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top-actions { display: flex; align-items: center; gap: 4px; }

.counter {
  font-size: 12.5px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  letter-spacing: .02em;
  padding: 0 8px;
  white-space: nowrap;
}

.icon-btn {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
  transition: background .16s ease, color .16s ease;
}
.icon-btn:hover { background: var(--accent-soft); color: var(--ink); }
.icon-btn svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.i-sun { display: none; }
[data-theme="dark"] .i-sun { display: block; }
[data-theme="dark"] .i-moon { display: none; }

.progress {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 2px;
  z-index: 50;
  background: transparent;
  pointer-events: none;
}
.progress i {
  display: block;
  height: 100%;
  width: 0;
  background: var(--accent);
  opacity: .55;
  transition: width .3s cubic-bezier(.4, 0, .2, 1);
}

/* ---------- 学习区 ---------- */
.stage {
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: 34px 24px 96px;
}

.crumbs {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 7px;
  min-height: 18px;
  margin: 0 2px 13px;
  font-size: 12.5px;
  letter-spacing: .045em;
  color: var(--muted);
}
.crumbs .ch { color: var(--ink-soft); }
.crumbs .sep { opacity: .45; }
.crumbs .sec { color: var(--muted); }

.card {
  position: relative;
  padding: 42px 46px;
  min-height: 260px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--card);
  box-shadow: var(--shadow);
}

.card-body {
  font-family: var(--serif);
  font-size: 18px;
  line-height: 1.95;
  letter-spacing: .012em;
  color: var(--ink);
  word-break: break-word;
  overflow-wrap: anywhere;
}
.card-body div { margin: .16em 0; }
.card-body br { line-height: 1; }
.card-body b, .card-body strong { font-weight: 600; }
.card-body u { text-decoration-color: var(--accent); text-underline-offset: 3px; }
.card-body .hl { color: var(--accent); font-weight: 600; }
.card-body > div:first-child { margin-top: 0; }

/* 空位 */
.blk {
  display: inline-block;
  vertical-align: baseline;
  height: 1.04em;
  margin: 0 .16em;
  border: 0;
  border-bottom: 1.5px dotted var(--line-strong);
  border-radius: 4px 4px 0 0;
  background: var(--accent-soft);
  cursor: pointer;
  transition: background .16s ease, border-color .16s ease;
}
.blk:hover { background: var(--answer-bg); border-bottom-color: var(--accent); }
.blk.s  { width: 3.4em; }
.blk.m  { width: 5.6em; }
.blk.l  { width: 7.6em; }
.blk.xl { width: 9.6em; }

.blk.on {
  display: inline;
  height: auto;
  margin: 0;
  padding: 0 .08em;
  border: 0;
  border-radius: 3px;
  background: var(--answer-bg);
  color: var(--ink);
  font-weight: inherit;
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
}
.blk.on .hl { color: inherit; }

/* 按钮 */
.actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 26px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 22px;
  border-radius: 999px;
  border: 1px solid transparent;
  font-size: 14px;
  letter-spacing: .02em;
  cursor: pointer;
  transition: transform .14s ease, background .16s ease, border-color .16s ease, box-shadow .16s ease;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: translateY(1px); }

.btn.primary {
  background: var(--accent);
  color: var(--accent-ink);
  box-shadow: 0 8px 18px -12px var(--accent-glow);
}
.btn.primary:hover { box-shadow: 0 10px 22px -12px var(--accent-glow); filter: brightness(1.04); }

.btn.ghost {
  background: transparent;
  border-color: var(--line-strong);
  color: var(--ink-soft);
}
.btn.ghost:hover { border-color: var(--muted); background: var(--accent-soft); }

.grades { display: flex; gap: 10px; }

kbd {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 10.5px;
  line-height: 1;
  padding: 3px 5px;
  border-radius: 4px;
  border: 1px solid currentColor;
  opacity: .5;
}

.hint {
  margin: 18px 0 0;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  letter-spacing: .01em;
}
.hint kbd { font-size: 10px; padding: 2px 4px; }

/* 完成状态 */
.done {
  max-width: 460px;
  margin: 8vh auto 0;
  text-align: center;
}
.done-mark {
  width: 54px;
  height: 54px;
  margin: 0 auto 20px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  border: 1px solid var(--accent);
  color: var(--accent);
  font-size: 24px;
  font-family: var(--sans);
}
.done h2 {
  margin: 0;
  font-family: var(--serif);
  font-size: 22px;
  font-weight: 600;
  letter-spacing: .04em;
}
.done-text {
  margin: 12px 0 0;
  font-size: 13.5px;
  line-height: 1.9;
  color: var(--muted);
}
.done-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 26px;
}

/* ---------- 抽屉 ---------- */
.mask {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(28, 22, 14, .26);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  opacity: 0;
  transition: opacity .3s ease;
}
.mask.is-on { opacity: 1; }

.drawer {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  z-index: 90;
  width: min(420px, 92vw);
  display: flex;
  flex-direction: column;
  background: var(--card);
  border-left: 1px solid var(--line);
  box-shadow: -30px 0 60px -40px rgba(30, 22, 12, .55);
  transform: translateX(101%);
  transition: transform .34s cubic-bezier(.32, .72, .28, 1);
  visibility: hidden;
}
.drawer.is-open { transform: none; visibility: visible; }

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 14px 12px 22px;
  border-bottom: 1px solid var(--line);
}
.drawer-head h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: .06em;
}

.drawer-scroll {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px 18px 30px;
}

.search { position: relative; }
.search svg {
  position: absolute;
  left: 12px;
  top: 50%;
  width: 16px;
  height: 16px;
  transform: translateY(-50%);
  fill: none;
  stroke: var(--muted);
  stroke-width: 1.6;
  stroke-linecap: round;
  pointer-events: none;
}
.search input {
  width: 100%;
  padding: 10px 12px 10px 36px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 13.5px;
  outline: none;
  transition: border-color .16s ease, background .16s ease;
}
.search input::placeholder { color: var(--muted); }
.search input:focus { border-color: var(--line-strong); background: var(--card); }
.search input::-webkit-search-cancel-button { -webkit-appearance: none; }

.quick { margin: 14px 0 4px; display: grid; gap: 2px; }
.quick-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 11px 12px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  font-size: 13.5px;
  color: var(--ink);
  cursor: pointer;
  transition: background .14s ease;
}
.quick-row:hover { background: var(--accent-soft); }
.quick-n { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

.toc { margin-top: 10px; }

.toc-ch {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  margin-top: 14px;
  padding: 12px 8px 7px;
  border: 0;
  border-top: 1px solid var(--line);
  background: transparent;
  font-size: 12.5px;
  font-weight: 600;
  letter-spacing: .05em;
  color: var(--ink-soft);
  cursor: pointer;
  text-align: left;
}
.toc-ch:first-child { border-top: 0; margin-top: 0; }
.toc-ch .n { font-weight: 400; color: var(--muted); font-variant-numeric: tabular-nums; }

.toc-sec {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 8px 10px 8px 18px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  font-size: 13px;
  color: var(--ink-soft);
  cursor: pointer;
  text-align: left;
  transition: background .14s ease;
}
.toc-sec:hover { background: var(--accent-soft); }
.toc-sec .n { font-size: 11.5px; color: var(--muted); font-variant-numeric: tabular-nums; }

.results { margin-top: 12px; }
.res-head {
  font-size: 11.5px;
  color: var(--muted);
  letter-spacing: .04em;
  padding: 4px 10px 8px;
}
.res-item {
  display: block;
  width: 100%;
  padding: 11px 12px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  text-align: left;
  cursor: pointer;
  transition: background .14s ease;
}
.res-item:hover { background: var(--accent-soft); }
.res-item .where {
  display: block;
  font-size: 11px;
  color: var(--muted);
  letter-spacing: .03em;
  margin-bottom: 3px;
}
.res-item .snippet {
  display: block;
  font-family: var(--serif);
  font-size: 13.5px;
  line-height: 1.6;
  color: var(--ink-soft);
}
.res-empty {
  padding: 26px 10px;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}

.drawer-foot {
  margin-top: 26px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}
.foot-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.foot-label { font-size: 12.5px; color: var(--muted); }

.seg {
  display: inline-flex;
  gap: 2px;
  padding: 2px;
  border-radius: 9px;
  background: var(--bg);
}
.seg button {
  padding: 6px 11px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
  transition: background .14s ease, color .14s ease;
}
.seg button.is-on {
  background: var(--card);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .08);
}

.foot-stats {
  margin-top: 14px;
  font-size: 12px;
  line-height: 1.8;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.link-btn {
  margin-top: 10px;
  padding: 0;
  border: 0;
  background: none;
  font-size: 12px;
  color: var(--muted);
  text-decoration: underline;
  text-underline-offset: 3px;
  text-decoration-color: var(--line-strong);
  cursor: pointer;
}
.link-btn:hover { color: var(--accent); }

.foot-note {
  margin: 16px 0 0;
  font-size: 11.5px;
  line-height: 1.75;
  color: var(--muted);
  opacity: .8;
}

/* ---------- 移动端 ---------- */
@media (max-width: 720px) {
  .topbar { padding-top: 11px; padding-bottom: 11px; }
  .brand-text p { display: none; }
  .stage { padding: 22px 15px calc(88px + env(safe-area-inset-bottom)); }
  .card { padding: 26px 21px; min-height: 220px; border-radius: 15px; }
  .card-body { font-size: 16.5px; line-height: 1.9; }
  .crumbs { font-size: 11.5px; margin-bottom: 10px; }
  .actions { gap: 8px; }
  .btn { padding: 10px 16px; font-size: 13.5px; }
  .grades { flex: 1; }
  .grades .btn { flex: 1; }
  .actions > .btn.primary:only-child { flex: 1; }
  .hint kbd { display: none; }
  .counter { padding: 0 4px; font-size: 12px; }
  .blk.xl { width: 8.6em; }
  .blk.l { width: 6.8em; }
}

@media (max-width: 380px) {
  .blk.s { width: 3em; }
  .blk.m { width: 4.8em; }
  .blk.l { width: 6em; }
  .blk.xl { width: 7.4em; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    transition-duration: .001ms !important;
  }
}
