:root {
    --bg: #0f172a;
    --card: #1e293b;
    --fg: #e2e8f0;
    --muted: #94a3b8;
    --accent: #06b6d4;
    --error: #f87171;
    --ok: #34d399;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--fg);
    min-height: 100vh;
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    /* darf umbrechen: verhindert page-weites horizontales Scrollen auf schmalen
       Viewports (die Nav war der Auslöser, nicht die Tabellen). */
    flex-wrap: wrap;
    gap: 0.5rem 1rem;
    padding: 0.75rem 1.5rem;
    background: #0b1220;
    border-bottom: 1px solid #1e293b;
}

.brand { font-weight: 700; color: var(--accent); }
.nav { display: flex; align-items: center; gap: 0.5rem 1rem; flex-wrap: wrap; }
.user { color: var(--muted); font-size: 0.9rem; }

.container {
    max-width: 480px;
    margin: 3rem auto;
    padding: 0 1rem;
}

/* Skaliert mit dem Fenster (kein fester width-Wert) und deckelt bei 1100px. */
.container-wide { max-width: 1100px; width: 100%; }

.nav a { margin-right: 0.75rem; font-size: 0.9rem; }

table { width: 100%; border-collapse: collapse; margin: 1rem 0; font-size: 0.9rem; }
th, td { text-align: left; padding: 0.5rem 0.6rem; border-bottom: 1px solid #334155; }
th { color: var(--muted); font-weight: 600; }
td.actions { white-space: nowrap; }
td.actions form { display: inline; }
/* Zahlen-Zellen: gleiche Ziffernbreite statt breiter Fixbreite, rechtsbündig. */
td.num, td.mono { font-variant-numeric: tabular-nums; }
td.num { text-align: right; }

/* Horizontales Scrollen bleibt IMMER innerhalb dieses Wrappers – nie die ganze
   Seite. Bei ausreichender Breite fällt der Balken weg (Tabelle passt). */
.table-scroll { overflow-x: auto; max-width: 100%; }

/* Sortierbare Kopfzellen (Server-seitig via ?sort=&dir=). Aktive Spalte farbig
   markiert, ▲/▼ als Textzeichen – CSS-only, kein Inline-Style, kein JS. */
th a.th-sort { color: inherit; text-decoration: none; display: inline-flex; align-items: center; gap: 0.3rem; }
th a.th-sort:hover { color: var(--fg); }
th.sorted { color: var(--accent); }
th.sorted a.th-sort { color: var(--accent); }
.sort-ind { font-size: 0.7rem; line-height: 1; }

.badge { padding: 0.1rem 0.5rem; border-radius: 999px; font-size: 0.75rem; }
.badge-admin { background: rgba(6,182,212,0.18); color: var(--accent); }
.badge-user { background: rgba(148,163,184,0.18); color: var(--muted); }
.badge-active { background: rgba(52,211,153,0.18); color: var(--ok); }
.badge-disabled { background: rgba(248,113,113,0.18); color: var(--error); }

/* Card-Header: Titel links, Aktion rechts; darf umbrechen statt zu überlappen. */
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 0.75rem 1rem; flex-wrap: wrap; }
.toolbar h1, .toolbar h2, .toolbar h3 { margin: 0; }
.btn-danger { background: var(--error); color: #300; }
select { padding: 0.5rem; border-radius: 8px; background: #0f172a; color: var(--fg); border: 1px solid #334155; }
code.mono { font-family: ui-monospace, monospace; color: var(--muted); font-size: 0.8rem; }

.card {
    background: var(--card);
    padding: 2rem;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

h1 { margin-top: 0; font-size: 1.4rem; }

label { display: block; margin: 1rem 0 0.35rem; font-size: 0.9rem; color: var(--muted); }

input[type="email"],
input[type="password"],
input[type="text"] {
    width: 100%;
    padding: 0.6rem 0.75rem;
    border-radius: 8px;
    border: 1px solid #334155;
    background: #0f172a;
    color: var(--fg);
    font-size: 1rem;
}

input:focus { outline: 2px solid var(--accent); border-color: var(--accent); }

.btn {
    margin-top: 1.25rem;
    width: 100%;
    padding: 0.7rem 1rem;
    border: none;
    border-radius: 8px;
    background: var(--accent);
    color: #04222a;
    font-weight: 600;
    font-size: 1rem;
    cursor: pointer;
}

.btn:hover { filter: brightness(1.08); }
.btn:disabled { opacity: 0.6; cursor: progress; }

/* Kompakter Button (Modifier von .btn). MUSS nach .btn stehen, damit width/
   margin-top zuverlässig überschrieben werden – sonst gewinnt .btn (gleiche
   Spezifität, spätere Regel) und der Button wird 100% breit / überlappt. */
.btn-sm {
    display: inline-block;
    width: auto;
    margin-top: 0;
    padding: 0.4rem 0.8rem;
    font-size: 0.85rem;
    line-height: 1.2;
}

.btn-link {
    background: none;
    border: none;
    color: var(--muted);
    cursor: pointer;
    padding: 0;
    font: inherit;
    text-decoration: underline;
}

.inline { display: inline; }

/* Checkbox-Zeile (z.B. "Diesem Gerät 30 Tage vertrauen") – Text nicht als Block. */
label.checkbox {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.9rem;
    color: var(--fg);
    margin: 1rem 0 0;
    cursor: pointer;
}
label.checkbox input { width: auto; margin: 0; }

.muted { color: var(--muted); font-size: 0.9rem; }
.muted a, a { color: var(--accent); }

.alert { padding: 0.75rem 1rem; border-radius: 8px; margin: 1rem 0; font-size: 0.92rem; }
.alert-error { background: rgba(248, 113, 113, 0.12); color: var(--error); }
.alert-info { background: rgba(6, 182, 212, 0.12); color: var(--accent); }

.status { margin-top: 1rem; font-size: 0.9rem; min-height: 1.2em; }
.status.error { color: var(--error); }
.status.ok { color: var(--ok); }

.facts { list-style: none; padding: 0; }
.facts li { padding: 0.4rem 0; border-bottom: 1px solid #334155; }

/* --- Modul 5: Kundenverwaltung --- */
:root { --warn: #fbbf24; }

.card + .card { margin-top: 1.25rem; }
h2 { font-size: 1.15rem; margin: 0 0 0.5rem; }
h3 { font-size: 1rem; margin: 0; }

textarea {
    width: 100%;
    padding: 0.6rem 0.75rem;
    border-radius: 8px;
    border: 1px solid #334155;
    background: #0f172a;
    color: var(--fg);
    font: inherit;
    resize: vertical;
}
input[type="date"],
input[type="number"],
input[type="search"] {
    width: 100%;
    padding: 0.6rem 0.75rem;
    border-radius: 8px;
    border: 1px solid #334155;
    background: #0f172a;
    color: var(--fg);
    font-size: 1rem;
    color-scheme: dark;
}

.grid-2, .grid-3 { display: grid; gap: 0.75rem; }
.grid-2 { grid-template-columns: 1fr 1fr; }
.grid-3 { grid-template-columns: 1fr 1fr 1fr; }
@media (max-width: 640px) {
    .grid-2, .grid-3 { grid-template-columns: 1fr; }
}

.filterbar { display: flex; gap: 0.6rem; align-items: center; flex-wrap: wrap; margin: 1rem 0; }
.filterbar input[type="search"] { flex: 1 1 260px; }
.filterbar select { width: auto; }

.subcard {
    border: 1px solid #334155;
    border-radius: 10px;
    padding: 1rem 1.25rem;
    margin-top: 1rem;
    background: rgba(15, 23, 42, 0.4);
}
.credential-actions { margin-top: 0.75rem; gap: 0.6rem; justify-content: flex-start; }
.help-steps { margin: 0.75rem 0 0; padding-left: 1.4rem; }
.help-steps li { margin-bottom: 0.6rem; line-height: 1.6; }
.help-steps li:last-child { margin-bottom: 0; }
.help-steps ul { margin: 0.3rem 0 0 0; padding-left: 1.2rem; }
.help-steps code { font-size: 0.82em; background: rgba(99,102,241,0.12); border-radius: 4px; padding: 1px 5px; }
.credential-actions form { display: inline; }

.alert-warn { background: rgba(251, 191, 36, 0.12); color: var(--warn); }
.badge-inactive { background: rgba(148, 163, 184, 0.18); color: var(--muted); }
.badge-warn { background: rgba(251, 191, 36, 0.18); color: var(--warn); }
.badge-expired { background: rgba(248, 113, 113, 0.18); color: var(--error); }

ul.tight { margin: 0.4rem 0 0; padding-left: 1.1rem; }
ul.tight li { padding: 0.15rem 0; }

/* --- Dashboard KPIs & Charts --- */

/* Einheitlicher Block-Rhythmus (Portal-Maß 1.25rem, wie .card + .card) zwischen
   ALLEN Top-Level-Dashboard-Blöcken – robust unabhängig vom Elementtyp. Löst das
   Problem, dass die frühere .card + .card-Regel über die Diagramm-Grid (ein div)
   hinweg nicht griff und Blöcke mit 0px Abstand aneinanderstießen. */
.dash-stack > * + * { margin-top: 1.25rem; }

/* margin-top bewusst NICHT hier: den Block-Abstand setzt .dash-stack > * + *
   (sonst überschreibt diese gleichspezifische, spätere Regel den Rhythmus). */
.kpi-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1.25rem; }
@media (max-width: 780px) { .kpi-grid { grid-template-columns: repeat(2, 1fr); } }
.kpi {
    background: rgba(15, 23, 42, 0.55);
    border: 1px solid #334155;
    border-left-width: 4px;
    border-radius: 10px;
    /* Padding auf die Diagramm-Cards abgestimmt (1.25 ↔ 1.5rem), damit Kacheln
       und Boxen als ein System wirken. */
    padding: 1.25rem;
}
.kpi-value { font-size: 1.7rem; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.kpi-label { color: var(--muted); font-size: 0.82rem; margin-top: 0.25rem; }
.kpi-sub { color: var(--muted); font-size: 0.72rem; margin-top: 0.15rem; opacity: 0.8; }
.kpi-accent { border-left-color: var(--accent); }
.kpi-accent .kpi-value { color: var(--accent); }
.kpi-warn { border-left-color: var(--warn); }
.kpi-warn .kpi-value { color: var(--warn); }
.kpi-error { border-left-color: var(--error); }
.kpi-error .kpi-value { color: var(--error); }
.kpi-ok { border-left-color: var(--ok); }
.kpi-ok .kpi-value { color: var(--ok); }
.chart-svg { width: 100%; height: auto; display: block; }

/* Header-Card: Begrüßung dicht unter die Überschrift, Rest im 1rem-Raster. */
.dash-head p { margin: 0.5rem 0 0; }
.dash-head .alert { margin: 1rem 0 0; }

/* Diagramm-Reihe: eigener Grid mit Block-Rhythmus als Spaltenabstand. Beide Boxen
   exakt gleich hoch (Grid-Default stretch); die Chart-Fläche wird darin zentriert,
   damit kein Inhalt oben „klebt". */
.chart-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; }
@media (max-width: 780px) { .chart-grid { grid-template-columns: 1fr; } }
/* Die allgemeine .card + .card-Regel (margin-top: 1.25rem) greift per Adjacent-
   Sibling auch auf Kinder von .chart-grid und verschiebt die rechte Box nach
   unten. Grid-gap übernimmt den Abstand – interner margin-top muss 0 sein. */
.chart-grid > .card { margin-top: 0; }
.card.chart-card { padding: 1.5rem; display: flex; flex-direction: column; height: 100%; }
.chart-body { flex: 1; display: flex; align-items: center; }

/* Kartenkopf: Icon + Titel links (wie Referenz-Optik), Zeitraum-Filter entfällt. */
.chart-head { display: flex; align-items: center; gap: 0.55rem; margin-bottom: 0.9rem; }
.chart-head h2 { margin: 0; }
.chart-icon { display: inline-flex; color: var(--accent); }

/* Links-only-Card: kein zusätzlicher Kopfabstand zur Card-Innenkante. */
.card > .dash-links:first-child { margin-top: 0; }

/* --- Reports --- */
.ticket-copy {
    width: 100%;
    font-family: ui-monospace, monospace;
    font-size: 0.82rem;
    background: #0f172a;
    color: var(--fg);
    border: 1px solid #334155;
    border-radius: 8px;
    padding: 0.6rem 0.75rem;
    resize: vertical;
}
.comment-form { display: flex; gap: 0.5rem; align-items: center; margin: 0; }
.comment-form input[type="text"] { flex: 1 1 200px; }
.card.ticket { padding: 1.25rem 1.5rem; }
.card.ticket + .card.ticket { margin-top: 0.75rem; }

/* Sekundär-Button (Filter-Umschalter, nicht aktiv). */
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid #334155; }
.btn-ghost:hover { filter: none; color: var(--fg); border-color: var(--accent); }

/* Kompakte Δ-Chips (einzeilig, kein Umbruch) in der Abweichungen-Zelle. */
.dev-cell { white-space: nowrap; }
.chip {
    display: inline-block;
    padding: 0.08rem 0.4rem;
    margin: 0 0.15rem 0 0;
    border-radius: 6px;
    font-size: 0.72rem;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

/* Spalten-Filterzeile im Tabellenkopf. */
.filter-row th { padding-top: 0.35rem; padding-bottom: 0.5rem; vertical-align: top; }
.filter-row input[type="text"],
.filter-row input[type="number"],
.filter-row select { width: 100%; padding: 0.3rem 0.4rem; font-size: 0.8rem; }
.num-range { display: flex; gap: 0.3rem; }
.num-range input { width: 50%; }
/* .filter-status: Select + "Filtern"-Button nebeneinander (Flex-Zeile).
   Ohne Flex würde .filter-row select { width:100% } den Select auf volle
   Zellbreite dehnen und den Button nach rechts außerhalb des sichtbaren
   Bereichs verdrängen. Flex lässt den Select wachsen (flex:1) und hält
   den Button kompakt. */
.filter-status { display: flex; align-items: center; gap: 0.3rem; white-space: nowrap; }
.filter-status select { flex: 1 1 auto; min-width: 0; margin-bottom: 0; }
/* Spezifität (0,2,0) > .btn (0,1,0): überschreibt width:100%/margin-top:1.25rem
   der generischen .btn-Regel für alle Buttons innerhalb der Filterzeile. */
.filter-row .btn { width: auto; margin-top: 0; }

/* Status-Editor (native details, kein JS): Zusammenfassung = Badge + Grund,
   aufgeklappt das Formular mit Freitext. */
.status-edit > summary { cursor: pointer; list-style: none; display: flex; align-items: center; gap: 0.4rem; flex-wrap: wrap; }
.status-edit > summary::-webkit-details-marker { display: none; }
.status-edit .comment-preview { font-size: 0.8rem; max-width: 22rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status-form { display: flex; flex-direction: column; gap: 0.4rem; margin-top: 0.5rem; max-width: 26rem; }
.status-form select, .status-form textarea { width: 100%; }
.status-form .btn { width: auto; align-self: flex-start; }

/* --- Doku --- */
.docs h1 { margin-top: 0; }
.docs h2 { margin-top: 1.6rem; border-bottom: 1px solid #334155; padding-bottom: 0.3rem; }
.docs h3 { margin-top: 1.2rem; }
.docs p, .docs li { line-height: 1.55; }
.docs code { font-family: ui-monospace, monospace; background: #0f172a; padding: 0.05rem 0.35rem; border-radius: 5px; font-size: 0.85rem; }
.docs pre { background: #0f172a; border: 1px solid #334155; border-radius: 8px; padding: 0.9rem 1rem; overflow-x: auto; }
.docs pre code { background: none; padding: 0; }
.docs blockquote { border-left: 3px solid var(--accent); margin: 1rem 0; padding: 0.2rem 0 0.2rem 1rem; color: var(--muted); }
.doc-figure { margin: 1.25rem 0; padding: 1rem; background: rgba(15,23,42,0.5); border: 1px solid #334155; border-radius: 10px; overflow-x: auto; }
.doc-figure svg.doc-diagram { width: 100%; height: auto; min-width: 640px; display: block; }

/* --- Portal-Abgleich --- */
.fetch-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.dash-links { display: flex; gap: 0.6rem; flex-wrap: wrap; margin-top: 1rem; }
table.compact { font-size: 0.82rem; }
/* Kompaktes Padding; nur die Datenzellen bleiben einzeilig. Die (langen)
   Kennzahlen-Überschriften dürfen umbrechen – das war der eigentliche
   Breiten-Treiber, der den Scrollbalken auch bei breitem Fenster erzwang. */
table.compact th, table.compact td { padding: 0.35rem 0.45rem; }
table.compact th { white-space: normal; vertical-align: bottom; }
.metric-cell { text-align: center; white-space: nowrap; font-variant-numeric: tabular-nums; }
.metric-cell .badge { font-size: 0.7rem; }
