/* ===== Tema: variabili chiaro/scuro ===== */
:root {
  --green: #2f8f52;
  --green-dark: #1f6b3c;
  --green-soft: #e6f4ea;
  --accent: #3a8fd0;
  --bg: #f5f7f5;
  --surface: #ffffff;
  --surface-2: #fafcfa;
  --border: #e1e8e1;
  --text: #1c2420;
  --text-muted: #6b756a;
  --shadow: 0 1px 2px rgba(20, 30, 20, 0.04), 0 4px 16px rgba(20, 30, 20, 0.06);
  --shadow-lg: 0 8px 30px rgba(20, 30, 20, 0.12);
  --radius: 12px;
  --info-bg: #e3f1fb; --info-fg: #1c6aa8;
  --warning-bg: #fdf1dc; --warning-fg: #a56a12;
  --critical-bg: #fbe2e2; --critical-fg: #b62b2b;
  --muted-bg: #eceeec; --muted-fg: #6b756a;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root { color-scheme: dark; }
  :root:not([data-theme="light"]) {
    --green: #4cb877;
    --green-dark: #3a9962;
    --green-soft: #163923;
    --accent: #5aa8e6;
    --bg: #141815;
    --surface: #1c211d;
    --surface-2: #20261f;
    --border: #2c342c;
    --text: #e8ede8;
    --text-muted: #9aa59a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 20px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.5);
    --info-bg: #163648; --info-fg: #7ec2f2;
    --warning-bg: #3c2f12; --warning-fg: #f0b74e;
    --critical-bg: #3c1717; --critical-fg: #f19a9a;
    --muted-bg: #262c26; --muted-fg: #9aa59a;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --green: #4cb877;
  --green-dark: #3a9962;
  --green-soft: #163923;
  --accent: #5aa8e6;
  --bg: #141815;
  --surface: #1c211d;
  --surface-2: #20261f;
  --border: #2c342c;
  --text: #e8ede8;
  --text-muted: #9aa59a;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 20px rgba(0, 0, 0, 0.35);
  --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.5);
  --info-bg: #163648; --info-fg: #7ec2f2;
  --warning-bg: #3c2f12; --warning-fg: #f0b74e;
  --critical-bg: #3c1717; --critical-fg: #f19a9a;
  --muted-bg: #262c26; --muted-fg: #9aa59a;
}

/* ===== Reset & base ===== */
* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background-color 0.2s ease, color 0.2s ease;
}

a { color: var(--accent); }

h1 { margin-top: 0; font-weight: 700; }
h2 {
  margin-top: 2rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.5rem;
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: 0.01em;
}
h3 { font-size: 0.95rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }

.muted { color: var(--text-muted); }
.empty { color: var(--text-muted); font-style: italic; }

/* ===== Layout: sidebar + contenuto ===== */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 232px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1.25rem 1rem;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--green-dark);
  padding: 0 0.5rem 1.25rem;
}
.brand svg { flex-shrink: 0; }

.sidebar nav { display: flex; flex-direction: column; gap: 0.15rem; flex: 1; }

.nav-link {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
  transition: background-color 0.15s ease, color 0.15s ease;
}
.nav-link svg { flex-shrink: 0; opacity: 0.85; }
.nav-link:hover { background: var(--green-soft); color: var(--green-dark); }
.nav-link.active { background: var(--green-soft); color: var(--green-dark); font-weight: 700; }

/* Gruppo di voci annidate (es. Documentazione > Coltivazioni / Schema) */
.nav-group { display: flex; flex-direction: column; gap: 0.15rem; }
.nav-group-label {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.55rem 0.7rem 0.35rem;
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.nav-group-label svg { flex-shrink: 0; opacity: 0.7; }
.nav-group-items {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  margin-left: 1.5rem;
  padding-left: 0.55rem;
  border-left: 1px solid var(--border);
}
.nav-link.nav-sub { padding-left: 0.55rem; font-size: 0.86rem; }

.theme-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 8px;
  padding: 0.5rem;
  cursor: pointer;
  font: inherit;
  font-size: 0.82rem;
}
.theme-toggle:hover { color: var(--text); border-color: var(--green); }

