:root {
  color-scheme: dark;
  --bg: #030303;
  --bg-2: #0b0b0b;
  --surface: rgba(18, 18, 18, 0.76);
  --surface-strong: rgba(24, 24, 24, 0.92);
  --surface-soft: rgba(255, 255, 255, 0.045);
  --text: #f4f4f4;
  --muted: #a3a3a3;
  --muted-2: #737373;
  --accent: #f5f5f5;
  --accent-2: #d4d4d4;
  --good: #e5e5e5;
  --bad: #a3a3a3;
  --warn: #c7c7c7;
  --border: rgba(245, 245, 245, 0.15);
  --border-strong: rgba(245, 245, 245, 0.3);
  --shadow: 0 24px 80px rgba(0, 0, 0, 0.42);
  --radius-lg: 28px;
  --radius-md: 18px;
  --radius-sm: 12px;
}

* { box-sizing: border-box; }

html { min-height: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at 18% -10%, rgba(255, 255, 255, 0.075), transparent 30rem),
    radial-gradient(circle at 88% 0%, rgba(255, 255, 255, 0.045), transparent 28rem),
    linear-gradient(135deg, var(--bg), var(--bg-2) 48%, #050505);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, sans-serif;
  letter-spacing: -0.01em;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(255,255,255,0.028) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.028) 1px, transparent 1px);
  background-size: 44px 44px;
  mask-image: linear-gradient(to bottom, rgba(0,0,0,0.75), transparent 70%);
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1.25rem;
  padding: 1.15rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: rgba(3, 3, 3, 0.78);
  backdrop-filter: blur(18px);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand { display: grid; gap: 0.18rem; }

.eyebrow {
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

h1, h2, p { margin: 0; }

h1 {
  font-size: clamp(1.45rem, 2.2vw, 2.15rem);
  line-height: 1.05;
  font-weight: 800;
}

h2 {
  font-size: 1.02rem;
  line-height: 1.2;
  font-weight: 750;
}

p {
  color: var(--muted);
  line-height: 1.55;
}

.tabs {
  display: flex;
  gap: 0.25rem;
  padding: 0.28rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: rgba(255,255,255,0.045);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.05);
}

button, select {
  font: inherit;
}

button {
  border: 1px solid transparent;
  background: rgba(255, 255, 255, 0.055);
  color: var(--text);
  border-radius: 999px;
  padding: 0.72rem 0.95rem;
  cursor: pointer;
  font-weight: 700;
  line-height: 1;
  transition: transform 150ms ease, border-color 150ms ease, background 150ms ease, color 150ms ease, box-shadow 150ms ease;
}

button:hover {
  transform: translateY(-1px);
  border-color: var(--border-strong);
  background: rgba(255,255,255,0.09);
}

button:active { transform: translateY(0); }
button:focus-visible, select:focus-visible, input:focus-visible {
  outline: 2px solid rgba(245, 245, 245, 0.72);
  outline-offset: 3px;
}

.tabs button {
  color: var(--muted);
  background: transparent;
  padding-inline: 1rem;
}

button.active, .tabs button.active {
  background: #f5f5f5;
  color: #050505;
  box-shadow: 0 10px 30px rgba(245, 245, 245, 0.1);
}

button.primary {
  border: 1px solid rgba(255, 255, 255, 0.82);
  background: #f5f5f5;
  color: #050505;
  box-shadow: 0 18px 34px rgba(255, 255, 255, 0.08);
}

button.running {
  border-color: rgba(255, 255, 255, 0.72);
  background: #d4d4d4;
  color: #050505;
}

select {
  width: 100%;
  border: 1px solid var(--border);
  background: rgba(7, 11, 18, 0.72);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 0.78rem 0.85rem;
}

input[type="range"] {
  width: 100%;
  accent-color: #e5e5e5;
}

.view { display: none; }
.view.active { display: block; }

#simulation-view.active {
  display: grid;
  grid-template-columns: minmax(640px, 1fr) minmax(360px, 420px);
  gap: 1.25rem;
  padding: 1.25rem;
  height: calc(100dvh - 96px);
}

