:root {
  --cream: #f7ecd8;
  --cream-deep: #efe0c0;
  --ink: #2a3a66;
  --ink-soft: #4a5d8c;
  --blue: #4ea8f5;
  --blue-deep: #2f7fd4;
  --green: #7ed957;
  --green-deep: #4caf50;
  --pink: #ff8fab;
  --yellow: #ffd54f;
  --tray: #c9955a;
  --tray-light: #e8c089;
  --tray-edge: #8b5a2b;
  --shadow: rgba(60, 40, 20, 0.18);
  --radius: 22px;
  --font-display: "Fredoka", system-ui, sans-serif;
  --font-ui: "Nunito", system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  max-height: 100%;
  overflow: hidden;
  font-family: var(--font-ui);
  color: var(--ink);
  background: var(--cream);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}

button {
  font: inherit;
  border: 0;
  background: none;
  cursor: pointer;
  padding: 0;
}

button:focus-visible {
  outline: 3px solid var(--blue);
  outline-offset: 3px;
}

img {
  max-width: 100%;
  display: block;
  pointer-events: none;
}

.loading {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  background: var(--cream);
  font-family: var(--font-display);
  font-size: 1.4rem;
  color: var(--ink-soft);
  transition: opacity 0.28s ease;
}

.loading.is-done {
  opacity: 0;
  pointer-events: none;
}

.app {
  position: fixed;
  inset: 0;
  isolation: isolate;
  width: 100%;
  height: 100%;
  height: 100dvh;
  max-width: none;
  margin: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.sky {
  position: absolute;
  inset: 0;
  z-index: -2;
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, #fff8e8 0%, transparent 55%),
    linear-gradient(180deg, #fff6e4 0%, var(--cream) 42%, #e8f5d8 100%);
}

.garden {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: min(72px, 12vh);
  z-index: -1;
  pointer-events: none;
}

.bush {
  position: absolute;
  bottom: -16px;
  width: min(120px, 28vw);
  height: min(72px, 11vh);
  background: radial-gradient(circle at 30% 40%, #8fd46a, #5aaa3a 70%);
  border-radius: 50% 50% 40% 40%;
  filter: drop-shadow(0 4px 0 rgba(0, 0, 0, 0.06));
}

.bush::before,
.bush::after {
  content: "";
  position: absolute;
  background: inherit;
  border-radius: 50%;
}

.bush::before {
  width: 90px;
  height: 80px;
  left: -30px;
  bottom: 10px;
}

.bush::after {
  width: 100px;
  height: 85px;
  right: -25px;
  bottom: 8px;
}

.bush-left {
  left: -30px;
}

.bush-right {
  right: -30px;
}

.flower {
  position: absolute;
  bottom: 28px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  box-shadow:
    10px 0 0 currentColor,
    -10px 0 0 currentColor,
    0 10px 0 currentColor,
    0 -10px 0 currentColor;
}

.flower::after {
  content: "";
  position: absolute;
  inset: 3px;
  background: #fff59d;
  border-radius: 50%;
}

.f1 {
  left: 18%;
  color: #ff8fab;
  animation: bob 3s ease-in-out infinite;
}

.f2 {
  left: 28%;
  color: #ffd54f;
  bottom: 40px;
  animation: bob 3.4s ease-in-out infinite 0.2s;
}

.f3 {
  right: 22%;
  color: #64b5f6;
  animation: bob 2.8s ease-in-out infinite 0.4s;
}

.f4 {
  right: 32%;
  color: #ff8fab;
  bottom: 48px;
  animation: bob 3.2s ease-in-out infinite 0.1s;
}

.f5 {
  left: 48%;
  color: #ffd54f;
  bottom: 22px;
  animation: bob 3.6s ease-in-out infinite 0.3s;
}

@keyframes bob {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-6px);
  }
}

.topbar {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: max(0.35rem, env(safe-area-inset-top)) 0.55rem 0.1rem;
  z-index: 5;
  flex-shrink: 0;
}

.topbar-spacer {
  flex: 1;
}

.icon-btn {
  width: clamp(48px, 12vw, 56px);
  height: clamp(48px, 12vw, 56px);
  flex-shrink: 0;
  transition: transform 0.15s ease;
}

.icon-btn:active {
  transform: scale(0.92);
}

.icon-btn img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  filter: drop-shadow(0 3px 0 var(--shadow));
}

