:root {
  --bg: #0a1420;
  --panel: #122334;
  --panel-2: #172c40;
  --line: #24405a;
  --ink: #e8f1f8;
  --dim: #8aa4bd;
  --accent: #38bdf8;
  --ship: #3f6389;
  --ship-own: #2dd4bf;
  --hit: #f0563f;
  --miss: #56779a;
  --gold: #f7c948;
  /* The 10-wide grid has to fit the narrowest phone without a sideways scroll:
     labels + 10 cells + gaps + page padding = 72px of overhead. */
  --cell: clamp(21px, calc((100vw - 72px) / 10), 32px);
  --lab: 18px;
  --radius: 12px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100dvh;
  background:
    radial-gradient(1100px 700px at 50% -10%, #16344d 0%, transparent 60%),
    var(--bg);
  color: var(--ink);
  font: 16px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

.screen {
  max-width: 760px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px calc(24px + env(safe-area-inset-bottom));
}
[hidden] { display: none !important; }

/* ---------- home ---------- */

.logo {
  margin: 12vh 0 0;
  text-align: center;
  font-size: clamp(34px, 11vw, 54px);
  font-weight: 800;
  letter-spacing: -0.02em;
}
.logo span { color: var(--accent); }
.tagline { margin: 6px 0 28px; text-align: center; color: var(--dim); }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  display: grid;
  gap: 10px;
}
label { font-size: 13px; color: var(--dim); }

input {
  width: 100%;
  padding: 13px 14px;
  font-size: 16px;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  font-family: inherit;
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

#code-input {
  text-transform: uppercase;
  letter-spacing: 0.35em;
  text-align: center;
  font-weight: 700;
  font-size: 20px;
}

button {
  padding: 13px 16px;
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  color: var(--ink);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); color: #05233a; border-color: transparent; }
button.ghost { background: none; border: none; font-size: 20px; padding: 6px 10px; color: var(--dim); }

.or {
  display: flex; align-items: center; gap: 12px;
  color: var(--dim); font-size: 13px; margin: 4px 0;
}
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: var(--line); }

.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; }

.rules { margin-top: 22px; color: var(--dim); font-size: 14px; }
.rules summary { cursor: pointer; }
.rules ol { padding-left: 20px; }

/* ---------- game shell ---------- */

.bar { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }

.code-chip {
  display: flex; align-items: baseline; gap: 8px;
  padding: 7px 12px; border-radius: 999px;
}
.code-chip .label { font-size: 10px; letter-spacing: 0.18em; color: var(--dim); }
.code-chip strong { font-size: 19px; letter-spacing: 0.22em; font-weight: 800; }

