/* ==========================================================================
   Self-Driving Car AI — vanilla CSS, themed via custom properties
   ========================================================================== */

:root {
  --bg: #0b0d12;
  --bg-elev: #131722;
  --panel: rgba(19, 23, 34, 0.85);
  --panel-border: rgba(255, 255, 255, 0.08);
  --road: #2b2f38;
  --lane: rgba(255, 255, 255, 0.85);
  --border: rgba(255, 255, 255, 0.95);
  --text: #e6e9ef;
  --text-dim: #9aa0ad;
  --accent: #3a86ff;
  --accent-2: #39d98a;
  --warn: #ffb547;
  --danger: #ff4d4f;

  --car-best: #3a86ff;
  --car-ai: #6ea8ff;
  --car-traffic: #d62828;
  --car-damaged: #ffd400;
  --car-windshield: rgba(255, 255, 255, 0.18);

  --sensor-beam: #ffd76a;
  --sensor-hit: #ffd76a;
  --sensor-danger: #ff4d4f;

  --node-bg: #0b0d12;
  --node-label: #ffffff;

  --chart-bg: rgba(11, 13, 18, 0.78);
  --chart-grid: rgba(255, 255, 255, 0.07);
  --chart-best: #39d98a;
  --chart-avg: rgba(255, 255, 255, 0.45);
  --chart-text: rgba(255, 255, 255, 0.85);

  --trail: rgba(58, 134, 255, 0.35);

  --shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
  --radius: 12px;
}

html[data-theme="light"] {
  --bg: #f4f6fb;
  --bg-elev: #ffffff;
  --panel: rgba(255, 255, 255, 0.94);
  --panel-border: rgba(0, 0, 0, 0.08);
  --road: #c9cfdb;
  --lane: rgba(20, 24, 35, 0.85);
  --border: rgba(20, 24, 35, 0.95);
  --text: #14202b;
  --text-dim: #4f5b6c;
  --accent: #2563eb;
  --accent-2: #16a34a;
  --car-best: #2563eb;
  --car-ai: #93b4f5;
  --car-traffic: #b91c1c;
  --car-damaged: #f59e0b;
  --car-windshield: rgba(0, 0, 0, 0.12);
  --sensor-beam: #c2410c;
  --sensor-hit: #c2410c;
  --sensor-danger: #b91c1c;
  --node-bg: #ffffff;
  --node-label: #14202b;
  --chart-bg: rgba(255, 255, 255, 0.92);
  --chart-grid: rgba(0, 0, 0, 0.08);
  --chart-best: #16a34a;
  --chart-avg: rgba(0, 0, 0, 0.4);
  --chart-text: rgba(0, 0, 0, 0.85);
  --trail: rgba(37, 99, 235, 0.3);
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.18);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial,
    sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body {
  display: grid;
  grid-template-columns: 1fr 320px;
  grid-template-rows: 100vh;
  transition: background-color 0.3s ease;
}

/* ---------- Stage ---------- */
#stage {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
}

#carCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  background: var(--road);
}

#networkCanvas {
  position: absolute;
  right: 12px;
  bottom: 12px;
  width: min(320px, 38vw);
  height: min(280px, 42vh);
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  backdrop-filter: blur(8px);
}

/* ---------- HUD ---------- */
#hud {
  position: absolute;
  top: 12px;
  left: 12px;
  display: grid;
  grid-template-columns: auto auto;
  gap: 4px 14px;
  padding: 12px 16px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  backdrop-filter: blur(8px);
  pointer-events: none;
  user-select: none;
}
.hud-row { display: contents; }
.hud-k { color: var(--text-dim); letter-spacing: 0.04em; text-transform: uppercase; font-size: 11px; }
.hud-v { color: var(--text); font-weight: 600; text-align: right; }

/* ---------- Chart ---------- */
#chartCanvas {
  position: absolute;
  left: 12px;
  bottom: 12px;
  width: min(320px, 38vw);
  height: 110px;
  border-radius: var(--radius);
  border: 1px solid var(--panel-border);
  box-shadow: var(--shadow);
  backdrop-filter: blur(8px);
}

/* ---------- Sidebar ---------- */
#sidebar {
  height: 100vh;
  background: var(--bg-elev);
  border-left: 1px solid var(--panel-border);
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
  box-shadow: var(--shadow);
}
#sidebar header h1 {
  margin: 0;
  font-size: 18px;
  letter-spacing: -0.01em;
}
#sidebar header .tagline {
  margin: 4px 0 0;
  color: var(--text-dim);
  font-size: 12px;
}

.panel-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.panel-group h2 {
  margin: 0 0 4px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
}

