:root {
  --ground: #05050a;
  --panel: rgba(14, 16, 26, 0.9);
  --line: rgba(255, 255, 255, 0.1);
  --text: #e6e8ef;
  --muted: #9097a8;
  --accent: #ff8a2a;
  --stim: #4fe07a;
  --silence: #e0404f;
  --chitin: #8a6a45;
  --leg: #c9a27a;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--ground);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow: hidden;
}

#brain { position: fixed; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: grab; }

.panel {
  position: fixed;
  top: 16px;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  padding: 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  backdrop-filter: blur(8px);
}

#panel { left: 16px; width: min(360px, calc(100vw - 32px)); }
#fly-panel { right: 16px; width: 300px; }

h1 { margin: 0; font-size: 20px; }
h2 { margin: 16px 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
#fly-panel h2 { margin-top: 0; }
.sub, .hint { margin: 4px 0 12px; color: var(--muted); }

.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row button { flex: 1; }

button, select, input {
  font: inherit;
  color: var(--text);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 10px;
}

button { cursor: pointer; }
button:hover { background: rgba(255, 255, 255, 0.12); }
#run { background: var(--accent); color: #140a02; border-color: transparent; font-weight: 600; }
#stimulate { border-color: var(--stim); }
#silence { border-color: var(--silence); }

.wide, select, #search { width: 100%; text-align: left; }
label { display: block; margin: 12px 0 4px; color: var(--muted); font-size: 12px; }
label output { color: var(--text); margin-left: 6px; }
input[type="range"] { width: 100%; padding: 0; accent-color: var(--accent); }
.toggle { display: flex; gap: 8px; align-items: center; color: var(--text); font-size: 14px; }
.save-row { margin-top: 12px; }
.save-row input { flex: 2; min-width: 0; }

.presets { display: grid; gap: 6px; }
.preset { display: grid; gap: 2px; text-align: left; padding: 9px 10px; }
.preset strong { font-size: 14px; }
.preset span { color: var(--muted); font-size: 12px; }
.preset small { color: var(--accent); font-size: 11px; font-variant-numeric: tabular-nums; }
.preset.active { border-color: var(--accent); background: rgba(255, 138, 42, 0.12); }

.groups { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 6px; }
.group { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 8px; border-radius: 6px; border-left: 3px solid; background: rgba(255, 255, 255, 0.04); }
.group.stimulate { border-color: var(--stim); }
.group.silence { border-color: var(--silence); }
.group button { padding: 2px 8px; font-size: 12px; }
.saved { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.saved-experiment { font-size: 12px; padding: 4px 8px; }

.stats { margin: 8px 0 0; color: var(--muted); font-variant-numeric: tabular-nums; }
.info { white-space: pre-line; margin: 8px 0; font-variant-numeric: tabular-nums; }
.status { min-height: 1.4em; margin: 12px 0 0; color: var(--text); }
.credit { margin: 12px 0 0; font-size: 11px; color: var(--muted); }
#share { margin-top: 12px; }

.caption { margin: 0 0 4px; font-size: 18px; font-weight: 600; color: var(--accent); min-height: 1.4em; }

#fly { width: 100%; height: auto; overflow: visible; }
#fly * { transform-box: view-box; }
#fly .ground { stroke: rgba(255, 255, 255, 0.15); stroke-width: 2; }
#fly .leg { fill: none; stroke: var(--leg); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
#fly .abdomen { fill: #6b4a2e; }
#fly .stripes { fill: none; stroke: #3d2a19; stroke-width: 4; }
#fly .thorax, #fly .head { fill: var(--chitin); }
#fly .eye { fill: #b3261e; }
#fly .antenna { stroke: var(--leg); stroke-width: 3; stroke-linecap: round; }
#fly #wings ellipse { fill: rgba(200, 220, 255, 0.25); stroke: rgba(200, 220, 255, 0.6); }
#wings { transform-origin: 140px 70px; transform: rotate(-8deg); transition: transform 0.2s; }

#proboscis { transform-origin: 165px 95px; transform: rotate(calc(var(--feed, 0) * -35deg)) scale(1, calc(0.5 + var(--feed, 0) * 1.8)); transition: transform 0.15s; }
#proboscis path { stroke: #e3bf93; stroke-width: 5; stroke-linecap: round; }
#proboscis ellipse { fill: #e3bf93; }

.leg-front { transform-origin: 142px 102px; }
.leg-mid { transform-origin: 128px 104px; }
.leg-hind { transform-origin: 112px 100px; }

#fly.forward .leg, #fly.backward .leg { animation: step 0.32s ease-in-out infinite alternate; }
#fly.forward .leg-mid, #fly.backward .leg-mid { animation-delay: -0.16s; }
#fly.forward #fly-body { animation: stride 0.64s ease-in-out infinite; }
#fly.backward #fly-body { animation: stride 0.64s ease-in-out infinite reverse; }
#fly.groom .leg-front { animation: groom 0.25s ease-in-out infinite alternate; }
#fly.escape #fly-body { animation: jump 0.7s ease-out infinite; }
#fly.escape #wings { transform: rotate(-40deg); }

@keyframes step { from { transform: rotate(-14deg); } to { transform: rotate(14deg); } }
@keyframes stride { 0% { transform: translateX(-6px); } 50% { transform: translateX(6px); } 100% { transform: translateX(-6px); } }
@keyframes groom { from { transform: rotate(-105deg); } to { transform: rotate(-125deg); } }
@keyframes jump { 0% { transform: translateY(0); } 35% { transform: translateY(-38px) rotate(-6deg); } 100% { transform: translateY(0); } }

.meters { display: grid; gap: 8px; margin-top: 8px; }
.meter { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; opacity: 0.55; transition: opacity 0.2s; }
.meter.on { opacity: 1; }
.meter-name { font-size: 13px; }
.meter-hz { font-size: 12px; font-variant-numeric: tabular-nums; color: var(--muted); }
.meter-bar { grid-column: 1 / -1; height: 6px; border-radius: 3px; background: rgba(255, 255, 255, 0.08); overflow: hidden; }
.meter-fill { height: 100%; width: 0; background: var(--accent); transition: width 0.15s; }
.meter-readout { grid-column: 1 / -1; font-size: 11px; color: var(--muted); }

@media (max-width: 760px) {
  #fly-panel { top: 16px; left: 16px; right: 16px; width: auto; max-height: 34vh; }
  #fly { max-height: 110px; }
  #panel { top: auto; bottom: 16px; max-height: 48vh; }
}
.play-link { display: block; margin-top: 12px; padding: 9px 10px; border-radius: 6px; background: rgba(106, 183, 255, 0.14); border: 1px solid #6ab7ff; color: var(--text); text-decoration: none; font-weight: 600; }
[hidden] { display: none !important; }