.opponent { margin-left: auto; font-size: 13px; color: var(--dim); }
.opponent::before {
  content: ""; display: inline-block; width: 7px; height: 7px;
  border-radius: 50%; background: var(--dim); margin-right: 6px; vertical-align: middle;
}
.opponent.here::before { background: #34d399; }

.status {
  margin: 4px 0 14px;
  font-size: clamp(17px, 4.6vw, 21px);
  font-weight: 700;
}
.status.go { color: var(--accent); }
.status.win { color: var(--gold); }

.board-wrap { margin-bottom: 16px; }
.board-wrap h2 {
  margin: 0 0 8px; font-size: 12px; font-weight: 700;
  letter-spacing: 0.16em; text-transform: uppercase; color: var(--dim);
}

/* ---------- grid ---------- */

.grid {
  display: grid;
  grid-template-columns: var(--lab) repeat(10, var(--cell));
  grid-auto-rows: var(--cell);
  gap: 2px;
  width: max-content;
  max-width: 100%;
}
.grid .lab {
  display: grid; place-items: center;
  font-size: 10px; color: var(--dim);
}
.grid .lab.row { height: var(--cell); }

.cell {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 3px;
  position: relative;
}
#target.live .cell { cursor: crosshair; }
#target.live .cell:hover { background: var(--panel-2); border-color: var(--accent); }
/* Dragging a hull must not scroll the page out from under the finger. */
#own.live { touch-action: none; }
#own.live .cell { cursor: cell; }
#own.live .cell.ship { cursor: grab; }

.cell.ship { background: var(--ship); border-color: #547aa5; }
#own .cell.ship { background: var(--ship-own); border-color: #5eead4; }

/* While placing, a hull is drawn as one continuous shape that bleeds across the
   grid gaps between its own squares and stops short at its ends. Without this a
   ship lying end-to-end against another reads as a single longer ship, which is
   exactly the thing you need to see before you tap one to turn it. */
#own.live .cell.ship { background: none; border-color: var(--line); }
#own.live .cell.ship::before {
  content: ""; position: absolute; border-radius: 4px; background: var(--ship-own);
}
#own.live .cell.hull-h::before { top: 3px; bottom: 3px; left: -4px; right: -4px; }
#own.live .cell.hull-v::before { left: 3px; right: 3px; top: -4px; bottom: -4px; }
#own.live .cell.cap-l::before { left: 3px; border-radius: 10px 4px 4px 10px; }
#own.live .cell.cap-r::before { right: 3px; border-radius: 4px 10px 10px 4px; }
#own.live .cell.cap-t::before { top: 3px; border-radius: 10px 10px 4px 4px; }
#own.live .cell.cap-b::before { bottom: 3px; border-radius: 4px 4px 10px 10px; }
#own.live .cell.cap-all::before { inset: 3px; border-radius: 50%; }
#own.live .cell.lifted::before { box-shadow: 0 0 0 2px var(--accent); }
.cell.lifted { z-index: 1; }
.cell.preview { background: #2b5f78; border-color: var(--accent); }
.cell.bad { background: #6b2b2b; border-color: var(--hit); }

.cell.miss::after {
  content: ""; position: absolute; inset: 0; margin: auto;
  width: 26%; height: 26%; border-radius: 50%; background: var(--miss);
}
.cell.hit { background: var(--hit); border-color: #ff8a76; }
.cell.hit::after {
  content: ""; position: absolute; inset: 0; margin: auto;
  width: 34%; height: 34%; border-radius: 50%;
  background: #2a0b06;
}
.cell.flash { animation: pop 0.5s ease-out; }
@keyframes pop {
  0% { transform: scale(1.5); box-shadow: 0 0 0 4px rgba(247,201,72,0.6); }
  100% { transform: scale(1); box-shadow: none; }
}

/* Your own grid is reference, not the thing you act on: shrink it in play. */
#own-wrap.secondary { --cell: clamp(16px, calc((100vw - 72px) / 13), 25px); --lab: 14px; opacity: 0.92; }

/* ---------- fleet + controls ---------- */

/* One row per ship size, used as the placement tray and as the "still afloat"
   readout in play, so the two always read the same way. */
.fleet, .tray { display: grid; gap: 5px; margin: 10px 0 0; }
.ship-row { display: flex; align-items: center; gap: 5px; flex-wrap: wrap; }
.ship-label {
  flex: none; width: 76px;
  font-size: 10px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--dim);
}
.glyph {
  display: inline-flex; gap: 2px; padding: 3px;
  background: none; border: 1px solid transparent; border-radius: 7px;
}
.glyph i { width: 8px; height: 12px; border-radius: 2px; background: var(--ship); }
.glyph.sunk i { background: var(--hit); }
.glyph.sunk { opacity: 0.65; }

.tray .glyph { padding: 6px; cursor: pointer; border-color: var(--line); background: var(--panel); }
.tray .glyph i { width: 11px; height: 16px; background: var(--ship-own); }
.tray .glyph.done { opacity: 0.35; }
.tray .glyph.done i { background: #3d5b73; }
.tray .glyph.current { border-color: var(--accent); background: #1d3d57; opacity: 1; }
.tray .glyph.current i { background: var(--ship-own); }

.buttons { display: flex; flex-wrap: wrap; gap: 8px; }
.buttons button { flex: 1 1 auto; min-width: 92px; }

.log {
  list-style: none; padding: 12px; margin: 0;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  font-size: 13px; color: var(--dim); display: grid; gap: 4px;
}
.log li:last-child { color: var(--ink); }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: #2a1616; border: 1px solid var(--hit); color: #ffd9d2;
  padding: 10px 16px; border-radius: 999px; font-size: 14px;
  max-width: 90vw; text-align: center; z-index: 10;
}

/* ---------- larger screens ---------- */

@media (min-width: 700px) {
  :root { --cell: 32px; --lab: 22px; }
  #game { display: grid; grid-template-columns: 1fr 1fr; gap: 0 24px; align-items: start; }
  .bar, .status, .controls { grid-column: 1 / -1; }
  #own-wrap.secondary { --cell: 32px; --lab: 22px; opacity: 1; }
  .fleet { font-size: 12px; }
}
@media (min-width: 960px) {
  :root { --cell: 38px; }
  #game { max-width: 900px; }
}

.toast.ok { background: #10281f; border-color: #34d399; color: #c9f7e5; }
