/* ===== Il Quaderno Enigmistico ===== */
:root {
  --paper: #fbf8f1;
  --card: #ffffff;
  --ink: #1b2430;
  --ink-soft: #55606e;
  --line: #d9d2c3;
  --brand: #1f3a5f;
  --brand-ink: #fbf8f1;
  --accent: #e8b10f;
  --accent-soft: #fdf1c7;
  --sel: #cfe3ff;
  --sel-strong: #8ab8f5;
  --ok: #2b8a3e;
  --ok-soft: #d9f2de;
  --bad: #c92a2a;
  --bad-soft: #ffe0e0;
  --cell: #ffffff;
  --cell-off: #ece6d9;
  --shadow: 0 1px 2px rgb(0 0 0 / .06), 0 4px 14px rgb(0 0 0 / .06);
  --shadow-lift: 0 2px 4px rgb(0 0 0 / .06), 0 12px 28px rgb(31 58 95 / .14);
  --mast: #1f3a5f;          /* testata (e barra del browser su mobile) */
  --mast-ink: #fbf8f1;
  --art: #fdf1c7;           /* fondo delle illustrazioni: i disegni hanno colori fissi */
  --grid-line: rgb(31 58 95 / .05);  /* quadretti del quaderno */
  --rule: #1b2430;          /* linee e bordi delle griglie di gioco */
  --radius: 12px;
  --serif: Georgia, "Iowan Old Style", "Palatino Linotype", "Book Antiqua", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

/* Tema scuro: scelto esplicitamente (data-theme="dark") oppure dal sistema se non c'è una scelta.
   I due blocchi devono restare identici. */
:root[data-theme="dark"] {
  --paper: #13171d; --card: #1c222a; --ink: #ece7dc; --ink-soft: #a3abb6; --line: #333b46;
  --brand: #9cc1ef; --brand-ink: #13171d; --accent: #f0c33c; --accent-soft: #3a321a;
  --sel: #233a57; --sel-strong: #3c6aa3; --ok: #69db7c; --ok-soft: #1e3a26; --bad: #ff8787; --bad-soft: #4a2222;
  --cell: #262d37; --cell-off: #0b0d11; --rule: #5d6875; --shadow: 0 1px 2px rgb(0 0 0 / .4);
  --shadow-lift: 0 2px 4px rgb(0 0 0 / .4), 0 12px 28px rgb(0 0 0 / .45);
  --mast: #172434; --mast-ink: #ece7dc; --art: #d9cca8; --grid-line: rgb(255 255 255 / .028);
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #13171d; --card: #1c222a; --ink: #ece7dc; --ink-soft: #a3abb6; --line: #333b46;
    --brand: #9cc1ef; --brand-ink: #13171d; --accent: #f0c33c; --accent-soft: #3a321a;
    --sel: #233a57; --sel-strong: #3c6aa3; --ok: #69db7c; --ok-soft: #1e3a26; --bad: #ff8787; --bad-soft: #4a2222;
    --cell: #262d37; --cell-off: #0b0d11; --rule: #5d6875; --shadow: 0 1px 2px rgb(0 0 0 / .4);
    --shadow-lift: 0 2px 4px rgb(0 0 0 / .4), 0 12px 28px rgb(0 0 0 / .45);
    --mast: #172434; --mast-ink: #ece7dc; --art: #d9cca8; --grid-line: rgb(255 255 255 / .028);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; min-height: 100vh; display: flex; flex-direction: column;
  background-color: var(--paper); color: var(--ink); font: 16px/1.5 var(--sans);
  /* carta a quadretti, come un quaderno */
  background-image: linear-gradient(var(--grid-line) 1px, transparent 1px), linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
  background-size: 24px 24px; background-position: -1px -1px;
}
a { color: var(--brand); }
button, input, select { font: inherit; color: inherit; }
img, svg { max-width: 100%; }
h1, h2, h3 { font-family: var(--serif); line-height: 1.2; margin: 0 0 .5em; }
.skip { position: absolute; left: -999px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.skip:focus { left: 8px; top: 8px; z-index: 99; background: var(--card); padding: 8px; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ----- Testata ----- */
.masthead { background: var(--mast); color: var(--mast-ink); border-bottom: 4px solid var(--accent); box-shadow: 0 2px 10px rgb(0 0 0 / .12); }
.masthead__inner {
  max-width: 1180px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap; justify-content: space-between;
}
.brand { display: flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; }
.brand img { border-radius: 8px; box-shadow: 0 0 0 2px rgb(255 255 255 / .25); }
.brand__text { display: flex; flex-direction: column; }
.brand__name { font: 700 1.3rem/1.1 var(--serif); letter-spacing: .01em; }
.brand__tag { font-size: .78rem; opacity: .8; }
.topnav { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.topnav a, .topnav button.linklike {
  color: inherit; text-decoration: none; padding: 6px 10px; border-radius: 8px; font-weight: 600; font-size: .95rem;
  background: none; border: 0; cursor: pointer;
}
.topnav a:hover, .topnav a.active, .topnav button.linklike:hover { background: rgb(255 255 255 / .14); }
.topnav a.active { box-shadow: inset 0 -3px 0 var(--accent); }
.topnav .theme-btn { display: inline-grid; place-items: center; width: 38px; height: 36px; padding: 0; margin-left: 4px; }
.theme-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.theme-btn svg .fill { fill: currentColor; stroke: none; }
#theme-slot { display: contents; }
@media (max-width: 560px) {
  .brand__tag { display: none; }
  .brand__name { font-size: 1.1rem; }
  .brand img { width: 32px; height: 32px; }
}

main { flex: 1; width: 100%; outline: none; }
.page { max-width: 1180px; margin: 0 auto; padding: 20px 16px 40px; }
.page--narrow { max-width: 460px; }
.footer { text-align: center; font-size: .85rem; color: var(--ink-soft); padding: 18px 16px 28px; border-top: 1px solid var(--line); background: var(--paper); }

/* ----- Elementi comuni ----- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 42px; padding: 8px 16px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--card); cursor: pointer; font-weight: 600; text-decoration: none; color: var(--ink);
  touch-action: manipulation; user-select: none; transition: border-color .15s, background-color .15s, transform .08s;
}
.btn:hover { border-color: var(--brand); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: default; }
.btn--primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn--accent { background: var(--accent); color: #1b2430; border-color: var(--accent); }
.btn--small { min-height: 34px; padding: 4px 10px; font-size: .9rem; }
.btn--ghost { background: transparent; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.muted { color: var(--ink-soft); }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.badge { display: inline-block; font-size: .75rem; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: var(--cell-off); color: var(--ink-soft); }
.badge--ok { background: var(--ok-soft); color: var(--ok); }
.badge--progress { background: var(--accent-soft); color: var(--ink); }
.dots { letter-spacing: 2px; color: var(--accent); font-size: .9rem; }
.dots .off { color: var(--line); }
.empty { text-align: center; padding: 40px 0; color: var(--ink-soft); }
.spinner { width: 34px; height: 34px; margin: 60px auto; border: 4px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ----- Home ----- */
.hero { margin: 4px 0 20px; }
.hero h1 { font-size: clamp(1.6rem, 4vw, 2.3rem); }
.hero h1::after { content: ""; display: block; width: 64px; height: 5px; margin-top: 10px; border-radius: 3px; background: var(--accent); }
.hero p { margin: 0; color: var(--ink-soft); max-width: 640px; }
.games { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 16px; }
.game-card { display: flex; flex-direction: column; text-decoration: none; color: inherit; overflow: hidden; transition: transform .18s, box-shadow .18s, border-color .18s; }
.game-card:hover, .game-card:focus-visible { transform: translateY(-3px); border-color: var(--brand); box-shadow: var(--shadow-lift); }
.game-card__art {
  aspect-ratio: 16 / 8; display: grid; place-items: center; border-bottom: 1px solid var(--line);
  background-color: var(--art);
  background-image: radial-gradient(rgb(31 58 95 / .09) 1.2px, transparent 1.4px);
  background-size: 14px 14px;
}
.game-card__art svg { width: 46%; height: auto; max-height: 86%; transition: transform .25s; }
.game-card:hover .game-card__art svg { transform: scale(1.05) rotate(-1.5deg); }
.game-card__body { padding: 14px 16px 16px; display: flex; flex-direction: column; gap: 4px; flex: 1; }
.game-card__body h2 { font-size: 1.25rem; margin: 0; }
.game-card__body p { margin: 0; color: var(--ink-soft); font-size: .93rem; flex: 1; }
.game-card__meta { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; font-size: .85rem; color: var(--ink-soft); }
.progressbar { height: 6px; border-radius: 6px; background: var(--cell-off); overflow: hidden; margin-top: 6px; }
.progressbar span { display: block; height: 100%; background: var(--ok); }

/* ----- Elenco puzzle ----- */
.crumbs { font-size: .9rem; margin-bottom: 10px; }
.crumbs a { text-decoration: none; }
.puzzles { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.puzzle-item { padding: 14px 16px; text-decoration: none; color: inherit; display: flex; flex-direction: column; gap: 6px; }
.puzzle-item { transition: transform .15s, box-shadow .15s, border-color .15s; }
.puzzle-item:hover, .puzzle-item:focus-visible { border-color: var(--brand); transform: translateY(-2px); box-shadow: var(--shadow-lift); }
.puzzle-item h3 { font-size: 1.08rem; margin: 0; }
.puzzle-item .row { justify-content: space-between; }
.filters { margin: 0 0 14px; }
.filters .btn[aria-pressed="true"] { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }

/* ----- Schermata di gioco ----- */
.play { max-width: 1180px; margin: 0 auto; padding: 12px 16px 32px; }
.play__head { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 8px 16px; justify-content: space-between; margin-bottom: 12px; }
/* Titolo e contatori sulla stessa riga se c'è posto, altrimenti i contatori vanno sotto. */
.play__title { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 14px; min-width: 0; flex: 1 1 260px; }
.play__crumb { display: flex; align-items: center; gap: 10px; flex-basis: 100%; }
.play__title h1 { font-size: clamp(1.2rem, 3vw, 1.6rem); margin: 0; }
.play__title a { font-size: .85rem; text-decoration: none; }
.stats { display: flex; gap: 6px; flex-wrap: wrap; }
.stat { display: inline-flex; align-items: baseline; gap: 6px; background: var(--card); border: 1px solid var(--line); border-radius: 99px; padding: 2px 12px; font-variant-numeric: tabular-nums; font-size: .95rem; font-weight: 700; }
.stat b { font-size: .68rem; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-soft); font-weight: 600; }
.toolbar { display: flex; flex-wrap: wrap; gap: 6px; }
.banner { padding: 10px 14px; border-radius: 10px; background: var(--accent-soft); margin-bottom: 12px; font-size: .93rem; }
.stage { position: relative; --avail-h: calc(100vh - 200px); }  /* --avail-h: lo ricalcola play.js */
.stage + div .banner { margin: 16px 0 0; }

/* ----- Modale e toast ----- */
.modal-backdrop { position: fixed; inset: 0; background: rgb(10 14 20 / .55); display: grid; place-items: center; padding: 16px; z-index: 50; animation: fade .15s; }
.modal { max-width: 420px; width: 100%; padding: 24px; text-align: center; max-height: 90vh; overflow: auto; }
.modal h2 { font-size: 1.6rem; }
.modal .score { font: 700 2.6rem/1 var(--serif); color: var(--brand); margin: 8px 0; }
.modal .row { justify-content: center; margin-top: 16px; }
@keyframes fade { from { opacity: 0; } }
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translate(-50%, 30px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--paper); padding: 10px 18px; border-radius: 10px; font-weight: 600; z-index: 60;
  transition: .2s; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ----- Form ----- */
.form { padding: 22px; display: flex; flex-direction: column; gap: 12px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-weight: 600; font-size: .92rem; }
.form input, .form select { padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--paper); font-weight: 400; font-size: 1rem; }
.form .error { color: var(--bad); font-size: .9rem; min-height: 1.2em; margin: 0; }
.tabs { display: flex; gap: 4px; margin-bottom: 12px; }
.tabs button { flex: 1; }
.tabs .btn[aria-pressed="true"] { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }

/* ----- Tabelle ----- */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: .93rem; }
table.data th, table.data td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
table.data th { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--ink-soft); }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }
.kpi { padding: 14px 16px; }
.kpi b { display: block; font: 700 1.8rem/1.1 var(--serif); color: var(--brand); }
.kpi span { font-size: .85rem; color: var(--ink-soft); }

