:root {
  color-scheme: light;
  --bg: #f4f6f3;
  --bg-glow-a: rgba(18, 128, 92, .10);
  --bg-glow-b: rgba(42, 120, 214, .08);
  --surface: #ffffff;
  --surface-2: #f7f9f6;
  --surface-3: #eef1ed;
  --line: rgba(16, 40, 32, .10);
  --line-strong: rgba(16, 40, 32, .18);
  --text: #0f1f1a;
  --text-2: #4c5b55;
  --text-3: #7b8882;
  --brand: #12805c;
  --brand-ink: #0b6548;
  --brand-soft: rgba(18, 128, 92, .10);
  --shadow: 0 1px 2px rgba(16, 40, 32, .05), 0 8px 24px rgba(16, 40, 32, .06);
  --header: rgba(244, 246, 243, .82);

  /* chart roles */
  --s1: #2a78d6;  /* field / primary series */
  --s2: #eb6834;  /* received */
  --s3: #1baf7a;  /* preprocessed */
  --s4: #4a3aa7;  /* analysed */
  --seq-0: #e6eef9; --seq-1: #b7d3f6; --seq-2: #6da7ec; --seq-3: #2a78d6; --seq-4: #104281;
  --grid: rgba(16, 40, 32, .08);
  --good: #0ca30c; --warning: #c98500; --serious: #d9663a; --critical: #d03b3b;
  --good-soft: rgba(12, 163, 12, .12); --warning-soft: rgba(250, 178, 25, .18);
  --critical-soft: rgba(208, 59, 59, .12); --neutral-soft: rgba(16, 40, 32, .07);
  --track: rgba(16, 40, 32, .09);
  --radius: 16px;
  --font: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0b100e; --bg-glow-a: rgba(27, 175, 122, .13); --bg-glow-b: rgba(57, 135, 229, .10);
    --surface: #131a17; --surface-2: #18211d; --surface-3: #1f2a25;
    --line: rgba(220, 240, 230, .09); --line-strong: rgba(220, 240, 230, .17);
    --text: #f1f6f3; --text-2: #b3c0ba; --text-3: #7f8d87;
    --brand: #2bc48c; --brand-ink: #5fdcae; --brand-soft: rgba(43, 196, 140, .13);
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 30px rgba(0, 0, 0, .28);
    --header: rgba(11, 16, 14, .78);
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #9085e9;
    --seq-0: #16263b; --seq-1: #184f95; --seq-2: #256abf; --seq-3: #5598e7; --seq-4: #b7d3f6;
    --grid: rgba(220, 240, 230, .08);
    --warning: #fab219; --serious: #ec835a; --critical: #e66767;
    --good-soft: rgba(12, 163, 12, .18); --warning-soft: rgba(250, 178, 25, .16);
    --critical-soft: rgba(230, 103, 103, .16); --neutral-soft: rgba(220, 240, 230, .08);
    --track: rgba(220, 240, 230, .10);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b100e; --bg-glow-a: rgba(27, 175, 122, .13); --bg-glow-b: rgba(57, 135, 229, .10);
  --surface: #131a17; --surface-2: #18211d; --surface-3: #1f2a25;
  --line: rgba(220, 240, 230, .09); --line-strong: rgba(220, 240, 230, .17);
  --text: #f1f6f3; --text-2: #b3c0ba; --text-3: #7f8d87;
  --brand: #2bc48c; --brand-ink: #5fdcae; --brand-soft: rgba(43, 196, 140, .13);
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 30px rgba(0, 0, 0, .28);
  --header: rgba(11, 16, 14, .78);
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #9085e9;
  --seq-0: #16263b; --seq-1: #184f95; --seq-2: #256abf; --seq-3: #5598e7; --seq-4: #b7d3f6;
  --grid: rgba(220, 240, 230, .08);
  --warning: #fab219; --serious: #ec835a; --critical: #e66767;
  --good-soft: rgba(12, 163, 12, .18); --warning-soft: rgba(250, 178, 25, .16);
  --critical-soft: rgba(230, 103, 103, .16); --neutral-soft: rgba(220, 240, 230, .08);
  --track: rgba(220, 240, 230, .10);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body {
  margin: 0; min-height: 100vh; color: var(--text); font-family: var(--font); font-size: 15px; line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  background:
    radial-gradient(60rem 30rem at 8% -10%, var(--bg-glow-a), transparent 70%),
    radial-gradient(50rem 28rem at 100% 0%, var(--bg-glow-b), transparent 70%),
    var(--bg);
  background-attachment: fixed;
}
a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; cursor: pointer; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; border-radius: 6px; }
[hidden] { display: none !important; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- header ---------- */
.topbar { position: sticky; top: 0; z-index: 1000; background: var(--header); backdrop-filter: saturate(160%) blur(14px); -webkit-backdrop-filter: saturate(160%) blur(14px); border-bottom: 1px solid var(--line); }
.topbar-inner, .filterbar-inner, main, .foot { width: min(1320px, 100%); margin: 0 auto; padding-inline: 20px; }
.topbar-inner { display: flex; align-items: center; gap: 20px; height: 58px; }
.brand { display: flex; align-items: center; gap: 10px; flex: none; }
.brand-mark { width: 32px; height: 32px; display: grid; place-items: center; border-radius: 10px; color: #fff; background: linear-gradient(145deg, #1aa877, #0b6548); box-shadow: 0 4px 12px rgba(18, 128, 92, .35); }
.brand-mark svg { width: 18px; height: 18px; }
.brand-text { font-weight: 750; letter-spacing: -.02em; font-size: 1.02rem; }
.brand-text small { font-weight: 500; color: var(--text-3); font-size: .86rem; margin-left: 4px; }
.tabs { display: flex; gap: 2px; margin-inline: auto; padding: 3px; background: var(--neutral-soft); border-radius: 12px; }
.tabs a { padding: 6px 14px; border-radius: 9px; color: var(--text-2); font-weight: 600; font-size: .9rem; white-space: nowrap; transition: background .15s, color .15s; }
.tabs a:hover { color: var(--text); }
.tabs a.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }
.topbar-actions { display: flex; align-items: center; gap: 10px; flex: none; }
.sync { display: inline-flex; align-items: center; gap: 7px; font-size: .8rem; color: var(--text-2); white-space: nowrap; }
.sync .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-3); }
.sync.ok .dot { background: var(--good); box-shadow: 0 0 0 0 rgba(12, 163, 12, .5); animation: pulse 2.4s infinite; }
.sync.stale .dot { background: var(--warning); }
.sync.bad .dot { background: var(--critical); }
@keyframes pulse { 70% { box-shadow: 0 0 0 7px rgba(12, 163, 12, 0); } 100% { box-shadow: 0 0 0 0 rgba(12, 163, 12, 0); } }
.icon-btn { width: 34px; height: 34px; display: grid; place-items: center; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); }
.icon-btn:hover { border-color: var(--line-strong); }
.icon-btn svg { width: 17px; height: 17px; }
.i-sun { display: none; }
:root[data-theme="dark"] .i-sun { display: block; } :root[data-theme="dark"] .i-moon { display: none; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .i-sun { display: block; } :root:not([data-theme="light"]) .i-moon { display: none; } }

