/* ==========================================================================
   Sudoku — foglio di stile. Mobile first: le regole base valgono per il
   telefono, i @media più avanti aggiungono il layout per schermi larghi.
   ========================================================================== */

:root {
  color-scheme: light dark;

  --bg: #eef1f7;
  --bg-glow: #dfe5f3;
  --surface: #ffffff;
  --surface-2: #f5f7fb;
  --surface-3: #eaeef6;
  --text: #1a1d27;
  --text-soft: #5c6377;
  --text-faint: #8b91a3;
  --border: #dde2ed;
  --line: #c8d0e0;
  --line-strong: #7d879e;

  --accent: #5457d6;
  --accent-soft: #e8e9fc;
  --accent-text: #ffffff;
  --given: #1a1d27;
  --user: #2f6bd8;
  --danger: #d63b52;
  --danger-soft: #fdecef;
  --good: #16916b;
  --good-soft: #e4f6ef;

  --shadow-sm: 0 1px 2px rgba(20, 25, 45, .06), 0 1px 3px rgba(20, 25, 45, .05);
  --shadow-md: 0 4px 12px rgba(20, 25, 45, .07), 0 1px 3px rgba(20, 25, 45, .05);
  --shadow-lg: 0 12px 34px rgba(20, 25, 45, .14), 0 3px 10px rgba(20, 25, 45, .08);

  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 18px;

  --n: 9;
  --cell: 34px;
  --nc: 3;
  --vfs: .54;
}

/* Tema scuro: prima quando lo chiede il sistema (a meno che l'utente non
   abbia forzato il chiaro), poi quando lo forza il pulsante. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1017;
    --bg-glow: #171b28;
    --surface: #171a24;
    --surface-2: #1e2230;
    --surface-3: #262b3b;
    --text: #e8eaf2;
    --text-soft: #a2a9bd;
    --text-faint: #737b91;
    --border: #2a2f3f;
    --line: #414a63;
    --line-strong: #737d99;

    --accent: #7d80f5;
    --accent-soft: #262a4d;
    --accent-text: #0e1017;
    --given: #e8eaf2;
    --user: #6fa8ff;
    --danger: #ff6b81;
    --danger-soft: #3a1f28;
    --good: #3ed6a5;
    --good-soft: #12332a;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-md: 0 4px 14px rgba(0, 0, 0, .45);
    --shadow-lg: 0 14px 40px rgba(0, 0, 0, .6);
  }
}

:root[data-theme="dark"] {
  --bg: #0e1017;
  --bg-glow: #171b28;
  --surface: #171a24;
  --surface-2: #1e2230;
  --surface-3: #262b3b;
  --text: #e8eaf2;
  --text-soft: #a2a9bd;
  --text-faint: #737b91;
  --border: #2a2f3f;
  --line: #414a63;
  --line-strong: #737d99;

  --accent: #7d80f5;
  --accent-soft: #262a4d;
  --accent-text: #0e1017;
  --given: #e8eaf2;
  --user: #6fa8ff;
  --danger: #ff6b81;
  --danger-soft: #3a1f28;
  --good: #3ed6a5;
  --good-soft: #12332a;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
  --shadow-md: 0 4px 14px rgba(0, 0, 0, .45);
  --shadow-lg: 0 14px 40px rgba(0, 0, 0, .6);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background:
    radial-gradient(1100px 620px at 50% -8%, var(--bg-glow), transparent 70%),
    var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

button { font: inherit; color: inherit; }

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.015em; }

/* ---------------------------------------------------------------- layout */

.app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(env(safe-area-inset-bottom, 0px));
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
}