.coach {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.55rem;
  width: min(460px, 94%);
  margin: 0.2rem auto 0.3rem;
  flex-shrink: 0;
  z-index: 4;
}

.coach-mascot {
  width: clamp(48px, 13vw, 64px);
  height: auto;
  flex-shrink: 0;
  filter: drop-shadow(0 3px 0 var(--shadow));
  animation: floaty 2.8s ease-in-out infinite;
}

.coach-bubble {
  position: relative;
  flex: 1;
  min-width: 0;
  background: linear-gradient(180deg, #ffffff 0%, #f3f8ff 100%);
  border: 3px solid #6db4f0;
  border-radius: 20px;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.9),
    0 4px 0 rgba(47, 127, 212, 0.22);
  padding: 0.7rem 1rem;
}

.coach-bubble::before {
  content: "";
  position: absolute;
  left: -8px;
  top: 50%;
  width: 14px;
  height: 14px;
  background: inherit;
  border-left: 3px solid #6db4f0;
  border-bottom: 3px solid #6db4f0;
  transform: translateY(-50%) rotate(45deg);
  border-radius: 2px;
  box-shadow: -2px 2px 0 rgba(47, 127, 212, 0.12);
}

.coach-text {
  position: relative;
  z-index: 1;
  margin: 0;
  text-align: center;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(1.05rem, 4.2vw, 1.3rem);
  color: var(--ink);
  line-height: 1.25;
  letter-spacing: 0.01em;
}

.coach.pop .coach-bubble {
  animation: pop-in 0.35s ease;
}

@keyframes pop-in {
  0% {
    transform: scale(0.9);
    opacity: 0.5;
  }
  70% {
    transform: scale(1.03);
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

.stage {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 0.15rem 0.75rem max(1rem, env(safe-area-inset-bottom));
  position: relative;
  overflow: hidden;
}

.screen {
  flex: 1;
  min-height: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  animation: fade-up 0.3s ease;
}

.screen.is-active {
  display: flex;
}

.screen[data-screen="grades"] {
  justify-content: center;
  gap: 0.35rem;
}

.screen[data-screen="play"] {
  justify-content: flex-start;
}

@keyframes fade-up {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.brand {
  margin-top: 0.25rem;
  text-align: center;
  flex-shrink: 0;
}

.brand-fruit {
  width: clamp(84px, 22vw, 120px);
  margin: 0 auto 0.35rem;
  animation: floaty 2.8s ease-in-out infinite;
  filter: drop-shadow(0 6px 0 var(--shadow));
}

@keyframes floaty {
  0%,
  100% {
    transform: translateY(0) rotate(-3deg);
  }
  50% {
    transform: translateY(-6px) rotate(3deg);
  }
}

.brand-title {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(2.2rem, 9vw, 3rem);
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--blue-deep);
  text-shadow: 0 3px 0 rgba(47, 127, 212, 0.2);
}

.brand-sub {
  margin: 0.25rem 0 0;
  color: var(--ink-soft);
  font-weight: 700;
  font-size: clamp(0.95rem, 3.5vw, 1.1rem);
}

.start-btn {
  margin-top: clamp(0.75rem, 3vh, 1.4rem);
  width: min(260px, 72%);
  transition: transform 0.15s ease;
  flex-shrink: 0;
}

.start-btn img {
  animation: pulse 2s ease-in-out infinite;
}

.start-btn:active {
  transform: scale(0.94);
}

.start-btn:active img {
  animation: none;
}

@keyframes pulse {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.04);
  }
}

.start-fruits {
  display: flex;
  gap: 0.45rem;
  margin-top: auto;
  padding-bottom: 0.85rem;
  justify-content: center;
  flex-shrink: 0;
}

.start-fruits img {
  width: clamp(36px, 8vmin, 48px);
  animation: floaty 2.6s ease-in-out infinite;
  filter: drop-shadow(0 3px 0 var(--shadow));
}

