/* MadDog Fleet — remote console.
 *
 * Two surfaces live in this file and they are lit differently on purpose:
 *
 *   PAGE CHROME  follows the operator's environment. PRODUCT.md records that
 *                both surfaces may be used anywhere, so there is a light
 *                branch for daylight as well as the dark default.
 *   HUD          sits on a video frame, not on the page, so it stays dark in
 *                both branches. Its job is to be legible over whatever the
 *                camera is pointed at, which is a different problem.
 *
 * Every contrast figure in the comments is measured, not estimated. HUD
 * figures are against the chip's own opaque background, which is the reason
 * the chips are opaque: a translucent chip's ratio is whatever the frame
 * happens to be, and that is not a number anyone can check.
 */

:root {
  --bg: #15181B;
  --panel: #1A1E22;
  --panel-2: #1C2024;
  --line: #2A2F35;
  --text: #C9CFD4;
  --muted: #8A939B;
  --accent: #7FA8B0;
  --ok: #86A88F;
  --warn: #B59A6E;
  --bad: #B07E7E;

  /* Text on a filled swatch. Its absence is what let .mode.SAFE inherit the
   * light body text onto red and sit at 2.14:1. */
  --on-ok: #15181B;        /* 6.80:1 */
  --on-warn: #15181B;      /* 6.63:1 */

  /* --bad stays exactly as it was, for text and borders:
   *   --bad on --panel            4.89:1   (.error, SUPERVISORY outline)
   * White on --bad is only 3.42:1, so a red FILL carrying white text uses the
   * same hue and saturation one step darker. H 0 / S 24% either way; only
   * lightness moves, 59% -> 48%.
   *   #fff on --bad-fill          5.17:1
   *   --bad-fill on --panel       3.24:1   (boundary, SC 1.4.11)
   */
  --bad-fill: #985D5D;
  --on-bad-fill: #ffffff;

  --radius: 10px;
  --ease: cubic-bezier(.16, 1, .3, 1);
  color-scheme: dark;
}

/* Daylight branch for the page chrome. The HUD block below opts out. */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #E7EAEC;
    --panel: #F2F4F5;
    --panel-2: #EDF0F2;
    --line: #6B7883;         /* 3.75:1 on --bg: a boundary, not a hairline */
    --text: #191D21;         /* 14.03:1 */
    --muted: #4A535B;        /*  6.49:1 */
    --accent: #1F4A55;       /*  8.01:1 */
    --ok: #2F5E40;
    --warn: #6E5118;
    --bad: #7A2E2E;          /*  7.26:1 as text on --panel */
    --bad-fill: #7A2E2E;     /*  9.30:1 with white */
    --on-ok: #ffffff;
    --on-warn: #ffffff;
    color-scheme: light;
  }
}
:root[data-theme="light"] {
  --bg: #E7EAEC;
  --panel: #F2F4F5;
  --panel-2: #EDF0F2;
  --line: #6B7883;
  --text: #191D21;
  --muted: #4A535B;
  --accent: #1F4A55;
  --ok: #2F5E40;
  --warn: #6E5118;
  --bad: #7A2E2E;
  --bad-fill: #7A2E2E;
  --on-ok: #ffffff;
  --on-warn: #ffffff;
  color-scheme: light;
}

* { box-sizing: border-box; }

html { scrollbar-color: var(--line) var(--bg); }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  caret-color: var(--accent);
}
.hidden { display: none !important; }