/* ===== Tastiera a schermo ===== */
.kbd { display: flex; flex-direction: column; gap: 5px; margin-top: 10px; user-select: none; touch-action: manipulation; }
.kbd__row { display: flex; gap: 4px; justify-content: center; }
.kbd button {
  flex: 1 1 0; max-width: 48px; min-width: 0; height: 46px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--card); font-weight: 700; font-size: 1.05rem; cursor: pointer; padding: 0; touch-action: manipulation;
}
.kbd button:active { background: var(--sel); }
.kbd button.wide { flex: 1.6 1 0; max-width: 76px; font-size: .9rem; }
@media (pointer: coarse) and (min-width: 700px) {
  .kbd { gap: 6px; }
  .kbd__row { gap: 6px; }
  .kbd button { height: 54px; max-width: 60px; font-size: 1.2rem; }
  .kbd button.wide { max-width: 92px; font-size: 1.1rem; }
}

/* ===== Cruciverba ===== */
/* La misura dello schema (--cw-size) dipende da colonne, righe e spazio verticale disponibile:
   così il cruciverba sta intero nello schermo, qualunque sia la definizione selezionata. */
.cw {
  --n: 13; --rows: 13; --cw-extra: 72px;
  --cw-size: max(260px, min(calc(var(--n) * 58px), calc((var(--avail-h) - var(--cw-extra)) * var(--n) / var(--rows))));
  display: grid; gap: 8px 20px; align-items: start;
  grid-template-columns: minmax(0, var(--cw-size)) minmax(260px, 1fr);
  grid-template-areas: "clue lists" "grid lists";
}
.cw__board { display: contents; }
.cw .kbd { grid-area: kbd; margin-top: 0; }
.cw__clue { grid-area: clue; display: flex; align-items: center; gap: 6px; background: var(--sel); border-radius: 10px; padding: 6px; min-height: 52px; }
.cw__clue p { flex: 1; margin: 0; font-weight: 600; font-size: .98rem; }
.cw__clue p small { color: var(--ink-soft); font-weight: 700; margin-right: 6px; }
.cw__clue .btn { min-height: 38px; padding: 4px 10px; }
.cw__grid {
  grid-area: grid; container-type: inline-size;
  display: grid; grid-template-columns: repeat(var(--n), minmax(0, 1fr)); gap: 0;
  width: min(100%, var(--cw-size)); border: 2px solid var(--rule); background: var(--rule); user-select: none; touch-action: manipulation;
}
.cw__cell {
  position: relative; aspect-ratio: 1; background: var(--cell); border: 0; outline: 1px solid var(--rule); outline-offset: -.5px;
  display: grid; place-items: center; font: 700 clamp(.85rem, 3.6vw, 1.35rem)/1 var(--sans); text-transform: uppercase; cursor: pointer; padding: 0;
  font-size: calc(100cqi / var(--n) * .52);
}
.cw__cell.off { background: var(--cell-off); cursor: default; outline-color: var(--cell-off); }
.cw__cell .num { position: absolute; top: 1px; left: 2px; font-size: clamp(.45rem, 1.6vw, .62rem); font-size: max(.45rem, calc(100cqi / var(--n) * .22)); font-weight: 600; color: var(--ink-soft); }
.cw__cell.word { background: var(--sel); }
.cw__cell.cur { background: var(--sel-strong); }
.cw__cell.wrong { color: var(--bad); }
.cw__cell.wrong::after { content: ""; position: absolute; inset: 18% ; border-bottom: 2px solid var(--bad); transform: rotate(-35deg); }
.cw__cell.revealed { color: var(--brand); }
.cw__cell.good { color: var(--ok); }
.cw__lists { grid-area: lists; display: grid; grid-template-columns: 1fr 1fr; gap: 16px; max-height: max(240px, var(--avail-h)); overflow: auto; padding-right: 4px; }
/* Con la tastiera a schermo (tablet in orizzontale): schema a sinistra a tutta altezza,
   definizione, tastiera ed elenchi a destra. */
