/* HKDSE 十二篇範文　溫書網站
   styles.css — 零依賴樣式，含深色模式與列印樣式 */

/* ── 變數 ─────────────────────────────────── */
:root {
  --bg:        #f7f5f0;
  --bg-alt:    #ffffff;
  --bg-sunk:   #efece4;
  --fg:        #1f1c18;
  --fg-soft:   #57514a;
  /* 同色相（原 #8c857b）壓暗，令三種底（--bg／--bg-alt／--bg-sunk）
     都過 WCAG AA 4.5:1：5.44／5.92／5.02（原 3.35／3.65／3.09）。 */
  --fg-faint:  #6a635a;
  --line:      #ddd8cc;
  --line-soft: #eae6dc;
  --accent:    #9a3b2a;
  --accent-2:  #1e6b5e;
  --accent-bg: #f3e9e5;
  --mark:      #ffe9a8;
  --mark-fg:   #4a3a00;
  --shadow:    0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.05);

  --font-scale: 1;
  --serif: "Noto Serif TC", "Source Han Serif TC", "Songti TC", "SimSun", "Times New Roman", serif;
  --sans:  "Noto Sans TC", "Source Han Sans TC", "PingFang TC", "Microsoft JhengHei", "Segoe UI", system-ui, sans-serif;
  --mono:  "Cascadia Mono", "Consolas", "Sarasa Mono TC", monospace;

  --sidebar-w: 264px;
  --rail-w:    200px;
  --top-h:     56px;
}

html[data-theme="dark"] {
  --bg:        #16151a;
  --bg-alt:    #1e1d23;
  --bg-sunk:   #121116;
  --fg:        #e9e6e0;
  --fg-soft:   #b3aea5;
  /* 同色相（原 #7d776f）調亮，令三種底都過 WCAG AA 4.5:1：
     5.53／5.09／5.72（原 4.10／3.78／4.24）。 */
  --fg-faint:  #938d86;
  --line:      #33313a;
  --line-soft: #2a2830;
  --accent:    #e08974;
  --accent-2:  #6fc2b1;
  --accent-bg: #2c2320;
  --mark:      #6b5a1f;
  --mark-fg:   #ffeeb8;
  --shadow:    0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.28);
}

/* ── 基礎 ─────────────────────────────────── */
* { box-sizing: border-box; }

html { scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--sans);
  font-size: calc(16px * var(--font-scale));
  line-height: 1.75;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

::selection { background: var(--mark); color: var(--mark-fg); }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 3px;
}

/* ── 頂欄 ─────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 40;
  height: var(--top-h);
  display: flex; align-items: center; gap: 12px;
  padding: 0 14px;
  background: color-mix(in srgb, var(--bg-alt) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(10px);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex; flex-direction: column; line-height: 1.15;
  text-decoration: none; color: inherit;
  padding-right: 6px; white-space: nowrap;
}
.brand-main { font-weight: 700; font-size: .98em; letter-spacing: .04em; }
.brand-sub  { font-size: .68em; color: var(--fg-faint); letter-spacing: .06em; }

.search-wrap { position: relative; flex: 1; max-width: 460px; margin-left: auto; }
#searchInput {
  width: 100%; padding: 7px 32px 7px 32px;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--bg-sunk); color: var(--fg);
  font-family: inherit; font-size: .86em;
}
#searchInput::placeholder { color: var(--fg-faint); }
#searchInput:focus { background: var(--bg-alt); border-color: var(--accent); outline: none; }
/* 關掉瀏覽器自帶的搜尋清除鈕：type="search" 在 Chromium / Safari 會自動長出一個 ✕，
   與我們自己的 .clear-btn 疊在一起變成兩個叉。清除功能由 #searchClear 負責。 */
#searchInput::-webkit-search-cancel-button,
#searchInput::-webkit-search-decoration { -webkit-appearance: none; appearance: none; display: none; }
.search-wrap::before {
  content: "⌕"; position: absolute; left: 11px; top: 50%;
  transform: translateY(-52%); color: var(--fg-faint);
  font-size: 1.05em; pointer-events: none;
}
.clear-btn { position: absolute; right: 3px; top: 50%; transform: translateY(-50%); }

.tools { display: flex; align-items: center; gap: 4px; }

.icon-btn {
  border: 1px solid transparent; background: transparent; color: var(--fg-soft);
  font-family: inherit; font-size: .82em; line-height: 1;
  padding: 7px 9px; border-radius: 8px; cursor: pointer;
  transition: background .14s, color .14s, border-color .14s;
}
.icon-btn:hover { background: var(--bg-sunk); color: var(--fg); border-color: var(--line); }
.icon-btn[aria-pressed="true"] { background: var(--accent-bg); color: var(--accent); }
.menu-btn { display: none; }

/* ── 版面 ─────────────────────────────────── */
.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr) var(--rail-w);
  gap: 0;
  max-width: 1500px;
  margin: 0 auto;
  align-items: start;
}

/* ── 側欄 ─────────────────────────────────── */
.sidebar {
  position: sticky; top: var(--top-h);
  height: calc(100vh - var(--top-h));
  overflow-y: auto; overscroll-behavior: contain;
  padding: 16px 10px 40px 16px;
  border-right: 1px solid var(--line-soft);
}

.side-head {
  display: flex; align-items: center; justify-content: space-between;
  font-size: .7em; font-weight: 700; letter-spacing: .14em;
  color: var(--fg-faint); text-transform: uppercase;
  padding: 10px 8px 8px;
}
.progress { font-family: var(--mono); letter-spacing: 0; font-size: .95em; }

.toc-list, .outline-list, .outline-rail { display: flex; flex-direction: column; gap: 1px; }

