/*
 * Theme tokens.
 *
 * The brand palette (https://coolors.co/palette/780000-c1121f-fdf0d5-003049-669bbc):
 *   #780000  barn red       — deep accent / hover
 *   #C1121F  fire engine    — primary accent (CTAs, highlights)
 *   #FDF0D5  papaya whip    — cream paper background (light mode)
 *   #003049  prussian blue  — surface/dark color (header, modals)
 *   #669BBC  sky blue       — soft accent / Player 1 line in dark mode
 *
 * The legacy variable names (--navy, --gold, --paper, …) are kept so the
 * rest of the CSS doesn't need touching; their *values* now come from the
 * new palette, and [data-theme="dark"] overrides them for dark mode.
 *
 * --surface is new: it represents "card-style elevated background".
 * In light mode it's white; in dark mode it's a slightly elevated dark
 * panel. Anywhere that previously hard-coded `background: white` should
 * use `var(--surface)` so cards flip cleanly with the theme.
 */
:root {
  --navy: #003049;            /* prussian — primary dark surface */
  --navy-2: #001f30;          /* slightly darker for hovers on dark */
  --gold: #C1121F;            /* fire red — primary accent (still named "gold" for legacy reasons) */
  --gold-soft: #ec5763;       /* lighter red for hover states on dark */
  --crimson: #780000;         /* barn red — deep accent / error */
  --teal: #669BBC;            /* sky blue — soft accent */
  --paper: #FDF0D5;           /* papaya whip — page background */
  --paper-2: #efe6c5;         /* slightly darker cream — modal footer / striped surfaces */
  --ink: #14181f;             /* near-black — text on light */
  --ink-soft: #4a5468;        /* muted text on light */
  --line: #d8c8a4;            /* parchment border on light */
  --shadow: 0 8px 28px rgba(0, 48, 73, 0.14);
  --radius: 12px;
  --radius-lg: 18px;

  --surface: #ffffff;                       /* card / board / modal body / input bg */
  --surface-tint: rgba(0, 48, 73, 0.04);    /* extremely soft tint for hover overlays on light surfaces */
  --backdrop: rgba(0, 48, 73, 0.55);        /* modal backdrop */
  --on-dark-hover: rgba(253, 240, 213, 0.08); /* hover overlay on dark surfaces */
  --modal-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
  --edge-undrawn: rgba(0, 48, 73, 0.10);
  --board-radial-1: rgba(193, 18, 31, 0.04);
  --board-radial-2: rgba(0, 48, 73, 0.06);

  --player1: #003049;         /* prussian — Player 1 line */
  --player1-soft: #d8e3eb;    /* very pale blue — Player 1 box fill */
  --player2: #C1121F;         /* fire red — Player 2 line */
  --player2-soft: #f4d6d8;    /* very pale pink — Player 2 box fill */
}

[data-theme="dark"] {
  --navy: #00131c;            /* deeper than prussian — page bg */
  --navy-2: #00080f;
  --gold: #C1121F;            /* keep fire red for primary accent */
  --gold-soft: #e85a6c;       /* lighter for visibility on dark */
  --crimson: #ff6b78;         /* error needs to be visible on dark — push brighter */
  --teal: #669BBC;            /* sky stays */
  --paper: #00131c;           /* repurposed: in dark mode, "paper" IS the dark page bg */
  --paper-2: #002235;         /* elevated dark surface — modal footer */
  --ink: #FDF0D5;             /* cream text */
  --ink-soft: rgba(253, 240, 213, 0.65);
  --line: rgba(102, 155, 188, 0.20);
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.55);

  --surface: #002738;
  --surface-tint: rgba(102, 155, 188, 0.07);
  --backdrop: rgba(0, 8, 16, 0.72);
  --on-dark-hover: rgba(253, 240, 213, 0.10);
  --modal-shadow: 0 20px 50px rgba(0, 0, 0, 0.7);
  --edge-undrawn: rgba(253, 240, 213, 0.10);
  --board-radial-1: rgba(193, 18, 31, 0.06);
  --board-radial-2: rgba(102, 155, 188, 0.05);

  --player1: #669BBC;
  --player1-soft: rgba(102, 155, 188, 0.22);
  --player2: #e85a6c;
  --player2-soft: rgba(232, 90, 108, 0.20);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  /* Ensure browser chrome (mobile address bar, scroll bounce) matches the theme */
  color-scheme: light dark;
}