.cw.has-kbd { --cw-extra: 0px; grid-template-areas: "grid clue" "grid kbd" "grid lists"; grid-template-rows: auto auto 1fr; }
.cw.has-kbd .cw__lists { max-height: max(160px, calc(var(--avail-h) - 270px)); }
.cw__lists h3 { font-size: 1rem; text-transform: uppercase; letter-spacing: .05em; position: sticky; top: 0; background: var(--paper); padding: 4px 0; margin: 0; }
.cw__lists ol { list-style: none; margin: 0; padding: 0; }
.cw__lists li { padding: 5px 8px; border-radius: 8px; cursor: pointer; font-size: .93rem; display: flex; gap: 6px; }
.cw__lists li b { min-width: 1.6em; color: var(--ink-soft); }
.cw__lists li.active { background: var(--sel); }
.cw__lists li.done { color: var(--ink-soft); text-decoration: line-through; text-decoration-color: var(--line); }
@media (max-width: 900px) {
  .cw, .cw.has-kbd { grid-template-columns: minmax(0, 1fr); grid-template-areas: "clue" "grid" "kbd" "lists"; grid-template-rows: none; }
  .cw { --cw-extra: 80px; }
  .cw.has-kbd { --cw-extra: 280px; }
  .cw__grid { margin: 0 auto; }
  .cw__lists, .cw.has-kbd .cw__lists { max-height: none; }
}
/* Sul telefono lo schema usa tutta la larghezza: la tastiera resta agganciata in basso. */
@media (max-width: 600px) { .cw, .cw.has-kbd { --cw-size: calc(var(--n) * 58px); } }
@media (max-width: 520px) { .cw__lists { grid-template-columns: 1fr; } }

