:root {
  --bg-1: #0b1d33;
  --bg-2: #13335a;
  --panel: rgba(255, 255, 255, 0.06);
  --panel-border: rgba(255, 255, 255, 0.12);
  --text: #e8f1fb;
  --muted: #8fa6c1;
  --water: #1c4f80;
  --water-2: #17456f;
  --grid: rgba(160, 200, 240, 0.18);
  --ship: #9aa7b5;
  --ship-edge: #56647a;
  --hit: #ff5a4e;
  --miss: #e8f1fb;
  --sunk: #7a1d1d;
  --accent: #ffc145;
  --ok: #3ddc97;
  --bad: #ff5a4e;
  --radius: 14px;
  --cell-gap: 2px;
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 10% -10%, #1d4c7d 0%, transparent 60%),
    radial-gradient(900px 500px at 110% 110%, #0f3a63 0%, transparent 60%),
    linear-gradient(180deg, var(--bg-1), var(--bg-2));
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

.hidden { display: none !important; }

#app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 14px 20px;
  border-bottom: 1px solid var(--panel-border);
  background: rgba(5, 14, 28, 0.35);
  backdrop-filter: blur(6px);
}

.topbar h1 {
  margin: 0;
  font-size: 1.35rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
}

.status {
  flex: 1;
  min-width: 120px;
  color: var(--muted);
  font-size: 0.95rem;
}

.topbar-actions { display: flex; gap: 8px; }

main {
  flex: 1;
  padding: 20px;
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
}

h2 { font-size: 1.05rem; margin: 0 0 10px; font-weight: 600; letter-spacing: 0.02em; }
h3 { font-size: 0.95rem; margin: 0 0 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }

.panel { animation: fadeIn 0.35s ease; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

kbd {
  font-family: inherit;
  font-size: 0.8em;
  padding: 1px 6px;
  border: 1px solid var(--panel-border);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.08);
}

/* Buttons */
.btn {
  font: inherit;
  font-size: 0.92rem;
  padding: 9px 16px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.08s ease, background 0.2s ease, opacity 0.2s ease;
  touch-action: manipulation;
}

.btn:hover:not(:disabled) { background: rgba(255, 255, 255, 0.16); }
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn.primary {
  background: linear-gradient(180deg, #ffd062, #f0a52a);
  color: #1b1200;
  border-color: transparent;
  font-weight: 700;
}
.btn.primary:hover:not(:disabled) { background: linear-gradient(180deg, #ffdc7e, #f5b544); }

.btn.ghost { background: transparent; }

/* Placement layout */
.placement-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 24px;
  align-items: start;
}

.boards {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 28px;
  justify-items: center;
}

.board-wrap {
  width: 100%;
  max-width: 440px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.board-wrap h2 { align-self: flex-start; }

.hint {
  color: var(--muted);
  font-size: 0.88rem;
  margin: 12px 0 0;
  max-width: 440px;
}

/* Board frame with labels */
.board-frame {
  width: 100%;
  display: grid;
  grid-template-columns: 1.1rem minmax(0, 1fr);
  grid-template-rows: 1.1rem auto;
  gap: 4px;
  align-items: stretch;
}

.col-labels, .row-labels {
  display: flex;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
}

.col-labels { grid-column: 2; grid-row: 1; }
.row-labels { grid-column: 1; grid-row: 2; flex-direction: column; }
.col-labels span, .row-labels span {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board {
  grid-column: 2;
  grid-row: 2;
  position: relative;
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  grid-template-rows: repeat(10, minmax(0, 1fr));
  gap: var(--cell-gap);
  padding: 6px;
  border-radius: var(--radius);
  background: linear-gradient(160deg, #0d2a4a, #0a2140);
  border: 1px solid var(--panel-border);
  box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.35), 0 12px 30px rgba(0, 0, 0, 0.3);
  aspect-ratio: 1 / 1;
  width: 100%;
  touch-action: none;
}

.cell {
  position: relative;
  border-radius: 4px;
  background: linear-gradient(160deg, var(--water), var(--water-2));
  box-shadow: inset 0 0 0 1px var(--grid);
  cursor: pointer;
  transition: background-color 0.2s ease, box-shadow 0.2s ease;
  min-width: 0;
  min-height: 0;
}

.cell:hover { box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.35); }

.board.locked .cell { cursor: default; }
.board.locked .cell:hover { box-shadow: inset 0 0 0 1px var(--grid); }
.board.placing .cell { cursor: crosshair; }

/* Ships (own board) */
.cell.ship {
  background: linear-gradient(160deg, #c3ccd7, var(--ship) 55%, #7d8a9b);
  box-shadow: inset 0 0 0 1px var(--ship-edge), 0 2px 4px rgba(0, 0, 0, 0.35);
}

.cell.preview-ok {
  background: rgba(61, 220, 151, 0.45);
  box-shadow: inset 0 0 0 2px var(--ok);
}
.cell.preview-bad {
  background: rgba(255, 90, 78, 0.4);
  box-shadow: inset 0 0 0 2px var(--bad);
}

/* Hits and misses */
.cell.miss::after {
  content: "";
  position: absolute;
  left: 50%; top: 50%;
  width: 28%; height: 28%;
  transform: translate(-50%, -50%) scale(0);
  border-radius: 50%;
  background: var(--miss);
  box-shadow: 0 0 6px rgba(255, 255, 255, 0.6);
  animation: splash 0.45s ease forwards;
}

.cell.hit {
  background: radial-gradient(circle at 50% 45%, #ffb199, var(--hit) 45%, #8a1f18 100%);
  box-shadow: inset 0 0 0 1px #6b1510;
}
.cell.hit::after {
  content: "";
  position: absolute;
  inset: 22%;
  border-radius: 50%;
  background: #fff3c4;
  box-shadow: 0 0 8px #ffb300;
  transform: scale(0);
  animation: pop 0.35s cubic-bezier(.2, 1.6, .4, 1) forwards;
}

.cell.sunk {
  background: radial-gradient(circle at 50% 45%, #7a2222, var(--sunk) 60%, #3d0e0e 100%);
  box-shadow: inset 0 0 0 1px #2a0707;
}
.cell.sunk::before {
  content: "";
  position: absolute;
  inset: 20%;
  border-radius: 50%;
  background: repeating-linear-gradient(45deg, #ff7a5c 0 2px, transparent 2px 5px);
  opacity: 0.55;
}

@keyframes splash {
  0% { transform: translate(-50%, -50%) scale(0); opacity: 1; }
  60% { transform: translate(-50%, -50%) scale(1.4); opacity: 0.9; }
  100% { transform: translate(-50%, -50%) scale(1); opacity: 0.85; }
}

@keyframes pop {
  to { transform: scale(1); }
}

.board.incoming { animation: boardRumble 0.3s ease; }
@keyframes boardRumble {
  0%, 100% { transform: none; }
  30% { transform: translateY(2px); }
  60% { transform: translateY(-1px); }
}

/* Fleet side panel */
.fleet-panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 16px;
}

.fleet-list {
  list-style: none;
  margin: 0 0 16px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.ship-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: rgba(255, 255, 255, 0.04);
  cursor: grab;
  touch-action: none;
  transition: background 0.2s ease, border-color 0.2s ease, opacity 0.2s ease;
}

.ship-item:hover { background: rgba(255, 255, 255, 0.09); }
.ship-item.selected { border-color: var(--accent); background: rgba(255, 193, 69, 0.12); }
.ship-item.placed { opacity: 0.5; }
.ship-item.dragging { opacity: 0.35; }

.ship-name { font-size: 0.88rem; white-space: nowrap; }

.ship-cells { display: flex; gap: 3px; }
.ship-cells i {
  display: block;
  width: 16px;
  height: 16px;
  border-radius: 4px;
  background: linear-gradient(160deg, #c3ccd7, var(--ship) 55%, #7d8a9b);
  box-shadow: inset 0 0 0 1px var(--ship-edge);
}

.ship-item.sunk-item .ship-cells i { background: var(--sunk); box-shadow: none; }

.placement-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.placement-actions .primary { grid-column: 1 / -1; }

/* Ghost piece while dragging */
.drag-ghost {
  position: fixed;
  left: 0; top: 0;
  pointer-events: none;
  z-index: 50;
  display: flex;
  gap: 2px;
  opacity: 0.85;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 6px 10px rgba(0, 0, 0, 0.45));
}
.drag-ghost i {
  display: block;
  width: var(--ghost-cell, 32px);
  height: var(--ghost-cell, 32px);
  border-radius: 6px;
  background: linear-gradient(160deg, #c3ccd7, var(--ship) 55%, #7d8a9b);
  box-shadow: inset 0 0 0 1px var(--ship-edge);
}
.drag-ghost.vertical { flex-direction: column; }

/* Battle */
.turn-indicator {
  display: inline-block;
  padding: 8px 18px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: 0.04em;
  margin: 0 auto 18px;
  background: rgba(61, 220, 151, 0.15);
  color: var(--ok);
  border: 1px solid rgba(61, 220, 151, 0.4);
  transition: background 0.3s ease, color 0.3s ease, border-color 0.3s ease;
  display: block;
  width: fit-content;
}
.turn-indicator.enemy {
  background: rgba(255, 90, 78, 0.14);
  color: #ff8a80;
  border-color: rgba(255, 90, 78, 0.4);
}

.fleet-status {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
  justify-content: center;
  min-height: 1.6rem;
}

.fleet-chip {
  font-size: 0.75rem;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid var(--panel-border);
  color: var(--text);
  transition: opacity 0.3s ease, text-decoration-color 0.3s ease;
}
.fleet-chip.sunk {
  opacity: 0.45;
  text-decoration: line-through;
  color: #ff8a80;
}

.log {
  margin: 18px auto 0;
  max-width: 600px;
  min-height: 2.2rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.92rem;
}

.log .msg { display: inline-block; animation: fadeIn 0.3s ease; }

.boards .board-wrap { max-width: 440px; }

/* Overlay */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(3, 10, 20, 0.7);
  backdrop-filter: blur(4px);
  animation: fadeIn 0.3s ease;
}

.overlay-card {
  width: min(92vw, 380px);
  padding: 28px 24px;
  text-align: center;
  border-radius: 18px;
  background: linear-gradient(180deg, #1a3f69, #102a4a);
  border: 1px solid var(--panel-border);
  box-shadow: 0 30px 60px rgba(0, 0, 0, 0.5);
  animation: cardIn 0.45s cubic-bezier(.2, 1.3, .4, 1);
}

@keyframes cardIn {
  from { transform: scale(0.9); opacity: 0; }
  to { transform: none; opacity: 1; }
}

.over-emoji { font-size: 3rem; line-height: 1; margin-bottom: 10px; }
.overlay-card h2 { font-size: 1.6rem; color: var(--accent); }
.overlay-card p { color: var(--muted); margin: 0 0 20px; }

/* Responsive */
@media (max-width: 860px) {
  .placement-layout { grid-template-columns: 1fr; }
  .fleet-panel { order: -1; }
  .fleet-list { flex-direction: row; flex-wrap: wrap; }
  .ship-item { flex: 1 1 200px; }
}

@media (max-width: 720px) {
  .boards { grid-template-columns: 1fr; gap: 22px; }
  .board-wrap { max-width: 420px; }
  .placement-actions { grid-template-columns: 1fr 1fr 1fr; }
  .placement-actions .primary { grid-column: auto; }
}

@media (max-width: 480px) {
  main { padding: 12px; }
  .topbar { padding: 10px 12px; gap: 8px; }
  .topbar h1 { font-size: 1.1rem; }
  .status { order: 3; flex-basis: 100%; min-width: 0; font-size: 0.85rem; }
  .btn { padding: 8px 12px; font-size: 0.85rem; }
  .board { padding: 4px; }
  .hint { font-size: 0.82rem; }
  .ship-cells i { width: 13px; height: 13px; }
}

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