/* ==========================================================================
   Magnet — structure, layout and components.
   No colours live here; everything comes from the theme variables.
   ========================================================================== */

:root {
  --radius: 10px;
  --radius-lg: 16px;
  --gap: 16px;
  --board-gap: 5px;
  --dur: 140ms;
  --ease: cubic-bezier(0.22, 0.61, 0.36, 1);
}

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

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background-color: var(--bg);
  background-image: var(--bg-tint);
  background-attachment: fixed;
}

[hidden] { display: none !important; }

h1, h2, h3 { font-family: var(--font-display); font-weight: 650; margin: 0; letter-spacing: -0.01em; }

code, .mono { font-family: var(--font-mono); }

button, input, select { font: inherit; color: inherit; }

::selection { background: var(--accent-soft); }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ------------------------------------------------------------- top bar --- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--gap);
  padding: 14px clamp(16px, 4vw, 32px);
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 70%, transparent);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 20;
}

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

.brand-mark {
  width: 18px; height: 18px;
  border-radius: 50%;
  background: radial-gradient(circle at 32% 30%, var(--p1-hi), var(--p1) 55%, var(--p1-lo));
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.brand-name {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 0.02em;
}

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

/* ------------------------------------------------------------- screens --- */

.screen {
  max-width: 1140px;
  margin: 0 auto;
  padding: clamp(20px, 4vw, 36px) clamp(16px, 4vw, 32px) 56px;
}

.hero { max-width: 640px; margin-bottom: 28px; }
.hero h1 { font-size: clamp(26px, 4vw, 38px); line-height: 1.15; }
.hero-lede { margin: 10px 0 0; color: var(--text-dim); font-size: 16px; }

.home-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr);
  gap: var(--gap);
  align-items: start;
}

/* --------------------------------------------------------------- cards --- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px;
  box-shadow: var(--shadow-card);
}

.card + .card { margin-top: var(--gap); }

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
}

/* Controls in a card header sit on the right, and drop to their own line rather
   than spilling out of the card when the heading leaves them too little room. */
.card-head > .segmented,
.card-head > .pill {
  margin-left: auto;
}

.card-head h2 { font-size: 14px; text-transform: uppercase; letter-spacing: 0.09em; color: var(--text-dim); font-weight: 650; }

.pill {
  font-family: var(--font-mono);
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-dim);
}

/* -------------------------------------------------------------- inputs --- */

.field { display: block; margin-bottom: 14px; }
.field--inline { display: flex; align-items: center; gap: 8px; margin: 0; }

.field-label {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  margin-bottom: 6px;
}
.field--inline .field-label { margin: 0; }

.field-hint { margin: 6px 0 0; font-size: 12.5px; color: var(--text-faint); }

.control {
  width: 100%;
  padding: 9px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  transition: border-color var(--dur) var(--ease), background var(--dur) var(--ease);
}
.control:hover { border-color: var(--border-strong); }
.control:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.control::placeholder { color: var(--text-faint); }

select.control { width: auto; padding-right: 28px; cursor: pointer; }

.control--code {
  font-family: var(--font-mono);
  font-size: 22px;
  letter-spacing: 0.35em;
  text-transform: uppercase;
  text-align: center;
  padding: 10px 12px;
}

.seat-dot { width: 9px; height: 9px; border-radius: 50%; }
.seat-dot--p1 { background: var(--p1); }
.seat-dot--p2 { background: var(--p2); }

.switch {
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: 13.5px;
  color: var(--text-dim);
  margin-top: 10px;
  cursor: pointer;
  user-select: none;
}
.switch input { accent-color: var(--accent); width: 15px; height: 15px; cursor: pointer; }

.form-error {
  margin: 0 0 12px;
  font-size: 13px;
  color: var(--danger);
}

/* ------------------------------------------------------------- buttons --- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 9px 15px;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease), opacity var(--dur) var(--ease);
}
.btn:hover:not(:disabled) { background: var(--surface-3); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.42; cursor: not-allowed; }

.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.btn--primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }

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

.btn--block { width: 100%; }
.btn--small { padding: 6px 11px; font-size: 13px; }

.button-row { display: flex; gap: 8px; flex-wrap: wrap; }
.button-row .btn { flex: 1 1 auto; }

/* ---------------------------------------------------------------- tabs --- */

.tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  border-radius: var(--radius);
  background: var(--surface-2);
  margin-bottom: 18px;
}

.tab {
  flex: 1;
  padding: 8px 10px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--text-dim);
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
}
.tab:hover { color: var(--text); }
.tab.is-active { background: var(--surface); color: var(--text); box-shadow: var(--shadow-card); }

.segmented { display: flex; gap: 3px; padding: 3px; border-radius: 8px; background: var(--surface-2); }

.seg {
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text-faint);
  font-size: 11.5px;
  font-weight: 650;
  letter-spacing: 0.03em;
  padding: 4px 9px;
  white-space: nowrap;
  cursor: pointer;
}
.seg:hover { color: var(--text-dim); }
.seg.is-active { background: var(--surface); color: var(--text); }

/* ---------------------------------------------------------- game layout --- */

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: clamp(16px, 3vw, 28px);
  align-items: start;
}

.board-panel { display: flex; flex-direction: column; gap: 14px; min-width: 0; }

/* ------------------------------------------------------------- players --- */

.players {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 10px;
}

.player-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  min-width: 0;
  transition: border-color var(--dur) var(--ease), background var(--dur) var(--ease);
}
.player-chip[data-seat='2'] { flex-direction: row-reverse; text-align: right; }

.player-chip.is-turn { border-color: var(--accent); background: var(--surface-2); }
.player-chip.is-winner { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.player-chip.is-offline { opacity: 0.55; }

.chip-stone {
  flex: none;
  width: 22px; height: 22px;
  border-radius: 50%;
  box-shadow: var(--stone-shadow);
}
.chip-stone--p1 { background: radial-gradient(circle at 32% 28%, var(--p1-hi), var(--p1) 55%, var(--p1-lo)); }
.chip-stone--p2 { background: radial-gradient(circle at 32% 28%, var(--p2-hi), var(--p2) 55%, var(--p2-lo)); }

.chip-body { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.chip-name { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip-meta { font-size: 11.5px; color: var(--text-faint); }
.chip-meta strong { font-family: var(--font-mono); color: var(--text-dim); font-weight: 600; }

.chip-status { flex: none; width: 7px; height: 7px; border-radius: 50%; background: var(--text-faint); }
.player-chip.is-online .chip-status { background: var(--accent); }

.players-vs { font-size: 11px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--text-faint); }

/* --------------------------------------------------------------- board --- */

.board-frame {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) 18px;
  gap: 6px;
  align-self: center;
  width: 100%;
  max-width: 620px;
}

.ranks, .files {
  display: grid;
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--text-faint);
}
/* the extra padding lines the labels up with the cells inside the board's frame */
.ranks { grid-template-rows: repeat(6, 1fr); place-items: center; padding: calc(var(--board-gap) + 1px) 0; }
.files { grid-column: 2; grid-template-columns: repeat(6, 1fr); place-items: center; padding: 0 calc(var(--board-gap) + 1px); }

.board-stage { position: relative; }

.board {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: var(--board-gap);
  aspect-ratio: 1;
  padding: var(--board-gap);
  border-radius: var(--radius-lg);
  background: var(--board-bg);
  border: 1px solid var(--board-border);
  box-shadow: var(--board-shadow);
}

