/* =================================================================
   Energietracker v1.2.0 — App Shell
   Sidebar + thin topbar layout, restored from v0.9.0.
   ================================================================= */

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg-0);
  color: var(--text-1);
  font-family: var(--font-sans);
  font-size: var(--fs-base);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); text-decoration: underline; }
h1, h2, h3, h4 { margin: 0; }

/* ── Accessibility (N1009) ──────────────────────────────────────── */
/* Sichtbarer Fokus-Ring als Baseline für die Tastatur-Navigation.
   Komponenten mit eigenem :focus-visible (z.B. .topbar__btn) überschreiben das. */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
/* Programmatisch fokussierte Container (z.B. <main> bei View-Wechsel) sollen
   keinen Ring zeigen — nur echte Tastatur-Navigation. */
#view:focus { outline: none; }

/* Nur für Screenreader sichtbar: visuell entfernt, aber im Accessibility-Tree. */
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Skip-Link: aus dem Viewport geschoben, bis er Tastatur-Fokus bekommt. */
.skip-link {
  position: fixed;
  top: 8px; left: 8px;
  z-index: 1000;
  padding: 10px 16px;
  background: var(--bg-1);
  color: var(--text-1);
  border: 1px solid var(--accent);
  border-radius: var(--r-sm);
  transform: translateY(-150%);
  transition: transform .15s ease;
}
.skip-link:focus {
  transform: translateY(0);
  text-decoration: none;
}

/* Nutzer mit „prefers-reduced-motion" bekommen keine Bewegungs-Animationen. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ── Top-level layout grid ──────────────────────────────────────── */
/* v2.5.3 (UI-01) — minmax(0, 1fr) statt 1fr: `1fr` heißt minmax(auto, 1fr),
   und die Mindestbreite breiter Tabellen dehnte die ganze Spalte — obwohl die
   Tabellen in .table-wrap selbst scrollen. Folge waren seitliches Scrollen auf
   dem iPhone in 13 von 21 Ansichten und abgeschnittene Knöpfe bei 1280 px. */
.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  grid-template-rows: var(--topbar-total) 1fr;
  grid-template-areas:
    "topbar topbar"
    "sidebar view";
  min-height: 100vh;
  min-height: 100dvh;
}

/* ── Topbar ─────────────────────────────────────────────────────── */
.topbar {
  grid-area: topbar;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 var(--sp-5);
  /* v2.11.0 (Review UI-25) — unter Uhr und Dynamic Island freilassen */
  padding-top: env(safe-area-inset-top, 0px);
  padding-left: max(var(--sp-5), env(safe-area-inset-left, 0px));
  padding-right: max(var(--sp-5), env(safe-area-inset-right, 0px));
  background: var(--bg-1);
  border-bottom: 1px solid var(--border-1);
  position: sticky; top: 0; z-index: 10;
}
.topbar__brand {
  display: flex; align-items: center; gap: var(--sp-3);
}
.topbar__logo {
  width: 22px; height: 22px;
  background-image: url("../img/icon-light.png");
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}
[data-theme="dark"] .topbar__logo {
  background-image: url("../img/icon-dark.png");
}
.topbar__title {
  font-family: var(--font-mono);
  font-size: 13px; font-weight: 500;
  letter-spacing: 0.04em;
  color: var(--text-1);
}
.topbar__status {
  font-family: var(--font-mono); font-size: 11px;
  color: var(--text-2);
  display: flex; align-items: center; gap: 8px;
}
.topbar__status .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent);
  display: inline-block;
}
.topbar__actions {
  display: flex; align-items: center; gap: var(--sp-3);
}
/* v2.6.0 — Offline-Hinweis (Daten aus dem Cache des Service Workers) */
.topbar__status--offline {
  color: var(--warning); font-weight: 600;
}
/* v2.6.0 — Abmelden: Textknopf statt Symbol */
.topbar__btn.topbar__btn--text {
  width: auto; padding: 0 var(--sp-3); font-size: var(--fs-sm);
}

