/* Wenet telemetry display.
 *
 * Colour roles come from the validated reference palette; both modes are
 * selected steps, not an automatic flip. The categorical slots identify
 * sources, and their order is fixed -- a source keeps its hue when others are
 * filtered out, so "the neck payload is orange" stays true.
 *
 * Three light-mode categorical slots sit below 3:1 on the surface. Every source
 * therefore carries a visible text label beside its colour dot, so identity is
 * never colour alone.
 */

:root {
  color-scheme: light;

  --surface-1:      #fcfcfb;   /* chart / panel surface */
  --plane:          #f9f9f7;   /* page behind the panels */
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #898781;
  --grid:           #e1e0d9;
  --axis:           #c3c2b7;
  --border:         rgba(11, 11, 11, 0.10);

  --series-1: #2a78d6;  --series-2: #eb6834;  --series-3: #1baf7a;
  --series-4: #eda100;  --series-5: #e87ba4;  --series-6: #008300;
  --series-7: #4a3aa7;  --series-8: #e34948;

  /* Sequential blue, for altitude on the map. One hue, light to dark. */
  --seq-100: #cde2fb; --seq-250: #86b6ef; --seq-400: #3987e5;
  --seq-550: #1c5cab; --seq-700: #0d366b;

  --good:     #0ca30c;
  --critical: #d03b3b;
  --warning:  #fab219;

  --radius: 10px;
  --pad: 16px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
}

:root[data-theme="dark"],
:root:where(:not([data-theme="light"])) {
  /* placeholder so the dark block below can be scoped consistently */
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-1:      #1a1a19;
    --plane:          #0d0d0d;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #898781;
    --grid:           #2c2c2a;
    --axis:           #383835;
    --border:         rgba(255, 255, 255, 0.10);

    --series-1: #3987e5;  --series-2: #d95926;  --series-3: #199e70;
    --series-4: #c98500;  --series-5: #d55181;  --series-6: #008300;
    --series-7: #9085e9;  --series-8: #e66767;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1:      #1a1a19;
  --plane:          #0d0d0d;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #898781;
  --grid:           #2c2c2a;
  --axis:           #383835;
  --border:         rgba(255, 255, 255, 0.10);

  --series-1: #3987e5;  --series-2: #d95926;  --series-3: #199e70;
  --series-4: #c98500;  --series-5: #d55181;  --series-6: #008300;
  --series-7: #9085e9;  --series-8: #e66767;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--plane);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.45;
}

h1, h2 { margin: 0; font-weight: 600; }
h1 { font-size: 15px; letter-spacing: -0.01em; }
h2 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em;
     color: var(--text-secondary); }

/* ---------------------------------------------------------------- top bar */

.topbar {
  display: flex;
  align-items: center;
  gap: var(--pad);
  padding: 10px var(--pad);
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 210px; }
.brand-mark {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--series-1); flex: none;
}

#flight-select {
  font: inherit;
  font-size: 12px;
  color: var(--text-secondary);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1px 6px;
  margin-top: 2px;
  max-width: 210px;
}

/* Stat tiles: a single headline number is a tile, not a chart. */
.tiles { display: flex; gap: 8px; flex: 1; flex-wrap: wrap; }

.tile {
  background: var(--plane);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 12px;
  min-width: 92px;
}
.tile-label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.06em;
              color: var(--text-muted); display: flex; align-items: center; gap: 5px; }
.tile-value { font-size: 21px; font-weight: 600; line-height: 1.15;
              color: var(--text-primary); }
.tile-unit  { font-size: 10px; color: var(--text-muted); }
/* Marks a value as last-known rather than current -- paired with the words
   "no fix", never colour alone. */
.tile-unit.stale { color: var(--warning); font-weight: 600; }

/* Cards for checked fields. They can outnumber the fixed tiles, so this scrolls
   sideways rather than growing the header until it eats the display. */
.tiles-dynamic {
  display: flex; gap: 8px; flex: 1; min-width: 0;
  overflow-x: auto; padding-bottom: 2px;
}
.tiles-dynamic .tile { flex: none; }
/* Field names are identifiers -- `a_x`, `hdc_temp`. Upper-casing them the way
   the fixed tiles do would misrepresent what the payload actually sends. */
