/* ---------------------------------------------------------------------------
   SpillSense design tokens
   Palette follows a validated categorical/sequential system: series hues are
   assigned in fixed order and re-stepped (not auto-inverted) for dark mode.
--------------------------------------------------------------------------- */
:root {
  color-scheme: light;

  --page: #f4f5f2;
  --surface: #fcfcfb;
  --surface-2: #f0f1ed;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --ink-3: #898781;
  --hairline: #e1e0d9;
  --border: rgba(11, 11, 11, 0.10);

  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --accent-soft: rgba(42, 120, 214, 0.10);

  --s1: #2a78d6; /* blue    */
  --s2: #eb6834; /* orange  */
  --s3: #1baf7a; /* aqua    */
  --s4: #eda100; /* yellow  */
  --s5: #e87ba4; /* magenta */
  --s6: #008300; /* green   */
  --s7: #4a3aa7; /* violet  */
  --s8: #e34948; /* red     */
  --s-muted: #898781;

  --status-good: #0ca30c;
  --status-warning: #b97e00;
  --status-serious: #c05a2e;
  --status-critical: #d03b3b;

  --shadow-1: 0 1px 2px rgba(11, 11, 11, 0.05), 0 1px 6px rgba(11, 11, 11, 0.04);
  --shadow-2: 0 8px 30px rgba(11, 11, 11, 0.14);
  --radius: 14px;
  --radius-s: 8px;

  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, "Cascadia Code", Consolas, monospace;
}

:root[data-theme="dark"] {
  color-scheme: dark;

  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232322;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --ink-3: #898781;
  --hairline: #2c2c2a;
  --border: rgba(255, 255, 255, 0.10);

  --accent: #3987e5;
  --accent-soft: rgba(57, 135, 229, 0.16);

  --s1: #3987e5;
  --s2: #d95926;
  --s3: #199e70;
  --s4: #c98500;
  --s5: #d55181;
  --s6: #008300;
  --s7: #9085e9;
  --s8: #e66767;

  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-serious: #ec835a;
  --status-critical: #e05656;

  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-2: 0 10px 34px rgba(0, 0, 0, 0.55);
}

/* --------------------------------- base --------------------------------- */
* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  background:
    radial-gradient(1100px 380px at 18% -120px, var(--accent-soft), transparent 65%),
    var(--page);
  color: var(--ink);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h2 { font-size: 15px; }
a { color: inherit; }
button { font: inherit; color: inherit; }

.skip-link {
  position: absolute; left: -999px; top: 8px; z-index: 200;
  background: var(--surface); padding: 8px 14px; border-radius: 8px;
}
.skip-link:focus { left: 8px; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  html { scroll-behavior: auto; }
}

/* -------------------------------- topbar -------------------------------- */
.topbar {
  position: sticky; top: 0; z-index: 60;
  backdrop-filter: blur(14px);
  background: color-mix(in srgb, var(--page) 78%, transparent);
  border-bottom: 1px solid var(--hairline);
}
.topbar-inner {
  max-width: 1440px; margin: 0 auto; padding: 10px 24px;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { width: 34px; height: 34px; fill: var(--accent); }
.brand-mark .brand-wave { stroke: var(--surface); }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-name { font-weight: 700; font-size: 17px; letter-spacing: -0.02em; }
.brand-sub { font-size: 12px; color: var(--ink-2); }
.topbar-nav { display: flex; align-items: center; gap: 4px; }
.nav-link {
  text-decoration: none; font-size: 13.5px; color: var(--ink-2);
  padding: 7px 11px; border-radius: 8px; transition: background 0.15s, color 0.15s;
}
.nav-link:hover { background: var(--surface-2); color: var(--ink); }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 34px; height: 34px; border: 1px solid var(--hairline); border-radius: 9px;
  background: var(--surface); cursor: pointer; transition: background 0.15s;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 17px; height: 17px; fill: none; stroke: var(--ink-2); stroke-width: 1.7; }
