/* ============================================================
   juego.css — 2048.

   Geometría del tablero, para que cuadre con juego.js.

   Ojo con contra qué se mide cada porcentaje:
     padding del tablero ..... 3% del ancho del tablero
     área interior ........... 94% del tablero
   Fichas y huecos viven DENTRO de esa área interior, así que sus
   medidas se expresan contra ella:
     separación .............. 2.5 / 94 = 2.66%
     lado de la ficha ........ (100% - 3*2.66%) / 4 = 23.005%
     paso de una ficha ....... (23.005 + 2.66) / 23.005 = 111.563%
   El paso va en porcentaje del propio tile porque translate mide
   los porcentajes contra el elemento, no contra el padre.

   Posición y animación van en propiedades separadas: `translate`
   coloca la ficha y `scale` la anima. Así una animación nunca pisa
   la posición, y no hay que repetir el translate en cada keyframe.
   ============================================================ */

:root {
  color-scheme: light;
  --j-bg: #f6f4ef;
  --j-texto: #34302c;
  --j-tenue: #9d968d;
  --j-tablero: #e8e3da;
  --j-hueco: #ddd7cc;
  --j-boton: #34302c;
  --j-claro: #fbfaf7;
  --j-ficha-2: #fbfaf7;
  --j-ficha-4: #f1ebe0;
  --j-ficha-txt: #4a443e;
  --j-velo: rgba(246, 244, 239, .78);
  --j-paso: 111.563%;

  /* la escalera: de durazno a índigo, pasando por coral y magenta */
  --j-8: #f2a66e;
  --j-16: #ee8a5c;
  --j-32: #e96c52;
  --j-64: #de4f4b;
  --j-128: #d6456f;
  --j-256: #b8458f;
  --j-512: #9148a8;
  --j-1024: #6a4fba;
  --j-2048: #4459c9;
  --j-super: #1f2233;

  /* borde de vidrio líquido: las sombras del LiquidButton, versión clara */
  --j-vidrio:
    0 0 6px rgba(0, 0, 0, .03), 0 2px 6px rgba(0, 0, 0, .08),
    inset 3px 3px .5px -3px rgba(0, 0, 0, .9), inset -3px -3px .5px -3px rgba(0, 0, 0, .85),
    inset 1px 1px 1px -.5px rgba(0, 0, 0, .6), inset -1px -1px 1px -.5px rgba(0, 0, 0, .6),
    inset 0 0 6px 6px rgba(0, 0, 0, .12), inset 0 0 2px 2px rgba(0, 0, 0, .06),
    0 0 12px rgba(255, 255, 255, .15);

  --j-suave: cubic-bezier(.25, .8, .25, 1);
  --j-rebote: cubic-bezier(.34, 1.56, .64, 1);
  --j-letra: ui-rounded, "SF Pro Rounded", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --j-bg: #141417;
    --j-texto: #eceae5;
    --j-tenue: #7c7974;
    --j-tablero: #1f1f24;
    --j-hueco: #2a2a30;
    --j-boton: #eceae5;
    --j-claro: #141417;
    --j-ficha-2: #3b3b42;
    --j-ficha-4: #4a4850;
    --j-ficha-txt: #eceae5;
    --j-velo: rgba(20, 20, 23, .8);
    --j-super: #f4f1ea;
    /* versión oscura del LiquidButton */
    --j-vidrio:
      0 0 8px rgba(0, 0, 0, .03), 0 2px 6px rgba(0, 0, 0, .08),
      inset 3px 3px .5px -3.5px rgba(255, 255, 255, .09), inset -3px -3px .5px -3.5px rgba(255, 255, 255, .85),
      inset 1px 1px 1px -.5px rgba(255, 255, 255, .6), inset -1px -1px 1px -.5px rgba(255, 255, 255, .6),
      inset 0 0 6px 6px rgba(255, 255, 255, .12), inset 0 0 2px 2px rgba(255, 255, 255, .06),
      0 0 12px rgba(0, 0, 0, .15);
  }
}

