/* Interfaccia utente: portfolio e dashboard impianto.
   Colori come token: [data-theme] su <html> sceglie chiaro o scuro. */

:root,
[data-theme="light"] {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --surface-2: #f0f3f7;
    --border: #dde3ea;
    --text: #1b2430;
    --text-muted: #667085;
    --accent: #1f6feb;
    --accent-soft: rgba(31, 111, 235, 0.12);
    --solar: #f59e0b;
    --ok: #16a34a;
    --warning: #d97706;
    --error: #dc2626;
    --nodata: #94a3b8;
    --grid: rgba(27, 36, 48, 0.08);
    --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
    color-scheme: light;
}

[data-theme="dark"] {
    --bg: #0f141b;
    --surface: #171e27;
    --surface-2: #1e2732;
    --border: #2a3542;
    --text: #e6ebf1;
    --text-muted: #94a3b8;
    --accent: #4c8dff;
    --accent-soft: rgba(76, 141, 255, 0.16);
    --solar: #fbbf24;
    --ok: #22c55e;
    --warning: #f59e0b;
    --error: #f87171;
    --nodata: #64748b;
    --grid: rgba(230, 235, 241, 0.08);
    --shadow: none;
    color-scheme: dark;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2 { margin: 0; font-weight: 600; }
h1 { font-size: 22px; display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
h2 { font-size: 15px; }
.muted { color: var(--text-muted); }
.strong { font-weight: 600; color: var(--text); }
small { font-size: 0.55em; font-weight: 500; color: var(--text-muted); }

/* --- Barra superiore --- */
.topbar {
    position: sticky; top: 0; z-index: 1000;
    display: flex; align-items: center; gap: 24px;
    padding: 0 24px; height: 56px;
    background: var(--surface); border-bottom: 1px solid var(--border);
}
/* Logo SM Electronics (giallo) con la scritta Energy Monitoring sotto */
.brand { display: flex; flex-direction: column; align-items: center; gap: 2px; color: var(--text-muted); }
.brand:hover { text-decoration: none; }
.brand-logo { height: 34px; width: auto; display: block; }
.brand-name { font-size: 9px; font-weight: 600; letter-spacing: .14em; text-transform: uppercase; white-space: nowrap; }
.nav { display: flex; gap: 4px; flex: 1; }
.nav a { padding: 6px 12px; border-radius: 6px; color: var(--text-muted); font-weight: 500; }
.nav a:hover { background: var(--surface-2); text-decoration: none; }
.nav a.active { color: var(--accent); background: var(--accent-soft); }
.topbar-actions { display: flex; align-items: center; gap: 12px; }
.topbar-actions form { margin: 0; }
.user { color: var(--text-muted); }

.icon-button {
    display: inline-flex; align-items: center; justify-content: center;
    width: 34px; height: 34px; border-radius: 8px;
    border: 1px solid var(--border); background: var(--surface); color: var(--text);
    cursor: pointer; font-size: 18px; line-height: 1;
}
.icon-button:hover { background: var(--surface-2); text-decoration: none; }
.icon-button.disabled { opacity: 0.35; cursor: default; }
.icon-button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
[data-theme="light"] .icon-moon, [data-theme="dark"] .icon-sun { display: none; }

.button, .button-ghost {
    padding: 7px 14px; border-radius: 8px; font: inherit; font-weight: 500; cursor: pointer;
}
.button { background: var(--accent); color: #fff; border: 1px solid var(--accent); }
.button-ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }
.button-ghost:hover { background: var(--surface-2); }

/* --- Pagina --- */
.page { max-width: 1400px; margin: 0 auto; padding: 24px; display: flex; flex-direction: column; gap: 20px; }
.page-header { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; }
.page-header p { margin: 4px 0 0; }
.breadcrumb { display: inline-block; margin-bottom: 6px; font-size: 13px; }

.messages { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.message { padding: 10px 14px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); }
.message.error { border-color: var(--error); color: var(--error); }
.message.success { border-color: var(--ok); color: var(--ok); }

/* --- KPI --- */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 16px; }
.kpi {
    display: flex; flex-direction: column; gap: 4px; padding: 16px 18px;
    background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
}
.kpi-label { color: var(--text-muted); font-size: 12px; font-weight: 500; text-transform: uppercase; letter-spacing: 0.04em; }
.kpi-value { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi-sub { color: var(--text-muted); font-size: 12px; }
.kpi-alert .kpi-value { color: var(--warning); }

/* --- Card --- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); padding: 16px 18px; }
.card-header { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.empty { color: var(--text-muted); margin: 8px 0; }

/* --- Stato --- */
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.status::before { content: ""; width: 9px; height: 9px; border-radius: 50%; background: currentColor; }
.status-ok { color: var(--ok); }
.status-warning { color: var(--warning); }
.status-error { color: var(--error); }
.status-nodata { color: var(--nodata); }

/* --- Tabelle --- */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 12px; font-weight: 600; color: var(--text-muted); padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: none; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.row-link { cursor: pointer; }
.row-link:hover td { background: var(--surface-2); }
.values { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-muted); font-size: 12px; white-space: nowrap; }
.chip strong { color: var(--text); }

/* --- Mappa --- */
.map { height: 380px; border-radius: 8px; overflow: hidden; }
.map-pin { width: 16px; height: 16px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.25); }
.leaflet-popup-content { font: 13px/1.45 inherit; }
.leaflet-container { background: var(--surface-2); }
[data-theme="dark"] .map .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(0.85) contrast(0.9); }

