:root {
  --color-primary:   #ED00A6;
  --color-accent-1:  #429DFF;
  --color-accent-2:  #F3A200;
  --color-accent-3:  #ACFF00;
  --color-accent-4:  #9900FF;
  --color-error:     #E74C3C;
  --color-dark:      #222B34;
  --color-light:     #F5FAFD;
  --color-white:     #FFFFFF;

  --font-title: 'Muli', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-body:  'Open Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  font-family: var(--font-body);
  background: linear-gradient(135deg, var(--color-accent-1) 0%, #E6F3FF 100%);
  background-attachment: fixed;
  color: var(--color-dark);
}

h1, h2, h3 { font-family: var(--font-title); margin: 0 0 8px; }

button { font-family: var(--font-body); cursor: pointer; }

/* ---------- Layout de pantallas ---------- */
.screen {
  display: none;
  min-height: 100vh;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.screen.active { display: flex; }

.card {
  background: var(--color-white);
  border-radius: 18px;
  box-shadow: 0 12px 30px rgba(0,0,0,0.25);
  padding: 28px 26px;
  max-width: 480px;
  width: 100%;
  text-align: center;
}
.card--menu { max-width: 620px; }
.card--modal { max-width: 560px; max-height: 85vh; overflow-y: auto; text-align: left; }

.title { font-size: 1.8rem; color: var(--color-dark); }
.title--sm { font-size: 1.4rem; text-align: center; }
.subtitle { margin: 0 0 20px; color: #5a6472; }
.section-label { font-size: 1rem; color: var(--color-accent-1); margin-bottom: 10px; }

/* ---------- Botones ---------- */
.btn {
  display: block;
  width: 100%;
  padding: 13px 18px;
  margin: 8px 0;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 700;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.btn:active { transform: scale(0.97); }
.btn-primary {
  background: linear-gradient(135deg, var(--color-accent-1), var(--color-accent-4));
  color: var(--color-white);
  box-shadow: 0 6px 14px rgba(66, 157, 255, 0.35);
}
.btn-ghost {
  background: var(--color-light);
  color: var(--color-dark);
}
.stack-buttons { display: flex; flex-direction: column; }

.icon-btn {
  background: rgba(255,255,255,0.15);
  border: none;
  color: var(--color-white);
  font-size: 1.3rem;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  line-height: 1;
  flex-shrink: 0;
}

/* ---------- Selección de personaje ---------- */
.char-select { margin-bottom: 18px; }
.char-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 8px;
}
.char-card {
  border: 3px solid transparent;
  border-radius: 14px;
  background: var(--color-light);
  padding: 8px;
  text-align: center;
  transition: border-color 0.15s ease, transform 0.15s ease;
}
.char-card.selected {
  border-color: var(--color-accent-1);
  transform: translateY(-3px);
  background: #eaf4ff;
}
.char-card .mascot-slot { width: 100%; height: auto; aspect-ratio: 1/1; margin: 0 auto; }

/* ---------- Selección de nivel ---------- */
.level-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 10px;
}
.level-grid--math { grid-template-columns: repeat(3, 1fr); }
.level-btn {
  border: 3px solid transparent;
  border-radius: 14px;
  background: var(--color-light);
  padding: 14px 8px;
  text-align: center;
  font-size: 0.88rem;
  font-weight: 700;
  color: var(--color-dark);
  transition: border-color 0.15s ease, transform 0.15s ease;
}
.level-btn.selected {
  border-color: var(--color-primary);
  transform: translateY(-2px);
  background: #fff0fa;
}

.table-picker {
  background: var(--color-light);
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 16px;
  text-align: left;
}
.table-picker-label { font-size: 0.85rem; font-weight: 700; color: var(--color-dark); }
.table-picker-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
  margin-top: 8px;
}
.table-btn {
  border: 2px solid transparent;
  border-radius: 8px;
  background: var(--color-white);
  padding: 8px 4px;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--color-dark);
}
.table-btn.selected {
  border-color: var(--color-accent-2);
  background: #fff6e6;
}

