:root {
  /* Classic black & white board theme */
  --felt: #2a2a2e;
  --felt-deep: #121214;
  --wood: #1a1a1c;
  --wood-light: #3a3a40;
  --wood-edge: #0a0a0b;
  --cream: #f5f0e6;
  --ink: #121214;
  --muted: #6b6b72;
  --panel: rgba(250, 250, 252, 0.94);
  --line: rgba(18, 18, 20, 0.12);
  --light-sq: #f3eee4;
  --light-sq-2: #e8e0d2;
  --dark-sq: #2c2c32;
  --dark-sq-2: #1a1a1e;
  /* Pieces: white (game "red") and black */
  --white-piece: #f7f7f5;
  --white-dark: #c8c4bc;
  --black-piece: #3a3a3e;
  --black-hi: #4a4a50;
  --accent: #e8e8ea;
  --accent-soft: rgba(255, 255, 255, 0.18);
  --gold: #d0d0d4;
  --board-frame: #2a2a2e;
  --theme-bg: #0a0a0b;
  --gold-soft: rgba(200, 200, 210, 0.28);
  --danger: #b42318;
  --shadow: 0 24px 60px rgba(0, 0, 0, 0.55);
  --radius: 18px;
  --font: "DM Sans", system-ui, sans-serif;
  --display: "Playfair Display", Georgia, serif;
}

/* Built-in themes. Game color "red" is presented as White. */
html[data-theme="classic"] {
  --light-sq: #f3eee4;
  --light-sq-2: #e8e0d2;
  --dark-sq: #2c2c32;
  --dark-sq-2: #1a1a1e;
  --white-piece: #f7f7f5;
  --white-dark: #c8c4bc;
  --black-piece: #3a3a3e;
  --black-hi: #8a8a92;
  --board-frame: #2a2a2e;
  --accent: #e8e8ea;
  --gold: #d0d0d4;
  --theme-bg: #0a0a0b;
}

html[data-theme="wood"] {
  --light-sq: #e6c99a;
  --light-sq-2: #cfa96f;
  --dark-sq: #8b3f2f;
  --dark-sq-2: #61271e;
  --white-piece: #f3e6c8;
  --white-dark: #cbb485;
  --black-piece: #3c211a;
  --black-hi: #785044;
  --board-frame: #5a311e;
  --accent: #d49a55;
  --gold: #e0a84f;
  --theme-bg: #21140f;
}

html[data-theme="emerald"] {
  --light-sq: #dce8d3;
  --light-sq-2: #bdd0af;
  --dark-sq: #176b50;
  --dark-sq-2: #0c4937;
  --white-piece: #fffdf1;
  --white-dark: #d8d2b5;
  --black-piece: #102b25;
  --black-hi: #315a50;
  --board-frame: #124c3a;
  --accent: #5fd0a5;
  --gold: #e3c55b;
  --theme-bg: #061d18;
}

html[data-theme="midnight"] {
  --light-sq: #aabbd5;
  --light-sq-2: #8096b9;
  --dark-sq: #192c51;
  --dark-sq-2: #0e1c38;
  --white-piece: #edf4ff;
  --white-dark: #aebed8;
  --black-piece: #3a3a3e;
  --black-hi: #9aa8c8;
  --board-frame: #101e3d;
  --accent: #7da8ef;
  --gold: #8eb8ff;
  --theme-bg: #050a17;
}

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

html, body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--theme-bg);
  min-height: 100dvh;
}

.ambient {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(900px 500px at 15% 10%, rgba(255, 255, 255, 0.06), transparent 55%),
    radial-gradient(700px 500px at 90% 80%, rgba(80, 80, 90, 0.2), transparent 50%),
    linear-gradient(160deg, color-mix(in srgb, var(--theme-bg), white 7%) 0%, var(--theme-bg) 48%, color-mix(in srgb, var(--theme-bg), var(--accent) 12%) 100%);
  pointer-events: none;
  z-index: 0;
}

.view {
  position: relative;
  z-index: 1;
  min-height: 100dvh;
}

.hidden { display: none !important; }

