:root {
  --bg: #2b2350;
  --panel: rgba(28, 22, 56, 0.92);
  --ink: #fff8ec;
  --muted: #c9bfe8;
  --gold: #ffc928;
  --accent: #ff6b4a;
  --mint: #4fd1c5;
  --line: rgba(255, 255, 255, 0.14);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --nav: calc(62px + var(--safe-b));
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
#stage { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
body:not(.ready) #stage { opacity: 0; }
#stage { transition: opacity 0.4s; }

/* top bar */
#top {
  position: fixed; top: calc(var(--safe-t) + 10px); left: 12px; right: 12px;
  display: flex; gap: 8px; justify-content: space-between; pointer-events: none;
}
.pill {
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
  padding: 6px 14px; display: flex; align-items: center; gap: 8px; font-size: 18px;
}
.pill.xp { flex-direction: column; align-items: stretch; gap: 3px; font-size: 13px; padding: 5px 14px 7px; min-width: 120px; }
.bar { height: 8px; border-radius: 99px; background: rgba(255,255,255,0.15); overflow: hidden; }
.bar > div { height: 100%; width: 0; background: var(--mint); border-radius: inherit; transition: width 0.25s; }

/* bottom sheet */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0;
  background: var(--panel); border-top: 1px solid var(--line);
  border-radius: 22px 22px 0 0; padding: 14px 14px 12px;
  max-width: 640px; margin: 0 auto; bottom: var(--nav); max-height: calc(60vh - var(--nav) + 40px); overflow-y: auto;
}
h1 { margin: 0 0 10px; font-size: 20px; }
#slots { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.slot {
  flex: 1 0 auto; border: 1px solid var(--line); background: transparent; color: var(--muted);
  border-radius: 12px; padding: 6px 8px; font: inherit; font-size: 12px; display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.slot span { font-size: 20px; }
.slot.on { background: rgba(255,255,255,0.12); color: var(--ink); border-color: var(--mint); }
#items { display: flex; gap: 10px; overflow-x: auto; padding: 12px 2px; scroll-snap-type: x mandatory; scrollbar-width: none; }
.item {
  flex: 0 0 min(72%, 230px); scroll-snap-align: start;
  border: 1px solid var(--line); border-radius: 16px; padding: 12px; display: flex; flex-direction: column; gap: 4px;
}
.item.on { border-color: var(--mint); box-shadow: inset 0 0 0 1px var(--mint); }
.item b { font-size: 17px; }
.item small { color: var(--gold); }
.item p { margin: 0 0 6px; color: var(--muted); font-size: 13px; flex: 1; }
footer { margin-top: 10px; font-size: 12px; color: var(--muted); text-align: center; }

/* buttons */
.btn {
  font: inherit; font-weight: 700; border: 0; border-radius: 14px; padding: 10px 14px;
  background: var(--mint); color: #12202a; cursor: pointer;
}
.btn:active:not(:disabled) { transform: scale(0.96); }
.btn:disabled { opacity: 0.45; }
.btn.gold { background: var(--gold); }
.btn.ghost { background: transparent; color: var(--ink); border: 1px solid var(--line); }
.btn.big { width: 100%; padding: 15px; font-size: 18px; background: var(--accent); color: #fff; }
.link { background: none; border: 0; color: var(--mint); font: inherit; text-decoration: underline; padding: 0; }

/* screens */
.screen { display: none; }
body[data-screen="home"] #home, body[data-screen="armory"] #armory, body[data-screen="rewards"] #rewards,
body[data-screen="parent"] #parent, body[data-screen="quest"] #quest, body[data-screen="battle"] #battle { display: block; }
body[data-screen="battle"] #tabs, body[data-screen="quest"] #tabs { display: none; }
.panel {
  position: fixed; inset: 0; bottom: var(--nav); overflow-y: auto; background: var(--bg);
  padding: calc(var(--safe-t) + 70px) 16px 24px;
}
.panel > * { max-width: 560px; margin-left: auto; margin-right: auto; }
#quest .enemybar .hp { margin-bottom: 2px; }
.questsheet {
  position: fixed; left: 0; right: 0; bottom: 0; top: 46vh; overflow-y: auto; max-width: 640px; margin: 0 auto;
  background: var(--panel); border-top: 1px solid var(--line); border-radius: 22px 22px 0 0;
  padding: 14px 14px calc(var(--safe-b) + 12px);
}
.questsheet h1 { font-size: 18px; margin-bottom: 8px; }
h2 { font-size: 16px; margin: 18px auto 8px; display: flex; justify-content: space-between; }
.purse { color: var(--gold); }
.hint { color: var(--muted); font-size: 13px; margin: 0 auto 8px; }
.list { display: flex; flex-direction: column; gap: 8px; }
.row { display: flex; gap: 10px; justify-content: center; align-items: center; flex-wrap: wrap; }
.line {
  display: flex; align-items: center; gap: 10px; border: 1px solid var(--line); border-radius: 14px; padding: 10px 12px;
  background: rgba(255,255,255,0.04);
}
.line .ico { font-size: 26px; }
.line .txt { flex: 1; display: flex; flex-direction: column; }
.line .txt small { color: var(--muted); }
.line .btn { padding: 8px 12px; }
.empty { color: var(--muted); font-size: 14px; }
.goalbar { height: 8px; margin-top: 5px; }
.goalbar > div { background: var(--gold); }

/* tab bar */
#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; height: var(--nav); padding-bottom: var(--safe-b);
  display: flex; background: #1c1638; border-top: 1px solid var(--line);
}
#tabs button {
  flex: 1; background: none; border: 0; color: var(--muted); font: inherit; font-size: 22px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
}
#tabs button span { font-size: 11px; }
#tabs button.on { color: var(--ink); }
#tabs button.on span { color: var(--mint); font-weight: 700; }

