/* ── Reset ── */
*, *::before, *::after { margin:0; padding:0; box-sizing:border-box; }

/* ── Tokens ── */
:root {
  /* ── Carbon palette ── */
  --bg:          #0d0d10;
  --surface:     #141418;
  --surface2:    #1a1a20;
  --surface3:    #22222c;
  --border:      rgba(255,255,255,0.07);
  --border-hi:   rgba(255,255,255,0.15);
  --text:        #eeeef8;
  --text-2:      #7878a0;
  --text-3:      #38384e;
  --teal:        #2dd4bf;
  --teal-dim:    rgba(45,212,191,0.14);
  --orange:      #f07340;
  --blue:        #5b6ef5;
  --blue-hi:     #7280f8;
  --danger:      #e05858;
  --danger-dim:  rgba(224,88,88,0.12);
  --green:       #22d37f;
  /* Board: warm cream light + near-black navy dark = max contrast */
  --board-light: #e8e3d8;
  --board-dark:  #1e3040;
  /* Last move: transparent gold — warm, visible on both square colors */
  --sq-last:     rgba(210,170,50,0.42);
  --sq-selected: rgba(45,212,191,0.38);
  --radius:      10px;
  --rail-w:      210px;
  --board-px:    496px;
  --font:        'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  --font-mono:   'SF Mono','Fira Code','Courier New',monospace;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* ─────────────────────────────────────────────────────────── */
/* HEADER                                                       */
/* ─────────────────────────────────────────────────────────── */
.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 40px;
  border-bottom: 1px solid var(--border);
}

.header-brand { display:flex; flex-direction:column; gap:3px; }

.logo {
  font-size: 1.45rem;
  font-weight: 700;
  letter-spacing: -0.5px;
  line-height: 1;
}
.logo span { color: var(--teal); }

.header-meta {
  font-family: var(--font-mono);
  font-size: 0.66rem;
  color: var(--text-2);   /* bumped from text-3 for legibility */
  letter-spacing: 0.5px;
}

.share-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 9px 18px;
  background: transparent;
  color: var(--text-2);
  border: 1px solid var(--border-hi);
  border-radius: 8px;
  font-family: var(--font);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.share-btn:hover {
  background: var(--surface2);
  color: var(--text);
  border-color: var(--border-hi);
}
.share-btn.copied {
  color: var(--teal);
  border-color: var(--teal);
}

/* ─────────────────────────────────────────────────────────── */
/* MAIN LAYOUT                                                  */
/* ─────────────────────────────────────────────────────────── */
.main-content {
  display: grid;
  /* 1fr sidebar | fixed board | 1fr ad — board is always centered */
  grid-template-columns: 1fr var(--board-px) 1fr;
  gap: 0 32px;
  align-items: start;
  padding: 36px 40px 0;
  max-width: 1400px;
  margin: 0 auto;
}

/* ─────────────────────────────────────────────────────────── */
/* LEFT RAIL / SIDEBAR                                          */
/* ─────────────────────────────────────────────────────────── */
.sidebar {
  justify-self: end;   /* hug the board edge within its 1fr column */
  width: var(--rail-w);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  height: var(--board-px);   /* match board height */
  position: sticky;
  top: 36px;
}

/* Three stacked sections that fill sidebar height */
.sidebar-top {
  /* layout handled by the grid block below */
  padding: 18px 16px 14px;
  border-bottom: 1px solid var(--border);
}

.sidebar-mid {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-bottom {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 16px 14px;
}

/* ── Sidebar top: CSS grid so we can reorder play/timer/status across breakpoints ── */
.sidebar-top {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto;
  row-gap: 10px;
  column-gap: 10px;
}
.sidebar-top .btn-play   { grid-column: 1; grid-row: 1; width: 100%; }
.sidebar-top .game-timer { grid-column: 2; grid-row: 1; align-self: center; }
.sidebar-top .game-status { grid-column: 1 / -1; grid-row: 2; }

.game-timer {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--text-2);
  letter-spacing: 0.5px;
  min-width: 34px;
  text-align: right;
  flex-shrink: 0;
}

