:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #6f6d68;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --ring: rgba(11, 11, 11, 0.10);
  --accent: #1c5cab;
  --accent-ink: #ffffff;
  --good: #0ca30c;
  --off: #898781;
  --notice: #e7f0fb;
  /* series, fixed per entity (categorical slots 1-4) */
  --s-grid: #2a78d6;
  --s-pv: #eb6834;
  --s-battery: #1baf7a;
  --s-load: #eda100;
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --ink: #ffffff; --ink-2: #c3c2b7; --muted: #9a988f;
    --grid: #2c2c2a; --axis: #383835; --ring: rgba(255, 255, 255, 0.10);
    --accent: #3987e5; --accent-ink: #0b0b0b; --notice: #16263b;
    --s-grid: #3987e5; --s-pv: #d95926; --s-battery: #199e70; --s-load: #c98500;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --ink: #ffffff; --ink-2: #c3c2b7; --muted: #9a988f;
  --grid: #2c2c2a; --axis: #383835; --ring: rgba(255, 255, 255, 0.10);
  --accent: #3987e5; --accent-ink: #0b0b0b; --notice: #16263b;
  --s-grid: #3987e5; --s-pv: #d95926; --s-battery: #199e70; --s-load: #c98500;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--page); color: var(--ink); }
a { color: var(--accent); }
h1 { font-size: 1.6rem; margin: 0 0 .2rem; letter-spacing: -0.01em; }
h2 { font-size: 1.05rem; margin: 0 0 .6rem; }
.muted { color: var(--ink-2); margin: 0; }
.small { font-size: .85rem; }
.mt { margin-top: 1.4rem; }
.sub { color: var(--muted); font-size: .8rem; }
.nowrap { white-space: nowrap; }