/* ---------- Ajustes ---------- */
.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 16px 0;
  text-align: left;
}
.setting-row label { font-weight: 700; }
.setting-row input[type="range"] { flex: 1; accent-color: var(--color-accent-1); }
.switch { width: 20px; height: 20px; accent-color: var(--color-accent-1); }

/* ---------- Pantalla de juego ---------- */
#screen-game { align-items: stretch; padding: 0; }
#screen-game.active { display: flex; flex-direction: column; }

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 18px;
  margin-bottom: 10px;
  color: var(--color-white);
  gap: 10px;
}
.game-info {
  display: flex;
  gap: 12px;
  align-items: center;
  background: rgba(255,255,255,0.15);
  padding: 8px 16px;
  border-radius: 20px;
  font-weight: 700;
  font-size: 0.85rem;
  flex-wrap: wrap;
  justify-content: center;
}
.hearts { letter-spacing: 2px; font-size: 1rem; }
.heart-lost { filter: grayscale(1); opacity: 0.45; }

.game-body {
  flex: 1;
  display: flex;
  gap: 18px;
  align-items: stretch;
  justify-content: center;
  padding: 0 18px 24px;
}

/* ---------- Pista de carrera ---------- */
.run-track {
  position: relative;
  flex: 1;
  min-width: 0;
  min-height: 380px;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 10px 26px rgba(0,0,0,0.25);
  --sky-a: #8FCBFF;
  --sky-b: #E6F5FF;
  --ground: #8ED17A;
}
.run-sky {
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, var(--sky-a) 0%, var(--sky-b) 78%);
  transition: background 0.6s ease;
}
.run-ground {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 22%;
  background: var(--ground);
  transition: background 0.6s ease;
  box-shadow: 0 -3px 0 rgba(0,0,0,0.08) inset;
  z-index: 3;
}
.sky-icon {
  position: absolute;
  top: 16px;
  right: 20px;
  font-size: 2.6rem;
  z-index: 1;
  animation: sky-icon-pulse 4s ease-in-out infinite;
  filter: drop-shadow(0 2px 6px rgba(0,0,0,0.15));
}
@keyframes sky-icon-pulse {
  0%, 100% { transform: scale(1) rotate(0deg); }
  50% { transform: scale(1.08) rotate(6deg); }
}

.sky-decor {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
}
.sky-decor-item {
  position: absolute;
  font-size: 1.6rem;
  opacity: 0.55;
  animation: decor-drift linear infinite;
  will-change: transform;
}

.run-decor {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
}
.decor-item {
  position: absolute;
  font-size: 2rem;
  opacity: 0.85;
  animation: decor-drift linear infinite;
  will-change: transform;
}
@keyframes decor-drift {
  from { transform: translateX(0); }
  to { transform: translateX(-140%); }
}