.toc-item {
  display: flex; gap: 9px; align-items: baseline;
  padding: 7px 9px; border-radius: 8px;
  text-decoration: none; color: var(--fg-soft);
  font-size: .84em; line-height: 1.45;
  border-left: 2px solid transparent;
  transition: background .12s, color .12s;
}
.toc-item:hover { background: var(--bg-sunk); color: var(--fg); }
.toc-item.active {
  background: var(--accent-bg); color: var(--accent);
  border-left-color: var(--accent); font-weight: 600;
}
.toc-num {
  font-family: var(--mono); font-size: .85em; color: var(--fg-faint);
  min-width: 1.6em; flex-shrink: 0;
}
.toc-item.active .toc-num { color: var(--accent); }
.toc-item.done .toc-num::after { content: "✓"; margin-left: 1px; }
.toc-title { flex: 1; }

.side-section { margin-top: 18px; border-top: 1px solid var(--line-soft); }

.outline-item {
  display: block; padding: 5px 9px; border-radius: 7px;
  text-decoration: none; color: var(--fg-soft);
  font-size: .79em; line-height: 1.5;
  border-left: 2px solid transparent;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.outline-item:hover { background: var(--bg-sunk); color: var(--fg); }
.outline-item.active { color: var(--accent); border-left-color: var(--accent); font-weight: 600; }
.outline-item.lv3 { padding-left: 20px; font-size: .75em; color: var(--fg-faint); }

/* ── 主內容 ───────────────────────────────── */
.content {
  min-width: 0;
  padding: 34px clamp(20px, 4vw, 56px) 120px;
  max-width: 860px;
  margin: 0 auto;
}
.content:focus { outline: none; }

.loading, .empty {
  color: var(--fg-faint); padding: 60px 0; text-align: center; font-size: .9em;
}

.article > :first-child { margin-top: 0; }

/* 標題 */
.article h1 {
  font-family: var(--serif);
  font-size: 1.9em; line-height: 1.35; font-weight: 700;
  letter-spacing: .02em;
  margin: 0 0 20px;
  padding-bottom: 16px;
  border-bottom: 2px solid var(--accent);
  color: var(--fg);
}
.article h2 {
  font-family: var(--serif);
  font-size: 1.3em; font-weight: 700;
  margin: 46px 0 14px;
  padding-left: 12px;
  border-left: 4px solid var(--accent);
  letter-spacing: .02em;
  scroll-margin-top: calc(var(--top-h) + 14px);
}
.article h3 {
  font-size: 1.06em; font-weight: 700;
  margin: 32px 0 10px;
  color: var(--accent-2);
  scroll-margin-top: calc(var(--top-h) + 14px);
}
.article h4 {
  font-size: .95em; font-weight: 700;
  margin: 24px 0 8px; color: var(--fg-soft);
  scroll-margin-top: calc(var(--top-h) + 14px);
}

/* 段落與文字 */
.article p { margin: 0 0 14px; }
.article strong { font-weight: 700; color: var(--fg); }
.article em { font-style: normal; color: var(--accent); font-weight: 600; }
.article hr {
  border: 0; height: 1px; background: var(--line);
  margin: 40px 0;
}
.article code {
  font-family: var(--mono); font-size: .88em;
  background: var(--bg-sunk); padding: 1px 5px; border-radius: 4px;
  border: 1px solid var(--line-soft);
}

/* ── 概念關係圖（``` 區塊）──────────────────
   框線字元（│─┌┴┐）在中文字型裡是「全形」寬度，在純等寬字型裡是半形，
   兩者混用就會錯位。這裡強制整個區塊用同一組字型，
   並靠 white-space: pre 保留行首空白。 */
.article pre.diagram {
  margin: 0 0 18px;
  padding: 16px 18px;
  background: var(--bg-sunk);
  border: 1px solid var(--line-soft);
  border-radius: 10px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.article pre.diagram code {
  display: block;
  font-family: var(--mono);      /* 框線與說明文字同字型，寬度才一致 */
  font-size: .84em;
  line-height: 1.65;
  white-space: pre;              /* 保留行首空白＝對齊的關鍵 */
  tab-size: 4;
  background: none;
  border: 0;
  padding: 0;
  border-radius: 0;
  color: var(--fg-soft);
}

/* ── 點字翻譯 ─────────────────────────────── */
/* 沒有注釋的字（純排版包裝）維持一般文字外觀，不可點。 */
.article .gl {
  border-radius: 3px;
  padding: 0 .5px;
}
/* 有注釋的字：細底線提示可點 */
.article .gl.has {
  cursor: pointer;
  border-bottom: 1.5px dotted color-mix(in srgb, var(--accent-2) 62%, transparent);
  transition: background .12s, color .12s, box-shadow .12s, border-bottom-color .12s;
  -webkit-tap-highlight-color: transparent;
}
.article .gl.has:hover {
  background: color-mix(in srgb, var(--accent-2) 20%, transparent);
  border-bottom-color: var(--accent-2);
}

/* 通假字：實線底線，與一般注釋的虛線區分
   （實線＝字形借用，虛線＝字義解釋） */
.article .gl.has.tong {
  border-bottom-style: solid;
  border-bottom-width: 1.5px;
  border-bottom-color: color-mix(in srgb, var(--accent) 70%, transparent);
}
.article .gl.has.tong:hover {
  background: color-mix(in srgb, var(--accent) 18%, transparent);
  border-bottom-color: var(--accent);
}
/* 通假字常常是從更長的詞條裡切出來的一小段（「句讀」的「讀」、「蛇蟺之穴」的「蟺」）。
   .gl 有 0.5px 左右內距，兩個相鄰片段各留一邊就會出現 1px 空隙，
   實線底線看起來就斷掉了；切點一律不留左內距。 */
.article .gl.has.tong + .gl.has,
.article .gl.has + .gl.has.tong {
  padding-left: 0;
}
/* 選中狀態 */
.article .gl.on {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 1px 6px color-mix(in srgb, var(--accent) 45%, transparent);
  border-bottom-color: transparent;
}

.gloss-pop {
  position: fixed;
  z-index: 70;
  max-width: min(340px, calc(100vw - 24px));
  background: var(--bg-alt);
  border: 1px solid var(--line);
  border-radius: 11px;
  box-shadow: 0 8px 32px rgba(0,0,0,.22);
  padding: 12px 14px;
  font-size: .86em;
  line-height: 1.65;
  animation: gp-in .13s ease-out;
}
@keyframes gp-in {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: none; }
}
/* 下方顯示時箭頭改向 */
.gloss-pop[data-below="1"] { animation-name: gp-in-down; }
@keyframes gp-in-down {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: none; }
}

