:root {
  --ink: #0c1418;
  --ink-2: #101c21;
  --surface: #142228;
  --surface-2: #1a2c33;
  --overlay: rgba(12, 20, 24, 0.72);
  --scrim: rgba(6, 10, 12, 0.55);
  --line: #27424a;
  --line-soft: #1e343b;
  --text: #f3ead9;
  --text-dim: #b9c4c2;
  --text-mute: #7f9494;
  --amber: #f2a65a;
  --amber-hot: #ffbe6e;
  --copper: #c96f3a;
  --teal: #37d6c0;
  --teal-deep: #1c8f83;
  --sky: #5aa9ff;
  --acid: #b6e34b;
  --success: #7bd88f;
  --warning: #ffb020;
  --danger: #ff5a5f;
  --info: #5aa9ff;
  --warm-neutral: #e8dcc8;
  --cool-neutral: #22333a;
  --radius: 14px;
  --radius-sm: 9px;
  --panel-w: 326px;
  --topbar-h: 62px;
  --font-display: "Space Grotesk", system-ui, sans-serif;
  --font-body: "Inter", system-ui, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, monospace;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

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

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  font-family: var(--font-body);
  background:
    radial-gradient(1200px 700px at 78% -10%, #1d3a40 0%, transparent 60%),
    radial-gradient(900px 600px at -10% 110%, #2a1f1a 0%, transparent 55%),
    var(--ink);
  color: var(--text);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

#app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }

/* ---------- Top bar ---------- */
.topbar {
  height: var(--topbar-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 18px;
  background: linear-gradient(180deg, rgba(20, 34, 40, 0.96), rgba(16, 28, 33, 0.9));
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(14px);
  z-index: 30;
  animation: dropIn 0.7s var(--ease) both;
}
.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }
.brand-mark { width: 30px; height: 30px; flex: 0 0 auto; }
.brand-mark path { fill: none; stroke: var(--amber); stroke-width: 1.8; stroke-linejoin: round; }
.brand-text { display: flex; flex-direction: column; line-height: 1.05; }
.brand-name { font-family: var(--font-display); font-weight: 700; letter-spacing: 0.22em; font-size: 16px; }
.brand-sub { font-family: var(--font-mono); font-size: 10px; letter-spacing: 0.14em; color: var(--text-mute); text-transform: uppercase; }

.topbar-status {
  display: flex; align-items: center; gap: 8px;
  font-family: var(--font-mono); font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--text-dim);
  padding: 6px 12px; border: 1px solid var(--line-soft); border-radius: 999px;
  background: rgba(12, 20, 24, 0.5);
}
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--success); box-shadow: 0 0 10px var(--success); animation: pulse 1.6s ease-in-out infinite; }
.topbar-status.paused .status-dot { background: var(--warning); box-shadow: 0 0 10px var(--warning); animation: none; }

.topbar-actions { margin-left: auto; display: flex; gap: 8px; }
.icon-btn {
  display: inline-flex; align-items: center; gap: 8px;
  height: 38px; padding: 0 13px;
  background: var(--surface); color: var(--text-dim);
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  font-family: var(--font-body); font-size: 12.5px; font-weight: 500;
  cursor: pointer; transition: all 0.22s var(--ease);
}
.icon-btn svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:hover { color: var(--text); border-color: var(--teal-deep); background: var(--surface-2); transform: translateY(-1px); }
.icon-btn:active { transform: translateY(0); }
.icon-btn.accent { color: var(--ink); background: linear-gradient(135deg, var(--amber-hot), var(--copper)); border-color: transparent; font-weight: 600; }
.icon-btn.accent:hover { filter: brightness(1.08); box-shadow: 0 6px 18px rgba(201, 111, 58, 0.35); }
.icon-btn[aria-pressed="true"]#soundBtn { color: var(--teal); border-color: var(--teal-deep); }
#soundBtn .mute-x { display: none; }
#soundBtn.muted .wave { display: none; }
#soundBtn.muted .mute-x { display: block; stroke: var(--danger); }
#soundBtn.muted { color: var(--text-mute); }
#pauseBtn .play-tri { display: none; }
#pauseBtn.paused .pause-bars { display: none; }
#pauseBtn.paused .play-tri { display: block; fill: currentColor; stroke: none; }
#pauseBtn.paused { color: var(--warning); border-color: #5a4a20; }