/* --- Grafico e periodo --- */
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.segmented a { padding: 6px 14px; color: var(--text-muted); font-weight: 500; }
.segmented a + a { border-left: 1px solid var(--border); }
.segmented a.active { background: var(--accent-soft); color: var(--accent); }
.segmented a:hover { text-decoration: none; background: var(--surface-2); }
.period-nav { display: flex; align-items: center; gap: 8px; }
.date-form { margin: 0; }
input[type="date"], select {
    padding: 6px 10px; border-radius: 8px; border: 1px solid var(--border);
    background: var(--surface); color: var(--text); font: inherit;
}
.period-kpis { display: flex; gap: 32px; flex-wrap: wrap; margin-bottom: 12px; }
.period-kpis div { display: flex; flex-direction: column; }
.period-kpis strong { font-size: 18px; font-variant-numeric: tabular-nums; }
.chart-box { position: relative; height: 360px; }
.chart-reset { position: absolute; top: 0; right: 0; padding: 3px 10px; font-size: 12px; background: var(--surface); }
.chart-reset[hidden] { display: none; }
/* --- Scelta delle variabili del grafico (pagina dispositivo) --- */
.var-picker { position: relative; }
.var-picker summary { cursor: pointer; padding: 6px 10px; border: 1px solid var(--border); border-radius: 8px;
                      background: var(--surface); list-style: none; }
.var-picker summary::-webkit-details-marker { display: none; }
.var-picker summary::after { content: " ▾"; color: var(--text-muted); }
.var-picker-panel { position: absolute; z-index: 20; top: calc(100% + 4px); left: 0; min-width: 280px; max-width: 90vw;
                    padding: 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
                    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2); display: flex; flex-direction: column; gap: 10px; margin: 0; }
