/* ==========================================================================
   EMCharts — design tokens + component styles (offline, no dependencies)
   --------------------------------------------------------------------------
   Theming: light values on :root; dark values under [data-theme="dark"] and,
   when the host has not stamped data-theme, under prefers-color-scheme: dark.
   Charts reference these tokens by role (var(--emc-*)), so a theme switch
   repaints them without re-rendering (heatmap/export read computed values and
   re-render automatically on theme change).

   Categorical palette (slots 1-8 = validated reference palette; 9-10 added and
   validated with the same six checks, light + dark, adjacent pairlist):
     order is the colour-vision-deficiency safety mechanism — do not re-order.
     Scatter / all-pairs charts: use at most 3 series.
   Energy media (validated as an adjacent set in this order):
     electricity, water, gas, thermal, solar, steam, diesel
   ========================================================================== */

:root {
  color-scheme: light;
  --emc-font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  /* surfaces & ink */
  --emc-page: #f9f9f7;
  --emc-surface: #fcfcfb;
  --emc-surface-2: #f3f2ee;
  --emc-ink: #0b0b0b;
  --emc-ink-2: #52514e;
  --emc-muted: #898781;
  --emc-axis-text: #6f6d67;
  --emc-grid: #e7e6e0;
  --emc-axis: #c3c2b7;
  --emc-border: rgba(11, 11, 11, 0.10);
  --emc-crosshair: #9f9d96;
  --emc-band: rgba(11, 11, 11, 0.045);
  --emc-brush: rgba(42, 120, 214, 0.12);
  --emc-null: #ecebe6;
  --emc-track: #ebeae5;
  --emc-other: #a9a79f;
  --emc-target: #52514e;
  --emc-total: #6f6d66;
  --emc-focus: #2a78d6;
  --emc-tip-bg: #ffffff;
  --emc-tip-shadow: 0 6px 24px rgba(11, 11, 11, 0.12), 0 1px 3px rgba(11, 11, 11, 0.08);

  /* categorical series */
  --emc-s1: #2a78d6;   /* blue */
  --emc-s2: #eb6834;   /* orange */
  --emc-s3: #1baf7a;   /* aqua */
  --emc-s4: #eda100;   /* yellow */
  --emc-s5: #e87ba4;   /* magenta */
  --emc-s6: #008300;   /* green */
  --emc-s7: #4a3aa7;   /* violet */
  --emc-s8: #e34948;   /* red */
  --emc-s9: #0095a0;   /* teal */
  --emc-s10: #9b5a14;  /* brown */

  /* sequential (magnitude; near-zero recedes toward the surface) */
  --emc-seq-1: #e3eefc;
  --emc-seq-2: #cde2fb;
  --emc-seq-3: #9ec5f4;
  --emc-seq-4: #6da7ec;
  --emc-seq-5: #3987e5;
  --emc-seq-6: #2a78d6;
  --emc-seq-7: #1c5cab;
  --emc-seq-8: #104281;
  --emc-seq-9: #0d366b;

  /* diverging (blue = below / saving, grey = nothing, red = above / overrun) */
  --emc-div-1: #184f95;
  --emc-div-2: #2a78d6;
  --emc-div-3: #6da7ec;
  --emc-div-4: #b7d3f6;
  --emc-div-5: #f0efec;
  --emc-div-6: #f6b8b1;
  --emc-div-7: #ec7c72;
  --emc-div-8: #c93936;
  --emc-div-9: #8f1f20;
  --emc-up: #e34948;       /* waterfall increase (increaseIs:'bad') */
  --emc-down: #2a78d6;     /* waterfall decrease */

  /* energy media */
  --emc-electricity: #eda100;
  --emc-water: #2a78d6;
  --emc-gas: #eb6834;
  --emc-thermal: #0095a0;
  --emc-solar: #008300;
  --emc-steam: #4a3aa7;
  --emc-diesel: #9b5a14;

  /* status (reserved: always paired with an icon + label) */
  --emc-good: #0ca30c;
  --emc-warn: #fab219;
  --emc-serious: #ec835a;
  --emc-bad: #d03b3b;
  --emc-good-text: #006300;
  --emc-warn-text: #8a5a00;
  --emc-bad-text: #b3261e;
}