/* —— Auth —— */
#view-auth {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.auth-card {
  width: min(420px, 100%);
  background: var(--panel);
  backdrop-filter: blur(16px);
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 24px;
  box-shadow: var(--shadow);
  padding: 2rem 1.75rem 1.5rem;
}

.logo {
  display: flex;
  gap: 0.9rem;
  align-items: center;
  margin-bottom: 1.5rem;
}

.logo h1 {
  margin: 0;
  font-family: var(--display);
  font-size: 1.55rem;
  letter-spacing: -0.02em;
}

.logo .sub {
  margin: 0.15rem 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.logo-mark {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background:
    linear-gradient(135deg, #f5f0e6 0% 50%, #1c1c1e 50% 100%);
  box-shadow:
    inset 0 0 0 2px rgba(255, 255, 255, 0.12),
    0 8px 20px rgba(0, 0, 0, 0.35);
  flex-shrink: 0;
}

.logo-mark.sm {
  width: 28px;
  height: 28px;
  border-radius: 8px;
}

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.35rem;
  background: rgba(18, 18, 20, 0.08);
  padding: 0.3rem;
  border-radius: 999px;
  margin-bottom: 1.25rem;
}

.tab {
  border: 0;
  background: transparent;
  padding: 0.55rem;
  border-radius: 999px;
  font: inherit;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}

.tab.active {
  background: #fff;
  color: var(--ink);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.auth-form label {
  display: block;
  margin-bottom: 0.9rem;
}

.auth-form label span {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

.auth-form input,
#join-code {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 12px;
  padding: 0.75rem 0.9rem;
  font: inherit;
  font-size: 1rem;
  color: var(--ink);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.auth-form input:focus,
#join-code:focus {
  border-color: #888;
  box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.08);
}

.btn {
  appearance: none;
  border: 0;
  border-radius: 999px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  padding: 0.75rem 1.2rem;
  transition: transform 0.12s ease, filter 0.12s ease, background 0.12s;
}

.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn.primary {
  background: linear-gradient(180deg, color-mix(in srgb, var(--board-frame), white 16%), var(--board-frame));
  color: #f5f5f7;
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.35);
}

.btn.primary:hover:not(:disabled) { filter: brightness(1.12); }

.btn.secondary {
  background: linear-gradient(180deg, #e8e4dc, #cfc8bc);
  color: #1a1a1e;
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.2);
}

.btn.secondary:hover:not(:disabled) { filter: brightness(1.04); }

.btn.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
}

.btn.sm { padding: 0.4rem 0.75rem; font-size: 0.85rem; }
.btn.wide { width: 100%; }

.error {
  margin: 0.75rem 0 0;
  color: var(--danger);
  font-size: 0.9rem;
  font-weight: 500;
}

.fine {
  margin: 1rem 0 0.5rem;
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.4;
}

.home-link {
  display: inline-block;
  margin-top: 0.5rem;
  color: #3a3a40;
  font-size: 0.85rem;
  font-weight: 600;
  text-decoration: none;
}

/* —— Lobby / topbar —— */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.25rem;
  color: #f0f0f2;
}

.brand-mini {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.brand-mini strong { font-size: 1.05rem; }

.user-chip {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  font-weight: 500;
}

.user-chip .btn.ghost {
  color: #d8d8dc;
  border-color: rgba(255, 255, 255, 0.18);
}

.lobby-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
  padding: 0.5rem 1.25rem 2rem;
  max-width: 1100px;
  margin: 0 auto;
}

.panel {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 1.35rem 1.25rem;
  border: 1px solid rgba(255, 255, 255, 0.25);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.25);
}

.panel h2 {
  margin: 0 0 0.4rem;
  font-family: var(--display);
  font-size: 1.25rem;
}

.panel p {
  margin: 0 0 1rem;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.45;
}

.join-row {
  display: flex;
  gap: 0.5rem;
}

#join-code {
  text-transform: uppercase;
  letter-spacing: 0.18em;
  font-weight: 700;
  text-align: center;
}

