:root {
  --bg-space: #060913;
  --bg-panel: rgba(10, 15, 29, 0.72);
  --bg-control: rgba(16, 24, 44, 0.85);
  --bg-subpanel: rgba(8, 14, 28, 0.6);
  --accent: #38bdf8;
  --accent-glow: rgba(56, 189, 248, 0.28);
  --accent-active: #0284c7;
  --accent-hover: #7dd3fc;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --text-faint: #64748b;
  --text-highlight: #38bdf8;
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
}

body, html {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg-space);
  font-family: var(--font-family);
  color: var(--text-main);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#canvas-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 1;
}

#render-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* Glassmorphic Panel */
.glass-panel {
  background: var(--bg-panel);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: none;
  outline: none;
  border-radius: 16px;
  color: var(--text-main);
  z-index: 10;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.spin {
  animation: spin 1s linear infinite;
  display: inline-block;
}

/* Left-Arranged Scrollable Master Sidebar HUD - Canonical iOS Modal Spring */
.left-hud-sidebar {
  position: absolute;
  top: 1rem;
  left: 1rem;
  width: 310px;
  height: calc(100vh - 2rem);
  max-height: calc(100vh - 2rem);
  max-width: calc(100vw - 2rem);
  display: flex;
  flex-direction: column;
  padding: 1.1rem;
  gap: 0.85rem;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: rgba(56, 189, 248, 0.25) transparent;
  border-radius: 16px !important;
  transform-origin: 0 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.left-hud-sidebar::-webkit-scrollbar { width: 4px; }
.left-hud-sidebar::-webkit-scrollbar-button {
  display: none !important;
  width: 0 !important;
  height: 0 !important;
}
.left-hud-sidebar::-webkit-scrollbar-thumb {
  background: rgba(56, 189, 248, 0.3);
  border-radius: 4px;
}
.left-hud-sidebar::-webkit-scrollbar-track {
  background: transparent;
}

/* Inner elements container for staged iOS cross-fading */
.left-hud-sidebar .hud-title-row,
.left-hud-sidebar .hud-section,
.left-hud-sidebar .seg-control,
.left-hud-sidebar .hud-header {
  transition: opacity 0.22s ease-out;
  opacity: 1;
  min-width: 272px;
  box-sizing: border-box;
  flex-shrink: 0;
}

.left-hud-sidebar .action-btn {
  transition: opacity 0.22s ease-out, background 0.15s ease, transform 0.1s ease, color 0.15s ease;
  opacity: 1;
  box-sizing: border-box;
  flex-shrink: 0;
}

.left-hud-sidebar .hud-top-actions > :not(#btn-collapse-hud) {
  transition: opacity 0.22s ease-out;
  opacity: 1;
}

/* ── Animation State: Disable overflow slider & scrollbars to eliminate jank ── */
.left-hud-sidebar.animating {
  overflow: hidden !important;
  pointer-events: none !important;
  user-select: none !important;
}

.left-hud-sidebar.animating * {
  pointer-events: none !important;
}

/* ── Collapsed Button State (2.75rem x 2.75rem pill) ── */
.left-hud-sidebar.collapsed {
  width: 2.75rem !important;
  height: 2.75rem !important;
  max-height: 2.75rem !important;
  padding: 0 !important;
  pointer-events: auto;
  cursor: pointer;
  border-radius: 16px !important;
  overflow: hidden !important;
  animation: none !important;
  transform: none !important;
  opacity: 0.5 !important;
  box-shadow: none !important;
  background: var(--bg-panel);
  transition:
    opacity 0.25s ease,
    box-shadow 0.25s ease,
    background 0.2s ease,
    transform 0.2s cubic-bezier(0.32, 0.72, 0, 1);
}

.left-hud-sidebar.collapsed:hover {
  opacity: 1.0 !important;
  background: var(--bg-control);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5) !important;
  transform: none !important;
}

/* Content hidden immediately during collapse */
.left-hud-sidebar.collapsed .hud-title-row,
.left-hud-sidebar.collapsed .hud-section,
.left-hud-sidebar.collapsed .action-btn,
.left-hud-sidebar.collapsed .seg-control {
  opacity: 0 !important;
  pointer-events: none;
  display: none !important;
}

/* Header collapses to fill entire button area */
.left-hud-sidebar.collapsed .hud-header {
  padding: 0 !important;
  margin: 0 !important;
  border: none !important;
  gap: 0 !important;
  width: 100% !important;
  height: 100% !important;
  min-width: 0 !important;
  max-width: 100% !important;
  flex-shrink: 1 !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
}

.left-hud-sidebar.collapsed .hud-top-actions {
  width: 100% !important;
  height: 100% !important;
  min-width: 0 !important;
  max-width: 100% !important;
  gap: 0 !important;
  padding: 0 !important;
  margin: 0 !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
}

/* Hide all action buttons except the morph toggle button */
.left-hud-sidebar.collapsed .hud-top-actions > :not(#btn-collapse-hud) {
  display: none !important;
}

.left-hud-sidebar.collapsed #btn-collapse-hud {
  width: 100% !important;
  height: 100% !important;
  min-width: 0 !important;
  max-width: 100% !important;
  border-radius: 16px !important;
  border: none !important;
  flex: unset !important;
  background: transparent !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  color: var(--accent) !important;
  opacity: 1 !important;
  visibility: visible !important;
  pointer-events: auto !important;
  cursor: pointer !important;
  padding: 0 !important;
  margin: 0 !important;
}

