:root {
  --deep: #061820;
  --foam: #8ec8c0;
  --foam-dim: #5a9a92;
  --brass: #c4a35a;
  --brass-hot: #e0c070;
  --ink: #e8f2ef;
  --ink-dim: #9bb5b0;
  --danger: #d45a4a;
  --hit: #e07050;
  --ship: #1a6b6b;
  --grid: rgba(142, 200, 192, 0.22);
  --panel: rgba(8, 36, 46, 0.88);
  --font-display: "Bebas Neue", Impact, sans-serif;
  --font-body: "Source Sans 3", "Segoe UI", sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --dock-h: 0px;
  --pad: 0.75rem;
  --touch: 48px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  height: 100%;
  touch-action: manipulation;
}

body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
  font-family: var(--font-body);
  color: var(--ink);
  background: var(--deep);
  overflow-x: hidden;
  padding-bottom: calc(var(--dock-h) + var(--safe-bottom));
  overscroll-behavior: none;
}

body.placing {
  --dock-h: 7.25rem;
}

body.in-game .top {
  padding: calc(0.45rem + var(--safe-top)) var(--pad) 0.15rem;
}

body.in-game .brand h1 {
  font-size: 1.55rem;
}

body.in-game .tagline {
  display: none;
}

.sea {
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background:
    radial-gradient(ellipse 90% 55% at 50% -8%, #124858 0%, transparent 55%),
    linear-gradient(180deg, #072530 0%, #061820 50%, #041018 100%);
}

.wave {
  position: absolute;
  left: -10%;
  width: 120%;
  height: 140px;
  opacity: 0.14;
  background: repeating-linear-gradient(
    105deg,
    transparent 0 40px,
    rgba(142, 200, 192, 0.35) 40px 42px,
    transparent 42px 90px
  );
  animation: drift 22s linear infinite;
}

.wave-a { bottom: 10%; }
.wave-b { bottom: -4%; opacity: 0.08; animation-direction: reverse; animation-duration: 30s; }

@keyframes drift {
  from { transform: translateX(0); }
  to { transform: translateX(-70px); }
}

.top {
  text-align: center;
  padding: calc(0.9rem + var(--safe-top)) var(--pad) 0.25rem;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
}

.brand-mark {
  width: 0.95rem;
  height: 0.95rem;
  border: 2px solid var(--brass);
  border-radius: 50% 50% 45% 45%;
  box-shadow: inset 0 -5px 0 -2px var(--foam-dim);
  position: relative;
  flex-shrink: 0;
}

.brand-mark::after {
  content: "";
  position: absolute;
  top: -0.4rem;
  left: 50%;
  width: 2px;
  height: 0.45rem;
  background: var(--brass);
  transform: translateX(-50%);
}

.brand h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(2.2rem, 11vw, 3.4rem);
  letter-spacing: 0.06em;
  font-weight: 400;
  line-height: 1;
}

.tagline {
  margin: 0.25rem 0 0;
  color: var(--ink-dim);
  font-size: 0.88rem;
}

main {
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  padding: 0.5rem var(--pad) 1.25rem;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--grid);
  padding: 1.15rem;
  margin-top: 0.75rem;
}

.lobby .field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 1rem;
}

.lobby .field span,
.join-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-dim);
}

input {
  font: inherit;
  font-size: 16px; /* iOS: no zoom on focus */
  color: var(--ink);
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--grid);
  padding: 0.85rem 0.9rem;
  outline: none;
  border-radius: 0;
  width: 100%;
  min-height: var(--touch);
}

input:focus {
  border-color: var(--foam-dim);
}

.actions {
  display: grid;
  gap: 0.55rem;
}

.join-block {
  margin-top: 1.15rem;
  padding-top: 1.15rem;
  border-top: 1px solid var(--grid);
  display: grid;
  gap: 0.45rem;
}

.join-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.55rem;
}

.code-input {
  text-transform: none;
  letter-spacing: 0.35em;
  text-align: center;
  font-weight: 700;
  font-size: 1.75rem;
  min-height: 3.6rem;
  padding: 0.9rem 0.75rem;
  width: 100%;
}

.join-btn {
  min-height: 3.2rem;
}

.btn {
  font: inherit;
  font-weight: 600;
  font-size: 1rem;
  cursor: pointer;
  border: 1px solid var(--foam-dim);
  background: transparent;
  color: var(--ink);
  padding: 0.75rem 1rem;
  min-height: var(--touch);
  transition: background 0.12s, border-color 0.12s, opacity 0.12s;
  touch-action: manipulation;
  user-select: none;
}

.btn:active { background: rgba(142, 200, 192, 0.14); }
.btn:disabled { opacity: 0.4; pointer-events: none; }

.btn.lg {
  width: 100%;
  font-size: 1.05rem;
}

