:root {
  --bg: #07080c;
  --panel: #0d0f16;
  --panel-2: #11141d;
  --line: rgba(255,255,255,0.07);
  --line-2: rgba(255,255,255,0.12);
  --txt: #e7e9f0;
  --txt-dim: #9aa0b4;
  --txt-faint: #626a82;
  --accent: #6ea8ff;
  --accent-2: #8b7dff;
  --good: #5ad6a8;
  --mono: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, monospace;
  --sans: "General Sans", system-ui, -apple-system, sans-serif;
  --panel-w: 380px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--txt);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

#app {
  display: grid;
  grid-template-columns: 1fr var(--panel-w);
  height: 100vh;
  width: 100vw;
}

/* ---------- Stage ---------- */
#stage {
  position: relative;
  overflow: hidden;
  background: #000;
}
#gl { display: block; width: 100%; height: 100%; }

#stage-hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  padding: 20px 24px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}
.hud-logo {
  display: flex; align-items: center; gap: 9px;
  font-weight: 600; font-size: 15px; letter-spacing: -0.01em;
  color: #fff;
  text-shadow: 0 1px 12px rgba(0,0,0,0.5);
  background: rgba(10,12,18,0.32);
  backdrop-filter: blur(14px) saturate(1.2);
  -webkit-backdrop-filter: blur(14px) saturate(1.2);
  padding: 8px 13px 8px 11px;
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,0.1);
}
.hud-logo svg { color: var(--accent); }

.hud-controls { display: flex; gap: 8px; pointer-events: auto; flex-wrap: wrap; justify-content: flex-end; max-width: 60%; }
.hud-btn {
  pointer-events: auto;
  font-family: var(--sans);
  font-size: 12.5px; font-weight: 500;
  color: var(--txt);
  background: rgba(10,12,18,0.4);
  backdrop-filter: blur(14px) saturate(1.2);
  -webkit-backdrop-filter: blur(14px) saturate(1.2);
  border: 1px solid rgba(255,255,255,0.1);
  padding: 8px 13px;
  border-radius: 10px;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .1s;
}
.hud-btn:hover { background: rgba(30,34,48,0.6); border-color: var(--line-2); }
.hud-btn:active { transform: scale(0.96); }
.hud-btn[aria-pressed="true"] { color: var(--accent); border-color: rgba(110,168,255,0.4); }
.hud-btn .caret { font-size: 10px; opacity: 0.7; margin-left: 2px; }

/* ---- scene picker dropdown ---- */
.scene-picker { position: relative; pointer-events: auto; }
.scene-picker[aria-expanded] { }
.scene-menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 320px;
  max-width: min(80vw, 320px);
  background: rgba(12,14,20,0.78);
  backdrop-filter: blur(22px) saturate(1.3);
  -webkit-backdrop-filter: blur(22px) saturate(1.3);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 14px;
  padding: 12px;
  box-shadow: 0 18px 50px rgba(0,0,0,0.55), inset 0 1px 0 rgba(255,255,255,0.06);
  opacity: 0;
  transform: translateY(-6px) scale(0.98);
  transform-origin: top right;
  pointer-events: none;
  transition: opacity .16s ease, transform .16s ease;
  z-index: 40;
}
.scene-picker.open .scene-menu { opacity: 1; transform: translateY(0) scale(1); pointer-events: auto; }
.scene-menu-head {
  font-size: 11px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase;
  color: var(--txt-dim, #8b93a7); margin: 2px 4px 10px;
}
.scene-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; max-height: 56vh; overflow-y: auto; }
.scene-item {
  display: flex; flex-direction: column; align-items: flex-start; gap: 3px;
  text-align: left;
  font-family: var(--sans); font-size: 12.5px; font-weight: 500;
  color: var(--txt);
  background: rgba(255,255,255,0.03);
  border: 1px solid rgba(255,255,255,0.07);
  border-radius: 9px;
  padding: 9px 10px;
  cursor: pointer;
  transition: background .13s, border-color .13s, transform .08s;
}
.scene-item:hover { background: rgba(110,168,255,0.1); border-color: rgba(110,168,255,0.3); }
.scene-item:active { transform: scale(0.97); }
.scene-item.active { background: rgba(110,168,255,0.16); border-color: rgba(110,168,255,0.55); color: #fff; }
.scene-item .scene-name { line-height: 1.1; }
.scene-item .scene-mouse { font-size: 10px; color: var(--accent); opacity: 0.85; font-weight: 500; }
.scene-grid::-webkit-scrollbar { width: 8px; }
.scene-grid::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.12); border-radius: 8px; }

