/* ===== CSS Custom Properties ===== */
:root {
  /* z-index layers (low → high) */
  --z-speed-fx: 2;
  --z-mirror: 9;
  --z-minimap: 10;
  --z-hud: 10;
  --z-ui: 10;
  --z-mobile-controls: 14;
  --z-wheel-hud: 13;
  --z-ui-toggle: 16;
  --z-rotate-overlay: 20;
  --z-loading: 40;
  --z-minimap-expanded: 120;

  /* spacing */
  --gap-sm: 6px;
  --gap-md: 10px;
  --gap-lg: 16px;

  /* surface colors */
  --surface-dark: rgba(20, 20, 20, 0.82);
  --surface-overlay: rgba(9, 17, 24, 0.68);
  --surface-subtle: rgba(255, 255, 255, 0.04);
  --surface-hover: rgba(255, 255, 255, 0.14);
  --border-subtle: rgba(255, 255, 255, 0.12);
  --border-default: rgba(255, 255, 255, 0.18);

  /* accent */
  --accent-blue: #7cc8ff;
  --accent-blue-bg: rgba(86, 190, 255, 0.24);
  --accent-blue-border: rgba(154, 221, 255, 0.72);
  --accent-signal: rgba(255, 181, 77, 0.22);

  /* panel radius */
  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-pill: 999px;

  /* safe area fallbacks */
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
}

/* ===== Reset & Base ===== */
* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: Arial, "Microsoft YaHei", sans-serif;
  background: #111;
  color: #fff;
}

#app {
  display: block;
  width: 100vw;
  height: 100vh;
}

/* ===== Speed visual effects ===== */
#speedVignette {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: var(--z-speed-fx);
  background: radial-gradient(ellipse at center, transparent 30%, rgba(0, 0, 0, 0.92) 100%);
  opacity: 0;
  will-change: opacity;
}

#speedBlur {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: var(--z-speed-fx);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  mask-image: radial-gradient(ellipse 56% 56% at 50% 50%, transparent 50%, black 82%);
  -webkit-mask-image: radial-gradient(ellipse 56% 56% at 50% 50%, transparent 50%, black 82%);
  opacity: 0;
  will-change: opacity;
}

#app,
#mobileControls,
#mobileControls *,
#rotateOverlay,
#rotateOverlay * {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* ===== Shared: buttons, inputs, labels ===== */
button,
.file-label,
input,
select {
  font: inherit;
}

button,
.file-label {
  border: 1px solid var(--border-default);
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

button:hover,
.file-label:hover {
  background: var(--surface-hover);
}

.file-label input {
  display: none;
}

select {
  min-width: 132px;
  padding: 8px 10px;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}

option {
  background: #16202a;
  color: #f4fbff;
}

input[type="number"] {
  width: 90px;
}

input[type="range"] {
  accent-color: var(--accent-blue);
}

/* ===== UI Panel (top-left controls) ===== */
#ui {
  position: fixed;
  top: var(--gap-lg);
  left: var(--gap-lg);
  z-index: var(--z-ui);
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  gap: var(--gap-md);
  align-items: flex-start;
}

#uiToggle {
  border: 1px solid var(--border-default);
  background: rgba(16, 20, 26, 0.8);
  color: #fff;
  padding: 9px 14px;
  border-radius: var(--radius-pill);
  backdrop-filter: blur(8px);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.24);
}

#uiToggle:hover {
  background: var(--surface-hover);
}

#ui.collapsed .panel {
  display: none;
}

#collapsedQuickActions {
  display: none;
  gap: 8px;
  flex-wrap: wrap;
}

#ui.collapsed #collapsedQuickActions {
  display: flex;
}

.quickViewBtn {
  border: 1px solid var(--border-default);
  background: rgba(16, 20, 26, 0.78);
  color: #fff;
  padding: 8px 12px;
  border-radius: var(--radius-pill);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.24);
  backdrop-filter: blur(8px);
  cursor: pointer;
}

.quickViewBtn.is-active {
  background: var(--accent-blue-bg);
  border-color: var(--accent-blue-border);
}

.panel {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100vh - 88px);
  background: var(--surface-dark);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: var(--gap-lg) 18px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
  pointer-events: auto;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

h1 {
  margin: 0 0 var(--gap-md);
  font-size: 22px;
}

p {
  margin: 0 0 12px;
  color: #d5d5d5;
}

/* ===== Panel Rows ===== */
.row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--gap-md);
  align-items: center;
  margin-bottom: 12px;
}

.controlsSection {
  display: grid;
  gap: 10px;
  margin-top: 16px;
  margin-bottom: 14px;
}

.controlGrid {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  gap: var(--gap-md);
  align-items: stretch;
}

.span-3 {
  grid-column: span 3;
}

.span-4 {
  grid-column: span 4;
}

.span-8 {
  grid-column: span 8;
}

.span-9 {
  grid-column: span 9;
}

.span-12 {
  grid-column: span 12;
}