.var-picker-list { display: flex; flex-direction: column; gap: 4px; max-height: 50vh; overflow-y: auto; }
.var-picker-list label { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
/* Serie accendibili del grafico dell'impianto */
.chart-toggles { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.chart-toggle { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px;
                border: 1px solid var(--border); background: transparent; color: var(--text-muted); font: inherit;
                font-size: 12px; cursor: pointer; }
.chart-toggle .dot { width: 9px; height: 9px; border-radius: 50%; background: currentColor; opacity: 0.35; }
.chart-toggle.on { color: var(--text); border-color: var(--text-muted); }
.chart-toggle.on .dot { opacity: 1; }

/* --- Comandi inverter --- */
.command-list { display: flex; flex-direction: column; }
.command-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap;
               padding: 10px 0; border-top: 1px solid var(--border); margin: 0; }
.command-name { display: flex; flex-direction: column; gap: 2px; }
.command-name .muted { font-size: 12px; }
.command-input { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.command-number { width: 110px; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--border);
                  background: var(--surface); color: var(--text); font: inherit; }
.command-confirm { display: flex; gap: 12px; align-items: center; margin-top: 12px; }

/* --- Esporta --- */
.export-form { display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap; }
.export-form label { display: flex; flex-direction: column; gap: 4px; color: var(--text-muted); font-size: 12px; }

@media (max-width: 700px) {
    .topbar { padding: 0 16px; gap: 12px; }
    .brand-name, .user { display: none; }
    .page { padding: 16px; }
    .map { height: 280px; }
    .chart-box { height: 280px; }
}

/* --- Login --- */
.login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card {
    width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 16px; padding: 28px;
    background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
}
.login-card h1 { font-size: 20px; }
.login-card .brand { gap: 6px; padding-bottom: 4px; }
.login-card .brand-logo { height: 72px; }
.login-card .brand-name { font-size: 12px; }
.login-form { display: flex; flex-direction: column; gap: 14px; }
.login-form label { display: flex; flex-direction: column; gap: 6px; color: var(--text-muted); font-size: 13px; }
.login-form input {
    padding: 9px 12px; border-radius: 8px; border: 1px solid var(--border);
    background: var(--surface); color: var(--text); font: inherit;
}
.login-form input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.login-form .button { margin-top: 4px; padding: 10px; }

/* --- Badge e schede --- */
.badge {
    display: inline-flex; align-items: center; justify-content: center; min-width: 18px; height: 18px;
    padding: 0 5px; margin-left: 4px; border-radius: 999px; background: var(--error); color: #fff;
    font-size: 11px; font-weight: 700; line-height: 1;
}
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-top: -8px; }
.tabs a {
    padding: 8px 14px; color: var(--text-muted); font-weight: 500;
    border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a.active { color: var(--accent); border-bottom-color: var(--accent); }

/* --- Allarmi --- */
.card-alert { border-color: var(--error); }
.all-clear::before { content: "✓ "; color: var(--ok); font-weight: 700; }

/* --- Scostamento inverter --- */
.deviation { display: flex; align-items: center; gap: 8px; min-width: 160px; }
.deviation-bar { position: relative; flex: 1; height: 6px; border-radius: 3px; background: var(--surface-2); }
.deviation-bar::before { content: ""; position: absolute; left: 50%; top: -3px; bottom: -3px; width: 1px; background: var(--border); }
.deviation-bar span { position: absolute; top: 0; bottom: 0; width: min(var(--w), 50%); border-radius: 3px; background: var(--ok); }
.deviation-bar span.neg { right: 50%; }
.deviation-bar span.pos { left: 50%; }
.deviation.warn .deviation-bar span { background: var(--warning); }
.deviation.bad .deviation-bar span { background: var(--error); }
.deviation.warn > .num { color: var(--warning); }
.deviation.bad > .num { color: var(--error); font-weight: 600; }

/* --- Pagina device --- */
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 20px; align-items: start; }
.stack { display: flex; flex-direction: column; gap: 20px; }
.buttons { display: flex; flex-wrap: wrap; gap: 10px; }
.buttons form { margin: 0; }
.switch {
    display: inline-flex; align-items: center; gap: 8px; padding: 8px 14px; border-radius: 8px;
    border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; cursor: pointer;
}
.switch:hover { background: var(--surface-2); }
.switch-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--nodata); }
.switch.on .switch-dot { background: var(--ok); }

/* --- Meteo (dashboard impianto) --- */
.weather { display: flex; align-items: center; gap: 32px; flex-wrap: wrap; }
.weather-now { display: flex; align-items: center; gap: 14px; }
.weather-now > div { display: flex; flex-direction: column; }
.weather-icon { font-size: 44px; line-height: 1; }
.weather-temp { font-size: 26px; }
.weather-facts { display: flex; gap: 28px; flex-wrap: wrap; margin: 0; }
.weather-facts dt { color: var(--text-muted); font-size: 12px; }
.weather-facts dd { margin: 2px 0 0; font-weight: 600; }

