/* ============================================================
   ENIGMA - tablero de crucigrama
   Objetivos: casillas grandes, todo visible sin scroll mientras se juega,
   excelente en escritorio, tablet (vertical y horizontal), movil y stylus.
   ============================================================ */
.play-body { overflow-x: hidden; }

.play-shell { display: flex; flex-direction: column; gap: .8rem; }

/* ============================================================
   CROMO COMÚN DE TODAS LAS PÁGINAS DE JUEGO
   (lo usan crucigrama, silábico, sudoku, damas y los juegos de
   PlayKit — ver templates/play_base.html)
   ============================================================ */
.play-body .page { max-width: 1240px; padding-top: 14px; padding-bottom: 28px; }
.play-shell { display: flex; flex-direction: column; gap: 14px; }

/* ---------- barra superior ---------- */
.play-topbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; min-height: 48px; }
.play-ident { display: flex; align-items: center; gap: 12px; min-width: 0; }
.play-ident-text { min-width: 0; }
.play-back {
  display: inline-grid; place-items: center; flex: none; width: 40px; height: 40px;
  border-radius: 50%; color: var(--ink); text-decoration: none;
  border: 1px solid var(--line-2); background: var(--surface);
  transition: background var(--dur), border-color var(--dur);
}
.play-back:hover { background: var(--surface-2); }
.play-back svg { width: 18px; height: 18px; }
.play-back:not(:has(svg)) { font-size: 1.05rem; }
.play-title {
  margin: 0; font-family: var(--font-ui); font-size: 1.125rem; font-weight: 600; letter-spacing: -.015em; line-height: 1.25;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.play-sub { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-top: 2px; font-size: var(--t-xs); color: var(--muted); }
.play-sub .chip { height: 20px; padding: 0 8px; font-size: .7rem; }

.play-timer-wrap { display: flex; align-items: center; gap: 4px; flex: none; }
.play-timer {
  font-family: var(--font-num); font-size: 1.05rem; font-weight: 500; letter-spacing: -.02em;
  font-variant-numeric: tabular-nums; min-width: 4ch; text-align: right; color: var(--ink);
  padding: 0 4px;
}
.play-timer.is-paused { color: var(--muted); }
.pause-btn .ico-play { display: none; }
.pause-btn[aria-pressed="true"] .ico-pause { display: none; }
.pause-btn[aria-pressed="true"] .ico-play { display: block; }
/* botón de pausa heredado en texto (si alguna plantilla aún lo usa) */
.play-timer-wrap .btn { min-height: 34px; }

/* ---------- barra de acciones ---------- */
.play-toolbar {
  position: relative; z-index: 20;
  display: flex; align-items: center; gap: 2px; flex-wrap: wrap;
  padding: 4px; border-radius: 14px;
  background: var(--surface); border: 1px solid var(--line);
}
.tool-group { position: relative; }
.tool-btn {
  display: inline-flex; align-items: center; gap: 7px; height: 36px; padding: 0 12px;
  font: inherit; font-size: var(--t-sm); font-weight: 500; color: var(--ink-2); text-decoration: none;
  background: transparent; border: 0; border-radius: 10px; cursor: pointer; white-space: nowrap;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation;
  transition: background var(--dur), color var(--dur);
}
.tool-btn svg { width: 17px; height: 17px; flex: none; }
.tool-btn:hover { background: var(--surface-2); color: var(--ink); }
.tool-btn:active { background: var(--surface-3); }
.tool-btn[aria-pressed="true"] { background: var(--ink); color: var(--on-ink); }
.tool-btn[disabled] { opacity: .38; cursor: default; pointer-events: none; }
.tool-group.open > .tool-btn { background: var(--surface-2); color: var(--ink); }
.tool-group > .tool-btn::after {
  content: ""; width: 6px; height: 6px; margin-left: 1px; margin-top: -3px;
  border-right: 1.5px solid currentColor; border-bottom: 1.5px solid currentColor; transform: rotate(45deg); opacity: .55;
}
.tool-more > .tool-btn::after { display: none; }
.tool-more { margin-left: auto; }
.tool-status ~ .tool-more { margin-left: 0; }
.tool-pop {
  position: absolute; top: calc(100% + 8px); left: 0; min-width: 230px; z-index: 40;
  padding: 6px; border-radius: 14px; display: none;
  background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow-lg);
}
.tool-pop-right { left: auto; right: 0; }
.tool-group.open .tool-pop { display: block; animation: pop-in .14s var(--ease); }
@keyframes pop-in { from { opacity: 0; transform: translateY(-4px); } }
.tool-pop [role="menuitem"] {
  display: flex; align-items: center; gap: 10px; width: 100%; min-height: 40px; padding: 0 12px;
  font: inherit; font-size: var(--t-sm); color: var(--ink); text-align: left; text-decoration: none;
  background: transparent; border: 0; border-radius: 9px; cursor: pointer;
}
.tool-pop [role="menuitem"] svg { width: 17px; height: 17px; color: var(--muted); flex: none; }
.tool-pop [role="menuitem"]:hover, .tool-pop [role="menuitem"]:focus-visible { background: var(--surface-2); outline: none; }
.tool-pop hr { border: 0; border-top: 1px solid var(--line); margin: 6px 4px; }
.tool-status {
  flex: 1 1 auto; min-width: 0; margin-left: auto; padding: 0 10px;
  font-size: var(--t-sm); color: var(--muted); text-align: right;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tool-sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; }