.primary-controls,
.secondary-controls {
  display: grid;
  align-items: stretch;
}

.primary-controls {
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}

.secondary-controls {
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

.primary-controls > *,
.secondary-controls > * {
  min-width: 0;
}

.control-field {
  display: grid;
  align-content: start;
  gap: 8px;
  min-height: 74px;
  padding: 10px 12px;
  background: var(--surface-subtle);
  border-radius: var(--radius-sm);
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.control-field > span:first-child,
.control-range > span:first-child {
  color: #cfe6f7;
  font-size: 12px;
  line-height: 1.35;
}

.control-field select,
.control-field input[type="number"] {
  width: 100%;
}

.action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  padding: 7px 10px;
  text-align: center;
  font-weight: 600;
}

.toggle-card {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 74px;
  padding: 10px 12px;
  background: var(--surface-subtle);
  border-radius: var(--radius-sm);
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.toggle-card input[type="checkbox"] {
  margin: 0;
  flex: 0 0 auto;
}

.toggle-card span {
  color: #e7f3fb;
  line-height: 1.45;
}

.row.compact label:not(.control-field):not(.toggle-card) {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface-subtle);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
}

.range-card {
  min-width: 220px;
  flex: 1 1 220px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: var(--gap-md);
  background: var(--surface-subtle);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
}

.range-card.control-range {
  position: relative;
  min-height: 74px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: center;
  gap: 12px;
  padding: 12px 14px;
}

.range-card.control-range > span:first-child {
  padding-right: 70px;
  line-height: 1.35;
}

.range-card.control-range input[type="range"] {
  width: 100%;
  margin: 0;
}

.control-value {
  position: absolute;
  top: 12px;
  right: 14px;
  min-width: 52px;
  padding: 3px 8px;
  border-radius: var(--radius-pill);
  background: rgba(124, 200, 255, 0.14);
  text-align: center;
  color: #bfe7ff;
  font-size: 11px;
  font-weight: 600;
  line-height: 1.2;
}

.range-card span:last-child {
  min-width: 52px;
  text-align: right;
  color: #bfe7ff;
}

/* ===== Acceleration Curve Card ===== */
.curveCard {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 12px;
  padding: 14px;
  border-radius: 16px;
  border: 1px solid rgba(150, 214, 255, 0.18);
  background: linear-gradient(180deg, rgba(10, 16, 22, 0.86), rgba(18, 28, 36, 0.76));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.03), 0 12px 24px rgba(0, 0, 0, 0.18);
}

.curveCardHeader {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--gap-md);
}

.curveCardTitle {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.curveCardTitle strong {
  font-size: 15px;
  letter-spacing: 0.03em;
}

.curveCardTitle span {
  color: #aac4d6;
  font-size: 12px;
}

.curveValue {
  min-width: 66px;
  padding: 7px 12px;
  border-radius: var(--radius-pill);
  background: rgba(124, 200, 255, 0.18);
  color: #dff3ff;
  text-align: center;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.curveSliderRow {
  display: flex;
  align-items: center;
  gap: 12px;
}

.curveSliderLabel {
  color: #bdd6e8;
  font-size: 12px;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

.curveSliderRow input[type="range"] {
  flex: 1 1 auto;
}

.curveChart {
  display: block;
  width: 100%;
  height: auto;
}

.curveChartFrame {
  fill: rgba(8, 14, 20, 0.74);
  stroke: rgba(164, 222, 255, 0.16);
  stroke-width: 1.2;
}

.curveChartGrid {
  fill: none;
  stroke: rgba(190, 225, 248, 0.12);
  stroke-width: 1;
}

.curveChartAxis {
  fill: none;
  stroke: rgba(214, 239, 255, 0.34);
  stroke-width: 1.4;
}

.curveChartLine {
  fill: none;
  stroke: #8edbff;
  stroke-width: 3.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 0 8px rgba(103, 211, 255, 0.26));
}

.curveChartGuide {
  stroke: rgba(255, 216, 128, 0.65);
  stroke-width: 1.3;
  stroke-dasharray: 4 5;
}

.curveChartDot {
  fill: #ffd880;
  stroke: rgba(255, 248, 230, 0.94);
  stroke-width: 2;
}

.curveChart text {
  fill: #aec8da;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.04em;
}

.curveMeta {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: var(--gap-md);
  color: #bed7e8;
  font-size: 12px;
}

.curveMeta span {
  padding: 7px 10px;
  border-radius: var(--radius-pill);
  background: var(--surface-subtle);
}

.panelUtility {
  display: flex;
  justify-content: flex-end;
  margin-top: 8px;
}

.subtleUtilityBtn {
  padding: 6px 10px;
  border-radius: var(--radius-pill);
  border-color: rgba(255, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.04);
  color: rgba(219, 235, 245, 0.78);
  font-size: 11px;
  line-height: 1.2;
  font-weight: 500;
  letter-spacing: 0.01em;
  box-shadow: none;
}

.subtleUtilityBtn:hover,
.subtleUtilityBtn:focus-visible {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.16);
  color: rgba(232, 244, 251, 0.9);
}

.curveMeta strong {
  color: #f3fbff;
}

/* ===== Tips ===== */
.tips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
  color: #ddd;
}