.btn.primary {
  background: linear-gradient(180deg, #1f7a72, #145a56);
  border-color: var(--foam-dim);
}

.btn.primary:active {
  background: linear-gradient(180deg, #17665f, #0f4a47);
}

.btn.tiny {
  padding: 0.35rem 0.7rem;
  font-size: 0.8rem;
  min-height: 2.2rem;
  margin-left: 0.35rem;
  vertical-align: middle;
}

.error {
  color: var(--danger);
  margin: 0.75rem 0 0;
  font-size: 0.92rem;
}

.status-bar {
  display: grid;
  gap: 0.35rem;
  margin-bottom: 0.75rem;
  padding: 0.55rem 0;
  border-bottom: 1px solid var(--grid);
}

.room-line {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.15rem;
}

.muted { color: var(--ink-dim); font-size: 0.8rem; margin-right: 0.3rem; }

.status-text {
  margin: 0;
  color: var(--brass-hot);
  font-weight: 600;
  font-size: 0.95rem;
  line-height: 1.35;
}

.place-tools {
  margin-bottom: 0.65rem;
}

.fleet {
  display: flex;
  gap: 0.45rem;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0.15rem 0 0.35rem;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.fleet::-webkit-scrollbar { display: none; }

.ship-chip {
  display: inline-flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.3rem;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--grid);
  background: rgba(0, 0, 0, 0.3);
  cursor: pointer;
  color: var(--ink);
  font: inherit;
  font-size: 0.78rem;
  min-height: var(--touch);
  min-width: 4.6rem;
  scroll-snap-align: start;
  flex-shrink: 0;
  touch-action: manipulation;
}

.ship-chip .bars {
  display: flex;
  gap: 2px;
}

.ship-chip .bars.vert {
  flex-direction: column;
}

.ship-chip .bars i {
  width: 11px;
  height: 11px;
  background: var(--ship);
  border: 1px solid var(--foam-dim);
}

.ship-chip.active {
  border-color: var(--brass);
  box-shadow: 0 0 0 1px var(--brass);
  background: rgba(196, 163, 90, 0.12);
}

.ship-chip.placed {
  opacity: 0.42;
}

.boards {
  display: grid;
  gap: 1.1rem;
}

.board-wrap {
  width: 100%;
}

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

.board-wrap h2 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 400;
  letter-spacing: 0.08em;
  font-size: 1.35rem;
  color: var(--foam);
}

.board-hint {
  font-size: 0.78rem;
  color: var(--ink-dim);
}

.board-frame {
  display: grid;
  grid-template-columns: 1.35rem 1fr;
  grid-template-rows: 1.35rem 1fr;
  width: 100%;
  max-width: min(100%, 92vw, 420px);
  margin: 0 auto;
  gap: 0;
}

.board-corner {
  grid-column: 1;
  grid-row: 1;
}

.board-cols {
  grid-column: 2;
  grid-row: 1;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  align-items: end;
  padding: 0 2px;
  gap: 2px;
}

.board-rows {
  grid-column: 1;
  grid-row: 2;
  display: grid;
  grid-template-rows: repeat(7, 1fr);
  justify-items: center;
  align-items: center;
  padding: 2px 0;
  gap: 2px;
}

.board-cols span,
.board-rows span {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--brass-hot);
  line-height: 1;
  user-select: none;
}

.board {
  grid-column: 2;
  grid-row: 2;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 2px;
  width: 100%;
  aspect-ratio: 1;
  background: rgba(0, 0, 0, 0.4);
  padding: 2px;
  border: 1px solid var(--grid);
  touch-action: none;
  user-select: none;
}

/* Battle: enemy board dominates on phone */
body.battle #boards {
  gap: 0.85rem;
}

body.battle #enemy-wrap {
  order: -1;
}

body.battle #own-wrap .board-frame {
  max-width: min(62vw, 240px);
  opacity: 0.92;
}

body.battle #own-wrap .board-cols span,
body.battle #own-wrap .board-rows span {
  font-size: 0.62rem;
}

body.battle #own-wrap h2 {
  font-size: 1.1rem;
}

/* Your turn — green blink on enemy board (shoot here) */
body.battle.my-turn #enemy-wrap .board {
  animation: blink-frame-green 1s ease-in-out infinite;
}

/* Opponent's turn — red blink on your board (under fire) */
body.battle.their-turn #own-wrap .board {
  animation: blink-frame-red 1s ease-in-out infinite;
}

@keyframes blink-frame-green {
  0%, 100% {
    box-shadow: 0 0 0 2px rgba(46, 200, 120, 0.25);
    outline: 2px solid rgba(46, 200, 120, 0.35);
    outline-offset: 2px;
  }
  50% {
    box-shadow: 0 0 0 3px rgba(46, 200, 120, 0.95), 0 0 14px rgba(46, 200, 120, 0.45);
    outline: 2px solid rgba(72, 230, 140, 1);
    outline-offset: 2px;
  }
}

@keyframes blink-frame-red {
  0%, 100% {
    box-shadow: 0 0 0 2px rgba(220, 70, 60, 0.25);
    outline: 2px solid rgba(220, 70, 60, 0.35);
    outline-offset: 2px;
  }
  50% {
    box-shadow: 0 0 0 3px rgba(230, 80, 70, 0.95), 0 0 14px rgba(220, 70, 60, 0.45);
    outline: 2px solid rgba(255, 100, 90, 1);
    outline-offset: 2px;
  }
}

