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

:root {
  --bg-deepest:   #111009;
  --bg-app:       #1a1410;
  --bg-panel:     #231c15;
  --bg-card:      #2d2318;
  --bg-hover:     #38301f;
  --bg-active:    #4a3c28;

  --sq-light:     #f0d9b5;
  --sq-dark:      #b58863;
  --sq-light-hl:  #cdd16f;
  --sq-dark-hl:   #aaa23a;
  --sq-selected:  #829769;
  --sq-target:    rgba(20, 85, 30, 0.5);
  --sq-capture:   rgba(180, 30, 30, 0.55);

  --text-primary: #f0e8d8;
  --text-secondary: #b8a88a;
  --text-muted:   #7a6a50;
  --text-active:  #f5d680;

  --gold:         #c8a96e;
  --gold-dim:     #8b6e3c;
  --green:        #5a9e72;
  --red:          #c04444;
  --explore:      #7ab4d4;

  --border:       #3a3020;
  --border-light: #4e4232;

  --font-display: 'Crimson Pro', 'Georgia', serif;
  --font-ui:      'Inter', 'Segoe UI', system-ui, sans-serif;
  --font-mono:    'JetBrains Mono', 'Fira Code', 'Courier New', monospace;

  --radius:       4px;
  --radius-lg:    8px;
  --shadow:       0 2px 8px rgba(0,0,0,0.5);
  --shadow-lg:    0 8px 32px rgba(0,0,0,0.7);
}

/* ═══════════════════════════════════════════════════════════════════════
   BOARD THEMES — each defines sq-light/dark and matching UI accent tones
   Applied as body.theme-{name} (dark mode default) +
              body.theme-light.theme-{name} (light mode override)
   ═══════════════════════════════════════════════════════════════════════ */

/* ── Walnut (default — warm brown wood) ─────────────────────────────── */
/* Dark Walnut: already defined in :root above */
body.theme-walnut {
  /* explicit so JS class toggling always works */
  --sq-light:     #f0d9b5;
  --sq-dark:      #b58863;
  --sq-light-hl:  #cdd16f;
  --sq-dark-hl:   #aaa23a;
  --sq-selected:  #829769;
}
body.theme-light.theme-walnut {
  --bg-deepest:   #d8cdb8;
  --bg-app:       #efe7d8;
  --bg-panel:     #f7f1e6;
  --bg-card:      #ffffff;
  --bg-hover:     #ece2cf;
  --bg-active:    #e2d4b8;
  --sq-light:     #f0d9b5;
  --sq-dark:      #b58863;
  --sq-light-hl:  #f5f17e;
  --sq-dark-hl:   #c8c25a;
  --sq-selected:  #aebd7f;
  --text-primary: #2b2218;
  --text-secondary:#5e4f3a;
  --text-muted:   #9c8b6e;
  --text-active:  #8a5a18;
  --gold:         #9c7a35;
  --gold-dim:     #c8a96e;
  --green:        #3f7d54;
  --red:          #b53c3c;
  --border:       #ddccab;
  --border-light: #c9b690;
  --shadow:       0 2px 8px rgba(80,60,30,0.15);
  --shadow-lg:    0 8px 32px rgba(80,60,30,0.25);
}

/* ── Slate (cool blue-grey) ──────────────────────────────────────────── */
body.theme-slate {
  --bg-deepest:   #0d1117;
  --bg-app:       #161b22;
  --bg-panel:     #1c2128;
  --bg-card:      #21262d;
  --bg-hover:     #2d333b;
  --bg-active:    #373e47;
  --sq-light:     #dee3e6;
  --sq-dark:      #8ca2ad;
  --sq-light-hl:  #f6f669;
  --sq-dark-hl:   #baca2b;
  --sq-selected:  #90a0b0;
  --text-primary: #e6edf3;
  --text-secondary:#8b949e;
  --text-muted:   #484f58;
  --text-active:  #79c0ff;
  --gold:         #58a6ff;
  --gold-dim:     #1f6feb;
  --green:        #3fb950;
  --red:          #f85149;
  --border:       #30363d;
  --border-light: #3d444d;
  --shadow:       0 2px 8px rgba(0,0,0,0.5);
  --shadow-lg:    0 8px 32px rgba(0,0,0,0.7);
}
body.theme-light.theme-slate {
  --bg-deepest:   #c8d0d8;
  --bg-app:       #dde3ea;
  --bg-panel:     #edf0f3;
  --bg-card:      #ffffff;
  --bg-hover:     #dde3ea;
  --bg-active:    #cdd4dc;
  --sq-light:     #dee3e6;
  --sq-dark:      #8ca2ad;
  --sq-light-hl:  #f6f669;
  --sq-dark-hl:   #baca2b;
  --sq-selected:  #90a0b0;
  --text-primary: #1c2128;
  --text-secondary:#444c56;
  --text-muted:   #8c959f;
  --text-active:  #0969da;
  --gold:         #0969da;
  --gold-dim:     #218bff;
  --green:        #1a7f37;
  --red:          #cf222e;
  --border:       #c8d0d8;
  --border-light: #b3bbc4;
  --shadow:       0 2px 8px rgba(30,50,70,0.12);
  --shadow-lg:    0 8px 32px rgba(30,50,70,0.2);
}

