:root {
  /* Surfaces — a cooler, deeper dark with an elevation ladder: each step up
     the z-stack is a touch lighter so cards read by elevation, not just borders
     (the premium dark-UI pattern — Linear/Vercel/Datadog). Values upgraded but
     every token NAME is unchanged, so all existing CSS/JS keeps working. */
  --bg: #0b0e14;
  --panel: #151a23;        /* card surface */
  --panel-2: #1d2531;      /* raised surface (menus, chips, active) */
  --surface-1: #11151d;    /* subtle fill below cards */
  --text: #e9edf3;
  --muted: #8893a2;
  --faint: #5b6675;        /* disabled / decorative only — not real text */
  --accent: #f26b21;       /* RouteForge brand — reserved for primary action/identity */
  --accent-2: #46a7ff;
  /* status colors: ONE green, ONE red, ONE amber — reserved for state ONLY,
     never decorative, so a red element always means "warning" */
  --ok: #33d17f;
  --err: #ff5f5f;
  --warn: #f3a63a;
  /* "flying/active" marks motion, not a problem — distinct from brand & amber */
  --flying: #f08a3c;
  /* borders: a hairline that reads as an edge on dark without a hard line */
  --border: #27313f;
  --border-strong: #3a4657;
  /* alpha tints for pills / badges / alert backgrounds */
  --accent-tint: rgba(242,107,33,.14);
  --ok-tint: rgba(51,209,127,.14);
  --warn-tint: rgba(243,166,58,.15);
  --err-tint: rgba(255,95,95,.14);
  --info-tint: rgba(70,167,255,.14);
  /* shape, glass, type */
  --r-sm: 6px; --r-md: 8px; --r-lg: 12px; --r-full: 9999px;
  --glass: rgba(8,11,16,.74);          /* floating panels over video/map */
  --elev: 0 8px 24px rgba(0,0,0,.45), 0 2px 6px rgba(0,0,0,.35);
  --mono: "SF Mono","JetBrains Mono",ui-monospace,Consolas,monospace;
}
/* every changing number reads as an instrument: fixed-width digits don't jitter */
.tnum, .osd b, .kv b + span { font-variant-numeric: tabular-nums; }

/* global status text classes — set className, never style.color literals */
.ok { color: var(--ok); }
.err { color: var(--err); }
.warn { color: var(--warn); }

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body, #app { height: 100%; }

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  /* refinery skyline (Unsplash, royalty-free) behind a dark wash — visible
     texture, panels stay full-contrast; fixed = image never scrolls or
     tiles. Falls back to the flat theme color offline. */
  background:
    linear-gradient(rgba(16, 21, 28, 0.62), rgba(16, 21, 28, 0.88)),
    url("bg-field.jpg") center / cover no-repeat fixed,
    var(--bg);
  color: var(--text);
}

#app { display: flex; }

#sidebar {
  width: 320px;
  min-width: 320px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 20px 18px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

#sidebar header h1 {
  font-size: 24px;
  letter-spacing: 0.5px;
}
#sidebar header h1 span { color: var(--accent); }
.tagline { color: var(--muted); font-size: 12px; margin-top: 2px; }

/* ONE card component for every page — the per-page .panel copies (radius
   12/10, padding 14/18/20/20 22) are gone; override locally only on purpose */
.panel, .panel2 {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
}
#sidebar .panel { background: none; border: none; border-radius: 0; padding: 0; }
.panel h2, .panel2 h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: var(--muted);
  margin-bottom: 10px;
}

/* ONE form-control base — pages stop re-inventing inputs */
input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]),
select, textarea {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 7px 10px;
  font-size: 13px;
  font-family: inherit;
}
input:focus:not(:focus-visible), select:focus:not(:focus-visible), textarea:focus:not(:focus-visible) { outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--accent-2); }

/* telemetry numbers never jiggle as values update */
.kv span, .stats .stat span, #osd-status, .tabular { font-variant-numeric: tabular-nums; }

/* Ctrl+K command palette */
#rf-pal-overlay { position: fixed; inset: 0; z-index: 3000; background: rgba(8, 12, 18, 0.55);
  display: flex; align-items: flex-start; justify-content: center; padding-top: 12vh; }
#rf-palette { width: min(520px, 92vw); background: var(--panel); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: 0 12px 40px rgba(0, 0, 0, 0.55); overflow: hidden; }
#rf-pal-in { width: 100%; border: none !important; border-bottom: 1px solid var(--border) !important;
  border-radius: 0 !important; background: var(--panel); padding: 13px 16px; font-size: 14.5px; }
