/* Trasparenza retributiva – foglio di stile
   Token di colore in :root, tema scuro con prefers-color-scheme, stampa ottimizzata. */

:root {
  color-scheme: light;
  --page: #f5f5f2;
  --surface: #ffffff;
  --surface-2: #fafaf8;
  --surface-3: #f0efeb;
  --ink: #111110;
  --ink-2: #4a4945;
  --muted: #6b6a65;
  --line: #e4e3dc;
  --line-strong: #c9c8bf;
  --link: #1f55a8;
  --btn: #1c1c1a;
  --btn-ink: #ffffff;
  --focus: #2a78d6;

  /* serie (palette validata: slot 2 e slot 1) */
  --series-f: #eb6834;
  --series-m: #2a78d6;
  --series-f-soft: #fbe3d9;
  --series-m-soft: #dbe9f9;
  --mark-neutral: #a8a69e;
  --mark-accent: #4a3aa7;

  /* stati: sempre con icona o etichetta */
  --good: #0ca30c;
  --good-ink: #0a6b0a;
  --good-bg: #e8f5e8;
  --warning: #fab219;
  --warning-ink: #7a4f00;
  --warning-bg: #fdf3dc;
  --critical: #d03b3b;
  --critical-ink: #a8231f;
  --critical-bg: #fbe9e8;
  --info-ink: #1f4f8f;
  --info-bg: #e7f0fb;

  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(17, 17, 16, 0.05), 0 1px 1px rgba(17, 17, 16, 0.03);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0f0f0e;
    --surface: #1a1a19;
    --surface-2: #201f1e;
    --surface-3: #272624;
    --ink: #f4f3ef;
    --ink-2: #c3c2b7;
    --muted: #9c9a92;
    --line: #2f2e2b;
    --line-strong: #45443f;
    --link: #8ab4f0;
    --btn: #f4f3ef;
    --btn-ink: #111110;
    --series-f: #d95926;
    --series-m: #3987e5;
    --series-f-soft: #3a2419;
    --series-m-soft: #192a3f;
    --mark-neutral: #75736c;
    --mark-accent: #9085e9;
    --good-ink: #5ccf5c;
    --good-bg: #16301a;
    --warning-ink: #f5c65a;
    --warning-bg: #33280f;
    --critical: #e66767;
    --critical-ink: #ff9b98;
    --critical-bg: #3a1a19;
    --info-ink: #9cc3f5;
    --info-bg: #172638;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0f0f0e;
  --surface: #1a1a19;
  --surface-2: #201f1e;
  --surface-3: #272624;
  --ink: #f4f3ef;
  --ink-2: #c3c2b7;
  --muted: #9c9a92;
  --line: #2f2e2b;
  --line-strong: #45443f;
  --link: #8ab4f0;
  --btn: #f4f3ef;
  --btn-ink: #111110;
  --series-f: #d95926;
  --series-m: #3987e5;
  --series-f-soft: #3a2419;
  --series-m-soft: #192a3f;
  --mark-neutral: #75736c;
  --mark-accent: #9085e9;
  --good-ink: #5ccf5c;
  --good-bg: #16301a;
  --warning-ink: #f5c65a;
  --warning-bg: #33280f;
  --critical: #e66767;
  --critical-ink: #ff9b98;
  --critical-bg: #3a1a19;
  --info-ink: #9cc3f5;
  --info-bg: #172638;
  --shadow: none;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 14px/1.5 var(--font);
}
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { line-height: 1.25; margin: 0 0 .5rem; font-weight: 650; }
h1 { font-size: 1.55rem; letter-spacing: -0.01em; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75rem; }
small, .small { font-size: .85em; }
.muted { color: var(--muted); }
.ink-2 { color: var(--ink-2); }
.nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.center { text-align: center; }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .92em; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ------------------------------------------------------------ struttura */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  height: 56px; padding: 0 20px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; color: var(--ink); white-space: nowrap; }
.brand:hover { text-decoration: none; }
.brand-mark { width: 26px; height: 26px; flex: none; }
.brand small { font-weight: 500; color: var(--muted); }
.switchers { display: flex; gap: 8px; align-items: center; min-width: 0; flex: 1; }
.switchers select { max-width: 280px; }
.user-menu { display: flex; align-items: center; gap: 12px; white-space: nowrap; color: var(--ink-2); }

