:root {
  --bg: #f4f5f2;
  --panel: #ffffff;
  --panel-2: #f9faf8;
  --ink: #1d2523;
  --ink-2: #55615e;
  --ink-3: #8a9592;
  --line: #e2e5e0;
  --line-2: #cfd4cd;
  --brand: #255d6b;
  --brand-ink: #ffffff;
  --brand-soft: #e3eef0;
  --accent: #c8662b;
  --ok: #2f7d4f;
  --ok-soft: #e4f2e9;
  --warn: #a8660b;
  --warn-soft: #fbf0dc;
  --bad: #b3372f;
  --bad-soft: #f9e5e3;
  --side: #1e2b2e;
  --side-ink: #c9d3d1;
  --side-active: #2f4448;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(20, 30, 28, .06), 0 4px 16px rgba(20, 30, 28, .06);
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font: 14px/1.45 var(--font); -webkit-font-smoothing: antialiased; }
a { color: var(--brand); }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 22px; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.muted { color: var(--ink-2); }
.small { font-size: 12px; }
.mono { font-family: var(--mono); font-size: 12.5px; }
.hide { display: none !important; }

/* ---------- Layout ---------- */
.shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.side { background: var(--side); color: var(--side-ink); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.brand { padding: 18px 18px 14px; color: #fff; font-weight: 650; font-size: 15px; line-height: 1.25; border-bottom: 1px solid rgba(255,255,255,.07); }
.brand small { display: block; font-weight: 400; color: var(--side-ink); font-size: 12px; margin-top: 3px; opacity: .8; }
.nav { padding: 10px 8px; display: flex; flex-direction: column; gap: 2px; overflow-y: auto; flex: 1; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 6px; color: var(--side-ink); text-decoration: none; font-weight: 500; }
.nav a:hover { background: rgba(255,255,255,.05); color: #fff; }
.nav a.active { background: var(--side-active); color: #fff; }
.nav svg { width: 18px; height: 18px; flex: none; opacity: .9; }
.nav .sep { height: 1px; background: rgba(255,255,255,.07); margin: 8px 6px; }
.side-foot { padding: 12px 16px; border-top: 1px solid rgba(255,255,255,.07); font-size: 12px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.side-foot button { background: none; border: 1px solid rgba(255,255,255,.18); color: var(--side-ink); border-radius: 6px; padding: 4px 8px; cursor: pointer; font: inherit; }
.side-foot button:hover { color: #fff; border-color: rgba(255,255,255,.35); }
.main { min-width: 0; padding: 22px 28px 60px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.topbar { display: none; }

/* ---------- Pannelli ---------- */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-h { padding: 12px 16px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.card-b { padding: 16px; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.kpi { padding: 14px 16px; }
.kpi .l { color: var(--ink-2); font-size: 12.5px; }
.kpi .v { font-size: 24px; font-weight: 650; margin-top: 4px; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.kpi .s { font-size: 12px; color: var(--ink-3); margin-top: 2px; }
.kpi.link { cursor: pointer; }
.kpi.link:hover { border-color: var(--line-2); }

/* ---------- Form ---------- */
label.f { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--ink-2); font-weight: 500; min-width: 0; }
input, select, textarea { font: inherit; color: var(--ink); background: #fff; border: 1px solid var(--line-2); border-radius: 6px; padding: 7px 9px; min-width: 0; width: 100%; }
textarea { resize: vertical; min-height: 64px; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--brand) 35%, transparent); outline-offset: 0; border-color: var(--brand); }
input[type=checkbox] { width: auto; }
input.num { text-align: right; }
.form { display: grid; gap: 12px; grid-template-columns: repeat(6, minmax(0, 1fr)); }
.form .c1 { grid-column: span 1; } .form .c2 { grid-column: span 2; } .form .c3 { grid-column: span 3; } .form .c4 { grid-column: span 4; } .form .c6 { grid-column: span 6; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13.5px; color: var(--ink); }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.filters input[type=search] { width: 280px; }
.filters select, .filters input[type=date] { width: auto; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: inherit; font-weight: 550; border-radius: 6px; padding: 7px 13px; border: 1px solid var(--line-2); background: #fff; color: var(--ink); cursor: pointer; white-space: nowrap; text-decoration: none; }
.btn:hover { background: var(--panel-2); border-color: var(--ink-3); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.danger { color: var(--bad); }
.btn.danger:hover { background: var(--bad-soft); border-color: var(--bad); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--panel-2); }
.btn.sm { padding: 4px 9px; font-size: 12.5px; }
.btn.big { padding: 12px 18px; font-size: 16px; }
.btn:disabled { opacity: .5; cursor: default; }
.btn svg { width: 16px; height: 16px; }

/* ---------- Tabelle ---------- */
.table-wrap { overflow-x: auto; }
table.t { width: 100%; border-collapse: collapse; }
.t th { text-align: left; font-size: 12px; font-weight: 600; color: var(--ink-2); padding: 9px 12px; border-bottom: 1px solid var(--line); background: var(--panel-2); white-space: nowrap; position: sticky; top: 0; }
.t td { padding: 8px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.t tr:last-child td { border-bottom: 0; }
.t tbody tr.click { cursor: pointer; }
.t tbody tr.click:hover td { background: var(--panel-2); }
.t th.sortable { cursor: pointer; }
.t th.sortable:hover { color: var(--ink); }
.t td.desc { max-width: 460px; }
.t tfoot td { font-weight: 650; background: var(--panel-2); border-top: 1px solid var(--line); }
.pager { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; border-top: 1px solid var(--line); gap: 10px; flex-wrap: wrap; }
.empty { padding: 36px 16px; text-align: center; color: var(--ink-3); }

.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 11.5px; font-weight: 600; background: var(--panel-2); color: var(--ink-2); border: 1px solid var(--line); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.bad { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.badge.brand { background: var(--brand-soft); color: var(--brand); border-color: transparent; }
.giac-bad { color: var(--bad); font-weight: 650; }
.giac-zero { color: var(--ink-3); }

/* ---------- Modale ---------- */
.overlay { position: fixed; inset: 0; background: rgba(20, 28, 27, .45); display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; z-index: 50; overflow-y: auto; }
.modal { background: var(--panel); border-radius: 10px; width: 100%; max-width: 760px; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal.sm { max-width: 440px; }
.modal.lg { max-width: 980px; }
.modal-h { padding: 16px 20px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.modal-b { padding: 18px 20px; }
.modal-f { padding: 12px 20px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; background: var(--panel-2); border-radius: 0 0 10px 10px; }
.modal-f .left { margin-right: auto; display: flex; gap: 8px; }
.x { background: none; border: 0; font-size: 22px; line-height: 1; cursor: pointer; color: var(--ink-3); padding: 2px 6px; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin: -4px 0 14px; }
.tabs button { background: none; border: 0; border-bottom: 2px solid transparent; padding: 8px 12px; font: inherit; font-weight: 550; color: var(--ink-2); cursor: pointer; }
.tabs button.on { color: var(--brand); border-bottom-color: var(--brand); }

/* ---------- Toast ---------- */
#toast-root { position: fixed; bottom: 18px; right: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 80; }
.toast { background: var(--ink); color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 380px; animation: tin .18s ease-out; }
.toast.err { background: var(--bad); }
@keyframes tin { from { transform: translateY(8px); opacity: 0; } }

/* ---------- Autocompletamento ---------- */
.ac { position: relative; }
.ac-list { position: absolute; left: 0; right: 0; top: calc(100% + 2px); background: #fff; border: 1px solid var(--line-2); border-radius: 6px; box-shadow: var(--shadow); max-height: 320px; overflow-y: auto; z-index: 40; min-width: 360px; }
.ac-item { padding: 7px 10px; cursor: pointer; display: flex; justify-content: space-between; gap: 12px; border-bottom: 1px solid var(--line); }
.ac-item:last-child { border-bottom: 0; }
.ac-item.on, .ac-item:hover { background: var(--brand-soft); }
.ac-item .r { color: var(--ink-2); font-size: 12px; text-align: right; white-space: nowrap; }

/* ---------- Editor documento ---------- */
.doc-head { display: grid; gap: 12px; grid-template-columns: 1.1fr .5fr .7fr 2fr 1fr; align-items: end; }
.righe td { padding: 4px 6px; }
.righe input, .righe select { padding: 5px 7px; }
.righe .col-desc { min-width: 280px; }
.righe .col-n { width: 90px; }
.righe .col-um { width: 64px; }
.righe .col-iva { width: 82px; }
.righe .col-x { width: 34px; }
.tot-box { display: grid; grid-template-columns: 1fr auto; gap: 4px 24px; min-width: 260px; }
.tot-box .big { font-size: 20px; font-weight: 700; }
.doc-foot { display: flex; justify-content: space-between; gap: 20px; align-items: flex-start; flex-wrap: wrap; }
.banner { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; font-size: 13px; }
.banner.ok { background: var(--ok-soft); color: var(--ok); }
.banner.warn { background: var(--warn-soft); color: var(--warn); }

/* ---------- Banco ---------- */
.banco { display: grid; grid-template-columns: minmax(0, 1fr) 420px; gap: 16px; align-items: start; }
.banco-search input { font-size: 18px; padding: 12px 14px; }
.banco-res { max-height: calc(100vh - 240px); overflow-y: auto; }
.res-item { display: flex; justify-content: space-between; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.res-item:hover { background: var(--brand-soft); }
.res-item .p { font-weight: 650; font-size: 15px; white-space: nowrap; }
.scontrino { position: sticky; top: 16px; }
.sc-righe { max-height: calc(100vh - 420px); min-height: 120px; overflow-y: auto; }
.sc-riga { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.sc-riga .d { font-weight: 550; }
.sc-riga .ctrl { display: flex; align-items: center; gap: 6px; }
.sc-riga .ctrl input { width: 64px; padding: 4px 6px; text-align: right; }
.sc-riga .ctrl input.pr { width: 84px; }
.qbtn { width: 28px; height: 28px; border-radius: 6px; border: 1px solid var(--line-2); background: #fff; cursor: pointer; font-size: 16px; line-height: 1; }
.sc-tot { padding: 14px; display: flex; justify-content: space-between; align-items: baseline; border-top: 2px solid var(--ink); }
.sc-tot .v { font-size: 30px; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---------- Grafico ---------- */
.chart { width: 100%; height: 220px; display: block; }
.chart text { font: 11px var(--font); fill: var(--ink-3); }
.chart .bar { fill: var(--brand); }
.chart .bar:hover { fill: var(--accent); }
.chart .grid-l { stroke: var(--line); }

/* ---------- Login ---------- */
.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; background: linear-gradient(160deg, #1e2b2e 0%, #255d6b 100%); }
.login .card { width: 100%; max-width: 380px; padding: 28px; }
.login h1 { margin-bottom: 4px; }
.login form { display: grid; gap: 14px; margin-top: 18px; }
.login .err { color: var(--bad); font-size: 13px; min-height: 18px; }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .banco { grid-template-columns: 1fr; }
  .scontrino { position: static; }
  .doc-head { grid-template-columns: 1fr 1fr 1fr; }
  .doc-head .span-all { grid-column: 1 / -1; }
}
@media (max-width: 800px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; inset: 0 auto 0 0; width: 240px; z-index: 60; transform: translateX(-100%); transition: transform .2s; }
  .side.open { transform: none; }
  .topbar { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--side); color: #fff; position: sticky; top: 0; z-index: 30; }
  .topbar button { background: none; border: 0; color: #fff; font-size: 22px; cursor: pointer; padding: 0 4px; }
  .main { padding: 16px 16px 50px; }
  .g2, .g3 { grid-template-columns: 1fr; }
  .form { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form .c3, .form .c4, .form .c6 { grid-column: span 2; }
  .form .c1 { grid-column: span 1; }
  .filters input[type=search] { width: 100%; }
  .doc-head { grid-template-columns: 1fr 1fr; }
}