.sim-panel {
  min-height: 520px;
  position: relative;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: #030303;
  box-shadow: var(--shadow), inset 0 1px 0 rgba(255,255,255,0.06);
}

.sim-panel::after {
  content: "Drag to orbit · Scroll to zoom · Click a car to follow";
  position: absolute;
  left: 1rem;
  bottom: 1rem;
  z-index: 1;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: rgba(3, 7, 17, 0.58);
  backdrop-filter: blur(16px);
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 650;
  padding: 0.55rem 0.8rem;
  pointer-events: none;
}

#viewport { width: 100%; height: 100%; }
#viewport canvas { display: block; width: 100% !important; height: 100% !important; }

.hud {
  position: absolute;
  left: 1rem;
  right: 1rem;
  top: 1rem;
  z-index: 1;
  display: flex;
  justify-content: space-between;
  gap: 0.8rem;
  pointer-events: none;
}

.hud span {
  max-width: 64%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  background: rgba(3, 7, 17, 0.62);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: 0 10px 28px rgba(0,0,0,0.2);
  backdrop-filter: blur(16px);
  padding: 0.58rem 0.82rem;
  color: #e5e5e5;
  font-size: 0.84rem;
  font-weight: 700;
}

.side-panel {
  min-height: 0;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding-right: 0.15rem;
  scrollbar-width: thin;
  scrollbar-color: rgba(148,163,184,0.35) transparent;
}

.card {
  background: linear-gradient(180deg, var(--surface-strong), var(--surface));
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1rem;
  box-shadow: var(--shadow), inset 0 1px 0 rgba(255,255,255,0.055);
  backdrop-filter: blur(18px) saturate(140%);
}

.card-heading {
  display: grid;
  gap: 0.22rem;
  margin-bottom: 0.9rem;
}

.controls { display: grid; gap: 0.9rem; }

.button-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.55rem;
}

.button-grid button {
  border-radius: var(--radius-md);
  min-height: 2.75rem;
}

.button-grid .wide { grid-column: 1 / -1; }

.field {
  display: grid;
  gap: 0.5rem;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 650;
}

.field span {
  display: flex;
  justify-content: space-between;
  gap: 0.7rem;
}

.field strong {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.cars-list { display: grid; gap: 0.7rem; }

.car-card {
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.04);
  border-radius: 20px;
  padding: 0.72rem;
  transition: transform 150ms ease, border-color 150ms ease, background 150ms ease;
}

.car-card:hover {
  transform: translateY(-1px);
  border-color: var(--border-strong);
  background: rgba(255,255,255,0.062);
}

.car-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.25rem;
}