.layout { display: grid; grid-template-columns: 236px minmax(0, 1fr); min-height: calc(100vh - 56px); }
.layout.no-side { grid-template-columns: minmax(0, 1fr); }
.sidebar {
  border-right: 1px solid var(--line); background: var(--surface);
  padding: 16px 10px 40px; position: sticky; top: 56px; height: calc(100vh - 56px); overflow-y: auto;
}
.sidebar h4 {
  font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
  margin: 18px 10px 6px; font-weight: 600;
}
.sidebar a {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 7px 10px; border-radius: var(--radius-sm); color: var(--ink-2);
}
.sidebar a:hover { background: var(--surface-3); text-decoration: none; color: var(--ink); }
.sidebar a.active { background: var(--surface-3); color: var(--ink); font-weight: 600; }
.sidebar .count {
  font-size: .75rem; min-width: 20px; padding: 0 6px; border-radius: 10px; text-align: center;
  background: var(--critical-bg); color: var(--critical-ink); font-weight: 600;
}
.main { padding: 24px 28px 60px; min-width: 0; }
.main.wide { padding: 24px 28px 60px; }
.container { max-width: 1240px; margin: 0 auto; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 18px;
  flex-wrap: wrap; }
.page-head p { color: var(--ink-2); max-width: 820px; margin: 0; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ------------------------------------------------------------ card e griglie */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px 20px; margin-bottom: 16px; min-width: 0;
}
.card > h2:first-child, .card > h3:first-child { margin-bottom: 10px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin-bottom: 10px;
  flex-wrap: wrap; }
.card-head h2, .card-head h3 { margin: 0; }
.card-sub { color: var(--muted); font-size: .88rem; margin: -4px 0 12px; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.span-2 { grid-column: span 2; }

/* ------------------------------------------------------------ KPI */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(168px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px;
  box-shadow: var(--shadow); min-width: 0;
}
.kpi .label { font-size: .8rem; color: var(--ink-2); display: flex; gap: 6px; align-items: baseline; }
.kpi .label .tag { font-weight: 700; color: var(--muted); }
.kpi .value { font-size: 1.75rem; font-weight: 650; margin-top: 4px; letter-spacing: -0.01em; }
.kpi .value.hero { font-size: 2.6rem; }
.kpi .detail { font-size: .8rem; color: var(--muted); margin-top: 2px; }
.kpi .delta { font-size: .8rem; margin-top: 4px; font-weight: 600; }
.delta.good { color: var(--good-ink); }
.delta.bad { color: var(--critical-ink); }
.delta.flat { color: var(--muted); }
.kpi.kpi-alert { border-color: var(--critical); }

/* ------------------------------------------------------------ badge e alert */
.badge {
  display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px;
  font-size: .78rem; font-weight: 600; white-space: nowrap; line-height: 1.6;
}
.badge-ok { background: var(--good-bg); color: var(--good-ink); }
.badge-critical { background: var(--critical-bg); color: var(--critical-ink); }
.badge-warning { background: var(--warning-bg); color: var(--warning-ink); }
.badge-info { background: var(--info-bg); color: var(--info-ink); }
.badge-muted { background: var(--surface-3); color: var(--muted); }
.badge-outline { border: 1px solid var(--line-strong); color: var(--ink-2); }
.alert {
  border-radius: var(--radius); padding: 12px 14px; margin-bottom: 14px; border: 1px solid transparent;
  display: flex; gap: 10px; align-items: flex-start;
}
.alert .icon { flex: none; font-weight: 700; }
.alert-info { background: var(--info-bg); color: var(--info-ink); }
.alert-warning { background: var(--warning-bg); color: var(--warning-ink); }
.alert-error { background: var(--critical-bg); color: var(--critical-ink); }
.alert-ok { background: var(--good-bg); color: var(--good-ink); }
.alert a { color: inherit; text-decoration: underline; }
.flashes { margin-bottom: 12px; }

/* ------------------------------------------------------------ tabelle */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.card .table-wrap { border-radius: var(--radius-sm); }
table { border-collapse: collapse; width: 100%; font-size: .88rem; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; text-align: left; }
th { background: var(--surface-2); font-weight: 600; color: var(--ink-2); font-size: .78rem; position: sticky; top: 0;
  z-index: 1; }
th.num, td.num { text-align: right; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: none; }
tr.muted-row td { color: var(--muted); }
tfoot td { font-weight: 600; background: var(--surface-2); }
.table-tools { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.cell-sub { display: block; color: var(--muted); font-size: .78rem; }

/* ------------------------------------------------------------ form */
label { font-weight: 600; font-size: .85rem; color: var(--ink-2); display: block; margin-bottom: 4px; }
input[type=text], input[type=password], input[type=number], input[type=date], input[type=search], input[type=file],
select, textarea {
  width: 100%; padding: 7px 10px; border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--ink); font: inherit;
}
input[type=file] { padding: 6px; }
textarea { min-height: 80px; resize: vertical; }
select.compact, input.compact { width: auto; padding: 4px 8px; font-size: .85rem; }
td select.compact { max-width: 190px; width: 100%; }
#voci td:nth-child(2) { min-width: 180px; max-width: 280px; }
#voci .cell-sub { font-size: .74rem; }
.field { margin-bottom: 14px; }
.help { color: var(--muted); font-size: .8rem; margin-top: 4px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 12px 18px; }
.check { display: flex; gap: 8px; align-items: flex-start; font-weight: 500; color: var(--ink); }
.check input { margin-top: 3px; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin: 0 0 14px; }
legend { font-weight: 650; padding: 0 6px; }
.radio-card {
  display: flex; gap: 10px; align-items: flex-start; padding: 12px; border: 1px solid var(--line);
  border-radius: var(--radius-sm); margin-bottom: 8px; cursor: pointer; font-weight: 500; color: var(--ink);
}
.radio-card:has(input:checked) { border-color: var(--ink); background: var(--surface-2); }
.radio-card input { margin-top: 3px; }
.radio-card .help { margin: 2px 0 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 7px 14px; border-radius: var(--radius-sm); border: 1px solid var(--line-strong);
  background: var(--surface); color: var(--ink); font: inherit; font-weight: 600; cursor: pointer;
  white-space: nowrap; line-height: 1.3;
}
.btn:hover { background: var(--surface-3); text-decoration: none; }
.btn-primary { background: var(--btn); color: var(--btn-ink); border-color: var(--btn); }
.btn-primary:hover { background: var(--btn); opacity: .88; }
.btn-danger { color: var(--critical-ink); border-color: var(--critical); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--link); padding-left: 6px; padding-right: 6px; }
.btn-sm { padding: 4px 10px; font-size: .82rem; }
.btn[disabled] { opacity: .45; cursor: not-allowed; }