/* Browser surfaces nobody drew still carry the design. */
::selection { background: var(--accent); color: var(--bg); }
::-webkit-scrollbar { width: 11px; height: 11px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb {
  background: var(--line); border-radius: 99px; border: 3px solid var(--bg);
}
::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* Buttons carry border: 0, so the UA ring is the only focus affordance left.
 * --accent is 6.90:1 on --bg and 6.49:1 on --panel. */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

button {
  font: inherit; font-weight: 600; cursor: pointer;
  background: var(--accent); color: var(--on-ok);
  border: 1px solid transparent; border-radius: 8px; padding: 8px 14px;
}
button:disabled { opacity: .4; cursor: not-allowed; }
button.ghost { background: transparent; color: var(--text); border-color: var(--line); }
button.warn { background: var(--warn); color: var(--on-warn); }
/* Was #fff on --bad at 3.42:1. Same red, one step darker, white kept. */
button.danger { background: var(--bad-fill); color: var(--on-bad-fill); }

input[type=email], input[type=password] {
  width: 100%; margin-top: 4px; padding: 9px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text); font: inherit;
}

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.brand { font-weight: 800; letter-spacing: .5px; }
.brand span { color: var(--accent); }
.brand em {
  font-style: normal; font-size: 11px; color: var(--warn); margin-left: 6px;
  border: 1px solid var(--warn); border-radius: 4px; padding: 0 4px;
}
.who { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.chip {
  font-size: 12px; padding: 2px 8px; border-radius: 99px;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--muted);
}
.light-toggle {
  background: transparent; color: var(--muted); border: 1px solid var(--line);
  border-radius: 99px; padding: 5px 11px; font-size: 12px; font-weight: 500;
  transition: color .35s var(--ease), border-color .35s var(--ease);
}
.light-toggle:hover { color: var(--text); border-color: var(--muted); }
.chip:empty { display: none; }
.brand { white-space: nowrap; }
@media (max-width: 560px) {
  .topbar { padding: 8px 12px; gap: 8px; }
  .who { gap: 6px; flex-wrap: nowrap; }
  #who-name { display: none; }
  .light-toggle, .admin-link, #logout { padding: 5px 9px; font-size: 12px; }
}
.admin-link {
  color: var(--accent); text-decoration: none; font-size: 13px; font-weight: 600;
  padding: 5px 11px; border: 1px solid var(--line); border-radius: 99px;
}
.admin-link:hover { border-color: var(--accent); }

.view { padding: 16px; max-width: 1500px; margin: 0 auto; }
.center { display: grid; place-items: center; min-height: 80vh; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.card h3 { margin: 0 0 10px; font-size: 16px; font-weight: 600; letter-spacing: -0.01em; }
.card h3 small { color: var(--muted); font-weight: 400; }
.card h4 { margin: 16px 0 6px; font-size: 12px; font-weight: 600; letter-spacing: .04em;
  text-transform: uppercase; color: var(--muted); }
.login { width: min(380px, 100%); display: grid; gap: 12px; }
.login h1 { margin: 0; font-size: 25px; letter-spacing: -0.02em; }
.error { color: var(--bad); min-height: 1em; margin: 0; }
.hint { color: var(--muted); font-size: 12px; margin: 6px 0 0; }
.section-title { margin: 4px 0 14px; font-size: 20px; letter-spacing: -0.015em; }

.fleet { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 14px; }
.vcard .head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 8px; }
.vcard .name { font-weight: 700; font-size: 16px; letter-spacing: -0.01em; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: var(--bad-fill); }
.dot.on { background: var(--ok); }

/* Drawn marks rather than ✓ / ✗ glyphs: one stroke weight, and they inherit
 * the token colour instead of whatever the font supplies. */
