:root {
  --paper: #f6ebd8;
  --paper-deep: #e8d4b0;
  --paper-edge: #d4b88a;
  --card: #fff9ef;
  --card-warm: #fff3e0;
  --ink: #1a1612;
  --ink-soft: #3a3228;
  --muted: #5a4e40;
  --cinnabar: #b02820;
  --seal: #d4472f;
  --seal-bright: #e85a3c;
  --teal: #0f3d3a;
  --teal-mid: #1a5c56;
  --teal-deep: #0a2c2a;
  --gold: #9a6a28;
  --gold-light: #e0c070;
  --line: #e0cba8;
  --ok: #0e6a3c;
  --ok-bg: #e2f6ea;
  --bad: #b02820;
  --bad-bg: #fde8e6;
  --note: #faf0d4;
  --life: #d4472f;
  --shadow: 0 12px 32px rgba(15, 61, 58, 0.16);
  --radius: 1rem;
  --font-sans: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", "Heiti TC", sans-serif;
  --font-serif: "Noto Serif TC", "Songti TC", "PMingLiU", "Source Han Serif TC", serif;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --tap: 52px;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font-sans);
  font-size: 1.05rem;
  line-height: 1.55;
  color: var(--ink);
  background: var(--paper);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  position: relative;
  overflow-x: hidden;
}
body.fx-off .ambient,
body.fx-off .mascot-bob,
body.fx-off .paper-bg::before,
body.fx-off .leaf {
  animation: none !important;
}
body.fx-off .ambient { opacity: 0.25; }

.paper-bg {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse 100% 70% at 12% 8%, rgba(26, 92, 86, 0.14), transparent 50%),
    radial-gradient(ellipse 90% 60% at 92% 88%, rgba(212, 71, 47, 0.12), transparent 48%),
    radial-gradient(ellipse 70% 50% at 50% 50%, rgba(224, 192, 112, 0.12), transparent 60%),
    linear-gradient(165deg, #f8efdc 0%, #f3e2c4 38%, #efe0c8 70%, #f5ead6 100%);
  animation: paperShift 18s ease-in-out infinite alternate;
}
@keyframes paperShift {
  0% { background-position: 0% 0%, 0% 0%, 0% 0%, 0 0; }
  100% { background-position: 0% 0%, 0% 0%, 18% 12%, 0 0; }
}
.paper-bg::before {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0.38;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='4' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='180' height='180' filter='url(%23n)' opacity='0.5'/%3E%3C/svg%3E");
  mix-blend-mode: multiply;
}
.paper-bg::after {
  content: "";
  position: absolute;
  inset: 8px;
  border: 2px solid rgba(15, 61, 58, 0.22);
  border-radius: 8px;
  box-shadow:
    inset 0 0 0 1px rgba(255, 249, 239, 0.5),
    0 0 0 1px rgba(154, 106, 40, 0.12);
  pointer-events: none;
}

.ambient {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  overflow: hidden;
}
.cloud {
  position: absolute;
  width: 90px;
  height: 36px;
  border-radius: 50px;
  background: rgba(255, 252, 245, 0.55);
  box-shadow:
    28px 6px 0 -4px rgba(255, 252, 245, 0.45),
    -22px 8px 0 -6px rgba(255, 252, 245, 0.4);
  opacity: 0.7;
  animation: cloudDrift 22s linear infinite;
}
.cloud::before {
  content: "";
  position: absolute;
  width: 42px; height: 42px;
  border-radius: 50%;
  background: inherit;
  top: -18px; left: 18px;
}
.cloud.c1 { top: 12%; left: -10%; animation-duration: 26s; }
.cloud.c2 { top: 48%; left: -15%; width: 70px; height: 28px; animation-duration: 32s; animation-delay: -8s; opacity: 0.5; }
.cloud.c3 { top: 72%; left: -12%; width: 110px; height: 40px; animation-duration: 28s; animation-delay: -14s; opacity: 0.55; }
@keyframes cloudDrift {
  0% { transform: translateX(0); }
  100% { transform: translateX(120vw); }
}
.leaf {
  position: absolute;
  width: 14px; height: 10px;
  background: rgba(15, 61, 58, 0.35);
  border-radius: 0 80% 0 80%;
  animation: leafFall 14s linear infinite;
  opacity: 0.55;
}
.leaf.l1 { left: 20%; top: -5%; animation-delay: 0s; }
.leaf.l2 { left: 70%; top: -5%; animation-delay: -7s; background: rgba(180, 40, 32, 0.28); }
@keyframes leafFall {
  0% { transform: translateY(0) rotate(0deg); }
  100% { transform: translateY(110vh) rotate(360deg); }
}
.ink-dot {
  position: absolute;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: rgba(15, 61, 58, 0.18);
  animation: inkFloat 9s ease-in-out infinite;
}
.ink-dot.d1 { top: 22%; left: 18%; }
.ink-dot.d2 { top: 38%; right: 14%; width: 10px; height: 10px; background: rgba(212, 71, 47, 0.2); animation-delay: -2s; }
.ink-dot.d3 { top: 60%; left: 8%; width: 5px; height: 5px; animation-delay: -4s; }
@keyframes inkFloat {
  0%, 100% { transform: translateY(0) scale(1); opacity: 0.5; }
  50% { transform: translateY(-18px) scale(1.15); opacity: 0.9; }
}