/* quests */
.week { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; align-items: center; font-size: 14px; margin-bottom: 10px; }
.week b { text-align: right; }
.week .bar { grid-column: 1 / -1; }
.week .bar > div { background: var(--gold); }
#quests { display: flex; flex-direction: column; gap: 8px; }
.quest {
  display: flex; align-items: center; gap: 12px; border: 1px solid var(--line); border-radius: 16px; padding: 10px 12px;
  background: rgba(255,255,255,0.04); color: var(--ink); font: inherit; text-align: left; width: 100%;
}
.quest .ico { font-size: 32px; }
.quest .txt { flex: 1; display: flex; flex-direction: column; }
.quest .txt small { color: var(--muted); }
.quest .state { font-weight: 800; color: var(--gold); white-space: nowrap; }
.quest.pending { border-color: var(--gold); }
.quest.pending .state { color: var(--gold); font-size: 13px; }
.quest.done { opacity: 0.6; }
.quest.done .state { color: var(--mint); }
.boss { display: flex; align-items: center; gap: 10px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.boss div { flex: 1; display: flex; flex-direction: column; font-size: 14px; }
.boss small { color: var(--muted); font-size: 12px; }

.monster { font-size: 72px; line-height: 1; margin: 6px auto; }
#q-steps { display: flex; flex-direction: column; gap: 8px; margin: 0 auto 12px; }
.step {
  font: inherit; font-size: 18px; font-weight: 700; padding: 11px 14px; border-radius: 16px; text-align: left;
  border: 2px solid var(--line); background: rgba(255,255,255,0.05); color: var(--ink); display: flex; gap: 12px; align-items: center;
}
.step::before { content: ""; width: 26px; height: 26px; border-radius: 8px; border: 2px solid var(--muted); flex: none; }
.step.on { border-color: var(--mint); background: rgba(79, 209, 197, 0.15); }
.step.on::before { content: "✓"; background: var(--mint); border-color: var(--mint); color: #12202a; display: grid; place-items: center; }
.back { display: block; margin: 10px auto 0; color: var(--muted); }

/* tooth brushing guide: four quadrants of the mouth */
#brush {
  position: fixed; left: 12px; right: 12px; bottom: calc(var(--safe-b) + 44px); max-width: 420px; margin: 0 auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 12px; text-align: center;
}
.mouth { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; width: 140px; margin: 0 auto; }
.mouth i { height: 34px; border-radius: 10px; background: rgba(255,255,255,0.12); }
.mouth i:nth-child(1) { border-top-left-radius: 30px; }
.mouth i:nth-child(2) { border-top-right-radius: 30px; }
.mouth i:nth-child(3) { border-bottom-left-radius: 30px; }
.mouth i:nth-child(4) { border-bottom-right-radius: 30px; }
.mouth i.on { background: var(--mint); box-shadow: 0 0 12px var(--mint); }
.mouth i.done { background: rgba(79, 209, 197, 0.35); }
#brushstep { font-size: 22px; font-weight: 800; margin: 8px 0; }
body[data-kind="timed"] .controls { display: none; }
body[data-kind="boss"] #brush { display: none; }

/* battle */
.enemybar {
  position: fixed; top: calc(var(--safe-t) + 64px); left: 12px; right: 12px; max-width: 520px; margin: 0 auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 10px 14px;
}
.enemybar .hp { height: 14px; margin: 6px 0 8px; }
.enemybar .hp > div { background: linear-gradient(90deg, #ff4a4a, #ff9b4a); }
.time { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.time .bar { flex: 1; }
.time .bar > div { background: var(--gold); transition: width 0.1s linear; }
.controls {
  position: fixed; left: 12px; right: 12px; bottom: calc(var(--safe-b) + 44px);
  display: flex; gap: 12px; max-width: 520px; margin: 0 auto;
}
.controls .btn { display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 34px; border-radius: 20px; }
.controls small { font-size: 14px; line-height: 1.1; }
.attack { flex: 2; background: var(--accent); color: #fff; padding: 18px; }
.spell { flex: 1; position: relative; overflow: hidden; background: #8fe8ff; }
.spell::after {
  content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: var(--cd, 0%);
  background: rgba(20, 16, 40, 0.55);
}
.flee { position: fixed; bottom: calc(var(--safe-b) + 12px); left: 0; right: 0; margin: auto; width: max-content; color: var(--muted); }

#speech {
  position: fixed; top: calc(var(--safe-t) + 160px); left: 50%; transform: translateX(-50%);
  margin: 0; max-width: 86vw; background: #fff8ec; color: #2b2350; font-weight: 700;
  padding: 8px 14px; border-radius: 14px; text-align: center; animation: pop 0.2s;
}
#speech[data-who="hero"] { background: #8fe8ff; }
body:not([data-screen="battle"]) #speech { top: calc(var(--safe-t) + 70px); }
#toast {
  position: fixed; top: calc(var(--safe-t) + 70px); left: 50%; transform: translateX(-50%); margin: 0;
  background: var(--gold); color: #2b2350; font-weight: 800; padding: 8px 16px; border-radius: 99px; animation: pop 0.2s;
}
#fx { position: fixed; inset: 0; pointer-events: none; }
.float {
  position: absolute; transform: translate(-50%, 0); font-weight: 900; font-size: 22px; color: #fff;
  text-shadow: 0 2px 0 #2b2350, 0 0 6px #2b2350; animation: rise 1s ease-out forwards; white-space: nowrap;
}
.float.big { font-size: 28px; color: var(--gold); }
@keyframes rise { to { transform: translate(-50%, -80px); opacity: 0; } }
@keyframes pop { from { transform: translateX(-50%) scale(0.8); } }

#result { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(10, 8, 25, 0.5); padding: 16px; }
#result[hidden] { display: none; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 22px; padding: 20px;
  width: min(100%, 360px); text-align: center; display: flex; flex-direction: column; gap: 10px;
}
.card h2 { margin: 0; font-size: 28px; }
.card p { margin: 0; color: var(--muted); }
.card .reward { color: var(--gold); font-size: 26px; font-weight: 900; }

/* 2D fallback when WebGL is missing */
#flat { display: none; }
body.flat #flat {
  position: fixed; inset: 0; display: flex; justify-content: center; align-items: center; gap: 12vw; font-size: 64px;
  padding-bottom: 40vh;
}
body.flat:not([data-screen="battle"]):not([data-screen="quest"]) #fb-enemy { display: none; }
#fb-enemy.hit { animation: shake 0.3s; }
#fb-enemy.lunge { animation: lunge 0.5s; }
@keyframes shake { 25% { transform: translateX(8px); } 75% { transform: translateX(-8px); } }
@keyframes lunge { 50% { transform: translateX(-30px) scale(1.1); } }

/* layering: bars and popups stay above full-screen panels */
#top, #tabs { z-index: 5; }
#speech, #toast { z-index: 6; }
#result { z-index: 10; }
.pend { color: var(--gold); font-size: 14px; font-weight: 700; opacity: 0.85; }
.card .monster { font-size: 56px; margin: 0; }

/* homework planning */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.chip {
  font: inherit; font-size: 15px; font-weight: 700; padding: 8px 12px; border-radius: 99px;
  border: 1px solid var(--line); background: rgba(255,255,255,0.08); color: var(--ink);
}
.chip:active { transform: scale(0.95); }
#q-planlist { margin-bottom: 12px; }
.btn.round { width: 38px; height: 38px; padding: 0; font-size: 20px; }
.count { min-width: 22px; text-align: center; font-size: 18px; }
.remove { color: var(--muted); text-decoration: none; font-size: 18px; padding: 4px 6px; }
.replan { display: block; margin: 0 auto 12px; font-size: 14px; }
#fx { z-index: 7; }
/* keep the main quest button reachable however long the list gets */
#q-start, #q-done:not(:disabled) { position: sticky; bottom: 0; box-shadow: 0 -8px 16px var(--panel); }
