/* SPDX-License-Identifier: AGPL-3.0-or-later */
/* Estils «Costa Verda» (sàlvia clar, blau oceà, ambre i terracota) per a Enfonsar la flota */

:root {
  --bg: #d7e6dd;
  --bg-grad-top: #edf4ef;
  --surface: #fcfefd;
  --surface-2: #edf4ef;
  --border: #c2d6ca;
  --border-strong: #94b4a2;
  --text: #16332c;
  --muted: #4b695e;
  --sea-1: #1179ad;
  --sea-2: #0b5a83;
  --sea-border: rgba(232, 247, 255, 0.24);
  --sea-frame: #e3ede7;
  --primary: #0d6896;
  --primary-hover: #094e72;
  --primary-soft: rgba(13, 104, 150, 0.11);
  --coral: #d44d38;
  --coral-soft: rgba(212, 77, 56, 0.11);
  --accent: #147a5f;
  --accent-hover: #0e5d48;
  --hit: #ea580c;
  --sunk: #dc2626;
  --ship-wood-1: #e0983c;
  --ship-wood-2: #b96d1a;
  --ship-border: #78400b;
  --cell-place: clamp(28px, min(4.7vw, calc((100dvh - 248px) / 10)), 76px);
  --cell-battle: clamp(25px, min(4vw, calc((100dvh - 312px) / 10)), 76px);
  --shell-max: 1680px;
  --radius: 14px;
  --shadow: 0 10px 26px rgba(18, 52, 42, 0.08);
  font-size: clamp(13px, min(1.05vw, 1.85vh), 17px);
}

