/* ============ 五十音の旅 — 和風主題 ============ */
:root {
  --kinari: #FAF6EE;
  --washi:  #F3EDE2;
  --washi2: #EAE0CF;
  --sumi:   #2B2A28;
  --sumi-soft: #6B665E;
  --ai:     #1B4B6B;
  --asagi:  #3A8FB7;
  --shu:    #C73E3A;
  --sakura: #FEDFE1;
  --matcha: #7D9C5B;
  --kin:    #C8A24B;
  --card-shadow: 0 2px 8px rgba(43, 42, 40, .08);
  --radius: 14px;
  --accent: var(--ai);        /* 章節主題色:片假名=藍 */
  --accent-soft: #DCE9F2;
}

/* 深色變數(共用):套用於「系統深色且未強制淺色」或「手動選深色」 */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --kinari: #1F1D1B;
    --washi:  #2A2724;
    --washi2: #35312C;
    --sumi:   #EFE9DE;
    --sumi-soft: #A79F92;
    --ai:     #5FA8C9;
    --asagi:  #7BC0DC;
    --shu:    #E0655F;
    --sakura: #5A3A3E;
    --matcha: #9DBB7F;
    --kin:    #D9B65E;
    --accent-soft: #24323C;
    --card-shadow: 0 2px 8px rgba(0, 0, 0, .35);
  }
}
:root[data-theme="dark"] {
  --kinari: #1F1D1B;
  --washi:  #2A2724;
  --washi2: #35312C;
  --sumi:   #EFE9DE;
  --sumi-soft: #A79F92;
  --ai:     #5FA8C9;
  --asagi:  #7BC0DC;
  --shu:    #E0655F;
  --sakura: #5A3A3E;
  --matcha: #9DBB7F;
  --kin:    #D9B65E;
  --accent-soft: #24323C;
  --card-shadow: 0 2px 8px rgba(0, 0, 0, .35);
}

/* 平假名章節主題(朱色) */
.theme-h { --accent: var(--shu); --accent-soft: var(--sakura); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-h { --accent-soft: #3C2A2C; }
}
:root[data-theme="dark"] .theme-h { --accent-soft: #3C2A2C; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* ---------- 自創 icon ---------- */
.icon { width: 1em; height: 1em; display: inline-block; vertical-align: -0.14em; flex: none; }
.icon.inline { vertical-align: -0.14em; margin-right: 2px; }
.brand-ico { width: 1.1em; height: 1.1em; vertical-align: -0.18em; }
.big-ico { display: inline-block; font-size: 52px; color: var(--accent); line-height: 1; margin: 4px auto 8px; }
.big-ico .icon { width: 1em; height: 1em; }
.card-ico { font-size: 60px; color: var(--accent); line-height: 1; }
.prompt-ico { font-size: 48px; color: var(--accent); line-height: 1; }
.node-ico { font-size: 1.9rem; color: var(--accent); line-height: 1; display: inline-flex; }
.lock-ico { font-size: 1.2rem; color: var(--sumi-soft); display: inline-flex; }
.icon-btn { font-size: 1.3rem; color: var(--sumi); display: inline-flex; padding: 2px; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--kinari);
  color: var(--sumi);
  font-family: "Zen Maru Gothic", "Hiragino Maru Gothic ProN", "Noto Sans JP",
               "Hiragino Kaku Gothic ProN", "Yu Gothic", "PingFang TC", "Meiryo", sans-serif;
  font-size: 16px;
  line-height: 1.6;
}

/* 教學假名大字:教科書體 */
.kana-glyph { font-family: "Klee One", "Hiragino Mincho ProN", "Yu Mincho", serif; }

body {
  min-height: 100svh;
  background: var(--kinari);
}

#app { max-width: 520px; margin: 0 auto; padding: 0 16px 96px; }

@media (min-width: 768px) { #app { max-width: 680px; } }

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  border: none;
  background: none;
  touch-action: manipulation;
}

h1, h2, h3 { line-height: 1.3; margin: 0.4em 0; }

/* ---------- 頂欄 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 4px;
  background: color-mix(in srgb, var(--kinari) 88%, transparent);
  backdrop-filter: blur(8px);
}
.topbar .brand { font-weight: 700; font-size: 1.05rem; color: var(--accent); flex: 1; }
.stat-chip {
  display: inline-flex; align-items: center; gap: 4px;
  background: var(--washi); border-radius: 999px; padding: 4px 10px;
  font-size: .85rem; box-shadow: var(--card-shadow);
}

/* ---------- 青海波 hero ---------- */
.hero {
  position: relative;
  border-radius: var(--radius);
  background: var(--accent);
  color: #FAF6EE;
  padding: 22px 20px 26px;
  overflow: hidden;
  box-shadow: var(--card-shadow);
}
.hero .seigaiha { position: absolute; inset: 0; opacity: .16; pointer-events: none; }
.hero h1 { font-size: 1.4rem; margin: 0 0 4px; }
.hero p { margin: 0; opacity: .92; font-size: .92rem; }

/* ---------- 冒險地圖 ---------- */
.map { list-style: none; padding: 8px 0; margin: 16px 0; }
.map li { display: flex; align-items: stretch; gap: 12px; }
.map .rail {
  display: flex; flex-direction: column; align-items: center; width: 34px; flex: none;
}
.map .rail .dot {
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--washi2); border: 3px solid var(--washi2); margin-top: 26px; flex: none;
}
.map li.done .rail .dot { background: var(--matcha); border-color: var(--matcha); }
.map li.current .rail .dot { background: var(--kinari); border-color: var(--accent); }
.map .rail .line { width: 3px; flex: 1; background: var(--washi2); }
.map li.done .rail .line { background: var(--matcha); }
.map li:last-child .rail .line { visibility: hidden; }