/* ---------- Stage layout ---------- */
.stage { flex: 1 1 auto; display: grid; grid-template-columns: 1fr var(--panel-w); min-height: 0; }

.viewport { position: relative; min-width: 0; min-height: 0; overflow: hidden; background: #0e2229; }
#scene { display: block; width: 100%; height: 100%; touch-action: none; cursor: grab; animation: fadeIn 1.1s var(--ease) both 0.15s; }
#scene:active { cursor: grabbing; }

.grain {
  position: absolute; inset: 0; pointer-events: none; opacity: 0.05; z-index: 3;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}
.vignette { position: absolute; inset: 0; pointer-events: none; z-index: 3; background: radial-gradient(120% 90% at 50% 42%, transparent 52%, rgba(6, 10, 12, 0.5) 100%); }

.overlay { position: absolute; z-index: 5; pointer-events: none; }
.overlay-title { top: 26px; left: 28px; max-width: min(430px, 46%); animation: riseIn 0.9s var(--ease) both 0.3s; }
.eyebrow { font-family: var(--font-mono); font-size: 10.5px; letter-spacing: 0.18em; text-transform: uppercase; color: var(--teal); margin-bottom: 10px; }
.overlay-title h1 { font-family: var(--font-display); font-size: clamp(44px, 6.4vw, 86px); font-weight: 700; letter-spacing: -0.02em; line-height: 0.92; color: var(--warm-neutral); text-shadow: 0 4px 30px rgba(6, 10, 12, 0.6); }
.h1-dot { color: var(--amber); }
.lede { margin-top: 12px; font-size: 13.5px; line-height: 1.55; color: var(--text-dim); max-width: 360px; text-shadow: 0 2px 12px rgba(6, 10, 12, 0.7); }
.tag-row { display: flex; gap: 7px; margin-top: 14px; flex-wrap: wrap; }
.tag { font-family: var(--font-mono); font-size: 10px; letter-spacing: 0.08em; padding: 4px 9px; border-radius: 999px; color: var(--text-dim); border: 1px solid var(--line); background: var(--overlay); backdrop-filter: blur(6px); }

.overlay-telemetry { left: 28px; bottom: 118px; display: flex; gap: 10px; flex-wrap: wrap; animation: riseIn 0.9s var(--ease) both 0.5s; }
.tele { display: flex; flex-direction: column; gap: 3px; padding: 8px 13px; border-radius: var(--radius-sm); background: var(--overlay); border: 1px solid var(--line-soft); backdrop-filter: blur(8px); min-width: 74px; }
.tele-k { font-family: var(--font-mono); font-size: 9px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--text-mute); }
.tele-v { font-family: var(--font-display); font-size: 15px; font-weight: 600; color: var(--amber-hot); }

.overlay-hint { right: 22px; bottom: 118px; display: flex; gap: 8px; align-items: center; font-family: var(--font-mono); font-size: 10.5px; color: var(--text-mute); background: var(--overlay); padding: 7px 12px; border-radius: 999px; border: 1px solid var(--line-soft); backdrop-filter: blur(6px); animation: riseIn 0.9s var(--ease) both 0.65s; }
.overlay-hint i { color: var(--teal-deep); font-style: normal; }