/* --- Scheda contatore --- */
.meter-forms { display: flex; flex-direction: column; gap: 16px; }
.meter-clock { display: grid; grid-template-columns: max-content 1fr; gap: 8px 24px; margin: 0; }
.meter-clock dt { color: var(--text-muted); }
.meter-clock dd { margin: 0; }
.actions { display: flex; gap: 12px; white-space: nowrap; }
.actions form { margin: 0; }
.actions .button-ghost { padding: 2px 10px; font-size: 12px; }
.error-text { color: var(--error); font-size: 12px; }
.row-selected td { background: var(--accent-soft); }
.row-total td { background: var(--surface-2); }

/* --- Selettore lingua --- */
.lang-switch { margin: 0; }
.lang-switch select {
    padding: 5px 8px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface);
    color: var(--text-muted); font: inherit; font-size: 12px; cursor: pointer;
}
.login-card .lang-switch { align-self: center; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* Accesso per app esterne (pagina del contatore) */
.meter-api-fields { display: grid; grid-template-columns: max-content 1fr; gap: 8px 12px; align-items: center; margin: 12px 0; }
.meter-api-fields dt { color: var(--text-muted); }
.meter-api-fields dd { margin: 0; display: flex; gap: 8px; min-width: 0; }
.meter-api-fields input { flex: 1; min-width: 0; font-family: ui-monospace, monospace; font-size: 13px; padding: 6px 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); color: var(--text); }
.meter-api-reads { width: 100%; border-collapse: collapse; font-size: 13px; }
.meter-api-reads td { padding: 4px 8px 4px 0; border-bottom: 1px solid var(--border); word-break: break-all; }