#fxCanvas {
  position: fixed;
  inset: 0;
  z-index: 40;
  pointer-events: none;
}
#burstLayer {
  position: fixed;
  inset: 0;
  z-index: 41;
  pointer-events: none;
  overflow: hidden;
}
.burst-emoji, .confetti-streak {
  position: absolute;
  animation: burstFly 0.9s ease-out forwards;
  font-size: 1.4rem;
  will-change: transform, opacity;
}
.confetti-streak {
  border-radius: 2px;
  font-size: 0;
}
@keyframes burstFly {
  0% { transform: translate(-50%, -50%) scale(0.6); opacity: 1; }
  100% { transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) rotate(var(--rot, 0deg)) scale(1); opacity: 0; }
}

#app {
  position: relative;
  z-index: 2;
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: 0 0.85rem calc(1rem + var(--safe-b));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.75rem 0 0.4rem;
}
.brand { display: flex; align-items: center; gap: 0.55rem; min-width: 0; }
.brand-mark {
  width: 42px; height: 42px;
  border-radius: 10px;
  background: linear-gradient(145deg, var(--teal-mid), var(--teal-deep));
  color: #f8efdc;
  display: grid; place-items: center;
  font-family: var(--font-serif);
  font-weight: 700;
  font-size: 1.15rem;
  box-shadow: 0 4px 12px rgba(15, 61, 58, 0.3);
  flex-shrink: 0;
}
.brand h1 {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--teal-deep);
  line-height: 1.25;
}
.brand p {
  margin: 0.1rem 0 0;
  font-size: 0.75rem;
  color: var(--muted);
}