/* ---------- Timeline ---------- */
.timeline {
  position: absolute; z-index: 6; left: 28px; right: 28px; bottom: 22px;
  padding: 13px 16px 12px; border-radius: var(--radius);
  background: var(--overlay); border: 1px solid var(--line);
  backdrop-filter: blur(14px); box-shadow: 0 12px 40px rgba(6, 10, 12, 0.4);
  animation: riseIn 0.9s var(--ease) both 0.6s;
}
.timeline-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 9px; }
.timeline-title { font-family: var(--font-mono); font-size: 10px; letter-spacing: 0.16em; text-transform: uppercase; color: var(--text-dim); }
.timeline-phase { font-family: var(--font-mono); font-size: 12px; font-weight: 600; color: var(--teal); }
.timeline-track { position: relative; height: 12px; border-radius: 999px; background: var(--ink); border: 1px solid var(--line-soft); cursor: pointer; overflow: hidden; }
.timeline-track:focus-visible { outline: 2px solid var(--teal); outline-offset: 3px; }
.timeline-fill { position: absolute; inset: 0 auto 0 0; width: 0%; background: linear-gradient(90deg, var(--teal-deep), var(--teal), var(--amber)); border-radius: 999px; }
.timeline-thumb { position: absolute; top: 50%; left: 0%; width: 16px; height: 16px; border-radius: 50%; background: var(--warm-neutral); border: 3px solid var(--teal); transform: translate(-50%, -50%); box-shadow: 0 0 12px rgba(55, 214, 192, 0.6); pointer-events: none; }
.timeline-ticks { position: absolute; inset: 0; display: flex; justify-content: space-between; padding: 0 25%; pointer-events: none; }
.timeline-ticks i { width: 1px; background: rgba(243, 234, 217, 0.16); }
.hoof-row { display: flex; gap: 8px; margin-top: 10px; }
.hoof { flex: 1; display: flex; align-items: center; justify-content: center; gap: 6px; font-family: var(--font-mono); font-size: 9.5px; letter-spacing: 0.06em; color: var(--text-mute); padding: 5px 4px; border-radius: 7px; border: 1px solid var(--line-soft); background: rgba(12, 20, 24, 0.5); transition: all 0.12s ease; }
.hoof b { font-weight: 600; color: var(--text-dim); }
.hoof.contact { color: var(--ink); background: linear-gradient(135deg, var(--amber-hot), var(--amber)); border-color: transparent; box-shadow: 0 0 14px rgba(242, 166, 90, 0.45); }
.hoof.contact b { color: var(--ink); }

