:root {
  --bg: #0b1c2e;
  --panel: #142b44;
  --text: #e6eef6;
  --accent: #3fb1d5;
  --hit: #e74c3c;
  --miss: #7f8c8d;
  --intel: #f1c40f;
  --ship: #2ecc71;
  --ship-sunk: #6b1a1a;
  --debug-ship: #9b59b6;
  --cell-size: 22px;
}

* { box-sizing: border-box; }

body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  margin: 0;
  padding: 20px;
  text-align: center;
}

h1, h2 { margin-top: 0; }

#top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  margin-bottom: 16px;
}

#top-bar h1 {
  margin: 0;
  font-size: 1.6rem;
}

.hamburger-wrap {
  position: relative;
}

#hamburger-btn {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--accent);
  border-radius: 6px;
  font-size: 1.4rem;
  padding: 4px 10px;
  cursor: pointer;
}

#hamburger-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  background: var(--panel);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 8px;
  min-width: 140px;
  z-index: 100;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

#hamburger-menu.hidden {
  display: none;
}

#hamburger-menu button {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  color: var(--text);
  border: none;
  border-radius: 4px;
  padding: 8px 10px;
  margin: 0;
  font-weight: 500;
}

#hamburger-menu button:hover {
  background: #1d3b5c;
}

#hamburger-menu .menu-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  font-weight: 500;
  font-size: 0.9rem;
  cursor: pointer;
  border-radius: 4px;
  white-space: nowrap;
}

#hamburger-menu .menu-option:hover {
  background: #1d3b5c;
}

#hamburger-menu .menu-option input[type="checkbox"] {
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
  flex-shrink: 0;
}

#status {
  background: var(--panel);
  padding: 12px 18px;
  border-radius: 8px;
  display: inline-block;
  margin-bottom: 18px;
  font-weight: 500;
  max-width: 90vw;
}

button {
  background: var(--accent);
  color: #062330;
  border: none;
  border-radius: 6px;
  padding: 8px 12px;
  margin: 4px;
  font-weight: 600;
  cursor: pointer;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

#setup, #game, #controls, #new-game-btn {
  margin-top: 10px;
}

.hidden { display: none !important; }

#setup {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: 30px;
  flex-wrap: wrap;
}

#setup-left {
  display: flex;
  flex-direction: column;
  gap: 16px;
  max-width: 420px;
}

.history-time {
  font-size: 0.75rem;
  opacity: 0.7;
  margin-top: 4px;
}

#pvp-lobby,
#pvp-footer {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

#pvp-footer {
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 700px;
  margin-left: auto;
  margin-right: auto;
}

#pvp-footer .setup-panel {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

#pvp-footer .setup-panel strong {
  flex-shrink: 0;
}

#pvp-footer .share-link {
  flex: 1;
  min-width: 0;
}

#pvp-lobby {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

#pvp-lobby.hidden {
  display: none;
}

.share-link {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 0.9rem;
  margin: 8px 0;
}

#setup-right {
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

#setup-panels {
  display: none;
}

.setup-panel {
  background: var(--panel);
  padding: 14px 18px;
  border-radius: 10px;
  text-align: left;
  max-width: 420px;
}

.setup-panel p, .setup-panel ul {
  font-size: 0.95rem;
  line-height: 1.4;
}

.setup-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}

#setup.guest-only #match-options-panel,
#setup.guest-only #game-history-panel {
  display: none;
}

.ability-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.ability-entry {
  font-size: 0.95rem;
  line-height: 1.4;
}

.setup-panel select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--accent);
  border-radius: 4px;
  padding: 6px 10px;
  font-size: 0.95rem;
}

.option-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 8px 0;
  font-size: 0.95rem;
}

.option-row input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.game-history-panel {
  width: 100%;
  margin-bottom: 16px;
}

#game-history {
  max-height: 240px;
  overflow-y: auto;
  padding-right: 6px;
}

.history-empty {
  font-size: 0.9rem;
  opacity: 0.7;
  margin: 0;
}