/* ── Forest (deep green) ─────────────────────────────────────────────── */
body.theme-forest {
  --bg-deepest:   #0a120a;
  --bg-app:       #121e12;
  --bg-panel:     #182618;
  --bg-card:      #1e2e1e;
  --bg-hover:     #263826;
  --bg-active:    #304830;
  --sq-light:     #eeeed2;
  --sq-dark:      #769656;
  --sq-light-hl:  #f6f669;
  --sq-dark-hl:   #baca2b;
  --sq-selected:  #8aaa5a;
  --text-primary: #d8ecd8;
  --text-secondary:#90b890;
  --text-muted:   #507050;
  --text-active:  #a8e090;
  --gold:         #90c860;
  --gold-dim:     #507038;
  --green:        #5ab870;
  --red:          #d05050;
  --border:       #263826;
  --border-light: #385038;
  --shadow:       0 2px 8px rgba(0,0,0,0.5);
  --shadow-lg:    0 8px 32px rgba(0,0,0,0.7);
}
body.theme-light.theme-forest {
  --bg-deepest:   #c0d0b8;
  --bg-app:       #d8e8d0;
  --bg-panel:     #ecf4e8;
  --bg-card:      #ffffff;
  --bg-hover:     #dcecd4;
  --bg-active:    #c8dcc0;
  --sq-light:     #eeeed2;
  --sq-dark:      #769656;
  --sq-light-hl:  #f6f669;
  --sq-dark-hl:   #baca2b;
  --sq-selected:  #8aaa5a;
  --text-primary: #1a2e1a;
  --text-secondary:#3a5e3a;
  --text-muted:   #7a9a7a;
  --text-active:  #2a6020;
  --gold:         #3a7820;
  --gold-dim:     #5a9840;
  --green:        #2a8040;
  --red:          #c03030;
  --border:       #c0d8b8;
  --border-light: #a8c8a0;
  --shadow:       0 2px 8px rgba(20,50,20,0.12);
  --shadow-lg:    0 8px 32px rgba(20,50,20,0.2);
}

/* ── Midnight (deep navy/purple) ─────────────────────────────────────── */
body.theme-midnight {
  --bg-deepest:   #06050f;
  --bg-app:       #0e0c1e;
  --bg-panel:     #14122a;
  --bg-card:      #1a1836;
  --bg-hover:     #222040;
  --bg-active:    #2c294e;
  --sq-light:     #d9d4f5;
  --sq-dark:      #7060a8;
  --sq-light-hl:  #f0e860;
  --sq-dark-hl:   #b8a820;
  --sq-selected:  #8878c0;
  --text-primary: #e0dcf8;
  --text-secondary:#9890c8;
  --text-muted:   #504870;
  --text-active:  #c8b8ff;
  --gold:         #b898ff;
  --gold-dim:     #7858c0;
  --green:        #60a878;
  --red:          #e05868;
  --border:       #201e3c;
  --border-light: #302e50;
  --shadow:       0 2px 8px rgba(0,0,0,0.6);
  --shadow-lg:    0 8px 32px rgba(0,0,0,0.8);
}
body.theme-light.theme-midnight {
  --bg-deepest:   #c0b8e0;
  --bg-app:       #d8d0f0;
  --bg-panel:     #eee8fc;
  --bg-card:      #ffffff;
  --bg-hover:     #e4dcf8;
  --bg-active:    #d4c8f0;
  --sq-light:     #d9d4f5;
  --sq-dark:      #7060a8;
  --sq-light-hl:  #f0e860;
  --sq-dark-hl:   #b8a820;
  --sq-selected:  #8878c0;
  --text-primary: #180e30;
  --text-secondary:#3c2878;
  --text-muted:   #8870b8;
  --text-active:  #5030a0;
  --gold:         #5030a0;
  --gold-dim:     #7050c0;
  --green:        #2a6848;
  --red:          #b02038;
  --border:       #ccc0e8;
  --border-light: #b8a8d8;
  --shadow:       0 2px 8px rgba(30,20,60,0.12);
  --shadow-lg:    0 8px 32px rgba(30,20,60,0.2);
}