body {
  display: flex;
  flex-direction: column;
  background-image:
    radial-gradient(circle at 20% 0%, var(--board-radial-1), transparent 40%),
    radial-gradient(circle at 80% 100%, var(--board-radial-2), transparent 40%);
  transition: background-color 0.2s ease, color 0.2s ease;
}

/* ---------- Header ---------- */

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  background: var(--navy);
  color: var(--paper);
  border-bottom: 3px solid var(--teal);
}

[data-theme="dark"] .site-header {
  /* In dark mode keep the header visible vs the body bg with a touch of depth */
  background: var(--surface);
  border-bottom-color: var(--gold);
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.logo {
  width: 96px;
  height: 96px;
  flex-shrink: 0;
}

.logo svg {
  width: 100%;
  height: 100%;
  display: block;
}

/* Themable SVG logo (fills come from CSS so they swap with the theme).
   The crest is laid out like a single Dot Game box: corner dots + edges
   in the two player colors, with the WDL monogram inside. */
.logo .crest-bg    { fill: var(--navy); }
.logo .crest-edge  { stroke-width: 4; stroke-linecap: round; fill: none; }
.logo .crest-edge-top,
.logo .crest-edge-right  { stroke: #C1121F; }
.logo .crest-edge-bottom,
.logo .crest-edge-left   { stroke: #669BBC; }
.logo .crest-dot   { fill: #FDF0D5; }
.logo .crest-text  { fill: var(--gold); }

[data-theme="dark"] .logo .crest-bg { fill: var(--paper-2); }

.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.brand-title {
  font-family: 'Playfair Display', Georgia, serif;
  font-weight: 900;
  font-size: 1.4rem;
  letter-spacing: 0.02em;
  line-height: 1.1;
  color: #FDF0D5;             /* always cream on dark header */
}

[data-theme="dark"] .brand-title { color: var(--ink); }

.brand-tag {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  color: var(--teal);
  margin-top: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}

/* ---------- Buttons ---------- */

.primary-btn,
.secondary-btn,
.ghost-btn {
  font-family: inherit;
  font-weight: 600;
  font-size: 0.95rem;
  letter-spacing: 0.02em;
  border-radius: 999px;
  padding: 10px 20px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: transform 0.06s ease, background 0.2s ease, color 0.2s ease, border-color 0.2s ease;
  white-space: nowrap;
}

.primary-btn {
  background: var(--gold);
  color: #FDF0D5;             /* white-on-red looks better than dark-on-red here */
  border-color: var(--gold);
}

.primary-btn:hover {
  background: var(--crimson);
  border-color: var(--crimson);
}

.secondary-btn {
  background: var(--navy);
  color: #FDF0D5;
  border-color: var(--navy);
}

[data-theme="dark"] .secondary-btn {
  background: var(--surface);
  color: var(--ink);
  border-color: var(--teal);
}

.secondary-btn:hover {
  background: var(--navy-2);
}

[data-theme="dark"] .secondary-btn:hover {
  background: var(--paper-2);
}

.ghost-btn {
  background: transparent;
  color: inherit;
  border-color: currentColor;
  opacity: 0.8;
}

.ghost-btn:hover {
  opacity: 1;
  background: var(--surface-tint);
}

.site-header .ghost-btn {
  color: #FDF0D5;
}

.site-header .ghost-btn:hover {
  background: var(--on-dark-hover);
}

[data-theme="dark"] .site-header .ghost-btn {
  color: var(--ink);
}

[data-theme="dark"] .site-header .ghost-btn:hover {
  background: var(--surface-tint);
}

.primary-btn:active,
.secondary-btn:active,
.ghost-btn:active,
.icon-btn:active {
  transform: translateY(1px);
}

button:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: 2px;
}

/* Icon-only round button used by the theme toggle */
.icon-btn {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  color: inherit;
  border: 2px solid currentColor;
  cursor: pointer;
  opacity: 0.85;
  transition: opacity 0.15s ease, background 0.2s ease, transform 0.06s ease;
}

.icon-btn:hover {
  opacity: 1;
  background: var(--on-dark-hover);
}

[data-theme="dark"] .site-header .icon-btn:hover {
  background: var(--surface-tint);
}

.icon-btn svg {
  width: 18px;
  height: 18px;
  display: block;
}

/* Show sun in dark mode (click to go light), moon in light mode (click to go dark).
   Selectors include .icon-btn so they outweigh `.icon-btn svg { display: block }`. */
.icon-btn .theme-icon-sun  { display: none; }
.icon-btn .theme-icon-moon { display: block; }
[data-theme="dark"] .icon-btn .theme-icon-sun  { display: block; }
[data-theme="dark"] .icon-btn .theme-icon-moon { display: none; }

/* ---------- Match stage ---------- */

.match-stage {
  flex: 1 1 auto;
  width: 100%;
  max-width: 1080px;
  margin: 0 auto;
  padding: 24px 20px 32px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

/* ---------- Scoreboard ---------- */

.scoreboard {
  display: grid;
  grid-template-columns: 1fr minmax(200px, 1.2fr) 1fr;
  gap: 12px;
  align-items: stretch;
}

.score-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 8px;
  border-top: 4px solid var(--player1);
}

.score-card[data-seat='2'] {
  border-top-color: var(--player2);
}

.score-card.is-active {
  outline: 3px solid var(--gold);
  outline-offset: -3px;
}

.score-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.player-label {
  font-size: 0.7rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.player-seat-badge {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  background: var(--player1);
  color: #FDF0D5;
  border-radius: 999px;
  padding: 2px 8px;
}

.score-card[data-seat='2'] .player-seat-badge {
  background: var(--player2);
}

.player-name {
  font-family: 'Playfair Display', Georgia, serif;
  font-weight: 700;
  font-size: 1.2rem;
  color: var(--ink);
  line-height: 1.1;
  word-break: break-word;
}

.player-score {
  font-family: 'Playfair Display', Georgia, serif;
  font-weight: 900;
  font-size: 2.4rem;
  color: var(--ink);
  line-height: 1;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.match-meta {
  background: var(--navy);
  color: #FDF0D5;
  border-radius: var(--radius);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-align: center;
  box-shadow: var(--shadow);
  border: 1px solid var(--navy-2);
}

[data-theme="dark"] .match-meta {
  background: var(--surface);
  color: var(--ink);
  border-color: var(--line);
}

.meta-status {
  font-size: 0.72rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--teal);
}

.meta-divider {
  width: 36px;
  height: 2px;
  background: var(--gold);
  border-radius: 2px;
  margin: 2px auto;
}

.meta-turn {
  font-family: 'Playfair Display', Georgia, serif;
  font-weight: 700;
  font-size: 1.15rem;
  color: inherit;
}

.meta-code-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
}

.meta-code-label {
  font-size: 0.7rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--teal);
}

.meta-code {
  font-family: 'Playfair Display', Georgia, serif;
  font-weight: 900;
  font-size: 1.25rem;
  letter-spacing: 0.2em;
  color: var(--gold);
}

.copy-btn {
  background: transparent;
  color: var(--teal);
  border: 1px solid var(--gold);
  border-radius: 6px;
  font-size: 0.72rem;
  padding: 3px 8px;
  cursor: pointer;
  font-family: inherit;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  transition: background 0.2s ease, color 0.2s ease;
}

.copy-btn:hover {
  background: var(--gold);
  color: #FDF0D5;
}

/* ---------- Board ---------- */

.board-frame {
  background: var(--surface);
  border-radius: var(--radius-lg);
  border: 1px solid var(--line);
  padding: 16px;
  box-shadow: var(--shadow);
  position: relative;
}

.board-stamp {
  position: absolute;
  top: 12px;
  right: 16px;
  font-size: 0.65rem;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  color: var(--teal);
  border: 1.5px solid var(--teal);
  padding: 3px 9px;
  border-radius: 4px;
  background: transparent;
  pointer-events: none;
}

.board-wrap {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

#board {
  width: 100%;
  height: auto;
  max-height: min(62vh, 600px);
  touch-action: manipulation;
  user-select: none;
}

#board .grid-dot {
  fill: var(--navy);
}

[data-theme="dark"] #board .grid-dot { fill: var(--ink); }

#board .edge-hit {
  fill: transparent;
  cursor: pointer;
}