.slider {
  display: grid;
  grid-template-columns: 90px 1fr 44px;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}
.slider span { color: var(--text-dim); }
.slider output { font-variant-numeric: tabular-nums; text-align: right; color: var(--text); }
.slider input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

.panel-group.buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

button {
  font-family: inherit;
  font-size: 13px;
  padding: 9px 10px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease, border-color 0.15s;
  user-select: none;
}
button:hover { background: var(--bg-elev); border-color: var(--accent); color: var(--text); }
button:active { transform: translateY(1px); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.creds {
  margin-top: auto;
  padding-top: 12px;
  border-top: 1px solid var(--panel-border);
  font-size: 12px;
  color: var(--text-dim);
}
.creds a { color: var(--accent); text-decoration: none; }
.creds a:hover { text-decoration: underline; }

#sidebar.collapsed {
  display: none;
}

/* Sidebar toggle (visible only on mobile or when collapsed) */
#sidebar-toggle {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 30;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 18px;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 0;
  background: var(--panel);
  backdrop-filter: blur(8px);
}
#sidebar.collapsed ~ #sidebar-toggle { display: flex; }

/* ---------- D-pad (touch) ---------- */
#dpad {
  position: absolute;
  right: 16px;
  bottom: calc(16px + min(280px, 42vh) + 8px);
  display: none;
  grid-template-columns: 56px 56px 56px;
  grid-template-rows: 56px 56px 56px;
  gap: 4px;
  z-index: 5;
  user-select: none;
  touch-action: none;
}
#dpad button {
  border-radius: 50%;
  background: var(--panel);
  font-size: 18px;
  padding: 0;
  backdrop-filter: blur(8px);
}
#dpad button[data-dir="up"] { grid-column: 2; grid-row: 1; }
#dpad button[data-dir="left"] { grid-column: 1; grid-row: 2; }
#dpad button[data-dir="right"] { grid-column: 3; grid-row: 2; }
#dpad button[data-dir="down"] { grid-column: 2; grid-row: 3; }

body.manual-mode #dpad,
@media (hover: none) and (pointer: coarse) {
  #dpad { display: grid; }
}
@media (hover: none) and (pointer: coarse) {
  #dpad { display: grid; }
}

/* ---------- Modal ---------- */
#help-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}
#help-modal.open { display: flex; }
.modal-card {
  position: relative;
  max-width: 520px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 24px 28px;
  box-shadow: var(--shadow);
}
.modal-card h2 { margin: 0 0 8px; }
.modal-card h3 { margin: 16px 0 6px; font-size: 14px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.08em; }
.modal-close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 36px;
  height: 36px;
  font-size: 22px;
  background: transparent;
  border: none;
}
.keys { width: 100%; border-collapse: collapse; font-size: 13px; }
.keys th { text-align: left; width: 30%; padding: 4px 8px 4px 0; color: var(--accent); font-weight: 600; }
.keys td { padding: 4px 0; color: var(--text); }
.modal-card ul { padding-left: 18px; }
.modal-card li { margin: 4px 0; }

/* ---------- Toast ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(40px);
  background: var(--bg-elev);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 999px;
  border: 1px solid var(--panel-border);
  box-shadow: var(--shadow);
  font-size: 13px;
  pointer-events: none;
  opacity: 0;
  transition: transform 0.25s ease, opacity 0.25s ease;
  z-index: 200;
}
#toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* ---------- Drag-drop highlight ---------- */
body.dropping #stage::after {
  content: "Drop brain.json to load";
  position: absolute;
  inset: 12px;
  border: 2px dashed var(--accent);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: var(--accent);
  background: rgba(58, 134, 255, 0.06);
  pointer-events: none;
  z-index: 50;
}

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  body {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
  }
  #sidebar {
    position: absolute;
    top: 0;
    right: 0;
    width: min(320px, 86vw);
    height: 100vh;
    z-index: 20;
    transform: translateX(100%);
    transition: transform 0.25s ease;
  }
  #sidebar:not(.collapsed) { transform: translateX(0); }
  #sidebar-toggle { display: flex; }
  #networkCanvas { width: min(220px, 60vw); height: min(180px, 28vh); }
  #chartCanvas { width: min(220px, 60vw); height: 90px; }
  #hud { font-size: 11px; padding: 8px 10px; gap: 2px 10px; }
}

@media (max-width: 480px) {
  #networkCanvas { display: none; }
  #chartCanvas { width: 160px; height: 70px; }
}

/* Smooth transitions when theme changes */
#carCanvas, #networkCanvas, #chartCanvas, #hud, #sidebar, button {
  transition: background-color 0.25s ease, border-color 0.25s ease,
    color 0.25s ease;
}
