/*
 * EIC collector dashboard.
 *
 * Colours are roles, defined once per mode. The chart palette is the
 * validated set of the data-viz method (fixed order; light and dark steps of
 * the same hues, each checked against its own surface). Site quantities keep
 * their slot everywhere: home 1, solar 2, grid 3, battery 4.
 */

:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --border-strong: rgba(11, 11, 11, 0.22);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --wash: rgba(11, 11, 11, 0.04);
  --focus: #2a78d6;

  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #6250d6;
  --series-8: #e34948;
  --deemph: #c3c2b7;

  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --good-ink: #006300;
  --critical-ink: #a32222;

  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #242423;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --border-strong: rgba(255, 255, 255, 0.24);
    --accent: #3987e5;
    --wash: rgba(255, 255, 255, 0.05);
    --focus: #6da7ec;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
    --deemph: #52514e;
    --good-ink: #0ca30c;
    --critical-ink: #e66767;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #242423;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --border-strong: rgba(255, 255, 255, 0.24);
  --accent: #3987e5;
  --wash: rgba(255, 255, 255, 0.05);
  --focus: #6da7ec;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
  --deemph: #52514e;
  --good-ink: #0ca30c;
  --critical-ink: #e66767;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--page);
  color: var(--ink);
  font: 14px/1.45 var(--font);
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3, p { margin: 0; }
h2 { font-size: 15px; font-weight: 600; }
h3 { font-size: 13px; font-weight: 600; color: var(--ink-2); }
a { color: var(--accent); }
code, pre, .mono { font-family: var(--mono); font-size: 12.5px; }
[hidden] { display: none !important; }

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

.boot { padding: 48px 24px; color: var(--muted); text-align: center; }

/* ---- shell ------------------------------------------------------------ */

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 650; font-size: 15px; letter-spacing: -0.01em; white-space: nowrap; }
.brand small { font-weight: 400; color: var(--muted); margin-left: 6px; }
.tabs { display: flex; gap: 2px; flex-wrap: wrap; }
.tabs a {
  padding: 6px 12px; border-radius: 8px;
  color: var(--ink-2); text-decoration: none; font-weight: 500;
}
.tabs a:hover { background: var(--wash); color: var(--ink); }
.tabs a[aria-current="page"] { background: var(--wash); color: var(--ink); box-shadow: inset 0 -2px 0 var(--accent); }
.topbar .spacer { flex: 1; }
.live { display: inline-flex; align-items: center; gap: 6px; color: var(--ink-2); font-size: 12.5px; white-space: nowrap; }

main { max-width: 1240px; margin: 0 auto; padding: 20px 24px 64px; display: grid; gap: 16px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  min-width: 0;
}
.card > header { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.card > header .note { color: var(--muted); font-size: 12.5px; }
.card > header .actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }

.row { display: grid; gap: 16px; }
.row.two { grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); }
.row.tiles { grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); }
@media (max-width: 520px) {
  .row.two { grid-template-columns: 1fr; }
  main { padding: 12px 12px 48px; }
  .topbar { padding: 8px 12px; }
}

/* ---- controls ----------------------------------------------------------- */

button, .button, select, input, textarea { font: inherit; color: inherit; }

button, .button {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 12px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface);
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
button:hover, .button:hover { background: var(--wash); }
button:disabled { opacity: 0.5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 550; }
button.primary:hover { filter: brightness(1.06); background: var(--accent); }
button.danger { color: var(--critical-ink); }
button.quiet { border-color: transparent; padding: 4px 8px; color: var(--ink-2); }
button.small { padding: 3px 9px; font-size: 12.5px; }

select, input[type="text"], input[type="password"], input[type="datetime-local"], input[type="search"], textarea {
  padding: 6px 10px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface);
  min-width: 0;
}
textarea { width: 100%; resize: vertical; font-family: var(--mono); font-size: 12.5px; }
label.field { display: grid; gap: 4px; font-size: 12.5px; color: var(--ink-2); }
label.field > span { font-weight: 500; }

