/* ==========================================================================
   Tokens. Light is the base definition; dark is redefined under both the OS
   media query and the explicit [data-theme] stamp so the toggle wins either
   way. Status colours are fixed in both modes and are never used for anything
   but state.
   ========================================================================== */

:root {
  color-scheme: light;

  --page:            #f9f9f7;
  --surface-1:       #fcfcfb;
  --surface-2:       #f2f1ed;
  --text-primary:    #0b0b0b;
  --text-secondary:  #52514e;
  --text-muted:      #898781;
  --grid:            #e1e0d9;
  --baseline:        #c3c2b7;
  --border:          rgba(11, 11, 11, 0.10);
  --border-strong:   rgba(11, 11, 11, 0.18);
  --series-1:        #2a78d6;
  --shadow:          0 1px 2px rgba(11,11,11,.06), 0 8px 24px rgba(11,11,11,.08);
  --map-filter:      saturate(0.35) brightness(1.03) contrast(0.96);

  /* Status palette - fixed, never themed. */
  --status-ok:       #0ca30c;
  --status-warn:     #fab219;
  --status-serious:  #ec835a;
  --status-crit:     #d03b3b;
  --status-offline:  #898781;
  --status-nodata:   #b9b7ae;

  --radius:   10px;
  --radius-s: 6px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page:           #0d0d0d;
    --surface-1:      #1a1a19;
    --surface-2:      #232322;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #898781;
    --grid:           #2c2c2a;
    --baseline:       #383835;
    --border:         rgba(255, 255, 255, 0.10);
    --border-strong:  rgba(255, 255, 255, 0.20);
    --series-1:       #3987e5;
    --shadow:         0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.5);
    --status-nodata:  #4a4a47;
    --map-filter:     invert(1) grayscale(1) brightness(0.86) contrast(0.92);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page:           #0d0d0d;
  --surface-1:      #1a1a19;
  --surface-2:      #232322;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #898781;
  --grid:           #2c2c2a;
  --baseline:       #383835;
  --border:         rgba(255, 255, 255, 0.10);
  --border-strong:  rgba(255, 255, 255, 0.20);
  --series-1:       #3987e5;
  --shadow:         0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.5);
  --status-nodata:  #4a4a47;
  --map-filter:     invert(1) grayscale(1) brightness(0.86) contrast(0.92);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font-family: var(--font);
  background: var(--page);
  color: var(--text-primary);
  font-size: 14px;
  overflow: hidden;
}

button { font-family: inherit; font-size: inherit; color: inherit; cursor: pointer; }

/* ==========================================================================
   Shell
   ========================================================================== */

#app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100vh;
}

/* --- header ------------------------------------------------------------ */

header {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 16px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand { display: flex; align-items: baseline; gap: 10px; margin-right: auto; }
.brand h1 { font-size: 15px; font-weight: 650; margin: 0; letter-spacing: -0.01em; }
.brand .sub { font-size: 12px; color: var(--text-muted); }

.status-pills { display: flex; gap: 6px; }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px 4px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--text-secondary);
}
.pill.zero { opacity: .45; }
.pill b { color: var(--text-primary); font-weight: 650; }

.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.dot.ok { background: var(--status-ok); }
.dot.warn { background: var(--status-warn); }
.dot.crit { background: var(--status-crit); }
.dot.offline { background: var(--status-offline); }
.dot.nodata { background: var(--status-nodata); }

.stat {
  display: flex; flex-direction: column; gap: 1px;
  padding-left: 16px; border-left: 1px solid var(--border);
}
.stat .k { font-size: 10px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-muted); }
.stat .v { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }

.icon-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  padding: 6px 10px;
  color: var(--text-secondary);
  font-size: 12px;
}
.icon-btn:hover { border-color: var(--border-strong); color: var(--text-primary); }

/* --- body --------------------------------------------------------------- */

.body { display: grid; grid-template-columns: minmax(0, 1fr) 400px; min-height: 0; }

#map { width: 100%; height: 100%; background: var(--surface-2); }
.leaflet-tile-pane { filter: var(--map-filter); }
.leaflet-control-attribution { font-size: 10px !important; }