.juego {
  min-height: 100dvh;
  background: var(--j-bg);
  color: var(--j-texto);
  padding: calc(env(safe-area-inset-top, 0px) + 28px) 20px calc(env(safe-area-inset-bottom, 0px) + 24px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-family: var(--j-letra);
  font-variant-numeric: tabular-nums;
  -webkit-user-select: none;
  user-select: none;
}

.juego > * { width: 100%; max-width: 440px; }

/* ---------- encabezado ---------- */

.j-top {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 22px;
}

.j-logo {
  margin: 0;
  font-size: 46px;
  font-weight: 800;
  line-height: .9;
  letter-spacing: -.045em;
}

.j-marcadores { display: flex; gap: 22px; }

.j-caja {
  position: relative;
  text-align: right;
}
.j-caja span {
  display: block;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: .14em;
  color: var(--j-tenue);
  margin-bottom: 3px;
}
.j-caja strong {
  display: block;
  font-size: 24px;
  font-weight: 700;
  line-height: 1;
  letter-spacing: -.02em;
  transform-origin: right center;
}
.j-caja strong.sube { animation: j-late .28s var(--j-rebote); }

/* el "+32" que flota sobre el puntaje */
.j-suma {
  position: absolute;
  right: 0;
  bottom: 0;
  font-size: 15px;
  font-weight: 700;
  color: var(--j-32);
  pointer-events: none;
  animation: j-flota .7s var(--j-suave) forwards;
}

.j-barra {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
}
.j-tams {
  display: flex;
  gap: 2px;
  padding: 3px;
  margin-right: auto;
  border-radius: 999px;
  background: color-mix(in srgb, var(--j-tablero) 70%, transparent);
}
.j-tams button {
  width: 34px;
  height: 32px;
  border-radius: 999px;
  border: 0;
  background: none;
  color: var(--j-tenue);
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: background-color .2s, color .2s;
}
.j-tams button.on { background: var(--j-bg); color: var(--j-texto); box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.j-btn.j-sec { padding: 0; width: 44px; justify-content: center; background: color-mix(in srgb, var(--j-boton) 16%, transparent); color: var(--j-texto); }
.j-btn.j-sec:disabled { opacity: .35; pointer-events: none; }
.j-btn.j-sec svg { width: 18px; height: 18px; }
.j-lema { margin: 0; font-size: 14px; line-height: 1.4; color: var(--j-tenue); }
.j-lema b { color: var(--j-texto); font-weight: 700; }

/* ---------- vidrio líquido ----------
   Port a CSS puro del LiquidButton (shadcn): tres capas.
     ::before  el borde, hecho solo de sombras interiores
     ::after   el fondo de atrás, deformado por el filtro SVG
               #container-glass (turbulencia + desplazamiento)
     contenido el texto, por encima de las dos
   Safari no acepta url() en backdrop-filter: ahí se queda con el
   desenfoque de la línea anterior, y el borde se ve igual. */

.vidrio,
.j-btn,
.j-teclado button:not(.j-ghost) {
  position: relative;
  isolation: isolate;
  background: transparent;
  transition: scale .3s var(--j-suave), filter .2s;
}
.vidrio::before, .vidrio::after,
.j-btn::before, .j-btn::after,
.j-teclado button:not(.j-ghost)::before,
.j-teclado button:not(.j-ghost)::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
}
.vidrio::before,
.j-btn::before,
.j-teclado button:not(.j-ghost)::before {
  z-index: 1;
  box-shadow: var(--j-vidrio);
}
.vidrio::after,
.j-btn::after,
.j-teclado button:not(.j-ghost)::after {
  z-index: -1;
  overflow: hidden;
  -webkit-backdrop-filter: blur(3px) saturate(1.5);
  backdrop-filter: blur(3px) saturate(1.5);
  backdrop-filter: url("#container-glass");
}
@media (hover: hover) {
  .j-btn:hover,
  .j-teclado button:not(.j-ghost):hover { scale: 1.05; }
}
.j-btn:active,
.j-teclado button:not(.j-ghost):active { scale: .95; filter: brightness(.94); transition-duration: .08s; }

.j-btn {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  height: 44px;
  padding: 0 20px;
  border: 0;
  border-radius: 999px;
  color: var(--j-texto);
  font: inherit;
  font-size: 15px;
  font-weight: 650;
  cursor: pointer;
}
.j-btn svg { position: relative; z-index: 2; width: 16px; height: 16px; stroke-width: 2.2; }
.j-btn:active svg { transform: rotate(-90deg); transition: transform .25s var(--j-suave); }

/* ---------- tablero ---------- */

.j-tablero {
  position: relative;
  aspect-ratio: 1;
  padding: 3%;
  border-radius: 20px;
  /* Manchas de color difuminadas detrás de todo, como el fondo de pantalla
     de iOS: un vidrio sobre un fondo liso no se nota; sobre esto, sí. */
  background:
    radial-gradient(circle at 18% 22%, color-mix(in srgb, var(--j-16) 45%, transparent), transparent 42%),
    radial-gradient(circle at 85% 30%, color-mix(in srgb, var(--j-2048) 38%, transparent), transparent 45%),
    radial-gradient(circle at 30% 85%, color-mix(in srgb, var(--j-256) 38%, transparent), transparent 45%),
    radial-gradient(circle at 80% 88%, color-mix(in srgb, var(--j-8) 42%, transparent), transparent 40%),
    var(--j-tablero);
  touch-action: none;
  container-type: inline-size;
}

.j-fondo {
  display: grid;
  grid-template-columns: repeat(var(--n, 4), 1fr);
  grid-template-rows: repeat(var(--n, 4), 1fr);
  gap: 2.66%;            /* = 2.5% del tablero, medido contra el área interior */
  width: 100%;
  height: 100%;
}
.j-fondo i { border-radius: 12px; background: color-mix(in srgb, var(--j-hueco) 55%, transparent); }

.j-tiles {
  position: absolute;
  inset: 3%;
  z-index: 0;            /* contexto propio: la ficha absorbida no se hunde bajo el tablero */
}

.tile {
  --t: var(--j-ficha-2);       /* el tinte del vidrio, lo pone cada valor */
  --opaco: 55%;                /* cuánto color lleva; el resto deja ver lo de atrás */
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  width: var(--j-tile, 23.005%);
  height: var(--j-tile, 23.005%);
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: var(--t);                                            /* sin color-mix: sólido */
  background: color-mix(in srgb, var(--t) var(--opaco), transparent);
  /* sin backdrop-filter: 16 fichas desenfocando mientras se deslizan
     trababan el juego, y sobre las manchas ya difusas del tablero
     la diferencia no se nota */
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .18);
  font-size: calc(42cqi / var(--n, 4));
  font-weight: 700;
  letter-spacing: -.03em;
  translate: calc(var(--c, 0) * var(--j-paso)) calc(var(--r, 0) * var(--j-paso));
  transition: translate .11s var(--j-suave), background-color .12s;
  will-change: translate;
}

