:root {
  --bg: #fff6e5;
  --bg-2: #ffe9c7;
  --dot: #ffe2b3;
  --ink: #2d2a32;
  --muted: #6d6875;
  --card: #ffffff;
  --line: #2d2a32;
  --primary: #ff8a3d;
  --good: #1f9d63;
  --bad: #e5484d;
  --initial: #2f6fed;
  --initial-bg: #e6efff;
  --medial: #0f8a74;
  --medial-bg: #dcf5ee;
  --final: #d6336c;
  --final-bg: #ffe6ef;
  --shadow: 0 5px 0 var(--line);
  --radius: 22px;
  --font: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  --font-kai: "LXGW WenKai TC", "TW-Kai", "BiauKai", "DFKai-SB", "Kaiti TC", "標楷體", var(--font);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background-color: var(--bg);
  background-image: radial-gradient(var(--dot) 1.6px, transparent 1.6px);
  background-size: 26px 26px;
  color: var(--ink);
  font-family: var(--font);
  font-size: 18px;
  line-height: 1.6;
  -webkit-tap-highlight-color: transparent;
}

a {
  color: var(--initial);
}

button {
  font: inherit;
  color: inherit;
}

h1,
h2 {
  line-height: 1.3;
}

/* ---------- 頂部 ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: rgba(255, 246, 229, 0.94);
  backdrop-filter: blur(6px);
  border-bottom: 3px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  margin-right: auto;
  color: var(--ink);
  font-size: 20px;
  font-weight: 900;
  text-decoration: none;
  white-space: nowrap;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  margin-right: 8px;
  border: 3px solid var(--line);
  border-radius: 11px;
  background: var(--primary);
  color: #fff;
  font-family: var(--font-kai);
  font-size: 22px;
}

.stars {
  padding: 2px 12px;
  border: 3px solid var(--line);
  border-radius: 999px;
  background: #fff;
  font-weight: 800;
  white-space: nowrap;
}

.stars.bump {
  animation: pop 0.5s;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 3px solid var(--line);
  border-radius: 14px;
  background: #fff;
  font-size: 22px;
  text-decoration: none;
}

main {
  max-width: 860px;
  margin: 0 auto;
  padding: 20px 16px 48px;
}

.footer {
  padding: 0 16px 28px;
  color: var(--muted);
  font-size: 14px;
  text-align: center;
}

/* ---------- 首頁 ---------- */
.hero {
  text-align: center;
}

.hero h1 {
  margin: 8px 0 0;
  font-size: clamp(32px, 8vw, 48px);
}

.hero p {
  margin: 4px 0;
}

.learned-info {
  color: var(--muted);
}

.notice {
  padding: 12px 16px;
  border: 3px solid var(--line);
  border-radius: 16px;
  background: #fff3bf;
}

.game-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 16px;
  margin: 20px 0 24px;
}

.game-tile {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 18px;
  border: 3px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: var(--shadow);
  color: var(--ink);
  text-decoration: none;
  transition: transform 0.12s, box-shadow 0.12s;
}

.game-tile:hover {
  transform: translateY(-2px);
}

.game-tile:active {
  transform: translateY(4px);
  box-shadow: 0 1px 0 var(--line);
}

.tile-icon {
  font-size: 44px;
  line-height: 1.1;
}

.tile-stage {
  position: absolute;
  top: 14px;
  right: 14px;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--bg-2);
  font-size: 13px;
  font-weight: 700;
}

.tile-stage.stage-spell {
  background: #e8e0ff;
}

.game-tile strong {
  font-size: 22px;
}

.tile-desc {
  color: var(--muted);
  font-size: 15px;
}

.parent-tips {
  margin: 16px 0;
  padding: 12px 18px;
  border: 3px solid var(--line);
  border-radius: 18px;
  background: #fff;
}

.parent-tips summary {
  font-weight: 800;
  cursor: pointer;
}

.parent-tips ul {
  margin: 10px 0 4px;
  padding-left: 22px;
}

.parent-tips li {
  margin: 4px 0;
}