#rf-pal-in:focus { outline: none; }
#rf-pal-list { max-height: 320px; overflow-y: auto; padding: 6px; }
.rf-pal-row { display: flex; justify-content: space-between; align-items: baseline; gap: 12px;
  padding: 8px 11px; border-radius: 7px; font-size: 13px; cursor: pointer; }
.rf-pal-row span { color: var(--muted); font-size: 11px; }
.rf-pal-row:hover, .rf-pal-row.sel { background: var(--panel-2); }
.rf-pal-row.sel { outline: 1px solid var(--accent-2); }
#rf-pal-foot { padding: 7px 14px; font-size: 10.5px; color: var(--muted); border-top: 1px solid var(--border); }

/* a long-running button says so */
button.busy { pointer-events: none; opacity: 0.65; position: relative; }
button.busy::after {
  content: ""; display: inline-block; width: 11px; height: 11px; margin-left: 8px;
  border: 2px solid currentColor; border-top-color: transparent; border-radius: 50%;
  vertical-align: -1px; animation: rf-spin 0.7s linear infinite;
}
@keyframes rf-spin { to { transform: rotate(360deg); } }

#dropzone, #img-dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 22px 12px;
  border: 1.5px dashed var(--border);
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--muted);
  text-align: center;
  font-size: 13px;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
#dropzone:hover, #dropzone.dragover,
#img-dropzone:hover, #img-dropzone.dragover {
  border-color: var(--accent);
  color: var(--text);
}
#dropzone strong, #img-dropzone strong { color: var(--accent); }

#load-status { margin-top: 8px; font-size: 12px; }
#load-status.ok { color: #6fd68a; }
#load-status.err { color: #ff6b6b; }

.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.field-grid label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 11.5px;
  color: var(--muted);
}
.field-grid input {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 7px 9px;
  font-size: 13px;
  width: 100%;
}
.field-grid input:focus { border-color: var(--accent-2); }
.field-grid input:focus:not(:focus-visible) { outline: none; }
.field-grid input:disabled { opacity: 0.55; }

.field-full {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 11.5px;
  color: var(--muted);
  margin-bottom: 10px;
}
.field-full select {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 7px 9px;
  font-size: 13px;
  font-family: inherit;
}
.field-full select:focus { border-color: var(--accent-2); }
.field-full select:focus:not(:focus-visible) { outline: none; }

/* every select and text input in the planner sidebar follows the panel
   theme — no control ever falls back to browser-default white */
:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; }

/* status toast mirrored over the map (setStatus writes it too) */
#map-toast { display: none; position: fixed; right: 90px; bottom: 22px; z-index: 1100; /* clear of the Cleatus FAB */
  max-width: 420px; padding: 10px 14px; border-radius: 9px; font-size: 13px; cursor: pointer;
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  box-shadow: 0 4px 18px rgba(0,0,0,.5); }
#map-toast.toast-ok { border-color: #2c5e38; color: #6fd68a; }
#map-toast.toast-err { border-color: #7a2727; color: #ff9c9c; }

#sidebar select,
#sidebar input[type="text"],
#sidebar input[type="number"] {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 7px 9px;
  font-size: 13px;
  font-family: inherit;
}
#sidebar select:focus,
#sidebar input[type="text"]:focus,
#sidebar input[type="number"]:focus { border-color: var(--accent-2); }
#sidebar select:focus:not(:focus-visible),
#sidebar input[type="text"]:focus:not(:focus-visible),
#sidebar input[type="number"]:focus:not(:focus-visible) { outline: none; }

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  margin-top: 10px;
  cursor: pointer;
}
.check input { accent-color: var(--accent); }

.hint { font-size: 11.5px; color: var(--muted); margin-top: 10px; line-height: 1.4; }