.filterbar { border-top: 1px solid var(--line); }
.filterbar-inner { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding-block: 8px; }
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-block: 2px; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; padding: 5px 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); color: var(--text-2); font-size: .84rem; font-weight: 600; white-space: nowrap; transition: all .15s; }
.chip:hover { border-color: var(--line-strong); color: var(--text); }
.chip.on { background: var(--text); border-color: var(--text); color: var(--bg); }
.segmented { display: flex; flex: none; padding: 3px; gap: 2px; border-radius: 10px; background: var(--neutral-soft); }
.segmented button { border: 0; background: transparent; padding: 4px 10px; border-radius: 7px; font-size: .8rem; font-weight: 600; color: var(--text-2); white-space: nowrap; }
.segmented button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }

/* ---------- layout ---------- */
main { padding-block: 22px 40px; min-height: 70vh; }
.view { display: grid; gap: 16px; animation: rise .35s ease both; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
.hero { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; padding: 4px 2px 2px; }
.hero h1 { margin: 0; font-size: clamp(1.45rem, 2.6vw, 2rem); letter-spacing: -.03em; font-weight: 780; line-height: 1.15; }
.hero p { margin: 6px 0 0; color: var(--text-2); max-width: 70ch; }
.hero .scope { font-size: .82rem; color: var(--text-3); white-space: nowrap; }
.grid { display: grid; gap: 16px; }
.g-kpi { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.g-kpi.four { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.g-2 { grid-template-columns: minmax(0, 1.55fr) minmax(0, 1fr); }
.g-half { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 1100px) { .g-kpi, .g-kpi.four { grid-template-columns: repeat(3, minmax(0, 1fr)); } .g-2, .g-3 { grid-template-columns: minmax(0, 1fr); } }
@media (max-width: 760px) {
  .g-kpi, .g-kpi.four, .g-half { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g-half.stack { grid-template-columns: minmax(0, 1fr); }
  .topbar-inner { height: auto; flex-wrap: wrap; padding-block: 10px; gap: 10px; }
  .tabs { order: 3; width: 100%; margin: 0; overflow-x: auto; scrollbar-width: none; }
  .tabs a { flex: 1; text-align: center; padding-inline: 8px; }
  .topbar-actions { margin-left: auto; }
  .filterbar-inner { flex-direction: column; align-items: stretch; }
  .segmented { align-self: flex-start; }
  .topbar-inner, .filterbar-inner, main, .foot { padding-inline: 14px; }
}
@media (max-width: 420px) { .sync span { display: none; } }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); min-width: 0; }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 16px 18px 0; }
.card-head h2 { margin: 0; font-size: 1rem; font-weight: 700; letter-spacing: -.01em; }
.card-head p { margin: 3px 0 0; color: var(--text-3); font-size: .82rem; }
.card-body { padding: 14px 18px 18px; }
.card-body.flush { padding: 10px 0 0; }
.chart { width: 100%; height: 300px; }
.chart.tall { height: 360px; }
.chart.short { height: 240px; }

/* ---------- KPI ---------- */
.kpi { padding: 16px 18px; position: relative; overflow: hidden; }
.kpi-label { display: flex; align-items: center; gap: 7px; color: var(--text-2); font-size: .8rem; font-weight: 600; }
.kpi-label i { width: 8px; height: 8px; border-radius: 3px; background: var(--c, var(--text-3)); flex: none; }
.kpi-value { font-size: clamp(1.6rem, 2.4vw, 2.15rem); font-weight: 780; letter-spacing: -.035em; line-height: 1.1; margin-top: 8px; font-variant-numeric: tabular-nums; }
.kpi-sub { color: var(--text-3); font-size: .8rem; margin-top: 4px; min-height: 1.2em; }
.kpi-sub b { color: var(--text-2); font-weight: 650; }
.meter { height: 6px; border-radius: 99px; background: var(--track); margin-top: 12px; overflow: hidden; }
.meter > span { display: block; height: 100%; border-radius: 99px; background: var(--c, var(--brand)); width: 0; transition: width .9s cubic-bezier(.2, .8, .2, 1); }
.kpi-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 10px; font-size: .78rem; color: var(--text-3); }
.delta { display: inline-flex; align-items: center; gap: 4px; font-weight: 650; color: var(--text-2); }
.delta.up { color: var(--good); }
.spark { width: 84px; height: 26px; flex: none; }
.spark path { fill: none; stroke: var(--c, var(--text-3)); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.spark .area { fill: var(--c, var(--text-3)); stroke: none; opacity: .12; }

/* ---------- pipeline ---------- */
.pipeline { display: grid; gap: 10px; }
.stage { display: grid; grid-template-columns: 150px minmax(0, 1fr) 96px; align-items: center; gap: 12px; }
.stage-name { font-size: .86rem; font-weight: 600; color: var(--text-2); }
.stage-bar { height: 30px; background: var(--track); border-radius: 8px; overflow: hidden; position: relative; }
.stage-bar > span { display: block; height: 100%; border-radius: 8px; background: var(--c); width: 0; min-width: 3px; transition: width .9s cubic-bezier(.2, .8, .2, 1); }
.stage-val { text-align: right; font-weight: 720; font-variant-numeric: tabular-nums; }
.stage-val small { display: block; font-weight: 500; color: var(--text-3); font-size: .74rem; }
.stage-drop { grid-column: 2 / 4; font-size: .76rem; color: var(--text-3); margin: -4px 0 2px; }
@media (max-width: 560px) { .stage { grid-template-columns: 104px minmax(0, 1fr) 78px; gap: 8px; } }

/* ---------- insights ---------- */
.insights { display: grid; gap: 10px; margin: 0; padding: 0; list-style: none; }
.insights li { display: grid; grid-template-columns: 26px minmax(0, 1fr); gap: 10px; align-items: start; font-size: .88rem; color: var(--text-2); }
.insights b { color: var(--text); font-weight: 650; }
.ico { width: 26px; height: 26px; border-radius: 8px; display: grid; place-items: center; font-size: .8rem; font-weight: 800; }
.ico.good { background: var(--good-soft); color: var(--good); }
.ico.warn { background: var(--warning-soft); color: var(--warning); }
.ico.bad { background: var(--critical-soft); color: var(--critical); }
.ico.info { background: var(--brand-soft); color: var(--brand-ink); }

/* ---------- tables ---------- */
.table-tools { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.search { position: relative; }
.search input { width: 240px; max-width: 100%; height: 34px; padding: 0 12px 0 32px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2); color: var(--text); font: inherit; font-size: .86rem; }
.search input::placeholder { color: var(--text-3); }
.search svg { position: absolute; left: 10px; top: 9px; width: 16px; height: 16px; color: var(--text-3); pointer-events: none; }
.btn { height: 34px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-size: .82rem; font-weight: 600; color: var(--text-2); }
.btn:hover { border-color: var(--line-strong); color: var(--text); }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .86rem; }
th, td { padding: 10px 12px; text-align: left; white-space: nowrap; }
th:first-child, td:first-child { padding-left: 18px; } th:last-child, td:last-child { padding-right: 18px; }
thead th { position: sticky; top: 0; font-size: .74rem; font-weight: 650; letter-spacing: .04em; text-transform: uppercase; color: var(--text-3); border-bottom: 1px solid var(--line); background: var(--surface); user-select: none; }
thead th[data-k] { cursor: pointer; }
thead th[data-k]:hover { color: var(--text); }
thead th.sorted::after { content: " ↓"; } thead th.sorted.asc::after { content: " ↑"; }
tbody tr { border-bottom: 1px solid var(--line); }
tbody tr:last-child { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
td.r, th.r { text-align: right; font-variant-numeric: tabular-nums; }
td .muted, .muted { color: var(--text-3); }
.table-foot { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 18px 14px; color: var(--text-3); font-size: .8rem; }
.cell-bar { display: grid; grid-template-columns: minmax(70px, 1fr) 44px; align-items: center; gap: 8px; min-width: 130px; }
.cell-bar .meter { margin: 0; height: 7px; }
.cell-bar b { font-weight: 650; font-variant-numeric: tabular-nums; text-align: right; font-size: .82rem; }
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 99px; font-size: .76rem; font-weight: 650; background: var(--neutral-soft); color: var(--text-2); }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.warn { background: var(--warning-soft); color: var(--warning); }
.badge.bad { background: var(--critical-soft); color: var(--critical); }
.badge.info { background: var(--brand-soft); color: var(--brand-ink); }
.steps { display: inline-flex; gap: 4px; align-items: center; }
.steps i { width: 22px; height: 7px; border-radius: 4px; background: var(--track); }
.steps i.on { background: var(--c); }

