/* ============================================================
   BATTLESHIP — Fleet Command
   Sonar-console theme: deep-sea ink, phosphor teal, amber fire.
   ============================================================ */

:root {
  --ink:        #06121f;
  --ink-2:      #0a1c2e;
  --panel:      #0d2338;
  --line:       #1c3a55;
  --teal:       #37f2d4;
  --teal-dim:   #1e8f80;
  --amber:      #ffb454;
  --fire:       #ff6b3d;
  --red:        #ff4757;
  --text:       #d7e6f2;
  --muted:      #7e99ad;
  --label-w:    1.4rem;
  --radius:     10px;
  --font-ui:    "Avenir Next", "Segoe UI", system-ui, sans-serif;
  --font-mono:  ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: var(--font-ui);
  color: var(--text);
  background:
    radial-gradient(1200px 700px at 70% -10%, #10314d 0%, transparent 60%),
    radial-gradient(900px 600px at 0% 110%, #0d2a3f 0%, transparent 55%),
    var(--ink);
  min-height: 100%;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* ---------------- top bar ---------------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.7rem 1rem;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(13,35,56,.9), rgba(10,28,46,.7));
  backdrop-filter: blur(4px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.title {
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  color: var(--teal);
  text-shadow: 0 0 18px rgba(55,242,212,.45);
  user-select: none;
}

.title::before { content: "⚓ "; letter-spacing: 0; }

.turn-indicator {
  flex: 1;
  text-align: center;
  font-family: var(--font-mono);
  font-size: clamp(0.78rem, 2.4vw, 0.95rem);
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

body.turn-enemy .turn-indicator::after {
  content: "";
  animation: dots 1.2s steps(4) infinite;
}
@keyframes dots { 0%{content:""} 25%{content:"."} 50%{content:".."} 75%{content:"..."} }

.icon-btn {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  font-size: 1rem;
  padding: 0.4rem 0.55rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { border-color: var(--teal-dim); }
.icon-btn .ic-off { display: none; }
.icon-btn.muted .ic-off { display: block; }
.icon-btn.muted .ic-on { display: none; }

/* ---------------- layout ---------------- */

.layout {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  gap: 1.4rem;
  justify-content: center;
  align-items: flex-start;
  padding: 1.2rem 1rem 2rem;
  max-width: 1500px;
  margin: 0 auto;
  width: 100%;
}

.board-panel {
  width: min(44vw, 470px);
  min-width: 300px;
}

.console {
  width: 300px;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.panel-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.45rem;
}

.panel-title {
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.fleet-count {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--teal);
}

.hint { font-size: 0.72rem; color: var(--muted); }

kbd {
  font-family: var(--font-mono);
  font-size: 0.72em;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 0.3em;
  background: var(--ink-2);
}

/* ---------------- board ---------------- */

.board {
  position: relative;
  padding: var(--label-w) 0 0 var(--label-w);
}

.col-labels, .row-labels {
  position: absolute;
  display: flex;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  color: var(--muted);
  user-select: none;
}
.col-labels {
  top: 0; left: var(--label-w); right: 0; height: var(--label-w);
}
.row-labels {
  left: 0; top: var(--label-w); bottom: 0; width: var(--label-w);
  flex-direction: column;
}
.col-labels span, .row-labels span {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.grid {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  grid-template-rows: repeat(10, 1fr);
  gap: 1px;
  background: var(--line);
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0,0,0,.45), inset 0 0 60px rgba(6,18,31,.5);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

/* faint CRT scanlines over the water */
.grid::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 6;
  pointer-events: none;
  background: repeating-linear-gradient(0deg,
    rgba(255,255,255,.022) 0 1px, transparent 1px 3px);
}

.cell {
  position: relative;
  background: rgba(15, 42, 63, 0.85);
}

/* hover crosshair on enemy waters while it is your turn */
body.phase-battle.turn-player #enemy-grid .cell:not(.hit):not(.miss) {
  cursor: crosshair;
}
body.phase-battle.turn-player #enemy-grid .cell:not(.hit):not(.miss):hover {
  background: rgba(55, 242, 212, 0.18);
  box-shadow: inset 0 0 0 1px var(--teal);
}

/* active board glow */
body.turn-player #enemy-board .grid {
  outline: 1px solid rgba(55,242,212,.35);
  outline-offset: 3px;
}
body.turn-enemy #player-board .grid {
  outline: 1px solid rgba(255,107,61,.4);
  outline-offset: 3px;
}
body.phase-placement #player-board .grid {
  outline: 1px solid rgba(55,242,212,.35);
  outline-offset: 3px;
}