.timer-bar-wrap {
  position: absolute;
  top: 10px;
  left: 10px;
  right: 10px;
  height: 8px;
  background: rgba(255,255,255,0.5);
  border-radius: 6px;
  overflow: hidden;
  z-index: 5;
}
.timer-bar {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, var(--color-accent-3), var(--color-accent-2));
  transition: width 0.1s linear;
}
.timer-bar.timer-low { background: linear-gradient(90deg, var(--color-error), #ff8a7a); }
.run-track.no-timer .timer-bar-wrap { visibility: hidden; }

.operation-bubble {
  position: absolute;
  top: 28px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--color-white);
  border-radius: 24px;
  padding: 15px 30px;
  font-family: var(--font-title);
  font-weight: 800;
  font-size: 2.25rem;
  color: var(--color-dark);
  box-shadow: 0 6px 14px rgba(0,0,0,0.2);
  z-index: 6;
  white-space: nowrap;
}

.obstacle {
  position: absolute;
  bottom: 20%;
  left: 100%;
  font-size: 3.9rem;
  transform: translate(-50%, 10%);
  z-index: 4;
  filter: drop-shadow(2px 4px 3px rgba(0,0,0,0.25));
}
.obstacle.effect-pop { animation: obstacle-pop 0.4s ease-in forwards; }
.obstacle.effect-melt { animation: obstacle-melt 0.45s ease-in forwards; }
.obstacle.effect-shatter { animation: obstacle-shatter 0.38s ease-in forwards; }
.obstacle.effect-roll { animation: obstacle-roll 0.45s ease-in forwards; }

@keyframes obstacle-pop {
  0% { transform: translate(-50%, 10%) scale(1) rotate(0deg); opacity: 1; }
  40% { transform: translate(-50%, 0%) scale(1.25) rotate(-8deg); opacity: 1; }
  100% { transform: translate(-50%, -12%) scale(0.2) rotate(20deg); opacity: 0; }
}
@keyframes obstacle-melt {
  0% { transform: translate(-50%, 10%) scale(1, 1); opacity: 1; filter: drop-shadow(2px 4px 3px rgba(0,0,0,0.25)) blur(0px); }
  60% { transform: translate(-50%, 45%) scale(1.15, 0.55); opacity: 0.85; filter: drop-shadow(2px 4px 3px rgba(0,0,0,0.15)) blur(0.5px); }
  100% { transform: translate(-50%, 75%) scale(1.3, 0.06); opacity: 0; filter: drop-shadow(0 0 0 rgba(0,0,0,0)) blur(1.5px); }
}
@keyframes obstacle-shatter {
  0% { transform: translate(-50%, 10%) scale(1) rotate(0deg); opacity: 1; }
  30% { transform: translate(-50%, 4%) scale(1.2) rotate(12deg); opacity: 1; }
  100% { transform: translate(-50%, 4%) scale(0.4) rotate(-30deg); opacity: 0; }
}
@keyframes obstacle-roll {
  0% { transform: translate(-50%, 10%) rotate(0deg); opacity: 1; }
  100% { transform: translate(-160%, 10%) rotate(-560deg); opacity: 0; }
}

.fx-particles {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 4;
}
.fx-particle {
  position: absolute;
  bottom: 20%;
  font-size: 1.1rem;
}
.fx-particle.fx-fly { animation: fx-fly 0.5s ease-out forwards; }
.fx-particle.fx-drip { animation: fx-drip 0.55s ease-in forwards; }
@keyframes fx-fly {
  0% { transform: translate(-50%, 0) scale(1); opacity: 1; }
  100% { transform: translate(var(--fx-dx, 40px), var(--fx-dy, -30px)) scale(0.4); opacity: 0; }
}
@keyframes fx-drip {
  0% { transform: translate(-50%, 0) scale(1); opacity: 0.9; }
  100% { transform: translate(-50%, 60px) scale(0.6); opacity: 0; }
}

.runner-wrap {
  position: absolute;
  bottom: 20%;
  left: 14%;
  width: 90px;
  height: 90px;
  transform: translate(-50%, 0);
  z-index: 5;
  animation: run-bounce 0.5s ease-in-out infinite;
}
.runner-wrap.jumping { animation: jump-arc 0.55s ease-out; }
.runner-wrap.hit { animation: hit-shake 0.5s ease-in-out; }
.runner-wrap.hit-gentle { animation: hit-shake-gentle 0.5s ease-in-out; }
.runner-wrap.paused-anim { animation-play-state: paused; }
/* Si el personaje tiene sprite de carrera, el movimiento lo dan los fotogramas, no el
   bote de todo el cuerpo; pero salto y tropiezo (más específicos) deben seguir ganando. */
.runner-wrap.has-sprite { animation: none; }
.runner-wrap.has-sprite.jumping { animation: jump-arc 0.55s ease-out; }
.runner-wrap.has-sprite.hit { animation: hit-shake 0.5s ease-in-out; }
.runner-wrap.has-sprite.hit-gentle { animation: hit-shake-gentle 0.5s ease-in-out; }

.mascot-slot { display: block; width: 100%; height: 100%; }
/* La capibara trae fondo negro de fábrica (sin transparencia); "spotlight" añade un fondo oscuro
   redondeado en vez de mostrar la caja negra suelta sobre el resto de la interfaz clara. */
.mascot-spotlight {
  background: radial-gradient(circle at 55% 45%, #3a2c1a 0%, #14100a 72%);
  border-radius: 50%;
  overflow: hidden;
}

/* ---------- Sprite de carrera (fotogramas reales, no solo la imagen entera botando) ----------
   El fotograma lo fija JavaScript directamente (background-position-x en línea), sin ninguna
   animación/transición CSS de por medio: así no hay riesgo de que el navegador suavice el
   cambio entre fotogramas ("paneo"), siempre es un salto seco de un fotograma al siguiente. */
.mascot-sprite {
  width: 100%;
  height: 100%;
  background-repeat: no-repeat;
  background-position: 0 0;
  display: block;
  pointer-events: none;
}

@keyframes run-bounce {
  0%, 100% { transform: translate(-50%, 0); }
  50% { transform: translate(-50%, -8px); }
}
@keyframes jump-arc {
  0% { transform: translate(-50%, 0) rotate(0deg); }
  40% { transform: translate(-50%, -95px) rotate(-10deg); }
  70% { transform: translate(-50%, -95px) rotate(6deg); }
  100% { transform: translate(-50%, 0) rotate(0deg); }
}
@keyframes hit-shake {
  0%, 100% { transform: translate(-50%, 0) rotate(0deg); }
  20% { transform: translate(-58%, 4px) rotate(-8deg); }
  40% { transform: translate(-42%, -4px) rotate(8deg); }
  60% { transform: translate(-56%, 4px) rotate(-6deg); }
  80% { transform: translate(-44%, 0) rotate(4deg); }
}
@keyframes hit-shake-gentle {
  0%, 100% { transform: translate(-50%, 0) rotate(0deg); }
  30% { transform: translate(-52%, 1px) rotate(-2deg); }
  60% { transform: translate(-48%, 0) rotate(2deg); }
}

.hit-flash {
  position: absolute;
  inset: 0;
  background: var(--color-error);
  opacity: 0;
  pointer-events: none;
  z-index: 8;
}
.hit-flash.active { animation: hit-flash-anim 0.4s ease-out; }
@keyframes hit-flash-anim {
  0% { opacity: 0.55; }
  100% { opacity: 0; }
}

/* Modo sensorial suave: destello contenido al borde de la pantalla, no cubre todo el juego. */
.hit-flash.gentle {
  background: transparent;
  box-shadow: inset 0 0 0 0 var(--color-error);
}
.hit-flash.gentle.active { animation: hit-flash-gentle-anim 0.4s ease-out; }
@keyframes hit-flash-gentle-anim {
  0% { box-shadow: inset 0 0 48px 12px var(--color-error); opacity: 0.5; }
  100% { box-shadow: inset 0 0 48px 12px var(--color-error); opacity: 0; }
}

.star-rain {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 9;
}
.star-rain span {
  position: absolute;
  top: -10%;
  font-size: 1.6rem;
  animation: star-fall linear forwards;
}
@keyframes star-fall {
  to { transform: translateY(120vh) rotate(320deg); opacity: 0.2; }
}

/* ---------- Panel de respuestas ---------- */
.answer-panel {
  background: rgba(255,255,255,0.95);
  border-radius: 16px;
  padding: 16px;
  width: 230px;
  flex-shrink: 0;
  box-shadow: 0 8px 20px rgba(0,0,0,0.2);
  display: flex;
  flex-direction: column;
}
.typed-display {
  background: var(--color-light);
  border-radius: 10px;
  padding: 8px 10px;
  text-align: center;
  font-weight: 800;
  font-size: 1.1rem;
  color: var(--color-accent-1);
  margin-bottom: 10px;
  min-height: 1.4em;
}
.answer-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}
.answer-btn {
  position: relative;
  border: 2px solid transparent;
  border-radius: 10px;
  background: var(--color-light);
  padding: 14px 4px;
  font-family: var(--font-title);
  font-weight: 800;
  font-size: 1.15rem;
  color: var(--color-dark);
  transition: transform 0.12s ease, background 0.15s ease;
}
.answer-btn:active { transform: scale(0.94); }
.answer-btn.correct { background: var(--color-accent-3); border-color: #4F9A00; }
.answer-btn.wrong { background: var(--color-error); color: var(--color-white); border-color: #8E241A; }
.answer-btn:disabled { opacity: 0.6; }

.answer-badge {
  position: absolute;
  top: -7px;
  right: -7px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.72rem;
  font-weight: 900;
  color: var(--color-white);
  line-height: 1;
}
.answer-badge-ok { background: #4F9A00; }
.answer-badge-no { background: #8E241A; border: 2px solid var(--color-white); }

/* ---------- Overlays (instrucciones / pausa / fin de partida) ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(34, 43, 52, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 50;
}
.overlay.hidden { display: none; }

.instructions-list { padding-left: 20px; margin: 0 0 18px; }
.instructions-list li { margin-bottom: 10px; line-height: 1.4; }

.tag {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 8px;
  font-weight: 700;
  color: var(--color-white);
}
.tag-magenta { background: var(--color-primary); }
.tag-green { background: var(--color-accent-3); color: var(--color-dark); }
.tag-red { background: var(--color-error); }

kbd {
  background: var(--color-dark);
  color: var(--color-white);
  border-radius: 4px;
  padding: 1px 6px;
  font-size: 0.85em;
}

.pause-progress { color: #5a6472; margin-bottom: 16px; }

.victory-stats { text-align: left; margin: 10px 0 18px; }
.victory-stats p { display: flex; justify-content: space-between; margin: 6px 0; }
.record-badge {
  text-align: center;
  background: var(--color-accent-2);
  color: var(--color-dark);
  font-weight: 800;
  padding: 8px;
  border-radius: 10px;
  margin-top: 10px;
}
.practice-note {
  text-align: center;
  background: var(--color-light);
  color: #55606E;
  font-weight: 700;
  font-size: 0.85rem;
  padding: 8px;
  border-radius: 10px;
  margin-top: 10px;
}

.hidden { display: none !important; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .game-body { flex-direction: column; align-items: stretch; }
  .answer-panel { width: 100%; }
  .answer-grid { grid-template-columns: repeat(4, 1fr); }
  .run-track { min-height: 300px; }
}
@media (max-width: 768px) {
  .card { padding: 22px 18px; }
  .char-grid { grid-template-columns: repeat(4, 1fr); }
}
@media (max-width: 480px) {
  .level-grid--math { grid-template-columns: repeat(2, 1fr); }
  .table-picker-grid { grid-template-columns: repeat(4, 1fr); }
  .game-info { font-size: 0.72rem; gap: 8px; padding: 6px 10px; }
  .operation-bubble { font-size: 1.725rem; padding: 12px 21px; }
  .answer-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- Movimiento reducido (respeta la preferencia del sistema operativo) ----------
   No toca el salto ni el tropiezo (son la señal principal de acierto/fallo); solo apaga las
   animaciones ambientales continuas y sustituye el destello y la lluvia de estrellas por
   variantes sin desplazamiento por la pantalla. */
@media (prefers-reduced-motion: reduce) {
  .sky-icon { animation: none; }
  .sky-decor-item,
  .decor-item { animation: none; }
  .runner-wrap { animation: none; }

  .star-rain span {
    top: 45%;
    animation: star-twinkle-reduced 1.4s ease-in-out forwards;
  }

  .hit-flash.active {
    animation: hit-flash-anim-reduced 0.4s ease-out;
  }
}
@keyframes star-twinkle-reduced {
  0% { opacity: 0; transform: scale(0.6); }
  30% { opacity: 1; transform: scale(1.1); }
  70% { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(0.9); }
}
@keyframes hit-flash-anim-reduced {
  0% { opacity: 0.3; }
  100% { opacity: 0; }
}