.gp-head {
  display: flex; align-items: baseline; gap: 8px;
  margin-bottom: 6px;
}
.gp-word {
  font-family: var(--serif);
  font-size: 1.5em; font-weight: 700;
  color: var(--accent);
  letter-spacing: .04em;
  line-height: 1.2;
}
.gp-badge {
  font-size: .62em; font-weight: 700; letter-spacing: .12em;
  color: var(--accent-2);
  border: 1px solid color-mix(in srgb, var(--accent-2) 45%, transparent);
  border-radius: 4px;
  padding: 1px 5px;
  white-space: nowrap;
}
/* 釋義：不加框，純文字接在下方（只有通假字才有框） */
.gp-body {
  margin-top: 8px;
  color: var(--fg);
  line-height: 1.65;
}
.gp-body.gp-none { color: var(--fg-faint); font-style: italic; }

/* 通假字提示列：通「本字」 */
.gp-tong {
  display: flex; align-items: center; gap: 7px;
  margin-top: 8px; padding-top: 8px;
  border-top: 1px dashed var(--line);
}
.gp-tong-tag {
  flex: none;
  font-size: .68em; font-weight: 700; letter-spacing: .1em;
  color: #fff;
  background: var(--accent);
  border-radius: 4px;
  padding: 1px 6px;
}
.gp-tong-zi {
  font-family: var(--serif);
  font-size: 1.32em; font-weight: 700;
  color: var(--accent);
  letter-spacing: .06em;
  line-height: 1.2;
}

/* 提示：原文區塊可點 */
.gloss-hint {
  display: flex; align-items: center; gap: 7px;
  margin: 0 0 16px;
  padding: 8px 13px;
  background: var(--accent-bg);
  border-radius: 9px;
  font-size: .78em;
  color: var(--fg-soft);
}
.gloss-hint b { color: var(--accent); }

/* 引言／元資料區塊 */
.article blockquote {
  margin: 0 0 22px;
  padding: 14px 18px;
  background: var(--bg-alt);
  border: 1px solid var(--line-soft);
  border-left: 3px solid var(--accent-2);
  border-radius: 0 10px 10px 0;
  color: var(--fg-soft);
  font-size: .9em;
  box-shadow: var(--shadow);
}
.article blockquote p { margin: 0 0 4px; }
.article blockquote p:last-child { margin-bottom: 0; }

/* 清單 */
.article ul, .article ol { margin: 0 0 16px; padding-left: 1.5em; }
.article li { margin: 0 0 6px; }
.article li::marker { color: var(--accent-2); }

/* 表格 */
.table-wrap {
  overflow-x: auto; overscroll-behavior-x: contain;
  margin: 0 0 24px;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  background: var(--bg-alt);
}
.article table {
  border-collapse: collapse; width: 100%;
  font-size: .85em; line-height: 1.6;
}
.article thead th {
  background: var(--bg-sunk);
  font-weight: 700; text-align: left;
  padding: 10px 13px;
  border-bottom: 2px solid var(--line);
  white-space: nowrap;
  position: sticky; top: 0; z-index: 1;
}
.article tbody td {
  padding: 9px 13px;
  border-bottom: 1px solid var(--line-soft);
  vertical-align: top;
}
.article tbody tr:last-child td { border-bottom: 0; }
.article tbody tr:hover { background: color-mix(in srgb, var(--accent-bg) 45%, transparent); }

/* 摺疊答案（默寫自測） */
.article details {
  margin: 0 0 22px;
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: 0 10px 10px 0;
  background: var(--bg-alt);
  overflow: hidden;
}
.article summary {
  cursor: pointer; padding: 11px 16px;
  font-weight: 700; font-size: .88em;
  color: var(--accent);
  background: var(--accent-bg);
  user-select: none;
  list-style: none;
}
.article summary::-webkit-details-marker { display: none; }
.article summary::before {
  content: "▸"; display: inline-block; margin-right: 8px;
  transition: transform .16s;
}
.article details[open] > summary::before { transform: rotate(90deg); }
.article details[open] > summary { border-bottom: 1px solid var(--line); }
.article details > *:not(summary) { padding: 0 16px; }
.article details > *:not(summary):first-of-type { padding-top: 14px; }
.article details > *:not(summary):last-child { padding-bottom: 14px; }
.article details .table-wrap { margin-left: 16px; margin-right: 16px; width: auto; }

