:root {
  --bg: #f7f5f0;
  --panel: #ffffff;
  --ink: #1f2328;
  --muted: #5f6672;
  --line: #e2ddd3;
  --accent: #8c2f39;
  --accent-soft: #f6e9ea;
  --key: #8c2f39;
  --tip-bg: #fff6e0;
  --tip-line: #e7b84a;
  --ok: #1d7a4f;
  --ok-bg: #e6f4ec;
  --bad: #b3261e;
  --bad-bg: #fbe9e7;
  --blank: #e9e3d6;
  --quote-bg: #f3f0ea;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
  --fs: 17px;
  --side-w: 290px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17181b; --panel: #202226; --ink: #e8e6e1; --muted: #a3a7ae; --line: #34373d;
    --accent: #e08a93; --accent-soft: #3a2629; --key: #f0a3ab;
    --tip-bg: #33301f; --tip-line: #a8862e; --ok: #6fd3a0; --ok-bg: #1c3428; --bad: #f28b82; --bad-bg: #3b2220;
    --blank: #3a3933; --quote-bg: #26282c; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #17181b; --panel: #202226; --ink: #e8e6e1; --muted: #a3a7ae; --line: #34373d;
  --accent: #e08a93; --accent-soft: #3a2629; --key: #f0a3ab;
  --tip-bg: #33301f; --tip-line: #a8862e; --ok: #6fd3a0; --ok-bg: #1c3428; --bad: #f28b82; --bad-bg: #3b2220;
  --blank: #3a3933; --quote-bg: #26282c; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html { font-size: var(--fs); }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: "Noto Sans TC", "Microsoft JhengHei", "PingFang TC", "Heiti TC", sans-serif;
  line-height: 1.75; -webkit-text-size-adjust: 100%;
}
a { color: var(--accent); }
button { font: inherit; color: inherit; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: 12px;
  padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
  font-size: 15px;
}
.brand { font-weight: 700; color: var(--ink); text-decoration: none; white-space: nowrap; font-size: 17px; }
.brand small { font-weight: 400; color: var(--muted); margin-left: 8px; font-size: 13px; }
.search { flex: 1; max-width: 340px; margin-left: auto; }
.search input {
  width: 100%; padding: 6px 12px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); color: var(--ink); font: inherit; font-size: 14px;
}
.tools { display: flex; gap: 6px; align-items: center; }
.tool, .icon-btn {
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 5px 10px;
  cursor: pointer; text-decoration: none; color: var(--ink); white-space: nowrap; font-size: 14px;
}
.tool:hover, .icon-btn:hover { border-color: var(--accent); }
.tool.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.icon-btn { display: none; font-size: 18px; padding: 2px 10px; }
.badge { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; background: var(--bad); color: #fff; font-size: 12px; text-align: center; line-height: 18px; }
.badge:empty { display: none; }

/* layout */
.layout { display: flex; align-items: flex-start; }
.sidebar {
  position: sticky; top: 49px; width: var(--side-w); flex: none; height: calc(100vh - 49px); overflow-y: auto;
  padding: 12px 8px 40px; border-right: 1px solid var(--line); background: var(--panel); font-size: 14px; line-height: 1.5;
}
.scrim { display: none; }
main { flex: 1; min-width: 0; padding: 24px 32px 80px; outline: none; }
.page { max-width: 900px; margin: 0 auto; }

/* sidebar */
.side-home { display: block; padding: 6px 10px; border-radius: 8px; color: var(--ink); text-decoration: none; font-weight: 600; }
.side-unit { margin-top: 6px; }
.side-unit > a {
  display: flex; gap: 8px; padding: 6px 10px; border-radius: 8px; color: var(--ink); text-decoration: none; font-weight: 600;
}
.side-unit > a .no { color: var(--accent); flex: none; }
.side-unit ul { list-style: none; margin: 2px 0 4px; padding: 0 0 0 14px; display: none; }
.side-unit.open ul { display: block; }
.side-unit li a {
  display: flex; gap: 6px; padding: 4px 10px; border-radius: 6px; color: var(--muted); text-decoration: none;
}
.side-unit li a .dot { flex: none; width: 1.1em; text-align: center; }
.side-unit li a .dot.full { color: var(--ok); }
.side-unit li a .dot.part { color: var(--tip-line); }
.sidebar a:hover { background: var(--bg); }
.sidebar a.cur { background: var(--accent-soft); color: var(--accent); }
.side-extra { margin-top: 14px; padding-top: 10px; border-top: 1px solid var(--line); }
.side-extra a { display: block; padding: 6px 10px; border-radius: 8px; color: var(--ink); text-decoration: none; }

/* headings & common */
.crumb { font-size: 14px; color: var(--muted); margin-bottom: 4px; }
.crumb a { color: var(--muted); }
h1 { font-size: 1.6rem; line-height: 1.35; margin: 0 0 8px; }
h2 { font-size: 1.25rem; margin: 32px 0 12px; }
.goal { color: var(--muted); margin: 0 0 20px; }
.goal::before { content: "學習目標｜"; color: var(--accent); font-weight: 600; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 20px 24px; box-shadow: var(--shadow);
}
.btn {
  display: inline-block; border: 1px solid var(--accent); background: var(--accent); color: #fff; border-radius: 8px;
  padding: 7px 16px; cursor: pointer; text-decoration: none; font-size: 15px;
}
.btn.ghost { background: transparent; color: var(--accent); }
.btn:disabled { opacity: .5; cursor: default; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }

/* notes */
.notes { font-size: 1rem; }
.notes p { margin: 0 0 12px; }
.notes h3 {
  font-size: 1.12rem; margin: 26px 0 10px; padding-left: 10px; border-left: 4px solid var(--accent);
}
.notes h3:first-child { margin-top: 0; }
.notes ul { margin: 0 0 14px; padding-left: 1.3em; }
.notes ul ul { margin: 2px 0 4px; }
.notes li { margin: 3px 0; }
.notes li::marker { color: var(--accent); }
.list-title { font-weight: 700; margin: 14px 0 4px; }
.notes b { color: var(--key); font-weight: 700; }
.table-wrap { overflow-x: auto; margin: 6px 0 18px; }
.notes table { border-collapse: collapse; width: 100%; font-size: .94rem; background: var(--panel); }
.notes caption { text-align: left; font-weight: 700; padding: 0 0 6px; }
.notes th, .notes td { border: 1px solid var(--line); padding: 7px 10px; vertical-align: top; text-align: left; }
.notes th { background: var(--accent-soft); white-space: nowrap; }
.notes td:first-child { font-weight: 600; white-space: nowrap; }
figure { margin: 12px 0 20px; }
figure img {
  display: block; max-width: 100%; max-height: 460px; margin: 0 auto; border-radius: 8px; border: 1px solid var(--line);
  background: #fff; cursor: zoom-in;
}
figcaption { text-align: center; font-size: .88rem; color: var(--muted); margin-top: 6px; }
.tip {
  background: var(--tip-bg); border-left: 4px solid var(--tip-line); border-radius: 8px; padding: 10px 14px; margin: 10px 0 16px;
}
.tip::before { content: "段考提醒"; display: block; font-weight: 700; font-size: .85rem; color: var(--muted); }

/* cloze mode */
body.cloze .notes b {
  color: transparent; background: var(--blank); border-radius: 4px; cursor: pointer; user-select: none;
  border-bottom: 2px solid var(--muted);
}
body.cloze .notes b.shown { color: var(--key); background: transparent; border-bottom-color: transparent; }
.cloze-bar { display: none; }
body.cloze .cloze-bar {
  display: flex; gap: 10px; align-items: center; position: sticky; top: 58px; z-index: 5; margin: 0 0 14px;
  padding: 8px 12px; background: var(--accent-soft); border-radius: 8px; font-size: 14px;
}

/* timeline */
.timeline { list-style: none; margin: 0; padding: 0; border-left: 3px solid var(--line); }
.timeline li { position: relative; padding: 4px 0 8px 18px; }
.timeline li::before {
  content: ""; position: absolute; left: -8px; top: 12px; width: 11px; height: 11px; border-radius: 50%; background: var(--accent);
}
.timeline .t { font-weight: 700; color: var(--accent); margin-right: 8px; }

/* section list on unit page */
.sec-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.sec-list a {
  display: flex; gap: 12px; align-items: center; padding: 12px 16px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; text-decoration: none; color: var(--ink);
}
.sec-list a:hover { border-color: var(--accent); }
.sec-list .k { color: var(--accent); font-weight: 700; flex: none; width: 2.2em; }
.sec-list .meta { margin-left: auto; color: var(--muted); font-size: 14px; white-space: nowrap; }

/* home */
.hero { margin-bottom: 24px; }
.hero p { color: var(--muted); margin: 0; }
.unit-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.unit-card { text-decoration: none; color: var(--ink); display: flex; flex-direction: column; gap: 6px; }
.unit-card:hover { border-color: var(--accent); }
.unit-card .no { color: var(--accent); font-weight: 700; font-size: 14px; }
.unit-card h3 { margin: 0; font-size: 1.1rem; }
.unit-card p { margin: 0; color: var(--muted); font-size: 14px; line-height: 1.6; }
.bar { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; margin-top: auto; }
.bar span { display: block; height: 100%; background: var(--ok); }
.bar-label { font-size: 13px; color: var(--muted); }
.help { margin-top: 28px; font-size: 15px; }
.help ul { padding-left: 1.2em; margin: 6px 0 0; }
.picker { display: flex; flex-wrap: wrap; gap: 8px 16px; margin: 10px 0 14px; }
.picker label { display: flex; gap: 6px; align-items: center; cursor: pointer; }
select { font: inherit; padding: 4px 8px; border-radius: 6px; border: 1px solid var(--line); background: var(--panel); color: var(--ink); }

/* quiz */
.quiz-head { display: flex; align-items: baseline; gap: 12px; margin: 40px 0 12px; padding-top: 18px; border-top: 2px dashed var(--line); }
.quiz-head h2 { margin: 0; }
.quiz-head .score { margin-left: auto; color: var(--muted); font-size: 15px; }
.q {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px 20px; margin-bottom: 14px; box-shadow: var(--shadow);
}
.q-top { display: flex; gap: 10px; }
.q-no { flex: none; font-weight: 700; color: var(--accent); }
.q-text { flex: 1; }
.q-src { font-size: 12px; color: var(--muted); margin-top: 2px; }
.passage {
  background: var(--quote-bg); border-left: 3px solid var(--muted); padding: 8px 12px; margin: 0 0 10px; border-radius: 6px; font-size: .95rem;
}
.q img { display: block; max-width: 100%; max-height: 340px; margin: 8px 0; border-radius: 6px; border: 1px solid var(--line); cursor: zoom-in; background: #fff; }
.opts { display: grid; gap: 8px; margin-top: 10px; }
.opt {
  display: flex; gap: 10px; text-align: left; width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); cursor: pointer; line-height: 1.55;
}
.opt:hover:not(:disabled) { border-color: var(--accent); }
.opt .L { flex: none; font-weight: 700; color: var(--muted); }
.opt.sel { border-color: var(--accent); background: var(--accent-soft); }
.opt.right { border-color: var(--ok); background: var(--ok-bg); }
.opt.right .L { color: var(--ok); }
.opt.wrong { border-color: var(--bad); background: var(--bad-bg); }
.opt.wrong .L { color: var(--bad); }
.opt:disabled { cursor: default; }
.q-actions { margin-top: 10px; }
.ex { margin-top: 12px; padding: 10px 12px; border-radius: 8px; font-size: .95rem; }
.ex.ok { background: var(--ok-bg); }
.ex.no { background: var(--bad-bg); }
.ex strong { display: block; }
.quiz-foot { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 8px; }
.result {
  text-align: center; padding: 20px; margin-bottom: 18px;
}
.result .big { font-size: 2.4rem; font-weight: 800; color: var(--accent); line-height: 1.2; }

/* prev/next */
.pager { display: flex; justify-content: space-between; gap: 12px; margin-top: 36px; }
.pager a {
  flex: 1; max-width: 48%; padding: 12px 16px; border: 1px solid var(--line); border-radius: 10px; text-decoration: none; color: var(--ink); background: var(--panel);
}
.pager a:hover { border-color: var(--accent); }
.pager small { display: block; color: var(--muted); }
.pager .next { text-align: right; margin-left: auto; }

/* search */
.hit { display: block; padding: 12px 16px; margin-bottom: 10px; text-decoration: none; color: var(--ink); }
.hit:hover { border-color: var(--accent); }
.hit .where { font-size: 13px; color: var(--muted); }
.hit mark { background: var(--tip-bg); color: inherit; padding: 0 2px; }

/* lightbox */
.lightbox {
  position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.85); display: flex; flex-direction: column;
  align-items: center; justify-content: center; padding: 16px; cursor: zoom-out;
}
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 100%; max-height: calc(100vh - 80px); background: #fff; border-radius: 6px; }
.lightbox p { color: #eee; margin: 10px 0 0; text-align: center; }

.empty { color: var(--muted); padding: 30px 0; text-align: center; }

@media (max-width: 900px) {
  .icon-btn { display: block; }
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; height: 100vh; z-index: 50; transform: translateX(-100%);
    transition: transform .2s; box-shadow: 4px 0 20px rgba(0,0,0,.15);
  }
  body.side-open .sidebar { transform: none; }
  body.side-open .scrim { display: block; position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.3); }
  main { padding: 18px 16px 60px; }
  .brand small { display: none; }
}
@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; gap: 8px; }
  .search { order: 5; max-width: none; flex-basis: 100%; }
  .tools { margin-left: auto; }
  .tool { padding: 4px 8px; }
  .card { padding: 16px; }
  .q { padding: 14px; }
  .pager a { max-width: none; }
  .pager { flex-direction: column; }
  .pager .next { margin-left: 0; }
  body.cloze .cloze-bar { top: 96px; }
  .notes td:first-child, .notes th { white-space: normal; min-width: 4.5em; }
}
@media print {
  .topbar, .sidebar, .pager, .quiz-foot, .cloze-bar, .q-actions { display: none !important; }
  main { padding: 0; }
  body { background: #fff; }
  .q, .card { box-shadow: none; break-inside: avoid; }
  figure img { max-height: 320px; }
}
.lead { color: var(--muted); margin: 0 0 8px; }