.tile[data-v="2"]    { --t: var(--j-ficha-2); --opaco: 50%; color: var(--j-ficha-txt); text-shadow: none; }
.tile[data-v="4"]    { --t: var(--j-ficha-4); --opaco: 62%; color: var(--j-ficha-txt); text-shadow: none; }
.tile[data-v="8"]    { --t: var(--j-8); }
.tile[data-v="16"]   { --t: var(--j-16); }
.tile[data-v="32"]   { --t: var(--j-32); }
.tile[data-v="64"]   { --t: var(--j-64); }
.tile[data-v="128"]  { --t: var(--j-128);  font-size: 8.4cqi; }
.tile[data-v="256"]  { --t: var(--j-256);  font-size: 8.4cqi; }
.tile[data-v="512"]  { --t: var(--j-512);  font-size: 8.4cqi; }
.tile[data-v="1024"] { --t: var(--j-1024); font-size: 6.8cqi; }
.tile[data-v="2048"] { --t: var(--j-2048); font-size: 6.8cqi;
                       box-shadow: 0 0 0 0 rgba(68, 89, 201, .5); animation: j-brilla 2.4s ease-in-out infinite; }
.tile[data-v="super"]{ --t: var(--j-super); --opaco: 70%; color: var(--j-bg); text-shadow: none; font-size: 5.8cqi; }

/* el vidrio: borde de sombras (::before) y un reflejo diagonal (::after) */
.tile::before,
.tile::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
}
.tile::before { box-shadow: var(--j-vidrio); }
.tile::after {
  background: linear-gradient(135deg, rgba(255, 255, 255, .38) 0%, rgba(255, 255, 255, .08) 38%, transparent 55%);
  mix-blend-mode: soft-light;
}

.tile.muriendo { z-index: 1; }
.tile.nuevo { animation: j-aparece .2s var(--j-rebote) .08s backwards; }
.tile.fusion { z-index: 3; animation: j-crece .22s var(--j-rebote) .05s; }

@keyframes j-aparece { from { scale: .2; opacity: 0; } }
@keyframes j-crece { 45% { scale: 1.14; } }
@keyframes j-late { 40% { transform: scale(1.12); } }
@keyframes j-flota {
  from { opacity: 0; translate: 0 4px; }
  20%  { opacity: 1; }
  to   { opacity: 0; translate: 0 -30px; }
}
@keyframes j-brilla { 50% { box-shadow: 0 0 22px 2px rgba(68, 89, 201, .45); } }

