/* =====================================================================
   香港學科學習平台 — 設計系統
   原則：大圓角卡片、柔和陰影、資訊密度高但不擁擠、深淺色跟隨系統。
   零 CDN、零外部字體、零依賴。
   ===================================================================== */

/* ---------- 1. 設計令牌 ---------- */
:root {
  --brand: #4f7cff;
  --brand-ink: #ffffff;
  --brand-soft: rgba(79, 124, 255, .12);

  --ok: #0b7a3c;
  --warn: #8a6100;
  --danger: #b8322a;
  --purple: #8b5cf6;
  --teal: #0ea5a5;
  --pink: #e0518a;
  --amber: #f59e0b;

  --bg: #f4f6fa;
  --bg-grad: radial-gradient(1200px 600px at 12% -10%, #e8eeff 0%, transparent 60%),
            radial-gradient(900px 500px at 100% 0%, #e9f7f4 0%, transparent 55%);
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --surface-3: #eef2f8;
  --ink: #1a2233;
  --ink-2: #3b4761;
  --muted: #5d6883;
  --line: #e3e8f0;
  --line-2: #d3dae6;

  --shadow-1: 0 1px 2px rgba(20, 30, 60, .05), 0 2px 8px rgba(20, 30, 60, .04);
  --shadow-2: 0 2px 6px rgba(20, 30, 60, .06), 0 10px 28px rgba(20, 30, 60, .07);
  --shadow-3: 0 8px 24px rgba(20, 30, 60, .12), 0 24px 60px rgba(20, 30, 60, .14);

  --r-xs: 8px;
  --r-sm: 11px;
  --r-md: 14px;
  --r-lg: 20px;
  --r-xl: 26px;
  --r-full: 999px;

  --nav-w: 232px;
  --top-h: 60px;
  --sp: 14px;

  --fs: 15px;
  --lh: 1.65;
}

:root[data-theme="dark"] {
  --brand: #6f92ff;
  --brand-soft: rgba(111, 146, 255, .16);

  --bg: #0d1119;
  --bg-grad: radial-gradient(1200px 600px at 12% -10%, #16203a 0%, transparent 60%),
            radial-gradient(900px 500px at 100% 0%, #10282a 0%, transparent 55%);
  --surface: #161b26;
  --surface-2: #1c2230;
  --surface-3: #242c3c;
  --ink: #e9edf6;
  --ink-2: #cdd6e6;
  --muted: #93a0ba;
  --line: #262e3e;
  --line-2: #333d51;

  --shadow-1: 0 1px 2px rgba(0, 0, 0, .3);
  --shadow-2: 0 2px 8px rgba(0, 0, 0, .35), 0 10px 30px rgba(0, 0, 0, .3);
  --shadow-3: 0 10px 30px rgba(0, 0, 0, .45), 0 28px 70px rgba(0, 0, 0, .4);
}

/* 字級：1 小 / 2 標準 / 3 大 (G9) */
:root { font-size: var(--fs); }
:root[style*="--font-scale:1"] { --fs: 14px; }
:root[style*="--font-scale:3"] { --fs: 16.5px; }

/* 減少動畫 (G7) */
:root[data-motion="off"] *,
:root[data-motion="off"] *::before,
:root[data-motion="off"] *::after {
  animation-duration: .001ms !important;
  transition-duration: .001ms !important;
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}

/* 版面密度 (G5) */
:root[data-density="compact"] { --sp: 9px; --r-lg: 15px; }
:root[data-density="compact"] .card { padding: 12px; }

/* 高對比 (G2) */
:root[data-contrast="high"] {
  --line: #9aa5b8; --muted: #4a5568; --ink: #000;
}
:root[data-contrast="high"][data-theme="dark"] {
  --line: #8a94a8; --muted: #c3ccdd; --ink: #fff;
}

/* ---------- 2. 基礎 ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  background-image: var(--bg-grad);
  background-attachment: fixed;
  color: var(--ink);
  font: var(--fs)/var(--lh) system-ui, -apple-system, "Segoe UI", "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}
h1, h2, h3, h4 { margin: 0; line-height: 1.3; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.12rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .7em; }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; color: inherit; }
img, svg { max-width: 100%; }
hr { border: 0; border-top: 1px solid var(--line); margin: var(--sp) 0; }
code, kbd, pre { font-family: ui-monospace, "SF Mono", "Cascadia Code", Consolas, monospace; font-size: .9em; }
kbd { background: var(--surface-3); border: 1px solid var(--line); border-radius: 6px; padding: 1px 6px; }

:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
  border-radius: 6px;
}
.skip-link {
  position: fixed; top: -60px; left: 12px; z-index: 999;
  background: var(--surface); color: var(--ink); padding: 10px 18px;
  border-radius: var(--r-sm); box-shadow: var(--shadow-2); transition: top .15s;
}
.skip-link:focus { top: 12px; }

/* ---------- 3. 應用外殼 ---------- */
.shell { display: grid; grid-template-columns: var(--nav-w) 1fr; min-height: 100vh; }

.nav {
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
  background: var(--surface); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 14px 12px; gap: 4px;
  z-index: 40;
}
.nav-brand {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px 14px; font-weight: 700; font-size: .98rem;
}
.nav-logo {
  width: 32px; height: 32px; border-radius: 10px; background: var(--brand);
  color: #fff; display: grid; place-items: center; flex: none;
}
.nav-logo svg { width: 20px; height: 20px; }
.nav-logo img { width: 20px; height: 20px; }
.nav-brand small { display: block; font-weight: 500; font-size: .68rem; color: var(--muted); letter-spacing: .02em; }
.nav-group { margin-top: 10px; padding: 0 10px; font-size: .68rem; font-weight: 700; color: var(--muted); letter-spacing: .07em; text-transform: uppercase; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 9px 11px; border-radius: var(--r-sm);
  color: var(--ink-2); cursor: pointer; border: 0; background: transparent; width: 100%; text-align: left;
  transition: background .13s, color .13s; font-size: .92rem;
}
.nav-item:hover { background: var(--surface-2); text-decoration: none; }
.nav-item[aria-current="page"] { background: var(--brand-soft); color: var(--brand); font-weight: 650; }
.nav-item .ic { width: 18px; height: 18px; flex: none; opacity: .85; }
.nav-item .badge {
  margin-left: auto; background: var(--danger); color: #fff; border-radius: var(--r-full);
  font-size: .68rem; padding: 1px 7px; font-weight: 700;
}
.nav-item .badge.muted { background: var(--surface-3); color: var(--muted); }
.nav-foot { margin-top: auto; padding: 10px; font-size: .72rem; color: var(--muted); border-top: 1px solid var(--line); }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 30; height: var(--top-h);
  display: flex; align-items: center; gap: 10px; padding: 0 18px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 1.05rem; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.content { padding: 20px 22px 60px; max-width: 1280px; width: 100%; }
.content.narrow { max-width: 860px; }

/* ---------- 4. 元件：卡片 ---------- */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg);
  padding: var(--sp); box-shadow: var(--shadow-1);
}
.card + .card { margin-top: var(--sp); }
.card-h { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.card-h h2, .card-h h3 { flex: 1; min-width: 0; }
.card-sub { color: var(--muted); font-size: .85rem; margin: -4px 0 10px; }

.grid { display: grid; gap: var(--sp); }
.grid.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid.auto { grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.grid.auto-lg { grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.tight { gap: 6px; }
.row.end { justify-content: flex-end; }
.row.between { justify-content: space-between; }
.spacer { flex: 1; }

/* ---------- 5. 元件：按鈕 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  padding: 8px 15px; border-radius: var(--r-sm); border: 1px solid var(--line-2);
  background: var(--surface); color: var(--ink); cursor: pointer; font-weight: 550; font-size: .9rem;
  transition: background .13s, border-color .13s, transform .08s;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { padding: 5px 10px; font-size: .82rem; border-radius: var(--r-xs); }
.btn.lg { padding: 12px 24px; font-size: 1rem; border-radius: var(--r-md); }
.btn.block { width: 100%; }
.btn.icon { padding: 7px; width: 34px; height: 34px; }
.btn .ic { width: 16px; height: 16px; }

.icon-btn {
  display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: var(--r-xs);
  border: 1px solid transparent; background: transparent; cursor: pointer; color: var(--muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }

/* ---------- 6. 元件：表單 ---------- */
.field { display: block; margin-bottom: 12px; }
.field > label, .field-label { display: block; font-size: .82rem; font-weight: 600; margin-bottom: 5px; color: var(--ink-2); }
.field-hint { font-size: .76rem; color: var(--muted); margin-top: 4px; }
.input, .select, .textarea {
  width: 100%; padding: 9px 12px; border: 1px solid var(--line-2); border-radius: var(--r-sm);
  background: var(--surface); color: var(--ink); transition: border-color .13s, box-shadow .13s;
}
.input:focus, .select:focus, .textarea:focus {
  outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft);
}
.textarea { min-height: 96px; resize: vertical; line-height: 1.7; }
.select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 16px) 52%, calc(100% - 11px) 52%; background-size: 5px 5px, 5px 5px; background-repeat: no-repeat; padding-right: 32px; }
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-size: .9rem; }
.check input { width: 17px; height: 17px; accent-color: var(--brand); }
.switch { position: relative; display: inline-block; width: 42px; height: 24px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--line-2); border-radius: var(--r-full); transition: background .18s; cursor: pointer; }
.switch span::before { content: ""; position: absolute; width: 18px; height: 18px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: transform .18s; box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.switch input:checked + span { background: var(--brand); }
.switch input:checked + span::before { transform: translateX(18px); }
.switch input:focus-visible + span { outline: 2px solid var(--brand); outline-offset: 2px; }
.range { width: 100%; accent-color: var(--brand); }
.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }

/* ---------- 7. 元件：徽章 / 標籤 / 進度 ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: var(--r-full);
  font-size: .74rem; font-weight: 650; background: var(--surface-3); color: var(--ink-2); white-space: nowrap;
}
.badge.brand { background: var(--brand-soft); color: var(--brand); }
.badge.ok { background: rgba(18,161,80,.14); color: var(--ok); }
.badge.warn { background: rgba(201,138,0,.16); color: var(--warn); }
.badge.danger { background: rgba(224,69,59,.14); color: var(--danger); }
.badge.purple { background: rgba(139,92,246,.15); color: var(--purple); }
.badge.teal { background: rgba(14,165,165,.15); color: var(--teal); }
.tag { display: inline-block; padding: 1px 8px; border-radius: var(--r-full); background: var(--surface-2); border: 1px solid var(--line); font-size: .74rem; color: var(--muted); }

.progress { height: 7px; background: var(--surface-3); border-radius: var(--r-full); overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--brand); border-radius: var(--r-full); transition: width .4s ease; }
.progress.thin { height: 4px; }

.meter { display: flex; align-items: center; gap: 10px; }
.meter-bar { flex: 1; }

/* ---------- 8. 元件：統計塊 ---------- */
.stat {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg);
  padding: 14px 16px; box-shadow: var(--shadow-1);
}
.stat-label { font-size: .78rem; color: var(--muted); font-weight: 600; letter-spacing: .02em; }
.stat-value { font-size: 1.7rem; font-weight: 720; letter-spacing: -.02em; margin-top: 2px; line-height: 1.15; }
.stat-value small { font-size: .8rem; font-weight: 550; color: var(--muted); margin-left: 3px; }
.stat-delta { font-size: .78rem; margin-top: 3px; font-weight: 600; }
.stat-delta.up { color: var(--danger); }   /* 華人習慣：增加用紅 */
.stat-delta.down { color: var(--ok); }
.stat-delta.flat { color: var(--muted); }

