/* Battleship: one framed sea board. The grid the player acts on is large and
   carries coordinates; the other grid is a smaller context panel in the same
   frame. Roles swap at Start battle (data-focus), or on Enlarge. The sea-blue
   frame extends the Connect Four board exception; marks reuse rust and ink. */
:root:not([data-game='battleship']) .only-battleship {
  display: none !important;
}
:root[data-game='battleship'] .app {
  /* Wider than the square boards; the height term keeps frame and action row
     on one screen, as the shared column does for the other games. */
  --col: min(680px, 100%, calc((100dvh - 200px) * 1.25));
}
.app[data-pregame='true'] .bs-wrap {
  filter: blur(0.6px);
}
.app[data-view='fatal'] .bs-wrap {
  filter: saturate(0.5) opacity(0.45);
}
.side-pip[data-color='blue'] {
  background: #416e81;
}

.bs-wrap {
  --bs-frame-a: #416e81;
  --bs-frame-b: #2f5366;
  --bs-label: #d9e6ec;
  --bs-cell: var(--paper, #f3efe7);
  --bs-fog: #e6e9e7;
  --bs-ship: #4d5c62;
  --bs-ship-edge: #3b474c;
  --bs-hit: var(--accent, #a8452f);
  --bs-sunk: var(--accent-deep, #8e3825);
  --bs-miss: #7f8c93;
  position: relative;
  align-self: start;
  display: grid;
  grid-template-columns: minmax(0, 1fr) clamp(150px, 29%, 196px);
  grid-template-areas: 'main side';
  gap: 20px;
  width: 100%;
  padding: 16px;
  border-radius: 16px;
  background: linear-gradient(145deg, var(--bs-frame-a), var(--bs-frame-b));
  box-shadow:
    0 1px 2px rgba(20, 40, 55, 0.12),
    0 10px 30px rgba(20, 40, 55, 0.18),
    inset 0 0 0 1px #ffffff14;
  color: var(--bs-label);
  transition: filter var(--motion-overlay) var(--ease-overlay);
}
.bs-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}
.bs-wrap[data-focus='home'] #bs-home-section,
.bs-wrap[data-focus='target'] #bs-target-section {
  grid-area: main;
}
.bs-wrap[data-focus='home'] #bs-target-section,
.bs-wrap[data-focus='target'] #bs-home-section {
  grid-area: side;
}
.bs-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  min-height: 36px;
}
.bs-heading {
  margin: 0;
  color: #fff;
  font: 600 14px/1.2 var(--sans, system-ui, sans-serif);
  letter-spacing: 0.01em;
}
.bs-subheading {
  display: block;
  min-height: 15px;
  margin-top: 3px;
  color: var(--bs-label);
  font: 500 12px/1.3 var(--sans, system-ui, sans-serif);
  opacity: 0.88;
}
.bs-swap {
  display: inline-flex;
  flex: none;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  margin: -6px -8px -6px 0;
  padding: 0 10px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: #fff;
  font: 600 13px/1 var(--sans, system-ui, sans-serif);
  cursor: pointer;
}
.bs-swap:hover {
  background: #ffffff1a;
}
.bs-swap:focus-visible {
  outline: 2px solid #fff;
  outline-offset: -2px;
}
.bs-swap svg {
  flex: none;
}

