:root {
  --purple-deep: #2a1048;
  --purple-mid: #4a1d7a;
  --purple-bright: #7b3fa8;
  --green: #3ecf6e;
  --green-soft: #6ee89a;
  --green-glow: rgba(62, 207, 110, 0.35);
  --white: #ffffff;
  --black: #0d0618;
  --font: "Fredoka", system-ui, sans-serif;
  --numpad-h: 0px;
}

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

html,
body {
  height: 100%;
}

body {
  font-family: var(--font);
  color: var(--white);
  background:
    radial-gradient(ellipse 90% 70% at 20% 10%, rgba(123, 63, 168, 0.55), transparent 55%),
    radial-gradient(ellipse 70% 60% at 85% 20%, rgba(62, 207, 110, 0.22), transparent 50%),
    radial-gradient(ellipse 80% 50% at 50% 100%, rgba(74, 29, 122, 0.8), transparent 55%),
    linear-gradient(165deg, var(--purple-deep) 0%, var(--black) 55%, #1a0a30 100%);
  background-attachment: fixed;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.stars {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background-image:
    radial-gradient(1.5px 1.5px at 8% 12%, rgba(255, 255, 255, 0.9), transparent),
    radial-gradient(1px 1px at 22% 28%, rgba(255, 255, 255, 0.7), transparent),
    radial-gradient(2px 2px at 38% 8%, rgba(110, 232, 154, 0.8), transparent),
    radial-gradient(1px 1px at 55% 18%, rgba(255, 255, 255, 0.6), transparent),
    radial-gradient(1.5px 1.5px at 72% 32%, rgba(255, 255, 255, 0.85), transparent),
    radial-gradient(1px 1px at 88% 14%, rgba(110, 232, 154, 0.7), transparent),
    radial-gradient(1px 1px at 15% 55%, rgba(255, 255, 255, 0.5), transparent),
    radial-gradient(2px 2px at 45% 62%, rgba(255, 255, 255, 0.75), transparent),
    radial-gradient(1px 1px at 68% 70%, rgba(110, 232, 154, 0.6), transparent),
    radial-gradient(1.5px 1.5px at 92% 58%, rgba(255, 255, 255, 0.65), transparent);
  animation: twinkle 4s ease-in-out infinite alternate;
}

@keyframes twinkle {
  from { opacity: 0.65; }
  to { opacity: 1; }
}

#game-root {
  position: relative;
  z-index: 1;
  min-height: 100%;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: max(0.75rem, env(safe-area-inset-top)) 1rem max(0.75rem, env(safe-area-inset-bottom));
}

body:has(.numpad.is-visible) #game-root {
  padding-bottom: calc(14.5rem + env(safe-area-inset-bottom));
}

.top-bar {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  gap: 0.65rem;
  min-height: 3.25rem;
  flex: 0 0 auto;
}

.stats {
  display: flex;
  gap: 0.65rem;
  flex-wrap: wrap;
}

.stat {
  background: rgba(13, 6, 24, 0.65);
  border: 2px solid rgba(110, 232, 154, 0.45);
  border-radius: 999px;
  padding: 0.45rem 1rem;
  font-weight: 700;
  font-size: clamp(1.35rem, 4.2vw, 2rem);
  color: var(--green-soft);
  line-height: 1.15;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}

.stat #score-value,
.stat #level-value {
  color: var(--white);
}

.play-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(0.5rem, 1.5vh, 1rem);
  position: relative;
  min-height: 0;
  width: 100%;
  margin: 0;
  padding: 0 0.5rem;
  overflow: visible;
}

.face {
  flex: 0 0 auto;
  font-size: clamp(3.5rem, 10vh, 5.5rem);
  line-height: 1;
  filter: drop-shadow(0 6px 16px rgba(0, 0, 0, 0.35));
}

.face.face-pop {
  animation: face-pop 0.35s ease;
}

@keyframes face-pop {
  0% { transform: scale(0.7); }
  55% { transform: scale(1.12); }
  100% { transform: scale(1); }
}