/* ---------- 9. 元件：表格 / 清單 ---------- */
.table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.table th, .table td { text-align: left; padding: 9px 11px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table th { font-size: .78rem; color: var(--muted); font-weight: 650; text-transform: uppercase; letter-spacing: .04em; white-space: nowrap; }
.table tbody tr:hover { background: var(--surface-2); }
.table td.num, .table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; }

.list-item {
  display: flex; align-items: center; gap: 11px; padding: 10px var(--sp);
  border-bottom: 1px solid var(--line);
}
.list-item:last-child { border-bottom: 0; }
.list-item:hover { background: var(--surface-2); }
.list-item .body { flex: 1; min-width: 0; }
.list-item .title { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-item .meta { font-size: .8rem; color: var(--muted); }
.list-item.done .title { text-decoration: line-through; color: var(--muted); }

/* ---------- 10. 元件：空狀態 / 載入 ---------- */
.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty .ic { width: 44px; height: 44px; opacity: .35; margin-bottom: 10px; }
.empty h3 { color: var(--ink-2); margin-bottom: 4px; }

.skeleton { background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 37%, var(--surface-2) 63%);
  background-size: 400% 100%; animation: sk 1.3s ease infinite; border-radius: var(--r-sm); height: 14px; }
@keyframes sk { 0% { background-position: 100% 50% } 100% { background-position: 0 50% } }