.brand-mark {
  width: 30px;
  height: 30px;
  flex: none;
  border-radius: 9px;
  background: linear-gradient(140deg, var(--accent), color-mix(in srgb, var(--accent) 55%, #12b7a4));
  display: grid;
  place-items: center;
  color: #fff;
  font-size: 15px;
  box-shadow: var(--shadow-sm);
}

.brand h1 { font-size: 17px; }

.brand .sub {
  display: none;
  font-size: 12px;
  color: var(--text-faint);
}

.topbar .spacer { flex: 1; }

.status-strip {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 12px 2px;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 12.5px;
  font-weight: 550;
  color: var(--text-soft);
  white-space: nowrap;
  box-shadow: var(--shadow-sm);
}

.chip b { color: var(--text); font-weight: 650; }
.chip.accent { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.chip.accent b { color: var(--accent); }
.chip .dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }

.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 8px 12px 12px;
  min-width: 0;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
}

/* ----------------------------------------------------------------- board */

.board-card {
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  position: relative;
}

.board-scroll {
  overflow: auto;
  display: flex;
  justify-content: center;
  padding: 2px;
  overscroll-behavior: contain;
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--n), var(--cell));
  border-top: 2px solid var(--line-strong);
  border-left: 2px solid var(--line-strong);
  border-radius: 3px;
  background: var(--surface);
  flex: none;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.cell {
  position: relative;
  width: var(--cell);
  height: var(--cell);
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  display: grid;
  place-items: center;
  cursor: pointer;
  background: var(--surface);
  transition: background-color .1s ease;
}

.cell.bx-r { border-right: 2px solid var(--line-strong); }
.cell.bx-b { border-bottom: 2px solid var(--line-strong); }

.cell .v {
  font-size: calc(var(--cell) * var(--vfs));
  font-weight: 600;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  color: var(--user);
}

.cell.given { background: var(--surface-2); }
.cell.given .v { color: var(--given); font-weight: 700; }

/* le annotazioni: numeri piccoli disposti sempre nella stessa casella */
.cell .notes {
  position: absolute;
  inset: 1px;
  display: grid;
  grid-template-columns: repeat(var(--nc), 1fr);
  grid-auto-rows: 1fr;
  pointer-events: none;
}

.cell .notes i {
  font-style: normal;
  font-size: max(5px, calc(var(--cell) / var(--nc) * .82));
  line-height: 1;
  display: grid;
  place-items: center;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

.cell .notes i.hot { color: var(--accent); font-weight: 700; }

/* evidenziazioni, dalla più debole alla più forte */
.cell.peer { background: var(--surface-3); }
.cell.given.peer { background: color-mix(in srgb, var(--surface-3) 70%, var(--surface-2)); }
.cell.same { background: var(--accent-soft); }
.cell.same .v { color: var(--accent); }
.cell.bad .v { color: var(--danger); }
.cell.bad { background: var(--danger-soft); }
.cell.sel { background: var(--accent); box-shadow: inset 0 0 0 2px var(--accent); }
.cell.sel .v { color: var(--accent-text); }
.cell.sel .notes i { color: color-mix(in srgb, var(--accent-text) 75%, transparent); }

@keyframes flash {
  0%, 100% { background: var(--surface); }
  35%      { background: var(--good); }
}
.cell.flash { animation: flash .85s ease 2; }

.board.done .cell { background: var(--good-soft); transition: background-color .5s ease; }
.board.done .cell .v { color: var(--good); }

@media (hover: hover) {
  .cell:hover { background: var(--surface-3); }
  .cell.sel:hover { background: var(--accent); }
}

/* --------------------------------------------------------------- tastiera */

.pad {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(clamp(38px, 10.5vw, 54px), 1fr));
  gap: 6px;
}

.key {
  position: relative;
  aspect-ratio: 1;
  min-height: 42px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: var(--r-md);
  font-size: clamp(15px, 4.6vw, 20px);
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform .08s ease, background-color .12s ease, border-color .12s ease;
  touch-action: manipulation;
}

.key:active { transform: scale(.94); }
.key.hot { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.key.spent { opacity: .38; }

.key .left {
  position: absolute;
  right: 3px;
  bottom: 1px;
  font-size: 9.5px;
  font-weight: 600;
  color: var(--text-faint);
  line-height: 1;
}
.key.hot .left { color: color-mix(in srgb, var(--accent-text) 80%, transparent); }

/* ------------------------------------------------------------- strumenti */

.tools {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

.tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 8px 2px;
  min-height: 54px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: var(--r-md);
  cursor: pointer;
  font-size: 10.5px;
  font-weight: 600;
  color: var(--text-soft);
  transition: transform .08s ease, background-color .12s ease;
  touch-action: manipulation;
}

.tool:active { transform: scale(.95); }
.tool .ico { font-size: 17px; line-height: 1; }
.tool.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.tool:disabled { opacity: .35; cursor: default; }
.tool .badge {
  position: absolute;
  transform: translate(18px, -16px);
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 999px;
  font-size: 9px;
  padding: 0 4px;
}

.btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: var(--r-md);
  padding: 9px 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: transform .08s ease, background-color .12s ease;
  touch-action: manipulation;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; }

