:root {
  --bg: #16191e;
  --panel: rgba(21, 24, 29, 0.84);
  --panel-solid: #15181d;
  --line: rgba(255, 255, 255, 0.07);
  --line-strong: rgba(255, 255, 255, 0.14);
  --text: #e8edf3;
  --text-2: #aab3bf;
  --text-3: #6f7985;
  --accent: #3d8bfd;
  --accent-2: #6aa8ff;
  --accent-soft: rgba(61, 139, 253, 0.14);
  --danger: #ff4d6a;
  --radius: 14px;
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, 'Cascadia Code', monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}

#world {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
  cursor: default;
  touch-action: none;
}
body[data-mode='build'] #world { cursor: crosshair; }
#world.panning { cursor: grabbing !important; }

button, select, input { font: inherit; color: inherit; }
.mono { font-family: var(--mono); }
.muted { color: var(--text-2); margin: 0 0 14px; }
.small { font-size: 12px; }

kbd {
  font: 600 10px/1 var(--mono);
  padding: 3px 5px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid var(--line);
  color: var(--text-2);
  margin-left: 6px;
  white-space: nowrap;
}

/* ------------------------------------------------------------- top bar */
.topbar {
  position: fixed;
  top: 12px;
  left: 12px;
  right: 12px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 12px 0 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  backdrop-filter: blur(16px) saturate(140%);
  -webkit-backdrop-filter: blur(16px) saturate(140%);
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }
.brand-name { font-weight: 700; letter-spacing: 0.02em; font-size: 15px; }
.brand-sub { font-size: 11px; color: var(--text-3); text-transform: uppercase; letter-spacing: 0.12em; }

.modes {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--line);
  border-radius: 11px;
}
.mode {
  display: flex;
  align-items: center;
  flex-direction: row-reverse;
  gap: 2px;
  border: 0;
  background: transparent;
  color: var(--text-2);
  font-weight: 600;
  padding: 7px 14px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.mode:hover { color: var(--text); }
.mode.active { background: var(--accent); color: #fff; }
.mode.active kbd { background: rgba(255, 255, 255, 0.2); color: #fff; border-color: transparent; }

.top-right { display: flex; align-items: center; gap: 8px; }
.pill {
  font-size: 11px;
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--text-2);
  min-width: 64px;
  text-align: center;
}
.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
  cursor: pointer;
  font-weight: 700;
  font-size: 15px;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.1); }

/* ------------------------------------------------------------- panels */
.panel {
  position: fixed;
  top: 80px;
  z-index: 5;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  backdrop-filter: blur(16px) saturate(140%);
  -webkit-backdrop-filter: blur(16px) saturate(140%);
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.15) transparent;
}
.panel-left {
  left: 12px;
  width: 296px;
  max-height: calc(100vh - 92px);
  padding: 18px 18px 14px;
}
.panel-right {
  right: 12px;
  width: 330px;
  max-height: calc(100vh - 92px - 190px);
  padding: 6px;
}
body[data-mode='build'] .panel-right,
body[data-mode='build'] .hud,
body[data-mode='drive'] .train-only { display: none; }

.mode-panel { display: none; }
body[data-mode='build'] [data-panel='build'],
body[data-mode='train'] [data-panel='train'],
body[data-mode='drive'] [data-panel='drive'] { display: block; }

h2 { font-size: 16px; margin: 0 0 4px; letter-spacing: -0.01em; }
h3 { font-size: 12px; margin: 0; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-2); font-weight: 600; }

.card { padding: 12px; border-radius: 10px; }
.card + .card { border-top: 1px solid var(--line); border-radius: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; gap: 8px; }

.field { margin: 0 0 14px; }
.field > label {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-2);
  margin-bottom: 7px;
  font-weight: 500;
}
output { font-family: var(--mono); color: var(--text); font-size: 12px; }
.row { display: flex; gap: 8px; }
.row select { flex: 1; }

select {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  padding: 7px 10px;
  outline: none;
}
select option { background: var(--panel-solid); }

.btn {
  border: 1px solid var(--line-strong);
  background: rgba(255, 255, 255, 0.05);
  border-radius: 9px;
  padding: 8px 12px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:hover { background: rgba(255, 255, 255, 0.1); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); }
.btn.primary kbd { background: rgba(255, 255, 255, 0.2); color: #fff; border-color: transparent; }
.btn.danger:hover { border-color: var(--danger); color: var(--danger); background: rgba(255, 77, 106, 0.08); }
.btn.wide { width: 100%; margin-bottom: 8px; }
.btn.accent-outline { border-color: rgba(61, 139, 253, 0.5); color: var(--accent-2); margin-top: 8px; }
.btn.accent-outline:hover { background: var(--accent-soft); }
.btn.engaged { background: #1f6fe0; box-shadow: 0 0 0 3px var(--accent-soft), 0 0 24px rgba(61, 139, 253, 0.45); }
.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 14px; }
.transport { display: grid; grid-template-columns: 1.4fr 1fr 1fr; gap: 8px; margin-bottom: 16px; }

.segmented {
  display: flex;
  padding: 3px;
  gap: 3px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--line);
}
.segmented button {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--text-2);
  padding: 6px 8px;
  border-radius: 7px;
  cursor: pointer;
  font-weight: 600;
  font-size: 12px;
}
.segmented button.active { background: rgba(255, 255, 255, 0.1); color: var(--text); }