/* dark — OS preference when the host has not chosen a theme */
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --emc-page: #0d0d0d;
    --emc-surface: #1a1a19;
    --emc-surface-2: #232321;
    --emc-ink: #ffffff;
    --emc-ink-2: #c3c2b7;
    --emc-muted: #898781;
    --emc-axis-text: #9d9b94;
    --emc-grid: #2c2c2a;
    --emc-axis: #45453f;
    --emc-border: rgba(255, 255, 255, 0.10);
    --emc-crosshair: #6f6d67;
    --emc-band: rgba(255, 255, 255, 0.05);
    --emc-brush: rgba(57, 135, 229, 0.18);
    --emc-null: #262624;
    --emc-track: #2c2c2a;
    --emc-other: #6f6d67;
    --emc-target: #c3c2b7;
    --emc-total: #8f8d86;
    --emc-focus: #6da7ec;
    --emc-tip-bg: #262624;
    --emc-tip-shadow: 0 8px 28px rgba(0, 0, 0, 0.55), 0 1px 3px rgba(0, 0, 0, 0.4);
    --emc-s1: #3987e5; --emc-s2: #d95926; --emc-s3: #199e70; --emc-s4: #c98500; --emc-s5: #d55181;
    --emc-s6: #008300; --emc-s7: #9085e9; --emc-s8: #e66767; --emc-s9: #00a3a6; --emc-s10: #b8691e;
    --emc-seq-1: #1c2735; --emc-seq-2: #1b3553; --emc-seq-3: #184f95; --emc-seq-4: #1c5cab; --emc-seq-5: #2a78d6;
    --emc-seq-6: #3987e5; --emc-seq-7: #6da7ec; --emc-seq-8: #9ec5f4; --emc-seq-9: #cde2fb;
    --emc-div-1: #9ec5f4; --emc-div-2: #5598e7; --emc-div-3: #2a78d6; --emc-div-4: #1d4474; --emc-div-5: #383835;
    --emc-div-6: #6b2c2a; --emc-div-7: #b53f3c; --emc-div-8: #e66767; --emc-div-9: #f5a8a2;
    --emc-up: #e66767; --emc-down: #3987e5;
    --emc-electricity: #c98500; --emc-water: #3987e5; --emc-gas: #d95926; --emc-thermal: #00a3a6;
    --emc-solar: #008300; --emc-steam: #9085e9; --emc-diesel: #b8691e;
    --emc-good-text: #3ec23e; --emc-warn-text: #fab219; --emc-bad-text: #f07c7c;
  }
}

/* dark — explicit host choice (wins over the OS preference both ways) */
[data-theme="dark"] {
  color-scheme: dark;
  --emc-page: #0d0d0d;
  --emc-surface: #1a1a19;
  --emc-surface-2: #232321;
  --emc-ink: #ffffff;
  --emc-ink-2: #c3c2b7;
  --emc-muted: #898781;
  --emc-axis-text: #9d9b94;
  --emc-grid: #2c2c2a;
  --emc-axis: #45453f;
  --emc-border: rgba(255, 255, 255, 0.10);
  --emc-crosshair: #6f6d67;
  --emc-band: rgba(255, 255, 255, 0.05);
  --emc-brush: rgba(57, 135, 229, 0.18);
  --emc-null: #262624;
  --emc-track: #2c2c2a;
  --emc-other: #6f6d67;
  --emc-target: #c3c2b7;
  --emc-total: #8f8d86;
  --emc-focus: #6da7ec;
  --emc-tip-bg: #262624;
  --emc-tip-shadow: 0 8px 28px rgba(0, 0, 0, 0.55), 0 1px 3px rgba(0, 0, 0, 0.4);
  --emc-s1: #3987e5; --emc-s2: #d95926; --emc-s3: #199e70; --emc-s4: #c98500; --emc-s5: #d55181;
  --emc-s6: #008300; --emc-s7: #9085e9; --emc-s8: #e66767; --emc-s9: #00a3a6; --emc-s10: #b8691e;
  --emc-seq-1: #1c2735; --emc-seq-2: #1b3553; --emc-seq-3: #184f95; --emc-seq-4: #1c5cab; --emc-seq-5: #2a78d6;
  --emc-seq-6: #3987e5; --emc-seq-7: #6da7ec; --emc-seq-8: #9ec5f4; --emc-seq-9: #cde2fb;
  --emc-div-1: #9ec5f4; --emc-div-2: #5598e7; --emc-div-3: #2a78d6; --emc-div-4: #1d4474; --emc-div-5: #383835;
  --emc-div-6: #6b2c2a; --emc-div-7: #b53f3c; --emc-div-8: #e66767; --emc-div-9: #f5a8a2;
  --emc-up: #e66767; --emc-down: #3987e5;
  --emc-electricity: #c98500; --emc-water: #3987e5; --emc-gas: #d95926; --emc-thermal: #00a3a6;
  --emc-solar: #008300; --emc-steam: #9085e9; --emc-diesel: #b8691e;
  --emc-good-text: #3ec23e; --emc-warn-text: #fab219; --emc-bad-text: #f07c7c;
}