*, *::before, *::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html, body {
  margin: 0;
  padding: 0;
  min-height: 100dvh;
  background:
    radial-gradient(circle at 18% 0%, #f7fbf8 0%, transparent 45%),
    radial-gradient(circle at 86% 12%, rgba(13, 104, 150, 0.08) 0%, transparent 42%),
    linear-gradient(180deg, var(--bg-grad-top), var(--bg));
  color: var(--text);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Ubuntu, sans-serif;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
  padding-bottom: 58px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.ic {
  width: 1.15rem;
  height: 1.15rem;
  flex: none;
}

/* Capçalera */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(247, 251, 248, 0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.topbar-inner {
  width: min(var(--shell-max), 100%);
  margin: 0 auto;
  padding: 0.55rem clamp(0.75rem, 2.2vw, 1.5rem);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.85rem;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.72rem;
  background: none;
  border: none;
  padding: 0.2rem 0.45rem;
  margin-left: -0.45rem;
  border-radius: 10px;
  color: var(--text);
  font-size: clamp(1.6rem, 2.4vw, 2.08rem);
  font-weight: 850;
  letter-spacing: -0.02em;
  line-height: 1.1;
  cursor: pointer;
}

.brand:hover {
  background: var(--primary-soft);
}

.brand-name {
  white-space: nowrap;
}

.brand-ic {
  width: clamp(2.25rem, 3.2vw, 2.85rem);
  height: clamp(2.25rem, 3.2vw, 2.85rem);
  border-radius: 9px;
  flex: none;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.45rem;
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.38rem 0.7rem;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.84rem;
  font-weight: 650;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.btn-icon:hover {
  background: var(--surface-2);
  border-color: var(--border-strong);
}

.btn-install-top {
  border-color: rgba(13, 104, 150, 0.4);
  background: rgba(13, 104, 150, 0.1);
  color: var(--primary);
}

.btn-install-top:hover {
  background: rgba(13, 104, 150, 0.18);
}

.btn-sound {
  border-color: rgba(20, 122, 95, 0.35);
  background: rgba(20, 122, 95, 0.08);
  color: var(--accent);
}

.btn-sound:hover {
  background: rgba(20, 122, 95, 0.15);
}

.btn-sound.is-muted {
  border-color: rgba(212, 77, 56, 0.45);
  background: rgba(212, 77, 56, 0.1);
  color: #b43624;
}

.btn-sound.is-muted:hover {
  background: rgba(212, 77, 56, 0.18);
}

.btn-exit {
  border-color: rgba(212, 77, 56, 0.45);
  color: #a63422;
  background: rgba(212, 77, 56, 0.08);
}

.btn-exit:hover {
  background: rgba(212, 77, 56, 0.16);
  border-color: rgba(212, 77, 56, 0.7);
}

/* Contenidor principal */
.app-shell {
  width: min(var(--shell-max), 100%);
  margin: 0 auto;
  padding: 0.6rem clamp(0.75rem, 2.2vw, 1.5rem) 0.75rem;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.screen {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  /* Ocupa l'alçada útil i reparteix l'espai sobrant a dalt i a baix */
  flex: 1;
  justify-content: center;
}

/* Botons generals */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  padding: 0.62rem 1rem;
  border-radius: 10px;
  border: 1px solid transparent;
  font-size: 0.92rem;
  font-weight: 650;
  cursor: pointer;
  transition: transform 0.1s, background 0.15s, border-color 0.15s, opacity 0.15s;
  text-decoration: none;
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-sm {
  padding: 0.42rem 0.78rem;
  font-size: 0.82rem;
  border-radius: 8px;
}

.btn-full {
  width: 100%;
}

.btn-primary {
  background: var(--primary);
  color: #ffffff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--surface-2);
  border-color: var(--border-strong);
  color: var(--text);
}

.btn-secondary:hover:not(:disabled) {
  background: #dce9e1;
}

.btn-accent {
  background: var(--accent);
  color: #ffffff;
}

.btn-accent:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-ghost {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface-2);
  border-color: var(--border-strong);
}

/* Pantalla inicial */
.screen-home {
  padding-top: clamp(0.4rem, 3.2dvh, 2rem);
}

.screen-home > * {
  width: 100%;
  max-width: 1040px;
  margin-left: auto;
  margin-right: auto;
}

@media (min-width: 821px) {
  .screen-home {
    flex: 1;
    justify-content: center;
    padding-top: 0;
    padding-bottom: 6dvh;
  }
}

.resume-bar {
  margin: 0 auto;
  padding: 0.55rem 0.85rem;
  border-radius: 10px;
  border: 1px solid rgba(13, 104, 150, 0.35);
  background: #f5faf7;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  font-size: 0.88rem;
  box-shadow: var(--shadow);
}

.resume-actions {
  display: flex;
  gap: 0.45rem;
}

.home-grid {
  display: grid;
  grid-template-columns: 1.25fr 1fr;
  gap: 0.95rem;
  align-items: stretch;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.card-head {
  display: flex;
  align-items: center;
  gap: 0.72rem;
}

.card-icon-wrap {
  width: 2.35rem;
  height: 2.35rem;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: var(--coral-soft);
  border: 1px solid rgba(217, 93, 57, 0.28);
  color: var(--coral);
  flex: none;
}

.card-ai .card-icon-wrap {
  background: rgba(20, 122, 95, 0.1);
  border-color: rgba(20, 122, 95, 0.28);
  color: var(--accent);
}

.card-online .card-icon-wrap {
  background: var(--primary-soft);
  border-color: rgba(21, 96, 130, 0.28);
  color: var(--primary);
}

.card-head h2 {
  margin: 0;
  font-size: 1.12rem;
}

.card-head p {
  margin: 0.15rem 0 0;
  color: var(--muted);
  font-size: 0.86rem;
}

.online-split {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 0.85rem;
  align-items: center;
  flex: 1;
}

.block-title {
  display: block;
  margin: 0 0 0.35rem;
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--muted);
}

.muted-sm {
  margin: 0.2rem 0 0.65rem;
  color: var(--muted);
  font-size: 0.82rem;
}

.divider-or {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
}

.join-row {
  display: flex;
  gap: 0.45rem;
}

.join-row input {
  flex: 1;
  min-width: 0;
  padding: 0.56rem 0.72rem;
  border-radius: 10px;
  border: 1px solid var(--border-strong);
  background: #f5faf7;
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.16em;
  text-transform: lowercase;
  text-align: center;
}

.join-row input::placeholder {
  letter-spacing: 0.03em;
  text-transform: none;
  font-weight: 500;
  font-size: 0.88rem;
  color: #5c7a6f;
}

.join-row input:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.inline-error {
  margin: 0;
  padding: 0.5rem 0.72rem;
  border-radius: 8px;
  background: rgba(220, 38, 38, 0.1);
  border: 1px solid rgba(220, 38, 38, 0.4);
  color: #991b1b;
  font-size: 0.85rem;
}

/* Nivells IA */
.ai-levels {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
  flex: 1;
  align-content: center;
}

.level-chip {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.15rem;
  padding: 0.58rem 0.65rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #f5faf7;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.level-chip strong {
  font-size: 0.9rem;
}

.level-chip span {
  font-size: 0.76rem;
  color: var(--muted);
  line-height: 1.25;
}

.level-chip:hover {
  border-color: var(--border-strong);
}

.level-chip.active {
  border-color: var(--accent);
  background: rgba(15, 118, 110, 0.1);
}

/* Bàner d'instal·lació mòbil */
.install-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.8rem;
  padding: 0.72rem 1rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: linear-gradient(90deg, #fcfefd, #e9f2ec);
  box-shadow: var(--shadow);
}

.install-banner-text {
  display: flex;
  align-items: center;
  gap: 0.7rem;
}

.install-ic {
  width: 1.65rem;
  height: 1.65rem;
  color: var(--primary);
}

.install-banner-text strong {
  display: block;
  font-size: 0.92rem;
}

.install-banner-text span {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
}

/* Barra superior de fase (col·locació) */
.phase-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.65rem;
  padding: 0.1rem 0.1rem 0.2rem;
}

.phase-info {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 0.55rem;
}

.phase-badge {
  padding: 0.22rem 0.65rem;
  border-radius: 999px;
  background: var(--primary-soft);
  border: 1px solid rgba(13, 104, 150, 0.32);
  color: var(--primary);
  font-size: 0.8rem;
  font-weight: 700;
}

.phase-status {
  font-size: 0.9rem;
  color: var(--text);
}

.room-share-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
}

.room-code-pill {
  padding: 0.28rem 0.65rem;
  border-radius: 8px;
  background: #fffaf0;
  border: 1px solid var(--border-strong);
  font-size: 0.84rem;
}

.room-code-pill strong {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0.14em;
  color: var(--primary);
  font-size: 1rem;
}

/* Disseny de col·locació */
.place-layout {
  display: grid;
  grid-template-columns: auto minmax(280px, 345px);
  justify-content: center;
  align-items: start;
  gap: 1.25rem;
}

.board-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.42rem;
}

.dock-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.8rem 0.95rem;
  box-shadow: var(--shadow);
}