.icon-moon { display: none; }
:root[data-theme="dark"] .icon-sun { display: none; }
:root[data-theme="dark"] .icon-moon { display: block; }

/* --------------------------------- layout -------------------------------- */
.page { max-width: 1440px; margin: 0 auto; padding: 22px 24px 60px; display: grid; gap: 18px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-1);
}
.card-head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 12px;
  padding: 14px 18px 10px;
}
.card-meta { font-size: 12.5px; color: var(--ink-3); }
.card-head-actions { display: flex; align-items: center; gap: 12px; }
a.btn { display: inline-flex; align-items: center; text-decoration: none; }
.btn-file { display: inline-flex; align-items: center; cursor: pointer; }
.btn-file.busy { opacity: 0.55; pointer-events: none; }

/* -------------------------------- filters -------------------------------- */
.filters { padding: 14px 18px 12px; }
.filter-grid {
  display: grid; gap: 10px 12px;
  grid-template-columns: minmax(210px, 1.6fr) repeat(6, minmax(120px, 1fr)) repeat(2, minmax(130px, 0.9fr)) auto;
  align-items: end;
}
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field-label { font-size: 11px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--ink-3); }
.field input, .field select {
  height: 34px; padding: 0 10px; min-width: 0;
  border: 1px solid var(--hairline); border-radius: var(--radius-s);
  background: var(--surface); color: var(--ink); font: inherit; font-size: 13.5px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.field input:hover, .field select:hover { border-color: var(--ink-3); }
.field input:focus, .field select:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.field-actions { justify-content: end; }

.btn {
  height: 34px; padding: 0 14px; border-radius: var(--radius-s);
  border: 1px solid var(--hairline); background: var(--surface);
  cursor: pointer; font-size: 13.5px; transition: background 0.15s;
}
.btn:hover { background: var(--surface-2); }
.btn-ghost { border-color: transparent; color: var(--ink-2); }
.btn-ghost:hover { color: var(--ink); background: var(--surface-2); }
.btn:disabled { opacity: 0.45; cursor: default; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chips:empty { margin-top: 0; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12.5px; padding: 3px 6px 3px 10px; border-radius: 999px;
  background: var(--accent-soft); color: var(--ink);
  border: 1px solid transparent; cursor: pointer;
}
.chip b { font-weight: 600; }
.chip .x { font-size: 13px; line-height: 1; color: var(--ink-2); padding: 2px 4px; border-radius: 50%; }
.chip:hover .x { color: var(--ink); }

/* ---------------------------------- KPIs --------------------------------- */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 18px; }
.kpi { padding: 16px 18px 14px; display: flex; flex-direction: column; gap: 2px; position: relative; overflow: hidden; }
.kpi::before {
  content: ""; position: absolute; inset: 0 auto 0 0; width: 3px;
  background: linear-gradient(180deg, var(--accent), transparent);
  opacity: 0.85;
}
.kpi-label { font-size: 11.5px; font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase; color: var(--ink-3); }
.kpi-value { font-size: 30px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.15; }
.kpi-note { font-size: 12px; color: var(--ink-3); }

/* ------------------------------- viz layout ------------------------------ */
.viz-grid { display: grid; grid-template-columns: minmax(0, 1.9fr) minmax(320px, 1fr); gap: 18px; }
.charts-col { display: grid; gap: 18px; align-content: start; }

.map-card { display: flex; flex-direction: column; overflow: hidden; position: relative; }
#map { flex: 1; min-height: 660px; background: var(--surface-2); }
.leaflet-container { font: inherit; }
.leaflet-container a.leaflet-popup-close-button { color: var(--ink-3); }
.leaflet-popup-content-wrapper, .leaflet-popup-tip {
  background: var(--surface); color: var(--ink);
  box-shadow: var(--shadow-2); border-radius: 10px;
}
.leaflet-popup-content { margin: 12px 14px; font: 13px/1.45 var(--font); }
.leaflet-bar a { background: var(--surface); color: var(--ink); border-bottom-color: var(--hairline); }
.leaflet-bar a:hover { background: var(--surface-2); }

.popup-title { font-weight: 650; margin-bottom: 2px; }
.popup-meta { color: var(--ink-2); font-size: 12px; margin-bottom: 6px; }
.popup-link { color: var(--accent); cursor: pointer; text-decoration: underline; font-size: 12.5px; }

/* Rendered as a Leaflet control (bottom-left), so it lives in the map's own
   control layer above tile/marker panes and anchors correctly at any size. */
.leaflet-control.map-legend {
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow-1);
  padding: 10px 12px; display: grid; gap: 5px;
  font: 12px/1.4 var(--font); color: var(--ink-2);
  backdrop-filter: blur(6px);
  max-width: 200px; margin: 0 0 12px 12px;
}
.leaflet-control.map-legend:empty { display: none; }
.legend-row { display: flex; align-items: center; gap: 7px; }
.legend-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; box-shadow: inset 0 0 0 1.5px rgba(0,0,0,0.12); }