.tips span,
.status div {
  padding: 6px 10px;
  border-radius: var(--radius-pill);
  background: rgba(255, 255, 255, 0.06);
}

/* ===== HUD (bottom status bar) ===== */
#hud {
  position: fixed;
  left: var(--gap-lg);
  right: var(--gap-lg);
  bottom: var(--gap-lg);
  z-index: var(--z-hud);
  pointer-events: none;
}

.status {
  display: flex;
  flex-wrap: wrap;
  gap: var(--gap-md);
}

#hud .status {
  max-width: min(920px, calc(100vw - 32px));
}

#hud .status div {
  backdrop-filter: blur(10px);
  background: rgba(10, 16, 22, 0.68);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

/* ===== Wheel HUD ===== */
#wheelHud {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  width: 172px;
  height: 172px;
  pointer-events: none;
  z-index: var(--z-hud);
}

.wheelHudTopMark {
  position: absolute;
  top: 8px;
  left: 50%;
  width: 8px;
  height: 24px;
  margin-left: -4px;
  border-radius: var(--radius-pill);
  background: linear-gradient(180deg, #ffd880, #ff9f43);
  box-shadow: 0 0 12px rgba(255, 180, 92, 0.45);
}

#wheelHudDial {
  position: relative;
  width: 100%;
  height: 100%;
}

#wheelHudDial svg {
  width: 100%;
  height: 100%;
  filter: drop-shadow(0 6px 14px rgba(0, 0, 0, 0.5));
}

/* ===== Signal & Audio status ===== */
#signalStatusBox[data-signal="left"],
#signalStatusBox[data-signal="right"] {
  background: var(--accent-signal);
  color: #ffd89b;
}

#audioStatusBox[data-audio="on"] {
  background: rgba(136, 235, 188, 0.18);
  color: #d6ffeb;
}

#audioStatusBox[data-audio="error"] {
  background: rgba(255, 110, 110, 0.16);
  color: #ffd0d0;
}

/* ===== Mirrors ===== */
#mirrorHud {
  position: fixed;
  top: var(--gap-lg);
  left: 50%;
  transform: translateX(-50%);
  display: none;
  gap: 12px;
  align-items: flex-start;
  pointer-events: none;
  z-index: var(--z-mirror);
}

#mirrorHud.cockpit-layout {
  top: 0;
  left: 0;
  right: 0;
  width: 100%;
  height: 100%;
  transform: none;
  display: block;
}

#mirrorHud.cockpit-layout .mirrorPane {
  position: absolute;
}

#mirrorHud.cockpit-layout #mirrorCenter {
  top: 18px;
  left: 50%;
  transform: translateX(-50%);
}

#mirrorHud.cockpit-layout #mirrorLeft {
  top: 128px;
  left: 14px;
}

#mirrorHud.cockpit-layout #mirrorRight {
  top: 128px;
  right: 14px;
}

.mirrorPane {
  position: relative;
  width: min(16vw, 168px);
  aspect-ratio: 1.38 / 1;
  border-radius: 16px 16px 22px 22px;
  border: 3px solid rgba(230, 243, 255, 0.88);
  box-shadow: 0 18px 36px rgba(0, 0, 0, 0.28);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.08), rgba(0, 0, 0, 0.16));
  overflow: hidden;
}

.mirrorCenter {
  width: min(20vw, 220px);
  aspect-ratio: 2.25 / 1;
  border-radius: 14px 14px 24px 24px;
}

.mirrorPane::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.18);
}

.mirrorLabel {
  position: absolute;
  left: 14px;
  bottom: 10px;
  padding: 4px 10px;
  border-radius: var(--radius-pill);
  background: rgba(12, 18, 24, 0.55);
  color: #eef8ff;
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

/* ===== Cockpit Signal HUD ===== */
#cockpitSignalHud {
  position: fixed;
  top: 108px;
  left: 50%;
  transform: translateX(-50%);
  display: none;
  gap: 14px;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 11;
}

#cockpitSignalHud.is-visible {
  display: flex;
}

.cockpitSignalLamp {
  min-width: 88px;
  padding: 8px 12px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--border-subtle);
  background: rgba(9, 14, 19, 0.56);
  color: rgba(232, 239, 245, 0.52);
  text-align: center;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  backdrop-filter: blur(10px);
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.24);
  transition: background 0.12s ease, color 0.12s ease, box-shadow 0.12s ease, transform 0.12s ease;
}

.cockpitSignalLamp.is-active {
  background: var(--accent-signal);
  color: #ffe1ad;
  box-shadow: 0 0 18px rgba(255, 181, 77, 0.28);
  transform: translateY(-1px);
}

