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

:root {
  --navy: #1b2a4a;
  --navy2: #23365e;
  --gold: #fed24a;
  --paper: #fdfbf2;
  --line: #b9d4e8;
  --ink: #2b3a55;
  --pencil: #4a5878;
  --good: #2fa46a;
  --bad: #e0564a;
  --font-ui: "Baloo 2", "Comic Sans MS", sans-serif;
  --font-px: "Press Start 2P", monospace;
  --font-hand: "Baloo 2", "Comic Sans MS", sans-serif;
}

html, body { height: 100%; }
body {
  background: var(--navy);
  background-image: radial-gradient(circle at 20% 10%, #2b4170 0%, var(--navy) 55%);
  color: #fff;
  font-family: var(--font-ui);
  overflow-x: hidden;
}

.screen { min-height: 100dvh; min-height: 100vh; display: flex; flex-direction: column; }
.hidden { display: none !important; }

/* ---------- splash / end ---------- */
.splash-inner {
  margin: auto; text-align: center; padding: 32px 20px; max-width: 520px; width: 100%;
}
.splash-kicker { font-family: var(--font-px); font-size: 11px; color: var(--gold); letter-spacing: 2px; margin-bottom: 18px; }
.splash-title { font-family: var(--font-px); font-size: clamp(26px, 7vw, 44px); line-height: 1.35; text-shadow: 0 4px 0 rgba(0,0,0,.35); margin-bottom: 14px; }
.splash-sub { font-size: 17px; font-weight: 700; color: #bcd0f0; letter-spacing: 1px; margin-bottom: 26px; }
.splash-eq {
  background: var(--paper); color: var(--ink); border-radius: 14px; padding: 18px 10px;
  font-size: 26px; font-weight: 700; margin-bottom: 28px; box-shadow: 0 6px 0 rgba(0,0,0,.3);
}
.big-btn {
  font-family: var(--font-px); font-size: 16px; padding: 18px 34px; border: 0; border-radius: 14px;
  background: var(--gold); color: #4a3403; cursor: pointer; box-shadow: 0 6px 0 #b8901c;
  transition: transform .08s;
}
.big-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 #b8901c; }
.splash-best { margin-top: 22px; font-size: 15px; color: #9fb4d8; min-height: 20px; }
#ver { margin-top: 10px; font-size: 12px; color: #56699b; }

#end-stars { font-size: 42px; margin: 6px 0 14px; letter-spacing: 4px; }
#end-msg { font-size: 18px; line-height: 1.5; color: #d7e2f5; margin-bottom: 26px; }

/* ---------- HUD ---------- */
#hud {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 14px 8px; gap: 10px;
}
#hud-level { font-family: var(--font-px); font-size: 10px; color: var(--gold); margin-bottom: 7px; letter-spacing: 1px; }
#progress { display: flex; gap: 4px; flex-wrap: wrap; }
#progress .dot {
  width: 13px; height: 13px; border-radius: 4px; background: #3d5182; border: 1px solid #56699b;
}
#progress .dot.done { background: var(--good); border-color: var(--good); }
#progress .dot.boss { border-radius: 50%; }
#progress .dot.cur { background: var(--gold); border-color: var(--gold); }
#hud-right { display: flex; gap: 14px; font-weight: 800; font-size: 17px; }

#boss-banner {
  text-align: center; background: #7c1f1f; color: #ffd9d9; font-weight: 800;
  padding: 8px 10px; font-size: 15px; letter-spacing: .5px;
  border-top: 2px solid #a53535; border-bottom: 2px solid #a53535;
  animation: bossPulse 1.2s infinite;
}
@keyframes bossPulse { 0%,100% { background: #7c1f1f; } 50% { background: #942828; } }

/* ---------- worksheet paper ---------- */
#paper {
  margin: 10px 12px 0; background: var(--paper); border-radius: 12px;
  box-shadow: 0 5px 0 rgba(0,0,0,.3);
  flex: 0 1 auto; max-height: 42vh; overflow-y: auto;
}
#paper-head {
  font-family: var(--font-hand); font-size: 16px; font-weight: 800; color: #8c2f2f;
  border-bottom: 2px solid #e5b8b8; padding: 5px 14px; background: #fff7f0;
}
#work { padding: 8px 14px 14px; }
#work .wline {
  color: var(--ink); font-size: 24px; font-weight: 700; padding: 7px 2px 6px 26px;
  border-bottom: 1px solid var(--line); position: relative;
  animation: writeIn .35s ease-out;
}
#work .wline.eqline { padding-left: 2px; font-size: 26px; }
#work .wline.note {
  font-family: var(--font-hand); font-size: 15.5px; color: var(--pencil); font-weight: 700;
}
#work .wline.answer { background: #eafff1; color: var(--good); border: 2px solid var(--good); border-radius: 8px; margin-top: 8px; text-align: center; padding-left: 2px; }
#work .wline.checkline { color: #6b7ba0; font-size: 20px; }
@keyframes writeIn { from { opacity: 0; transform: translateX(-8px); } to { opacity: 1; transform: none; } }