/* A row of presets: the chosen one is filled. */
.segmented { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 8px; overflow: hidden; }
.segmented button { border: 0; border-radius: 0; padding: 6px 12px; background: transparent; color: var(--ink-2); }
.segmented button + button { border-left: 1px solid var(--border); }
.segmented button[aria-pressed="true"] { background: var(--ink); color: var(--surface); font-weight: 550; }

.filters { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.filters .custom { display: inline-flex; gap: 8px; align-items: center; color: var(--ink-2); }

/* ---- status: always an icon and a label, never colour alone --------------- */

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 2px 9px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface-2);
  font-size: 12.5px; white-space: nowrap; color: var(--ink);
}
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--muted); }
.dot.good { background: var(--good); }
.dot.warning { background: var(--warning); border-radius: 1px; transform: rotate(45deg); }
.dot.serious { background: var(--serious); border-radius: 1px; }
.dot.critical { background: var(--critical); border-radius: 1px; }
.dot.idle { background: transparent; box-shadow: inset 0 0 0 2px var(--muted); }
.key { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.linekey { width: 14px; height: 2px; border-radius: 1px; flex: none; }

.banner {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 10px 14px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--surface-2);
}
.banner strong { font-weight: 600; }
.error-text { color: var(--critical-ink); }
.muted { color: var(--muted); }
.secondary { color: var(--ink-2); }

/* ---- unit header ------------------------------------------------------------ */

.unit-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.unit-head h1 { font-size: 20px; font-weight: 650; letter-spacing: -0.01em; }
.unit-head .meta { color: var(--ink-2); font-size: 12.5px; display: flex; gap: 14px; flex-wrap: wrap; }

/* ---- stat tiles -------------------------------------------------------------- */

.tile { display: grid; gap: 6px; align-content: start; }
.tile .label { display: flex; align-items: center; gap: 8px; color: var(--ink-2); font-size: 12.5px; font-weight: 500; }
.tile .value { font-size: 28px; font-weight: 600; letter-spacing: -0.02em; line-height: 1.15; }
.tile .value small { font-size: 15px; font-weight: 500; color: var(--ink-2); margin-left: 3px; letter-spacing: 0; }
.tile .sub { color: var(--ink-2); font-size: 12.5px; min-height: 18px; }
.tile svg.spark { display: block; width: 100%; height: 32px; }
.meter { height: 8px; border-radius: 4px; background: color-mix(in srgb, var(--series-4) 22%, var(--surface)); overflow: hidden; }
.meter > span { display: block; height: 100%; border-radius: 4px; background: var(--series-4); }
.meter.progress { background: color-mix(in srgb, var(--accent) 20%, var(--surface)); }
.meter.progress > span { background: var(--accent); transition: width 0.3s; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 4px 24px; }
.kpis > div { display: grid; gap: 2px; padding: 4px 0; }
.kpis .label { color: var(--ink-2); font-size: 12.5px; display: flex; align-items: center; gap: 8px; }
.kpis .value { font-size: 20px; font-weight: 600; letter-spacing: -0.01em; }
.kpis .value small { font-size: 13px; font-weight: 500; color: var(--ink-2); margin-left: 3px; }

/* ---- tables -------------------------------------------------------------------- */

