/* Palette roles (light first, dark overrides below). */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-soft: #cde2fb;
  --accent-track: #b7d3f6;
  --series-1: #2a78d6;
  --series-2: #eb6834; /* chargers; validated against series-1 for CVD in both modes */
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.06);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #242423;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --accent-soft: #184f95;
    --accent-track: #104281;
    --series-1: #3987e5;
    --series-2: #d95926;
    --good-text: #0ca30c;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--page);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
a { color: var(--accent); }
h1, h2, h3 { margin: 0; font-weight: 600; }
h2 { font-size: 18px; }
h3 { font-size: 14px; color: var(--text-2); font-weight: 600; }
code, pre { font-family: ui-monospace, Consolas, "Cascadia Mono", monospace; font-size: 12.5px; }
pre { background: var(--surface-2); padding: 10px 12px; border-radius: 6px; overflow-x: auto; margin: 0; }
button, select, input {
  font: inherit;
  color: inherit;
}
button {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.08); }
button[disabled] { opacity: 0.6; cursor: default; }
input[type="date"] {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { color: var(--accent); display: inline-flex; }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-title { font-weight: 600; font-size: 15px; }
.brand-sub { color: var(--muted); font-size: 12px; }
.tabs { display: flex; gap: 4px; margin-left: auto; margin-right: auto; flex-wrap: wrap; justify-content: center; }
.tabs a {
  text-decoration: none;
  color: var(--text-2);
  padding: 6px 14px;
  border-radius: 999px;
  font-weight: 500;
}
.tabs a:hover { background: var(--surface-2); }
.tabs a.active { background: var(--accent); color: #fff; }
.conn { display: flex; align-items: center; gap: 8px; color: var(--text-2); font-size: 13px; }
.conn-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.conn-dot[data-state="ok"] { background: var(--good); }
.conn-dot[data-state="demo"] { background: var(--warning); }
.conn-dot[data-state="snapshot"] { background: var(--accent); }
.conn-dot[data-state="error"] { background: var(--critical); }

/* Layout */
main { flex: 1; width: 100%; max-width: 1240px; margin: 0 auto; padding: 24px; }
.view { display: flex; flex-direction: column; gap: 20px; }
/* The hidden attribute must win over display: flex on views, the custom date
   range and anything else that is toggled with el.hidden. */
[hidden] { display: none !important; }
.view-head { display: flex; align-items: baseline; gap: 16px; flex-wrap: wrap; }
.view-head .spacer { flex: 1; }
.subtle { color: var(--muted); font-size: 12.5px; }
.footer { color: var(--muted); font-size: 12px; text-align: center; padding: 16px 24px 24px; }
.footer-credit { margin: 6px auto 0; max-width: 720px; }
.footer-credit a { color: inherit; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: baseline; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head .spacer { flex: 1; }
.grid { display: grid; gap: 16px; }
.grid > * { min-width: 0; } /* cards shrink below their content width, so tables scroll instead of widening the page */
.grid-records { grid-template-columns: minmax(280px, 1fr) minmax(0, 2.2fr); }
@media (max-width: 800px) { .grid-records { grid-template-columns: 1fr; } }
.grid-tiles { grid-template-columns: repeat(auto-fit, minmax(min(100%, 180px), 1fr)); }
/* Nothing may widen the page: sections and cards shrink, and wide charts and
   tables scroll sideways inside their card instead. */
.topbar, .tabs, main, .view, .view > *, .card, .card > * { min-width: 0; max-width: 100%; }
.chart { overflow-x: auto; }


.grid-evses { grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }

/* Hero + tiles */
.hero { display: grid; grid-template-columns: minmax(260px, 1.2fr) 2fr; gap: 16px; align-items: stretch; }
.hero .grid-tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (max-width: 800px) { .hero { grid-template-columns: 1fr; } }
@media (max-width: 480px) { .hero .grid-tiles { grid-template-columns: 1fr; } }
.hero-figure { font-size: 52px; font-weight: 600; line-height: 1.05; letter-spacing: -0.01em; }
.hero-figure small { font-size: 20px; font-weight: 500; color: var(--text-2); margin-left: 4px; }
.hero-sub { color: var(--text-2); margin-top: 6px; }
.meter { margin-top: 14px; height: 10px; border-radius: 999px; background: var(--accent-track); overflow: hidden; }
.meter > span { display: block; height: 100%; background: var(--accent); border-radius: 999px; transition: width 400ms ease; }
.meter-legend { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; margin-top: 6px; }

.tile { display: flex; flex-direction: column; gap: 4px; }
.tile .label { color: var(--text-2); font-size: 12.5px; }
.tile .value { font-size: 26px; font-weight: 600; line-height: 1.1; }
.tile .value small { font-size: 14px; font-weight: 500; color: var(--text-2); margin-left: 3px; }
.tile .hint { color: var(--muted); font-size: 12px; }

/* Status pills - always icon + label, never color alone */
.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 10px 2px 8px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 500;
  background: var(--surface-2);
  color: var(--text-2);
  white-space: nowrap;
}
.pill::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.pill[data-bucket="charging"] { color: var(--good-text); }
.pill[data-bucket="charging"]::before { background: var(--good); }
.pill[data-bucket="available"] { color: var(--text-2); }
.pill[data-bucket="available"]::before { background: var(--accent); }
.pill[data-bucket="occupied"]::before { background: var(--warning); }
.pill[data-bucket="unavailable"] { color: var(--critical); }
.pill[data-bucket="unavailable"]::before { background: var(--critical); }
.pill[data-bucket="unknown"]::before { background: var(--muted); }

/* EVSE cards */
.evse { display: flex; flex-direction: column; gap: 10px; }
.evse-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.evse-name { font-weight: 600; font-size: 15px; }
.evse-id { color: var(--muted); font-size: 12px; font-family: ui-monospace, Consolas, monospace; }
.evse-power { font-size: 24px; font-weight: 600; line-height: 1.1; }
.evse-power small { font-size: 13px; color: var(--text-2); font-weight: 500; margin-left: 3px; }
.evse-meta { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; color: var(--text-2); font-size: 12.5px; }
.evse-meta dt { color: var(--muted); }
.evse-meta dd { margin: 0; }
.evse[data-bucket="charging"] { border-color: color-mix(in srgb, var(--good) 45%, var(--border)); }
.evse[data-bucket="unavailable"] { opacity: 0.85; }

/* Filters */
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.filters .chip {
  border-radius: 999px;
  padding: 5px 12px;
}
.filters .chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.filters .custom { display: flex; gap: 6px; align-items: center; margin-left: 8px; flex-wrap: wrap; }

/* Tables */
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--grid); vertical-align: top; }
th { color: var(--text-2); font-weight: 600; font-size: 12px; white-space: nowrap; position: sticky; top: 0; background: var(--surface); }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
th .sort-mark { color: var(--muted); margin-left: 4px; font-size: 10px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--surface-2); }
.mono { font-family: ui-monospace, Consolas, "Cascadia Mono", monospace; font-size: 12px; }
.empty { color: var(--muted); padding: 24px; text-align: center; }

