:root {
  --bg: #0a111b;
  --panel: #111c2b;
  --panel-2: #0d1620;
  --line: #1e2e42;
  --text: #e6edf5;
  --muted: #8aa0b8;
  --accent: #5fcf6b;
  --accent-2: #5fa8ff;
  --danger: #e0635a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  height: auto;
  padding: 14px 18px;
  gap: 12px;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.header-left { flex: 1 1 auto; min-width: 0; }
header h1 { margin: 0; font-size: 22px; }
header .sub { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

/* --- auth area (top-right of header) --- */
.auth-area {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.auth-user {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
}
.auth-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #1a2b40;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 7px 13px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, border-color .15s;
}
.auth-btn:hover { background: #213650; }
.auth-btn.muted { color: var(--muted); }
.auth-btn.google { border-color: #4285f4; }
.auth-btn.google:hover { background: #1a2b4a; border-color: #5a9af8; }
.auth-btn.apple { border-color: #aaa; }
.auth-btn.apple:hover { background: #22222a; }

main {
  display: flex;
  gap: 14px;
  flex: 0 0 60vh;
  min-height: 520px;
  max-height: 700px;
}

.stage-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
  min-height: 0;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}

#stage {
  flex: 1;
  width: 100%;
  min-height: 0;
  display: block;
}

/* right-hand live leaderboard: one horizontal bar per car, furthest on top */
.leaderboard {
  flex: 0 0 280px;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
}
.bars-head {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin-bottom: 8px;
}
.bars-head small { text-transform: none; letter-spacing: 0; opacity: .7; }
#bars { flex: 1; width: 100%; min-height: 0; display: block; }

.toolbar {
  display: flex;
  gap: 8px;
  padding: 10px;
  border-top: 1px solid var(--line);
  background: var(--panel);
  flex-wrap: wrap;
}

.btn {
  background: #1a2b40;
  color: var(--text);
  border: 1px solid var(--line);
  padding: 8px 14px;
  border-radius: 7px;
  font-size: 14px;
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: #213650; }
.btn.primary { border-color: var(--accent); }
.btn.primary.active { background: #1d3a26; border-color: var(--accent); }
.btn.danger:hover { background: #3a1f1f; border-color: var(--danger); }

/* bottom settings strip: control groups laid out in horizontal columns */
.settings {
  flex: 0 0 auto;
  max-height: none;
  overflow-y: auto;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 24px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 18px;
}
.group {
  flex: 1 1 230px;
  min-width: 210px;
  max-width: 340px;
}
.group h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin: 14px 0 8px;
}
.group h2:first-child { margin-top: 0; }
.group h2 small { text-transform: none; letter-spacing: 0; font-size: 11px; opacity: .7; }

.stats { display: grid; grid-template-columns: 1fr auto; gap: 6px 10px; margin: 0; }
.stats dt { color: var(--muted); font-size: 13px; }
.stats dd { margin: 0; font-variant-numeric: tabular-nums; font-weight: 600; text-align: right; font-size: 13px; }

.chart { width: 100%; height: 96px; border-radius: 8px; border: 1px solid var(--line); }

.legend { display: flex; gap: 16px; margin-top: 6px; font-size: 12px; color: var(--muted); }
.legend .dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 4px; vertical-align: middle; }
.legend .dot.best { background: var(--accent); }
.legend .dot.avg { background: var(--accent-2); }

.slider { display: block; font-size: 13.5px; color: var(--muted); margin-bottom: 14px; }
.slider span { color: var(--text); font-weight: 700; float: right; font-variant-numeric: tabular-nums; }
.slider input { width: 100%; margin-top: 7px; height: 6px; accent-color: var(--accent); cursor: pointer; }

/* segmented toggle (color mode) */
.seg { display: flex; gap: 6px; }
.seg-btn {
  flex: 1; padding: 7px 8px; font-size: 13px; cursor: pointer;
  background: #1a2b40; color: var(--text);
  border: 1px solid var(--line); border-radius: 7px;
}
.seg-btn.active { background: #1d3a26; border-color: var(--accent); }

/* mass color legend bar */
.massbar { display: flex; align-items: center; gap: 8px; margin-top: 8px; font-size: 11px; color: var(--muted); }
.massbar i {
  flex: 1; height: 10px; border-radius: 5px;
  background: linear-gradient(90deg, hsl(220,75%,55%), hsl(140,75%,55%), hsl(60,75%,55%), hsl(0,75%,55%));
}

/* small standalone field label (e.g. above the end-mode toggle) */
.field-label { font-size: 13px; color: var(--muted); margin: 4px 0 6px; }

/* disabled live slider (e.g. generation time while in "until stop" mode) */
.slider.disabled { opacity: .4; pointer-events: none; }

/* dual-thumb min/max range slider */
.dual-field { margin-bottom: 14px; }
.dual-head { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.dual-head b { color: var(--text); font-variant-numeric: tabular-nums; }
.dual { position: relative; height: 22px; }
.dual-rail, .dual-fill { position: absolute; top: 9px; height: 4px; border-radius: 2px; }
.dual-rail { left: 0; right: 0; background: var(--line); }
.dual-fill { background: var(--accent); }
.dual input {
  position: absolute; top: 0; left: 0; width: 100%; height: 22px; margin: 0;
  background: none; pointer-events: none; -webkit-appearance: none; appearance: none;
  outline: none;
}
.dual input::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; pointer-events: auto;
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--accent); border: 2px solid #0a111b; cursor: pointer;
}
.dual input::-moz-range-thumb {
  pointer-events: auto; width: 14px; height: 14px; border-radius: 50%;
  background: var(--accent); border: 2px solid #0a111b; cursor: pointer;
}
.dual input.lo { z-index: 4; }
.dual input.hi { z-index: 3; }
.dual input:hover, .dual input:focus { z-index: 6; } /* raise whichever thumb you reach for */

/* seed rows */
.seed-row { display: flex; align-items: center; gap: 6px; margin-bottom: 10px; flex-wrap: wrap; }
.seed-row label { font-size: 13px; color: var(--muted); width: 64px; }
.seed-input {
  flex: 1; min-width: 90px; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.mini {
  background: #1a2b40; color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 9px; cursor: pointer; font-size: 13px;
}
.mini:hover { background: #213650; }
.mini.wide { padding: 6px 12px; }
.seed-row .hint { font-size: 11px; color: var(--muted); width: 100%; }

.help { margin-top: 14px; font-size: 13px; color: var(--muted); }
.help summary { cursor: pointer; color: var(--text); }
.help p { line-height: 1.5; }

/* genome inspector in the leaderboard panel */
.inspector-head {
  font-size: 11px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--muted); margin: 10px 0 6px;
}
.inspector-canvas { width: 100%; height: 110px; display: block; border-radius: 6px; border: 1px solid var(--line); }
.inspector-text { font-size: 11px; color: var(--muted); margin-top: 6px; line-height: 1.5; min-height: 1.5em; }

/* save / load row in Seeds panel */
.save-row { display: flex; gap: 8px; margin: 10px 0; }
.save-row .mini { flex: 1; text-align: center; }

/* state dialogs */
dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 20px;
  min-width: 340px;
  max-width: 500px;
  width: min(94vw, 460px);
}
dialog::backdrop { background: rgba(0,0,0,0.65); }
dialog h3 { margin: 0 0 6px; font-size: 15px; }
.dialog-hint { margin: 0 0 12px; font-size: 13px; color: var(--muted); }
dialog textarea {
  width: 100%; height: 180px; display: block;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 6px;
  padding: 10px; font-size: 12px; font-family: monospace;
  resize: vertical;
}
.dialog-btns { display: flex; gap: 8px; margin-top: 12px; justify-content: flex-end; }

/* --- leaderboard dialog --- */
#leaderboardDialog {
  min-width: 360px;
  max-width: 540px;
  width: min(96vw, 520px);
}
.lb-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.lb-header h3 { margin: 0; font-size: 17px; }
.lb-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.lb-table th {
  text-align: left;
  color: var(--muted);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .06em;
  padding: 4px 8px 8px;
  border-bottom: 1px solid var(--line);
}
.lb-table td {
  padding: 7px 8px;
  border-bottom: 1px solid rgba(30,46,66,.6);
  font-variant-numeric: tabular-nums;
}
.lb-table td.lb-rank { color: var(--muted); width: 28px; }
.lb-table td.lb-dist { font-weight: 700; color: var(--accent); }
.lb-table td.lb-date { color: var(--muted); font-size: 11px; }
.lb-table tr.lb-me td { background: rgba(95,207,107,.07); }
.lb-table tr.lb-me td:first-child { border-left: 3px solid var(--accent); }
.lb-loading { text-align: center; padding: 24px; color: var(--muted); }
.lb-submit-row { margin-top: 14px; }

@media (max-width: 860px) {
  main { flex-direction: column; }
  .leaderboard { flex: 0 0 150px; }
  .settings { max-height: 44vh; }
  .auth-area { gap: 6px; }
  .auth-btn { padding: 6px 10px; font-size: 12px; }
}
