:root {
  --bg: #0b1d33;
  --panel: #12304f;
  --water: #1c4f7c;
  --water-hover: #2a6aa3;
  --ship: #8a99a8;
  --hit: #e2463b;
  --miss: #d7e3ee;
  --sunk: #6b1f1a;
  --text: #eef4fa;
  --muted: #9fb5ca;
  --accent: #f5b83d;
  --cell: clamp(26px, 4.2vw, 38px);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: radial-gradient(circle at top, #163a62, var(--bg) 60%);
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
}

header { text-align: center; padding: 1.5rem 1rem 0.5rem; }
h1 { margin: 0; font-size: 2.4rem; letter-spacing: 0.2em; text-transform: uppercase; }
.tagline { margin: 0.3rem 0 0; color: var(--muted); }

main { width: 100%; max-width: 1000px; padding: 0 1rem; }

.panel {
  background: var(--panel);
  border-radius: 10px;
  padding: 0.8rem 1rem;
  margin: 1rem 0;
}

#setup-controls, #battle-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  align-items: center;
  justify-content: center;
}

button, select {
  font: inherit;
  color: var(--text);
  background: #1f4a75;
  border: 1px solid #3a6c9c;
  border-radius: 6px;
  padding: 0.45rem 0.9rem;
  cursor: pointer;
  touch-action: manipulation;
}
button:not(.cell):hover:not(:disabled), select:hover { background: #29608f; }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--accent); color: #1b1b1b; border-color: var(--accent); font-weight: 700; }
button.primary:hover:not(:disabled) { background: #ffcb61; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.badge { padding: 0.3rem 0.7rem; border-radius: 999px; background: #0d2440; color: var(--accent); font-weight: 600; }
#rotate-btn { min-width: 12em; }

.status { text-align: center; font-size: 1.15rem; min-height: 1.6em; margin: 0.5rem 0; }

.boards { display: flex; flex-wrap: wrap; justify-content: center; gap: 2rem; }
.board-wrap { display: flex; flex-direction: column; align-items: center; }
.board-wrap h2 { margin: 0.2rem 0 0.6rem; font-size: 1.1rem; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }

.board {
  display: grid;
  grid-template-columns: repeat(11, var(--cell));
  grid-template-rows: repeat(11, var(--cell));
  gap: 2px;
}

.label {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  color: var(--muted);
  user-select: none;
}

.cell {
  padding: 0;
  border: none;
  border-radius: 3px;
  background: var(--water);
  position: relative;
  cursor: default;
}
.board.enemy.active .cell:not(.miss):not(.hit):not(.sunk) { cursor: crosshair; }
.board.enemy.active .cell:not(.miss):not(.hit):not(.sunk):hover { background: var(--water-hover); }
.board.placing .cell { cursor: pointer; }
.board.placing .cell.ship { touch-action: none; cursor: grab; }

.cell.ship { background: var(--ship); }
.cell.preview { background: #4fb37a; }
.cell.preview.invalid { background: #b0503e; }
.cell.selected { box-shadow: inset 0 0 0 3px var(--accent); }
.cell.hit { background: var(--hit); }
.cell.sunk { background: var(--sunk); }
.cell.miss::after, .cell.hit::after, .cell.sunk::after {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  border-radius: 50%;
}
.cell.miss::after { width: 30%; height: 30%; background: var(--miss); opacity: 0.8; }
.cell.hit::after, .cell.sunk::after { width: 45%; height: 45%; background: #ffd9a8; opacity: 0.9; }
.cell.sunk::after { background: #f08a5d; }
.cell.last-shot { outline: 2px solid var(--accent); outline-offset: -2px; }
.cell.revealed { background: #5b6a79; }

.fleet { list-style: none; padding: 0; margin: 0.8rem 0 0; display: flex; flex-wrap: wrap; gap: 0.4rem; justify-content: center; max-width: calc(var(--cell) * 11 + 20px); }
.fleet li {
  padding: 0.25rem 0.6rem;
  border-radius: 6px;
  background: #173b61;
  font-size: 0.85rem;
}
.fleet li.selectable { cursor: pointer; border: 1px solid #3a6c9c; }
.fleet li.selectable:hover { background: #22507f; }
.fleet li.current { border-color: var(--accent); color: var(--accent); }
.fleet li.placed { opacity: 0.55; }
.fleet li.sunk { text-decoration: line-through; color: var(--hit); opacity: 0.8; }

.stats { text-align: center; }
.stats h2 { margin: 0 0 0.3rem; font-size: 1rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.1em; }
.stats p { margin: 0; }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(3, 10, 20, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.overlay[hidden] { display: none; }
.modal {
  background: var(--panel);
  border: 2px solid var(--accent);
  border-radius: 12px;
  padding: 1.5rem 2rem;
  text-align: center;
  max-width: 420px;
}
.modal h2 { margin-top: 0; font-size: 1.8rem; }
.modal button { margin: 0.3rem; }

footer { margin: auto 0 1rem; padding-top: 1.5rem; }
footer a { color: var(--muted); }

[hidden] { display: none !important; }

/* ---------- Effects ---------- */

header { position: relative; width: 100%; max-width: 1000px; }
.mute { position: absolute; top: 1.5rem; right: 1rem; font-size: 0.85rem; padding: 0.3rem 0.7rem; }

@media (max-width: 600px) {
  .mute { position: static; margin-top: 0.6rem; }
  h1 { font-size: 1.9rem; letter-spacing: 0.15em; }
}

.board-wrap { position: relative; }

.fx { position: absolute; inset: 0; pointer-events: none; border-radius: 3px; }

.fx.burst {
  z-index: 2;
  background: radial-gradient(circle, #fff6c2 0%, #ffb347 28%, #e2463b 52%, rgba(226, 70, 59, 0) 70%);
  animation: burst 0.6s ease-out forwards;
}
@keyframes burst {
  0% { transform: scale(0.2); opacity: 1; }
  60% { transform: scale(1.9); opacity: 0.9; }
  100% { transform: scale(2.4); opacity: 0; }
}

.fx.splash {
  z-index: 2;
  border: 2px solid var(--miss);
  border-radius: 50%;
  animation: splash 0.6s ease-out forwards;
}
@keyframes splash {
  0% { transform: scale(0.1); opacity: 1; }
  100% { transform: scale(1.7); opacity: 0; }
}

.fx.sink-flash { z-index: 1; background: #ffcf70; animation: sink-flash 0.8s ease-out both; }
@keyframes sink-flash {
  0% { opacity: 0; }
  25% { opacity: 0.95; }
  100% { opacity: 0; }
}

.fx.crosshair {
  z-index: 3;
  border: 2px solid var(--accent);
  border-radius: 50%;
  animation: aim 0.55s ease-in forwards;
}
.fx.crosshair::before, .fx.crosshair::after {
  content: "";
  position: absolute;
  background: var(--accent);
}
.fx.crosshair::before { left: 50%; top: -25%; bottom: -25%; width: 2px; margin-left: -1px; }
.fx.crosshair::after { top: 50%; left: -25%; right: -25%; height: 2px; margin-top: -1px; }
@keyframes aim {
  0% { transform: scale(2.4) rotate(-90deg); opacity: 0; }
  70% { transform: scale(0.9) rotate(0deg); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

.board.shake { animation: shake 0.4s ease-in-out; }
@keyframes shake {
  0%, 100% { transform: translate(0, 0); }
  20% { transform: translate(-4px, 1px); }
  40% { transform: translate(4px, -1px); }
  60% { transform: translate(-3px, 0); }
  80% { transform: translate(2px, 1px); }
}

/* Hits on ships still afloat flicker like fire; sunk ships stay still. */
.cell.hit::after { animation: ember 1.4s ease-in-out infinite alternate; }
@keyframes ember {
  from { opacity: 0.6; transform: scale(0.85); }
  to { opacity: 1; transform: scale(1.05); }
}

.banner {
  position: absolute;
  top: 45%;
  left: 50%;
  z-index: 5;
  padding: 0.6rem 1.2rem;
  border-radius: 8px;
  font-size: 1.4rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  white-space: nowrap;
  pointer-events: none;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  animation: banner 1.8s ease-out forwards;
}
.banner.good { background: var(--accent); color: #1b1b1b; }
.banner.bad { background: var(--hit); color: #fff; }
@keyframes banner {
  0% { transform: translate(-50%, -50%) scale(0.3); opacity: 0; }
  15% { transform: translate(-50%, -50%) scale(1.12); opacity: 1; }
  25% { transform: translate(-50%, -50%) scale(1); }
  80% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
  100% { transform: translate(-50%, -75%) scale(1); opacity: 0; }
}

@media (max-width: 939px) {
  .banner { position: fixed; }
}

.status.pop { animation: pop 0.35s ease-out; }
@keyframes pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.06); }
  100% { transform: scale(1); }
}

.modal { animation: modal-in 0.35s ease-out; }
@keyframes modal-in {
  from { transform: translateY(20px) scale(0.95); opacity: 0; }
  to { transform: none; opacity: 1; }
}

.confetti { position: fixed; inset: 0; z-index: 20; overflow: hidden; pointer-events: none; }
.confetti span {
  position: absolute;
  top: -14px;
  width: 8px;
  height: 14px;
  border-radius: 2px;
  animation-name: fall;
  animation-timing-function: linear;
  animation-fill-mode: forwards;
}
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }

.defeat-flash {
  position: fixed;
  inset: 0;
  z-index: 20;
  pointer-events: none;
  background: radial-gradient(circle, rgba(0, 0, 0, 0) 40%, rgba(160, 20, 20, 0.6) 100%);
  animation: defeat-flash 1.5s ease-out forwards;
}
@keyframes defeat-flash {
  0% { opacity: 0; }
  20% { opacity: 1; }
  100% { opacity: 0; }
}

/* ---------- Legend ---------- */

.legend {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.6rem 1.2rem;
  margin: 1rem 0 0;
  padding: 0;
  font-size: 0.85rem;
  color: var(--muted);
}
.legend li { display: flex; align-items: center; gap: 0.4rem; }
.legend .swatch { display: inline-block; width: 18px; height: 18px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
