/* Tema accesible para mayores: fuentes grandes, alto contraste, toques amplios.
   Paleta negra (petición de Felipe): fondos casi negros, acento amarillo. */
:root {
  --bg: #0a0a0c;
  --bg2: #17181c;
  --card: #232529;
  --ink: #f2f3f5;
  --muted: #9aa1ab;
  --accent: #ffcf4d;
  --accent-ink: #23180a;
  --good: #4ee08a;
  --bad: #ff6b6b;
  --radius: 18px;
}
* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 20px;
  -webkit-user-select: none; user-select: none;
  overscroll-behavior: none;
}
#app {
  min-height: 100%;
  max-width: 640px;
  margin: 0 auto;
  padding: calc(18px + env(safe-area-inset-top)) 16px calc(24px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

/* Cabecera */
.topbar { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; min-height: 52px; }
.topbar h1 { font-size: 24px; font-weight: 800; letter-spacing: .3px; }
.topbar .spacer { flex: 1; }
.iconbtn {
  border: 0; background: var(--bg2); color: var(--ink);
  min-width: 52px; height: 52px; border-radius: 14px; font-size: 22px; font-weight: 700;
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center; padding: 0 14px;
}
.iconbtn:active { transform: scale(.96); }

/* Botones grandes */
.btn {
  border: 0; border-radius: 14px; padding: 16px 20px; font-size: 20px; font-weight: 800;
  cursor: pointer; letter-spacing: .3px; width: 100%;
}
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-ghost { background: var(--bg2); color: var(--ink); }
.btn-row { display: flex; gap: 12px; margin-top: 14px; }
.btn-row .btn { width: auto; flex: 1; }

/* Menú de juegos */
.game-list { display: grid; gap: 14px; }
.game-tile {
  background: var(--card); border: 0; border-radius: var(--radius);
  padding: 20px; display: flex; align-items: center; gap: 16px; cursor: pointer; text-align: left;
  color: var(--ink);
}
.game-tile:active { transform: scale(.99); }
.game-tile .emoji { font-size: 40px; line-height: 1; }
.game-tile .meta { flex: 1; }
.game-tile .meta b { font-size: 22px; display: block; }
.game-tile .meta span { color: var(--muted); font-size: 16px; }
.game-tile .lvl { background: var(--bg); color: var(--accent); font-weight: 800; padding: 8px 12px; border-radius: 12px; font-size: 16px; white-space: nowrap; }

/* Área de juego */
#stage { flex: 1; display: flex; flex-direction: column; }
.hint { color: var(--muted); font-size: 18px; text-align: center; margin-bottom: 14px; }

/* Cartas de memoria (la proporción exacta cols/rows se fija por JS según el nº de cartas) */
.grid { display: grid; gap: 10px; margin: auto; width: 100%; }
.mcard {
  border-radius: 14px; border: 0; cursor: pointer; min-width: 0; min-height: 0;
  background: var(--bg2); font-size: 34px; display: flex; align-items: center; justify-content: center;
  transition: transform .12s, background .12s;
}
.mcard.up { background: var(--card); }
.mcard.done { background: var(--good); color: var(--accent-ink); }
.mcard .face { visibility: hidden; }
.mcard.up .face, .mcard.done .face { visibility: visible; }

/* Opciones (cálculo / atención) */
.question { font-size: 44px; font-weight: 800; text-align: center; margin: 10px 0 22px; }
.options { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.opt {
  background: var(--card); border: 0; color: var(--ink); border-radius: 14px;
  padding: 22px; font-size: 30px; font-weight: 800; cursor: pointer;
}
.opt.ok { background: var(--good); color: var(--accent-ink); }
.opt.no { background: var(--bad); }
.opt:active { transform: scale(.98); }

/* Rejilla de atención (cols x cols, siempre cuadrada: la proporción va en el contenedor) */
.attn-grid { display: grid; gap: 8px; margin: auto; width: 100%; aspect-ratio: 1; }
.attn-cell {
  border: 0; border-radius: 12px; background: var(--card); min-width: 0; min-height: 0;
  font-size: 30px; cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.attn-cell:active { transform: scale(.95); }

/* Match-3 (Fichas) */
.m3-hud { display: flex; justify-content: space-around; align-items: center; margin-bottom: 8px; }
.m3-stat { text-align: center; }
.m3-stat b { display: block; font-size: 28px; color: var(--accent); }
.m3-stat span { font-size: 14px; color: var(--muted); }
.m3-moves b {
  width: 60px; height: 60px; border-radius: 50%; margin: 0 auto 2px;
  background: #1c1e24; box-shadow: inset 0 0 0 3px #3a3e46, 0 3px 8px rgba(0,0,0,.35);
  display: flex; align-items: center; justify-content: center; font-size: 26px; color: #fff;
}
.m3-progress { height: 10px; background: var(--bg2); border-radius: 6px; overflow: hidden; margin-bottom: 14px; }
.m3-progress-fill { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), #ffe27a); transition: width .3s; }

/* Panel del tablero con casillas en damero, como el tablero real de Candy Crush.
   La proporción cuadrada va en el TABLERO (aspect-ratio en el padre), no en cada celda:
   poner aspect-ratio en hijos de una rejilla con columnas "fr" hace que algunos móviles
   calculen mal la altura de las filas y las celdas salen ovaladas ("apepinadas"). */
.m3-grid {
  display: grid; gap: 4px; margin: auto; width: 100%; max-width: 520px; aspect-ratio: 1;
  position: relative; padding: 8px; border-radius: 16px; background: #101114;
  box-shadow: inset 0 0 0 2px #33363c, 0 8px 24px rgba(0,0,0,.45);
  touch-action: none; /* el arrastre intercambia caramelos, no hace scroll de la página */
}
.m3-cell {
  border: 0; border-radius: 10px; position: relative; min-width: 0; min-height: 0;
  cursor: pointer; display: flex; align-items: center; justify-content: center; padding: 9%;
}
.m3-cell:nth-child(odd) { background: #202228; }
.m3-cell:nth-child(even) { background: #17181c; }
.m3-cell.sel { box-shadow: inset 0 0 0 3px var(--accent); }
.m3-cell.sel .m3-candy { animation: m3-selpulse .45s ease-in-out infinite alternate; }
@keyframes m3-selpulse { from { transform: scale(1); } to { transform: scale(1.14); } }

/* Puntos flotantes al estallar un grupo */
.m3-score-pop {
  position: absolute; transform: translateX(-50%); z-index: 5; pointer-events: none;
  color: #ffe27a; font-weight: 900; font-size: 22px; text-shadow: 0 2px 6px rgba(0,0,0,.7);
  animation: m3-float .8s ease-out forwards;
}
@keyframes m3-float { from { opacity: 1; margin-top: 0; } to { opacity: 0; margin-top: -44px; } }
.m3-cell.m3-ice1::after, .m3-cell.m3-ice2::after {
  position: absolute; inset: 6%; border-radius: 20%;
  display: flex; align-items: center; justify-content: center; font-size: 55%;
  pointer-events: none;
}
.m3-cell.m3-ice1::after {
  content: "❄️";
  background: linear-gradient(160deg, rgba(225,247,255,.6), rgba(150,205,255,.4));
  box-shadow: inset 0 0 10px rgba(255,255,255,.85), 0 0 6px rgba(120,190,255,.55);
}
.m3-cell.m3-ice2::after {
  content: "🧊";
  background: linear-gradient(160deg, rgba(225,247,255,.82), rgba(120,180,235,.68));
  box-shadow: inset 0 0 12px rgba(255,255,255,.95), 0 0 10px rgba(90,160,230,.75);
}

/* Los degradados de color viven en --candy-grad para poder APILAR encima el brillo
   especular y las rayas de los potenciadores sin machacarse entre sí. */
.m3-candy {
  --glint: radial-gradient(closest-side at 30% 24%, rgba(255,255,255,.95), rgba(255,255,255,0) 42%);
  width: 100%; height: 100%;
  background-image: var(--glint), var(--candy-grad, none);
  box-shadow: inset 0 -20% 30% rgba(0,0,0,.4), inset 0 15% 20% rgba(255,255,255,.35), 0 3px 6px rgba(0,0,0,.4);
  transition: transform .22s cubic-bezier(.34,1.56,.64,1), opacity .22s;
}
.m3-candy.pop { transform: scale(0) rotate(35deg); opacity: 0; }
.m3-candy.shake { animation: m3-shake .26s ease; }

/* Potenciadores: rayas (fila/columna), envuelto (explota 3x3), bomba de color */
.m3-candy.m3-line-row { background-image: var(--glint), repeating-linear-gradient(0deg, rgba(255,255,255,.8) 0 3px, transparent 3px 9px), var(--candy-grad, none); }
.m3-candy.m3-line-col { background-image: var(--glint), repeating-linear-gradient(90deg, rgba(255,255,255,.8) 0 3px, transparent 3px 9px), var(--candy-grad, none); }
.m3-candy.m3-wrapped {
  box-shadow: 0 0 0 3px rgba(255,255,255,.9), inset 0 -20% 30% rgba(0,0,0,.4), inset 0 15% 20% rgba(255,255,255,.35), 0 0 10px 2px rgba(255,255,255,.65);
  animation: m3-pulse 1.1s ease-in-out infinite;
}
.m3-candy.m3-bomb {
  border-radius: 50%; clip-path: none;
  background-image: radial-gradient(closest-side at 32% 26%, rgba(255,255,255,.8), rgba(255,255,255,0) 40%),
    repeating-conic-gradient(from 0deg, #ff4d4d 0 30deg, #ffd24d 30deg 60deg, #4dff88 60deg 90deg, #4dc3ff 90deg 120deg, #b34dff 120deg 150deg, #1a1a2e 150deg 180deg);
  animation: m3-hue 2.4s linear infinite;
}
@keyframes m3-pulse { 0%, 100% { filter: brightness(1); } 50% { filter: brightness(1.3); } }
@keyframes m3-hue { to { filter: hue-rotate(360deg); } }
@keyframes m3-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-12%); }
  75% { transform: translateX(12%); }
}

/* Rojo: redondo | Amarillo: cuadrado | Verde: rombo | Azul: hexágono | Morado: pentágono | Naranja: triángulo
   Forma Y color distintos por tipo, para que se distinga sin depender del color. */
.m3-c0 { --candy-grad: radial-gradient(circle at 35% 30%, #ff9d94, #f03828 58%, #8c130b); border-radius: 50%; }
.m3-c1 { --candy-grad: radial-gradient(circle at 35% 30%, #ffe98f, #ffc21f 58%, #9c7205); border-radius: 22%; }
.m3-c2 { --candy-grad: radial-gradient(circle at 35% 30%, #a8f5b9, #22bd58 58%, #0e5a2c); clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%); }
.m3-c3 { --candy-grad: radial-gradient(circle at 35% 30%, #a6dcff, #2596f5 58%, #114a85); clip-path: polygon(25% 6%, 75% 6%, 100% 50%, 75% 94%, 25% 94%, 0% 50%); }
.m3-c4 { --candy-grad: radial-gradient(circle at 35% 30%, #e8c2ff, #9b51e0 58%, #5b2c86); clip-path: polygon(50% 2%, 92% 34%, 76% 92%, 24% 92%, 8% 34%); }
.m3-c5 { --candy-grad: radial-gradient(circle at 35% 30%, #ffd9b0, #f2994a 58%, #a85b1a); clip-path: polygon(50% 4%, 96% 96%, 4% 96%); }

/* Tetris (Bloques): tablero COLS x ROWS, proporción fijada en el contenedor por JS */
.t-grid {
  display: grid; gap: 2px; margin: 0 auto; width: 100%; max-width: 300px;
  padding: 6px; border-radius: 14px; background: #101114;
  box-shadow: inset 0 0 0 2px #33363c, 0 8px 24px rgba(0,0,0,.45);
}
.t-cell { min-width: 0; min-height: 0; border-radius: 4px; background: #17181c; }
.t-cell.flash { background: #fff; box-shadow: 0 0 10px rgba(255,255,255,.9); }
.t1 { background: linear-gradient(150deg, #7de6f5, #23b6d6); }
.t2 { background: linear-gradient(150deg, #ffe27a, #f2b71c); }
.t3 { background: linear-gradient(150deg, #cf9bf0, #9b51e0); }
.t4 { background: linear-gradient(150deg, #8af0a8, #2fbd5d); }
.t5 { background: linear-gradient(150deg, #ff9d94, #ee4433); }
.t6 { background: linear-gradient(150deg, #92c1fb, #3f8ef7); }
.t7 { background: linear-gradient(150deg, #ffc48a, #f28d2e); }
.t-controls { display: flex; gap: 10px; justify-content: center; margin-top: 14px; }
.t-btn {
  flex: 1; max-width: 86px; height: 64px; font-size: 28px; border: 0; border-radius: 14px;
  background: var(--card); color: var(--ink); cursor: pointer;
}
.t-btn:active { transform: scale(.94); background: var(--bg2); }

/* Vista rápida (velocidad de procesamiento): arena con 8 posiciones periféricas + centro */
.v-arena {
  position: relative; width: 100%; max-width: 340px; aspect-ratio: 1; margin: 0 auto;
  background: #101114; border-radius: 16px; box-shadow: inset 0 0 0 2px #33363c;
}
.v-spot {
  position: absolute; width: 17%; aspect-ratio: 1; transform: translate(-50%, -50%);
  border-radius: 50%; border: 0; background: #17181c; color: var(--ink);
  display: flex; align-items: center; justify-content: center; font-size: 26px; padding: 0;
}
.v-spot.v-center { background: #202228; box-shadow: inset 0 0 0 2px #3a3e46; }
.v-spot.pickable { background: #232529; box-shadow: inset 0 0 0 2px #3a3e46; cursor: pointer; }
.v-spot.pickable:active { transform: translate(-50%, -50%) scale(.92); }
.v-spot.ok { background: var(--good); }
.v-spot.no { background: var(--bad); }

/* Marcador de progreso dentro del nivel */
.progress { display: flex; gap: 8px; justify-content: center; margin-bottom: 16px; }
.pip { width: 14px; height: 14px; border-radius: 50%; background: var(--bg2); }
.pip.on { background: var(--accent); }

/* Overlay de resultado */
.overlay { position: fixed; inset: 0; display: none; align-items: center; justify-content: center; background: rgba(0,0,0,.85); z-index: 10; padding: 20px; }
.overlay.show { display: flex; }
.result { background: var(--bg2); border-radius: 22px; padding: 30px 26px; max-width: 380px; width: 100%; text-align: center; }
.result h2 { font-size: 30px; margin-bottom: 6px; }
.result .stars { font-size: 46px; margin: 10px 0 6px; letter-spacing: 6px; }
.result p { color: var(--muted); font-size: 18px; margin-bottom: 8px; }

/* Toast */
#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(90px);
  background: var(--accent); color: var(--accent-ink); font-weight: 800; padding: 12px 20px;
  border-radius: 14px; transition: transform .3s; z-index: 20; font-size: 17px; max-width: 90%;
}
#toast.show { transform: translateX(-50%) translateY(0); }

/* Diálogo de código */
.code-box { display: flex; flex-direction: column; gap: 12px; }
.code-value { font-size: 30px; font-weight: 800; letter-spacing: 4px; color: var(--accent); text-align: center; background: var(--bg); padding: 14px; border-radius: 14px; -webkit-user-select: all; user-select: all; }
input.code-input {
  font-size: 26px; text-align: center; letter-spacing: 4px; text-transform: uppercase;
  padding: 14px; border-radius: 14px; border: 2px solid var(--card); background: var(--bg); color: var(--ink); width: 100%;
}

/* Fichas: objetivos y herramientas, manteniendo el tema accesible original. */
.m3-stage { display: flex; flex-direction: column; gap: 10px; width: 100%; }
.m3-level-title { font-size: 18px; font-weight: 650; color: var(--muted); text-align: center; }
.m3-stage .m3-hud { justify-content: space-between; gap: 16px; margin: 0; }
.m3-objective { flex: 1; }
.m3-goal-text { font-size: 20px; line-height: 1.25; margin-bottom: 5px; }
.m3-objective > b { color: var(--accent); font-size: 24px; }
.m3-stage .m3-progress { margin: 0; height: 8px; }
.m3-stage .m3-moves b { width: 52px; height: 52px; }
.m3-low-moves b { color: var(--accent); box-shadow: inset 0 0 0 3px var(--accent); }
.m3-toolbar { display: flex; gap: 7px; }
.m3-tool { flex: 1; min-height: 44px; border: 1px solid #494b51; border-radius: 10px;
  background: var(--card); color: var(--ink); font: inherit; font-size: 15px; cursor: pointer; padding: 6px; }
.m3-tool:disabled { opacity: .55; }
.m3-tool:focus-visible, .m3-cell:focus-visible, .m3-skip:focus-visible { outline: 3px solid white; outline-offset: 2px; }
.m3-status { min-height: 42px; color: #bfc4cc; font-size: 16px; line-height: 1.35; text-align: center; }
.m3-stage .m3-grid { margin: 0 auto; overflow: hidden; }
.m3-candy { transition: transform var(--m3-motion, 220ms) cubic-bezier(.34,1.56,.64,1), opacity var(--m3-motion, 220ms);
  box-shadow: inset 0 -5px 9px rgba(0,0,0,.35), inset 0 4px 6px rgba(255,255,255,.25), 0 3px 6px rgba(0,0,0,.4); }
.m3-candy.m3-wrapped { box-shadow: 0 0 0 3px rgba(255,255,255,.9), inset 0 -5px 9px rgba(0,0,0,.4), 0 0 8px rgba(255,255,255,.65); }
.m3-cell { isolation: isolate; }
.m3-cell .m3-candy { z-index: 1; }
.m3-hole { visibility: hidden; }
.m3-cell.m3-hinted { box-shadow: inset 0 0 0 3px #ffdf77; background: #474027; }
.m3-cell.m3-jelly1::before, .m3-cell.m3-jelly2::before { content: ""; position: absolute; inset: 3px;
  border: 2px solid #8adaff; border-radius: 8px; background: rgba(55,145,200,.25); z-index: 0; }
.m3-cell.m3-jelly2::before { border-style: double; border-width: 5px; background: rgba(55,145,200,.4); }
.m3-cell.m3-jelly2::after { content: "2"; position: absolute; bottom: 0; right: 1px; z-index: 2;
  background: #072e46; color: white; border-radius: 4px; font-size: 12px; padding: 0 3px; }
.m3-cell.m3-block1, .m3-cell.m3-block2 { background: linear-gradient(145deg,#c58c50,#744723);
  border: 3px solid #e8b77f; box-shadow: inset 0 0 0 2px #5b371b; opacity: 1; cursor: default; }
.m3-cell.m3-block1::after, .m3-cell.m3-block2::after { content: "1"; font-size: 22px; font-weight: 800; color: white; text-shadow: 0 1px 3px black; }
.m3-cell.m3-block2::after { content: "2"; }
.m3-damaged { filter: brightness(1.5); }
.m3-tutorial { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--accent);
  border-radius: 12px; background: #2a2519; font-size: 16px; line-height: 1.3; }
.m3-tutorial[hidden] { display: none; }
.m3-tutorial b { color: var(--accent); display: block; margin-bottom: 3px; }
.m3-skip { min-height: 44px; background: var(--bg2); border: 0; color: white; border-radius: 8px; padding: 7px 12px; font: inherit; cursor: pointer; }
.m3-help-dialog { position: fixed; margin: auto; width: min(560px, calc(100% - 28px)); max-height: 85svh;
  overflow: auto; border: 1px solid #656871; border-radius: 18px; background: var(--bg2); color: var(--ink); padding: 24px; }
.m3-help-dialog::backdrop { background: rgba(0,0,0,.85); }
.m3-help-dialog h2 { font-size: 24px; margin-bottom: 14px; }
.m3-help-dialog p { font-size: 18px; line-height: 1.45; margin-bottom: 14px; }
@media (max-width: 380px) {
  .m3-stage .m3-grid { gap: 3px; padding: 5px; }
  .m3-goal-text { font-size: 18px; }
  .m3-tool { font-size: 14px; }
}
@media (prefers-reduced-motion: reduce) {
  .m3-candy, .m3-candy.m3-wrapped, .m3-candy.m3-bomb, .m3-cell.sel .m3-candy { animation: none; transition: none; }
  .m3-progress-fill { transition: none; }
}