.spinner { width: 20px; height: 20px; border: 2px solid var(--line-2); border-top-color: var(--brand);
  border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg) } }

/* ---------- 11. 元件：Toast ---------- */
.toast-root { position: fixed; z-index: 900; bottom: 20px; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast { background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: var(--r-full);
  box-shadow: var(--shadow-3); font-size: .88rem; font-weight: 550; animation: toastIn .22s ease; max-width: 90vw; }
.toast.ok { background: var(--ok); color: #fff; }
.toast.err { background: var(--danger); color: #fff; }
.toast.warn { background: var(--warn); color: #fff; }
@keyframes toastIn { from { opacity: 0; transform: translateY(10px) } }

/* ---------- L1 行動端三件套 ---------- */
/* L1a：輸入框字級 ≥ 16px，防 iOS 聚焦時強制放大整頁 */
@media (max-width: 820px) {
  .input, .select, .textarea, .cmdk-input { font-size: 16px; }
}
/* L1b：100dvh 取代 100vh，iOS 底部不再被裁 */
@supports (height: 100dvh) {
  @media (max-width: 820px) {
    body { min-height: 100dvh; }
    .shell { min-height: 100dvh; }
    .nav { height: 100dvh; }
  }
}
/* L1c：Safe area —— iPhone Home 指示條不壓 toast / 抽屜 */
@supports (padding: env(safe-area-inset-bottom)) {
  .toast-root { bottom: calc(20px + env(safe-area-inset-bottom)); }
  .drawer-sheet, .nav.open { padding-bottom: env(safe-area-inset-bottom); }
  .modal-f { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
}
/* L1d：iOS 的 background-attachment: fixed 重繪閃爍 → 行動端改走捲動 */
@media (max-width: 820px) {
  body { background-attachment: scroll; }
}

/* L2：觸控目標 ≥ 44px —— 桌面視覺不變，用 ::after 撐出命中區 */
@media (pointer: coarse) {
  .icon-btn::after, .btn.sm::after, .chip::after, .nav-item::after, .cmdk-item::after {
    content: ""; position: absolute; inset: -10px; border-radius: inherit;
  }
  .icon-btn, .btn.sm, .chip, .nav-item, .cmdk-item { position: relative; }
}

/* ---------- 12. 元件：Modal ---------- */
.modal-root { position: fixed; inset: 0; z-index: 800; pointer-events: none; }
.modal-back { position: absolute; inset: 0; background: rgba(10, 15, 28, .5); backdrop-filter: blur(3px);
  display: grid; place-items: center; padding: 20px; pointer-events: auto; animation: fade .16s ease; }
@keyframes fade { from { opacity: 0 } }
.modal { background: var(--surface); border-radius: var(--r-xl); box-shadow: var(--shadow-3);
  width: 100%; max-width: 520px; max-height: 88vh; overflow: auto; animation: pop .2s cubic-bezier(.2,.9,.3,1.2); }
.modal.wide { max-width: 780px; }
.modal.xwide { max-width: 980px; }
.modal-h { display: flex; align-items: center; gap: 10px; padding: 18px 20px 12px; position: sticky; top: 0; background: var(--surface); z-index: 2; }
.modal-h h3 { flex: 1; font-size: 1.05rem; }
.modal-b { padding: 0 20px 18px; }
.modal-f { padding: 14px 20px 18px; display: flex; gap: 10px; justify-content: flex-end; position: sticky; bottom: 0; background: var(--surface); }
@keyframes pop { from { opacity: 0; transform: translateY(14px) scale(.98) } }

/* ---------- 13. 元件：分頁 / 篩選條 ---------- */
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: var(--sp); }
.toolbar .input { width: auto; min-width: 180px; flex: 1; max-width: 340px; }
.chips { display: flex; gap: 7px; flex-wrap: wrap; }
.chip { padding: 5px 13px; border-radius: var(--r-full); border: 1px solid var(--line-2); background: var(--surface);
  cursor: pointer; font-size: .85rem; font-weight: 550; transition: all .13s; }
.chip:hover { background: var(--surface-2); }
.chip[aria-pressed="true"] { background: var(--brand); border-color: var(--brand); color: #fff; }

/* 測驗限時倒數（A5） */
.quiz-timer {
  font-variant-numeric: tabular-nums; font-weight: 650; font-size: .92rem;
  padding: 7px 14px; border-radius: var(--r-full); margin-bottom: 12px;
  background: var(--brand-soft); color: var(--brand); display: inline-block;
}
.quiz-timer.timeup { background: rgba(224,69,59,.14); color: var(--danger); }

.pager { display: flex; gap: 8px; align-items: center; justify-content: center; margin-top: var(--sp); font-size: .88rem; }

/* ---------- 14. 學科卡片 ---------- */
.sub-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 15px;
  cursor: pointer; transition: transform .14s, box-shadow .14s, border-color .14s; position: relative;
  box-shadow: var(--shadow-1); overflow: hidden;
}
.sub-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-2); border-color: var(--line-2); text-decoration: none; }
.sub-card::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 3px; background: var(--c, var(--brand)); opacity: .9; }
.sub-card .sc-head { display: flex; gap: 11px; align-items: flex-start; }
.sub-icon { width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center; flex: none;
  background: color-mix(in srgb, var(--c, var(--brand)) 14%, transparent); color: var(--c, var(--brand)); }
.sub-icon svg { width: 21px; height: 21px; }
.sub-card h3 { font-size: .98rem; }
.sub-card .en { font-size: .74rem; color: var(--muted); font-weight: 500; }
.sub-card .desc { font-size: .83rem; color: var(--ink-2); margin-top: 8px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.sub-card .sc-foot { display: flex; align-items: center; gap: 6px; margin-top: 11px; flex-wrap: wrap; }
.sub-card .fav { position: absolute; top: 10px; right: 10px; }

/* ---------- 15. 溫習卡 ---------- */
.flip { perspective: 1400px; margin: 0 auto; max-width: 720px; }
.flip-inner { position: relative; width: 100%; min-height: 300px; transition: transform .42s cubic-bezier(.2,.8,.3,1);
  transform-style: preserve-3d; cursor: pointer; }
.flip-inner.flipped { transform: rotateY(180deg); }
.flip-face { position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-xl); box-shadow: var(--shadow-2);
  display: grid; place-items: center; padding: 32px;
  /* L3：長文不溢出——內容可捲動，不蓋住評分按鈕 */
  overflow-y: auto; -webkit-overflow-scrolling: touch; }
.flip-face.back { transform: rotateY(180deg); background: var(--surface-2); }
.flip-text { font-size: 1.4rem; line-height: 1.6; text-align: center; white-space: pre-wrap; word-break: break-word; max-width: 100%; }
.flip-face .side-tag { position: absolute; top: 14px; left: 18px; font-size: .72rem; color: var(--muted); font-weight: 650; letter-spacing: .06em; }
.rate-row { display: flex; gap: 9px; justify-content: center; margin-top: 18px; flex-wrap: wrap; }
.rate-btn { flex: 1; min-width: 90px; max-width: 150px; padding: 11px 8px; border-radius: var(--r-md);
  border: 1px solid var(--line-2); background: var(--surface); cursor: pointer; font-weight: 650; font-size: .88rem;
  display: flex; flex-direction: column; align-items: center; gap: 2px; transition: all .13s; }
.rate-btn small { font-weight: 500; font-size: .72rem; color: var(--muted); }
.rate-btn b { font-size: 1rem; }
.rate-btn:hover { transform: translateY(-1px); box-shadow: var(--shadow-1); }
.rate-btn[data-r="0"]:hover { border-color: var(--danger); color: var(--danger); }
.rate-btn[data-r="1"]:hover { border-color: var(--warn); color: var(--warn); }
.rate-btn[data-r="2"]:hover { border-color: var(--brand); color: var(--brand); }
.rate-btn[data-r="3"]:hover { border-color: var(--ok); color: var(--ok); }

/* ---------- 16. 圖表 ---------- */
.chart { width: 100%; overflow: visible; }
.chart text { fill: var(--muted); font-size: 11px; font-family: inherit; }
.chart .grid-line { stroke: var(--line); stroke-width: 1; }
.chart .axis { stroke: var(--line-2); stroke-width: 1; }
.chart .bar { transition: opacity .15s; }
.chart .bar:hover { opacity: .78; }
.heat { display: grid; grid-auto-flow: column; grid-template-rows: repeat(7, 1fr); gap: 3px; overflow-x: auto; padding-bottom: 4px; }
.heat i { width: 11px; height: 11px; border-radius: 3px; background: var(--surface-3); display: block; }
.heat i[data-l="1"] { background: color-mix(in srgb, var(--brand) 28%, var(--surface-3)); }
.heat i[data-l="2"] { background: color-mix(in srgb, var(--brand) 52%, var(--surface-3)); }
.heat i[data-l="3"] { background: color-mix(in srgb, var(--brand) 76%, var(--surface-3)); }
.heat i[data-l="4"] { background: var(--brand); }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: .78rem; color: var(--muted); align-items: center; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; margin-right: 4px; vertical-align: -1px; }

