:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --ink: #161b26;
  --ink-2: #424a5c;
  --muted: #6c7487;
  --line: #e4e7ec;
  --line-2: #eef0f3;
  --side: #17131c;
  --side-ink: #b9b2c2;
  --accent: #6b2f5c;          /* plum */
  --accent-2: #8a3d77;
  --accent-soft: #f4ecf2;
  --ok: #18794e; --ok-soft: #e6f4ec;
  --warn: #a35a00; --warn-soft: #fdf1e1;
  --bad: #b42318; --bad-soft: #fdecea;
  --info: #175cd3; --info-soft: #eaf1fd;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16,24,40,.05), 0 1px 3px rgba(16,24,40,.06);
  --shadow-lg: 0 20px 48px -12px rgba(16,24,40,.25);
  --font: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 var(--font); color: var(--ink); background: var(--bg); -webkit-font-smoothing: antialiased; }
a { color: var(--accent); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; letter-spacing: -.015em; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.shell { display: grid; grid-template-columns: 236px 1fr; min-height: 100vh; }
.side { background: var(--side); color: var(--side-ink); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; padding: 18px 12px; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 20px; color: #fff; }
.brand-mark { width: 32px; height: 32px; border-radius: 9px; background: linear-gradient(140deg, #8a3d77, #4f1f43); display: grid; place-items: center; flex: none; }
.brand-name { font-weight: 700; font-size: 15px; line-height: 1.1; }
.brand-sub { font-size: 11.5px; color: var(--side-ink); }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-label { font-size: 10.5px; text-transform: uppercase; letter-spacing: .09em; color: #7d7488; padding: 14px 10px 6px; }
.nav a { display: flex; align-items: center; gap: 10px; color: var(--side-ink); padding: 8px 10px; border-radius: 8px; font-weight: 500; }
.nav a svg { width: 17px; height: 17px; opacity: .85; flex: none; }
.nav a:hover { background: rgba(255,255,255,.06); color: #fff; }
.nav a.active { background: rgba(255,255,255,.1); color: #fff; }
.nav a .count { margin-left: auto; background: var(--accent-2); color: #fff; font-size: 11px; font-weight: 600; border-radius: 20px; padding: 1px 7px; }
.side-foot { margin-top: auto; border-top: 1px solid rgba(255,255,255,.08); padding: 12px 8px 0; font-size: 12.5px; }
.side-foot .who { color: #fff; font-weight: 600; }
.side-foot button { background: none; border: 0; color: var(--side-ink); padding: 0; cursor: pointer; text-decoration: underline; font-size: 12px; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar { height: 60px; display: flex; align-items: center; gap: 16px; padding: 0 28px; border-bottom: 1px solid var(--line); background: rgba(255,255,255,.85); backdrop-filter: blur(8px); position: sticky; top: 0; z-index: 20; }
.search { position: relative; flex: 0 1 420px; }
.search input { width: 100%; height: 36px; border: 1px solid var(--line); background: var(--bg); border-radius: 9px; padding: 0 12px 0 34px; outline: none; }
.search input:focus { border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px var(--accent-soft); }
.search svg { position: absolute; left: 11px; top: 10px; width: 16px; height: 16px; color: var(--muted); }
.search-results { position: absolute; top: 42px; left: 0; right: 0; background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow-lg); max-height: 420px; overflow: auto; padding: 6px; }
.search-results a { display: block; padding: 8px 10px; border-radius: 7px; color: var(--ink); }
.search-results a:hover, .search-results a.sel { background: var(--accent-soft); }
.search-results .k { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.topbar .today { margin-left: auto; color: var(--muted); font-size: 13px; }
.content { padding: 26px 28px 60px; max-width: 1480px; width: 100%; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head h1 { font-size: 23px; }
.page-head .sub { color: var(--muted); margin-top: 3px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---------- controls ---------- */
.btn { display: inline-flex; align-items: center; gap: 7px; height: 36px; padding: 0 14px; border-radius: 9px; border: 1px solid var(--line); background: #fff; font-weight: 600; cursor: pointer; white-space: nowrap; box-shadow: var(--shadow); color: var(--ink); }
.btn:hover { border-color: #cfd4dc; background: #fafbfc; }
.btn svg { width: 16px; height: 16px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); }
.btn.danger { color: var(--bad); }
.btn.ghost { background: transparent; border-color: transparent; box-shadow: none; }
.btn.ghost:hover { background: var(--line-2); }
.btn.sm { height: 30px; padding: 0 10px; font-size: 13px; border-radius: 8px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { width: 30px; height: 30px; border-radius: 7px; border: 0; background: transparent; display: inline-grid; place-items: center; cursor: pointer; color: var(--ink-2); }
.icon-btn:hover { background: var(--line-2); }
.icon-btn svg { width: 16px; height: 16px; }

.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field label { font-size: 12.5px; font-weight: 600; color: var(--ink-2); }
.field .hint { font-size: 12px; color: var(--muted); }
.input, .field input, .field select, .field textarea { height: 36px; border: 1px solid #d5d9e0; border-radius: 8px; padding: 0 10px; background: #fff; outline: none; width: 100%; }
.field textarea { height: auto; min-height: 76px; padding: 8px 10px; resize: vertical; }
.input:focus, .field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.field input[readonly] { background: var(--bg); color: var(--ink-2); }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; cursor: pointer; padding-top: 22px; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }
.form-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 14px 14px; }
.form-grid .c1 { grid-column: span 1; } .form-grid .c2 { grid-column: span 2; } .form-grid .c3 { grid-column: span 3; } .form-grid .c4 { grid-column: span 4; } .form-grid .c6 { grid-column: span 6; }
.form-section { margin-bottom: 24px; }
.form-section h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); margin-bottom: 12px; padding-bottom: 8px; border-bottom: 1px solid var(--line-2); }

.seg { display: inline-flex; background: var(--line-2); border-radius: 9px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 6px 12px; border-radius: 7px; font-weight: 600; color: var(--ink-2); cursor: pointer; font-size: 13px; }
.seg button.on { background: #fff; color: var(--ink); box-shadow: var(--shadow); }

.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 18px; overflow-x: auto; }
.tabs button { border: 0; background: none; padding: 10px 14px; font-weight: 600; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs button.on { color: var(--accent); border-bottom-color: var(--accent); }
.tabs button .count { background: var(--line-2); color: var(--ink-2); border-radius: 20px; padding: 0 7px; font-size: 11.5px; margin-left: 6px; }

/* ---------- cards ---------- */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 18px; border-bottom: 1px solid var(--line-2); }
.card-head h2 { font-size: 15px; }
.card-body { padding: 16px 18px; }
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 18px; }
.kpi { padding: 16px 18px; }
.kpi .lbl { color: var(--muted); font-size: 12.5px; font-weight: 600; }
.kpi .val { font-size: 25px; font-weight: 700; letter-spacing: -.02em; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .foot { font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.kpi .bar { height: 6px; border-radius: 4px; background: var(--line-2); margin-top: 10px; overflow: hidden; }
.kpi .bar > i { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.kpi.bad .val { color: var(--bad); }
.grid-2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: 18px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 18px; }
.stack { display: flex; flex-direction: column; gap: 18px; }

.todo { list-style: none; margin: 0; padding: 0; }
.todo li { display: flex; align-items: center; gap: 12px; padding: 10px 18px; border-bottom: 1px solid var(--line-2); }
.todo li:last-child { border-bottom: 0; }
.todo .t { flex: 1; min-width: 0; }
.todo .t b { display: block; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.todo .t span { font-size: 12.5px; color: var(--muted); }
.empty { padding: 26px 18px; text-align: center; color: var(--muted); }

.badge { display: inline-flex; align-items: center; gap: 5px; height: 22px; padding: 0 8px; border-radius: 20px; font-size: 12px; font-weight: 600; background: var(--line-2); color: var(--ink-2); white-space: nowrap; }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .8; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.info { background: var(--info-soft); color: var(--info); }
.badge.plum { background: var(--accent-soft); color: var(--accent); }
.badge.plain::before { display: none; }

/* ---------- tables ---------- */
.tbl-wrap { overflow-x: auto; }
.tbl-tools { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--line-2); flex-wrap: wrap; }
.tbl-tools .input { width: 260px; height: 34px; }
.tbl-tools .spacer { flex: 1; }
.tbl-tools .selinfo { font-weight: 600; color: var(--accent); }
table.tbl { width: 100%; border-collapse: separate; border-spacing: 0; }
.tbl th { position: sticky; top: 0; background: #fafbfc; text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; padding: 9px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; cursor: default; user-select: none; }
.tbl th.sortable { cursor: pointer; }
.tbl th.sortable:hover { color: var(--ink); }
.tbl th .arr { opacity: .6; margin-left: 3px; }
.tbl td { padding: 10px 12px; border-bottom: 1px solid var(--line-2); vertical-align: middle; }
.tbl tbody tr { transition: background .1s; }
.tbl tbody tr.click { cursor: pointer; }
.tbl tbody tr:hover { background: #fafbfd; }
.tbl tbody tr.sel { background: var(--accent-soft); }
.tbl td.chk, .tbl th.chk { width: 36px; padding-right: 0; }
.tbl input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; vertical-align: middle; }
.tbl tfoot td { font-weight: 700; background: #fafbfc; border-top: 1px solid var(--line); border-bottom: 0; }
.tbl .strong { font-weight: 600; }
.tbl .sub { font-size: 12.5px; color: var(--muted); }
.tbl-more { padding: 12px; text-align: center; }
.tbl td.act { text-align: right; white-space: nowrap; width: 1%; }

/* ---------- overlays ---------- */
.overlay { position: fixed; inset: 0; background: rgba(15,18,28,.45); z-index: 50; display: flex; justify-content: flex-end; animation: fade .15s ease; }
.overlay.center { justify-content: center; align-items: flex-start; padding-top: 8vh; }
@keyframes fade { from { opacity: 0; } }
@keyframes slide { from { transform: translateX(30px); opacity: 0; } }
@keyframes pop { from { transform: translateY(8px) scale(.98); opacity: 0; } }
.drawer { width: min(860px, 100vw); height: 100%; background: #fff; box-shadow: var(--shadow-lg); display: flex; flex-direction: column; animation: slide .2s ease; }
.drawer-head, .modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 18px 24px; border-bottom: 1px solid var(--line); }
.drawer-head h2, .modal-head h2 { font-size: 18px; }
.drawer-body { flex: 1; overflow: auto; padding: 22px 24px; }
.drawer-foot, .modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 24px; border-top: 1px solid var(--line); background: #fafbfc; }
.drawer-foot .left { margin-right: auto; }
.modal { width: min(560px, 94vw); background: #fff; border-radius: 14px; box-shadow: var(--shadow-lg); animation: pop .18s ease; max-height: 84vh; display: flex; flex-direction: column; }
.modal.wide { width: min(820px, 94vw); }
.modal-body { padding: 20px 24px; overflow: auto; }
.modal-head { padding: 16px 24px; }

#toasts { position: fixed; right: 20px; bottom: 20px; z-index: 80; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--ink); color: #fff; padding: 11px 16px; border-radius: 10px; box-shadow: var(--shadow-lg); max-width: 420px; animation: pop .18s ease; font-weight: 500; }
.toast.err { background: var(--bad); }
.toast.ok { background: #14532d; }

/* ---------- details ---------- */
.dl { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px 18px; }
.dl div > span { display: block; font-size: 12px; color: var(--muted); font-weight: 600; margin-bottom: 2px; }
.dl div > b { font-weight: 600; }
.hero { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; flex-wrap: wrap; margin-bottom: 18px; }
.hero h1 { font-size: 24px; }
.crumbs { color: var(--muted); font-size: 13px; margin-bottom: 8px; }
.crumbs a { color: var(--muted); } .crumbs a:hover { color: var(--accent); }
.note { background: var(--info-soft); color: #1d3f7a; border-radius: 9px; padding: 11px 14px; font-size: 13px; }
.note.warn { background: var(--warn-soft); color: #6b3d00; }

/* chart */
.chart { width: 100%; height: 220px; }
.chart .ax { fill: var(--muted); font-size: 11px; }
.chart .gl { stroke: var(--line-2); }
.legend { display: flex; gap: 14px; font-size: 12.5px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

/* istat grid */
.istat-grid { display: grid; grid-template-columns: 90px repeat(12, 1fr); gap: 6px; align-items: center; }
.istat-grid .h { font-size: 11.5px; text-transform: uppercase; color: var(--muted); font-weight: 600; text-align: center; }
.istat-grid input { height: 32px; text-align: center; padding: 0 4px; border: 1px solid #d5d9e0; border-radius: 7px; width: 100%; outline: none; }
.istat-grid input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.istat-grid .y { font-weight: 700; }

/* login */
.login { min-height: 100vh; display: grid; grid-template-columns: 1fr 1fr; }
.login-art { background: radial-gradient(1200px 600px at -10% 110%, #8a3d77 0%, transparent 55%), radial-gradient(900px 500px at 120% -10%, #3b1631 0%, transparent 60%), #17131c; color: #fff; padding: 56px; display: flex; flex-direction: column; justify-content: space-between; }
.login-art h1 { font-size: 40px; line-height: 1.1; max-width: 460px; letter-spacing: -.03em; }
.login-art p { color: #cfc6d6; max-width: 420px; font-size: 15px; }
.login-form { display: grid; place-items: center; padding: 40px; }
.login-form form { width: min(380px, 100%); display: flex; flex-direction: column; gap: 16px; }
.login-form h2 { font-size: 24px; }
.login-form .btn { height: 42px; justify-content: center; }
.err-msg { color: var(--bad); font-weight: 500; min-height: 20px; }

@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .dl { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: static; height: auto; flex-direction: row; flex-wrap: wrap; padding: 10px; }
  .nav { flex-direction: row; flex-wrap: wrap; } .nav-label, .side-foot { display: none; }
  .content { padding: 18px 14px 40px; }
  .topbar { padding: 0 14px; }
  .form-grid { grid-template-columns: repeat(2, 1fr); }
  .form-grid > * { grid-column: span 2 !important; }
  .login { grid-template-columns: 1fr; } .login-art { display: none; }
  .kpis { grid-template-columns: 1fr; }
}
@media print {
  .side, .topbar, .tbl-tools, .actions, .tabs { display: none !important; }
  .shell { display: block; } .content { padding: 0; } .card { box-shadow: none; border: 0; }
  body { background: #fff; }
}

/* ---------- ricerca con suggerimenti ---------- */
.search { flex: 0 1 560px; }
.search input { height: 40px; border: 1.5px solid #d5d9e0; background: #fff; padding-right: 92px; font-size: 14.5px; }
.search svg { top: 12px; }
.search-btn { position: absolute; right: 4px; top: 4px; height: 32px; padding: 0 16px; border: 0; border-radius: 7px; background: var(--accent); color: #fff; font-weight: 700; letter-spacing: .06em; font-size: 12.5px; cursor: pointer; }
.search-btn:hover { background: var(--accent-2); }
.search-results { top: 46px; }
.search-results .k { padding: 10px 10px 4px; font-weight: 700; }
mark { background: #f6e3b4; color: inherit; border-radius: 2px; padding: 0 1px; }
.ac { position: relative; }
.ac-list { position: absolute; left: 0; right: 0; top: 40px; z-index: 30; background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow-lg); max-height: 300px; overflow: auto; padding: 5px; }
.ac-item { padding: 8px 10px; border-radius: 7px; cursor: pointer; }
.ac-item b { display: block; font-weight: 600; }
.ac-item span { display: block; font-size: 12.5px; color: var(--muted); }
.ac-item.sel, .ac-item:hover { background: var(--accent-soft); }
.ac-new { color: var(--accent); font-weight: 600; border-top: 1px solid var(--line-2); margin-top: 4px; }
.ac-empty { padding: 10px; color: var(--muted); font-size: 13px; }

/* ---------- canone a scaletta ---------- */
.scaletta { grid-column: span 6; background: var(--accent-soft); border: 1px solid #e6d3e0; border-radius: 10px; padding: 14px 16px; display: flex; flex-direction: column; gap: 12px; }
.scaletta-head { display: flex; flex-direction: column; gap: 2px; }
.scaletta-rows { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
.scaletta-rows input { background: #fff; }