/* —— Color and theme controls —— */
.color-choice {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  margin: 0 0 0.5rem;
  padding: 0;
  border: 0;
}

.color-choice legend,
.field-label {
  grid-column: 1 / -1;
  display: block;
  margin-bottom: 0.4rem;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.color-choice label {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.42rem;
  padding: 0.58rem 0.45rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.88rem;
  background: rgba(255, 255, 255, 0.55);
}

.color-choice label:has(input:checked) {
  border-color: var(--board-frame);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent), transparent 55%);
}

.color-choice input { accent-color: var(--board-frame); }
.mini-piece { width: 17px; height: 17px; border-radius: 50%; display: inline-block; }
.mini-piece.white { background: var(--white-piece); border: 1px solid #999; }
.mini-piece.black { background: var(--black-piece); border: 1px solid #555; }
.panel p.choice-hint { min-height: 2.2em; margin: 0 0 0.65rem; font-size: 0.78rem; }

.theme-panel { grid-column: span 2; }
.theme-picker-row { display: flex; gap: 0.5rem; }
#theme-picker,
.theme-creator input[type="text"] {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 11px;
  padding: 0.65rem 0.75rem;
  font: inherit;
}
.theme-creator { margin-top: 0.9rem; }
.theme-creator summary { color: var(--muted); font-size: 0.86rem; font-weight: 700; cursor: pointer; }
#form-theme { margin-top: 0.8rem; }
.theme-name { display: grid; gap: 0.35rem; color: var(--muted); font-size: 0.78rem; font-weight: 700; }
.color-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 0.5rem;
  margin: 0.75rem 0;
}
.color-grid label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  padding: 0.45rem 0.55rem;
  border-radius: 10px;
  background: rgba(18, 18, 20, 0.05);
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
}
.color-grid input[type="color"] { width: 34px; height: 28px; padding: 0; border: 0; background: transparent; cursor: pointer; }

.status-line {
  text-align: center;
  color: #c8c8ce;
  min-height: 1.5rem;
  padding: 0 1rem 1.5rem;
}

/* —— Game —— */
.game-layout {
  display: grid;
  grid-template-columns: minmax(200px, 260px) 1fr;
  gap: 1.25rem;
  padding: 0 1.25rem 1.5rem;
  max-width: 1400px;
  margin: 0 auto;
  align-items: start;
}

.play-stage {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  gap: 0;
  min-width: 0;
  --board-pct: 70%;
}

.board-pane {
  flex: 1 1 var(--board-pct);
  min-width: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.video-pane {
  flex: 0 0 calc(100% - var(--board-pct) - 10px);
  min-width: 120px;
  max-width: 420px;
  display: flex;
  flex-direction: column;
}

.layout-splitter {
  flex: 0 0 10px;
  cursor: col-resize;
  touch-action: none;
  position: relative;
  margin: 0 2px;
  border-radius: 999px;
  background: transparent;
  align-self: stretch;
  min-height: 48px;
}
.layout-splitter::before {
  content: "";
  position: absolute;
  top: 20%;
  bottom: 20%;
  left: 3px;
  width: 4px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--accent), transparent 40%);
  opacity: 0.7;
}
.layout-splitter:hover::before,
.layout-splitter:focus-visible::before {
  opacity: 1;
  background: var(--accent);
}

.layout-slider-row {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.72rem;
  font-weight: 650;
  color: var(--muted);
  margin-top: 0.35rem;
}
.layout-slider-row input[type="range"] {
  width: 100%;
  accent-color: var(--board-frame);
  min-height: 32px;
}

.side-panel {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 1rem;
  border: 1px solid rgba(255, 255, 255, 0.25);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.25);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.player-card.order-top { order: 1; }
.turn-banner { order: 2; }
.player-card.order-bottom { order: 3; }
.side-actions { order: 4; }
.challenge-panel { order: 5; }
.chat-panel { order: 6; }
.rules-hint { order: 7; }

.player-card {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 0.65rem;
  align-items: center;
  padding: 0.65rem 0.7rem;
  border-radius: 14px;
  background: rgba(18, 18, 20, 0.05);
  border: 2px solid transparent;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.player-card.active-turn {
  border-color: var(--board-frame);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent), transparent 70%);
}

