:root {
    --bg: #0f1117;
    --surface: #1a1d27;
    --border: #2a2d3a;
    --text: #e1e4ed;
    --text-muted: #8b8fa3;
    --solar: #f5a623;
    --grid: #5b8def;
    --battery: #4ecdc4;
    --load: #e85d75;
    --heatpump: #c084fc;
    --positive: #4ecdc4;
    --negative: #e85d75;
    --accent: #5b8def;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    padding: 1.5rem;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
}

header h1 {
    font-size: 1.25rem;
    font-weight: 600;
}

.status {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--negative);
    transition: background 0.3s;
}

.status-dot.connected { background: var(--positive); }

/* === Energy Overview row === */
.overview {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 0.75rem;
    margin-bottom: 1.25rem;
}

.overview-tile {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.85rem 1rem;
    text-align: center;
}

.overview-tile .label {
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
}

.overview-tile .value {
    font-size: 1.4rem;
    font-weight: 700;
    line-height: 1.2;
}

.overview-tile .value .unit {
    font-size: 0.7rem;
    font-weight: 400;
    color: var(--text-muted);
}

.overview-tile .sub {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-top: 0.15rem;
}

.clr-solar    { color: var(--solar); }
.clr-grid     { color: var(--grid); }
.clr-battery  { color: var(--battery); }
.clr-load     { color: var(--load); }
.clr-heatpump { color: var(--heatpump); }

/* === Card grid === */
.grid-layout {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 1rem;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1.25rem;
}

.card-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.card-icon {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
}

.card-icon.solar    { background: rgba(245,166,35,0.15); color: var(--solar); }
.card-icon.grid     { background: rgba(91,141,239,0.15); color: var(--grid); }
.card-icon.battery  { background: rgba(78,205,196,0.15); color: var(--battery); }
.card-icon.load     { background: rgba(232,93,117,0.15); color: var(--load); }
.card-icon.heatpump { background: rgba(192,132,252,0.15); color: var(--heatpump); }

.card-title {
    font-size: 0.85rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
}

.card-state {
    margin-left: auto;
    font-size: 0.7rem;
    font-weight: 500;
    padding: 0.15em 0.55em;
    border-radius: 4px;
    text-transform: capitalize;
}

.power-value {
    font-size: 2rem;
    font-weight: 700;
    line-height: 1.2;
    margin-bottom: 0.75rem;
}

.power-value .unit {
    font-size: 0.9rem;
    font-weight: 400;
    color: var(--text-muted);
    margin-left: 0.15em;
}

.detail-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem 1rem;
}

.detail-grid.three-col {
    grid-template-columns: 1fr 1fr 1fr;
}

.detail-item {
    display: flex;
    flex-direction: column;
}

