/* Battaglia Navale — plancia di comando (colori del prototipo, tema chiaro e scuro) */

:root {
  --ground: #DDE3DE;
  --paper: #EFF2EC;
  --paper-2: #E6EBE4;
  --ink: #16272C;
  --muted: #5F716E;
  --line: #B7C2BC;
  --water: #C6D5DA;
  --water-line: #B2C3C8;
  --fleet: #35505A;
  --fleet-edge: #22383F;
  --accent: #A32A6B;
  --hit: #C0521A;
  --sunk: #8C2A18;
  --miss: #78898A;
  --ok: #2F6B4F;
  --place: #2F7FB5;
  --veil: rgba(230, 235, 228, .86);
  --shadow: 3px 4px 10px rgba(0, 0, 0, .12), 0 0 2px rgba(0, 0, 0, .06);
  --radius: 3px;

  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Consolas, "DejaVu Sans Mono", monospace;
  --sans: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --ground: #0B1317;
    --paper: #121F26;
    --paper-2: #0E1A20;
    --ink: #DDE6E4;
    --muted: #8AA09F;
    --line: #29393F;
    --water: #163039;
    --water-line: #21444F;
    --fleet: #6D929F;
    --fleet-edge: #9CBAC3;
    --accent: #E2609F;
    --hit: #EE8C3E;
    --sunk: #C0442A;
    --miss: #5B7176;
    --ok: #62B08A;
    --place: #5AA7DC;
    --veil: rgba(11, 19, 23, .86);
    --shadow: 3px 4px 12px rgba(0, 0, 0, .45);
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--ground);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  padding: 0 max(12px, env(safe-area-inset-right)) 32px max(12px, env(safe-area-inset-left));
}

main { max-width: 1100px; margin: 0 auto; }

[hidden] { display: none !important; }

/* ---------------------------------------------------------------- intestazione */

.flag {
  max-width: 1100px;
  margin: 0 auto 14px;
  padding: 12px 0 10px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-bottom: 3px double var(--line);
}

.rose { flex: 0 0 auto; color: var(--accent); }

.flag-title { flex: 1; min-width: 0; }

.flag h1 {
  margin: 0;
  font-family: var(--serif);
  font-weight: 400;
  font-size: clamp(1.35rem, 5vw, 2.1rem);
  letter-spacing: .05em;
  line-height: 1.1;
}

.flag p { margin: 2px 0 0; color: var(--muted); font-size: .85rem; }

.flag-actions { display: flex; align-items: center; gap: 8px; }

.me { font-weight: 600; font-size: .9rem; }

/* ---------------------------------------------------------------- elementi comuni */

.card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}

.card h2, .modal-box h2 {
  margin: 0 0 10px;
  font-family: var(--serif);
  font-weight: 400;
  font-size: 1.3rem;
  letter-spacing: .03em;
}

.card p { margin: 0 0 12px; color: var(--muted); }

.narrow { max-width: 420px; margin: 8vh auto 0; }

.count { color: var(--muted); font-family: var(--sans); font-size: .9rem; }

.btn {
  font: inherit;
  font-weight: 600;
  font-size: .95rem;
  padding: 10px 16px;
  min-height: 44px;
  border-radius: var(--radius);
  border: 1px solid var(--fleet-edge);
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
}
.btn:hover:not(:disabled) { background: var(--paper-2); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--fleet-edge); border-color: var(--fleet-edge); color: var(--paper); }
.btn.primary:hover:not(:disabled) { background: var(--fleet); }
.btn.danger { background: var(--sunk); border-color: var(--sunk); color: #fff; }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.small { min-height: 34px; padding: 5px 11px; font-size: .85rem; }
.btn.wide { width: 100%; }
.btn:focus-visible, .cell:focus-visible, .seg button:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.controls { display: flex; flex-wrap: wrap; gap: 8px; }
.controls.stack { flex-direction: column; }
.controls.stack .btn { width: 100%; }

.field { display: block; margin-bottom: 12px; }
.field span { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 4px; }
.field input {
  width: 100%;
  font: inherit;
  font-size: 1.1rem;
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--ground);
  color: var(--ink);
}
.field input:focus { outline: 3px solid var(--accent); outline-offset: 1px; }

