:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f2f1ed;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --good: #006300;
  --bad: #d03b3b;
  --warn: #8a5a00;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 4px 16px rgba(0, 0, 0, .04);
  /* Datenreihen – feste Zuordnung je Größe */
  --s-direct: #2a78d6;
  --s-batt: #1baf7a;
  --s-batt-out: #6250d6;
  --s-feed: #eda100;
  --s-grid: #eb6834;
  --s-base: #6250d6;
  --s-red: #e34948;
  --s-1: #2a78d6; --s-2: #eb6834; --s-3: #1baf7a; --s-4: #eda100;
  --s-5: #e87ba4; --s-6: #008300; --s-7: #6250d6; --s-8: #e34948;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321;
    --ink: #f0efec; --ink-2: #c3c2b7; --muted: #898781; --grid: #2c2c2a; --axis: #383835;
    --border: rgba(255, 255, 255, 0.10); --accent: #3987e5; --good: #0ca30c; --bad: #e66767; --warn: #fab219;
    --shadow: none;
    --s-direct: #3987e5; --s-batt: #199e70; --s-batt-out: #9085e9; --s-feed: #c98500; --s-grid: #d95926;
    --s-base: #9085e9; --s-red: #e66767;
    --s-1: #3987e5; --s-2: #d95926; --s-3: #199e70; --s-4: #c98500;
    --s-5: #d55181; --s-6: #008300; --s-7: #9085e9; --s-8: #e66767;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321;
  --ink: #f0efec; --ink-2: #c3c2b7; --muted: #898781; --grid: #2c2c2a; --axis: #383835;
  --border: rgba(255, 255, 255, 0.10); --accent: #3987e5; --good: #0ca30c; --bad: #e66767; --warn: #fab219;
  --shadow: none;
  --s-direct: #3987e5; --s-batt: #199e70; --s-batt-out: #9085e9; --s-feed: #c98500; --s-grid: #d95926;
  --s-base: #9085e9; --s-red: #e66767;
  --s-1: #3987e5; --s-2: #d95926; --s-3: #199e70; --s-4: #c98500;
  --s-5: #d55181; --s-6: #008300; --s-7: #9085e9; --s-8: #e66767;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page); color: var(--ink);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 1.05rem; margin: 0 0 .25rem; }
h3 { font-size: .95rem; margin: 0 0 .5rem; color: var(--ink-2); font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.num, td.n, th.n { font-variant-numeric: tabular-nums; text-align: right; }
.hidden { display: none !important; }

/* ---------- Kopf & Navigation */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--border);
}
.topbar-inner { max-width: 1320px; margin: 0 auto; padding: 0 16px; display: flex; align-items: center; gap: 16px; height: 56px; }
.brand { font-weight: 700; display: flex; align-items: center; gap: 8px; color: var(--ink); white-space: nowrap; }
.brand svg { width: 22px; height: 22px; color: var(--s-feed); }
.nav { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; flex: 1; }
.nav::-webkit-scrollbar { display: none; }
.nav a {
  color: var(--ink-2); padding: 6px 10px; border-radius: 8px; white-space: nowrap; font-size: .92rem;
  display: flex; align-items: center; gap: 6px;
}
.nav a:hover { background: var(--surface-2); text-decoration: none; color: var(--ink); }
.nav a.active { background: var(--surface); color: var(--ink); box-shadow: inset 0 0 0 1px var(--border); font-weight: 600; }
.nav .dot { width: 8px; height: 8px; border-radius: 50%; }
.top-actions { display: flex; gap: 6px; align-items: center; }
.icon-btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--ink-2);
  width: 34px; height: 34px; border-radius: 8px; display: grid; place-items: center; cursor: pointer;
}
.icon-btn:hover { color: var(--ink); }
.icon-btn svg { width: 18px; height: 18px; }

main { max-width: 1320px; margin: 0 auto; padding: 20px 16px 64px; }
.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }

/* ---------- Karten & Raster */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 16px; box-shadow: var(--shadow); min-width: 0;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.kpis { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; }
.kpi .label { color: var(--ink-2); font-size: .82rem; display: flex; align-items: center; gap: 6px; }
.kpi .value { white-space: nowrap; font-size: 1.3rem; font-weight: 650; margin-top: 2px; letter-spacing: -.01em; }
.kpi .sub { color: var(--muted); font-size: .8rem; }
.kpi .sw { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
@media (max-width: 900px) { .g2, .g3 { grid-template-columns: 1fr; } }
.stack > * + * { margin-top: 16px; }

.chart-box { position: relative; height: 300px; }
.chart-box.tall { height: 360px; }
.chart-box.short { height: 240px; }

/* ---------- Bedienelemente */
@media (max-width: 600px) {
  .page-head .period { width: 100%; }
  .page-head .period .lbl { flex: 1; }
}
.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; border: 1px solid var(--border); }
.seg button {
  border: 0; background: transparent; color: var(--ink-2); padding: 5px 12px; border-radius: 7px;
  cursor: pointer; font: inherit; font-size: .88rem;
}
.seg button.on { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.period { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.period .lbl { min-width: 120px; text-align: center; font-weight: 600; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin: 0 0 16px; overflow-x: auto; }
.tabs button {
  background: none; border: 0; border-bottom: 2px solid transparent; padding: 8px 12px; color: var(--ink-2);
  cursor: pointer; font: inherit; white-space: nowrap;
}
.tabs button.on { color: var(--ink); border-color: var(--accent); font-weight: 600; }

button.btn, a.btn, label.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface);
  color: var(--ink); padding: 7px 12px; border-radius: 8px; cursor: pointer; font: inherit; font-size: .9rem;
  text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 3px 8px; font-size: .82rem; }
.btn:disabled { opacity: .55; cursor: wait; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

input, select, textarea {
  font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--axis);
  border-radius: 8px; padding: 7px 10px; min-width: 0;
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--accent); }
.form { display: grid; gap: 12px; align-items: start; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.form label, .field { display: grid; gap: 4px; align-content: start; font-size: .85rem; color: var(--ink-2); }
.form label.check { display: flex; align-items: center; gap: 8px; }
.form .full { grid-column: 1 / -1; }
.hint { font-size: .8rem; color: var(--muted); }

/* ---------- Tabellen */
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: .88rem; }
th, td { padding: 7px 10px; border-bottom: 1px solid var(--grid); text-align: left; white-space: nowrap; }
th { color: var(--ink-2); font-weight: 600; font-size: .8rem; position: sticky; top: 0; background: var(--surface); }
tr.sum td { font-weight: 650; border-top: 1px solid var(--axis); }
tbody tr:hover td { background: var(--surface-2); }

.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: .75rem; background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--border); }
.badge.ok { color: var(--good); }
.badge.err { color: var(--bad); }

/* ---------- Dialog & Toast */
dialog {
  border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--ink);
  padding: 20px; width: min(560px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
dialog::backdrop { background: rgba(0, 0, 0, .35); }
dialog h2 { margin-bottom: 12px; }
dialog .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 100; display: grid; gap: 8px; }
.toast { background: var(--ink); color: var(--page); padding: 10px 16px; border-radius: 10px; font-size: .9rem; max-width: 90vw; box-shadow: 0 8px 24px rgba(0,0,0,.2); }
.toast.err { background: var(--bad); color: #fff; }

.drop {
  display: block; cursor: pointer;
  border: 2px dashed var(--axis); border-radius: 14px; padding: 28px; text-align: center; color: var(--ink-2);
  transition: background .15s;
}
.drop.over { background: var(--surface-2); border-color: var(--accent); }

.empty { padding: 36px 16px; text-align: center; color: var(--muted); }
.legend-note { font-size: .8rem; color: var(--muted); margin-top: 6px; }

/* ---------- Login */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth { width: min(420px, 100%); }
.auth .brand { justify-content: center; font-size: 1.3rem; margin-bottom: 16px; }
.auth form { display: grid; gap: 12px; }
.qr { background: #fff; border-radius: 12px; padding: 8px; width: fit-content; margin: 8px auto; }
.qr svg { display: block; width: 220px; height: 220px; }
code.secret { word-break: break-all; font-size: .85rem; background: var(--surface-2); padding: 4px 8px; border-radius: 6px; display: inline-block; }