/* ---------------------------------------------------------------- chart root */
.emc {
  position: relative;
  font-family: var(--emc-font);
  color: var(--emc-ink);
  min-width: 0;
}
.emc-root { position: relative; width: 100%; }
.emc-svg {
  display: block;
  width: 100%;
  overflow: visible;
  font-family: var(--emc-font);
  -webkit-tap-highlight-color: transparent;
  touch-action: pan-y;
}
.emc-svg:focus { outline: none; }
.emc-svg:focus-visible { outline: 2px solid var(--emc-focus); outline-offset: 2px; border-radius: 4px; }
.emc-svg text { font-family: var(--emc-font); fill: var(--emc-axis-text); font-size: 11px; }
.emc-svg .emc-tick { font-variant-numeric: tabular-nums; }
.emc-svg .emc-tick-strong { fill: var(--emc-ink-2); font-weight: 600; }
.emc-svg .emc-unit { fill: var(--emc-muted); font-size: 11px; }
.emc-svg .emc-lbl { fill: var(--emc-ink-2); font-size: 11px; }
.emc-svg .emc-lbl-strong { fill: var(--emc-ink); font-size: 12px; font-weight: 600; }
.emc-svg .emc-val { fill: var(--emc-ink-2); font-size: 11px; font-variant-numeric: tabular-nums; }
.emc-svg .emc-halo { paint-order: stroke; stroke: var(--emc-surface); stroke-width: 3px; stroke-linejoin: round; }
.emc-svg .emc-grid { stroke: var(--emc-grid); stroke-width: 1; shape-rendering: crispEdges; }
.emc-svg .emc-axisline { stroke: var(--emc-axis); stroke-width: 1; shape-rendering: crispEdges; }
.emc-svg .emc-zero { stroke: var(--emc-axis); stroke-width: 1; shape-rendering: crispEdges; }
.emc-svg .emc-band { fill: var(--emc-band); }
.emc-svg .emc-band-lbl { fill: var(--emc-muted); font-size: 10px; }
.emc-svg .emc-thr { stroke-width: 1.25; stroke-dasharray: 5 4; fill: none; }
.emc-svg .emc-thr-lbl { fill: var(--emc-ink-2); font-size: 10.5px; }
.emc-svg .emc-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.emc-svg .emc-line.emc-dashed { stroke-dasharray: 6 4; stroke-linecap: butt; }
.emc-svg .emc-area { stroke: none; }
.emc-svg .emc-marker { stroke: var(--emc-surface); stroke-width: 2; }
.emc-svg .emc-xhair { stroke: var(--emc-crosshair); stroke-width: 1; shape-rendering: crispEdges; pointer-events: none; }
.emc-svg .emc-hit { fill: transparent; }
.emc-svg .emc-brush { fill: var(--emc-brush); stroke: var(--emc-focus); stroke-width: 1; }
.emc-svg .emc-mark { transition: filter 120ms ease, opacity 120ms ease; }
.emc-svg .emc-mark.emc-hot { filter: brightness(1.12) saturate(1.08); }
.emc-svg.emc-dim-others .emc-mark:not(.emc-hot) { opacity: 0.45; }
.emc-svg .emc-kbd-ring { fill: none; stroke: var(--emc-focus); stroke-width: 2; pointer-events: none; }
.emc-svg .emc-link { fill: none; transition: stroke-opacity 140ms ease; }
.emc-svg.emc-sk-focus .emc-link { stroke-opacity: 0.12 !important; }
.emc-svg.emc-sk-focus .emc-link.emc-on { stroke-opacity: 0.62 !important; }
.emc-svg.emc-sk-focus .emc-node:not(.emc-on) { opacity: 0.5; }
.emc-svg .emc-node { transition: opacity 140ms ease; }
.emc-svg .emc-connector { stroke: var(--emc-axis); stroke-width: 1; shape-rendering: crispEdges; }
.emc-svg .emc-cell-ring { fill: none; stroke: var(--emc-ink); stroke-width: 1.5; pointer-events: none; }
.emc-svg .emc-gauge-val { fill: var(--emc-ink); font-weight: 600; }
.emc-svg .emc-center-val { fill: var(--emc-ink); font-weight: 600; }
.emc-svg .emc-center-lbl { fill: var(--emc-muted); }
.emc-svg .emc-center-val, .emc-svg .emc-center-lbl, .emc-svg .emc-gauge-val, .emc-svg .emc-halo, .emc-svg .emc-val, .emc-svg .emc-unit { pointer-events: none; }
.emc-svg .emc-reg-line { fill: none; stroke: var(--emc-ink-2); stroke-width: 1.5; stroke-dasharray: 6 4; }
.emc-clickable .emc-svg .emc-mark, .emc-clickable .emc-svg .emc-hit-click { cursor: pointer; }
.emc-zoomable .emc-svg .emc-plot-hit { cursor: crosshair; }

