/*
 * 脈絡筆記 — 樣式
 * 版面概念：左邊篩選、中間是會呼吸的圖、右邊是一張「卷宗」式的筆記面板。
 * 條文引文用明體（法典的排版慣例），其餘用無襯線。
 */
:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --ink: #161a20;
  --ink-2: #48505c;
  --muted: #78818e;
  --line: #dce1e8;
  --accent: #1f3f73;
  --accent-ink: #ffffff;
  --accent-soft: #e4ebf6;
  --warn-ink: #7a4f00;
  --warn-bg: #fbf0d6;
  --danger: #b42318;
  --other: #9aa1ab;
  --c1: #2a78d6; --c2: #eb6834; --c3: #1baf7a; --c4: #eda100;
  --c5: #e87ba4; --c6: #008300; --c7: #6250d6; --c8: #e34948;
  --shadow: 0 10px 30px rgba(20, 30, 50, 0.12);
  /* 循序色階（藍，淺→深）：領域盲點矩陣用 */
  --hm1: #cde2fb; --hm2: #9ec5f4; --hm3: #6da7ec; --hm4: #3987e5; --hm5: #256abf; --hm6: #184f95;
  --hm-ink-lo: #161a20; --hm-ink-hi: #ffffff; --hm-ink4: #161a20;
  --ok: #1b7f4d; --err: #b42318;

  --font-ui: "Inter", system-ui, -apple-system, "Segoe UI", "PingFang TC", "PingFang SC", "Hiragino Sans GB",
    "Noto Sans CJK TC", "Noto Sans TC", "Noto Sans SC", "Microsoft JhengHei", "Microsoft YaHei", sans-serif;
  --font-text: "Songti TC", "Songti SC", "Noto Serif CJK TC", "Noto Serif TC", "Noto Serif SC", "PMingLiU", "SimSun", serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1216; --surface: #161a20; --surface-2: #1d232b; --ink: #e7eaef; --ink-2: #b3bbc6; --muted: #7f8995;
    --line: #2a313b; --accent: #a3bdea; --accent-ink: #0f1216; --accent-soft: #1f2a3c;
    --warn-ink: #f2c66b; --warn-bg: #3a2d12; --danger: #ff8a80; --other: #6b7480;
    --c1: #3987e5; --c2: #d95926; --c3: #199e70; --c4: #c98500;
    --c5: #d55181; --c6: #0ca30c; --c7: #9085e9; --c8: #e66767;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
    --hm1: #104281; --hm2: #1c5cab; --hm3: #256abf; --hm4: #5598e7; --hm5: #86b6ef; --hm6: #b7d3f6;
    --hm-ink-lo: #e7eaef; --hm-ink-hi: #0f1216; --hm-ink4: #0f1216;
    --ok: #4cc38a; --err: #ff8a80;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --hm1: #104281; --hm2: #1c5cab; --hm3: #256abf; --hm4: #5598e7; --hm5: #86b6ef; --hm6: #b7d3f6;
  --hm-ink-lo: #e7eaef; --hm-ink-hi: #0f1216; --hm-ink4: #0f1216;
  --ok: #4cc38a; --err: #ff8a80;
  --bg: #0f1216; --surface: #161a20; --surface-2: #1d232b; --ink: #e7eaef; --ink-2: #b3bbc6; --muted: #7f8995;
  --line: #2a313b; --accent: #a3bdea; --accent-ink: #0f1216; --accent-soft: #1f2a3c;
  --warn-ink: #f2c66b; --warn-bg: #3a2d12; --danger: #ff8a80; --other: #6b7480;
  --c1: #3987e5; --c2: #d95926; --c3: #199e70; --c4: #c98500;
  --c5: #d55181; --c6: #0ca30c; --c7: #9085e9; --c8: #e66767;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.55 var(--font-ui);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

.app {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-block: 0 max(12px, env(safe-area-inset-bottom, 0px));
  gap: 10px;
}