/* v2.6.0 — Anmeldebildschirm (opt-in) */
.login-screen {
  position: fixed; inset: 0; z-index: 200;
  display: grid; place-items: center; padding: var(--sp-4);
  background: var(--bg-0);
}
.login-card {
  width: min(360px, 100%);
  display: flex; flex-direction: column; gap: var(--sp-3);
  padding: var(--sp-6); border-radius: var(--r-lg);
  background: var(--bg-1); border: 1px solid var(--border-1);
  box-shadow: var(--shadow-lg, 0 10px 30px rgba(0,0,0,.2));
}
.login-card__brand {
  display: flex; align-items: center; gap: var(--sp-2);
  font-family: var(--font-mono); font-size: var(--fs-xs); letter-spacing: .12em; color: var(--text-2);
}
.login-card h1 { font-size: var(--fs-xl, 22px); margin: 0; }
.login-card__label { font-size: var(--fs-sm); color: var(--text-2); }
.login-card .input { font-size: 16px; }   /* iOS zoomt unter 16 px */
.login-card .btn { justify-content: center; }
.login-card__hint { font-size: var(--fs-xs); margin: 0; }
.topbar__btn {
  background: transparent;
  border: 1px solid var(--border-2);
  color: var(--text-1);
  width: 32px; height: 32px;
  border-radius: var(--r-sm);
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 15px; line-height: 1;
  transition: var(--transition);
}
.topbar__btn:hover {
  background: var(--bg-2);
  border-color: var(--border-strong);
}
.topbar__btn:active { transform: translateY(1px); }
.topbar__btn:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}
.topbar__btn-icon { display: inline-block; }

/* ── Sidebar ────────────────────────────────────────────────────── */
.sidebar {
  grid-area: sidebar;
  background: var(--bg-1);
  border-right: 1px solid var(--border-1);
  display: flex; flex-direction: column;
  padding: var(--sp-3) 0 0;
  position: sticky; top: var(--topbar-total);
  height: calc(100vh - var(--topbar-total));
  height: calc(100dvh - var(--topbar-total));
  overflow-y: auto;
}
.sidebar__nav {
  display: flex; flex-direction: column; gap: 2px;
  padding: 0 var(--sp-3);
  flex: 1;
}
/* v2.11.0 — Bereiche als einzelne Einträge, die Verbrauchsarten als Gruppe */
.sidebar__group { display: flex; flex-direction: column; gap: 2px; margin: var(--sp-2) 0; }
/* v2.11.0 (Review UI-09) — 11 px in --text-2 statt 9 px unter 3:1 */
.sidebar__group-label {
  font-size: 11px; font-weight: 600;
  color: var(--text-2);
  text-transform: uppercase; letter-spacing: 0.12em;
  padding: var(--sp-3) var(--sp-3) var(--sp-1);
}
.sidebar__item {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: 8px 14px;
  color: var(--text-2);
  font-size: 13px; font-weight: 500;
  border-radius: var(--r-sm);
  position: relative;
  transition: var(--transition);
}
.sidebar__item:hover {
  background: var(--bg-2);
  color: var(--text-1);
  text-decoration: none;
}
.sidebar__icon {
  width: 22px; height: 22px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 4px;
  background: var(--bg-3);
  font-size: 11px;
  flex-shrink: 0;
}
.sidebar__item.active {
  background: var(--bg-2);
  color: var(--text-1);
}
.sidebar__item.active::before {
  content: '';
  position: absolute; left: 0; top: 0; bottom: 0;
  width: 2px; border-radius: 1px;
  background: var(--accent);
}
/* Der farbige Aktiv-Marker je Verbrauchsart kommt seit v2.2.0 aus
   utility-theme.js (SSOT) — vorher hatten ihn nur gas/strom/wasser. */
.sidebar__footer {
  border-top: 1px solid var(--border-1);
  padding: var(--sp-3) var(--sp-4);
  font-size: 11px; color: var(--text-3);
  display: flex; gap: 6px; align-items: center;
}
.sidebar__footer a { color: var(--text-2); }