.tiles-dynamic .tile-label { text-transform: none; letter-spacing: 0; font-size: 11px; }
.tiles-dynamic::-webkit-scrollbar { height: 6px; }
.tiles-dynamic::-webkit-scrollbar-thumb { background: var(--grid); border-radius: 3px; }

/* The dot ties the card to its source; the label carries the identity, so the
   colour never has to do that alone. */
.tile-key { width: 7px; height: 7px; border-radius: 50%; flex: none; }
.tile-source { font-size: 9px; color: var(--text-muted);
               overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
               max-width: 108px; }
.tile.at-cursor { outline: 1px solid var(--axis); outline-offset: -1px; }

.topbar-actions { display: flex; align-items: center; gap: 8px; }

.status { display: inline-flex; align-items: center; gap: 6px; font-size: 12px;
          color: var(--text-secondary); white-space: nowrap; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); }
.status[data-state="live"]    .status-dot { background: var(--good); }
.status[data-state="stalled"] .status-dot { background: var(--critical); }
.status[data-state="replay"]  .status-dot { background: var(--warning); }

#flight-name { cursor: text; }
#flight-name:hover { text-decoration: underline dotted var(--text-muted); }
#flight-name[contenteditable="true"] {
  outline: 2px solid var(--series-1); outline-offset: 2px; border-radius: 3px;
  cursor: text;
}

/* ------------------------------------------------------------ export menu */

.menu { position: relative; }

.menu-panel {
  position: absolute; right: 0; top: calc(100% + 6px);
  z-index: 30;
  min-width: 232px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
  padding: 6px;
}
.menu-panel[hidden] { display: none; }

.menu-label {
  font-size: 10px; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--text-muted); padding: 6px 8px 2px;
}
.menu-item {
  display: block;
  padding: 5px 8px;
  border-radius: 6px;
  font-size: 12px;
  color: var(--text-secondary);
  text-decoration: none;
}
.menu-item:hover { background: var(--plane); color: var(--text-primary); }

