/* Theme: based on Caspio Flex "Plum" (dark variant by default), with the logo's
   leaf colours carrying the data: green = solar, teal = battery, plum = interface accent.
   When embedded in Caspio Flex, app.js overrides these with the Flex page's own
   --cas-global-* theme values so the dashboard matches the app exactly. */
:root, :root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #211B26;
  --surface: #2B2331;
  --surface-2: #362C3E;
  --rule: #463A50;
  --ink: #F2ECF5;
  --muted: #B3A5BF;
  --accent: #D69BC5;
  --accent-ink: #2A1D28;
  --solar: #8CC641;
  --battery: #63C5B4;
  --import: #EF8A6E;
  --export: #97A5F0;
  --load: #A497B0;
  --site1: #8CC641; --site2: #63C5B4; --site3: #D69BC5; --site4: #E9C46A;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  --radius: 10px;
  --font: "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, sans-serif;
  --display: "Segoe UI Variable Display", "Segoe UI", system-ui, sans-serif;
}
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #F6F2F7;
  --surface: #FFFFFF;
  --surface-2: #F1EAF3;
  --rule: #E1D6E6;
  --ink: #2A2030;
  --muted: #6E6078;
  --accent: #8E4F84;
  --accent-ink: #FFFFFF;
  --solar: #5B9A1C;
  --battery: #24907F;
  --import: #C4553B;
  --export: #4E5FC2;
  --load: #7D6F88;
  --site1: #5B9A1C; --site2: #24907F; --site3: #B05E9C; --site4: #C4962A;
  --shadow: 0 8px 24px rgba(60, 30, 70, 0.14);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 var(--font);
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { font-family: var(--display); margin: 0; }
h2 { font-size: 1.05rem; font-weight: 600; }
button, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 16px;
  padding: 10px 28px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--rule);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand .logo { height: 30px; width: auto; display: block; }
