:root {
  --bg: #04070f;
  --bg2: #08111f;
  --panel: rgba(10, 22, 40, 0.72);
  --cyan: #25f0ff;
  --green: #2bff9c;
  --red: #ff3b5f;
  --amber: #ffcf4d;
  --text: #d7f4ff;
  --dim: #6f93a8;
  --grid: rgba(37, 240, 255, 0.14);
  --font-title: 'Orbitron', sans-serif;
  --font-mono: 'Share Tech Mono', monospace;
  --glow: 0 0 12px rgba(37, 240, 255, 0.55);
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-mono);
  overflow-x: hidden;
}

/* ---------- radar de fundo ---------- */
#bgRadar {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  z-index: 0;
  pointer-events: none;
  opacity: 0.7;
}

#app { position: relative; z-index: 1; min-height: 100vh; }

.glass {
  background: var(--panel);
  backdrop-filter: blur(14px) saturate(130%);
  border: 1px solid var(--grid);
  border-radius: 16px;
  box-shadow: 0 0 24px rgba(0, 20, 40, 0.6), inset 0 0 30px rgba(37, 240, 255, 0.05);
}

.hidden { display: none !important; }

/* ---------- vista autenticação ---------- */
#view-auth {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 400px;
  padding: 40px 32px;
}
.brand { text-align: center; margin-bottom: 28px; }
.brand-title {
  font-family: var(--font-title);
  font-size: 2.6rem;
  letter-spacing: 6px;
  color: var(--cyan);
  text-shadow: var(--glow), 0 0 28px rgba(37,240,255,.4);
}
.blink { animation: blink 1.1s steps(1) infinite; color: var(--green); }
@keyframes blink { 50% { opacity: 0; } }
.brand-sub { color: var(--dim); margin-top: 6px; letter-spacing: 2px; }
.radar-logo {
  width: 60px; height: 60px;
  margin: 0 auto 12px;
  border: 2px solid var(--cyan);
  border-radius: 50%;
  position: relative;
  animation: pulse 2.4s ease-in-out infinite;
}
.radar-logo::after {
  content: '';
  position: absolute;
  inset: 20%;
  border: 1px solid var(--green);
  border-radius: 50%;
}
.radar-logo::before {
  content: '';
  position: absolute;
  left: 50%; top: 50%;
  width: 40%; height: 2px;
  background: linear-gradient(90deg, var(--cyan), transparent);
  transform-origin: left center;
  animation: sweep 2s linear infinite;
}
@keyframes sweep { to { transform: rotate(360deg); } }
@keyframes pulse { 0%,100% { box-shadow: 0 0 6px var(--cyan);} 50% { box-shadow: 0 0 22px var(--cyan);} }

.tabs { display: flex; gap: 8px; margin-bottom: 22px; }
.tab {
  flex: 1;
  padding: 10px;
  background: transparent;
  border: 1px solid var(--grid);
  color: var(--dim);
  font-family: var(--font-mono);
  letter-spacing: 1px;
  cursor: pointer;
  border-radius: 8px;
  transition: all .2s;
}
.tab.active {
  color: var(--bg);
  background: var(--cyan);
  border-color: var(--cyan);
  box-shadow: var(--glow);
}
.field { margin-bottom: 16px; }
.field label { display: block; font-size: .78rem; color: var(--dim); letter-spacing: 1px; margin-bottom: 6px; }
.field input {
  width: 100%;
  padding: 11px 12px;
  background: rgba(0,0,0,.35);
  border: 1px solid var(--grid);
  border-radius: 8px;
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 1rem;
  outline: none;
  transition: border .2s, box-shadow .2s;
}
.field input:focus { border-color: var(--cyan); box-shadow: var(--glow); }
.msg { min-height: 1.2em; font-size: .82rem; color: var(--amber); margin: 4px 0 10px; }
.msg.dim { color: var(--dim); }
.msg.err { color: var(--red); }
.msg.ok { color: var(--green); }