/* Chart */
.chart { width: 100%; }
.chart svg { width: 100%; height: auto; display: block; overflow: visible; }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; }
.chart .axis-line { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .bar { fill: var(--series-1); }
.chart .bar-hit { fill: transparent; cursor: default; }
.chart .bar-hit:hover + .bar, .chart g:hover .bar { fill: color-mix(in srgb, var(--series-1) 80%, var(--text)); }
.chart .bar-label { fill: var(--text-2); font-size: 11px; font-weight: 600; }

.tooltip {
  position: fixed;
  z-index: 20;
  pointer-events: none;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 12.5px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  max-width: 260px;
}
.tooltip strong { display: block; }

/* Messages */
.notice { border-left: 3px solid var(--accent); padding: 10px 14px; background: var(--surface); border-radius: 6px; }
.notice.error { border-left-color: var(--critical); }
.notice.warn { border-left-color: var(--warning); }
.notice h3 { margin-bottom: 4px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; }
.kv dd { min-width: 0; overflow-wrap: anywhere; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-all; }
.ok { color: var(--good-text); font-weight: 600; }
.bad { color: var(--critical); font-weight: 600; }
.loading { color: var(--muted); padding: 24px; text-align: center; }

/* Analysis tab. Heatmap ramp: one hue (blue), validated as an ordinal ramp -
   light starts at step 250 and dark ends at step 600 of the reference palette. */
:root {
  --heat-1: #86b6ef;
  --heat-2: #5598e7;
  --heat-3: #2a78d6;
  --heat-4: #1c5cab;
  --heat-5: #0d366b;
}
@media (prefers-color-scheme: dark) {
  :root {
    --heat-1: #184f95;
    --heat-2: #256abf;
    --heat-3: #3987e5;
    --heat-4: #6da7ec;
    --heat-5: #9ec5f4;
  }
}
.filters label { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; color: var(--text-2); }
.filters input[type="number"] { width: 68px; background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 4px 6px; }
.heatmap { overflow-x: auto; }
.heatmap svg { width: 100%; min-width: 700px; height: auto; display: block; } /* cells stay tappable on phones; the card scrolls sideways */
.heatmap .cell:hover { stroke: var(--text); stroke-width: 1.5px; }
.ramp-legend { display: flex; align-items: center; gap: 6px; margin-top: 10px; font-size: 12px; color: var(--muted); }
.ramp-legend i { display: inline-block; width: 22px; height: 12px; border-radius: 3px; }
.ramp-legend .spacer { flex: 1; }
#analysis-heatmap-card[data-view="chart"] .heatmap-table,
#analysis-heatmap-card[data-view="table"] .heatmap { display: none; }
.grid-analysis { grid-template-columns: repeat(auto-fit, minmax(360px, 1fr)); }
.chart .cap-line { stroke: var(--muted); stroke-width: 1; }
.chart .bar.partial { opacity: 0.55; }

/* Meter tab and limits. Everything else = series-1 (blue), chargers = series-2
   (orange). Limits are drawn in text ink, never a series colour: dashed for a
   limit on the chargers, dotted for a ceiling on the whole facility. */
.chart .seg.other { fill: var(--series-1); }
.chart .seg.chargers { fill: var(--series-2); }
.chart .seg.partial { opacity: 0.55; }
.chart g:hover .seg { filter: brightness(0.88); }
.chart .limit-line, .chart .limit-mark { stroke: var(--text-2); stroke-width: 2; fill: none; stroke-dasharray: 6 4; }
.chart .limit-line.facility, .chart .limit-mark.facility { stroke-dasharray: 2 3; }
.chart .limit-line.fuse { stroke: var(--muted); stroke-width: 1; stroke-dasharray: none; }
.chart .limit-label { fill: var(--text-2); paint-order: stroke; stroke: var(--surface); stroke-width: 3px; }
.series-legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 12px; color: var(--text-2); margin: 4px 0 8px; }
.series-legend span { display: inline-flex; align-items: center; gap: 6px; }
.swatch { display: inline-block; width: 14px; height: 10px; border-radius: 2px; }
.swatch.other { background: var(--series-1); }
.swatch.chargers { background: var(--series-2); }
.swatch.limit { height: 0; width: 18px; border-top: 2px dashed var(--text-2); border-radius: 0; }
.swatch.limit.facility { border-top-style: dotted; }
.limits-legend { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 12px; }
.limit-chip { display: inline-block; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border); }
.limit-chip.chargers::before { content: '- - '; color: var(--muted); }
.limit-chip.facility::before { content: '··· '; color: var(--muted); }
.filters select { background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 4px 6px; }
.tariff-form { margin-bottom: 8px; }
.tariff-form input[type="number"] { width: 72px; }
#meter-power-card[data-view="chart"] .power-table,
#meter-power-card[data-view="table"] .chart,
#meter-power-card[data-view="table"] .series-legend { display: none; }
.power-table { max-height: 420px; overflow-y: auto; }

