:root {
  --bg: #12141c;
  --surface: #1b1e2a;
  --surface-2: #242838;
  --line: #333849;
  --text: #e8eaf2;
  --muted: #939ab5;
  --accent: #6c8cff;
  --correct: #4a9d5f;
  --present: #c8a33a;
  --absent: #3a3f52;
  --danger: #d95a5a;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  /* Altezza esatta del viewport: la schermata di gioco deve stare tutta dentro,
     senza scorrimento. dvh tiene conto della barra degli indirizzi mobile. */
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  display: flex;
  flex-direction: column;
  /* iOS Safari: tocchi rapidi e ravvicinati (giocando si martella la tastiera)
     venivano letti come doppio-tap e la pagina zoomava. manipulation disattiva
     il double-tap-zoom lasciando intatto il pinch per accessibilita'. */
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;  /* iOS non deve ridimensionare i testi da solo */
}

/* Il double-tap-zoom si aggancia all'elemento toccato: va tolto anche da tasti,
   bottoni e dai contenitori di gioco (un tocco puo' cadere fra due tasti). */
.key, .tab, button, .cell,
.keyboard, .krow, .board, .boardwrap, .play, main { touch-action: manipulation; }

/* Il flash grigio di iOS a ogni tocco fa sembrare l'app poco reattiva. */
button, .key, .tab { -webkit-tap-highlight-color: transparent; }

/* ------------------------------------------------------------------ topbar */
.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.logo { margin: 0; font-size: 1.25rem; letter-spacing: -0.02em; }
.logo span { color: var(--accent); }

.tabs { display: flex; gap: 0.25rem; flex: 1; flex-wrap: wrap; }

.tab {
  background: none;
  border: 0;
  color: var(--muted);
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius);
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab.is-active { background: var(--surface-2); color: var(--text); }

.account { display: flex; align-items: center; gap: 0.6rem; font-size: 0.9rem; }
.account .who { color: var(--muted); }

/* -------------------------------------------------------------------- main */
main {
  flex: 1;
  min-height: 0;               /* indispensabile: senza, il figlio non si comprime */
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: 1rem 1rem 1.25rem;
  display: flex;
  flex-direction: column;
  overflow-y: auto;            /* scorrono le altre schede, non il gioco */
}

.view { display: none; }
.view.is-active { display: block; }

/* La scheda di gioco non scorre mai: si adatta all'altezza disponibile. */
#view-game.is-active,
.play {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

h2 { font-size: 1rem; font-weight: 600; color: var(--muted); margin: 1.5rem 0 0.75rem; }
h2:first-child { margin-top: 0; }

.muted { color: var(--muted); font-size: 0.9rem; }

/* -------------------------------------------------------------------- auth */
.gate { max-width: 340px; margin: 2rem auto; text-align: center; }
.gate h2 { color: var(--text); font-size: 1.3rem; margin-bottom: 0.35rem; }

.authform { display: flex; flex-direction: column; gap: 0.75rem; margin-top: 1.5rem; text-align: left; }

.authform label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.85rem; color: var(--muted); }