.cell {
  position: relative;
  appearance: none;
  border: 0;
  padding: 0;
  border-radius: 7px;
  background: var(--cell-dark);
  cursor: default;
  transition: background var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.cell--light { background: var(--cell-light); }

.cell--target { box-shadow: inset 0 0 0 1px var(--border-strong); }
.cell--target-1 { background-image: linear-gradient(var(--target-p1), var(--target-p1)); }
.cell--target-2 { background-image: linear-gradient(var(--target-p2), var(--target-p2)); }

/* the small corner glyph marking a goal square */
.cell--target::before {
  content: '';
  position: absolute;
  inset: 6px;
  border-radius: 4px;
  border: 1.5px dashed currentColor;
  opacity: 0.35;
  pointer-events: none;
}
.cell--target-1::before { color: var(--p1); }
.cell--target-2::before { color: var(--p2); }

.cell.is-interactive { cursor: pointer; }
.cell.is-interactive:hover { background-color: var(--cell-hover); }

.cell--last { box-shadow: inset 0 0 0 2px var(--ring-last); }
.cell--selected { box-shadow: inset 0 0 0 2px var(--ring-select); }

/* legal-destination marker */
.cell--move::after {
  content: '';
  position: absolute;
  left: 50%; top: 50%;
  width: 26%; height: 26%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: var(--hint);
  pointer-events: none;
}
.cell--move.cell--winning::after {
  width: 46%; height: 46%;
  background: transparent;
  border: 2.5px solid var(--hint-strong);
}
.cell--move:hover::after { background: var(--hint-strong); }

/* inspecting an opponent stone: show its reach, but muted and not clickable */
.board.is-inspect .cell--move::after { opacity: 0.4; }
.board.is-inspect .cell--move:hover::after { background: var(--hint); }

/* --------------------------------------------------------------- stone --- */

.stone {
  position: absolute;
  inset: 11%;
  border-radius: 50%;
  box-shadow: var(--stone-shadow);
  pointer-events: none;
}
.stone--1 { background: radial-gradient(circle at 32% 28%, var(--p1-hi), var(--p1) 52%, var(--p1-lo)); }
.stone--2 { background: radial-gradient(circle at 32% 28%, var(--p2-hi), var(--p2) 52%, var(--p2-lo)); }

.stone-count {
  position: absolute;
  right: -3px;
  bottom: -3px;
  min-width: 16px;
  height: 16px;
  padding: 0 3px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  color: var(--text-dim);
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 700;
  line-height: 14px;
  text-align: center;
}
.stone--frozen { opacity: 0.62; }
.stone--frozen .stone-count { color: var(--text-faint); }

.cell--selected .stone { box-shadow: var(--stone-shadow), 0 0 0 2px var(--ring-select); }

/* subtle arrival cue for the stone that just moved */
.stone--arrived { animation: arrive 160ms var(--ease); }
@keyframes arrive {
  from { transform: scale(0.9); opacity: 0.6; }
  to   { transform: scale(1); opacity: 1; }
}

/* the winning corner breathes a few times, then stops */
.cell--victory { animation: victory 1.1s var(--ease) 3; }
@keyframes victory {
  0%, 100% { box-shadow: inset 0 0 0 2px var(--hint-strong); }
  50%      { box-shadow: inset 0 0 0 5px var(--hint-strong); }
}

/* -------------------------------------------------------------- status --- */

.status {
  margin: 0;
  min-height: 22px;
  text-align: center;
  font-size: 14px;
  color: var(--text-dim);
}
.status strong { color: var(--text); font-weight: 650; }

/* ------------------------------------------------------------- overlay --- */

.board-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: var(--radius-lg);
  background: var(--overlay);
  backdrop-filter: blur(1px);
  z-index: 5;
}

.banner {
  text-align: center;
  padding: 22px 26px;
  border-radius: var(--radius-lg);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  box-shadow: var(--shadow-card);
  animation: rise 200ms var(--ease);
  max-width: 86%;
}
@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: none; }
}

.banner-kicker { margin: 0; font-size: 11px; letter-spacing: 0.16em; text-transform: uppercase; color: var(--text-faint); }
.banner-title { margin: 6px 0 0; font-family: var(--font-display); font-size: 24px; font-weight: 700; }
.banner-sub { margin: 6px 0 0; font-size: 13.5px; color: var(--text-dim); }
.banner-actions { display: flex; gap: 8px; justify-content: center; margin-top: 16px; flex-wrap: wrap; }

/* ------------------------------------------------------------- session --- */

.code-row { display: flex; align-items: center; gap: 10px; }

.session-code {
  flex: 1;
  font-family: var(--font-mono);
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 0.28em;
  text-align: center;
  padding: 8px 4px 8px 12px;
  border-radius: var(--radius);
  background: var(--surface-2);
  border: 1px dashed var(--border-strong);
}

/* ------------------------------------------------------------ move log --- */

.movelog {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 176px;
  overflow-y: auto;
  font-family: var(--font-mono);
  font-size: 12.5px;
}

