:root {
  --bg: #f6f3ec;
  --dot: #ddd6c6;
  --panel: #fffdf8;
  --ink: #1f2430;
  --muted: #6b6f7b;
  --line: #e6e0d2;
  --accent: #7c3aed;
  --node: #ffffff;
  --shadow: 0 1px 2px rgba(31,36,48,.06), 0 4px 14px rgba(31,36,48,.07);
  --font: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, -apple-system, sans-serif;
  --hdr: 56px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171c; --dot: #262a33; --panel: #1b1e25; --ink: #e8e9ee; --muted: #9aa0ad;
    --line: #2c313b; --node: #20242c; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 14px rgba(0,0,0,.3);
  }
}
:root[data-theme="dark"] {
  --bg: #15171c; --dot: #262a33; --panel: #1b1e25; --ink: #e8e9ee; --muted: #9aa0ad;
  --line: #2c313b; --node: #20242c; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 14px rgba(0,0,0,.3);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--font); color: var(--ink); background: var(--bg); overflow: hidden; -webkit-font-smoothing: antialiased; }
button { font: inherit; color: inherit; }

/* ── Header ── */
.hdr { position: fixed; inset: 0 0 auto 0; height: var(--hdr); display: flex; align-items: center; gap: 10px;
  padding: 0 14px; background: color-mix(in srgb, var(--panel) 88%, transparent); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line); z-index: 20; }
.brand { display: flex; align-items: baseline; gap: 8px; white-space: nowrap; font-weight: 700; letter-spacing: .02em; }
.brand small { font-weight: 500; color: var(--muted); font-size: 12px; }
.brand .sym { color: var(--accent); font-family: "Times New Roman", serif; font-style: italic; font-size: 20px; }
.search { flex: 1; max-width: 320px; margin-left: auto; position: relative; }
.search input { width: 100%; height: 36px; border-radius: 10px; border: 1px solid var(--line); background: var(--node);
  color: var(--ink); padding: 0 12px 0 32px; font: inherit; font-size: 14px; outline: none; }
.search input:focus { border-color: var(--accent); }
.search svg { position: absolute; left: 10px; top: 10px; width: 16px; height: 16px; color: var(--muted); }
.search .count { position: absolute; right: 10px; top: 9px; font-size: 12px; color: var(--muted); }
.tools { display: flex; gap: 4px; }
.iconbtn { height: 36px; min-width: 36px; padding: 0 10px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--node); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px; font-size: 13px; }
.iconbtn:hover { border-color: var(--accent); }
.iconbtn svg { width: 16px; height: 16px; }

/* ── Map canvas ── */
.map { position: fixed; inset: var(--hdr) 0 0 0; }
.mm { overflow: hidden; touch-action: none; cursor: grab; user-select: none; -webkit-user-select: none;
  background-image: radial-gradient(var(--dot) 1px, transparent 1px); background-size: 22px 22px; }