/* ── End-game button row ── */
.end-game-btns {
  display: flex;
  gap: 8px;
}

/* ── Play button — most prominent ── */
.btn-play {
  width: 100%;
  height: 48px; /* fixed so Play/Pause text swap doesn't resize */
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  background: var(--teal);
  color: #081a18;
  border: none;
  border-radius: 8px;
  font-family: var(--font);
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  letter-spacing: 0.3px;
  transition: background 0.12s, transform 0.1s, box-shadow 0.15s;
}
.btn-play:hover { background: #3de0cc; transform: translateY(-1px); }
.btn-play:active { transform: scale(0.98); }

@keyframes play-pulse {
  0%,100% { box-shadow: 0 0 0 0 var(--teal-dim); }
  50%      { box-shadow: 0 0 0 8px transparent; }
}
.btn-play.song-ready {
  animation: play-pulse 1.8s ease-in-out infinite;
  box-shadow: 0 0 20px rgba(45,212,191,0.45);
}

/* ── Game status ── */
.game-status {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-height: 1.2em;
  transition: color 0.2s;
}
.game-status[data-state="check"] {
  color: var(--danger);
  animation: blink 1.1s ease-in-out infinite;
}
.game-status[data-state="thinking"] { color: var(--text-3); }
.game-status[data-state="end"]      { color: var(--text); font-weight: 600; }
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:0.55} }

/* ── Divider ── */
.rail-divider {
  height: 1px;
  background: var(--border);
  margin: 0 -2px;
}

/* ── Game action buttons ── */
.game-btns {
  display: flex;
  gap: 8px;
}

.rail-btn {
  flex: 1;
  padding: 9px 10px;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 7px;
  font-family: var(--font);
  font-size: 0.78rem;
  font-weight: 500;
  cursor: pointer;
  text-align: center;
  white-space: nowrap;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}
.rail-btn:hover { background: var(--surface3); border-color: var(--border-hi); }

.rail-btn-danger { color: var(--danger); border-color: rgba(224,88,88,0.2); flex: none; }
.rail-btn-danger:hover { background: var(--danger-dim); border-color: var(--danger); }

.btn-forfeit-armed {
  color: var(--danger) !important;
  border-color: var(--danger) !important;
  background: var(--danger-dim) !important;
}
.rail-btn-danger:disabled { opacity:0.4; cursor:not-allowed; }

.rail-btn-full {
  flex: none;
  width: 100%;
}

.rail-btn-new {
  background: var(--surface2);
  color: var(--teal);
  border-color: rgba(45,212,191,0.25);
}
.rail-btn-new:hover {
  background: var(--teal-dim);
  border-color: var(--teal);
  color: var(--teal);
}

/* ── Control groups ── */
.control-group {
  display: flex;
  flex-direction: column;
  gap: 9px;
}

.control-label {
  font-size: 0.67rem;
  font-weight: 700;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--text-3);
}

.control-label-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.67rem;
  font-weight: 700;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--text-3);
}

.bpm-display {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--text-2);
  letter-spacing: 0;
  font-weight: 500;
}

/* ── Difficulty pills ── */
.diff-pills {
  display: flex;
  background: var(--surface3);
  border-radius: 7px;
  padding: 3px;
  gap: 2px;
}
.diff-pill {
  flex: 1;
  padding: 6px 0;
  background: transparent;
  border: none;
  border-radius: 5px;
  color: var(--text-3);
  font-family: var(--font);
  font-size: 0.74rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.diff-pill.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 4px rgba(0,0,0,0.4);
}
.diff-pill:hover:not(.active) { color: var(--text-2); }