.main-content {
  flex: 1;
  min-width: 0;
  padding: 1.75rem 2rem 3rem;
  max-width: 1180px;
}

/* ===== Card di riepilogo ===== */
.stat-row { display: flex; gap: 1rem; flex-wrap: wrap; }

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem 1.6rem;
  min-width: 150px;
  text-align: center;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.stat-card:hover { transform: translateY(-1px); box-shadow: var(--shadow-lg); }
.stat-card.stat-warning { border-color: color-mix(in srgb, var(--warning-fg) 40%, var(--border)); }
.stat-card.stat-info { border-color: color-mix(in srgb, var(--info-fg) 40%, var(--border)); }
.stat-value { font-size: 1.9rem; font-weight: 800; color: var(--green-dark); line-height: 1.2; }
.stat-label { color: var(--text-muted); font-size: 0.82rem; margin-top: 0.15rem; }

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 1rem;
}

.field-card {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem;
  text-decoration: none;
  color: var(--text);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.field-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }

.field-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.6rem;
}

.field-metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.35rem 0.6rem;
  font-size: 0.9rem;
}
.metric-label { display: block; color: var(--text-muted); font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.03em; }
.metric-value { font-weight: 700; }
.field-updated { margin-top: 0.6rem; font-size: 0.75rem; color: var(--text-muted); }

.field-weather-chip {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.6rem;
  padding-top: 0.6rem;
  border-top: 1px dashed var(--border);
  font-size: 0.85rem;
  color: var(--text-muted);
}
.field-weather-chip svg { flex-shrink: 0; }

/* ===== Badge ===== */
.badge {
  display: inline-block;
  padding: 0.18rem 0.55rem;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.badge-info { background: var(--info-bg); color: var(--info-fg); }
.badge-warning { background: var(--warning-bg); color: var(--warning-fg); }
.badge-critical { background: var(--critical-bg); color: var(--critical-fg); }
.badge-muted { background: var(--muted-bg); color: var(--muted-fg); }

/* ===== Tabelle ===== */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td {
  text-align: left;
  padding: 0.65rem 0.85rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.88rem;
}
.table tr:last-child td { border-bottom: none; }
.table th { color: var(--text-muted); font-weight: 600; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.03em; background: var(--surface-2); }
.table tbody tr { transition: background-color 0.1s ease; }
.table tbody tr:hover { background: var(--surface-2); }

/* ===== Form ===== */
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.85rem 1.1rem;
}
.form-grid label { display: flex; flex-direction: column; font-size: 0.82rem; color: var(--text-muted); gap: 0.3rem; }
.form-grid label.full { grid-column: 1 / -1; }
.form-grid label.checkbox { flex-direction: row; align-items: center; gap: 0.5rem; }
.form-grid input, .form-grid select, .form-grid textarea {
  font: inherit;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
}
.form-grid input:focus, .form-grid select:focus, .form-grid textarea:focus {
  outline: 2px solid var(--green);
  outline-offset: 1px;
}
.form-grid button { grid-column: 1 / -1; justify-self: start; }

.inline-form { display: inline-block; margin: 0 0.25rem 0 0; }

button, .btn {
  background: var(--green);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 0.5rem 1rem;
  cursor: pointer;
  font: inherit;
  font-weight: 600;
  transition: background-color 0.15s ease, transform 0.1s ease;
}
button:hover { background: var(--green-dark); }
button:active { transform: scale(0.98); }
button.danger { background: var(--critical-fg); }
button.danger:hover { filter: brightness(0.9); }
button.secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
button.secondary:hover { border-color: var(--green); color: var(--green-dark); }