input {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.6rem 0.7rem;
  font: inherit;
  /* Minimo 16px: sotto questa soglia iOS Safari zooma automaticamente quando il
     campo prende il focus. I campi di login ereditavano 0.85rem (13.6px) dalla
     label che li contiene, quindi al login lo schermo zoomava. */
  font-size: max(1rem, 16px);
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

button.primary {
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: var(--radius);
  padding: 0.65rem 1rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
button.primary:hover { filter: brightness(1.1); }
button.primary:disabled { opacity: 0.5; cursor: default; }

button.link {
  background: none; border: 0; color: var(--muted);
  font: inherit; font-size: 0.85rem; cursor: pointer; padding: 0;
  text-decoration: underline;
}
button.link:hover { color: var(--text); }

.switcher { display: flex; gap: 0.25rem; background: var(--surface); padding: 0.25rem; border-radius: var(--radius); }
.switcher.wide { margin-bottom: 1rem; }
.switcher button {
  flex: 1; background: none; border: 0; color: var(--muted);
  padding: 0.5rem; border-radius: 7px; font: inherit; font-size: 0.85rem; cursor: pointer;
}
.switcher button.is-active { background: var(--surface-2); color: var(--text); }

.formerror { color: var(--danger); font-size: 0.85rem; margin: 0; }

/* ------------------------------------------------------------------- board */
.timer {
  flex: none;
  font-variant-numeric: tabular-nums;
  font-size: clamp(1.4rem, 5vh, 2rem);
  font-weight: 300;
  text-align: center;
  color: var(--muted);
  margin-bottom: 0.6rem;
}

/* Il tabellone prende lo spazio che avanza fra timer e tastiera, mantenendo le
   proporzioni 5 colonne x 6 righe. E' quello che permette alla schermata di
   stare in un telefono senza scorrere: si rimpicciolisce invece di sforare. */
/* Il contenitore prende lo spazio che avanza fra timer e tastiera; il tabellone
   dentro si dimensiona per proporzione.
   NON mettere flex:1 direttamente sul tabellone: flex gli imporrebbe un'altezza
   definita, aspect-ratio potrebbe solo derivarne la larghezza, e max-width la
   taglierebbe — risultato, celle rettangolari. */
.boardwrap {
  flex: 1;
  /* Soglia sotto la quale il tabellone diventa illeggibile: se proprio non c'e'
     spazio, meglio far scorrere la pagina (main ha overflow-y: auto) che
     ridurre le celle a pochi pixel. */
  min-height: 150px;
  container-type: size;   /* abilita cqw/cqh sul tabellone */
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 0.6rem;
}

.board {
  /* Il vincolo puo' essere l'altezza (telefono alto e stretto) o la larghezza
     (finestra bassa e larga). Con una sola delle due dimensioni definita, il
     rapporto si rompe e le celle diventano rettangolari: qui prendiamo
     esplicitamente il minore dei due limiti. */
  height: 100%;                                /* ripiego se cq non e' supportato */
  height: min(100cqh, 100cqw * 6 / 5);
  aspect-ratio: 5 / 6;                         /* 5 colonne x 6 righe */
  max-width: 100%;
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 5px;
}

.row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 5px; min-height: 0; }

.cell {
  display: grid;
  place-items: center;
  /* cqmin non e' disponibile ovunque: vmin scala con la dimensione minore. */
  font-size: clamp(1.1rem, 6vmin, 2rem);
  font-weight: 700;
  border: 2px solid var(--line);
  border-radius: 6px;
  text-transform: uppercase;
  min-width: 0;
  overflow: hidden;
}
.cell.filled { border-color: var(--muted); }
.cell.correct { background: var(--correct); border-color: var(--correct); }
.cell.present { background: var(--present); border-color: var(--present); color: #1a1a1a; }
.cell.absent  { background: var(--absent);  border-color: var(--absent); }

.message { flex: none; text-align: center; min-height: 1.4em; font-size: 0.95rem; margin: 0 0 0.5rem; }
.message.error { color: var(--danger); }
.message.win { color: var(--correct); font-weight: 600; }

/* ------------------------------------------- aggiunta parola al dizionario */
.addbox { flex: none; text-align: center; margin: 0 0 0.5rem; }

button.ghost {
  background: none;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.45rem 0.9rem;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}
button.ghost:hover { border-color: var(--accent); color: var(--accent); }
button.ghost:disabled { opacity: 0.5; cursor: default; }

.lbnote {
  margin: 0 0 0.75rem;
  font-size: 0.76rem;
  color: var(--muted);
  line-height: 1.45;
}
.lbnote:empty { display: none; }

.lbdet { display: block; font-size: 0.72rem; color: var(--muted); font-weight: 400; margin-top: 0.1rem; }

/* ------------------------------------------------------------- dashboard */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 0.6rem;
}

.kpi {
  background: var(--surface);
  border: 1px solid var(--line);
  border-top: 3px solid var(--player-color, var(--accent));
  border-radius: var(--radius);
  padding: 0.75rem 0.85rem;
}
.kpi .name { font-size: 0.75rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.kpi .rank { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 0.15rem; }
.kpi .rank small { font-size: 0.75rem; font-weight: 400; color: var(--muted); }
.kpi .sub  { font-size: 0.75rem; color: var(--muted); margin-top: 0.25rem; }

.insights { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-top: 0.75rem; }
.insight {
  background: rgba(108, 140, 255, 0.12);
  border: 1px solid rgba(108, 140, 255, 0.35);
  border-radius: 99px;
  padding: 0.3rem 0.7rem;
  font-size: 0.78rem;
  color: #a9bcff;
}

.charts-grid { display: grid; grid-template-columns: 1fr; gap: 0.75rem; }
@media (min-width: 820px) { .charts-grid { grid-template-columns: 1fr 1fr; } }

.chart-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.9rem 1rem 1rem;
}
.chart-card h3 { margin: 0 0 0.7rem; font-size: 0.92rem; font-weight: 600; }
.chart-card .note { margin: 0.6rem 0 0; font-size: 0.74rem; color: var(--muted); line-height: 1.4; }

/* Altezza fissa: senza, Chart.js in un contenitore flessibile cresce a ogni
   ridisegno finche' la pagina non diventa chilometrica. */
.chart-wrap { position: relative; height: 240px; }

.wordtable { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.wordtable th {
  text-align: left;
  padding: 0.35rem 0.5rem;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border-bottom: 1px solid var(--line);
}
.wordtable td { padding: 0.4rem 0.5rem; border-bottom: 1px solid var(--line); }
.wordtable tr:last-child td { border-bottom: 0; }
.wordtable .w { font-weight: 700; letter-spacing: 0.06em; }
.wordtable .num { font-variant-numeric: tabular-nums; text-align: right; }

.barcell { width: 38%; }
.bar-bg { background: var(--surface-2); border-radius: 4px; height: 7px; }
.bar-fill { height: 100%; border-radius: 4px; }

/* -------------------------------------------------------------- manuale */
/* Finestra modale invece di una sesta scheda: a 320px la barra non reggerebbe
   un altro pulsante, e qui il contenuto puo' scorrere liberamente senza
   intaccare il vincolo "gioco tutto in una schermata". */
.help {
  flex: none;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
}
.help:hover { background: var(--surface-2); color: var(--text); }

.manual {
  width: min(640px, 92vw);
  max-height: 85dvh;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  overflow: hidden;
}
.manual::backdrop { background: rgba(0, 0, 0, 0.65); }

.manualhead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.9rem 1.1rem;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  background: var(--surface);
}
.manualhead h2 { margin: 0; font-size: 1.1rem; color: var(--text); }

.manualbody {
  padding: 0.25rem 1.1rem 1.25rem;
  overflow-y: auto;
  max-height: calc(85dvh - 3.5rem);
  font-size: 0.92rem;
  line-height: 1.55;
  /* Senza questo la barra di scorrimento resta chiara e stona sul fondo scuro. */
  scrollbar-width: thin;
  scrollbar-color: var(--line) transparent;
}
.manualbody h3 {
  margin: 1.3rem 0 0.4rem;
  font-size: 0.95rem;
  color: var(--accent);
}
.manualbody p { margin: 0.5rem 0; }
.manualbody ul { margin: 0.5rem 0; padding-left: 1.1rem; }
.manualbody li { margin: 0.3rem 0; }
.manualbody strong { color: #fff; }
.manualbody em { color: var(--text); font-style: normal; font-weight: 700; }

.legend { list-style: none; padding-left: 0; }
.legend li { display: flex; align-items: center; gap: 0.6rem; }

.swatch {
  width: 1.15rem;
  height: 1.15rem;
  border-radius: 4px;
  flex: none;
}
.swatch.correct { background: var(--correct); }
.swatch.present { background: var(--present); }
.swatch.absent  { background: var(--absent); }

/* Griglia d'esempio: celle piccole a misura fissa, non quelle del gioco. */
.demo { display: flex; gap: 5px; margin: 0.6rem 0; }
.demo .cell {
  width: 2.4rem;
  height: 2.4rem;
  font-size: 1.1rem;
  border-width: 2px;
}

/* ------------------------------------------------------------ avvio partita */
.startbox { flex: none; text-align: center; margin: 0 0 0.75rem; }
.startbox .primary { padding: 0.65rem 1.6rem; font-size: 1rem; }

.starthint {
  margin: 0.45rem 0 0;
  font-size: 0.8rem;
  color: var(--muted);
}

/* ------------------------------------------------------------- condivisione */
.sharebox { flex: none; text-align: center; margin: 0 0 0.75rem; }
.sharebox .primary { padding: 0.55rem 1.4rem; }

.sharehint {
  margin: 0.4rem 0 0;
  font-size: 0.85rem;
  color: var(--correct);
}
.sharehint:empty { display: none; }

.sharefallback {
  width: 100%;
  margin-top: 0.75rem;
  padding: 0.6rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  font: inherit;
  font-size: max(0.85rem, 16px);  /* evita lo zoom iOS al focus (vedi input) */
  line-height: 1.5;
  resize: vertical;
  min-height: 8.5rem;
  text-align: left;
}

/* ---------------------------------------------------------------- keyboard */
.keyboard { flex: none; display: flex; flex-direction: column; gap: 5px; }
.krow { display: flex; gap: 5px; justify-content: center; }

.key {
  flex: 1;
  min-width: 0;
  height: clamp(38px, 6.2vh, 52px);
  background: var(--surface-2);
  border: 0;
  border-radius: 5px;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
  text-transform: uppercase;
}
.key:hover { filter: brightness(1.2); }
.key.wide { flex: 1.6; font-size: 0.7rem; }
.key.correct { background: var(--correct); }
.key.present { background: var(--present); color: #1a1a1a; }
.key.absent  { background: var(--absent); color: var(--muted); }

/* ------------------------------------------------------------ card & table */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 0.6rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.85rem;
}
.card .value { font-size: 1.5rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.card .label { font-size: 0.75rem; color: var(--muted); margin-top: 0.2rem; }

.table { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }

.trow {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.7rem 0.9rem;
  border-bottom: 1px solid var(--line);
  font-size: 0.9rem;
}
.trow:last-child { border-bottom: 0; }
.trow.head { color: var(--muted); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; }

.trow .rank { width: 1.6rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.trow .name { flex: 1; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.trow .num  { font-variant-numeric: tabular-nums; text-align: right; min-width: 4.5rem; }

.pill { font-size: 0.7rem; padding: 0.15rem 0.45rem; border-radius: 99px; font-weight: 600; }
.pill.won  { background: rgba(74,157,95,0.2);  color: #7cc98d; }
.pill.lost { background: rgba(217,90,90,0.18); color: #e58b8b; }

.lookup { display: flex; gap: 0.5rem; margin-bottom: 1rem; }
.lookup input { flex: 1; }

/* -------------------------------------------------------------------- team */
.teamswitch { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-bottom: 1rem; }

.teamswitch button {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 99px;
  color: var(--muted);
  padding: 0.35rem 0.8rem;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}
.teamswitch button.is-active { background: var(--surface-2); border-color: var(--accent); color: var(--text); }

.invite {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.6rem 0.75rem;
  margin-bottom: 1rem;
}
.invite code {
  flex: 1;
  font-size: 0.78rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.teamhead { display: flex; align-items: baseline; gap: 0.6rem; margin-bottom: 0.5rem; }
.teamhead h3 { margin: 0; font-size: 1.15rem; }
.teamhead .muted { font-size: 0.8rem; }

.pill.in_progress { background: rgba(108,140,255,0.18); color: #9db0ff; }
.pill.not_started { background: var(--surface-2); color: var(--muted); }

.empty { padding: 1.5rem; text-align: center; color: var(--muted); font-size: 0.9rem; }

/* --------------------------------------------------------------- telefono */
/* Obiettivo: la schermata di gioco deve stare tutta senza scorrere. La barra
   in alto e' la voce piu' costosa perche' logo + 4 schede + account non entrano
   in una riga: la dividiamo in due righe compatte invece di lasciarla andare a
   capo da sola. */
@media (max-width: 560px) {
  .topbar {
    padding: 0.45rem 0.75rem 0.5rem;
    gap: 0.4rem 0.5rem;
  }
  .logo { font-size: 1rem; }

  .account { font-size: 0.8rem; margin-left: auto; }

  .tabs {
    order: 3;
    width: 100%;
    gap: 0.2rem;
    flex-wrap: nowrap;         /* una riga sola: si comprimono, non vanno a capo */
  }
  .tab {
    flex: 1 1 0;
    min-width: 0;
    padding: 0.35rem 0.15rem;
    font-size: 0.75rem;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  main { padding: 0.75rem 0.75rem 1rem; }
  .cards { grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); gap: 0.5rem; }
}

/* Telefoni molto stretti (320px): serve un altro giro di compressione perche'
   "Statistiche" e "Cronologia" entrino senza troncarsi. */
@media (max-width: 380px) {
  .topbar { padding: 0.4rem 0.5rem 0.45rem; }
  .logo { font-size: 0.95rem; }
  .account { font-size: 0.75rem; }
  .tab { font-size: 0.68rem; padding: 0.3rem 0.1rem; }
  main { padding: 0.6rem 0.5rem 0.75rem; }
}

/* Schermi bassi (telefoni in orizzontale, finestre ridotte): si comprime tutto
   il comprimibile prima di arrendersi allo scorrimento. */
@media (max-height: 620px) {
  .timer { font-size: 1.3rem; margin-bottom: 0.35rem; }
  .boardwrap { margin-bottom: 0.35rem; }
  .message { min-height: 1.2em; margin-bottom: 0.35rem; font-size: 0.85rem; }
  .sharebox { margin-bottom: 0.5rem; }
  .sharebox .primary { padding: 0.4rem 1rem; font-size: 0.85rem; }
}

/* Telefono in orizzontale: impilare tabellone e tastiera non ci sta in altezza
   (il tabellone si riduceva a celle da 3px). Li affianchiamo: il tabellone
   occupa tutta l'altezza a sinistra, il resto sta a destra. */
@media (orientation: landscape) and (max-height: 560px) {
  main { max-width: 100%; padding: 0.5rem 0.75rem; }

  #view-game.is-active > .play {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    grid-template-areas:
      "board timer"
      "board message"
      "board keyboard"
      "board share";
    column-gap: 1rem;
    align-items: center;
  }

  .boardwrap {
    grid-area: board;
    min-height: 0;               /* in orizzontale lo spazio e' quello: puo' scalare */
    margin: 0;
    align-self: stretch;
    /* Niente contenimento qui: la colonna della griglia e' larga "auto" e deve
       potersi dimensionare sul contenuto. Con container-type: size collasserebbe
       a zero e il tabellone diventerebbe di pochi pixel. */
    container-type: normal;
  }

  /* In orizzontale il limite e' sempre l'altezza: il rapporto basta da solo. */
  .board {
    height: 100%;
    width: auto;
    max-width: none;
  }

  .timer { grid-area: timer; margin: 0; font-size: 1.4rem; }
  .message { grid-area: message; margin: 0.2rem 0 0; font-size: 0.8rem; min-height: 1.1em; }
  .keyboard { grid-area: keyboard; align-self: center; }
  .sharebox { grid-area: share; margin: 0.4rem 0 0; }
}
