:root {
    color-scheme: dark;
    --bg: #0b1220;
    --surface: #131c2e;
    --surface-2: #1c2840;
    --line: #243049;
    --text: #e6edf7;
    --muted: #8696b4;
    --amber: #f59e0b;
    --emerald: #10b981;
    --violet: #8b5cf6;
    --rose: #f43f5e;
    --slate: #64748b;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", system-ui, sans-serif;
}

code { font-family: ui-monospace, "JetBrains Mono", Menlo, Consolas, monospace; font-size: 0.92em; }

.dash {
    width: 100%;
    margin: 0 auto;
    padding: 1.5rem 1.5rem 4rem;
}

.dash-header {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.dash-header h1 {
    margin: 0;
    font-size: 1.6rem;
    font-weight: 600;
    letter-spacing: -0.01em;
}

.meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem 0.75rem;
    color: var(--muted);
    font-size: 0.9rem;
    flex: 1 1 auto;
    min-width: 0;
}

.refresh {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 0.35rem 0.7rem;
    font-size: 0.85rem;
    cursor: pointer;
}
.refresh:hover { background: var(--line); }
.refresh:disabled { opacity: 0.5; cursor: not-allowed; }

.tuya-toggle {
    border-radius: 6px;
    padding: 0.35rem 0.8rem;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    border: 1px solid transparent;
}
.tuya-toggle:disabled { opacity: 0.5; cursor: not-allowed; }
.tuya-toggle-on  { background: rgba(16, 185, 129, 0.18); color: #6ee7b7; border-color: rgba(16, 185, 129, 0.4); }
.tuya-toggle-on:hover  { background: rgba(16, 185, 129, 0.32); }
.tuya-toggle-off { background: rgba(244, 63, 94, 0.18); color: #fda4af; border-color: rgba(244, 63, 94, 0.4); }
.tuya-toggle-off:hover { background: rgba(244, 63, 94, 0.32); }

.auto { display: inline-flex; align-items: center; gap: 0.3rem; font-size: 0.85rem; }

.meta-right {
    margin-left: auto;
    display: inline-flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.4rem 0.5rem;
}

.tiles {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.tile {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 1.1rem 1.25rem;
    border-top: 3px solid var(--slate);
}

.tile-amber   { border-top-color: var(--amber); }
.tile-emerald { border-top-color: var(--emerald); }
.tile-violet  { border-top-color: var(--violet); }
.tile-rose    { border-top-color: var(--rose); }
.tile-slate   { border-top-color: var(--slate); }

.tile-label {
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--muted);
}
.tile-value {
    font-size: 2rem;
    font-weight: 600;
    margin-top: 0.35rem;
}
.tile-sub {
    font-size: 0.85rem;
    color: var(--muted);
    margin-top: 0.2rem;
}

.card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 1.1rem 1.25rem;
    margin-bottom: 1.5rem;
}
.card h2 {
    margin: 0 0 0.9rem 0;
    font-size: 1rem;
    font-weight: 600;
    color: var(--muted);
    letter-spacing: 0.02em;
    text-transform: uppercase;
}
.card h2 code {
    text-transform: none;
    color: var(--text);
    font-weight: 500;
    margin-left: 0.5rem;
}

table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
th { text-align: left; color: var(--muted); font-weight: 500; padding: 0.4rem 0.6rem; border-bottom: 1px solid var(--line); }
td { padding: 0.55rem 0.6rem; border-bottom: 1px solid var(--line); }
tr:last-child td { border-bottom: none; }

.badge {
    display: inline-block;
    font-size: 0.75rem;
    padding: 0.15rem 0.5rem;
    border-radius: 999px;
    background: var(--surface-2);
}
.badge-online  { background: rgba(16, 185, 129, 0.18); color: #6ee7b7; }
.badge-offline { background: rgba(244, 63, 94, 0.18); color: #fda4af; }
.badge-alert   { background: rgba(245, 158, 11, 0.18); color: #fcd34d; }

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 0.6rem;
}
.kv {
    background: var(--surface-2);
    border-radius: 8px;
    padding: 0.55rem 0.75rem;
    border-left: 3px solid transparent;
}
.k { font-size: 0.75rem; color: var(--muted); }
.v { font-size: 1rem; margin-top: 0.15rem; }
.u { color: var(--muted); font-size: 0.85rem; margin-left: 0.2rem; }

.kv-pv        { border-left-color: #f59e0b; }
.kv-ac        { border-left-color: #06b6d4; }
.kv-grid      { border-left-color: #f43f5e; }
.kv-house     { border-left-color: #8b5cf6; }
.kv-battery   { border-left-color: #10b981; }
.kv-temp      { border-left-color: #f97316; }
.kv-daily     { border-left-color: #14b8a6; }
.kv-lifetime  { border-left-color: #64748b; }
.kv-generator { border-left-color: #475569; }
.kv-static    { border-left-color: #1c2840; }

.kv-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    margin-bottom: 0.8rem;
}
.kv-chip {
    font-size: 0.7rem;
    color: var(--muted);
    padding: 0.15rem 0.55rem;
    border-radius: 999px;
    background: var(--surface-2);
    border-left: 3px solid transparent;
}

.error {
    background: rgba(244, 63, 94, 0.1);
    border: 1px solid rgba(244, 63, 94, 0.4);
    color: #fda4af;
    padding: 0.75rem 1rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: 0.85rem;
}

.loading { color: var(--muted); }

.topnav {
    background: var(--surface);
    border-bottom: 1px solid var(--line);
}
.topnav-inner {
    width: 100%;
    margin: 0 auto;
    padding: 0.8rem 1.5rem;
    display: flex;
    align-items: center;
    gap: 1.5rem;
}
.navlinks {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    flex-wrap: wrap;
}
.nav-toggle {
    display: none;
    background: none;
    border: 0;
    color: var(--text);
    font-size: 1.5rem;
    line-height: 1;
    padding: 0.1rem 0.4rem;
    margin-left: auto;
    cursor: pointer;
    border-radius: 6px;
}
.nav-toggle:hover { background: var(--surface-2); }
.brand {
    font-weight: 600;
    color: var(--amber);
    letter-spacing: 0.02em;
}
.navlink {
    color: var(--muted);
    text-decoration: none;
    font-size: 0.92rem;
    padding: 0.25rem 0;
    border-bottom: 2px solid transparent;
}
.navlink:hover { color: var(--text); }
.navlink.active {
    color: var(--text);
    border-bottom-color: var(--amber);
}

.chart-host {
    position: relative;
    height: 420px;
    width: 100%;
}

.status-banner {
    background: var(--surface);
    border-bottom: 1px solid var(--line);
    padding: 0.5rem 1.5rem;
    display: flex;
    flex-wrap: wrap;
    gap: 1rem 1.5rem;
    align-items: center;
    color: var(--text);
    font-size: 0.85rem;
}
.status-banner.sb-warn { color: #fda4af; background: rgba(244, 63, 94, 0.06); }
.sb-section { display: inline-flex; align-items: baseline; gap: 0.4rem; }
.sb-emoji { font-size: 1.05rem; }
.sb-label { color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; font-size: 0.72rem; }
.sb-val { font-weight: 600; }
.sb-sub { color: var(--muted); font-size: 0.8rem; }
.sb-link { margin-left: auto; color: var(--amber); text-decoration: none; font-size: 0.8rem; }
.sb-link:hover { text-decoration: underline; }
.chart-card h2 { margin-bottom: 0.5rem; }

.muted { color: var(--muted); font-size: 0.82rem; }

.gap-pill {
    background: rgba(139, 92, 246, 0.2);
    color: #c4b5fd;
    padding: 0.15rem 0.6rem;
    border-radius: 999px;
    font-size: 0.8rem;
}

.rule-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 0.7rem 1rem;
}
.rule-input {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}
.rule-input span {
    font-size: 0.78rem;
    color: var(--muted);
}
.rule-input input {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 0.4rem 0.6rem;
    font-size: 0.95rem;
}

pre.raw {
    background: var(--bg);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 0.8rem 1rem;
    overflow: auto;
    max-height: 480px;
    color: var(--muted);
    font-size: 0.82rem;
    margin: 0;
}

@media (max-width: 720px) {
    .dash { padding: 1rem 0.85rem 3rem; }
    .dash-header { gap: 0.5rem; margin-bottom: 1rem; }
    .dash-header h1 { font-size: 1.25rem; }

    .meta { font-size: 0.8rem; gap: 0.35rem 0.55rem; }
    .meta-right { margin-left: 0; width: 100%; justify-content: flex-start; }

    .refresh, .tuya-toggle { padding: 0.3rem 0.55rem; font-size: 0.8rem; }

    .topnav-inner {
        padding: 0.55rem 0.85rem;
        gap: 0.6rem;
        flex-wrap: wrap;
        align-items: center;
    }
    .nav-toggle { display: inline-block; }
    .navlinks {
        display: none;
        width: 100%;
        flex-direction: column;
        align-items: stretch;
        gap: 0;
        margin-top: 0.4rem;
        border-top: 1px solid var(--line);
        padding-top: 0.3rem;
    }
    .topnav-inner.nav-open .navlinks { display: flex; }
    .navlinks .navlink {
        padding: 0.6rem 0.4rem;
        border-bottom: 1px solid var(--line);
        border-radius: 0;
        font-size: 0.95rem;
    }
    .navlinks .navlink:last-child { border-bottom: none; }
    .navlinks .navlink.active { border-bottom-color: var(--amber); }

    .status-banner {
        padding: 0.5rem 0.85rem;
        gap: 0.4rem 1rem;
        font-size: 0.78rem;
    }
    .sb-link { margin-left: 0; }

    .tiles {
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
        gap: 0.55rem;
        margin-bottom: 1rem;
    }
    .tile { padding: 0.75rem 0.85rem; }
    .tile-value { font-size: 1.35rem; }
    .tile-label { font-size: 0.68rem; }
    .tile-sub { font-size: 0.72rem; }

    .card {
        padding: 0.85rem 0.9rem;
        margin-bottom: 1rem;
        border-radius: 10px;
    }
    .card h2 { font-size: 0.85rem; margin-bottom: 0.65rem; }

    .chart-host { height: 340px; }

    .grid, .rule-grid {
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
        gap: 0.5rem;
    }

    table { font-size: 0.82rem; }
    th, td { padding: 0.35rem 0.4rem; }

    /* Wide tables (devices, peak demand) scroll horizontally rather than overflowing the card. */
    .card { overflow-x: auto; }
    table { white-space: nowrap; }
}