/* ── View / main content area ───────────────────────────────────── */
.view {
  grid-area: view;
  padding: var(--sp-5) var(--sp-6);
  max-width: var(--content-max);
  width: 100%;
  min-width: 0;
}
.loading {
  display: flex; align-items: center; justify-content: center;
  height: 240px; color: var(--text-3); font-style: italic;
}

/* ── View header (title + subtitle + actions) ───────────────────── */
.view-header {
  display: flex; align-items: flex-start; justify-content: space-between;
  margin-bottom: var(--sp-4); gap: var(--sp-4); flex-wrap: wrap;
}
.view-header__title {
  font-size: 20px; font-weight: 700; line-height: 1.2;
  color: var(--text-1);
}
.view-header__subtitle {
  font-size: 12px; color: var(--text-2);
  margin-top: 4px;
}
.view-header__actions {
  display: flex; gap: 8px; flex-wrap: wrap;
}

/* ── KPI grid ───────────────────────────────────────────────────── */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(200px, 100%), 1fr));
  gap: 14px;
  margin-bottom: var(--sp-5);
}
.kpi {
  position: relative;
  background: var(--bg-1);
  border: 1px solid var(--border-1);
  border-radius: var(--r-md);
  padding: 16px 18px;
  transition: var(--transition);
  overflow: hidden;
}
/* v2.12.0 (Review UI-21) — kein Anheben beim Überfahren: die Kacheln sind
   nicht klickbar und versprachen mit der Bewegung etwas, das nicht kam */
.kpi::after {
  content: ''; position: absolute;
  top: 0; left: 0; right: 0; height: 2px;
  background: linear-gradient(90deg, var(--kpi-c, var(--accent)), transparent);
}
/* .kpi.c-<key> wird seit v2.2.0 aus der SSOT erzeugt (utility-theme.js). */
.kpi.c-blue   { --kpi-c: var(--c-blue); }
.kpi.c-violet { --kpi-c: var(--c-violet); }
.kpi.c-yellow { --kpi-c: var(--c-yellow); }
.kpi.c-red    { --kpi-c: var(--c-red); }

.kpi__label {
  font-size: 11px; font-weight: 500; color: var(--text-2);
  text-transform: uppercase; letter-spacing: 0.07em;
  margin-bottom: 8px;
  display: flex; align-items: center; gap: 6px;
  /* v2.11.0 (Review UI-21) — „EIGENVERBRAUCHSQUOT…" wurde abgeschnitten */
  flex-wrap: wrap; overflow-wrap: anywhere; hyphens: auto;
}
.kpi__value {
  font-family: var(--font-mono); font-size: 22px; font-weight: 500;
  line-height: 1.1; color: var(--text-1);
}
.kpi__sub {
  font-size: 12px; color: var(--text-2);
  margin-top: 6px; font-family: var(--font-mono);
}
.kpi__sub.positive { color: var(--success); }
.kpi__sub.negative { color: var(--danger); }
.kpi__trend {
  display: inline-flex; align-items: center; gap: 3px;
  margin-left: 6px; padding: 1px 7px;
  font-size: 11px; font-weight: 600;
  border-radius: 100px;
}
.kpi__trend.up { background: var(--danger-soft); color: var(--danger); }
.kpi__trend.down { background: var(--success-soft); color: var(--success); }
.kpi__trend.flat { background: var(--bg-3); color: var(--text-2); }

/* ── Status banner (overdue alert / OK banner) ──────────────────── */
.status-banner {
  display: flex; align-items: center; gap: var(--sp-4);
  padding: 12px 16px;
  border-radius: var(--r-md);
  margin-bottom: var(--sp-5);
  border: 1px solid;
  font-size: 13px;
}
.status-banner.ok    { background: var(--success-soft); border-color: color-mix(in srgb, var(--success) 30%, transparent); color: var(--success); }
.status-banner.warn  { background: var(--warning-soft); border-color: rgba(245,200,66,.3); color: var(--warning); }
.status-banner.alert { background: var(--danger-soft);   border-color: color-mix(in srgb, var(--danger) 30%, transparent); color: var(--danger); }
.status-banner__icon { font-size: 18px; line-height: 1; }
.status-banner__text { flex: 1; }
.status-banner strong { font-weight: 600; }

