:root {
    --bg: #f2f4f7;
    --bg-elevated: #ffffff;
    --bg-subtle: #f8f9fb;
    --text: #1a1d21;
    --text-muted: #667085;
    --border: #e4e7ec;
    --accent: #d97706;
    --accent-hover: #b45f04;
    --accent-contrast: #ffffff;
    --pv: #ca8a04;
    --grid: #6b7280;
    --charge: #2563eb;
    --danger: #dc2626;
    --ok: #16a34a;
    --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
    --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
    --radius: 12px;
    --radius-sm: 8px;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #101214;
        --bg-elevated: #1a1d21;
        --bg-subtle: #202329;
        --text: #edeef0;
        --text-muted: #9aa2ad;
        --border: #2b3036;
        --accent: #f59e0b;
        --accent-hover: #fbbf24;
        --accent-contrast: #14171a;
        --pv: #eab308;
        --grid: #9aa2ad;
        --charge: #60a5fa;
        --danger: #f87171;
        --ok: #4ade80;
        --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 12px rgba(0, 0, 0, 0.35);
        --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    }
}

:root[data-theme="dark"] {
    --bg: #101214;
    --bg-elevated: #1a1d21;
    --bg-subtle: #202329;
    --text: #edeef0;
    --text-muted: #9aa2ad;
    --border: #2b3036;
    --accent: #f59e0b;
    --accent-hover: #fbbf24;
    --accent-contrast: #14171a;
    --pv: #eab308;
    --grid: #9aa2ad;
    --charge: #60a5fa;
    --danger: #f87171;
    --ok: #4ade80;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 12px rgba(0, 0, 0, 0.35);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }

html { color-scheme: light dark; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
    line-height: 1.5;
}

header.app-header {
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
    backdrop-filter: blur(6px);
}

.app-header-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.75rem 1.5rem;
}

header.app-header h1 {
    font-size: 1.15rem;
    font-weight: 700;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    letter-spacing: -0.01em;
}

.nav-toggle {
    display: none;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    width: 2.25rem;
    height: 2.25rem;
    padding: 0;
    cursor: pointer;
}

.nav-toggle span {
    display: block;
    width: 16px;
    height: 2px;
    margin: 0 auto;
    background: var(--text);
    border-radius: 1px;
}

nav.app-nav {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    flex-wrap: wrap;
    padding: 0 1.5rem 0.75rem;
}

nav.app-nav a {
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 500;
    padding: 0.45rem 0.7rem;
    border-radius: var(--radius-sm);
    transition: background-color 0.15s ease, color 0.15s ease;
}

nav.app-nav a.active {
    color: var(--accent);
    background: color-mix(in srgb, var(--accent) 14%, transparent);
}

nav.app-nav a:hover {
    color: var(--accent);
    background: var(--bg-subtle);
}

main {
    padding: 1.5rem;
    max-width: 1100px;
    margin: 0 auto;
}

h2 {
    font-size: 1.05rem;
    font-weight: 600;
    margin: 0 0 0.9rem;
    letter-spacing: -0.01em;
}

.card {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    padding: 1.25rem 1.5rem;
    margin-bottom: 1.25rem;
    transition: box-shadow 0.2s ease;
}

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
    gap: 1.5rem;
}

.stat-label {
    color: var(--text-muted);
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

.stat-icon {
    width: 15px;
    height: 15px;
    flex-shrink: 0;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.6;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.stat-value {
    font-size: 1.9rem;
    font-weight: 700;
    margin-top: 0.3rem;
    letter-spacing: -0.02em;
    font-variant-numeric: tabular-nums;
}

.stat-value.pv { color: var(--pv); }
.stat-value.grid { color: var(--grid); }
.stat-value.charge { color: var(--charge); }
.stat-value.danger { color: var(--danger); }
.stat-value.ok { color: var(--ok); }

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

th, td {
    text-align: left;
    padding: 0.65rem 0.6rem;
    border-bottom: 1px solid var(--border);
}

th { color: var(--text-muted); font-weight: 600; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; }

tbody tr:hover {
    background: var(--bg-subtle);
}

tbody tr:last-child td {
    border-bottom: none;
}

form.device-form {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 0.9rem;
    align-items: end;
}

label {
    display: block;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-muted);
    margin-bottom: 0.3rem;
}

input, select {
    width: 100%;
    padding: 0.5rem 0.65rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--bg-subtle);
    color: var(--text);
    font-size: 0.9rem;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input:focus, select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}