.chip {
  display: inline-block;
  margin: 0 3px;
  padding: 0 10px;
  border: 2px solid var(--gc, var(--line));
  border-radius: 999px;
  background: var(--gbg, #fff);
  color: var(--gc, var(--ink));
  font-size: 15px;
  font-weight: 700;
}

/* ---------- 顏色分類 ---------- */
.g-initial {
  --gc: var(--initial);
  --gbg: var(--initial-bg);
}

.g-medial {
  --gc: var(--medial);
  --gbg: var(--medial-bg);
}

.g-final {
  --gc: var(--final);
  --gbg: var(--final-bg);
}

/* ---------- 按鈕 ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 52px;
  padding: 10px 22px;
  border: 3px solid var(--line);
  border-radius: 999px;
  background: #fff;
  box-shadow: 0 4px 0 var(--line);
  color: var(--ink);
  font-size: 19px;
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.08s, box-shadow 0.08s;
}

.btn:active:not(:disabled) {
  transform: translateY(3px);
  box-shadow: 0 1px 0 var(--line);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn.primary {
  background: var(--primary);
  color: #fff;
}

.btn.good {
  background: var(--good);
  color: #fff;
}

.btn.retry {
  background: #ffd166;
}

.btn.sound {
  background: #bde0fe;
}

.btn.big {
  min-height: 62px;
  padding: 12px 28px;
  font-size: 22px;
}

.btn.tiny {
  min-height: 38px;
  padding: 4px 14px;
  box-shadow: 0 3px 0 var(--line);
  font-size: 15px;
}

.btn small {
  display: block;
  font-size: 14px;
  font-weight: 500;
  opacity: 0.9;
}

.row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin: 16px 0;
}

.row.left {
  justify-content: flex-start;
}

/* ---------- 遊戲共用 ---------- */
.game-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 16px;
}

.game-head h1 {
  margin: 0;
  font-size: clamp(26px, 6vw, 34px);
}

.back {
  color: var(--muted);
  font-weight: 700;
  text-decoration: none;
}

.tip {
  margin: 8px 0 16px;
  color: var(--muted);
}

.start {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  text-align: center;
}

.start > .btn.big {
  flex-direction: column;
  width: min(100%, 380px);
  line-height: 1.3;
}

.empty,
.result {
  margin: 24px auto;
  padding: 24px;
  max-width: 480px;
  border: 3px solid var(--line);
  border-radius: 26px;
  background: #fff;
  box-shadow: var(--shadow);
  text-align: center;
}

.result h2 {
  margin: 4px 0;
  font-size: 32px;
}

.result-emoji {
  font-size: 72px;
  line-height: 1.1;
}

.result-stars {
  font-size: 24px;
  font-weight: 800;
}

.progress {
  max-width: 420px;
  height: 16px;
  margin: 0 auto;
  overflow: hidden;
  border: 3px solid var(--line);
  border-radius: 999px;
  background: #fff;
}

.progress span {
  display: block;
  height: 100%;
  background: var(--good);
  transition: width 0.4s;
}

.progress-text {
  margin: 6px 0;
  color: var(--muted);
  font-weight: 700;
  text-align: center;
}

.msg {
  min-height: 1.8em;
  margin: 12px 0;
  font-size: 20px;
  font-weight: 700;
  text-align: center;
}

.msg.good {
  color: var(--good);
}

.nosound {
  color: var(--muted);
  font-weight: 700;
}

.group-label {
  margin: 4px 0 0;
  color: var(--muted);
  font-weight: 700;
  text-align: center;
}