.start-fruits img:nth-child(2) {
  animation-delay: 0.2s;
  width: clamp(34px, 7.5vmin, 44px);
}

.start-fruits img:nth-child(3) {
  animation-delay: 0.35s;
}

.start-fruits img:nth-child(4) {
  animation-delay: 0.5s;
  width: clamp(34px, 7.5vmin, 46px);
}

.section-title {
  margin: 0.1rem 0 0.25rem;
  font-family: var(--font-display);
  font-size: clamp(1rem, 3.6vmin, 1.25rem);
  color: var(--ink);
  text-align: center;
  flex-shrink: 0;
}

.screen[data-screen="grades"] .section-title {
  display: none; /* speech bubble already says “Pick your grade!” */
}

.grade-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: clamp(10px, 2.5vw, 16px);
  width: min(740px, 96%);
  flex: 0 0 auto;
  align-items: center;
  justify-items: center;
  padding: 0.5rem 0 0.65rem;
}

.grade-btn {
  width: 100%;
  max-width: min(128px, 28vw);
  aspect-ratio: 1;
  padding: 0;
  transition: transform 0.15s ease;
  filter: drop-shadow(0 3px 0 var(--shadow));
}

.grade-btn:hover {
  transform: translateY(-2px);
}

.grade-btn:active {
  transform: scale(0.94);
}

.grade-btn img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.hud {
  width: 100%;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.4rem;
  margin-bottom: 0.3rem;
  flex-shrink: 0;
}

.hearts {
  display: flex;
  gap: 0.1rem;
}

.heart {
  width: clamp(28px, 7vw, 34px);
  height: clamp(28px, 7vw, 34px);
  object-fit: contain;
  transition: opacity 0.25s ease, filter 0.25s ease;
}

.heart.is-lost {
  opacity: 0.28;
  filter: grayscale(1);
}

.progress-wrap {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-width: 0;
}

.progress-track {
  height: 14px;
  border-radius: 999px;
  background: #f0e2c4;
  border: 3px solid #e0c992;
  overflow: hidden;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.08);
}

.progress-fill {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, #8fd46a, #4caf50);
  transition: width 0.35s ease;
}

.progress-label {
  font-size: 0.75rem;
  font-weight: 800;
  color: var(--ink-soft);
  text-align: center;
}

.grade-pill {
  font-family: var(--font-display);
  font-size: 0.78rem;
  font-weight: 600;
  color: #fff;
  background: var(--blue);
  padding: 0.35rem 0.55rem;
  border-radius: 999px;
  box-shadow: 0 3px 0 rgba(47, 127, 212, 0.35);
  white-space: nowrap;
}

.equation {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  flex-wrap: wrap;
  min-height: clamp(48px, 9vh, 64px);
  margin: 0.15rem 0 0.4rem;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(1.85rem, 8vw, 2.5rem);
  flex-shrink: 0;
}

.eq-num {
  min-width: 1.1em;
  text-align: center;
  transition: transform 0.2s ease;
}

.eq-num.is-correct {
  color: var(--green-deep) !important;
  transform: scale(1.12);
}

.eq-num.is-wrong {
  color: #e53935 !important;
  animation: shake-x 0.35s ease;
}

.eq-op {
  color: var(--blue-deep);
}

.eq-sum {
  color: var(--ink);
}

.answer-box {
  display: inline-grid;
  place-items: center;
  width: clamp(52px, 12vw, 64px);
  height: clamp(52px, 12vw, 64px);
  border-radius: 14px;
  background: #fff;
  border: 3px solid var(--blue);
  box-shadow: 0 3px 0 rgba(47, 127, 212, 0.25);
  color: var(--ink);
  font-size: clamp(1.5rem, 6vw, 1.9rem);
}

.answer-box.is-correct {
  border-color: var(--green-deep);
  background: #e8f8e0;
}

.answer-box.is-wrong {
  border-color: #e53935;
  background: #ffe8e8;
  animation: shake-x 0.35s ease;
}