.bs-board {
  width: 100%;
}
.bs-grid {
  display: grid;
  grid-template-columns: 18px repeat(10, minmax(0, 1fr));
  grid-template-rows: 18px repeat(10, minmax(0, 1fr));
  gap: 2px;
  width: 100%;
  aspect-ratio: 11 / 11;
}
.bs-axis-label {
  display: grid;
  place-items: center;
  color: var(--bs-label);
  font: 600 11px/1 var(--sans, system-ui, sans-serif);
  opacity: 0.9;
}
.bs-rank-label {
  font-variant-numeric: tabular-nums;
}
.bs-cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 3px;
  background: var(--bs-cell);
  box-shadow: inset 0 1px 2px #142e3e30;
  color: var(--ink, #232925);
  cursor: pointer;
  touch-action: manipulation;
}
.bs-cell:disabled {
  cursor: default;
}
#bs-target .bs-cell:not(.has-ship) {
  background: var(--bs-fog);
  box-shadow: inset 0 1px 2px #142e3e22;
}
#bs-target .bs-cell:not(:disabled):hover {
  background: #fffdf7;
  box-shadow:
    inset 0 0 0 2px #ffffffaa,
    inset 0 1px 2px #142e3e30;
}
.bs-cell:focus-visible {
  z-index: 1;
  outline: 3px solid #fff;
  outline-offset: -1px;
}
/* Own and revealed ships: contiguous cells read as one hull. */
.bs-cell.has-ship {
  background: var(--bs-ship);
  box-shadow:
    inset 0 0 0 1px var(--bs-ship-edge),
    inset 0 2px 0 #ffffff14;
}
.bs-cell.ship-h.ship-start {
  border-top-left-radius: 999px;
  border-bottom-left-radius: 999px;
}
.bs-cell.ship-h.ship-end {
  border-top-right-radius: 999px;
  border-bottom-right-radius: 999px;
}
.bs-cell.ship-v.ship-start {
  border-top-left-radius: 999px;
  border-top-right-radius: 999px;
}
.bs-cell.ship-v.ship-end {
  border-bottom-left-radius: 999px;
  border-bottom-right-radius: 999px;
}
/* Marks: miss = small grey dot, hit = rust peg, sunk = whole hull in deep rust. */
.bs-miss-dot {
  width: 22%;
  height: 22%;
  border-radius: 50%;
  background: var(--bs-miss);
}
.bs-hit-mark {
  width: 52%;
  height: 52%;
  border-radius: 50%;
  background: var(--bs-hit);
  box-shadow:
    inset 0 -2px 3px #0003,
    0 1px 1px #0002;
}
.bs-cell.has-ship .bs-hit-mark {
  background: #d9583f;
}
#bs-target .bs-cell.is-sunk,
.bs-cell.is-sunk {
  background: var(--bs-sunk);
  box-shadow: inset 0 0 0 1px #6f2b1c;
}
.bs-cell.is-sunk .bs-hit-mark {
  background: #f3d3c9;
  box-shadow: none;
  opacity: 0.9;
}
.bs-cell.is-pending {
  box-shadow:
    inset 0 0 0 2px var(--ink, #232925),
    inset 0 1px 2px #142e3e30;
}
.bs-cell.is-pending::after {
  content: '';
  width: 34%;
  height: 34%;
  border: 2px solid var(--ink, #232925);
  border-radius: 50%;
}
#bs-target .bs-cell.is-preview,
.bs-cell.is-preview {
  z-index: 1;
  background: #fffdf7;
  outline: 3px solid var(--bs-hit);
  outline-offset: -1px;
}
.bs-cell.is-preview::before {
  content: '';
  position: absolute;
  inset: 24%;
  border: 2px solid var(--bs-hit);
  border-radius: 50%;
}
.bs-cell.is-preview::after {
  content: '';
  position: absolute;
  inset: 0;
  background:
    linear-gradient(var(--bs-hit), var(--bs-hit)) center / 2px 100% no-repeat,
    linear-gradient(var(--bs-hit), var(--bs-hit)) center / 100% 2px no-repeat;
  -webkit-mask: radial-gradient(circle, transparent 0 36%, #000 36%);
  mask: radial-gradient(circle, transparent 0 36%, #000 36%);
}

/* Ship list: silhouettes with names instead of bare lengths. */
.bs-fleet {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}
.bs-ship-status {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #fff;
  font: 500 12px/1 var(--sans, system-ui, sans-serif);
}
.bs-ship-hull {
  display: inline-grid;
  grid-auto-flow: column;
  gap: 2px;
}
.bs-ship-hull i {
  width: 10px;
  height: 10px;
  border-radius: 1px;
  background: #cfdbe1;
}
.bs-ship-hull i:first-child {
  border-radius: 5px 1px 1px 5px;
}
.bs-ship-hull i:last-child {
  border-radius: 1px 5px 5px 1px;
}
.bs-ship-name {
  opacity: 0.92;
}
.bs-ship-status.is-sunk {
  color: #e9b9ad;
}
.bs-ship-status.is-sunk .bs-ship-hull i {
  background: var(--bs-hit);
}
.bs-ship-status.is-sunk .bs-ship-name {
  text-decoration: line-through;
  text-decoration-color: #e9b9ad99;
}

/* The large grid lists its ships in one wrapping row below the coordinates. */
.bs-wrap[data-focus='home'] #bs-home-section .bs-fleet,
.bs-wrap[data-focus='target'] #bs-target-section .bs-fleet {
  flex-direction: row;
  flex-wrap: wrap;
  gap: 6px 14px;
  min-height: 34px;
  margin-top: 2px;
}
/* The context grid drops coordinates and keeps its ship list beside/below it. */
.bs-wrap[data-focus='home'] #bs-target-section .bs-grid,
.bs-wrap[data-focus='target'] #bs-home-section .bs-grid {
  grid-template-columns: repeat(10, minmax(0, 1fr));
  grid-template-rows: repeat(10, minmax(0, 1fr));
  gap: 1.5px;
  aspect-ratio: 1;
}
.bs-wrap[data-focus='home'] #bs-target-section .bs-axis-label,
.bs-wrap[data-focus='home'] #bs-target-section .bs-axis-corner,
.bs-wrap[data-focus='target'] #bs-home-section .bs-axis-label,
.bs-wrap[data-focus='target'] #bs-home-section .bs-axis-corner {
  display: none;
}
.bs-wrap[data-focus='home'] #bs-target-section .bs-cell,
.bs-wrap[data-focus='target'] #bs-home-section .bs-cell {
  border-radius: 2px;
}
.bs-wrap[data-focus='home'] #bs-target-section .bs-cell.is-preview,
.bs-wrap[data-focus='target'] #bs-home-section .bs-cell.is-preview {
  outline-width: 2px;
}
.bs-wrap[data-focus='home'] #bs-target-section .bs-cell.is-pending::after,
.bs-wrap[data-focus='target'] #bs-home-section .bs-cell.is-pending::after {
  display: none;
}
.bs-wrap[data-focus='home'] #bs-target-section .bs-miss-dot,
.bs-wrap[data-focus='target'] #bs-home-section .bs-miss-dot {
  width: 30%;
  height: 30%;
}
.bs-wrap[data-focus='home'] #bs-target-section .bs-hit-mark,
.bs-wrap[data-focus='target'] #bs-home-section .bs-hit-mark {
  width: 62%;
  height: 62%;
  box-shadow: none;
}