.node {
  flex: 1; display: flex; align-items: center; gap: 12px; text-align: left;
  background: var(--washi); border-radius: var(--radius);
  padding: 12px 14px; margin: 6px 0; box-shadow: var(--card-shadow);
  border: 2px solid transparent;
  transition: transform .15s;
}
.node:active { transform: scale(.98); }
.node .emoji { font-size: 1.7rem; flex: none; }
.node .t { flex: 1; min-width: 0; }
.node .t b { display: block; font-size: .98rem; }
.node .t small { color: var(--sumi-soft); font-size: .8rem; }
li.current .node { border-color: var(--accent); }
li.locked .node { opacity: .45; filter: grayscale(.7); }
li.boss .node { border-style: dashed; border-color: var(--kin); }

/* 判子印章 */
.hanko {
  flex: none; width: 44px; height: 44px; border-radius: 50%;
  border: 3px solid var(--shu); color: var(--shu);
  display: grid; place-items: center;
  font-family: "Yuji Syuku", "Klee One", serif;
  font-size: 1.2rem; transform: rotate(-8deg);
  background: color-mix(in srgb, var(--shu) 8%, transparent);
}
.hanko.stamp-in { animation: stampIn .45s cubic-bezier(.2, 2.2, .4, 1) both; }
@keyframes stampIn { from { transform: rotate(-8deg) scale(2.4); opacity: 0; } to { transform: rotate(-8deg) scale(1); opacity: 1; } }

.badge-chip { display: inline-block; background: color-mix(in srgb, var(--kin) 18%, transparent);
  border: 1px solid var(--kin); border-radius: 999px; padding: 3px 12px; margin: 3px; font-size: .85rem; }

/* ---------- 卡片 ---------- */
.card {
  background: var(--washi); border-radius: var(--radius);
  padding: 18px; margin: 14px 0; box-shadow: var(--card-shadow);
  border: 1px solid color-mix(in srgb, var(--kin) 20%, transparent);
}

.progressbar { height: 8px; border-radius: 4px; background: var(--washi2); overflow: hidden; margin: 12px 0; }
.progressbar > div { height: 100%; background: var(--accent); border-radius: 4px; transition: width .3s; }

.stepper { display: flex; gap: 6px; justify-content: center; font-size: .78rem; color: var(--sumi-soft); flex-wrap: wrap; }
.stepper .on { color: var(--accent); font-weight: 700; }

/* ---------- 教學大卡 ---------- */
.learn-card { text-align: center; }
.learn-card .glyph {
  font-size: clamp(88px, 30vw, 150px); line-height: 1.15; color: var(--accent);
}
.learn-card .romaji { font-size: 1.6rem; letter-spacing: .1em; color: var(--sumi-soft); }
.learn-card .pair { font-size: .95rem; color: var(--sumi-soft); }
.learn-card .pair .kana-glyph { font-size: 1.3rem; color: var(--sumi); }