.map-wrap { position: relative; min-height: 0; }

.replay-banner {
  position: absolute; z-index: 500; top: 12px; left: 50%; transform: translateX(-50%);
  display: none; align-items: center; gap: 10px;
  padding: 7px 12px;
  background: var(--surface-1);
  border: 1px solid var(--status-warn);
  border-radius: 999px;
  box-shadow: var(--shadow);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.replay-banner.on { display: flex; }
.replay-banner .tag {
  font-size: 10px; font-weight: 700; letter-spacing: .08em;
  color: var(--status-warn);
}

/* --- side panel --------------------------------------------------------- */

aside {
  background: var(--surface-1);
  border-left: 1px solid var(--border);
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  min-height: 0;
}

.panel-scroll { overflow-y: auto; padding: 14px; }

.panel-head { display: flex; align-items: flex-start; gap: 10px; margin-bottom: 4px; }
.panel-head h2 { margin: 0; font-size: 15px; font-weight: 650; letter-spacing: -0.01em; }
.panel-head .meta { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.back-btn { margin-left: auto; }

.sev-chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 600; letter-spacing: .02em;
  border: 1px solid var(--border);
}
.sev-chip.ok      { color: var(--status-ok); }
.sev-chip.warn    { color: var(--status-warn); }
.sev-chip.crit    { color: var(--status-crit); }
.sev-chip.offline { color: var(--status-offline); }
.sev-chip.nodata  { color: var(--status-nodata); }

.badge-sim {
  font-size: 9px; font-weight: 700; letter-spacing: .07em;
  padding: 2px 5px; border-radius: 4px;
  border: 1px dashed var(--border-strong); color: var(--text-muted);
}

/* fleet list */
.router-card {
  display: grid;
  grid-template-columns: auto minmax(0,1fr) auto;
  gap: 10px; align-items: center;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  margin-bottom: 8px;
  cursor: pointer;
}
.router-card:hover { border-color: var(--border-strong); }
.router-card .idx {
  width: 26px; height: 26px; border-radius: 50%;
  display: grid; place-items: center;
  font-size: 12px; font-weight: 700; color: #fff;
}
.router-card .nm { font-size: 13px; font-weight: 600; }
.router-card .sm { font-size: 11px; color: var(--text-muted); margin-top: 1px; }
.router-card .rt { text-align: right; font-variant-numeric: tabular-nums; }
.router-card .rt b { font-size: 14px; font-weight: 650; }
.router-card .rt span { display: block; font-size: 10px; color: var(--text-muted); }

/* detail sections */
.section-title {
  font-size: 10px; text-transform: uppercase; letter-spacing: .07em;
  color: var(--text-muted); font-weight: 650;
  margin: 16px 0 7px;
}

.kv { display: grid; grid-template-columns: 108px minmax(0,1fr); gap: 3px 10px; font-size: 12px; }
.kv dt { color: var(--text-muted); }
.kv dd { margin: 0; color: var(--text-primary); font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.kv dd.mono { font-family: var(--mono); font-size: 11px; }

.metric-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 8px; }

.tile {
  border: 1px solid var(--border);
  border-left: 3px solid var(--baseline);
  border-radius: var(--radius-s);
  padding: 8px 10px;
  background: var(--surface-2);
}
.tile.ok   { border-left-color: var(--status-ok); }
.tile.warn { border-left-color: var(--status-warn); }
.tile.crit { border-left-color: var(--status-crit); }
.tile .k { font-size: 10px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-muted); }
.tile .v { font-size: 17px; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 1px; }
.tile .v small { font-size: 11px; font-weight: 500; color: var(--text-secondary); margin-left: 2px; }
.tile .lim { font-size: 10px; color: var(--text-muted); font-variant-numeric: tabular-nums; }