/* ---------- aviso de fin de partida ---------- */

.j-aviso {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  border-radius: 20px;
  background: var(--j-velo);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  animation: j-fade .35s var(--j-suave);
}
.j-aviso p {
  margin: 0;
  padding: 0 24px;
  font-size: 26px;
  font-weight: 800;
  letter-spacing: -.02em;
  text-align: center;
}
.j-aviso > * { animation: j-sube .4s var(--j-suave) .08s backwards; }
.j-aviso .j-btn { animation-delay: .16s; }

@keyframes j-fade { from { opacity: 0; } }
@keyframes j-sube { from { opacity: 0; translate: 0 10px; } }

.j-ayuda {
  margin: 20px 0 6vh;   /* el conjunto queda un poco arriba del centro */
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--j-tenue);
  text-align: center;
}

/* ---------- código de nivel ---------- */

.j-codigo {
  position: fixed;
  inset: 0;
  z-index: 150;
  background: var(--j-bg);
  color: var(--j-texto);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 30px 24px calc(40px + env(safe-area-inset-bottom, 0px));
  text-align: center;
  font-family: var(--j-letra);
  animation: j-fade .22s var(--j-suave);
}
.j-codigo > * { animation: j-sube .35s var(--j-suave) backwards; }
.j-codigo > .j-teclado { animation-delay: .06s; }

.j-codigo h2 { margin: 0 0 6px; font-size: 26px; font-weight: 800; letter-spacing: -.02em; }
.j-codigo p { margin: 0 0 30px; font-size: 14px; color: var(--j-tenue); }
.j-codigo p.err { color: var(--j-64); font-weight: 600; }

.j-puntos { display: flex; gap: 14px; min-height: 12px; margin-bottom: 40px; }
.j-puntos i {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--j-hueco);
  transition: background-color .15s, scale .2s var(--j-rebote);
}
.j-puntos i.on { background: var(--j-texto); scale: 1.15; }
.j-puntos.mal { animation: j-niega .38s ease; }
.j-puntos.mal i.on { background: var(--j-64); }

@keyframes j-niega {
  20% { translate: -9px 0; }
  40% { translate: 8px 0; }
  60% { translate: -5px 0; }
  80% { translate: 3px 0; }
}

.j-teclado { display: grid; grid-template-columns: repeat(3, 74px); gap: 16px 24px; }
.j-teclado button {
  height: 74px;
  border: 0;
  border-radius: 50%;
  color: var(--j-texto);
  font: inherit;
  font-size: 28px;
  font-weight: 600;
  cursor: pointer;
}
.j-teclado .j-ghost {
  background: none;
  font-size: 14px;
  font-weight: 600;
  color: var(--j-tenue);
}

/* El botón de confirmar solo aparece al crear una contraseña nueva:
   es el único momento en que nadie sabe dónde termina el código. */
.j-listo {
  justify-content: center;
  margin-top: 30px;
  min-width: 246px;
  height: 54px;
  font-size: 17px;
}

/* paso de Face ID después del código */
.j-face {
  justify-content: center;
  gap: 10px;
  margin-top: 8px;
  min-width: 260px;
  height: 56px;
  font-size: 17px;
}
.j-face svg { position: relative; z-index: 2; width: 26px; height: 26px; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.j-otra {
  margin-top: 18px;
  padding: 10px 16px;
  border: 0;
  background: none;
  color: var(--j-tenue);
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.j-codigo.con-face .j-teclado,
.j-codigo.con-face .j-puntos { display: none; }
.j-codigo.con-face .j-face { animation: j-sube .4s var(--j-suave); }

@media (prefers-reduced-motion: reduce) {
  .juego *, .j-codigo, .j-codigo * {
    animation-duration: 1ms !important;
    animation-delay: 0s !important;
    animation-iteration-count: 1 !important;
  }
}

/* ---------- editor del patrón (vive dentro de la bóveda) ---------- */

.patron-grid {
  display: grid;
  grid-template-columns: repeat(4, 62px);
  gap: 10px;
  margin-bottom: 26px;
}
.patron-grid button {
  aspect-ratio: 1;
  border: 0;
  border-radius: 10px;
  background: var(--bg-3);
  color: var(--txt-dim);
  font-size: 18px;
  font-weight: 700;
}
.patron-grid button.elegida {
  background: var(--accent);
  color: #fff;
}
.patron-acciones { display: flex; gap: 12px; }
.patron-acciones .btn { min-width: 130px; }
.btn[disabled] { opacity: .4; pointer-events: none; }