/* 注釋摺疊（已有點字翻譯，紙本注釋表收起來備查） */
.article details.fold {
  border-left-color: var(--line);
  background: transparent;
}
.article details.fold > summary {
  color: var(--fg-soft);
  background: transparent;
  border: 1px dashed var(--line);
  border-radius: 8px;
  font-weight: 600;
  padding: 8px 14px;
}
.article details.fold[open] > summary {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  border-bottom-style: solid;
  border-bottom-color: var(--line);
}
.article details.fold > summary .fold-tag {
  display: inline-block;
  margin-right: 7px;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: .82em;
  font-weight: 700;
  color: var(--accent);
  background: var(--accent-bg);
}
.article details.fold > summary .fold-text {
  font-weight: 400;
  opacity: .78;
}

/* 搜尋高亮 */
mark.hit { background: var(--mark); color: var(--mark-fg); border-radius: 2px; padding: 0 1px; }

/* ── 右側目錄 ─────────────────────────────── */
.rail {
  position: sticky; top: var(--top-h);
  height: calc(100vh - var(--top-h));
  overflow-y: auto; overscroll-behavior: contain;
  padding: 16px 16px 40px 10px;
  border-left: 1px solid var(--line-soft);
}
.outline-rail .outline-item { font-size: .76em; white-space: normal; }

/* ── 搜尋面板 ─────────────────────────────── */
.search-panel {
  position: fixed; z-index: 60;
  top: calc(var(--top-h) + 8px); left: 50%; transform: translateX(-50%);
  width: min(720px, calc(100vw - 32px));
  max-height: min(70vh, 640px);
  display: flex; flex-direction: column;
  background: var(--bg-alt);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 12px 48px rgba(0,0,0,.18);
  overflow: hidden;
}
/* hidden 屬性必須能蓋過 display:flex，否則面板會一直顯示 */
.search-panel[hidden] { display: none !important; }
.sp-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; border-bottom: 1px solid var(--line-soft);
  font-size: .78em; color: var(--fg-faint); letter-spacing: .06em;
}
.sp-body { overflow-y: auto; padding: 6px; }
.sp-hit {
  display: block; width: 100%; text-align: left;
  padding: 10px 12px; border: 0; border-radius: 9px;
  background: transparent; color: var(--fg); font-family: inherit;
  font-size: .84em; line-height: 1.6; cursor: pointer;
}
.sp-hit:hover, .sp-hit:focus-visible { background: var(--bg-sunk); }
.sp-hit .sp-src {
  display: block; font-size: .78em; color: var(--accent-2);
  font-weight: 700; margin-bottom: 3px;
}
.sp-hit .sp-text { color: var(--fg-soft); }
.sp-empty { padding: 40px 16px; text-align: center; color: var(--fg-faint); font-size: .86em; }

/* ── 其他 ─────────────────────────────────── */
.scrim {
  position: fixed; inset: var(--top-h) 0 0 0; z-index: 25;
  background: rgba(0,0,0,.34);
  backdrop-filter: blur(1px);
}

.to-top {
  position: fixed; right: 20px; bottom: 24px; z-index: 30;
  width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--bg-alt); color: var(--fg-soft);
  font-size: 1.05em; cursor: pointer;
  box-shadow: var(--shadow);
}
.to-top:hover { color: var(--accent); border-color: var(--accent); }

/* ── 響應式 ───────────────────────────────── */
/* 三段式（左篇目 + 內容 + 右目錄）需要約 1180px 才放得下：
   264 + 內容 860 + 200 = 1324，扣掉內距後 1180 是極限。
   平板（iPad 直向 768、橫向 1024）只保留「左篇目 + 內容」，
   因為左側欄本身已經有「本篇目錄」，右欄只是重複，且會把正文擠窄。 */
@media (max-width: 1180px) {
  .layout { grid-template-columns: var(--sidebar-w) minmax(0, 1fr); }
  .rail { display: none; }
}

@media (max-width: 860px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .menu-btn { display: block; }
  .brand-sub { display: none; }

  .sidebar {
    position: fixed; z-index: 35;
    top: var(--top-h); left: 0; bottom: 0;
    width: min(300px, 84vw); height: auto;
    background: var(--bg-alt);
    border-right: 1px solid var(--line);
    transform: translateX(-102%);
    transition: transform .2s ease;
    box-shadow: 0 0 32px rgba(0,0,0,.2);
  }
  .sidebar.open { transform: none; }

  .content { padding: 22px 18px 100px; }
  .article h1 { font-size: 1.5em; }
  .article h2 { font-size: 1.16em; margin-top: 36px; }
  .article table { font-size: .8em; }

  /* 手機／平板：目標為 iPhone SE (375)、iPhone 14 Pro (393)、
     Android 常見 (360/412)、iPad 直向 (768)。
     重點是不要橫向捲動、觸控目標夠大、表格能讀。 */
  .topbar { gap: 8px; padding: 0 10px; }
  .brand { padding-right: 0; }
  .icon-btn { padding: 9px 10px; font-size: .9em; }  /* 觸控目標放大 */
  #searchInput { font-size: 16px; }                   /* <16px 時 iOS 會自動放大頁面 */
}