.error { color: var(--sunk); font-weight: 600; margin: 0 0 12px; }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; margin-bottom: 12px; }
.seg button {
  font: inherit; font-size: .9rem; padding: 8px 14px; border: 0; background: transparent; color: var(--muted); cursor: pointer;
}
.seg button[aria-pressed="true"] { background: var(--fleet-edge); color: var(--paper); font-weight: 600; }

/* ---------------------------------------------------------------- lobby */

.lobby { display: grid; gap: 14px; }
@media (min-width: 760px) { .lobby { grid-template-columns: 3fr 2fr; align-items: start; } }

.users { list-style: none; margin: 0; padding: 0; }
.users li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 0;
  border-bottom: 1px solid var(--line);
}
.users li:last-child { border-bottom: 0; }
.users .name { flex: 1; min-width: 0; font-weight: 600; overflow-wrap: anywhere; }
.users .state { font-size: .8rem; color: var(--muted); }
.users .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--ok); flex: 0 0 auto; }
.users .dot.busy { background: var(--hit); }
.users .empty { color: var(--muted); display: block; border: 0; }

.rules { margin-top: 14px; font-size: .9rem; color: var(--muted); }
.rules summary { cursor: pointer; font-weight: 600; color: var(--ink); }
.rules ul { padding-left: 18px; margin: 8px 0 0; }

/* ---------------------------------------------------------------- partita: disposizione
 *
 * Telefono: una griglia grande, a tutta larghezza, e l'altra in miniatura accanto al
 * cruscotto. data-focus sul contenitore dice quale mare è grande:
 *   own → la tua flotta (schieramento, o se la si ingrandisce toccando la miniatura)
 *   foe → il mare nemico (battaglia e fine partita)
 * PC: i due mari sempre affiancati.
 */

.theatre {
  display: grid;
  gap: 10px;
  grid-template-columns: 124px minmax(0, 1fr);
  grid-template-areas:
    "mini hud"
    "main main"
    "setup setup";
}

.hud { grid-area: hud; }
.setup { grid-area: setup; }

.theatre[data-focus="own"] .sea-own,
.theatre[data-focus="foe"] .sea-foe { grid-area: main; }
.theatre[data-focus="own"] .sea-foe,
.theatre[data-focus="foe"] .sea-own { grid-area: mini; }

.sea-own { view-transition-name: sea-own; }
.sea-foe { view-transition-name: sea-foe; }
::view-transition-group(*) { animation-duration: .38s; animation-timing-function: ease-in-out; }

@media (min-width: 900px) {
  .theatre {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-areas:
      "hud hud"
      "own foe"
      "setup .";
    column-gap: 22px;
  }
  .theatre .sea-own { grid-area: own !important; }
  .theatre .sea-foe { grid-area: foe !important; }
}

/* ---------------------------------------------------------------- cruscotto */

.hud {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 9px 11px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas:
    "turn clock"
    "event event"
    "fleets actions";
  gap: 4px 8px;
  align-content: start;
}

.turn { grid-area: turn; margin: 0; font-family: var(--serif); font-size: 1.1rem; line-height: 1.2; }
.turn.mine { color: var(--accent); }
.clock { grid-area: clock; font-family: var(--mono); font-size: 1.25rem; font-weight: 700; line-height: 1.1; }
.clock.low { color: var(--sunk); }
.event { grid-area: event; margin: 0; font-size: .85rem; color: var(--muted); min-height: 1.2em; }
.event.hit { color: var(--hit); font-weight: 600; }
.event.sunk { color: var(--sunk); font-weight: 700; }
/* regole del posizionamento, evidenziate in rosa */
.rules-hint {
  display: block;
  margin-bottom: 4px;
  padding: 4px 8px;
  border-radius: var(--radius);
  border-left: 3px solid var(--accent);
  background: color-mix(in srgb, var(--accent), var(--paper) 86%);
  color: var(--ink);
}

.fleets {
  grid-area: fleets;
  display: flex;
  flex-direction: column;
  font-size: .78rem;
  color: var(--muted);
  border-top: 1px solid var(--line);
  padding-top: 5px;
}
.fleets b { color: var(--ink); }
.fleets span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud-actions { grid-area: actions; display: flex; align-items: flex-end; border-top: 1px solid var(--line); padding-top: 5px; }