/* ── Morph icon cross-fade ── */
.morph-icon-open,
.morph-icon-closed {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  color: var(--text-muted);
  transition: opacity 0.15s ease, transform 0.15s ease;
  pointer-events: none;
}

.morph-icon-open svg,
.morph-icon-closed svg {
  display: block;
  stroke: currentColor;
}

.morph-icon-open  { opacity: 1; transform: scale(1); visibility: visible; }
.morph-icon-closed { opacity: 0; transform: scale(0.6); visibility: hidden; }

.left-hud-sidebar.collapsed .morph-icon-open  { display: none !important; }
.left-hud-sidebar.collapsed .morph-icon-closed {
  position: static !important;
  display: inline-flex !important;
  align-items: center !important;
  justify-content: center !important;
  opacity: 1 !important;
  visibility: visible !important;
  transform: none !important;
  color: var(--accent) !important;
  width: 100% !important;
  height: 100% !important;
}

.left-hud-sidebar.collapsed .morph-icon-closed svg {
  display: block !important;
  visibility: visible !important;
  stroke: var(--accent) !important;
  color: var(--accent) !important;
  width: 20px !important;
  height: 20px !important;
}

#btn-collapse-hud {
  position: relative;
}

/* HUD Header: Two Rows (Title on top, Buttons below) */
.hud-header {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding-bottom: 0.65rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  flex-shrink: 0;
}

.hud-title-row {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.3rem;
}

.hud-title {
  font-size: 1.08rem;
  font-weight: 300;
  letter-spacing: 0.04em;
  color: var(--text-main);
  text-transform: none;
}

.hud-top-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  gap: 0.35rem;
}

.hud-top-actions .icon-btn {
  flex: 1;
  min-width: 0;
  width: auto;
  height: 2.15rem;
}

/* Sidebar Group Category Header with In-Line Rule */
.hud-group-title {
  font-size: 0.82rem;
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0.02em;
  color: var(--accent);
  margin-top: 0.85rem;
  margin-bottom: 0.25rem;
  padding-bottom: 0;
  border-bottom: none;
  display: flex;
  align-items: center;
  gap: 0.65rem;
  width: 100%;
}

.hud-group-title::after {
  content: "";
  flex: 1;
  height: 1px;
  background: rgba(255, 255, 255, 0.1);
}

.left-hud-sidebar .hud-group-title {
  transition: opacity 0.22s ease-out;
  opacity: 1;
  min-width: 272px;
  box-sizing: border-box;
  flex-shrink: 0;
}

.left-hud-sidebar.collapsed .hud-group-title {
  display: none !important;
}

/* Stepper Dropdown Row with Quick Changing Left/Right Chevrons */
.select-stepper-row {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  width: 100%;
}

.stepper-btn {
  width: 2rem !important;
  height: 2rem !important;
  min-width: 2rem !important;
  flex-shrink: 0;
  border-radius: 6px;
  background: var(--bg-subpanel);
  border: none !important;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, color 0.15s ease;
}

