:root {
  --bg: #04060d;
  --panel: rgba(12, 16, 28, 0.82);
  --panel-border: rgba(120, 160, 255, 0.18);
  --text: #d8e2f5;
  --muted: #7e8aa8;
  --accent: #6aa0ff;
  --accent2: #ff9a5a;
  --panel-width: 312px;
  --toggle-width: 28px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  height: 100dvh; /* track the visual viewport on mobile (address-bar show/hide) */
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

#gl {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: grab;
  touch-action: none; /* one-finger orbit + pinch zoom, no browser gestures */
}
#gl:active { cursor: grabbing; }

#hud {
  position: fixed;
  top: calc(12px + env(safe-area-inset-top));
  left: calc(16px + env(safe-area-inset-left));
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  color: var(--muted);
  letter-spacing: 0.02em;
  pointer-events: none;
  text-shadow: 0 1px 4px #000;
}
#hud span { margin-right: 14px; }
#hud-status { color: var(--accent); }

#panel {
  position: fixed;
  top: 0;
  right: 0;
  width: var(--panel-width);
  height: 100%;
  height: 100dvh;
  padding: 18px 18px 24px;
  padding-top: calc(18px + env(safe-area-inset-top));
  padding-right: calc(18px + env(safe-area-inset-right));
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
  background: var(--panel);
  border-left: 1px solid var(--panel-border);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 10;
}
/* translateX(100%) slides the drawer off; visibility:hidden (flipped after the
   slide) makes iOS Safari actually STOP painting it. Without visibility, a
   transformed element that still carries backdrop-filter keeps rendering a
   blurred "ghost" off-screen (a long-standing WebKit bug) — so on iPhone the
   panel appeared not to hide at all. visibility also hides it if iOS ignores
   the transform entirely. pointer-events:none keeps the off-screen panel from
   swallowing taps. */
#panel.hidden {
  transform: translateX(100%);
  visibility: hidden;
  pointer-events: none;
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1),
    visibility 0s linear 0.28s;
}

#panel header { margin-bottom: 14px; }
#panel h1 {
  font-size: 19px;
  font-weight: 650;
  margin: 0;
  letter-spacing: 0.01em;
  background: linear-gradient(90deg, var(--accent2), var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
#panel .sub {
  margin: 2px 0 0;
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.group-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  margin: 16px 0 8px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--panel-border);
}

.ctrl { margin-bottom: 11px; }
.ctrl .row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 4px;
}
.ctrl label { font-size: 12.5px; color: var(--text); }
.ctrl .val {
  font-size: 12px;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 16px; /* tall hit area; the track is painted as a thin centered band */
  margin: 0;
  background: linear-gradient(90deg, var(--accent), #2a3550) center / 100% 3px no-repeat;
  border-radius: 3px;
  outline: none;
  cursor: pointer;
  touch-action: pan-y; /* vertical drags scroll the panel; horizontal drags move the slider */
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  margin-top: -5.5px;
  border-radius: 50%;
  background: #eef3ff;
  border: 2px solid var(--accent);
  cursor: pointer;
  box-shadow: 0 0 6px rgba(106, 160, 255, 0.7);
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 3px;
  background: transparent;
  border: 0;
}
input[type="range"]::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border: 2px solid var(--accent);
  border-radius: 50%;
  background: #eef3ff;
  cursor: pointer;
  box-shadow: 0 0 6px rgba(106, 160, 255, 0.7);
}
input[type="range"]::-moz-range-track {
  height: 3px;
  border-radius: 3px;
  background: transparent; /* the band painted on the element already shows the track */
  border: 0;
}
input[type="range"]::-moz-range-progress {
  background: transparent;
}

select, .ctrl.check label {
  width: 100%;
}
select {
  width: 100%;
  padding: 7px 9px;
  font-size: 12.5px;
  color: var(--text);
  background: rgba(20, 26, 42, 0.9);
  border: 1px solid var(--panel-border);
  border-radius: 7px;
  outline: none;
  cursor: pointer;
  touch-action: manipulation;
}

.ctrl.check .row { cursor: pointer; }
.ctrl.check input { accent-color: var(--accent); width: 15px; height: 15px; }

.buttons {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}
button {
  flex: 1;
  padding: 9px 8px;
  font-size: 12.5px;
  font-weight: 550;
  color: var(--text);
  background: rgba(28, 36, 58, 0.9);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s, border-color 0.15s;
}
button:hover { background: rgba(40, 52, 84, 0.95); border-color: var(--accent); }
button.primary { background: linear-gradient(90deg, #2b4a8f, #2f6bd0); }
button.active { border-color: var(--accent2); color: #ffd9b8; }

#panel-toggle {
  position: fixed;
  right: var(--panel-width);
  top: calc(16px + env(safe-area-inset-top));
  width: var(--toggle-width);
  height: 40px;
  flex: none;
  border-radius: 8px 0 0 8px;
  border-right: none;
  background: var(--panel);
  transition: right 0.28s cubic-bezier(0.4, 0, 0.2, 1),
    transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 11;
}
#panel.hidden + #panel-toggle {
  right: env(safe-area-inset-right);
  transform: rotate(180deg);
}

#error-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(2, 4, 10, 0.92);
  z-index: 100;
}
#error-overlay[hidden] { display: none; }
#error-overlay .box {
  max-width: 440px;
  padding: 28px 32px;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: var(--panel);
}
#error-overlay h2 { margin: 0 0 10px; color: var(--accent2); }
#error-overlay p { color: var(--muted); line-height: 1.5; font-size: 14px; }

/* Slim scrollbar */
#panel::-webkit-scrollbar { width: 8px; }
#panel::-webkit-scrollbar-thumb {
  background: rgba(106, 160, 255, 0.25);
  border-radius: 8px;
}

/* ---- Touch devices: roomier hit targets ---------------------------------- */
@media (pointer: coarse) {
  input[type="range"] { height: 26px; }
  input[type="range"]::-webkit-slider-thumb { width: 20px; height: 20px; margin-top: -8.5px; }
  input[type="range"]::-moz-range-thumb { width: 20px; height: 20px; }
  .ctrl.check input { width: 20px; height: 20px; }
  .ctrl { margin-bottom: 14px; }
  select { padding: 10px 10px; font-size: 13.5px; }
  button { padding: 12px 8px; font-size: 13.5px; min-height: 44px; }
}

/* ---- Phones (either orientation): compact drawer, starts collapsed ------- */
@media (max-width: 560px), (max-height: 560px) {
  :root {
    --panel-width: min(90vw, 330px);
    --toggle-width: 36px;
  }

  #panel {
    padding-left: 16px;
    padding-right: calc(16px + env(safe-area-inset-right));
  }
  #panel h1 { font-size: 21px; }
  #panel .sub { font-size: 10.5px; }
  .ctrl label { font-size: 13.5px; }
  #hud { font-size: 11px; }

  #panel-toggle {
    height: 54px;
    font-size: 16px;
  }
}