.marker-cluster-small, .marker-cluster-medium, .marker-cluster-large { background: color-mix(in srgb, var(--accent) 25%, transparent); }
.marker-cluster-small div, .marker-cluster-medium div, .marker-cluster-large div {
  background: var(--accent); color: var(--accent-ink); font: 600 12px/28px var(--font);
}

/* --------------------------------- charts -------------------------------- */
.chart-card { padding-bottom: 12px; }
.chart-box { position: relative; height: 200px; padding: 4px 14px 0; }
.chart-box-donut { height: 220px; display: grid; place-items: center; }
/* Positioned on the ring's actual center by the donutCenterLabel plugin;
   the 50%/50% here is only the pre-first-draw fallback. */
.donut-center {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  display: grid; justify-items: center; text-align: center;
  line-height: 1.15; pointer-events: none;
}
.donut-center span:first-child { font-size: 26px; font-weight: 700; letter-spacing: -0.02em; }
.donut-caption { font-size: 11.5px; color: var(--ink-3); }

/* --------------------------------- tables -------------------------------- */
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: left; padding: 9px 14px; border-top: 1px solid var(--hairline); white-space: nowrap; }
th {
  font-size: 11px; font-weight: 650; letter-spacing: 0.05em; text-transform: uppercase;
  color: var(--ink-3); border-top: none;
}
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr { cursor: pointer; transition: background 0.12s; }
tbody tr:hover { background: var(--surface-2); }
tbody tr.no-click { cursor: default; }
.th-sort {
  background: none; border: none; padding: 0; cursor: pointer;
  font: inherit; letter-spacing: inherit; text-transform: inherit; color: inherit;
}
.th-sort:hover { color: var(--ink); }
.sort-ind { font-size: 10px; }

.cell-sub { display: block; font-size: 11.5px; color: var(--ink-3); }
.qty-cell { display: inline-flex; align-items: center; gap: 8px; justify-content: flex-end; }
.qty-bar { width: 54px; height: 4px; border-radius: 2px; background: var(--surface-2); overflow: hidden; }
.qty-bar i { display: block; height: 100%; background: var(--s1); border-radius: 2px; }

.table-foot {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; border-top: 1px solid var(--hairline);
}
.pager { display: flex; gap: 6px; }

/* ---------------------------------- pills -------------------------------- */
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--ink-2);
}
.pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pill-Reported { color: var(--status-warning); background: color-mix(in srgb, var(--status-warning) 12%, transparent); }
.pill-UnderInvestigation { color: var(--status-serious); background: color-mix(in srgb, var(--status-serious) 12%, transparent); }
.pill-CleanupInProgress { color: var(--accent); background: var(--accent-soft); }
.pill-Closed { color: var(--status-good); background: color-mix(in srgb, var(--status-good) 12%, transparent); }
.pill-ReferredToOtherAgency { color: var(--ink-2); }
.pill-Failed { color: var(--status-critical); background: color-mix(in srgb, var(--status-critical) 12%, transparent); }
.pill-Succeeded { color: var(--status-good); background: color-mix(in srgb, var(--status-good) 12%, transparent); }
.pill-CompletedWithRejects { color: var(--status-warning); background: color-mix(in srgb, var(--status-warning) 12%, transparent); }