@media (max-width: 560px) {
  .tools #fontMinus, .tools #fontPlus { display: none; }
  .brand-main { font-size: .9em; }

  /* 內容：左右留白縮到最小，行長才好讀 */
  .content { padding: 18px 14px calc(88px + env(safe-area-inset-bottom)); }

  .article h1 { font-size: 1.35em; padding-bottom: 12px; }
  .article h2 { font-size: 1.1em; margin: 30px 0 12px; padding-left: 9px; }
  .article h3 { font-size: 1.02em; }
  .article hr { margin: 26px 0; }

  /* 長網址／長英文字不要撐破版面 */
  .article { overflow-wrap: anywhere; word-break: break-word; }

  /* 表格：縮小內距塞更多字，並讓每列維持可讀 */
  .table-wrap { border-radius: 8px; margin-bottom: 18px; }
  .article table { font-size: .78em; }
  .article thead th { padding: 8px 10px; white-space: nowrap; }
  .article tbody td { padding: 7px 10px; }

  /* 摺疊區：內距縮小，避免窄螢幕上內容被擠成一直條 */
  .article details { border-radius: 0 8px 8px 0; }
  .article summary { padding: 10px 13px; font-size: .92em; }
  .article details > *:not(summary) { padding: 0 13px; }
  .article details .table-wrap { margin-left: 13px; margin-right: 13px; }

  /* 點字翻譯彈卡：手機上貼齊螢幕寬，字放大一點比較好點 */
  .gloss-pop {
    max-width: calc(100vw - 20px);
    padding: 11px 13px;
    font-size: .92em;
    left: 10px !important;
    right: 10px;
    width: auto;
  }
  .gp-word { font-size: 1.6em; }

  /* 引言區塊縮小內距 */
  .article blockquote { padding: 12px 14px; border-radius: 0 8px 8px 0; }
  .article ul, .article ol { padding-left: 1.25em; }

  /* 搜尋面板：幾乎滿版，結果才讀得到 */
  .search-panel {
    top: calc(var(--top-h) + 6px);
    width: calc(100vw - 16px);
    max-height: min(76vh, 560px);
    border-radius: 12px;
  }
  .sp-hit { padding: 10px; }

  /* 回頂部按鈕避開 iPhone 底部橫條 */
  .to-top {
    right: 14px;
    bottom: calc(18px + env(safe-area-inset-bottom));
    width: 42px; height: 42px;
  }

  /* 側欄滑出時項目加大觸控範圍 */
  .toc-item { padding: 10px 11px; font-size: .88em; }
  .outline-item { padding: 9px 11px; }

  /* 側欄底部留出安全區域，最後一項不會被 iPhone 橫條蓋住 */
  .sidebar { padding-bottom: calc(40px + env(safe-area-inset-bottom)); }

  /* 觸控目標：實測 44px 為行動裝置建議下限，
     靠 min-height + flex 置中撐出來，不影響文字排版 */
  .icon-btn { min-height: 44px; min-width: 44px; display: inline-flex;
              align-items: center; justify-content: center; }
  .toc-item { min-height: 44px; }
  .outline-item { min-height: 40px; }
  .article summary { min-height: 44px; display: flex; align-items: center; }
  .sp-hit { min-height: 52px; }
  .to-top { width: 46px; height: 46px; }
}

/* 極窄（iPhone SE 直向 320～360）再收一級 */
@media (max-width: 380px) {
  .brand-main { font-size: .82em; letter-spacing: .01em; }
  .topbar { gap: 6px; padding: 0 8px; }
  .icon-btn { padding: 8px 8px; }
  .content { padding-left: 12px; padding-right: 12px; }
  .article table { font-size: .74em; }
}

/* 橫向／平板：這一區沒有額外覆寫。
   右側目錄的顯示與否統一由上面的寬度斷點決定，
   這裡只調整捲動定位的留白。 */
@media (min-width: 861px) and (max-width: 1180px) {
  .article h2, .article h3 { scroll-margin-top: calc(var(--top-h) + 12px); }
}

/* 觸控裝置：取消 hover 效果（iOS 會讓第一次點擊只觸發 hover） */
@media (hover: none) {
  .article .gl.has:hover { background: none; }
  .article tbody tr:hover { background: none; }
  .toc-item:hover, .outline-item:hover { background: none; }
  #searchInput:hover { background: var(--bg-sunk); }
}

/* 使用者放大字體時，手機上不要把表格也一起放太大 */
@media (max-width: 560px) and (min-resolution: 2dppx) {
  .article table { -webkit-text-size-adjust: 100%; }
}