/* Sub-tabs under Chargers (Live, Records, Charging points) */
.subtabs { display: flex; gap: 4px; margin-bottom: 14px; border-bottom: 1px solid var(--border); overflow-x: auto; }
.subtabs a { text-decoration: none; color: var(--text-2); padding: 8px 14px; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; font-weight: 500; }
.subtabs a:hover { color: var(--text); }
.subtabs a.active { color: var(--accent); border-bottom-color: var(--accent); }

/* When do the peaks happen */
.typical-days { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.chart .load-band { fill: var(--surface-2); }
.chart .band-label { fill: var(--text-2); font-size: 11px; }
.chart .p95-mark { stroke: var(--text); stroke-width: 2; }
.chart .base-line { stroke: var(--muted); stroke-width: 1.5; stroke-dasharray: 4 3; }
.swatch.p95 { height: 0; border-top: 2px solid var(--text); border-radius: 0; }
.swatch.base { height: 0; border-top: 1.5px dashed var(--muted); border-radius: 0; }
.swatch.band { background: var(--surface-2); border: 1px solid var(--border); }
.conf { display: inline-block; font-size: 11px; padding: 0 6px; border-radius: 999px; border: 1px solid var(--border); color: var(--text-2); white-space: nowrap; }
.conf-measured, .conf-likely { border-color: var(--good); }
.conf-estimated, .conf-possible { border-color: var(--warning); }
.conf-unknown { border-style: dashed; }

/* Solar: the 3D model */
.solar-3d { position: relative; height: 480px; border-radius: 8px; overflow: hidden; background: var(--surface-2); touch-action: none; }
.solar-3d canvas { display: block; width: 100%; height: 100%; }
@media (max-width: 640px) { .solar-3d { height: 340px; } }
.solar-sun { align-items: center; margin-top: 8px; }
.solar-sun .chip .swatch { margin-right: 6px; }
/* Elpris nu */
.now-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 12px; margin-bottom: 16px; }
.now-tiles .card { margin: 0; }
.now-tiles .value { font-size: 24px; }
.price-chart { width: 100%; height: auto; display: block; margin: 6px 0 4px; }
.price-chart .grid-line { stroke: var(--grid); stroke-width: 1; }
.price-chart .axis { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.price-chart .now-line { stroke: var(--text); stroke-width: 1.5; stroke-dasharray: 3 3; }
.price-chart .now-label { fill: var(--text); font-weight: 600; }
.glare-heat td, .glare-heat th { padding: 3px 4px; min-width: 22px; }
.glare-heat td[style] { box-shadow: inset 0 0 0 1px var(--surface); }
.glare-heat tr.glare-high td:first-child { border-left: 3px solid #c8501a; }
.solar-sun .sun-time { display: inline-flex; align-items: center; gap: 6px; }
.solar-sun .sun-time input[type="range"] { width: min(260px, 50vw); }

/* Economics */
.chart .market-band { fill: var(--accent-soft); opacity: 0.6; }
.chart .dot.other { fill: var(--series-1); stroke: var(--surface); stroke-width: 2; }
.chart .dot.chargers { fill: var(--series-2); stroke: var(--surface); stroke-width: 2; }
tr.subtotal td { border-top: 1px solid var(--axis); }
tr.total td { border-top: 2px solid var(--axis); }

/* Guide ("Så funkar det") */
.guide { display: flex; flex-direction: column; gap: 16px; max-width: 900px; }
.guide p, .guide li { line-height: 1.55; }
.guide h3 { font-size: 15px; color: var(--text); margin-bottom: 6px; }
.guide .kv dd { word-break: normal; }
.diagram { margin: 10px 0; overflow-x: auto; }
.diagram svg { width: 100%; min-width: 520px; height: auto; display: block; }
.flow-box rect { fill: var(--surface-2); stroke: var(--axis); }
.flow-box.chargers rect { stroke: var(--series-2); stroke-width: 2; }
.flow-box.meter rect { stroke: var(--series-1); stroke-width: 2; }
.flow-title { fill: var(--text); font-size: 14px; font-weight: 600; }
.flow-sub { fill: var(--text-2); font-size: 12px; }
.flow-arrow { stroke: var(--muted); stroke-width: 2; fill: none; }
.flow-head { fill: var(--muted); }

/* Phones */
@media (max-width: 640px) {
  main { padding: 12px; }
  .view { gap: 14px; }
  .card { padding: 14px; }
  .topbar { gap: 8px 12px; padding: 8px 12px; }
  .tabs { order: 3; width: 100%; flex-wrap: nowrap; overflow-x: auto; justify-content: flex-start; margin: 0; scrollbar-width: none; }
  .tabs::-webkit-scrollbar { display: none; }
  .tabs a { padding: 6px 11px; font-size: 14px; white-space: nowrap; flex: none; }
  .conn { margin-left: auto; font-size: 12px; }
  h2 { font-size: 17px; }
  .grid-tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .tile .value { font-size: 22px; }
  .tile .hint { font-size: 11.5px; }
  .limits-legend { font-size: 11px; gap: 4px; }
  .limit-chip { padding: 1px 6px; }
  .filters { gap: 6px; }
  .filters .chip { padding: 5px 10px; font-size: 13px; }
  .filters label { font-size: 12.5px; }
  /* Charts keep readable text and scroll sideways. */
  .chart svg { min-width: 620px; }
  .typical-days .chart svg { min-width: 420px; }
  .diagram svg { min-width: 480px; }
  .notice { padding: 10px 12px; }
  .kv { grid-template-columns: 1fr; gap: 2px 0; }
  .kv dt { margin-top: 6px; }
}
@media (max-width: 380px) {
  .grid-tiles { grid-template-columns: 1fr; }
}