#board .edge {
  stroke-linecap: round;
  pointer-events: none;
  transition: stroke 0.15s ease, opacity 0.15s ease;
}

#board .edge-undrawn {
  stroke: var(--edge-undrawn);
}

#board .edge-undrawn.is-hover-p1 {
  stroke: var(--player1);
  opacity: 0.6;
}

#board .edge-undrawn.is-hover-p2 {
  stroke: var(--player2);
  opacity: 0.6;
}

#board .edge-p1 { stroke: var(--player1); }
#board .edge-p2 { stroke: var(--player2); }

#board .box-fill {
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease;
}

#board .box-fill.is-claimed {
  opacity: 1;
}

#board .box-fill-p1 { fill: var(--player1-soft); }
#board .box-fill-p2 { fill: var(--player2-soft); }

#board .box-initial {
  font-family: 'Playfair Display', Georgia, serif;
  font-weight: 900;
  text-anchor: middle;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease;
}

#board .box-initial.is-claimed { opacity: 1; }
#board .box-initial.box-initial-p1 { fill: var(--player1); }
#board .box-initial.box-initial-p2 { fill: var(--player2); }

/* ---------- Controls ---------- */

.match-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
}

/* ---------- Footer ---------- */

.site-footer {
  text-align: center;
  padding: 16px;
  color: var(--ink-soft);
  font-size: 0.78rem;
  letter-spacing: 0.04em;
  border-top: 1px solid var(--line);
  margin-top: auto;
}