.board-card-head,
.dock-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.6rem;
}

.board-card-head {
  width: 100%;
  padding: 0 0.1rem 0 1.45rem;
  margin-bottom: 0;
}

.dock-head {
  margin-bottom: 0.5rem;
}

.board-card-head h2,
.dock-head h2 {
  margin: 0;
  font-size: 1rem;
}

.board-sub,
.dock-count {
  font-size: 0.78rem;
  color: var(--muted);
}

.dock-count {
  font-weight: 700;
  color: var(--primary);
}

/* Tauler 10x10 */
.board-wrap {
  display: inline-block;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.board-grid {
  display: grid;
  grid-template-columns: 1.4rem repeat(10, var(--cell-size));
  grid-template-rows: 1.35rem repeat(10, var(--cell-size));
  gap: 2px;
  padding: 0;
  background: transparent;
  border: none;
  box-shadow: none;
}

#board-place .board-grid {
  --cell-size: var(--cell-place);
}

.battle-boards .board-grid {
  --cell-size: var(--cell-battle);
}

.coord-label {
  display: grid;
  place-items: center;
  font-size: 0.74rem;
  font-weight: 750;
  color: #24473d;
  font-variant-numeric: tabular-nums;
}

.cell {
  position: relative;
  width: var(--cell-size);
  height: var(--cell-size);
  padding: 0;
  border: 1px solid var(--sea-border);
  border-radius: 4px;
  background: linear-gradient(145deg, var(--sea-1), var(--sea-2));
  background-color: var(--sea-2);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: border-color 0.1s, transform 0.08s;
}

.cell:focus-visible {
  outline: 2px solid #fff7ed;
  outline-offset: -2px;
  z-index: 2;
}