.history-item {
  background: #0b1c2e;
  border: 1px solid #1d3b5c;
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 8px;
  text-align: left;
  font-size: 0.9rem;
  line-height: 1.4;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.history-item:hover {
  background: #142b44;
  border-color: var(--accent);
}

.history-item:last-child {
  margin-bottom: 0;
}

.history-main {
  flex: 1;
  min-width: 0;
}

.history-id {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85rem;
  color: var(--accent);
  word-break: break-all;
}

.history-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}

.history-status {
  font-weight: 500;
}

.history-status.in-progress { color: var(--accent); }
.history-status.completed { color: var(--text); }
.history-status.won { color: var(--ship); }
.history-status.lost { color: var(--hit); }

.history-debug {
  font-size: 0.75rem;
  padding: 2px 6px;
  border-radius: 4px;
  background: #9b59b6;
  color: #fff;
  font-weight: 600;
}

.history-arrow {
  font-size: 1.2rem;
  color: var(--accent);
  flex-shrink: 0;
}

#dock {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: flex-start;
  min-height: 48px;
  margin: 10px 0;
  padding: 6px;
  border: 1px dashed transparent;
  border-radius: 6px;
  transition: background 0.15s, border-color 0.15s;
}

#dock.drop-target {
  background: rgba(47, 179, 101, 0.12);
  border-color: var(--ship);
}

.ship {
  display: flex;
  cursor: grab;
  border-radius: 4px;
  overflow: hidden;
  opacity: 0.85;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.ship:hover { opacity: 1; }

.ship.dragging {
  opacity: 0.35;
  cursor: grabbing;
}

.ship.vertical { flex-direction: column; }

.ship .cell { background: var(--ship); }

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  border: 1px solid #0b1c2e;
  background: var(--panel);
}

#player-setup-board {
  touch-action: none;
}

.board {
  display: grid;
  grid-template-columns: repeat(24, var(--cell-size));
  gap: 1px;
  justify-content: center;
  margin: 10px auto;
  padding: 6px;
  background: #071221;
  border-radius: 6px;
  width: fit-content;
}

.board .cell {
  background: #1d3b5c;
  cursor: pointer;
  border-radius: 1px;
  transition: background 0.12s;
  position: relative;
}

.board .cell:hover { background: #25527a; }

.board .cell.ship { background: var(--ship); }
.board .cell.hit { background: var(--hit); cursor: default; }
.board .cell.miss { background: var(--miss); cursor: default; }
.board .cell.sunk { background: var(--ship-sunk); }
.board .cell.intel-empty { background: #345b82; }
.board .cell.intel-ship { background: #d4ac0d; }

#enemy-board .cell.ship { background: #1d3b5c; }
#enemy-board .cell.ship.hit,
#enemy-board .cell.ship.sunk { background: var(--ship-sunk); }

#enemy-board.debug .cell.ship:not(.hit):not(.sunk) { background: var(--debug-ship); }
#enemy-board.debug .cell.hit { background: var(--hit); }
#enemy-board.debug .cell.miss { background: var(--miss); }
#enemy-board.debug .cell.sunk { background: var(--ship-sunk); }

#enemy-board.sonar-hover .cell:hover {
  background: #c9a227 !important;
  opacity: 0.85 !important;
}

#enemy-board .cell.intel-empty { background: #345b82; }
#enemy-board .cell.intel-ship { background: #d4ac0d; }

#game {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 30px;
}

.board-wrap h2 { margin: 0 0 6px; }

#controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
}

#targeting-banner {
  background: var(--panel);
  border: 1px solid var(--accent);
  color: var(--accent);
  padding: 10px 18px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 1rem;
  text-align: center;
}

#targeting-banner.hidden {
  display: none;
}

#ability-bar {
  display: flex;
  justify-content: center;
  align-items: stretch;
  flex-wrap: wrap;
  gap: 10px;
  width: 100%;
}

.ability-card {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 110px;
  max-width: 150px;
  flex: 1 1 110px;
  padding: 10px 8px;
  border-radius: 10px;
  border: 2px solid transparent;
  background: #0e2235;
  color: var(--text);
  cursor: pointer;
  transition: transform 0.1s, border-color 0.15s, background 0.15s;
  text-align: center;
  overflow: hidden;
}