.mm.dragging { cursor: grabbing; }
.mm-stage { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.mm-links { position: absolute; left: 0; top: 0; overflow: visible; }
.mm-links path { fill: none; stroke-width: 2; opacity: .45; }
.mm-nodes { position: absolute; left: 0; top: 0; }
.mm-node { position: absolute; left: 0; top: 0; width: max-content; max-width: 240px; padding: 8px 14px; border-radius: 12px;
  background: var(--node); border: 1.5px solid color-mix(in srgb, var(--c) 35%, var(--line)); box-shadow: var(--shadow);
  cursor: pointer; transition: border-color .15s, box-shadow .15s; }
.mm-node:hover { border-color: var(--c); }
.mm-node .mm-title { font-size: 14px; font-weight: 600; line-height: 1.35; }
.mm-node .mm-sum { font-size: 12px; color: var(--muted); margin-top: 2px; line-height: 1.4; }
.mm-node.depth-0 { background: var(--c); border-color: var(--c); color: #fff; padding: 14px 20px; max-width: 220px; }
.mm-node.depth-0 .mm-title { font-size: 20px; }
.mm-node.depth-0 .mm-sum { color: rgba(255,255,255,.82); }
.mm-node.depth-1 { border-left: 5px solid var(--c); padding: 10px 16px; }
.mm-node.depth-1 .mm-title { font-size: 16px; }
.mm-node.depth-3 { padding: 6px 12px; border-radius: 999px; }
.mm-node.depth-3 .mm-title { font-size: 13px; font-weight: 500; }
.mm-node.selected { box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 30%, transparent), var(--shadow); border-color: var(--c); }
.mm-node.hit { background: color-mix(in srgb, #facc15 30%, var(--node)); }
.mm-node.depth-0.hit { background: var(--c); }
.mm-toggle { position: absolute; right: -11px; top: 50%; transform: translateY(-50%); width: 22px; height: 22px;
  border-radius: 50%; border: 1.5px solid var(--c); background: var(--node); color: var(--c); font-size: 11px; font-weight: 700;
  line-height: 1; padding: 0; cursor: pointer; display: flex; align-items: center; justify-content: center; }
.mm-toggle:hover { background: var(--c); color: #fff; }
.mm-toggle::before { content: ""; position: absolute; inset: -10px; } /* 放大觸控範圍 */

/* ── Detail panel ── */
.panel { position: fixed; top: calc(var(--hdr) + 12px); right: 12px; bottom: 12px; width: min(460px, calc(100vw - 24px));
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; box-shadow: 0 10px 40px rgba(0,0,0,.12);
  display: flex; flex-direction: column; z-index: 15; transform: translateX(calc(100% + 24px)); transition: transform .28s ease; }
.panel.open { transform: none; }
.panel-head { padding: 14px 16px 10px; border-bottom: 1px solid var(--line); display: flex; gap: 10px; align-items: flex-start; }
.panel-head .grow { flex: 1; min-width: 0; }
.crumbs { font-size: 12px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 4px; }
.crumbs a { color: inherit; text-decoration: none; cursor: pointer; }
.crumbs a:hover { color: var(--accent); }
.crumbs span.sep { opacity: .5; }
.panel h2 { margin: 0; font-size: 20px; line-height: 1.3; }
.panel .sub { color: var(--muted); font-size: 13px; margin-top: 4px; }
.panel-body { flex: 1; overflow: auto; padding: 4px 18px 18px; -webkit-overflow-scrolling: touch; }
.panel-foot { padding: 10px 14px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: space-between; }
.panel-foot .iconbtn { flex: 1; }
.closebtn { border: 0; background: transparent; cursor: pointer; padding: 4px; border-radius: 8px; color: var(--muted); }
.closebtn:hover { background: var(--line); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 14px 0 4px; }
.chip { border: 1px solid var(--line); background: var(--node); border-radius: 999px; padding: 5px 12px; font-size: 13px; cursor: pointer; }
.chip:hover { border-color: var(--accent); color: var(--accent); }
.grab { display: none; }

/* ── Markdown content ── */
.md { font-size: 15px; line-height: 1.75; }
.md h1 { font-size: 22px; margin: 18px 0 8px; }
.md h2, .md h3 { font-size: 17px; margin: 18px 0 6px; }
.md p { margin: 10px 0; }
.md ul, .md ol { padding-left: 1.4em; margin: 8px 0; }
.md strong { color: var(--accent); font-weight: 700; }
.md table { border-collapse: collapse; width: 100%; font-size: 14px; margin: 12px 0; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; }
.md th { background: color-mix(in srgb, var(--accent) 7%, var(--panel)); }
.md .math-block { overflow-x: auto; overflow-y: hidden; padding: 4px 0; margin: 8px 0; }
.md code { background: var(--line); padding: 1px 5px; border-radius: 4px; font-size: .9em; }
.md blockquote { margin: 10px 0; padding: 4px 14px; border-left: 3px solid var(--accent); color: var(--muted); }
.md .katex { font-size: 1.05em; }
.empty { color: var(--muted); font-style: italic; padding: 16px 0; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(80px); background: var(--ink); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 50; transition: transform .25s; pointer-events: none; }
.toast.show { transform: translateX(-50%); }

/* ── Mobile ── */
@media (max-width: 720px) {
  .brand small, .tools .label { display: none; }
  .hdr { gap: 6px; padding: 0 10px; }
  .search { max-width: none; }
  .tools .opt { display: none; }
  .panel { top: auto; left: 0; right: 0; bottom: 0; width: 100%; height: 62vh; border-radius: 18px 18px 0 0;
    transform: translateY(calc(100% + 10px)); padding-bottom: env(safe-area-inset-bottom); }
  .panel.full { height: calc(100vh - var(--hdr)); }
  .grab { display: block; width: 44px; height: 5px; border-radius: 3px; background: var(--line); margin: 8px auto 0; cursor: pointer; }
  .panel-head { padding-top: 8px; }
  .mm-node { max-width: 200px; }
}

/* ── 練習題：看答案 ── */
.md details { margin: 6px 0 18px; border: 1px solid var(--line); border-radius: 10px; background: var(--node); overflow: hidden; }
.md details summary { cursor: pointer; padding: 7px 12px; font-size: 13px; color: var(--accent); font-weight: 600; list-style: none; user-select: none; }
.md details summary::-webkit-details-marker { display: none; }
.md details summary::before { content: "▸ "; }
.md details[open] summary::before { content: "▾ "; }
.md details[open] summary { border-bottom: 1px dashed var(--line); }
.md details > :not(summary) { padding: 0 14px; }