/* rules overlay */
.rules-btn {
  font-family: var(--font-ui); font-weight: 800; font-size: 15px; cursor: pointer;
  background: var(--navy2); color: var(--gold); border: 2px solid #55679a; border-radius: 10px;
  padding: 6px 12px; margin-top: 14px;
}
#hud-right .rules-btn { margin-top: 0; padding: 4px 9px; font-size: 16px; }
#rules {
  position: fixed; inset: 0; z-index: 50; background: rgba(10, 16, 32, .8);
  display: flex; align-items: flex-start; justify-content: center; overflow-y: auto; padding: 20px 12px;
}
#rules-card {
  background: var(--paper); color: var(--ink); border-radius: 14px; max-width: 560px; width: 100%;
  padding: 18px 20px 24px; box-shadow: 0 8px 0 rgba(0,0,0,.4); position: relative;
}
#rules-card h2 { font-size: 21px; margin: 4px 0 6px; color: #8c2f2f; }
#rules-card h3 { font-size: 17px; margin: 16px 0 4px; color: #2b6cb0; }
#rules-card p { font-size: 15.5px; line-height: 1.55; margin: 4px 0; }
#rules-close {
  position: sticky; top: 0; float: right; font-family: var(--font-ui); font-weight: 800;
  background: var(--bad); color: #fff; border: 0; border-radius: 8px; padding: 6px 12px; cursor: pointer;
}

/* teacher-style working */
.eqgrid { display: grid; grid-template-columns: 1fr auto 1fr; column-gap: 10px; align-items: baseline; padding-left: 2px !important; }
.eqgrid .lft { text-align: right; }
.eqgrid .eqs { text-align: center; }
.eqgrid .rgt { text-align: left; }
.oprow {
  color: #2b6cb0; font-family: var(--font-hand); font-size: 19px; font-weight: 800;
  border-bottom: 2px solid #55627e; padding: 1px 0 4px; animation: writeIn .4s ease-out;
}
.divrow { color: #2b6cb0; animation: writeIn .4s ease-out; padding: 3px 0 2px; }
.cancel { position: relative; display: inline-block; }
.cancel::after {
  content: ""; position: absolute; left: -3px; right: -3px; top: 50%;
  border-top: 3px solid rgba(224, 86, 74, .85); transform: rotate(-12deg);
}
.hl { background: #fff3a6; border-radius: 5px; padding: 0 3px; box-shadow: 0 0 0 2px #f2d43c; }
.dgA { color: #2b6cb0; } .dgB { color: #c2452f; }

/* stacked fractions */
.frac { display: inline-flex; flex-direction: column; vertical-align: middle; text-align: center; margin: 0 2px; line-height: 1; }
.frac .fn { font-size: .68em; padding: 0 3px 2px; border-bottom: 2px solid currentColor; }
.frac .fd { font-size: .68em; padding: 2px 3px 0; }
em { font-style: italic; font-family: Georgia, serif; padding: 0 1px; }

/* ---------- coach ---------- */
#coach {
  margin: 10px 12px 0; background: #fff; color: #333; border-radius: 12px; padding: 10px 14px;
  font-size: 16.5px; font-weight: 600; line-height: 1.4; display: flex; gap: 10px; align-items: flex-start;
  box-shadow: 0 4px 0 rgba(0,0,0,.25);
}
#coach.good { background: #e2f8ec; color: #14663d; }
#coach.bad  { background: #fdeceb; color: #8c2b23; }
#coach-emoji { font-size: 22px; }

/* ---------- Q & A ---------- */
#qa { padding: 12px 12px calc(18px + env(safe-area-inset-bottom)); margin-top: auto; }
#question { font-family: var(--font-px); font-size: 12px; line-height: 1.7; color: var(--gold); margin-bottom: 10px; text-align: center; }
#choices { display: flex; flex-direction: column; gap: 10px; }
#choices button {
  font-family: var(--font-ui); font-size: 18px; font-weight: 700; text-align: left;
  padding: 13px 16px; border-radius: 12px; border: 2px solid #55679a;
  background: var(--navy2); color: #eaf1ff; cursor: pointer; transition: transform .08s;
  line-height: 1.35;
}
#choices button:active { transform: scale(.98); }
#choices button.right { background: var(--good); border-color: var(--good); color: #fff; }
#choices button.wrong { background: var(--bad); border-color: var(--bad); color: #fff; animation: shake .3s; }
#choices button.okay { background: #a3720e; border-color: #d9a72e; color: #fff7df; }
#choices button:disabled { opacity: .55; }
#choices button.right:disabled, #choices button.wrong:disabled { opacity: 1; }
@keyframes shake { 0%,100% { transform: none; } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

#next-btn { width: 100%; margin-top: 10px; }
#skip-btn {
  display: block; margin: 10px auto 0; background: none; border: 0; cursor: pointer;
  color: #8fa4c8; font-family: var(--font-ui); font-weight: 700; font-size: 14px;
  text-decoration: underline;
}
#skip-btn.hidden { display: none; }

/* medium screens: keep it phone-shaped, centered */
@media (min-width: 760px) and (max-width: 819px) {
  #game { max-width: 560px; margin: 0 auto; width: 100%; }
}

/* wide screens (iPad landscape, desktop): worksheet pinned left, Q&A right — no scrolling */
@media (min-width: 820px) {
  #game {
    max-width: 1200px; margin: 0 auto; width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr);
    grid-template-rows: auto auto auto 1fr;
    grid-template-areas:
      "hud   hud"
      "boss  boss"
      "paper coach"
      "paper qa";
    column-gap: 10px;
    align-content: start;
  }
  #hud { grid-area: hud; }
  #boss-banner { grid-area: boss; }
  #paper { grid-area: paper; align-self: start; max-height: calc(100vh - 140px); max-height: calc(100dvh - 140px); }
  #coach { grid-area: coach; align-self: start; }
  #qa { grid-area: qa; align-self: start; margin-top: 0; padding-top: 12px; }
}
