:root {
    --bg: #eef2f7;
    --surface: #ffffff;
    --surface-2: #f6f8fb;
    --border: #e2e8f0;
    --text: #1a202c;
    --muted: #64748b;
    --accent: #4f46e5;
    --accent-hover: #4338ca;
    --solar: #f59e0b;
    --battery: #10b981;
    --load: #ef4444;
    --grid: #6366f1;
    --ok: #16a34a;
    --warn: #d97706;
    --bad: #dc2626;
    --error-bg: #fee2e2;
    --error-text: #b91c1c;
    --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 8px 24px rgba(15, 23, 42, .06);
    --radius: 14px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0b1120;
        --surface: #131c2e;
        --surface-2: #1a2438;
        --border: #263247;
        --text: #e5e9f0;
        --muted: #94a3b8;
        --accent: #818cf8;
        --accent-hover: #a5b4fc;
        --error-bg: #3b1219;
        --error-text: #fca5a5;
        --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    }
}

* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

body {
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    line-height: 1.45;
    -webkit-font-smoothing: antialiased;
}

.sprite { display: none; }

.icon, .brand-icon {
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}
.icon { width: 20px; height: 20px; flex-shrink: 0; }
.brand-icon { width: 48px; height: 48px; color: var(--solar); }
.brand-icon.small { width: 36px; height: 36px; }

/* ============= LOGIN ============= */
.login {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px 16px;
}

.login-card {
    width: 100%;
    max-width: 380px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 36px 32px;
}

.brand { text-align: center; margin-bottom: 28px; }
.brand h1 { font-size: 24px; margin-top: 8px; }
.brand p { color: var(--muted); font-size: 14px; margin-top: 4px; }

form label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    margin: 16px 0 6px;
}

form input {
    width: 100%;
    padding: 11px 14px;
    font: inherit;
    color: var(--text);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 9px;
}

form input:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
    border-color: transparent;
}

.btn-primary {
    width: 100%;
    margin-top: 24px;
    padding: 12px;
    font: inherit;
    font-weight: 600;
    color: #fff;
    background: var(--accent);
    border: 0;
    border-radius: 9px;
    cursor: pointer;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: .6; cursor: progress; }

.btn-ghost {
    padding: 8px 14px;
    font: inherit;
    font-size: 14px;
    color: var(--text);
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
}
.btn-ghost:hover { background: var(--surface-2); }

button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.error-message, .notice {
    background: var(--error-bg);
    color: var(--error-text);
    padding: 10px 14px;
    border-radius: 9px;
    font-size: 14px;
}

/* ============= DASHBOARD ============= */
.dashboard { max-width: 1200px; margin: 0 auto; padding: 0 20px 24px; }

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 16px;
    padding: 24px 0;
}

.title { display: flex; align-items: center; gap: 12px; }
.title h1 { font-size: 22px; }
.subtle { color: var(--muted); font-size: 13px; }

.topbar-right { display: flex; align-items: center; gap: 12px; }

.status {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    font-weight: 600;
    padding: 6px 12px;
    border-radius: 999px;
    background: var(--surface);
    border: 1px solid var(--border);
}

.status-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.status[data-state="live"] .status-dot { background: var(--ok); animation: pulse 2s infinite; }
.status[data-state="stale"] .status-dot { background: var(--warn); }
.status[data-state="error"] .status-dot { background: var(--bad); }

@keyframes pulse { 50% { opacity: .45; } }

@media (prefers-reduced-motion: reduce) {
    .status-dot { animation: none !important; }
    .meter-fill { transition: none !important; }
}

.content { display: grid; gap: 20px; }

.metrics {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 16px;
}

.card, .panel, .stats {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.card { padding: 20px; border-top: 4px solid var(--c); }
.card.solar, .flow-node.solar { --c: var(--solar); }
.card.battery, .flow-node.battery { --c: var(--battery); }
.card.load, .flow-node.load { --c: var(--load); }
.card.grid, .flow-node.grid { --c: var(--grid); }

.card-head {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: .06em;
}
.card-head .icon { color: var(--c); }

.value {
    font-size: 42px;
    font-weight: 700;
    letter-spacing: -.02em;
    margin: 10px 0 6px;
    font-variant-numeric: tabular-nums;
}

.unit { font-size: 16px; font-weight: 500; color: var(--muted); margin-left: 6px; }

.detail { font-size: 13px; color: var(--muted); margin-top: 4px; }
.detail strong { color: var(--text); font-variant-numeric: tabular-nums; }

.meter {
    height: 8px;
    margin: 8px 0 10px;
    background: var(--surface-2);
    border-radius: 999px;
    overflow: hidden;
}
.meter-fill {
    height: 100%;
    width: 0;
    background: var(--battery);
    border-radius: inherit;
    transition: width .4s ease;
}

.panel { padding: 20px; }
.panel h2 { font-size: 16px; margin-bottom: 16px; }

.flow {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
}

.flow-node {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 16px 8px;
    border-radius: 12px;
    background: var(--surface-2);
    position: relative;
}
.flow-node .icon { width: 28px; height: 28px; color: var(--c); }
.flow-label { font-size: 12px; color: var(--muted); }
.flow-value { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }

.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 0;
}
.stat { padding: 20px; border-right: 1px solid var(--border); }
.stat:last-child { border-right: 0; }
.stat-label { font-size: 13px; font-weight: 600; color: var(--muted); }
.stat-value { font-size: 30px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.stat-hint { font-size: 12px; color: var(--muted); }

.footer {
    margin-top: 20px;
    font-size: 12px;
    color: var(--muted);
    text-align: center;
}

@media (max-width: 640px) {
    .value { font-size: 34px; }
    .flow { grid-template-columns: repeat(2, 1fr); }
    .stat { border-right: 0; border-bottom: 1px solid var(--border); }
    .stat:last-child { border-bottom: 0; }
    .topbar-right { width: 100%; justify-content: space-between; }
}