/* Disable difficulty during active game */
.diff-pills.disabled {
  opacity: 0.4;
  pointer-events: none;
}
.diff-pills.disabled .diff-pill {
  pointer-events: none;
  cursor: default;
}

/* Mobile clone hidden on desktop; desktop original hidden on mobile (via media query).
   !important needed because .rail-btn-matrix { display:flex } has equal specificity
   and appears later in the cascade. */
.matrix-btn-mobile { display: none !important; }

/* ── See Matrix button ── */
.rail-btn-matrix {
  width: 100%;
  flex: none;
  background: var(--surface2);
  color: var(--text-2);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.rail-btn-matrix.active {
  background: var(--teal-dim);
  border-color: var(--teal);
  color: var(--teal);
}

/* ── EQ icon ── */
.eq-icon {
  display: inline-flex;
  align-items: flex-end;
  gap: 2px;
  height: 14px;
  flex-shrink: 0;
}

.eq-bar {
  display: block;
  width: 3px;
  border-radius: 1px;
  background: currentColor;
  opacity: 0.75;
  height: 4px;
}

/* Always animate — static bars look broken */
.eq-bar:nth-child(1) { animation: eq-bounce 0.9s ease-in-out infinite; animation-delay: 0s; }
.eq-bar:nth-child(2) { animation: eq-bounce 0.7s ease-in-out infinite; animation-delay: 0.15s; }
.eq-bar:nth-child(3) { animation: eq-bounce 1.1s ease-in-out infinite; animation-delay: 0.05s; }
.eq-bar:nth-child(4) { animation: eq-bounce 0.8s ease-in-out infinite; animation-delay: 0.3s; }

@keyframes eq-bounce {
  0%, 100% { height: 3px; }
  50%       { height: 13px; }
}

/* ── Sound select ── */
.select-input {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 8px 10px;
  color: var(--text);
  font-family: var(--font);
  font-size: 0.82rem;
  cursor: pointer;
  outline: none;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%2340405a' stroke-width='1.5' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 28px;
  transition: border-color 0.12s;
}
.select-input:focus { border-color: var(--border-hi); }
.select-input option { background: var(--surface2); }

/* ── BPM slider ── */
.bpm-slider {
  width: 100%;
  -webkit-appearance: none;
  appearance: none;
  height: 3px;
  background: var(--surface3);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}
.bpm-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--teal);
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.1s;
}
.bpm-slider::-webkit-slider-thumb:hover {
  transform: scale(1.2);
  box-shadow: 0 0 0 4px var(--teal-dim);
}
.bpm-slider::-moz-range-thumb {
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--teal);
  border: none;
  cursor: pointer;
}
.bpm-range-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.62rem;
  color: var(--text-2);
  font-family: var(--font-mono);
  margin-top: -4px;
}

/* ─────────────────────────────────────────────────────────── */
/* BOARD SECTION + FLIP                                         */
/* ─────────────────────────────────────────────────────────── */
.board-section {
  flex-shrink: 0;
}

/* 3D flip container */
.board-flip-container {
  width: var(--board-px);
  height: var(--board-px);
  perspective: 1200px;
}

.board-flip-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.65s cubic-bezier(0.4, 0, 0.2, 1);
}

.board-flip-inner.flipped {
  transform: rotateY(180deg);
}

.board-flip-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: var(--radius);
  overflow: hidden;
}

.board-flip-back {
  transform: rotateY(180deg);
}

/* ── Chess board (front) ── */
.board-wrapper {
  width: 100%;
  height: 100%;
  box-shadow:
    0 0 0 1px var(--border),
    0 8px 40px rgba(0,0,0,0.5);
  border-radius: var(--radius);
  overflow: hidden;
  transition: box-shadow 0.5s ease;
}