/* ── Parchment (antique warm sepia) ─────────────────────────────────── */
body.theme-parchment {
  --bg-deepest:   #100c08;
  --bg-app:       #1e1610;
  --bg-panel:     #281e16;
  --bg-card:      #32261c;
  --bg-hover:     #3e3028;
  --bg-active:    #4a3c30;
  --sq-light:     #f5deb3;
  --sq-dark:      #c8a06c;
  --sq-light-hl:  #e8e878;
  --sq-dark-hl:   #c0b840;
  --sq-selected:  #a89050;
  --text-primary: #f0e0c8;
  --text-secondary:#c8a880;
  --text-muted:   #806848;
  --text-active:  #f0c878;
  --gold:         #d4a050;
  --gold-dim:     #906830;
  --green:        #70a060;
  --red:          #c04040;
  --border:       #382e22;
  --border-light: #4a3e30;
  --shadow:       0 2px 8px rgba(0,0,0,0.5);
  --shadow-lg:    0 8px 32px rgba(0,0,0,0.7);
}
body.theme-light.theme-parchment {
  --bg-deepest:   #d8c8a8;
  --bg-app:       #f0e8d0;
  --bg-panel:     #faf4e4;
  --bg-card:      #fffff0;
  --bg-hover:     #f0e8d0;
  --bg-active:    #e8d8b8;
  --sq-light:     #f5deb3;
  --sq-dark:      #c8a06c;
  --sq-light-hl:  #e8e878;
  --sq-dark-hl:   #c0b840;
  --sq-selected:  #a89050;
  --text-primary: #2c1e10;
  --text-secondary:#6c4828;
  --text-muted:   #a08060;
  --text-active:  #8c5020;
  --gold:         #8c5020;
  --gold-dim:     #b07030;
  --green:        #406830;
  --red:          #a83020;
  --border:       #d8c8a0;
  --border-light: #c8b080;
  --shadow:       0 2px 8px rgba(60,40,20,0.15);
  --shadow-lg:    0 8px 32px rgba(60,40,20,0.25);
}

/* Coord label colours — applied for all themes */
.square.light .coord { color: var(--sq-dark); }
.square.dark  .coord { color: var(--sq-light); }

/* ─── Settings UI ─────────────────────────────────────────────────────── */
.settings-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  margin-bottom: 4px;
}