/* Fleet-phase actions share the game-action row under the board. */
.bs-controls {
  display: contents;
}
.bs-controls:empty {
  display: none;
}
.bs-controls button {
  min-height: 44px;
  padding: 0 18px;
  border: 1px solid var(--invite-line, #e7e3da);
  border-radius: 10px;
  font: 600 15px/1.2 var(--sans, system-ui, sans-serif);
  white-space: nowrap;
  cursor: pointer;
}
.bs-controls .bs-primary-action {
  margin-left: auto;
  border-color: var(--invite-action, #2e5a47);
  background: var(--invite-action, #2e5a47);
  color: #fff;
}
.bs-controls .bs-primary-action:hover:not(:disabled) {
  background: var(--invite-action-hover, #244936);
}
.bs-controls .bs-secondary-action {
  background: var(--surface, #fbf9f4);
  color: var(--ink, #232925);
}
.bs-controls button:disabled {
  opacity: 0.65;
  cursor: default;
}

@media (max-width: 520px) {
  .bs-wrap {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: 'main' 'side';
    gap: 14px;
    padding: 12px;
  }
  /* Context panel: thumbnail beside its ship list. */
  .bs-wrap[data-focus='home'] #bs-target-section,
  .bs-wrap[data-focus='target'] #bs-home-section {
    display: grid;
    grid-template-columns: 136px minmax(0, 1fr);
    gap: 8px 14px;
    align-items: start;
  }
  .bs-wrap[data-focus='home'] #bs-target-section .bs-head,
  .bs-wrap[data-focus='target'] #bs-home-section .bs-head {
    grid-column: 1 / -1;
  }
  .bs-wrap[data-focus='home'] #bs-target-section .bs-fleet,
  .bs-wrap[data-focus='target'] #bs-home-section .bs-fleet {
    margin-top: 0;
  }
  /* Pregame on narrow screens: the card covers the empty context grid below
     the large grid. Short screens move it farther up over the large grid. */
  :root[data-game='battleship'] .board-area {
    --invite-top: calc(100cqw + 8px);
    --invite-bottom: auto;
    container-type: inline-size;
  }
  .bs-controls button {
    padding: 0 14px;
  }
}
@media (max-width: 520px) and (max-height: 680px) {
  :root[data-game='battleship'] .board-area {
    /* Anchor the card's bottom above a 20px viewport gutter after the 134px
       header/status area. Extra setup content grows upward without assuming
       the shared card's height; both fleet grids keep their size. */
    --invite-top: auto;
    --invite-bottom: max(0px, calc(100% + 154px - 100dvh));
  }
}
@media (prefers-reduced-motion: reduce) {
  .bs-wrap {
    transition: none;
  }
}
