:root {
  --canvas: #f7f6f2;
  --surface: #ffffff;
  --surface-2: #fcfbf8;
  --ink: #252422;
  --muted: #6b6862;
  --faint: #9b968d;
  --line: rgba(37, 36, 34, .12);
  --line-strong: rgba(37, 36, 34, .2);
  --accent: #0f6b66;
  --accent-soft: #e5f2f0;
  --danger: #a33a3a;
  --warn: #9a5b25;
  --code: #efece5;
  --shadow: 0 10px 30px rgba(30, 25, 20, .06);
  --sans: "Segoe UI", "Microsoft JhengHei UI", "Microsoft JhengHei", "Noto Sans TC", sans-serif;
  --serif: Georgia, "PMingLiU", "Songti TC", serif;
  --mono: "Cascadia Code", "SFMono-Regular", Consolas, monospace;
}

[data-theme=dark] {
  --canvas: #171817;
  --surface: #20211f;
  --surface-2: #242522;
  --ink: #edeae3;
  --muted: #b8b3a9;
  --faint: #8a857d;
  --line: rgba(237, 234, 227, .14);
  --line-strong: rgba(237, 234, 227, .24);
  --accent: #59b8ad;
  --accent-soft: rgba(89, 184, 173, .13);
  --danger: #e08a8a;
  --warn: #d7a46a;
  --code: #2a2b28;
  --shadow: 0 10px 30px rgba(0, 0, 0, .22);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--canvas);
  color: var(--ink);
  font: 14px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
button, input { font: inherit; }
button { color: inherit; }
a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }
::selection { background: var(--accent-soft); color: var(--ink); }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
.skip-link {
  position: fixed; left: 12px; top: -60px; z-index: 100;
  background: var(--ink); color: var(--canvas); padding: 8px 14px; border-radius: 4px;
}
.skip-link:focus { top: 12px; }

.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 300px; flex: 0 0 300px; height: 100vh; position: sticky; top: 0;
  display: flex; flex-direction: column; gap: 12px; padding: 18px 14px 14px;
  background: var(--surface); border-right: 1px solid var(--line); z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 4px 4px 14px; }
.brand-mark {
  width: 38px; height: 38px; border-radius: 8px; display: grid; place-items: center;
  background: var(--ink); color: var(--canvas); font: 700 20px/1 var(--serif);
}
.brand-title { font-size: 15px; font-weight: 650; letter-spacing: .01em; }
.brand-sub { margin-top: 2px; font-size: 11.5px; color: var(--muted); }

.search-wrap { position: relative; }
.search-wrap input {
  width: 100%; height: 40px; padding: 0 40px 0 12px;
  border: 1px solid var(--line); border-radius: 6px;
  background: var(--canvas); color: var(--ink); outline: none;
}
.search-wrap input::placeholder { color: var(--faint); }
.search-wrap input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.search-kbd {
  position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
  padding: 1px 6px; border: 1px solid var(--line); border-radius: 4px;
  background: var(--surface); color: var(--faint); font: 11px/1.4 var(--mono);
}
.search-meta { min-height: 16px; font-size: 11.5px; color: var(--muted); padding: 0 4px; }
.search-results {
  max-height: 38vh; overflow-y: auto; display: flex; flex-direction: column; gap: 2px;
  padding-bottom: 8px; border-bottom: 1px solid var(--line);
}
.search-result {
  width: 100%; border: 0; background: transparent; text-align: left;
  padding: 9px 8px; border-radius: 5px; cursor: pointer;
}
.search-result:hover, .search-result:focus-visible { background: var(--canvas); outline: none; }
.result-head { display: flex; gap: 8px; align-items: baseline; }
.result-num { font: 11px var(--mono); color: var(--accent); }
.result-title { font-size: 12.5px; font-weight: 600; line-height: 1.35; }
.result-snippet {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden; margin-top: 3px; font-size: 11.5px; line-height: 1.5; color: var(--muted);
}