.settings-swatch {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 6px 4px;
  background: var(--bg-card);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.settings-swatch:hover { border-color: var(--border-light); background: var(--bg-hover); }
.settings-swatch.active { border-color: var(--gold); background: var(--bg-active); }

.swatch-board {
  display: grid;
  grid-template-columns: 1fr 1fr;
  width: 28px;
  height: 28px;
  border-radius: 3px;
  overflow: hidden;
  flex-shrink: 0;
}
.swatch-sq.light { background: var(--sq-light); }
.swatch-sq.dark  { background: var(--sq-dark); }

.swatch-piece {
  width: 28px;
  height: 28px;
  object-fit: contain;
}

.swatch-label {
  font-size: 0.62rem;
  color: var(--text-muted);
  text-align: center;
  line-height: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}
.settings-swatch.active .swatch-label { color: var(--text-active); }

/* Per-theme board swatch colours — override .swatch-sq inside each button */
[data-theme="walnut"]    .swatch-sq.light { background: #f0d9b5; }
[data-theme="walnut"]    .swatch-sq.dark  { background: #b58863; }
[data-theme="slate"]     .swatch-sq.light { background: #dee3e6; }
[data-theme="slate"]     .swatch-sq.dark  { background: #8ca2ad; }
[data-theme="forest"]    .swatch-sq.light { background: #eeeed2; }
[data-theme="forest"]    .swatch-sq.dark  { background: #769656; }
[data-theme="midnight"]  .swatch-sq.light { background: #d9d4f5; }
[data-theme="midnight"]  .swatch-sq.dark  { background: #7060a8; }
[data-theme="parchment"] .swatch-sq.light { background: #f5deb3; }
[data-theme="parchment"] .swatch-sq.dark  { background: #c8a06c; }


/* ─── Typography ─────────────────────────────────────────────────────── */
@import url('https://fonts.googleapis.com/css2?family=Crimson+Pro:ital,wght@0,400;0,600;1,400&family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500&display=swap');

html { font-size: 14px; }
body {
  font-family: var(--font-ui);
  background: var(--bg-app);
  color: var(--text-primary);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ─── Layout ─────────────────────────────────────────────────────────── */
#app-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  background: var(--bg-deepest);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  min-height: 96px;
}

/* Menu button always on left — hidden on desktop since sidebar is always visible */
#btn-menu { display: none; }

#logo-plate {
  display: flex;
  align-items: center;
  padding: 8px 14px;
  border-radius: 12px;
  transition: background 0.15s;
}

#app-logo {
  height: 76px;
  width: auto;
  display: block;
}

/* Dark mode plate colours per theme */
body:not(.theme-light) #logo-plate                { background: #eee6d6; }
body:not(.theme-light).theme-slate     #logo-plate { background: #dde3e6; }
body:not(.theme-light).theme-forest    #logo-plate { background: #e2ecdc; }
body:not(.theme-light).theme-midnight  #logo-plate { background: #e4dff5; }
body:not(.theme-light).theme-parchment #logo-plate { background: #f0e4cc; }

#main-layout {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

/* ─── Left sidebar: game list ────────────────────────────────────────── */
#sidebar-left {
  width: 220px;
  min-width: 180px;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
}

#sidebar-left-header {
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  gap: 6px;
}
.sidebar-header-row h2 {
  flex: 1;
  font-family: var(--font-display);
  font-size: 1rem;
  color: var(--text-secondary);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ─── Library items ──────────────────────────────────────────────────── */
.library-item {
  position: relative;
  padding: 9px 12px 9px 14px;
  cursor: pointer;
  border-left: 3px solid transparent;
  transition: background 0.1s, border-color 0.1s;
}
.library-item:hover { background: var(--bg-hover); }
.library-item.active { background: var(--bg-active); border-left-color: var(--gold); }

.library-item-name {
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right: 52px;
}
.library-item-count {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 2px;
}
.library-item-actions {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity 0.1s;
}
.library-item:hover .library-item-actions { opacity: 1; }

.lib-action-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.7rem;
  cursor: pointer;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 3px;
  transition: color 0.1s, background 0.1s;
}
.lib-action-btn:hover { color: var(--text-primary); background: var(--bg-active); }
.lib-action-btn.delete:hover { color: var(--red); background: rgba(180,50,50,0.15); }

/* inline rename input */
.library-rename-input {
  width: 100%;
  background: var(--bg-card);
  border: 1px solid var(--gold-dim);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.82rem;
  padding: 3px 6px;
  font-family: var(--font-ui);
  outline: none;
}

#game-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px 0;
}

.game-item {
  position: relative;
  padding: 8px 12px;
  cursor: pointer;
  border-left: 3px solid transparent;
  transition: background 0.1s, border-color 0.1s;
}
.game-item:hover { background: var(--bg-hover); }
.game-item.active { background: var(--bg-active); border-left-color: var(--gold); }

.game-players {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right: 20px;
}
.game-meta {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.game-delete-btn {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1rem;
  cursor: pointer;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 3px;
  opacity: 0;
  transition: opacity 0.1s, color 0.1s;
}
.game-item:hover .game-delete-btn { opacity: 1; }
.game-delete-btn:hover { color: var(--red); background: rgba(180,50,50,0.15); }

.empty-state {
  padding: 20px 12px;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.78rem;
  line-height: 1.6;
}

/* ─── Center: board ──────────────────────────────────────────────────── */
#center-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 12px 12px 0;
  overflow: hidden;
  min-width: 0;
  min-height: 0;
  --engine-panel-h: 38px; /* header only when off; JS expands when analysis on */
}

#board-wrap {
  position: relative;
  /* Board fills available height minus fixed chrome (header ~96px, controls ~48px, status ~24px, padding) */
  width: min(
    calc(100vh - 96px - var(--engine-panel-h) - 90px),
    calc(100vw - 520px - 24px),
    560px
  );
  aspect-ratio: 1;
  flex-shrink: 0;
}

#board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 100%;
  height: 100%;
  border: 3px solid var(--bg-deepest);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg), inset 0 0 0 1px var(--gold-dim);
  overflow: hidden;
  user-select: none;
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.05s;
}
.square.light { background: var(--sq-light); }
.square.dark  { background: var(--sq-dark); }
.square.last-move.light { background: var(--sq-light-hl); }
.square.last-move.dark  { background: var(--sq-dark-hl); }
.square.selected { background: var(--sq-selected) !important; }
.square.move-target::after {
  content: '';
  position: absolute;
  width: 32%;
  height: 32%;
  border-radius: 50%;
  background: var(--sq-target);
  pointer-events: none;
  z-index: 2;
}
.square.capture-target::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 50%;
  box-shadow: inset 0 0 0 5px var(--sq-capture);
  pointer-events: none;
  z-index: 2;
}

.piece {
  width: 88%;
  height: 88%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
  position: relative;
  z-index: 3;
  filter: drop-shadow(1px 2px 2px rgba(0,0,0,0.35));
  transition: transform 0.05s;
}
.piece:active { cursor: grabbing; transform: scale(1.1); }
.piece img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; }

.coord {
  position: absolute;
  font-family: var(--font-ui);
  font-size: 0.6rem;
  font-weight: 600;
  line-height: 1;
  pointer-events: none;
  z-index: 1;
}
.rank-label { top: 2px; left: 2px; }
.file-label { bottom: 2px; right: 2px; }
.square.light .coord { color: var(--sq-dark); }
.square.dark  .coord { color: var(--sq-light); }

/* Menu button — hidden on desktop, shown on mobile/tablet via media query */
#btn-menu { display: none; }

/* ─── Controls bar ────────────────────────────────────────────────────── */
#controls-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 10px;
}

