:root {
  --bg: #0f1419;
  --bg-elevated: #1a2332;
  --bg-card: #1e2a3a;
  --border: #2d3a4d;
  --text: #e8eef7;
  --muted: #8b9bb4;
  --accent: #3d9cf0;
  --accent-hover: #5aaff5;
  --accent-soft: rgba(61, 156, 240, 0.15);
  --success: #3ecf8e;
  --danger: #f07178;
  --warn: #ffcc66;
  --light-sq: #e8edf5;
  --dark-sq: #6b8cbe;
  --light-sq-hover: #d4e0f0;
  --dark-sq-hover: #5a7aad;
  --select: #f0c14b;
  --select-ring: rgba(240, 193, 75, 0.55);
  --legal: rgba(62, 207, 142, 0.55);
  --last-move: rgba(240, 193, 75, 0.35);
  --check: rgba(240, 113, 120, 0.45);
  --radius: 14px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  --piece-font: "Segoe UI Symbol", "Apple Color Emoji", "Noto Sans Symbols2", "DejaVu Sans", serif;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

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

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100dvh;
}

.hidden {
  display: none !important;
}

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

.muted {
  color: var(--muted);
}

.small {
  font-size: 0.85rem;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 1.25rem;
  background: rgba(8, 12, 18, 0.82);
  backdrop-filter: blur(8px);
}

.modal-card {
  width: min(100%, 400px);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 4px);
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

.modal-brand {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 0.75rem;
}

.modal-card h1,
.modal-card h2 {
  margin: 0 0 0.5rem;
  font-size: 1.45rem;
  font-weight: 700;
}

.modal-card p {
  margin: 0 0 1.25rem;
  line-height: 1.45;
}

#name-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

input[type="text"] {
  width: 100%;
  padding: 0.85rem 1rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1.05rem;
  outline: none;
}

input[type="text"]:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* Buttons */
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  padding: 0.75rem 1.1rem;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.btn:active {
  transform: scale(0.98);
}

.btn.primary {
  background: var(--accent);
  border-color: transparent;
  color: #041018;
}

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

.btn.ghost {
  background: transparent;
}

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

/* App shell */
.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, #152030 0%, var(--bg) 100%);
  position: sticky;
  top: 0;
  z-index: 20;
}

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

.brand-icon {
  font-size: 1.75rem;
  line-height: 1;
}

.brand-title {
  font-weight: 800;
  letter-spacing: 0.02em;
}

.brand-sub {
  font-size: 0.75rem;
  color: var(--muted);
}

.topbar-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  justify-content: flex-end;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.65rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  white-space: nowrap;
}

.pill.offline {
  color: var(--warn);
}

.pill.online {
  color: var(--success);
}

.pill.player {
  color: var(--accent);
  max-width: 9rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.main {
  flex: 1;
  width: min(1100px, 100%);
  margin: 0 auto;
  padding: 1rem;
}

.lobby-bar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.lobby-bar h2 {
  margin: 0;
  font-size: 1.2rem;
}

.lobby-bar p {
  margin: 0.25rem 0 0;
}

/* Boards grid */
.boards {
  display: grid;
  gap: 1.25rem;
  grid-template-columns: 1fr;
}

@media (min-width: 900px) {
  .boards {
    grid-template-columns: 1fr 1fr;
  }
}

.game-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.2);
}

.game-card:focus-within,
.game-card.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), 0 8px 28px rgba(0, 0, 0, 0.28);
}

.game-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.75rem 0.9rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elevated);
}

.game-title {
  font-weight: 700;
  font-size: 0.95rem;
}

.game-meta {
  font-size: 0.78rem;
  color: var(--muted);
}

.turn-badge {
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0.25rem 0.55rem;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid rgba(61, 156, 240, 0.35);
}

.turn-badge.over {
  background: rgba(240, 113, 120, 0.15);
  color: var(--danger);
  border-color: rgba(240, 113, 120, 0.35);
}