input[type='range'] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  border-radius: 4px;
  background: linear-gradient(to right, var(--accent) var(--fill, 50%), rgba(255, 255, 255, 0.1) var(--fill, 50%));
  outline: none;
  cursor: pointer;
}
input[type='range']::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--accent);
}
input[type='range']::-moz-range-thumb {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--accent);
}

.check {
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: 13px;
  color: var(--text-2);
  margin: 0 0 9px;
  cursor: pointer;
}
.check input { accent-color: var(--accent); width: 15px; height: 15px; margin: 0; }
.check kbd { margin-left: auto; }

details { border-top: 1px solid var(--line); padding: 12px 0 2px; }
summary {
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-2);
  margin-bottom: 12px;
  list-style: none;
  display: flex;
  justify-content: space-between;
}
summary::-webkit-details-marker { display: none; }
summary::after { content: '+'; color: var(--text-3); }
details[open] summary::after { content: '−'; }

.howto { margin: 14px 0 0; padding: 12px 0 0 16px; border-top: 1px solid var(--line); color: var(--text-2); font-size: 12.5px; }
.howto li { margin-bottom: 6px; }
.howto b { color: var(--text); font-weight: 600; }

.stat-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.stat-row div, .stats div {
  background: rgba(255, 255, 255, 0.035);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 9px;
}
.stat-row span, .stats span { display: block; font-size: 10.5px; color: var(--text-3); text-transform: uppercase; letter-spacing: 0.06em; }
.stat-row b, .stats b { font: 600 15px/1.3 var(--mono); }

.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.progress { height: 3px; background: rgba(255, 255, 255, 0.07); border-radius: 3px; margin-top: 10px; overflow: hidden; }
.progress div { height: 100%; width: 0; background: var(--accent); transition: width 0.2s linear; }

.net-canvas { width: 100%; height: 190px; display: block; }
.chart-wrap { position: relative; }
.chart-canvas { width: 100%; height: 140px; display: block; cursor: crosshair; }
.legend { display: flex; gap: 10px; font-size: 11px; color: var(--text-2); }
.legend i { display: inline-block; width: 10px; height: 3px; border-radius: 2px; margin-right: 5px; vertical-align: middle; }

.tooltip {
  position: absolute;
  pointer-events: none;
  background: #0e1014;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 7px 9px;
  font-size: 11.5px;
  min-width: 118px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}
.tt-title { color: var(--text-2); margin-bottom: 3px; }
.tt-row { display: flex; align-items: center; gap: 6px; }
.tt-row i { width: 8px; height: 8px; border-radius: 50%; }
.tt-row b { margin-left: auto; font-family: var(--mono); }

/* ------------------------------------------------------------- overlays */
.minimap-wrap {
  position: fixed;
  right: 12px;
  bottom: 12px;
  width: 330px;
  height: 178px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  overflow: hidden;
  z-index: 5;
}
#minimap { width: 100%; height: 100%; display: block; cursor: pointer; }

