/* ════════════════════════════════════════════════════════════════════════════
   bi.css — VendorIQ BI kit: SVG/HTML charts + drill-down layers (no canvas).

   Look-and-feel lives ONLY in the --bi-* tokens below; public/js/bi/bi-kit.js
   never sets colours, sizes or timings — it only flips data-* attributes and
   the per-element --bi-pct / --bi-i custom properties.

   Tokens fall back to the EXCO theme (public/css/exco.css: --surface, --text,
   --blue …) so the kit follows [data-theme="dark"|"light"] automatically.
   ════════════════════════════════════════════════════════════════════════════ */

/* Animatable percentage — lets bars/arcs tween when the value changes. */
@property --bi-pct {
  syntax: '<number>';
  inherits: true;
  initial-value: 0;
}

/* Declared on body too: several pages (Finance, Pay Office, standalone portals) set their
   theme tokens on body, and a var() on :root alone would keep the light values in dark mode. */
:root, body {
  --bi-surface:     var(--surface, #161B22);
  --bi-surface-2:   var(--surface2, #21262D);
  --bi-border:      var(--border, #30363D);
  --bi-text:        var(--text, #C9D1D9);
  --bi-muted:       var(--muted, #8B949E);
  --bi-track:       var(--surface2, #21262D);
  --bi-radius:      10px;
  --bi-gap:         12px;

  /* Series palette — one hue per pillar, then categorical extras. */
  --bi-ops:         var(--blue, #58A6FF);     /* Operational efficiency */
  --bi-fin:         var(--green, #3FB950);    /* Financial contribution */
  --bi-tgt:         var(--purple, #A371F7);   /* Targets */
  --bi-c1: var(--blue, #58A6FF);  --bi-c2: var(--green, #3FB950); --bi-c3: var(--amber, #FB923C);
  --bi-c4: var(--purple, #A371F7); --bi-c5: var(--cyan, #39C5CF); --bi-c6: var(--red, #F85149);
  --bi-c7: #E3B341; --bi-c8: #DB61A2;

  /* Status (target attainment) */
  --bi-good:        var(--green, #3FB950);
  --bi-warn:        var(--amber, #FB923C);
  --bi-bad:         var(--red, #F85149);

  --bi-ease:        cubic-bezier(.2, .7, .2, 1);
  --bi-dur:         420ms;
  --bi-dur-layer:   320ms;
}

@media (prefers-reduced-motion: reduce) {
  :root { --bi-dur: 1ms; --bi-dur-layer: 1ms; }
}

/* ── Screen-reader layer ──────────────────────────────────────────────────── */
/* The wrapper is what gets clipped: a <table> ignores width:1px and would widen the page. */
.bi-sr-wrap, .bi-sr {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0;
}
.bi-sr-wrap .bi-sr { position: static !important; }

/* ── Card shell ───────────────────────────────────────────────────────────── */
.bi-card {
  background: var(--bi-surface);
  border: 1px solid var(--bi-border);
  border-radius: var(--bi-radius);
  padding: 16px 18px;
  box-shadow: var(--card-shadow, none);
  min-width: 0;
}
.bi-card-hd { display: flex; align-items: baseline; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.bi-card-title { font-size: 13px; font-weight: 700; color: var(--bi-text); letter-spacing: .01em; }
.bi-card-sub { font-size: 11.5px; color: var(--bi-muted); }
.bi-card-hd .bi-crumbs { margin-left: auto; }

.bi-pillar-ops { border-top: 3px solid var(--bi-ops); }
.bi-pillar-fin { border-top: 3px solid var(--bi-fin); }
.bi-pillar-tgt { border-top: 3px solid var(--bi-tgt); }

.bi-grid { display: grid; gap: var(--bi-gap); grid-template-columns: repeat(auto-fit, minmax(min(100%, 340px), 1fr)); }
.bi-grid-kpi { display: grid; gap: var(--bi-gap); grid-template-columns: repeat(auto-fit, minmax(min(100%, 190px), 1fr)); }

/* ── Drill-down: breadcrumbs + stacked layers ─────────────────────────────── */
.bi-crumbs { display: flex; gap: 4px; align-items: center; font-size: 11.5px; flex-wrap: wrap; }
.bi-crumb {
  background: none; border: 0; padding: 2px 6px; border-radius: 6px; cursor: pointer;
  color: var(--bi-muted); font: inherit;
}
.bi-crumb:hover, .bi-crumb:focus-visible { color: var(--bi-text); background: var(--bi-surface-2); outline: none; }
.bi-crumb[aria-current="page"] { color: var(--bi-text); font-weight: 700; cursor: default; background: none; }
.bi-crumb-sep { color: var(--bi-muted); opacity: .6; }

/* All layers share one grid cell; only the active one is visible/interactive.
   Switching depth flips data-drill-level on the wrapper — layers are kept, never wiped. */
.bi-drill { display: grid; }
.bi-layer {
  grid-area: 1 / 1;
  opacity: 0;
  transform: translate3d(24px, 0, 0);
  transition: opacity var(--bi-dur-layer) var(--bi-ease), transform var(--bi-dur-layer) var(--bi-ease), visibility 0s linear var(--bi-dur-layer);
  visibility: hidden;
  pointer-events: none;
  min-width: 0;
}
.bi-layer[data-state="above"] { transform: translate3d(-24px, 0, 0); }
.bi-layer[data-state="active"] {
  opacity: 1; transform: none; visibility: visible; pointer-events: auto;
  transition: opacity var(--bi-dur-layer) var(--bi-ease), transform var(--bi-dur-layer) var(--bi-ease), visibility 0s;
}

.bi-empty { padding: 22px 8px; text-align: center; color: var(--bi-muted); font-size: 12.5px; }

/* ── a) Nested bar rows (ranking / group comparison) ──────────────────────── */
.bi-rows { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.bi-row {
  --bi-color: var(--bi-c1);
  display: grid; grid-template-columns: minmax(90px, 34%) 1fr auto; gap: 10px; align-items: center;
  padding: 5px 6px; border-radius: 8px; cursor: default;
  animation: bi-row-in var(--bi-dur) var(--bi-ease) both;
  animation-delay: calc(var(--bi-i, 0) * 28ms);
}
.bi-row[data-drillable="true"] { cursor: pointer; }
.bi-row[data-drillable="true"]:hover, .bi-row:focus-visible { background: var(--bi-surface-2); outline: none; }
.bi-row:focus-visible { box-shadow: 0 0 0 2px var(--bi-ops); }
.bi-row-label { font-size: 13px; color: var(--bi-text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bi-row-label small { display: block; color: var(--bi-muted); font-size: 11.5px; }
.bi-row-track { position: relative; height: 12px; background: var(--bi-track); border-radius: 6px; overflow: hidden; }
.bi-row-bar {
  position: absolute; inset: 0; border-radius: 6px; background: var(--bi-color);
  transform-origin: left center;
  transform: scaleX(calc(var(--bi-pct) / 100));
  /* Only transform animates (compositor) — --bi-pct just feeds the target scale. */
  transition: transform var(--bi-dur) var(--bi-ease);
}
/* Optional secondary measure (e.g. target) as a marker line on the track. */
.bi-row-mark {
  position: absolute; top: -2px; bottom: -2px; width: 2px; background: var(--bi-text); opacity: .8;
  left: calc(var(--bi-mark, 0) * 1%);
  transition: left var(--bi-dur) var(--bi-ease);
}
.bi-row-val { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 13px; color: var(--bi-text); text-align: right; white-space: nowrap; }
.bi-row-val small { display: block; font-weight: 400; color: var(--bi-muted); font-size: 11.5px; }
.bi-row-chev { color: var(--bi-muted); margin-left: 4px; }
.bi-row[data-status="good"] { --bi-color: var(--bi-good); }
.bi-row[data-status="warn"] { --bi-color: var(--bi-warn); }
.bi-row[data-status="bad"]  { --bi-color: var(--bi-bad); }

@keyframes bi-row-in { from { opacity: 0; transform: translate3d(0, 6px, 0); } to { opacity: 1; transform: none; } }

/* ── b) Micro-KPI with trend spark ────────────────────────────────────────── */
.bi-kpi {
  --bi-color: var(--bi-ops);
  position: relative; background: var(--bi-surface); border: 1px solid var(--bi-border);
  border-radius: var(--bi-radius); padding: 13px 14px 10px; min-width: 0;
  display: grid; grid-template-rows: auto auto auto 34px; gap: 2px;
}
.bi-kpi[data-drillable="true"] { cursor: pointer; }
.bi-kpi[data-drillable="true"]:hover, .bi-kpi:focus-visible { border-color: var(--bi-color); outline: none; }
.bi-kpi::before { content: ''; position: absolute; inset: 0 auto 0 0; width: 3px; border-radius: var(--bi-radius) 0 0 var(--bi-radius); background: var(--bi-color); }
.bi-kpi-lbl { font-size: 11.5px; font-weight: 600; color: var(--bi-muted); text-transform: uppercase; letter-spacing: .05em; }
.bi-kpi-val { font-variant-numeric: tabular-nums; font-weight: 700; font-size: clamp(18px, 2.2vw, 24px); line-height: 1.15; color: var(--bi-text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bi-kpi-meta { display: flex; gap: 8px; align-items: center; font-size: 11.5px; color: var(--bi-muted); min-height: 16px; }
.bi-delta { font-weight: 700; }
.bi-delta[data-dir="up"]   { color: var(--bi-good); }
.bi-delta[data-dir="down"] { color: var(--bi-bad); }
.bi-delta[data-dir="flat"] { color: var(--bi-muted); }
.bi-delta[data-polarity="inverse"][data-dir="up"]   { color: var(--bi-bad); }
.bi-delta[data-polarity="inverse"][data-dir="down"] { color: var(--bi-good); }
.bi-kpi-target { position: relative; height: 4px; border-radius: 2px; background: var(--bi-track); overflow: hidden; margin-top: 4px; }
.bi-kpi-target > i {
  position: absolute; inset: 0; background: var(--bi-color); transform-origin: left;
  transform: scaleX(calc(min(var(--bi-pct), 100) / 100));
  transition: transform var(--bi-dur) var(--bi-ease);
}
.bi-spark { width: 100%; height: 34px; display: block; overflow: visible; }
.bi-spark-line { fill: none; stroke: var(--bi-color); stroke-width: 1.75; stroke-linejoin: round; stroke-linecap: round; vector-effect: non-scaling-stroke; }
.bi-spark-area { fill: var(--bi-color); opacity: .14; }
.bi-spark-dot { fill: var(--bi-color); }

/* ── c) Segmented donut ───────────────────────────────────────────────────── */
.bi-donut { display: grid; grid-template-columns: minmax(110px, 150px) minmax(0, 1fr); gap: 14px; align-items: center; max-width: 620px; }
@media (max-width: 520px) { .bi-donut { grid-template-columns: 1fr; justify-items: center; } }
.bi-donut svg { width: 100%; max-width: 170px; aspect-ratio: 1; overflow: visible; }
.bi-donut-seg {
  fill: none; stroke-width: 15; cursor: pointer;
  transition: stroke-width var(--bi-dur) var(--bi-ease), opacity var(--bi-dur) var(--bi-ease), stroke-dasharray var(--bi-dur) var(--bi-ease), stroke-dashoffset var(--bi-dur) var(--bi-ease);
}
.bi-donut-seg:hover, .bi-donut-seg:focus-visible, .bi-donut-seg[aria-pressed="true"] { stroke-width: 19; outline: none; }
.bi-donut[data-focus] .bi-donut-seg:not([aria-pressed="true"]) { opacity: .35; }
.bi-donut-total { font-weight: 700; font-size: 12px; font-variant-numeric: tabular-nums; fill: var(--bi-text); pointer-events: none; }
.bi-donut-cap { font-size: 8.5px; fill: var(--bi-muted); text-transform: uppercase; letter-spacing: .06em; pointer-events: none; }
.bi-legend { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; min-width: 0; }
.bi-legend li {
  display: grid; grid-template-columns: 10px minmax(0, 1fr) auto auto; gap: 8px; align-items: center;
  font-size: 12px; color: var(--bi-text); padding: 3px 6px; border-radius: 6px; cursor: pointer;
}
.bi-legend li:hover, .bi-legend li:focus-visible, .bi-legend li[aria-pressed="true"] { background: var(--bi-surface-2); outline: none; }
.bi-legend-sw { width: 10px; height: 10px; border-radius: 3px; }
.bi-legend-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bi-legend-val { font-weight: 600; font-size: 12px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.bi-legend-pct { font-size: 11.5px; color: var(--bi-muted); min-width: 36px; text-align: right; white-space: nowrap; }

/* ── Column trend (monthly actual vs target) ──────────────────────────────── */
.bi-cols { width: 100%; height: auto; display: block; overflow: visible; }
.bi-col { fill: var(--bi-color, var(--bi-tgt)); opacity: .85; transform-box: fill-box; transform-origin: bottom; transition: transform var(--bi-dur) var(--bi-ease), opacity .15s; cursor: pointer; }
.bi-col:hover, .bi-col:focus-visible { opacity: 1; outline: none; }
.bi-col[data-status="good"] { fill: var(--bi-good); }
.bi-col[data-status="warn"] { fill: var(--bi-warn); }
.bi-col[data-status="bad"]  { fill: var(--bi-bad); }
.bi-col-target { fill: none; stroke: var(--bi-text); stroke-width: 1.5; stroke-dasharray: 4 3; vector-effect: non-scaling-stroke; }
.bi-axis { font-size: 11.5px; fill: var(--bi-muted); }
.bi-gridline { stroke: var(--chart-grid, var(--bi-border)); stroke-width: 1; vector-effect: non-scaling-stroke; }

/* Tooltip (single floating element, positioned with transform only). */
.bi-tip {
  position: fixed; top: 0; left: 0; z-index: 9999; pointer-events: none;
  background: var(--tip-bg, var(--bi-surface)); border: 1px solid var(--tip-border, var(--bi-border));
  color: var(--tip-title, var(--bi-text)); font-size: 12px; padding: 7px 10px; border-radius: 8px;
  box-shadow: 0 8px 24px var(--shadow, rgba(0,0,0,.3)); opacity: 0; transition: opacity .12s;
  max-width: 260px;
}
.bi-tip[data-show="true"] { opacity: 1; }
.bi-tip b { display: block; margin-bottom: 2px; }
.bi-tip span { color: var(--tip-body, var(--bi-muted)); }

/* Loading shimmer while a layer's data is in flight. */
.bi-loading { position: relative; min-height: 120px; }
.bi-loading::after {
  content: ''; position: absolute; inset: 0; border-radius: 8px;
  background: linear-gradient(90deg, transparent, var(--bi-surface-2), transparent);
  background-size: 200% 100%; animation: bi-shimmer 1.2s linear infinite; opacity: .6;
}
@keyframes bi-shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

/* Inline legend for multi-series charts */
.bi-legend-inline { display: flex; flex-wrap: wrap; gap: 12px; font-size: 12px; color: var(--bi-muted); margin-bottom: 6px; }
.bi-legend-inline span { display: inline-flex; align-items: center; gap: 6px; }
.bi-legend-inline i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.bi-col-group { cursor: default; }
.bi-col-group[data-drillable="true"] { cursor: pointer; }
.bi-col-group:hover .bi-col, .bi-col-group:focus-visible .bi-col { opacity: 1; }
.bi-col-group:focus-visible { outline: none; }
.bi-col-group:focus-visible rect:first-child { stroke: var(--bi-ops); stroke-width: 1.5; }

/* ── Pillar bands (Performance page): header, 4 KPI tiles, two charts ───── */
.bi-band { margin-bottom: 22px; }
.bi-band-hd { display: flex; align-items: center; gap: 10px; margin: 0 0 10px; flex-wrap: wrap; }
.bi-band-dot { width: 10px; height: 10px; border-radius: 3px; flex-shrink: 0; }
.bi-band-title { font-size: 14px; font-weight: 700; color: var(--bi-text); margin: 0; letter-spacing: .01em; }
.bi-band-sub { font-size: 11.5px; color: var(--bi-muted); }
.bi-kpi-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); margin-bottom: var(--bi-gap); }
@media (max-width: 1100px) { .bi-kpi-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 520px)  { .bi-kpi-4 { grid-template-columns: 1fr; } }
.bi-grid-2 { display: grid; gap: var(--bi-gap); grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: start; }
@media (max-width: 1000px) { .bi-grid-2 { grid-template-columns: 1fr; } }

/* Clickable chart parts: visible keyboard focus, a pointer, and a gentle hover */
.bi-chart [tabindex], .bi-legend-item, .bi-row[tabindex] { cursor: pointer; }
.bi-chart [tabindex]:focus-visible, .bi-legend-item:focus-visible, .bi-row:focus-visible { outline: 2px solid var(--bi-accent, var(--accent, #0d9488)); outline-offset: 2px; border-radius: 4px; }
@media (max-width: 640px) { .bi-axis { font-size: 12px; } .bi-row-label, .bi-row-val { font-size: 13.5px; } }