/* ---------------------------------------------------------------- legend */
.emc-legend {
  display: flex; flex-wrap: wrap; gap: 4px 14px;
  margin: 0 0 6px 0; padding: 0;
  font-size: 12px; line-height: 18px; color: var(--emc-ink-2);
  list-style: none;
}
.emc-legend-item {
  display: inline-flex; align-items: center; gap: 6px;
  border: 0; background: none; padding: 1px 2px; margin: 0;
  font: inherit; color: inherit; cursor: pointer; border-radius: 4px;
}
.emc-legend-item:focus-visible { outline: 2px solid var(--emc-focus); outline-offset: 1px; }
.emc-legend-item[aria-pressed="false"] { opacity: 0.42; }
.emc-legend-item[aria-pressed="false"] .emc-legend-name { text-decoration: line-through; }
.emc-legend-static { cursor: default; }
.emc-key { display: inline-block; flex: none; }
.emc-key-line { width: 14px; height: 2px; border-radius: 1px; }
.emc-key-dash { width: 14px; height: 0; border-top: 2px dashed; background: none !important; }
.emc-key-rect { width: 10px; height: 10px; border-radius: 2px; }
.emc-key-dot { width: 8px; height: 8px; border-radius: 50%; }
.emc-legend-val { color: var(--emc-ink); font-variant-numeric: tabular-nums; font-weight: 600; }
.emc-legend-pct { color: var(--emc-muted); font-variant-numeric: tabular-nums; }

/* colour-scale legend (heatmap) */
.emc-scale { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; font-size: 11px; color: var(--emc-axis-text); margin: 0 0 6px 0; font-variant-numeric: tabular-nums; }
.emc-scale > span { white-space: nowrap; }
.emc-scale-bar { height: 8px; flex: 0 1 220px; min-width: 80px; border-radius: 2px; }
.emc-scale-null { display: inline-flex; align-items: center; gap: 4px; margin-left: 6px; }
.emc-scale-null i { width: 10px; height: 8px; border-radius: 2px; background: var(--emc-null); display: inline-block; }

/* zoom reset */
.emc-reset {
  position: absolute; top: 0; right: 0; z-index: 2;
  font: 500 11px/1 var(--emc-font); color: var(--emc-ink-2);
  background: var(--emc-surface); border: 1px solid var(--emc-border); border-radius: 6px;
  padding: 5px 8px; cursor: pointer; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}
