:root {
  --ink: #2b241c;
  --ink-soft: #5a4e3d;
  --paper: #e9e0c9;
  --paper-deep: #d4c7a8;
  --line: rgba(120, 100, 70, 0.22);
  --margin: #c97878;
  --hit: #8b2e2e;
  --miss: #4a5a6a;
  --ship: #3a4a38;
  --accent: #3f5c3c;
  --danger: #7a2e2e;
  --shadow: rgba(60, 45, 25, 0.18);
  --font-display: "Caveat", "Segoe Print", cursive;
  --font-body: "Patrick Hand", "Segoe Print", cursive;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  height: 100%;
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 18px;
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--line) 1px, transparent 1px),
    linear-gradient(90deg, var(--line) 1px, transparent 1px),
    linear-gradient(90deg, transparent 47px, var(--margin) 47px, var(--margin) 49px, transparent 49px),
    radial-gradient(ellipse at 20% 0%, #f4ecda 0%, var(--paper) 45%, var(--paper-deep) 100%);
  background-size: 22px 22px, 22px 22px, 100% 100%, 100% 100%;
  background-attachment: fixed;
  -webkit-tap-highlight-color: transparent;
}

#app {
  min-height: 100dvh;
  max-width: 56rem;
  margin: 0 auto;
  padding: 1rem 1rem 2rem;
  position: relative;
}

.view { display: none; animation: pageSettle .45s ease-out; }
.view.active { display: block; }

@keyframes pageSettle {
  from { opacity: 0; transform: translateY(8px) rotate(-0.3deg); }
  to { opacity: 1; transform: none; }
}
@keyframes inkReveal {
  from { opacity: 0; filter: blur(2px); letter-spacing: .12em; }
  to { opacity: 1; filter: none; letter-spacing: .02em; }
}
@keyframes scribbleIn {
  from { opacity: 0; transform: scale(.6) rotate(-12deg); }
  to { opacity: 1; transform: none; }
}
@keyframes flashPop {
  0% { opacity: 0; transform: scale(.8); }
  20% { opacity: 1; transform: scale(1.05); }
  100% { opacity: 0; transform: scale(1.2); }
}

.ink-reveal { animation: inkReveal .7s ease-out; }
.page-settle { animation: pageSettle .5s ease-out; }

.hero {
  min-height: 42dvh;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding: 2rem 0 1.5rem;
}
.brand {
  font-family: var(--font-display);
  font-size: clamp(4.2rem, 18vw, 6.5rem);
  font-weight: 700;
  line-height: .95;
  margin: 0;
  text-shadow: 1px 2px 0 rgba(255,255,255,.35);
}
.tagline {
  margin: .6rem 0 0;
  font-size: 1.25rem;
  color: var(--ink-soft);
}

.home-actions {
  display: flex;
  flex-direction: column;
  gap: .75rem;
  margin-top: 1rem;
}
.join-row {
  display: grid;
  grid-template-columns: 1fr 1.2fr;
  gap: .5rem;
}
.join-row input {
  text-align: center;
  letter-spacing: .25em;
  text-transform: uppercase;
  font-size: 1.4rem;
}

.btn, input, select {
  font-family: inherit;
  font-size: 1.25rem;
  border: 2.5px solid var(--ink);
  border-radius: 3px;
  background: rgba(255,252,245,.72);
  color: var(--ink);
  padding: .9rem 1rem;
  box-shadow: 2px 3px 0 var(--shadow);
}
.btn {
  cursor: pointer;
  font-weight: 700;
  letter-spacing: .02em;
}
.btn:active { transform: translateY(2px); box-shadow: 1px 1px 0 var(--shadow); }
.btn-primary {
  background: var(--accent);
  color: #f4efe3;
  border-color: #243522;
  font-size: 1.45rem;
  padding: 1.05rem 1rem;
}
.btn-secondary { background: rgba(255,252,245,.85); }
.btn-ghost {
  background: transparent;
  box-shadow: none;
  border-style: dashed;
}
.btn-danger {
  background: var(--danger);
  color: #f6ece8;
  border-color: #3a1515;
}

.gear {
  position: fixed;
  top: .75rem;
  right: .75rem;
  width: 2.6rem;
  height: 2.6rem;
  border-radius: 50%;
  border: 2px solid var(--ink);
  background: rgba(255,252,245,.8);
  font-size: 1.2rem;
  cursor: pointer;
  z-index: 5;
}

.sub-hero h1 {
  font-family: var(--font-display);
  font-size: 2.8rem;
  margin: .5rem 0 .25rem;
}
.settings-form {
  display: flex;
  flex-direction: column;
  gap: .75rem;
  max-width: 22rem;
}
.settings-form label {
  display: flex;
  flex-direction: column;
  gap: .25rem;
}
.settings-form fieldset {
  border: 2px dashed var(--ink-soft);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .5rem;
  padding: .75rem;
}
.settings-form fieldset label {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
}
.settings-form fieldset input { width: 4.5rem; padding: .4rem; }
.err { color: var(--hit); min-height: 1.2em; }
.wake-hint { font-size: 1rem; color: var(--ink-soft); }