/* ---------- 17. 番茄鐘 ---------- */
.pomo { text-align: center; }
.pomo-ring { position: relative; width: 220px; height: 220px; margin: 0 auto; }
.pomo-ring svg { transform: rotate(-90deg); }
.pomo-ring circle { fill: none; stroke-width: 10; stroke-linecap: round; }
.pomo-ring .bg { stroke: var(--surface-3); }
.pomo-ring .fg { stroke: var(--brand); transition: stroke-dashoffset .5s linear, stroke .3s; }
.pomo-ring .time { position: absolute; inset: 0; display: grid; place-items: center; }
.pomo-ring .time b { font-size: 2.9rem; font-weight: 720; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.pomo-ring .time span { display: block; font-size: .78rem; color: var(--muted); font-weight: 600; }

/* ---------- 18. 命令面板 ---------- */
.cmdk { position: fixed; inset: 0; z-index: 850; background: rgba(10,15,28,.5); backdrop-filter: blur(3px);
  display: flex; justify-content: center; padding-top: 12vh; }
.cmdk-box { width: min(620px, 92vw); background: var(--surface); border-radius: var(--r-xl);
  box-shadow: var(--shadow-3); overflow: hidden; animation: pop .18s ease; align-self: flex-start; }
.cmdk-input { width: 100%; border: 0; border-bottom: 1px solid var(--line); padding: 16px 18px;
  background: transparent; font-size: 1rem; outline: none; }
.cmdk-list { max-height: 52vh; overflow-y: auto; }
.cmdk-group { padding: 8px 18px 3px; font-size: .7rem; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); font-weight: 700; }
.cmdk-item { display: flex; align-items: center; gap: 10px; padding: 9px 18px; cursor: pointer; }
.cmdk-item[aria-selected="true"] { background: var(--brand-soft); }
.cmdk-item .ic { width: 17px; height: 17px; color: var(--muted); }
.cmdk-item .kbd { margin-left: auto; }
.cmdk-item .sub { color: var(--muted); font-size: .82rem; }

