:root {
  --bg: #f5f5f8;
  --surface: #ffffff;
  --text: #1c1d2b;
  --muted: #62647a;
  --border: #dcdde6;
  --primary: #3b3f9a;
  --primary-hover: #2f3282;
  --primary-text: #ffffff;
  --focus: #8a8ef0;
  --night-1: #1b1d45;
  --night-2: #2b2f6b;
  --moon: #f1d78a;
  --error-bg: #fdecec;
  --error-text: #9b1c1c;
  --info-bg: #ebeefe;
  --info-text: #2f3282;
  --success-bg: #e8f6ee;
  --success-text: #17663a;
  --danger: #c0392b;
  --danger-hover: #a5301f;
  --danger-text: #ffffff;
  --row-hover: #f8f8fb;
  /* Colori dei grafici: palette validata (daltonismo e contrasto) su superficie chiara */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --chart-grid: #ececf1;
  --chart-axis: #c9cad6;
  --mark-ok: #1e8a4c;
  --mark-ko: #8a8ca0;
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101120;
    --surface: #181a2e;
    --text: #e9e9f2;
    --muted: #a0a2b8;
    --border: #2c2e48;
    --primary: #8a8ef0;
    --primary-hover: #a3a6f5;
    --primary-text: #12132a;
    --focus: #b9bbf8;
    --error-bg: #3a1c22;
    --error-text: #f3b4b4;
    --info-bg: #23264a;
    --info-text: #c6c8fa;
    --success-bg: #173226;
    --success-text: #a6e3bf;
    --danger: #e5675a;
    --danger-hover: #ee8579;
    --danger-text: #1a0f0e;
    --row-hover: #1e2036;
    --series-1: #3987e5;
    --series-2: #d95926;
    --chart-grid: #26283f;
    --chart-axis: #3a3d5c;
    --mark-ok: #4cc47e;
    --mark-ko: #7d8099;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }

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

h1 { font-size: 1.6rem; line-height: 1.2; margin: 0 0 .25rem; }
.muted { color: var(--muted); margin: 0; }

/* ---------- Login ---------- */

.auth {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}

.auth-brand {
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: flex-end;
  padding: 3rem;
  color: #fff;
  background:
    radial-gradient(1.5px 1.5px at 20% 30%, #fff9 50%, transparent 51%),
    radial-gradient(1px 1px at 70% 20%, #fff8 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 85% 55%, #fff7 50%, transparent 51%),
    radial-gradient(1px 1px at 35% 70%, #fff6 50%, transparent 51%),
    radial-gradient(1px 1px at 55% 45%, #fff5 50%, transparent 51%),
    linear-gradient(160deg, var(--night-2), var(--night-1) 70%);
}

.auth-brand .brand-mark {
  width: 56px;
  height: 56px;
  color: var(--moon);
  margin-bottom: 1.25rem;
}

.auth-brand-name {
  font-size: 2.5rem;
  font-weight: 700;
  letter-spacing: -.02em;
  margin: 0;
}

.auth-brand-tag {
  max-width: 26ch;
  margin: .5rem 0 0;
  font-size: 1.15rem;
  color: #ffffffc7;
}

.auth-panel {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem 1rem;
}

.auth-card {
  width: 100%;
  max-width: 380px;
}

.auth-card-head { margin-bottom: 1.5rem; }
.auth-card-head .brand-mark { display: none; }

.field { margin-bottom: 1rem; }

label {
  display: block;
  font-weight: 600;
  font-size: .9rem;
  margin-bottom: .35rem;
}

input[type="text"],
input[type="password"],
input[type="search"],
select {
  width: 100%;
  height: 46px;
  padding: 0 .85rem;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  transition: border-color .15s, box-shadow .15s;
}

input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 35%, transparent);
}

.password-field { position: relative; }
.password-field input { padding-right: 5.5rem; }

.password-toggle {
  position: absolute;
  top: 50%;
  right: 6px;
  transform: translateY(-50%);
  height: 34px;
  padding: 0 .7rem;
  font: inherit;
  font-size: .85rem;
  font-weight: 600;
  color: var(--primary);
  background: transparent;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
}

.password-toggle:hover { background: color-mix(in srgb, var(--primary) 10%, transparent); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 46px;
  padding: 0 1.25rem;
  font: inherit;
  font-weight: 600;
  border-radius: 10px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background-color .15s;
}

.btn:focus-visible,
.password-toggle:focus-visible,
.link-button:focus-visible,
.topbar-brand:focus-visible,
.topnav a:focus-visible,
.row-link:focus-visible,
.back-link:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.btn-primary { background: var(--primary); color: var(--primary-text); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:disabled { opacity: .7; cursor: progress; }
.btn-block { width: 100%; margin-top: .5rem; }

.btn-ghost {
  height: 36px;
  padding: 0 .9rem;
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}
.btn-ghost:hover { background: var(--bg); }

.alert {
  padding: .75rem .9rem;
  border-radius: 10px;
  margin-bottom: 1.25rem;
  font-size: .95rem;
}
.alert-error { background: var(--error-bg); color: var(--error-text); }
.alert-info { background: var(--info-bg); color: var(--info-text); }
.alert-success { background: var(--success-bg); color: var(--success-text); }

.auth-help {
  margin: 1.5rem 0 0;
  font-size: .875rem;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 820px) {
  .auth { grid-template-columns: 1fr; }
  .auth-brand { display: none; }
  .auth-panel { align-items: flex-start; padding-top: 12vh; }
  .auth-card-head .brand-mark {
    display: block;
    width: 44px;
    height: 44px;
    color: var(--primary);
    margin-bottom: 1rem;
  }
}

/* ---------- Area riservata ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .75rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: .6rem;
  color: var(--text);
  font-weight: 700;
  text-decoration: none;
  border-radius: 6px;
}
.topbar-brand .brand-mark { width: 28px; height: 28px; color: var(--primary); }

.topbar-left { display: flex; align-items: center; gap: 1.5rem; min-width: 0; }
.topnav { display: flex; gap: .25rem; }
.topnav a {
  padding: .4rem .75rem;
  border-radius: 8px;
  color: var(--muted);
  font-weight: 600;
  text-decoration: none;
}
.topnav a:hover { color: var(--text); background: var(--bg); }
.topnav a[aria-current="page"] { color: var(--primary); background: var(--info-bg); }

.flashes { max-width: 1080px; margin: 1.25rem auto 0; padding: 0 1.5rem; }
.flashes .alert { margin-bottom: .5rem; }

.topbar-user { display: flex; align-items: center; gap: .75rem; }
.topbar-user form { margin: 0; }
.topbar-name { font-weight: 600; }

.badge {
  font-size: .75rem;
  font-weight: 600;
  padding: .15rem .55rem;
  border-radius: 999px;
  background: var(--info-bg);
  color: var(--info-text);
}

.content {
  max-width: 1080px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  margin-top: 1.75rem;
}

.stat {
  display: flex;
  flex-direction: column;
  gap: .25rem;
  padding: 1.1rem 1.25rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.stat-label { font-size: .875rem; color: var(--muted); }
.stat-value { font-size: 1.75rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-value-sm { font-size: 1.15rem; padding-top: .45rem; }

/* ---------- Gestione utenti ---------- */

.content-narrow { max-width: 760px; }

.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1.5rem;
}

.back-link {
  display: inline-block;
  margin-bottom: .75rem;
  color: var(--muted);
  font-weight: 600;
  text-decoration: none;
  border-radius: 4px;
}
.back-link:hover { color: var(--primary); }

h2 { font-size: 1.15rem; margin: 0 0 .75rem; }
.section-title { margin-top: 2rem; }

.btn-secondary { background: var(--surface); color: var(--primary); border-color: var(--primary); }
.btn-secondary:hover { background: var(--info-bg); }
.btn-danger { background: var(--danger); color: var(--danger-text); }
.btn-danger:hover:not(:disabled) { background: var(--danger-hover); }
.btn-danger:disabled { opacity: .45; cursor: not-allowed; }
a.btn { text-decoration: none; }

.filters {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(0, 1fr) minmax(0, 1fr) auto;
  gap: .75rem;
  align-items: end;
  margin-bottom: 1.25rem;
}
.filters .field { margin: 0; }
.filters-actions { display: flex; gap: .5rem; }
.filters-actions .btn { height: 46px; }

select {
  appearance: none;
  padding-right: 2.25rem;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
                    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%;
  background-size: 5px 5px;
  background-repeat: no-repeat;
}
select:disabled, input:disabled { opacity: .6; }

.table-wrap {
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.table { width: 100%; border-collapse: collapse; font-size: .95rem; }
.table th, .table td { padding: .75rem 1rem; text-align: left; white-space: nowrap; }
.table th {
  font-size: .8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .03em;
  border-bottom: 1px solid var(--border);
}
.table td { border-bottom: 1px solid var(--border); }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: var(--row-hover); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }

.row-link { color: var(--primary); font-weight: 600; text-decoration: none; border-radius: 4px; }
.row-link:hover { text-decoration: underline; }

.pill {
  display: inline-block;
  font-size: .75rem;
  font-weight: 600;
  padding: .15rem .55rem;
  border-radius: 999px;
  background: var(--bg);
  color: var(--muted);
  border: 1px solid var(--border);
}
.pill-ok { background: var(--success-bg); color: var(--success-text); border-color: transparent; }
.pill-off { background: var(--error-bg); color: var(--error-text); border-color: transparent; }

.empty {
  padding: 3rem 1rem;
  text-align: center;
  color: var(--muted);
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}
.empty p { margin: 0; }

.pagination {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 1rem;
  margin-top: 1rem;
}
.pagination > :last-child { justify-self: end; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  margin-top: 1.25rem;
}
.panel > .muted, .panel > p { margin: 0 0 1rem; }
.panel-danger { border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }
.panel-danger h2 { color: var(--danger); }

.label-row { display: flex; align-items: baseline; justify-content: space-between; gap: 1rem; }
.link-button {
  padding: 0;
  font: inherit;
  font-size: .85rem;
  font-weight: 600;
  color: var(--primary);
  background: none;
  border: 0;
  border-radius: 4px;
  cursor: pointer;
}
.link-button:hover { text-decoration: underline; }

.field-hint, .field-error { margin: .35rem 0 0; font-size: .85rem; }
.field-hint { color: var(--muted); }
.field-error { color: var(--error-text); font-weight: 600; }
input[aria-invalid="true"] { border-color: var(--error-text); }

.checkbox {
  display: flex;
  align-items: center;
  gap: .6rem;
  font-weight: 500;
  margin: .25rem 0 .25rem;
  cursor: pointer;
}
.checkbox input { width: 18px; height: 18px; margin: 0; accent-color: var(--primary); }

.form-actions { display: flex; gap: .75rem; flex-wrap: wrap; margin-top: 1.25rem; }

.user-badges { display: flex; gap: .4rem; margin: .5rem 0 0; }

.facts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 1px;
  margin: 0;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.facts > div { background: var(--surface); padding: .9rem 1rem; }
.facts dt { font-size: .8rem; color: var(--muted); }
.facts dd { margin: .2rem 0 0; font-weight: 600; }

.history { list-style: none; margin: 0; padding: 0; }
.history li {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  padding: .65rem 0;
  border-bottom: 1px solid var(--border);
  font-size: .95rem;
}

@media (max-width: 720px) {
  .filters { grid-template-columns: 1fr 1fr; }
  .filters .field:first-child { grid-column: 1 / -1; }
  .filters-actions { grid-column: 1 / -1; }
}

@media (max-width: 560px) {
  .topbar { padding: .75rem 1rem; }
  .topbar-left { gap: .75rem; }
  .topbar-brand span, .topbar-name, .topbar-user .badge { display: none; }
  .content { padding: 1.5rem 1rem; }
  .flashes { padding: 0 1rem; }
  .panel { padding: 1.1rem; }
}

/* ---------- Notti e grafici ---------- */

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.inline-form { display: flex; gap: .5rem; }
.inline-form input { width: 200px; }
.inline-form .btn { height: 46px; }

.month-nav { display: flex; align-items: center; gap: .5rem; }
.month-nav form { margin: 0; }
.month-nav select { width: auto; min-width: 180px; font-weight: 600; }
.month-nav .btn { height: 46px; width: 46px; padding: 0; font-size: 1.25rem; }
.btn.is-disabled { opacity: .35; pointer-events: none; }

.night-nav { display: flex; gap: .5rem; flex-wrap: wrap; }

.stat-sub { font-size: .85rem; color: var(--muted); }
.stat-link { color: inherit; text-decoration: none; transition: border-color .15s; }
.stat-link:hover { border-color: var(--primary); }
.stat-link:hover .stat-sub { color: var(--primary); }
.stat-link:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.stat-value { font-variant-numeric: normal; }

.chart { position: relative; }
.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: .35rem 1.25rem;
  list-style: none;
  margin: 0 0 .75rem;
  padding: 0;
  font-size: .875rem;
  color: var(--muted);
}
.chart-legend li { display: flex; align-items: center; gap: .4rem; white-space: nowrap; }
.chart-swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 3px;
  margin-right: .4rem;
  flex: none;
}
.chart-legend .chart-swatch { margin-right: 0; }
.chart-swatch.s1 { background: var(--series-1); }
.chart-swatch.s2 { background: var(--series-2); }

.chart-canvas { position: relative; width: 100%; min-height: 210px; }
.chart-canvas svg { display: block; overflow: visible; }
.chart-grid { stroke: var(--chart-grid); stroke-width: 1; }
.chart-axis { stroke: var(--chart-axis); stroke-width: 1; }
.chart-tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart-bar.s1 { fill: var(--series-1); }
.chart-bar.s2 { fill: var(--series-2); }
.chart-mark { fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.chart-mark.ok { stroke: var(--mark-ok); }
.chart-mark.ko { stroke: var(--mark-ko); stroke-width: 1.6; }
.chart-mark-icon { flex: none; overflow: visible; }
.chart-xtitle { fill: var(--muted); font-size: 11px; }
.chart-hover { fill: var(--text); opacity: 0; }
.chart-hover.is-active { opacity: .05; }
.chart-hit { fill: transparent; cursor: default; }
.chart a .chart-hit { cursor: pointer; }
.chart a:focus-visible { outline: none; }
.chart a:focus-visible .chart-hit { stroke: var(--focus); stroke-width: 2; }
.chart-hit:focus-visible { outline: none; stroke: var(--focus); stroke-width: 2; }

.chart-tooltip {
  position: absolute;
  z-index: 2;
  pointer-events: none;
  min-width: 150px;
  padding: .55rem .7rem;
  font-size: .85rem;
  line-height: 1.45;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgb(0 0 0 / .12);
  white-space: nowrap;
}
.chart-tooltip > div { display: flex; align-items: center; }
.chart-tip-title { font-weight: 600; margin-bottom: .1rem; }

.data-details { margin-top: 1.25rem; }
.data-details summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--primary);
  border-radius: 4px;
}
.data-details summary:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.data-details .table-wrap { margin-top: .75rem; max-height: 420px; overflow-y: auto; }

.notes { white-space: normal; color: var(--muted); }

.row-link-plain { font-weight: 500; }

@media (max-width: 720px) {
  .page-head .inline-form { width: 100%; }
  .inline-form input { flex: 1; width: auto; }
  .month-nav select { min-width: 0; flex: 1; }
  .month-nav form { flex: 1; display: flex; }
}

@media (max-width: 560px) {
  .stats { grid-template-columns: 1fr 1fr; gap: .75rem; margin-top: 1.25rem; }
  .stat { padding: .9rem 1rem; }
  .stat-value { font-size: 1.35rem; }
  .stat-value-sm { font-size: 1.05rem; padding-top: .3rem; }
}

.devices { list-style: none; margin: 0; padding: 0; }
.devices li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .75rem 0;
  border-top: 1px solid var(--border);
}
.devices li > div { display: flex; flex-direction: column; min-width: 0; }
.devices .muted { font-size: .85rem; }
.devices form { margin: 0; }