.equation {
  flex: 0 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: auto;
  max-width: 100%;
  min-height: 0;
  cursor: pointer;
  border-radius: 1.25rem;
  padding: 0.5rem 0.65rem 0.75rem;
  outline: none;
  transition: background 0.2s ease;
  overflow: visible;
}

.equation:focus-visible {
  outline: 3px solid var(--green);
  outline-offset: 4px;
}

.equation.is-compact {
  animation: compact-pulse 2.8s ease-in-out infinite;
}

@keyframes compact-pulse {
  0%, 100% { background: transparent; }
  50% { background: rgba(62, 207, 110, 0.06); }
}

.equation.problem-in {
  animation: problem-in 0.4s ease;
}

@keyframes problem-in {
  from {
    opacity: 0;
    transform: scale(0.94);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.equation-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: center;
  gap: 0.35rem 0.65rem;
  width: max-content;
  max-width: 100%;
}

.operand-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  flex: 0 0 auto;
  width: max-content;
  max-width: min(46vw, 28rem);
  overflow: visible;
}

.operand-num {
  font-size: clamp(4.5rem, 16vw, 9rem);
  font-weight: 700;
  line-height: 1;
  letter-spacing: -0.02em;
  text-shadow: 0 4px 0 rgba(0, 0, 0, 0.2);
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

.unit-marker {
  font-size: 0.42em;
  margin-left: 0.08em;
  line-height: 1.1;
}

.unit-marker.unit-removed,
.em-removed {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.unit-marker.unit-removed .em-glyph,
.em-removed .em-glyph {
  display: inline-block;
  line-height: 1;
}

/* Red prohibition-style circle + cross over subtracted objects */
.unit-marker.unit-removed::after,
.em-removed::after {
  content: "";
  position: absolute;
  inset: -18%;
  border: 0.12em solid #e63946;
  border-radius: 50%;
  box-sizing: border-box;
  pointer-events: none;
  z-index: 1;
  box-shadow: 0 0 0 0.02em rgba(230, 57, 70, 0.35);
}

.unit-marker.unit-removed::before,
.em-removed::before {
  content: "";
  position: absolute;
  left: 8%;
  right: 8%;
  top: 50%;
  height: 0.12em;
  background: #e63946;
  transform: rotate(-40deg);
  border-radius: 1px;
  pointer-events: none;
  z-index: 2;
}

.op-col.minus {
  color: #ff8fab;
}

.count-pack-removed {
  opacity: 0.95;
}


.op-col {
  font-size: clamp(3.5rem, 12vw, 7rem);
  font-weight: 600;
  line-height: 1;
  padding-top: 0.1em;
  color: var(--green-soft);
  align-self: flex-start;
  flex: 0 0 auto;
}

.answer-col {
  align-self: flex-start;
  flex: 0 0 auto;
}

.answer-input {
  width: clamp(5.5rem, 18vw, 10rem);
  min-height: clamp(4.5rem, 16vw, 9rem);
  font-family: var(--font);
  font-size: clamp(4rem, 14vw, 8rem);
  font-weight: 700;
  text-align: center;
  color: var(--white);
  background: rgba(13, 6, 24, 0.45);
  border: 3px solid rgba(110, 232, 154, 0.55);
  border-radius: 0.85rem;
  outline: none;
  caret-color: var(--green);
  padding: 0 0.25rem;
  line-height: 1.1;
}

.answer-input:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 4px var(--green-glow);
}

.answer-input.is-wrong {
  animation: shake 0.45s ease;
  border-color: #ff6b9d;
  box-shadow: 0 0 0 4px rgba(255, 107, 157, 0.3);
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-6px); }
  80% { transform: translateX(6px); }
}

.count-display {
  width: max-content;
  max-width: min(46vw, 28rem);
  overflow: visible;
  padding: 0.4rem 0.2rem 0.55rem;
}

.count-pack {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: center;
  gap: 0.45rem;
  width: max-content;
  max-width: 100%;
  overflow: visible;
}