.checks {
  list-style: none; padding: 0; margin: 8px 0; font-size: 12px;
  display: grid; grid-template-columns: 1fr 1fr; gap: 3px 10px;
}
.checks li { display: flex; align-items: baseline; gap: 6px; }
.checks li::before {
  content: ""; flex: none; width: 11px; height: 11px; margin-top: 3px;
  background: currentColor; color: var(--bad);
  -webkit-mask: var(--mark-x) center / 11px 11px no-repeat;
          mask: var(--mark-x) center / 11px 11px no-repeat;
}
.checks li.ok::before {
  color: var(--ok);
  -webkit-mask-image: var(--mark-check);
          mask-image: var(--mark-check);
}
.checks li.unknown::before {
  color: var(--muted);
  -webkit-mask-image: var(--mark-dash);
          mask-image: var(--mark-dash);
}
:root {
  --mark-check: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 12 12" fill="none" stroke="black" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M2 6.4 4.5 9 10 3"/></svg>');
  --mark-x: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 12 12" fill="none" stroke="black" stroke-width="1.8" stroke-linecap="round"><path d="M3 3l6 6M9 3l-6 6"/></svg>');
  --mark-dash: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 12 12" fill="none" stroke="black" stroke-width="1.8" stroke-linecap="round"><path d="M2.5 6h7"/></svg>');
}
.vcard .actions { display: flex; gap: 8px; margin-top: 10px; }
.meta { color: var(--muted); font-size: 12px; }

.drive { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 14px; }
@media (max-width: 1000px) { .drive { grid-template-columns: 1fr; } }
.video-wrap {
  position: relative; background: #000; border-radius: var(--radius);
  overflow: hidden; aspect-ratio: 16 / 9;
}
video { width: 100%; height: 100%; object-fit: contain; display: block; }

/* ── HUD ──────────────────────────────────────────────────────────────────
 * Stays dark in both branches: it is over the camera, not over the page.
 * Every text-bearing element sits on an opaque surface, so each ratio below
 * holds whatever the drone is pointed at.
 */
.hud {
  position: absolute; inset: 0; pointer-events: none;
  display: flex; flex-direction: column; justify-content: space-between; gap: 8px;
  padding: 10px;
  --hud-surface: #1A1E22;
  --hud-line: #2A2F35;
  --hud-text: #C9CFD4;      /* 10.66:1 on --hud-surface */
  --hud-muted: #8A939B;     /*  5.37:1 */
}
.hud-top { display: flex; flex-wrap: wrap; gap: 6px; align-items: flex-start; }
.pill, .mode {
  font: 600 12px/1 ui-monospace, Consolas, monospace;
  font-variant-numeric: tabular-nums;
  padding: 6px 8px; border-radius: 5px;
  background: var(--hud-surface); border: 1px solid var(--hud-line);
  color: var(--hud-text);
}
.pill.dim { color: var(--hud-muted); }
.pill.warn { background: var(--warn); border-color: var(--warn); color: var(--on-warn); }
.pill.bad { background: var(--bad-fill); border-color: var(--bad-fill); color: var(--on-bad-fill); }

/* All four rungs declare their own text colour. SUPERVISORY and SAFE share a
 * hue, so they are told apart by fill or outline, never by colour alone. */
.mode { font-size: 13px; letter-spacing: .06em; }
.mode.DIRECT { background: var(--ok); border-color: var(--ok); color: var(--on-ok); }
.mode.ASSISTED { background: var(--warn); border-color: var(--warn); color: var(--on-warn); }
.mode.SUPERVISORY { background: var(--hud-surface); border-color: var(--bad); color: var(--bad); }
.mode.SAFE { background: var(--bad-fill); border-color: var(--bad-fill); color: var(--on-bad-fill); }

.banner {
  align-self: center; max-width: 88%; text-align: center;
  padding: 9px 16px; border-radius: 6px; font-weight: 700; letter-spacing: .3px;
  background: var(--bad-fill); border: 1px solid var(--bad-fill); color: var(--on-bad-fill);
}

.hud-bottom { display: flex; justify-content: space-between; align-items: flex-end; gap: 10px; }
/* The label used to hang outside the circle at bottom: -18px over bare video,
 * where it measured 1.47:1 against a bright frame. It is now inside an opaque
 * group, in normal flow, so text scaling cannot push it into anything. */
