/* ============================================================
   BackendMeteo - portale
   Colori come variabili; tema scuro automatico.
   ============================================================ */

:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --text: #1c2530;
  --text-2: #5b6876;
  --border: #d9dfe5;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --danger: #c62828;
  --ok: #2e7d32;
  --warn: #ef8f00;
  --old: #c62828;
  --none: #8a96a3;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0, 0, 0, .08);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161c;
    --surface: #1a2129;
    --surface-2: #232c36;
    --text: #e3e8ee;
    --text-2: #9aa7b4;
    --border: #2f3a46;
    --accent: #4c8dff;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); display: flex; flex-direction: column; }
a { color: var(--accent); }
h1 { font-size: 1.35rem; margin: 0 0 16px; }
h2 { font-size: 1.1rem; margin: 24px 0 12px; }

/* ─── Barra superiore ─────────────────────────────── */
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 0 16px; height: 52px;
  background: var(--surface); border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.brand { font-weight: 700; letter-spacing: .2px; white-space: nowrap; }
.brand span { color: var(--accent); }
.nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; scrollbar-width: none; }
.nav a {
  text-decoration: none; color: var(--text-2); padding: 7px 12px; border-radius: 6px; white-space: nowrap;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--surface-2); color: var(--text); font-weight: 600; }
.user-menu { display: flex; align-items: center; gap: 8px; color: var(--text-2); white-space: nowrap; }
.role-badge { font-size: .75rem; padding: 2px 8px; border-radius: 10px; background: var(--surface-2); }

/* ─── Contenuto ───────────────────────────────────── */
main { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.page { padding: 24px; max-width: 1100px; width: 100%; margin: 0 auto; overflow-y: auto; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px;
}
.toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 16px; flex-wrap: wrap; }
.toolbar .spacer { flex: 1; }
.muted { color: var(--text-2); }
.small { font-size: .85rem; }

/* ─── Tabelle ─────────────────────────────────────── */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .4px; color: var(--text-2); font-weight: 600; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
td.num { font-variant-numeric: tabular-nums; }

/* ─── Form ────────────────────────────────────────── */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px 18px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field.wide { grid-column: 1 / -1; }
label { font-size: .85rem; color: var(--text-2); }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 6px; padding: 8px 10px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea { min-height: 70px; resize: vertical; }
.check { display: flex; align-items: center; gap: 8px; }
.check input { width: auto; }
.checklist { max-height: 260px; overflow-y: auto; border: 1px solid var(--border); border-radius: 6px; padding: 6px 10px; }
.checklist .check { padding: 4px 0; }

button, a.button {
  font: inherit; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: 6px; cursor: pointer; white-space: nowrap;
}
button:hover, a.button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--danger); }
button.link { border: none; background: none; color: var(--accent); padding: 4px 6px; }
.actions { display: flex; gap: 8px; margin-top: 20px; flex-wrap: wrap; }

