:root {
  --bg: #0b1220;
  --panel: #121a2c;
  --panel-2: #1a2440;
  --ink: #e6ecf7;
  --ink-dim: #9aa7c2;
  --accent: #5aa9ff;
  --accent-2: #7af0c8;
  --warn: #ffb86b;
  --bad: #e8453d;
  --line: #243049;
}
* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg); color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", sans-serif;
  line-height: 1.5;
  font-size: 14px;
}
.dim { color: var(--ink-dim); }
b { color: inherit; }
button {
  background: var(--panel-2); color: var(--ink); border: 1px solid var(--line);
  padding: 6px 12px; border-radius: 6px; cursor: pointer; font: inherit;
  transition: background .12s, border-color .12s;
}
button:hover { background: #243057; border-color: var(--accent); }
button.primary { background: var(--accent); color: #0b1220; border-color: transparent; font-weight: 600; }
button.primary:hover { background: #7cbcff; }
button.danger { color: var(--bad); }
select, input[type="range"] { background: var(--panel-2); color: var(--ink); border: 1px solid var(--line); border-radius: 6px; padding: 4px 6px; font: inherit; }

/* ---------- Layout ---------- */
.app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
}
header.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 12px 18px; border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #0e1730 0%, #0b1220 100%);
}
header h1 {
  font-size: 17px; margin: 0; font-weight: 600;
  letter-spacing: 0.5px;
}
header h1 .accent { color: var(--accent); }
header .meta { color: var(--ink-dim); font-size: 12px; }
header .controls { margin-left: auto; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
header .controls label { font-size: 12px; color: var(--ink-dim); }

.body {
  display: grid;
  grid-template-columns: 1fr 320px;
  grid-template-rows: 1fr 130px 280px;
  grid-template-areas:
    "map sidebar"
    "timeline sidebar"
    "network sidebar";
  gap: 1px;
  background: var(--line);
  overflow: hidden;
}
.map-pane { grid-area: map; background: #08101e; position: relative; }
.timeline-pane { grid-area: timeline; background: var(--panel); padding: 6px 8px; }
.network-pane { grid-area: network; background: var(--panel); position: relative; }
.sidebar-pane {
  grid-area: sidebar; background: var(--panel);
  display: grid; grid-template-rows: auto 1fr auto;
  overflow: hidden;
}
.sidebar-summary { padding: 12px 14px; border-bottom: 1px solid var(--line); font-size: 13px; }
.sidebar-summary > div { margin-bottom: 2px; }
.sidebar-list-h, .sidebar-detail-h {
  padding: 10px 14px 6px;
  font-size: 11px; letter-spacing: 1.5px; text-transform: uppercase;
  color: var(--ink-dim);
  border-bottom: 1px solid var(--line);
  background: var(--panel-2);
}
.sidebar-list { overflow-y: auto; flex: 1; min-height: 0; }
.sidebar-detail { border-top: 1px solid var(--line); max-height: 50vh; overflow-y: auto; }

#map { width: 100%; height: 100%; }
#network { width: 100%; height: 100%; display: block; }
#timeline { width: 100%; height: 100%; display: block; }

/* ---------- Anomaly list rows ---------- */
.anom-row {
  display: grid;
  grid-template-columns: 4px 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  transition: background .1s;
}
.anom-row:hover { background: var(--panel-2); }
.anom-row.sel { background: #1d2b50; }
.anom-bar { width: 4px; height: 32px; background: var(--line); border-radius: 2px; position: relative; overflow: hidden; }
.anom-fill { position: absolute; bottom: 0; left: 0; right: 0; }
.anom-info { font-size: 12px; }
.anom-info b { font-family: ui-monospace, monospace; font-size: 13px; }
.anom-score { font-family: ui-monospace, monospace; font-weight: 600; }

/* ---------- Detail panel ---------- */
.sidebar-detail .detail-head {
  display: flex; justify-content: space-between; align-items: flex-start;
  padding: 12px 14px 4px;
}
.sidebar-detail .big {
  font-family: ui-monospace, monospace; font-size: 18px; font-weight: 600;
}
.sidebar-detail .route {
  padding: 6px 14px 12px;
  font-family: ui-monospace, monospace;
}
.sidebar-detail .route .arrow { color: var(--ink-dim); margin: 0 6px; }
.status-pill {
  font-size: 11px; padding: 3px 8px; border-radius: 999px; text-transform: uppercase;
  letter-spacing: 1px; font-weight: 600;
}
.status-completed { background: #16382a; color: var(--accent-2); }
.status-diverted { background: #4a2d18; color: #ffc28a; }
.status-returned { background: #443a18; color: #f1d97a; }
.status-cancelled { background: #4a1f1c; color: #ffb1ac; }
.scores {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
  padding: 8px 14px;
}
.scores .score-item {
  background: var(--panel-2); border-radius: 6px; padding: 8px 10px;
  text-align: center;
}
.scores .score-item .dim { font-size: 10px; letter-spacing: 1px; text-transform: uppercase; }
.scores .score-item .big { font-size: 17px; }
.section-h {
  padding: 10px 14px 4px;
  font-size: 10px; letter-spacing: 1.5px; text-transform: uppercase; color: var(--ink-dim);
}
.flag {
  margin: 4px 14px;
  background: var(--panel-2);
  border-left: 3px solid var(--bad);
  padding: 6px 10px;
  font-size: 12px;
  border-radius: 0 4px 4px 0;
}
.flag b { font-family: ui-monospace, monospace; color: #ffd2cb; font-size: 11px; letter-spacing: 0.5px; }
.sidebar-detail .dim { padding: 0 14px 14px; font-size: 12px; line-height: 1.7; }

/* ---------- Network view ---------- */
.network-pane .net-h {
  position: absolute; top: 8px; left: 12px;
  font-size: 11px; letter-spacing: 1.5px; text-transform: uppercase;
  color: var(--ink-dim); pointer-events: none;
}
.network-pane circle.stressed {
  animation: pulseRing 1.6s ease-in-out infinite;
}
.network-pane circle.selected {
  stroke: var(--accent); stroke-width: 2.5;
}
.network-pane line.pulse {
  stroke-dasharray: 4 6;
  animation: dash 1.4s linear infinite;
}
@keyframes dash { to { stroke-dashoffset: -20; } }
@keyframes pulseRing {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}

/* ---------- Timeline ---------- */
.timeline-pane .tl-h {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 11px; letter-spacing: 1.5px; text-transform: uppercase;
  color: var(--ink-dim); padding: 0 6px;
}
.tl-legend { display: flex; gap: 12px; font-size: 10px; }
.tl-legend span { display: inline-flex; align-items: center; gap: 4px; letter-spacing: 0.5px; }
.tl-legend .swatch { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
.swatch-completed { background: #3b9c6e; }
.swatch-diverted  { background: #e89045; }
.swatch-returned  { background: #d8a83d; }
.swatch-cancelled { background: #e8453d; }

/* ---------- Floating tip ---------- */
.ap-floating-tip {
  position: absolute;
  background: #f6f8ff; color: #0b1220;
  padding: 6px 10px; border-radius: 6px;
  font-size: 12px; line-height: 1.45;
  pointer-events: none; opacity: 0;
  box-shadow: 0 4px 14px rgba(0,0,0,.4);
  transition: opacity .12s;
  z-index: 9999;
}
.ap-tip { background: #f6f8ff; color: #0b1220; border: none; padding: 4px 8px; font-size: 11px; }
.ap-tip::before { display: none; }

/* ---------- Footer ---------- */
.app footer {
  padding: 6px 18px; border-top: 1px solid var(--line);
  font-size: 11px; color: var(--ink-dim);
  display: flex; justify-content: space-between; align-items: center;
}
.app footer a { color: var(--accent); text-decoration: none; }

/* ---------- Live OpenSky status ---------- */
.live-status {
  position: absolute; bottom: 10px; left: 10px;
  background: rgba(11, 18, 32, 0.85);
  padding: 6px 10px; border-radius: 6px;
  font-size: 11px; color: var(--ink-dim);
  border: 1px solid var(--line);
  z-index: 1000;
}

/* Scrollbars */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #324569; }

/* MapLibre dark-theme tweaks */
.maplibregl-ctrl-group {
  background: var(--panel) !important;
  border: 1px solid var(--line) !important;
}
.maplibregl-ctrl-group button {
  background: transparent !important;
  border-bottom: 1px solid var(--line) !important;
}
.maplibregl-ctrl-group button:last-child { border-bottom: 0 !important; }
.maplibregl-ctrl-group button span {
  filter: invert(1) brightness(1.2);
}
.maplibregl-ctrl-attrib {
  background: rgba(11, 18, 32, 0.8) !important;
  color: var(--ink-dim) !important;
  font-size: 10px !important;
}
.maplibregl-ctrl-attrib a { color: var(--accent) !important; }

/* MapLibre popups (used for traj / airport hover) */
.ap-ml-popup .maplibregl-popup-content {
  background: #f6f8ff;
  color: #0b1220;
  font-size: 12px;
  line-height: 1.45;
  padding: 6px 10px;
  border-radius: 6px;
  box-shadow: 0 4px 14px rgba(0,0,0,.4);
}
.ap-ml-popup .maplibregl-popup-tip { display: none; }

/* Topbar control widgets */
.controls .score-readout {
  font-family: ui-monospace, monospace;
  width: 30px;
  display: inline-block;
}
.controls input[type="range"] { width: 90px; }
.controls select { min-width: 110px; }
footer .footer-right a { margin-right: 6px; }
