:root {
  --ink: #25224a;
  --muted: #706d91;
  --purple: #6c4cff;
  --purple-dark: #5536df;
  --yellow: #ffcc4d;
  --green: #31c98b;
  --red: #ff6b7d;
  --card: rgba(255, 255, 255, 0.94);
  font-family: "Nunito", system-ui, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: linear-gradient(155deg, #e9e2ff 0%, #dff4ff 48%, #fff3cb 100%);
  overflow-x: hidden;
}

button, input { font: inherit; }
button { cursor: pointer; }

.sky { position: fixed; inset: 0; pointer-events: none; overflow: hidden; }
.cloud { position: absolute; width: 145px; height: 42px; border-radius: 50px; background: rgba(255,255,255,.65); filter: blur(.3px); }
.cloud::before, .cloud::after { content: ""; position: absolute; border-radius: 50%; background: inherit; }
.cloud::before { width: 64px; height: 64px; left: 25px; bottom: 6px; }
.cloud::after { width: 80px; height: 80px; right: 16px; bottom: 2px; }
.cloud-one { top: 14%; left: -35px; transform: scale(.75); }
.cloud-two { right: -20px; top: 22%; transform: scale(1.15); }
.sparkle { position: absolute; color: #fff; font-size: 24px; animation: twinkle 2.4s ease-in-out infinite; }
.sparkle-one { left: 10%; top: 45%; }
.sparkle-two { right: 13%; top: 67%; animation-delay: 1.1s; }

.app-shell { position: relative; width: min(1100px, calc(100% - 32px)); margin: 0 auto; padding: 28px 0 40px; }
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 24px; }
.brand-mark { display: grid; place-items: center; width: 56px; height: 56px; font-size: 30px; border-radius: 18px; background: white; box-shadow: 0 8px 22px rgba(66, 45, 142, .16); transform: rotate(-5deg); }
.eyebrow, .brand h1 { margin: 0; }
.eyebrow { font-size: 11px; font-weight: 800; color: var(--purple); text-transform: uppercase; letter-spacing: 1.5px; }
.brand h1 { font: 700 27px/1 "Fredoka", sans-serif; }
.sound-button { margin-left: auto; width: 48px; height: 48px; border: 0; border-radius: 16px; background: rgba(255,255,255,.78); font-size: 22px; box-shadow: 0 6px 16px rgba(66, 45, 142, .12); }

.screen { display: none; }
.screen.is-active { display: block; animation: screenIn .45s ease both; }
.start-screen, .result-screen { background: var(--card); border: 2px solid rgba(255,255,255,.8); border-radius: 34px; padding: clamp(26px, 5vw, 58px); box-shadow: 0 25px 70px rgba(75, 53, 151, .15); }
.welcome-copy { position: relative; max-width: 620px; margin: 0 auto 36px; text-align: center; }
.welcome-copy h2, .result-screen h2 { font: 700 clamp(32px, 5vw, 52px)/1.05 "Fredoka", sans-serif; margin: 5px 0 14px; }
.welcome-copy p:last-child, .result-message { margin: 0 auto; color: var(--muted); font-size: 18px; line-height: 1.55; }
.kicker { margin: 0; color: var(--purple); text-transform: uppercase; font-weight: 900; font-size: 13px; letter-spacing: 2px; }
.planet { position: relative; display: grid; place-items: center; width: 94px; height: 94px; margin: -20px auto 18px; border-radius: 50%; background: linear-gradient(140deg, #8b73ff, #6244ed); box-shadow: inset -12px -10px 0 rgba(62,38,178,.2), 0 12px 25px rgba(82,52,206,.25); color: white; font: 700 24px "Fredoka"; }
.planet-ring { position: absolute; width: 138px; height: 35px; border: 9px solid var(--yellow); border-radius: 50%; transform: rotate(-12deg); box-shadow: 0 4px 0 #e8a927; }
.planet-face { z-index: 1; }

.mode-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
.mode-card { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 15px; min-height: 116px; padding: 20px; border: 0; border-radius: 24px; text-align: left; color: var(--ink); transition: transform .2s, box-shadow .2s; }
.mode-card:hover { transform: translateY(-5px); box-shadow: 0 14px 28px rgba(55,41,108,.16); }
.mode-card:focus-visible, button:focus-visible, input:focus-visible { outline: 4px solid rgba(108,76,255,.28); outline-offset: 3px; }
.plus-card { background: #e3ddff; }
.minus-card { background: #d9f6ed; }
.mix-card { background: #fff0c9; }
.tables-card { background: #dceeff; }
.division-card { background: #d9f5f5; }
.big-plus-card { background: #e2f7dc; }
.big-minus-card { background: #ffe2e7; }
.fractions-card { background: #eee1ff; }
.mode-icon { display: grid; place-items: center; width: 58px; height: 58px; border-radius: 18px; color: white; font: 700 37px "Fredoka"; }
.plus-card .mode-icon { background: var(--purple); }
.minus-card .mode-icon { background: var(--green); }
.mix-card .mode-icon { background: #f4a62a; }
.tables-card .mode-icon { background: #378bd1; }
.division-card .mode-icon { background: #1ca3a3; }
.big-plus-card .mode-icon { background: #57b83c; }
.big-minus-card .mode-icon { background: #ef6479; }
.fractions-card .mode-icon { background: #9656d8; }
.fraction-icon { font-size: 31px; }
.mode-copy { display: flex; flex-direction: column; }
.mode-copy strong { font: 700 21px "Fredoka"; }
.mode-copy small { margin-top: 5px; color: var(--muted); font-size: 13px; }
.mode-arrow { font-size: 25px; }

.game-topbar { display: flex; align-items: center; gap: 18px; margin-bottom: 22px; }
.back-button { flex: 0 0 48px; height: 48px; border: 0; border-radius: 15px; background: white; color: var(--ink); font-size: 25px; box-shadow: 0 7px 18px rgba(66,45,142,.13); }
.mission-progress { flex: 1; }
.progress-label { display: flex; justify-content: space-between; margin-bottom: 7px; font-size: 14px; font-weight: 800; }
.progress-label span { color: var(--purple); }
.progress-track { height: 13px; overflow: hidden; border: 3px solid white; border-radius: 30px; background: rgba(255,255,255,.55); box-shadow: 0 4px 10px rgba(66,45,142,.08); }
.progress-track span { display: block; width: 0; height: 100%; border-radius: inherit; background: linear-gradient(90deg, #6c4cff, #a179ff); transition: width .45s ease; }
.score-pill, .timer-pill { display: flex; align-items: center; justify-content: center; gap: 7px; min-width: 76px; padding: 11px 16px; border-radius: 16px; background: white; box-shadow: 0 7px 18px rgba(66,45,142,.13); font-size: 19px; }
.timer-pill { min-width: 112px; transition: color .25s, background .25s, transform .25s; }
.timer-pill.hurry { color: #d94058; background: #fff0f2; animation: timerPulse 1s ease-in-out infinite; }

.game-layout { display: grid; grid-template-columns: 230px 1fr; gap: 22px; }
.star-map, .question-card { border: 2px solid rgba(255,255,255,.85); background: var(--card); box-shadow: 0 22px 55px rgba(75,53,151,.14); }
.star-map { padding: 22px; border-radius: 28px; align-self: stretch; }
.star-map p { margin: 0 0 16px; font: 700 18px "Fredoka"; text-align: center; }
.star-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px 7px; }
.mission-star { color: #dcd9e9; font-size: 23px; text-align: center; transition: color .25s, transform .35s; }
.mission-star.done { color: var(--yellow); transform: scale(1.18) rotate(8deg); text-shadow: 0 2px 0 #e2a724; }
.mission-star.missed { color: #dedbe8; }

.question-card { position: relative; display: flex; flex-direction: column; justify-content: center; min-height: 500px; padding: clamp(30px, 6vw, 70px); border-radius: 32px; text-align: center; overflow: hidden; }
.question-card::before { content: ""; position: absolute; inset: 18px; border: 2px dashed #e8e3f7; border-radius: 22px; pointer-events: none; }
.question-prompt { margin: 0 0 20px; color: var(--muted); font-weight: 800; }
.sum { position: relative; display: flex; align-items: center; justify-content: center; gap: clamp(10px, 2vw, 22px); margin: 0 0 38px; font: 700 clamp(40px, 8vw, 74px)/1 "Fredoka"; }
.sum.has-fractions { font-size: clamp(34px, 7vw, 62px); }
.fraction { display: inline-grid; grid-template-rows: 1fr 1fr; min-width: 1.15em; vertical-align: middle; line-height: .9; }
.fraction > span:first-child { padding: 0 .12em .09em; border-bottom: .08em solid currentColor; }
.fraction > span:last-child { padding: .09em .12em 0; }
.operator { color: var(--purple); }
.equals { color: #aaa5c1; }
.answer-slot { display: grid; place-items: center; min-width: 100px; min-height: 95px; padding: 0 10px; border-radius: 21px; background: #f1eefc; color: var(--purple); }
form { position: relative; z-index: 1; max-width: 480px; width: 100%; margin: 0 auto; }
form label { display: block; margin-bottom: 8px; font-size: 13px; font-weight: 900; text-align: left; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }
.answer-row { display: grid; grid-template-columns: 145px 1fr; gap: 12px; }
input { width: 100%; min-width: 0; padding: 12px; border: 3px solid #ded8f3; border-radius: 17px; background: white; color: var(--ink); font: 700 30px "Fredoka"; text-align: center; }
input:focus { border-color: var(--purple); outline: none; }
.check-button, .primary-button, .secondary-button { border: 0; border-radius: 17px; font-weight: 900; }
.check-button { padding: 14px 20px; color: white; background: var(--purple); box-shadow: 0 7px 0 var(--purple-dark); font-size: 17px; }
.check-button:active { transform: translateY(4px); box-shadow: 0 3px 0 var(--purple-dark); }
.check-button span { margin-left: 7px; }
.feedback { min-height: 40px; margin-top: 23px; font: 700 18px "Fredoka"; }
.answer-hint { position: relative; z-index: 1; min-height: 18px; margin: 12px 0 -12px; color: var(--muted); font-size: 13px; font-weight: 800; }
.number-pad { position: relative; z-index: 1; display: none; grid-template-columns: repeat(3, 1fr); gap: 8px; width: min(100%, 390px); margin: 22px auto 0; }
.number-pad button { min-height: 52px; border: 0; border-radius: 14px; color: var(--ink); background: #f1eefc; box-shadow: 0 3px 0 #d9d2ef; font: 700 22px "Fredoka"; touch-action: manipulation; user-select: none; }
.number-pad button:active { transform: translateY(2px); box-shadow: 0 1px 0 #d9d2ef; }
.number-pad .erase-key { color: #d94058; background: #fff0f2; }
.number-pad .fraction-key.is-disabled { visibility: hidden; }
.number-pad .enter-key { grid-column: 1 / -1; color: white; background: var(--green); box-shadow: 0 4px 0 #199567; }
.number-pad .enter-key span { margin-left: 6px; }
.feedback.correct { color: #159d68; }
.feedback.incorrect { color: #e44960; }
.streak { position: absolute; right: 28px; top: 26px; display: none; padding: 9px 13px; border-radius: 14px; background: #fff1ca; color: #a96900; font-weight: 900; }
.streak.show { display: block; animation: pop .4s ease both; }
.question-card.correct-flash { animation: good .55s ease; }
.question-card.wrong-shake { animation: shake .35s ease; }

.result-screen { text-align: center; }
.result-burst { display: grid; place-items: center; width: 120px; height: 120px; margin: -15px auto 23px; border-radius: 50%; background: #fff0be; font-size: 64px; box-shadow: 0 0 0 14px rgba(255,204,77,.17); animation: float 2.8s ease-in-out infinite; }
.result-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; max-width: 650px; margin: 32px auto; }
.result-stats div { display: flex; flex-direction: column; padding: 22px 10px; border-radius: 20px; background: #f4f1ff; }
.result-stats span { font-size: 25px; }
.result-stats strong { margin: 6px 0 2px; font: 700 23px "Fredoka"; }
.result-stats small { color: var(--muted); font-weight: 800; }
.result-actions { display: flex; justify-content: center; gap: 12px; }
.primary-button, .secondary-button { padding: 15px 23px; }
.primary-button { color: white; background: var(--purple); box-shadow: 0 6px 0 var(--purple-dark); }
.secondary-button { color: var(--purple); background: #ebe6ff; }

.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 10; overflow: hidden; }
.confetti-piece { position: absolute; top: -20px; width: 11px; height: 17px; border-radius: 3px; animation: fall 1.7s linear forwards; }

@keyframes screenIn { from { opacity: 0; transform: translateY(12px); } }
@keyframes twinkle { 50% { opacity: .3; transform: scale(.7); } }
@keyframes pop { 0% { transform: scale(.5); } 70% { transform: scale(1.12); } }
@keyframes float { 50% { transform: translateY(-8px) rotate(3deg); } }
@keyframes good { 50% { box-shadow: 0 22px 55px rgba(49,201,139,.35); border-color: #8ce1bf; } }
@keyframes shake { 25% { transform: translateX(-7px); } 50% { transform: translateX(7px); } 75% { transform: translateX(-4px); } }
@keyframes fall { to { transform: translate3d(var(--drift), 105vh, 0) rotate(720deg); } }
@keyframes timerPulse { 50% { transform: scale(1.05); } }

@media (max-width: 760px) {
  .app-shell { width: min(100% - 20px, 650px); padding-top: 15px; }
  .brand { margin-bottom: 14px; }
  .brand-mark { width: 47px; height: 47px; font-size: 25px; }
  .mode-grid { grid-template-columns: 1fr; }
  .mode-card { min-height: 88px; }
  .game-layout { grid-template-columns: 1fr; }
  .star-map { display: none; }
  .question-card { min-height: 470px; padding: 48px 20px 26px; }
  .game-topbar { gap: 9px; }
  .score-pill { padding: 10px; }
  .timer-pill { min-width: 88px; padding: 10px 8px; font-size: 16px; }
  .progress-label span { display: none; }
  .answer-row { grid-template-columns: 110px 1fr; }
  .sum { margin-bottom: 30px; }
  .answer-slot { min-width: 75px; min-height: 74px; }
  .answer-hint { margin-bottom: -6px; }
  .result-stats { grid-template-columns: 1fr 1fr 1fr; gap: 7px; }
  .result-stats div { padding: 15px 4px; }
  .result-actions { flex-direction: column; }
}

@media (any-pointer: coarse) and (max-width: 1400px) {
  body.playing { height: 100dvh; overflow: hidden; }
  body.playing .app-shell { width: 100%; height: 100dvh; padding: max(7px, env(safe-area-inset-top)) 8px max(7px, env(safe-area-inset-bottom)); }
  body.playing .brand { display: none; }
  body.playing .game-screen { height: 100%; }
  body.playing .game-topbar { height: 48px; margin-bottom: 7px; gap: 6px; }
  body.playing .back-button { flex-basis: 42px; width: 42px; height: 42px; border-radius: 13px; }
  body.playing .mission-progress { min-width: 0; }
  body.playing .progress-label { margin-bottom: 4px; font-size: 12px; }
  body.playing .progress-track { height: 10px; border-width: 2px; }
  body.playing .timer-pill { min-width: 82px; padding: 8px 7px; font-size: 15px; }
  body.playing .score-pill { min-width: 51px; padding: 8px 6px; font-size: 16px; }
  body.playing .game-layout { display: block; height: calc(100% - 55px); }
  body.playing .star-map { display: none; }
  body.playing .question-card { justify-content: flex-start; height: 100%; min-height: 0; padding: 9px 14px 8px; border-radius: 22px; }
  body.playing .question-card::before { inset: 7px; border-radius: 17px; }
  body.playing .question-prompt { display: none; }
  body.playing .sum { flex: 0 0 auto; min-height: 66px; margin: 0 0 6px; gap: clamp(6px, 2.5vw, 13px); font-size: clamp(35px, 11vw, 54px); }
  body.playing .sum.has-fractions { min-height: 82px; font-size: clamp(30px, 9vw, 46px); }
  body.playing .answer-slot { min-width: 61px; min-height: 55px; border-radius: 14px; }
  body.playing form { flex: 0 0 auto; }
  body.playing form label { display: none; }
  body.playing .answer-row { display: block; width: min(145px, 100%); margin: 0 auto; }
  body.playing input { height: 49px; padding: 5px 9px; border-radius: 14px; font-size: 26px; }
  body.playing .answer-hint { flex: 0 0 auto; min-height: 15px; margin: 4px 0 0; font-size: 11px; }
  body.playing .number-pad { display: grid; flex: 1 1 auto; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, minmax(38px, 1fr)); gap: 6px; width: min(100%, 430px); min-height: 174px; max-height: 218px; margin: 5px auto 0; }
  body.playing .number-pad button { min-height: 0; border-radius: 11px; font-size: 20px; }
  body.playing .number-pad [data-key="1"] { grid-area: 1 / 1; }
  body.playing .number-pad [data-key="2"] { grid-area: 1 / 2; }
  body.playing .number-pad [data-key="3"] { grid-area: 1 / 3; }
  body.playing .number-pad [data-key="4"] { grid-area: 2 / 1; }
  body.playing .number-pad [data-key="5"] { grid-area: 2 / 2; }
  body.playing .number-pad [data-key="6"] { grid-area: 2 / 3; }
  body.playing .number-pad [data-key="7"] { grid-area: 3 / 1; }
  body.playing .number-pad [data-key="8"] { grid-area: 3 / 2; }
  body.playing .number-pad [data-key="9"] { grid-area: 3 / 3; }
  body.playing .number-pad [data-key="0"] { grid-area: 4 / 1 / 5 / 4; }
  body.playing .number-pad .erase-key { grid-area: 1 / 4; }
  body.playing .number-pad .fraction-key { grid-area: 2 / 4; }
  body.playing .number-pad .enter-key { grid-area: 3 / 4 / span 2 / 5; padding: 3px; font-size: 16px; }
  body.playing .number-pad .enter-key span { display: block; margin: 3px 0 0; }
  body.playing .feedback { flex: 0 0 29px; min-height: 29px; margin-top: 5px; font-size: 15px; line-height: 1.2; }
  body.playing .streak { right: 14px; top: 8px; padding: 5px 8px; font-size: 12px; }
  .check-button { display: none; }
}

@media (any-pointer: coarse) and (max-width: 1400px) and (max-height: 600px) {
  body.playing .game-topbar { height: 42px; margin-bottom: 4px; }
  body.playing .game-layout { height: calc(100% - 46px); }
  body.playing .sum { min-height: 51px; font-size: clamp(31px, 9vw, 43px); }
  body.playing .sum.has-fractions { min-height: 62px; font-size: clamp(27px, 8vw, 39px); }
  body.playing input { height: 42px; }
  body.playing .answer-hint { display: none; }
  body.playing .number-pad { min-height: 150px; max-height: 174px; }
  body.playing .feedback { flex-basis: 23px; min-height: 23px; margin-top: 3px; font-size: 13px; }
}

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