:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f2f3f5);
  --text: var(--tg-theme-text-color, #1c1c1e);
  --muted: var(--tg-theme-hint-color, #8a8a8e);
  --accent: var(--tg-theme-button-color, #2b8be6);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --grid: rgba(128, 128, 128, 0.18);
  --c-out: #f08c2e;
  --c-in: #2fb36b;
  --c-soc: #2b8be6;
  --c-solar: #e6c229;
  --c-off: rgba(220, 70, 70, 0.16);
  --c-off-solid: #dc4646;
  --c-temp: #e5533d;
  --c-hum: #3a8fd8;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg2);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

main { max-width: 760px; margin: 0 auto; padding: 12px 12px 32px; }

.top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin: 4px 4px 12px; }
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 15px; margin: 0 0 8px; font-weight: 600; }
.muted { color: var(--muted); font-size: 13px; }
.hidden { display: none !important; }

.error { background: #fde8e8; color: #9b1c1c; border-radius: 12px; padding: 12px; margin-bottom: 12px; }

.cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 12px; }
@media (min-width: 620px) { .cards { grid-template-columns: repeat(4, 1fr); } }
.card { background: var(--bg); border-radius: 14px; padding: 12px; min-height: 96px; }
.card-label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.card-value { font-size: 24px; font-weight: 700; margin: 2px 0 4px; white-space: nowrap; }
.card-sub { color: var(--muted); font-size: 12px; }
.card.off .card-value { color: var(--c-off-solid); }
.card.on .card-value { color: var(--c-in); }

.meter { height: 6px; background: var(--grid); border-radius: 3px; overflow: hidden; margin-bottom: 4px; }
.meter-fill { height: 100%; width: 0; background: var(--c-in); border-radius: 3px; transition: width .4s; }
.meter-fill.low { background: var(--c-off-solid); }
.meter-fill.mid { background: var(--c-out); }

.tabs { display: flex; background: var(--bg); border-radius: 12px; padding: 4px; gap: 4px; margin-bottom: 12px;
        position: sticky; top: 0; z-index: 5; }
.tabs button { flex: 1; border: 0; background: transparent; color: var(--text); padding: 8px 0; border-radius: 9px;
               font: inherit; font-weight: 600; cursor: pointer; }
.tabs button.active { background: var(--accent); color: var(--accent-text); }

.panel { background: var(--bg); border-radius: 14px; padding: 12px; margin-bottom: 12px; }
.panel h2 + .chart, .panel .legend + .chart { margin-top: 4px; }
.chart { width: 100%; height: 180px; position: relative; touch-action: pan-y; }
.chart.timeline { height: 56px; }
.chart + h2 { margin-top: 16px; }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart .empty { display: flex; height: 100%; align-items: center; justify-content: center; color: var(--muted); font-size: 13px; }

.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12px; color: var(--muted); }
.legend span::before { content: ""; display: inline-block; width: 10px; height: 10px; border-radius: 3px;
                       margin-right: 5px; vertical-align: -1px; background: var(--dot); }

svg .axis { fill: var(--muted); font-size: 10px; }
svg .gridline { stroke: var(--grid); stroke-width: 1; }
svg .cursor { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }

.tooltip { position: fixed; z-index: 20; pointer-events: none; background: var(--text); color: var(--bg);
           border-radius: 8px; padding: 6px 8px; font-size: 12px; line-height: 1.35; white-space: nowrap;
           box-shadow: 0 2px 10px rgba(0,0,0,.2); }

.list { list-style: none; padding: 0; margin: 8px 0 0; }
.list li { display: flex; justify-content: space-between; gap: 8px; padding: 7px 0; border-top: 1px solid var(--grid); font-size: 14px; }
.list li .dur { font-weight: 600; white-space: nowrap; }
.list li.ongoing .dur { color: var(--c-off-solid); }

table.stats { width: 100%; border-collapse: collapse; font-size: 13px; table-layout: fixed; }
table.stats td .muted { font-size: 11px; }
table.stats th, table.stats td { padding: 6px 4px; text-align: right; border-top: 1px solid var(--grid); white-space: nowrap; }
table.stats th:first-child, table.stats td:first-child { text-align: left; }
table.stats thead th { border-top: 0; color: var(--muted); font-weight: 500; font-size: 12px; }

.facts { display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; margin: 0; font-size: 14px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; text-align: right; font-weight: 600; }

footer { text-align: center; margin-top: 8px; }
svg .tl-on { fill: var(--c-in); fill-opacity: .35; }
svg .tl-off { fill: var(--c-off-solid); }
.chart.tall { height: 230px; }
.small { font-size: 11px; margin-top: 8px; }
.subhead { font-size: 13px; margin: 12px 0 4px; color: var(--muted); font-weight: 600; }
.grid-now { display: flex; align-items: baseline; gap: 10px; margin: 2px 0 4px; }
.grid-volt { font-size: 28px; font-weight: 700; }
.pill { font-size: 12px; padding: 2px 8px; border-radius: 10px; background: var(--grid); color: var(--muted); }
.pill.ok { background: rgba(47,179,107,.15); color: #1f8a50; }
.pill.warn { background: rgba(240,140,46,.18); color: #b5641a; }
.pill.bad { background: rgba(220,70,70,.15); color: #c23a3a; }