.ability-card:hover:not(:disabled):not(.lost) {
  background: #16334d;
  transform: translateY(-2px);
}

.ability-card.basic {
  border-color: var(--accent);
  background: #0b2b3a;
}

.ability-card.damage {
  background: #2e1515;
  border-color: #5a2a2a;
}

.ability-card.damage:hover:not(:disabled):not(.lost) {
  background: #3d1b1b;
}

.ability-card.intel {
  background: #0f2238;
  border-color: #1d4a73;
}

.ability-card.intel:hover:not(:disabled):not(.lost) {
  background: #152d47;
}

.ability-card.active {
  outline: none;
  box-shadow: 0 0 0 3px var(--text);
}

.ability-card:disabled,
.ability-card.lost {
  cursor: not-allowed;
  opacity: 0.55;
}

.ability-card.lost {
  filter: grayscale(100%) brightness(0.7);
}

.ability-card .ability-name {
  font-weight: 700;
  font-size: 0.95rem;
  line-height: 1.2;
}

.ability-card .ability-ship {
  font-size: 0.75rem;
  opacity: 0.8;
  margin-top: 2px;
}

.ability-card .ability-hint {
  font-size: 0.7rem;
  opacity: 0.65;
  margin-top: 4px;
  line-height: 1.2;
}

.ability-cooldown {
  position: absolute;
  top: 4px;
  right: 4px;
  background: #0b1c2e;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 50%;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
}

.ability-sunk {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);
  color: var(--hit);
  font-weight: 800;
  font-size: 1.1rem;
  letter-spacing: 1px;
  text-transform: uppercase;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9);
}

#target-controls {
  background: var(--panel);
  padding: 8px 14px;
  border-radius: 8px;
}

#target-label {
  margin-right: 8px;
  font-weight: 500;
}

#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #5a1a1a;
  color: #fff;
  padding: 12px 22px;
  border-radius: 8px;
  border: 1px solid #e74c3c;
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
  font-weight: 500;
  z-index: 1000;
  opacity: 0;
  transition: opacity 0.25s ease, bottom 0.25s ease;
  pointer-events: none;
}

#toast.visible {
  opacity: 1;
  bottom: 32px;
}

#toast.hidden {
  display: none;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 24px;
}

.modal-overlay.hidden {
  display: none;
}

.modal {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--accent);
  border-radius: 12px;
  max-width: 720px;
  width: 100%;
  max-height: calc(100vh - 48px);
  display: flex;
  flex-direction: column;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
  text-align: left;
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--accent);
  border-radius: 6px;
  width: 34px;
  height: 34px;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  z-index: 10;
  padding: 0;
  margin: 0;
}

.modal-close:hover {
  background: #1d3b5c;
}

.modal-body {
  overflow-y: auto;
  padding: 24px 28px 28px;
  color: var(--text);
  font-size: 0.92rem;
  line-height: 1.5;
}

.modal-body h2 {
  margin: 0 0 12px;
  padding-right: 44px;
  font-size: 1.35rem;
}

.modal-body h3 {
  margin: 22px 0 8px;
  font-size: 1.05rem;
  color: var(--accent);
  border-bottom: 1px solid #1d3b5c;
  padding-bottom: 4px;
}

.modal-body h4 {
  margin: 0 0 4px;
  font-size: 0.98rem;
}

.modal-body p {
  margin: 0 0 10px;
}

.modal-body ul {
  margin: 6px 0 12px 20px;
  padding: 0;
}

.modal-body li {
  margin-bottom: 6px;
}

.modal-body .rule-intro {
  font-style: italic;
  opacity: 0.9;
}

.modal-body .rule-ship {
  background: #0e2235;
  border-left: 3px solid var(--accent);
  border-radius: 6px;
  padding: 10px 14px;
  margin: 10px 0;
}

.modal-body .rule-ship-name {
  color: var(--ship);
  font-weight: 700;
}

.modal-body .rule-ship-size {
  opacity: 0.7;
  font-weight: 400;
}

@media (max-width: 1000px) {
  #game { flex-direction: column; }
  :root { --cell-size: 18px; }
}

@media (max-width: 560px) {
  :root { --cell-size: 14px; }
}