.game-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  margin-bottom: .5rem;
}
.linkish {
  background: none;
  border: none;
  font: inherit;
  color: var(--ink-soft);
  text-decoration: underline;
  cursor: pointer;
  padding: .25rem;
  box-shadow: none;
}
.room-badge {
  font-size: 1.2rem;
  border: 2px solid var(--ink);
  padding: .2rem .7rem;
  letter-spacing: .2em;
  background: rgba(255,255,255,.5);
}

.boards {
  display: grid;
  gap: 1.25rem;
}
@media (min-width: 720px) {
  .boards { grid-template-columns: 1fr 1fr; }
  #view-game.active {
    display: grid;
    grid-template-columns: 1fr 9rem;
    gap: 1rem;
    align-items: start;
  }
  #view-game.active .game-top { grid-column: 1 / -1; }
  #view-game.active .boards { grid-column: 1; }
  #view-game.active .fleet-panel { grid-column: 2; }
}

.board-label {
  font-family: var(--font-display);
  font-size: 1.8rem;
  margin: 0 0 .35rem;
  font-weight: 700;
}
.grid {
  display: grid;
  gap: 0;
  width: 100%;
  aspect-ratio: 1;
  border: 2.5px solid var(--ink);
  background:
    linear-gradient(var(--line) 1px, transparent 1px),
    linear-gradient(90deg, var(--line) 1px, transparent 1px),
    rgba(255,252,245,.55);
  background-size: calc(100% / var(--n, 15)) calc(100% / var(--n, 15)),
                   calc(100% / var(--n, 15)) calc(100% / var(--n, 15)), auto;
  box-shadow: 3px 4px 0 var(--shadow);
  touch-action: manipulation;
  user-select: none;
}
.cell {
  position: relative;
  border: none;
  background: transparent;
  padding: 0;
  cursor: crosshair;
  min-width: 0;
  min-height: 0;
}
.grid-own .cell { cursor: default; }
.cell.ship::after {
  content: "";
  position: absolute;
  inset: 18%;
  background: var(--ship);
  opacity: .55;
  border-radius: 2px;
  transform: rotate(-2deg);
}
.cell.miss::after {
  content: "·";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--miss);
  font-size: clamp(.6rem, 2.2vw, 1rem);
  animation: scribbleIn .25s ease-out;
}
.cell.hit::after,
.cell.sunk::after {
  content: "✕";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--hit);
  font-weight: 700;
  font-size: clamp(.7rem, 2.8vw, 1.15rem);
  transform: rotate(-8deg);
  animation: scribbleIn .28s ease-out;
}
.cell.sunk::after { color: #5a1a1a; text-decoration: underline; }
.cell.pending { outline: 2px dashed var(--accent); outline-offset: -2px; }

.fleet-panel {
  margin-top: 1rem;
  padding: .5rem .25rem;
}
.fleet-panel h2 {
  font-family: var(--font-display);
  font-size: 1.7rem;
  margin: 0 0 .4rem;
}
.fleet-panel ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: .45rem;
}
.fleet-panel li {
  border: 2px solid var(--ink);
  padding: .2rem .55rem;
  background: rgba(255,255,255,.45);
  font-size: 1.05rem;
}
.fleet-panel li.sunk {
  opacity: .45;
  text-decoration: line-through;
}
.turn-hint {
  margin: .75rem 0 0;
  color: var(--ink-soft);
  font-size: 1.15rem;
}

.shot-flash {
  position: fixed;
  left: 50%;
  top: 40%;
  transform: translate(-50%, -50%);
  font-family: var(--font-display);
  font-size: clamp(2.5rem, 12vw, 4rem);
  pointer-events: none;
  z-index: 20;
  animation: flashPop .9s ease-out forwards;
  text-shadow: 2px 2px 0 rgba(255,255,255,.5);
}
.shot-flash.hit { color: var(--hit); }
.shot-flash.miss { color: var(--miss); }
.shot-flash.sunk { color: var(--danger); }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(40, 32, 22, .35);
  display: none;
  place-items: center;
  z-index: 30;
  padding: 1rem;
}
/* Author display must not override the HTML hidden attribute (Firefox). */
.overlay:not([hidden]) {
  display: grid;
}
.overlay[hidden] {
  display: none !important;
}
.overlay-card {
  background: var(--paper);
  border: 3px solid var(--ink);
  padding: 1.5rem;
  width: min(20rem, 100%);
  text-align: center;
  box-shadow: 4px 6px 0 var(--shadow);
}
.end-title {
  font-family: var(--font-display);
  font-size: 2.6rem;
  margin: 0 0 1rem;
}