.ghost, .btn, .choice, .chip, .match-item, .sort-item, .cat-btn {
  font: inherit;
  cursor: pointer;
  border: none;
  appearance: none;
  -webkit-appearance: none;
}
.ghost {
  background: transparent;
  color: var(--teal);
  border: 1.5px solid rgba(15, 61, 58, 0.35);
  border-radius: 999px;
  padding: 0.4rem 0.85rem;
  font-size: 0.85rem;
  font-weight: 600;
  min-height: 40px;
}
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  width: 100%;
  min-height: var(--tap);
  padding: 0.75rem 1rem;
  border-radius: 999px;
  font-weight: 700;
  font-size: 1.05rem;
  background: linear-gradient(145deg, var(--teal-mid), var(--teal-deep));
  color: #f8efdc;
  box-shadow: 0 8px 20px rgba(15, 61, 58, 0.28);
  transition: transform 0.12s ease, filter 0.12s;
}
.btn:active { transform: scale(0.97); }
.btn.secondary {
  background: linear-gradient(145deg, #fff9ef, #f0e0c0);
  color: var(--teal-deep);
  border: 1.5px solid rgba(15, 61, 58, 0.2);
  box-shadow: var(--shadow);
}
.btn.danger {
  background: linear-gradient(145deg, var(--seal-bright), var(--cinnabar));
}
.btn:disabled { opacity: 0.45; pointer-events: none; }

.card {
  background: var(--card);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem 1.05rem;
  margin: 0.65rem 0;
  animation: cardIn 0.35s ease;
}
@keyframes cardIn {
  from { opacity: 0; transform: translateY(12px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

.mascot-hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  margin: 0.4rem 0 0.8rem;
}
.mascot-img-wrap {
  width: 140px; height: 140px;
  border-radius: 50%;
  overflow: hidden;
  border: 3px solid rgba(154, 106, 40, 0.45);
  box-shadow: 0 10px 28px rgba(15, 61, 58, 0.2);
  background: #f3e6c8;
}
.mascot-img-wrap img {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
}
.mascot-fallback {
  width: 100%; height: 100%;
  display: grid; place-items: center;
  font-size: 4rem;
  background: linear-gradient(160deg, #d8efe9, #f3e6c8);
}
.mascot-bob { animation: bob 2.4s ease-in-out infinite; }
@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}
.mascot-cheer-pop { animation: cheerPop 0.55s ease; }
@keyframes cheerPop {
  0% { transform: scale(1); }
  40% { transform: scale(1.12) rotate(-4deg); }
  70% { transform: scale(1.08) rotate(3deg); }
  100% { transform: scale(1); }
}
.mascot-encourage-shake { animation: encShake 0.45s ease; }
@keyframes encShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}
.mascot-bubble {
  max-width: 92%;
  background: #fff;
  border: 1.5px solid var(--line);
  border-radius: 1rem;
  padding: 0.55rem 0.9rem;
  font-size: 0.92rem;
  color: var(--ink-soft);
  box-shadow: 0 4px 14px rgba(0,0,0,0.06);
  text-align: center;
  position: relative;
}
.mascot-bubble::before {
  content: "";
  position: absolute;
  top: -7px; left: 50%;
  transform: translateX(-50%);
  border: 7px solid transparent;
  border-bottom-color: #fff;
  filter: drop-shadow(0 -1px 0 var(--line));
}

.prefs {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  justify-content: center;
  margin: 0.5rem 0 0.8rem;
}
.prefs label {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: rgba(255,249,239,0.85);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.35rem 0.75rem;
  font-size: 0.82rem;
  color: var(--muted);
}

.level-map {
  display: grid;
  gap: 0.65rem;
  margin: 0.75rem 0;
}
.level-pill {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.85rem 1rem;
  border-radius: 1rem;
  background: linear-gradient(120deg, #fff9ef, #f3e8d0);
  border: 1.5px solid var(--line);
  box-shadow: var(--shadow);
  text-align: left;
}
.level-pill .badge {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--teal);
  color: #f8efdc;
  display: grid; place-items: center;
  font-family: var(--font-serif);
  font-weight: 700;
  flex-shrink: 0;
}
.level-pill strong { display: block; font-size: 1rem; color: var(--teal-deep); }
.level-pill span { font-size: 0.8rem; color: var(--muted); }

.hud {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.45rem 0.6rem;
  align-items: center;
  margin: 0.35rem 0 0.6rem;
  padding: 0.65rem 0.8rem;
  background: rgba(15, 61, 58, 0.92);
  color: #f8efdc;
  border-radius: 1rem;
  box-shadow: 0 8px 24px rgba(15, 61, 58, 0.28);
}
.hud .lives {
  font-size: 1.15rem;
  letter-spacing: 0.08em;
  color: #ffb4a8;
}
.hud .meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem 0.7rem;
  font-size: 0.8rem;
  opacity: 0.95;
}
.hud .meta b { color: var(--gold-light); }
.progress {
  grid-column: 1 / -1;
  height: 8px;
  background: rgba(255,255,255,0.15);
  border-radius: 999px;
  overflow: hidden;
}
.progress > i {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--gold-light), #fff3c4, var(--seal-bright));
  border-radius: inherit;
  transition: width 0.3s ease;
}
.timer-bar {
  margin: 0.4rem 0 0.6rem;
  height: 10px;
  background: rgba(176, 40, 32, 0.12);
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid rgba(176, 40, 32, 0.2);
}
.timer-bar > i {
  display: block;
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, var(--seal), var(--gold));
  transform-origin: left center;
  transition: width 0.1s linear;
}
.timer-bar.warn > i { background: linear-gradient(90deg, #b02820, #e85a3c); }

.coach-bar {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  margin: 0.4rem 0 0.7rem;
  padding: 0.45rem 0.65rem;
  background: rgba(255,249,239,0.92);
  border: 1.5px solid var(--line);
  border-radius: 999px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.05);
}
.coach-face {
  width: 48px; height: 48px;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
  border: 2px solid rgba(154, 106, 40, 0.4);
  background: #f3e6c8;
}
.coach-face img { width: 100%; height: 100%; object-fit: cover; display: block; }
.coach-msg { font-size: 0.88rem; color: var(--ink-soft); line-height: 1.35; }

.q-title {
  font-family: var(--font-serif);
  font-size: 1.08rem;
  font-weight: 700;
  color: var(--teal-deep);
  margin: 0 0 0.75rem;
  line-height: 1.45;
}
.type-tag {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--teal);
  background: rgba(26, 92, 86, 0.1);
  border-radius: 999px;
  padding: 0.15rem 0.55rem;
  margin-bottom: 0.45rem;
}

.choices { display: grid; gap: 0.55rem; }
.choice {
  text-align: left;
  min-height: var(--tap);
  padding: 0.75rem 0.9rem;
  border-radius: 0.9rem;
  background: #fff;
  border: 1.5px solid var(--line);
  color: var(--ink);
  font-weight: 500;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  transition: transform 0.1s, border-color 0.15s, background 0.15s;
}
.choice:active { transform: scale(0.98); }
.choice.selected {
  border-color: var(--teal-mid);
  background: #e8f5f2;
}
.choice.correct {
  border-color: var(--ok);
  background: var(--ok-bg);
}
.choice.wrong {
  border-color: var(--bad);
  background: var(--bad-bg);
  animation: shakeX 0.4s ease;
}
@keyframes shakeX {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  60% { transform: translateX(6px); }
}