.car-top strong { font-size: 0.94rem; }
.badge { font-size: 0.76rem; color: var(--muted); line-height: 1.45; }
.metric { color: #f5f5f5; font-variant-numeric: tabular-nums; font-weight: 800; }
.car-card.done .metric { color: #d4d4d4; }
.car-card.crashed .metric { color: #a3a3a3; }

.car-svg {
  width: 100%;
  height: 78px;
  margin: 0.55rem 0;
  background: rgba(3, 7, 17, 0.55);
  border: 1px solid rgba(148,163,184,0.12);
  border-radius: 14px;
}

details { color: var(--muted); }
summary { cursor: pointer; font-weight: 650; }

pre {
  max-height: 360px;
  overflow: auto;
  background: rgba(3, 7, 17, 0.62);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 0.85rem;
  color: #d4d4d4;
  white-space: pre-wrap;
  font-size: 0.78rem;
  line-height: 1.5;
}

.random-layout {
  display: grid;
  grid-template-columns: minmax(420px, 0.78fr) minmax(360px, 1fr);
  gap: 1.25rem;
  padding: 1.25rem;
}

.random-stage { min-height: 520px; }
.random-stage > p { margin-bottom: 1rem; }
.large-svg svg {
  width: 100%;
  height: 360px;
  background: rgba(3, 7, 17, 0.62);
  border: 1px solid var(--border);
  border-radius: 22px;
  margin-top: 1rem;
}
.gene-card pre { max-height: calc(100vh - 220px); }

.leaderboard-layout {
  padding: 1.25rem;
}

.leaderboard-card > p { margin-bottom: 1rem; }

.leaderboard-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 1rem;
}

.leaderboard-map {
  border: 1px solid var(--border);
  border-radius: 22px;
  background: rgba(255,255,255,0.035);
  padding: 0.85rem;
}

.leaderboard-map-heading {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.75rem;
  margin-bottom: 0.7rem;
}

.leaderboard-map-heading h3 {
  margin: 0;
  font-size: 1rem;
}

.leaderboard-map-heading span {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.leaderboard-entry {
  display: grid;
  grid-template-columns: 3rem minmax(120px, 180px) 1fr;
  gap: 0.7rem;
  align-items: center;
  padding: 0.65rem 0;
  border-top: 1px solid rgba(255,255,255,0.08);
}

.leaderboard-rank {
  color: #050505;
  background: #f5f5f5;
  border-radius: 999px;
  padding: 0.38rem 0.55rem;
  text-align: center;
  font-weight: 850;
  font-variant-numeric: tabular-nums;
}

.leaderboard-preview .car-svg {
  margin: 0;
  height: 64px;
}

.leaderboard-meta {
  display: grid;
  gap: 0.18rem;
  min-width: 0;
}

.leaderboard-meta strong {
  font-size: 0.92rem;
}

.leaderboard-meta span {
  color: var(--muted);
  font-size: 0.76rem;
  line-height: 1.35;
}

.genealogy-layout {
  display: grid;
  grid-template-columns: minmax(640px, 1fr) 340px;
  gap: 1.25rem;
  padding: 1.25rem;
  height: calc(100dvh - 96px);
}

.genealogy-card, .legend-card { min-height: 0; overflow: auto; }
.genealogy-card > p { margin-bottom: 1rem; }

.genealogy-tree {
  overflow: auto;
  min-height: 560px;
  background: rgba(3, 7, 17, 0.58);
  border: 1px solid var(--border);
  border-radius: 22px;
}
.genealogy-tree svg { min-width: 100%; display: block; }
.gene-node { cursor: pointer; }
.gene-node.removed { opacity: 0.34; }
.gene-node text { font-size: 10px; fill: #e5e5e5; pointer-events: none; }
.gene-edge { fill: none; stroke: #737373; stroke-width: 1.4; opacity: 0.72; }
.gene-edge.elite { stroke: #f5f5f5; stroke-width: 2; }
.gene-edge.copy, .gene-edge.copy-mutation { stroke: #d4d4d4; }
.gene-edge.crossover, .gene-edge.crossover-mutation { stroke: #a3a3a3; }

.legend-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin: 0.52rem 0;
  color: var(--muted);
  font-weight: 650;
}
.legend-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  display: inline-block;
  box-shadow: 0 0 0 5px rgba(255,255,255,0.04);
}
.legend-dot.random { background: #737373; }
.legend-dot.elite { background: #f5f5f5; }
.legend-dot.copy { background: #d4d4d4; }
.legend-dot.crossover { background: #a3a3a3; }
.small-note { font-size: 0.85rem; margin: 0.9rem 0; }

@media (max-width: 1050px) {
  .app-header { align-items: flex-start; flex-direction: column; }
  .tabs { width: 100%; overflow: auto; }
  .tabs button { flex: 1; }
  #simulation-view.active { grid-template-columns: 1fr; height: auto; }
  .sim-panel { height: 62vh; min-height: 420px; }
  .random-layout, .genealogy-layout { grid-template-columns: 1fr; height: auto; }
  .leaderboard-entry { grid-template-columns: 2.8rem 1fr; }
  .leaderboard-preview { grid-column: 1 / -1; }
}

@media (max-width: 620px) {
  .app-header, #simulation-view.active, .random-layout, .genealogy-layout, .leaderboard-layout { padding: 0.9rem; }
  .card, .sim-panel { border-radius: 22px; }
  .button-grid { grid-template-columns: 1fr; }
  .button-grid .wide { grid-column: auto; }
  .hud { flex-direction: column; align-items: flex-start; }
  .hud span { max-width: 100%; }
  .sim-panel::after { display: none; }
}