/* ---------- 頂欄 ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding-block: max(10px, env(safe-area-inset-top, 0px)) 0;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; letter-spacing: 0.02em; white-space: nowrap; }
.brand svg { width: 22px; height: 22px; color: var(--accent); }
.brand .sub { font-weight: 500; font-size: 12px; color: var(--muted); letter-spacing: 0.08em; }
.search { position: relative; flex: 1 1 260px; max-width: 520px; min-width: 0; }
.search input {
  width: 100%; height: 36px; padding: 0 12px 0 34px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--surface);
}
.search::before {
  content: ""; position: absolute; left: 12px; top: 11px; width: 11px; height: 11px;
  border: 2px solid var(--muted); border-radius: 50%; pointer-events: none;
}
.search::after {
  content: ""; position: absolute; left: 23px; top: 23px; width: 6px; height: 2px; background: var(--muted);
  transform: rotate(45deg); pointer-events: none;
}
.dropdown {
  position: absolute; z-index: 30; top: 40px; left: 0; right: 0; max-height: 360px; overflow: auto;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); padding: 4px;
}
.dropdown button {
  display: flex; width: 100%; gap: 8px; align-items: center; text-align: left; border: 0; background: none;
  padding: 8px 10px; border-radius: 6px;
}
.dropdown button:hover, .dropdown button.active { background: var(--surface-2); }
.dropdown .empty { padding: 10px; color: var(--muted); }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.btn {
  height: 34px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface);
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.small { height: 28px; padding: 0 9px; font-size: 13px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- 主區 ---------- */
.main {
  min-height: 0;
  display: grid;
  grid-template-columns: 228px minmax(0, 1fr) 410px;
  gap: 10px;
}
.sidebar, .panel, .stage {
  min-height: 0; min-width: 0;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.sidebar { overflow: auto; padding: 14px; display: flex; flex-direction: column; gap: 18px; }
.side-group h4 {
  margin: 0 0 8px; font-size: 11px; font-weight: 600; letter-spacing: 0.1em; color: var(--muted);
}
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; padding: 2px; gap: 2px; background: var(--surface-2); }
.seg button { border: 0; background: none; padding: 3px 9px; border-radius: 6px; font-size: 13px; color: var(--ink-2); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,0.12); }
.check-list { display: flex; flex-direction: column; gap: 2px; }
.check {
  display: flex; align-items: center; gap: 8px; padding: 3px 4px; border-radius: 6px; cursor: pointer; user-select: none;
  font-size: 13px;
}
.check:hover { background: var(--surface-2); }
.check input { margin: 0; accent-color: var(--accent); }
.check .count { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; }
.swatch { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.swatch.square { border-radius: 2px; background: transparent !important; border: 1.5px solid currentColor; }
.stats { font-size: 12px; color: var(--muted); line-height: 1.7; font-variant-numeric: tabular-nums; }
.side-close { display: none; }

.stage { position: relative; overflow: hidden; background: var(--surface); }
#graph { position: absolute; inset: 0; }
#graph canvas { display: block; }
.stage-tools {
  position: absolute; left: 10px; top: 10px; right: 10px; display: flex; gap: 6px; flex-wrap: wrap; align-items: center;
  pointer-events: none;
}
.stage-tools > * { pointer-events: auto; }
.stage-tools .seg { background: color-mix(in srgb, var(--surface) 88%, transparent); }
.stage-tools .btn { background: color-mix(in srgb, var(--surface) 88%, transparent); }
.filter-toggle { display: none !important; }
.stage-legend {
  position: absolute; left: 10px; bottom: 10px; display: flex; gap: 10px; flex-wrap: wrap; font-size: 12px; color: var(--ink-2);
  background: color-mix(in srgb, var(--surface) 90%, transparent); border-radius: 8px; padding: 6px 10px; max-width: calc(100% - 20px);
}
.stage-legend span { display: inline-flex; align-items: center; gap: 5px; }
.lg-line { width: 18px; height: 0; border-top: 1.6px solid var(--ink-2); }
.lg-line.dashed { border-top: 1.6px dashed var(--ink); }
.lg-line.dotted { border-top: 1.4px dotted var(--muted); }
.lg-box { width: 9px; height: 9px; border: 1.5px solid var(--ink-2); border-radius: 2px; }
.demo-banner {
  position: absolute; right: 10px; bottom: 10px; max-width: 320px; font-size: 12px; color: var(--ink-2);
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; box-shadow: var(--shadow);
}
.demo-banner button { margin-top: 6px; }
.install-warn { margin-top: 6px; padding: 6px 8px; border-radius: 6px; background: var(--warn-bg); color: var(--warn-ink); }
.share-setup { display: grid; gap: 12px; }
.share-setup p { margin: 0; }
.share-grid { display: grid; grid-template-columns: minmax(170px, 230px) 1fr; gap: 16px; align-items: start; }
.qr { background: #ffffff; border: 1px solid var(--line); border-radius: 8px; padding: 6px; min-height: 120px; display: grid; place-items: center; }
.qr svg { display: block; width: 100%; height: auto; }
.qr .stats { color: #555b66; padding: 8px; }
.passphrase { font-family: var(--font-mono); font-size: 24px; font-weight: 600; letter-spacing: 0.08em; color: var(--ink); user-select: all; }
@media (max-width: 560px) { .share-grid { grid-template-columns: 1fr; } .qr { max-width: 260px; } }
.graph-empty {
  position: absolute; inset: 0; display: grid; place-items: center; text-align: center; color: var(--muted); padding: 24px;
}

/* ---------- 筆記面板 ---------- */
.panel { display: flex; flex-direction: column; overflow: hidden; }
.panel-scroll { overflow: auto; padding: 18px 18px 28px; display: flex; flex-direction: column; gap: 18px; min-height: 0; }
.panel-empty { color: var(--ink-2); display: flex; flex-direction: column; gap: 12px; }
.panel-empty h2 { margin: 0; font-size: 18px; text-wrap: balance; }
.panel-empty ol { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 6px; }
.panel-head { display: flex; flex-direction: column; gap: 8px; }
.meta-row { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.pill {
  display: inline-flex; align-items: center; gap: 5px; height: 22px; padding: 0 8px; border-radius: 11px; font-size: 12px;
  background: var(--surface-2); color: var(--ink-2); white-space: nowrap;
}
.pill.kind { font-weight: 600; color: var(--ink); }
.pill.warn { background: var(--warn-bg); color: var(--warn-ink); }
.pill .swatch { width: 8px; height: 8px; }
.panel-title { margin: 0; font-size: 20px; line-height: 1.35; text-wrap: balance; overflow-wrap: anywhere; }
.panel-actions { display: flex; gap: 4px; flex-wrap: wrap; }
.panel-close { display: none; }

.note-body { overflow-wrap: anywhere; color: var(--ink); }
.note-body p { margin: 0 0 0.7em; }
.note-body h1, .note-body h2, .note-body h3, .note-body h4 { margin: 1.1em 0 0.4em; font-size: 15px; letter-spacing: 0.02em; }
.note-body h1:first-child, .note-body h2:first-child, .note-body h3:first-child { margin-top: 0; }
.note-body ul, .note-body ol { margin: 0 0 0.7em; padding-left: 1.3em; }
.note-body blockquote {
  margin: 0 0 0.9em; padding: 8px 12px; border-left: 3px solid var(--line); background: var(--surface-2);
  font-family: var(--font-text); font-size: 15px; line-height: 1.75; border-radius: 0 6px 6px 0;
}
.note-body code { font-family: var(--font-mono); font-size: 12.5px; background: var(--surface-2); padding: 1px 4px; border-radius: 4px; }
.note-body .empty-body { color: var(--muted); }
.wl { color: var(--accent); text-decoration: none; border-bottom: 1px solid color-mix(in srgb, var(--accent) 40%, transparent); cursor: pointer; background: none; border-top: 0; border-left: 0; border-right: 0; padding: 0; font: inherit; }
.wl.ghost { color: var(--muted); border-bottom-style: dashed; }
.cite {
  font: inherit; font-size: 0.95em; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  border-radius: 5px; padding: 0 4px; margin: 0 1px; cursor: pointer; line-height: 1.4;
}
.cite:hover { border-color: var(--ink-2); }
.tag { color: var(--ink-2); font-size: 0.92em; }

.block { display: flex; flex-direction: column; gap: 8px; }
.block > h3 {
  margin: 0; font-size: 12px; letter-spacing: 0.1em; color: var(--muted); font-weight: 600;
  display: flex; align-items: baseline; gap: 8px;
}
.block > h3 .n { font-variant-numeric: tabular-nums; }
.block .hint { margin: 0; font-size: 12px; color: var(--muted); }
.rel-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.rel-list li { display: flex; align-items: center; gap: 8px; padding: 4px 6px; border-radius: 6px; min-width: 0; }
.rel-list li:hover { background: var(--surface-2); }
.rel-tag {
  flex: none; font-size: 11.5px; color: var(--ink-2); border: 1px solid var(--line); border-radius: 4px; padding: 0 5px;
  white-space: nowrap; font-variant-numeric: tabular-nums;
}
.rel-list .go { flex: 1; min-width: 0; text-align: left; background: none; border: 0; padding: 0; color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rel-list .go:hover { color: var(--accent); }
.rel-list .x { flex: none; border: 0; background: none; color: var(--muted); padding: 0 4px; border-radius: 4px; }
.rel-list .x:hover { color: var(--danger); background: var(--surface); }
.rel-note { color: var(--muted); font-size: 12px; }

.sugg-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.sugg {
  border: 1px dashed color-mix(in srgb, var(--ink) 35%, transparent); border-radius: 8px; padding: 10px 10px 8px;
  display: flex; flex-direction: column; gap: 6px;
}
.sugg-top { display: flex; align-items: center; gap: 8px; min-width: 0; }
.sugg-top .go { flex: 1; min-width: 0; text-align: left; border: 0; background: none; padding: 0; font-weight: 600; color: var(--ink); overflow-wrap: anywhere; }
.sugg-top .go:hover { color: var(--accent); }
.meter { flex: none; display: inline-flex; gap: 2px; }
.meter i { width: 5px; height: 12px; border-radius: 1.5px; background: var(--line); display: block; }
.meter i.on { background: var(--ink-2); }
.reasons { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; font-size: 12.5px; color: var(--ink-2); }
.reasons li { padding-left: 14px; position: relative; overflow-wrap: anywhere; }
.reasons li::before { content: ""; position: absolute; left: 3px; top: 0.62em; width: 5px; height: 5px; border-radius: 50%; background: var(--muted); }
.reasons li.cross::before { background: var(--ink); border-radius: 1px; transform: rotate(45deg); }
.reasons li.cross { color: var(--ink); font-weight: 500; }
.sugg-actions { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.sugg-actions select { height: 28px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); padding: 0 6px; font-size: 13px; }
.ai-out { font-size: 13px; background: var(--surface-2); border-radius: 6px; padding: 8px 10px; display: flex; flex-direction: column; gap: 4px; }
.ai-out b { font-weight: 600; }

.case-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; }
.case-col { border: 1px solid var(--line); border-radius: 8px; padding: 8px; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.case-col h5 { margin: 0; font-size: 12px; letter-spacing: 0.06em; color: var(--ink-2); }
.case-col .chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip-btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 5px; padding: 1px 6px; font-size: 12.5px; text-align: left;
  max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chip-btn:hover { border-color: var(--ink-2); }
.steps { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 3px; font-size: 12.5px; }
.steps li { display: flex; gap: 6px; align-items: baseline; color: var(--muted); }
.steps li::before { content: ""; flex: none; width: 8px; height: 8px; border-radius: 50%; border: 1.5px solid var(--muted); transform: translateY(1px); }
.steps li.done { color: var(--ink); }
.steps li.done::before { background: var(--ink); border-color: var(--ink); }
.steps li .miss { color: var(--warn-ink); font-size: 11.5px; }

.foot-meta { font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); padding-top: 10px; font-variant-numeric: tabular-nums; }

/* 編輯 */
.edit-form { display: flex; flex-direction: column; gap: 10px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 12px; color: var(--ink-2); font-weight: 600; }
.field input, .field select, .field textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 7px; background: var(--surface); padding: 7px 9px;
}
.field textarea { min-height: 240px; resize: vertical; line-height: 1.6; }
.field .help { font-size: 12px; color: var(--muted); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.form-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }

/* ---------- 對話框 ---------- */
.modal-back {
  position: fixed; inset: 0; z-index: 50; background: rgba(10, 14, 20, 0.45); display: grid; place-items: center;
  padding: max(16px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px)) max(16px, env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
}
.modal {
  background: var(--surface); color: var(--ink); border-radius: 12px; box-shadow: var(--shadow); border: 1px solid var(--line);
  width: min(760px, 100%); max-height: 100%; display: flex; flex-direction: column; overflow: hidden;
}
.modal.wide { width: min(1040px, 100%); }
.modal.narrow { width: min(420px, 100%); }
.modal-head { display: flex; align-items: center; gap: 10px; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 17px; flex: 1; text-wrap: balance; }
.modal-body { padding: 16px 18px; overflow: auto; display: flex; flex-direction: column; gap: 16px; min-height: 0; }
.modal-foot { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; align-items: center; }
.modal-foot .grow { flex: 1; color: var(--muted); font-size: 12.5px; min-width: 0; }
.modal textarea.big { width: 100%; min-height: 180px; border: 1px solid var(--line); border-radius: 8px; padding: 10px; background: var(--surface); line-height: 1.6; resize: vertical; }
.section-title { margin: 0; font-size: 13px; letter-spacing: 0.06em; color: var(--ink-2); }
.preview-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.preview-item { border: 1px solid var(--line); border-radius: 8px; padding: 10px; display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 8px 10px; }
.preview-item .fields { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr) minmax(0, 1.2fr); gap: 6px; }
.preview-item input, .preview-item select { border: 1px solid var(--line); border-radius: 6px; padding: 4px 7px; background: var(--surface); min-width: 0; width: 100%; }
.preview-item .body { grid-column: 2; font-size: 12.5px; color: var(--ink-2); white-space: pre-wrap; overflow-wrap: anywhere; max-height: 5.4em; overflow: hidden; }
.preview-item .rel { grid-column: 2; font-size: 12px; color: var(--muted); }

.insight-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 14px; }
.card { border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.card h3 { margin: 0; font-size: 14px; }
.card .desc { margin: 0; font-size: 12.5px; color: var(--muted); }
.card ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.card li { font-size: 13px; min-width: 0; overflow-wrap: anywhere; }
.pair { display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); gap: 6px; align-items: center; }
.pair .arrow { color: var(--muted); text-align: center; }
.pair .chip-btn { width: 100%; }
.stack { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; }
.gap-row .pair + .pair { margin-top: 2px; }
.gap-row { display: flex; flex-direction: column; gap: 4px; padding-bottom: 6px; border-bottom: 1px solid var(--line); }
.gap-row:last-child { border-bottom: 0; }
.gap-head { display: flex; gap: 8px; align-items: center; font-weight: 600; flex-wrap: wrap; }
.bar { height: 6px; border-radius: 3px; background: var(--surface-2); flex: 1; min-width: 60px; max-width: 140px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--ink-2); border-radius: 3px; }
.kpis { display: flex; gap: 20px; flex-wrap: wrap; }
.kpi { display: flex; flex-direction: column; }
.kpi b { font-size: 22px; font-variant-numeric: tabular-nums; }
.kpi span { font-size: 12px; color: var(--muted); }