@keyframes shake-x {
  0%,
  100% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-6px);
  }
  75% {
    transform: translateX(6px);
  }
}

.app.shake .stage {
  animation: shake-x 0.4s ease;
}

.trays {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(0.3rem, 1.5vmin, 0.55rem);
  width: 100%;
  flex: 1;
  min-height: 0;
}

.trays--single .tray {
  max-width: min(260px, 78%);
  width: 75%;
}

.tray {
  position: relative;
  flex: 1;
  max-width: min(220px, 44vw);
  min-height: 0;
  height: 100%;
  max-height: min(32vh, 200px);
  padding: clamp(0.5rem, 1.5vw, 0.8rem) clamp(0.4rem, 1.2vw, 0.65rem);
  background:
    linear-gradient(180deg, var(--tray-light), var(--tray) 55%, #b8824a);
  border: 4px solid var(--tray-edge);
  border-radius: 14px;
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.35),
    0 5px 0 rgba(139, 90, 43, 0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.tray--mystery {
  background: linear-gradient(180deg, #d7c4a4, #b89a72);
}

.tray-fruits {
  display: flex;
  flex-wrap: wrap;
  gap: 0.12rem;
  justify-content: center;
  align-items: center;
  max-width: 100%;
  max-height: 100%;
}

.fruit {
  width: clamp(30px, 9vw, 44px);
  height: auto;
  animation: fruit-in 0.35s ease both;
  animation-delay: calc(var(--i, 0) * 0.04s);
  filter: drop-shadow(0 2px 0 rgba(0, 0, 0, 0.12));
}

.fruit.is-ghost {
  opacity: 0.28;
  filter: grayscale(0.4);
}

@keyframes fruit-in {
  from {
    opacity: 0;
    transform: scale(0.5) translateY(8px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.tray-mystery-mark {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-family: var(--font-display);
  font-size: clamp(2rem, 8vmin, 2.8rem);
  font-weight: 700;
  color: rgba(42, 58, 102, 0.55);
  pointer-events: none;
  z-index: 1;
}

.tray-plus {
  font-family: var(--font-display);
  font-size: clamp(1.4rem, 5vmin, 1.9rem);
  font-weight: 700;
  color: var(--blue-deep);
  flex-shrink: 0;
}

.choices {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: clamp(0.45rem, 2vw, 0.7rem);
  width: 100%;
  max-width: 460px;
  margin-top: 0.45rem;
  padding-bottom: 0.2rem;
  flex-shrink: 0;
}

.choice {
  aspect-ratio: 1;
  max-height: min(88px, 14vh);
  min-height: 64px;
  border-radius: 16px;
  background: linear-gradient(180deg, #e8f4ff, #cfe8ff);
  border: 3px solid #7eb8ef;
  box-shadow: 0 4px 0 rgba(47, 127, 212, 0.28);
  font-family: var(--font-display);
  font-size: clamp(1.45rem, 6vw, 1.9rem);
  font-weight: 700;
  color: var(--ink);
  transition: transform 0.12s ease, background 0.15s ease;
}

.choice:active:not(:disabled) {
  transform: translateY(3px);
  box-shadow: 0 2px 0 rgba(47, 127, 212, 0.28);
}

.choice:disabled {
  cursor: default;
}

.choice.is-correct {
  background: linear-gradient(180deg, #e4f8d8, #b8e986);
  border-color: var(--green-deep);
  box-shadow: 0 5px 0 rgba(76, 175, 80, 0.35);
}

.choice.is-wrong {
  background: linear-gradient(180deg, #ffe0e0, #ffb3b3);
  border-color: #e53935;
  animation: shake-x 0.35s ease;
}

.feedback {
  position: absolute;
  top: 42%;
  left: 50%;
  width: 88px;
  transform: translate(-50%, -50%) scale(0.6);
  opacity: 0;
  pointer-events: none;
  z-index: 6;
}

.feedback:not([hidden]) {
  animation: feedback-pop 0.7s ease forwards;
}

@keyframes feedback-pop {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.4);
  }
  35% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.12);
  }
  70% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -60%) scale(0.9);
  }
}

.trophy {
  width: clamp(72px, 16vmin, 110px);
  margin: 0.15rem auto 0.15rem;
  animation: floaty 2.4s ease-in-out infinite;
  filter: drop-shadow(0 6px 0 var(--shadow));
}

.results-grade {
  margin: 0;
  font-weight: 800;
  font-size: clamp(0.85rem, 2.5vmin, 1rem);
  color: var(--ink-soft);
}

.results-score {
  margin: 0.2rem 0 0.45rem;
  font-family: var(--font-display);
  font-size: clamp(1.7rem, 6vmin, 2.3rem);
  font-weight: 700;
  color: var(--blue-deep);
}

.results-stars {
  display: flex;
  gap: 0.35rem;
  margin-bottom: 0.75rem;
}

.result-star {
  width: clamp(44px, 10vmin, 60px);
  opacity: 0.45;
  transform: scale(0.8);
}

.result-star.is-earned {
  opacity: 1;
  animation: star-pop 0.45s ease both;
  animation-delay: calc(var(--i, 1) * 0.12s);
}

@keyframes star-pop {
  0% {
    transform: scale(0.4) rotate(-12deg);
  }
  70% {
    transform: scale(1.15) rotate(6deg);
  }
  100% {
    transform: scale(1) rotate(0);
  }
}

.results-actions {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  width: min(260px, 88%);
  flex-shrink: 0;
}

.text-btn {
  font-family: var(--font-display);
  font-size: clamp(1rem, 3.5vmin, 1.15rem);
  font-weight: 600;
  color: #fff;
  padding: 0.65rem 1rem;
  border-radius: 999px;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.15);
  transition: transform 0.12s ease;
}

.text-btn:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.15);
}