.ctrl-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-primary);
  width: 34px;
  height: 34px;
  border-radius: var(--radius);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  transition: background 0.1s, border-color 0.1s;
}
.ctrl-btn:hover { background: var(--bg-hover); border-color: var(--border-light); }
.ctrl-btn:active { background: var(--bg-active); }

#status-line {
  margin-top: 8px;
  font-size: 0.78rem;
  color: var(--text-secondary);
  text-align: center;
  min-height: 1.2em;
  font-family: var(--font-display);
  font-style: italic;
}

/* ─── Explore bar ─────────────────────────────────────────────────────── */
#explore-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  padding: 8px 12px;
  background: rgba(122, 180, 212, 0.12);
  border: 1px solid rgba(122, 180, 212, 0.3);
  border-radius: var(--radius);
  width: 100%;
  max-width: 400px;
}
#explore-bar.hidden { display: none; }
#explore-bar span {
  color: var(--explore);
  font-size: 0.78rem;
  flex: 1;
}

.explore-btn {
  padding: 4px 10px;
  border-radius: var(--radius);
  border: 1px solid;
  font-size: 0.75rem;
  cursor: pointer;
  font-family: var(--font-ui);
  transition: background 0.1s;
}
#btn-save-var {
  background: rgba(90, 158, 114, 0.2);
  border-color: rgba(90, 158, 114, 0.5);
  color: var(--green);
}
#btn-save-var:hover { background: rgba(90, 158, 114, 0.35); }
#btn-cancel-explore {
  background: rgba(180, 50, 50, 0.15);
  border-color: rgba(180, 50, 50, 0.4);
  color: var(--red);
}
#btn-cancel-explore:hover { background: rgba(180, 50, 50, 0.3); }

/* ─── Engine panel ───────────────────────────────────────────────────── */
#engine-panel {
  width: 100%;
  max-width: 560px;
  margin-top: 8px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  flex-shrink: 0;
}

#engine-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  gap: 8px;
  background: var(--bg-card);
}

#engine-title {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

#engine-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

#engine-toggle-label {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.75rem;
  color: var(--text-secondary);
  cursor: pointer;
  user-select: none;
}
#engine-toggle {
  accent-color: var(--gold);
  cursor: pointer;
  width: 14px;
  height: 14px;
}

#engine-lines-control {
  display: flex;
  align-items: center;
  gap: 4px;
}
#engine-lines-count {
  font-size: 0.72rem;
  color: var(--text-muted);
  min-width: 42px;
  text-align: center;
}
.engine-lines-btn {
  width: 22px !important;
  height: 22px !important;
  font-size: 0.9rem !important;
  padding: 0 !important;
}

#engine-depth-info {
  font-size: 0.7rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
  min-width: 50px;
  text-align: right;
}

#engine-lines {
  padding: 4px 0;
}

.engine-line {
  display: grid;
  grid-template-columns: 38px 52px 1fr;
  align-items: baseline;
  gap: 0 6px;
  padding: 4px 10px;
  font-size: 0.78rem;
  line-height: 1.5;
  border-bottom: 1px solid var(--border);
}
.engine-line:last-child { border-bottom: none; }