.bigcard {
  display: grid;
  place-items: center;
  width: min(62vw, 240px);
  aspect-ratio: 1 / 1.1;
  margin: 12px auto 4px;
  border: 6px solid var(--gc, var(--line));
  border-radius: 32px;
  background: var(--gbg, #fff);
  box-shadow: 0 8px 0 var(--gc, var(--line));
  color: var(--gc, var(--ink));
  font-family: var(--font-kai);
  font-size: clamp(110px, 32vw, 160px);
  line-height: 1;
  user-select: none;
  transition: background-color 0.3s, color 0.3s, border-color 0.3s, box-shadow 0.3s;
}

.bigcard.syllable-card {
  width: auto;
  min-width: min(70vw, 240px);
  aspect-ratio: auto;
  padding: 20px 28px;
  font-size: clamp(72px, 20vw, 110px);
}

[data-say] {
  cursor: pointer;
}

/* ---------- 聽音拍卡 ---------- */
.options {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
  max-width: 440px;
  margin: 16px auto;
}

.option {
  aspect-ratio: 1;
  border: 4px solid var(--gc, var(--line));
  border-radius: 26px;
  background: var(--gbg, #fff);
  box-shadow: 0 6px 0 var(--gc, var(--line));
  color: var(--gc, var(--ink));
  font-family: var(--font-kai);
  font-size: clamp(64px, 20vw, 96px);
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
}

.option:active:not(:disabled) {
  transform: translateY(4px);
  box-shadow: 0 2px 0 var(--gc, var(--line));
}

.option:disabled {
  cursor: default;
}

.option.wrong {
  opacity: 0.35;
}

.option.correct {
  box-shadow: 0 0 0 7px var(--good), 0 6px 0 var(--gc, var(--line));
  animation: pop 0.5s;
}

.wrong {
  animation: shake 0.45s;
}

/* ---------- 翻牌配對 ---------- */
.memory-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  max-width: 620px;
  margin: 12px auto;
}

@media (min-width: 640px) {
  .memory-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

.mcard {
  aspect-ratio: 3 / 4;
  padding: 0;
  border: 0;
  background: none;
  perspective: 800px;
  cursor: pointer;
  touch-action: manipulation;
}

.mcard-inner {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.45s;
}

.mcard.flipped .mcard-inner {
  transform: rotateY(180deg);
}

.mcard-back,
.mcard-front {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 3px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 4px 0 var(--line);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.mcard-back {
  background: var(--primary) repeating-linear-gradient(45deg, transparent 0 10px, rgba(255, 255, 255, 0.18) 10px 20px);
  color: #fff;
  font-size: 34px;
}

.mcard-front {
  transform: rotateY(180deg);
  background: #fff;
}

.mcard-front.sym {
  border-color: var(--gc);
  background: var(--gbg);
  color: var(--gc);
  font-family: var(--font-kai);
  font-size: clamp(48px, 13vw, 72px);
  line-height: 1;
}

.pic-emoji {
  font-size: clamp(40px, 11vw, 58px);
  line-height: 1.1;
}

.pic-word {
  font-size: 18px;
  font-weight: 700;
}

.mcard.matched .mcard-front {
  box-shadow: 0 0 0 5px var(--good);
  animation: pop 0.45s;
}

/* ---------- 郵差送信 ---------- */
.mailboxes {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  max-width: 520px;
  margin: 20px auto 0;
}

.mailbox {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 14px 6px;
  border: 4px solid var(--gc);
  border-radius: 20px;
  background: var(--gbg);
  box-shadow: 0 5px 0 var(--gc);
  color: var(--gc);
  font-size: 22px;
  font-weight: 900;
  cursor: pointer;
  touch-action: manipulation;
}

.mailbox:disabled {
  cursor: default;
}

.mailbox span {
  font-size: 40px;
  line-height: 1.1;
}

/* ---------- 拼音組合 ---------- */
.picture {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
}

.pic-big {
  font-size: clamp(84px, 24vw, 116px);
  line-height: 1.1;
}

.slots {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin: 18px 0;
}

.slot {
  width: 74px;
  height: 84px;
  border: 4px solid var(--gc, var(--line));
  border-radius: 18px;
  background: var(--gbg, #fff);
  color: var(--gc, var(--ink));
  font-family: var(--font-kai);
  font-size: 46px;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
}

.slot.is-empty {
  border-style: dashed;
  background: rgba(255, 255, 255, 0.7);
  color: var(--muted);
}

.slot.tone-slot {
  width: 86px;
  font-size: 40px;
}

.slot.tone-slot.is-empty,
.slot.tone-slot.is-first {
  font-family: var(--font);
  font-size: 18px;
  font-weight: 700;
}

.slot.wrong {
  border-color: var(--bad);
  box-shadow: 0 0 0 4px rgba(229, 72, 77, 0.35);
}

.keys {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  max-width: 560px;
  margin: 12px auto;
}

.key {
  min-width: 66px;
  height: 66px;
  padding: 0 8px;
  border: 3px solid var(--gc, var(--line));
  border-radius: 16px;
  background: var(--gbg, #fff);
  box-shadow: 0 4px 0 var(--gc, var(--line));
  color: var(--gc, var(--ink));
  font-family: var(--font-kai);
  font-size: 38px;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
}

.key:disabled {
  opacity: 0.25;
  box-shadow: none;
  transform: translateY(4px);
  cursor: default;
}

.tone-key {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: 70px;
  height: 72px;
  font-family: var(--font);
  font-size: 14px;
  font-weight: 700;
}

.tone-key.active {
  background: #ffd166;
}

.tone-icon {
  width: 34px;
  height: 28px;
  fill: none;
  stroke: currentColor;
  stroke-width: 5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tone-dot {
  font-size: 18px;
  line-height: 28px;
}

.answer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 24px;
  max-width: 420px;
  margin: 24px auto 8px;
  padding: 20px;
  border: 4px solid var(--line);
  border-radius: 28px;
  background: #fff;
  box-shadow: var(--shadow);
}

.answer-word {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.answer-ch {
  font-family: var(--font-kai);
  font-size: 76px;
  line-height: 1.1;
}

.answer-zy {
  color: var(--initial);
  font-family: var(--font-kai);
  font-size: 36px;
}

/* ---------- 聲調溜滑梯 ---------- */
.tone-choices,
.tone-demo {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  width: 100%;
  max-width: 560px;
  margin: 16px auto;
}

@media (min-width: 640px) {
  .tone-choices,
  .tone-demo {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

.tone-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 14px 8px;
  border: 3px solid var(--line);
  border-radius: 20px;
  background: #fff;
  box-shadow: 0 5px 0 var(--line);
  cursor: pointer;
  touch-action: manipulation;
}

.tone-btn:active:not(:disabled) {
  transform: translateY(3px);
  box-shadow: 0 2px 0 var(--line);
}

.tone-btn:disabled {
  cursor: default;
}

.tone-btn .tone-icon {
  width: 52px;
  height: 42px;
  stroke: var(--primary);
}

.tone-btn.wrong {
  opacity: 0.4;
}

.tone-btn.correct {
  background: #d3f9e3;
  box-shadow: 0 0 0 6px var(--good);
  animation: pop 0.5s;
}

.tone-syl {
  font-family: var(--font-kai);
  font-size: 36px;
  line-height: 1.15;
}

.tone-name {
  color: var(--muted);
  font-weight: 700;
}

/* ---------- 設定 ---------- */
.settings h2 {
  margin: 28px 0 4px;
}

.sym-group {
  margin: 12px 0;
  padding: 8px 16px 16px;
  border: 3px solid var(--line);
  border-radius: 18px;
  background: #fff;
}

.sym-group legend {
  padding: 0 6px;
  font-weight: 700;
}

.sym-group .row {
  margin: 6px 0 12px;
}

.sym-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.sym-toggle {
  position: relative;
}

.sym-toggle input {
  position: absolute;
  inset: 0;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.sym-toggle span {
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border: 3px dashed var(--gc);
  border-radius: 14px;
  background: #fff;
  color: var(--gc);
  font-family: var(--font-kai);
  font-size: 32px;
  opacity: 0.4;
  transition: opacity 0.15s, background-color 0.15s;
}

.sym-toggle input:checked + span {
  border-style: solid;
  background: var(--gbg);
  box-shadow: 0 3px 0 var(--gc);
  opacity: 1;
}

.sym-toggle input:focus-visible + span {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
}

.radio {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-weight: 700;
}

/* ---------- 國字注音 ---------- */
.zy {
  display: inline-flex;
  align-items: center;
  vertical-align: middle;
  white-space: nowrap;
  line-height: 1;
}

.zy-ch {
  line-height: 1.2;
}

.zy-py {
  position: relative;
  display: inline-flex;
  margin: 0 0.75em 0 0.1em;
  font-family: var(--font-kai);
  font-size: max(0.45em, 10px);
  font-weight: 400;
  line-height: 1;
}

body.with-zhuyin {
  font-size: 20px;
  line-height: 1.9;
}

.zy-col {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.zy-sym {
  display: block;
  height: 1em;
}

.zy-dot {
  display: block;
  height: 0.6em;
  line-height: 0.6em;
}

.zy-tone {
  position: absolute;
  top: calc((var(--n) - 1) * 1em);
  left: 100%;
  transform: translateY(-40%);
}

/* 加注音後小字要放大一點才看得清楚 */
body.with-zhuyin .tile-desc,
body.with-zhuyin .btn small {
  font-size: 17px;
}

body.with-zhuyin .tile-stage,
body.with-zhuyin .footer {
  font-size: 16px;
}

body.with-zhuyin .pic-word {
  font-size: 22px;
}

body.with-zhuyin .brand {
  font-size: 19px;
}

@media (max-width: 440px) {
  body.with-zhuyin .brand-text {
    display: none;
  }
}

/* ---------- 動畫 ---------- */
.pop {
  animation: pop 0.45s;
}

.burst {
  position: fixed;
  inset: 0;
  z-index: 50;
  overflow: hidden;
  pointer-events: none;
}

.burst span {
  position: absolute;
  top: -60px;
  animation: fall 1.7s ease-in forwards;
}

@keyframes pop {
  0% {
    transform: scale(0.85);
  }
  60% {
    transform: scale(1.06);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes shake {
  0%,
  100% {
    transform: translateX(0);
  }
  20% {
    transform: translateX(-10px);
  }
  40% {
    transform: translateX(10px);
  }
  60% {
    transform: translateX(-6px);
  }
  80% {
    transform: translateX(6px);
  }
}

@keyframes fall {
  to {
    transform: translateY(115vh) rotate(540deg);
  }
}

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