.icon-btn {
  width: 36px;
  height: 36px;
  flex: none;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  display: grid;
  place-items: center;
  font-size: 16px;
  cursor: pointer;
  touch-action: manipulation;
}

/* ------------------------------------------------------------- messaggio */

.note-bar {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  padding: 10px 12px;
  font-size: 13px;
  color: var(--text-soft);
  line-height: 1.4;
}
.note-bar .ico { flex: none; font-size: 15px; line-height: 1.3; }
.note-bar.warn { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.note-bar.ok { background: var(--good-soft); color: var(--good); border-color: transparent; }
.note-bar b { color: inherit; }

/* --------------------------------------------------------- pannello setup */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 20, .45);
  backdrop-filter: blur(2px);
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease;
}
body.sheet-open .sheet-backdrop { opacity: 1; pointer-events: auto; }

.setup {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  visibility: hidden;
  max-height: 88vh;
  overflow-y: auto;
  border-radius: var(--r-lg) var(--r-lg) 0 0;
  padding: 14px 14px calc(18px + env(safe-area-inset-bottom, 0px));
  transform: translateY(102%);
  transition: transform .26s cubic-bezier(.32, .72, 0, 1);
  box-shadow: var(--shadow-lg);
}
body.sheet-open .setup { transform: translateY(0); visibility: visible; }

.setup .grab {
  width: 38px;
  height: 4px;
  border-radius: 99px;
  background: var(--border);
  margin: 0 auto 12px;
}

.field { margin-bottom: 14px; }

.field > label {
  display: block;
  font-size: 12px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-faint);
  margin-bottom: 7px;
}

.seg {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 4px;
  padding: 4px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
}

.seg button {
  border: 0;
  background: transparent;
  border-radius: var(--r-sm);
  padding: 9px 6px;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text-soft);
  cursor: pointer;
  transition: background-color .14s ease, color .14s ease;
  touch-action: manipulation;
}
.seg button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}
.seg.accent button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); }

.size-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 6px;
}

.size-opt {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: var(--r-md);
  padding: 8px 4px;
  cursor: pointer;
  text-align: center;
  line-height: 1.15;
  transition: background-color .12s ease, border-color .12s ease;
  touch-action: manipulation;
}
.size-opt .big { display: block; font-size: 14px; font-weight: 700; }
.size-opt .small { display: block; font-size: 10px; color: var(--text-faint); }
.size-opt[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.size-opt[aria-pressed="true"] .small { color: color-mix(in srgb, var(--accent-text) 78%, transparent); }

.level-help {
  font-size: 12.5px;
  color: var(--text-faint);
  margin-top: 7px;
  min-height: 2.4em;
}

/* ----------------------------------------------------------- generazione */

.overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: none;
  place-items: center;
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  backdrop-filter: blur(3px);
  border-radius: var(--r-lg);
  text-align: center;
  padding: 20px;
}
.overlay.on { display: grid; }
.overlay .title { font-weight: 650; margin-bottom: 4px; }
.overlay .sub { font-size: 12.5px; color: var(--text-faint); }

.bar {
  width: min(240px, 70vw);
  height: 6px;
  border-radius: 99px;
  background: var(--surface-3);
  overflow: hidden;
  margin: 12px auto 0;
}
.bar > span {
  display: block;
  height: 100%;
  width: 0;
  border-radius: 99px;
  background: var(--accent);
  transition: width .18s ease;
}

/* --------------------------------------------------------------- vittoria */

.win {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: none;
  place-items: center;
  padding: 22px;
  background: rgba(10, 12, 20, .5);
  backdrop-filter: blur(3px);
}
.win.on { display: grid; }

.win-card {
  background: var(--surface);
  border-radius: var(--r-lg);
  padding: 26px 22px;
  text-align: center;
  max-width: 340px;
  width: 100%;
  box-shadow: var(--shadow-lg);
  animation: pop .32s cubic-bezier(.2, 1.3, .4, 1);
}
@keyframes pop { from { transform: scale(.86); opacity: 0; } }