.engine-line-rank {
  font-size: 0.68rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
}
.engine-line-score {
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 0.82rem;
  text-align: right;
}
.engine-line-score.positive { color: #6dbf85; }
.engine-line-score.negative { color: #d46060; }
.engine-line-score.even     { color: var(--text-secondary); }
.engine-line-score.mate     { color: #e8a44a; }

.engine-line-moves {
  color: var(--text-primary);
  font-family: var(--font-display);
  font-size: 0.82rem;
  word-break: break-word;
}

.engine-thinking {
  padding: 8px 10px;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-style: italic;
  text-align: center;
}
.engine-off {
  padding: 8px 10px;
  color: var(--text-muted);
  font-size: 0.75rem;
  text-align: center;
}



/* ─── Right sidebar: tabs ────────────────────────────────────────────── */
#sidebar-right {
  width: 280px;
  min-width: 240px;
  min-height: 0;
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
}

.tab-bar {
  display: flex;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.tab-btn {
  flex: 1;
  padding: 9px 4px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  font-family: var(--font-ui);
  font-size: 0.72rem;
  font-weight: 500;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  transition: color 0.1s, border-color 0.1s;
}
.tab-btn:hover { color: var(--text-secondary); }
.tab-btn.active { color: var(--gold); border-bottom-color: var(--gold); }

.tab-panel { display: none; flex: 1; overflow: hidden; flex-direction: column; }
.tab-panel.active { display: flex; min-height: 0; }

/* ─── PGN tab (moves + annotation) ──────────────────────────────────── */
#tab-pgn {
  overflow: hidden;
}
#tab-pgn.active {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#move-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 10px 12px;
  font-family: var(--font-display);
  font-size: 1rem;
  line-height: 1.9;
}

.move-num {
  color: var(--text-muted);
  font-size: 0.82rem;
  margin-right: 2px;
  font-style: italic;
}

.move-token {
  display: inline-block;
  padding: 1px 5px;
  border-radius: 3px;
  cursor: pointer;
  color: var(--text-primary);
  margin-right: 2px;
  transition: background 0.1s, color 0.1s;
}
.move-token:hover { background: var(--bg-hover); color: var(--text-active); }
.move-token.active {
  background: var(--bg-active);
  color: var(--text-active);
  font-weight: 600;
}
.move-token.explore-token { color: var(--explore); font-style: italic; }

.move-annotation {
  display: block;
  font-size: 0.8rem;
  color: var(--green);
  font-style: italic;
  padding: 2px 6px 4px;
  line-height: 1.4;
  font-family: var(--font-ui);
}

.variation-block {
  display: block;
  margin: 3px 0 3px 12px;
  padding: 3px 8px;
  border-left: 2px solid var(--border-light);
  font-size: 0.88rem;
  color: var(--text-secondary);
  background: rgba(0,0,0,0.15);
  border-radius: 0 3px 3px 0;
}
.variation-move { color: var(--text-secondary); font-size: 0.88rem; }
.explore-line { margin-top: 4px; font-style: italic; color: var(--explore); }

/* ─── Annotation section (within PGN tab) ───────────────────────────── */
#annotate-section {
  flex-shrink: 0;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.ann-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 4px;
  font-weight: 500;
}

#annotation-text {
  width: 100%;
  height: 90px;
  resize: vertical;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-family: var(--font-ui);
  font-size: 0.82rem;
  padding: 8px;
  outline: none;
  line-height: 1.5;
  transition: border-color 0.15s;
}
#annotation-text:focus { border-color: var(--gold-dim); }
#annotation-text::placeholder { color: var(--text-muted); }

.nag-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}

#nag-select {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.82rem;
  padding: 5px 8px;
  outline: none;
  cursor: pointer;
}
#nag-select:focus { border-color: var(--gold-dim); }
#nag-select option { background: var(--bg-card); }

/* ─── Info tab ───────────────────────────────────────────────────────── */
#tab-info {
  padding: 12px;
  gap: 8px;
  overflow-y: auto;
}

.meta-row {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.meta-row label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-muted);
  font-weight: 500;
}
.meta-row input, .meta-row select {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.82rem;
  padding: 5px 8px;
  outline: none;
  transition: border-color 0.15s;
  font-family: var(--font-ui);
}
.meta-row input:focus, .meta-row select:focus { border-color: var(--gold-dim); }

/* ─── Import tab ─────────────────────────────────────────────────────── */
#tab-import {
  padding: 12px;
  gap: 10px;
  overflow-y: auto;
}

#pgn-input {
  width: 100%;
  height: 140px;
  resize: vertical;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  padding: 8px;
  outline: none;
  line-height: 1.4;
  transition: border-color 0.15s;
}
#pgn-input:focus { border-color: var(--gold-dim); }
#pgn-input::placeholder { color: var(--text-muted); font-family: var(--font-ui); }

.file-upload-area {
  border: 1px dashed var(--border-light);
  border-radius: var(--radius);
  padding: 12px;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.78rem;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.file-upload-area:hover { border-color: var(--gold-dim); background: var(--bg-hover); }
.file-upload-area label { cursor: pointer; }
.file-upload-area input[type="file"] { display: none; }

/* ─── Export tab ─────────────────────────────────────────────────────── */
#tab-export {
  padding: 12px;
  gap: 8px;
  overflow-y: auto;
}

.theme-toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.theme-toggle-row span {
  font-size: 0.82rem;
  color: var(--text-secondary);
}

/* ─── Settings tab ───────────────────────────────────────────────────── */
#tab-settings {
  padding: 12px;
  gap: 8px;
  overflow-y: auto;
}