/* ── Card ───────────────────────────────────────────────────────── */
.card {
  background: var(--bg-1);
  border: 1px solid var(--border-1);
  border-radius: var(--r-md);
  padding: var(--sp-5);
  margin-bottom: var(--sp-5);
}
/* .card--<key> wird seit v2.2.0 aus der SSOT erzeugt (utility-theme.js). */
.card__title {
  font-size: 11px; font-weight: 500;
  color: var(--text-2);
  text-transform: uppercase; letter-spacing: 0.07em;
  margin-bottom: 14px;
  display: flex; align-items: center; gap: 8px;
}
.card__title-action { margin-left: auto; }

/* ── Generic grid helpers ───────────────────────────────────────── */
.grid { display: grid; gap: var(--sp-4); }
/* v2.0.0 (UX) — im Grid übernimmt der gap den Abstand; der zusätzliche
   margin-bottom der Karte entfällt, damit Karten-Reihen gleichmäßig sitzen. */
.grid > .card { margin-bottom: 0; }
/* v2.5.3 (UI-01) — min(…, 100%): Auf dem iPhone SE (375 px) war eine
   360-px-Spalte samt Rand breiter als der Bildschirm. */
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(min(360px, 100%), 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(min(280px, 100%), 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(min(220px, 100%), 1fr)); }

/* ── Misc helpers ───────────────────────────────────────────────── */
.muted { color: var(--text-2); }
/* v2.11.0 (Review UI-09) — „–" für fehlende Werte ist Information, kein
   gesperrtes Element: --text-3 (5:1) statt --text-disabled (unter 3:1) */
