/* ============================================
   Dashboard - Tablet-first Responsive CSS
   ============================================ */

:root {
    --bg: #0f1923;
    --bg-card: #1a2733;
    --bg-card-hover: #223344;
    --text: #e8eaed;
    --text-muted: #8899aa;
    --accent-green: #00e676;
    --accent-blue: #40c4ff;
    --accent-orange: #ffab40;
    --accent-red: #ff5252;
    --accent-yellow: #ffd740;
    --border-radius: 12px;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.3);
}

* {
    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;
    overflow-x: hidden;
    -webkit-tap-highlight-color: transparent;
}

/* Header */
header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    background: linear-gradient(135deg, #1a2733, #0f1923);
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    position: sticky;
    top: 0;
    z-index: 100;
}

/* Barra di countdown della rotazione automatica, sul bordo inferiore
   dell'header: si svuota da sinistra a destra nell'arco dell'intervallo. */
.auto-rotate-bar {
    position: absolute;
    left: 0;
    bottom: -1px;
    height: 3px;
    width: 100%;
    background: var(--accent-blue);
    opacity: 0.8;
    transform-origin: left center;
    pointer-events: none;
}

.auto-rotate-bar.running {
    animation: auto-rotate-countdown linear forwards;
}

@keyframes auto-rotate-countdown {
    from { transform: scaleX(1); }
    to { transform: scaleX(0); }
}

header h1 {
    font-size: 1.4rem;
    font-weight: 600;
    color: var(--accent-blue);
}

#nav-tabs {
    display: flex;
    gap: 8px;
}

.tab {
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.15);
    color: var(--text-muted);
    padding: 8px 20px;
    border-radius: 20px;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s;
}

.tab.active {
    background: var(--accent-blue);
    color: #000;
    border-color: var(--accent-blue);
    font-weight: 600;
}

.tab-icon-only {
    padding: 8px 14px;
    font-size: 1.1rem;
    line-height: 1;
}

/* Error Banner */
.error-banner {
    background: var(--accent-red);
    color: white;
    padding: 10px 24px;
    font-size: 0.9rem;
    text-align: center;
}

/* Daikin heating alarm banner: a differenza di .error-banner non si
   auto-nasconde — resta finché has_any_issue non torna false lato API. */
.alarm-banner {
    background: var(--accent-red);
    color: white;
    padding: 12px 24px;
    font-size: 0.95rem;
    font-weight: 700;
    text-align: center;
    cursor: pointer;
    animation: alarm-pulse 1.5s ease-in-out infinite;
}

@keyframes alarm-pulse {
    0%, 100% { background: var(--accent-red); }
    50% { background: #b71c1c; }
}

.hidden {
    display: none !important;
}

/* Panels */
.panel {
    display: none;
    padding: 20px 24px;
    max-width: 1200px;
    margin: 0 auto;
}

.panel.active {
    display: block;
}

/* Card Grid */
.card-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
    margin-bottom: 20px;
}

.card-grid-2 {
    grid-template-columns: repeat(2, 1fr);
}

/* Cards */
.card {
    background: var(--bg-card);
    border-radius: var(--border-radius);
    padding: 20px;
    box-shadow: var(--shadow);
    transition: background 0.2s;
    position: relative;
    overflow: hidden;
}

.card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
}

.card-power::before { background: var(--accent-green); }
.card-consumption::before { background: var(--accent-orange); }
.card-grid-io::before { background: var(--accent-blue); }
.card-daily::before { background: var(--accent-blue); }
.card-battery::before { background: var(--accent-yellow); }
.card-inverter::before { background: var(--accent-green); }
.card-alarms::before { background: var(--accent-red); }