/* alerts */
.alert-row {
  display: flex; gap: 8px; align-items: flex-start;
  padding: 7px 9px; border-radius: var(--radius-s);
  border: 1px solid var(--border);
  background: var(--surface-2);
  margin-bottom: 6px;
  font-size: 12px;
}
.alert-row .ico { font-size: 11px; font-weight: 700; line-height: 1.35; flex: none; }
.alert-row.warn    .ico { color: var(--status-warn); }
.alert-row.crit    .ico { color: var(--status-crit); }
.alert-row.offline .ico { color: var(--status-offline); }
.alert-row .who { color: var(--text-muted); font-size: 11px; }
.alert-row time { color: var(--text-muted); font-size: 11px; margin-left: auto; white-space: nowrap;
                  font-variant-numeric: tabular-nums; }

.empty { color: var(--text-muted); font-size: 12px; padding: 10px 2px; }

/* history charts */
.chart-block { margin-bottom: 12px; }
.chart-head {
  display: flex; align-items: baseline; gap: 8px; margin-bottom: 2px;
}
.chart-head .t { font-size: 11px; font-weight: 600; color: var(--text-secondary); }
.chart-head .rng { font-size: 10px; color: var(--text-muted); margin-left: auto;
                   font-variant-numeric: tabular-nums; }
.chart-head .cur { font-size: 11px; color: var(--text-primary); font-variant-numeric: tabular-nums; }
.spark { width: 100%; height: 44px; display: block; overflow: visible; }

/* events dock */
.events-dock { border-top: 1px solid var(--border); max-height: 190px; display: flex; flex-direction: column; }
.events-dock .hd {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 14px; font-size: 10px; text-transform: uppercase;
  letter-spacing: .07em; color: var(--text-muted); font-weight: 650;
}
.events-dock .list { overflow-y: auto; padding: 0 14px 12px; }
.event-row { display: flex; gap: 8px; font-size: 11.5px; padding: 4px 0; border-top: 1px solid var(--grid); }
.event-row:first-child { border-top: none; }
.event-row time { color: var(--text-muted); font-variant-numeric: tabular-nums; flex: none; }
.event-row .msg { overflow-wrap: anywhere; }
.event-row.raised .msg  { color: var(--text-primary); }
.event-row.cleared .msg { color: var(--text-muted); }

/* ==========================================================================
   Timeline
   ========================================================================== */

.timeline {
  background: var(--surface-1);
  border-top: 1px solid var(--border);
  padding: 8px 16px 10px;
  user-select: none;
}

.tl-controls { display: flex; align-items: center; gap: 8px; margin-bottom: 7px; flex-wrap: wrap; }

.tl-label { font-size: 10px; text-transform: uppercase; letter-spacing: .07em;
            color: var(--text-muted); font-weight: 650; }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius-s); overflow: hidden; }
.seg button {
  background: var(--surface-2); border: none; padding: 4px 9px;
  font-size: 11px; color: var(--text-secondary); border-right: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}
.seg button:last-child { border-right: none; }
.seg button[aria-pressed="true"] { background: var(--series-1); color: #fff; font-weight: 600; }

.live-btn {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border); border-radius: var(--radius-s);
  background: var(--surface-2); padding: 4px 10px; font-size: 11px; font-weight: 600;
  color: var(--text-muted);
}
.live-btn[data-live="true"] { color: var(--status-crit); border-color: var(--status-crit); }
.live-btn[data-live="true"] .dot { background: var(--status-crit); animation: blink 2s infinite; }
.live-btn .dot { background: var(--text-muted); }
@keyframes blink { 0%,100% { opacity: 1; } 50% { opacity: .25; } }

.tl-clock { font-family: var(--mono); font-size: 12px; font-variant-numeric: tabular-nums; margin-left: auto; }
.tl-clock .hint { color: var(--text-muted); font-family: var(--font); font-size: 11px; margin-left: 8px; }