@media (hover: hover) {
  .cell.interactive:not(.shot-aigua):not(.shot-tocat):not(.shot-enfonsat):not(.shot-halo):not(.ship):not(.preview-ok):not(.preview-bad):hover {
    border-color: #fef3c7;
    background: linear-gradient(145deg, #2788af, #1b6b8f);
  }
}

/* Marge d'aigua obligatori en col·locació */
.cell.ship-halo {
  background: linear-gradient(145deg, #175c79, #124861);
}

.cell.ship-halo::after {
  content: '';
  width: 22%;
  height: 22%;
  border-radius: 50%;
  background: rgba(255, 250, 238, 0.28);
}

/* Vaixells (fusta càlida de teca i detall de llautó) */
.cell.ship {
  background: linear-gradient(145deg, var(--ship-wood-1), var(--ship-wood-2));
  border-color: var(--ship-border);
}

.cell.ship::before {
  content: '';
  width: 34%;
  height: 34%;
  border-radius: 50%;
  background: rgba(255, 244, 224, 0.55);
  border: 1px solid rgba(120, 66, 26, 0.45);
}

.cell.ship-h.ship-start { border-top-left-radius: 48%; border-bottom-left-radius: 48%; }
.cell.ship-h.ship-end   { border-top-right-radius: 48%; border-bottom-right-radius: 48%; }
.cell.ship-v.ship-start { border-top-left-radius: 48%; border-top-right-radius: 48%; }
.cell.ship-v.ship-end   { border-bottom-left-radius: 48%; border-bottom-right-radius: 48%; }

.cell.ship-reveal {
  background: rgba(235, 198, 152, 0.48);
  border-color: rgba(254, 243, 199, 0.78);
  border-style: dashed;
}

/* Previsualització de col·locació */
.cell.preview-ok {
  background: linear-gradient(145deg, #34c48f, #13946a);
  border-color: #ecfdf5;
}

.cell.preview-bad {
  background: linear-gradient(145deg, #ef5350, #c62828);
  border-color: #fee2e2;
}

.cell.preview-ok::before,
.cell.preview-bad::before,
.cell.preview-ok::after,
.cell.preview-bad::after {
  display: none;
}

/* Estats de tret */
.cell.shot-aigua {
  background: linear-gradient(145deg, #134d68, #0c394e);
  background-color: #10465e;
  cursor: default;
}

.cell.shot-aigua::after {
  content: '';
  width: 42%;
  height: 42%;
  border-radius: 50%;
  border: 2px solid rgba(240, 249, 255, 0.78);
  background: rgba(240, 249, 255, 0.22);
}

/* Aigua marcada automàticament al voltant d'un vaixell enfonsat */
.cell.shot-halo {
  background: linear-gradient(145deg, #165572, #10425a);
  background-color: #134e69;
  cursor: default;
}

.cell.shot-halo::after {
  content: '';
  width: 24%;
  height: 24%;
  border-radius: 50%;
  background: rgba(224, 244, 250, 0.48);
}

.cell.shot-tocat {
  background: linear-gradient(145deg, #f97316, #ea580c);
  border-color: #ffedd5;
  cursor: default;
  animation: popHit 0.24s ease-out;
}

.cell.shot-tocat::after {
  content: '';
  width: 46%;
  height: 46%;
  border-radius: 50%;
  background: #fff7ed;
  box-shadow: 0 0 8px rgba(255, 237, 213, 0.9);
}

.cell.shot-enfonsat {
  background: linear-gradient(145deg, #dc2626, #991b1b);
  border-color: #fecaca;
  cursor: default;
  animation: popHit 0.28s ease-out;
}

.cell.shot-tocat::before,
.cell.shot-enfonsat::before {
  display: none;
}

.cell.shot-enfonsat::after {
  content: '✕';
  color: #fff5f5;
  font-size: calc(var(--cell-size) * 0.54);
  font-weight: 800;
  line-height: 1;
}

.cell.shot-pending {
  border-color: #fef08a;
  background: linear-gradient(145deg, #1d8cc4, #0f6894);
}

.cell.shot-pending::after {
  content: '';
  width: 42%;
  height: 42%;
  border-radius: 50%;
  border: 2px solid #fef08a;
  opacity: 0.9;
}

@keyframes popHit {
  0% { transform: scale(0.84); }
  65% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

.cell.last-shot {
  box-shadow: inset 0 0 0 2px #fde047, 0 0 10px rgba(253, 224, 71, 0.65);
  z-index: 2;
}

.cell.aim-pending {
  border-color: #fde047;
  background: rgba(250, 204, 21, 0.35);
  box-shadow: inset 0 0 0 2px #fde047;
}

/* Drassana */
.ship-dock {
  display: flex;
  flex-direction: column;
  gap: 0.42rem;
}

.dock-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.48rem 0.65rem;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: #f5faf7;
  color: var(--text);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.dock-item:hover {
  border-color: var(--border-strong);
}

.dock-item.selected {
  border-color: var(--primary);
  background: rgba(21, 96, 130, 0.1);
}

.dock-item.placed {
  border-color: rgba(15, 118, 110, 0.42);
  background: rgba(15, 118, 110, 0.08);
}

.dock-item-name {
  font-size: 0.88rem;
  font-weight: 650;
}

.dock-pips {
  display: flex;
  align-items: center;
  gap: 3px;
}

.pip {
  width: 0.72rem;
  height: 0.72rem;
  border-radius: 3px;
  background: var(--ship-wood-1);
  border: 1px solid var(--ship-border);
}

.dock-item.placed .pip {
  background: var(--accent);
  border-color: #0b5a54;
}

.dock-tools {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.45rem;
  margin-top: 0.65rem;
}

.dock-tools #btn-rotate {
  grid-column: 1 / -1;
}

.dock-tools .btn {
  min-width: 0;
  padding: 0.5rem 0.65rem;
  font-size: 0.84rem;
  white-space: nowrap;
}

.kbd-hint {
  padding: 0.02rem 0.36rem;
  border-radius: 5px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  background: var(--surface-2);
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1.3;
}

@media (hover: none) {
  .kbd-hint {
    display: none;
  }
}

.btn-ready {
  margin-top: 0.65rem;
}

.btn-ready:disabled {
  opacity: 1;
  background: #e2ebe5;
  border-color: var(--border);
  color: var(--muted);
}

/* Combat */
.battle-hud {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  padding: 0.1rem 0.1rem 0.2rem;
}

.turn-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.3rem 0.75rem;
  border-radius: 999px;
  font-size: 0.88rem;
  font-weight: 700;
}

.turn-pill[data-turn="me"] {
  background: rgba(15, 118, 110, 0.12);
  border: 1px solid rgba(15, 118, 110, 0.45);
  color: #0b5a54;
}

.turn-pill[data-turn="rival"] {
  background: rgba(217, 93, 57, 0.12);
  border: 1px solid rgba(217, 93, 57, 0.45);
  color: #a93c1e;
}

.turn-pill[data-turn="done"] {
  background: var(--primary-soft);
  border: 1px solid rgba(21, 96, 130, 0.45);
  color: var(--primary);
}

.pulse-dot {
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  background: currentColor;
}

.hud-sep {
  width: 1px;
  height: 1.2rem;
  background: var(--border-strong);
}

.last-event {
  font-size: 0.88rem;
  color: var(--text);
  font-weight: 650;
}

.battle-boards {
  display: grid;
  grid-template-columns: repeat(2, auto);
  justify-content: center;
  gap: clamp(0.85rem, 2.4vw, 2rem);
}

.battle-boards .board-grid {
  transition: opacity 0.25s, filter 0.25s;
}

@media (min-width: 821px) {
  .battle-boards[data-turn="me"] .board-mine .board-grid,
  .battle-boards[data-turn="pending"] .board-mine .board-grid,
  .battle-boards[data-turn="rival"] .board-enemy .board-grid {
    opacity: 0.68;
    filter: saturate(0.75);
  }
}

.battle-boards[data-turn="rival"] .board-enemy .cell {
  cursor: wait;
}

.battle-boards[data-turn="me"] .board-enemy .board-card-head h2,
.battle-boards[data-turn="rival"] .board-mine .board-card-head h2 {
  color: var(--accent);
}

.battle-boards[data-turn="me"] .board-enemy .board-card-head h2::before,
.battle-boards[data-turn="rival"] .board-mine .board-card-head h2::before {
  content: '';
  display: inline-block;
  width: 0.5rem;
  height: 0.5rem;
  margin-right: 0.4rem;
  border-radius: 50%;
  background: currentColor;
  vertical-align: 0.1em;
}

.fleet-status-strip {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  width: 100%;
  max-width: calc(1.4rem + 10 * var(--cell-battle) + 1.15rem);
  gap: 0.35rem 0.85rem;
  margin-top: 0.3rem;
  padding-left: 1.4rem;
}

.fleet-mini {
  position: relative;
  display: inline-flex;
  gap: 2px;
  transition: opacity 0.2s;
}

.fleet-mini i {
  display: block;
  width: 12px;
  height: 12px;
  background: linear-gradient(180deg, var(--ship-wood-1), var(--ship-wood-2));
  border: 1px solid var(--ship-border);
  border-radius: 2px;
}

.fleet-mini i:first-child {
  border-top-left-radius: 6px;
  border-bottom-left-radius: 6px;
}

.fleet-mini i:last-child {
  border-top-right-radius: 6px;
  border-bottom-right-radius: 6px;
}

.fleet-mini.sunk {
  opacity: 0.6;
}

.fleet-mini.sunk i {
  background: linear-gradient(180deg, #ef4444, #991b1b);
  border-color: #7f1d1d;
}

.fleet-mini.sunk::after {
  content: '';
  position: absolute;
  left: -3px;
  right: -3px;
  top: 50%;
  height: 2px;
  margin-top: -1px;
  background: #7f1d1d;
}

.confirm-shot-toggle {
  display: none;
  position: relative;
  align-items: center;
  justify-content: center;
  gap: 0.2rem;
  padding: 0 0.6rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 800;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, color 0.15s;
}

.confirm-shot-toggle:has(.confirm-shot-input:checked) {
  border-color: var(--primary);
  background: var(--primary);
  color: #ffffff;
}

.confirm-shot-input {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
  pointer-events: none;
}

.confirm-shot-toggle:has(.confirm-shot-input:focus-visible) {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* Tabs mòbils */
.mobile-tabs {
  display: none;
  grid-template-columns: 1fr 1fr auto;
  gap: 0.45rem;
}

.mobile-tab {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  padding: 0.52rem 0.7rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 700;
  cursor: pointer;
}

.mobile-tab.active {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
}

.tab-badge {
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 0.75rem;
}

/* Banner final de partida */
.gameover-banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.85rem;
  padding: 0.75rem 1rem;
  border-radius: 12px;
  border: 1px solid var(--border-strong);
  background: #f5faf7;
  box-shadow: var(--shadow);
}

.gameover-banner.win {
  border-color: rgba(15, 118, 110, 0.65);
}

.gameover-banner.lose {
  border-color: rgba(220, 38, 38, 0.55);
}

.gameover-main h2 {
  margin: 0;
  font-size: 1.15rem;
}

.gameover-main p {
  margin: 0.15rem 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.fairplay-note {
  color: #0b5a54 !important;
  font-weight: 600;
}

.fairplay-warning {
  color: #991b1b !important;
}

.gameover-stats {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.stat-box {
  padding: 0.35rem 0.68rem;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  text-align: center;
}

.stat-box strong {
  display: block;
  font-size: 1rem;
  color: var(--primary);
}

.stat-box span {
  display: block;
  font-size: 0.72rem;
  line-height: 1.15;
  color: var(--muted);
}

.gameover-actions {
  display: flex;
  gap: 0.5rem;
}

/* Modals */
.modal {
  border: none;
  padding: 0;
  background: transparent;
  max-width: min(440px, 92vw);
  color: var(--text);
}

.modal::backdrop {
  background: rgba(23, 49, 71, 0.55);
  backdrop-filter: blur(4px);
}

.modal-card {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  padding: 1.05rem 1.15rem;
  box-shadow: var(--shadow);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.55rem;
}

.modal-head h3 {
  margin: 0;
  font-size: 1.08rem;
}

.btn-close {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  width: 2rem;
  height: 2rem;
  border-radius: 8px;
  cursor: pointer;
}

.qr-box {
  width: min(230px, 66vw);
  margin: 0.7rem auto 0.5rem;
  padding: 0.35rem;
  border-radius: 12px;
  background: #ffffff;
  border: 1px solid var(--border);
}

.qr-box svg {
  display: block;
  width: 100%;
  height: auto;
}

.qr-code-display {
  text-align: center;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: 0.2em;
  color: var(--primary);
  margin-bottom: 0.25rem;
}

.qr-url-text {
  margin: 0 0 0.7rem;
  padding: 0.32rem 0.55rem;
  border-radius: 8px;
  background: #eff6f2;
  border: 1px solid var(--border);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.74rem;
  color: var(--muted);
  text-align: center;
  word-break: break-all;
  user-select: all;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.9rem;
}

.rules-list,
.install-steps {
  margin: 0.4rem 0 0;
  padding-left: 1.15rem;
  display: flex;
  flex-direction: column;
  gap: 0.42rem;
  font-size: 0.88rem;
  color: var(--text);
}

/* Modal de regles visual */
.modal-rules {
  max-width: min(720px, 94vw);
  width: min(720px, 94vw);
}

.modal-card-rules {
  max-height: 88dvh;
  display: flex;
  flex-direction: column;
  padding: 1.1rem 1.2rem;
  background: linear-gradient(180deg, #fcfefd, #f5faf7);
}

.modal-head-rules {
  padding-bottom: 0.7rem;
  margin-bottom: 0.8rem;
  border-bottom: 1px solid var(--border);
}

.rules-title-wrap {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.rules-title-ic {
  width: 2.35rem;
  height: 2.35rem;
  border-radius: 10px;
  background: rgba(13, 104, 150, 0.12);
  border: 1px solid rgba(13, 104, 150, 0.25);
  color: var(--primary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
}

.rules-subtitle {
  margin: 0.12rem 0 0;
  color: var(--muted);
  font-size: 0.82rem;
}

.rules-grid {
  display: grid;
  grid-template-columns: 1.05fr 0.95fr;
  gap: 0.72rem;
  overflow-y: auto;
  padding-right: 2px;
}

.rule-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.78rem 0.88rem;
  display: flex;
  flex-direction: column;
  gap: 0.48rem;
}

.rule-card-wide {
  grid-column: 1 / -1;
}

.rule-card-soft {
  background: #eff6f2;
}

.rule-card-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.rule-card-head h4 {
  margin: 0;
  font-size: 0.94rem;
  color: var(--text);
}

.rule-step {
  width: 1.45rem;
  height: 1.45rem;
  border-radius: 999px;
  background: var(--primary);
  color: #fcfefd;
  font-size: 0.78rem;
  font-weight: 800;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
}

.rule-desc {
  margin: 0;
  font-size: 0.83rem;
  color: var(--muted);
  line-height: 1.4;
}

.rule-fleet-list {
  display: flex;
  flex-direction: column;
  gap: 0.36rem;
  margin-top: 0.1rem;
}

.rule-ship-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  font-size: 0.82rem;
  padding: 0.22rem 0.45rem;
  border-radius: 8px;
  background: #f5faf7;
  border: 1px solid #dbe9e0;
}

.mini-ship {
  display: inline-flex;
  gap: 2px;
}

.mini-ship i {
  display: block;
  width: 14px;
  height: 14px;
  background: linear-gradient(180deg, var(--ship-wood-1), var(--ship-wood-2));
  border: 1px solid var(--ship-border);
  border-radius: 2px;
}

.mini-ship i:first-child {
  border-top-left-radius: 7px;
  border-bottom-left-radius: 7px;
}

.mini-ship i:last-child {
  border-top-right-radius: 7px;
  border-bottom-right-radius: 7px;
}

.rule-diagram-wrap {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem;
  border-radius: 10px;
  background: #eef5f0;
  border: 1px solid #d4e3da;
}

.rule-mini-grid {
  display: grid;
  grid-template-columns: repeat(5, 24px);
  grid-template-rows: repeat(3, 24px);
  gap: 2px;
  padding: 5px;
  border-radius: 9px;
  background: #f7fbf8;
  border: 1px solid var(--border);
}

.rg-cell {
  position: relative;
  border-radius: 4px;
  background: var(--sea-1);
}

.rg-halo {
  background: #0d4b6e;
}

.rg-halo::after {
  content: "";
  position: absolute;
  inset: 9px;
  border-radius: 50%;
  background: rgba(226, 244, 253, 0.5);
}

.rg-ship {
  background: linear-gradient(180deg, var(--ship-wood-1), var(--ship-wood-2));
  border: 1px solid var(--ship-border);
  border-radius: 3px;
}

.rg-bow-w {
  border-top-left-radius: 11px;
  border-bottom-left-radius: 11px;
}

.rg-bow-e {
  border-top-right-radius: 11px;
  border-bottom-right-radius: 11px;
}

.rule-caption {
  font-size: 0.76rem;
  font-weight: 600;
  color: var(--muted);
  text-align: center;
}

.rule-legend-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.55rem;
}

.rule-legend-item {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.45rem 0.55rem;
  border-radius: 9px;
  background: #f5faf7;
  border: 1px solid #dbe9e0;
}

.rule-legend-item strong {
  display: block;
  font-size: 0.83rem;
}

.rule-legend-item span {
  display: block;
  font-size: 0.76rem;
  color: var(--muted);
  line-height: 1.28;
}

.rg-demo {
  position: relative;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  flex: none;
}

.rg-water {
  background: #0d4b6e;
  border: 1px solid rgba(255, 255, 255, 0.25);
}

.rg-water::after {
  content: "";
  position: absolute;
  inset: 8px;
  border-radius: 50%;
  border: 2px solid rgba(226, 244, 253, 0.78);
}

.rg-hit {
  background: radial-gradient(circle at 50% 42%, #ff9b42, var(--hit));
  border: 1px solid rgba(255, 255, 255, 0.45);
}

.rg-hit::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 11px;
  height: 11px;
  transform: translate(-50%, -50%) rotate(45deg);
  background: #fff7ed;
  border-radius: 2px 10px 10px 10px;
}

.rg-sunk {
  background: radial-gradient(circle at 50% 40%, #ef4444, #991b1b);
  border: 1px solid #fecaca;
}

.rg-sunk::before,
.rg-sunk::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 14px;
  height: 2.5px;
  border-radius: 2px;
  background: #fff7ed;
}

.rg-sunk::before {
  transform: translate(-50%, -50%) rotate(45deg);
}

.rg-sunk::after {
  transform: translate(-50%, -50%) rotate(-45deg);
}

.rule-modes-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.55rem;
}

.rule-mode-pill {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 0.5rem 0.65rem;
  display: flex;
  flex-direction: column;
  gap: 0.16rem;
}

.rule-mode-pill strong {
  font-size: 0.83rem;
  color: var(--primary);
}

.rule-mode-pill span {
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.35;
}

.modal-actions-rules {
  margin-top: 0.78rem;
  padding-top: 0.65rem;
  border-top: 1px solid var(--border);
}

/* Toast */
.toast {
  position: fixed;
  bottom: 68px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 40;
  padding: 0.52rem 0.95rem;
  border-radius: 999px;
  background: #16332c;
  border: 1px solid rgba(244, 249, 246, 0.35);
  color: #fcfefd;
  font-size: 0.86rem;
  font-weight: 600;
  box-shadow: var(--shadow);
}

/* Peu corporatiu (felip-app-footer) integrat amb la paleta Costa Verda */
.felip-app-footer {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 15;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 10px;
  padding: 6px clamp(14px, 3vw, 34px) 7px;
  border-top: 1px solid var(--border);
  background: rgba(244, 249, 246, 0.96);
  backdrop-filter: blur(8px);
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.24;
  text-align: left;
}

.felip-license-image-link {
  display: block;
  line-height: 0;
  flex: none;
}

.felip-license-image {
  display: block;
  width: 74px;
  height: auto;
}

.felip-footer-text {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}

.felip-footer-text p {
  margin: 0;
}

.felip-footer-text a {
  color: var(--text);
  font-weight: 650;
  text-decoration: none;
}

.felip-footer-text a:hover,
.felip-footer-text a:focus-visible {
  color: var(--primary);
  outline: none;
}

@media (min-width: 821px) {
  .install-banner {
    display: none;
  }
}

/* Responsive: tauletes i mòbils */
@media (max-width: 900px) {
  .home-grid {
    grid-template-columns: 1fr;
  }

  .online-split {
    grid-template-columns: 1fr;
    gap: 0.65rem;
  }

  .place-layout {
    grid-template-columns: 1fr;
    justify-items: center;
  }

  .board-card,
  .dock-card {
    width: min(100%, 440px);
  }
}

@media (max-width: 820px) {
  :root {
    font-size: 14px;
    --cell-place: clamp(24px, min(calc((100cqw - 42px) / 10), 5dvh), 40px);
    --cell-battle: clamp(24px, min(calc((100cqw - 42px) / 10), 5.6dvh), 40px);
  }

  body {
    padding-bottom: 58px;
  }

  .topbar-inner {
    padding: 0.42rem 0.6rem;
    gap: 0.4rem;
  }

  .brand {
    font-size: clamp(1.05rem, 4.6vw, 1.4rem);
    gap: 0.45rem;
    margin-left: -0.25rem;
    padding: 0.1rem 0.25rem;
    min-width: 0;
  }

  .brand-name {
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .brand-ic {
    width: 2rem;
    height: 2rem;
    border-radius: 7px;
  }

  .topbar-actions {
    gap: 0.3rem;
  }

  .topbar .btn-icon {
    width: 2.45rem;
    height: 2.45rem;
    justify-content: center;
    padding: 0;
  }

  .topbar .btn-icon .btn-label,
  .btn-install-top {
    display: none !important;
  }

  .app-shell {
    padding: 0.45rem 0.4rem 0.65rem;
    container-type: inline-size;
  }

  /* Les distàncies creixen amb l'alçada disponible */
  .screen {
    gap: 0.5rem;
  }

  /* Combat al mòbil: l'espai sobrant es reparteix en tres parts iguals
     (sobre el torn, entre el torn i el tauler, i sota el tauler) */
  .screen-battle {
    justify-content: flex-start;
  }

  .screen-battle > .battle-hud,
  .screen-battle > .mobile-tabs {
    margin-top: auto;
  }

  .screen-battle > .battle-boards {
    margin-bottom: auto;
  }

  .battle-boards .board-card {
    gap: clamp(0.38rem, 2.2dvh, 1.4rem);
  }

  .mobile-tab {
    padding-block: clamp(0.52rem, 1.5dvh, 0.95rem);
  }

  .turn-pill {
    padding: clamp(0.3rem, 0.9dvh, 0.5rem) 0.85rem;
  }

  .battle-hud {
    padding-bottom: clamp(0rem, 0.8dvh, 0.5rem);
  }

  .card {
    padding: 0.82rem 0.92rem;
    gap: 0.65rem;
  }

  .hide-sm {
    display: none;
  }

  .mobile-tabs {
    display: grid;
  }

  .battle-boards {
    grid-template-columns: 1fr;
    justify-items: center;
  }

  .battle-boards[data-mobile-view="enemy"] .board-mine {
    display: none;
  }

  .battle-boards[data-mobile-view="mine"] .board-enemy {
    display: none;
  }

  .confirm-shot-toggle {
    display: flex;
  }

  .install-banner {
    flex-direction: column;
    align-items: stretch;
  }

  .board-card {
    width: 100%;
    max-width: 440px;
    gap: 0.38rem;
  }

  .board-grid {
    padding: 0;
    grid-template-columns: 1.2rem repeat(10, var(--cell-size));
    grid-template-rows: 1.15rem repeat(10, var(--cell-size));
  }

  .board-card-head {
    padding-left: 1.25rem;
  }

  .battle-boards .board-card-head {
    display: none;
  }

  /* Torn i últim tret sempre en dues files fixes: el tauler no es mou */
  .battle-hud {
    flex-direction: column;
    flex-wrap: nowrap;
    gap: 0.3rem;
  }

  .turn-pill,
  .last-event {
    max-width: 100%;
  }

  #turn-text,
  .last-event {
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .last-event {
    line-height: 1.35;
    min-height: 1.35em;
  }

  .hud-sep {
    display: none;
  }

  /* Pantalla final compacta: títol a dalt, estadístiques i botons en una sola fila */
  .gameover-banner {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: stretch;
    gap: 0.5rem 0.55rem;
    padding: 0.6rem 0.65rem;
  }

  .gameover-main {
    grid-column: 1 / -1;
  }

  .gameover-main h2 {
    font-size: 1rem;
  }

  .gameover-stats {
    flex-wrap: nowrap;
    gap: 0.35rem;
  }

  .stat-box {
    flex: 1 1 0;
    min-width: 0;
    padding: 0.3rem 0.25rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .gameover-actions {
    flex-direction: column;
    justify-content: center;
    gap: 0.35rem;
  }

  .gameover-actions .btn {
    padding: 0.42rem 0.75rem;
    font-size: 0.82rem;
  }

  .fleet-mini i {
    width: clamp(9px, 1.7dvh, 15px);
    height: clamp(9px, 1.7dvh, 15px);
  }

  .board-card-head,
  .fleet-status-strip {
    width: 100%;
  }

  .fleet-status-strip {
    max-width: 100%;
    justify-content: center;
    padding-left: 1.2rem;
    gap: 0.35rem 0.6rem;
  }

  .felip-license-image {
    width: 60px;
  }

  .felip-app-footer {
    font-size: 0.72rem;
    gap: 8px;
    padding: 5px 12px 6px;
  }

  .rules-grid,
  .rule-legend-grid,
  .rule-modes-row {
    grid-template-columns: 1fr;
  }

  .modal-card-rules {
    padding: 0.9rem;
  }
}