.settings-sec { display: flex; flex-direction: column; gap: 10px; }
.settings-sec h3 { margin: 0; font-size: 14px; }
.settings-sec p { margin: 0; font-size: 12.5px; color: var(--ink-2); }
.btn-row { display: flex; gap: 6px; flex-wrap: wrap; }
.notice { font-size: 12.5px; background: var(--surface-2); border-radius: 8px; padding: 8px 10px; color: var(--ink-2); }
.notice.warn { background: var(--warn-bg); color: var(--warn-ink); }
.copy-area { width: 100%; min-height: 120px; font-family: var(--font-mono); font-size: 12px; border: 1px solid var(--line); border-radius: 8px; padding: 8px; background: var(--surface-2); }

.toast {
  position: fixed; z-index: 60; left: 50%; transform: translateX(-50%);
  bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  background: var(--ink); color: var(--bg); padding: 9px 14px; border-radius: 8px; box-shadow: var(--shadow); font-size: 13.5px;
  max-width: calc(100% - 32px);
}
.spinner { width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--ink); border-radius: 50%; animation: spin 0.8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }

/* ---------- 頁面切換、同步狀態 ---------- */
.page-tabs { flex: none; }
.page-tabs button { padding: 4px 12px; font-size: 13.5px; }
.sync-btn { gap: 7px; }
.sync-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.sync-btn[data-state="ok"] .sync-dot, .sync-btn[data-state="idle"] .sync-dot { background: var(--ok); }
.sync-btn[data-state="error"] .sync-dot { background: var(--err); }
.sync-btn[data-state="error"] .sync-text { color: var(--err); }
.sync-btn[data-state="offline"] .sync-dot { background: transparent; border: 1.5px solid var(--muted); }
.sync-btn[data-state="syncing"] .sync-dot { background: transparent; border: 2px solid var(--line); border-top-color: var(--ink); animation: spin 0.8s linear infinite; width: 11px; height: 11px; }
#sy-provider { align-self: flex-start; }
.sy-auto { display: flex; align-items: flex-end; padding-bottom: 6px; }
.help summary { cursor: pointer; font-size: 12.5px; color: var(--accent); }
.help ol { margin: 6px 0 0; padding-left: 20px; font-size: 12.5px; color: var(--ink-2); display: flex; flex-direction: column; gap: 3px; }
.hint { font-size: 12.5px; color: var(--muted); margin: 0; }