button {
  font: inherit;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
button:hover:not(:disabled) { border-color: var(--accent-2); }
button:disabled { opacity: 0.4; cursor: not-allowed; }

button.primary {
  width: 100%;
  background: var(--accent);
  border-color: var(--accent);
  color: #1a1208;
  font-weight: 600;
}
button.primary:hover:not(:disabled) { background: #ff8443; }
/* destructive actions look like it, EVERYWHERE — this was only defined
   inside ops/pilot inline styles, so Delete/Remove rendered as plain gray */
button.danger { border-color: #7a2727; color: #ff9c9c; }
button.danger:hover:not(:disabled) { background: #3a1d1d; border-color: #a33; }

.btn-row { display: flex; gap: 10px; }
.btn-row button { flex: 1; font-size: 13px; }

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 12px;
}
.stats.hidden { display: none; }
.stat {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 8px;
  text-align: center;
}
.stat span { display: block; font-size: 17px; font-weight: 600; color: var(--accent-2); }
.stat label { font-size: 10.5px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.8px; }

footer.muted { margin-top: auto; font-size: 10.5px; color: var(--muted); line-height: 1.5; }
.muted { color: var(--muted); }

#map { flex: 1; background: #0c1117; }

/* Top navigation bar (injected by auth.js on every signed-in page) */
.rf-topnav {
  position: sticky;
  top: 0;
  z-index: 1200; /* above Leaflet controls */
  display: flex;
  align-items: center;
  gap: 2px;
  flex-wrap: wrap;
  padding: 0 14px;
  min-height: 42px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.rf-topnav a {
  color: var(--muted);
  text-decoration: none;
  font-size: 13px;
  padding: 6px 12px;
  border-radius: 7px;
}
.rf-topnav a:hover { color: var(--text); background: var(--panel-2); }
.rf-topnav a.active { color: var(--accent); background: var(--panel-2); font-weight: 600; }
#rf-search {
  width: 170px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 7px;
  color: var(--text);
  padding: 5px 10px;
  font-size: 12.5px;
  margin-right: 4px;
}
#rf-search.miss { border-color: #ff6b6b; }
.rf-topnav a.rf-nav-user { color: var(--text); font-weight: 600; }
#rf-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.3px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  cursor: pointer;
  flex: 0 0 auto;
  margin-left: 6px;
  border: 1px solid var(--border);
  user-select: none;
}
#rf-avatar:hover { border-color: var(--accent); }
#rf-avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* avatar cropper overlay */
#rf-crop {
  position: fixed;
  inset: 0;
  z-index: 2000;
  background: rgba(5, 8, 12, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
}
#rf-crop-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
}
#rf-crop-card b { font-size: 14.5px; }
.rf-crop-hint { color: var(--muted); font-size: 11.5px; margin-top: -6px; }
#rf-crop-cv { border-radius: 10px; cursor: grab; touch-action: none; background: #0b0f14; }
#rf-crop-cv:active { cursor: grabbing; }
#rf-crop-zoom { width: 240px; accent-color: var(--accent); }
.rf-crop-row { display: flex; gap: 10px; }
.rf-crop-row button { padding: 8px 22px; }

/* ---------- phones & small tablets ---------- */
@media (max-width: 820px) {
  /* planner: sidebar and map stack instead of sitting side by side */
  #app { flex-direction: column; }
  #sidebar {
    width: 100%;
    min-width: 0;
    max-height: 48vh;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 14px 14px;
  }
  #map { flex: 1; min-height: 45vh; }

  /* top bar: tighter, search shrinks, rows wrap cleanly */
  .rf-topnav { padding: 4px 8px; gap: 0; min-height: 40px; }
  .rf-topnav a { padding: 6px 9px; font-size: 12.5px; }
  #rf-search { width: 110px; font-size: 12px; padding: 5px 8px; }

  /* two-column form grids collapse to one column */
  .grid2 { grid-template-columns: 1fr; }
  .grid3 { grid-template-columns: 1fr 1fr; }
  .field-grid { grid-template-columns: 1fr; }
  .op-row { grid-template-columns: 1fr; }

  /* wide tables (Manage users) scroll sideways instead of breaking the page */
  #user-groups details { overflow-x: auto; }
  #user-groups table { min-width: 560px; }
}

/* Cleatus — floating help bot */
#cleatus-fab {
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 1400;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--accent);
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
}
#cleatus-fab:hover { transform: scale(1.06); }
#cleatus-panel {
  position: fixed;
  right: 22px;
  bottom: 84px;
  z-index: 1400;
  width: min(340px, calc(100vw - 44px));
  height: 430px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#cleatus-panel[hidden] { display: none; }
#cleatus-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
  font-size: 14px;
}
#cleatus-head .dim { color: var(--muted); font-size: 11.5px; flex: 1; }
#cleatus-head button { background: none; border: none; color: var(--muted); font-size: 18px; cursor: pointer; padding: 0 2px; }
#cleatus-msgs { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.c-msg { max-width: 86%; padding: 8px 11px; border-radius: 11px; font-size: 12.5px; line-height: 1.55; white-space: pre-wrap; }
.c-msg.c-bot { background: var(--panel-2); border: 1px solid var(--border); align-self: flex-start; }
.c-msg.c-user { background: var(--accent); color: #fff; align-self: flex-end; }
.c-msg.c-wait { color: var(--muted); }
#cleatus-row { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--border); }
#cleatus-row textarea {
  flex: 1;
  resize: none;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 8px 10px;
  font-size: 13px;
  font-family: inherit;
}
#cleatus-row button { padding: 6px 14px; }
/* full-height app pages (planner map) share the column with the bar */
body.has-topnav { display: flex; flex-direction: column; }
body.has-topnav #app { height: auto; flex: 1; min-height: 0; }

