:root {
  --top-h: 50vh; --gap: 8px; --bot-h: 50vh;
  --case: #5E666E; --frame: #454C53; --panel: #3E454C; --btn: #4A5158; --line: #6E7780;
  --ocean: #1E5AA0; --ocean-lit: #2F74C4; --deep: #123E70;
  --ship: #AEB6BE; --ship-edge: #7D868F; --ship-sel: #DDE2E6; --sunk: #737B83; --sunk-hole: #4E555C;
  --hit: #E03131; --fire: #C92A2A; --miss: #F4F5F6;
  --p1: #F2C14E; --p1-ink: #2A2100; --p2: #9CC9FF; --p2-ink: #0B2340;
  --me: var(--p1); --me-ink: var(--p1-ink);
  --ink: #F4F5F6; --ink-2: #D5DADF; --ink-3: #C9D0D6;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
  --display: 'Space Grotesk', Inter, system-ui, sans-serif;
  --cell: 30px; --pad: 14px;
}
body[data-seat="1"] { --me: var(--p2); --me-ink: var(--p2-ink); }

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: #0B0F13; color: var(--ink);
  font-family: Inter, system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent; -webkit-user-select: none; user-select: none;
  touch-action: manipulation; overscroll-behavior: none;
}
input { -webkit-user-select: text; user-select: text; }