@media (prefers-reduced-motion: reduce) {
  body.battle.my-turn #enemy-wrap .board {
    animation: none;
    box-shadow: 0 0 0 3px rgba(46, 200, 120, 0.9);
    outline: 2px solid rgba(72, 230, 140, 1);
    outline-offset: 2px;
  }
  body.battle.their-turn #own-wrap .board {
    animation: none;
    box-shadow: 0 0 0 3px rgba(230, 80, 70, 0.9);
    outline: 2px solid rgba(255, 100, 90, 1);
    outline-offset: 2px;
  }
  .wave, .cell.hit, .toast { animation: none; }
}

.cell {
  position: relative;
  background: rgba(20, 70, 82, 0.55);
  border: none;
  padding: 0;
  margin: 0;
  min-width: 0;
  min-height: 0;
  aspect-ratio: 1;
  cursor: default;
  touch-action: none;
}

.cell.interactive {
  cursor: pointer;
}

.cell.interactive:active,
.cell.press {
  background: rgba(142, 200, 192, 0.35);
}

.cell.preview { background: rgba(196, 163, 90, 0.5); }
.cell.preview-bad { background: rgba(212, 90, 74, 0.45); }
.cell.ship { background: var(--ship); box-shadow: inset 0 0 0 1px var(--foam-dim); }
.cell.hit {
  background: var(--hit);
  animation: pulse-hit 0.3s ease;
}
.cell.hit::after,
.cell.sunk::after {
  content: "";
  position: absolute;
  inset: 26%;
  background: #2a1010;
  clip-path: polygon(20% 0, 0 20%, 30% 50%, 0 80%, 20% 100%, 50% 70%, 80% 100%, 100% 80%, 70% 50%, 100% 20%, 80% 0, 50% 30%);
}
.cell.sunk { background: #8a3a30; }
.cell.miss::after {
  content: "";
  position: absolute;
  inset: 36%;
  border-radius: 50%;
  background: var(--foam-dim);
  opacity: 0.75;
}

@keyframes pulse-hit {
  from { transform: scale(0.82); }
  to { transform: scale(1); }
}

.place-dock {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  display: grid;
  gap: 0.45rem;
  padding: 0.55rem var(--pad) calc(0.55rem + var(--safe-bottom));
  background: rgba(6, 24, 32, 0.96);
  border-top: 1px solid var(--grid);
  backdrop-filter: blur(10px);
}

.orient {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.35rem;
  padding: 0.2rem;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--grid);
}

.orient-btn {
  min-height: 2.85rem;
  padding: 0.4rem 0.5rem;
  font-size: 0.92rem;
  border-color: transparent;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  background: transparent;
}

.orient-btn .orient-icon {
  font-size: 1.15rem;
  line-height: 1;
  color: var(--foam);
  font-weight: 700;
}

.orient-btn.active {
  background: rgba(31, 122, 114, 0.55);
  border-color: var(--foam-dim);
  box-shadow: inset 0 0 0 1px var(--foam-dim);
}

.dock-row {
  display: grid;
  grid-template-columns: 1fr 1.35fr;
  gap: 0.4rem;
}

.dock-btn {
  min-height: 3rem;
  padding: 0.5rem 0.35rem;
  font-size: 0.92rem;
  line-height: 1.15;
}

.dock-ready {
  font-size: 1.05rem;
}

#btn-confirm-place:disabled {
  opacity: 0.4;
}

.end-panel {
  margin-top: 1.1rem;
  text-align: center;
  padding: 1.1rem;
  border: 1px solid var(--brass);
  background: rgba(8, 36, 46, 0.94);
}

.end-panel p {
  font-family: var(--font-display);
  font-size: 2.1rem;
  letter-spacing: 0.06em;
  margin: 0 0 0.9rem;
}

.end-actions {
  display: grid;
  gap: 0.5rem;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(1rem + var(--dock-h) + var(--safe-bottom));
  transform: translateX(-50%);
  max-width: calc(100% - 2rem);
  background: rgba(6, 24, 32, 0.96);
  border: 1px solid var(--brass);
  color: var(--ink);
  padding: 0.75rem 1rem;
  z-index: 50;
  font-size: 0.92rem;
  text-align: center;
  animation: toast-in 0.18s ease;
  pointer-events: none;
}

@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

[hidden] { display: none !important; }

/* Wider phones / small tablets */
@media (min-width: 520px) {
  main { max-width: 560px; }
  .board-frame { max-width: 420px; }
  body.battle #own-wrap .board-frame { max-width: 260px; }
}

@media (min-width: 860px) {
  main { max-width: 900px; }
  body.battle #boards {
    grid-template-columns: 1fr 1fr;
    align-items: start;
  }
  body.battle #enemy-wrap,
  body.battle #own-wrap { order: 0; }
  body.battle #own-wrap .board-frame {
    max-width: 380px;
    opacity: 1;
  }
  .place-dock {
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    width: min(560px, 100%);
  }
}