/* Leaflet tweaks for dark UI */
.leaflet-container { font: inherit; }

/* Render standard OSM tiles as a dark basemap (keyless alternative to CARTO) */
.dark-tiles {
  filter: invert(1) hue-rotate(180deg) brightness(0.85) contrast(0.9) saturate(0.5);
}

.leaflet-control-layers {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.leaflet-control-layers-toggle {
  filter: invert(0.85);
}
.leaflet-control-layers label { font-size: 13px; }
.leaflet-control-layers input { accent-color: var(--accent); }
.well-label {
  background: rgba(16, 21, 28, 0.85);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 11px;
  white-space: nowrap;
  box-shadow: none;
}
.well-label::before { display: none; }

.order-badge {
  background: var(--accent);
  color: #1a1208;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  border: 2px solid #10151c;
}
.order-badge.start { background: #6fd68a; }

/* dark-themed marker popup with asset controls */
.leaflet-popup-content-wrapper, .leaflet-popup-tip {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  box-shadow: 0 4px 18px rgba(0,0,0,0.5);
}
.leaflet-popup-close-button { color: var(--muted) !important; }
.asset-popup { display: flex; flex-direction: column; gap: 8px; font-size: 13px; }
.asset-popup .ap-row { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
.asset-popup select {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px;
  color: var(--text); padding: 4px 7px; font-size: 12.5px; flex: 1;
}
.asset-popup input[type="range"] { width: 100%; accent-color: #e06bff; }
.asset-popup .ap-name {
  flex: 1;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px;
  color: var(--text); padding: 5px 8px; font-size: 13px; font-weight: 600;
}
.asset-popup .ap-name:focus { border-color: var(--accent-2); }
.asset-popup .ap-name:focus:not(:focus-visible) { outline: none; }
.asset-popup .ap-tag { font-size: 11px; color: var(--muted); margin-top: -4px; }
.asset-popup .ap-btns { display: flex; gap: 8px; }
.asset-popup .ap-btns button { flex: 1; }
.asset-popup .ap-dv { color: var(--text); font-weight: 600; }
.asset-popup .ap-info { font-size: 11.5px; color: var(--muted); line-height: 1.4; }
.asset-popup button {
  font-size: 12px; padding: 6px 10px;
}

.launch-badge {
  background: #16263b;
  color: #fff;
  border: 2px solid #f26b21;
  border-radius: 6px;
  min-width: 24px;
  height: 22px;
  padding: 0 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}
#add-launch.active, #add-sites.active { border-color: var(--accent); color: var(--accent); }

.row-split { display: flex; gap: 8px; align-items: stretch; }
.seg-toggle { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg-toggle .seg {
  border: none; border-radius: 0; padding: 8px 12px; font-size: 12.5px;
  background: var(--panel-2); color: var(--muted);
}
.seg-toggle .seg.active { background: var(--accent); color: #1a1208; font-weight: 600; }
#add-sites { flex: 1; font-size: 12.5px; }

/* heavy-lift pages: friendly phone notice (hidden on desktop) */
.mobile-note { display: none; }
@media (max-width: 820px) {
  .mobile-note {
    display: flex;
    gap: 12px;
    align-items: center;
    background: var(--panel-2);
    border: 1px solid var(--accent);
    border-radius: 10px;
    padding: 12px 14px;
    font-size: 13px;
    line-height: 1.5;
  }
  .mobile-note img { width: 34px; height: 34px; flex: 0 0 auto; }
}

/* ---- in-app modal (replaces browser prompt/confirm/alert) ---- */
#rf-modal-overlay { position: fixed; inset: 0; z-index: 3000; background: rgba(6,10,16,.72);
  display: flex; align-items: center; justify-content: center; padding: 18px; }
#rf-modal { background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  max-width: 440px; width: 100%; padding: 20px 22px; box-shadow: 0 10px 40px rgba(0,0,0,.6); }
#rf-modal h3 { font-size: 15px; margin: 0 0 8px; }
#rf-modal .rf-modal-body { font-size: 13px; color: var(--muted); line-height: 1.55; white-space: pre-wrap; }
#rf-modal input[type="text"] { width: 100%; margin-top: 12px; background: var(--panel-2);
  border: 1px solid var(--border); border-radius: 6px; color: var(--text); padding: 8px 10px; font-size: 13.5px; }
#rf-modal input[type="text"]:focus { border-color: var(--accent-2); }
#rf-modal input[type="text"]:focus:not(:focus-visible) { outline: none; }
#rf-modal .rf-modal-row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }
#rf-modal .rf-modal-row button { width: auto; padding: 8px 16px; font-size: 13px; }