.speak-btn {
  display: inline-grid; place-items: center;
  width: 54px; height: 54px; border-radius: 50%;
  background: var(--accent); color: #FAF6EE; font-size: 1.5rem;
  box-shadow: var(--card-shadow); margin: 8px;
}
.speak-btn .icon { width: 1em; height: 1em; }
.speak-btn:active { transform: scale(.94); }
.center-block { display: flex; margin-left: auto; margin-right: auto; }

/* 聽力題:大播放鈕 */
.listen-play {
  display: inline-grid; place-items: center;
  width: 96px; height: 96px; border-radius: 50%;
  background: var(--accent); color: #FAF6EE; font-size: 3rem;
  box-shadow: var(--card-shadow); margin: 6px auto;
}
.listen-play .icon { width: 1em; height: 1em; }
.listen-play:active { transform: scale(.96); }

/* 拼字提示列 */
.romaji-prompt { font-size: 1.5rem; letter-spacing: .08em; color: var(--sumi-soft); margin-top: 4px; }

/* ---------- 口說練習 ---------- */
.speak-card .glyph { font-size: clamp(52px, 18vw, 88px); }
.mic-area { text-align: center; margin: 14px 0 8px; }
.mic-btn {
  display: inline-grid; place-items: center;
  width: 84px; height: 84px; border-radius: 50%;
  background: var(--shu); color: #FAF6EE; font-size: 2.2rem;
  box-shadow: var(--card-shadow);
}
.mic-btn .icon { width: 1em; height: 1em; }
.mic-btn:active { transform: scale(.95); }
.mic-btn.disabled { background: var(--washi2); color: var(--sumi-soft); }
.mic-btn.listening { animation: micPulse 1s infinite; }
@keyframes micPulse {
  0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--shu) 55%, transparent); }
  50% { box-shadow: 0 0 0 14px color-mix(in srgb, var(--shu) 0%, transparent); }
}
.mic-hint { margin-top: 8px; font-size: .88rem; }

/* ---------- 手寫練習 ---------- */
.write-card { text-align: center; }
.write-target { display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 6px; }
.write-target .glyph-sm { font-size: 2.4rem; color: var(--accent); line-height: 1; }
.write-target .romaji { font-size: 1.2rem; color: var(--sumi-soft); }
.speak-btn.sm { width: 40px; height: 40px; font-size: 1.1rem; margin: 0; }
.hw-wrap {
  position: relative; margin: 10px auto; border-radius: 16px;
  background: var(--kinari); border: 2px dashed var(--washi2);
  box-shadow: inset 0 1px 4px rgba(43, 42, 40, .06);
  touch-action: none; overflow: hidden;
}
.hw-guide, .hw-ink { position: absolute; inset: 0; border-radius: 14px; display: block; }
.hw-ink { touch-action: none; cursor: crosshair; }
.write-controls { display: flex; gap: 10px; margin: 10px 0 2px; }
.write-controls .btn-ghost { margin: 0; padding: 10px; font-size: .9rem; }
.stroke-hint { font-size: .85rem; margin: 2px 0 8px; }
.stroke-hint b { color: var(--accent); }

/* 羅馬拼音提示連結 */
.hint-link {
  display: inline-block; margin-top: 6px; padding: 4px 12px;
  font-size: .82rem; color: var(--accent);
  border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
  border-radius: 999px; background: transparent;
}
.hint-link:active { transform: scale(.96); }

/* ---------- 測驗 ---------- */
/* 情境敘述框 */
.scene {
  background: var(--accent-soft);
  border-left: 4px solid var(--accent);
  border-radius: 10px;
  padding: 10px 14px;
  margin: 4px 0 10px;
  font-size: .95rem;
  line-height: 1.55;
  color: var(--sumi);
}

.quiz-prompt { text-align: center; margin: 8px 0 4px; }
.quiz-prompt .big {
  font-size: clamp(64px, 22vw, 110px); line-height: 1.2; color: var(--sumi);
}
.quiz-prompt .word-big { font-size: clamp(38px, 12vw, 64px); }
.quiz-q { text-align: center; color: var(--sumi-soft); font-size: .95rem; margin-bottom: 10px; }