.hud {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 10px 18px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  z-index: 6;
}
.hud-speed { display: flex; align-items: baseline; gap: 5px; min-width: 88px; }
.hud-speed b { font: 700 30px/1 var(--mono); letter-spacing: -0.03em; }
.hud-speed span { font-size: 11px; color: var(--text-3); }
.hud-wheel { width: 34px; height: 34px; color: var(--text-2); transition: transform 0.08s linear; }
.hud-bars { display: grid; gap: 5px; }
.bar { display: flex; align-items: center; gap: 7px; font: 600 9.5px var(--mono); color: var(--text-3); }
.bar div { width: 90px; height: 5px; background: rgba(255, 255, 255, 0.08); border-radius: 3px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 3px; }
.bar i.brk { background: var(--danger); }
.hud-badge {
  font: 700 11px var(--mono);
  letter-spacing: 0.1em;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--line-strong);
  color: var(--text-2);
}
.hud-badge.on { background: var(--accent); border-color: var(--accent); color: #fff; box-shadow: 0 0 20px rgba(61, 139, 253, 0.5); }
.hud-badge.crash { background: var(--danger); border-color: var(--danger); color: #fff; }

.hint {
  position: fixed;
  left: 50%;
  bottom: 96px;
  transform: translateX(-50%);
  font-size: 12.5px;
  color: var(--text-2);
  background: rgba(14, 16, 20, 0.7);
  border: 1px solid var(--line);
  padding: 7px 14px;
  border-radius: 999px;
  pointer-events: none;
  white-space: nowrap;
  z-index: 6;
}
.hint:empty { display: none; }
body[data-mode='build'] .hint { bottom: 22px; }

.toast {
  position: fixed;
  top: 84px;
  left: 50%;
  transform: translate(-50%, -8px);
  background: #0e1014;
  border: 1px solid var(--line-strong);
  border-left: 3px solid var(--accent);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 20;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { border-left-color: var(--danger); }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: grid;
  place-items: center;
  z-index: 30;
}
.modal[hidden] { display: none; }
.modal-card {
  width: min(460px, calc(100vw - 32px));
  background: var(--panel-solid);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  padding: 18px;
}
.modal table { width: 100%; border-collapse: collapse; font-size: 13px; }
.modal td { padding: 7px 0; border-top: 1px solid var(--line); color: var(--text-2); }
.modal td:first-child { width: 42%; }
.modal td kbd { margin: 0 3px 0 0; }

/* ------------------------------------------------------------- responsive */
@media (max-width: 1100px) {
  .panel-right, .minimap-wrap { width: 280px; }
  .panel-right { max-height: calc(100vh - 92px - 170px); }
  .minimap-wrap { height: 158px; }
}
}

@media (max-height: 820px) and (min-width: 861px) {
  .net-canvas { height: 150px; }
  .chart-canvas { height: 110px; }
  .minimap-wrap { height: 130px; }
  .panel-right { max-height: calc(100vh - 92px - 142px); }
}

/* ------------------------------------------------------------- HUD extras */
.hud-gen { font-size: 11px; color: var(--text-2); white-space: nowrap; }
.hud-gen:empty { display: none; }
#panelToggle { display: none; }
button { touch-action: manipulation; }

/* ------------------------------------------------------------- touch driving */
.touch-drive {
  position: fixed;
  left: max(14px, env(safe-area-inset-left));
  right: max(14px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  display: none;
  align-items: flex-end;
  justify-content: space-between;
  gap: 10px;
  z-index: 8;
  pointer-events: none;
}
.touch-drive button {
  pointer-events: auto;
  touch-action: none;
  border: 1px solid var(--line-strong);
  background: rgba(21, 24, 29, 0.78);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.06s, background 0.1s, border-color 0.1s;
}
.td-steer, .td-pedals { display: flex; gap: 12px; align-items: flex-end; }
.td-steer button {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  display: grid;
  place-items: center;
}
.td-steer svg { width: 30px; height: 30px; fill: none; stroke: currentColor; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.td-pedals button { font: 700 12px var(--mono); letter-spacing: 0.08em; border-radius: 18px; }
.td-pedals .brake { width: 74px; height: 74px; color: #ff8fa1; }
.td-pedals .gas { width: 80px; height: 118px; color: #9cc5ff; }
.touch-drive button.pressed { transform: scale(0.94); background: var(--accent); border-color: var(--accent); color: #fff; }
.td-pedals .brake.pressed { background: var(--danger); border-color: var(--danger); }
.td-mid {
  position: absolute;
  left: 50%;
  bottom: 132px;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
}
.td-pill { font: 700 11px var(--mono); letter-spacing: 0.08em; padding: 10px 14px; border-radius: 999px; white-space: nowrap; }
.td-pill.engaged { background: var(--accent); border-color: var(--accent); color: #fff; box-shadow: 0 0 20px rgba(61, 139, 253, 0.5); }

@media (pointer: coarse) {
  body[data-mode='drive'] .touch-drive { display: flex; }
  body[data-mode='drive'] .hint { display: none; }
}

/* ------------------------------------------------------------- phones */
@media (max-width: 860px), (pointer: coarse) and (max-height: 560px) {
  .topbar { left: 8px; right: 8px; top: max(8px, env(safe-area-inset-top)); gap: 8px; height: 52px; padding: 0 8px; }
  .brand > div, .pill, .mode kbd, #helpBtn { display: none; }
  .mode { padding: 7px 12px; }
  #panelToggle { display: grid; place-items: center; }
  body.panel-open #panelToggle { background: var(--accent); border-color: var(--accent); }

  .panel-left {
    display: none;
    left: 8px;
    right: 8px;
    width: auto;
    top: auto;
    bottom: max(8px, env(safe-area-inset-bottom));
    max-height: 64vh;
    max-height: 64dvh;
    z-index: 25;
    box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.5);
  }
  body.panel-open .panel-left { display: block; }
  .panel-right, .minimap-wrap, .hint { display: none; }

  body[data-mode='drive'] .touch-drive { display: flex; }
  .hud { bottom: auto; top: 70px; gap: 10px; padding: 7px 12px; border-radius: 12px; }
  .hud-speed { min-width: 0; }
  .hud-speed b { font-size: 22px; }
  .hud-wheel, .hud-bars { display: none; }
}

@media (max-width: 380px) {
  .td-steer button { width: 64px; height: 64px; }
  .td-pedals .brake { width: 62px; height: 62px; }
  .td-pedals .gas { width: 68px; height: 104px; }
  .td-pill { padding: 8px 10px; font-size: 10px; }
}