.btn-primary, .btn-secondary, .btn-small, .chip-btn, .chip {
  font-family: var(--font-mono);
  letter-spacing: 1px;
  cursor: pointer;
  border: none;
  transition: transform .12s, box-shadow .2s, background .2s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.btn-primary {
  width: 100%;
  padding: 13px;
  font-size: 1rem;
  color: var(--bg);
  background: linear-gradient(90deg, var(--cyan), var(--green));
  border-radius: 10px;
  font-weight: 700;
  box-shadow: 0 0 18px rgba(43,255,156,.35);
}
.btn-primary:disabled { opacity: .4; cursor: not-allowed; box-shadow: none; }
.btn-primary:not(:disabled):hover { transform: translateY(-2px); box-shadow: 0 0 26px rgba(43,255,156,.6); }
.btn-secondary {
  width: 100%;
  padding: 11px;
  margin-top: 10px;
  background: transparent;
  border: 1px solid var(--cyan);
  color: var(--cyan);
  border-radius: 10px;
}
.btn-secondary:hover { background: rgba(37,240,255,.12); box-shadow: var(--glow); }
.btn-small {
  padding: 8px 14px;
  background: rgba(37,240,255,.14);
  border: 1px solid var(--cyan);
  color: var(--cyan);
  border-radius: 8px;
  font-size: .85rem;
}
.btn-small:hover { box-shadow: var(--glow); }
.chip-btn, .chip {
  display: inline-block;
  padding: 7px 12px;
  border-radius: 999px;
  font-size: .8rem;
  background: rgba(37,240,255,.1);
  border: 1px solid var(--grid);
  color: var(--text);
}
.chip-btn:hover { border-color: var(--cyan); box-shadow: var(--glow); }
.chip-btn.danger { color: var(--red); border-color: rgba(255,59,95,.4); }
.chip-btn.danger:hover { box-shadow: 0 0 12px rgba(255,59,95,.5); }

/* ---------- topbar / lobby ---------- */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  position: sticky;
  top: 0;
  z-index: 5;
  border-radius: 0;
  border-left: none; border-right: none; border-top: none;
}
.brand-mini { font-family: var(--font-title); color: var(--cyan); letter-spacing: 3px; font-weight: 700; text-shadow: var(--glow); }
.topbar-user { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.lobby {
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: 20px;
  padding: 24px;
  max-width: 1100px;
  margin: 0 auto;
}
@media (max-width: 860px) { .lobby { grid-template-columns: 1fr; } }
.lobby-main { padding: 22px; }
.lobby-main h2 { font-family: var(--font-title); font-size: 1.1rem; letter-spacing: 2px; margin-bottom: 16px; color: var(--green); text-shadow: 0 0 10px rgba(43,255,156,.5); }
.online-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 12px;
  min-height: 80px;
}
.player-card {
  border: 1px solid var(--grid);
  border-radius: 12px;
  padding: 12px;
  background: rgba(0,0,0,.3);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.player-card .p-name { font-weight: 700; color: var(--text); word-break: break-word; }
.player-card .p-stat { font-size: .78rem; color: var(--dim); }
.player-card .p-online { font-size: .72rem; color: var(--green); }
.player-card .p-offline { font-size: .72rem; color: var(--red); }
.player-card .row { display: flex; gap: 8px; }
.player-card .row button { flex: 1; padding: 6px; font-size: .78rem; }

.panel { padding: 18px; margin-bottom: 18px; }
.panel h3 { font-family: var(--font-title); font-size: .95rem; letter-spacing: 2px; color: var(--cyan); margin-bottom: 12px; }
.divider { height: 1px; background: var(--grid); margin: 14px 0; }
.small-label { font-size: .75rem; color: var(--dim); display: block; margin: 10px 0 6px; }
.invite-row { display: flex; gap: 8px; }
.invite-row input {
  flex: 1;
  min-width: 0;
  padding: 8px;
  background: rgba(0,0,0,.35);
  border: 1px solid var(--grid);
  border-radius: 8px;
  color: var(--text);
  font-family: var(--font-mono);
  font-size: .78rem;
}
.leaderboard { font-size: .85rem; }
.leaderboard .lb-row { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px dashed var(--grid); }
.leaderboard .lb-num { color: var(--dim); width: 22px; }
.leaderboard .lb-name { flex: 1; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.leaderboard .lb-stat { color: var(--amber); }
.leaderboard .lb-me { color: var(--green); }

/* ---------- jogo ---------- */
.game-notice {
  text-align: center;
  padding: 10px;
  margin: 16px auto;
  max-width: 720px;
  font-size: .95rem;
  letter-spacing: 1px;
  color: var(--amber);
}
.phase { padding: 20px; animation: fadein .3s; }
@keyframes fadein { from { opacity: 0; transform: translateY(8px);} to { opacity: 1; transform: none;} }

.placing-wrap {
  display: flex;
  gap: 24px;
  justify-content: center;
  align-items: flex-start;
  max-width: 1100px;
  margin: 0 auto;
  flex-wrap: wrap;
}
.combat-wrap {
  display: flex;
  gap: 20px;
  justify-content: center;
  align-items: flex-start;
  max-width: 1500px;
  margin: 0 auto;
  flex-wrap: nowrap;
}
.board-container { text-align: center; min-width: 0; }
.board-title { display: flex; justify-content: space-between; align-items: center; gap: 14px; margin-bottom: 10px; font-family: var(--font-title); letter-spacing: 2px; color: var(--cyan); font-size: .9rem; }

.board {
  display: inline-grid;
  grid-template-columns: 24px repeat(10, clamp(24px, 3.1vw, 44px));
  grid-template-rows: 24px repeat(10, clamp(24px, 3.1vw, 44px));
  gap: 2px;
  background: rgba(0, 30, 40, .55);
  padding: 8px;
  border: 1px solid var(--grid);
  border-radius: 12px;
  box-shadow: inset 0 0 40px rgba(37,240,255,.06);
  user-select: none;
}
.cell-label { display: flex; align-items: center; justify-content: center; font-size: .68rem; color: var(--dim); }
.cell {
  border: 1px solid rgba(37,240,255,.18);
  border-radius: 4px;
  background: rgba(0,0,0,.25);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s, box-shadow .15s, transform .1s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.own .cell.ship { background: radial-gradient(circle at 30% 30%, #1c6b6e, #0a2f3a); border-color: var(--cyan); box-shadow: inset 0 0 8px rgba(37,240,255,.35); }
.own .cell.shot-hit { background: radial-gradient(circle, var(--red), #5a0d1e); border-color: var(--red); animation: boom .45s ease-out; }
.own .cell.shot-miss { background: radial-gradient(circle, #122b3f 0%, transparent 70%); border-color: var(--grid); }
.own .cell.sunk-cell { background: #3a0a12; border-color: var(--red); }

.enemy .cell { cursor: crosshair; touch-action: manipulation; }
.enemy .cell:hover:not(.blocked) { background: rgba(37,240,255,.18); box-shadow: var(--glow); transform: scale(1.06); }
.enemy .cell.shot-hit { background: radial-gradient(circle, var(--red), #5a0d1e); border-color: var(--red); animation: boom .45s ease-out; cursor: default; }
.enemy .cell.shot-miss { background: radial-gradient(circle, rgba(60,140,200,.5) 0%, transparent 75%); border-color: var(--grid); cursor: default; }
.enemy .cell.sunk-cell { background: #3a0a12; border-color: var(--red); }
.enemy .cell.blocked { cursor: not-allowed; }

.cell.ship.cursor-ok { outline: 2px solid var(--green); outline-offset: -2px; box-shadow: 0 0 12px rgba(43,255,156,.5); }
.cell.ship.cursor-bad { outline: 2px solid var(--red); outline-offset: -2px; }

@keyframes boom {
  0% { transform: scale(.6); filter: brightness(2.4); }
  100% { transform: scale(1); filter: brightness(1); }
}

.placement-preview {
  position: absolute;
  inset: 0;
  border-radius: 4px;
  animation: pulse 1.2s infinite;
}
.preview-ok { background: rgba(43,255,156,.35); box-shadow: inset 0 0 10px rgba(43,255,156,.6); }
.preview-bad { background: rgba(255,59,95,.35); box-shadow: inset 0 0 10px rgba(255,59,95,.6); }

.board-actions { display: flex; gap: 10px; justify-content: center; margin-top: 14px; flex-wrap: wrap; }
.board-actions .btn-primary { width: auto; }

.fleet-panel { min-width: 180px; padding: 18px; }
.fleet-panel h3 { font-family: var(--font-title); font-size: .9rem; letter-spacing: 2px; color: var(--cyan); margin-bottom: 12px; }
.fleet-item { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; font-size: .8rem; }
.fleet-item .fleet-dots { display: inline-flex; gap: 3px; }
.fleet-item .fleet-dot { width: 14px; height: 14px; border-radius: 3px; background: var(--cyan); box-shadow: 0 0 6px var(--cyan); }
.fleet-item .fleet-dot.placed { background: var(--green); box-shadow: 0 0 6px var(--green); }
.fleet-item .fleet-name { color: var(--dim); }

.hud {
  min-width: 220px;
  padding: 18px;
  text-align: center;
  align-self: stretch;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}
.turn { font-family: var(--font-title); font-size: 1rem; letter-spacing: 2px; color: var(--amber); line-height: 1.5; }
.turn.your { color: var(--green); text-shadow: 0 0 12px rgba(43,255,156,.7); }
.turn.wait { color: var(--dim); }
.timer-wrap { position: relative; width: 88px; height: 88px; }
.timer-svg { width: 88px; height: 88px; transform: rotate(-90deg); }
.timer-bg { fill: none; stroke: rgba(255,255,255,.08); stroke-width: 6; }
.timer-fg {
  fill: none;
  stroke: var(--green);
  stroke-width: 6;
  stroke-linecap: round;
  stroke-dasharray: 163.4;
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 1s linear, stroke .3s;
  filter: drop-shadow(0 0 6px var(--green));
}
.timer-fg.danger { stroke: var(--red); filter: drop-shadow(0 0 6px var(--red)); }
.timer-num { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-family: var(--font-title); font-size: 1.5rem; color: var(--text); }
.battle-log {
  text-align: left;
  font-size: .8rem;
  color: var(--dim);
  max-height: 120px;
  overflow-y: auto;
  width: 100%;
  border-top: 1px dashed var(--grid);
  padding-top: 10px;
}
.battle-log .log-hit { color: var(--red); }
.battle-log .log-miss { color: var(--dim); }
.battle-log .log-sunk { color: var(--amber); }
.battle-log .log-info { color: var(--green); }

/* ---------- modais / toast ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(2,6,14,.7);
  backdrop-filter: blur(4px);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal {
  width: 100%;
  max-width: 680px;
  max-height: 86vh;
  overflow-y: auto;
  padding: 28px;
  position: relative;
  animation: fadein .25s;
}
.modal.small { max-width: 420px; }
.modal h2 { font-family: var(--font-title); letter-spacing: 2px; color: var(--cyan); margin-bottom: 16px; font-size: 1.1rem; }
.modal-close { position: absolute; top: 12px; right: 14px; background: none; border: none; color: var(--dim); font-size: 1.6rem; cursor: pointer; }
.modal-close:hover { color: var(--red); }
.howto-body ol, .howto-body ul { margin-left: 20px; line-height: 1.8; }
.howto-body, .modal p { line-height: 1.8; font-size: .92rem; }
.modal summary { cursor: pointer; color: var(--green); margin: 12px 0 6px; font-size: .95rem; }
.modal-actions { display: flex; gap: 12px; margin-top: 22px; }
.modal-actions .btn-primary, .modal-actions .btn-secondary { width: auto; margin-top: 0; flex: 1; }
.gameover-neon { font-size: 1.8rem !important; text-align: center; }
.gameover-neon.win { color: var(--green); text-shadow: 0 0 18px rgba(43,255,156,.8); }
.gameover-neon.lose { color: var(--red); text-shadow: 0 0 18px rgba(255,59,95,.8); }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--cyan);
  border-radius: 10px;
  padding: 12px 20px;
  z-index: 100;
  box-shadow: var(--glow);
  animation: fadein .2s;
  max-width: 90vw;
  font-size: .9rem;
}

.fx-splash { position: fixed; pointer-events: none; z-index: 200; width: 8px; height: 8px; border-radius: 50%; }

/* ============ VERSÃO PARA TELEMÓVEL ============ */
@media (max-width: 720px) {
  body { overflow-x: hidden; }
  #app { font-size: 1rem; }

  /* topbar compacta */
  .topbar {
    padding: 10px 12px;
    gap: 6px;
    flex-wrap: wrap;
  }
  .brand-mini { font-size: .95rem; letter-spacing: 2px; }
  .topbar-user { width: 100%; justify-content: flex-end; gap: 6px; }
  .chip-btn, .chip { padding: 7px 10px; font-size: .75rem; }

  /* grelha de jogo à medida do ecrã — mais pequena para caberem as duas quadras */
  .board {
    grid-template-columns: 16px repeat(10, clamp(16px, 5.2vw, 26px));
    grid-template-rows: 16px repeat(10, clamp(16px, 5.2vw, 26px));
    gap: 1px;
    padding: 5px;
    max-width: 96vw;
  }
  .cell-label { font-size: .58rem; }
  .placing-wrap, .combat-wrap { gap: 14px; padding: 8px; flex-direction: column; align-items: center; }

  /* em telemóvel o mar inimigo aparece primeiro (é onde se toca) */
  .combat-wrap .board-container:nth-child(3) { order: -1; }
  .combat-wrap .board-container:nth-child(2) { order: 3; }

  .hud {
    min-width: 0;
    width: 100%;
    padding: 12px;
    gap: 8px;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
  }
  .hud > * { flex-shrink: 1; }
  .timer-wrap { width: 60px; height: 60px; }
  .timer-svg { width: 60px; height: 60px; }
  .timer-num { font-size: 1.1rem; }
  .battle-log { max-height: 90px; font-size: .72rem; }
  .turn { font-size: .85rem; }

  .board-title { font-size: .75rem; margin-bottom: 6px; }

  /* lobby */
  .lobby { padding: 14px; gap: 14px; }
  .lobby-main, .panel { padding: 14px; }
  .online-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .player-card { padding: 10px; }
  .player-card .row { flex-direction: column; }
  .player-card .row button { width: 100%; }

  /* painel de colocação */
  .fleet-panel { min-width: 0; width: 100%; }
  .fleet-item { padding: 6px 8px; margin-bottom: 6px; background: rgba(0,0,0,.25); border-radius: 8px; }
  .fleet-name { font-size: .78rem; }

  /* botões maiores para toque */
  .board-actions .btn-primary { padding: 14px 18px; font-size: .95rem; }
  .btn-secondary, .btn-primary { padding: 13px; }
  .btn-small { padding: 10px 14px; }

  /* login */
  .login-card { padding: 28px 20px; }
  .brand-title { font-size: 2.1rem; }

  /* modais */
  .modal { padding: 20px 16px; }
  .modal-actions { flex-direction: column; }

  .game-notice { margin: 10px 12px; font-size: .82rem; }

  /* radar de fundo mais subtil */
  #bgRadar { opacity: .5; }

  /* brilho do telemóvel: tap feedback */
  .cell:active { transform: scale(.92); }
  .enemy .cell { -webkit-tap-highlight-color: rgba(37,240,255,.25); }
}