:root {
  --bg: #07131f;
  --panel: rgba(16, 38, 58, .78);
  --line: rgba(140, 200, 255, .18);
  --text: #e6f1fb;
  --muted: #8aa5bd;
  --accent: #38d6c4;
  --accent2: #ffb347;
  --hit: #ff4d3d;
  --good: #3ddc84;
  --bad: #ff5a5f;
  --dc: clamp(22px, 6.4vw, 32px);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  background:
    radial-gradient(1200px 700px at 70% -10%, #143a5c 0%, transparent 60%),
    radial-gradient(900px 600px at 0% 100%, #0b3b47 0%, transparent 55%),
    var(--bg);
  background-attachment: fixed;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

.hidden { display: none !important; }
h1, h2 { margin: 0; }
kbd {
  background: rgba(255,255,255,.1); border: 1px solid var(--line); border-radius: 4px;
  padding: 0 5px; font-size: .85em;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: rgba(7, 19, 31, .7); backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 50;
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--accent); }
.brand h1 { font-size: 1.25rem; letter-spacing: .18em; text-transform: uppercase; color: var(--text); }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.record { color: var(--muted); font-size: .85rem; font-variant-numeric: tabular-nums; }

.icon-btn {
  width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--line);
  background: rgba(255,255,255,.05); color: var(--text); display: grid; place-items: center; cursor: pointer;
}
.icon-btn:hover { background: rgba(255,255,255,.1); }
.icon-btn .ico-off { display: none; }
.icon-btn.muted .ico-on { display: none; }
.icon-btn.muted .ico-off { display: block; color: var(--muted); }

.btn {
  font: inherit; font-weight: 600; color: var(--text); cursor: pointer;
  min-height: 44px; padding: 0 16px; border-radius: 12px;
  border: 1px solid var(--line); background: rgba(255,255,255,.07);
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  transition: background .15s, transform .1s, box-shadow .15s;
}
.btn:hover:not(:disabled) { background: rgba(255,255,255,.14); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.ghost { background: transparent; }
.btn.primary {
  background: linear-gradient(180deg, #43e0cd, #1fae9d); color: #04211d; border-color: transparent;
  box-shadow: 0 6px 20px rgba(56, 214, 196, .25);
}
.btn.primary:hover:not(:disabled) { background: linear-gradient(180deg, #58ecda, #27bfae); }
.btn.big { min-height: 52px; font-size: 1.05rem; width: 100%; }
.btn:focus-visible, .icon-btn:focus-visible, select:focus-visible { outline: 2px solid var(--accent2); outline-offset: 2px; }
.badge {
  font-size: .72rem; font-weight: 700; padding: 2px 8px; border-radius: 999px;
  background: rgba(56, 214, 196, .18); color: var(--accent);
}

main { max-width: 1240px; margin: 0 auto; padding: 16px 16px 40px; }
.screen.hidden { display: none; }

.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: 18px;
  padding: 20px; backdrop-filter: blur(6px);
}
.board-title {
  font-size: .95rem; text-transform: uppercase; letter-spacing: .16em; color: var(--muted);
  margin-bottom: 8px; transition: color .3s;
}

/* ---------- board ---------- */
.board-host { container-type: inline-size; width: 100%; }
.board-wrap {
  --lab: clamp(12px, 5.5cqw, 26px);
  display: grid; grid-template-columns: var(--lab) 1fr; grid-template-rows: var(--lab) auto;
  position: relative;
}
.labels {
  display: grid; color: var(--muted); font-weight: 700;
  font-size: clamp(8px, 3cqw, 13px); align-items: center; justify-items: center;
}
.labels.cols { grid-template-columns: repeat(10, 1fr); }
.labels.rows { grid-template-rows: repeat(10, 1fr); }

.grid {
  position: relative; display: grid; grid-template: repeat(10, 1fr) / repeat(10, 1fr);
  aspect-ratio: 1; border-radius: 8px; overflow: hidden; isolation: isolate;
  border: 2px solid rgba(140, 200, 255, .35);
  background: linear-gradient(160deg, #11527f 0%, #0a3858 55%, #082a45 100%);
  transition: box-shadow .3s;
  touch-action: manipulation;
}
.grid::before {
  content: ''; position: absolute; inset: 0; z-index: 0; pointer-events: none;
  background: repeating-linear-gradient(115deg, rgba(255,255,255,.05) 0 2px, transparent 2px 16px);
  animation: drift 16s linear infinite;
}
@keyframes drift { to { background-position: 160px 0; } }

.cell { position: relative; box-shadow: inset 0 0 0 1px rgba(160, 210, 255, .15); outline: none; }
.enemy-grid .cell { cursor: default; }
.grid.active { box-shadow: 0 0 0 2px var(--accent), 0 0 26px rgba(56, 214, 196, .4); }
.grid.active .cell:not(.shot) { cursor: crosshair; }
@media (hover: hover) {
  .grid.active .cell:not(.shot):hover { background: rgba(56, 214, 196, .3); }
}
.enemy-grid .cell:focus-visible { outline: 2px solid var(--accent2); outline-offset: -2px; z-index: 6; }
.grid.incoming { box-shadow: 0 0 0 2px var(--bad), 0 0 26px rgba(255, 90, 95, .35); }

.ships { position: absolute; inset: 0; z-index: 2; pointer-events: none; }

/* ---------- ships ---------- */
.ship { position: absolute; filter: drop-shadow(0 3px 3px rgba(0, 0, 0, .5)); }
.ship .hull {
  position: absolute;
  background:
    radial-gradient(circle at 50% 50%, rgba(25, 40, 55, .6) 0 20%, transparent 23%) 0 0 / calc(100% / var(--len)) 100% repeat-x,
    linear-gradient(180deg, #b3c5d6 0%, #7d93a7 45%, #4e6276 100%);
}
.ship.h .hull {
  inset: 14% 1.5%;
  clip-path: polygon(0 0, calc(100% - var(--bow)) 0, 100% 50%, calc(100% - var(--bow)) 100%, 0 100%);
  border-radius: 6px 0 0 6px;
}
.ship.v .hull {
  inset: 1.5% 14%;
  clip-path: polygon(0 0, 100% 0, 100% calc(100% - var(--bow)), 50% 100%, 0 calc(100% - var(--bow)));
  border-radius: 6px 6px 0 0;
  background:
    radial-gradient(circle at 50% 50%, rgba(25, 40, 55, .6) 0 20%, transparent 23%) 0 0 / 100% calc(100% / var(--len)) repeat-y,
    linear-gradient(90deg, #b3c5d6 0%, #7d93a7 45%, #4e6276 100%);
}
.interactive .ship { pointer-events: auto; cursor: grab; touch-action: none; }
.ship.selected { filter: drop-shadow(0 0 6px var(--accent)) drop-shadow(0 3px 3px rgba(0,0,0,.5)); }
.ship.dragging { opacity: .25; }
.ship.nope { animation: nope .4s; }
@keyframes nope { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
.ship.preview { pointer-events: none; z-index: 3; }
.ship.preview.ok .hull { background: rgba(61, 220, 132, .55); }
.ship.preview.bad .hull { background: rgba(255, 90, 95, .55); }
.ship.ghost { position: fixed; z-index: 1000; pointer-events: none; opacity: .92; filter: drop-shadow(0 8px 10px rgba(0,0,0,.5)); }
.ship.ghost.bad .hull { background: linear-gradient(180deg, #ff9a9d, #d24a4e); }
.ship.sunk .hull { background:
    radial-gradient(circle at 50% 50%, rgba(0, 0, 0, .55) 0 20%, transparent 23%) 0 0 / calc(100% / var(--len)) 100% repeat-x,
    linear-gradient(180deg, #5a3a3a, #2b1b1b); }
.ship.v.sunk .hull { background:
    radial-gradient(circle at 50% 50%, rgba(0, 0, 0, .55) 0 20%, transparent 23%) 0 0 / 100% calc(100% / var(--len)) repeat-y,
    linear-gradient(90deg, #5a3a3a, #2b1b1b); }
.ship.revealed { opacity: .7; }
.ship.reveal-in { animation: revealIn .8s ease-out; }
@keyframes revealIn { from { opacity: 0; transform: scale(1.12); } to { opacity: 1; transform: none; } }
.ship.dock { position: relative; width: calc(var(--len) * var(--dc)); height: var(--dc); }

/* ---------- shot markers ---------- */
.peg { position: absolute; inset: 26%; border-radius: 50%; z-index: 3; animation: pegIn .3s cubic-bezier(.3, 1.6, .5, 1) both; pointer-events: none; }
.cell.miss .peg {
  inset: 33%;
  background: radial-gradient(circle at 35% 30%, #fff, #b7cbe0 60%, #8ea6bd);
  box-shadow: 0 1px 3px rgba(0, 0, 0, .4);
}
.cell.hit .peg {
  background: radial-gradient(circle at 35% 30%, #ffe0a8, #ff6b3d 45%, #c4210f);
  box-shadow: 0 0 10px 2px rgba(255, 90, 40, .75);
  animation: pegIn .3s cubic-bezier(.3, 1.6, .5, 1) both, flicker 1.1s ease-in-out .35s infinite;
}
.cell.sunk .peg {
  background: radial-gradient(circle at 35% 30%, #999, #4a4a4a 55%, #222);
  box-shadow: 0 0 8px rgba(255, 60, 40, .6);
  animation: pegIn .3s both;
}
@keyframes pegIn { from { transform: scale(0); } to { transform: scale(1); } }
@keyframes flicker { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.18); filter: brightness(1.3); } }

.burst {
  position: absolute; left: 50%; top: 50%; width: 320%; height: 320%; translate: -50% -50%;
  border-radius: 50%; z-index: 4; pointer-events: none;
  background: radial-gradient(circle, #fff6c9 0%, #ffb13d 25%, rgba(255, 80, 20, .7) 45%, transparent 70%);
  animation: boom .75s ease-out forwards;
}
@keyframes boom { from { transform: scale(0); opacity: 1; } to { transform: scale(1.4); opacity: 0; } }
.spark {
  position: absolute; left: 50%; top: 50%; width: 14%; height: 14%; border-radius: 50%;
  background: #ffcf5a; z-index: 4; pointer-events: none; box-shadow: 0 0 6px #ff8a2b;
  animation: spark .65s ease-out forwards;
}
@keyframes spark {
  from { transform: translate(-50%, -50%) scale(1); opacity: 1; }
  to { transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) scale(.2); opacity: 0; }
}
.ripple {
  position: absolute; left: 50%; top: 50%; width: 100%; height: 100%; translate: -50% -50%;
  border: 2px solid rgba(255, 255, 255, .85); border-radius: 50%; z-index: 4; pointer-events: none;
  animation: ripple .95s ease-out forwards;
}
.ripple.r2 { animation-delay: .18s; opacity: 0; animation-fill-mode: both; }
@keyframes ripple { from { transform: scale(.2); opacity: 1; } to { transform: scale(2.8); opacity: 0; } }

.cell.aim::after {
  content: ''; position: absolute; inset: 6%; z-index: 5; pointer-events: none;
  border: 2px solid var(--accent2); border-radius: 50%; box-shadow: 0 0 8px var(--accent2);
  background:
    linear-gradient(var(--accent2), var(--accent2)) 50% 0 / 2px 30% no-repeat,
    linear-gradient(var(--accent2), var(--accent2)) 50% 100% / 2px 30% no-repeat,
    linear-gradient(var(--accent2), var(--accent2)) 0 50% / 30% 2px no-repeat,
    linear-gradient(var(--accent2), var(--accent2)) 100% 50% / 30% 2px no-repeat;
  animation: aim .5s ease-out infinite;
}
@keyframes aim { from { transform: scale(2); opacity: .2; } to { transform: scale(1); opacity: 1; } }

.callout {
  position: absolute; left: 50%; top: 50%; translate: -50% -50%; z-index: 20; pointer-events: none;
  font-weight: 900; letter-spacing: .1em; font-size: clamp(22px, 9cqw, 56px);
  text-shadow: 0 2px 0 rgba(0,0,0,.5), 0 0 24px currentColor;
  animation: callout 1.15s ease-out forwards; white-space: nowrap;
}
.callout.hit { color: #ff6a4d; }
.callout.miss { color: #cfe4f7; }
.callout.sunk { color: #ffd166; }
@keyframes callout {
  0% { transform: scale(.4); opacity: 0; }
  18% { transform: scale(1.18); opacity: 1; }
  70% { transform: scale(1); opacity: 1; }
  100% { transform: translateY(-18px) scale(1); opacity: 0; }
}
.shake { animation: shake .45s; }
@keyframes shake {
  10%, 90% { transform: translate(-1px, 0); } 20%, 80% { transform: translate(3px, 1px); }
  30%, 50%, 70% { transform: translate(-5px, -1px); } 40%, 60% { transform: translate(5px, 1px); }
}

/* ---------- placement ---------- */
.place-layout {
  display: grid; grid-template-columns: minmax(0, 560px) minmax(300px, 380px); gap: 28px;
  justify-content: center; align-items: start;
}
.dock-panel h2 { font-size: 1.3rem; margin-bottom: 6px; }
.hint { color: var(--muted); font-size: .9rem; line-height: 1.45; margin: 0 0 14px; }
.dock { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; }
.dock-item {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 8px 12px; border-radius: 12px; border: 1px solid var(--line);
  background: rgba(255, 255, 255, .04); cursor: grab; touch-action: none; transition: background .15s, border-color .15s;
}
.dock-item:hover { background: rgba(255, 255, 255, .08); }
.dock-item.selected { border-color: var(--accent); background: rgba(56, 214, 196, .12); box-shadow: 0 0 0 1px var(--accent) inset; }
.dock-item.placed { opacity: .5; }
.dock-item.placed .dock-name::after { content: ' \2713'; color: var(--good); }
.dock-item.dragging { opacity: .3; }
.dock-name { font-weight: 600; font-size: .95rem; }
.dock-len { color: var(--muted); font-size: .8rem; margin-left: 6px; }
.place-count { margin: 8px 0 12px; color: var(--muted); font-size: .9rem; min-height: 1.3em; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.btn-row.center { justify-content: center; }
.btn-row .btn { flex: 1 1 auto; }
.field { display: block; margin-bottom: 14px; }
.field span { display: block; font-size: .8rem; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); margin-bottom: 6px; }
select {
  width: 100%; min-height: 44px; font: inherit; color: var(--text); padding: 0 12px; border-radius: 12px;
  background: #0d2438; border: 1px solid var(--line);
}

/* ---------- battle ---------- */
.status {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 14px; margin-bottom: 14px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 14px;
}
.turn-pill {
  display: inline-flex; align-items: center; gap: 10px; padding: 8px 16px; border-radius: 999px;
  font-weight: 700; white-space: nowrap; border: 1px solid transparent; transition: background .3s, color .3s;
}
.turn-pill .dot { width: 10px; height: 10px; border-radius: 50%; background: currentColor; animation: pulse 1.2s ease-in-out infinite; }
.turn-pill.player { background: rgba(56, 214, 196, .16); color: var(--accent); border-color: rgba(56, 214, 196, .4); }
.turn-pill.cpu { background: rgba(255, 90, 95, .14); color: #ff8b8f; border-color: rgba(255, 90, 95, .4); }
.turn-pill.over { background: rgba(255, 179, 71, .15); color: var(--accent2); border-color: rgba(255, 179, 71, .4); }
.turn-pill.over .dot { animation: none; }
@keyframes pulse { 50% { transform: scale(1.6); opacity: .5; } }

.log { list-style: none; margin: 0; padding: 0; flex: 1 1 260px; min-width: 0; font-size: .92rem; }
.log li { color: var(--muted); padding: 1px 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.log li:first-child { color: var(--text); font-weight: 600; }
.log li.hit:first-child { color: #ff8a70; }
.log li.sunk:first-child { color: #ffd166; }
.log li:nth-child(n+4) { display: none; }

.battle-layout {
  --bw: clamp(300px, min(44vw, 100vh - 330px), 560px);
  display: grid; grid-template-columns: repeat(2, minmax(0, var(--bw))); justify-content: center;
  column-gap: 32px; row-gap: 0;
  grid-template-areas: "eT oT" "eB oB" "eF oF";
}
.enemy-title { grid-area: eT; } .enemy-board { grid-area: eB; } .enemy-tracker { grid-area: eF; }
.own-title { grid-area: oT; } .own-board { grid-area: oB; } .own-tracker { grid-area: oF; }
.board-title.hot { color: var(--accent); }
.board-title.hot-bad { color: #ff8b8f; }

.tracker { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; align-content: flex-start; }
.chip {
  display: flex; align-items: center; gap: 8px; padding: 5px 10px; border-radius: 999px;
  background: rgba(255,255,255,.06); border: 1px solid var(--line); font-size: .8rem;
}
.chip.sunk { opacity: .7; border-color: rgba(255, 90, 95, .5); }
.chip.sunk .chip-name { text-decoration: line-through; }
.pips { display: inline-flex; gap: 2px; }
.pip { width: 9px; height: 9px; border-radius: 2px; background: rgba(255,255,255,.28); }
.pip.hit { background: var(--hit); box-shadow: 0 0 5px var(--hit); }

/* ---------- end dialog ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 200; display: grid; place-items: center; padding: 16px;
  background: rgba(3, 9, 15, .72); backdrop-filter: blur(5px); animation: fadeIn .4s;
}
@keyframes fadeIn { from { opacity: 0; } }
.dialog {
  width: min(460px, 100%); text-align: center; padding: 28px 24px;
  background: linear-gradient(180deg, #123350, #0a2136); border: 1px solid var(--line); border-radius: 22px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, .6); animation: pop .5s cubic-bezier(.2, 1.4, .4, 1);
}
@keyframes pop { from { transform: scale(.8); opacity: 0; } }
.dialog h2 { font-size: 2.2rem; letter-spacing: .06em; text-transform: uppercase; }
.dialog.win h2 { color: var(--accent2); text-shadow: 0 0 24px rgba(255, 179, 71, .5); }
.dialog.lose h2 { color: #ff7377; text-shadow: 0 0 24px rgba(255, 90, 95, .4); }
.end-emblem { font-size: 3rem; margin-bottom: 4px; line-height: 1; }
.end-emblem svg { width: 64px; height: 64px; }
.dialog.win .end-emblem { color: var(--accent2); } .dialog.lose .end-emblem { color: #ff7377; }
.end-sub { color: var(--muted); margin: 8px 0 18px; }
.end-stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 20px; }
.stat { background: rgba(255,255,255,.05); border: 1px solid var(--line); border-radius: 12px; padding: 10px; }
.stat b { display: block; font-size: 1.4rem; font-variant-numeric: tabular-nums; }
.stat span { font-size: .75rem; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); }

.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 300; overflow: hidden; }
.confetti i {
  position: absolute; top: -20px; width: 9px; height: 14px; opacity: .95;
  animation: fall linear forwards;
}
@keyframes fall { to { transform: translate(var(--x), 110vh) rotate(var(--r)); } }

/* ---------- responsive ---------- */
@media (max-width: 820px) {
  .place-layout { grid-template-columns: minmax(0, 1fr); gap: 16px; }
  .place-host { max-width: 520px; margin: 0 auto; }
  .dock-panel { padding: 16px; }
  .dock { display: grid; grid-template-columns: 1fr 1fr; }
  .dock-item { flex-direction: column; align-items: flex-start; }
}
@media (max-width: 760px) {
  main { padding: 10px 10px 32px; }
  .topbar { padding-left: 12px; padding-right: 12px; }
  .brand h1 { font-size: 1rem; letter-spacing: .12em; }
  .record { display: none; }
  .status { padding: 8px 10px; gap: 6px 10px; }
  .log { flex-basis: 100%; }
  .log li:nth-child(n+2) { display: none; }
  .battle-layout {
    grid-template-columns: 48% minmax(0, 1fr); column-gap: 12px;
    grid-template-areas: "eT eT" "eB eB" "eF eF" "oT oT" "oB oF";
  }
  .enemy-tracker { margin: 8px 0 12px; gap: 4px; flex-wrap: nowrap; }
  .enemy-tracker .chip { padding: 4px 6px; flex: 1 1 0; justify-content: center; }
  .chip-name { display: none; }
  .enemy-tracker .pip { width: 6px; height: 6px; }
  .own-tracker { flex-direction: column; flex-wrap: nowrap; margin-top: 0; padding-top: var(--lab, 16px); gap: 4px; }
  .own-tracker .chip { justify-content: space-between; padding: 3px 8px; font-size: .72rem; }
  .own-tracker .pip { width: 7px; height: 7px; }
  .dialog { padding: 22px 16px; }
  .dialog h2 { font-size: 1.8rem; }
}
@media (max-width: 380px) { .btn { padding: 0 10px; } }
@media (prefers-reduced-motion: reduce) {
  .grid::before, .turn-pill .dot, .cell.hit .peg { animation: none; }
  .shake { animation: none; }
}