.turn-badge.check {
  background: rgba(255, 204, 102, 0.15);
  color: var(--warn);
  border-color: rgba(255, 204, 102, 0.35);
}

.board-wrap {
  padding: 0.75rem;
}

.board {
  --sq: min(11.5vw, 52px);
  width: calc(var(--sq) * 8);
  max-width: 100%;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid #3a4a62;
  user-select: none;
  touch-action: manipulation;
  aspect-ratio: 1;
}

@media (min-width: 480px) {
  .board {
    --sq: min(10vw, 56px);
  }
}

@media (min-width: 900px) {
  .board {
    --sq: 48px;
  }
}

.sq {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  font-family: var(--piece-font);
  font-size: clamp(1.55rem, 5.5vw, 2.35rem);
  line-height: 1;
  cursor: pointer;
  border: none;
  padding: 0;
  color: #111;
  -webkit-user-select: none;
  user-select: none;
}

.sq.light {
  background: var(--light-sq);
}

.sq.dark {
  background: var(--dark-sq);
}

.sq.light:hover {
  background: var(--light-sq-hover);
}

.sq.dark:hover {
  background: var(--dark-sq-hover);
}

.sq.selected {
  box-shadow: inset 0 0 0 3px var(--select);
  background: var(--select) !important;
}

.sq.last-from,
.sq.last-to {
  box-shadow: inset 0 0 0 999px var(--last-move);
}

.sq.in-check {
  box-shadow: inset 0 0 0 999px var(--check);
}

.sq.legal::after {
  content: "";
  position: absolute;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: var(--legal);
  pointer-events: none;
}

.sq.legal.capture::after {
  width: 86%;
  height: 86%;
  background: transparent;
  border: 3px solid var(--legal);
  border-radius: 50%;
}

.sq .coord {
  position: absolute;
  font-size: 0.55rem;
  font-weight: 700;
  font-family: var(--font);
  opacity: 0.55;
  pointer-events: none;
  color: inherit;
  mix-blend-mode: multiply;
}

.sq .coord.file {
  right: 2px;
  bottom: 1px;
}

.sq .coord.rank {
  left: 2px;
  top: 1px;
}

.piece {
  pointer-events: none;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.25));
  font-weight: 400;
}

.piece.white {
  color: #fafafa;
  text-shadow:
    0 0 1px #222,
    0 1px 2px rgba(0, 0, 0, 0.55),
    1px 1px 0 #333,
    -1px -1px 0 #333,
    1px -1px 0 #333,
    -1px 1px 0 #333;
}

.piece.black {
  color: #1a1a1a;
  text-shadow:
    0 0 1px #fff,
    0 1px 1px rgba(255, 255, 255, 0.25);
}

.game-footer {
  display: grid;
  gap: 0.5rem;
  padding: 0 0.9rem 0.9rem;
}

/* Focus mode */
.focus-bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.85rem;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg-elevated);
}