.match-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}
.match-item {
  min-height: 48px;
  padding: 0.55rem 0.5rem;
  border-radius: 0.75rem;
  border: 1.5px solid var(--line);
  background: #fff;
  font-size: 0.88rem;
  text-align: center;
  line-height: 1.35;
}
.match-item.active {
  border-color: var(--teal-mid);
  background: #e8f5f2;
  box-shadow: 0 0 0 2px rgba(26, 92, 86, 0.2);
}
.match-item.done {
  opacity: 0.45;
  pointer-events: none;
  background: var(--ok-bg);
  border-color: var(--ok);
}
.match-col-title {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--muted);
  text-align: center;
}

.sort-list { display: grid; gap: 0.45rem; }
.sort-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-height: 48px;
  padding: 0.55rem 0.7rem;
  border-radius: 0.75rem;
  border: 1.5px solid var(--line);
  background: #fff;
  font-size: 0.92rem;
  text-align: left;
}
.sort-item .num {
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--teal);
  color: #fff;
  display: grid; place-items: center;
  font-size: 0.8rem;
  font-weight: 700;
  flex-shrink: 0;
}
.sort-actions {
  display: flex;
  gap: 0.35rem;
  margin-left: auto;
}
.sort-actions button {
  width: 36px; height: 36px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--note);
  font-size: 1rem;
  cursor: pointer;
}


.classify-cats {
  display: grid;
  gap: 0.45rem;
  margin-bottom: 0.7rem;
}
.cat-btn {
  min-height: 44px;
  border-radius: 0.75rem;
  border: 1.5px dashed rgba(15, 61, 58, 0.35);
  background: rgba(26, 92, 86, 0.06);
  color: var(--teal-deep);
  font-weight: 600;
  font-size: 0.9rem;
  padding: 0.5rem;
}
.cat-btn.active {
  border-style: solid;
  border-color: var(--teal-mid);
  background: #e8f5f2;
}
.classify-pool { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.chip {
  padding: 0.45rem 0.7rem;
  border-radius: 999px;
  background: #fff;
  border: 1.5px solid var(--line);
  font-size: 0.85rem;
}
.chip.active { border-color: var(--seal); background: #fff0ec; }
.chip.placed {
  opacity: 0.55;
  background: var(--ok-bg);
  border-color: var(--ok);
}
.placed-list {
  margin-top: 0.55rem;
  font-size: 0.82rem;
  color: var(--muted);
  line-height: 1.5;
}

.feedback-flash {
  position: fixed;
  left: 50%;
  top: 28%;
  transform: translate(-50%, -20%) scale(0.85);
  z-index: 50;
  padding: 0.55rem 1.1rem;
  border-radius: 999px;
  font-weight: 700;
  font-size: 1.05rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.25s;
}
.feedback-flash.show {
  opacity: 1;
  transform: translate(-50%, 0) scale(1);
}
.feedback-flash.ok { background: var(--ok-bg); color: var(--ok); border: 2px solid var(--ok); }
.feedback-flash.bad { background: var(--bad-bg); color: var(--bad); border: 2px solid var(--bad); }

.hint-box {
  margin-top: 0.65rem;
  padding: 0.55rem 0.75rem;
  background: var(--note);
  border-left: 3px solid var(--gold);
  border-radius: 0 0.6rem 0.6rem 0;
  font-size: 0.88rem;
  color: var(--ink-soft);
}

.result-screen { text-align: center; padding: 0.5rem 0 1rem; }
.result-screen h2 {
  font-family: var(--font-serif);
  color: var(--teal-deep);
  margin: 0.4rem 0;
}
.score-big {
  font-size: 2.4rem;
  font-weight: 800;
  color: var(--seal);
  font-family: var(--font-serif);
}
.high-score {
  font-size: 0.9rem;
  color: var(--muted);
  margin: 0.3rem 0 1rem;
}

.shake-panel { animation: shakeX 0.4s ease; }

.footer-note {
  text-align: center;
  font-size: 0.75rem;
  color: var(--muted);
  margin: 1rem 0 0.4rem;
  line-height: 1.45;
}

@media (max-width: 380px) {
  .brand h1 { font-size: 0.95rem; }
  .mascot-img-wrap { width: 120px; height: 120px; }
  .q-title { font-size: 1rem; }
}
