/*
 * Drone Emulator — interface styles.
 *
 * Everything themable is a custom property on :root. Theme rules that redefine
 * a *painted* property instead of a token gain specificity from their own
 * scope — `:root[data-theme] a` scores higher than a two-class component rule —
 * and silently outrank the component that owns the element. Keeping themes on
 * tokens means every component still resolves its own declaration.
 */

:root {
  color-scheme: dark;

  --bg: #0b0e13;
  --ink: #e8eef6;
  --ink-dim: #93a2b5;
  --ink-faint: #61708333;

  --hud: #6ef2c0;
  --hud-dim: #6ef2c0aa;
  --hud-back: #050a0ecc;
  --hud-edge: #6ef2c033;

  --warn: #ffc233;
  --danger: #ff4d3d;
  --good: #39e08a;
  --accent: #ff7a45;
  --sky: #4a8fd4;
  --ground-tone: #6d5b3f;

  --panel: #10151ce6;
  --panel-edge: #ffffff1a;
  --panel-radius: 10px;

  --shadow: 0 10px 30px #00000066;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;

  /* HUD type scales with the stage, not the viewport: the canvas fills the
     window here, but keeping the unit in one place means a letterboxed stage
     is a one-line change rather than a sweep of breakpoints. */
  --hud-size: clamp(10px, 1.05vmin + 7px, 15px);
  --hud-gap: clamp(8px, 1.4vmin, 18px);
}

* { box-sizing: border-box; }

/*
 * `hidden` is only a user-agent rule (`display: none`), so *any* author rule
 * that sets `display` on the same element outranks it and the element stays
 * painted while `el.hidden` reads true. Several components below set their own
 * `display`, so the attribute is reinstated here once rather than being
 * remembered per component.
 */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  touch-action: none;
}

#stage {
  position: relative;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
}

#scene {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }

/* ========================= HUD shell ========================= */

.hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  font-size: var(--hud-size);
  letter-spacing: 0.04em;
  text-shadow: 0 1px 3px #000a, 0 0 12px #0006;
  transition: opacity 180ms ease;
}

.hud[data-hidden="true"] { opacity: 0; }

.panel {
  position: absolute;
  display: flex;
  flex-direction: column;
  gap: 0.35em;
  padding: 0.7em 0.9em;
  background: var(--hud-back);
  border: 1px solid var(--hud-edge);
  border-radius: var(--panel-radius);
  backdrop-filter: blur(6px);
}

.panel--topleft { top: var(--hud-gap); left: var(--hud-gap); min-width: 11em; }
.panel--topright { top: var(--hud-gap); right: var(--hud-gap); min-width: 12em; }
.panel--topcentre {
  top: var(--hud-gap);
  left: 50%;
  transform: translateX(-50%);
  padding: 0.45em 0.6em;
}
.panel--left { top: 50%; left: var(--hud-gap); transform: translateY(-50%); }
.panel--right {
  top: 50%;
  right: var(--hud-gap);
  transform: translateY(-50%);
  align-items: center;
}
.panel--bottomleft { bottom: calc(var(--hud-gap) + 3.4em); left: var(--hud-gap); }
.panel--bottomright { bottom: calc(var(--hud-gap) + 3.4em); right: var(--hud-gap); min-width: 13em; }

.readout {
  display: flex;
  justify-content: space-between;
  gap: 1.2em;
  align-items: baseline;
}
.readout__label { color: var(--ink-dim); font-size: 0.82em; }
.readout__value { color: var(--hud); font-weight: 600; }