/* ---------- 意外關聯 ---------- */
.main.page-discover { grid-template-columns: minmax(0, 1fr) 410px; }
.main.page-discover .sidebar, .main.page-discover .stage { display: none; }
.discover {
  min-height: 0; min-width: 0; overflow: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.dv { padding: 18px 20px 28px; display: flex; flex-direction: column; gap: 14px; max-width: 980px; }
.dv-head { display: flex; gap: 18px; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; }
.dv-title { flex: 1 1 360px; min-width: 0; }
.dv-title h2 { margin: 0 0 4px; font-size: 20px; text-wrap: balance; }
.dv-title p { margin: 0; color: var(--ink-2); font-size: 13px; max-width: 62ch; }
.dv-tabs { align-self: flex-start; }
.dv-tabs button { padding: 4px 12px; font-size: 13.5px; }
.dv-stale { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.dv-body { display: flex; flex-direction: column; gap: 12px; }
.dv-busy { display: flex; gap: 10px; align-items: center; color: var(--ink-2); padding: 30px 0; }
.dv-filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.dv-filters select { height: 30px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface); padding: 0 8px; font-size: 13px; }
.dv-kinds { display: inline-flex; flex-wrap: wrap; gap: 2px 6px; }
.dv-kinds .check { padding: 2px 4px; font-size: 12.5px; }
.pill.strong { background: var(--accent-soft); color: var(--ink); font-weight: 600; }
.pill.explore { background: transparent; border: 1px dashed var(--accent); color: var(--ink); }
#sec-learn .stats { display: flex; flex-direction: column; gap: 6px; }
#sec-learn .seg { flex-wrap: wrap; }
.pill .x { border: 0; background: none; padding: 0 0 0 4px; color: var(--ink-2); }
.dv-list { display: flex; flex-direction: column; gap: 10px; }
.dv-card {
  border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; display: flex; flex-direction: column; gap: 8px; min-width: 0;
}
.dv-card:hover { border-color: color-mix(in srgb, var(--ink) 30%, var(--line)); }
.dv-pair { display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); gap: 8px; align-items: stretch; }
.dv-node {
  display: grid; grid-template-columns: auto minmax(0, 1fr); grid-template-rows: auto auto; column-gap: 7px; align-items: center;
  text-align: left; border: 1px solid var(--line); background: var(--surface-2); border-radius: 8px; padding: 7px 10px; min-width: 0;
}
.dv-node:hover { border-color: var(--ink-2); }
.dv-node .swatch { grid-row: 1; }
.dv-node-t { font-weight: 600; overflow-wrap: anywhere; line-height: 1.35; }
.dv-node-s { grid-column: 2; font-size: 12px; color: var(--muted); }
.dv-q { align-self: center; color: var(--muted); font-weight: 700; font-size: 15px; }
.dv-badges { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.dv-score { margin-left: auto; display: inline-flex; gap: 8px; align-items: center; }
.dv-path { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; font-size: 12.5px; }
.dv-path .rel-note { margin-right: 4px; }
.dv-path .arrow { color: var(--muted); }
.dv-end { color: var(--ink-2); }
.dv-gaps { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.dv-gaps li { display: flex; gap: 7px; align-items: center; flex-wrap: wrap; font-size: 13.5px; font-weight: 500; }
.dv-gaps .rel-note { font-weight: 400; margin-right: auto; }
.chips-wrap { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.chips-wrap .chip-btn { max-width: 14em; }

.hm-wrap { overflow-x: auto; max-width: 100%; }
.hm { border-collapse: separate; border-spacing: 2px; font-size: 12.5px; }
.hm th { font-weight: 500; color: var(--ink-2); white-space: nowrap; padding: 2px 6px; }
.hm th[scope="row"] { text-align: right; }
.hm th[scope="row"] .swatch, .hm-col .swatch { display: inline-block; margin-right: 5px; vertical-align: -1px; width: 8px; height: 8px; }
.hm thead th { vertical-align: bottom; width: 58px; max-width: 58px; }
.hm-col { display: flex; flex-direction: column; align-items: center; gap: 4px; white-space: normal; line-height: 1.25; text-align: center; padding-bottom: 2px; }
.hm-col .swatch { margin: 0; }
.hm td { padding: 0; }
.hm-off { background: transparent; }
.hm-cell {
  width: 58px; height: 46px; border: 0; border-radius: 4px; display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: var(--surface-2); color: var(--muted); line-height: 1.15; font-variant-numeric: tabular-nums;
}
.hm-cell b { font-size: 15px; }
.hm-cell span { font-size: 10.5px; opacity: 0.85; }
.hm-cell:not(:disabled):hover { outline: 2px solid var(--ink); outline-offset: 1px; }
.hm-cell:disabled { cursor: default; }
.hm-cell[data-s="1"], .hm-legend i[data-s="1"] { background: var(--hm1); color: var(--hm-ink-lo); }
.hm-cell[data-s="2"], .hm-legend i[data-s="2"] { background: var(--hm2); color: var(--hm-ink-lo); }
.hm-cell[data-s="3"], .hm-legend i[data-s="3"] { background: var(--hm3); color: var(--hm-ink-lo); }
.hm-cell[data-s="4"], .hm-legend i[data-s="4"] { background: var(--hm4); color: var(--hm-ink4); }
.hm-cell[data-s="5"], .hm-legend i[data-s="5"] { background: var(--hm5); color: var(--hm-ink-hi); }
.hm-cell[data-s="6"], .hm-legend i[data-s="6"] { background: var(--hm6); color: var(--hm-ink-hi); }
.hm-legend { display: flex; gap: 2px; align-items: center; font-size: 12px; color: var(--muted); }
.hm-legend i { width: 22px; height: 10px; border-radius: 2px; display: block; }
.hm-legend span { margin: 0 4px; }

/* ---------- 窄螢幕 ---------- */
@media (max-width: 1180px) {
  .main, .main.page-discover { grid-template-columns: minmax(0, 1fr) 380px; }
  .sidebar {
    position: fixed; z-index: 40; top: 0; bottom: 0; left: 0; width: min(300px, 86vw); border-radius: 0;
    padding-top: max(14px, env(safe-area-inset-top, 0px)); padding-left: max(14px, env(safe-area-inset-left, 0px));
    box-shadow: var(--shadow); transform: translateX(-105%); transition: transform 0.2s ease;
  }
  .sidebar.open { transform: none; }
  .side-close { display: inline-flex; align-self: flex-end; }
  .filter-toggle { display: inline-flex !important; }
}
@media (max-width: 760px) {
  .main, .main.page-discover { grid-template-columns: minmax(0, 1fr); }
  .topbar { gap: 8px; }
  .search { order: 5; flex-basis: 100%; max-width: none; }
  .sync-btn .sync-text { display: none; }
  .sync-btn { padding: 0 10px; }
  .dv { padding: 14px 14px 24px; }
  .dv-pair { grid-template-columns: minmax(0, 1fr); }
  .dv-q { justify-self: center; }
  .dv-score { margin-left: 0; }
  .hm-cell { width: 44px; height: 40px; }
  .hm thead th { width: 44px; max-width: 44px; }
  .panel {
    position: fixed; z-index: 35; left: 0; right: 0; bottom: 0; height: min(72%, 640px); border-radius: 14px 14px 0 0;
    box-shadow: var(--shadow); transform: translateY(105%); transition: transform 0.22s ease;
    padding-left: env(safe-area-inset-left, 0px); padding-right: env(safe-area-inset-right, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  .panel.open { transform: none; }
  .panel-close { display: inline-flex; }
  .actions .label-long { display: none; }
  .brand .sub { display: none; }
  .stage-legend { display: none; }
  .demo-banner { left: 10px; right: 10px; max-width: none; }
  .preview-item .fields { grid-template-columns: 1fr; }
  .row2 { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  .sidebar, .panel { transition: none; }
}