.options { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.options.single-col { grid-template-columns: 1fr; }
.opt {
  background: var(--washi); border: 2px solid var(--washi2);
  border-radius: var(--radius); padding: 16px 8px;
  font-size: 1.15rem; min-height: 60px;
  box-shadow: var(--card-shadow);
  transition: transform .1s, border-color .15s, background .15s;
}
.opt.kana-glyph { font-size: 2rem; }
.opt:active { transform: scale(.97); }
.opt.correct { border-color: var(--matcha); background: color-mix(in srgb, var(--matcha) 18%, var(--washi)); animation: pop .3s; }
.opt.wrong { border-color: var(--shu); background: color-mix(in srgb, var(--shu) 14%, var(--washi)); animation: shake .35s; }
.opt:disabled { cursor: default; }
@keyframes pop { 50% { transform: scale(1.06); } }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

.feedback { text-align: center; min-height: 32px; font-weight: 700; margin-top: 10px; }
.feedback.good { color: var(--matcha); }
.feedback.bad { color: var(--shu); }

/* 配對 */
.match-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.match-col { display: grid; gap: 10px; }
.opt.selected { border-color: var(--asagi); background: var(--accent-soft); }
.opt.matched { opacity: .35; border-color: var(--matcha); }

/* 拼字 tile */
.assembly-slots {
  display: flex; gap: 8px; justify-content: center; flex-wrap: wrap;
  min-height: 64px; margin: 12px 0; padding: 10px;
  border-bottom: 3px dashed var(--washi2);
}
.slot-tile, .tile {
  min-width: 52px; height: 56px; padding: 0 8px;
  display: grid; place-items: center;
  background: var(--washi); border: 2px solid var(--washi2); border-radius: 12px;
  font-size: 1.6rem; box-shadow: var(--card-shadow);
}
.tile:active { transform: scale(.95); }
.tile.used { opacity: .25; pointer-events: none; }
.tiles { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

/* ---------- 單字卡(翻卡) ---------- */
.flashcard { perspective: 1000px; height: 300px; margin: 12px 0; }
.flashcard .inner {
  position: relative; width: 100%; height: 100%;
  transform-style: preserve-3d; transition: transform .5s;
}
.flashcard.flipped .inner { transform: rotateY(180deg); }
.flashcard .face {
  position: absolute; inset: 0; backface-visibility: hidden;
  background: var(--washi); border-radius: var(--radius);
  border: 1px solid color-mix(in srgb, var(--kin) 25%, transparent);
  box-shadow: var(--card-shadow);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 6px; padding: 18px; text-align: center;
}
.flashcard .back { transform: rotateY(180deg); }
.flashcard .face .w { font-size: clamp(40px, 14vw, 64px); color: var(--accent); }
.flashcard .face .e { font-size: 2.4rem; }
.flashcard .face .zh { font-size: 1.3rem; font-weight: 700; }
.flashcard .face .note { font-size: .88rem; color: var(--sumi-soft); }
.mark-hint { font-size: .8rem; color: var(--kin); border: 1px dashed var(--kin); border-radius: 8px; padding: 2px 8px; }

.grade-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.grade-row .opt { font-size: .9rem; min-height: 52px; padding: 8px 4px; }

/* ---------- 五十音圖 ---------- */
.chart-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.chart-cell {
  aspect-ratio: 1; display: grid; place-items: center; border-radius: 10px;
  background: var(--washi2); color: var(--sumi-soft); font-size: 1.4rem;
  position: relative; direction: ltr;
}
.chart-cell.lv0 { background: var(--washi2); opacity: .5; }
.chart-cell.lv1 { background: color-mix(in srgb, var(--accent) 15%, var(--washi)); color: var(--sumi); }
.chart-cell.lv2 { background: color-mix(in srgb, var(--accent) 38%, var(--washi)); color: var(--sumi); }
.chart-cell.lv3 { background: var(--accent); color: #FAF6EE; }
.chart-cell small { position: absolute; bottom: 2px; right: 5px; font-size: .55rem; opacity: .8; }
.seg-toggle { display: flex; gap: 8px; margin: 10px 0; }
.seg-toggle button { flex: 1; padding: 9px; border-radius: 999px; background: var(--washi); box-shadow: var(--card-shadow); }
.seg-toggle button.on { background: var(--accent); color: #FAF6EE; font-weight: 700; }

/* ---------- 單字圖片/媒體 ---------- */
.word-media { display: inline-flex; align-items: center; justify-content: center; overflow: hidden; line-height: 0; }
.word-media img { width: 100%; height: 100%; object-fit: cover; display: block; }
.word-media .media-icon { display: none; line-height: 1; color: var(--accent); }
.word-media.no-img .media-icon,
.word-media.img-failed .media-icon { display: inline-flex; }
.word-media.img-failed img { display: none; }

.card-media { width: 100%; max-width: 240px; height: 150px; border-radius: 12px;
  background: var(--washi2); margin: 0 auto 8px; font-size: 60px; }

/* ---------- 單字圖卡資料庫 ---------- */
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0 14px; }
.chip { padding: 6px 14px; border-radius: 999px; background: var(--washi); box-shadow: var(--card-shadow); font-size: .9rem; }
.chip.on { background: var(--accent); color: #FAF6EE; font-weight: 700; }
.wl-sec { margin: 16px 0; }
.wl-day { font-size: 1rem; margin: 10px 0 8px; }
.wl-lock { font-size: .72rem; color: var(--sumi-soft); border: 1px solid var(--washi2); border-radius: 8px; padding: 1px 7px; margin-left: 4px; }
.wl-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
@media (min-width: 600px) { .wl-grid { grid-template-columns: repeat(4, 1fr); } }
.wl-card { display: flex; flex-direction: column; align-items: center; text-align: center;
  background: var(--washi); border-radius: 12px; padding: 8px; box-shadow: var(--card-shadow);
  border: 1px solid color-mix(in srgb, var(--kin) 16%, transparent); }
.wl-card:active { transform: scale(.97); }
.wl-card.locked { opacity: .5; filter: grayscale(.6); }
.wl-media { width: 100%; height: 92px; border-radius: 10px; background: var(--washi2); font-size: 38px; margin-bottom: 6px; }
.wl-kana { font-size: 1.4rem; color: var(--accent); line-height: 1.2; margin-top: 2px; }
.wl-zh { font-size: .85rem; }
.wl-rom { font-size: .74rem; }
.wl-card.locked .lock-ico { font-size: 1.4rem; margin: 14px 0; }
/* 6 個分頁時縮小一點 */
.tabbar a { min-width: 50px; padding: 4px 8px; }

/* ---------- 主要按鈕 ---------- */
.btn-primary {
  display: block; width: 100%; padding: 15px;
  background: var(--accent); color: #FAF6EE;
  border-radius: var(--radius); font-size: 1.05rem; font-weight: 700;
  box-shadow: var(--card-shadow); margin: 14px 0;
}
.btn-primary:active { transform: scale(.98); }
.btn-primary:disabled { opacity: .4; }
.btn-ghost {
  display: block; width: 100%; padding: 13px; border-radius: var(--radius);
  border: 2px solid var(--accent); color: var(--accent); font-weight: 700; margin: 10px 0;
}
.btn-danger { border-color: var(--shu); color: var(--shu); }

/* ---------- 底部導覽 ---------- */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  display: flex; justify-content: space-around;
  background: color-mix(in srgb, var(--washi) 94%, transparent);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--washi2);
  padding: 6px 0 max(6px, env(safe-area-inset-bottom));
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  text-decoration: none; color: var(--sumi-soft); font-size: .68rem;
  padding: 4px 12px; border-radius: 10px; min-width: 56px;
}
.tabbar a .ico { font-size: 1.3rem; }
.tabbar a.on { color: var(--accent); font-weight: 700; }

/* ---------- 結業 ---------- */
.confetti-burst { text-align: center; font-size: 3rem; animation: pop .5s; }
.cert {
  border: 3px double var(--kin); border-radius: var(--radius);
  padding: 24px; text-align: center; background: var(--washi);
}
.cert h2 { color: var(--accent); }

/* 統計 */
.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; text-align: center; }
.stat-grid .card { margin: 0; padding: 12px 6px; }
.stat-grid b { font-size: 1.5rem; color: var(--accent); display: block; }
.stat-grid small { color: var(--sumi-soft); font-size: .75rem; }

.muted { color: var(--sumi-soft); font-size: .88rem; }
.center { text-align: center; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