@media (min-width: 900px) {
  .hud {
    padding: 12px 16px;
    grid-template-columns: minmax(0, 1fr) auto auto;
    grid-template-areas:
      "turn clock actions"
      "event event event"
      "fleets fleets fleets";
    gap: 4px 16px;
    align-items: center;
  }
  .turn { font-size: 1.4rem; }
  .clock { font-size: 1.6rem; }
  .fleets { flex-direction: row; gap: 18px; font-size: .85rem; }
  .hud-actions { border: 0; padding: 0; }
}

/* ---------------------------------------------------------------- mari */

.sea { min-width: 0; }

.sea h2 {
  margin: 0 0 6px;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  font-family: var(--serif);
  font-weight: 400;
  font-size: 1.05rem;
  letter-spacing: .03em;
}

.hint-swap { display: none; font-family: var(--sans); font-size: .7rem; color: var(--muted); letter-spacing: 0; }

.chart-wrap { position: relative; }

.chart {
  --lab: minmax(14px, .6fr);
  display: grid;
  grid-template-columns: var(--lab) repeat(10, minmax(0, 1fr));
  gap: 2px;
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.lab {
  display: flex;
  align-items: center;
  justify-content: center;
  font: 600 .68rem/1 var(--mono);
  color: var(--muted);
}

.cell {
  appearance: none;
  aspect-ratio: 1;
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 1px solid var(--water-line);
  border-radius: 2px;
  background: var(--water);
  position: relative;
  cursor: default;
  color: inherit;
}

.chart.target .cell.free { cursor: crosshair; }
/* mentre spara l'avversario il mare nemico si attenua: si capisce che bisogna aspettare */
.theatre[data-phase="battle"] #chart-foe:not(.target) { opacity: .6; transition: opacity .3s; }
.chart.target .cell.free:hover { background: color-mix(in srgb, var(--water), var(--accent) 22%); }
/* schieramento: le navi si trascinano (niente scorrimento della pagina sul mare) e si girano con un tocco */
.chart.placing { touch-action: none; }
.chart.placing .cell.ship { cursor: grab; }
.cell.lifted { opacity: .35; }
.cell.shake { animation: shake .35s ease-in-out; }
@keyframes shake { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }

.cell.ship { background: var(--fleet); border-color: var(--fleet-edge); }
.cell.ship.revealed { background: color-mix(in srgb, var(--fleet), var(--water) 45%); border-style: dashed; }

/* acqua: un cerchio vuoto */
.cell.miss::after {
  content: "";
  position: absolute;
  inset: 34%;
  border-radius: 50%;
  background: var(--miss);
}

/* colpito: una croce */
.cell.hit { background: var(--hit); border-color: var(--hit); }
.cell.hit::before, .cell.hit::after {
  content: "";
  position: absolute;
  left: 18%; right: 18%;
  top: calc(50% - 1.5px);
  height: 3px;
  background: #fff;
  border-radius: 2px;
  transform: rotate(45deg);
}
.cell.hit::after { transform: rotate(-45deg); }
.cell.sunk { background: var(--sunk); border-color: var(--sunk); }

.cell.preview { background: var(--place); border-color: var(--place); }
.cell.blocked { background: color-mix(in srgb, var(--sunk), var(--water) 35%); border-color: var(--sunk); }

/* l'ultimo colpo arrivato si illumina per un attimo */
.cell.fresh { animation: fresh .9s ease-out; }
@keyframes fresh {
  0% { box-shadow: 0 0 0 0 var(--accent); transform: scale(1.18); z-index: 1; }
  100% { box-shadow: 0 0 0 10px transparent; transform: scale(1); }
}
.cell.flash { animation: flash .6s ease-in-out 2; }
@keyframes flash { 50% { filter: brightness(1.6); } }

@media (prefers-reduced-motion: reduce) {
  .cell.fresh, .cell.flash, .cell.shake { animation: none; }
  ::view-transition-group(*) { animation-duration: 0s; }
}

.veil {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 10px;
  background: var(--veil);
  border-radius: var(--radius);
  font-family: var(--serif);
  font-size: 1rem;
  color: var(--ink);
}
.veil:empty { display: none; }

/* ---------------------------------------------------------------- periscopio (telefono) */

@media (max-width: 899px) {
  .theatre[data-focus="own"] .sea-foe,
  .theatre[data-focus="foe"] .sea-own { cursor: pointer; }

  .theatre[data-focus="own"] .sea-foe .chart,
  .theatre[data-focus="foe"] .sea-own .chart {
    grid-template-columns: repeat(10, minmax(0, 1fr));
    gap: 1px;
    pointer-events: none;
  }
  .theatre[data-focus="own"] .sea-foe .lab,
  .theatre[data-focus="foe"] .sea-own .lab { display: none; }
  .theatre[data-focus="own"] .sea-foe h2,
  .theatre[data-focus="foe"] .sea-own h2 { font-size: .8rem; display: block; margin-bottom: 3px; }
  .theatre[data-focus="own"] .sea-foe .hint-swap,
  .theatre[data-focus="foe"] .sea-own .hint-swap { display: block; }
  .theatre[data-focus="own"] .sea-foe .cell,
  .theatre[data-focus="foe"] .sea-own .cell { border-width: 0; border-radius: 1px; }
  .theatre[data-focus="own"] .sea-foe .cell.miss::after,
  .theatre[data-focus="foe"] .sea-own .cell.miss::after { inset: 30%; }
  .theatre[data-focus="own"] .sea-foe .cell.hit::before,
  .theatre[data-focus="own"] .sea-foe .cell.hit::after,
  .theatre[data-focus="foe"] .sea-own .cell.hit::before,
  .theatre[data-focus="foe"] .sea-own .cell.hit::after { display: none; }
  .theatre[data-focus="own"] .sea-foe .veil,
  .theatre[data-focus="foe"] .sea-own .veil { font-size: .7rem; padding: 4px; }
}

/* ---------------------------------------------------------------- schieramento */

.setup {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
}

.setup .controls .btn { flex: 1 1 0; }

/* ---------------------------------------------------------------- finestre e avvisi */

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(8, 14, 17, .55);
}