.dim   { color: var(--text-3); }
.num   { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.success-text { color: var(--success); }
.danger-text  { color: var(--danger); }
.warning-text { color: var(--warning); }

.empty {
  text-align: center; padding: 60px 20px; color: var(--text-2);
}
.empty-icon { font-size: 36px; opacity: 0.4; margin-bottom: 12px; }
.empty h3   { margin-bottom: 8px; color: var(--text-1); }
.empty p    { font-size: 13px; }

/* ── Bereichs-Tabs (v2.11.0, Review UI-12) ─────────────────────────
   Seiten eines Bereichs („Kosten & Verträge": Verträge · Wechsel prüfen ·
   Rechnung prüfen) als Tabs über der Ansicht; die Seitenleiste nennt nur
   den Bereich. */
.section-tabs {
  display: flex; gap: var(--sp-1);
  margin: calc(-1 * var(--sp-2)) 0 var(--sp-4);
  border-bottom: 1px solid var(--border-1);
  overflow-x: auto; scrollbar-width: none;
}
.section-tabs::-webkit-scrollbar { display: none; }
.section-tabs__tab {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 10px 14px; white-space: nowrap;
  color: var(--text-2); font-size: var(--fs-sm); font-weight: 500;
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.section-tabs__tab:hover { color: var(--text-1); text-decoration: none; }
.section-tabs__tab.active { color: var(--text-1); border-bottom-color: var(--accent); }
.section-tabs__tab[data-utility].active { border-bottom-color: var(--util, var(--accent)); }
/* Die Verbrauchsarten stehen am Mac in der Seitenleiste */
.section-tabs--mobile { display: none; }

/* Kartenkopf mit Aktion daneben (die Aktion steht nicht in der Überschrift) */
.card__head {
  display: flex; align-items: center; gap: var(--sp-2);
  margin-bottom: 14px;
}
.card__head > .card__title { margin-bottom: 0; }
.card__head > :last-child:not(.card__title) { margin-left: auto; }

.sidebar__error { padding: var(--sp-3); font-size: var(--fs-sm); color: var(--text-2); }
.sidebar__error .btn { margin-top: var(--sp-2); }
.sidebar__label { flex: 1; min-width: 0; }

/* Erfassen am Mac: Knopf in der Kopfleiste */
.topbar__capture { gap: 4px; }

/* Tab-Leiste und Menü-Hintergrund gibt es nur auf dem iPhone */
.tabbar, .nav-backdrop { display: none; }

/* ── Responsive ─────────────────────────────────────────────────────
   v2.11.0 (Review UI-05) — bis v2.10 stand hier die ganze Seitenleiste als
   Block über dem Inhalt (auf dem iPhone SE drei Viertel des Bildschirms).
   Jetzt: Tab-Leiste unten, Seitenleiste als Menü hinter „Mehr". */
@media (max-width: 800px) {
  html, body { font-size: 1rem; }   /* 16 px Grundschrift */
  .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: var(--topbar-total) 1fr;
    grid-template-areas:
      "topbar"
      "view";
  }
  /* .btn aus components.css kommt später und setzt display — daher doppelt */
  .topbar .topbar__capture { display: none; }
  .topbar { padding-left: max(var(--sp-4), env(safe-area-inset-left, 0px)); padding-right: max(var(--sp-4), env(safe-area-inset-right, 0px)); }

  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 70;
    width: min(320px, 86vw); height: 100vh; height: 100dvh;
    padding-top: calc(var(--sp-4) + env(safe-area-inset-top, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    padding-left: env(safe-area-inset-left, 0px);
    transform: translateX(-105%);
    transition: transform 200ms ease-out, visibility 0s linear 200ms;
    visibility: hidden;
    box-shadow: var(--shadow-3);
  }
  body.nav-open .sidebar { transform: none; visibility: visible; transition: transform 200ms ease-out; }
  body.nav-open { overflow: hidden; }
  body.nav-open .nav-backdrop {
    display: block; position: fixed; inset: 0; z-index: 65;
    background: var(--bg-overlay);
  }
  .sidebar__item { padding: 12px 14px; font-size: var(--fs-md); }
  .sidebar__footer { padding-bottom: calc(var(--sp-3) + env(safe-area-inset-bottom, 0px)); font-size: var(--fs-sm); }
  /* v2.13.0 — „Hilfe" steht hier; auch ein Fußzeilen-Link braucht 44 px zum Tippen */
  .sidebar__footer a { display: inline-flex; align-items: center; justify-content: center; min-height: 44px; min-width: 44px; padding: 0 var(--sp-2); }

  .tabbar {
    display: grid; grid-template-columns: repeat(5, minmax(0, 1fr));
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 60;
    height: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px));
    padding: 0 env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
    background: var(--bg-1);
    border-top: 1px solid var(--border-2);
  }
  .tabbar__item {
    position: relative;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    min-height: 44px; padding: 4px 2px;
    background: none; border: none; cursor: pointer;
    color: var(--text-2); font: inherit; font-size: 11px; font-weight: 500;
    text-decoration: none;
  }
  .tabbar__item:hover { text-decoration: none; color: var(--text-1); }
  .tabbar__item.active { color: var(--accent); }
  .tabbar__icon { font-size: 20px; line-height: 1; }
  .tabbar__label { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .tabbar__item--capture .tabbar__icon {
    display: inline-flex; align-items: center; justify-content: center;
    width: 36px; height: 36px; border-radius: 50%;
    background: var(--accent); color: var(--accent-fg); font-size: 22px; font-weight: 600;
  }
  .tabbar__badge { position: absolute; top: 2px; left: 50%; margin-left: 6px; }
  .tabbar__badge .sidebar__badge { margin-left: 0; }

  .view {
    padding: var(--sp-4);
    padding-left: max(var(--sp-4), env(safe-area-inset-left, 0px));
    padding-right: max(var(--sp-4), env(safe-area-inset-right, 0px));
    /* Platz für die Tab-Leiste */
    padding-bottom: calc(var(--sp-6) + var(--tabbar-h) + env(safe-area-inset-bottom, 0px));
  }
  .section-tabs--mobile { display: flex; }
  .section-tabs { margin-top: 0; }
  .section-tabs__tab { min-height: 44px; }
}

/* Knöpfe im Kartentitel erben nicht dessen Versalien */
.card__title .btn { text-transform: none; letter-spacing: normal; font-size: var(--fs-sm); }