@media (max-width: 640px) {
  .play-body .page { padding-top: 8px; padding-left: 12px; padding-right: 12px; }
  .play-shell { gap: 10px; }
  .play-back { width: 36px; height: 36px; }
  .play-title { font-size: 1rem; }
  .tool-btn { padding: 0 10px; }
  .tool-btn .lbl { display: none; }
  .tool-btn.keep-lbl .lbl { display: inline; }
  /* en móvil el mensaje flota bajo la barra (tipo aviso) en vez de
     ocupar una línea: así el tablero no salta mientras se juega */
  .tool-status {
    position: absolute; top: calc(100% + 8px); left: 50%; transform: translateX(-50%);
    width: max-content; max-width: calc(100% - 16px); margin: 0; padding: 8px 14px;
    background: var(--ink); color: var(--on-ink); border-radius: var(--r-pill);
    font-size: var(--t-xs); text-align: center; white-space: normal; box-shadow: var(--shadow-md);
    pointer-events: none; z-index: 30; animation: pop-in .16s var(--ease);
  }
  .tool-status:empty { display: none; }
}

/* selector segmentado dentro de la barra (p. ej. Pintar / Marcar) */
.tool-seg { display: inline-flex; gap: 2px; padding: 2px; border-radius: 12px; background: var(--surface-2); }
.tool-seg .tool-btn { height: 32px; }
.tool-seg .tool-btn[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow-sm); }
.tool-btn svg rect { transition: fill var(--dur); }

/* tablero oculto en pausa (todos los juegos) */
.play-shell.is-paused .board-host { filter: blur(10px); pointer-events: none; user-select: none; }

/* ============================================================
   LAYOUT
   ============================================================ */
.cw { --cell: 40px; }
.cw-layout {
  display: grid;
  grid-template-columns: min-content minmax(260px, 1fr);
  gap: clamp(1rem, 2.5vw, 2.2rem);
  align-items: start;
}
.cw-left { display: flex; flex-direction: column; gap: .7rem; min-width: 0; }

/* barra de pista activa (siempre visible) */
.cw-current {
  display: flex; align-items: stretch; gap: 0;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; overflow: hidden;
  min-height: 52px;
}
.cw-current-nav {
  flex: none; width: 46px; border: 0; background: var(--paper-2); color: var(--ink-soft);
  font-size: 1.4rem; cursor: pointer; transition: background .12s, color .12s;
}
.cw-current-nav:first-child { border-right: 1px solid var(--line); }
.cw-current-nav:last-child { border-left: 1px solid var(--line); }
.cw-current-nav:hover { background: var(--sel-weak); color: var(--sel-ink); }
.cw-current-text { flex: 1 1 auto; padding: .5rem .8rem; min-width: 0; display: flex; flex-direction: column; justify-content: center; gap: .1rem; }
.cw-current-label { font-size: .68rem; text-transform: uppercase; letter-spacing: .12em; color: var(--sel-ink); font-weight: 600; }
.cw-current-clue { font-size: 1.02rem; color: var(--ink); line-height: 1.3; }

/* ---------- tablero ---------- */
.cw-board-wrap { position: relative; align-self: start; }
.cw-board {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-template-rows: repeat(var(--rows), var(--cell));
  gap: 1px;
  background: var(--board-lines);
  border: 2px solid var(--ink);
  border-radius: 4px;
  touch-action: manipulation;
  user-select: none; -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.cw-board:focus { outline: none; }
.cw-board:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }

.cw-cell {
  position: relative;
  background: var(--surface);
  display: grid; place-items: center;
  font-family: var(--font-body);
  font-weight: 600;
  font-size: calc(var(--cell) * 0.5);
  color: var(--ink);
  text-transform: uppercase;
  cursor: pointer;
  transition: background-color .1s ease;
}
.cw-block { background: var(--block); cursor: default; }
.cw-num {
  position: absolute; top: 2px; left: 3px;
  font-size: calc(var(--cell) * 0.26); font-weight: 500;
  color: var(--muted); line-height: 1; pointer-events: none;
}
.cw-letter { line-height: 1; }

.cw-in-word { background: var(--hl-soft); }
.cw-cursor { background: var(--hl-strong); box-shadow: inset 0 0 0 2px var(--sel); }

.cw-correct .cw-letter { color: var(--success); }
.cw-revealed .cw-letter { color: var(--sel-ink); font-style: italic; }
.cw-wrong::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(135deg, transparent 62%, var(--danger) 62%, var(--danger) 76%, transparent 76%);
  pointer-events: none;
}

/* palabra recien completada: destello suave que recorre la palabra */
.cw-wordflash {
  animation: cw-wordflash .55s ease both;
  animation-delay: calc(var(--flash-i, 0) * 32ms);
}
@keyframes cw-wordflash {
  0%   { background: var(--surface); }
  35%  { background: color-mix(in srgb, var(--sel) 30%, var(--surface)); }
  100% { background: var(--surface); }
}
.cw-in-word.cw-wordflash { background: color-mix(in srgb, var(--sel) 16%, var(--surface)); }

/* ---------- momento de resolver ---------- */
.cw-board-wrap { overflow: hidden; border-radius: 5px; }
.cw-sweep {
  position: absolute; top: 0; bottom: 0; left: 0; width: 42%;
  pointer-events: none; opacity: 0;
  background: linear-gradient(90deg,
    transparent 0%,
    color-mix(in srgb, var(--sel) 8%, transparent) 35%,
    color-mix(in srgb, var(--sel) 42%, transparent) 50%,
    color-mix(in srgb, var(--sel) 8%, transparent) 65%,
    transparent 100%);
  mix-blend-mode: screen;
}
.cw.cw-sweeping .cw-sweep {
  animation: cw-sweep 1.15s cubic-bezier(.4, 0, .2, 1) forwards;
}
@keyframes cw-sweep {
  0%   { opacity: 0; transform: translateX(-115%); }
  12%  { opacity: 1; }
  88%  { opacity: 1; }
  100% { opacity: 0; transform: translateX(238%); }
}

/* las letras "asientan" a medida que pasa el barrido (escalonado por columna) */
.cw-done .cw-cell:not(.cw-block) {
  animation: cw-settle .5s cubic-bezier(.2, .9, .3, 1.1) both;
  animation-delay: calc(var(--col, 0) * 44ms + 180ms);
}
.cw-done .cw-letter { color: var(--success); }
.cw-done.cw-done-final .cw-cell:not(.cw-block) { animation: none; }
@keyframes cw-settle {
  0%   { transform: scale(1); }
  45%  { transform: scale(1.08); }
  100% { transform: scale(1); }
}

.cw-paused .cw-board { filter: blur(8px); pointer-events: none; }

/* ---------- teclado en pantalla ---------- */
.cw-keyboard {
  display: none; flex-direction: column; gap: 6px;
  background: var(--paper-2); border: 1px solid var(--line); border-radius: 12px; padding: 7px;
}
.cw-keyboard.is-visible { display: flex; }
.cw-kb-row { display: flex; gap: 5px; justify-content: center; }
.cw-key {
  flex: 1 1 0; min-width: 0; max-width: 44px; height: 44px;
  font: inherit; font-size: 1rem; font-weight: 600;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line-strong); border-radius: 8px; cursor: pointer;
  display: grid; place-items: center; touch-action: manipulation;
  transition: background .1s, transform .06s;
}
.cw-key:active { background: var(--sel-weak); color: var(--sel-ink); transform: translateY(1px); }
.cw-key-wide { flex: 1.6 1 0; max-width: 64px; font-size: 1.15rem; }

/* crucigrama silabico: boton explicito para confirmar la silaba y avanzar
   (no hay auto-avance al escribir, a diferencia del crucigrama clasico) */
.cw-key-confirm {
  flex: 1 1 auto; max-width: none; width: 100%; height: 44px;
  font-size: .95rem; font-weight: 600; letter-spacing: .01em;
  background: var(--sel-weak); color: var(--sel-ink);
  border: 1px solid var(--sel); border-radius: 8px; cursor: pointer;
  touch-action: manipulation;
}
.cw-key-confirm:active { transform: translateY(1px); }