/* ---------------- markers ---------------- */

.cell.miss::after {
  content: "";
  position: absolute;
  inset: 32%;
  border-radius: 50%;
  background: #bfe6ff;
  box-shadow: 0 0 6px rgba(191,230,255,.8);
  animation: pop-in .25s ease-out;
}

.cell.hit { background: rgba(255, 107, 61, 0.14); }

/* hit pegs live in the peg layer, above ship overlays */
.peg {
  position: absolute;
  transform: translate(-50%, -50%);
  pointer-events: none;
}
.peg-hit {
  width: 6.5%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: radial-gradient(circle, #ffe08a 0%, var(--fire) 55%, #b3261e 100%);
  box-shadow: 0 0 14px rgba(255,107,61,.9);
  animation: pop-in .25s ease-out, flicker 1.1s ease-in-out .3s infinite alternate;
}

@keyframes pop-in {
  from { transform: scale(0.2); opacity: 0; }
  to   { transform: scale(1);   opacity: 1; }
}
@keyframes flicker {
  from { filter: brightness(1); }
  to   { filter: brightness(1.35); }
}

/* transient FX elements (positioned by JS) */
.fx {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  transform: translate(-50%, -50%);
}
.fx-boom {
  width: 26%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: radial-gradient(circle, #fff3c4 0%, var(--amber) 35%, var(--fire) 65%, transparent 72%);
  animation: boom .55s ease-out forwards;
}
@keyframes boom {
  0%   { transform: translate(-50%,-50%) scale(.3); opacity: 1; }
  100% { transform: translate(-50%,-50%) scale(2.6); opacity: 0; }
}
.fx-splash {
  width: 22%;
  aspect-ratio: 1;
  border-radius: 50%;
  border: 2px solid rgba(191,230,255,.9);
  animation: splash .5s ease-out forwards;
}
@keyframes splash {
  0%   { transform: translate(-50%,-50%) scale(.3); opacity: 1; }
  100% { transform: translate(-50%,-50%) scale(2.2); opacity: 0; }
}

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

/* ---------------- ships ---------------- */

.layer { position: absolute; inset: 0; pointer-events: none; }

.ship {
  position: absolute;
  border-radius: 999px;
  background: linear-gradient(180deg, #5d7f97 0%, #3b5a72 45%, #24405a 100%);
  border: 1px solid #6f93ad;
  box-shadow: inset 0 2px 3px rgba(255,255,255,.25),
              inset 0 -3px 5px rgba(0,0,0,.4),
              0 2px 6px rgba(0,0,0,.5);
  pointer-events: none;
  touch-action: none;
}

/* only your own ships are grabbable, and only during placement */
body.phase-placement #player-layer .ship { pointer-events: auto; cursor: grab; }

/* deck stripes */
.ship::before {
  content: "";
  position: absolute;
  inset: 22% 12%;
  border-radius: 999px;
  background: repeating-linear-gradient(90deg,
    transparent 0 14%, rgba(0,0,0,.22) 14% 16%);
}
.ship.vertical::before {
  background: repeating-linear-gradient(0deg,
    transparent 0 14%, rgba(0,0,0,.22) 14% 16%);
}

.ship.sunk {
  background: linear-gradient(180deg, #7a2e2e 0%, #571d24 50%, #3a1218 100%);
  border-color: var(--red);
  animation: sunk-flash .8s ease-in-out 2;
}
@keyframes sunk-flash {
  0%,100% { filter: brightness(1); }
  50%     { filter: brightness(1.8); }
}

/* enemy ships are only drawn when sunk / game over */
#enemy-layer .ship { background: linear-gradient(180deg, #8a4a3a, #5c2a26); border-color: #b0604a; }
#enemy-layer .ship.hidden-ship { opacity: 0; }

/* placement ghost */
.ghost {
  position: absolute;
  border-radius: 999px;
  pointer-events: none;
  z-index: 4;
  border: 2px dashed var(--teal);
  background: rgba(55,242,212,.22);
}
.ghost.invalid {
  border-color: var(--red);
  background: rgba(255,71,87,.22);
}

/* ---------------- sonar sweep (enemy board) ---------------- */

.sweep {
  position: absolute;
  top: var(--label-w); left: var(--label-w); right: 0; bottom: 0;
  border-radius: 6px;
  overflow: hidden;
  pointer-events: none;
  z-index: 7;
}
.sweep::before {
  content: "";
  position: absolute;
  inset: -25%;
  background: conic-gradient(from 0deg, rgba(55,242,212,.20) 0deg, transparent 60deg);
  animation: sweep 6s linear infinite;
}
@keyframes sweep { to { transform: rotate(360deg); } }

/* ---------------- tray / console ---------------- */

.tray {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.7rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.tray-ship {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.35rem 0.4rem;
  border: 1px solid transparent;
  border-radius: 8px;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.tray-ship:hover { border-color: var(--line); background: var(--ink-2); }
.tray-ship.selected {
  border-color: var(--teal);
  background: rgba(55,242,212,.08);
  box-shadow: 0 0 12px rgba(55,242,212,.25);
}
.tray-ship.placed { opacity: 0.45; cursor: default; }

.mini {
  display: flex;
  gap: 2px;
}
.mini i {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  background: linear-gradient(180deg, #5d7f97, #24405a);
  border: 1px solid #6f93ad;
}
.tray-ship .sname {
  font-family: var(--font-mono);
  font-size: 0.74rem;
  color: var(--muted);
}

.controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  margin-top: 0.7rem;
}

.btn {
  font-family: var(--font-ui);
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.6rem 0.7rem;
  cursor: pointer;
  transition: border-color .15s, background .15s, transform .05s;
}
.btn:hover:not(:disabled) { border-color: var(--teal-dim); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-primary {
  grid-column: 1 / -1;
  border-color: var(--teal);
  letter-spacing: 0.06em;
}
.btn-primary:not(:disabled) {
  color: #052019;
  background: linear-gradient(180deg, #4df5d9 0%, #22bfa6 100%);
  box-shadow: 0 0 18px rgba(55,242,212,.35);
}

body.phase-battle #tray-panel,
body.phase-over #tray-panel { display: none; }

/* ---------------- log ---------------- */

.log {
  font-family: var(--font-mono);
  font-size: 0.76rem;
  line-height: 1.5;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.6rem 0.7rem;
  min-height: 4.5rem;
  max-height: 10rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.log div { padding: 0.08rem 0; }
.log .l-hit  { color: var(--amber); }
.log .l-miss { color: var(--muted); }
.log .l-sunk { color: var(--red); font-weight: 700; }
.log .l-info { color: var(--teal); }

/* ---------------- overlay (win/lose) ---------------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(4, 12, 21, 0.78);
  backdrop-filter: blur(3px);
  animation: fade-in .3s ease-out;
}
.overlay[hidden] { display: none; }
@keyframes fade-in { from { opacity: 0; } }

.overlay-card {
  text-align: center;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 2.2rem 2.6rem;
  box-shadow: 0 24px 60px rgba(0,0,0,.6);
  animation: card-in .35s cubic-bezier(.2,1.4,.4,1);
  max-width: min(90vw, 420px);
}
@keyframes card-in {
  from { transform: translateY(18px) scale(.95); opacity: 0; }
}

.overlay-title {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: 0.2em;
  text-transform: uppercase;
}
.overlay-title.win  { color: var(--teal); text-shadow: 0 0 24px rgba(55,242,212,.6); }
.overlay-title.lose { color: var(--red);  text-shadow: 0 0 24px rgba(255,71,87,.55); }

.overlay-sub { margin-top: 0.5rem; color: var(--muted); }

.overlay-stats {
  margin: 1.2rem 0 1.5rem;
  display: flex;
  gap: 1.4rem;
  justify-content: center;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--text);
}
.overlay-stats b { display: block; font-size: 1.25rem; color: var(--teal); }

/* ---------------- responsive ---------------- */

@media (max-width: 1180px) {
  .board-panel { width: min(92vw, 470px); }
  .console { width: min(92vw, 470px); }
  /* enemy board on top: it is where the action is */
  #enemy-panel { order: 1; }
  #player-panel { order: 2; }
  .console { order: 3; }
}

@media (min-width: 1181px) {
  #enemy-panel { order: 1; }
  .console { order: 2; }
  #player-panel { order: 3; }
}

@media (max-width: 420px) {
  :root { --label-w: 1.15rem; }
  .board-panel { min-width: 0; width: calc(100vw - 1.6rem); }
  .title { font-size: 1.05rem; }
}

/* ---------------- a11y ---------------- */

:focus-visible {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  .sweep::before { animation: none; }
  .shake { animation: none; }
  .peg-hit { animation: pop-in .25s ease-out; }
  .ship.sunk { animation: none; }
  * { scroll-behavior: auto !important; }
}