.message { padding: 10px 14px; border-radius: 6px; margin-bottom: 14px; }
.message.error { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.message.ok { background: color-mix(in srgb, var(--ok) 14%, transparent); color: var(--ok); }

/* ─── Login ───────────────────────────────────────── */
.login-wrap { flex: 1; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-wrap .card { width: 100%; max-width: 380px; }
.login-wrap .field { margin-bottom: 14px; }
.login-wrap button { width: 100%; margin-top: 6px; }

/* ─── Mappa (homepage) ────────────────────────────── */
.map-layout { flex: 1; display: flex; min-height: 0; }
.map-side {
  width: 320px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.map-side .head { padding: 14px; border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 10px; }
.station-list { overflow-y: auto; flex: 1; }
.station-item { display: flex; gap: 10px; align-items: flex-start; padding: 10px 14px; border-bottom: 1px solid var(--border); cursor: pointer; }
.station-item:hover { background: var(--surface-2); }
.station-item .name { font-weight: 600; }
.dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; margin-top: 4px; border: 2px solid rgba(255, 255, 255, .8); }
.dot.ok { background: var(--ok); } .dot.warn { background: var(--warn); }
.dot.old { background: var(--old); } .dot.none { background: var(--none); }
#map { flex: 1; min-height: 300px; }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: .8rem; color: var(--text-2); }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend .dot { margin: 0; }

.popup h3 { margin: 0 0 4px; font-size: 1rem; }
.popup table { margin-top: 6px; }
.popup td { padding: 3px 6px; border: none; }
.popup td:first-child { color: #5b6876; }

.mini-map { height: 320px; border-radius: 6px; border: 1px solid var(--border); margin-top: 6px; }

/* ─── Telefono ────────────────────────────────────── */
@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; height: auto; padding: 8px 12px; gap: 8px; }
  .nav { order: 3; flex: 1 0 100%; }
  .page { padding: 16px; }
  .map-layout { flex-direction: column-reverse; }
  .map-side { width: 100%; max-height: 40vh; border-right: none; border-top: 1px solid var(--border); }
  .hide-mobile { display: none; }
  /* 16px: sotto questa misura iPhone ingrandisce la pagina quando si tocca un campo */
  input, select, textarea { font-size: 16px; }
  button { min-height: 38px; }
  button.link { min-height: 32px; }
  h1 { font-size: 1.2rem; }
}
@media (max-width: 480px) {
  /* barra in alto più compatta: nome e ruolo nascosti per lasciare spazio ai pulsanti */
  #user-name, .role-badge { display: none; }
  .card { padding: 14px; }
  .page { padding: 12px; }
}

/* ─── Cruscotti a widget (Previsioni) ─────────────── */
.widgets { display: grid; grid-template-columns: repeat(auto-fill, minmax(360px, 1fr)); gap: 16px; }
.widget h2 { margin: 0 0 12px; font-size: 1rem; }
.widget.wide { grid-column: span 2; }
.widget.clickable { cursor: pointer; }
.widget.clickable:hover { border-color: var(--accent); }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 12px; margin-bottom: 8px; }
.kpi-value { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.news { margin: 0; padding-left: 18px; }
.news li { margin-bottom: 8px; }
@media (max-width: 760px) { .widget.wide { grid-column: auto; } .widgets { grid-template-columns: minmax(0, 1fr); } .widget { min-width: 0; } }

/* ─── Scelta delle misure e tabella dati ──────────── */
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 3px 4px 3px 10px; border-radius: 14px;
  background: var(--surface-2); border: 1px solid var(--border); font-size: .9rem;
}
.chip-x { border: none; background: none; padding: 0 6px; font-size: 1.1rem; line-height: 1; color: var(--text-2); cursor: pointer; }
.chip-x:hover { color: var(--danger); }
button.link { border: none; background: none; color: var(--accent); padding: 0 6px; cursor: pointer; }
.pick-group { padding: 6px 0; border-bottom: 1px solid var(--border); }
.pick-group:last-child { border-bottom: none; }
.pick-measures { display: flex; flex-wrap: wrap; gap: 2px 18px; padding-left: 8px; }
.data-table thead { position: sticky; top: 0; background: var(--surface); z-index: 1; }
.data-table th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
.data-table th.sortable:hover { color: var(--text); }
.data-table th[data-sort="desc"]::after { content: " ▼"; }
.data-table th[data-sort="asc"]::after { content: " ▲"; }
.data-table tr.filters th { padding: 4px 6px; text-transform: none; letter-spacing: 0; }
.col-filter { padding: 4px 6px; font-size: .85rem; min-width: 70px; }
a.button { display: inline-block; text-decoration: none; color: var(--text); }

/* ─── Cruscotto idrometro ─────────────────────────── */
.chart-overlay {
  position: absolute; top: 6px; left: 72px; z-index: 2; pointer-events: none;
  background: color-mix(in srgb, var(--surface) 88%, transparent); border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 10px; line-height: 1.5;
}
.popover {
  position: absolute; right: 16px; top: 54px; z-index: 5; width: 280px; padding: 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 4px 16px rgba(0, 0, 0, .18);
}
.icon-button { font-size: 1.15rem; line-height: 1; padding: 5px 9px; }
details.card > summary { cursor: pointer; }
@media (max-width: 760px) { .chart-overlay { position: static; margin-bottom: 8px; } }
pre.code { background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 10px 12px; overflow-x: auto; font-size: .85rem; }