.stepper-btn:hover {
  background: rgba(255, 255, 255, 0.12) !important;
  color: #ffffff !important;
}

.select-stepper-row .custom-select {
  flex: 1;
  min-width: 0;
}

/* Perfectly Centered Icon Buttons */
.icon-btn {
  background: var(--bg-subpanel);
  border: none;
  outline: none;
  color: var(--text-muted);
  width: 2.15rem;
  height: 2.15rem;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
  flex-shrink: 0;
  padding: 0;
}

.icon-btn svg,
.icon-btn i {
  width: 1rem;
  height: 1rem;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  margin: auto;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.12);
  color: #ffffff;
}

.icon-btn.active {
  background: var(--accent-active);
  color: #ffffff;
}

/* Top-Right Telemetry HUD */
.telemetry-hud {
  position: absolute;
  top: 1rem;
  right: 1rem;
  padding: 0.85rem 1rem;
  min-width: 210px;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  transform-origin: top right;
  border-radius: 14px;
  background: var(--bg-panel);
  box-shadow: none !important;
  opacity: 0.5;
  transition:
    opacity 0.25s ease,
    box-shadow 0.25s ease,
    background 0.2s ease;
  visibility: visible;
  overflow: hidden;
  will-change: transform, opacity, width, height;
}

.telemetry-hud:hover {
  opacity: 1.0 !important;
  background: var(--bg-control);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5) !important;
}

.telemetry-hud.hidden {
  display: none !important;
  opacity: 0 !important;
  pointer-events: none !important;
  visibility: hidden !important;
}

.telemetry-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 0.35rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.telemetry-title {
  font-size: 0.74rem;
  text-transform: none;
  letter-spacing: 0.02em;
  color: var(--text-muted);
  font-weight: 500;
}

.telemetry-grid {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
}

.telemetry-item {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}

.telemetry-label {
  color: var(--text-faint);
  font-size: 0.68rem;
  font-weight: 400;
  text-transform: none;
}

.telemetry-val {
  font-weight: 500;
  color: var(--text-highlight);
}

/* HUD Form Sections */
.hud-section {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  flex-shrink: 0;
}

.control-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.76rem;
  color: var(--text-muted);
  margin-bottom: 0.15rem;
}

/* Segmented Control Buttons */
.seg-control {
  display: flex;
  background: var(--bg-subpanel);
  border: none;
  padding: 3px;
  border-radius: 8px;
  gap: 2px;
}

.seg-btn {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-muted);
  padding: 0.42rem 0.4rem;
  font-size: 0.78rem;
  font-family: inherit;
  font-weight: 400;
  border-radius: 6px;
  cursor: pointer;
  text-align: center;
  text-transform: none;
  transition: background 0.15s ease, color 0.15s ease;
}

.seg-btn:hover {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-main);
}

.seg-btn.active {
  background: var(--accent);
  color: #020617;
  font-weight: 500;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

/* Action Buttons */
.action-btn {
  width: 100%;
  background: var(--bg-subpanel);
  border: none;
  outline: none;
  color: var(--accent);
  padding: 0.55rem 1rem;
  font-size: 0.82rem;
  font-family: inherit;
  font-weight: 500;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  transition: background 0.15s ease, color 0.15s ease;
}

.action-btn:hover {
  background: rgba(56, 189, 248, 0.16);
  color: #ffffff;
}

.action-btn.primary {
  background: var(--accent);
  color: #020617;
  font-weight: 500;
}

.action-btn.primary:hover {
  background: var(--accent-hover);
  color: #020617;
}

.action-btn.primary.running {
  background: #f43f5e;
  color: #ffffff;
}

.action-btn-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.4rem;
  width: 100%;
}

.action-btn-group .action-btn {
  min-width: 0 !important;
  width: 100% !important;
}

/* File Upload Label with Clean Background */
.file-upload-label {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  width: 100%;
  background: var(--bg-subpanel);
  border: none;
  color: var(--text-main);
  padding: 0.52rem 0.8rem;
  font-size: 0.78rem;
  font-weight: 400;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
  box-sizing: border-box;
}

.file-upload-label:hover {
  background: rgba(255, 255, 255, 0.12);
  color: #ffffff;
}

/* Toggle Switches */
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.78rem;
  color: var(--text-muted);
  text-transform: none;
}

