:root {
  --bg: #111217;
  --panel: #181b1f;
  --card: #1f2228;
  --card-hi: #282c34;
  --line: #30343c;
  --ink: #e8eaf0;
  --text: #ced3dd;
  --muted: #a5acba;
  --faint: #6e7684;
  --accent: #3d71d9;
  --blue: #6e9fff;
  --solar: #f5b700;
  --battery: #3ccb7f;
  --grid: #4ea1ff;
  --home: #ff8a4c;
  --soc: #b48cff;
  --red: #f2495c;
  --amber: #ff9830;
  --teal: #57c7c8;
  --radius: 12px;
  --cell: 74px;
}
[data-theme="light"] {
  --bg: #f3f5f9;
  --panel: #ffffff;
  --card: #ffffff;
  --card-hi: #eef1f6;
  --line: #dde2ea;
  --ink: #141821;
  --text: #2b3240;
  --muted: #5d6676;
  --faint: #8c95a4;
  --solar: #d99a00;
  --battery: #1f9d5c;
  --grid: #2c7be5;
  --home: #e8642a;
  --soc: #8a5cf0;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select { font: inherit; }
.boot { padding: 48px; color: var(--muted); }

/* ── top bar ── */
.top {
  display: flex; align-items: center; gap: 18px;
  padding: 10px 20px; background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); font-weight: 700; font-size: 17px; white-space: nowrap; }
.brand img { width: 26px; height: 26px; border-radius: 6px; }
.nav { display: flex; gap: 4px; flex-wrap: wrap; }
.nav a { color: var(--muted); padding: 6px 12px; border-radius: 8px; }
.nav a:hover { background: var(--card); color: var(--ink); text-decoration: none; }
.nav a.on { background: #263452; color: #fff; }
[data-theme="light"] .nav a.on { background: #dfe8fb; color: #1a3d8f; }
.spacer { flex: 1; }
.who { color: var(--muted); font-size: 13px; }
.btn {
  background: var(--card-hi); color: var(--ink); border: 1px solid var(--line);
  border-radius: 8px; padding: 6px 12px; cursor: pointer;
}
.btn:hover { border-color: var(--faint); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }

main { padding: 20px; max-width: 1600px; margin: 0 auto; }
h1 { font-size: 22px; color: var(--ink); margin: 4px 0 16px; font-weight: 650; }
h2 { font-size: 15px; color: var(--ink); margin: 0 0 10px; font-weight: 650; }
.sub { color: var(--muted); margin: -10px 0 16px; }
.err { color: var(--red); }
.muted { color: var(--muted); }

/* ── login ── */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 20px; }
.login {
  width: 100%; max-width: 360px; background: var(--card); border: 1px solid var(--line);
  border-radius: 16px; padding: 28px;
}
.login .brand { margin-bottom: 18px; font-size: 20px; }
.login label { display: block; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; margin: 12px 0 4px; }
.login input {
  width: 100%; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--bg); color: var(--ink); font-size: 15px;
}
.login input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.login .btn { width: 100%; margin-top: 18px; padding: 10px; }
.login .err { margin-top: 12px; min-height: 20px; }

/* ── cards & lists ── */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; position: relative; overflow: hidden;
}
a.card { color: inherit; display: block; }
a.card:hover { border-color: var(--faint); text-decoration: none; }
.card .title { color: var(--ink); font-weight: 650; font-size: 15px; }
.card .meta { color: var(--muted); font-size: 12.5px; margin-top: 4px; }
.pill { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; padding: 2px 9px; border-radius: 20px; border: 1px solid var(--line); color: var(--text); }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }
.s-online { background: var(--battery); }
.s-connecting { background: var(--amber); }
.s-error { background: var(--red); }
.s-disabled { background: var(--faint); }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-top: 12px; }
.kpi .l { color: var(--muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; }
.kpi .v { color: var(--ink); font-size: 19px; font-weight: 700; }

table.tbl { width: 100%; border-collapse: collapse; }
table.tbl th { text-align: left; color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; padding: 6px 8px; border-bottom: 1px solid var(--line); }
table.tbl td { padding: 7px 8px; border-bottom: 1px solid var(--line); color: var(--text); }
table.tbl tr:last-child td { border-bottom: 0; }
table.tbl td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.tbl th.num { text-align: right; }

/* ── dashboard grid ── */
.dash-head { display: flex; align-items: baseline; gap: 14px; margin-bottom: 14px; flex-wrap: wrap; }
.dash-head h1 { margin: 0; }
.updated { color: var(--faint); font-size: 12px; }
.grid {
  display: grid; grid-template-columns: repeat(12, 1fr);
  grid-auto-rows: var(--cell); gap: 12px;
}
.w {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; display: flex; flex-direction: column; min-width: 0; min-height: 0; overflow: hidden;
  position: relative;
}
.w::before { content: ""; position: absolute; left: 0; top: 12px; bottom: 12px; width: 3px; border-radius: 2px; background: var(--wc, transparent); }
.w-title { color: var(--muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; font-weight: 600; display: flex; gap: 8px; align-items: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.w-title .p { color: var(--faint); text-transform: none; letter-spacing: 0; font-weight: 400; }
.w-body { flex: 1; min-height: 0; position: relative; display: flex; flex-direction: column; justify-content: center; }
.w-err { color: var(--red); font-size: 12px; }
.big { font-size: clamp(22px, 3.2vw, 40px); font-weight: 700; color: var(--ink); line-height: 1.1; font-variant-numeric: tabular-nums; }
.big small { font-size: .5em; color: var(--muted); font-weight: 500; margin-left: 4px; }
.val-sub { color: var(--muted); font-size: 12.5px; margin-top: 4px; }
.multi { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 8px; }
.multi .l { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.multi .v { color: var(--ink); font-size: 20px; font-weight: 700; }
svg { display: block; }
.chart { width: 100%; height: 100%; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12px; color: var(--muted); margin-top: 4px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.tip {
  position: absolute; pointer-events: none; background: rgba(15,18,24,.94); color: #e8eaf0;
  border: 1px solid #464e5e; border-radius: 8px; padding: 6px 9px; font-size: 12px; white-space: nowrap;
  font-variant-numeric: tabular-nums; z-index: 3; display: none;
}
.text-w { color: var(--text); white-space: pre-wrap; overflow: auto; }
.text-w h3 { margin: 0 0 4px; color: var(--ink); font-size: 18px; }
.flow-line { fill: none; stroke-width: 3; stroke-linecap: round; }
.flow-line.on { stroke-dasharray: 6 9; animation: flow 1.1s linear infinite; }
.flow-line.rev { animation-direction: reverse; }
@keyframes flow { to { stroke-dashoffset: -30; } }
.list-scroll { overflow: auto; flex: 1; }

/* embedded (iframe) */
body.embed main { padding: 12px; max-width: none; }
body.embed .dash-head { margin-bottom: 10px; }
body.embed.single main { padding: 0; }
body.embed.single .grid { display: block; height: 100vh; }
body.embed.single .w { height: 100vh; border-radius: 0; border: 0; }

@media (max-width: 900px) {
  /* One widget per row on small screens, in reading order. */
  .grid { grid-template-columns: 1fr; grid-auto-rows: 62px; }
  .grid .w { grid-column: 1 / -1 !important; grid-row: auto / span var(--h, 3) !important; }
  .grid .w.w-chart { grid-row: auto / span 5 !important; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 560px) {
  main { padding: 12px; }
  .top { padding: 8px 12px; gap: 10px; }
  .who { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .flow-line.on { animation: none; }
}
