:root {
  --bg: #f6efe2;
  --paper: #fffaf0;
  --ink: #2b2118;
  --ink-soft: #6b5b4b;
  --seal: #8b2f2f;
  --seal-dark: #6e2222;
  --line: #e2d5bd;
  --ok: #1f7a4d;
  --ok-bg: #e4f4ea;
  --bad: #b3261e;
  --bad-bg: #fbe9e7;
  --focus: #1b5fa8;
  --shadow: 0 2px 10px rgba(80, 50, 20, .12);
  --radius: 16px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1d1814;
    --paper: #2a231d;
    --ink: #f2e8d8;
    --ink-soft: #bfae98;
    --seal: #d4685f;
    --seal-dark: #b5493f;
    --line: #473c30;
    --ok: #6fd3a0;
    --ok-bg: #173628;
    --bad: #ff8f86;
    --bad-bg: #40201d;
    --focus: #7db8f0;
    --shadow: 0 2px 10px rgba(0, 0, 0, .4);
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", "Heiti TC", system-ui, sans-serif;
  font-size: 18px;
  line-height: 1.7;
  -webkit-tap-highlight-color: transparent;
}
.serif { font-family: "Noto Serif TC", "Songti TC", "PMingLiU", serif; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* ---------- header ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px;
  background: var(--seal); color: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,.2);
}
.topbar a, .topbar button.linklike {
  color: #fff; text-decoration: none; background: rgba(255,255,255,.16);
  border: 0; border-radius: 999px; padding: 6px 14px; font-size: 16px; cursor: pointer; min-height: 40px;
  display: inline-flex; align-items: center;
}
.topbar .title { font-weight: 700; font-size: 18px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .meta { font-size: 15px; opacity: .92; white-space: nowrap; }

.wrap { max-width: 980px; margin: 0 auto; padding: 18px 16px 120px; }

/* ---------- home ---------- */
.hero { text-align: center; padding: 26px 8px 8px; }
.hero h1 { font-size: clamp(28px, 5vw, 42px); margin: 0 0 6px; color: var(--seal); letter-spacing: .08em; }
.hero p { margin: 0; color: var(--ink-soft); }
.units { display: grid; gap: 18px; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); margin-top: 24px; }
.unit-card {
  display: block; text-decoration: none; color: inherit;
  background: var(--paper); border: 2px solid var(--line); border-radius: 22px; padding: 22px;
  box-shadow: var(--shadow); transition: transform .12s ease, border-color .12s ease;
}
.unit-card:hover, .unit-card:focus-visible { transform: translateY(-2px); border-color: var(--seal); }
.unit-card .chapter { display: inline-block; background: var(--seal); color: #fff; border-radius: 999px; padding: 2px 14px; font-size: 15px; }
.unit-card h2 { margin: 10px 0 8px; font-size: 26px; }
.unit-card .stats { color: var(--ink-soft); font-size: 16px; }
.unit-card .go { margin-top: 16px; display: inline-block; font-weight: 700; color: var(--seal); }
.unit-card .best { margin-top: 6px; font-size: 15px; color: var(--ok); }
.howto { margin-top: 28px; background: var(--paper); border: 1px dashed var(--line); border-radius: var(--radius); padding: 16px 20px; color: var(--ink-soft); font-size: 16px; }
.howto ol { margin: 6px 0 0; padding-left: 22px; }

/* ---------- cards viewer ---------- */
.viewer-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.viewer-head h2 { margin: 0; font-size: 22px; color: var(--seal); }
.viewer-head .count { color: var(--ink-soft); font-size: 16px; }
.hint { color: var(--ink-soft); font-size: 15px; margin: 0 0 10px; }
.stage {
  background: var(--paper); border: 2px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 8px; text-align: center;
}
.stage button.imgbtn { display: block; width: 100%; border: 0; background: none; padding: 0; cursor: zoom-in; }
.stage img { width: 100%; max-width: 760px; height: auto; border-radius: 10px; display: block; margin: 0 auto; background: #eadfc9; }
.stage .cap { font-weight: 700; margin: 10px 0 2px; }
.navrow { display: flex; gap: 10px; margin: 14px 0; }
.btn {
  min-height: 52px; padding: 10px 22px; border-radius: 14px; border: 2px solid var(--seal);
  background: var(--paper); color: var(--seal); font-weight: 700; font-size: 18px; cursor: pointer;
}
.btn:hover { background: color-mix(in srgb, var(--seal) 10%, var(--paper)); }
.btn.primary { background: var(--seal); color: #fff; }
.btn.primary:hover { background: var(--seal-dark); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.navrow .btn { flex: 1; }
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 10px; margin-top: 6px; }
.thumb { border: 2px solid var(--line); border-radius: 10px; background: var(--paper); padding: 4px; cursor: pointer; text-align: center; font-size: 13px; line-height: 1.3; }
.thumb img { width: 100%; aspect-ratio: 3/4; object-fit: cover; object-position: top; border-radius: 6px; display: block; }
.thumb[aria-current="true"] { border-color: var(--seal); box-shadow: 0 0 0 2px var(--seal); }
.thumb span { display: block; margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.startbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
  background: color-mix(in srgb, var(--paper) 94%, transparent); backdrop-filter: blur(6px);
  border-top: 2px solid var(--line); padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
}
.startbar .in { max-width: 980px; margin: 0 auto; display: flex; gap: 14px; align-items: center; flex-wrap: wrap; justify-content: space-between; }
.startbar label { display: inline-flex; align-items: center; gap: 8px; font-size: 16px; cursor: pointer; min-height: 44px; }
.startbar input[type=checkbox] { width: 22px; height: 22px; accent-color: var(--seal); }
.startbar .btn { min-width: 220px; }

/* zoom overlay */
.overlay { position: fixed; inset: 0; z-index: 50; background: rgba(15, 10, 5, .92); display: none; flex-direction: column; }
.overlay.open { display: flex; }
.overlay .bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 14px; color: #fff; }
.overlay .bar .btn { min-height: 44px; background: #fff; border-color: #fff; color: var(--seal-dark); }
.overlay .scroller { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; touch-action: pan-x pan-y pinch-zoom; text-align: center; padding: 0 8px 24px; }
.overlay img { width: min(100%, 1100px); height: auto; border-radius: 8px; }

/* ---------- quiz ---------- */
.progress { height: 10px; background: var(--line); border-radius: 999px; overflow: hidden; margin: 4px 0 14px; }
.progress > i { display: block; height: 100%; background: var(--seal); width: 0; transition: width .25s ease; }
.qhead { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; color: var(--ink-soft); font-size: 16px; }
.qhead .btn { min-height: 44px; font-size: 16px; padding: 6px 16px; }
.question { font-size: clamp(21px, 3.2vw, 26px); font-weight: 700; line-height: 1.6; margin: 12px 0 18px; }
.opts { display: grid; gap: 12px; }
.opt {
  display: flex; gap: 14px; align-items: flex-start; text-align: left; width: 100%;
  min-height: 64px; padding: 14px 18px; border-radius: 14px; border: 2px solid var(--line);
  background: var(--paper); cursor: pointer; font-size: 20px; line-height: 1.55;
  box-shadow: var(--shadow);
}
.opt:hover:not(:disabled) { border-color: var(--seal); }
.opt .key {
  flex: none; width: 36px; height: 36px; border-radius: 50%; border: 2px solid var(--seal); color: var(--seal);
  display: inline-flex; align-items: center; justify-content: center; font-weight: 700; font-size: 18px; margin-top: 1px;
}
.opt .txt { flex: 1; }
.opt .mark { flex: none; font-weight: 700; font-size: 16px; margin-top: 3px; display: none; }
.opt:disabled { cursor: default; }
.opt.correct { border-color: var(--ok); background: var(--ok-bg); }
.opt.correct .key { background: var(--ok); border-color: var(--ok); color: #fff; }
.opt.correct .mark { display: inline; color: var(--ok); }
.opt.wrong { border-color: var(--bad); background: var(--bad-bg); }
.opt.wrong .key { background: var(--bad); border-color: var(--bad); color: #fff; }
.opt.wrong .mark { display: inline; color: var(--bad); }
.opt.dim { opacity: .6; }

.feedback { margin-top: 18px; border-radius: var(--radius); padding: 16px 20px; border: 2px solid; }
.feedback.ok { border-color: var(--ok); background: var(--ok-bg); }
.feedback.no { border-color: var(--bad); background: var(--bad-bg); }
.feedback h3 { margin: 0 0 6px; font-size: 22px; }
.feedback.ok h3 { color: var(--ok); }
.feedback.no h3 { color: var(--bad); }
.feedback .row { margin: 6px 0; }
.feedback .lab { font-weight: 700; }
.feedback .src { font-size: 15px; color: var(--ink-soft); }
.nextbar { position: sticky; bottom: 0; padding: 12px 0 calc(12px + env(safe-area-inset-bottom)); background: linear-gradient(to top, var(--bg) 70%, transparent); margin-top: 10px; }
.nextbar .btn { width: 100%; }

/* ---------- result ---------- */
.score { text-align: center; background: var(--paper); border: 2px solid var(--line); border-radius: 24px; padding: 26px 18px; box-shadow: var(--shadow); }
.score .big { font-size: 64px; font-weight: 800; color: var(--seal); line-height: 1.1; }
.score .big small { font-size: 28px; color: var(--ink-soft); font-weight: 600; }
.score p { margin: 8px 0 0; }
.actions { display: flex; flex-wrap: wrap; gap: 12px; margin: 18px 0; }
.actions .btn { flex: 1 1 200px; }
.review h3 { margin: 22px 0 8px; font-size: 22px; color: var(--seal); }
.rv { background: var(--paper); border: 2px solid var(--line); border-radius: var(--radius); padding: 14px 18px; margin-bottom: 12px; }
.rv .q { font-weight: 700; }
.rv .a { color: var(--ok); font-weight: 700; margin-top: 4px; }
.rv .you { color: var(--bad); font-size: 16px; }
.rv .e { margin-top: 4px; }
.rv .s { font-size: 14px; color: var(--ink-soft); margin-top: 4px; }
.allgood { text-align: center; color: var(--ok); font-weight: 700; padding: 14px; }

[hidden] { display: none !important; }
@media (max-width: 560px) {
  body { font-size: 17px; }
  .opt { font-size: 18px; padding: 12px 14px; }
  .startbar .btn { width: 100%; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
