:root {
  --ink: #3a2a1a;
  --ink-soft: #7a6550;
  --card: #fffaf0;
  --line: #f0d9b5;
  --primary: #ff8a3d;
  --primary-dark: #e06a1d;
  --good: #2fbf71;
  --bad: #ff5a6a;
  --math: #4fa8ff;
  --english: #b46bff;
  --font: 'Fredoka', 'PingFang TC', 'Noto Sans TC', 'Microsoft JhengHei', system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #bfe9ff; color: var(--ink); font-family: var(--font); }
body { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
button { font-family: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
.hidden { display: none !important; }

#gl { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
#flash { position: fixed; inset: 0; pointer-events: none; background: radial-gradient(transparent 50%, rgba(255, 40, 60, .55)); opacity: 0; transition: opacity .4s; }
#flash.on { opacity: 1; transition: none; }

/* ---------- HUD ---------- */
#hud {
  position: fixed; top: 0; left: 0; right: 0; display: flex; justify-content: space-between; align-items: flex-start; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 14px 0; pointer-events: none; z-index: 5;
}
.hud-group { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; pointer-events: auto; }
.chip {
  display: flex; align-items: center; gap: 5px; background: rgba(255, 255, 255, .88); border-radius: 999px;
  padding: 5px 12px; font-weight: 700; font-size: 18px; box-shadow: 0 3px 0 rgba(0, 0, 0, .12); border: 0;
}
.chip img { width: 24px; height: 24px; }
.chip.level { background: var(--primary); color: #fff; }
#hearts img { width: 24px; height: 24px; transition: transform .2s; }
#hearts img.empty { filter: grayscale(1) opacity(.35); }
.btn-round { width: 40px; height: 40px; justify-content: center; padding: 0; font-size: 18px; }
#fruitChip.done { background: #fff1a8; animation: pulse 1s infinite; }

#bossbar { display: flex; align-items: center; gap: 8px; background: rgba(40, 20, 50, .82); color: #fff; padding: 6px 14px 6px 6px; border-radius: 999px; pointer-events: none; }
#bossFace { width: 44px; height: 44px; border-radius: 50%; border: 3px solid #ffd23f; object-fit: cover; }
.bossinfo { display: flex; flex-direction: column; gap: 4px; min-width: 140px; }
.bossinfo span { font-weight: 700; font-size: 15px; }
.bar { height: 12px; background: rgba(255, 255, 255, .25); border-radius: 99px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 100%; background: linear-gradient(90deg, #ff5a6a, #ffb03b); border-radius: 99px; transition: width .25s; }

#effects { position: fixed; top: calc(env(safe-area-inset-top) + 62px); right: 14px; display: flex; flex-direction: column; gap: 6px; z-index: 5; pointer-events: none; }
#effects .chip { font-size: 15px; }

#toast {
  position: fixed; top: calc(env(safe-area-inset-top) + 70px); left: 50%; transform: translate(-50%, -10px); z-index: 6;
  background: #fff; padding: 10px 20px; border-radius: 16px; font-size: 20px; font-weight: 700; box-shadow: 0 6px 0 rgba(0, 0, 0, .12);
  opacity: 0; transition: opacity .2s, transform .2s; pointer-events: none; white-space: nowrap; max-width: 92vw; overflow: hidden; text-overflow: ellipsis;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

#banner {
  position: fixed; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  pointer-events: none; z-index: 7; opacity: 0; transition: opacity .25s;
}
#banner.show { opacity: 1; }
#banner .b-title { font-size: clamp(40px, 9vw, 84px); font-weight: 700; color: #fff; -webkit-text-stroke: 3px var(--ink); paint-order: stroke fill; text-shadow: 0 6px 0 rgba(0, 0, 0, .2); animation: bounce-in .5s; }
#banner .b-sub { font-size: clamp(18px, 3.5vw, 28px); font-weight: 700; background: rgba(255, 255, 255, .92); padding: 8px 22px; border-radius: 99px; }
#banner .b-face { width: clamp(120px, 22vw, 200px); height: clamp(120px, 22vw, 200px); border-radius: 50%; border: 6px solid #ffd23f; box-shadow: 0 0 0 8px rgba(255, 90, 106, .6); animation: wobble .6s infinite alternate; object-fit: cover; }
#banner .b-faces { display: flex; gap: 14px; }
#banner .b-face.small { width: clamp(90px, 16vw, 150px); height: clamp(90px, 16vw, 150px); }
#banner.boss { background: radial-gradient(rgba(60, 0, 40, .45), rgba(60, 0, 40, .1)); }

#floaters { position: fixed; inset: 0; pointer-events: none; z-index: 4; }
.floater { position: absolute; font-weight: 700; font-size: 24px; color: #fff; -webkit-text-stroke: 2px var(--ink); paint-order: stroke fill; transform: translate(-50%, -50%); animation: float-up 1s forwards; }

/* ---------- 觸控按鈕 ---------- */
#touch { position: fixed; inset: auto 0 0 0; height: 0; z-index: 5; }
.dpad { position: fixed; left: calc(env(safe-area-inset-left) + 18px); bottom: calc(env(safe-area-inset-bottom) + 18px); width: 186px; height: 186px; }
.dpad button {
  position: absolute; width: 64px; height: 64px; border-radius: 18px; border: 0; background: rgba(255, 255, 255, .7);
  font-size: 26px; box-shadow: 0 4px 0 rgba(0, 0, 0, .15); color: var(--ink);
}
.dpad button:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0, 0, 0, .15); background: #fff; }
.dpad .up { left: 61px; top: 0; } .dpad .down { left: 61px; bottom: 0; }
.dpad .left { left: 0; top: 61px; } .dpad .right { right: 0; top: 61px; }
#btnFire {
  position: fixed; right: calc(env(safe-area-inset-right) + 22px); bottom: calc(env(safe-area-inset-bottom) + 30px);
  width: 112px; height: 112px; border-radius: 50%; border: 0; background: radial-gradient(circle at 35% 30%, #d9a6ff, #8a2be2);
  color: #fff; font-weight: 700; font-size: 18px; box-shadow: 0 6px 0 #5a1a9a; display: flex; flex-direction: column; align-items: center; justify-content: center;
}
#btnFire img { width: 46px; height: 46px; }
#btnFire:active { transform: translateY(4px); box-shadow: 0 2px 0 #5a1a9a; }

/* ---------- 畫面 / 面板 ---------- */
.screen { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; z-index: 10; padding: 16px; background: rgba(30, 40, 60, .25); overflow: auto; }
#screen-title { background: linear-gradient(rgba(191, 233, 255, .1), rgba(191, 233, 255, .35)); }
.panel {
  background: var(--card); border-radius: 28px; padding: 24px 28px; text-align: center; max-width: 560px; width: 100%;
  box-shadow: 0 10px 0 rgba(0, 0, 0, .12), 0 20px 50px rgba(0, 0, 0, .15); border: 4px solid var(--line);
  display: flex; flex-direction: column; gap: 12px; align-items: center; max-height: calc(100vh - 32px); overflow: auto;
}
.panel h2 { margin: 0; font-size: 32px; }
.panel h3 { margin: 8px 0 0; font-size: 20px; color: var(--ink-soft); align-self: flex-start; }
.logo { display: flex; align-items: center; gap: 10px; }
.logo img { width: 84px; height: 84px; animation: wobble 1.2s infinite alternate ease-in-out; }
.logo h1 { margin: 0; font-size: clamp(44px, 10vw, 68px); color: #fff; -webkit-text-stroke: 4px #6b3d16; paint-order: stroke fill; text-shadow: 0 6px 0 rgba(0, 0, 0, .15); letter-spacing: 4px; }
.subtitle { margin: 0; font-size: 20px; color: var(--ink-soft); font-weight: 600; }
.howto { margin: 4px 0 0; font-size: 14px; color: var(--ink-soft); }

.big-btn {
  border: 0; background: var(--primary); color: #fff; font-size: 28px; font-weight: 700; padding: 14px 44px; border-radius: 999px;
  box-shadow: 0 6px 0 var(--primary-dark); transition: transform .1s;
}
.big-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--primary-dark); }
.mid-btn { border: 0; background: #fff; font-size: 18px; font-weight: 700; padding: 10px 20px; border-radius: 999px; box-shadow: 0 4px 0 rgba(0, 0, 0, .12); border: 2px solid var(--line); }
.mid-btn:active { transform: translateY(3px); }
.row-btns { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }

.setting-row { display: flex; align-items: center; gap: 12px; font-weight: 700; font-size: 18px; flex-wrap: wrap; justify-content: center; }
.seg { display: inline-flex; background: #f3e6d0; border-radius: 999px; padding: 4px; gap: 2px; flex-wrap: wrap; justify-content: center; }
.seg button { border: 0; background: transparent; font-size: 17px; font-weight: 700; padding: 6px 14px; border-radius: 999px; color: var(--ink-soft); }
.seg button.on { background: #fff; color: var(--ink); box-shadow: 0 2px 0 rgba(0, 0, 0, .1); }
.toggles label { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.toggles input { width: 22px; height: 22px; accent-color: var(--primary); }

.big-score { font-size: 24px; margin: 0; }
.trophy { width: 110px; height: 110px; animation: bounce-in .6s; }

.gallery-panel { max-width: 760px; }
.boss-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; width: 100%; }
.boss-card { background: #fff; border-radius: 18px; padding: 12px; border: 3px solid var(--line); display: flex; flex-direction: column; align-items: center; gap: 4px; }
.boss-card img.face { width: 96px; height: 96px; border-radius: 50%; border: 4px solid #ffd23f; object-fit: cover; }
.boss-card .faces { display: flex; justify-content: center; }
.boss-card img.face.small { width: 62px; height: 62px; margin: 0 -6px; border-width: 3px; }
.boss-card b { font-size: 20px; }
.boss-card small { color: var(--ink-soft); font-size: 14px; }
.minion-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; width: 100%; }
.minion-card { background: #fff; border-radius: 16px; padding: 10px; border: 3px solid var(--line); font-size: 14px; color: var(--ink-soft); }
.minion-card b { display: block; font-size: 18px; color: var(--ink); }
.minion-card .blob { width: 44px; height: 44px; border-radius: 50%; margin: 0 auto 4px; border: 3px solid rgba(0, 0, 0, .12); }
.face-list { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.face-list img { width: 60px; height: 60px; border-radius: 50%; }

/* ---------- 題目視窗 ---------- */
.modal { position: fixed; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center; padding: 12px; background: rgba(30, 30, 50, .45); }
.quiz-card {
  position: relative; background: var(--card); border-radius: 28px; width: min(640px, 100%); max-height: calc(100vh - 24px); overflow-y: auto; overflow-x: hidden;
  padding: 18px 22px 22px; border: 5px solid var(--line); box-shadow: 0 12px 0 rgba(0, 0, 0, .15); display: flex; flex-direction: column; gap: 12px;
}
.quiz-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.tag { color: #fff; font-weight: 700; padding: 4px 12px; border-radius: 999px; font-size: 16px; }
.tag.math { background: var(--math); }
.tag.english { background: var(--english); }
.quiz-title { font-weight: 700; font-size: 18px; color: var(--ink-soft); }
.reward-peek { margin-left: auto; display: flex; align-items: center; gap: 4px; font-size: 14px; color: var(--ink-soft); font-weight: 600; background: #fff; border-radius: 99px; padding: 3px 10px 3px 4px; border: 2px dashed var(--line); }
.reward-peek img { width: 26px; height: 26px; }
.quiz-prompt { display: flex; align-items: center; justify-content: center; gap: 10px; font-size: clamp(24px, 5vw, 34px); font-weight: 700; text-align: center; line-height: 1.35; }
.icon-btn { border: 0; background: #fff; border-radius: 50%; width: 44px; height: 44px; flex: none; font-size: 20px; box-shadow: 0 3px 0 rgba(0, 0, 0, .12); }
.quiz-visual { display: flex; align-items: center; justify-content: center; gap: 16px; flex-wrap: wrap; }
.quiz-visual:empty { display: none; }
.quiz-img { width: clamp(120px, 26vw, 180px); height: clamp(120px, 26vw, 180px); animation: bounce-in .5s; filter: drop-shadow(0 6px 6px rgba(0, 0, 0, .15)); }
.big-text { font-size: clamp(56px, 13vw, 96px); font-weight: 700; color: var(--english); letter-spacing: 4px; }
.listen-btn { border: 0; background: var(--english); color: #fff; font-size: 24px; font-weight: 700; padding: 16px 28px; border-radius: 22px; box-shadow: 0 5px 0 #7d3fc4; }

.choices { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.choice {
  position: relative; border: 3px solid var(--line); background: #fff; border-radius: 20px; min-height: 76px; font-size: clamp(22px, 4.6vw, 30px); font-weight: 700;
  box-shadow: 0 5px 0 rgba(0, 0, 0, .1); padding: 8px 12px; display: flex; align-items: center; justify-content: center; transition: transform .1s, background .2s;
}
.choice:active:not(:disabled) { transform: translateY(3px); }
.choice .key { position: absolute; left: 10px; top: 8px; font-size: 13px; color: #bba68a; }
.choice img { width: 84px; height: 84px; }
.choices.images .choice { min-height: 110px; }
.choice.right { background: #d8f8e6; border-color: var(--good); animation: pulse .6s 2; }
.choice.wrong { background: #ffe1e4; border-color: var(--bad); animation: shake .4s; opacity: .7; }
.choice:disabled { cursor: default; }

.speak { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.mic-btn {
  border: 0; background: linear-gradient(#ff7a8a, #ff4d6a); color: #fff; font-size: 24px; font-weight: 700; padding: 16px 30px; border-radius: 999px;
  box-shadow: 0 6px 0 #c92f4a; display: flex; align-items: center; gap: 10px;
}
.mic-ico { font-size: 34px; }
.mic-btn.listening { animation: pulse .8s infinite; background: linear-gradient(#42d39b, #22b37b); box-shadow: 0 6px 0 #138a5c; }
.mic-btn:disabled { opacity: .5; }
.heard { min-height: 26px; font-size: 20px; color: var(--ink-soft); font-weight: 600; }
.speak-tools { display: flex; gap: 10px; }
.small-btn { border: 2px solid var(--line); background: #fff; font-size: 16px; font-weight: 700; padding: 8px 16px; border-radius: 999px; }

.quiz-feedback { text-align: center; font-size: 22px; font-weight: 700; min-height: 0; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.quiz-feedback.hint { color: #c46a00; }
.quiz-feedback.good { color: var(--good); }
.quiz-feedback.bad { color: var(--ink); }
.quiz-feedback .yay { font-size: 30px; animation: bounce-in .5s; }
.quiz-feedback .got { display: flex; align-items: center; gap: 6px; color: var(--ink); background: #fff7cf; padding: 6px 16px; border-radius: 99px; }
.quiz-feedback .got img { width: 34px; height: 34px; }
.quiz-feedback .explain { font-size: 18px; color: var(--ink-soft); font-weight: 600; }
.inline-img { width: 48px; height: 48px; vertical-align: middle; }
.next-btn { border: 0; background: var(--primary); color: #fff; font-size: 24px; font-weight: 700; padding: 12px 36px; border-radius: 999px; box-shadow: 0 5px 0 var(--primary-dark); margin-top: 4px; }

.frac { display: inline-flex; flex-direction: column; align-items: center; vertical-align: middle; line-height: 1; margin: 0 3px; font-size: .8em; }
.frac span:first-child { border-bottom: 3px solid currentColor; padding: 0 4px 2px; }
.frac span:last-child { padding-top: 2px; }

.confetti { position: absolute; top: 30%; width: 10px; height: 14px; border-radius: 3px; animation: confetti 1.4s ease-out forwards; pointer-events: none; }

/* ---------- 動畫 ---------- */
.pop-in { animation: bounce-in .4s; }
@keyframes bounce-in { 0% { transform: scale(.6); opacity: 0; } 70% { transform: scale(1.05); opacity: 1; } 100% { transform: scale(1); } }
@keyframes pulse { 50% { transform: scale(1.06); } }
@keyframes shake { 20%, 60% { transform: translateX(-8px); } 40%, 80% { transform: translateX(8px); } }
@keyframes wobble { from { transform: rotate(-6deg); } to { transform: rotate(6deg); } }
@keyframes float-up { from { opacity: 1; margin-top: 0; } to { opacity: 0; margin-top: -60px; } }
@keyframes confetti { from { transform: translate(0, 0) rotate(0); opacity: 1; } to { transform: translate(var(--dx), 260px) rotate(540deg); opacity: 0; } }

@media (max-width: 640px) {
  .chip { font-size: 15px; padding: 4px 9px; }
  .chip img, #hearts img { width: 20px; height: 20px; }
  #bossbar .bossinfo { min-width: 90px; }
  .dpad { transform: scale(.85); transform-origin: bottom left; }
  #btnFire { width: 92px; height: 92px; }
  .choices.images .choice { min-height: 96px; }
  .choice img { width: 64px; height: 64px; }
}