/* Animated border glow by active player */
.board-wrapper[data-turn="w"] {
  box-shadow:
    0 0 0 2px rgba(45,212,191,0.55),
    0 0 18px rgba(45,212,191,0.20),
    0 8px 40px rgba(0,0,0,0.5);
  animation: glow-pulse-w 2.2s ease-in-out infinite;
}
.board-wrapper[data-turn="b"] {
  box-shadow:
    0 0 0 2px rgba(240,115,64,0.55),
    0 0 18px rgba(240,115,64,0.20),
    0 8px 40px rgba(0,0,0,0.5);
  animation: glow-pulse-b 2.2s ease-in-out infinite;
}

@keyframes glow-pulse-w {
  0%,100% { box-shadow: 0 0 0 2px rgba(45,212,191,0.45), 0 0 12px rgba(45,212,191,0.12), 0 8px 40px rgba(0,0,0,0.5); }
  50%     { box-shadow: 0 0 0 2px rgba(45,212,191,0.80), 0 0 24px rgba(45,212,191,0.30), 0 8px 40px rgba(0,0,0,0.5); }
}
@keyframes glow-pulse-b {
  0%,100% { box-shadow: 0 0 0 2px rgba(240,115,64,0.45), 0 0 12px rgba(240,115,64,0.12), 0 8px 40px rgba(0,0,0,0.5); }
  50%     { box-shadow: 0 0 0 2px rgba(240,115,64,0.80), 0 0 24px rgba(240,115,64,0.30), 0 8px 40px rgba(0,0,0,0.5); }
}

#chess-board {
  display: grid;
  grid-template-columns: repeat(8, 62px);
  grid-template-rows: repeat(8, 62px);
  width: var(--board-px);
  height: var(--board-px);
}

/* ── Squares ── */
.square {
  width: 62px; height: 62px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  transition: filter 0.1s;
}
.sq-light { background: var(--board-light); }
.sq-dark  { background: var(--board-dark); }
.sq-last-move { background: var(--sq-last) !important; }
.sq-selected  { background: var(--sq-selected) !important; }

.sq-matrix-hover::before {
  content:'';
  position:absolute; inset:0;
  background: rgba(255,255,255,0.14);
  pointer-events:none; z-index:4;
}

.sq-target::after {
  content:'';
  position:absolute;
  width:20px; height:20px;
  border-radius:50%;
  background: rgba(0,0,0,0.18);
  pointer-events:none; z-index:2;
}
.sq-target.has-piece::after {
  width:60px; height:60px;
  border-radius:0;
  border:4px solid rgba(0,0,0,0.22);
  background:transparent;
}

/* File sweep — playhead column highlight on board */
.sq-file-active::after {
  content:'';
  position:absolute; inset:0;
  background: rgba(45,212,191,0.18);
  pointer-events:none; z-index:3;
  animation: sweep-fade 0.3s ease-out forwards;
}
@keyframes sweep-fade {
  0%   { opacity:1; }
  100% { opacity:0; }
}

/* Matrix overlay (used when overlay mode active on front) */
.sq-matrix-overlay {
  position:absolute; inset:0;
  z-index:3; pointer-events:none;
}
.sq-overlay-white { background: rgba(45,212,191,0.40); }
.sq-overlay-black { background: rgba(240,115,64,0.40); }
.sq-overlay-both  { background: linear-gradient(135deg,rgba(45,212,191,0.42) 50%,rgba(240,115,64,0.42) 50%); }

/* ── Pieces ── */
.piece {
  font-size: 40px; line-height:1;
  user-select:none; position:relative; z-index:1;
}
.piece-w {
  color: #f5f0e4;
  text-shadow: -1px -1px 0 #333, 1px -1px 0 #333, -1px 1px 0 #333, 1px 1px 0 #333;
}
.piece-b {
  color: #181820;
  text-shadow: -1px -1px 0 #aaa, 1px -1px 0 #aaa, -1px 1px 0 #aaa, 1px 1px 0 #aaa;
}