.detail-label {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.detail-value {
    font-size: 0.95rem;
    font-weight: 500;
}

.detail-value .unit {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* PV strings */
.pv-strings {
    margin-top: 0.75rem;
    padding-top: 0.75rem;
    border-top: 1px solid var(--border);
    display: grid;
    grid-template-columns: 1fr 5.5rem 9rem;
    gap: 0 0.75rem;
}

.pv-strings h3 {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
    margin-bottom: 0.5rem;
    grid-column: 1 / -1;
}

.pv-string-row { display: contents; }
.pv-string-row > span { padding: 0.35rem 0; }
.pv-string-row + .pv-string-row > span { border-top: 1px solid var(--border); }
.pv-string-name  { font-size: 0.8rem; color: var(--text-muted); }
.pv-string-power { font-size: 0.9rem; font-weight: 600; text-align: right; }
.pv-string-detail{ font-size: 0.75rem; color: var(--text-muted); text-align: right; }

/* Battery bar */
.battery-bar-container { margin-bottom: 0.75rem; }
.battery-bar-bg {
    width: 100%; height: 8px; background: var(--border);
    border-radius: 4px; overflow: hidden;
}
.battery-bar {
    height: 100%; border-radius: 4px; background: var(--battery);
    transition: width 0.6s ease;
}
.battery-soc {
    font-size: 2rem; font-weight: 700; line-height: 1.2;
}
.battery-soc .unit {
    font-size: 0.9rem; font-weight: 400; color: var(--text-muted);
}
.battery-power-row {
    display: flex; justify-content: space-between; align-items: baseline;
    margin-bottom: 0.75rem;
}

/* Flow indicators */
.flow-indicator {
    font-size: 0.8rem; font-weight: 500; padding: 0.15em 0.5em;
    border-radius: 4px;
}
.flow-indicator.charging   { background: rgba(78,205,196,0.15); color: var(--positive); }
.flow-indicator.discharging{ background: rgba(232,93,117,0.15); color: var(--negative); }
.flow-indicator.idle       { background: rgba(139,143,163,0.15); color: var(--text-muted); }
.flow-indicator.exporting  { background: rgba(78,205,196,0.15); color: var(--positive); }
.flow-indicator.importing  { background: rgba(232,93,117,0.15); color: var(--negative); }

/* Heat pump temps */
.temp-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.4rem 0;
}
.temp-row + .temp-row { border-top: 1px solid var(--border); }
.temp-label { font-size: 0.8rem; color: var(--text-muted); }
.temp-values { display: flex; gap: 0.75rem; align-items: baseline; }
.temp-actual {
    font-size: 1rem; font-weight: 600;
}
.temp-target {
    font-size: 0.8rem; color: var(--text-muted);
}
.temp-actual .unit, .temp-target .unit {
    font-size: 0.7rem; color: var(--text-muted);
}

.section-divider {
    border: none;
    border-top: 1px solid var(--border);
    margin: 0.75rem 0;
}

.no-data {
    color: var(--text-muted);
    font-size: 0.85rem;
    font-style: italic;
    padding: 1rem 0;
}

/* === Energy Flow Diagram === */
.flow-diagram {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1rem;
    margin-bottom: 1.25rem;
    display: flex;
    justify-content: center;
    overflow: hidden;
}

.flow-diagram svg {
    width: 100%;
    max-width: 600px;
    height: auto;
}

.flow-node-bg {
    fill: var(--bg);
    stroke: var(--border);
    stroke-width: 1.5;
}

.flow-node-icon {
    font-size: 22px;
    text-anchor: middle;
    dominant-baseline: central;
}

.flow-node-label {
    fill: var(--text-muted);
    font-size: 10px;
    text-anchor: middle;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-family: inherit;
}

.flow-node-value {
    fill: var(--text);
    font-size: 13px;
    font-weight: 700;
    text-anchor: middle;
    font-family: inherit;
}

.flow-path {
    fill: none;
    stroke: var(--border);
    stroke-width: 2;
    stroke-linecap: round;
}

.flow-path.active {
    stroke-opacity: 0.4;
}

.flow-particle {
    opacity: 0;
    transition: opacity 0.3s;
    filter: url(#glow);
}

.flow-particle.active {
    opacity: 1;
}

.flow-path.solar.active    { stroke: var(--solar); }
.flow-path.grid.active     { stroke: var(--grid); }
.flow-path.battery.active  { stroke: var(--battery); }
.flow-path.heatpump.active { stroke: var(--heatpump); }

.flow-particle.solar    { fill: var(--solar); }
.flow-particle.grid     { fill: var(--grid); }
.flow-particle.battery  { fill: var(--battery); }
.flow-particle.heatpump { fill: var(--heatpump); }
.flow-particle.load     { fill: var(--load); }

/* === History Chart === */
.history-section {
    margin-top: 1.25rem;
}

.history-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1.25rem;
}

.history-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}

.history-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.history-title .card-icon {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    background: rgba(91,141,239,0.15);
    color: var(--accent);
}

.history-title .card-title {
    font-size: 0.85rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
}

.history-ranges {
    margin-left: auto;
    display: flex;
    gap: 0.35rem;
}

.history-range-btn {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-muted);
    padding: 0.3em 0.7em;
    border-radius: 6px;
    font-size: 0.75rem;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.2s;
}

.history-range-btn:hover {
    border-color: var(--accent);
    color: var(--text);
}

.history-range-btn.active {
    background: rgba(91,141,239,0.15);
    border-color: var(--accent);
    color: var(--accent);
}

.history-chart-wrap {
    position: relative;
    width: 100%;
    height: 280px;
}

.history-chart-wrap canvas {
    width: 100%;
    height: 100%;
    display: block;
}

.history-legend {
    display: flex;
    gap: 1rem;
    margin-top: 0.75rem;
    flex-wrap: wrap;
    justify-content: center;
}

.history-legend-item {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.75rem;
    color: var(--text-muted);
    cursor: pointer;
    user-select: none;
}

.history-legend-item.disabled {
    opacity: 0.35;
}

.history-legend-swatch {
    width: 12px;
    height: 3px;
    border-radius: 2px;
}

.history-no-data {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-muted);
    font-size: 0.85rem;
    font-style: italic;
}

.history-tooltip {
    position: absolute;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.5rem 0.75rem;
    font-size: 0.75rem;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s;
    z-index: 10;
    white-space: nowrap;
}

.history-tooltip.visible { opacity: 1; }

.history-tooltip-time {
    color: var(--text-muted);
    margin-bottom: 0.3rem;
    font-size: 0.7rem;
}

.history-tooltip-row {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    line-height: 1.5;
}

.history-tooltip-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

@media (max-width: 700px) {
    body { padding: 1rem; }
    .power-value, .battery-soc { font-size: 1.6rem; }
    .overview-tile .value { font-size: 1.1rem; }
    .detail-grid.three-col { grid-template-columns: 1fr 1fr; }
    .flow-diagram { padding: 0.5rem; }
    .history-chart-wrap { height: 220px; }
    .history-header { gap: 0.5rem; }
    .history-ranges { margin-left: 0; }
}

/* ── Header right group ───────────────────────────────────────────── */

.header-right {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.logout-btn {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-muted);
    font-size: 0.75rem;
    padding: 0.3rem 0.7rem;
    border-radius: 6px;
    cursor: pointer;
    transition: border-color 0.15s, color 0.15s;
}

.logout-btn:hover {
    border-color: var(--text-muted);
    color: var(--text);
}

