:root {
  --ink: #1b2a41;
  --ink-2: #2a3d5a;
  --paper: #ffffff;
  --surface: #f2f4f7;
  --line: #d9dee5;
  --line-2: #e8ebf0;
  --text: #1d2433;
  --muted: #5b6575;
  --accent: #0b6e99;
  --accent-soft: #e3f1f7;
  --late: #b42318;
  --late-soft: #fdecea;
  --warn: #b54708;
  --warn-soft: #fef3e2;
  --ok: #067647;
  --ok-soft: #e7f6ee;
  --radius: 6px;
  --font: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 14px/1.45 var(--font); color: var(--text); background: var(--surface); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
h1 { font-size: 22px; font-weight: 600; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 16px; font-weight: 600; margin: 28px 0 10px; }
h3 { font-size: 14px; font-weight: 600; margin: 18px 0 8px; }
.num, td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }

/* ---------- impaginazione */
.app { display: grid; grid-template-columns: 228px 1fr; min-height: 100vh; }
.side { background: var(--ink); color: #c9d3e1; display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.brand { padding: 18px 18px 14px; border-bottom: 1px solid var(--ink-2); }
.brand b { display: block; color: #fff; font-size: 15px; font-weight: 600; }
.brand span { font-size: 12px; color: #93a3ba; }
.side nav { padding: 10px 8px; flex: 1; overflow-y: auto; }
.side nav a { display: flex; justify-content: space-between; align-items: center; color: #c9d3e1; padding: 7px 10px; border-radius: 5px; margin: 1px 0; }
.side nav a:hover { background: var(--ink-2); text-decoration: none; color: #fff; }
.side nav a.on { background: #fff; color: var(--ink); font-weight: 600; }
.side nav .grp { font-size: 12px; color: #7f90a8; padding: 14px 10px 4px; }
.side .count { font-size: 11px; background: #3b4f6e; color: #fff; padding: 0 7px; border-radius: 9px; }
.side nav a.on .count { background: var(--ink); }
.who { padding: 12px 18px; border-top: 1px solid var(--ink-2); font-size: 12px; }
.who b { color: #fff; display: block; font-size: 13px; }
.who form { margin-top: 6px; display: flex; gap: 10px; }
.who button, .who a { background: none; border: 0; color: #93a3ba; padding: 0; font: inherit; cursor: pointer; }
.who button:hover, .who a:hover { color: #fff; }
main { padding: 22px 28px 60px; min-width: 0; }
.pagehead { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 16px; flex-wrap: wrap; }
.pagehead p { margin: 4px 0 0; color: var(--muted); max-width: 80ch; }
.crumbs { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.crumbs a { color: var(--muted); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- elementi */
.panel { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; margin-bottom: 16px; }
.panel > h2:first-child, .panel > h3:first-child { margin-top: 0; }
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 13px; border: 1px solid var(--line); background: var(--paper);
  color: var(--text); border-radius: 5px; font: inherit; font-weight: 500; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { border-color: #b5bfcc; text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #095a7d; }
.btn.danger { color: var(--late); border-color: #f0c4bf; }
.btn.small { padding: 3px 9px; font-size: 12px; }
input[type=text], input[type=password], input[type=date], input[type=number], input[type=email], select, textarea {
  font: inherit; padding: 6px 8px; border: 1px solid var(--line); border-radius: 5px; background: #fff; color: var(--text); min-width: 0; }
textarea { width: 100%; min-height: 64px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px var(--accent-soft); }
input[readonly] { background: var(--surface); }
label { font-size: 12px; color: var(--muted); display: block; margin-bottom: 3px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; background: var(--paper); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 10px 12px; margin-bottom: 14px; }
.filters .f { display: flex; flex-direction: column; }
.filters select, .filters input { max-width: 220px; }

.flash { padding: 10px 14px; border-radius: 5px; margin-bottom: 12px; border: 1px solid; }
.flash.ok { background: var(--ok-soft); border-color: #b4e2c8; color: #05512f; }
.flash.error { background: var(--late-soft); border-color: #f4c7c1; color: #8a1a12; }
.flash.info { background: var(--accent-soft); border-color: #b9dcea; color: #0a4f6e; }

/* ---------- tabelle */
.tablewrap { overflow-x: auto; background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); }
table.grid { border-collapse: collapse; width: 100%; font-size: 13px; }
.grid th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12px; background: #f7f8fa; border-bottom: 1px solid var(--line);
  padding: 7px 10px; position: sticky; top: 0; white-space: nowrap; }
.grid td { padding: 7px 10px; border-bottom: 1px solid var(--line-2); vertical-align: top; }
.grid tr:last-child td { border-bottom: 0; }
.grid tbody tr:hover td { background: #fafbfc; }
.grid tr.tot td { font-weight: 600; background: #f7f8fa; border-top: 1px solid var(--line); }
.grid tr.sub td { background: #f7f8fa; font-weight: 600; }
.grid td.tit { max-width: 270px; }
.grid .clip { display: block; max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 12px; background: var(--surface); color: var(--text);
  border: 1px solid var(--line); white-space: nowrap; }
.badge.chiuso { background: var(--ok-soft); border-color: #bfe5cf; color: #05512f; }
.badge.storico { background: #eef0f3; color: var(--muted); }
.badge.nuovo { background: var(--accent-soft); border-color: #bfdeea; color: #0a4f6e; }
.badge.alto, .badge.ambiguo, .badge.errore { background: var(--late-soft); border-color: #f4c7c1; color: var(--late); }
.badge.medio, .badge.variato, .badge.non_abbinato { background: var(--warn-soft); border-color: #f7d9b2; color: var(--warn); }
.badge.abbinato, .badge.aggregato, .badge.presente, .badge.gia_abbinato { background: var(--ok-soft); border-color: #bfe5cf; color: #05512f; }
.alerts { display: flex; flex-direction: column; gap: 2px; }
.alert-l { font-size: 12px; display: flex; gap: 6px; align-items: baseline; }
.alert-l::before { content: ""; width: 7px; height: 7px; border-radius: 50%; flex: none; transform: translateY(-1px); background: var(--muted); }
.alert-l.alto { color: var(--late); } .alert-l.alto::before { background: var(--late); }
.alert-l.medio { color: var(--warn); } .alert-l.medio::before { background: var(--warn); }
.alert-l.info { color: var(--muted); }

/* ---------- barra dei termini: giorno 0 → 20 → 40 */
.tbar { position: relative; width: 132px; height: 22px; margin-top: 1px; }
.tbar .track { position: absolute; left: 0; right: 0; top: 8px; height: 6px; border-radius: 3px; overflow: hidden;
  background: linear-gradient(90deg, #d7e7ef 0 40%, #f7e3c6 40% 80%, #f6cfca 80% 100%); }
.tbar .fill { position: absolute; left: 0; top: 0; bottom: 0; background: var(--accent); }
.tbar.w .fill { background: var(--warn); }
.tbar.l .fill { background: var(--late); }
.tbar.c .fill { background: #9aa6b5; }
.tbar .tick { position: absolute; top: 4px; width: 1px; height: 14px; background: #8b97a7; }
.tbar .esito { position: absolute; top: 5px; width: 10px; height: 10px; margin-left: -5px; border-radius: 50%; background: #fff;
  border: 2px solid var(--ok); }
.tbar .lbl { position: absolute; top: -3px; font-size: 10px; color: var(--muted); transform: translateX(-50%); display: none; }
.tlegend { font-size: 11px; color: var(--muted); }

/* ---------- scheda pagamento */
.sheet { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 16px; align-items: start; }
.kv { display: grid; grid-template-columns: 170px 1fr; gap: 4px 12px; font-size: 13px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.fgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px 16px; }
.fgrid .wide { grid-column: 1 / -1; }
.fgrid input, .fgrid select { width: 100%; }
.field.err input, .field.err select { border-color: var(--late); }
.field .msg { color: var(--late); font-size: 12px; }
fieldset { border: 0; padding: 0; margin: 0 0 18px; }
legend { font-weight: 600; font-size: 14px; margin-bottom: 10px; padding: 0; }
.savebar { position: sticky; bottom: 0; background: var(--paper); border-top: 1px solid var(--line); margin: 0 -18px -16px; padding: 12px 18px;
  display: flex; gap: 10px; align-items: center; border-radius: 0 0 var(--radius) var(--radius); }
.term-big { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.term-big div { background: var(--surface); border-radius: 5px; padding: 8px 10px; }
.term-big b { display: block; font-size: 16px; font-variant-numeric: tabular-nums; }
.conflict { border: 1px solid #f4c7c1; background: var(--late-soft); border-radius: var(--radius); padding: 12px 16px; margin-bottom: 14px; }
.conflict table { font-size: 13px; border-collapse: collapse; }
.conflict td, .conflict th { padding: 3px 12px 3px 0; text-align: left; }
.timeline { list-style: none; padding: 0; margin: 0; font-size: 12px; }
.timeline li { padding: 6px 0; border-bottom: 1px solid var(--line-2); }
.timeline li:last-child { border-bottom: 0; }
.timeline .when { color: var(--muted); }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 10px; }
.tabs button { background: none; border: 0; border-bottom: 2px solid transparent; padding: 6px 10px; font: inherit; color: var(--muted); cursor: pointer; }
.tabs button.on { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.tabpane[hidden] { display: none; }

/* ---------- cruscotto */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 0; background: var(--paper);
  border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 16px; }
.kpis a { padding: 14px 16px; border-right: 1px solid var(--line-2); color: var(--text); display: block; }
.kpis a:last-child { border-right: 0; }
.kpis a:hover { background: #fafbfc; text-decoration: none; }
.kpis b { display: block; font-size: 26px; font-weight: 600; font-variant-numeric: tabular-nums; line-height: 1.1; }
.kpis span { font-size: 12px; color: var(--muted); }
.kpis .alto b { color: var(--late); }
.kpis .medio b { color: var(--warn); }
.hbar { height: 8px; background: var(--accent); border-radius: 2px; min-width: 1px; }
.hbar.ok { background: var(--ok); }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.stack { display: flex; height: 10px; border-radius: 3px; overflow: hidden; background: var(--line-2); min-width: 120px; }
.stack span { display: block; height: 100%; }

/* ---------- import */
.upl { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 14px; }
.upl .panel { margin: 0; display: flex; flex-direction: column; gap: 8px; }
.upl p { margin: 0; color: var(--muted); font-size: 13px; flex: 1; }
.counts { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 14px; }
.counts a { display: inline-flex; gap: 6px; align-items: center; padding: 4px 10px; border-radius: 14px; border: 1px solid var(--line);
  background: var(--paper); color: var(--text); font-size: 13px; }
.counts a.on { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.counts b { font-variant-numeric: tabular-nums; }
.cand { margin: 4px 0; padding: 6px 8px; border: 1px solid var(--line-2); border-radius: 4px; font-size: 12px; }
.cand label { display: flex; gap: 6px; color: var(--text); font-size: 12px; margin: 0; cursor: pointer; }
.msgs { margin: 4px 0 0; padding-left: 16px; font-size: 12px; color: var(--muted); }

/* ---------- login */
.loginwrap { min-height: 100vh; display: grid; place-items: center; background: var(--ink); padding: 16px; }
.loginbox { background: var(--paper); border-radius: 8px; padding: 30px 30px 26px; width: 100%; max-width: 380px; }
.loginbox h1 { font-size: 19px; margin-bottom: 4px; }
.loginbox p { color: var(--muted); margin: 0 0 18px; font-size: 13px; }
.loginbox input { width: 100%; margin-bottom: 12px; padding: 9px 10px; }
.loginbox .btn { width: 100%; justify-content: center; padding: 9px; }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .side { position: static; height: auto; }
  .side nav { display: flex; flex-wrap: wrap; }
  .side nav .grp { width: 100%; }
  main { padding: 16px; }
  .sheet, .two { grid-template-columns: 1fr; }
}
@media print {
  .side, .filters, .actions, .savebar { display: none !important; }
  .app { display: block; }
}
.beta, .brand b .beta { display: inline-block; font-size: 10px; font-weight: 600; letter-spacing: .02em; padding: 1px 6px; border-radius: 8px;
  background: #f7d9b2; color: #7a3306; vertical-align: 3px; margin-left: 4px; }