.player-card.is-bot {
  background: rgba(18, 18, 20, 0.08);
  border-style: dashed;
  border-color: rgba(18, 18, 20, 0.15);
}


.swatch {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  box-shadow: inset 0 2px 4px rgba(255, 255, 255, 0.25), 0 4px 8px rgba(0, 0, 0, 0.25);
}

.swatch.red,
.swatch.white {
  background: radial-gradient(circle at 35% 30%, #fff, var(--white-piece) 55%, var(--white-dark));
  border: 1px solid rgba(0, 0, 0, 0.12);
}

.swatch.black {
  background: radial-gradient(circle at 35% 30%, #5a5a60, var(--black-piece) 60%, #0a0a0b);
}

.pname { font-weight: 700; font-size: 0.95rem; }
.prole { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }

.caps {
  min-width: 2rem;
  text-align: center;
  font-weight: 700;
  font-size: 1.1rem;
  color: #3a3a40;
}

.turn-banner {
  text-align: center;
  font-weight: 700;
  font-size: 0.95rem;
  padding: 0.75rem;
  border-radius: 12px;
  background: linear-gradient(180deg, color-mix(in srgb, var(--board-frame), white 15%), var(--board-frame));
  color: #f0f0f2;
  letter-spacing: 0.02em;
}

.turn-banner.waiting {
  background: linear-gradient(180deg, #8a8a90, #5c5c62);
}

.turn-banner.win {
  background: linear-gradient(180deg, #e8e4dc, #b8b0a4);
  color: #1a1a1e;
}

.rules-hint {
  margin: 0.25rem 0 0;
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.45;
}

.muted { color: #b0b0b8; font-size: 0.8rem; display: block; }

.game-top .brand-mini { align-items: flex-start; }

/* —— Board —— */
.board-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0.5rem;
}

.board {
  --sq: min(6.2vw, 46px);
  width: calc(var(--sq) * 12);
  height: calc(var(--sq) * 12);
  display: grid;
  grid-template-columns: repeat(12, var(--sq));
  grid-template-rows: repeat(12, var(--sq));
  border-radius: 10px;
  overflow: hidden;
  box-shadow:
    0 0 0 10px var(--board-frame),
    0 0 0 14px color-mix(in srgb, var(--board-frame), black 55%),
    0 28px 50px rgba(0, 0, 0, 0.55),
    inset 0 0 40px rgba(0, 0, 0, 0.2);
  background: var(--dark-sq-2);
  touch-action: manipulation;
  user-select: none;
}

.square {
  position: relative;
  width: var(--sq);
  height: var(--sq);
}

.square.light {
  background:
    linear-gradient(145deg, var(--light-sq) 0%, var(--light-sq-2) 100%);
}

.square.dark {
  background:
    radial-gradient(120% 80% at 50% 0%, rgba(255, 255, 255, 0.05), transparent 50%),
    linear-gradient(160deg, var(--dark-sq) 0%, var(--dark-sq-2) 100%);
}

.square.highlight::after {
  content: "";
  position: absolute;
  inset: 18%;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.45);
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.35);
  pointer-events: none;
  z-index: 1;
}

.square.highlight.capture::after {
  background: rgba(220, 80, 60, 0.45);
  inset: 12%;
  border-radius: 50%;
  border: 2px solid rgba(255, 200, 180, 0.55);
  box-sizing: border-box;
}

.square.selected {
  box-shadow: inset 0 0 0 3px #f5f0e6;
}

.square.last-from,
.square.last-to {
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.4);
}

.piece {
  position: absolute;
  inset: 10%;
  border-radius: 50%;
  z-index: 2;
  cursor: pointer;
  transition: transform 0.18s cubic-bezier(0.2, 0.8, 0.2, 1), filter 0.15s;
  box-shadow:
    0 6px 10px rgba(0, 0, 0, 0.45),
    inset 0 2px 4px rgba(255, 255, 255, 0.35),
    inset 0 -4px 8px rgba(0, 0, 0, 0.35);
}

.piece::before {
  content: "";
  position: absolute;
  inset: 14%;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.2);
  box-shadow: inset 0 0 8px rgba(0, 0, 0, 0.2);
}