/* ── Tone matrix (back face) ── */
.matrix-wrapper {
  width: 100%;
  height: 100%;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 16px;
  box-shadow: 0 0 0 1px var(--border);
  border-radius: var(--radius);
}

.tone-matrix {
  display: grid;
  grid-template-columns: repeat(16, 1fr);
  grid-template-rows: repeat(16, 1fr);
  width: 450px;
  height: 450px;
  gap: 2px;
}

.mcell {
  background: var(--surface3);
  border-radius: 2px;
  transition: background 0.1s, filter 0.1s;
}
.mcell-white { background: var(--teal); }
.mcell-black { background: var(--orange); }
.mcell-boundary { border-bottom: 2px solid rgba(255,255,255,0.1); }

.mcell-col-hover { filter: brightness(1.3); }
.mcell-cell-hover { outline: 1px solid rgba(255,255,255,0.4); filter: brightness(1.6); }

@keyframes cell-pop {
  0%   { transform:scale(0.4); opacity:0.3; }
  60%  { transform:scale(1.15); }
  100% { transform:scale(1); opacity:1; }
}
.mcell-pop { animation: cell-pop 0.22s ease-out forwards; }

.mcell-playhead { background: rgba(255,255,255,0.10); }
.mcell-white.mcell-playhead { background: var(--teal);   filter: brightness(1.6); }
.mcell-black.mcell-playhead { background: var(--orange); filter: brightness(1.6); }

.matrix-legend {
  display: flex;
  gap: 20px;
  font-size: 0.7rem;
  color: var(--text-2);
}
.legend-w { color: var(--teal); }
.legend-b { color: var(--orange); }

/* ─────────────────────────────────────────────────────────── */
/* RECENT GAMES                                                */
/* ─────────────────────────────────────────────────────────── */
.recent-section {
  max-width: 790px;
  margin: 80px auto 0;
  padding: 0 40px 80px;
}

/* Tabs */
.recent-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0;
}
.recent-tab {
  padding: 8px 16px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-3);
  font-family: var(--font);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  margin-bottom: -1px;
  transition: color 0.12s, border-color 0.12s;
}
.recent-tab.active {
  color: var(--text);
  border-bottom-color: var(--teal);
}
.recent-tab:hover:not(.active) { color: var(--text-2); }

.recent-grid {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
}

.no-games-msg {
  font-size: 0.8rem;
  color: var(--text-3);
  padding: 32px 0;
}

/* Game cards */
.game-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  transition: border-color 0.15s, transform 0.18s, box-shadow 0.18s;
}
.game-card:hover {
  border-color: var(--border-hi);
  transform: translateY(-3px);
  box-shadow: 0 8px 30px rgba(0,0,0,0.35);
}

.mini-matrix {
  display: block;
  border-radius: 6px;
  image-rendering: pixelated;
}

.game-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.game-result {
  font-size: 0.67rem;
  font-weight: 700;
  letter-spacing: 0.5px;
  padding: 3px 8px;
  border-radius: 4px;
}
.result-win     { color: var(--green);  background: rgba(34,211,127,0.12); }
.result-loss    { color: var(--danger); background: var(--danger-dim); }
.result-draw    { color: var(--text-2); background: var(--surface3); }
.result-forfeit { color: var(--text-2); background: var(--surface3); }
.result-game    { color: var(--text-2); background: var(--surface3); }

.game-date {
  font-size: 0.7rem;
  color: var(--text-3);
  font-family: var(--font-mono);
}

.play-game-btn {
  width: 100%;
  padding: 7px 0;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 7px;
  color: var(--text-2);
  font-family: var(--font);
  font-size: 0.75rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}
.play-game-btn:hover {
  background: var(--teal);
  border-color: var(--teal);
  color: #081a18;
  font-weight: 600;
}
.play-game-btn.playing {
  background: var(--teal-dim);
  border-color: var(--teal);
  color: var(--teal);
}

