:root {
  --paper: #f5efe2;
  --paper-deep: #ece3cf;
  --ink: #2b2620;
  --ink-soft: #5c5348;
  --cinnabar: #b3402a;
  --cinnabar-dark: #8e2f1d;
  --gold: #a8842c;
  --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%; }
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; cursor: pointer; }

/* ── 頂欄 ── */
.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; }
.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 { color: var(--cinnabar); border-bottom-color: var(--cinnabar); font-weight: 700; }

main { flex: 1; width: 100%; max-width: 880px; margin: 0 auto; padding: 1rem; }
.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; }
.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; }
.pill.lv-基礎 { background: #e8f0e4; }
.pill.lv-進階 { background: #f4ece0; }
.pill.lv-挑戰 { background: #f6e4e0; }
.disputed-mark { color: var(--gold); font-size: .85rem; }

.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: .45; cursor: default; }
.btnrow { display: flex; gap: .6rem; flex-wrap: wrap; margin-top: .8rem; }

/* ── 概念導讀 ── */
.origin-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: .7rem; margin: .6rem 0; }
.origin-cell { border: 1px dashed var(--line); border-radius: .5rem; padding: .7rem; background: var(--paper-deep); }
.origin-cell b { display: block; margin-bottom: .3rem; }
.fyimark { display: inline-block; width: 1.5rem; text-align: center; }
.has { color: var(--green); font-weight: 700; }
.hasnot { color: var(--ink-soft); opacity: .6; }
.concept-toc { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: .4rem; }
.example-chars { font-size: 1.5rem; letter-spacing: .35em; font-weight: 700; color: var(--cinnabar-dark); }

/* ── 說文引文與故事 ── */
.shuowen-quote {
  border-left: 4px solid var(--cinnabar); background: var(--paper-deep);
  padding: .5rem .8rem; border-radius: 0 .4rem .4rem 0;
  font-size: .95rem; margin: .3rem 0 .7rem;
}
.story-card h3 { color: var(--cinnabar-dark); margin-top: 1.3rem; letter-spacing: .06em; }
.story-card p { text-indent: 0; }
.story-characters { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .7rem; margin: 1rem 0 1.2rem; }
.story-characters figure { overflow: hidden; border: 1px solid var(--line); border-radius: .65rem; background: var(--paper-deep); }
.story-characters img { display: block; width: 100%; height: auto; aspect-ratio: 4 / 5; object-fit: cover; }
.story-characters figcaption { display: grid; gap: .15rem; padding: .55rem .65rem; line-height: 1.35; }
.story-characters figcaption b { color: var(--cinnabar-dark); }
.story-characters figcaption span { color: var(--ink-soft); font-size: .78rem; }

/* ── 字例總覽 ── */
.filterbar { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: .8rem; align-items: center; }
.filterbar select, .filterbar input {
  font-family: inherit; font-size: .92rem; padding: .4rem .6rem;
  border: 1px solid var(--line); border-radius: .45rem; background: var(--card); color: var(--ink);
}
.char-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(76px, 1fr)); gap: .5rem; }
.char-tile {
  background: var(--card); border: 1px solid var(--line); border-radius: .55rem;
  padding: .5rem .2rem; text-align: center; cursor: pointer; box-shadow: var(--shadow);
}
.char-tile .big { font-size: 1.7rem; font-weight: 700; }
.char-tile .zy { font-size: .72rem; color: var(--ink-soft); }
.char-tile.mastered { outline: 2px solid var(--green); }
.char-detail-overlay {
  position: fixed; inset: 0; background: rgba(40, 30, 15, .55); z-index: 50;
  display: flex; align-items: center; justify-content: center; padding: 1rem;
}
.char-detail { max-width: 480px; width: 100%; max-height: 85vh; overflow-y: auto; }
.char-detail .headchar { font-size: 3.2rem; font-weight: 700; color: var(--cinnabar-dark); line-height: 1.1; }