/* Weather card accents */
.card-temp-out::before { background: var(--accent-blue); }
.card-temp-in::before { background: var(--accent-orange); }
.card-pressure::before { background: #b388ff; }
.card-co2::before { background: var(--accent-green); }
.card-noise::before { background: #ea80fc; }
.card-humidity-out::before { background: var(--accent-blue); }
.card-rain::before { background: #448aff; }
.card-wind::before { background: #69f0ae; }
.card-forecast::before { background: var(--accent-yellow); }

/* Heating (Daikin) card accents */
.card-heating-status::before { background: var(--accent-green); }
.card-heating-outdoor::before { background: var(--accent-blue); }
.card-heating-water::before { background: var(--accent-orange); }
.card-heating-tank::before { background: #448aff; }

.card-heating-status.status-card-warn::before { background: var(--accent-orange); }
.card-heating-status.status-card-error::before { background: var(--accent-red); }

/* Card icon */
.card-icon {
    font-size: 1.6rem;
    margin-bottom: 6px;
    line-height: 1;
    filter: grayscale(0.2);
}

.card-label {
    font-size: 0.8rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 8px;
}

.card-value {
    font-size: 1.8rem;
    font-weight: 700;
    line-height: 1.2;
}

.card-sub {
    font-size: 0.9rem;
    color: var(--text-muted);
    margin-top: 4px;
}

/* Battery bar */
.battery-bar {
    height: 6px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 3px;
    margin-top: 12px;
    overflow: hidden;
}

.battery-fill {
    height: 100%;
    border-radius: 3px;
    background: var(--accent-green);
    transition: width 0.5s ease, background 0.3s;
}

.battery-fill.low {
    background: var(--accent-red);
}

.battery-fill.mid {
    background: var(--accent-orange);
}

/* Charts */
.chart-section {
    margin-bottom: 20px;
}

.chart-container {
    background: var(--bg-card);
    border-radius: var(--border-radius);
    padding: 20px;
    box-shadow: var(--shadow);
}

.chart-container h3 {
    font-size: 0.9rem;
    color: var(--text-muted);
    margin-bottom: 12px;
    font-weight: 500;
}

.chart-container canvas {
    width: 100% !important;
    height: 220px !important;
}

.chart-full {
    width: 100%;
}

/* Forecast list */
.forecast-list {
    display: flex;
    gap: 12px;
    margin-top: 8px;
}

.forecast-day {
    flex: 1;
    text-align: center;
}

.forecast-date {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-bottom: 4px;
}

.forecast-icon {
    font-size: 1.5rem;
    line-height: 1;
    margin-bottom: 4px;
}

.forecast-desc {
    font-size: 0.7rem;
    color: var(--text);
    margin-bottom: 2px;
}

.forecast-detail {
    font-size: 0.7rem;
    color: var(--text-muted);
}

/* Cameras panel - maximize video space */
#panel-cameras {
    padding: 6px;
    max-width: 100%;
}

.cameras-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: 1fr 1fr;
    gap: 4px;
    height: calc(100vh - 70px);
}

.camera-cell {
    background: #000;
    border-radius: 6px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.camera-header {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 3px 8px;
    background: rgba(255, 255, 255, 0.05);
    flex-shrink: 0;
}

.camera-name {
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.camera-video-wrap {
    position: relative;
    flex: 1;
    min-height: 0;
    background: #000;
}

.camera-video-wrap iframe {
    width: 100%;
    height: 100%;
    border: none;
    pointer-events: none;
}

.camera-video-wrap .camera-snapshot-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

/* Snapshot loading placeholder: shown until the first image ever
   arrives, instead of a broken-image icon. Used both in the cameras
   grid and the Home camera cell. */
.snapshot-loading {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    font-size: 0.8rem;
    background: #000;
}

/* Stale indicator: shown over the last good snapshot when a refresh
   fails, so the old image stays visible but the user knows it's not
   current. */
.snapshot-stale-icon {
    display: none;
    position: absolute;
    top: 6px;
    left: 6px;
    z-index: 1;
    font-size: 0.9rem;
    line-height: 1;
    background: rgba(0, 0, 0, 0.6);
    color: var(--accent-orange);
    border-radius: 50%;
    width: 26px;
    height: 26px;
    align-items: center;
    justify-content: center;
}

.snapshot-stale-icon.visible {
    display: flex;
}

/* Camera zoom modal */
.zoom-modal {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 200;
    background: var(--bg);
    flex-direction: column;
}

.zoom-modal.active {
    display: flex;
}

.zoom-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    background: rgba(255, 255, 255, 0.03);
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    flex-shrink: 0;
}

.zoom-title {
    font-size: 1.1rem;
    font-weight: 600;
    text-transform: uppercase;
}

.zoom-stale {
    font-size: 0.8rem;
    color: #ff9800;
    background: rgba(255, 152, 0, 0.15);
    border: 1px solid rgba(255, 152, 0, 0.4);
    border-radius: 4px;
    padding: 2px 8px;
    margin-right: auto;
}

.zoom-close {
    background: var(--accent-red);
    color: #fff;
    border: none;
    border-radius: 8px;
    padding: 10px 20px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
}

.zoom-body {
    position: relative;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    background: #000;
}

.zoom-body img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    will-change: transform;
    user-select: none;
    -webkit-user-select: none;
}

/* Hidden until it actually has a loaded src — without this an <img> with
   no/broken src renders the browser's broken-image icon + alt text on
   top of the "Caricamento..." placeholder. */
.zoom-body img:not([src]) {
    display: none;
}

/* Cameras responsive */
@media (orientation: portrait) {
    .cameras-grid {
        grid-template-columns: repeat(2, 1fr);
        grid-template-rows: repeat(3, 1fr);
    }
}

@media (max-width: 600px) {
    .cameras-grid {
        grid-template-columns: 1fr;
        grid-template-rows: auto;
        height: auto;
    }
    .camera-video-wrap {
        aspect-ratio: 16 / 9;
    }
}

/* Section titles */
.section-title {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-muted);
    margin: 16px 0 10px;
    padding-bottom: 6px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.section-title:first-child {
    margin-top: 0;
}

/* Domotics cards */
.card-thermo::before { background: #ff6e40; }
.card-light::before { background: var(--accent-yellow); }
.card-switch::before { background: var(--accent-blue); }

.card-light.light-on {
    background: rgba(255, 215, 64, 0.1);
}

.thermo-humidity {
    color: var(--accent-blue);
}

/* Buttons */
.btn-light, .btn-switch {
    display: block;
    width: 100%;
    margin-top: 10px;
    padding: 10px;
    border: none;
    border-radius: 8px;
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.5px;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-light.btn-off {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text);
}

.btn-light.btn-on {
    background: var(--accent-yellow);
    color: #000;
}

.btn-switch {
    background: var(--accent-blue);
    color: #000;
}

.btn-switch:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.switch-status {
    font-size: 1.2rem;
    font-weight: 700;
}

/* Not available value */
.nd-value {
    color: var(--text-muted);
    font-size: 1.2rem;
    font-weight: 500;
}

/* Stale data badge: shown on a card when the last fetch failed/was
   rate-limited and we're displaying the last known good value instead. */
.stale-badge {
    margin-top: 6px;
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--accent-orange);
    background: rgba(255, 152, 0, 0.12);
    border: 1px solid rgba(255, 152, 0, 0.35);
    border-radius: 4px;
    padding: 2px 6px;
    display: inline-block;
    width: fit-content;
}

/* Status colors */
.status-ok { color: var(--accent-green); }
.status-warn { color: var(--accent-orange); }
.status-error { color: var(--accent-red); }

/* Footer */
footer {
    position: sticky;
    bottom: 0;
    background: linear-gradient(135deg, #1a2733, #0f1923);
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    padding: 10px 24px;
    z-index: 100;
}

.footer-panel {
    display: none;
    flex-wrap: wrap;
    justify-content: center;
    gap: 16px;
}

/* Mini stato batterie, sempre visibile in ogni schermata (non solo
   Home/Fotovoltaico) sopra la riga di stato specifica del pannello. */
.footer-batteries {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 18px;
    padding-bottom: 8px;
    margin-bottom: 8px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.footer-battery {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.footer-battery-icon {
    font-size: 1rem;
}

.footer-battery-soc {
    font-weight: 700;
    color: var(--text);
}

.footer-battery-soc.low { color: var(--accent-red); }
.footer-battery-soc.mid { color: var(--accent-orange); }

.footer-panel.active {
    display: flex;
}

.footer-item {
    font-size: 0.9rem;
    color: var(--text-muted);
    padding: 2px 0;
}

.footer-item.ok::before {
    content: '\25CF ';
    color: var(--accent-green);
}

.footer-item.error::before {
    content: '\25CF ';
    color: var(--accent-red);
}

.footer-error {
    color: var(--accent-red);
    font-size: 0.8rem;
    margin-left: 4px;
}

/* Responsive - Portrait tablet */
@media (orientation: portrait) {
    .card-grid {
        grid-template-columns: repeat(3, 1fr);
    }

    .card-grid-2 {
        grid-template-columns: 1fr;
    }

    .chart-section {
        grid-template-columns: 1fr;
    }

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

/* Small screens */
@media (max-width: 600px) {
    .card-grid {
        grid-template-columns: 1fr;
    }

    header {
        flex-direction: column;
        gap: 10px;
    }

    .panel {
        padding: 12px;
    }
}

/* ============================================
   Home Panel
   ============================================ */

.home-batteries {
    grid-template-columns: repeat(3, 1fr);
}

.home-bottom {
    display: flex;
    gap: 12px;
    padding: 0 12px 12px;
    flex: 1;
    min-height: 0;
}

.home-camera {
    flex: 1.5;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.home-camera-label {
    font-size: 0.8rem;
    color: var(--text-secondary);
    padding: 0 4px 4px;
}

.home-camera-wrap {
    position: relative;
    flex: 1;
    display: flex;
    flex-direction: column;
    border-radius: 8px;
    overflow: hidden;
    background: #000;
    min-height: 500px;
}

.home-camera-wrap iframe {
    flex: 1;
    width: 100%;
    border: none;
    display: block;
}

.home-camera-wrap .camera-snapshot-img {
    flex: 1;
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.home-info-col {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 0;
    overflow-y: auto;
}

.home-info-col .card {
    flex-shrink: 0;
}

@media (orientation: portrait) {
    /* Home panel portrait: 3 stacked rows */
    #panel-home.active {
        display: flex;
        flex-direction: column;
        overflow: hidden;
    }

    .home-batteries {
        flex-shrink: 0;
    }

    /* Second row: camera full width, flex-grows to fill space */
    .home-bottom {
        flex-direction: column;
        flex: 1;
        min-height: 0;
    }

    .home-camera {
        flex: 1;
        min-height: 0;
    }

    .home-camera-wrap {
        flex: 1;
    }

    /* Third row: 3 cards side by side */
    .home-info-col {
        flex-direction: row;
        flex-shrink: 0;
        overflow: visible;
    }

    .home-info-col .card {
        flex: 1;
        min-width: 0;
    }
}

/* Alert flash overlay (spia cancello ON) */
#alert-flash-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 9999;
    background: rgba(255, 255, 255, 0.55);
    pointer-events: none;
}

#alert-flash-overlay.flash-active {
    display: block;
    animation: screen-flash 0.6s ease-out forwards;
}

/* Pulsante silenzia allarme sonoro (1 ora) — vedi alarm.js */
.alarm-mute-btn {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 9999;
    padding: 12px 18px;
    border: none;
    border-radius: 24px;
    font-size: 0.95rem;
    font-weight: 700;
    color: white;
    background: var(--accent-red);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    cursor: pointer;
}

.alarm-mute-btn.alarm-muted {
    background: #555;
}

@keyframes screen-flash {
    0%   { opacity: 1; }
    100% { opacity: 0; }
}

/* Loading shimmer */
@keyframes shimmer {
    0% { opacity: 0.6; }
    50% { opacity: 1; }
    100% { opacity: 0.6; }
}

.loading .card-value {
    animation: shimmer 1.5s ease-in-out infinite;
    color: var(--text-muted);
}

/* Settings panel */
.settings-radio-group {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-top: 12px;
}

.settings-radio {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.95rem;
    color: var(--text);
    cursor: pointer;
}

.settings-radio input {
    width: 18px;
    height: 18px;
}

.settings-pin-input {
    width: 100%;
    margin-top: 12px;
    padding: 10px;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.15);
    background: var(--bg);
    color: var(--text);
    font-size: 1.1rem;
    letter-spacing: 0.3rem;
    text-align: center;
}

.settings-save-btn {
    width: 100%;
    margin-top: 12px;
    padding: 10px;
    border: none;
    border-radius: 8px;
    background: var(--accent-blue);
    color: #0f1923;
    font-weight: 600;
    cursor: pointer;
}

.settings-logout-btn {
    background: var(--accent-red);
    color: #fff;
}

.settings-message {
    margin-top: 10px;
    font-size: 0.85rem;
    min-height: 1.2em;
    color: var(--text-muted);
}

.settings-message.ok {
    color: var(--accent-green);
}

.settings-message.error {
    color: var(--accent-red);
}

/* ============================================
   Tesla
   ============================================ */

.card-tesla-battery::before { background: var(--accent-green); }
.card-tesla-charge::before { background: var(--accent-yellow); }
.card-tesla-status::before { background: #e82127; }
.card-tesla-port::before { background: var(--accent-blue); }
.card-tesla-climate::before { background: var(--accent-orange); }
.card-tesla-charge-settings::before { background: var(--accent-yellow); }
.card-tesla-lock::before { background: var(--accent-green); }
.card-tesla-sentry::before { background: #e82127; }
.card-tesla-find::before { background: #b388ff; }

.tesla-setup-card {
    margin-bottom: 20px;
}

.card-tesla-setup::before { background: var(--text-muted); }

.tesla-setup-list {
    list-style: none;
    margin-top: 6px;
}

.tesla-setup-list li {
    padding: 4px 0;
    font-size: 0.95rem;
}

.tesla-setup-list li.missing {
    color: var(--text-muted);
}

.tesla-setup-hint {
    margin: 4px 0 0 26px;
    font-size: 0.85rem;
    color: var(--accent-orange);
}

.tesla-setup-log {
    margin: 6px 0 0 26px;
    padding: 8px;
    max-height: 160px;
    overflow: auto;
    font-size: 0.75rem;
    white-space: pre-wrap;
    word-break: break-all;
    color: var(--text-muted);
    background: rgba(0, 0, 0, 0.3);
    border-radius: 6px;
}

.tesla-pair-link {
    color: var(--accent-blue);
    word-break: break-all;
}

.tesla-connect-btn {
    max-width: 320px;
    margin-top: 14px;
}

/* Barra batteria con tacca sul limite di carica */
.tesla-battery-bar {
    position: relative;
    height: 10px;
    border-radius: 5px;
    overflow: visible;
}

.tesla-limit-marker {
    position: absolute;
    top: -3px;
    width: 2px;
    height: 16px;
    background: var(--text);
    transform: translateX(-1px);
    transition: left 0.5s ease;
}

.tesla-asleep {
    color: var(--text-muted);
}

.tesla-label-spaced {
    margin-top: 16px;
}

.tesla-btn-row {
    display: flex;
    gap: 8px;
}

.btn-tesla {
    flex: 1;
    display: block;
    width: 100%;
    margin-top: 10px;
    padding: 10px 6px;
    border: none;
    border-radius: 8px;
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.5px;
    cursor: pointer;
    transition: all 0.2s;
    background: var(--accent-blue);
    color: #000;
}

.btn-tesla.btn-tesla-off {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text);
}

/* Primo tocco sui comandi delicati: in attesa di conferma */
.btn-tesla.btn-tesla-confirm {
    background: var(--accent-red);
    color: #fff;
}

.btn-tesla:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.tesla-stepper {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 10px;
}

.tesla-stepper span {
    min-width: 70px;
    text-align: center;
    font-size: 1.4rem;
    font-weight: 700;
}

.btn-step {
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.1);
    color: var(--text);
    font-size: 1.4rem;
    font-weight: 700;
    cursor: pointer;
}

.tesla-stepper .btn-apply {
    flex: 1;
    margin-top: 0;
}