/* ---------- 19. 雜項 ---------- */
.muted { color: var(--muted); }
.tiny { font-size: .78rem; }
.small { font-size: .86rem; }
.b { font-weight: 650; }
.nowrap { white-space: nowrap; }
.center { text-align: center; }
.right { text-align: right; }
.num { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }
.mt { margin-top: var(--sp); }
.mb { margin-bottom: var(--sp); }
.mt-s { margin-top: 7px; }
.mb-s { margin-bottom: 7px; }
.avatar { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; color: #fff;
  font-weight: 700; font-size: .8rem; flex: none; }
.banner { padding: 11px 15px; border-radius: var(--r-md); margin-bottom: var(--sp); border: 1px solid; font-size: .88rem; }
.banner.info { background: var(--brand-soft); border-color: color-mix(in srgb, var(--brand) 35%, transparent); }
.banner.warn { background: rgba(201,138,0,.12); border-color: rgba(201,138,0,.35); }
.banner.urgent { background: rgba(224,69,59,.12); border-color: rgba(224,69,59,.35); }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: var(--sp); overflow-x: auto; }
.tab { padding: 9px 15px; border: 0; background: transparent; cursor: pointer; color: var(--muted); font-weight: 600;
  border-bottom: 2px solid transparent; white-space: nowrap; font-size: .9rem; }