.brand h1 { font-size: 1.15rem; font-weight: 600; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.period { display: flex; background: var(--surface); border: 1px solid var(--rule); border-radius: 999px; padding: 3px; }
.period button {
  border: 0; background: none; padding: 6px 16px; border-radius: 999px; cursor: pointer; color: var(--muted);
}
.period button:hover { color: var(--ink); }
.period button[aria-selected="true"] { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.sync { justify-self: end; display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 0.88rem; }
.quiet {
  border: 1px solid var(--rule); background: var(--surface); border-radius: 6px; padding: 5px 12px; cursor: pointer;
}
.quiet:hover { border-color: var(--accent); }

.notice, .errors { margin: 16px 28px 0; padding: 10px 14px; border-radius: 8px; font-size: 0.92rem; border: 1px solid; }
.notice { background: color-mix(in srgb, var(--accent) 12%, var(--surface)); border-color: color-mix(in srgb, var(--accent) 35%, transparent); }
.errors { background: color-mix(in srgb, var(--import) 12%, var(--surface)); border-color: color-mix(in srgb, var(--import) 40%, transparent); }
.errors ul { margin: 4px 0 0; padding-left: 18px; }
.err-note { color: var(--muted); font-size: 0.85rem; }

main { padding: 8px 28px 40px; max-width: 1680px; margin: 0 auto; }

/* ---------- hero ---------- */
.hero {
  display: grid; grid-template-columns: minmax(260px, 0.8fr) 1.6fr; gap: 48px; align-items: center;
  padding: 28px 0 24px;
}
.hero-figure {
  margin: 0; font-family: var(--display); font-weight: 300; line-height: 0.95;
  font-size: clamp(4rem, 9vw, 7.5rem); letter-spacing: -0.03em; color: var(--solar);
}
.hero-unit { font-size: 0.32em; font-weight: 500; letter-spacing: 0; margin-left: 0.18em; color: var(--muted); }
.hero-caption { margin: 8px 0 0; color: var(--muted); font-size: 1.05rem; }
.hero-battery { margin: 14px 0 0; font-size: 1rem; }
.hero-battery strong { color: var(--battery); font-weight: 600; }

.hero-co2 { margin: 6px 0 0; font-size: 1rem; }
.hero-co2 strong { color: var(--solar); font-weight: 600; }
.stats dd.co2 { color: var(--solar); }
.site-co2 { margin-top: 4px; }
.ledger { display: grid; gap: 14px; }
.ledger-row { display: grid; grid-template-columns: 92px 1fr 84px; align-items: center; gap: 14px; }
.ledger-name { color: var(--muted); font-size: 0.92rem; }
.ledger-total { text-align: right; font-weight: 600; }
.ledger-bar { display: flex; height: 44px; border-radius: 8px; overflow: hidden; background: var(--surface-2); }
.ledger-seg {
  display: flex; align-items: center; padding: 0 10px; min-width: 0; white-space: nowrap; overflow: hidden;
  font-size: 0.85rem; font-weight: 600; transition: flex-basis 600ms ease;
}
.ledger-key { display: flex; flex-wrap: wrap; gap: 6px 18px; padding-left: 106px; color: var(--muted); font-size: 0.85rem; }
.ledger-key span { display: inline-flex; align-items: center; gap: 6px; }

/* ---------- period stats ---------- */
.stats { border-top: 1px solid var(--rule); border-bottom: 1px solid var(--rule); padding: 16px 0 18px; }
.stats h2 { color: var(--muted); font-weight: 500; font-size: 0.92rem; margin-bottom: 8px; }
.stats dl { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px 24px; margin: 0; }
.stats dt { color: var(--muted); font-size: 0.85rem; }
.stats dd { margin: 2px 0 0; font-family: var(--display); font-size: 1.6rem; font-weight: 500; letter-spacing: -0.01em; }
.stats dd small { font-size: 0.55em; color: var(--muted); font-weight: 500; margin-left: 3px; }

.stats-note { margin: 10px 0 0; color: var(--muted); font-size: 0.82rem; }
.stats-note:empty { display: none; }
.muted { color: var(--muted); }

/* ---------- sites ---------- */
.sites {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  margin: 24px 0; background: var(--surface); border: 1px solid var(--rule); border-radius: var(--radius); overflow: hidden;
}
.site { padding: 18px 20px 20px; border-left: 1px solid var(--rule); border-top: 3px solid var(--site-color, var(--accent)); }
.site:first-child { border-left: 0; }
.site-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.site-name { font-size: 1.1rem; font-weight: 600; }
.status { font-size: 0.8rem; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status.online::before { background: var(--battery); }
.status.fault::before { background: var(--import); }
.site-model { color: var(--muted); font-size: 0.8rem; margin: 2px 0 12px; }
.site-solar { font-family: var(--display); font-size: 2.3rem; font-weight: 400; color: var(--solar); line-height: 1; letter-spacing: -0.02em; }
.site-solar small { font-size: 0.42em; color: var(--muted); margin-left: 4px; letter-spacing: 0; }
.site-rows { margin: 14px 0 0; display: grid; gap: 7px; }
.site-row { display: flex; justify-content: space-between; gap: 8px; font-size: 0.92rem; }
.site-row span:first-child { color: var(--muted); }
/* battery graphic */
.site { container-type: inline-size; min-width: 0; }
.bat {
  display: grid; grid-template-columns: minmax(84px, 120px) 1fr; column-gap: 14px; row-gap: 8px;
  grid-template-areas: "svg pct" "svg info" "packs packs";
  align-items: center; margin-top: 16px; padding: 12px; background: var(--surface-2); border-radius: 8px; min-width: 0;
}
.bat > .bat-svg { grid-area: svg; }
.bat > .bat-pct { grid-area: pct; align-self: end; }
.bat > .bat-text { grid-area: info; align-self: start; }
.bat > .bat-packs { grid-area: packs; }
/* Narrow cards (e.g. five or more sites side by side): percentage beside the battery,
   details underneath at full width so nothing spills out of the panel. */
@container (max-width: 300px) {
  .bat { grid-template-columns: minmax(72px, 104px) 1fr; grid-template-areas: "svg pct" "info info" "packs packs"; }
  .bat > .bat-pct { align-self: center; }
}
.bat-svg { width: 100%; height: auto; display: block; }
.bat-shell { fill: none; stroke: var(--muted); stroke-width: 3; }
.bat-nub { fill: var(--muted); }
.bat-empty { fill: var(--bg); }
.bat-tick { stroke: var(--surface-2); stroke-width: 2; }
.bat-symbol { fill: var(--bg); stroke: var(--ink); stroke-width: 2.5; stroke-linejoin: round; }
path.bat-symbol:not(.pulse) { fill: none; stroke: var(--ink); stroke-width: 4; stroke-linecap: round; }
.bat-text { display: grid; gap: 2px; min-width: 0; overflow-wrap: anywhere; }
.bat-pct { font-family: var(--display); font-size: clamp(1.5rem, 9cqi, 1.9rem); font-weight: 500; line-height: 1.05; letter-spacing: -0.02em; }
.bat-sub { font-size: 0.85rem; color: var(--muted); }
.bat-cap b { color: var(--ink); font-weight: 600; }
.bat-lowtag { color: var(--import); font-weight: 600; }
.bat-flow { font-size: 0.85rem; color: var(--muted); }
.bat-low { box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--import) 55%, transparent); }
.bat-packs {
  grid-column: 1 / -1; list-style: none; margin: 4px 0 0; padding: 10px 0 0;
  border-top: 1px solid var(--rule); display: grid; gap: 6px;
}
.bat-packs li { display: grid; grid-template-columns: 34px 1fr auto; align-items: center; gap: 8px; font-size: 0.85rem; color: var(--muted); }
.bat-packs svg { width: 34px; height: 16px; display: block; }
.bat-packs b { color: var(--ink); font-weight: 600; }
.bat-shell.mini { stroke-width: 1.5; }
.bat-none { display: flex; justify-content: center; color: var(--muted); font-size: 0.88rem; background: none; border: 1px dashed var(--rule); }
.pulse { animation: bat-pulse 1.8s ease-in-out infinite; }
@keyframes bat-pulse { 50% { opacity: 0.6; } }
.site-today { margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--rule); font-size: 0.85rem; color: var(--muted); }
.site-today b { color: var(--ink); font-weight: 600; }
.importing { color: var(--import); }
.exporting { color: var(--export); }
.charging, .discharging { color: var(--battery); }

