/* ---------------------------------------------------------------------------
 * Irrigazione — foglio di stile unico.
 *
 * Nessun framework: la pagina è una sola e il peso conta, perché si apre da un
 * telefono in giardino, spesso su rete lenta.
 *
 * Il tema segue quello del sistema. La gerarchia visiva è pensata perché le
 * informazioni importanti — errori, stato, settore attivo — si vedano senza
 * scorrere e senza dover leggere.
 * ------------------------------------------------------------------------- */

:root {
    color-scheme: light dark;

    --bg: #f4f6f5;
    --surface: #ffffff;
    --surface-2: #eef1f0;
    --border: #dde3e1;
    --text: #16211e;
    --text-dim: #5d6b67;

    --accent: #0f766e;
    --accent-soft: #d6f0ec;
    --accent-text: #ffffff;

    --ok: #15803d;
    --ok-soft: #dcfce7;
    --warn: #b45309;
    --warn-soft: #fef3c7;
    --error: #b91c1c;
    --error-soft: #fee2e2;

    --radius: 14px;
    --radius-lg: 20px;
    --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 8px 24px -12px rgb(0 0 0 / 18%);
    --gap: 16px;
    --max: 900px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0d1412;
        --surface: #151f1c;
        --surface-2: #1d2926;
        --border: #2a3936;
        --text: #e6efec;
        --text-dim: #93a5a0;

        --accent: #2dd4bf;
        --accent-soft: #10312e;
        --accent-text: #04201c;

        --ok: #4ade80;
        --ok-soft: #0f2e1c;
        --warn: #fbbf24;
        --warn-soft: #33260a;
        --error: #f87171;
        --error-soft: #3a1616;

        --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 10px 30px -14px rgb(0 0 0 / 70%);
    }
}

* { box-sizing: border-box; }

body {
    margin: 0;
    padding: 0 max(12px, env(safe-area-inset-left)) calc(32px + env(safe-area-inset-bottom));
    background: var(--bg);
    color: var(--text);
    font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    -webkit-text-size-adjust: 100%;
}

h1, h2 { line-height: 1.25; }

.sr-only {
    position: absolute;
    width: 1px; height: 1px;
    padding: 0; margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

.hidden { display: none !important; }

/* --- Barra superiore ----------------------------------------------------- */

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--gap);
    max-width: var(--max);
    margin: 0 auto;
    padding: calc(14px + env(safe-area-inset-top)) 4px 10px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 9px;
    font-size: 1.05rem;
    font-weight: 650;
    letter-spacing: -0.01em;
}

.brand-mark {
    width: 26px;
    height: 26px;
    color: var(--accent);
}

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

.logout-form { margin: 0; }

.icon-button {
    display: grid;
    place-items: center;
    width: 38px;
    height: 38px;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 50%;
    background: var(--surface);
    color: var(--text-dim);
    cursor: pointer;
}

.icon-button svg { width: 18px; height: 18px; }
.icon-button:hover { color: var(--text); }

/* --- Indicatore di collegamento ------------------------------------------ */

.pill {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 5px 12px 5px 10px;
    border-radius: 999px;
    font-size: 0.8rem;
    font-weight: 600;
    white-space: nowrap;
}

.pill-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: currentColor;
}

.pill-online  { background: var(--ok-soft);     color: var(--ok); }
.pill-offline { background: var(--error-soft);  color: var(--error); }
.pill-unknown { background: var(--surface-2);   color: var(--text-dim); }

.pill-online .pill-dot { animation: breathe 2.4s ease-in-out infinite; }

@keyframes breathe {
    0%, 100% { opacity: 1; }
    50%      { opacity: 0.35; }
}

/* --- Schede --------------------------------------------------------------- */

.tabs {
    display: flex;
    gap: 4px;
    max-width: var(--max);
    margin: 0 auto 18px;
    padding: 4px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
}