.win-card .mark { font-size: 40px; line-height: 1; }
.win-card h2 { font-size: 21px; margin: 8px 0 4px; }
.win-card p { margin: 0 0 16px; color: var(--text-soft); font-size: 13.5px; }
.win-stats {
  display: flex;
  justify-content: center;
  gap: 18px;
  margin-bottom: 18px;
}
.win-stats div { font-size: 12px; color: var(--text-faint); }
.win-stats b { display: block; font-size: 18px; color: var(--text); font-variant-numeric: tabular-nums; }

.sr {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ======================================================== schermi larghi */

@media (min-width: 760px) {
  body { font-size: 15.5px; }

  .brand h1 { font-size: 19px; }
  .brand .sub { display: block; }

  .topbar { padding: 12px 20px; }
  .status-strip { padding: 12px 20px 0; }

  .stage {
    flex-direction: row;
    align-items: flex-start;
    gap: 16px;
    padding: 12px 20px 24px;
    max-width: 1180px;
    width: 100%;
    margin: 0 auto;
  }

  .board-card { flex: 1; min-width: 0; padding: 14px; }

  .side {
    width: 310px;
    flex: none;
    display: flex;
    flex-direction: column;
    gap: 12px;
    position: sticky;
    top: 76px;
  }

  /* Su schermo largo il cassetto diventa una finestra al centro: lo stesso
     elemento, solo posizionato diversamente. */
  .setup {
    left: 50%;
    right: auto;
    top: 50%;
    bottom: auto;
    width: min(560px, 92vw);
    max-height: 86vh;
    border-radius: var(--r-lg);
    padding: 20px;
    transform: translate(-50%, -46%) scale(.97);
    opacity: 0;
    transition: transform .2s cubic-bezier(.2, .9, .3, 1), opacity .18s ease;
  }
  body.sheet-open .setup { transform: translate(-50%, -50%) scale(1); opacity: 1; visibility: visible; }
  .setup .grab { display: none; }
  .only-mobile { display: none !important; }

  .tools { grid-template-columns: repeat(5, 1fr); }
  .key { min-height: 38px; }
  .pad { grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); }
}

@media (max-width: 759px) {
  .only-desktop { display: none !important; }

  /* Su telefono l'app occupa esattamente lo schermo e la griglia si prende
     tutto lo spazio che avanza fra intestazione e tastierina: nessun calcolo
     a mano, ci pensa il flex. dvh tiene conto delle barre del browser. */
  .app { height: 100vh; height: 100dvh; }
  .stage { flex: 1 1 auto; min-height: 0; }
  .board-card { flex: 1 1 auto; min-height: 0; }
  .board-scroll { flex: 1 1 auto; min-height: 0; }
  .side { flex: 0 0 auto; }

  /* la tastiera resta a portata di pollice in fondo allo schermo */
  .side {
    position: sticky;
    bottom: 0;
    z-index: 25;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom, 0px));
    margin: 0 -12px -12px;
    background: color-mix(in srgb, var(--bg) 92%, transparent);
    backdrop-filter: blur(12px);
    border-top: 1px solid var(--border);
  }
  .side .panel { box-shadow: none; border: 0; background: transparent; }
  .pad { gap: 5px; }
  .tool { min-height: 42px; padding: 5px 2px; font-size: 9.5px; gap: 1px; }
  .tool .ico { font-size: 15px; }

  /* Ogni pixel tolto qui è un pixel dato alla griglia: le informazioni di
     stato restano su una riga sola (scorrevole) e il messaggio su due. */
  .status-strip { flex-wrap: nowrap; overflow-x: auto; padding: 6px 12px 0; }
  .status-strip::-webkit-scrollbar { display: none; }
  .chip { font-size: 12px; padding: 3px 9px; }
  .note-bar { padding: 7px 10px; font-size: 12.5px; }
  .note-bar > span:last-child {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  /* La barra comandi non può prendersi più di metà schermo: con 25 tasti
     resterebbe una fessura per la griglia. Oltre quel limite è la tastierina
     a scorrere, mentre strumenti e messaggio restano sempre visibili. */
  .side { flex: 0 0 auto; min-height: 0; max-height: 54vh; }
  .side #padPanel { flex: 1 1 auto; min-height: 46px; overflow-y: auto; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    transition-duration: .01ms !important;
  }
}