/* ── Login overlay ────────────────────────────────────────────────── */

.login-overlay {
    position: fixed;
    inset: 0;
    background: var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 1.5rem;
}

.login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 2.5rem 2rem;
    width: 100%;
    max-width: 360px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}

.login-title {
    font-size: 1.3rem;
    font-weight: 700;
    text-align: center;
    margin-bottom: 0.4rem;
}

.login-subtitle {
    font-size: 0.85rem;
    color: var(--text-muted);
    text-align: center;
    margin-bottom: 1.8rem;
}

.login-field {
    margin-bottom: 1rem;
}

.login-input {
    width: 100%;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    font-size: 1rem;
    padding: 0.65rem 0.9rem;
    outline: none;
    transition: border-color 0.15s;
}

.login-input:focus {
    border-color: var(--accent);
}

.login-error {
    min-height: 1.2rem;
    font-size: 0.8rem;
    color: var(--negative);
    margin-bottom: 0.75rem;
    text-align: center;
}

.login-submit {
    width: 100%;
    background: var(--accent);
    border: none;
    border-radius: 8px;
    color: #fff;
    font-size: 0.95rem;
    font-weight: 600;
    padding: 0.7rem;
    cursor: pointer;
    transition: opacity 0.15s;
}

.login-submit:hover { opacity: 0.85; }
.login-submit:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── Card clickable ───────────────────────────────────────────────── */

.card { cursor: pointer; transition: border-color 0.15s; }
.card:hover { border-color: var(--text-muted); }

/* ── Adapter Detail Modal ─────────────────────────────────────────── */

dialog#adapterModal {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 0;
    width: min(680px, 95vw);
    max-height: 90vh;
    overflow: hidden;
    display: none;
    flex-direction: column;
    color: var(--text);
    box-shadow: 0 16px 48px rgba(0,0,0,0.6);
    position: fixed;
    top: 5vh;
    left: 50%;
    transform: translateX(-50%);
    margin: 0;
}

dialog#adapterModal[open] {
    display: flex;
}

dialog#adapterModal::backdrop {
    background: rgba(0,0,0,0.65);
    backdrop-filter: blur(2px);
}

dialog#adapterModal[open] {
    display: flex;
}

.modal-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1.25rem 1.5rem 1rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.modal-header-left {
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

.modal-icon {
    font-size: 1.3rem;
    line-height: 1;
}

.modal-title {
    font-size: 1.1rem;
    font-weight: 700;
    margin: 0;
}

.modal-status {
    font-size: 0.78rem;
    font-weight: 500;
    padding: 0.2rem 0.55rem;
    border-radius: 999px;
    background: rgba(78,205,196,0.15);
    color: var(--battery);
}

.modal-status.stale {
    background: rgba(200,200,200,0.12);
    color: var(--text-muted);
}

.modal-status.disconnected {
    background: rgba(232,93,117,0.15);
    color: var(--negative);
}

.modal-close {
    background: none;
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-muted);
    font-size: 1.3rem;
    line-height: 1;
    padding: 0.15rem 0.5rem;
    cursor: pointer;
    transition: border-color 0.15s, color 0.15s;
    flex-shrink: 0;
}

.modal-close:hover {
    border-color: var(--text-muted);
    color: var(--text);
}

.modal-body {
    padding: 1.25rem 1.5rem;
    overflow-y: auto;
    flex: 1;
}

/* Modal sections */
.modal-section {
    margin-bottom: 1.5rem;
}

.modal-section:last-child {
    margin-bottom: 0;
}

.modal-section-title {
    font-size: 0.78rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    margin-bottom: 0.65rem;
    display: block;
}

.modal-source-label {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-muted);
    margin: 0.75rem 0 0.4rem;
    padding-bottom: 0.3rem;
    border-bottom: 1px solid var(--border);
}

/* Modal data grid */
.modal-detail-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.35rem 1rem;
}

.modal-detail-item {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
}

.modal-detail-label {
    font-size: 0.73rem;
    color: var(--text-muted);
}

.modal-detail-value {
    font-size: 0.92rem;
    font-weight: 500;
}

/* Modal stale overlay */
.modal-header.stale .modal-status::after {
    content: " · stale";
}

/* Modal chart section */
.modal-chart-section {
    border-top: 1px solid var(--border);
    padding: 1rem 1.5rem 1.25rem;
    flex-shrink: 0;
    background: var(--surface);
}

.modal-chart-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.75rem;
}

.modal-ranges {
    display: flex;
    gap: 0.3rem;
}

.history-chart-wrap canvas {
    width: 100%;
    height: auto;
    display: block;
}

/* Responsive */
@media (max-width: 480px) {
    dialog#adapterModal {
        width: 100vw;
        max-height: 100vh;
        border-radius: 0;
        margin: 0;
        max-width: 100vw;
    }

    .modal-detail-grid {
        grid-template-columns: 1fr;
    }

    .modal-header {
        padding: 1rem;
    }

    .modal-body {
        padding: 1rem;
    }

    .modal-chart-section {
        padding: 0.75rem 1rem 1rem;
    }
}