.tab {
    flex: 1;
    padding: 9px 8px;
    border: 0;
    border-radius: 999px;
    background: transparent;
    color: var(--text-dim);
    font: inherit;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

.tab.is-active {
    background: var(--accent);
    color: var(--accent-text);
}

main {
    max-width: var(--max);
    margin: 0 auto;
}

.panel { display: none; }
.panel.is-active { display: block; }

.section-title {
    margin: 28px 0 6px;
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-dim);
}

.hint {
    margin: 0 0 12px;
    font-size: 0.85rem;
    color: var(--text-dim);
}

.notice {
    margin: 0 0 16px;
    padding: 12px 14px;
    border-radius: var(--radius);
    background: var(--warn-soft);
    color: var(--warn);
    font-size: 0.88rem;
}

/* --- Messaggi in evidenza ------------------------------------------------- */

.alerts:not(:empty) { margin-bottom: 16px; }

.alert {
    display: flex;
    gap: 12px;
    padding: 14px 16px;
    margin-bottom: 10px;
    border-radius: var(--radius);
    border-left: 4px solid currentColor;
    animation: slide-in 0.25s ease-out;
}

@keyframes slide-in {
    from { opacity: 0; transform: translateY(-6px); }
    to   { opacity: 1; transform: none; }
}

.alert-error { background: var(--error-soft); color: var(--error); }
.alert-warn  { background: var(--warn-soft);  color: var(--warn); }
.alert-info  { background: var(--accent-soft); color: var(--accent); }

.alert-body { color: var(--text); }
.alert-title { display: block; margin-bottom: 3px; font-weight: 680; color: inherit; }
.alert-detail { display: block; font-size: 0.87rem; color: var(--text-dim); }

.alert-icon { flex: 0 0 auto; width: 20px; height: 20px; margin-top: 2px; }

/* --- Stato principale ----------------------------------------------------- */

.hero {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--gap);
    flex-wrap: wrap;
    padding: 22px;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--surface);
    box-shadow: var(--shadow);
}

.hero-state { min-width: 0; }

.hero-label {
    display: block;
    font-size: 1.5rem;
    font-weight: 680;
    letter-spacing: -0.02em;
}

.hero-detail {
    display: block;
    margin-top: 2px;
    color: var(--text-dim);
    font-size: 0.92rem;
}

.hero.is-running { border-color: var(--accent); }
.hero.is-running .hero-label { color: var(--accent); }

/* --- Spia consumi ---------------------------------------------------------
 * Riproduce sullo schermo i tre modi della spia fisica: spenta, accesa fissa e
 * lampeggiante 2s/8s. Chi ha già visto la spia in giardino la riconosce.
 * ------------------------------------------------------------------------ */

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

.lamp-bulb {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--surface-2);
    border: 2px solid var(--border);
    transition: background 0.2s, box-shadow 0.2s, border-color 0.2s;
}

.lamp-bulb.is-on {
    background: var(--warn);
    border-color: var(--warn);
    box-shadow: 0 0 0 6px var(--warn-soft), 0 0 22px -2px var(--warn);
}

.lamp-bulb.is-blink {
    background: var(--error);
    border-color: var(--error);
    animation: blink-2-8 10s steps(1, end) infinite;
}

/* 2s accesa, 8s spenta: la stessa cadenza del relè. Cfr. Docs/FIRMWARE.md §9.2 */
@keyframes blink-2-8 {
    0%  { opacity: 1; box-shadow: 0 0 0 6px var(--error-soft), 0 0 22px -2px var(--error); }
    20% { opacity: 0.25; box-shadow: none; }
    100% { opacity: 0.25; box-shadow: none; }
}

.lamp-meta { display: flex; flex-direction: column; }

.lamp-title {
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.07em;
    text-transform: uppercase;
    color: var(--text-dim);
}

.lamp-text { font-size: 0.9rem; }

/* --- Comandi -------------------------------------------------------------- */

.actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 14px;
}

.action {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 9px;
    min-height: 54px;
    padding: 0 18px;
    border: 0;
    border-radius: var(--radius);
    font: inherit;
    font-weight: 650;
    cursor: pointer;
    transition: filter 0.15s, opacity 0.15s;
}

.action svg { width: 19px; height: 19px; }
.action:hover:not(:disabled) { filter: brightness(1.08); }
.action:active:not(:disabled) { transform: translateY(1px); }

.action:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.action-go { background: var(--accent); color: var(--accent-text); }

.action-stop {
    background: var(--surface);
    color: var(--error);
    border: 1px solid var(--border);
}

.action-stop.is-armed {
    background: var(--error);
    color: #fff;
    border-color: var(--error);
}

/* --- Settori -------------------------------------------------------------- */

.sector-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 10px;
}

.sector {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 3px;
    min-height: 78px;
    padding: 13px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    font: inherit;
    text-align: left;
    cursor: pointer;
    overflow: hidden;
    transition: border-color 0.15s, background 0.15s;
}

.sector:hover:not(:disabled) { border-color: var(--accent); }

.sector:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.sector-name {
    font-weight: 640;
    line-height: 1.3;
}

.sector-time {
    font-size: 0.82rem;
    color: var(--text-dim);
}

.sector.is-active {
    background: var(--accent-soft);
    border-color: var(--accent);
}

.sector.is-active .sector-name { color: var(--accent); }

/* Barretta animata sul settore in irrigazione: dice "sta scorrendo acqua"
   senza pretendere di sapere quanto manca — il tempo residuo lo conosce solo
   la centralina, e non lo pubblica. */
.sector.is-active::after {
    content: "";
    position: absolute;
    inset: auto 0 0 0;
    height: 3px;
    background: linear-gradient(90deg, transparent, var(--accent), transparent);
    background-size: 200% 100%;
    animation: flow 1.6s linear infinite;
}

@keyframes flow {
    from { background-position: 200% 0; }
    to   { background-position: -200% 0; }
}

/* --- Diagnostica ---------------------------------------------------------- */

.diagnostics {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 10px;
    margin: 24px 0 0;
    padding: 14px 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    font-size: 0.85rem;
}

.diagnostics div { min-width: 0; }
.diagnostics dt { color: var(--text-dim); font-size: 0.75rem; }
.diagnostics dd { margin: 1px 0 0; font-variant-numeric: tabular-nums; }

/* --- Configurazione ------------------------------------------------------- */

.programs { display: flex; flex-direction: column; gap: 8px; }

.program {
    display: flex;
    align-items: center;
    gap: 10px;
}

.program input[type="time"] {
    flex: 1;
    max-width: 160px;
}

.config-sectors { display: flex; flex-direction: column; gap: 8px; }

.config-sector {
    display: grid;
    grid-template-columns: 1fr 92px;
    gap: 10px;
    align-items: center;
}

.params {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
    gap: 12px;
}

.params label {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.params label > span { font-size: 0.85rem; font-weight: 600; }
.params label > small { font-size: 0.75rem; color: var(--text-dim); }

input[type="text"],
input[type="time"],
input[type="number"],
input[type="password"] {
    width: 100%;
    min-height: 44px;
    padding: 0 12px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    color: var(--text);
    font: inherit;
}

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

.ghost {
    min-height: 42px;
    padding: 0 16px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-size: 0.88rem;
    font-weight: 600;
    cursor: pointer;
}

.ghost:hover { border-color: var(--accent); color: var(--accent); }
.ghost-danger:hover { border-color: var(--error); color: var(--error); }

.save-row {
    display: flex;
    align-items: center;
    gap: 14px;
    margin-top: 22px;
    flex-wrap: wrap;
}

.save-status { font-size: 0.87rem; color: var(--text-dim); }
.save-status.is-ok { color: var(--ok); }
.save-status.is-error { color: var(--error); }

/* --- Dispositivi ---------------------------------------------------------- */

.devices { display: flex; flex-direction: column; gap: 8px; }

.device {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
}

.device-meta { display: flex; flex-direction: column; min-width: 0; }
.device-meta small { color: var(--text-dim); font-size: 0.78rem; }

.device-pending { border-color: var(--warn); background: var(--warn-soft); }
.device-actions { display: flex; gap: 8px; flex: 0 0 auto; }

/* --- Eventi --------------------------------------------------------------- */

.event-filters {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 14px;
}

.chip {
    padding: 7px 14px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
    color: var(--text-dim);
    font: inherit;
    font-size: 0.83rem;
    font-weight: 600;
    cursor: pointer;
}

.chip.is-active {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-text);
}

