:root {
  --paper: #f5efe2;
  --paper-deep: #ece3cf;
  --ink: #2b2620;
  --ink-soft: #5c5348;
  --cinnabar: #b3402a;
  --cinnabar-dark: #8e2f1d;
  --gold: #7c5f16;
  --green: #4a6b4f;
  --card: #fffdf7;
  --line: #d8cdb4;
  --shadow: 0 2px 10px rgba(60, 45, 20, .12);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html {
  -webkit-text-size-adjust: 100%;
  scroll-padding-top: 6.5rem;
  scroll-padding-bottom: calc(5.5rem + env(safe-area-inset-bottom));
}
body {
  font-family: "Noto Serif TC", "Songti TC", "PMingLiU", serif;
  background: var(--paper);
  background-image: radial-gradient(rgba(120, 95, 50, .05) 1px, transparent 1px);
  background-size: 22px 22px;
  color: var(--ink);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
button { font-family: inherit; color: inherit; cursor: pointer; }
button, select, input[type="search"] { min-height: 44px; }
:where(button, a, input, select, textarea, [tabindex]):focus-visible {
  outline: 3px solid #176b9b;
  outline-offset: 3px;
}
.battle-shell :where(button, [tabindex]):focus-visible { outline-color: #ffe5a3; }

/* ── 頂欄 ── */
.topbar { background: var(--card); border-bottom: 3px solid var(--cinnabar); box-shadow: var(--shadow); position: sticky; top: 0; z-index: 20; }
.brand { display: flex; align-items: baseline; gap: .6rem; padding: .7rem 1rem .3rem; flex-wrap: wrap; }
.seal {
  display: inline-flex; align-items: center; justify-content: center;
  width: 2.1rem; height: 2.1rem; border-radius: .35rem;
  background: var(--cinnabar); color: var(--paper);
  font-size: 1.35rem; font-weight: 700; align-self: center;
}
.brand h1 { font-size: 1.35rem; letter-spacing: .12em; }
.subtitle { color: var(--ink-soft); font-size: .85rem; }
.tabs {
  display: flex; overflow-x: auto; padding: 0 .5rem; gap: .15rem;
  overscroll-behavior-inline: contain; scrollbar-width: thin;
}
.tabs button {
  border: none; background: none; padding: .55rem .8rem; font-size: .95rem;
  color: var(--ink-soft); border-bottom: 3px solid transparent; white-space: nowrap;
}
.tabs button.active, .tabs button[aria-current="page"] { color: var(--cinnabar); border-bottom-color: var(--cinnabar); font-weight: 700; }

main { flex: 1; width: 100%; max-width: 880px; margin: 0 auto; padding: 1rem 1rem calc(6rem + env(safe-area-inset-bottom)); }
.view { display: none; }
.view.active { display: block; animation: fadein .25s ease; }
@keyframes fadein { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; } }

/* ── 通用卡片 ── */
.card { background: var(--card); border: 1px solid var(--line); border-radius: .7rem; box-shadow: var(--shadow); padding: 1.1rem 1.2rem; margin-bottom: 1rem; }
.card h2 { font-size: 1.15rem; color: var(--cinnabar-dark); margin-bottom: .6rem; letter-spacing: .08em; }
.card h3 { font-size: 1.02rem; margin: .8rem 0 .35rem; }
.card p { line-height: 1.85; margin-bottom: .5rem; }
.muted { color: var(--ink-soft); font-size: .88rem; }
.stage-notes { list-style: none; padding: 0; margin: .8rem 0; display: grid; gap: .5rem; }
.stage-notes li { line-height: 1.75; font-size: .92rem; border-left: 2px solid var(--line); padding-left: .6rem; }
.stage-notes strong { color: var(--cinnabar-dark); }

.pill {
  display: inline-block; padding: .1rem .55rem; border-radius: 99px;
  font-size: .8rem; border: 1px solid var(--line); background: var(--paper-deep); margin: 0 .15rem .2rem 0;
}
.pill.cat-象形 { background: #e8f0e4; border-color: #9fbc9a; color: #2f5233; }
.pill.cat-指事 { background: #e6ecf4; border-color: #93a9c8; color: #2c4568; }
.pill.cat-會意 { background: #f4ece0; border-color: #cfae7e; color: #6e4f1e; }
.pill.cat-形聲 { background: #f6e4e0; border-color: #d49a8c; color: #8e2f1d; }
.pill.cat-轉注 { background: #ece4f2; border-color: #ab93c4; color: #533670; }
.pill.cat-假借 { background: #e2f0ef; border-color: #8dbcb8; color: #1f5752; }

.btn {
  display: inline-block; border: none; border-radius: .5rem;
  padding: .6rem 1.3rem; font-size: 1rem; letter-spacing: .1em;
  background: var(--cinnabar); color: #fff; box-shadow: var(--shadow);
  transition: transform .1s;
}
.btn:active { transform: scale(.96); }
.btn.ghost { background: var(--card); color: var(--cinnabar); border: 1.5px solid var(--cinnabar); }
.btn.small { padding: .35rem .8rem; font-size: .88rem; }
.btn:disabled { opacity: 1; cursor: default; color: var(--ink-soft); background: var(--paper-deep); border-color: var(--line); box-shadow: none; }
.btn.locked { border-style: dashed; }
.btnrow { display: flex; gap: .6rem; flex-wrap: wrap; margin-top: .8rem; }

/* ── 字源探蹤軌跡 stage-strip ── */
.stage-strip { display: flex; gap: 0.5rem; overflow-x: auto; padding: 0.5rem 0; }
.stage-slot { flex: 0 0 auto; width: 96px; height: 120px; border-radius: 8px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: var(--card); border: 1px solid var(--line); }
.stage-slot img { max-width: 80px; max-height: 80px; object-fit: contain; }
.stage-slot.placeholder { border-style: dashed; color: var(--ink-soft); }
.stage-slot.placeholder .placeholder-glyph { font-size: 2.4rem; line-height: 1; color: var(--ink-soft); }
.stage-slot.placeholder .placeholder-caption { font-size: 0.7rem; text-align: center; padding: 0 4px; margin-top: .25rem; }
.stage-slot .stage-label { font-size: 0.75rem; margin-top: 4px; }

/* ── 出題 / 測驗 ── */
.q-stem { font-size: 1.08rem; line-height: 1.9; margin-bottom: .9rem; }
.q-stem .stem-char { font-size: 2.4rem; font-weight: 700; color: var(--cinnabar-dark); display: block; margin: .3rem 0; }
.quiz-options { display: grid; gap: .55rem; }
.option-btn {
  text-align: left; background: var(--card); border: 1.5px solid var(--line);
  border-radius: .55rem; padding: .65rem .9rem; font-size: 1rem; line-height: 1.6;
}
.option-btn:not(:disabled):hover { border-color: var(--cinnabar); }
.option-btn.correct { border-color: var(--green); background: #e8f0e4; font-weight: 700; }
.option-btn.wrong { border-color: #9c3b2e; background: #f6e4e0; }
.option-btn.selected { border-color: var(--gold); background: var(--paper-deep); }
.feedback { margin-top: .8rem; padding: .7rem .9rem; border-radius: .5rem; background: var(--paper-deep); line-height: 1.8; font-size: .95rem; }
.quiz-stem-image { text-align: center; margin: .5rem 0 .9rem; }
.quiz-stem-image img { max-width: 160px; max-height: 160px; }
.option-btn img { display: block; max-width: 100%; max-height: 120px; margin: 0 auto; }
.option-btn .option-caption { display: block; text-align: center; margin-top: .3rem; font-size: .85rem; color: var(--ink-soft); }
.option-btn .stage-label-fallback { display: block; text-align: center; }

/* ── 大師對戰 ── */
.battle-shell { overflow: hidden; }
.battle-shell.is-fighting { padding: 0; border-color: #8b6827; background: #1e211b; }
.battle-shell.is-fighting .battle-lobby { display: none; }
.hp-bar { height: 12px; background: rgba(16, 12, 9, .7); border: 1px solid rgba(222, 190, 116, .55); border-radius: 99px; overflow: hidden; }
.hp-fill { height: 100%; background: linear-gradient(90deg, #3c7c4c, #83bb69); transition: width .45s ease; }
.hp-fill.enemy { background: linear-gradient(90deg, #9f2b22, #dc7153); }
.battle-question { padding: 1rem 1.2rem 1.2rem; color: var(--ink); background: var(--card); }
.battle-question .q-stem { padding: .6rem .8rem; border-left: 3px solid var(--gold); background: var(--paper-deep); }
.battle-question .option-btn { background: #fffdf7; }
.battle-result { padding: 2rem 1.2rem; text-align: center; color: var(--ink); background: var(--card); }
.battle-result p { margin-top: .45rem; }
.battle-goal { margin-top: .6rem; font-size: .9rem; color: var(--ink-soft); line-height: 1.7; }
.beaten-badge { display: inline-block; margin-left: .3rem; padding: .05rem .4rem; border-radius: 99px; font-size: .75rem; background: var(--green); color: #fff; }

.foot { text-align: center; padding: 1.2rem 1rem calc(6rem + env(safe-area-inset-bottom)); color: var(--ink-soft); font-size: .8rem; line-height: 1.8; }
.foot a { color: inherit; text-underline-offset: .18em; }

@media (max-width: 480px) {
  .brand h1 { font-size: 1.15rem; }
  main { padding: .7rem .7rem calc(6rem + env(safe-area-inset-bottom)); }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto !important; }
  .view.active { animation: none !important; }
  .btn, .hp-fill { transition: none !important; }
  .btn:active { transform: none; }
}

@media (forced-colors: active) {
  .option-btn.correct, .option-btn.wrong { outline: 3px solid CanvasText; }
}