/* ===== Crucipuzzle ===== */
.ws {
  --n: 10; --rows: 10;
  --ws-size: max(280px, min(760px, calc((var(--avail-h) - 12px) * var(--n) / var(--rows))));
  display: grid; grid-template-columns: minmax(0, var(--ws-size)) minmax(220px, 1fr); gap: 20px; align-items: start;
}
.ws__board { position: relative; touch-action: none; user-select: none; -webkit-user-select: none; }
.ws__grid {
  container-type: inline-size; display: grid; grid-template-columns: repeat(var(--n), minmax(0, 1fr)); background: var(--card);
  border: 2px solid var(--rule); border-radius: 6px; padding: 4px;
}
.ws__cell { aspect-ratio: 1; display: grid; place-items: center; font: 700 clamp(.9rem, 4.2vw, 1.4rem)/1 var(--sans); font-size: calc(100cqi / var(--n) * .5); position: relative; z-index: 1; }
.ws__cell.left { color: var(--brand); background: var(--accent-soft); border-radius: 6px; }
.ws__drag { stroke: var(--brand); }
.ws__svg { position: absolute; inset: 6px; width: calc(100% - 12px); height: calc(100% - 12px); pointer-events: none; z-index: 0; }
.ws__words { display: flex; flex-wrap: wrap; gap: 6px; }
.ws__words span { padding: 3px 9px; border-radius: 99px; border: 1px solid var(--line); background: var(--card); font-size: .88rem; font-weight: 600; letter-spacing: .03em; }
.ws__words span.found { text-decoration: line-through; color: var(--ink-soft); background: var(--ok-soft); border-color: transparent; }
.ws__key { margin-top: 16px; padding: 14px; }
.ws__key .slots { display: flex; gap: 10px; flex-wrap: wrap; margin: 8px 0; }
.ws__key input { width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--paper); font: 700 1.1rem var(--sans); letter-spacing: .2em; text-transform: uppercase; }
@media (max-width: 860px), (orientation: portrait) and (max-width: 1100px) {
  .ws { grid-template-columns: minmax(0, 1fr); --ws-size: max(280px, calc((var(--avail-h) - 190px) * var(--n) / var(--rows))); }
  .ws__board { max-width: var(--ws-size); margin: 0 auto; width: 100%; }
}