.movelog li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px;
  border-radius: 6px;
  color: var(--text-dim);
}
.movelog li:nth-child(odd) { background: var(--surface-2); }
.movelog li:last-child { color: var(--text); }

.movelog .log-n { color: var(--text-faint); min-width: 20px; }
.movelog .log-dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.movelog .log-dot--1 { background: var(--p1); }
.movelog .log-dot--2 { background: var(--p2); }
.movelog .log-note { margin-left: auto; font-size: 11px; color: var(--text-faint); }

.empty {
  margin: 0;
  padding: 10px 0;
  font-size: 13px;
  color: var(--text-faint);
  text-align: center;
}

/* ---------------------------------------------------------- scoreboard --- */

.scoreboard table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.scoreboard th {
  text-align: left;
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint);
  font-weight: 600;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}
.scoreboard td { padding: 7px 0; border-bottom: 1px solid var(--border); }
.scoreboard tr:last-child td { border-bottom: 0; }
.scoreboard .num { text-align: right; font-family: var(--font-mono); width: 42px; }
.scoreboard .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 1px; }
.scoreboard .rank { color: var(--text-faint); font-family: var(--font-mono); width: 22px; }
.scoreboard .wins { color: var(--text); font-weight: 650; }
.scoreboard .rate { color: var(--text-dim); }

.scoreboard-foot {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}

.scoreboard-note {
  margin: 16px 0 6px;
  font-size: 11px;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-faint);
}

/* --------------------------------------------------------- game history --- */

.gamelist { list-style: none; margin: 0; padding: 0; }

.gamerow {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.gamerow:last-child { border-bottom: 0; }

.gamerow-result { font-size: 13.5px; }
.gamerow-result strong { font-weight: 650; }
.gamerow-beat { color: var(--text-faint); }
.gamerow-result > span:last-child { color: var(--text-dim); }

.gamerow-meta {
  display: flex;
  gap: 6px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-faint);
}
.gamerow-dot { opacity: 0.6; }

/* --------------------------------------------------------------- toast --- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--surface-3);
  border: 1px solid var(--border-strong);
  color: var(--text);
  font-size: 13.5px;
  box-shadow: var(--shadow-card);
  z-index: 60;
  animation: rise 160ms var(--ease);
}
.toast--error { border-color: var(--danger); color: var(--danger); }

/* -------------------------------------------------------------- dialog --- */

.dialog {
  max-width: 480px;
  width: calc(100% - 32px);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  padding: 24px;
  box-shadow: var(--shadow-card);
}
.dialog::backdrop { background: var(--overlay-strong); }
.dialog h2 { font-size: 19px; margin-bottom: 14px; }

.rules-list { margin: 0 0 16px; padding-left: 18px; }
.rules-list li { margin-bottom: 8px; color: var(--text-dim); }
.rules-list strong { color: var(--text); }
.rules-list code, .dialog-note code { font-size: 12.5px; padding: 1px 5px; border-radius: 4px; background: var(--surface-2); }

.dialog-note { font-size: 13px; color: var(--text-faint); margin: 0 0 18px; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }

/* ---------------------------------------------------------- responsive --- */

@media (max-width: 960px) {
  .game-layout { grid-template-columns: minmax(0, 1fr); }
  .home-grid { grid-template-columns: minmax(0, 1fr); }
  .sidebar { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: var(--gap); align-items: start; }
  .sidebar .card + .card { margin-top: 0; }
}

@media (max-width: 560px) {
  body { font-size: 14.5px; }
  .players { grid-template-columns: 1fr; }
  .players-vs { display: none; }
  .player-chip[data-seat='2'] { flex-direction: row; text-align: left; }
  .board-frame { grid-template-columns: 14px minmax(0, 1fr); }
  .brand-name { font-size: 16px; }

  /* the top bar has to survive 320px: drop the label, tighten everything */
  .topbar { padding: 12px 16px; gap: 10px; }
  .topbar-actions { gap: 8px; min-width: 0; }
  .topbar-actions .field-label { display: none; }
  .topbar-actions select.control { padding: 8px 24px 8px 10px; }
  .btn { padding: 8px 12px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