/* ===== Mini Map ===== */
#miniMap {
  position: fixed;
  right: var(--gap-lg);
  bottom: 18px;
  width: min(19vw, 210px);
  aspect-ratio: 1 / 1;
  border-radius: var(--radius-lg);
  border: 3px solid rgba(214, 236, 255, 0.88);
  background: linear-gradient(180deg, rgba(14, 22, 28, 0.18), rgba(5, 10, 14, 0.26));
  box-shadow: 0 20px 34px rgba(0, 0, 0, 0.28);
  overflow: hidden;
  pointer-events: auto;
  z-index: var(--z-minimap);
  cursor: zoom-in;
  touch-action: none;
  transition: inset 0.18s ease, width 0.18s ease, border-radius 0.18s ease, box-shadow 0.18s ease;
}

#miniMap.expanded {
  inset: 12px;
  width: auto;
  height: auto;
  aspect-ratio: auto;
  border-radius: var(--radius-lg);
  box-shadow: 0 24px 46px rgba(0, 0, 0, 0.34);
  cursor: crosshair;
  z-index: var(--z-minimap-expanded);
}

#miniMap.expanded.is-panning {
  cursor: grabbing;
}

#miniMap::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.16);
}

.miniMapLabel {
  position: absolute;
  left: 12px;
  top: 10px;
  padding: 4px 10px;
  border-radius: var(--radius-pill);
  background: rgba(8, 14, 20, 0.54);
  color: #eef8ff;
  font-size: 12px;
  letter-spacing: 0.08em;
  pointer-events: none;
}

.miniMapHint {
  position: absolute;
  left: 12px;
  bottom: 10px;
  max-width: calc(100% - 24px);
  padding: 5px 10px;
  border-radius: var(--radius-pill);
  background: rgba(8, 14, 20, 0.58);
  color: rgba(238, 248, 255, 0.92);
  font-size: 12px;
  line-height: 1.35;
  pointer-events: none;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.16s ease, transform 0.16s ease;
  z-index: 2;
}

#miniMap.expanded .miniMapHint {
  opacity: 1;
  transform: translateY(0);
}

#miniMapClose {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: var(--radius-pill);
  border: 1px solid rgba(255, 255, 255, 0.24);
  background: rgba(8, 14, 20, 0.72);
  color: #eef8ff;
  font-size: 18px;
  line-height: 1;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 2;
}

#miniMap.expanded #miniMapClose {
  display: inline-flex;
}

body.minimap-expanded #mobileControls,
body.minimap-expanded #wheelHud,
body.minimap-expanded #mirrorHud,
body.minimap-expanded #cockpitSignalHud,
body.minimap-expanded #hud,
body.minimap-expanded #ui,
body.minimap-expanded #firstRunHint {
  opacity: 0;
  pointer-events: none;
  visibility: hidden;
}

/* ===== First Run Hint ===== */
.firstRunHint {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 35;
  width: min(520px, calc(100vw - 32px));
  transform: translateX(-50%);
  display: grid;
  gap: 6px;
  padding: 14px 44px 14px 16px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: var(--radius-lg);
  background: rgba(8, 14, 20, 0.82);
  color: #eef8ff;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.32);
  backdrop-filter: blur(10px);
}

.firstRunHint[hidden] {
  display: none;
}

.firstRunHint strong {
  font-size: 14px;
}

.firstRunHint span {
  font-size: 13px;
  line-height: 1.45;
  color: rgba(238, 248, 255, 0.86);
}

.firstRunMobile {
  display: none;
}

#firstRunHintClose {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: var(--radius-pill);
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.08);
  color: #eef8ff;
  font-size: 18px;
  line-height: 1;
}

@media (hover: none), (pointer: coarse), (max-width: 900px) {
  .firstRunDesktop {
    display: none;
  }

  .firstRunMobile {
    display: inline;
  }
}

/* ===== Mobile Controls (hidden on desktop) ===== */
#mobileControls {
  display: none;
}

#rotateOverlay {
  display: none;
}

.mobileCluster,
.mobilePad,
.mobileDriveStack,
.mobileAuxRow {
  display: flex;
}

.mobileSteerRow,
.mobileViewResetStack {
  display: flex;
}

.mobileControl {
  min-width: 50px;
  min-height: 50px;
  padding: 0 12px;
  border: 1px solid var(--border-default);
  border-radius: 15px;
  background: var(--surface-overlay);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.22);
  color: #f4fbff;
  backdrop-filter: blur(10px);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  font-size: 13px;
}

.mobileControl:active,
.mobileControl.active {
  background: rgba(86, 190, 255, 0.3);
  border-color: var(--accent-blue-border);
}

.mobileControlLarge {
  min-width: 76px;
}

.mobileControlTall {
  min-height: 72px;
}

.mobileThrottleLever {
  display: none;
}

.mobileThrottle {
  background: linear-gradient(180deg, rgba(40, 138, 93, 0.82), rgba(22, 78, 56, 0.88));
  min-height: 72px;
}