/* ---------- Loading / fatal ---------- */
.loading, .fatal { position: absolute; inset: 0; z-index: 20; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px; background: radial-gradient(80% 60% at 50% 40%, #16303a, var(--ink)); transition: opacity 0.6s var(--ease); }
.loading.done { opacity: 0; pointer-events: none; }
.loading-horse { width: 130px; height: 90px; }
.loading-horse path { fill: none; stroke: var(--amber); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; stroke-dasharray: 400; animation: draw 2.2s var(--ease) infinite; }
.loading p { font-family: var(--font-mono); font-size: 12px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--text-mute); }
.fatal { text-align: center; padding: 30px; }
.fatal h2 { font-family: var(--font-display); font-size: 24px; color: var(--danger); margin-bottom: 10px; }
.fatal p { max-width: 420px; font-size: 14px; line-height: 1.6; color: var(--text-dim); }

/* ---------- Control panel ---------- */
.panel { position: relative; min-height: 0; background: linear-gradient(180deg, var(--ink-2), #0e181d); border-left: 1px solid var(--line); z-index: 20; animation: slideIn 0.8s var(--ease) both 0.2s; }
.panel-scroll { height: 100%; overflow-y: auto; overscroll-behavior: contain; padding: 18px 18px 22px; scrollbar-width: thin; scrollbar-color: var(--teal-deep) var(--ink); }
.panel-scroll::-webkit-scrollbar { width: 9px; }
.panel-scroll::-webkit-scrollbar-track { background: var(--ink); border-left: 1px solid var(--line-soft); }
.panel-scroll::-webkit-scrollbar-thumb { background: linear-gradient(180deg, var(--teal-deep), #2a5a52); border-radius: 999px; border: 2px solid var(--ink); }
.panel-scroll::-webkit-scrollbar-thumb:hover { background: linear-gradient(180deg, var(--teal), var(--teal-deep)); }

.ctrl-group { padding: 16px 0 18px; border-bottom: 1px solid var(--line-soft); }
.ctrl-group:first-child { padding-top: 4px; }
.ctrl-heading { display: flex; align-items: center; gap: 10px; font-family: var(--font-display); font-size: 13px; font-weight: 600; letter-spacing: 0.1em; text-transform: uppercase; color: var(--warm-neutral); margin-bottom: 15px; }
.ctrl-heading .idx { font-family: var(--font-mono); font-size: 10px; color: var(--teal); border: 1px solid var(--teal-deep); border-radius: 5px; padding: 2px 5px; letter-spacing: 0; }

.field { display: block; margin-bottom: 15px; }
.field-label { display: flex; justify-content: space-between; align-items: baseline; font-size: 12px; font-weight: 500; color: var(--text-dim); margin-bottom: 8px; }
.field-label output { font-family: var(--font-mono); font-size: 11.5px; font-weight: 600; color: var(--amber-hot); }

select {
  width: 100%; height: 38px; padding: 0 12px; appearance: none;
  background: var(--surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%2337d6c0' stroke-width='1.6' stroke-linecap='round'/%3E%3C/svg%3E") no-repeat right 12px center;
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  color: var(--text); font-family: var(--font-body); font-size: 12.5px; cursor: pointer;
  transition: border-color 0.2s var(--ease);
}
select:hover, select:focus { border-color: var(--teal-deep); outline: none; }

input[type="range"] { -webkit-appearance: none; appearance: none; width: 100%; height: 22px; background: transparent; cursor: pointer; }
input[type="range"]::-webkit-slider-runnable-track { height: 5px; border-radius: 999px; background: linear-gradient(90deg, var(--teal-deep), var(--line)); border: 1px solid var(--line-soft); }
input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 17px; height: 17px; margin-top: -7px; border-radius: 50%; background: radial-gradient(circle at 35% 30%, var(--amber-hot), var(--copper)); border: 2px solid var(--ink); box-shadow: 0 0 0 1px var(--copper), 0 2px 8px rgba(0, 0, 0, 0.5); transition: transform 0.15s var(--ease), box-shadow 0.15s var(--ease); }
input[type="range"]:hover::-webkit-slider-thumb { transform: scale(1.15); box-shadow: 0 0 0 1px var(--amber), 0 0 14px rgba(242, 166, 90, 0.55); }
input[type="range"]::-moz-range-track { height: 5px; border-radius: 999px; background: linear-gradient(90deg, var(--teal-deep), var(--line)); border: 1px solid var(--line-soft); }
input[type="range"]::-moz-range-thumb { width: 15px; height: 15px; border-radius: 50%; background: radial-gradient(circle at 35% 30%, var(--amber-hot), var(--copper)); border: 2px solid var(--ink); box-shadow: 0 0 0 1px var(--copper); }
input[type="range"]:focus-visible { outline: 2px solid var(--teal); outline-offset: 4px; border-radius: 999px; }

.swatches { display: flex; gap: 9px; }
.swatch { width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; display: grid; place-items: center; transition: all 0.2s var(--ease); }
.swatch i { width: 22px; height: 22px; border-radius: 6px; background: var(--c); display: block; box-shadow: inset 0 -3px 6px rgba(0, 0, 0, 0.3); }
.swatch:hover { transform: translateY(-2px); border-color: var(--text-mute); }
.swatch.active { border-color: var(--teal); box-shadow: 0 0 0 1px var(--teal), 0 0 16px rgba(55, 214, 192, 0.35); }
.swatch:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }

.toggle-row { display: flex; gap: 9px; flex-wrap: wrap; margin-bottom: 6px; }
.toggle { display: inline-flex; align-items: center; gap: 9px; padding: 8px 13px 8px 9px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); color: var(--text-dim); font-family: var(--font-body); font-size: 12px; font-weight: 500; cursor: pointer; transition: all 0.22s var(--ease); }
.toggle .knob { position: relative; width: 30px; height: 17px; border-radius: 999px; background: var(--ink); border: 1px solid var(--line); transition: all 0.22s var(--ease); flex: 0 0 auto; }
.toggle .knob::after { content: ""; position: absolute; top: 2px; left: 2px; width: 11px; height: 11px; border-radius: 50%; background: var(--text-mute); transition: all 0.22s var(--ease); }
.toggle:hover { border-color: var(--teal-deep); color: var(--text); }
.toggle[aria-pressed="true"] { color: var(--teal); border-color: var(--teal-deep); background: rgba(28, 143, 131, 0.14); }
.toggle[aria-pressed="true"] .knob { background: var(--teal-deep); border-color: var(--teal); }
.toggle[aria-pressed="true"] .knob::after { left: 15px; background: var(--warm-neutral); box-shadow: 0 0 8px rgba(55, 214, 192, 0.8); }
.toggle:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }

.stats { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 12px; font-family: var(--font-mono); font-size: 10.5px; color: var(--text-mute); }
.stats b { color: var(--teal); font-weight: 600; }

.panel-footer { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; padding-top: 18px; }
.btn { height: 40px; border-radius: var(--radius-sm); font-family: var(--font-body); font-size: 12.5px; font-weight: 600; cursor: pointer; border: 1px solid var(--line); transition: all 0.22s var(--ease); }
.btn.ghost { background: var(--surface); color: var(--text-dim); }
.btn.ghost:hover { color: var(--text); border-color: var(--teal-deep); background: var(--surface-2); transform: translateY(-1px); }
.btn.primary { grid-column: 1 / -1; background: linear-gradient(135deg, var(--amber-hot), var(--copper)); color: var(--ink); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.08); transform: translateY(-1px); box-shadow: 0 8px 22px rgba(201, 111, 58, 0.4); }
.btn:active { transform: translateY(0); }
.btn:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }

/* ---------- Animations ---------- */
@keyframes dropIn { from { opacity: 0; transform: translateY(-16px); } to { opacity: 1; transform: none; } }
@keyframes slideIn { from { opacity: 0; transform: translateX(24px); } to { opacity: 1; transform: none; } }
@keyframes riseIn { from { opacity: 0; transform: translateY(18px); } to { opacity: 1; transform: none; } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }
@keyframes draw { 0% { stroke-dashoffset: 400; } 55% { stroke-dashoffset: 0; } 100% { stroke-dashoffset: -400; } }

/* ---------- Responsive ---------- */
@media (max-width: 1080px) {
  :root { --panel-w: 292px; }
  .overlay-title { max-width: 52%; }
  .lede { display: none; }
}
@media (max-width: 860px) {
  body { overflow: auto; }
  #app { height: auto; min-height: 100dvh; }
  .stage { grid-template-columns: 1fr; grid-template-rows: 56vh auto; }
  .viewport { min-height: 380px; }
  .panel { border-left: none; border-top: 1px solid var(--line); }
  .panel-scroll { height: auto; max-height: none; overflow: visible; }
  .overlay-title { top: 16px; left: 16px; max-width: 60%; }
  .overlay-title h1 { font-size: 40px; }
  .overlay-telemetry { left: 16px; bottom: 108px; gap: 6px; }
  .tele { min-width: 60px; padding: 6px 9px; }
  .tele-v { font-size: 13px; }
  .overlay-hint { display: none; }
  .timeline { left: 12px; right: 12px; bottom: 12px; padding: 10px 12px; }
  .hoof { font-size: 8.5px; }
  .btn-label { display: none; }
  .icon-btn { padding: 0 11px; }
  .topbar-status { display: none; }
}
@media (max-width: 480px) {
  .overlay-telemetry .tele:nth-child(n+3) { display: none; }
  .tag-row { display: none; }
  .hoof-row { gap: 5px; }
  .hoof { flex-direction: column; gap: 1px; padding: 4px 2px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; animation-iteration-count: 1 !important; transition-duration: 0.001ms !important; }
}