.text-btn--green {
  background: linear-gradient(180deg, #9be86a, #5fc23a);
}

.text-btn--blue {
  background: linear-gradient(180deg, #6cbcf7, #3b8fe0);
}

.fx-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 20;
  overflow: hidden;
}

.fx-coin {
  position: absolute;
  left: 50%;
  top: 40%;
  width: 56px;
  margin-left: -28px;
  animation: coin-up 0.85s ease forwards;
  filter: drop-shadow(0 4px 0 var(--shadow));
}

@keyframes coin-up {
  0% {
    opacity: 0;
    transform: translateY(20px) scale(0.5);
  }
  30% {
    opacity: 1;
    transform: translateY(0) scale(1.1);
  }
  100% {
    opacity: 0;
    transform: translateY(-80px) scale(0.8);
  }
}

@media (min-width: 700px) {
  .grade-btn {
    max-width: 130px;
  }
}

/* Phones: bigger tap targets, 3+2 grade layout */
@media (max-width: 560px) {
  .grade-grid {
    grid-template-columns: repeat(6, minmax(0, 1fr));
    width: min(420px, 96%);
    gap: 12px;
  }

  .grade-btn {
    max-width: none;
  }

  .grade-btn:nth-child(1) {
    grid-column: 1 / 3;
  }

  .grade-btn:nth-child(2) {
    grid-column: 3 / 5;
  }

  .grade-btn:nth-child(3) {
    grid-column: 5 / 7;
  }

  .grade-btn:nth-child(4) {
    grid-column: 2 / 4;
  }

  .grade-btn:nth-child(5) {
    grid-column: 4 / 6;
  }

  .start-fruits img {
    width: clamp(42px, 11vw, 54px);
  }

  .start-fruits img:nth-child(2),
  .start-fruits img:nth-child(4) {
    width: clamp(40px, 10vw, 50px);
  }

  .text-btn {
    font-size: 1.15rem;
    padding: 0.8rem 1.1rem;
  }

  .result-star {
    width: clamp(52px, 14vw, 68px);
  }

  .trophy {
    width: clamp(88px, 24vw, 120px);
  }
}

/* Only shrink decorative bits on very short frames — keep controls large */
@media (max-height: 430px) {
  .start-fruits,
  .garden .flower,
  .brand-sub {
    display: none;
  }

  .coach-mascot {
    width: 40px;
  }

  .coach-text {
    font-size: 1rem;
  }
}