.mobileBrake {
  background: linear-gradient(180deg, rgba(164, 58, 58, 0.84), rgba(100, 24, 24, 0.88));
  min-height: 72px;
}

.mobileBrakePrimary {
  min-height: 90px;
}

.mobileReverse {
  background: linear-gradient(180deg, rgba(62, 92, 172, 0.82), rgba(28, 48, 100, 0.88));
  min-height: 72px;
}

/* Signal lever */
.mobileSignalLever {
  width: 64px;
  height: 52px;
  position: relative;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  background: var(--surface-overlay);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.22);
  backdrop-filter: blur(10px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 7px 0;
  overflow: hidden;
}

.mobileSignalLever.dragging {
  border-color: rgba(255, 210, 80, 0.5);
  background: rgba(255, 190, 50, 0.1);
}

.mobileSignalLever[data-signal="left"],
.mobileSignalLever[data-signal="right"] {
  border-color: rgba(255, 210, 80, 0.7);
}

.signalLeverKnob {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: rgba(200, 215, 228, 0.88);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
  pointer-events: none;
  will-change: transform;
}

.mobileSignalLever.dragging .signalLeverKnob {
  background: rgba(255, 215, 80, 0.9);
}

.mobileSignalLever[data-signal="left"] .signalLeverKnob,
.mobileSignalLever[data-signal="right"] .signalLeverKnob {
  background: rgba(255, 210, 80, 0.8);
}

.signalLeverLabel {
  font-size: 10px;
  color: rgba(255, 255, 255, 0.32);
  pointer-events: none;
  line-height: 1;
  z-index: 1;
}

.mobileSignalLever[data-signal="left"] .signalLeverLabelTop {
  color: rgba(255, 210, 80, 0.95);
}

.mobileSignalLever[data-signal="right"] .signalLeverLabelBottom {
  color: rgba(255, 210, 80, 0.95);
}

/* Steer zone */
.mobileSteerZone {
  width: 100%;
  min-height: 110px;
  position: relative;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  border: 1px solid var(--border-default);
  border-radius: 15px;
  background: var(--surface-overlay);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.22);
  backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.mobileSteerZone.active {
  background: rgba(86, 190, 255, 0.18);
  border-color: var(--accent-blue-border);
}

.mobileSteerIndicator {
  position: absolute;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: rgba(154, 221, 255, 0.85);
  box-shadow: 0 0 8px rgba(86, 190, 255, 0.6);
  pointer-events: none;
  transition: background 0.1s;
  will-change: transform;
}

.mobileSteerZone.active .mobileSteerIndicator {
  background: rgba(86, 190, 255, 1);
}

.mobileSteerHint {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.28);
  pointer-events: none;
  letter-spacing: 0.04em;
}

.mobileHandbrake {
  background: linear-gradient(180deg, rgba(160, 120, 30, 0.82), rgba(90, 60, 10, 0.88));
  min-width: 56px;
}

.mobileWheelSlot {
  width: 56px;
  height: 56px;
  flex: 0 0 56px;
}

.mobileSteerRow {
  align-items: flex-end;
  gap: 8px;
}

.mobileViewResetStack {
  flex-direction: row;
  gap: 8px;
}

/* Rotate overlay */
.rotateCard {
  width: min(86vw, 320px);
  padding: 18px 20px;
  border-radius: 22px;
  background: rgba(7, 12, 18, 0.86);
  border: 1px solid var(--border-subtle);
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.34);
  backdrop-filter: blur(12px);
  text-align: center;
}

.rotateCard strong,
.rotateCard span {
  display: block;
}

.rotateCard strong {
  margin-bottom: 8px;
  font-size: 20px;
}

.rotateCard span {
  color: #dce7f1;
  line-height: 1.45;
}

/* ===== Loading Overlay ===== */
#loadingOverlay {
  position: fixed;
  inset: 0;
  z-index: var(--z-loading);
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(4, 8, 14, 0.78);
  backdrop-filter: blur(6px);
}

#loadingOverlay.visible {
  display: flex;
}

.loadingCard {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  color: rgba(210, 230, 245, 0.9);
  font-size: 14px;
  letter-spacing: 0.05em;
  min-width: min(320px, calc(100vw - 48px));
}

.loadingSpinner {
  width: 36px;
  height: 36px;
  border: 3px solid rgba(255, 255, 255, 0.1);
  border-top-color: rgba(154, 221, 255, 0.85);
  border-radius: 50%;
  animation: loadingSpin 0.75s linear infinite;
}

@keyframes loadingSpin {
  to { transform: rotate(360deg); }
}

.loadingDetail {
  min-height: 18px;
  color: rgba(210, 230, 245, 0.72);
  font-size: 12px;
  letter-spacing: 0;
}

.loadingProgress {
  width: 100%;
  height: 6px;
  overflow: hidden;
  border-radius: var(--radius-pill);
  background: rgba(255, 255, 255, 0.12);
}