.events {
    margin: 0;
    padding: 0;
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.event {
    display: flex;
    gap: 12px;
    align-items: baseline;
    padding: 11px 14px;
    border: 1px solid var(--border);
    border-left: 3px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    font-size: 0.9rem;
}

.event time {
    flex: 0 0 auto;
    color: var(--text-dim);
    font-size: 0.78rem;
    font-variant-numeric: tabular-nums;
}

.event-error { border-left-color: var(--error); }
.event-warn  { border-left-color: var(--warn); }
.event-info  { border-left-color: var(--accent); }

.empty {
    padding: 28px 16px;
    border: 1px dashed var(--border);
    border-radius: var(--radius);
    color: var(--text-dim);
    text-align: center;
    font-size: 0.9rem;
}

/* --- Messaggio temporaneo ------------------------------------------------- */

.toast {
    position: fixed;
    left: 50%;
    bottom: calc(20px + env(safe-area-inset-bottom));
    transform: translate(-50%, 130%);
    max-width: min(440px, calc(100vw - 24px));
    padding: 12px 18px;
    border-radius: 999px;
    background: var(--text);
    color: var(--bg);
    font-size: 0.88rem;
    font-weight: 600;
    box-shadow: var(--shadow);
    transition: transform 0.25s ease-out;
    z-index: 20;
}

.toast.is-visible { transform: translate(-50%, 0); }
.toast.is-error { background: var(--error); color: #fff; }

/* --- Pagina di accesso ---------------------------------------------------- */

.gate {
    display: grid;
    place-items: center;
    min-height: 100dvh;
    padding: 20px;
}

.gate-card {
    width: min(400px, 100%);
    padding: 30px 26px;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--surface);
    box-shadow: var(--shadow);
}

.gate-mark {
    width: 46px;
    height: 46px;
    margin-bottom: 14px;
    color: var(--accent);
}

.gate-card h1 { margin: 0 0 10px; font-size: 1.5rem; letter-spacing: -0.02em; }

.gate-note {
    margin: 0 0 18px;
    color: var(--text-dim);
    font-size: 0.87rem;
}

.gate-error {
    margin: 0 0 16px;
    padding: 11px 14px;
    border-radius: 10px;
    background: var(--error-soft);
    color: var(--error);
    font-size: 0.88rem;
    font-weight: 600;
}

.gate-card label {
    display: block;
    margin: 14px 0 5px;
    font-size: 0.85rem;
    font-weight: 600;
}

.gate-card button[type="submit"] {
    width: 100%;
    min-height: 50px;
    margin-top: 22px;
    border: 0;
    border-radius: var(--radius);
    background: var(--accent);
    color: var(--accent-text);
    font: inherit;
    font-weight: 650;
    cursor: pointer;
}

.gate-link {
    display: inline-block;
    margin-top: 18px;
    color: var(--accent);
    font-weight: 600;
}

/* --- Schermi stretti ------------------------------------------------------ */

@media (max-width: 520px) {
    .actions { grid-template-columns: 1fr; }
    .hero { padding: 18px; }
    .hero-label { font-size: 1.3rem; }
    .sector-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
    }
}