.chapter-list { flex: 1; overflow-y: auto; padding: 0 2px 0 0; scrollbar-width: thin; }
.chapter-list::-webkit-scrollbar { width: 8px; }
.chapter-list::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 8px; }
.chapter-link {
  display: flex; gap: 10px; align-items: flex-start; width: 100%;
  border: 0; border-radius: 5px; background: transparent; text-align: left;
  padding: 8px; cursor: pointer; line-height: 1.35;
}
.chapter-link:hover { background: var(--canvas); }
.chapter-link[aria-current=true] { background: var(--accent-soft); color: var(--accent); box-shadow: inset 2px 0 0 var(--accent); }
.chapter-num { min-width: 28px; padding-top: 1px; text-align: right; font: 11px var(--mono); color: var(--muted); }
.chapter-link[aria-current=true] .chapter-num { color: var(--accent); }
.chapter-name {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden; font-size: 12.5px; font-weight: 500;
}
.chapter-link.is-hidden { display: none; }
.sidebar-foot {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 4px 2px; border-top: 1px solid var(--line);
  font-size: 11px; color: var(--faint);
}
.sidebar-live { display: inline-flex; align-items: center; gap: 5px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 100vh; }
.topbar {
  position: sticky; top: 0; z-index: 10; height: 56px;
  display: flex; align-items: center; gap: 12px; padding: 0 28px;
  background: color-mix(in srgb, var(--canvas) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--line);
}
.topbar-title {
  flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: 13px; font-weight: 600; color: var(--muted);
}
.topbar-actions { display: flex; align-items: center; gap: 14px; }
.icon-btn {
  position: relative;
  width: 34px; height: 34px; display: grid; place-items: center;
  border: 1px solid var(--line); border-radius: 6px; background: var(--surface);
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); border-color: var(--line-strong); }
.icon-btn:active { transform: scale(.97); }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:disabled { opacity: .45; cursor: not-allowed; }
.mark-btn[data-count]:not([data-count="0"])::after {
  content: attr(data-count);
  position: absolute; top: -5px; right: -5px;
  min-width: 15px; height: 15px; padding: 0 3px;
  border-radius: 8px; background: var(--accent); color: var(--surface);
  font: 600 9px/15px var(--mono); text-align: center;
}
.menu-btn { display: none; }
@media (min-width: 981px) {
  body.sidebar-collapsed .sidebar { display: none; }
}
@media (max-width: 980px) {
  .sidebar-btn { display: none; }
}
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 11.5px; color: var(--muted); white-space: nowrap; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); display: inline-block; }
.dot-small { width: 6px; height: 6px; }
.status.online .dot { background: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.status.warn .dot { background: var(--warn); }
.status.offline .dot { background: var(--danger); }

.article {
  width: min(860px, 100%); margin: 0 auto; padding: 44px 32px 64px;
  outline: none; animation: article-in .22s ease-out both;
}
@keyframes article-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.article-header { margin-bottom: 28px; padding-bottom: 20px; border-bottom: 1px solid var(--line); }
.article-kicker { display: flex; align-items: center; gap: 8px; margin-bottom: 9px; font: 11px var(--mono); color: var(--accent); letter-spacing: .06em; text-transform: uppercase; }
.article h1 {
  margin: 0 0 10px; font: 650 30px/1.25 var(--sans); letter-spacing: -.02em;
}
.article-meta { font-size: 12px; color: var(--muted); }
.article-meta span + span::before { content: "·"; margin: 0 7px; color: var(--faint); }
.article h2 {
  margin: 2.1em 0 .65em; font: 650 24px/1.35 var(--sans); letter-spacing: -.01em;
  scroll-margin-top: 70px;
}
.article h3 { margin: 1.7em 0 .55em; font: 650 19px/1.45 var(--sans); scroll-margin-top: 70px; }
.article h4 { margin: 1.4em 0 .4em; font-size: 16px; }
.article p { margin: 0 0 1.05em; font-size: 17px; line-height: 1.85; }
.article strong { font-weight: 700; color: var(--ink); }
.article em { font-style: italic; }
.article ul, .article ol { margin: 0 0 1.2em; padding-left: 1.45em; }
.article li { margin: .35em 0; font-size: 17px; line-height: 1.8; }
.article li::marker { color: var(--faint); }
.article hr { border: 0; border-top: 1px solid var(--line); margin: 2em 0; }
.article blockquote {
  margin: 1.2em 0; padding: 2px 0 2px 16px; border-left: 2px solid var(--accent);
  color: var(--muted);
}
.article code {
  padding: .12em .35em; border-radius: 4px; background: var(--code);
  font: 12px var(--mono);
}
mark.user-mark {
  padding: 0 1px; border-radius: 2px;
  background: #f1d36b; color: inherit;
  box-decoration-break: clone; -webkit-box-decoration-break: clone;
  cursor: pointer;
}
mark.user-mark:hover { background: #e8c34c; }
[data-theme=dark] mark.user-mark { background: #806a24; color: #f4edcf; }
[data-theme=dark] mark.user-mark:hover { background: #9a7f2c; }
mark.user-mark.mark-hit { animation: mark-hit 1s ease-out; }
@keyframes mark-hit {
  0% { box-shadow: 0 0 0 3px var(--accent-soft); }
  100% { box-shadow: 0 0 0 0 transparent; }
}

.table-wrap {
  margin: 1.2em 0; overflow-x: auto; border: 1px solid var(--line);
  border-radius: 8px; background: var(--surface);
}
.article table { width: 100%; border-collapse: collapse; font-size: 13px; line-height: 1.55; }
.article thead { background: var(--surface-2); }
.article th, .article td {
  padding: 8px 10px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
  text-align: left; vertical-align: top;
}
.article th { font-weight: 650; white-space: nowrap; }
.article tr:last-child td { border-bottom: 0; }
.article th:last-child, .article td:last-child { border-right: 0; }
.article tbody tr:nth-child(even) { background: color-mix(in srgb, var(--canvas) 45%, transparent); }

.note-figure {
  margin: 1.4em 0 1.6em; padding: 8px; border: 1px solid var(--line);
  border-radius: 10px; background: var(--surface); text-align: center;
}
.note-figure a { display: block; }
.note-figure img { display: block; max-width: 100%; height: auto; margin: 0 auto; border-radius: 6px; }
.note-figure figcaption { margin-top: 8px; font-size: 12px; color: var(--muted); }
.fig-appendix { margin-top: 2.4em; padding-top: 1.4em; border-top: 1px solid var(--line); }

.pager {
  width: min(860px, 100%); margin: 0 auto; padding: 0 32px 56px;
  display: flex; justify-content: space-between; gap: 12px;
}
.pager button {
  max-width: 46%; padding: 10px 14px; border: 1px solid var(--line);
  border-radius: 6px; background: var(--surface); cursor: pointer; text-align: left;
}
.pager button:hover { border-color: var(--line-strong); background: var(--surface-2); }
.pager button.next { text-align: right; margin-left: auto; }
.pager .pager-label { display: block; font-size: 10.5px; color: var(--faint); text-transform: uppercase; letter-spacing: .06em; }
.pager .pager-title { display: block; margin-top: 3px; font-size: 12.5px; font-weight: 500; }

.loading { padding-top: 28px; }
.loading-line { display: block; height: 14px; margin-bottom: 14px; border-radius: 4px; background: linear-gradient(90deg, var(--surface-2), var(--canvas), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.4s infinite linear; }
.loading-line.short { width: 62%; }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

.error-box { margin-top: 24px; padding: 18px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.error-box strong { display: block; margin-bottom: 6px; color: var(--danger); }
.error-box code { font: 12px var(--mono); }

.tts-panel {
  position: fixed; left: 50%; bottom: 70px; z-index: 46;
  width: min(760px, calc(100vw - 32px)); transform: translateX(-50%);
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: 11px 14px; border: 1px solid var(--line-strong); border-radius: 8px;
  background: var(--surface); box-shadow: var(--shadow);
}
.tts-info { flex: 1 1 150px; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.tts-info strong { font-size: 12.5px; font-weight: 650; }
.tts-info span { font-size: 11.5px; color: var(--muted); }
.tts-controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.tts-btn {
  height: 30px; padding: 0 11px; border: 1px solid var(--line); border-radius: 5px;
  background: var(--canvas); color: var(--ink); font-size: 12px; cursor: pointer;
}
.tts-btn:hover { border-color: var(--line-strong); background: var(--surface-2); }
.tts-btn:disabled { opacity: .45; cursor: not-allowed; }
.tts-field { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; color: var(--muted); white-space: nowrap; }
.tts-field select {
  height: 30px; max-width: 170px; padding: 0 5px; border: 1px solid var(--line);
  border-radius: 5px; background: var(--canvas); color: var(--ink); font-size: 12px;
}
.tts-field input[type="range"] { width: 92px; accent-color: var(--accent); }
.tts-field output { min-width: 24px; font: 11px var(--mono); color: var(--faint); }

.marks-panel {
  position: fixed; top: 64px; right: 24px; z-index: 44;
  width: min(360px, calc(100vw - 32px)); max-height: min(70vh, 540px);
  overflow-y: auto; padding: 12px; border: 1px solid var(--line-strong);
  border-radius: 8px; background: var(--surface); box-shadow: var(--shadow);
}
.marks-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding-bottom: 8px; border-bottom: 1px solid var(--line); }
.marks-head strong { font-size: 12.5px; font-weight: 650; }
.marks-close {
  border: 0; background: transparent; color: var(--muted); font-size: 11.5px;
  cursor: pointer; padding: 2px 4px; border-radius: 4px;
}
.marks-close:hover { background: var(--canvas); color: var(--ink); }
.marks-list { display: flex; flex-direction: column; gap: 7px; padding-top: 8px; }
.mark-item { display: flex; align-items: flex-start; gap: 6px; padding: 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface-2); }
.mark-jump {
  flex: 1; min-width: 0; border: 0; background: transparent; color: var(--ink);
  text-align: left; font-size: 12px; line-height: 1.55; cursor: pointer;
}
.mark-jump:hover { color: var(--accent); }
.mark-del {
  flex: none; border: 0; background: transparent; color: var(--faint);
  font-size: 10.5px; cursor: pointer; padding: 1px 3px; border-radius: 3px;
}
.mark-del:hover { color: var(--danger); background: var(--canvas); }
.marks-empty { padding: 14px 4px 6px; color: var(--faint); font-size: 12px; line-height: 1.6; }

.toast {
  position: fixed; left: 50%; bottom: 24px; z-index: 50;
  transform: translate(-50%, 12px); opacity: 0; pointer-events: none;
  padding: 9px 14px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--ink); color: var(--canvas); font-size: 12.5px;
  box-shadow: var(--shadow); transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.offline-notice {
  position: fixed; inset: auto 16px 16px; z-index: 80; max-width: 620px; margin: 0 auto;
  padding: 14px 16px; border: 1px solid var(--line-strong); border-radius: 8px;
  background: var(--surface); box-shadow: var(--shadow); font-size: 13px;
}
.offline-notice strong { display: block; margin-bottom: 3px; }
.offline-notice code { font: 12px var(--mono); }

.overlay { display: none; }
@media (max-width: 980px) {
  .sidebar {
    position: fixed; left: 0; top: 0; transform: translateX(-102%);
    transition: transform .22s ease; box-shadow: var(--shadow);
  }
  body.nav-open .sidebar { transform: translateX(0); }
  body.nav-open .overlay {
    display: block; position: fixed; inset: 0; z-index: 15;
    background: rgba(20, 18, 16, .28); backdrop-filter: blur(2px);
  }
  .menu-btn { display: grid; }
  .topbar { padding: 0 18px; }
  .article { padding: 34px 24px 52px; }
  .pager { padding: 0 24px 44px; }
}
@media (max-width: 640px) {
  .topbar-title { display: none; }
  .status span:last-child { display: none; }
  .tts-panel { bottom: 64px; flex-direction: column; align-items: stretch; gap: 7px; padding: 10px 12px; }
  .tts-info { flex: none; }
  .tts-controls { gap: 6px; }
  .tts-field select { max-width: 128px; }
  .tts-field input[type="range"] { width: 76px; }
  .marks-panel { top: 58px; right: 12px; left: 12px; width: auto; }
  .article h1 { font-size: 26px; }
  .article p, .article li { font-size: 16px; line-height: 1.8; }
  .article h2 { font-size: 21px; }
  .article h3 { font-size: 18px; }
  .article { padding: 26px 20px 44px; }
  .pager { padding: 0 20px 36px; }
  .pager button { max-width: 48%; padding: 9px 10px; }
  .pager .pager-title { font-size: 11.5px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