/* Game color "red" rendered as white pieces */
.piece.red {
  background:
    radial-gradient(circle at 32% 28%, #ffffff, var(--white-piece) 48%, var(--white-dark) 78%);
  border: 1px solid rgba(0, 0, 0, 0.1);
}

.piece.black {
  background:
    radial-gradient(circle at 32% 28%, var(--black-hi), var(--black-piece) 52%, #1e1e22 82%);
  border: 1.5px solid color-mix(in srgb, var(--black-hi), white 35%);
  box-shadow:
    0 5px 12px rgba(0, 0, 0, 0.55),
    0 1px 0 rgba(255, 255, 255, 0.22),
    inset 0 2px 4px rgba(255, 255, 255, 0.28),
    inset 0 -4px 8px rgba(0, 0, 0, 0.4);
}
.piece.black::before {
  border-color: rgba(255, 255, 255, 0.32);
}

.piece.king::after {
  content: "♛";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: calc(var(--sq) * 0.38);
  color: #c9a227;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.55);
  pointer-events: none;
}

.piece.red.king::after {
  color: #8a7010;
  text-shadow: 0 1px 1px rgba(255, 255, 255, 0.5);
}

.piece:hover {
  transform: scale(1.06);
  filter: brightness(1.08);
}

.piece.selected-piece {
  transform: scale(1.1) translateY(-2px);
  filter: brightness(1.12);
  z-index: 3;
}

.piece.dim {
  opacity: 0.45;
  cursor: default;
}

@media (max-width: 820px) {
  .theme-panel { grid-column: auto; }

  .game-layout {
    grid-template-columns: 1fr;
  }

  .side-panel {
    order: 3;
  }

  .play-stage {
    order: 1;
    flex-direction: column;
    --board-pct: 100%;
  }

  .board-pane {
    flex: 0 0 auto;
    width: 100%;
  }

  .layout-splitter {
    flex: 0 0 12px;
    cursor: row-resize;
    width: 100%;
    min-height: 16px;
    margin: 4px 0;
  }
  .layout-splitter::before {
    top: 5px;
    bottom: auto;
    left: 20%;
    right: 20%;
    width: auto;
    height: 4px;
  }

  .video-pane {
    flex: 0 0 auto;
    max-width: none;
    width: 100%;
    min-width: 0;
  }

  .av-tile-remote {
    max-height: 42vh;
    min-height: 140px;
    aspect-ratio: 16 / 10;
  }

  .board {
    --sq: min(7.5vw, 52px);
  }
}

@media (max-width: 480px) {
  .board {
    --sq: calc((100vw - 2rem) / 12);
  }

  .board-wrap {
    padding: 0.25rem;
  }

  .topbar {
    padding: 0.75rem 0.9rem;
  }

  .auth-card {
    padding: 1.5rem 1.15rem 1.25rem;
  }
}


/* —— In-room chat —— */
.chat-panel {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  min-height: 140px;
  max-height: 220px;
  border-radius: 12px;
  background: rgba(18, 18, 20, 0.06);
  border: 1px solid rgba(18, 18, 20, 0.1);
  padding: 0.55rem;
}

.chat-panel.hidden { display: none !important; }

.chat-header {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.82rem;
  line-height: 1.35;
  min-height: 72px;
}

.chat-msg {
  word-break: break-word;
}

.chat-msg .chat-from {
  font-weight: 650;
  color: var(--ink);
  margin-right: 0.35rem;
}

.chat-msg.mine .chat-from { color: color-mix(in srgb, var(--board-frame), black 10%); }

.chat-msg .chat-text { color: #2a2a30; }

.chat-form {
  display: flex;
  gap: 0.4rem;
}

.chat-form input {
  flex: 1;
  min-width: 0;
  border-radius: 999px;
  border: 1px solid var(--line);
  padding: 0.4rem 0.75rem;
  font: inherit;
  font-size: 0.85rem;
  background: #fff;
}

/* —— WebRTC AV —— */
.av-panel {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  border-radius: 12px;
  background: rgba(18, 18, 20, 0.08);
  border: 1px solid rgba(18, 18, 20, 0.12);
  padding: 0.55rem;
  height: 100%;
  min-height: 0;
}

.av-panel.hidden { display: none !important; }

/* Ensure remote tile stays visible once a call is live */
.av-panel.call-active .av-tile-remote,
.av-panel.call-active #remote-video {
  display: block;
  visibility: visible;
  opacity: 1;
  min-height: 120px;
}

.video-pane:has(.av-panel.hidden) { display: none; }
.play-stage:has(.av-panel.hidden) .layout-splitter { display: none; }
.play-stage:has(.av-panel.hidden) .board-pane { flex: 1 1 100%; }

.av-tiles {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 140px;
}

.av-tile {
  position: relative;
  aspect-ratio: 3 / 4;
  max-height: min(62vh, 520px);
  border-radius: 12px;
  overflow: hidden;
  background: #121214;
  border: 1px solid rgba(0, 0, 0, 0.25);
  flex: 1;
}

.av-tile-remote {
  aspect-ratio: auto;
  min-height: 180px;
}

.av-tile video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #0a0a0b;
  display: block;
}

.av-tile.pip {
  position: absolute;
  right: 8px;
  bottom: 8px;
  width: 34%;
  max-width: 140px;
  aspect-ratio: 3 / 4;
  border-radius: 10px;
  border: 2px solid rgba(255, 255, 255, 0.35);
  z-index: 2;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.45);
}