.link-btn {
  background: none;
  color: var(--critical-fg);
  padding: 0;
  text-decoration: underline;
  font-weight: 500;
}
.link-btn:hover { background: none; }

.add-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

/* ===== Dialog (modali) ===== */
dialog {
  border: none;
  border-radius: var(--radius);
  padding: 1.5rem;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
  max-width: 560px;
  width: calc(100% - 2rem);
}
dialog h2 { margin-top: 0; border: none; padding: 0; }
dialog::backdrop {
  background: rgba(10, 15, 10, 0.45);
  backdrop-filter: blur(2px);
}

@supports (transition: display allow-discrete) {
  dialog {
    opacity: 0;
    transform: translateY(8px) scale(0.98);
    transition: opacity 0.18s ease, transform 0.18s ease, display 0.18s allow-discrete, overlay 0.18s allow-discrete;
  }
  dialog[open] { opacity: 1; transform: translateY(0) scale(1); }
  @starting-style {
    dialog[open] { opacity: 0; transform: translateY(8px) scale(0.98); }
  }
  dialog::backdrop {
    opacity: 0;
    transition: opacity 0.18s ease, display 0.18s allow-discrete, overlay 0.18s allow-discrete;
  }
  dialog[open]::backdrop { opacity: 1; }
  @starting-style {
    dialog[open]::backdrop { opacity: 0; }
  }
}

.dialog-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 0.25rem; }

/* ===== Tab campi (pagina sensori) ===== */
.field-tabs { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.field-tab {
  padding: 0.4rem 0.9rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 600;
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}
.field-tab:hover { border-color: var(--green); color: var(--green-dark); }
.field-tab.active { background: var(--green); border-color: var(--green); color: white; }

/* ===== Blocchi di codice con copia ===== */
.code-block {
  position: relative;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin: 0.6rem 0 1rem;
}
.code-block pre {
  margin: 0;
  padding: 1rem 5.5rem 1rem 1rem;
  font-family: "SFMono-Regular", Consolas, Menlo, monospace;
  font-size: 0.82rem;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text);
}
.copy-btn {
  position: absolute;
  top: 0.6rem;
  right: 0.6rem;
  background: var(--surface);
  color: var(--text-muted);
  border: 1px solid var(--border);
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0.3rem 0.6rem;
}
.copy-btn:hover { color: var(--green-dark); border-color: var(--green); background: var(--surface); }

/* ===== Grafici / meteo ===== */
.chart-toggle { margin-bottom: 0.75rem; display: flex; gap: 0.4rem; }
.range-btn { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); font-weight: 500; }
.range-btn.active { background: var(--green); color: white; border-color: var(--green); }

.chart-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1rem;
}
.chart-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem;
}
.chart-box h3 { margin: 0 0 0.75rem; }

.weather-current {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem 1.5rem;
}
.weather-current .weather-temp { font-size: 2.4rem; font-weight: 800; color: var(--green-dark); }
.weather-current .weather-label { color: var(--text-muted); font-size: 0.9rem; }
.weather-current-details { display: flex; gap: 1.5rem; font-size: 0.85rem; color: var(--text-muted); }

.forecast-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 0.75rem; margin-top: 1rem; }
.forecast-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 0.6rem;
  text-align: center;
  box-shadow: var(--shadow);
}
.forecast-card .forecast-day { font-size: 0.78rem; color: var(--text-muted); text-transform: uppercase; }
.forecast-card .forecast-icon { margin: 0.35rem 0; }
.forecast-card .forecast-temps { font-size: 0.85rem; font-weight: 600; }
.forecast-card .forecast-precip { font-size: 0.75rem; color: var(--accent); margin-top: 0.2rem; }

/* ===== Vari ===== */
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; }
.plain-list { list-style: none; padding: 0; margin: 0; }
.plain-list li { padding: 0.4rem 0; border-bottom: 1px solid var(--border); }
.plain-list li:last-child { border-bottom: none; }