.switch {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider-round {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: var(--bg-control);
  border: none;
  transition: background-color 0.2s ease;
  border-radius: 20px;
}

.slider-round:before {
  position: absolute;
  content: "";
  height: 14px;
  width: 14px;
  left: 3px;
  bottom: 3px;
  background-color: #ffffff;
  transition: transform 0.2s ease;
  border-radius: 50%;
}

input:checked + .slider-round {
  background-color: var(--accent-active);
}

input:checked + .slider-round:before {
  transform: translateX(16px);
}

/* Custom Dropdown Select */
.custom-select {
  background: var(--bg-subpanel);
  border: none;
  color: var(--text-main);
  font-size: 0.78rem;
  font-weight: 400;
  padding: 0.45rem 0.6rem;
  border-radius: 8px;
  outline: none;
  cursor: pointer;
  width: 100%;
  font-family: inherit;
  transition: background 0.15s ease;
}

.custom-select:hover {
  background: rgba(255, 255, 255, 0.1);
}

.custom-select:focus {
  background: rgba(255, 255, 255, 0.12);
}

.custom-select option {
  background: #090e1c;
  color: #ffffff;
}

/* Range Sliders with Increased Label Gaps */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
  margin-top: 0.25rem;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  transition: box-shadow 0.15s ease, background 0.15s ease;
}

input[type="range"]::-webkit-slider-thumb:hover {
  box-shadow: 0 0 10px var(--accent);
  background: var(--accent-hover);
}

/* Info Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.28s ease;
}

.modal-backdrop.show {
  opacity: 1;
  pointer-events: auto;
}

.modal-content {
  width: 92%;
  max-width: 680px;
  max-height: 85vh;
  padding: 0;
  border-radius: 18px;
  background: var(--bg-control);
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.1);
  overflow: hidden;
  position: relative;
  will-change: transform, opacity;
  transform-origin: center center;
}

.modal-inner {
  padding: 1.4rem;
  width: 100%;
  box-sizing: border-box;
  overflow-y: auto;
  overflow-x: hidden;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: rgba(56, 189, 248, 0.25) transparent;
}

.modal-inner::-webkit-scrollbar { width: 4px; }
.modal-inner::-webkit-scrollbar-button {
  display: none !important;
  width: 0 !important;
  height: 0 !important;
}
.modal-inner::-webkit-scrollbar-thumb {
  background: rgba(56, 189, 248, 0.3);
  border-radius: 4px;
}
.modal-inner::-webkit-scrollbar-track {
  background: transparent;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-title {
  font-size: 1.05rem;
  font-weight: 500;
  color: var(--accent);
}

.modal-body {
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  font-weight: 400;
}

/* Modal Table Styling */
.table-responsive {
  width: 100%;
  overflow-x: auto;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(0, 0, 0, 0.25);
  scrollbar-width: thin;
  scrollbar-color: rgba(56, 189, 248, 0.25) transparent;
}

.table-responsive::-webkit-scrollbar { height: 4px; }
.table-responsive::-webkit-scrollbar-button {
  display: none !important;
  width: 0 !important;
  height: 0 !important;
}
.table-responsive::-webkit-scrollbar-thumb {
  background: rgba(56, 189, 248, 0.3);
  border-radius: 4px;
}
.table-responsive::-webkit-scrollbar-track {
  background: transparent;
}

.modal-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.76rem;
  text-align: left;
}

.modal-table th,
.modal-table td {
  padding: 0.45rem 0.65rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  white-space: nowrap;
}

.modal-table th {
  background: rgba(255, 255, 255, 0.04);
  color: var(--accent);
  font-weight: 500;
}

.modal-table td {
  color: var(--text-muted);
}

.stat-best { color: #34d399; font-weight: 500; }
.stat-good { color: #38bdf8; font-weight: 500; }
.stat-bad { color: #fbbf24; font-weight: 500; }
.stat-chaos { color: #f43f5e; font-weight: 500; }

.modal-section-title {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--accent);
}

.modal-grid-ref {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.6rem;
}

.modal-ref-card {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.07);
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.modal-ref-head {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--accent);
}

.modal-ref-list {
  font-size: 0.72rem;
  line-height: 1.4;
  color: var(--text-muted);
}
