:root {
  --bg: #0b2545;
  --bg2: #13315c;
  --panel: #16365f;
  --line: #2a5384;
  --text: #eef4fb;
  --muted: #9db4cf;
  --accent: #36c2e8;
  --good: #4cd08a;
  --bad: #ef5350;
  --warn: #ffb74d;
  --ship: #8fa6bf;
  --water: #1b4a7a;
  --on-accent: #04202e;
  --on-good: #052a17;
  --cell-text: #bfe7f7;
  --ship-hi: #b4c4d8;
  --ship-lo: #7c91aa;
  --sel-ship-bg: #d6e4f2;
  --miss-dot: #dff3ff;
  --hit-bg: #7a2a2a;
  --hit-core: #ffd166;
  --hit-ring: #ef5350;
  --sunk-bg: #3a1010;
  --sunk-core: #777;
  --sunk-ring: #b03030;
  --sel-bg: #2c6aa3;
  --enemy-ship: #5b6c80;
  --sel-btn: #1f4f80;
  --boom-bg: #6b2a12;
  --note-bad-bg: #5a2a0f;
  --note-bad-fg: #ffd9b8;
  --note-good-bg: #12402b;
  --note-good-fg: #c8f5dc;
  --cb-glyph: #fff;
  --shadow: #0008;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
  touch-action: manipulation;
}
#app { position: relative; min-height: 100%; padding: calc(12px + var(--safe-t)) 14px calc(18px + var(--safe-b)); max-width: 520px; margin: 0 auto; }
h1 { font-size: 30px; margin: 8px 0 2px; letter-spacing: .5px; }
h2 { font-size: 19px; margin: 14px 0 8px; }
p { margin: 6px 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.boot { text-align: center; margin-top: 40vh; color: var(--muted); }

/* ---------- controls ---------- */
button, input { font: inherit; color: inherit; }
button {
  appearance: none; border: 0; border-radius: 12px; padding: 13px 16px; min-height: 48px;
  background: var(--panel); color: var(--text); font-weight: 600; cursor: pointer;
  border: 1px solid var(--line);
}
button:active { transform: scale(.98); }
button.primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
button.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
button.ghost { background: transparent; }
button.wa { background: #25d366; border-color: #25d366; color: #05260f; }
button:disabled { opacity: .4; pointer-events: none; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.row { display: flex; gap: 8px; align-items: center; }
.row > * { flex: 1; }
.row.wrap { flex-wrap: wrap; }
input[type=text] {
  width: 100%; padding: 13px 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg2); font-size: 18px; -webkit-user-select: text; user-select: text;
}
input.code { text-align: center; letter-spacing: 6px; font-size: 28px; text-transform: uppercase; font-weight: 700; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 12px; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; flex: 1; background: transparent; padding: 11px 6px; font-size: 15px; }
.seg button.on { background: var(--accent); color: var(--on-accent); }
.stepper { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 6px 0; }
.stepper .lab { flex: 1; }
.stepper .lab small { display: block; color: var(--muted); font-weight: 400; }
.stepper .btns { display: flex; align-items: center; gap: 10px; }
.stepper .btns button { min-height: 40px; width: 44px; padding: 0; }
.stepper .val { min-width: 20px; text-align: center; font-weight: 700; font-size: 18px; }
.toggle { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; gap: 10px; }
.toggle button { min-height: 38px; padding: 6px 14px; min-width: 64px; }
.toggle button.on { background: var(--good); border-color: var(--good); color: var(--on-good); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.chip { font-size: 12px; padding: 4px 9px; border-radius: 99px; background: var(--bg2); border: 1px solid var(--line); color: var(--muted); }
.presets { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.presets button { font-size: 14px; padding: 10px 8px; text-align: left; line-height: 1.2; }
.presets small { display: block; font-weight: 400; color: var(--muted); }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.topbar button { min-height: 38px; padding: 6px 12px; font-size: 14px; }
.banner { padding: 10px 12px; border-radius: 12px; text-align: center; font-weight: 700; font-size: 18px; margin: 6px 0; }
.banner.mine { background: var(--good); color: var(--on-good); }
.banner.theirs { background: var(--panel); border: 1px solid var(--line); color: var(--muted); }
.banner.win { background: var(--good); color: var(--on-good); font-size: 24px; }
.banner.lose { background: var(--bad); color: #fff; font-size: 24px; }
.banner.net { background: var(--warn); color: #2b1a00; font-size: 14px; padding: 6px; }
.event { min-height: 40px; font-size: 14px; color: var(--muted); text-align: center; padding: 2px 4px; }
.event b { color: var(--text); }
.code-big { font-size: 34px; letter-spacing: 8px; font-weight: 800; text-align: center; color: var(--accent); }

/* ---------- boards ---------- */
.board-wrap { display: flex; justify-content: center; }
.board {
  --cs: min(calc((100vw - 32px) / (var(--n) + 1)), 40px);
  display: grid;
  grid-template-columns: repeat(calc(var(--n) + 1), var(--cs));
  grid-auto-rows: var(--cs);
  gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: 6px; overflow: hidden;
}
.board.battle { --cs: max(24px, min(calc((100vw - 32px) / (var(--n) + 1)), calc((100dvh - 430px) / (var(--n) + 1)), 40px)); }
.board.mini { --cs: min(calc((100vw - 40px) * .5 / (var(--n) + 1)), 20px); }
.board.mini .cell { font-size: 9px; }
.board .lab { background: var(--bg); color: var(--muted); font-size: calc(var(--cs) * .38); display: flex; align-items: center; justify-content: center; }
.cell {
  background: var(--water); position: relative; display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--cs) * .38); font-weight: 700; color: var(--cell-text);
}
.cell.ship { background: var(--ship); }
.cell.sel-ship { background: var(--sel-ship-bg); outline: 2px solid var(--accent); outline-offset: -2px; z-index: 1; }
.cell.bad { background: var(--bad); }
.cell.miss::after { content: ""; width: 28%; height: 28%; border-radius: 50%; background: var(--miss-dot); opacity: .85; }
.cell.hit { background: var(--hit-bg); }
.cell.hit::after { content: ""; width: 58%; height: 58%; border-radius: 50%; background: radial-gradient(circle, var(--hit-core) 0 25%, var(--hit-ring) 26% 100%); }
.cell.sunk { background: var(--sunk-bg); }
.cell.sunk::after { background: radial-gradient(circle, var(--sunk-core) 0 25%, var(--sunk-ring) 26% 100%); }
.cell.last { animation: pulse 1s ease-out 2; }
.cell.sel { outline: 3px solid var(--warn); outline-offset: -3px; z-index: 2; background: var(--sel-bg); }
.cell.prev { background: var(--sel-bg); box-shadow: inset 0 0 0 2px var(--warn); }
.cell.radar { color: var(--accent); }
.cell.sonar { color: var(--warn); }
@keyframes pulse { 0% { box-shadow: inset 0 0 0 4px #fff; } 100% { box-shadow: inset 0 0 0 0 #fff; } }
.cell.enemy-ship { background: var(--enemy-ship); }
.cell.enemy-ship.hit { background: var(--hit-bg); }

.fleet { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.fleet button { flex: 1 1 30%; min-height: 44px; padding: 6px 8px; font-size: 13px; display: flex; flex-direction: column; align-items: center; gap: 3px; }
.fleet button.on { border-color: var(--accent); background: var(--sel-btn); }
.fleet button.done .blocks i { background: var(--good); }
.blocks { display: flex; gap: 2px; }
.blocks i { width: 9px; height: 9px; background: var(--ship); border-radius: 2px; display: block; }
.blocks i.x { background: var(--bad); }

.below { display: flex; gap: 10px; align-items: flex-start; margin-top: 8px; }
.below > .mini-wrap { flex: 0 0 auto; }
.below > .info { flex: 1; min-width: 0; font-size: 13px; }
.info h3 { margin: 0 0 4px; font-size: 13px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .6px; }
.intel { margin: 0; padding: 0; list-style: none; }
.intel li { padding: 1px 0; }

.modes { display: flex; gap: 6px; overflow-x: auto; margin: 8px 0 6px; padding-bottom: 2px; }
.modes button { flex: 0 0 auto; padding: 8px 12px; min-height: 40px; font-size: 14px; }
.modes button.on { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.hint { font-size: 13px; color: var(--muted); text-align: center; min-height: 18px; margin-bottom: 6px; }
.firebar { position: sticky; bottom: calc(6px + var(--safe-b)); }
.firebar button { width: 100%; font-size: 19px; min-height: 54px; }

#toast {
  position: fixed; left: 50%; bottom: calc(80px + var(--safe-b)); transform: translateX(-50%) translateY(20px);
  background: #000d; color: #fff; padding: 10px 16px; border-radius: 99px; font-size: 14px; opacity: 0; pointer-events: none;
  transition: .25s; max-width: 90vw; text-align: center; z-index: 10;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.shake { animation: shake .3s; }
@keyframes shake { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }

/* ---------- social: score, clock, nudge, emotes ---------- */
.score { display: flex; justify-content: center; align-items: baseline; gap: 10px; font-size: 15px; color: var(--muted); margin: 2px 0 4px; }
.score .lead { color: var(--text); }
.score b { font-size: 1.25em; color: var(--text); }
.score.big { font-size: 20px; margin: 6px 0 10px; }
.score .dash { opacity: .6; }
.clock { margin-left: 10px; font-variant-numeric: tabular-nums; font-weight: 700; }
.clock.urgent { color: var(--bad); animation: blink 1s steps(2) infinite; }
.banner.mine .clock.urgent { color: #7a0d0d; }
@keyframes blink { 50% { opacity: .35; } }
.small-btn { min-height: 36px; padding: 6px 14px; font-size: 14px; }
.tray {
  position: fixed; right: 14px; top: calc(62px + var(--safe-t)); display: grid; grid-template-columns: repeat(4, 52px); gap: 6px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 8px; box-shadow: 0 8px 24px var(--shadow); z-index: 6;
}
.tray button { min-height: 0; height: 52px; padding: 0; font-size: 26px; background: var(--bg2); }
.emote-float { position: fixed; bottom: 22%; z-index: 20; pointer-events: none; text-align: center; animation: floatup 2.4s ease-out forwards; }
.emote-float .e { font-size: 56px; filter: drop-shadow(0 4px 8px #0008); }
.emote-float .w { font-size: 12px; background: #000a; color: #fff; padding: 2px 8px; border-radius: 99px; display: inline-block; }
@keyframes floatup { 0% { transform: translateY(20px) scale(.6); opacity: 0; } 15% { opacity: 1; transform: translateY(0) scale(1.1); } 100% { transform: translateY(-38vh) scale(1); opacity: 0; } }

/* ---------- variants: mines, moving ships ---------- */
.cell.mine { font-size: calc(var(--cs) * .5); }
.cell.boom { background: var(--boom-bg); font-size: calc(var(--cs) * .5); }
.cell.boom::after, .cell.mine::after { content: none; }
.cell.locked { filter: saturate(.4) brightness(.8); }
.board.mini .cell.mine, .board.mini .cell.boom { font-size: 11px; }
.dpad { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 8px 0; }
.dpad button { font-size: 24px; min-height: 48px; padding: 0; }
.dpad button.on { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.note { text-align: center; font-size: 14px; padding: 6px 10px; border-radius: 10px; margin: 4px 0; }
.note.bad { background: var(--note-bad-bg); color: var(--note-bad-fg); }
.note.good { background: var(--note-good-bg); color: var(--note-good-fg); }
.fleet button .small { font-size: 12px; color: var(--muted); }

/* ====================================================================
   Themes: light, colour-blind friendly, reduced effects
   ==================================================================== */
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #e8f1fa; --bg2: #d7e6f5; --panel: #ffffff; --line: #a9c2dc;
  --text: #0b2545; --muted: #4a6585;
  --accent: #0a84c1; --on-accent: #ffffff;
  --good: #2e9e63; --on-good: #ffffff; --bad: #d64541; --warn: #e6900a;
  --ship: #6e86a3; --ship-hi: #8aa3c0; --ship-lo: #56708f;
  --water: #9ccbea; --cell-text: #0b3b66;
  --sel-ship-bg: #33507a; --miss-dot: #ffffff;
  --hit-bg: #f0b8a8; --hit-core: #ffe08a; --hit-ring: #d64541;
  --sunk-bg: #c97b70; --sunk-core: #555; --sunk-ring: #8f2a2a;
  --sel-bg: #6fb3e0; --enemy-ship: #8aa0ba; --sel-btn: #cfe6f8; --boom-bg: #f0c9a0;
  --note-bad-bg: #ffe1c9; --note-bad-fg: #7a3000; --note-good-bg: #d4f3e2; --note-good-fg: #0b4b2b;
  --cb-glyph: #7a1f10; --shadow: #0003;
}
:root[data-theme="light"] .cell.ship:not(.sel-ship) { color: #fff; }

/* Colour-blind friendly (Okabe-Ito): blue/orange instead of green/red, and hits get a ✕ shape. */
:root[data-cb="on"] {
  --good: #0072b2; --on-good: #ffffff; --bad: #d55e00; --warn: #e69f00;
  --hit-core: #f0e442; --hit-ring: #d55e00; --sunk-ring: #7a3000;
}
:root[data-cb="on"] .banner.mine, :root[data-cb="on"] .banner.win { color: #fff; }
:root[data-cb="on"] .cell.hit::after,
:root[data-cb="on"] .cell.sunk::after {
  content: "✕"; background: none; color: var(--cb-glyph); width: auto; height: auto;
  font-size: calc(var(--cs) * .75); font-weight: 900; line-height: 1;
}
:root[data-cb="on"] .cell.miss::after { background: none; border: 2px solid var(--miss-dot); width: 34%; height: 34%; opacity: 1; }
:root[data-cb="on"] .cell.hit { background: #8a3b00; }
:root[data-cb="on"][data-theme="light"] .cell.hit { background: #ffd0a8; }
:root[data-cb="on"] .cell.sunk { background: #2b1200; }
:root[data-cb="on"][data-theme="light"] .cell.sunk { background: #d9a37a; }
:root[data-cb="on"] .note.bad { background: #5a3300; }
:root[data-cb="on"] .note.good { background: #003a5c; color: #d6eeff; }
:root[data-cb="on"][data-theme="light"] .note.bad { background: #ffe9c4; }
:root[data-cb="on"][data-theme="light"] .note.good { background: #d6ecfb; color: #00395a; }

/* Turn off all motion */
:root[data-fx="off"] *, :root[data-fx="off"] *::before, :root[data-fx="off"] *::after { animation: none !important; transition: none !important; }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}

/* ====================================================================
   Nicer ships, water and effects
   ==================================================================== */
.board { position: relative; }
.board::after {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(105deg, transparent 42%, #ffffff14 50%, transparent 58%);
  background-size: 280% 100%; animation: sheen 7s linear infinite;
}
@keyframes sheen { from { background-position: 140% 0; } to { background-position: -140% 0; } }

.cell.ship:not(.sel-ship):not(.hit):not(.boom) { background: linear-gradient(180deg, var(--ship-hi), var(--ship-lo)); }
.cell.ship.hull-v:not(.sel-ship):not(.hit):not(.boom) { background: linear-gradient(90deg, var(--ship-hi), var(--ship-lo)); }
.cell.hull-h.hull-start { border-radius: calc(var(--cs) * .5) 0 0 calc(var(--cs) * .5); }
.cell.hull-h.hull-end { border-radius: 0 calc(var(--cs) * .5) calc(var(--cs) * .5) 0; }
.cell.hull-v.hull-start { border-radius: calc(var(--cs) * .5) calc(var(--cs) * .5) 0 0; }
.cell.hull-v.hull-end { border-radius: 0 0 calc(var(--cs) * .5) calc(var(--cs) * .5); }
.cell.hull-one { border-radius: calc(var(--cs) * .5); }
.cell.ship.hull-mid:not(.hit):not(.boom)::before,
.cell.ship.hull-start:not(.hit):not(.boom)::before {
  content: ""; position: absolute; width: 22%; height: 22%; border-radius: 50%; background: #ffffff55;
}

.cell.hit.last::after { animation: boom .7s ease-out; }
.cell.miss.last::after { animation: ripple .9s ease-out; }
@keyframes boom { 0% { transform: scale(.2); filter: brightness(3); } 55% { transform: scale(1.5); } 100% { transform: scale(1); filter: none; } }
@keyframes ripple { 0% { transform: scale(.3); box-shadow: 0 0 0 0 #fff; } 100% { transform: scale(1); box-shadow: 0 0 0 calc(var(--cs) * .5) #fff0; } }
.cell.sunk::before { content: ""; position: absolute; inset: 20%; border-radius: 50%; background: #0006; filter: blur(2px); animation: smoke 2.4s ease-in-out infinite; }
@keyframes smoke { 0%, 100% { transform: translateY(0) scale(.8); opacity: .5; } 50% { transform: translateY(-18%) scale(1.15); opacity: .15; } }
.banner.mine { animation: pop .35s ease-out; }
@keyframes pop { 0% { transform: scale(.94); } 60% { transform: scale(1.03); } 100% { transform: scale(1); } }

/* confetti on victory */
.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 30; overflow: hidden; }
.confetti i { position: absolute; top: -16px; width: 9px; height: 15px; opacity: .95; animation: fall var(--d) cubic-bezier(.3, .6, .5, 1) forwards; }
@keyframes fall { to { transform: translate(var(--x), 105vh) rotate(var(--r)); opacity: .9; } }

/* ====================================================================
   Drag to place, replay, settings
   ==================================================================== */
.board.placing .cell.ship, .fleet button { touch-action: none; }
.fleet button { cursor: grab; }
.cell.drag-ok { box-shadow: inset 0 0 0 3px var(--good); background: var(--sel-bg) !important; }
.cell.drag-bad { box-shadow: inset 0 0 0 3px var(--bad); background: #8a2f2f66 !important; }
body.dragging { cursor: grabbing; }

.replay-banner { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 16px; }
.replay-banner button { min-height: 34px; padding: 4px 12px; font-size: 13px; }
.cell.flash { animation: flash .6s ease-out; box-shadow: inset 0 0 0 3px var(--warn); z-index: 2; }
.cell.flashed { box-shadow: inset 0 0 0 2px var(--warn); }
@keyframes flash { 0% { box-shadow: inset 0 0 0 8px var(--warn), 0 0 14px var(--warn); } 100% { box-shadow: inset 0 0 0 3px var(--warn); } }

.gear { position: absolute; top: calc(10px + var(--safe-t)); right: 12px; min-height: 40px; padding: 6px 12px; }
.backdrop { position: fixed; inset: 0; background: #0007; z-index: 40; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; max-width: 520px; margin: 0 auto; z-index: 41;
  background: var(--panel); border: 1px solid var(--line); border-radius: 18px 18px 0 0;
  padding: 14px 16px calc(18px + var(--safe-b)); box-shadow: 0 -8px 30px var(--shadow);
  animation: slideup .22s ease-out;
}
.sheet h2 { margin-top: 0; }
@keyframes slideup { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