/* ---------- charts ---------- */
.charts-head { display: flex; justify-content: space-between; align-items: center; margin: 8px 0 12px; }
.site-filter { color: var(--muted); font-size: 0.9rem; display: flex; align-items: center; gap: 8px; }
.site-filter select { background: var(--surface); color: var(--ink); border: 1px solid var(--rule); border-radius: 6px; padding: 5px 8px; }
.site-filter option { background: var(--surface); color: var(--ink); }
.chart-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(480px, 1fr)); gap: 16px; }
.panel { background: var(--surface); border: 1px solid var(--rule); border-radius: var(--radius); margin: 0; padding: 16px 18px 12px; min-width: 0; }
.panel figcaption { font-family: var(--display); font-weight: 600; margin-bottom: 6px; }
.panel .empty { color: var(--muted); }

.chart-legend { display: flex; flex-wrap: wrap; gap: 4px 6px; margin-bottom: 4px; }
.legend-item {
  display: inline-flex; align-items: center; gap: 6px; border: 0; background: none; cursor: pointer;
  padding: 3px 6px; border-radius: 5px; font-size: 0.82rem; color: var(--muted);
}
.legend-item[aria-pressed="false"] { opacity: 0.4; text-decoration: line-through; }
.legend-item:hover { background: var(--surface-2); }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; flex: none; }
.chart-plot { position: relative; }
.chart-plot svg { display: block; overflow: visible; }
.axis text { fill: var(--muted); font-size: 11px; }
.axis line.grid { stroke: var(--rule); stroke-width: 1; }
.axis line.zero { stroke: var(--muted); stroke-width: 1; }
line.now { stroke: var(--accent); stroke-dasharray: 3 4; }
line.cursor { stroke: var(--muted); }
rect.band { fill: var(--surface-2); }
.chart-tip {
  position: absolute; pointer-events: none; z-index: 2; min-width: 170px;
  background: var(--surface-2); border: 1px solid var(--rule); border-radius: 8px; padding: 8px 10px;
  box-shadow: var(--shadow); font-size: 0.82rem;
}
.chart-tip strong { display: block; margin-bottom: 4px; }
.chart-tip div { display: flex; align-items: center; gap: 6px; }
.chart-tip b { margin-left: auto; padding-left: 12px; font-weight: 600; }
.chart-tip .tip-total { border-top: 1px solid var(--rule); margin-top: 4px; padding-top: 4px; }

/* ---------- custom period ---------- */
.range-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; margin: 14px 28px 0;
  padding: 10px 14px; background: var(--surface); border: 1px solid var(--rule); border-radius: 8px; }
.range-bar label { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 0.9rem; }
.range-bar input { background: var(--surface-2); color: var(--ink); border: 1px solid var(--rule); border-radius: 6px; padding: 5px 8px; font: inherit; }
.range-status { color: var(--muted); font-size: 0.88rem; }
.embedded .range-bar { margin: 8px 4px 0; }

/* ---------- table ---------- */
.table-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 10px; }
.table-head h2 { margin-bottom: 0; }
.table-wrap { margin-top: 16px; }
.table-wrap h2 { margin-bottom: 10px; }
.scroll { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 0.92rem; }
th, td { padding: 9px 12px; text-align: right; border-bottom: 1px solid var(--rule); white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
thead th { color: var(--muted); font-weight: 500; font-size: 0.85rem; }
tbody tr:hover { background: var(--surface-2); }
tfoot td { font-weight: 600; border-bottom: 0; }

/* ---------- embedded in Caspio Flex ---------- */
.embedded, .embedded body { background: var(--bg); }
.embedded .topbar { position: static; background: transparent; backdrop-filter: none; padding: 6px 4px 12px; }
.embedded main { padding: 0 4px 8px; max-width: none; }
.embedded .notice, .embedded .errors { margin: 12px 4px 0; }
.embedded.no-brand .brand { visibility: hidden; }

@media (max-width: 900px) {
  .topbar { grid-template-columns: 1fr; justify-items: start; padding: 12px 16px; }
  .sync { justify-self: start; }
  main { padding: 8px 16px 32px; }
  .hero { grid-template-columns: 1fr; gap: 24px; }
  .ledger-row { grid-template-columns: 74px 1fr 70px; }
  .ledger-key { padding-left: 0; }
  .chart-grid { grid-template-columns: 1fr; }
  .site { border-left: 0; }
  .embedded.no-brand .brand { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .ledger-seg { transition: none; }
  .pulse { animation: none; }
}