.tab[aria-selected="true"] { color: var(--brand); border-bottom-color: var(--brand); }
.md-body { line-height: 1.8; }
.md-body h1, .md-body h2, .md-body h3 { margin: .9em 0 .4em; }
.md-body ul, .md-body ol { padding-left: 22px; margin: .4em 0; }
.md-body code { background: var(--surface-3); padding: 1px 6px; border-radius: 5px; }
.md-body pre { background: var(--surface-3); padding: 12px 14px; border-radius: var(--r-md); overflow-x: auto; }
.md-body blockquote { margin: .6em 0; padding: 4px 14px; border-left: 3px solid var(--brand); color: var(--ink-2); }
.md-body table { border-collapse: collapse; width: 100%; margin: .6em 0; }
.md-body th, .md-body td { border: 1px solid var(--line); padding: 6px 10px; }

/* ---------- 19b. 工具箱 ---------- */
.tool-card { transition: transform .16s, box-shadow .16s, border-color .16s; }
.tool-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-2); border-color: var(--brand); }

.calc { max-width: 420px; }
.calc-hist { min-height: 18px; text-align: right; font-variant-numeric: tabular-nums; }
.calc-hist div:hover { text-decoration: underline; cursor: pointer; }
.calc-display {
  font-size: 1.32rem; text-align: right; padding: 12px 14px; border-radius: var(--r-md);
  background: var(--surface-2); border: 1px solid var(--line); color: var(--ink-2);
  word-break: break-all; min-height: 48px; line-height: 1.4; font-variant-numeric: tabular-nums;
}
.calc-out {
  font-size: 1.9rem; font-weight: 700; text-align: right; padding: 6px 14px 12px;
  color: var(--brand); font-variant-numeric: tabular-nums; word-break: break-all; min-height: 42px;
}
.calc-pad { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 12px; }
.calc-pad.fn { margin-top: 8px; }
.calc-key { padding: 13px 4px; font-size: 1.05rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.calc-pad.fn .calc-key { padding: 9px 2px; font-size: .82rem; }

/* ---------- 19c. 使用者下拉選單 ---------- */
.user-wrap { position: relative; }
.user-menu {
  position: absolute; top: calc(100% + 8px); right: 0; z-index: 60;
  min-width: 210px; padding: 6px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-md); box-shadow: var(--shadow-2);
  animation: um-in .13s ease-out;
}
@keyframes um-in { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }
.user-menu[data-motion-off] { animation: none; }
.user-head {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 10px 12px; border-bottom: 1px solid var(--line); margin-bottom: 6px;
}
.user-menu button.um-item {
  display: flex; align-items: center; gap: 9px; width: 100%;
  padding: 9px 10px; border: 0; background: none; border-radius: var(--r-sm);
  color: var(--ink); font-size: .9rem; text-align: left; cursor: pointer;
}
.user-menu button.um-item:hover { background: var(--surface-2); }
.user-menu button.um-item.danger { color: var(--danger); }
.user-menu button.um-item.danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }
.user-menu .um-sep { height: 1px; background: var(--line); margin: 6px 4px; }