.loadingProgressBar {
  width: 0%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, rgba(126, 211, 255, 0.95), rgba(134, 239, 172, 0.95));
  transition: width 0.16s ease;
}

/* ==========================================================
   RESPONSIVE: Tablet (max-width: 900px)
   ========================================================== */
@media (max-width: 900px) {
  .curveSliderRow {
    gap: 8px;
  }

  .curveSliderLabel {
    font-size: 11px;
  }

  #mirrorHud {
    top: 12px;
    gap: 8px;
  }

  .mirrorPane {
    width: min(18vw, 126px);
  }

  .mirrorCenter {
    width: min(22vw, 160px);
  }

  #mirrorHud.cockpit-layout #mirrorLeft,
  #mirrorHud.cockpit-layout #mirrorRight {
    top: 112px;
  }

  #miniMap {
    width: min(20vw, 136px);
  }

  #miniMap.expanded {
    inset: 12px;
    width: auto;
    height: auto;
  }
}

/* ==========================================================
   RESPONSIVE: Small screens (max-width: 700px)
   ========================================================== */
@media (max-width: 700px) {
  .range-card {
    min-width: 100%;
  }

  #hud {
    left: 12px;
    right: 12px;
    bottom: 12px;
  }

  #wheelHud {
    width: 136px;
    height: 136px;
    bottom: 12px;
  }

  #mirrorHud {
    top: auto;
    left: 50%;
    bottom: 16px;
    transform: translateX(-50%);
  }

  #mirrorHud.cockpit-layout {
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    transform: none;
  }

  #mirrorHud.cockpit-layout #mirrorCenter {
    top: 12px;
  }

  #mirrorHud.cockpit-layout #mirrorLeft {
    top: 88px;
    left: 8px;
  }

  #mirrorHud.cockpit-layout #mirrorRight {
    top: 88px;
    right: 8px;
  }

  #miniMap {
    right: 10px;
    top: max(10px, var(--sat));
    bottom: auto;
    width: min(24vw, 120px);
  }

  #miniMap.expanded {
    right: 10px;
    top: max(10px, var(--sat));
    bottom: auto;
    width: min(60vw, 300px);
  }

  .mirrorPane {
    width: min(21vw, 92px);
  }

  .mirrorCenter {
    width: min(28vw, 126px);
  }
}

/* ==========================================================
   RESPONSIVE: Touch / Mobile (single consolidated block)
   ========================================================== */