header.top {
  display: flex; align-items: center; gap: 1.5rem; flex-wrap: wrap;
  padding: .7rem max(16px, calc((100vw - 1120px) / 2));
  background: var(--surface); border-bottom: 1px solid var(--ring);
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 650; color: var(--ink); text-decoration: none; }
.brand-sub { color: var(--muted); font-weight: 500; }
header nav { display: flex; gap: 1rem; flex: 1; }
header nav a { color: var(--ink-2); text-decoration: none; padding: .2rem 0; border-bottom: 2px solid transparent; }
header nav a[aria-current] { color: var(--ink); border-color: var(--accent); }
.who { display: flex; align-items: center; gap: .8rem; color: var(--ink-2); font-size: .9rem; margin: 0; }
.who span { max-width: 22ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

main { max-width: 1120px; margin: 0 auto; padding: 1.6rem 16px 3rem; }
.head { margin-bottom: 1.2rem; }
.crumb { margin: 0 0 .3rem; font-size: .9rem; }
.card { background: var(--surface); border: 1px solid var(--ring); border-radius: 12px; padding: 1.2rem 1.3rem; margin-bottom: 1.2rem; }
.card.flush { padding: 0; overflow-x: auto; }
.card.narrow, .login { max-width: 460px; margin: 4rem auto; }
.login { text-align: center; padding: 2.2rem; }
.login h1 { margin-top: .8rem; }
.login .button { margin-top: 1.4rem; }
.empty { text-align: center; padding: 2.5rem; }

button, .button {
  font: inherit; border: 1px solid var(--ring); background: var(--surface); color: var(--ink);
  border-radius: 8px; padding: .45rem .9rem; cursor: pointer; text-decoration: none; display: inline-block;
}
.button.primary, button[type=submit]:not(.link) { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
button.link { background: none; border: none; color: var(--accent); padding: 0; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input, select { font: inherit; color: var(--ink); background: var(--page); border: 1px solid var(--axis); border-radius: 8px; padding: .4rem .6rem; }

table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-weight: 600; font-size: .8rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
th, td { padding: .7rem 1rem; border-bottom: 1px solid var(--grid); vertical-align: top; }
tbody tr:last-child td { border-bottom: none; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.sites tbody tr:hover { background: var(--page); }
.rowlink { font-weight: 600; color: var(--ink); text-decoration: none; }
.rowlink:hover { text-decoration: underline; }
table.plain th, table.plain td { padding: .55rem .6rem; }
table.log td { font-size: .88rem; }
.tag { background: var(--notice); color: var(--ink-2); border-radius: 4px; padding: 0 .35rem; }

.status { display: inline-flex; align-items: center; gap: .4rem; font-weight: 600; }
.status i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.status.ok i { background: var(--good); }
.status.off { color: var(--ink-2); }
.status.off i { background: var(--off); }

.notice { background: var(--notice); border-radius: 8px; padding: .7rem 1rem; }
form.inline { display: flex; gap: .5rem; flex-wrap: wrap; margin: 0; }
form.inline input { flex: 1; min-width: 12rem; }
form.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: .8rem; margin-top: .8rem; align-items: end; }
form.grid label { display: grid; gap: .25rem; font-size: .9rem; color: var(--ink-2); }
form.grid p, form.grid button { grid-column: 1 / -1; justify-self: start; }
summary { cursor: pointer; color: var(--accent); }

.flow { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: .5rem; }
.tile { background: var(--surface); border: 1px solid var(--ring); border-radius: 12px; padding: 1rem 1.1rem; border-top: 4px solid var(--c); }
#t-grid { --c: var(--s-grid); } #t-pv { --c: var(--s-pv); } #t-bat { --c: var(--s-battery); } #t-load { --c: var(--s-load); }
.tile .label { color: var(--ink-2); font-weight: 600; display: flex; justify-content: space-between; }
.tile .value { font-size: 1.9rem; font-weight: 650; margin: .2rem 0 0; letter-spacing: -0.01em; }
.tile .dir { color: var(--muted); font-size: .9rem; min-height: 1.3em; }
.soc { color: var(--ink); }
.socbar { height: 6px; border-radius: 3px; background: var(--grid); margin-top: .5rem; overflow: hidden; }
.socbar span { display: block; height: 100%; width: 0; background: var(--s-battery); border-radius: 3px; transition: width .6s; }
.stale .value { color: var(--muted); }

.chartcard { margin-top: 1.2rem; }
.charthead { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; }
.seg { display: inline-flex; border: 1px solid var(--ring); border-radius: 8px; overflow: hidden; }
.seg button { border: none; border-radius: 0; background: none; color: var(--ink-2); padding: .3rem .8rem; }
.seg button[aria-pressed="true"] { background: var(--page); color: var(--ink); font-weight: 600; }
.legend { display: flex; gap: 1.2rem; flex-wrap: wrap; margin: .4rem 0 .2rem; font-size: .9rem; color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: .4rem; }
.legend i { width: 14px; height: 3px; border-radius: 2px; display: inline-block; }
.chart { position: relative; height: 280px; }
.chart.small { height: 140px; }
.chart svg { width: 100%; height: 100%; display: block; overflow: visible; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .zero { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .cross { stroke: var(--axis); stroke-width: 1; }
.chart .dot { stroke: var(--surface); stroke-width: 2; }
.chart .nodata { fill: var(--muted); font-size: 13px; }
.tip { position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--ring); border-radius: 8px;
  padding: .5rem .7rem; font-size: .85rem; box-shadow: 0 4px 14px rgba(0,0,0,.12); white-space: nowrap; z-index: 2; }
.tip b { display: block; margin-bottom: .2rem; }
.tip div { display: flex; justify-content: space-between; gap: 1rem; font-variant-numeric: tabular-nums; }
.tip i { width: 10px; height: 3px; display: inline-block; border-radius: 2px; margin-right: .35rem; vertical-align: middle; }
.tableview { margin-top: 1rem; }
.tableview table { margin-top: .6rem; font-size: .88rem; }
.tableview th, .tableview td { padding: .35rem .6rem; }

@media (max-width: 760px) {
  .flow { grid-template-columns: repeat(2, 1fr); }
  .tile .value { font-size: 1.5rem; }
  header nav { order: 3; flex-basis: 100%; }
  table.sites th:nth-child(4), table.sites td:nth-child(4) { display: none; }
}

/* ---- energy-flow diagram ------------------------------------------------ */
.flowcard { padding: .6rem .6rem .8rem; }
.flowviz { display: block; width: 100%; max-width: 640px; height: auto; margin: 0 auto; overflow: visible; }
.flowviz .base { stroke: var(--grid); stroke-width: 2; }
.flowviz .dots { stroke-width: 6; stroke-linecap: round; stroke-dasharray: 0 16; opacity: 0; }
.flowviz .dots.on { opacity: 1; animation: flow var(--dur, 1.5s) linear infinite; }
.flowviz .dots.on.rev { animation-direction: reverse; }
@keyframes flow { from { stroke-dashoffset: 0; } to { stroke-dashoffset: -16; } }
.flowviz .hub { fill: var(--axis); }
.flowviz .disc { fill: var(--page); }
.flowviz .ring { fill: none; stroke: var(--grid); stroke-width: 6; transition: stroke .4s; }
.flowviz .node.on .ring { stroke: var(--c); }
.flowviz .node[data-node="grid"] { --c: var(--s-grid); }
.flowviz .node[data-node="pv"] { --c: var(--s-pv); }
.flowviz .node[data-node="battery"] { --c: var(--s-battery); }
.flowviz .node[data-node="load"] { --c: var(--s-load); }
.flowviz .soc-ring { fill: none; stroke: var(--s-battery); stroke-width: 6; stroke-dasharray: 0 100; stroke-linecap: round; transition: stroke-dasharray .8s; }
.flowviz .node[data-node="battery"] .ring { stroke: var(--grid); }
.flowviz .icon { fill: none; stroke: var(--ink-2); stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.flowviz .node.on .icon { stroke: var(--c); }
.flowviz .icon .fill { fill: var(--s-battery); stroke: none; }
.flowviz .val { fill: var(--ink); font-size: 22px; font-weight: 650; font-variant-numeric: tabular-nums; }
.flowviz .cap { fill: var(--muted); font-size: 14px; }
.flowviz .soc { fill: var(--ink); font-size: 15px; font-weight: 650; }
.flowviz.stale .val { fill: var(--muted); }
.flownote { text-align: center; color: var(--muted); font-size: .85rem; margin: .2rem 0 0; }
.live-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--good); margin-right: .4rem; vertical-align: middle; }
.flowviz.stale ~ .flownote .live-dot { background: var(--off); }
@media (prefers-reduced-motion: reduce) { .flowviz .dots.on { animation: none; } }
@media (max-width: 560px) {
  .flowviz .val { font-size: 32px; }
  .flowviz .cap { font-size: 22px; }
  .flowviz .soc { font-size: 20px; }
}