.hundred-pack {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  gap: 0;
  padding: 0.4rem;
  background: rgba(13, 6, 24, 0.4);
  border: 2px solid rgba(123, 63, 168, 0.55);
  border-radius: 0.55rem;
  line-height: 1;
  font-size: clamp(0.85rem, 2.2vw, 1.35rem);
  width: clamp(9rem, 28vw, 16rem);
  overflow: visible;
}

.ten-bundle {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 0.12rem 0.18rem;
  padding: 0.45rem 0.55rem;
  background: rgba(13, 6, 24, 0.35);
  border: 2px solid rgba(62, 207, 110, 0.4);
  border-radius: 0.5rem;
  font-size: clamp(1.5rem, 4.5vw, 2.35rem);
  line-height: 1;
  overflow: visible;
}

.ten-bundle .em-gap {
  margin-left: 0.2rem;
}

.ones-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.2rem;
  font-size: clamp(1.85rem, 5.5vw, 2.85rem);
  line-height: 1;
  overflow: visible;
  animation: bob 2.4s ease-in-out infinite;
}

.ten-bundle,
.hundred-pack {
  animation: bob 2.8s ease-in-out infinite;
}

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

@media (max-width: 640px) {
  .operand-col {
    max-width: min(48vw, 16rem);
  }

  .count-display {
    max-width: min(48vw, 16rem);
  }

  .hundred-pack {
    width: clamp(7.5rem, 40vw, 12rem);
    font-size: clamp(0.7rem, 3.2vw, 1rem);
  }
}

.equation.is-compact .count-display {
  display: none;
}

.compact-hint {
  text-align: center;
  font-size: 0.85rem;
  color: var(--green-soft);
  opacity: 0.85;
  margin-top: 0.35rem;
  font-weight: 500;
}

.celebrate-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  opacity: 0;
  transition: opacity 0.2s ease;
  z-index: 5;
}

.celebrate-layer.is-active {
  opacity: 1;
}

.speech-bubble {
  position: absolute;
  left: var(--bx);
  top: var(--by);
  transform: translate(-50%, 0);
  background: #fff;
  color: var(--purple-deep);
  border: 3px solid var(--purple-deep);
  border-radius: 1.1rem;
  padding: 0.45rem 0.75rem;
  font-weight: 600;
  font-size: clamp(0.85rem, 2.8vw, 1.1rem);
  white-space: nowrap;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
  animation: bubble-in 0.45s ease both;
  animation-delay: var(--delay);
}

.speech-bubble::after {
  content: "";
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  border: 6px solid transparent;
  border-top-color: var(--purple-deep);
}

@keyframes bubble-in {
  from {
    opacity: 0;
    transform: translate(-50%, 12px) scale(0.85);
  }
  to {
    opacity: 1;
    transform: translate(-50%, 0) scale(1);
  }
}

.party-emoji {
  position: absolute;
  left: var(--px);
  top: var(--py);
  font-size: clamp(1.4rem, 5vw, 2.2rem);
  transform: rotate(var(--rot));
  animation: party-bounce 0.7s ease both;
  animation-delay: var(--delay);
}

@keyframes party-bounce {
  0% {
    opacity: 0;
    transform: translateY(20px) scale(0.5) rotate(var(--rot));
  }
  60% {
    opacity: 1;
    transform: translateY(-8px) scale(1.15) rotate(var(--rot));
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1) rotate(var(--rot));
  }
}

.numpad {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.45rem;
  padding: 0.65rem max(0.75rem, env(safe-area-inset-right))
    max(0.75rem, env(safe-area-inset-bottom)) max(0.75rem, env(safe-area-inset-left));
  background: linear-gradient(180deg, transparent, rgba(13, 6, 24, 0.92) 18%);
  max-width: 420px;
  margin: 0 auto;
}

.numpad[hidden] {
  display: none !important;
}

.numpad.is-visible {
  --numpad-h: 14.5rem;
}