.hud-fps {
  position: absolute; right: 24px; bottom: 18px;
  font-family: var(--mono); font-size: 11px; color: var(--txt-faint);
  background: rgba(10,12,18,0.32); padding: 4px 8px; border-radius: 7px;
  pointer-events: none;
}

#gl-error {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  text-align: center; padding: 40px; color: var(--txt-dim); font-size: 14px; line-height: 1.6;
  background: var(--bg);
}
#gl-error[hidden] { display: none; }

/* ---------- Panel ---------- */
#panel {
  background: var(--panel);
  border-left: 1px solid var(--line);
  display: flex; flex-direction: column;
  height: 100vh;
}
.panel-head {
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.panel-title h1 { font-size: 16px; font-weight: 600; letter-spacing: -0.015em; }
.panel-title p { font-size: 12px; color: var(--txt-dim); margin-top: 5px; line-height: 1.5; }
.panel-title code { font-family: var(--mono); font-size: 11px; color: var(--accent); }
.panel-actions { display: flex; gap: 8px; margin-top: 15px; }
.action {
  flex: 1;
  font-family: var(--sans); font-size: 13px; font-weight: 500;
  color: var(--txt);
  background: var(--panel-2);
  border: 1px solid var(--line-2);
  padding: 9px 12px; border-radius: 9px; cursor: pointer;
  transition: background .15s, border-color .15s, transform .1s;
}
.action:hover { background: #161a25; border-color: rgba(255,255,255,0.2); }
.action:active { transform: scale(0.98); }
.action.primary {
  background: linear-gradient(180deg, var(--accent), #5a93f0);
  color: #08101f; font-weight: 600; border-color: transparent;
}
.action.primary:hover { background: linear-gradient(180deg, #82b6ff, var(--accent)); }

#controls {
  overflow-y: auto;
  flex: 1;
  padding: 4px 0 12px;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.13) transparent;
}
#controls::-webkit-scrollbar { width: 9px; }
#controls::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 5px; border: 2px solid var(--panel); }

/* footer */
.panel-foot {
  flex-shrink: 0;
  padding: 11px 20px;
  border-top: 1px solid var(--line);
  font-size: 11px;
  color: var(--txt-faint);
  line-height: 1.5;
}
.panel-foot a { color: var(--txt-dim); text-decoration: none; border-bottom: 1px solid var(--line-2); }
.panel-foot a:hover { color: var(--accent); border-bottom-color: var(--accent); }

/* groups */
.group { border-bottom: 1px solid var(--line); }
.group-head {
  display: flex; align-items: center; gap: 9px;
  padding: 13px 20px;
  cursor: pointer; user-select: none;
  transition: background .12s;
}
.group-head:hover { background: var(--panel-2); }
.group-head .chev {
  width: 14px; height: 14px; color: var(--txt-faint);
  transition: transform .2s ease;
}
.group.collapsed .chev { transform: rotate(-90deg); }
.group-head .g-name { font-size: 13px; font-weight: 600; letter-spacing: -0.01em; flex: 1; }
.group-head .g-dot { width: 7px; height: 7px; border-radius: 50%; }
.group-body { padding: 4px 20px 16px; display: grid; gap: 13px; }
.group.collapsed .group-body { display: none; }

/* control rows */
.ctrl { display: grid; gap: 6px; }
.ctrl-top { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.ctrl-label {
  font-size: 12px; color: var(--txt-dim); font-weight: 500;
  display: flex; align-items: center; gap: 5px;
  cursor: help;
}
.ctrl-label .conf {
  font-size: 8.5px; font-weight: 600; padding: 1px 4px; border-radius: 4px;
  text-transform: uppercase; letter-spacing: 0.03em; line-height: 1.4;
}
.conf.confirmed { background: rgba(90,214,168,0.14); color: #6fe0b4; }
.conf.reverse   { background: rgba(110,168,255,0.14); color: #8ab9ff; }
.conf.inferred  { background: rgba(255,196,110,0.13); color: #ffcb7a; }
.ctrl-val {
  font-family: var(--mono); font-size: 11.5px; color: var(--txt);
  background: var(--panel-2); padding: 2px 7px; border-radius: 6px;
  border: 1px solid var(--line); min-width: 52px; text-align: right;
}
.ctrl-val[contenteditable] { outline: none; cursor: text; }
.ctrl-val[contenteditable]:focus { border-color: var(--accent); color: #fff; }

input[type=range] {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 4px; border-radius: 3px;
  background: linear-gradient(90deg, var(--accent) var(--fill,50%), rgba(255,255,255,0.1) var(--fill,50%));
  cursor: pointer;
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 15px; height: 15px; border-radius: 50%;
  background: #fff; border: 2px solid #2a3245;
  box-shadow: 0 1px 5px rgba(0,0,0,0.4);
  transition: transform .1s;
}
input[type=range]::-webkit-slider-thumb:hover { transform: scale(1.15); }
input[type=range]::-moz-range-thumb {
  width: 15px; height: 15px; border-radius: 50%; background: #fff; border: 2px solid #2a3245; cursor: pointer;
}

/* toggle */
.toggle { position: relative; width: 38px; height: 22px; flex-shrink: 0; }
.toggle input { opacity: 0; width: 0; height: 0; position: absolute; }
.toggle .track {
  position: absolute; inset: 0; border-radius: 11px;
  background: rgba(255,255,255,0.12); transition: background .2s; cursor: pointer;
}
.toggle .track::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 16px; height: 16px; border-radius: 50%; background: #fff;
  transition: transform .2s;
}
.toggle input:checked + .track { background: var(--accent); }
.toggle input:checked + .track::after { transform: translateX(16px); }

/* color */
.color-row { display: flex; align-items: center; gap: 9px; }
.color-swatch {
  width: 30px; height: 26px; border-radius: 7px; border: 1px solid var(--line-2);
  cursor: pointer; overflow: hidden; flex-shrink: 0; position: relative;
  background-image: linear-gradient(45deg,#333 25%,transparent 25%),linear-gradient(-45deg,#333 25%,transparent 25%),linear-gradient(45deg,transparent 75%,#333 75%),linear-gradient(-45deg,transparent 75%,#333 75%);
  background-size: 8px 8px; background-position: 0 0,0 4px,4px -4px,-4px 0;
}
.color-swatch input { opacity: 0; width: 100%; height: 100%; cursor: pointer; }
.color-swatch .fill { position: absolute; inset: 0; pointer-events: none; }
.alpha-mini { flex: 1; }

.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 13px; }
.note { font-size: 11px; color: var(--txt-faint); line-height: 1.5; margin-top: -2px; }

/* tooltip */
.tip {
  position: fixed; z-index: 99;
  max-width: 280px; padding: 10px 12px;
  background: #1a1e2b; border: 1px solid var(--line-2);
  border-radius: 9px; font-size: 11.5px; line-height: 1.55; color: var(--txt);
  box-shadow: 0 8px 30px rgba(0,0,0,0.5);
  pointer-events: none; opacity: 0; transform: translateY(4px);
  transition: opacity .14s, transform .14s;
}
.tip.show { opacity: 1; transform: translateY(0); }
.tip .tip-conf { display: block; margin-top: 6px; font-size: 10px; color: var(--txt-faint); }

/* toast */
.toast {
  position: fixed; bottom: 26px; left: calc(50% - var(--panel-w)/2); transform: translateX(-50%) translateY(20px);
  background: var(--good); color: #042418; font-weight: 600; font-size: 13px;
  padding: 11px 20px; border-radius: 11px; box-shadow: 0 10px 40px rgba(0,0,0,0.4);
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 100;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* responsive */
@media (max-width: 860px) {
  #app { grid-template-columns: 1fr; grid-template-rows: 45vh 1fr; }
  #panel { border-left: none; border-top: 1px solid var(--line); height: auto; }
  .toast { left: 50%; }
  :root { --panel-w: 0px; }
}
