.sudoku {
  --n: 9;
  --size: min(94vw, 480px, calc(100vh - 300px));
  --cell: calc(var(--size) / var(--n));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 4px 12px 16px;
  min-height: calc(100vh - 80px);
}
@supports (height: 100dvh) {
  .sudoku { --size: min(94vw, 480px, calc(100dvh - 300px)); min-height: calc(100dvh - 80px); }
}
@media (min-height: 760px) {
  .pad button { min-height: 64px; }
}
@supports not (height: 100dvh) {
  .sudoku { --size: min(94vw, 480px, calc(var(--vh) * 100 - 300px)); }
}

.stats {
  width: var(--size);
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--muted);
  font-size: 15px;
  font-weight: 600;
}
.stats b { color: var(--text); font-variant-numeric: tabular-nums; margin-left: 4px; }
.stats .bump { display: inline-block; animation: bump-red .4s; }
@keyframes bump-red { 40% { transform: scale(1.5); color: var(--danger); } }

/* ---------- tablero ---------- */
.board {
  position: relative;
  width: var(--size);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
  background: var(--surface);
  border: 3px solid var(--line-strong);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  touch-action: manipulation;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  font-size: calc(var(--cell) * .58);
  font-weight: 650;
  line-height: 1;
  cursor: pointer;
  transition: background-color .15s;
}
.cell.alt { background: color-mix(in srgb, var(--surface-2) 45%, var(--surface)); }
.cell.edge-x { border-right: 0; }
.cell.edge-y { border-bottom: 0; }
.cell.box-x { border-right: 2.5px solid var(--line-strong); }
.cell.box-y { border-bottom: 2.5px solid var(--line-strong); }

.cell.peer { background: var(--surface-2); }
.cell.same { background: var(--blue-soft); }
.cell.selected { background: var(--accent-soft); box-shadow: inset 0 0 0 2.5px var(--accent); z-index: 1; }
.cell.given { color: var(--text); font-weight: 800; }
.cell.user { color: var(--blue); }
.cell.wrong { color: var(--danger); background: var(--danger-soft); }
.cell.selected.wrong { box-shadow: inset 0 0 0 2.5px var(--danger); }

/* Dibujos */
.sym { width: 80%; height: 80%; display: block; }
.cell.given .sym { filter: drop-shadow(0 1.5px 0 rgb(0 0 0 / .18)); }
.cell.user .sym { width: 74%; height: 74%; }
.cell.wrong .sym { opacity: .55; }
.cell.wrong::after {
  content: ""; position: absolute; right: 6%; bottom: 6%;
  width: 26%; height: 26%; border-radius: 50%;
  background: var(--danger);
  box-shadow: 0 0 0 2px var(--surface);
}
.theme-numeros .cell.wrong::after { display: none; }

.cell.pop .sym, .cell.pop .num { animation: cell-pop .35s cubic-bezier(.2, 1.8, .4, 1); }
.cell.nope { animation: cell-nope .35s; }
.cell.glow { animation: cell-glow .9s ease-out; animation-delay: var(--gd, 0s); }