/* ─────────────────────────────────────────────────────────── */
/* MODAL                                                       */
/* ─────────────────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.72);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.modal-backdrop.hidden { display: none; }

.modal-box {
  background: var(--surface);
  border: 1px solid var(--border-hi);
  border-radius: 16px;
  padding: 32px;
  width: 400px;
  max-width: calc(100vw - 48px);
  box-shadow: 0 24px 80px rgba(0,0,0,0.6);
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}
.modal-title { font-size: 1.05rem; font-weight: 700; }
.modal-close {
  background:none; border:none;
  color: var(--text-3); font-size: 1rem; cursor:pointer;
  padding: 4px 6px; border-radius:4px;
  transition: color 0.12s, background 0.12s;
}
.modal-close:hover { color:var(--text); background:var(--surface3); }
.modal-body {
  font-size:0.875rem; color:var(--text-2);
  line-height:1.65; margin-bottom:22px;
}
.modal-input-row { display:flex; gap:8px; }
.modal-input {
  flex:1;
  background:var(--surface3); border:1px solid var(--border);
  border-radius:7px; padding:10px 12px;
  color:var(--text); font-family:var(--font); font-size:0.875rem;
  outline:none; transition:border-color 0.12s;
}
.modal-input:focus { border-color:var(--blue); }
.modal-input::placeholder { color:var(--text-3); }
.modal-submit {
  background:var(--blue); color:#fff; border:none;
  border-radius:7px; padding:10px 18px;
  font-family:var(--font); font-size:0.875rem; font-weight:600;
  cursor:pointer; transition:background 0.12s; white-space:nowrap;
}
.modal-submit:hover { background:var(--blue-hi); }
.modal-submit:disabled { opacity:0.6; cursor:default; }
.modal-confirm { margin-top:14px; font-size:0.875rem; color:var(--green); }

/* ─────────────────────────────────────────────────────────── */
/* AD PLACEHOLDERS                                             */
/* ─────────────────────────────────────────────────────────── */

/* Right rail — holds the 300×250 rect */
.ad-rail {
  justify-self: start;  /* hug the board edge within its 1fr column */
  display: flex;
  align-items: flex-start;
  padding-top: 0;
}

/* Base slot */
.ad-slot {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px dashed rgba(255,255,255,0.12);
  border-radius: 8px;
  background: rgba(255,255,255,0.02);
  position: relative;
}

.ad-label {
  font-family: var(--font-mono);
  font-size: 0.58rem;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--text-3);
  opacity: 0.6;
  pointer-events: none;
  user-select: none;
}

/* Leaderboard 728×90 */
.ad-leaderboard {
  width: 728px;
  height: 90px;
  margin: 0 auto 24px;
  max-width: calc(100vw - 80px);
}

/* 300×250 rectangle */
.ad-rect {
  width: 300px;
  height: 250px;
}

/* Between-sections banner */
.ad-banner {
  height: 90px;
}

/* ─────────────────────────────────────────────────────────── */
/* UTILITY                                                     */
/* ─────────────────────────────────────────────────────────── */
.hidden { display:none !important; }