/* ---------- map ---------- */
.map { height: 520px; border-radius: 0 0 var(--radius) var(--radius); overflow: hidden; background: var(--surface-3); z-index: 0; }
@media (max-width: 760px) { .map { height: 400px; } }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: .8rem; color: var(--text-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 50%; background: var(--c); }
.legend i.sq { border-radius: 3px; }
.leaflet-container { font-family: var(--font); background: var(--surface-3); }
.leaflet-tile-pane { filter: saturate(.45) contrast(.95); }
:root[data-theme="dark"] .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) saturate(.35) brightness(.85) contrast(.9); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) saturate(.35) brightness(.85) contrast(.9); } }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.leaflet-popup-content { margin: 12px 14px; font-size: .84rem; line-height: 1.5; }
.leaflet-popup-content b { font-weight: 700; }
.leaflet-control-attribution { background: var(--header) !important; color: var(--text-3) !important; font-size: 10px; }
.leaflet-control-attribution a { color: var(--text-2) !important; }
.leaflet-bar a { background: var(--surface); color: var(--text); border-color: var(--line) !important; }
.leaflet-bar a:hover { background: var(--surface-2); color: var(--text); }

/* ---------- misc ---------- */
.loading { display: grid; place-items: center; gap: 14px; padding: 18vh 0; color: var(--text-2); text-align: center; }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--track); border-top-color: var(--brand); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.empty { padding: 28px 18px; text-align: center; color: var(--text-3); font-size: .9rem; }
.note { font-size: .8rem; color: var(--text-3); margin: 10px 0 0; }
.foot { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding-block: 18px 30px; color: var(--text-3); font-size: .78rem; border-top: 1px solid var(--line); }
.tooltip { position: fixed; z-index: 2000; pointer-events: none; background: var(--text); color: var(--bg); padding: 6px 9px; border-radius: 8px; font-size: .78rem; max-width: 260px; box-shadow: var(--shadow); }
.heat { display: grid; gap: 2px; font-size: .78rem; overflow-x: auto; }
.heat .hrow { display: grid; gap: 2px; align-items: center; }
.heat .hlabel { color: var(--text-2); font-weight: 600; padding-right: 8px; white-space: nowrap; }
.heat .hhead { color: var(--text-3); font-size: .7rem; text-align: center; writing-mode: vertical-rl; transform: rotate(180deg); justify-self: center; padding-bottom: 6px; height: 96px; white-space: nowrap; }
.heat .hcell { height: 30px; border-radius: 5px; display: grid; place-items: center; font-variant-numeric: tabular-nums; font-size: .72rem; color: var(--text); background: var(--track); min-width: 34px; }
.heat .hcell.zero { color: transparent; }
.heat .hcell.hi { color: #fff; }
