:root {
  --bg: #0f0f10;
  --panel: #18181b;
  --tile: #333437;
  --tile-empty: #1d1d20;
  --tile-border: #3a3a3e;
  --hover: #5a5a5e;
  --selected: #3399ff;
  --solved: #4ca64c;
  --failed: #565758;
  --clue: #2bb6c6;        /* cian de pista */
  --text: #ffffff;
  --muted: #9a9a9e;
  --key: #6b7280;
  --key-blocked: #2a2a2d;
  --good: #4ca64c;
  --warn: #d4a017;
  --bad: #e0564f;
  --radius: 6px;
  --font: "Helvetica Neue", Helvetica, Arial, "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* el atributo hidden debe ocultar siempre (algunas reglas con display lo pisarían) */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  overscroll-behavior: none;
  touch-action: manipulation;   /* evita el zoom por doble toque en móvil */
}

/* refuerzo en los elementos que se tocan rápido (iOS) */
.key, .tile, .row, .icon-btn, .btn, .kbo-btn, .modal-close {
  touch-action: manipulation;
}

body {
  display: flex;
  justify-content: center;
  align-items: stretch;
}

#app {
  width: 100%;
  max-width: 500px;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) 8px env(safe-area-inset-bottom);
}

/* ---------- Formato web (escritorio / pantallas anchas) ---------- */
@media (min-width: 700px) {
  body {
    align-items: center;
    padding: 28px 16px;
    background:
      radial-gradient(1200px 600px at 50% -10%, #1c1c22 0%, var(--bg) 60%);
  }
  #app {
    height: auto;
    max-height: calc(100dvh - 56px);
    max-width: 440px;
    background: #131316;
    border: 1px solid #232328;
    border-radius: 20px;
    box-shadow: 0 24px 70px rgba(0,0,0,.55);
    padding: 4px 16px 18px;
    overflow: hidden;
  }
  /* en escritorio el tablero no se estira a toda la altura: tamaño natural */
  .board { flex: 0 0 auto; padding: 14px 4px; }
  .tile { font-size: 28px; }
  .clue { margin: 12px 6px; }
  .key { max-width: 40px; height: 50px; }
  .key.wide { max-width: 58px; }
}

/* ---------- Topbar ---------- */
.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;   /* lados iguales → logo siempre centrado */
  align-items: center;
  padding: 10px 4px 8px;
  border-bottom: 1px solid #242427;
}
#btnHelp { justify-self: start; }
.logo {
  font-size: clamp(15px, 4.5vw, 20px);
  font-weight: 800;
  letter-spacing: 1px;
  margin: 0;
  text-align: center;
  justify-self: center;
}
.logo span { color: var(--solved); }
.topbar-right { display: flex; align-items: center; gap: 8px; justify-self: end; }
.clock {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 16px;
  min-width: 52px;
  text-align: right;
  color: var(--muted);
  transition: color .3s;
}
.clock.urgent { color: var(--bad); }
.icon-btn {
  background: transparent;
  border: none;
  color: #c9c9ce;            /* claro para que se vean bien sobre el fondo oscuro */
  font-size: 18px;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.icon-btn:hover { background: #242427; color: var(--text); }
.icon-btn svg { width: 21px; height: 21px; fill: currentColor; display: block; }

/* etiqueta sutil de día temático (color se fija por JS según el tema) */
.theme-badge {
  text-align: center;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 2px;
  text-transform: uppercase;
  margin: 10px 0 -6px;
}

/* ---------- Clue ---------- */
.clue {
  text-align: center;
  align-self: stretch;
  margin: 14px 10px;
  font-size: clamp(16px, 4.6vw, 19px);
  min-height: 2.6em;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1.3;
  font-weight: 500;
}

/* cuenta atrás al próximo reto (persistente, bajo la pista) */
.next-countdown {
  text-align: center;
  margin: -4px 10px 6px;
  font-size: 13px;
  color: var(--muted);
}
.next-countdown b { color: var(--clue); font-variant-numeric: tabular-nums; }

/* subtítulo explicativo dentro de modales */
.modal-sub { text-align: center; color: var(--muted); font-size: 13px; margin: -6px 0 14px; line-height: 1.4; }

/* ---------- Board ---------- */
/* zona de juego: pista + tablero juntos y centrados verticalmente */
.play {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;            /* escritorio: centrado (se ve mejor en pantallas anchas) */
  align-items: center;                /* centra el tablero horizontalmente de forma fiable */
  padding: 4px 6px;
}
/* En móvil/táctil: centrado (como en web) y casillas algo más grandes para que
   el tablero llene mejor la pantalla alta y no quede flotando ni pequeño. */
@media (pointer: coarse) {
  /* 90vw para llenar el ancho, 56vh para no desbordar en móviles cortos, 360px tope */
  .board { max-width: min(90vw, 56vh, 360px); }
}
.board {
  flex: 0 0 auto;
  width: 100%;
  max-width: 330px;       /* tablero centrado y más estrecho que el teclado */
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: clamp(6px, 1.8vw, 10px);
  padding: 8px 0;
}
.row {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr)); /* columnas iguales, no crecen con el contenido */
  align-items: start;     /* que mande aspect-ratio (no estirar la celda verticalmente) */
  gap: clamp(5px, 1.6vw, 9px);
  cursor: pointer;
}
.row[data-locked="true"] { cursor: default; }