/* ─────────────────────────────────────────────────────────── */
/* RESPONSIVE — MOBILE  (≤ 720px)                              */
/* ─────────────────────────────────────────────────────────── */
@media (max-width: 720px) {

  /* ── Token overrides ── */
  :root {
    --board-px: calc(100vw - 32px);
    --sq:       calc((100vw - 32px) / 8);
  }

  /* ── Ads: hide desktop slots, keep banner ── */
  .ad-leaderboard { display: none; }
  .ad-rail        { display: none; }
  .ad-banner {
    max-width: calc(100vw - 32px);
    height: 60px;
    margin: 0 auto 40px;
    padding: 0 !important;
  }

  /* ── Header ── */
  .site-header  { padding: 12px 16px; }
  .logo         { font-size: 1.15rem; }
  .header-meta  { font-size: 0.6rem; }
  .challenge-btn { padding: 8px 14px; font-size: 0.78rem; }

  /* ── Main layout: single column, board on top ── */
  .main-content {
    grid-template-columns: 1fr;
    grid-template-areas:
      "board"
      "sidebar";
    padding: 16px 16px 0;
    gap: 12px;
    max-width: 100%;
  }
  .board-section { grid-area: board; }

  /* ── Board scaling ── */
  .board-flip-container {
    width: var(--board-px);
    height: var(--board-px);
  }
  #chess-board {
    grid-template-columns: repeat(8, var(--sq));
    grid-template-rows:    repeat(8, var(--sq));
    width:  var(--board-px);
    height: var(--board-px);
  }
  .square { width: var(--sq); height: var(--sq); }
  .piece  { font-size: calc(var(--sq) * 0.62); }

  .sq-target::after {
    width:  calc(var(--sq) * 0.32);
    height: calc(var(--sq) * 0.32);
  }
  .sq-target.has-piece::after {
    width:  calc(var(--sq) * 0.95);
    height: calc(var(--sq) * 0.95);
  }

  /* Tone matrix back-face scales with board */
  .tone-matrix {
    width:  calc(var(--board-px) - 40px);
    height: calc(var(--board-px) - 40px);
  }

  /* ── Sidebar: 2-column grid, sits below board ── */
  .sidebar {
    grid-area:    sidebar;
    justify-self: stretch;
    width:        100%;
    height:       auto;       /* override desktop fixed height */
    position:     static;     /* undo sticky */
    overflow:     visible;

    /* switch from flex column → 2-col grid */
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto auto;
    grid-template-areas:
      "top top"
      "mid bot";
  }

  .sidebar-top {
    grid-area:     top;
    border-right:  none;
    border-bottom: 1px solid var(--border);
    padding: 14px 14px 12px;
  }

  /* Mobile: play button full-width, timer moves next to status text */
  .sidebar-top .btn-play    { grid-column: 1 / -1; grid-row: 1; }
  .sidebar-top .game-timer  { grid-column: 1; grid-row: 2; align-self: center; justify-self: start; text-align: left; }
  .sidebar-top .game-status { grid-column: 2; grid-row: 2; text-align: right; }

  .sidebar-mid {
    grid-area:     mid;
    flex:          none;                        /* override desktop flex:1 */
    overflow:      visible;
    border-bottom: none;
    border-right:  1px solid var(--border);
    padding: 12px 12px 16px;
    gap: 8px;
  }

  .sidebar-bottom {
    grid-area: bot;
    padding: 12px 12px 16px;
  }

  /* Swap See Matrix: hide desktop original, show mobile clone */
  .matrix-btn-desktop { display: none !important; }
  .matrix-btn-mobile  { display: flex !important; }

  /* Forfeit + New Game stack vertically in the mid column on mobile */
  .end-game-btns { flex-direction: column; gap: 6px; }
  .rail-btn-danger { flex: none; }

  /* ── Recent games → horizontal carousel ── */
  .recent-section {
    margin: 32px auto 0;
    padding: 0 16px 64px;
    max-width: 100%;
  }

  .recent-grid {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x mandatory;
    scrollbar-width: none;
    gap: 10px;
    padding-bottom: 4px;
    /* 2⅓ cards visible; drag to see more */
  }
  .recent-grid::-webkit-scrollbar { display: none; }

  .game-card {
    flex: 0 0 calc(100% / 2.33 - 8px);
    scroll-snap-align: start;
    min-width: 0;
  }

  /* Let mini matrix fill card width (overrides inline style from JS) */
  .mini-matrix {
    width: 100% !important;
    height: auto !important;
    aspect-ratio: 1;
  }
}

/* ── Very small phones (≤ 390px) ─────────────────────────── */
@media (max-width: 390px) {
  .game-card { flex: 0 0 calc(100% / 1.8 - 8px); }
}