/* --------------------------------- intake -------------------------------- */
.quarantine { padding: 6px 18px 18px; }
.quarantine h3 { font-size: 13.5px; margin: 10px 0; color: var(--ink-2); }
.q-row {
  border: 1px solid var(--hairline); border-radius: 10px;
  padding: 10px 14px; margin-bottom: 8px; background: var(--surface-2);
}
.q-row .raw {
  font: 12px/1.5 var(--mono); color: var(--ink-2);
  overflow-x: auto; white-space: pre; padding: 6px 0 2px;
}
.q-reasons { margin: 4px 0 0; padding-left: 18px; font-size: 13px; color: var(--status-critical); }
.q-reasons li { margin: 1px 0; }

/* --------------------------------- drawer -------------------------------- */
.drawer-scrim {
  position: fixed; inset: 0; z-index: 90;
  background: rgba(8, 8, 8, 0.35); backdrop-filter: blur(2px);
  opacity: 0; transition: opacity 0.2s;
}
.drawer-scrim.open { opacity: 1; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 100;
  width: min(480px, 94vw);
  background: var(--surface); border-left: 1px solid var(--border);
  box-shadow: var(--shadow-2);
  transform: translateX(102%); transition: transform 0.25s cubic-bezier(0.2, 0.8, 0.25, 1);
  display: flex; flex-direction: column;
}
.drawer.open { transform: translateX(0); }
.drawer-head {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 10px;
  padding: 18px 20px 12px; border-bottom: 1px solid var(--hairline);
}
.drawer-head h2 { font-size: 18px; margin: 6px 0 2px; }
.drawer-body { padding: 14px 20px 28px; overflow-y: auto; }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; margin-top: 8px; }
.detail { display: flex; flex-direction: column; gap: 1px; }
.detail.wide { grid-column: 1 / -1; }
.detail dt { font-size: 11px; font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase; color: var(--ink-3); }
.detail dd { margin: 0; font-size: 14px; }
.detail dd.mono { font-family: var(--mono); font-size: 13px; }
.drawer-desc {
  margin: 12px 0 4px; padding: 12px 14px; border-radius: 10px;
  background: var(--surface-2); font-size: 14px;
}

/* -------------------------------- states --------------------------------- */
.skeleton { position: relative; overflow: hidden; background: var(--surface-2); border-radius: 6px; color: transparent !important; }
.skeleton::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--ink) 6%, transparent), transparent);
  animation: shimmer 1.4s infinite;
}
@keyframes shimmer { from { transform: translateX(-100%); } to { transform: translateX(100%); } }

.empty { padding: 26px 18px; text-align: center; color: var(--ink-3); font-size: 13.5px; }

.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%);
  z-index: 120; padding: 10px 18px; border-radius: 10px;
  background: var(--ink); color: var(--page); font-size: 13.5px;
  box-shadow: var(--shadow-2);
}

.footer { padding: 8px 4px 0; color: var(--ink-3); font-size: 12.5px; }

/* ------------------------------- responsive ------------------------------ */
@media (max-width: 1180px) {
  .filter-grid { grid-template-columns: repeat(4, 1fr); }
  .viz-grid { grid-template-columns: 1fr; }
  #map { min-height: 440px; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 640px) {
  .page { padding: 14px 12px 40px; }
  .filter-grid { grid-template-columns: repeat(2, 1fr); }
  .kpis { grid-template-columns: 1fr 1fr; gap: 12px; }
  .kpi-value { font-size: 24px; }
  .brand-sub { display: none; }
}