.stickbox {
  background: var(--hud-surface); border: 1px solid var(--hud-line);
  border-radius: 7px; padding: 8px 8px 7px; text-align: center;
  pointer-events: auto; touch-action: none;
}
.stickpad {
  width: 62px; height: 62px; margin: 0 auto 6px; position: relative;
  border-radius: 50%; border: 1px solid var(--hud-line); background: #15181B;
}
.stickpad::before, .stickpad::after { content: ""; position: absolute; background: var(--hud-line); }
.stickpad::before { left: 50%; top: 9px; bottom: 9px; width: 1px; }
.stickpad::after { top: 50%; left: 9px; right: 9px; height: 1px; }
.stickpad i {
  position: absolute; width: 11px; height: 11px; border-radius: 50%;
  background: var(--accent); left: 50%; top: 50%;
  transform: translate(-50%, -50%) translate(var(--dx, 0px), var(--dy, 0px));
}
.stick-label { font: 500 11px/1 ui-monospace, Consolas, monospace; letter-spacing: .05em; color: var(--hud-text); }
.stick-value {
  margin-top: 3px; font: 400 11px/1.3 ui-monospace, Consolas, monospace;
  color: var(--hud-muted); font-variant-numeric: tabular-nums;
}
.alt {
  font: 700 14px/1 ui-monospace, Consolas, monospace; font-variant-numeric: tabular-nums;
  background: var(--hud-surface); border: 1px solid var(--hud-line); color: var(--hud-text);
  padding: 7px 10px; border-radius: 5px;
}

/* ── touch devices ────────────────────────────────────────────────────────
 * Keyed on `pointer: coarse`, the input device, rather than on width. The old
 * `max-width: 720px` stopped matching the moment a phone was turned sideways
 * — 844px on an iPhone 14, 915px on a Pixel 7 — and both drag surfaces
 * vanished, leaving only the display-only sticks inside the video.
 */
.touch-controls { display: none; }
.fs-toggle { display: none; }

@media (pointer: coarse) {
  .hud-bottom { display: none; }        /* readout only; the pads are the input */
  .touch-controls { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin-top: 10px; }
  .fs-toggle { display: inline-flex; }
  .touch-pad {
    aspect-ratio: 1; position: relative; border-radius: 12px;
    background: var(--panel); border: 1px solid var(--line);
    display: flex; align-items: flex-end; justify-content: center; padding: 10px;
    touch-action: none; -webkit-user-select: none; user-select: none;
    -webkit-touch-callout: none;
  }
  .touch-pad .ring {
    position: absolute; left: 50%; top: 46%; transform: translate(-50%, -50%);
    width: 62%; aspect-ratio: 1; border-radius: 50%;
    border: 1px solid var(--line); background: var(--bg);
  }
  .touch-pad .knob {
    position: absolute; left: 50%; top: 46%;
    width: 24%; aspect-ratio: 1; border-radius: 50%;
    background: var(--accent); border: 1px solid var(--accent);
    transform: translate(-50%, -50%) translate(var(--dx, 0px), var(--dy, 0px));
  }
  .touch-pad .tl {
    position: relative; font: 500 11px/1 ui-monospace, Consolas, monospace;
    letter-spacing: .05em; color: var(--text);
  }
  .controls-bar button { flex: 1 1 auto; min-height: 44px; }
  .pill, .mode { font-size: 11.5px; padding: 5px 7px; }
}

/* Portrait phone: stack. Video, then the two pads, then the buttons. */
@media (pointer: coarse) and (orientation: portrait) {
  .drive { grid-template-columns: 1fr; }
  .hud { gap: 6px; }
}

/* Landscape phone or tablet: a full-bleed cockpit.
 *
 * The picture fills the viewport. The pads sit over its lower corners, sized
 * from the short side so they stay thumb-sized on a 390px-tall screen. The
 * map, telemetry and admin panels are hidden — there is no room for them, and
 * whoever is holding the phone sideways is flying, not reading.
 */