/* ------------------------------------------------------------ tab e filtri */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 14px; flex-wrap: wrap; }
.tabs a { padding: 8px 12px; color: var(--ink-2); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a.active { color: var(--ink); border-bottom-color: var(--ink); font-weight: 600; }
.tabs a:hover { text-decoration: none; color: var(--ink); }
.pills { display: flex; gap: 6px; flex-wrap: wrap; }
.pill { padding: 3px 10px; border-radius: 999px; border: 1px solid var(--line-strong); color: var(--ink-2);
  font-size: .82rem; }
.pill.active { background: var(--ink); color: var(--page); border-color: var(--ink); }
.pill:hover { text-decoration: none; }

/* ------------------------------------------------------------ grafici */
.chart { display: block; width: 100%; height: auto; overflow: visible; font-family: var(--font); }
.chart-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.chart text { font-size: 12px; }
.chart .t-small { font-size: 11px; }
.t-primary { fill: var(--ink); }
.t-secondary { fill: var(--ink-2); }
.t-muted { fill: var(--muted); }
.t-on-f, .t-on-m { fill: #ffffff; }
.mk-f { fill: var(--series-f); stroke: none; }
.mk-m { fill: var(--series-m); stroke: none; }
.mk-neutral { fill: var(--mark-neutral); }
.mk-critical { fill: var(--critical); }
.mk-accent { fill: var(--mark-accent); }
.chart .dot { stroke: var(--surface); stroke-width: 2px; }
.chart .line { fill: none; stroke-width: 2px; stroke-linejoin: round; stroke-linecap: round; }
.chart .line.mk-f { stroke: var(--series-f); fill: none; }
.chart .line.mk-m { stroke: var(--series-m); fill: none; }
.chart .line.mk-accent { stroke: var(--mark-accent); fill: none; }
.chart .line.mk-neutral { stroke: var(--mark-neutral); fill: none; }
.chart .grid { stroke: var(--line); stroke-width: 1px; }
.chart .axis { stroke: var(--line-strong); stroke-width: 1px; }
.chart .threshold { stroke: var(--critical); stroke-width: 1px; opacity: .55; }
.chart .band { fill: var(--critical-bg); opacity: .6; }
.chart .mean-mark { stroke: var(--ink); stroke-width: 2px; }
.chart [data-tip] { cursor: default; }
.chart [data-tip]:hover { opacity: .85; }
.legend { display: flex; gap: 14px; align-items: center; font-size: .82rem; color: var(--ink-2); margin: 2px 0 10px;
  flex-wrap: wrap; }
.key { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: -8px; }
.key-f { background: var(--series-f); }
.key-m { background: var(--series-m); }
.key-crit { background: var(--critical); }
.key-neutral { background: var(--mark-neutral); }
.key-line { display: inline-block; width: 16px; height: 2px; background: var(--critical); opacity: .6; margin-right: -8px;
  vertical-align: middle; }
details.table-view { margin-top: 8px; }
details.table-view summary { cursor: pointer; color: var(--link); font-size: .85rem; }
.tooltip {
  position: fixed; z-index: 100; pointer-events: none; background: var(--ink); color: var(--page);
  padding: 6px 9px; border-radius: 6px; font-size: .8rem; max-width: 320px; box-shadow: 0 4px 16px rgba(0,0,0,.18);
  opacity: 0; transition: opacity .08s;
}
.tooltip.show { opacity: 1; }
.bar-inline { display: inline-block; height: 8px; border-radius: 2px; vertical-align: middle; }

/* ------------------------------------------------------------ elementi specifici */
.steps { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; margin-bottom: 16px; }
.step { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.step .n { font-size: .75rem; color: var(--muted); font-weight: 700; }
.step .t { font-weight: 600; margin: 2px 0; }
.step.done .n::after { content: " ✓"; color: var(--good-ink); }
.step.todo { border-style: dashed; }
.kv { display: grid; grid-template-columns: minmax(160px, 38%) 1fr; gap: 6px 14px; font-size: .9rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty h2 { color: var(--ink); }
.company-row td:first-child a { font-weight: 600; color: var(--ink); }
.stack > * + * { margin-top: 8px; }
.letter { max-width: 760px; }
.letter h3 { margin-top: 14px; }
.sep { border: none; border-top: 1px solid var(--line); margin: 18px 0; }
.verdict { display: flex; gap: 14px; align-items: center; padding: 14px 16px; border-radius: var(--radius); }
.verdict .dot-big { width: 18px; height: 18px; border-radius: 50%; flex: none; }
.verdict.verde { background: var(--good-bg); color: var(--good-ink); }
.verdict.verde .dot-big { background: var(--good); }
.verdict.ambra { background: var(--warning-bg); color: var(--warning-ink); }
.verdict.ambra .dot-big { background: var(--warning); }
.verdict.rosso { background: var(--critical-bg); color: var(--critical-ink); }
.verdict.rosso .dot-big { background: var(--critical); }
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 400px; }
.print-only { display: none; }
.hide { display: none !important; }

/* ------------------------------------------------------------ responsive */
@media (max-width: 1100px) {
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; border-right: none; border-bottom: 1px solid var(--line);
    display: flex; gap: 4px; overflow-x: auto; padding: 8px 12px; white-space: nowrap; }
  .sidebar h4 { display: none; }
  .sidebar a { flex: none; }
  .main, .main.wide { padding: 16px; }
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .topbar { height: auto; min-height: 56px; padding: 8px 16px; gap: 8px 12px; flex-wrap: wrap; }
  .brand small, .user-menu .who, .user-menu .admin-link { display: none; }
  .switchers { order: 3; flex-basis: 100%; }
  .switchers select { max-width: none; flex: 1; min-width: 0; }
  .user-menu { margin-left: auto; }
  .sidebar { top: 0; }
}
@media (max-width: 560px) {
  .kpi .value { font-size: 1.45rem; }
  .kpi .value.hero { font-size: 2rem; }
  .kv { grid-template-columns: 1fr; }
}

/* ------------------------------------------------------------ stampa */
@media print {
  :root { color-scheme: light; }
  body { background: #fff; font-size: 11px; }
  .topbar, .sidebar, .no-print, .actions, .btn, details.table-view summary, .flashes, .tabs { display: none !important; }
  .layout { display: block; }
  .main { padding: 0; }
  .card, .kpi, .table-wrap { box-shadow: none; break-inside: avoid; }
  .print-only { display: block; }
  .page-break { break-before: page; }
  a { color: inherit; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  th { position: static; }
  @page { size: A4; margin: 14mm 12mm; }
}