/* --- Flussi di energia (ui/_energy_flow.html) --- */
.flow-layout { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 24px; align-items: center; }
@media (max-width: 800px) { .flow-layout { grid-template-columns: 1fr; } }
.flow-svg { width: 100%; max-width: 480px; height: auto; justify-self: center; overflow: visible; }
.flow-hub { fill: var(--surface-2); stroke: var(--border); stroke-width: 2; }
.flow-node circle { fill: var(--surface-2); stroke-width: 3; }
.flow-node.pv circle { stroke: #f5b301; }
.flow-node.grid circle { stroke: #8b5cf6; }
.flow-node.load circle { stroke: #3b82f6; }
.flow-node.battery circle { stroke: var(--ok); }
.flow-icon { font-size: 22px; text-anchor: middle; }
.flow-label { fill: var(--text-muted); font-size: 12px; }
.flow-value { fill: var(--text); font-size: 14px; font-weight: 600; }
.flow-line { fill: none; stroke-width: 4; stroke-linecap: round; stroke-dasharray: 2 10; }
.flow-line.idle { stroke: var(--border); stroke-dasharray: none; stroke-width: 2; }
.flow-line.pv { stroke: #f5b301; }
.flow-line.grid { stroke: #8b5cf6; }
.flow-line.load { stroke: #3b82f6; }
.flow-line.battery { stroke: var(--ok); }
.flow-line.to-hub { animation: flow-dash 0.9s linear infinite; }
.flow-line.from-hub { animation: flow-dash 0.9s linear infinite reverse; }
@keyframes flow-dash { from { stroke-dashoffset: 24; } to { stroke-dashoffset: 0; } }
@media (prefers-reduced-motion: reduce) { .flow-line.to-hub, .flow-line.from-hub { animation: none; } }
.flow-today h3 { margin: 0 0 10px; font-size: 15px; }
.flow-totals { display: grid; grid-template-columns: 1fr auto; gap: 6px 16px; margin: 0 0 16px; }
.flow-totals dt { color: var(--text-muted); }
.flow-totals dd { margin: 0; font-weight: 600; text-align: right; }
.flow-ratio { margin-bottom: 12px; }
.flow-ratio-head { display: flex; justify-content: space-between; margin-bottom: 4px; }
.flow-ratio p { margin: 4px 0 0; font-size: 12px; }
.flow-bar { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.flow-bar span { display: block; height: 100%; background: var(--ok); border-radius: 4px; }
.flow-bar.alt span { background: #3b82f6; }
/* --- Form della dashboard (account, invito, procedura guidata) --- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px 20px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > label { color: var(--text-muted); font-size: 13px; font-weight: 500; }
.field input:not([type="checkbox"]), .field select, .field textarea {
    padding: 8px 11px; border-radius: 8px; border: 1px solid var(--border);
    background: var(--surface); color: var(--text); font: inherit; width: 100%;
}
.field input:focus, .field select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.field-check { flex-direction: row; flex-wrap: wrap; align-items: center; gap: 8px; align-self: center; }
.field-check .help, .field-check .errorlist { flex-basis: 100%; }
.field .help { color: var(--text-muted); font-size: 12px; margin: 0; }
.field .errorlist, .form-errors { list-style: none; margin: 0; padding: 0; color: var(--error); font-size: 12px; }
.form-actions { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 18px; }
.form-actions .right { display: flex; gap: 10px; margin-left: auto; }
.button:disabled { opacity: .5; cursor: default; }
.button-danger { background: transparent; color: var(--error); border: 1px solid var(--error); padding: 2px 10px; border-radius: 8px; font: inherit; font-size: 12px; cursor: pointer; }
a.button, a.button-ghost { display: inline-block; text-decoration: none; }
a.button:hover { text-decoration: none; filter: brightness(1.08); }
/* --- Procedura guidata --- */
.wizard { display: flex; gap: 0; list-style: none; margin: 0; padding: 0; counter-reset: step; overflow-x: auto; }
.wizard li { flex: 1; min-width: 92px; display: flex; flex-direction: column; align-items: center; gap: 6px; position: relative; font-size: 12px; color: var(--text-muted); text-align: center; }
.wizard li::before { content: ""; position: absolute; top: 15px; left: -50%; width: 100%; height: 2px; background: var(--border); z-index: 0; }
.wizard li:first-child::before { display: none; }
.wizard li.done::before, .wizard li.current::before { background: var(--accent); }
.wizard .n { position: relative; z-index: 1; width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; background: var(--surface); border: 2px solid var(--border); font-weight: 700; color: var(--text-muted); }
.wizard li.done .n { background: var(--accent); border-color: var(--accent); color: #fff; }
.wizard li.current .n { border-color: var(--accent); color: var(--accent); }
.wizard li.current { color: var(--text); font-weight: 600; }
.wizard a { color: inherit; display: contents; }
.wizard-intro { margin: 0 0 16px; color: var(--text-muted); }
.setup-map { height: 380px; border-radius: 8px; overflow: hidden; margin-bottom: 14px; }
.gw-status { display: grid; grid-template-columns: max-content 1fr; gap: 6px 18px; margin: 0; }
.gw-status dt { color: var(--text-muted); }
.gw-status dd { margin: 0; font-weight: 500; }
.checklist { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.checklist li::before { content: "○ "; color: var(--warning); font-weight: 700; }
.checklist li.ok::before { content: "✓ "; color: var(--ok); }
.port-custom[hidden] { display: none; }
.invite-qr { display: flex; gap: 18px; align-items: center; flex-wrap: wrap; }
.invite-qr svg { width: 148px; height: 148px; background: #fff; border-radius: 6px; }
.invite-qr input { font-family: ui-monospace, monospace; font-size: 12px; }
.login-card.wide { max-width: 520px; }
.account-plants .actions a { font-size: 13px; }
.draft { color: var(--warning); font-size: 12px; font-weight: 600; }
.field[hidden] { display: none; }

/* Titolo di una sezione dentro un form (es. Irraggiamento nella procedura guidata) */
.form-section { font-size: 1rem; margin: 22px 0 6px; }