.numpad button {
  font-family: var(--font);
  font-size: 1.55rem;
  font-weight: 600;
  min-height: 3rem;
  border: none;
  border-radius: 0.85rem;
  background: rgba(74, 29, 122, 0.85);
  color: var(--white);
  cursor: pointer;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.35);
  transition: transform 0.1s ease, background 0.15s ease;
}

.numpad button:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.35);
}

.numpad .key-back {
  background: rgba(42, 16, 72, 0.95);
}

.numpad .key-enter {
  background: var(--green);
  color: var(--black);
}

.back-link {
  position: fixed;
  left: max(0.85rem, env(safe-area-inset-left));
  bottom: max(0.85rem, env(safe-area-inset-bottom));
  z-index: 12;
  color: rgba(255, 255, 255, 0.75);
  text-decoration: none;
  font-weight: 500;
  font-size: 0.95rem;
  padding: 0.4rem 0.65rem;
  border-radius: 0.5rem;
  background: rgba(13, 6, 24, 0.45);
  transition: color 0.15s ease, background 0.15s ease;
}

.back-link:hover,
.back-link:focus-visible {
  color: var(--white);
  background: rgba(13, 6, 24, 0.7);
  outline: 2px solid var(--green);
}

.back-link.is-disabled {
  pointer-events: none;
  opacity: 0;
}

.numpad.is-visible ~ .back-link,
body:has(.numpad.is-visible) .back-link {
  bottom: calc(var(--numpad-h) + 0.35rem);
}

/* When numpad visible, lift back link via game-root padding already; position above numpad */
body:has(.numpad.is-visible) .back-link {
  bottom: calc(14.5rem + env(safe-area-inset-bottom));
}

.fireworks-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  background: rgba(6, 2, 14, 0.72);
  padding-bottom: max(2rem, env(safe-area-inset-bottom));
  pointer-events: auto;
}

.fireworks-overlay[hidden] {
  display: none !important;
}

.fireworks-overlay canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.fireworks-face {
  position: relative;
  z-index: 2;
  font-size: clamp(4.5rem, 16vw, 7rem);
  margin-bottom: auto;
  margin-top: 10vh;
  filter: drop-shadow(0 8px 24px rgba(0, 0, 0, 0.4));
  animation: face-pop 0.5s ease;
  pointer-events: none;
}

.continue-btn {
  position: relative;
  z-index: 10;
  font-family: var(--font);
  font-size: clamp(1.25rem, 4.5vw, 1.65rem);
  font-weight: 700;
  min-height: 3.5rem;
  min-width: min(92vw, 320px);
  padding: 1rem 1.75rem;
  border: none;
  border-radius: 999px;
  background: var(--green);
  color: var(--black);
  cursor: pointer;
  pointer-events: auto;
  touch-action: manipulation;
  box-shadow:
    0 0 0 5px rgba(62, 207, 110, 0.3),
    0 12px 32px rgba(0, 0, 0, 0.4);
  margin-bottom: 1.5rem;
}

.continue-btn.is-pulse {
  animation: continue-pulse 1.4s ease-in-out infinite;
}

@keyframes continue-pulse {
  0%, 100% { transform: scale(1); box-shadow: 0 0 0 4px rgba(62, 207, 110, 0.25), 0 10px 28px rgba(0, 0, 0, 0.35); }
  50% { transform: scale(1.05); box-shadow: 0 0 0 8px rgba(62, 207, 110, 0.4), 0 12px 32px rgba(0, 0, 0, 0.4); }
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (min-width: 901px) and (pointer: fine) {
  .numpad {
    /* kept for optional force-show; normally hidden via JS */
  }
}

@media (prefers-reduced-motion: reduce) {
  .stars,
  .face.face-pop,
  .equation.problem-in,
  .equation.is-compact,
  .ones-row,
  .ten-bundle,
  .hundred-pack,
  .speech-bubble,
  .party-emoji,
  .continue-btn.is-pulse,
  .fireworks-face {
    animation: none !important;
  }
}