/* ─── Buttons ────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: var(--radius);
  font-family: var(--font-ui);
  font-size: 0.78rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.1s, border-color 0.1s;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-primary);
  width: 100%;
}
.btn:hover { background: var(--bg-hover); border-color: var(--border-light); }
.btn:active { background: var(--bg-active); }
.btn-primary {
  background: rgba(200, 169, 110, 0.15);
  border-color: var(--gold-dim);
  color: var(--gold);
}
.btn-primary:hover { background: rgba(200, 169, 110, 0.25); }
.btn-green {
  background: rgba(90, 158, 114, 0.15);
  border-color: rgba(90, 158, 114, 0.4);
  color: var(--green);
}
.btn-green:hover { background: rgba(90, 158, 114, 0.28); }

/* ─── Modals ─────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(2px);
}
.modal-overlay.hidden { display: none; }

.modal-box {
  background: var(--bg-panel);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-lg);
  padding: 20px;
  min-width: 300px;
  max-width: 460px;
  box-shadow: var(--shadow-lg);
  /* Bounds the box to the viewport and makes it a flex column so a
     sticky footer is possible: anything NOT wrapped in .modal-scroll
     (the heading, and the close/action button row) is a plain flex
     child that sizes to its own content rather than growing/scrolling,
     so it stays pinned in place while only .modal-scroll's contents
     scroll. Without this, a modal taller than the viewport would just
     overflow past the top/bottom of the screen with nothing able to
     reach it — see .modal-scroll below. */
  max-height: 90vh;
  max-height: 90dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
/* Wrap a modal's variable/long content in this — everything outside it
   (heading, footer buttons) stays pinned; only this region scrolls. Only
   needed on modals whose content can plausibly grow long (lists, forms,
   settings) — short fixed-content modals don't need it, though they
   still inherit the safety cap above so they can never overflow either. */
.modal-scroll {
  overflow-y: auto;
  min-height: 0;
}
.modal-box h3 {
  font-family: var(--font-display);
  font-size: 1.1rem;
  color: var(--gold);
  margin-bottom: 14px;
}

/* Promotion modal */
#promo-pieces {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin: 8px 0 14px;
}
.promo-btn {
  width: 64px;
  height: 64px;
  background: var(--sq-light);
  border: 2px solid var(--border-light);
  border-radius: var(--radius);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.1s, transform 0.1s;
}
.promo-btn:hover { background: var(--sq-light-hl); transform: translateY(-2px); }
.promo-btn svg { width: 48px; height: 48px; }

/* Export modal */
#export-game-list {
  max-height: 200px;
  overflow-y: auto;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  margin: 8px 0;
}
.export-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px;
  font-size: 0.8rem;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: 3px;
  transition: background 0.1s;
}
.export-row:hover { background: var(--bg-hover); }
.export-row input { accent-color: var(--gold); cursor: pointer; }

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}
.modal-actions .btn { width: auto; flex: 1; }

.select-row {
  display: flex;
  gap: 8px;
  margin-bottom: 2px;
}
.select-row .btn { padding: 4px 8px; font-size: 0.72rem; }

/* ─── Toast ──────────────────────────────────────────────────────────── */
#toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  color: var(--text-primary);
  padding: 8px 18px;
  border-radius: 20px;
  font-size: 0.8rem;
  box-shadow: var(--shadow);
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
  z-index: 200;
  white-space: nowrap;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { border-color: var(--red); color: #e08080; }

/* ─── Scrollbars ─────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: var(--bg-panel); }
::-webkit-scrollbar-thumb { background: var(--border-light); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

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

.delete-all-btn {
  margin: 8px 12px;
  width: calc(100% - 24px);
  font-size: 0.72rem;
  color: var(--red);
  border-color: rgba(180,50,50,0.3);
  background: rgba(180,50,50,0.06);
}
.delete-all-btn:hover {
  background: rgba(180,50,50,0.15);
  border-color: var(--red);
}

/* ─── Game selection mode ────────────────────────────────────────────── */
#game-select-toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#game-select-toolbar.hidden { display: none; }

.game-item.selectable {
  padding-left: 8px;
  display: grid;
  grid-template-columns: 20px 1fr auto;
  align-items: center;
  gap: 6px;
}
.game-item.selectable .game-item-text { min-width: 0; }
.game-item input[type="checkbox"] {
  accent-color: var(--gold);
  cursor: pointer;
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}
.game-item.selected { background: var(--bg-active); border-left-color: var(--gold-dim); }

.btn-select-active {
  background: rgba(200,169,110,0.2) !important;
  border-color: var(--gold-dim) !important;
  color: var(--gold) !important;
}
.section-sep {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}
.small-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-muted);
  font-weight: 500;
  margin-bottom: 4px;
}

/* ─── Panel collapse handle (mobile only) ───────────────────────────── */
#panel-handle {
  display: none;
  align-items: center;
  justify-content: center;
  height: 28px;
  cursor: pointer;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  background: var(--bg-card);
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
#panel-handle-icon {
  font-size: 1rem;
  color: var(--text-muted);
  line-height: 1;
  transition: transform 0.2s;
}
/* ▴ when expanded = tap to collapse; ▾ when collapsed = tap to expand */
#sidebar-right.panel-expanded #panel-handle-icon { transform: rotate(180deg); }
/* Hide annotation section and tab/panel content when panel-collapsed class is applied */
#sidebar-right.panel-collapsed .tab-bar,
#sidebar-right.panel-collapsed .tab-panel { display: none; }