.av-label {
  position: absolute;
  left: 6px;
  bottom: 5px;
  font-size: 0.65rem;
  font-weight: 650;
  color: #f0f0f2;
  background: rgba(0, 0, 0, 0.55);
  padding: 0.1rem 0.4rem;
  border-radius: 999px;
  pointer-events: none;
  z-index: 3;
}

.av-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.av-error {
  margin: 0;
  font-size: 0.75rem;
  color: var(--danger);
  line-height: 1.35;
}

@media (max-width: 820px) {
  .chat-panel {
    max-height: 160px;
  }
}

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

.inline-theme select {
  max-width: 9.5rem;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: rgba(0, 0, 0, 0.28);
  color: #f0f0f2;
  font: inherit;
  font-size: 0.8rem;
  padding: 0.35rem 0.6rem;
}

@media (max-width: 480px) {
  .inline-theme select { max-width: 7.5rem; }
  .theme-panel { grid-column: auto; }
}

/* —— Roles / tournaments / admin —— */
.role-badge {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 0.2rem 0.45rem;
  border-radius: 999px;
  background: color-mix(in srgb, var(--accent), transparent 70%);
  border: 1px solid var(--line);
  color: var(--ink);
}
.role-badge[data-role="superadmin"] {
  background: color-mix(in srgb, #c9a227, transparent 55%);
}
.role-badge[data-role="admin"] {
  background: color-mix(in srgb, #4a7c59, transparent 55%);
}

.tourney-list, .user-admin-list, .admin-tourney-controls, .match-list {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  margin: 0.75rem 0;
}

.tourney-row, .user-admin-row, .match-row, .admin-match-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.55rem 0.65rem;
  border-radius: 10px;
  background: rgba(18, 18, 20, 0.05);
  border: 1px solid rgba(18, 18, 20, 0.08);
}

.tourney-meta, .admin-selected {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
}

.tourney-actions, .row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
}