/* the scrub track */
.tl-track {
  position: relative;
  display: grid;
  grid-template-columns: 124px minmax(0, 1fr);
  gap: 2px 8px;
  align-items: center;
  cursor: crosshair;
  padding-bottom: 14px;
}
.tl-row-label {
  font-size: 10.5px; color: var(--text-muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tl-lane {
  position: relative; height: 12px; border-radius: 3px;
  background: var(--grid); overflow: hidden;
}
.tl-lane canvas { display: block; width: 100%; height: 100%; }

.tl-axis {
  position: absolute; left: 132px; right: 0; bottom: 0; height: 12px;
  pointer-events: none;
}
.tl-axis span {
  position: absolute; transform: translateX(-50%);
  font-size: 9.5px; color: var(--text-muted); font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.tl-playhead {
  position: absolute; top: 0; bottom: 12px; width: 1px;
  background: var(--text-primary); pointer-events: none;
  box-shadow: 0 0 0 1px var(--surface-1);
}
.tl-playhead::before {
  content: ""; position: absolute; top: -3px; left: 50%;
  transform: translateX(-50%);
  border-left: 5px solid transparent; border-right: 5px solid transparent;
  border-top: 6px solid var(--text-primary);
}
.tl-playhead.hidden { display: none; }

.tl-hover {
  position: absolute; top: 0; bottom: 12px; width: 1px;
  background: var(--text-muted); pointer-events: none; opacity: .6;
}
.tl-hover.hidden { display: none; }

/* ==========================================================================
   Map markers & tooltip
   ========================================================================== */

.marker-pin {
  width: 30px; height: 30px; border-radius: 50%;
  display: grid; place-items: center;
  font: 700 12px/1 var(--font); color: #fff;
  border: 2px solid var(--surface-1);
  box-shadow: 0 2px 8px rgba(0,0,0,.45);
  transition: transform .12s ease;
}
.marker-pin.selected { transform: scale(1.25); }
.marker-wrap { position: relative; }
.marker-wrap.pulse::after {
  content: ""; position: absolute; inset: -6px;
  border-radius: 50%; border: 2px solid var(--status-crit);
  animation: pulse 1.8s ease-out infinite;
}
@keyframes pulse {
  0%   { transform: scale(.7); opacity: .9; }
  100% { transform: scale(1.5); opacity: 0; }
}

.leaflet-tooltip.rt-tip {
  background: var(--surface-1);
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px 12px;
  font-family: var(--font);
  font-size: 12px;
  white-space: normal;
  width: 268px;
  opacity: 1 !important;
}
.leaflet-tooltip.rt-tip::before { display: none; }

.tip-head { display: flex; align-items: center; gap: 7px; margin-bottom: 7px; }
.tip-head .nm { font-weight: 650; font-size: 13px; }
.tip-sec {
  font-size: 9.5px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-muted); font-weight: 650;
  margin: 8px 0 3px; padding-top: 6px; border-top: 1px solid var(--grid);
}
.tip-grid { display: grid; grid-template-columns: auto minmax(0,1fr); gap: 2px 10px; }
.tip-grid .k { color: var(--text-muted); }
.tip-grid .v { font-variant-numeric: tabular-nums; text-align: right; }
.tip-grid .v.warn { color: var(--status-warn); font-weight: 600; }
.tip-grid .v.crit { color: var(--status-crit); font-weight: 600; }
.tip-foot { margin-top: 7px; font-size: 10.5px; color: var(--text-muted); }

@media (max-width: 1100px) {
  .body { grid-template-columns: minmax(0,1fr) 330px; }
}

/* ==========================================================================
   Map legend — track casing (router identity) and track fill (RSRP)
   ========================================================================== */

.map-legend {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 9px 11px;
  font-size: 11px;
  color: var(--text-secondary);
  min-width: 210px;
}
.map-legend .lg-t {
  font-size: 9.5px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-muted); font-weight: 650; margin-bottom: 5px;
}
.lg-row { display: flex; align-items: center; gap: 7px; padding: 1.5px 0; }
.lg-line { width: 18px; height: 4px; border-radius: 2px; flex: none; }
.lg-nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lg-x { margin-left: auto; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.lg-ramp { display: flex; height: 8px; border-radius: 2px; overflow: hidden; }
.lg-ramp span { flex: 1; }
.lg-scale {
  display: flex; justify-content: space-between; margin-top: 2px;
  font-size: 9.5px; color: var(--text-muted); font-variant-numeric: tabular-nums;
}

/* Sampling profile switch */
.sampling-seg { width: 100%; }
.sampling-seg button { flex: 1; padding: 6px 8px; }
.sampling-note { margin-top: 5px; font-size: 10.5px; color: var(--text-muted); }