.site-footer a {
  color: var(--teal);
}

/* ---------- Modals ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: var(--backdrop);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
  animation: fade-in 0.15s ease-out;
}

.modal-backdrop[hidden] {
  display: none;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius-lg);
  width: min(100%, 460px);
  box-shadow: var(--modal-shadow);
  border: 1px solid var(--line);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  max-height: calc(100dvh - 32px);
  animation: pop-in 0.18s ease-out;
  color: var(--ink);
}

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  background: var(--navy);
  color: #FDF0D5;
  border-bottom: 3px solid var(--gold);
}

[data-theme="dark"] .modal-header {
  background: var(--paper-2);
  color: var(--ink);
}

.modal-header h2 {
  margin: 0;
  font-family: 'Playfair Display', Georgia, serif;
  font-weight: 700;
  font-size: 1.3rem;
}

.close-btn {
  background: transparent;
  color: #FDF0D5;
  border: none;
  font-size: 1.6rem;
  line-height: 1;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

[data-theme="dark"] .modal-header .close-btn { color: var(--ink); }

.close-btn:hover {
  background: var(--on-dark-hover);
}

[data-theme="dark"] .modal-header .close-btn:hover {
  background: var(--surface-tint);
}

.modal-body {
  padding: 18px;
  overflow-y: auto;
  background: var(--surface);
  color: var(--ink);
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 14px 18px;
  border-top: 1px solid var(--line);
  background: var(--paper-2);
}

.modal-footer .ghost-btn {
  color: var(--ink-soft);
}

/* ---------- Form fields ---------- */

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
  flex: 1;
}

