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

  --maze-bg:      #14181F;
  --maze-wall:    #429DFF;
  --maze-wall-2:  #2C6FCC;
  --accent-highlight:    #F3A200;
  --numeric-color: #429DFF;
  --alpha-color:   #ED00A6;

  --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, #F3A200 0%, #FFF3DA 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: 560px; }
.card--modal { max-width: 560px; max-height: 85vh; overflow-y: auto; text-align: left; }
.card--cartel { max-width: 480px; text-align: center; border: 4px solid var(--accent-highlight); }

.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-4); 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-primary), var(--color-accent-2));
  color: var(--color-white);
  box-shadow: 0 6px 14px rgba(243, 162, 0, 0.35);
}
.btn-ghost {
  background: var(--color-light);
  color: var(--color-dark);
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.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;
}

/* ---------- Selección de nivel (menú) ---------- */
.level-select-wrap {
  max-height: 40vh;
  overflow-y: auto;
  border-radius: 14px;
  background: var(--color-light);
  padding: 12px;
  margin-bottom: 18px;
}
.phase-heading {
  font-size: 0.82rem;
  font-weight: 800;
  color: var(--color-dark);
  text-align: left;
  margin: 12px 4px 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.level-select-wrap > *:first-child { margin-top: 0; }
.phase-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.phase-dot--numeric { background: var(--numeric-color); }
.phase-dot--alpha { background: var(--alpha-color); }
.phase-dot--mixed { background: linear-gradient(135deg, var(--numeric-color) 50%, var(--alpha-color) 50%); }

.phase-btn {
  display: block;
  width: 100%;
  border: none;
  border-radius: 12px;
  background: var(--color-white);
  color: var(--color-dark);
  font-weight: 800;
  font-size: 1rem;
  text-align: left;
  padding: 16px 18px;
  margin: 0 0 10px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.14);
}
.phase-btn.done { background: var(--color-accent-3); color: var(--color-dark); }
.phase-btn-sub { display: block; font-weight: 600; font-size: 0.78rem; opacity: 0.75; margin-top: 3px; }

.level-checkpoint-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(52px, 1fr));
  gap: 6px;
  margin-bottom: 10px;
}
.lvl-btn {
  border: none;
  border-radius: 9px;
  background: var(--color-white);
  color: var(--color-dark);
  font-weight: 800;
  font-size: 0.82rem;
  padding: 10px 0;
  box-shadow: 0 2px 4px rgba(0,0,0,0.12);
}
.lvl-btn.done { background: var(--color-accent-3); color: var(--color-dark); }

/* ---------- 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-primary); }
.setting-row select {
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 0.95rem;
  padding: 6px 10px;
  border-radius: 8px;
  border: 2px solid var(--color-light);
  background: var(--color-white);
  color: var(--color-dark);
}
.switch { width: 20px; height: 20px; accent-color: var(--color-primary); }
.settings-divider { border: none; border-top: 2px solid var(--color-light); margin: 18px 0 4px; }
.settings-group-label { font-size: 0.8rem; font-weight: 700; color: var(--color-accent-4); text-align: left; margin: 0; }

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

.game-header {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 14px;
  color: var(--color-white);
  position: sticky;
  top: 0;
  z-index: 15;
  background: rgba(20, 24, 31, 0.55);
  backdrop-filter: blur(6px);
}
.game-header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.game-info {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  align-self: center;
  background: rgba(255,255,255,0.15);
  padding: 6px 14px;
  border-radius: 20px;
  font-weight: 700;
  font-size: 0.82rem;
}
.mode-badge {
  padding: 4px 14px;
  border-radius: 14px;
  font-weight: 800;
  font-size: 0.92rem;
}
.mode-badge--numeric { background: var(--numeric-color); }
.mode-badge--alpha { background: var(--alpha-color); }
.mode-badge--mixed { background: linear-gradient(135deg, var(--numeric-color) 50%, var(--alpha-color) 50%); }
.next-badge {
  background: var(--accent-highlight);
  color: var(--color-dark);
  padding: 2px 10px;
  border-radius: 12px;
  font-weight: 800;
}
.lives { letter-spacing: 2px; }

.game-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 10px;
  padding: 4px 12px 24px;
  position: relative;
}

.level-toast {
  /* Fija justo debajo de la cabecera (nunca se mueve ni se solapa con ella: por eso
     va por detrás en z-index y solo cambia de opacidad, no de posición). */
  position: absolute;
  top: 6px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(20,24,31,0.92);
  color: var(--color-white);
  padding: 8px 18px;
  border-radius: 20px;
  font-weight: 800;
  font-size: 0.88rem;
  z-index: 5;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease;
  white-space: nowrap;
}
.level-toast.show { opacity: 1; }

.maze-wrap {
  background: var(--maze-bg);
  border-radius: 16px;
  padding: 10px;
  box-shadow: 0 10px 26px rgba(0,0,0,0.35);
  max-width: 100%;
  touch-action: none;
}
#maze-canvas { display: block; max-width: 100%; height: auto; }

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 52px);
  grid-template-rows: repeat(3, 52px);
  gap: 4px;
  user-select: none;
  -webkit-user-select: none;
}
.dpad-btn {
  border: none;
  border-radius: 10px;
  background: rgba(255,255,255,0.85);
  color: var(--color-dark);
  font-size: 1.3rem;
  font-weight: 800;
  touch-action: none;
}
.dpad-btn:active { background: var(--accent-highlight); }
.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 3; }

/* ---------- Cartel de cambio de dinámica ---------- */
.cartel-icon { font-size: 2.6rem; margin-bottom: 6px; }
.cartel-text { font-size: 1.02rem; line-height: 1.5; margin: 10px 0 18px; }
.cartel-example { margin-bottom: 10px; }
.cartel-example-group { margin-bottom: 12px; }
.cartel-example-label { font-size: 0.8rem; font-weight: 700; color: #5a6472; margin-bottom: 6px; }
.cartel-example-row {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
}
.cartel-chip {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  color: var(--color-white);
}

/* ---------- Overlays ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(34, 43, 52, 0.78);
  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; }
.instructions-subtitle { text-align: left; margin-top: 6px; }

.tag {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 8px;
  font-weight: 700;
  color: var(--color-white);
}
.tag-numeric { background: var(--numeric-color); }
.tag-alpha { background: var(--alpha-color); }
.tag-pac { background: var(--accent-highlight); color: var(--color-dark); }

.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-white);
  font-weight: 800;
  padding: 8px;
  border-radius: 10px;
  margin-top: 10px;
}

/* Confeti sencillo */
.confetti {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}
.confetti span {
  position: absolute;
  top: -10px;
  width: 8px;
  height: 14px;
  opacity: 0.9;
  animation: confetti-fall linear forwards;
}
@keyframes confetti-fall {
  to { transform: translateY(110vh) rotate(600deg); opacity: 0.3; }
}

.hidden { display: none !important; }

/* ---------- Responsive ---------- */
@media (max-width: 768px) {
  .card { padding: 22px 18px; }
}
@media (max-width: 480px) {
  .level-checkpoint-grid { grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); }
  .lvl-btn { font-size: 0.74rem; padding: 7px 0; }
  .game-info { font-size: 0.72rem; padding: 5px 10px; gap: 7px; }
  .dpad { grid-template-columns: repeat(3, 44px); grid-template-rows: repeat(3, 44px); }
}