#app { position: fixed; inset: 0; display: flex; flex-direction: column; }
.hinge { flex: none; height: var(--gap); background: #0B0F13; }
.screen {
  flex: none; position: relative; overflow: hidden;
  height: var(--top-h);
  padding: var(--pad);
  padding-left: max(var(--pad), env(safe-area-inset-left));
  padding-right: max(var(--pad), env(safe-area-inset-right));
  background: var(--case);
  display: flex; gap: 12px; align-items: stretch;
}
#top { padding-top: max(var(--pad), env(safe-area-inset-top)); }
#bottom { height: var(--bot-h); padding-bottom: max(var(--pad), env(safe-area-inset-bottom)); }

/* ---------- board ---------- */
.board {
  position: relative; flex: none; align-self: center;
  padding: 6px 8px 8px 6px; background: var(--frame); border-radius: 12px;
  display: flex; flex-direction: column; gap: 4px;
}
.cols { display: flex; gap: 2px; padding-left: 24px; font: 700 11px/14px var(--mono); color: #E4E8EB; }
.cols span { width: var(--cell); text-align: center; }
.bbody { display: flex; gap: 4px; }
.rows { display: flex; flex-direction: column; gap: 2px; padding-top: 2px; }
.rows span { width: 18px; height: var(--cell); display: flex; align-items: center; justify-content: center; font: 700 11px/1 var(--mono); color: #E4E8EB; }
.grid {
  display: grid; grid-template-columns: repeat(10, var(--cell)); grid-auto-rows: var(--cell);
  gap: 2px; padding: 2px; background: var(--deep); border-radius: 4px;
}
.board.dim .grid { opacity: 0.5; }

.cell {
  appearance: none; border: 0; margin: 0; padding: 0; font: inherit; color: inherit;
  width: var(--cell); height: var(--cell);
  display: flex; align-items: center; justify-content: center;
  background: var(--ocean); border-radius: 3px; position: relative;
}
button.cell { cursor: pointer; }
button.cell:disabled { cursor: default; }
.hole { width: max(4px, calc(var(--cell) * 0.2)); height: max(4px, calc(var(--cell) * 0.2)); border-radius: 50%; background: var(--deep); }
.cell.ship { background: var(--ship); border: 1px solid var(--ship-edge); border-radius: 2px; }
.cell.ship .hole { background: var(--ship-edge); }
.cell.ship.sunk { background: var(--sunk); }
.cell.ship.sunk .hole { background: var(--sunk-hole); }
.cell.h0 { border-radius: calc(var(--cell) * 0.45) 2px 2px calc(var(--cell) * 0.45); }
.cell.h1 { border-radius: 2px calc(var(--cell) * 0.45) calc(var(--cell) * 0.45) 2px; }
.cell.v0 { border-radius: calc(var(--cell) * 0.45) calc(var(--cell) * 0.45) 2px 2px; }
.cell.v1 { border-radius: 2px 2px calc(var(--cell) * 0.45) calc(var(--cell) * 0.45); }
.cell.ship.sel { background: var(--ship-sel); border: 2px solid var(--me); }
.cell.ghost { background: rgba(221, 226, 230, 0.8); }
.cell.ghost.bad { background: rgba(224, 49, 49, 0.65); }
.cell.aim { background: var(--ocean-lit); box-shadow: inset 0 0 0 2px var(--me); }
.cell.ring { box-shadow: inset 0 0 0 2px #F2C14E; }
.cell.aim svg { width: calc(var(--cell) * 0.78); height: calc(var(--cell) * 0.78); }
button.cell:not(:disabled):hover:not(.aim):not(.ship) { background: #2766AE; }

.peg {
  width: calc(var(--cell) * 0.48); height: calc(var(--cell) * 0.48); border-radius: 50%;
  box-shadow: inset -2px -3px 0 rgba(0, 0, 0, 0.28), 0 1px 2px rgba(0, 0, 0, 0.45);
}
.peg.hit { background: var(--hit); }
.peg.miss { background: var(--miss); box-shadow: inset -2px -3px 0 rgba(0, 0, 0, 0.18), 0 1px 2px rgba(0, 0, 0, 0.45); }
.peg.new { animation: peg-drop 0.4s cubic-bezier(.2, .9, .3, 1.3); }
@keyframes peg-drop { from { transform: scale(2.2); opacity: 0; } }

/* overlays on a board */
.overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; padding: 10px; }
.card {
  width: min(260px, 92%); padding: 18px 16px; border-radius: 14px;
  background: rgba(28, 32, 36, 0.94); display: flex; flex-direction: column; gap: 10px;
}
.card.center { align-items: center; text-align: center; }
.card.accent { border: 2px solid var(--me); }
.card.grey { border: 2px solid #8A939B; }
.card h1 { margin: 0; font: 700 34px/1 var(--display); letter-spacing: 0.01em; }
.card .duo { margin: 0; padding-left: 0.6em; font: 700 14px/1 var(--mono); letter-spacing: 0.6em; color: var(--me); }
.card p { margin: 0; font: 400 13px/1.4 Inter, sans-serif; color: var(--ink-2); }
.card .huge { font: 700 clamp(28px, calc(var(--cell) * 1.4), 52px)/1 var(--mono); letter-spacing: 0.16em; padding-left: 0.16em; }
.card .banner { font: 700 clamp(28px, calc(var(--cell) * 1.3), 44px)/1 var(--display); letter-spacing: 0.04em; }
.card .row { display: flex; align-items: center; gap: 8px; }
.wait { color: var(--me); font: 600 13px/1.2 Inter, sans-serif; }

.toast {
  position: absolute; left: 30px; right: 10px; bottom: 12px;
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 12px;
  background: rgba(28, 32, 36, 0.95); border: 2px solid var(--hit);
  animation: toast-in 0.25s ease-out;
}
.toast.miss { border-color: var(--miss); }
.toast .badge { flex: none; padding: 7px 9px; border-radius: 8px; background: var(--fire); color: #fff; font: 700 13px/1 var(--display); letter-spacing: 0.06em; }
.toast.miss .badge { background: var(--miss); color: #15191C; }
.toast b { display: block; font: 700 15px/1.15 var(--display); }
.toast small { display: block; margin-top: 3px; font: 500 12px/1.2 var(--mono); color: var(--ink-2); }
@keyframes toast-in { from { transform: translateY(12px); opacity: 0; } }

.opps { display: flex; flex-direction: column; gap: 8px; max-height: 60%; overflow: auto; }
.opp { display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 12px; background: var(--panel); }
.opp .av { width: 38px; height: 38px; flex: none; border-radius: 50%; background: var(--p1); color: var(--p1-ink); display: flex; align-items: center; justify-content: center; font: 700 12px/1 var(--mono); }
.opp .who { flex: 1; display: flex; flex-direction: column; gap: 4px; font: 600 14px/1 Inter, sans-serif; }
.opp .who small { font: 500 11px/1 var(--mono); color: var(--ink-2); }

/* ---------- rail ---------- */
.rail { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 8px; }
.plate { flex: none; padding: 10px 4px; border-radius: 10px; text-align: center; font: 700 12px/1 var(--mono); letter-spacing: 0.08em; background: var(--me); color: var(--me-ink); }
.plate.grey { background: #8A939B; color: #15191C; }
.plate.red { background: var(--fire); color: #fff; }
.panel { padding: 10px; border-radius: 12px; background: var(--panel); display: flex; flex-direction: column; gap: 7px; min-height: 0; }
.panel.grow { flex: 1; overflow: auto; }
.lbl { font: 700 10px/1.2 var(--mono); letter-spacing: 0.1em; color: var(--ink-3); }
.big { font: 700 28px/1 var(--mono); }
.big.me { color: var(--me); }
.big.dimmed { color: #8A939B; }
.note { margin: 0; font: 400 12px/1.4 Inter, sans-serif; color: var(--ink-2); overflow-wrap: anywhere; }
.spacer { flex: 1; }
.split { display: flex; justify-content: space-between; align-items: flex-end; gap: 6px; }
.two { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
.bar { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 4px; }
.bar i { height: 6px; border-radius: 3px; background: var(--line); }
.bar i.on { background: var(--p1); }
.bar.p2 i.on { background: var(--p2); }
.err { margin: 0; padding: 8px 10px; border-radius: 10px; background: rgba(201, 42, 42, 0.25); border: 1px solid var(--hit); font: 500 12px/1.3 Inter, sans-serif; }
.chip { flex: none; display: flex; align-items: center; gap: 6px; padding: 6px 8px; border-radius: 8px; background: rgba(28, 32, 36, 0.6); font: 500 11px/1.2 Inter, sans-serif; color: var(--ink-2); }
.chip::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: #F2994A; flex: none; }

.btn {
  appearance: none; margin: 0; font: 600 13px/1.2 Inter, sans-serif;
  min-height: 44px; padding: 6px 8px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--btn); color: var(--ink);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  text-align: center; cursor: pointer; flex: none;
}
.btn small { font: 500 11px/1 Inter, sans-serif; opacity: 0.9; }
.btn.primary, .btn.on { background: var(--me); color: var(--me-ink); border-color: transparent; font-weight: 700; }
.btn.tall { min-height: 52px; }
.btn.fire { min-height: 56px; border: 0; background: var(--fire); color: #fff; font: 700 19px/1 var(--display); letter-spacing: 0.1em; box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.25); }
.btn.danger { background: var(--fire); color: #fff; border-color: transparent; font-weight: 700; }
.btn.outline { background: var(--panel); border: 2px solid var(--me); color: var(--ink); flex-direction: row; gap: 8px; }
.btn:disabled:not(.on) { background: var(--btn); color: var(--ink-3); border-color: transparent; box-shadow: none; cursor: default; }
.btn:disabled.on { cursor: default; }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:focus-visible, .cell:focus-visible, input:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }

.portlist { display: grid; grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); gap: 6px; }
.port { align-items: flex-start; padding: 6px 8px; border-style: dashed; border-color: #8A939B; gap: 5px; min-height: 44px; font-size: 11px; }
.port .shipicon i { width: 9px; height: 9px; }
.port.on { background: var(--btn); color: var(--ink); border: 2px solid var(--me); }
.shipicon { display: flex; gap: 1px; }
.shipicon i { width: 14px; height: 11px; background: var(--ship); border: 1px solid var(--ship-edge); border-radius: 2px; }
.shipicon i:first-child { border-radius: 6px 2px 2px 6px; }
.shipicon i:last-child { border-radius: 2px 6px 6px 2px; }

.fleet { display: flex; flex-direction: column; gap: 7px; }
.frow { display: flex; justify-content: space-between; align-items: center; gap: 4px; font: 600 11px/1 Inter, sans-serif; }
.frow.sunk span { color: #FFB0B0; text-decoration: line-through; }
.pegs { display: flex; gap: 2px; flex: none; }
.pegs i { width: 8px; height: 8px; border-radius: 50%; background: var(--ship); }
.pegs i.on { background: var(--hit); }

.field { width: 100%; height: 46px; padding: 0 10px; border-radius: 10px; border: 1px solid var(--line); background: #2E343A; color: var(--ink); font: 700 20px/1 var(--mono); letter-spacing: 0.18em; }
.field::placeholder { color: #8A939B; }

.stat { display: flex; flex-direction: column; gap: 4px; }
.stat b { font: 700 22px/1 var(--mono); }
.stat b.red { color: #FF8A8A; }

/* ---------- gate ---------- */
#gate { position: fixed; inset: 0; z-index: 10; background: #0B0F13; display: flex; align-items: center; justify-content: center; padding: 24px; }
#gate[hidden] { display: none; }
.gate-card { max-width: 360px; display: flex; flex-direction: column; align-items: center; gap: 16px; text-align: center; }
.gate-card h1 { margin: 0; font: 700 32px/1 var(--display); }
.gate-card h1 span { color: var(--p1); font-family: var(--mono); letter-spacing: 0.3em; font-size: 18px; vertical-align: middle; }
.gate-card p { margin: 0; font: 400 15px/1.5 Inter, sans-serif; color: var(--ink-2); }
.gate-card .btn { width: 100%; }