.badge {
  align-self: flex-start;
  padding: 0.22em 0.6em;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.86em;
  letter-spacing: 0.12em;
  border: 1px solid currentColor;
}
.badge[data-state="disarmed"] { color: var(--ink-dim); }
.badge[data-state="armed"] { color: var(--good); background: #39e08a1f; }
.badge[data-state="auto"] { color: var(--sky); background: #4a8fd41f; }
.badge[data-state="crashed"] { color: var(--danger); background: #ff4d3d24; }

/* ========================= Attitude ========================= */

.centre {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
}

.attitude {
  width: min(34vmin, 300px);
  height: auto;
  opacity: 0.92;
  overflow: visible;
}

.att-sky { fill: #2f6bb855; }
.att-ground { fill: #6d5b3f66; }
.att-line { stroke: var(--hud); stroke-width: 2; }
.att-bezel { fill: none; stroke: var(--hud-edge); stroke-width: 2; }
.att-ladder-line { stroke: var(--hud-dim); stroke-width: 1.4; }
.att-ladder-text {
  fill: var(--hud-dim);
  font: 600 11px var(--mono);
  text-anchor: middle;
  dominant-baseline: middle;
}
.att-roll-mark { stroke: var(--hud-dim); stroke-width: 1.6; }
.att-roll-pointer { fill: var(--hud); }
.att-aircraft path {
  fill: none;
  stroke: var(--accent);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.att-aircraft circle { fill: var(--accent); }

.crosshair {
  position: absolute;
  width: 1.4em;
  height: 1.4em;
  border: 1px solid var(--hud-dim);
  border-radius: 50%;
  opacity: 0.5;
}

/*
 * The attitude indicator belongs in the middle of the *pilot's* view, which is
 * the nose camera. In the external views the middle of the screen is where the
 * aircraft itself is, and a gauge sitting on top of it hides the one thing
 * worth looking at — so it stands down to a corner instead.
 */
.hud:not([data-view="fpv"]) .centre {
  place-items: start center;
  padding-top: 8em;
}
.hud:not([data-view="fpv"]) .attitude { width: min(19vmin, 160px); opacity: 0.78; }
.hud:not([data-view="fpv"]) .crosshair { display: none; }

/* ========================= Heading tape ========================= */

.heading { position: relative; width: min(42vmin, 340px); }
.heading__window {
  overflow: hidden;
  height: 1.9em;
  position: relative;
  /* Fade the ends so labels leaving the window dissolve instead of being
     sliced in half, which otherwise reads as a wrong compass point. */
  -webkit-mask-image: linear-gradient(90deg, transparent, #000 12%, #000 88%, transparent);
  mask-image: linear-gradient(90deg, transparent, #000 12%, #000 88%, transparent);
}
.heading__tape {
  position: absolute;
  top: 0;
  left: 50%;
  display: flex;
  align-items: center;
  height: 100%;
  will-change: transform;
}
.heading__tick {
  position: relative;
  width: 3.2em;
  flex: 0 0 3.2em;
  text-align: center;
  color: var(--ink-dim);
  font: 600 0.85em var(--mono);
}
.heading__tick--major { color: var(--hud); }
.heading__tick::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -0.35em;
  width: 1px;
  height: 0.4em;
  background: var(--hud-edge);
}
.heading__pointer {
  position: absolute;
  left: 50%;
  bottom: -0.15em;
  width: 0;
  height: 0;
  transform: translateX(-50%);
  border-left: 0.42em solid transparent;
  border-right: 0.42em solid transparent;
  border-bottom: 0.5em solid var(--accent);
}
.heading__value {
  text-align: center;
  color: var(--hud);
  font-weight: 700;
  margin-top: 0.5em;
}

/* ========================= Tapes ========================= */

.tape { text-align: center; min-width: 6.4em; }
.tape__label { color: var(--ink-dim); font-size: 0.78em; letter-spacing: 0.16em; }
.tape__value {
  color: var(--hud);
  font-size: 1.9em;
  font-weight: 700;
  line-height: 1.1;
}
.tape__unit { color: var(--ink-dim); font-size: 0.76em; margin-top: -0.2em; }
.tape__sub { color: var(--ink-dim); font-size: 0.82em; margin-top: 0.3em; }

.vsi { display: flex; align-items: center; gap: 0.4em; margin-top: 0.5em; }
.vsi__scale {
  position: relative;
  flex: 1;
  height: 0.42em;
  background: #ffffff14;
  border-radius: 999px;
  overflow: hidden;
}
.vsi__needle {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 2px;
  background: var(--hud);
  transform-origin: left;
}
.vsi__value { color: var(--hud-dim); font-size: 0.8em; min-width: 3.2em; }

.homearrow { margin-top: 0.6em; width: 2.1em; height: 2.1em; }
.homearrow svg { width: 100%; height: 100%; }
.homearrow polygon { fill: var(--accent); opacity: 0.85; }

/* ========================= Motors ========================= */

.motors { display: flex; gap: 1em; align-items: center; }
.motors__grid {
  display: grid;
  grid-template-columns: repeat(2, 1.5em);
  gap: 0.35em;
}
.motor { display: flex; flex-direction: column; align-items: center; gap: 0.15em; }
.motor__bar {
  display: block;
  width: 0.55em;
  height: 3.2em;
  background: #ffffff14;
  border-radius: 2px;
  position: relative;
  overflow: hidden;
}
.motor__bar i {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 0%;
  background: linear-gradient(to top, var(--good), var(--hud));
  transition: none;
}
.motor[data-damaged="true"] .motor__bar i { background: linear-gradient(to top, #b34, #f75); }
.motor__id { color: var(--ink-dim); font: 600 0.68em var(--mono); }
.motors__meta { display: flex; flex-direction: column; gap: 0.3em; min-width: 9em; }

.telemetry {
  display: block;
  margin-top: 0.6em;
  width: min(30vw, 300px);
  height: auto;
  border-radius: 6px;
  background: #050a0e99;
  border: 1px solid var(--hud-edge);
}

/* ========================= Battery ========================= */

.battery__top, .battery__bottom {
  display: flex;
  justify-content: space-between;
  gap: 0.8em;
}
.battery__pct { font-size: 1.5em; font-weight: 700; color: var(--hud); }
.battery__volts { color: var(--ink-dim); align-self: flex-end; }
.battery__label { color: var(--ink-dim); font-size: 0.8em; }
.battery__bottom .mono { color: var(--ink-dim); font-size: 0.82em; }
.battery__bar {
  height: 0.5em;
  background: #ffffff14;
  border-radius: 999px;
  overflow: hidden;
  margin: 0.35em 0;
}
.battery__fill {
  height: 100%;
  width: 100%;
  background: var(--good);
  border-radius: 999px;
}
.battery[data-level="warning"] .battery__fill { background: var(--warn); }
.battery[data-level="warning"] .battery__pct { color: var(--warn); }
.battery[data-level="critical"] .battery__fill { background: var(--danger); }
.battery[data-level="critical"] .battery__pct { color: var(--danger); }
.battery[data-level="critical"] { animation: pulse 1.1s ease-in-out infinite; }

@keyframes pulse { 50% { opacity: 0.55; } }

/* ========================= Race ========================= */

.race { display: flex; flex-direction: column; gap: 0.3em; }
.race__head { display: flex; justify-content: space-between; align-items: baseline; }
.race__title { color: var(--ink-dim); font-size: 0.8em; letter-spacing: 0.16em; }
.race__state { color: var(--ink-dim); font-size: 0.85em; }
.race[data-active="true"] .race__state { color: var(--good); }
.race__row {
  display: flex;
  justify-content: space-between;
  gap: 1em;
  color: var(--ink-dim);
  font-size: 0.9em;
}
.race__row .mono { color: var(--hud); }
.race__row--best .mono { color: var(--warn); }
.race__hint { color: var(--ink-dim); font-size: 0.78em; margin-top: 0.2em; }
.race[data-active="true"] .race__hint { visibility: hidden; }

/* ========================= Messages ========================= */

.messages {
  position: absolute;
  bottom: calc(var(--hud-gap) + 3.6em);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column-reverse;
  align-items: center;
  gap: 0.3em;
  max-width: min(46em, 80vw);
}
.message {
  padding: 0.35em 0.9em;
  border-radius: 999px;
  background: var(--hud-back);
  border: 1px solid var(--hud-edge);
  color: var(--ink);
  font-size: 0.92em;
  animation: rise 220ms ease-out;
}
.message[data-kind="crash"], .message[data-kind="failsafe"] {
  color: var(--danger);
  border-color: #ff4d3d55;
}
.message[data-kind="gate"], .message[data-kind="lap"] {
  color: var(--good);
  border-color: #39e08a55;
}
.message[data-kind="fault"] { color: var(--warn); border-color: #ffc23355; }

@keyframes rise { from { opacity: 0; transform: translateY(6px); } }

.banner {
  position: absolute;
  top: 26%;
  left: 50%;
  transform: translateX(-50%);
  padding: 0.6em 1.4em;
  border-radius: 8px;
  background: #1a0d0ce6;
  border: 1px solid #ff4d3d66;
  color: var(--danger);
  font-size: 1.5em;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-align: center;
}
.banner small {
  display: block;
  font-size: 0.5em;
  font-weight: 500;
  letter-spacing: 0.06em;
  color: var(--ink-dim);
  margin-top: 0.3em;
}

/* ========================= Toolbar ========================= */

.toolbar {
  position: absolute;
  bottom: var(--hud-gap);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 0.4em;
  padding: 0.4em;
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  border-radius: 999px;
  box-shadow: var(--shadow);
  z-index: 5;
  max-width: calc(100vw - 2 * var(--hud-gap));
  overflow-x: auto;
  scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }

.tool {
  display: inline-flex;
  align-items: center;
  gap: 0.45em;
  padding: 0.5em 0.9em;
  border-radius: 999px;
  border: 1px solid transparent;
  background: #ffffff0f;
  color: var(--ink);
  font: 500 0.86rem var(--font);
  cursor: pointer;
  white-space: nowrap;
  transition: background 120ms ease, border-color 120ms ease;
}
.tool:hover { background: #ffffff1c; }
.tool:focus-visible { outline: 2px solid var(--hud); outline-offset: 2px; }
.tool--primary { background: #39e08a22; border-color: #39e08a55; color: var(--good); }
.tool--primary[data-armed="true"] { background: #ff4d3d22; border-color: #ff4d3d55; color: var(--danger); }
.tool--icon { padding: 0.5em 0.75em; font-size: 1rem; }

kbd {
  font: 600 0.72em var(--mono);
  padding: 0.12em 0.4em;
  border-radius: 4px;
  background: #ffffff14;
  border: 1px solid #ffffff22;
  color: var(--ink-dim);
}

.iconbtn {
  background: none;
  border: 0;
  color: var(--ink-dim);
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 0.2em;
}
.iconbtn:hover { color: var(--ink); }

/* ========================= Drawer ========================= */

.drawer {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(23rem, 88vw);
  padding: 1rem 1.2rem 6rem;
  background: #0d1218f2;
  border-left: 1px solid var(--panel-edge);
  box-shadow: var(--shadow);
  overflow-y: auto;
  z-index: 6;
  animation: slide 180ms ease-out;
}
@keyframes slide { from { transform: translateX(100%); } }

.drawer__head { display: flex; justify-content: space-between; align-items: center; }
.drawer__head h2 { margin: 0; font-size: 1.05rem; letter-spacing: 0.04em; }
.drawer__section { margin-top: 1.3rem; }
.drawer__section h3 {
  margin: 0 0 0.7rem;
  font-size: 0.72rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-dim);
}

.field { display: flex; flex-direction: column; gap: 0.35rem; margin-bottom: 0.9rem; font-size: 0.86rem; }
.field > span { display: flex; justify-content: space-between; color: var(--ink-dim); }
.field output { color: var(--hud); font-family: var(--mono); }
.field--check { flex-direction: row; align-items: center; gap: 0.6rem; }
.field--check span { color: var(--ink); }

select, input[type="range"] { width: 100%; accent-color: var(--hud); }
select {
  padding: 0.45rem 0.5rem;
  border-radius: 6px;
  background: #ffffff10;
  border: 1px solid var(--panel-edge);
  color: var(--ink);
  font: inherit;
}
input[type="checkbox"] { width: 1rem; height: 1rem; accent-color: var(--hud); }

.spec { display: grid; grid-template-columns: 1fr auto; gap: 0.35rem 1rem; margin: 0; font-size: 0.85rem; }
.spec dt { color: var(--ink-dim); }
.spec dd { margin: 0; font-family: var(--mono); color: var(--hud); text-align: right; }

/* ========================= Touch ========================= */

.touch {
  position: absolute;
  inset: auto 0 0 0;
  height: 40%;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  padding: 0 var(--hud-gap) calc(var(--hud-gap) + 3.6em);
  pointer-events: none;
  z-index: 4;
}
.touch__stick {
  position: relative;
  width: 8.5rem;
  height: 8.5rem;
  border-radius: 50%;
  border: 1px solid var(--hud-edge);
  background: #050a0e66;
  pointer-events: auto;
  touch-action: none;
  display: grid;
  place-items: center;
}
.touch__stick span {
  position: absolute;
  bottom: -1.3em;
  font-size: 0.66rem;
  color: var(--ink-dim);
  letter-spacing: 0.1em;
}
.touch__knob {
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  background: #6ef2c033;
  border: 1px solid var(--hud);
  will-change: transform;
}

/* ========================= Overlay ========================= */

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: var(--hud-gap);
  background: #05080ce6;
  backdrop-filter: blur(10px);
  z-index: 10;
  overflow-y: auto;
}

.overlay__card {
  width: min(52rem, 100%);
  padding: clamp(1.2rem, 3vmin, 2.2rem);
  border-radius: 16px;
  background: #0d1218;
  border: 1px solid var(--panel-edge);
  box-shadow: var(--shadow);
  margin: auto;
}
.overlay__card h1 {
  margin: 0 0 0.4rem;
  font-size: clamp(1.6rem, 4.4vmin, 2.4rem);
  letter-spacing: -0.01em;
}
.overlay__lead { margin: 0 0 1.4rem; color: var(--ink-dim); line-height: 1.6; max-width: 46ch; }
.overlay__cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)); gap: 1.6rem; }
.overlay__card h2 {
  margin: 0 0 0.6rem;
  font-size: 0.72rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--hud);
}
.keys { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.45rem; }
.keys li { display: flex; align-items: center; gap: 0.3rem; font-size: 0.88rem; }
.keys li span { color: var(--ink-dim); margin-left: 0.4rem; }
.overlay__note { color: var(--ink-dim); font-size: 0.8rem; margin: 0.9rem 0 0; }

.overlay__firstflight { margin-top: 1.6rem; }
.overlay__firstflight ol { margin: 0; padding-left: 1.2rem; color: var(--ink-dim); line-height: 1.7; font-size: 0.9rem; }
.overlay__firstflight strong { color: var(--ink); }

.overlay__go {
  margin-top: 1.6rem;
  width: 100%;
  padding: 0.85rem;
  border-radius: 10px;
  border: 1px solid #39e08a66;
  background: #39e08a1f;
  color: var(--good);
  font: 600 1rem var(--font);
  letter-spacing: 0.06em;
  cursor: pointer;
}
.overlay__go:hover { background: #39e08a33; }

.loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  gap: 0.8rem;
  grid-auto-flow: row;
  align-content: center;
  background: var(--bg);
  color: var(--ink-dim);
  z-index: 20;
  font-size: 0.9rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}
.loading span {
  width: 2.2rem;
  height: 2.2rem;
  border-radius: 50%;
  border: 2px solid var(--hud-edge);
  border-top-color: var(--hud);
  animation: spin 800ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ========================= Responsive ========================= */

@media (max-width: 860px) {
  .panel--bottomleft .telemetry { display: none; }
  .panel--bottomleft .motors__meta { min-width: 7em; }
  .attitude { width: 30vmin; }
}

@media (max-width: 620px) {
  .panel--topcentre { display: none; }
  .panel--bottomright { bottom: calc(var(--hud-gap) + 3.4em); }
  .panel--left, .panel--right { transform: translateY(-50%) scale(0.92); }
  .tool { padding: 0.5em 0.7em; font-size: 0.78rem; }
  .tool kbd { display: none; }
}

@media (max-height: 520px) {
  .panel--bottomleft, .panel--bottomright { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; }
}