@keyframes cell-pop { from { transform: scale(.3) rotate(-12deg); } }
@keyframes cell-nope { 20%, 60% { transform: translateX(-10%); } 40%, 80% { transform: translateX(10%); } }
@keyframes cell-glow {
  0% { background: var(--surface); }
  25% { background: #ffe7a3; transform: scale(.88); }
  60% { transform: scale(1.04); }
  100% { transform: none; }
}

.notes {
  position: absolute; inset: 2px;
  display: grid;
  grid-template-columns: repeat(var(--nb), 1fr);
  font-size: calc(var(--cell) / var(--nb) * .72);
  font-weight: 700;
  color: var(--muted);
}
.notes span { display: grid; place-items: center; }
.notes span.hit { color: var(--blue); background: var(--blue-soft); border-radius: 4px; }
.notes .sym { width: 86%; height: 86%; }

.board.won .cell { animation: wave .7s ease-in-out both; animation-delay: var(--d); }
@keyframes wave { 50% { transform: scale(.78) rotate(6deg); background: #ffe7a3; } }

/* ---------- herramientas ---------- */
.tools {
  width: var(--size);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}
.tools button {
  border: 0; background: none; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 6px 0;
  border-radius: 12px;
  color: var(--muted);
  font-size: 13px; font-weight: 700;
}
.tools button:active { background: var(--surface-2); transform: scale(.95); }
.tools svg {
  width: 26px; height: 26px;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
.tools .badge {
  font-style: normal; font-size: 10px; font-weight: 800;
  padding: 1px 5px; border-radius: 6px;
  background: var(--surface-2); color: var(--muted);
}
.tools button[aria-pressed="true"] { color: var(--blue); }
.tools button[aria-pressed="true"] .badge { background: var(--blue); color: var(--surface); }

/* ---------- teclado ---------- */
.pad {
  width: var(--size);
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  gap: 5px;
}
.pad button {
  position: relative;
  border: 0; cursor: pointer;
  border-radius: 14px;
  padding: 8px 0 6px;
  min-height: 56px;
  background: var(--surface);
  box-shadow: 0 3px 0 var(--line), var(--shadow);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--blue);
  transition: transform .1s, opacity .3s;
}
.pad button:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--line), var(--shadow); }
.pad .num { font-size: clamp(22px, 7vw, 32px); font-weight: 700; line-height: 1.1; }
.pad .sym { width: min(40px, 82%); height: auto; aspect-ratio: 1; }
.pad small {
  font-size: 11px; color: var(--muted); font-weight: 700;
}
.pad button.active { background: var(--blue-soft); }
.pad button.complete { opacity: .28; box-shadow: none; }
.pad button.done-anim { animation: pad-done .6s cubic-bezier(.2, 1.6, .4, 1); }
@keyframes pad-done { 30% { transform: scale(1.3) rotate(-8deg); } }
.pad.notes-mode .num { font-size: clamp(16px, 5vw, 22px); color: var(--muted); }
.pad.notes-mode .sym { width: min(28px, 60%); opacity: .8; }
.pad.notes-mode button { box-shadow: 0 3px 0 var(--blue-soft), var(--shadow); outline: 1.5px dashed var(--blue); outline-offset: -4px; }

/* Con 4 o 6 símbolos los botones son grandes: mostrar en 2 filas no hace falta */
.sudoku[data-n="4"] .pad button, .sudoku[data-n="6"] .pad button { min-height: 66px; }

/* ---------- diálogos ---------- */
.trophy { font-size: 54px; animation: trophy 1s cubic-bezier(.2, 1.6, .4, 1); }
@keyframes trophy { 0% { transform: scale(.1) rotate(-40deg); } 60% { transform: scale(1.2) rotate(10deg); } }
.btn.link { background: none; box-shadow: none; color: var(--muted); }
dialog .label { margin: 14px 0 6px; font-size: 13px; font-weight: 800; text-transform: uppercase; letter-spacing: .06em; text-align: left; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.seg button {
  border: 2px solid var(--line); border-radius: 14px;
  background: var(--surface); padding: 9px 4px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 1px;
}
.seg button b { font-size: 16px; }
.seg button small { font-size: 11px; color: var(--muted); font-weight: 600; }
.seg button.on { border-color: var(--accent); background: var(--accent-soft); }
.themes { display: grid; gap: 8px; }
.theme-card {
  display: flex; align-items: center; gap: 12px;
  border: 2px solid var(--line); border-radius: 16px;
  background: var(--surface); padding: 8px 12px; cursor: pointer;
  text-align: left;
}
.theme-card.on { border-color: var(--accent); background: var(--accent-soft); }
.theme-card b { flex: 1; font-size: 16px; }
.theme-card .preview { display: flex; gap: 2px; }
.theme-card .preview > * { width: 34px; height: 34px; display: grid; place-items: center; }
.theme-card .preview .sym { width: 34px; height: 34px; }
.theme-card .preview .num { font-size: 24px; font-weight: 800; color: var(--blue); }

/* Pantallas anchas (compu): tablero y controles lado a lado */
@media (min-width: 820px) and (min-height: 560px) {
  .topbar { max-width: 880px; }
  .sudoku {
    --size: min(540px, calc(100vh - 120px));
    display: grid;
    grid-template-columns: var(--size) 270px;
    grid-template-areas: "stats ." "board side";
    justify-content: center;
    align-content: center;
    column-gap: 28px;
    align-items: start;
  }
  .stats { grid-area: stats; }
  .board { grid-area: board; }
  .tools, .pad { grid-column: 2; width: 270px; }
  .tools { grid-row: 2; align-self: start; }
  .pad { grid-row: 2; align-self: end; grid-template-columns: repeat(3, 1fr); gap: 9px; }
  .pad button { min-height: 76px; }
  .pad .num { font-size: 30px; }
  .pad .sym { width: 46px; }
}
@media (min-width: 820px) and (min-height: 560px) {
  @supports (height: 100dvh) {
    .sudoku { --size: min(540px, calc(100dvh - 120px)); }
  }
}
@media (prefers-reduced-motion: reduce) {
  .board.won .cell, .cell.pop .sym, .cell.pop .num, .cell.glow, .cell.nope { animation: none; }
}