.emc-reset:hover { color: var(--emc-ink); background: var(--emc-surface-2); }
.emc-reset:focus-visible { outline: 2px solid var(--emc-focus); outline-offset: 1px; }
.emc-reset[hidden] { display: none; }

/* ---------------------------------------------------------------- tooltip */
.emc-tooltip {
  position: fixed; left: 0; top: 0; z-index: 10000;
  pointer-events: none;
  min-width: 120px; max-width: min(320px, calc(100vw - 16px));
  padding: 8px 10px;
  background: var(--emc-tip-bg); color: var(--emc-ink);
  border: 1px solid var(--emc-border); border-radius: 8px;
  box-shadow: var(--emc-tip-shadow);
  font: 12px/1.35 var(--emc-font);
  opacity: 0; transition: opacity 80ms ease;
}
.emc-tooltip.emc-show { opacity: 1; }
.emc-tip-title { color: var(--emc-ink-2); font-size: 11px; margin-bottom: 5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.emc-tip-row { display: grid; grid-template-columns: 14px auto 1fr; align-items: center; column-gap: 7px; padding: 1px 0; }
.emc-tip-row .emc-key-line, .emc-tip-row .emc-key-dash { width: 12px; }
.emc-tip-val { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; color: var(--emc-ink); }
.emc-tip-name { color: var(--emc-ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.emc-tip-row.emc-tip-em .emc-tip-name { color: var(--emc-ink); }
.emc-tip-row.emc-tip-sub .emc-tip-val { font-weight: 500; color: var(--emc-ink-2); }
.emc-tip-sep { height: 1px; background: var(--emc-border); margin: 5px 0; }
.emc-tip-note { color: var(--emc-muted); font-size: 11px; margin-top: 4px; }

/* ---------------------------------------------------------------- KPI tile */
.emc-kpi {
  position: relative;
  display: flex; flex-direction: column; gap: 2px;
  min-width: 0;
  font-family: var(--emc-font);
}
.emc-kpi-label {
  display: flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--emc-ink-2); line-height: 16px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.emc-kpi-medium { width: 8px; height: 8px; border-radius: 2px; flex: none; }
.emc-kpi-value { display: flex; align-items: baseline; gap: 5px; flex-wrap: wrap; margin-top: 2px; }
.emc-kpi-num { font-size: 28px; line-height: 34px; font-weight: 600; color: var(--emc-ink); letter-spacing: -0.01em; }
.emc-kpi-unit { font-size: 13px; color: var(--emc-ink-2); }
.emc-kpi-delta {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 12px; line-height: 16px; font-weight: 600; font-variant-numeric: tabular-nums;
}
.emc-kpi-delta.emc-good { color: var(--emc-good-text); }
.emc-kpi-delta.emc-bad { color: var(--emc-bad-text); }
.emc-kpi-delta.emc-flat { color: var(--emc-ink-2); }
.emc-kpi-delta svg { width: 10px; height: 10px; flex: none; }
.emc-kpi-cmp { font-weight: 400; color: var(--emc-muted); }
.emc-kpi-spark { margin-top: 6px; }

/* ---------------------------------------------------------------- table view */
.emc-table-wrap { max-height: 320px; overflow: auto; border: 1px solid var(--emc-border); border-radius: 6px; }
.emc-table { border-collapse: collapse; width: 100%; font: 12px/1.4 var(--emc-font); color: var(--emc-ink); }
.emc-table th, .emc-table td { padding: 4px 8px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--emc-grid); font-variant-numeric: tabular-nums; }
.emc-table th:first-child, .emc-table td:first-child { text-align: left; }
.emc-table thead th { position: sticky; top: 0; background: var(--emc-surface-2); color: var(--emc-ink-2); font-weight: 600; }

.emc-sr { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

@media (forced-colors: active) {
  .emc-svg .emc-line { stroke: CanvasText !important; }
  .emc-tooltip { border: 1px solid CanvasText; }
}
@media (prefers-reduced-motion: reduce) {
  .emc-svg .emc-mark, .emc-svg .emc-link, .emc-svg .emc-node, .emc-tooltip { transition: none; }
}