.filter-bar {
  display: flex;
  gap: 1rem;
  align-items: end;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.85rem 1.1rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
.filter-bar label { display: flex; flex-direction: column; font-size: 0.8rem; color: var(--text-muted); gap: 0.25rem; }

.section-header { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.section-header h2 { flex: 1; margin-bottom: 0; }

details { margin-top: 1rem; }
summary { cursor: pointer; color: var(--green-dark); font-weight: 600; }

/* ===== Contenuti lunghi (pagine di documentazione) ===== */
pre {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  overflow-x: auto;
  font-size: 0.82rem;
  line-height: 1.55;
}
pre code { background: none; border: none; padding: 0; font-size: inherit; }
code {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 0.08em 0.35em;
  font-size: 0.86em;
}
.main-content ul, .main-content ol { padding-left: 1.25rem; }
.main-content li { margin-bottom: 0.35rem; }
.main-content li:last-child { margin-bottom: 0; }

@media (max-width: 860px) {
  .app-shell { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; flex-direction: row; align-items: center; overflow-x: auto; }
  .sidebar nav { flex-direction: row; }
  /* La sidebar diventa orizzontale: il gruppo si appiattisce e le voci
     figlie tornano allo stesso livello delle altre. */
  .nav-group { flex-direction: row; align-items: center; }
  .nav-group-label { display: none; }
  .nav-group-items { flex-direction: row; margin-left: 0; padding-left: 0; border-left: none; }
  .nav-link.nav-sub { padding-left: 0.7rem; font-size: 0.9rem; }
  .brand { padding: 0 0.75rem 0 0; }
  .theme-toggle { margin-left: auto; }
  .main-content { padding: 1.25rem; max-width: 100%; }
  .two-col { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
}

/* ===== Pagina di login ===== */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 2rem;
  width: 320px;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}
.login-card label { display: flex; flex-direction: column; font-size: 0.82rem; color: var(--text-muted); gap: 0.3rem; }
.login-card input {
  font: inherit;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
}
.login-error { background: var(--critical-bg); color: var(--critical-fg); padding: 0.5rem 0.75rem; border-radius: 8px; font-size: 0.85rem; }

/* Selettore di coordinate su mappa (partials/coord_picker.html). */
.form-grid .coord-picker { grid-column: 1 / -1; display: flex; flex-direction: column; gap: 0.6rem; }
.coord-picker-inputs { display: grid; grid-template-columns: 1fr 1fr; gap: 0.85rem 1.1rem; }
.coord-picker-map {
  height: 300px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  /* La mappa sta dentro un <dialog>, che crea un nuovo contesto di stacking:
     senza questo i controlli di zoom di Leaflet finiscono sopra al resto. */
  z-index: 0;
}
.coord-picker-hint { display: flex; align-items: center; gap: 0.6rem; margin: 0; font-size: 0.82rem; }
.coord-picker-readout { font-variant-numeric: tabular-nums; color: var(--text); font-weight: 600; }
.coord-picker-hint .link-btn { margin-left: auto; }
@media (max-width: 620px) {
  .coord-picker-inputs { grid-template-columns: 1fr; }
  .coord-picker-map { height: 220px; }
}

/* Avvisi in testa a una pagina (es. irrigazione bloccata). */
.banner {
  padding: 0.7rem 0.9rem;
  border-radius: var(--radius);
  font-size: 0.88rem;
  margin: 0 0 1rem;
  border: 1px solid transparent;
}
.banner-critical { background: var(--critical-bg); color: var(--critical-fg); border-color: currentColor; }
.banner-info { background: var(--info-bg); color: var(--info-fg); border-color: currentColor; }

/* Registro pH/EC della soluzione nutritiva. */
.solution-targets { display: flex; gap: 1.5rem; flex-wrap: wrap; font-size: 0.85rem; margin-bottom: 0.8rem; }
.solution-value { font-variant-numeric: tabular-nums; font-weight: 600; }
.solution-value.out-of-range { color: var(--critical-fg); }