@media print {
  :root { --font-scale: .92; }
  .topbar, .sidebar, .rail, .to-top, .scrim, .search-panel { display: none !important; }
  .layout { display: block; max-width: none; }
  .content { max-width: none; padding: 0; }

  body { background: #fff; color: #000; font-size: 10.5pt; }
  .article h1 { border-bottom-color: #000; font-size: 16pt; }
  .article h2 { border-left-color: #000; font-size: 13pt; page-break-after: avoid; }
  .article h3 { color: #000; page-break-after: avoid; }

  .article blockquote, .table-wrap, .article details {
    background: #fff; box-shadow: none; border-color: #999;
  }
  .article details { page-break-inside: avoid; }
  .article details > summary { background: #fff; color: #000; }
  .article details[open] > summary { border-bottom-color: #999; }
  .article thead th { background: #eee; position: static; }
  .article tbody tr:hover { background: none; }
  .article table { font-size: 9pt; }

  a { color: #000; text-decoration: none; }
  a[href^="http"]::after { content: " (" attr(href) ")"; font-size: 8pt; color: #555; }
}

/* ── 原文朗讀播放器（配合 src/player.js） ─────────────────────
   只有該篇有音檔時才出現：player.js 讀到 audio/index.json 之後，
   才移除 [hidden] 並在 body 加上 .has-tts。
   播放列固定在頂欄之下（不是 sticky），用 body.has-tts 的 .layout padding-top
   把版面推下來，免得蓋住內文；側欄的 sticky 高度與捲動定位跟着修正。 */
:root {
  --tts-h:    44px;
  --tts-bg:   #fffdf7;
  --tts-bd:   #e2ddd0;
  --tts-hl:   #ffe9a8;   /* 逐句高亮的底色（淺色） */
  --tts-hl-fg:#2b2100;
}
html[data-theme="dark"] {
  --tts-bg:   #201e24;
  --tts-bd:   #34313a;
  --tts-hl:   #6a5716;
  --tts-hl-fg:#fff2c4;
}

.tts-bar {
  position: fixed;
  top: var(--top-h); left: 0; right: 0;
  height: var(--tts-h);
  z-index: 38;                     /* 頂欄是 40，要在它之下 */
  display: flex; align-items: center; gap: 8px;
  padding: 0 10px;
  background: var(--tts-bg);
  border-bottom: 1px solid var(--tts-bd);
  box-shadow: 0 1px 6px rgba(0, 0, 0, .05);
}
.tts-bar[hidden] { display: none; }

/* 讓位（44 px）加在 .layout 上，唔好加在 body：
   body 有 padding-top 會連 sticky 頂欄一齊推低 44 px —— 頁面停喺最頂
   （scrollY === 0，sticky 未開始黏）時頂欄就落到 44–100 px，正好蓋住
   播放列（fixed top:56px、z-index 38 < 頂欄 40），滑鼠點唔到播放鍵同進度條。
   改為推 .layout，頂欄永遠喺 0–56，播放列 56–100，兩者永不重疊；
   內文第一行仍然喺 100 + .content padding，同改動前一樣。 */
body.has-tts .layout { padding-top: var(--tts-h); }
body.has-tts .sidebar,
body.has-tts .rail {
  top: calc(var(--top-h) + var(--tts-h));
  height: calc(100vh - var(--top-h) - var(--tts-h));
}
body.has-tts .article h2,
body.has-tts .article h3 { scroll-margin-top: calc(var(--top-h) + var(--tts-h) + 14px); }

.tts-play {
  flex: 0 0 auto;
  width: 30px; height: 30px;
  border: 0; border-radius: 50%;
  background: var(--accent); color: #fff;
  font-size: 13px; line-height: 1;
  cursor: pointer;
}
.tts-meta { flex: 0 1 auto; min-width: 0; max-width: 30%; position: relative; }
.tts-title {
  display: block;
  font-size: .82em; color: var(--fg-soft);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
/* 「下載中 xx%」：冇 Range 的主機在背景預取整首 mp3，
   預取未完成前點未緩衝的句子會顯示這行（role=status 供螢幕報讀器）。
   它是播放列裡的 flex 項目，不是浮層 —— 所以不會遮住播放列本身或原文；
   窄螢幕靠下面的 .tts-bar.is-busy 規則收起 .tts-time 讓位。 */
.tts-status {
  flex: 0 0 auto;
  padding: 2px 7px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent-bg);
  color: var(--fg);
  font-size: .72em; line-height: 1.3;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  max-width: 42vw; overflow: hidden; text-overflow: ellipsis;
}
.tts-status[hidden] { display: none; }
.tts-track {
  flex: 1 1 auto; min-width: 60px;
  height: 6px; border-radius: 3px;
  background: var(--bg-sunk);
  cursor: pointer; touch-action: none;
  overflow: hidden;
}
.tts-fill { height: 100%; width: 0; background: var(--accent); border-radius: 3px; }
.tts-time {
  flex: 0 0 auto;
  font-size: .72em; color: var(--fg-soft);
  font-variant-numeric: tabular-nums; white-space: nowrap;
}
.tts-btn {
  flex: 0 0 auto;
  height: 28px; padding: 0 7px;
  border: 1px solid var(--tts-bd); border-radius: 7px;
  background: var(--bg); color: var(--fg);
  font-size: .74em; line-height: 1; cursor: pointer;
}
.tts-btn:hover { background: var(--bg-sunk); }
.tts-btn[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.tts-btn:focus-visible, .tts-play:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* 逐句高亮：優先用 CSS Custom Highlight API（不動 DOM，跨 <span class="gl"> 都準） */
::highlight(tts) { background-color: var(--tts-hl); color: var(--tts-hl-fg); }
/* 後備（舊瀏覽器）：player.js 退化成在字的 span 加 .tts-on */
.article .gl.tts-on { background: var(--tts-hl); color: var(--tts-hl-fg); border-radius: 2px; }

/* ── 窄螢幕（手機）：播放列的觸控目標 ─────────────────────────────
   頂欄的 .icon-btn 一早放大到 44 px（見上面 @media 的註解），但播放列是後來
   才加的，窄螢幕規則一直冇跟上：390 px 實測播放鍵 28×28、倍速鍵 21×26、
   進度條只有 6 px 高（可命中區 7 px）—— 用拇指根本拖唔到。
   觸控下限：播放鍵 ≥44×44、其他按鈕 ≥40×40、進度條可拖命中區 ≥20 px。
   高度跟住由 44 提到 52 px（44 px 放唔落 44 px 的按鈕）；
   --tts-h 已用於 .layout padding-top、.sidebar/.rail 的 top/height 與
   scroll-margin-top，所以只要改這一個變數，其餘會自動跟隨。 */
@media (max-width: 560px) {
  :root { --tts-h: 52px; }
  .tts-bar { gap: 4px; padding: 0 6px; }
  /* 窄螢幕先犧牲篇名（.tts-meta 收成 0 寬，但保留節點）。 */
  .tts-meta { max-width: 0; overflow: visible; }
  .tts-title { display: none; }
  /* 狀態列「下載中 xx%」出現時再收起時間，讓它一定放得下（不換行、不遮原文） */
  .tts-bar.is-busy .tts-time { display: none; }

  .tts-play { width: 44px; height: 44px; font-size: 15px; }
  .tts-btn { height: 40px; min-width: 40px; padding: 0 6px; font-size: .72em; }

  /* 進度條：視覺上仍然係 6 px 細條，但整條 20 px 高都收得到拖動。
     條本身透明，由 ::before 畫幼條、.tts-fill 置中，所以 .tts-fill 唔會變粗。 */
  .tts-track {
    position: relative; min-width: 0;
    height: 20px; border-radius: 0;
    background: transparent; overflow: visible;
    touch-action: none;                 /* 保留：拖進度條時唔好交畀瀏覽器捲動 */
  }
  .tts-track::before {
    content: ""; position: absolute; left: 0; right: 0; top: 50%;
    height: 6px; margin-top: -3px; border-radius: 3px; background: var(--bg-sunk);
  }
  .tts-fill {
    position: absolute; left: 0; top: 50%;
    height: 6px; margin-top: -3px; border-radius: 3px;
  }

  /* 內文容器：窄螢幕 margin:auto + fit-content 會令佢撐到 466 px，
     全頁橫向捲動（390 px 多 76 px、360 px 多 103 px）。改成填滿格線。 */
  .content { width: 100%; margin: 0; max-width: none; }
}

/* 420 px 以下：時間碼讓位，保住 44 px 播放鍵 + 4×40 px 按鈕 + 可拖的進度條。 */
@media (max-width: 420px) {
  .tts-time { display: none; }
}


@media print {
  .tts-bar { display: none !important; }
  body.has-tts .layout { padding-top: 0; }
}

/* ── 主動回憶練習（配合 src/practice.js） ─────────────────────
   兩塊：① 篇內遮蓋（注釋／通假字表的特定欄位）② 練習中心（抽卡・默寫・錯題本）。
   遮蓋刻意用 font-size:0 + 固定尺寸灰塊，令格子寬度不隨原文字數變動
   —— 否則「答案有幾個字」會從排版寬度洩漏出去，等於沒遮。 */
:root {
  --quiz-bg:      #ffffff;
  --quiz-bd:      #e2ddd0;
  --quiz-mask:    #d9d3c6;
  --quiz-ok:      #1e6b5e;
  --quiz-ok-bg:   #e7f2ef;
  --quiz-bad:     #b3261e;
  --quiz-bad-bg:  #fbeae8;
  --quiz-miss:    #8a6a00;
  --quiz-miss-bg: #fdf3d6;
}
html[data-theme="dark"] {
  --quiz-bg:      #201e24;
  --quiz-bd:      #34313a;
  --quiz-mask:    #45414c;
  --quiz-ok:      #6fc2b1;
  --quiz-ok-bg:   #1b2b28;
  --quiz-bad:     #f08a80;
  --quiz-bad-bg:  #33201e;
  --quiz-miss:    #e0c069;
  --quiz-miss-bg: #322b16;
}

/* ① 篇內遮蓋 */
.mask-wrap { position: relative; }
.mask-btn {
  display: inline-block;
  margin: 2px 0 6px;
  padding: 3px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg-alt);
  color: var(--fg-soft);
  font-family: var(--sans);
  font-size: .78em;
  line-height: 1.5;
  cursor: pointer;
}
.mask-btn:hover { background: var(--bg-sunk); color: var(--fg); }
.mask-btn[aria-pressed="true"] {
  background: var(--accent-bg);
  border-color: var(--accent);
  color: var(--accent);
}
details > summary .mask-btn { float: right; margin: 0 0 0 8px; }

.mask-wrap.masked td.maskable { font-size: 0; cursor: pointer; }
.mask-wrap.masked td.maskable::before {
  content: "";
  display: inline-block;
  width: 72px;
  height: 12px;
  border-radius: 3px;
  background: var(--quiz-mask);
  vertical-align: middle;
}
.mask-wrap.masked td.maskable.revealed { font-size: inherit; }
.mask-wrap.masked td.maskable.revealed::before { display: none; }

/* 行內注釋段落（01-論語「**注釋**：…」）：整段尾巴遮成色塊，點一下現形 */
.mask-wrap.masked .mask-text {
  background: var(--quiz-mask);
  color: transparent;
  border-radius: 3px;
  cursor: pointer;
}
.mask-wrap.masked .mask-text.revealed { background: none; color: inherit; cursor: inherit; }

/* ② 練習中心 */
.practice {
  max-width: 780px;
  margin: 0 auto;
  padding: 4px 0 64px;
}
.practice-head h1 {
  margin: 0 0 6px;
  font-family: var(--serif);
  font-size: 1.45em;
  font-weight: 700;
}
.practice-sub {
  margin: 0 0 16px;
  color: var(--fg-soft);
  font-size: .88em;
}

.practice-scope {
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg-alt);
}
.scope-title {
  margin: 0 0 8px;
  font-size: .82em;
  font-weight: 600;
  color: var(--fg-soft);
}
.scope-chips,
.practice-opts {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.practice-opts { margin: 0 0 12px; }
.scope-bulk { margin-top: 8px; display: flex; gap: 6px; }

.scope-chip,
.kind-chip,
.tab,
.btn,
.btn-mini,
.btn-primary,
.btn-warn {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg-alt);
  color: var(--fg);
  font-family: var(--sans);
  font-size: .84em;
  line-height: 1.6;
  padding: 4px 10px;
  cursor: pointer;
}
.btn-mini { font-size: .78em; padding: 3px 8px; border-radius: 999px; color: var(--fg-soft); }
.scope-chip[aria-pressed="true"],
.kind-chip[aria-pressed="true"] {
  background: var(--accent-bg);
  border-color: var(--accent);
  color: var(--accent);
}
.btn:hover, .btn-mini:hover, .scope-chip:hover, .kind-chip:hover, .tab:hover { background: var(--bg-sunk); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-primary:hover { background: var(--accent); filter: brightness(1.06); }
.btn-warn { color: var(--quiz-bad); border-color: var(--quiz-bad); }
.btn[disabled], .btn-primary[disabled] { opacity: .5; cursor: default; }

.practice-tabs {
  display: flex;
  gap: 4px;
  margin: 18px 0 12px;
  border-bottom: 1px solid var(--line);
}
.tab {
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  background: none;
  color: var(--fg-soft);
  font-size: .9em;
  padding: 6px 12px;
  margin-bottom: -1px;
}
.tab[aria-selected="true"] { border-bottom-color: var(--accent); color: var(--accent); font-weight: 600; }

.practice-progress {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: baseline;
  margin-bottom: 8px;
  font-size: .82em;
  color: var(--fg-soft);
}
.practice-progress .stat { color: var(--fg-faint); }
.practice-progress .wrong-flag { color: var(--quiz-bad); }
.practice-hint { color: var(--fg-faint); font-size: .82em; margin: 8px 0; }

/* 抽卡 */
.flashcard {
  display: block;
  width: 100%;
  min-height: 150px;
  padding: 24px 20px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--quiz-bg);
  box-shadow: var(--shadow);
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.flashcard.flipped { cursor: default; }
.card-front { font-family: var(--serif); font-size: 1.2em; line-height: 1.8; }
.flashcard.flipped .card-front { font-size: 1em; color: var(--fg-soft); }
.card-back { margin-top: 10px; border-top: 1px dashed var(--line); padding-top: 10px; }
.card-back.hidden { display: none; }
.card-answer { font-family: var(--serif); font-size: 1.1em; }
.card-src { margin-top: 8px; color: var(--fg-soft); font-size: .82em; }
.card-note { margin-top: 6px; color: var(--fg-faint); font-size: .78em; }
.card-actions { display: flex; gap: 8px; margin-top: 12px; }
.card-actions.hidden { display: none; }

/* 默寫 */
.cloze-box { border: 1px solid var(--line); border-radius: 12px; background: var(--quiz-bg); padding: 16px 18px; }
.cloze-sentence { font-family: var(--serif); font-size: 1.12em; line-height: 2.4; }
.cloze-slot { display: inline-block; position: relative; margin: 0 2px; }
.cloze-input {
  width: 4.4em;
  padding: 2px 4px;
  border: 0;
  border-bottom: 2px solid var(--accent);
  background: var(--bg-sunk);
  color: var(--fg);
  font-family: var(--serif);
  font-size: 1em;
  text-align: center;
}
.cloze-input:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.cloze-input.ok { border-bottom-color: var(--quiz-ok); background: var(--quiz-ok-bg); }
.cloze-input.bad { border-bottom-color: var(--quiz-bad); background: var(--quiz-bad-bg); }
.cloze-answer { display: block; font-size: .7em; color: var(--quiz-bad); white-space: nowrap; }
.cloze-fb { display: block; font-size: .7em; white-space: nowrap; }
.cloze-fb i { font-style: normal; padding: 0 2px; border-radius: 2px; }
.ch-ok { color: var(--quiz-ok); }
.ch-bad { color: var(--quiz-bad); background: var(--quiz-bad-bg); }
.ch-bad em { font-style: normal; font-size: .85em; }
.ch-miss { color: var(--quiz-miss); background: var(--quiz-miss-bg); }
.ch-extra { color: var(--quiz-miss); text-decoration: line-through; }

/* 錯字表 */
.badchars {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  margin-top: 20px;
  padding-top: 10px;
  border-top: 1px dashed var(--line);
  font-size: .84em;
}
.badchars-title, .badchars-empty { color: var(--fg-soft); font-size: .92em; }
.badchar { border: 1px solid var(--line); border-radius: 6px; background: var(--bg-alt); padding: 1px 7px; }
.badchar b { font-family: var(--serif); font-weight: 700; }
.badchar i { font-style: normal; color: var(--fg-faint); font-size: .82em; margin-left: 3px; }

/* 錯題本 */
.practice-end h2, .wrong-head h2 {
  margin: 4px 0 6px;
  font-family: var(--serif);
  font-size: 1.15em;
}
.practice-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }
.wrong-head { display: flex; align-items: baseline; justify-content: space-between; flex-wrap: wrap; }
.wrong-list { width: 100%; border-collapse: collapse; font-size: .86em; }
.wrong-list th, .wrong-list td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--line-soft);
  text-align: left;
  vertical-align: middle;
}
.wrong-list th { color: var(--fg-soft); font-weight: 600; font-size: .92em; }
.wrong-front { font-family: var(--serif); }
.wrong-w { color: var(--quiz-bad); font-variant-numeric: tabular-nums; }

@media (max-width: 560px) {
  .practice { padding-bottom: 40px; }
  .flashcard { padding: 18px 14px; min-height: 120px; }
  .card-front { font-size: 1.08em; }
  .cloze-input { width: 3.4em; }
  .cloze-box { padding: 12px; }
  .wrong-list { font-size: .78em; }
  /* 窄螢幕先犧牲類別與日期，保住卡片與 w */
  .wrong-list th:nth-child(2), .wrong-list td:nth-child(2),
  .wrong-list th:nth-child(5), .wrong-list td:nth-child(5) { display: none; }
}

@media print {
  /* 列印一律顯示：遮蓋沒有意義，還要印得出來 */
  .mask-wrap.masked td.maskable { font-size: inherit; }
  .mask-wrap.masked td.maskable::before { display: none; }
  .mask-wrap.masked .mask-text { background: none; color: inherit; }
  .mask-btn, .practice-tabs, .scope-bulk, .practice-actions, .card-actions, .practice-progress, .kind-chip, .scope-chip, .badchars { display: none !important; }
  .practice { max-width: none; padding: 0; }
  .card-back.hidden { display: block !important; }
  .flashcard, .cloze-box { box-shadow: none; border-color: #999; background: #fff; page-break-inside: avoid; }
}
