:root {
  color-scheme: light;
  --page: #f9f9f7; --surface: #fcfcfb; --surface-2: #f0efec;
  --ink: #0b0b0b; --ink-2: #52514e; --muted: #898781;
  --grid: #e1e0d9; --axis: #c3c2b7; --border: rgba(11,11,11,0.10);
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a;
  --good: #0ca30c; --warn: #fab219; --serious: #ec835a; --crit: #d03b3b;
  --accent: #2a78d6; --accent-ink: #fff;
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --surface-2: #262624;
    --ink: #f0efec; --ink-2: #c3c2b7; --muted: #898781;
    --grid: #2c2c2a; --axis: #383835; --border: rgba(255,255,255,0.10);
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --accent: #3987e5;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #262624;
  --ink: #f0efec; --ink-2: #c3c2b7; --muted: #898781;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255,255,255,0.10);
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --accent: #3987e5;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--page); color: var(--ink); }
body { font: 15px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif; min-height: 100vh; overflow-x: hidden; }
.hidden { display: none !important; }
h1 { font-size: 26px; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 15px; margin: 0 0 12px; color: var(--ink-2); font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; word-break: break-word; }
.lead { font-size: 17px; font-weight: 600; margin: 12px 0 4px; }
.err { color: var(--crit); min-height: 1.4em; }

.top { display: flex; justify-content: space-between; align-items: flex-end; padding: calc(var(--safe-t) + 16px) 16px 8px; }
main { padding: 0 16px calc(84px + var(--safe-b)); max-width: 720px; margin: 0 auto; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 16px; margin-bottom: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 12px; }
.grid2 .card { margin: 0; }
.row2, .row3 { display: grid; gap: 12px 16px; }
.row2 { grid-template-columns: 1fr 1fr; }
.row3 { grid-template-columns: 1fr 1fr 1fr; }
.kv { display: flex; flex-direction: column; min-width: 0; }
.kv span, .tile span { color: var(--muted); font-size: 12px; }
.kv b { font-size: 17px; font-weight: 600; overflow-wrap: anywhere; }
.tile { display: flex; flex-direction: column; gap: 2px; min-height: 92px; }
.tile b { font-size: 18px; line-height: 1.25; }
.tile small { font-size: 12px; }

.hero { display: flex; align-items: center; gap: 12px; }
#batt { width: 42%; max-width: 170px; flex: none; overflow: visible; }
.soc b { display: block; font-size: 40px; font-weight: 700; line-height: 1.05; letter-spacing: -0.02em; }
.soc span { font-size: 14px; display: block; }
#batt .shell { fill: none; stroke: var(--ink-2); stroke-width: 5; }
#batt .cap { fill: var(--ink-2); }
#batt .empty { fill: var(--surface-2); }
#batt .liquid { fill: var(--s1); }
#batt .liquid.low { fill: var(--warn); }
#batt .level { transition: transform 1.2s cubic-bezier(.3,.7,.3,1); }
#batt .wave { animation: wave 4s linear infinite; }
#batt .chev { fill: none; stroke: rgba(255,255,255,.55); stroke-width: 5; stroke-linecap: round; stroke-linejoin: round; opacity: 0; transition: opacity .4s; }
#batt.charging .chev { opacity: 1; animation: up 1.6s linear infinite; }
#batt.discharging .chev { opacity: 1; animation: up 1.6s linear infinite reverse; }
#batt.charging .wave, #batt.discharging .wave { animation-duration: 1.8s; }
#batt .minline { stroke: var(--ink); stroke-width: 2; stroke-dasharray: 6 5; opacity: .8; transition: transform 1.2s; }
#batt .mintext { fill: var(--ink-2); font-size: 13px; transition: transform 1.2s; }
@keyframes wave { from { transform: translateX(0); } to { transform: translateX(-100px); } }
@keyframes up { from { transform: translateY(40px); } to { transform: translateY(0); } }
@media (prefers-reduced-motion: reduce) { #batt .wave, #batt .chev { animation: none !important; } #batt .level { transition: none; } }
.flow svg { display: block; width: 100%; height: auto; overflow: visible; }
.flow .disc { fill: var(--surface-2); }
.flow .icon { fill: none; stroke: var(--ink); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.flow .fval { fill: var(--ink); font-size: 15px; font-weight: 700; font-variant-numeric: tabular-nums; }
.flow .flab { fill: var(--muted); font-size: 12px; }
.flow .rail { stroke: var(--grid); stroke-width: 4; stroke-linecap: round; }
.flow .dots { stroke-width: 5; stroke-linecap: round; stroke-dasharray: 0 14; animation: flow 1.5s linear infinite; }
.flow .link.rev .dots { animation-direction: reverse; }
.flow .link.idle .dots { display: none; }
@keyframes flow { from { stroke-dashoffset: 14; } to { stroke-dashoffset: 0; } }
@media (prefers-reduced-motion: reduce) { .flow .dots { animation: none; } }
#flowSub { margin: 12px 0 0; }

.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; margin-bottom: 12px; }
.seg button { flex: 1; border: 0; background: none; color: var(--ink-2); padding: 8px; border-radius: 8px; font: inherit; }
.seg button.on { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.15); }

.chart { position: relative; width: 100%; touch-action: pan-y; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .ref { stroke: var(--ink-2); stroke-width: 1.5; stroke-dasharray: 5 4; fill: none; }
.chart .reflabel { fill: var(--ink-2); font-size: 11px; }
.chart .cross { stroke: var(--ink-2); stroke-width: 1; }
.chart.loading { opacity: .5; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 8px; font-size: 12px; color: var(--ink-2); }
.legend i { display: inline-block; width: 14px; height: 2px; vertical-align: middle; margin-right: 6px; border-radius: 1px; }
.tip { position: fixed; z-index: 30; pointer-events: none; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; font-size: 12px; box-shadow: 0 6px 20px rgba(0,0,0,.25); min-width: 120px; }
.tip .t { color: var(--muted); margin-bottom: 4px; }
.tip .r { display: flex; align-items: center; gap: 6px; }
.tip .r i { width: 12px; height: 2px; border-radius: 1px; }
.tip .r b { font-variant-numeric: tabular-nums; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); font-size: 12px; color: var(--ink); }
.chip::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.chip.good::before { background: var(--good); } .chip.warn::before { background: var(--warn); } .chip.crit::before { background: var(--crit); }
details { margin-top: 12px; } summary { color: var(--accent); font-size: 13px; cursor: pointer; }

.bar { position: relative; height: 28px; background: var(--surface-2); border-radius: 8px; margin: 4px 0 26px; }
.bar .fill { position: absolute; inset: 0 auto 0 0; border-radius: 8px; background: var(--s1); transition: width .6s; }
.bar .mark { position: absolute; top: -4px; bottom: -4px; width: 2px; background: var(--ink-2); }
.bar .mark span { position: absolute; top: 34px; left: 50%; transform: translateX(-50%); font-size: 11px; color: var(--muted); white-space: nowrap; }
.bar .val { position: absolute; right: 8px; top: 50%; transform: translateY(-50%); font-size: 13px; font-weight: 600; }

.phases { display: flex; list-style: none; padding: 0; margin: 0; gap: 4px; }
.phases li { flex: 1; text-align: center; font-size: 11px; color: var(--muted); padding: 8px 2px; border-radius: 8px; background: var(--surface-2); }
.phases li.done { color: var(--ink-2); }
.phases li.on { background: var(--accent); color: var(--accent-ink); font-weight: 700; }

.actions { display: grid; gap: 8px; }
button { font: inherit; cursor: pointer; }
.primary, .act { border: 0; border-radius: 12px; padding: 14px; font-weight: 600; background: var(--accent); color: var(--accent-ink); }
.act { background: var(--surface-2); color: var(--ink); text-align: left; }
.act.danger { color: var(--crit); }
.act:disabled { opacity: .45; }
.ghost { border: 1px solid var(--border); background: none; color: var(--ink); border-radius: 10px; padding: 8px 12px; }
.row-btn { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.row-btn button { flex: 1; }

.tabs { position: fixed; left: 0; right: 0; bottom: 0; display: flex; background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-top: 1px solid var(--border); padding: 6px 4px calc(6px + var(--safe-b)); z-index: 10; }
.tabs button { flex: 1; border: 0; background: none; color: var(--muted); font-size: 11px; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px; }
.tabs svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabs button.on { color: var(--accent); }

.screen { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: 100%; max-width: 360px; display: grid; gap: 12px; text-align: center; }
.login label { display: grid; gap: 4px; text-align: left; font-size: 13px; color: var(--ink-2); }
.login input { font: inherit; font-size: 16px; padding: 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); color: var(--ink); }
.logo { width: 56px; margin: 0 auto; fill: none; stroke: var(--accent); stroke-width: 3; }
.logo .fill { fill: var(--accent); stroke: none; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.5); display: grid; place-items: end center; padding: 16px 16px calc(16px + var(--safe-b)); z-index: 40; }
.modal .card { width: 100%; max-width: 480px; margin: 0; }
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(90px + var(--safe-b)); background: var(--ink); color: var(--page); padding: 10px 16px; border-radius: 12px; font-size: 14px; z-index: 50; max-width: 90vw; }