/* ---------- pistas ---------- */
.cw-clues { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; align-content: start; }
.cw-clue-col {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  display: flex; flex-direction: column; min-height: 0; overflow: hidden;
}
.cw-clue-title {
  font-family: var(--font-ui); font-size: .72rem; font-weight: 500; text-transform: uppercase;
  letter-spacing: .12em; color: var(--muted); margin: 0; padding: .8rem 1rem .5rem;
  border-bottom: 1px solid var(--line);
}
.cw-clue-list { list-style: none; margin: 0; padding: .25rem; overflow-y: auto; scrollbar-width: thin; }
.cw-clue {
  display: flex; gap: .55rem; padding: .5rem .7rem; cursor: pointer; font-size: .92rem; line-height: 1.35;
  border-radius: 8px; transition: background .1s;
}
.cw-clue:hover { background: var(--paper-2); }
.cw-clue-n { font-weight: 700; color: var(--muted); flex: none; min-width: 1.7ch; text-align: right; font-variant-numeric: tabular-nums; }
.cw-clue.cw-active { background: var(--sel-weak); }
.cw-clue.cw-active .cw-clue-n, .cw-clue.cw-active .cw-clue-t { color: var(--sel-ink); }
.cw-clue.cw-crossing { background: color-mix(in srgb, var(--sel-weak) 50%, transparent); }
.cw-clue.is-done .cw-clue-t { color: var(--muted); text-decoration: line-through; text-decoration-color: color-mix(in srgb, var(--muted) 50%, transparent); }

.cw-hidden-input {
  position: absolute; opacity: 0; width: 1px; height: 1px;
  left: 50%; top: 45%; font-size: 16px; border: 0; padding: 0;
}

/* ============================================================
   RESPONSIVE
   ============================================================ */

/* --- escritorio ancho: pistas en dos columnas, tablero fija por altura --- */
@media (min-width: 1001px) {
  .cw { --cell: clamp(30px, min(4.4vw, 6.6vh), 58px); }
  .cw-clues { position: sticky; top: 1rem; max-height: calc(100vh - 2rem); }
  .cw-clue-list { max-height: calc(100vh - 12rem); }
}

/* --- tablet horizontal --- */
/* tablet: tablero arriba (centrado) y pistas debajo en dos columnas —
   con la barra lateral en riel, 11×11 y 13×13 no caben al lado de las pistas */
@media (min-width: 701px) and (max-width: 1000px) {
  .cw { --cell: clamp(30px, min(5.6vw, 7vh), 52px); }
  .cw-layout { grid-template-columns: minmax(0, 1fr); }
  .cw-left { align-items: center; }
  .cw-left > * { width: 100%; }
  .cw-board-wrap { align-self: stretch; display: flex; justify-content: center; }
  .cw-clues { grid-template-columns: 1fr 1fr; }
  .cw-clue-list { max-height: 34vh; }
}

/* --- tablet vertical y movil: una sola columna, teclado abajo --- */
@media (max-width: 700px) {
  .play-body .page { padding-bottom: .5rem; }
  .play-shell { gap: .6rem; }
  .play-title { font-size: 1.05rem; }
  .play-timer { font-size: 1.25rem; }
  .cw-layout { grid-template-columns: 1fr; gap: .7rem; }
  .cw { --cell: min(calc((100vw - 1.6rem) / var(--cols)), calc((92vh - 20rem) / var(--rows))); }
  .cw-board { margin: 0 auto; }
  .cw-board-wrap { align-self: stretch; display: flex; justify-content: center; }
  .cw-clues { grid-template-columns: 1fr; gap: .7rem; order: 3; }
  .cw-clue-list { max-height: 46vh; }
  .cw-keyboard.is-visible { position: sticky; bottom: .3rem; z-index: 15; }
  .tool-status { flex-basis: 100%; text-align: left; padding-left: .3rem; order: 5; }
}

@media (max-width: 380px) {
  .cw-key { height: 40px; font-size: .92rem; }
}