@media (pointer: coarse) and (orientation: landscape) {
  body.flying { overflow: hidden; overscroll-behavior: none; }
  body.flying .topbar { display: none; }
  body.flying #view-drive { padding: 0; max-width: none; }
  body.flying #view-drive > .section-title { display: none; }
  body.flying .drive { display: block; }
  body.flying .side { display: none; }

  body.flying .stage {
    position: fixed; inset: 0; z-index: 20; background: #000;
    touch-action: none;
  }
  body.flying .video-wrap {
    position: absolute; inset: 0; aspect-ratio: auto; border-radius: 0;
  }
  body.flying .hud {
    padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
             10px max(12px, env(safe-area-inset-left));
  }

  /* The pads, over the lower corners, where thumbs rest. */
  body.flying .touch-controls {
    position: absolute; inset: auto 0 0 0; z-index: 2; margin: 0;
    display: flex; justify-content: space-between; align-items: flex-end;
    padding: 0 max(14px, env(safe-area-inset-right))
             max(14px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
    pointer-events: none;            /* only the pads themselves take touches */
  }
  body.flying .touch-pad {
    pointer-events: auto;
    width: clamp(128px, 44vh, 210px);
    background: rgba(26, 30, 34, .62);
    border-color: rgba(138, 147, 155, .38);
    backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
  }
  body.flying .touch-pad .ring { background: rgba(21, 24, 27, .55); }

  /* Buttons along the top right, clear of the thumbs and of the picture's
   * centre. Compact: the pilot reaches up for these, rarely. */
  /* Wraps inside its own column on the right. It used to be nowrap with
   * justify-content: flex-end, so buttons that did not fit overflowed out of
   * its LEFT edge and landed on the status pills. */
  body.flying .controls-bar {
    position: absolute; z-index: 3; margin: 0;
    top: max(8px, env(safe-area-inset-top)); right: max(12px, env(safe-area-inset-right));
    flex-wrap: wrap; gap: 6px; width: min(52vw, 470px); justify-content: flex-end;
    align-content: flex-start;
  }
  body.flying .controls-bar .spacer,
  body.flying .controls-bar #session-role { display: none; }
  body.flying .controls-bar button {
    flex: 0 0 auto; min-height: 40px; padding: 6px 11px; font-size: 12px;
  }
  body.flying .hud-top { max-width: calc(100vw - min(52vw, 470px) - 48px); }
  body.flying #input-hint { display: none; }
}

.controls-bar { display: flex; gap: 8px; align-items: center; margin-top: 10px; flex-wrap: wrap; }
.spacer { flex: 1; }
.side { display: grid; gap: 14px; align-content: start; }
#map { width: 100%; height: auto; background: var(--panel-2); border-radius: 8px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; margin: 0; font-size: 12px; }
.kv dt { color: var(--muted); }
.kv dd {
  margin: 0; text-align: right; font-family: ui-monospace, Consolas, monospace;
  font-variant-numeric: tabular-nums;
}
.kv dd.flag { color: var(--warn); }

.admin .toggles { display: grid; gap: 6px; margin-bottom: 8px; }
.admin .row { display: flex; gap: 8px; flex-wrap: wrap; margin: 6px 0; }
.admin .row button { padding: 8px 11px; font-size: 12px; min-height: 36px; }
.slider { display: grid; grid-template-columns: 70px 1fr 54px; align-items: center; gap: 6px; font-size: 12px; }
.slider output { text-align: right; font-family: ui-monospace, Consolas, monospace; font-variant-numeric: tabular-nums; }
.audit {
  margin: 0; padding-left: 0; list-style: none; max-height: 220px; overflow: auto;
  font: 11px/1.5 ui-monospace, Consolas, monospace; color: var(--muted);
}
.audit b { color: var(--text); font-weight: 600; }

.toast {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
  max-width: calc(100% - 32px);
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  padding: 10px 16px; border-radius: 8px;
}
.toast.err { border-color: var(--bad); }

@media (prefers-reduced-motion: reduce) {
  .light-toggle { transition: none; }
}