/* ── 閃卡 ── */
.flashcard {
  min-height: 260px; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; cursor: pointer; user-select: none;
}
.flashcard .headchar { font-size: 4.5rem; font-weight: 700; }
.flash-progress { text-align: center; margin-bottom: .5rem; color: var(--ink-soft); font-size: .9rem; }
.grade-row { display: flex; gap: .5rem; justify-content: center; margin-top: .9rem; flex-wrap: wrap; }
.grade-row .btn.again { background: #9c3b2e; }
.grade-row .btn.hard { background: var(--gold); }
.grade-row .btn.good { background: var(--green); }

/* ── 出題 / 對戰 ── */
.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; }
.opt-list { display: grid; gap: .55rem; }
.opt {
  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;
}
.opt:not(:disabled):hover { border-color: var(--cinnabar); }
.opt.correct { border-color: var(--green); background: #e8f0e4; font-weight: 700; }
.opt.wrong { border-color: #9c3b2e; background: #f6e4e0; }
.feedback { margin-top: .8rem; padding: .7rem .9rem; border-radius: .5rem; background: var(--paper-deep); line-height: 1.8; font-size: .95rem; }
.q-meta { display: flex; justify-content: space-between; align-items: center; margin-bottom: .6rem; color: var(--ink-soft); font-size: .88rem; flex-wrap: wrap; gap: .3rem; }

/* 血條 */
.hp-wrap { display: flex; flex-direction: column; gap: .15rem; flex: 1; min-width: 120px; }
.hp-bar { height: 14px; background: var(--paper-deep); border: 1px solid var(--line); border-radius: 99px; overflow: hidden; }
.hp-fill { height: 100%; background: linear-gradient(90deg, var(--green), #7ba86f); transition: width .4s; }
.hp-fill.enemy { background: linear-gradient(90deg, var(--cinnabar), #d4735e); }
.battle-head { display: flex; gap: 1rem; align-items: center; margin-bottom: .8rem; }
.battle-avatar { width: 72px; height: 72px; object-fit: cover; border-radius: 50%; border: 2px solid var(--gold); box-shadow: var(--shadow); }
.combo-tag { font-weight: 700; color: var(--gold); }
.roster { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: .6rem; }
.master-card { text-align: center; padding: .8rem .5rem; }
.master-portrait { display: block; width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: .55rem; border: 1px solid var(--line); margin-bottom: .45rem; }
.master-card .name { font-weight: 700; margin: .2rem 0; }
.master-card.locked { opacity: .5; filter: grayscale(1); }
.master-card .beaten { color: var(--green); font-size: .82rem; }
.shake { animation: shake .3s; }
@keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

/* ── 戰績 ── */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: .6rem; margin-bottom: .8rem; }
.stat-cell { background: var(--paper-deep); border-radius: .5rem; padding: .7rem; text-align: center; }
.stat-cell .num { font-size: 1.6rem; font-weight: 700; color: var(--cinnabar-dark); }
.stat-cell .lbl { font-size: .82rem; color: var(--ink-soft); }
.weak-list { display: flex; flex-wrap: wrap; gap: .4rem; }
.weak-item { border: 1px solid var(--line); background: var(--card); border-radius: .5rem; padding: .3rem .6rem; cursor: pointer; }
.weak-item b { font-size: 1.2rem; }
textarea.io { width: 100%; min-height: 90px; font-family: monospace; font-size: .8rem; border: 1px solid var(--line); border-radius: .5rem; padding: .5rem; background: var(--card); color: var(--ink); }

.foot { text-align: center; padding: 1.2rem 1rem 2rem; color: var(--ink-soft); font-size: .8rem; line-height: 1.8; }

@media (max-width: 480px) {
  .brand h1 { font-size: 1.15rem; }
  .flashcard .headchar { font-size: 3.4rem; }
  main { padding: .7rem; }
  .story-characters { grid-template-columns: 1fr; }
  .story-characters figure { display: grid; grid-template-columns: 42% 1fr; align-items: center; }
  .battle-avatar { width: 54px; height: 54px; }
}

/* ── 插圖 ── */
.story-fig { margin: .4rem 0 .9rem; }
.story-fig img {
  display: block; width: 100%; max-width: 100%; border-radius: .55rem;
  border: 1px solid var(--line); box-shadow: var(--shadow);
}