/* ---------- capas (pausa, confirmación, ayuda, victoria, error) ---------- */
.cw-overlay {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center; padding: 20px;
  background: color-mix(in srgb, var(--bg) 55%, rgba(10,11,12,.55));
  -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
  opacity: 0; transition: opacity .22s ease;
}
.cw-overlay.is-open, .cw-overlay:not([hidden]):not(.win-overlay) { opacity: 1; }
.cw-overlay-card {
  width: 100%; max-width: 420px; max-height: calc(100dvh - 40px); overflow-y: auto;
  padding: clamp(24px, 5vw, 36px); border-radius: var(--r-xl); text-align: center;
  background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow-lg);
  animation: card-in .28s var(--ease);
}
.cw-overlay-card:focus { outline: none; }
@keyframes card-in { from { opacity: 0; transform: translateY(10px) scale(.985); } }
.cw-overlay-card h2 { font-family: var(--font-display); font-weight: 400; font-size: clamp(1.9rem, 1.5rem + 1.6vw, 2.4rem); letter-spacing: -.01em; line-height: 1.05; margin: 0 0 10px; }
.cw-overlay-card p { color: var(--ink-2); margin: 0 0 8px; }
.cw-overlay-card .eyebrow, .cw-overlay-card .overline { margin-bottom: 10px; }
.win-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin-top: 22px; }
.win-actions .btn { flex: 1 1 auto; }

.help-card { text-align: left; max-width: 460px; }
.help-head { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.help-head h2 { margin: 0; font-size: 1.9rem; }
.help-card .hub-rules { margin-top: 12px; }
.help-card .win-actions { justify-content: flex-end; }
.help-card .win-actions .btn { flex: 0 0 auto; }

/* ---------- victoria ---------- */
.win-overlay .win-card { transform: translateY(12px) scale(.985); opacity: 0; transition: transform .45s var(--ease), opacity .35s ease; animation: none; }
.win-overlay.is-open .win-card { transform: none; opacity: 1; }
.win-card { max-width: 440px; }
.win-card h2 { font-size: clamp(2.4rem, 2rem + 2vw, 3.1rem); margin-bottom: 4px; }
.win-badges { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; margin: 10px 0 0; }
.win-badges:empty { display: none; }
.win-badge {
  display: inline-flex; align-items: center; height: 24px; padding: 0 10px; border-radius: var(--r-pill);
  font-size: var(--t-xs); font-weight: 500; background: var(--success-weak); color: var(--success);
}
.win-badge-line { background: var(--surface-2); color: var(--ink-2); }
.win-badge-gold { background: var(--warn-weak); color: var(--warn); }
.win-thumb { margin: 18px auto 0; width: min(200px, 60%); }
.win-thumb:empty { display: none; }
.win-thumb canvas { display: block; width: 100%; height: auto; border-radius: 10px; border: 1px solid var(--line); }
.win-stats {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(80px, 1fr)); gap: 1px; margin: 22px 0 6px;
  background: var(--line); border: 1px solid var(--line); border-radius: var(--r-md); overflow: hidden;
}
.win-stats:empty { display: none; }
.win-stat { background: var(--surface); padding: 12px 6px; }
.win-stat-v { display: block; font-family: var(--font-num); font-size: 1.25rem; font-weight: 500; letter-spacing: -.02em; color: var(--ink); font-variant-numeric: tabular-nums; }
.win-stat-l { display: block; margin-top: 2px; font-size: .68rem; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
.win-compare { font-size: var(--t-sm); color: var(--muted); margin: 10px 0 0; }
.win-compare:empty { display: none; }
.win-next { margin-top: 18px; }
.win-next-link {
  display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 2px 12px;
  padding: 14px 16px; border-radius: var(--r-md); text-align: left; text-decoration: none;
  background: var(--ink); color: var(--on-ink);
  transition: background var(--dur);
}
.win-next-link:hover { background: color-mix(in srgb, var(--ink) 86%, var(--bg)); }
.win-next-k { grid-column: 1; font-size: var(--t-xs); opacity: .7; }
.win-next-t { grid-column: 1; font-weight: 600; }
.win-next-link svg { grid-column: 2; grid-row: 1 / span 2; width: 18px; height: 18px; }

/* interruptores dentro del menú "Más" (sonido, marcar errores al escribir) */
.fx-item .fx-state {
  margin-left: auto; width: 30px; height: 18px; border-radius: 999px; background: var(--surface-3);
  position: relative; flex: none; transition: background var(--dur);
}
.fx-item .fx-state::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%;
  background: var(--surface); box-shadow: var(--shadow-sm); transition: transform var(--dur) var(--ease);
}
.fx-item[aria-pressed="true"] .fx-state { background: var(--ink); }
.fx-item[aria-pressed="true"] .fx-state::after { transform: translateX(12px); }
.fx-item[aria-pressed="true"] svg { color: var(--ink); }

@media (prefers-reduced-motion: reduce) {
  .cw-done .cw-cell:not(.cw-block), .cw-overlay-card, .win-card, .win-overlay,
  .tool-group.open .tool-pop, .cw-sweep, .cw-wordflash { animation: none !important; transition: none !important; }
  .win-overlay.is-open .win-card { transform: none; opacity: 1; }
}