/* ===== Sudoku ===== */
.sd {
  --sd-size: max(280px, min(700px, var(--avail-h)));
  display: grid; grid-template-columns: minmax(0, var(--sd-size)) minmax(220px, 320px); gap: 24px; align-items: start; justify-content: center;
}
.sd__grid { container-type: inline-size; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); border: 3px solid var(--rule); background: var(--rule); gap: 3px; user-select: none; touch-action: manipulation; }
.sd__box { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1px; min-width: 0; }
.sd__cell, .cw__cell, .ws__cell { min-width: 0; }
.sd__cell {
  aspect-ratio: 1; background: var(--cell); border: 0; padding: 0; position: relative; cursor: pointer;
  font: 600 clamp(1rem, 5vw, 1.7rem)/1 var(--sans); font-size: calc(100cqi / 9 * .5); color: var(--brand); display: grid; place-items: center;
}
.sd__cell.given { color: var(--ink); font-weight: 800; }
.sd__cell.peer { background: color-mix(in srgb, var(--sel) 55%, var(--cell)); }
.sd__cell.same { background: var(--sel); }
.sd__cell.cur { background: var(--sel-strong); }
.sd__cell.conflict { color: var(--bad); }
.sd__cell.wrong { background: var(--bad-soft); }
.sd__notes { position: absolute; inset: 2px; display: grid; grid-template-columns: repeat(3, 1fr); font-size: clamp(.45rem, 1.8vw, .7rem); font-size: calc(100cqi / 27 * .62); color: var(--ink-soft); font-weight: 600; }
.sd__notes span { display: grid; place-items: center; }
.sd__pad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.sd__pad button { height: 58px; font-size: 1.4rem; }
.sd__help { font-size: .88rem; margin-top: 10px; }
.sd__pad .btn[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #1b2430; }
.sd__pad .count { display: block; font-size: .65rem; color: var(--ink-soft); font-weight: 600; }
/* Verticale: griglia grande in alto, tastierino in due righe sotto. */
@media (max-width: 760px), (orientation: portrait) and (max-width: 1100px) {
  .sd { grid-template-columns: minmax(0, 1fr); gap: 12px; }
  .sd__grid { width: min(100%, max(280px, calc(var(--avail-h) - 160px))); margin: 0 auto; }
  .sd > div:last-child { width: min(100%, 680px); margin: 0 auto; }
  .sd__pad { grid-template-columns: repeat(6, 1fr); gap: 6px; }
  .sd__pad button { height: 56px; font-size: 1.35rem; padding: 0; }
  .sd__help { text-align: center; }
}
@media (max-width: 480px) { .sd__pad button { height: 48px; font-size: 1.2rem; } }

/* ===== Trova le differenze ===== */
.df { --ratio: 1.333; display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin: 0 auto; max-width: max(560px, calc((var(--avail-h) - 70px) * var(--ratio) * 2 + 14px)); }
.df figure { margin: 0; }
.df figcaption { font-size: .8rem; color: var(--ink-soft); text-align: center; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; margin-top: 4px; }
.df svg { display: block; width: 100%; height: auto; border-radius: 10px; border: 2px solid var(--rule); cursor: crosshair; touch-action: manipulation; background: #fff; }
.df .hit { fill: none; stroke: var(--bad); stroke-width: 3.5; }
.df .hit.hint { stroke: var(--accent); stroke-dasharray: 6 5; animation: pulse 1s ease-in-out 3; }
.df .miss { stroke: var(--bad); stroke-width: 3; stroke-linecap: round; animation: fadeout .9s forwards; }
@keyframes fadeout { 70% { opacity: 1; } to { opacity: 0; } }
@keyframes pulse { 50% { opacity: .2; } }
/* Tablet in verticale: una immagine sopra l'altra, grandi quanto lo schermo permette. */
@media (orientation: portrait) and (min-width: 701px) and (max-width: 1100px) {
  .df { grid-template-columns: minmax(0, 1fr); gap: 8px; max-width: max(420px, calc((var(--avail-h) - 90px) / 2 * var(--ratio))); }
}
@media (max-width: 700px) { .df { grid-template-columns: 1fr; gap: 8px; max-width: none; } }

/* ===== Unisci i puntini ===== */
.dt { max-width: max(300px, min(780px, calc(var(--avail-h) - 110px))); margin: 0 auto; }
.dt svg { display: block; width: 100%; height: auto; background: var(--card); border: 2px solid var(--rule); border-radius: 12px; touch-action: manipulation; user-select: none; }
.dt .dot { fill: var(--ink); }
.dt .dot.done { fill: var(--brand); }
.dt .dot-hit { fill: transparent; cursor: pointer; }
.dt .dot-label { font: 600 3.1px var(--sans); fill: var(--ink-soft); pointer-events: none; }
.dt .next .dot { fill: var(--accent); }
.dt .next.pulse .dot { animation: pulse .7s ease-in-out infinite; }
.dt .trail { fill: none; stroke: var(--brand); stroke-width: .9; stroke-linejoin: round; stroke-linecap: round; }
.dt .shape { opacity: 0; transition: opacity .8s; }
.dt.solved .shape { opacity: 1; }
.dt.solved .dot, .dt.solved .dot-label { opacity: 0; transition: opacity .6s; }
.dt__name { text-align: center; font: 700 1.6rem var(--serif); min-height: 1.4em; margin-top: 10px; }
.shake { animation: shake .35s; }
@keyframes shake { 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }

/* ===== Anagrammi ===== */
.an { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr)); gap: 12px; }
.an__item { padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.an__item.active { border-color: var(--brand); box-shadow: 0 0 0 2px var(--sel-strong); }
.an__item.solved { background: var(--ok-soft); border-color: transparent; }
.an__clue { font-weight: 600; margin: 0; display: flex; justify-content: space-between; gap: 8px; }
.an__clue small { color: var(--ink-soft); white-space: nowrap; }
.an__slots, .an__tiles { display: flex; gap: 5px; flex-wrap: wrap; min-height: 42px; }
.an__slots button, .an__tiles button {
  width: 38px; height: 42px; border-radius: 8px; border: 1px solid var(--line); background: var(--card);
  font: 700 1.15rem var(--sans); cursor: pointer; padding: 0; touch-action: manipulation;
}
.an__slots button { border-style: dashed; background: var(--paper); }
.an__slots button.filled { border-style: solid; background: var(--card); }
.an__slots button.fixed { color: var(--brand); }
.an__item.solved .an__slots button { border: 0; background: transparent; color: var(--ok); }
.an__tiles button { background: var(--accent-soft); border-color: transparent; }
.an__tiles button.used { visibility: hidden; }
/* Tablet: tessere grandi e schede larghe abbastanza da tenere anche le parole lunghe su una riga
   (una colonna in verticale, due in orizzontale). */
@media (pointer: coarse) and (min-width: 600px) {
  .an { grid-template-columns: repeat(auto-fill, minmax(min(100%, 480px), 1fr)); }
  .an__slots button, .an__tiles button { width: 42px; height: 50px; font-size: 1.3rem; }
  .an__slots, .an__tiles { gap: 6px; min-height: 50px; }
}
@media (max-width: 420px) { .an__slots button, .an__tiles button { width: 32px; height: 38px; font-size: 1rem; } }

/* ===== Touch (tablet prima di tutto) ===== */
@media (pointer: coarse) {
  .btn { min-height: 46px; }
  .btn--small { min-height: 44px; padding: 6px 14px; }
  .topnav a, .topnav button.linklike { padding: 10px 12px; }
  .topnav .theme-btn { width: 44px; height: 44px; }
  .cw__clue .btn { min-height: 46px; min-width: 46px; }
  .cw__lists li { padding: 9px 10px; }
  .puzzle-item { padding: 18px; }
  /* niente zoom da doppio tocco, selezione del testo o menu della pressione lunga mentre si gioca */
  .stage { touch-action: manipulation; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
  .stage input { -webkit-user-select: text; user-select: text; }
}
@media (pointer: coarse) and (min-width: 700px) { body { font-size: 17px; } }

@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }

/* Su telefono la tastiera del cruciverba resta agganciata in basso mentre si scorre. */
@media (max-width: 900px) {
  .cw__board .kbd { position: sticky; bottom: 0; z-index: 5; background: var(--paper); padding: 8px 0 calc(8px + env(safe-area-inset-bottom)); margin-top: 6px; box-shadow: 0 -6px 12px -8px rgb(0 0 0 / .25); }
}