.status-pill {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.15rem 0.4rem;
  border-radius: 999px;
  background: rgba(18, 18, 20, 0.08);
}
.status-open { background: color-mix(in srgb, #4a7c59, transparent 60%); }
.status-running, .status-playing, .status-ready { background: color-mix(in srgb, #3b6ea5, transparent 55%); }
.status-finished, .status-bye { background: rgba(18, 18, 20, 0.12); }
.status-draft { background: rgba(18, 18, 20, 0.06); }

.tourney-detail {
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--line);
}

.tourney-cols {
  display: grid;
  grid-template-columns: 1fr 1.4fr;
  gap: 0.75rem;
}

.plain-list {
  margin: 0.35rem 0 0;
  padding-left: 1.1rem;
}

.stack-form {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.stack-form label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
}

.stack-form input, .role-select {
  border-radius: 10px;
  border: 1px solid var(--line);
  padding: 0.55rem 0.7rem;
  font: inherit;
  background: #fff;
  min-height: 44px;
}

.muted.tiny, .tiny { font-size: 0.75rem; }

.admin-panel code, .superadmin-panel code {
  font-size: 0.8em;
}

@media (max-width: 820px) {
  .lobby-grid {
    grid-template-columns: 1fr;
  }

  .tourney-cols {
    grid-template-columns: 1fr;
  }

  .tourney-row, .user-admin-row, .match-row, .admin-match-row {
    flex-direction: column;
    align-items: stretch;
  }

  .tourney-actions, .row-actions {
    width: 100%;
  }

  .tourney-actions .btn,
  .row-actions .btn {
    flex: 1 1 auto;
    min-height: 44px;
  }

  .chat-panel {
    max-width: 100%;
    max-height: 260px;
  }

  .av-controls {
    flex-wrap: wrap;
  }

  .av-controls .btn {
    min-height: 44px;
  }

  .side-panel {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  .game-layout, .lobby-grid, .view {
    overflow-x: hidden;
  }

  .topbar {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .user-chip {
    flex-wrap: wrap;
    justify-content: flex-end;
  }
}

@media (max-width: 480px) {
  .panel {
    padding: 1rem 0.9rem;
  }

  .btn.wide, .btn.primary, .btn.secondary {
    min-height: 46px;
  }

  .join-row {
    flex-wrap: wrap;
  }

  .join-row input {
    width: 100%;
    min-height: 44px;
  }

  .join-row .btn {
    width: 100%;
    min-height: 44px;
  }
}


/* —— House rules / Challenge Strike —— */
.house-rules {
  margin: 0.65rem 0 0.85rem;
  padding: 0.65rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(18, 18, 20, 0.04);
}
.house-rules legend {
  padding: 0 0.25rem;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}
.toggle-row, .num-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  font-size: 0.85rem;
  font-weight: 600;
  margin: 0.35rem 0;
}
.num-row input[type="number"] {
  width: 4.2rem;
  border-radius: 8px;
  border: 1px solid var(--line);
  padding: 0.35rem 0.45rem;
  font: inherit;
  min-height: 36px;
}
#bot-difficulty {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 11px;
  padding: 0.65rem 0.75rem;
  font: inherit;
  margin-bottom: 0.55rem;
  min-height: 44px;
}

.challenge-panel {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  border-radius: 12px;
  background: rgba(18, 18, 20, 0.06);
  border: 1px solid rgba(18, 18, 20, 0.1);
  padding: 0.55rem;
}
.challenge-panel.hidden { display: none !important; }
.challenge-header {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
.challenge-pools { font-size: 0.82rem; line-height: 1.35; }
.challenge-status {
  margin: 0;
  min-height: 1.2em;
  font-size: 0.8rem;
  font-weight: 600;
  color: #3a3a40;
}
.challenge-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}
.challenge-actions .btn { min-height: 40px; }

.square.challenge-target {
  box-shadow: inset 0 0 0 3px #e8a54b;
}
.square.pending-challenge {
  box-shadow: inset 0 0 0 3px #d94a3a;
  animation: pulse-challenge 1.2s ease-in-out infinite;
}
@keyframes pulse-challenge {
  0%, 100% { filter: brightness(1); }
  50% { filter: brightness(1.15); }
}

.btn.danger {
  background: linear-gradient(180deg, #d4544a, #a32820);
  color: #fff;
  box-shadow: 0 8px 18px rgba(160, 40, 30, 0.35);
}
.btn.danger:hover:not(:disabled) { filter: brightness(1.06); }

.swatch.black {
  background: radial-gradient(circle at 35% 30%, var(--black-hi), var(--black-piece) 60%, #1a1a1e);
  border: 1px solid color-mix(in srgb, var(--black-hi), white 25%);
}
.mini-piece.black {
  background: var(--black-piece);
  border: 1px solid color-mix(in srgb, var(--black-hi), white 30%);
  box-shadow: 0 1px 2px rgba(0,0,0,0.35);
}

/* —— Profile / continue —— */
.profile-summary { display: grid; gap: 0.35rem; margin-bottom: 0.75rem; font-size: 0.92rem; }
.profile-stats { font-size: 1.05rem; margin-top: 0.25rem; }
.profile-sub { font-size: 0.85rem; margin: 0.75rem 0 0.35rem; opacity: 0.85; }
.profile-history {
  list-style: none; padding: 0; margin: 0 0 0.5rem;
  max-height: 9rem; overflow: auto; font-size: 0.82rem;
}
.profile-history li {
  padding: 0.35rem 0.45rem; border-radius: 6px;
  background: color-mix(in srgb, var(--accent, #888) 8%, transparent);
  margin-bottom: 0.25rem;
}
.continue-panel { border: 1px solid color-mix(in srgb, #f59e0b 50%, transparent); }

/* —— Clocks —— */
.pclock {
  font-variant-numeric: tabular-nums;
  font-size: 0.85rem;
  opacity: 0.85;
  margin-top: 0.15rem;
}
.player-card.active-turn .pclock { opacity: 1; font-weight: 600; color: var(--accent, #f5d78e); }

/* —— Pending challenge banner —— */
.pending-challenge-banner {
  order: 4;
  background: color-mix(in srgb, #f59e0b 22%, #1a1a1e);
  border: 1px solid #f59e0b;
  border-radius: 10px;
  padding: 0.75rem 0.85rem;
  margin: 0.35rem 0;
}
.pending-challenge-banner.hidden { display: none !important; }
.pcb-title { font-weight: 700; color: #fbbf24; margin-bottom: 0.25rem; }
.pcb-text { font-size: 0.88rem; margin: 0 0 0.5rem; }
.pcb-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }

/* Orange ring for challenge target / pending */
.square.challenge-target {
  box-shadow: inset 0 0 0 3px #f59e0b, 0 0 0 2px rgba(245, 158, 11, 0.55);
  outline: 2px solid rgba(245, 158, 11, 0.4);
}
.square.pending-challenge {
  box-shadow: inset 0 0 0 3px #f59e0b, 0 0 12px rgba(245, 158, 11, 0.65);
  animation: pulse-challenge 1.2s ease-in-out infinite;
}
.piece.challenge-ring {
  box-shadow: 0 0 0 3px #f59e0b, 0 0 10px rgba(245, 158, 11, 0.7);
}

/* —— Move animations —— */
.piece.animating {
  position: relative;
  z-index: 5;
  pointer-events: none;
  transition: transform 0.28s cubic-bezier(0.22, 0.9, 0.3, 1);
  will-change: transform;
}
.piece.animating.capture-slow {
  transition-duration: 0.7s;
}
.piece.captured-flash {
  outline: 3px solid #ef4444;
  box-shadow: 0 0 14px rgba(239, 68, 68, 0.85);
  animation: capture-pulse 0.55s ease-in-out;
  z-index: 4;
}
@keyframes capture-pulse {
  0% { transform: scale(1); opacity: 1; }
  40% { transform: scale(1.18); opacity: 1; }
  100% { transform: scale(0.4); opacity: 0; }
}
.board.anim-lock .piece { pointer-events: none; }

/* —— AV icon buttons —— */
.btn.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  min-width: 2.75rem;
  justify-content: center;
}
.btn.icon-btn .icon {
  width: 1.15rem;
  height: 1.15rem;
  flex-shrink: 0;
}
.btn.icon-btn .icon.hidden { display: none; }
.btn.icon-btn.is-off { opacity: 0.75; }
.btn.icon-btn.is-off .icon-label { text-decoration: line-through; }
@media (max-width: 640px) {
  .btn.icon-btn .icon-label { font-size: 0.7rem; }
}