.modal-box {
  width: 100%;
  max-width: 380px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 4px 6px 24px rgba(0, 0, 0, .3);
  padding: 20px;
}
.modal-box p { color: var(--muted); margin: 0 0 14px; }
.modal-box .verdict { font-family: var(--serif); font-size: 1.15rem; color: var(--ink); }
.rematch-status { color: var(--accent) !important; font-weight: 600; min-height: 1.2em; }

.spinner {
  width: 34px;
  height: 34px;
  margin: 0 auto 16px;
  border-radius: 50%;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toasts {
  position: fixed;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(92vw, 420px);
  pointer-events: none;
}
.toast {
  background: var(--ink);
  color: var(--paper);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: .9rem;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .3);
  opacity: 0;
  transform: translateY(-8px);
  transition: opacity .25s, transform .25s;
}
.toast.show { opacity: 1; transform: none; }

.offline {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 150;
  background: var(--sunk);
  color: #fff;
  text-align: center;
  padding: 8px;
  font-size: .9rem;
}

/* ---------------------------------------------------------------- schieramento sul telefono
 * Il mare nemico, ancora vuoto, non si mostra: griglia, navi e pulsanti stanno in una
 * schermata. In fondo al file perché prevalga sulle regole base. */

@media (max-width: 899px) {
  .theatre[data-phase="setup"] {
    grid-template-areas:
      "hud hud"
      "main main"
      "setup setup";
  }
  .theatre[data-phase="setup"] .sea-foe { display: none; }
  .theatre[data-phase="setup"] .sea-own h2 { display: none; }
  .setup { padding: 10px; }
  body[data-screen="game"] .flag { margin-bottom: 8px; padding: 6px 0; }
  body[data-screen="game"] .flag p { display: none; }
  body[data-screen="game"] .rose { width: 26px; height: 26px; }
  body[data-screen="game"] .flag h1 { font-size: 1.3rem; white-space: nowrap; }
  /* durante lo schieramento i conteggi delle navi non servono: più spazio per navi e Pronto */
  .theatre[data-phase="setup"] .fleets { display: none; }
  .theatre[data-phase="setup"] .hud-actions { border-top: 0; padding-top: 0; }
  .theatre[data-phase="setup"] .hud { grid-template-areas: "turn clock" "event actions"; align-items: center; }
  .hud-actions .btn.small { min-height: 32px; padding: 4px 8px; font-size: .78rem; }
}