.field-label {
  font-size: 0.72rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.field input,
.field select {
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  font: inherit;
  font-size: 1rem;
  background: var(--surface);
  color: var(--ink);
  width: 100%;
}

.field-row {
  display: flex;
  gap: 12px;
}

.hint {
  margin: 4px 0 0;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.hint.error {
  color: var(--crimson);
}

/* ---------- Invite specific ---------- */

.invite-body {
  text-align: center;
}

.invite-code-display {
  font-family: 'Playfair Display', Georgia, serif;
  font-weight: 900;
  font-size: 2.6rem;
  letter-spacing: 0.3em;
  color: var(--ink);
  background: var(--surface);
  border: 2px dashed var(--gold);
  border-radius: var(--radius);
  padding: 16px;
  margin: 16px 0 12px;
  user-select: all;
}

[data-theme="dark"] .invite-code-display { background: var(--paper-2); }

.invite-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  margin-bottom: 6px;
}

#joinCodeInput {
  text-transform: uppercase;
  letter-spacing: 0.3em;
  font-family: 'Playfair Display', Georgia, serif;
  font-weight: 900;
  font-size: 1.4rem;
  text-align: center;
}

/* ---------- Game over modal ---------- */

.modal-trophy .modal-body {
  text-align: center;
}

.trophy {
  font-size: 4rem;
  margin-bottom: 8px;
}

.winner-line {
  font-family: 'Playfair Display', Georgia, serif;
  font-weight: 900;
  font-size: 1.6rem;
  color: var(--ink);
}

.final-scoreline {
  margin-top: 8px;
  font-size: 1rem;
  color: var(--ink-soft);
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--navy);
  color: #FDF0D5;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.9rem;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.25s ease, opacity 0.25s ease;
  z-index: 200;
  border: 1px solid var(--gold);
}

[data-theme="dark"] .toast {
  background: var(--paper-2);
  color: var(--ink);
}

.toast.is-visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ---------- Responsive ---------- */

@media (max-width: 720px) {
  .site-header { padding: 10px 14px; }
  .brand-title { font-size: 1.05rem; }
  .brand-tag { font-size: 0.65rem; letter-spacing: 0.12em; }
  .logo { width: 64px; height: 64px; }
  .header-actions .ghost-btn { padding: 8px 12px; font-size: 0.85rem; }
  .header-actions .icon-btn { width: 36px; height: 36px; }
  .header-actions .icon-btn svg { width: 16px; height: 16px; }

  .match-stage { padding: 14px 12px 24px; gap: 14px; }

  .scoreboard {
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      'meta meta'
      'p1 p2';
    gap: 8px;
  }
  .match-meta { grid-area: meta; padding: 10px 12px; }
  #scoreCard1 { grid-area: p1; }
  #scoreCard2 { grid-area: p2; }
  .score-card { padding: 10px 12px; }
  .player-name { font-size: 1rem; }
  .player-score { font-size: 1.9rem; }
  .meta-turn { font-size: 1rem; }

  .board-frame { padding: 12px 8px 10px; }
  .board-stamp { display: none; }

  .match-controls .primary-btn,
  .match-controls .secondary-btn { flex: 1 1 calc(50% - 5px); padding: 12px 14px; font-size: 0.9rem; }

  .invite-code-display { font-size: 1.9rem; padding: 12px; }
  #joinCodeInput { font-size: 1.2rem; }
}

@media (max-width: 380px) {
  .match-controls .primary-btn,
  .match-controls .secondary-btn { flex-basis: 100%; }
}