@media (hover: none) and (pointer: coarse) {
  /* Lock body for fullscreen game feel */
  html {
    overflow-y: auto;
    min-height: calc(100% + 2px);
  }

  body {
    position: fixed;
    top: 0;
    left: 0;
  }

  #app {
    height: 100dvh;
  }

  /* Rotate overlay */
  #rotateOverlay {
    position: fixed;
    inset: 0;
    z-index: var(--z-rotate-overlay);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: rgba(4, 8, 12, 0.58);
  }

  body.mobile-portrait #rotateOverlay {
    display: flex;
  }

  body.mobile-portrait #mobileControls,
  body.mobile-portrait #miniMap,
  body.mobile-portrait #wheelHud,
  body.mobile-portrait #mirrorHud {
    display: none !important;
  }

  /* Hide desktop HUD status on mobile */
  #hud .status {
    display: none;
  }

  /* Mobile Controls: fixed position layout */
  #mobileControls {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: 0;
    display: block;
    z-index: var(--z-mobile-controls);
    pointer-events: none;
  }

  .mobileClusterCenter {
    display: none;
  }

  .mobileCluster {
    flex-direction: column;
    gap: var(--gap-md);
    pointer-events: auto;
  }

  /* Left cluster: steer + view/reset */
  .mobileClusterLeft {
    position: fixed;
    left: calc(var(--gap-md) + var(--sal));
    bottom: calc(var(--gap-md) + var(--sab));
    width: min(44vw, 260px);
    max-width: calc(100vw - 200px);
    align-items: stretch;
    pointer-events: auto;
    z-index: var(--z-mobile-controls);
  }

  /* Right cluster: signal + drive buttons */
  .mobileClusterRight {
    position: fixed;
    right: calc(var(--gap-md) + var(--sar));
    bottom: calc(var(--gap-md) + var(--sab));
    width: auto;
    align-items: flex-end;
    gap: 8px;
    pointer-events: auto;
    z-index: var(--z-mobile-controls);
  }

  /* Steer row: grid layout for steer zone + view/reset buttons */
  .mobileSteerRow {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end;
    gap: 8px;
  }

  .mobileSteerZone {
    min-height: 88px;
  }

  .mobileViewResetStack {
    flex-direction: column;
    gap: 8px;
  }

  .mobileViewResetStack .mobileControl {
    min-width: 56px;
    min-height: 40px;
    padding: 0 10px;
    font-size: 12px;
  }

  /* Drive stack: horizontal row on right side */
  .mobileDriveStack {
    width: auto;
    max-width: none;
  }

  .mobileDriveStackRight {
    display: flex;
    flex-direction: row;
    align-items: flex-end;
    justify-content: flex-end;
    gap: var(--gap-md);
  }

  .mobileHandbrake {
    display: none;
  }

  .mobileSignalLever {
    width: 58px;
    height: 46px;
    margin-bottom: var(--gap-sm);
    align-self: flex-end;
    margin-left: auto;
  }

  .signalLeverLabel {
    font-size: 10px;
    font-weight: 700;
    color: rgba(255, 255, 255, 0.56);
  }

  /* Throttle / Brake / Reverse: sized for comfortable touch */
  .mobileControlTall,
  .mobileThrottle,
  .mobileBrake,
  .mobileThrottleLever,
  .mobileReverse {
    min-width: 54px;
    min-height: 60px;
  }

  body.mobile-linear-throttle-enabled .mobileThrottle {
    display: none;
  }

  body.mobile-linear-throttle-enabled .mobileThrottleLever {
    display: block;
  }

  body:not(.mobile-linear-throttle-enabled) .mobileThrottleLever {
    display: none;
  }

  .mobileThrottle {
    width: clamp(44px, 10.5vw, 54px);
    min-width: clamp(44px, 10.5vw, 54px);
    min-height: clamp(80px, 18vw, 100px);
    padding: 0 10px;
    border-radius: 20px 20px 14px 14px;
    font-size: 13px;
    font-weight: 700;
    letter-spacing: 0.04em;
    background:
      linear-gradient(180deg, rgba(60, 164, 112, 0.96), rgba(22, 88, 58, 0.98)),
      repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.08) 0 5px, rgba(255, 255, 255, 0.02) 5px 10px);
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.14),
      inset 0 -3px 0 rgba(0, 0, 0, 0.18),
      0 10px 18px rgba(0, 0, 0, 0.24);
  }

  .mobileThrottleLever {
    --throttle-level: 0;
    position: relative;
    width: clamp(44px, 10.5vw, 54px);
    min-width: clamp(44px, 10.5vw, 54px);
    height: clamp(108px, 24vw, 136px);
    padding: 8px 0 10px;
    border: 1px solid rgba(88, 182, 126, 0.65);
    border-radius: 22px;
    background:
      linear-gradient(180deg, rgba(20, 42, 31, 0.95), rgba(11, 24, 18, 0.98)),
      rgba(255, 255, 255, 0.04);
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.1),
      0 12px 22px rgba(0, 0, 0, 0.28);
    backdrop-filter: blur(10px);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
  }

  .mobileThrottleLever[data-active="true"] {
    border-color: rgba(126, 226, 160, 0.9);
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.16),
      0 0 0 1px rgba(108, 212, 146, 0.24),
      0 14px 24px rgba(0, 0, 0, 0.32);
  }

  .mobileThrottleLeverLabel {
    display: block;
    text-align: center;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.04em;
    color: rgba(223, 245, 232, 0.92);
    pointer-events: none;
  }

  .mobileThrottleLeverTrack {
    position: absolute;
    inset: 28px 8px 10px;
    border-radius: 16px;
    background:
      linear-gradient(180deg, rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0.02)),
      rgba(4, 12, 8, 0.5);
    box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.34);
    overflow: hidden;
  }

  body.mobile-linear-throttle-invert .mobileThrottleLeverTrack {
    transform: rotate(180deg);
  }

  .mobileThrottleLeverFill {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: calc(var(--throttle-level) * 100%);
    min-height: calc(var(--throttle-level) * 18px);
    background:
      linear-gradient(180deg, rgba(113, 234, 157, 0.42), rgba(36, 154, 88, 0.88)),
      repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.1) 0 4px, rgba(255, 255, 255, 0.02) 4px 8px);
    transform-origin: bottom;
    pointer-events: none;
  }

  .mobileThrottleLeverKnob {
    position: absolute;
    left: 50%;
    bottom: calc(6px + var(--throttle-level) * (100% - 34px));
    width: 28px;
    height: 28px;
    border-radius: 50%;
    transform: translateX(-50%);
    background: linear-gradient(180deg, rgba(240, 255, 245, 0.96), rgba(146, 220, 171, 0.96));
    box-shadow:
      0 4px 10px rgba(0, 0, 0, 0.36),
      inset 0 1px 0 rgba(255, 255, 255, 0.8);
    pointer-events: none;
  }

  .mobileBrakePrimary {
    min-width: clamp(76px, 18vw, 92px);
    min-height: clamp(66px, 14vw, 78px);
    padding: 0 16px;
    border-radius: 20px 20px 14px 14px;
    font-size: 14px;
    font-weight: 700;
    letter-spacing: 0.05em;
    background:
      linear-gradient(180deg, rgba(214, 88, 88, 0.96), rgba(120, 26, 26, 0.96)),
      repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.08) 0 6px, rgba(255, 255, 255, 0.02) 6px 12px);
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.14),
      inset 0 -3px 0 rgba(0, 0, 0, 0.18),
      0 10px 18px rgba(0, 0, 0, 0.24);
  }

  .mobileReverse {
    min-width: 50px;
    min-height: 56px;
    border-radius: 16px;
    padding: 0 12px;
  }

  /* Wheel HUD: small on mobile */
  #wheelHud {
    left: 50%;
    bottom: calc(var(--gap-md) + var(--sab));
    top: auto;
    transform: translateX(-50%);
    width: 52px;
    height: 52px;
    z-index: var(--z-wheel-hud);
  }

  /* Mirrors: top center */
  #mirrorHud {
    top: calc(8px + var(--sat));
    left: 50%;
    bottom: auto;
    transform: translateX(-50%);
    gap: var(--gap-sm);
    z-index: 12;
  }

  .mirrorPane {
    width: clamp(72px, 15vw, 90px);
    border-width: 2px;
    border-radius: 12px 12px 16px 16px;
  }

  .mirrorCenter {
    width: clamp(104px, 22vw, 128px);
  }

  .mirrorLabel {
    left: 8px;
    bottom: 6px;
    padding: 3px 6px;
    font-size: 9px;
  }

  #mirrorHud.cockpit-layout #mirrorCenter {
    top: calc(8px + var(--sat));
  }

  #mirrorHud.cockpit-layout #mirrorLeft {
    top: calc(56px + var(--sat));
    left: calc(6px + var(--sal));
  }

  #mirrorHud.cockpit-layout #mirrorRight {
    top: calc(56px + var(--sat));
    right: calc(6px + var(--sar));
  }

  /* Cockpit signal */
  #cockpitSignalHud {
    top: calc(60px + var(--sat));
    gap: 8px;
  }

  .cockpitSignalLamp {
    min-width: 68px;
    padding: 6px 10px;
    font-size: 10px;
  }

  /* Mini map: top-right on mobile, below mirrors */
  #miniMap {
    right: calc(var(--gap-md) + var(--sar));
    top: calc(72px + var(--sat));
    bottom: auto;
    width: min(20vw, 108px);
    border-width: 2px;
    border-radius: 16px;
    z-index: 12;
  }

  #miniMap.expanded {
    inset: calc(8px + var(--sat)) calc(8px + var(--sar)) calc(8px + var(--sab)) calc(8px + var(--sal));
    width: auto;
    height: auto;
    border-radius: 16px;
  }

  #mirrorHud.cockpit-layout ~ #miniMap {
    top: calc(110px + var(--sat));
  }

  #mirrorHud.cockpit-layout ~ #miniMap.expanded {
    top: calc(8px + var(--sat));
  }

  .miniMapLabel {
    left: 8px;
    top: 8px;
    padding: 3px 7px;
    font-size: 10px;
  }

  #miniMapClose {
    top: 8px;
    right: 8px;
    width: 24px;
    height: 24px;
    font-size: 16px;
  }

  /* UI panel: respect safe areas */
  #ui {
    top: calc(var(--gap-md) + var(--sat));
    left: calc(var(--gap-md) + var(--sal));
    right: calc(var(--gap-md) + var(--sar));
    max-width: calc(100vw - 20px - var(--sal) - var(--sar));
  }

  #uiToggle {
    display: inline-flex;
    position: relative;
    z-index: var(--z-ui-toggle);
  }

  #collapsedQuickActions {
    gap: var(--gap-sm);
    max-width: min(260px, calc(100vw - 20px));
  }

  .quickViewBtn {
    padding: 7px 10px;
    font-size: 12px;
  }

  .panel {
    width: min(420px, calc(100vw - 20px - var(--sal) - var(--sar)));
    max-height: calc(100vh - 74px - var(--sat) - var(--sab));
    padding: 12px;
    border-radius: 12px;
  }

  .row {
    gap: 8px;
    margin-bottom: 10px;
  }

  .controlGrid {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .span-3,
  .span-4,
  .span-8,
  .span-9,
  .span-12 {
    grid-column: auto;
  }

  .primary-controls,
  .secondary-controls {
    grid-template-columns: 1fr;
  }

  .row.compact label,
  .range-card,
  .action-btn,
  .toggle-card {
    width: 100%;
  }

  .control-field,
  .range-card.control-range,
  .toggle-card,
  .action-btn {
    min-height: 40px;
  }

  h1 {
    font-size: 17px;
    margin-bottom: 8px;
  }

  .tips {
    display: none;
  }

  p,
  .tips,
  .tips span,
  .tips strong {
    font-size: 12px;
  }
}

/* ==========================================================
   RESPONSIVE: Very small mobile (max-width: 420px)
   ========================================================== */
@media (hover: none) and (pointer: coarse) and (max-width: 420px) {
  #wheelHud {
    display: none !important;
  }

  .mobileSteerZone {
    min-height: 72px;
  }

  .mobileBrakePrimary {
    min-height: 64px;
  }

  .mobileClusterLeft {
    width: min(42vw, 200px);
    max-width: calc(100vw - 180px);
  }
}