@media (max-width: 380px) { .row3 { grid-template-columns: 1fr 1fr; } .hero { flex-direction: column; } #batt { width: 50%; } }

/* PV */
.pv-total { display: flex; flex-direction: column; margin-bottom: 12px; }
.pv-total b { font-size: 34px; font-weight: 700; line-height: 1.1; }
.pv-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.pv-table th { text-align: right; font-size: 12px; color: var(--muted); font-weight: 500; padding: 4px 0; }
.pv-table th:first-child, .pv-table td:first-child { text-align: left; }
.pv-table td { text-align: right; padding: 8px 0 8px 8px; border-top: 1px solid var(--border); font-weight: 600; white-space: nowrap; font-size: 14px; }
.pv-table td:first-child { white-space: nowrap; padding-left: 0; }
.pv-table td:first-child { font-weight: 500; }
.pv-table small { font-weight: 400; display: block; margin-left: 18px; font-size: 11px; }
.key { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 8px; vertical-align: baseline; }
.k1 { background: var(--s1); } .k2 { background: var(--s2); } .k3 { background: var(--s3); }
.share { display: flex; gap: 2px; height: 10px; margin-top: 12px; border-radius: 5px; overflow: hidden; background: var(--surface-2); }
.share i { display: block; height: 100%; transition: flex-grow .6s; }
.tabs button { padding: 4px 2px; }
.pv-table tr.tot td { font-weight: 700; }
.legend i.dash { background: none !important; border-top: 2px dashed currentColor; height: 0; }
.legend i.hatch { width: 12px; height: 8px; border-radius: 2px; background: repeating-linear-gradient(45deg, var(--ink-2) 0 2px, transparent 2px 4px) !important; }