.focus-title {
  font-weight: 700;
  font-size: 0.95rem;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app.focus-mode .boards {
  grid-template-columns: 1fr;
  max-width: 560px;
  margin: 0 auto;
}

.app.focus-mode .board {
  --sq: min(12vw, 64px);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.btn.sm {
  padding: 0.4rem 0.7rem;
  font-size: 0.82rem;
}

.turn-badge.replay {
  background: rgba(61, 156, 240, 0.12);
  color: var(--accent);
  border-color: rgba(61, 156, 240, 0.35);
}

.game-card.game-over {
  border-color: rgba(240, 113, 120, 0.45);
}

.game-over-banner {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.7rem 0.9rem;
  background: linear-gradient(90deg, rgba(240, 113, 120, 0.18), rgba(240, 113, 120, 0.06));
  border-bottom: 1px solid rgba(240, 113, 120, 0.35);
  color: var(--text);
  font-size: 0.9rem;
}

.game-over-banner strong {
  color: var(--danger);
  font-size: 1rem;
}

.replay-banner {
  padding: 0.5rem 0.9rem;
  background: var(--accent-soft);
  border-bottom: 1px solid rgba(61, 156, 240, 0.25);
  font-size: 0.85rem;
  color: var(--accent);
}

.linkish {
  appearance: none;
  border: none;
  background: none;
  color: inherit;
  font: inherit;
  font-weight: 700;
  text-decoration: underline;
  cursor: pointer;
  padding: 0;
}

.replay-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  padding: 0 0.75rem 0.35rem;
  flex-wrap: wrap;
}

.replay-label {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--muted);
  min-width: 5.5rem;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.board.replaying,
.board.finished {
  opacity: 0.98;
}

.board.finished .sq {
  cursor: default;
}

.move-log {
  max-height: 11rem;
  overflow: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.35rem;
  font-size: 0.8rem;
  line-height: 1.45;
}

.move-line {
  display: grid;
  grid-template-columns: 2.4rem 1fr auto;
  grid-template-areas:
    "ply san by"
    "ply when when";
  gap: 0.1rem 0.4rem;
  width: 100%;
  text-align: left;
  padding: 0.4rem 0.45rem;
  border: none;
  border-bottom: 1px solid rgba(45, 58, 77, 0.5);
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
  border-radius: 8px;
}

.move-line:hover {
  background: rgba(61, 156, 240, 0.08);
}

.move-line.active {
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 1px rgba(61, 156, 240, 0.35);
}

.move-line .ply {
  grid-area: ply;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.move-line .san {
  grid-area: san;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.move-line .by {
  grid-area: by;
  color: var(--muted);
  text-align: right;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 7rem;
}

.move-line .when {
  grid-area: when;
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 0.6rem;
  font-size: 0.72rem;
  color: var(--muted);
}

.move-line .when .abs {
  opacity: 0.85;
}

.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  background: var(--bg-elevated);
}

.empty-icon {
  font-size: 2.5rem;
  margin-bottom: 0.5rem;
}

.empty-state h3 {
  margin: 0 0 0.35rem;
}

.empty-state p {
  margin: 0 0 1rem;
}

.footer {
  text-align: center;
  padding: 1rem;
  border-top: 1px solid var(--border);
}

/* Global toast — stays inside main column */
.main > .toast {
  position: relative;
  z-index: 15;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  margin: 0 0 0.75rem;
  padding: 0.65rem 0.9rem;
  border-radius: 10px;
  background: #3a2a12;
  border: 1px solid rgba(255, 204, 102, 0.4);
  color: var(--warn);
  font-size: 0.9rem;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.toast.error {
  background: #3a1a1e;
  border-color: rgba(240, 113, 120, 0.4);
  color: var(--danger);
}

.toast.ok {
  background: #123528;
  border-color: rgba(62, 207, 142, 0.4);
  color: var(--success);
}

/* Per-board alert — clipped to card */
.card-alert {
  margin: 0;
  padding: 0.55rem 0.9rem;
  font-size: 0.85rem;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  overflow-wrap: anywhere;
  word-break: break-word;
}

.card-alert.error {
  background: #3a1a1e;
  color: var(--danger);
}

.card-alert.ok {
  background: #123528;
  color: var(--success);
}

.card-alert:not(.error):not(.ok) {
  background: #3a2a12;
  color: var(--warn);
}

/* Mode tabs */
.mode-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.35rem;
  margin-bottom: 0.85rem;
  padding: 0.3rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.mode-tab {
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 700;
  font-size: 0.88rem;
  padding: 0.65rem 0.4rem;
  border-radius: 10px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
}

.mode-tab:hover {
  color: var(--text);
  background: rgba(255, 255, 255, 0.04);
}

.mode-tab.active {
  color: var(--text);
  background: var(--bg-card);
  border-color: var(--border);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

.tab-count {
  font-size: 0.72rem;
  font-weight: 700;
  min-width: 1.4rem;
  padding: 0.1rem 0.4rem;
  border-radius: 999px;
  background: var(--bg);
  color: var(--muted);
  border: 1px solid var(--border);
}

.mode-tab.active .tab-count {
  color: var(--accent);
  border-color: rgba(61, 156, 240, 0.35);
  background: var(--accent-soft);
}

.mode-panel {
  display: none;
}

.mode-panel.active {
  display: block;
}

.panel-blurb {
  margin: 0 0 0.75rem;
}

.mode-tabs.hidden,
#lobby-panels.hidden {
  display: none !important;
}

/* Stronger last-move / replay highlights */
.sq.last-from {
  box-shadow: inset 0 0 0 999px rgba(240, 193, 75, 0.42) !important;
}

.sq.last-to {
  box-shadow: inset 0 0 0 999px rgba(61, 156, 240, 0.45) !important;
}

.sq-tag {
  position: absolute;
  top: 2px;
  left: 2px;
  z-index: 2;
  font-family: var(--font);
  font-size: 0.55rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  padding: 0 3px;
  border-radius: 3px;
  line-height: 1.2;
  pointer-events: none;
}

.sq-tag.from-tag {
  background: #f0c14b;
  color: #1a1200;
}

.sq-tag.to-tag {
  background: #3d9cf0;
  color: #041018;
}

.move-callout {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.35rem 0.55rem;
  margin-top: 0.55rem;
  padding: 0.45rem 0.65rem;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  font-weight: 600;
}

.move-callout-san {
  color: var(--accent);
  font-weight: 800;
}

.move-callout-arrow {
  opacity: 0.7;
}

.move-line .uci {
  font-weight: 600;
  font-size: 0.75em;
}

.move-line .ply.white-side {
  color: #dce8f8;
}

.move-line .ply.black-side {
  color: #9fb0c9;
}

.game-card {
  overflow: hidden;
  max-width: 100%;
}

/* Promotion */
.promo-card {
  text-align: center;
}

.promo-choices {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
  margin: 1rem 0;
}

.promo-btn {
  font-family: var(--piece-font);
  font-size: 2.4rem;
  padding: 0.5rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--light-sq);
  color: #111;
  cursor: pointer;
}

.promo-btn:hover {
  border-color: var(--accent);
  background: #fff;
}

/* Lobby sections */
.lobby-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  justify-content: flex-end;
}

.section-empty {
  margin: 0.25rem 0 0;
  padding: 0.75rem;
  border: 1px dashed var(--border);
  border-radius: 10px;
}

.section-empty.hidden {
  display: none;
}

.mode-banner {
  padding: 0.7rem 0.9rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.88rem;
  display: grid;
  gap: 0.45rem;
}

.mode-banner.vote-mode {
  background: linear-gradient(90deg, rgba(167, 139, 250, 0.14), transparent);
}

.mode-banner.bots-mode {
  background: linear-gradient(90deg, rgba(62, 207, 142, 0.12), transparent);
}

.seat-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.seat-chip {
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
}

.seat-chip.me {
  border-color: var(--accent);
  color: var(--accent);
}

.seat-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.vote-tally {
  display: grid;
  gap: 0.4rem;
}

.vote-row {
  display: grid;
  gap: 0.15rem;
}

.vote-bar {
  height: 6px;
  background: var(--bg);
  border-radius: 999px;
  overflow: hidden;
}

.vote-bar span {
  display: block;
  height: 100%;
  background: #a78bfa;
  border-radius: 999px;
}

.turn-badge.vote {
  background: rgba(167, 139, 250, 0.15);
  color: #c4b5fd;
  border-color: rgba(167, 139, 250, 0.4);
}

.turn-badge.bots {
  background: rgba(62, 207, 142, 0.12);
  color: var(--success);
  border-color: rgba(62, 207, 142, 0.35);
}

.sq.vote-hot {
  box-shadow: inset 0 0 0 999px rgba(167, 139, 250, 0.28);
}

.board.spectate .sq {
  cursor: default;
}

.field-label {
  display: block;
  margin: 0.75rem 0 0.35rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}

.select {
  width: 100%;
  padding: 0.7rem 0.85rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

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

.focus-boards {
  max-width: 560px;
  margin: 0 auto;
}

#lobby-sections.hidden {
  display: none;
}