input[type="checkbox"] {
    width: auto;
}

button, .btn {
    background: var(--accent);
    color: var(--accent-contrast);
    border: none;
    border-radius: var(--radius-sm);
    padding: 0.55rem 1rem;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    display: inline-block;
    transition: background-color 0.15s ease, transform 0.05s ease;
}

button:hover, .btn:hover { background: var(--accent-hover); }
button:active, .btn:active { transform: translateY(1px); }

button.secondary {
    background: transparent;
    color: var(--text);
    border: 1px solid var(--border);
}

button.secondary:hover {
    background: var(--bg-subtle);
    color: var(--text);
}

.mode-switch {
    display: inline-flex;
    gap: 0.25rem;
    padding: 0.25rem;
    background: var(--bg-subtle);
    border-radius: calc(var(--radius-sm) + 0.25rem);
}

.mode-switch button.secondary {
    border: none;
    color: var(--text-muted);
}

.mode-switch button.secondary.selected {
    background: var(--bg-elevated);
    color: var(--accent);
    box-shadow: var(--shadow-sm);
    font-weight: 700;
}

.badge {
    display: inline-block;
    padding: 0.2rem 0.6rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 600;
}

.badge.ok { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.badge.danger { background: color-mix(in srgb, var(--danger) 18%, transparent); color: var(--danger); }

.theme-toggle {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: var(--radius-sm);
    padding: 0.4rem 0.65rem;
    font-size: 1rem;
    line-height: 1;
}

.theme-toggle:hover {
    background: var(--bg-subtle);
}

.hint {
    color: var(--text-muted);
    font-size: 0.85rem;
    margin: 0 0 1rem;
}

@media (max-width: 480px) {
    .nav-toggle {
        display: flex;
    }

    nav.app-nav {
        display: none;
        flex-direction: column;
        align-items: stretch;
        gap: 0.15rem;
        padding: 0.25rem 1rem 0.75rem;
    }

    nav.app-nav.nav-open {
        display: flex;
    }

    nav.app-nav a {
        padding: 0.6rem 0.7rem;
    }
}

@media (max-width: 640px) {
    main {
        padding: 1rem;
    }

    .stat-value {
        font-size: 1.5rem;
    }
}

/* Energiefluss-Diagramm (Live-Dashboard) */
.flow-diagram-wrap {
    max-width: 460px;
    margin: 0 auto;
}

.flow-diagram {
    width: 100%;
    height: auto;
    overflow: visible;
}

.flow-node circle {
    transition: stroke 0.2s ease;
}

.flow-icon {
    fill: none;
    stroke: currentColor;
    stroke-width: 1.6;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.flow-node-label {
    fill: var(--text-muted);
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.flow-label {
    fill: var(--text);
    font-size: 12px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.flow-line {
    transition: stroke-width 0.4s ease, opacity 0.3s ease;
}

.flow-line-idle {
    opacity: 0.3;
}

.flow-line-active {
    opacity: 1;
    stroke-dasharray: 9 7;
    animation: flow-dash 0.6s linear infinite;
    filter: drop-shadow(0 0 3px color-mix(in srgb, currentColor 55%, transparent));
}

.flow-line-active.flow-line-reversed {
    animation-direction: reverse;
}

@keyframes flow-dash {
    to { stroke-dashoffset: -32; }
}

@media (prefers-reduced-motion: reduce) {
    .flow-line-active {
        animation: none;
    }
}