/* ─── Widget (come rain3.html) ────────────────────── */
.station-widgets { margin-bottom: 16px; scroll-margin-top: 70px; }
.value-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 12px; }
.value-card {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 84px;
  padding: 14px 16px; border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
}
.vc-label { font-weight: 700; }
.vc-value { font-weight: 800; font-size: 1.75rem; white-space: nowrap; }
.vc-unit { font-weight: 600; font-size: 1rem; }
.vc-trend { font-size: 1.1rem; margin-left: 6px; }
.vc-trend.up { color: #c92a2a; }
.vc-trend.down { color: #167f3d; }
.flash { animation: flashOutline 1.6s ease-out 1; }
@keyframes flashOutline {
  0% { box-shadow: 0 0 0 4px rgba(0, 123, 255, .35); }
  50% { box-shadow: 0 0 0 10px rgba(0, 123, 255, .15); }
  100% { box-shadow: 0 0 0 0 rgba(0, 123, 255, 0); }
}

/* ─── Tabella e grafici: menu stazioni verticale ─── */
.data-layout { display: grid; grid-template-columns: 280px minmax(0, 1fr); gap: 16px; align-items: start; }
.data-side { position: sticky; top: 0; display: flex; flex-direction: column; gap: 8px; max-height: calc(100vh - 110px); padding: 12px; }
.station-menu { overflow-y: auto; margin: 0 -6px; }
.station-item {
  display: flex; align-items: center; gap: 6px; width: 100%; text-align: left; border: none; background: none;
  padding: 7px 6px; border-radius: 6px; white-space: normal;
}
.station-item:hover { background: var(--surface-2); }
.station-item.has { font-weight: 600; }
.station-item .grow { flex: 1; }
.station-item .caret { color: var(--text-2); width: 12px; }
.station-item .count { background: var(--accent); color: var(--accent-text); border-radius: 10px; padding: 0 7px; font-size: .75rem; }
.station-measures { padding: 2px 6px 8px 24px; }
.station-measures .check { padding: 3px 0; }
.data-top { margin-bottom: 12px; }
.data-table th { position: relative; }
.data-table th[data-filter="1"]::before { content: "⏷ "; color: var(--accent); }
.col-menu {
  position: absolute; top: 100%; left: 0; z-index: 10; min-width: 220px; padding: 10px; text-transform: none; letter-spacing: 0;
  font-weight: 400; color: var(--text); text-align: left; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; box-shadow: 0 4px 16px rgba(0, 0, 0, .18); display: flex; flex-direction: column; gap: 4px; cursor: default;
}
.col-menu > button { text-align: left; }
.data-table th.num:last-child .col-menu { left: auto; right: 0; }
@media (max-width: 900px) {
  .data-layout { grid-template-columns: minmax(0, 1fr); }
  .data-side { position: static; max-height: 38vh; }
}

/* ─── Menu a tendina Impostazioni ─────────────────── */
.nav-menu { border: none; background: none; color: var(--text-2); padding: 7px 12px; border-radius: 6px; }
.nav-menu:hover, .nav-menu.active { background: var(--surface-2); color: var(--text); }
.nav-menu.active { font-weight: 600; }
.nav-drop {
  position: fixed; z-index: 1000; min-width: 220px; padding: 6px; display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 6px 20px rgba(0, 0, 0, .18);
}
.nav-drop a { text-decoration: none; color: var(--text); padding: 8px 12px; border-radius: 6px; }
.nav-drop a:hover { background: var(--surface-2); }
.nav-drop a.active { font-weight: 600; background: var(--surface-2); }
.radar-legend { display: inline-flex; gap: 2px; align-items: center; font-size: .75rem; color: var(--text-2); }
.radar-swatch { padding: 1px 5px; border-radius: 3px; color: #222; font-size: .7rem; }

/* ─── Allarmi a formula ───────────────────────────── */
.state-badge { display: inline-block; min-width: 26px; padding: 2px 8px; border-radius: 12px; color: #fff; font-weight: 700; text-align: center; }
.formula-inline { font-size: .8rem; white-space: pre-wrap; word-break: break-word; }
textarea.formula { font-family: ui-monospace, Consolas, monospace; font-size: .95rem; width: 100%; }
.formula-layout { display: grid; grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.formula-help { border: 1px solid var(--border); border-radius: 8px; padding: 10px; background: var(--surface-2); }
.formula-help select { max-width: 100%; }
.op-buttons { display: flex; flex-wrap: wrap; gap: 4px; }
.op-buttons button { padding: 3px 8px; font-family: ui-monospace, Consolas, monospace; font-size: .85rem; }
@media (max-width: 900px) { .formula-layout { grid-template-columns: 1fr; } }
.radar-info { background: var(--surface); color: var(--text); padding: 4px 8px; border-radius: 6px; box-shadow: var(--shadow); font-size: .8rem; display: flex; gap: 3px; align-items: center; flex-wrap: wrap; max-width: 90vw; }
dialog.dialog { border: 1px solid var(--border); border-radius: 10px; padding: 16px; max-width: min(900px, 94vw); width: 100%; background: var(--surface); color: var(--text); box-shadow: 0 10px 40px rgba(0,0,0,.3); }
dialog.dialog::backdrop { background: rgba(0, 0, 0, .35); }
.pick-list { display: flex; flex-direction: column; align-items: stretch; max-height: 260px; }
.pick-row { display: grid; grid-template-columns: auto 1fr auto; gap: 8px; align-items: center; justify-content: start; padding: 4px 0; border-bottom: 1px solid var(--border); }
.pick-row:last-child { border-bottom: none; }

/* Log in tempo reale */
.log-view { height: calc(100vh - 260px); min-height: 300px; overflow: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
            padding: 8px 10px; font: 12px/1.45 ui-monospace, SFMono-Regular, Consolas, monospace; }
.log-line { display: grid; grid-template-columns: auto auto 1fr; gap: 10px; white-space: pre-wrap; word-break: break-word; }
.log-time { color: var(--text-2); }
.log-level { font-weight: 600; }

/* Permessi per sezione (Utenti) */
.perm-list { max-height: 420px; }
.perm-row { grid-template-columns: 1fr auto auto; align-items: center; }
.perm-row select { min-width: 130px; }
.perm-sub > span:first-child { padding-left: 18px; }
.perm-off { opacity: .45; }