.btn {
  font: inherit; font-size: 12px;
  color: var(--text-secondary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 9px;
  cursor: pointer;
}
.btn:hover { color: var(--text-primary); }

/* ----------------------------------------------------------------- layout */

.layout {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  gap: var(--pad);
  padding: var(--pad);
  align-items: start;
}

.panel {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.panel-actions { display: flex; align-items: center; gap: 6px; }
.hint { font-size: 11px; color: var(--text-muted); }

.content { display: flex; flex-direction: column; gap: var(--pad); min-width: 0; }

/* ---------------------------------------------------------------- sources */

.sources { padding: 4px; }

.payload-group { margin-bottom: 4px; }
.payload-name {
  font-size: 11px; font-weight: 600; color: var(--text-secondary);
  padding: 6px 8px 2px;
}

.source {
  border-radius: 8px;
  padding: 6px 8px;
  margin-bottom: 2px;
}
.source-head { display: flex; align-items: center; gap: 7px; }
.source-dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.source-name { font-size: 12px; font-weight: 600; color: var(--text-primary);
               overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.source-meta { margin-left: auto; font-size: 10px; color: var(--text-muted);
               font-variant-numeric: tabular-nums; white-space: nowrap; }
.source.stalled .source-name { color: var(--text-muted); }
.source-flag { font-size: 10px; color: var(--critical); font-weight: 600; }

.fields { margin: 4px 0 0; display: flex; flex-direction: column; gap: 1px; }

.field {
  display: flex; align-items: center; gap: 7px;
  padding: 2px 4px 2px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
}
.field:hover { background: var(--plane); }
.field input { margin: 0; accent-color: var(--series-1); flex: none; }
.field-name { color: var(--text-secondary); overflow: hidden;
              text-overflow: ellipsis; white-space: nowrap; }
.field-value {
  margin-left: auto; color: var(--text-primary);
  font-variant-numeric: tabular-nums; white-space: nowrap;
}
.field-unit { color: var(--text-muted); font-size: 10px; }
.field.text-field { cursor: default; opacity: 0.75; }
.field.text-field:hover { background: none; }

.reporters { padding: 8px 12px 12px; display: flex; flex-direction: column; gap: 6px; }
.reporter { display: flex; align-items: center; gap: 8px; font-size: 12px; }
.reporter-name { font-weight: 600; }
.reporter-bar { flex: 1; height: 4px; border-radius: 2px; background: var(--grid);
                overflow: hidden; }
.reporter-fill { height: 100%; background: var(--series-1); border-radius: 2px; }
.reporter-count { color: var(--text-muted); font-variant-numeric: tabular-nums;
                  font-size: 11px; }

/* -------------------------------------------------------------------- map */

.map-wrap { position: relative; height: 340px; }
#map { width: 100%; height: 100%; display: block; cursor: grab; }
#map.dragging { cursor: grabbing; }

.map-empty, .map-attrib { position: absolute; }
.map-empty {
  inset: 0; display: grid; place-items: center;
  color: var(--text-muted); font-size: 13px; pointer-events: none;
}
/* `display` here would otherwise beat the hidden attribute's display:none. */
.map-empty[hidden] { display: none; }
.map-attrib {
  right: 6px; bottom: 4px;
  font-size: 9px; color: var(--text-muted);
  background: color-mix(in srgb, var(--surface-1) 78%, transparent);
  padding: 1px 5px; border-radius: 4px;
}

/* Scale legend for the sequential altitude ramp. */
.ramp-legend { display: inline-flex; align-items: center; gap: 5px; font-size: 10px;
               color: var(--text-muted); font-variant-numeric: tabular-nums; }
.ramp-bar {
  width: 60px; height: 6px; border-radius: 3px;
  background: linear-gradient(90deg, var(--seq-100), var(--seq-250),
              var(--seq-400), var(--seq-550), var(--seq-700));
}
.ramp-caption { text-transform: uppercase; letter-spacing: 0.05em; }

/* ----------------------------------------------------------------- charts */

.range-readout { font-size: 10px; color: var(--text-muted);
                 font-variant-numeric: tabular-nums; white-space: nowrap; }

#range-select {
  font: inherit; font-size: 11px;
  color: var(--text-secondary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 3px 6px;
}

.chart canvas { cursor: crosshair; }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 6px;
             overflow: hidden; }
.seg {
  font: inherit; font-size: 11px; padding: 3px 10px;
  background: var(--surface-1); color: var(--text-secondary);
  border: 0; cursor: pointer;
}
.seg.active { background: var(--series-1); color: #fff; }

.charts { padding: 8px; display: flex; flex-direction: column; gap: 6px; }

/* Small multiples: one field per panel, one series each. A single series needs
   no legend -- the title names it. */
.chart {
  border-radius: 8px;
  padding: 6px 8px 2px;
}
.chart-head { display: flex; align-items: baseline; gap: 8px; }
.chart-key { width: 9px; height: 9px; border-radius: 50%; flex: none;
             transform: translateY(-1px); }
.chart-title { font-size: 12px; font-weight: 600; color: var(--text-primary); }
.chart-sub { font-size: 10px; color: var(--text-muted); }
.chart-last { margin-left: auto; font-size: 12px; font-variant-numeric: tabular-nums;
              color: var(--text-primary); }
.chart canvas { width: 100%; height: 108px; display: block; }

.empty { color: var(--text-muted); font-size: 12px; padding: 16px; text-align: center; }
.empty-sub { font-size: 11px; opacity: 0.8; }

.tooltip {
  position: fixed; z-index: 50; pointer-events: none;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 11px;
  color: var(--text-primary);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.tooltip b { font-weight: 600; }
.tooltip-dim { color: var(--text-muted); }

/* ------------------------------------------------------------- responsive */

@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .topbar { gap: 10px; }
  .tiles { order: 3; width: 100%; }
  .map-wrap { height: 260px; }
}

/* A chase vehicle in direct sun: bigger type, heavier ink. */
@media (prefers-contrast: more) {
  body { font-size: 15px; }
  .tile-value { font-size: 24px; }
  :root { --text-secondary: var(--text-primary); --border: rgba(11,11,11,0.3); }
}