/* ═══════════════════════════════════════════════════════════════════════
   RESPONSIVE — mobile-first
   ≤767px  : phone  — single column, board top, bottom sheet tabs
   768–1023px: tablet — drawer sidebar, board + right panel stacked
   ≥1024px : desktop — full three-column layout (default above)
   ═══════════════════════════════════════════════════════════════════════ */

/* ─── Tablet (768–1023px) ────────────────────────────────────────────── */
@media (max-width: 1023px) and (min-width: 768px) {
  #btn-menu { display: flex; }
  #sidebar-left {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    z-index: 50;
    width: 260px;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: var(--shadow-lg);
  }
  #sidebar-left.drawer-open { transform: translateX(0); }
  #drawer-overlay {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 49;
  }
  #drawer-overlay.active { display: block; }
  #sidebar-right { width: 260px; min-width: 220px; }
  #board-wrap {
    width: min(
      calc(100vh - 96px - var(--engine-panel-h) - 90px),
      calc(100vw - 260px - 32px),
      480px
    );
  }
}

/* ─── Phone (≤767px) ─────────────────────────────────────────────────── */
@media (max-width: 767px) {
  /* ── Header ── */
  #app-header {
    min-height: 60px;
    padding: 6px 10px;
    gap: 8px;
  }
  #btn-menu { display: flex; }
  #app-logo { height: 42px; }
  #logo-plate { padding: 4px 8px; border-radius: 8px; }

  /* ── Drawer ── */
  #sidebar-left {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    z-index: 50;
    width: 80vw;
    max-width: 300px;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: var(--shadow-lg);
  }
  #sidebar-left.drawer-open { transform: translateX(0); }
  #drawer-overlay {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 49;
  }
  #drawer-overlay.active { display: block; }

  /* ── Layout: true flex column filling the screen ── */
  body { overflow: hidden; }
  #main-layout {
    flex-direction: column;
    overflow: hidden;
  }

  /* ── Center col: shrinks to exactly fit board + controls + engine ── */
  #center-col {
    flex: 0 0 auto;          /* don't flex-grow; let sidebar-right take rest */
    padding: 6px 8px 0;
    align-items: center;
    min-height: 0;
    overflow: visible;
  }

  /* Board: square that fills available width */
  #board-wrap {
    width: calc(100vw - 16px);
    max-width: 480px;
  }

  #controls-bar { margin-top: 6px; }
  #status-line  { font-size: 0.7rem; margin-top: 3px; }
  #engine-panel { max-width: 100%; margin-top: 6px; }

  /* ── Right sidebar: flex-grows to fill remaining space ── */
  #sidebar-right {
    flex: 1;
    min-height: 0;
    width: 100%;
    min-width: unset;
    border-left: none;
    border-top: 2px solid var(--border);
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  /* Panel collapse handle: visible on mobile */
  #panel-handle { display: flex; }

  /* EXPANDED state: panel flies up and covers the board */
  #sidebar-right.panel-expanded {
    position: fixed;
    left: 0; right: 0;
    bottom: 0;
    /* top of header so it covers the board but not the header */
    top: 60px;
    z-index: 40;
    border-top: 2px solid var(--border);
    box-shadow: 0 -4px 20px rgba(0,0,0,0.2);
    flex: unset;
    height: auto;
  }

  /* In expanded state, hide the annotation editor on PGN tab
     (more room for the move list) */
  #sidebar-right:not(.panel-expanded) #annotate-section { display: none; }

  /* Handle icon: ▴ when expanded (tap to collapse back), ▾ when collapsed */
  #sidebar-right.panel-expanded #panel-handle-icon { transform: rotate(180deg); }

  /* Tabs and panels scroll inside */
  .tab-btn { font-size: 0.62rem; padding: 8px 2px; letter-spacing: 0.02em; }
  #move-list { font-size: 0.9rem; }
  #annotate-section { padding: 8px 10px; gap: 6px; }
  #annotation-text { height: 80px; }
  .settings-grid { grid-template-columns: repeat(3, 1fr); gap: 5px; }
  .swatch-piece { width: 24px; height: 24px; }
  .swatch-board { width: 24px; height: 24px; }
  .swatch-label { font-size: 0.55rem; }
  .modal-box { min-width: unset; width: 90vw; padding: 16px; }
}

/* ─── Very small phones (≤374px) ────────────────────────────────────── */
@media (max-width: 374px) {
  .tab-btn { font-size: 0.55rem; padding: 7px 1px; }
  #app-logo { height: 34px; }
}