.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--grid); vertical-align: top; }
th { color: var(--ink-2); font-weight: 550; font-size: 12.5px; white-space: nowrap; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
.table-scroll { max-height: 320px; overflow: auto; border: 1px solid var(--grid); border-radius: 8px; }
.table-scroll thead th { position: sticky; top: 0; background: var(--surface); }

/* ---- charts --------------------------------------------------------------------- */

.chart { position: relative; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart svg:focus-visible { outline: 2px solid var(--focus); outline-offset: 4px; border-radius: 6px; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart text.endlabel { fill: var(--ink-2); font-size: 11.5px; font-variant-numeric: normal; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { stroke: none; opacity: 0.10; }
.chart .leader { stroke: var(--axis); stroke-width: 1; }
.chart .crosshair { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart .marker { stroke: var(--surface); stroke-width: 2; }
.chart .bar { transition: opacity 0.1s; }
.chart .bar.dim { opacity: 0.45; }
.chart.loading svg, .chart.loading .legend { opacity: 0.45; }
.chart .empty { color: var(--muted); padding: 48px 0; text-align: center; }

.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12.5px; color: var(--ink-2); }
.legend > span, .legend > button { display: inline-flex; align-items: center; gap: 6px; }

.tooltip {
  position: fixed; z-index: 50; pointer-events: none;
  min-width: 140px; max-width: 320px;
  padding: 8px 10px; border-radius: 8px;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--border-strong);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.14);
  font-size: 12.5px;
}
.tooltip .when { color: var(--ink-2); margin-bottom: 4px; }
.tooltip .line { display: grid; grid-template-columns: 14px auto 1fr; gap: 8px; align-items: center; }
.tooltip .line b { font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tooltip .line span.name { color: var(--ink-2); text-align: right; }
.tooltip .range { color: var(--muted); font-size: 11.5px; grid-column: 2 / 4; margin-top: -2px; }

/* ---- live log -------------------------------------------------------------------- */

.log { font-size: 13px; }
.log .entry { border-bottom: 1px solid var(--grid); }
.log .entry:last-child { border-bottom: 0; }
.log .entry > button {
  display: grid; grid-template-columns: 86px 96px 92px 1fr; gap: 10px; align-items: baseline;
  width: 100%; text-align: left; white-space: normal;
  border: 0; border-radius: 0; background: transparent; padding: 6px 4px;
}
.log .entry > button:hover { background: var(--wash); }
.log .time { color: var(--muted); font-variant-numeric: tabular-nums; font-family: var(--mono); font-size: 12px; }
.log .unit { color: var(--ink-2); font-family: var(--mono); font-size: 12px; overflow: hidden; text-overflow: ellipsis; }
.log .kind { display: inline-flex; align-items: center; gap: 6px; color: var(--ink-2); }
.log .message { color: var(--ink); overflow-wrap: anywhere; }
.log pre { margin: 0 4px 8px 196px; padding: 8px 10px; border-radius: 8px; background: var(--surface-2); overflow: auto; max-height: 280px; }
@media (max-width: 760px) {
  .log .entry > button { grid-template-columns: 70px 1fr; }
  .log .unit, .log .kind { display: none; }
  .log pre { margin-left: 4px; }
}

/* ---- forms and small pieces ------------------------------------------------------- */

.auth { max-width: 380px; margin: 12vh auto; display: grid; gap: 14px; }
.auth h1 { font-size: 20px; font-weight: 650; }
.stack { display: grid; gap: 12px; }
.inline { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.drop {
  border: 1px dashed var(--border-strong); border-radius: var(--radius);
  padding: 18px; text-align: center; color: var(--ink-2);
}
.drop.over { background: var(--wash); border-color: var(--accent); }
.secret { font-family: var(--mono); font-size: 12.5px; padding: 6px 10px; border-radius: 8px; background: var(--surface-2); overflow-wrap: anywhere; }
dl.facts { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
dl.facts dt { color: var(--ink-2); }
dl.facts dd { margin: 0; overflow-wrap: anywhere; }
.series-pick { max-height: 260px; overflow: auto; border: 1px solid var(--grid); border-radius: 8px; padding: 6px 10px; display: grid; gap: 2px; }
.series-pick label { display: flex; gap: 8px; align-items: center; padding: 2px 0; }
.series-pick .group { font-weight: 600; color: var(--ink-2); font-size: 12.5px; margin-top: 6px; }
.series-pick .unit { color: var(--muted); }