/* ---------- 20. 響應式 ---------- */
@media (max-width: 900px) {
  .grid.g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid.g3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .nav {
    position: fixed; inset: 0 auto 0 0; width: 258px; transform: translateX(-100%);
    transition: transform .22s cubic-bezier(.3,.8,.4,1); z-index: 100; box-shadow: var(--shadow-3);
  }
  .nav.open { transform: none; }
  .nav-mask { position: fixed; inset: 0; background: rgba(10,15,28,.45); z-index: 99; animation: fade .2s; }
  .content { padding: 14px 14px 84px; }
  .grid.g2, .grid.g3, .grid.g4 { grid-template-columns: 1fr; }
  .topbar { padding: 0 12px; }
  h1 { font-size: 1.28rem; }
  .flip-text { font-size: 1.15rem; }
  .pomo-ring { width: 190px; height: 190px; }
  .pomo-ring .time b { font-size: 2.4rem; }
  .modal { max-height: 92vh; border-radius: var(--r-lg); }
}
@media print {
  .nav, .topbar, .btn, .toast-root { display: none !important; }
  .shell { grid-template-columns: 1fr; }
  .card { box-shadow: none; break-inside: avoid; }
}

/* ---------- 啟動畫面 / 無腳本提示（修 L11：原先這些 class 在 CSS 不存在，載入時是裸文字） ---------- */
.boot-screen {
  position: fixed; inset: 0; z-index: 50;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px;
  background: var(--bg); color: var(--ink-2);
  font: 600 16px/1.5 system-ui, -apple-system, "PingFang TC", "Microsoft JhengHei", sans-serif;
}
.boot-logo { color: var(--brand); animation: boot-spin 1.1s linear infinite; }
@keyframes boot-spin { to { transform: rotate(360deg); } }
.boot-text { color: var(--muted); font-weight: 500; }
.noscript-box {
  position: fixed; inset: 0; z-index: 60;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; padding: 24px; text-align: center;
  background: var(--bg); color: var(--ink);
}
.noscript-box h1 { font-size: 22px; color: var(--brand); margin: 0; }
.noscript-box p { max-width: 420px; color: var(--ink-2); }

/* ---- Phase G 工具：公式預覽 / 徽章補充 ---- */
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.ok:hover { filter: brightness(1.07); }
.badge.muted { background: var(--surface-3); color: var(--muted); }
.banner.ok { background: rgba(18,161,80,.12); border-color: rgba(18,161,80,.35); }
.banner.info { background: var(--brand-soft); border-color: color-mix(in srgb, var(--brand) 35%, transparent); }

/* 公式編輯器：LaTeX 風格即時預覽（零依賴，純 CSS） */
.math-preview {
  min-height: 64px; padding: 16px; border-radius: var(--r-md);
  background: var(--surface); border: 1px solid var(--line); text-align: center;
  font-size: 1.35rem; line-height: 2;
}
.math { display: inline; }
.frac { display: inline-flex; flex-direction: column; vertical-align: middle; text-align: center; margin: 0 .15em; }
.frac .fnum { border-bottom: 1.5px solid currentColor; padding: 0 .35em; line-height: 1.25; }
.frac .fden { padding: 0 .35em; line-height: 1.25; }
.sqrt { display: inline-flex; align-items: flex-start; }
.sqrt::before { content: "√"; font-size: 1.1em; }
.radicand { border-top: 1.5px solid currentColor; padding: 0 .35em; }
sup, sub { font-size: .72em; }