.tile {
  width: 100%;
  aspect-ratio: 1 / 1;
  min-width: 0;
  overflow: hidden;
  display: grid;
  place-items: center;
  font-size: clamp(20px, 6.5vw, 30px);
  font-weight: 800;
  text-transform: uppercase;
  border-radius: var(--radius);
  background: var(--tile);
  border: 2px solid transparent;
  user-select: none;
  transition: background .15s, border-color .15s, transform .15s;
}

/* fila no resuelta = tiles vacíos con borde */
.row[data-state="idle"] .tile { background: var(--tile-empty); border-color: var(--tile-border); }
.row[data-state="idle"]:hover:not([data-locked="true"]) .tile { border-color: var(--hover); }
.row[data-state="selected"] .tile { background: var(--tile-empty); border-color: var(--selected); }

/* pistas reveladas (cian) dentro de fila no resuelta
   (especificidad alta para ganar a .row[data-state="idle"] .tile)
   OJO: clase "hint", NO "clue" (colisionaría con el párrafo .clue y su min-height) */
.row[data-state="idle"] .tile.hint,
.row[data-state="selected"] .tile.hint,
.tile.hint {
  background: var(--clue);
  border-color: transparent;
  color: #fff;
}

/* fila resuelta / fallada */
.row[data-state="solved"] .tile { background: var(--solved); border-color: transparent; color: #fff; }
.row[data-state="failed"] .tile { background: var(--failed); border-color: transparent; color: #cfcfcf; }

.tile.pop { animation: pop .12s ease; }
@keyframes pop { 0%{transform:scale(.85)} 100%{transform:scale(1)} }

/* Revelado de pista / acierto: aparición con escala (no deforma la casilla) */
.tile.reveal { animation: reveal .32s cubic-bezier(.2,.7,.3,1.3); }
@keyframes reveal {
  0%   { transform: scale(.5); }
  60%  { transform: scale(1.08); }
  100% { transform: scale(1); }
}
.row.shake { animation: shake .4s ease; }
@keyframes shake {
  0%,100%{transform:translateX(0)} 20%{transform:translateX(-6px)}
  40%{transform:translateX(6px)} 60%{transform:translateX(-4px)} 80%{transform:translateX(4px)}
}

/* ---------- Keyboard ---------- */
.kb-wrap { position: relative; }

/* al terminar el reto del día, difuminamos el teclado y mostramos el overlay */
.kb-wrap.done .keyboard {
  filter: blur(5px);
  opacity: .35;
  pointer-events: none;
  user-select: none;
}
.kb-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 8px;
  padding: 12px;
}
.kbo-title { font-size: clamp(17px, 5vw, 21px); font-weight: 800; letter-spacing: .5px; }
.kbo-sub { color: var(--muted); font-size: 14px; }
.kbo-sub b { color: var(--clue); font-variant-numeric: tabular-nums; font-size: 16px; }
.kbo-btn {
  margin-top: 4px;
  border: 1px solid #3a3a3e;
  background: #1f1f23;
  color: var(--text);
  font-weight: 700;
  font-size: 14px;
  padding: 9px 18px;
  border-radius: 10px;
  cursor: pointer;
}
.kbo-btn:hover { background: #2a2a2e; }

.keyboard {
  display: flex;
  flex-direction: column;
  gap: 7px;
  padding: 14px 4px max(8px, env(safe-area-inset-bottom));
  margin-top: 8px;
  border-top: 1px solid #2a2a2e;
}
.kb-row { display: flex; gap: 5px; justify-content: center; }
.key {
  flex: 1;
  height: clamp(44px, 12vw, 56px);
  max-width: 44px;
  border: none;
  border-radius: 6px;
  background: var(--key);
  color: #fff;
  font-size: clamp(13px, 4vw, 17px);
  font-weight: 700;
  text-transform: uppercase;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background .1s, opacity .2s, transform .05s;
}
.key:active { transform: scale(.94); }
.key.wide { flex: 1.5; max-width: 64px; }
.key.confirm { background: var(--solved); }
.key.confirm:hover { filter: brightness(1.1); }
.key.del { background: #44464a; }
.key.blocked {
  background: var(--key-blocked);
  color: #4a4a4d;
  pointer-events: none;
}
.key svg { width: 22px; height: 22px; fill: #fff; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.6);
  display: grid;
  place-items: center;
  z-index: 50;
  padding: 16px;
  animation: fade .15s ease;
}
@keyframes fade { from{opacity:0} to{opacity:1} }
.modal {
  background: var(--panel);
  border-radius: 14px;
  width: 100%;
  max-width: 420px;
  max-height: 86dvh;
  overflow-y: auto;
  position: relative;
  padding: 28px 22px 24px;
  box-shadow: 0 18px 50px rgba(0,0,0,.5);
}
.modal-close {
  position: absolute;
  top: 12px; right: 14px;
  background: transparent; border: none;
  color: var(--muted); font-size: 20px; cursor: pointer;
}
.modal-content h2 { margin: 0 0 14px; font-size: 22px; text-align: center; }
.modal-content h3 { margin: 18px 0 8px; font-size: 15px; }
.modal-content p { line-height: 1.5; font-size: 14.5px; color: #d8d8dc; margin: 8px 0; }
.modal-content .big { font-size: 24px; font-weight: 800; text-align: center; margin: 6px 0 4px; }

.legend { display: flex; align-items: center; gap: 10px; margin: 8px 0; font-size: 14px; }
.legend .swatch { width: 26px; height: 26px; border-radius: 5px; flex: 0 0 auto; }

.stats-grid { display: flex; justify-content: space-around; text-align: center; margin: 6px 0 4px; }
.stats-grid .num { font-size: 30px; font-weight: 800; }
.stats-grid .lbl { font-size: 11px; color: var(--muted); text-transform: uppercase; }

.share-grid {
  font-size: 26px; text-align: center; letter-spacing: 4px; line-height: 1.2;
  margin: 14px 0; user-select: none;
}

/* ---- Resultado de fin de partida (estilo propio, sin emojis) ---- */
.result-score { text-align: center; font-size: 56px; font-weight: 800; line-height: 1; margin: 2px 0; }
.result-score span { font-size: 24px; color: var(--muted); font-weight: 700; }
.result-score.perfect { color: var(--solved); }
.result-msg { text-align: center; font-size: 15px; color: #d8d8dc; margin: 4px 0 2px; }

.result-tiles { display: flex; gap: 7px; justify-content: center; margin: 16px 0; }
.result-tiles .rt { width: 32px; height: 32px; border-radius: 7px; }
.result-tiles .rt.ok { background: var(--solved); }
.result-tiles .rt.no { background: var(--failed); }

.result-meta { display: flex; gap: 10px; margin: 16px 0 4px; }
.result-meta > div {
  flex: 1; background: #0e0e10; border: 1px solid #242427;
  border-radius: 12px; padding: 10px 8px; text-align: center;
}
.result-meta .rm-label {
  display: block; font-size: 10.5px; color: var(--muted);
  text-transform: uppercase; letter-spacing: .5px; margin-bottom: 5px;
}
.result-meta b { font-size: 18px; font-weight: 800; font-variant-numeric: tabular-nums; }
.result-meta b#cd { color: var(--clue); }

.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; padding: 14px; margin-top: 14px;
  border: none; border-radius: 10px; cursor: pointer;
  font-size: 16px; font-weight: 700; color: #fff;
  background: var(--solved);
}
.btn:hover { filter: brightness(1.08); }
.btn.secondary { background: #3a3a3e; }

.countdown { text-align: center; color: var(--muted); font-size: 13px; margin-top: 12px; }
.countdown b { color: var(--text); font-variant-numeric: tabular-nums; }

.countdown-box {
  text-align: center;
  background: #0e1a1c;
  border: 1px solid #1d3a3f;
  color: var(--muted);
  padding: 12px;
  border-radius: 12px;
  margin: 14px 0;
  font-size: 13px;
  line-height: 1.5;
}
.countdown-box b {
  display: block;
  margin-top: 2px;
  color: var(--clue);
  font-size: 22px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  top: 70px; left: 50%; transform: translateX(-50%);
  background: #fff; color: #111;
  padding: 10px 18px; border-radius: 8px; font-weight: 700; font-size: 14px;
  z-index: 80; box-shadow: 0 6px 20px rgba(0,0,0,.4);
  animation: fade .15s ease;
}
