:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --text: #1f2933;
  --muted: #6b7785;
  --border: #dde3ea;
  --primary: #0b6bcb;
  --primary-dark: #08529c;
  --ok: #1f9d55;
  --warn: #d98e04;
  --err: #d64545;
  --sidebar: #12263a;
  --sidebar-text: #c9d6e3;
  --radius: 8px;
}
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.45 system-ui, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--primary); }
h1 { font-size: 22px; margin: 0 0 16px; }
h2 { font-size: 17px; margin: 0 0 12px; }
.muted { color: var(--muted); }
.hidden { display: none !important; }

/* Layout */
.topbar { position: fixed; top: 0; left: 0; right: 0; height: 56px; background: var(--panel); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px; padding: 0 16px; z-index: 20; }
.brand { font-weight: 700; font-size: 19px; color: var(--sidebar); }
.brand span { color: var(--primary); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.burger { display: none; background: none; border: 0; font-size: 22px; cursor: pointer; }
.sidebar { position: fixed; top: 56px; bottom: 0; left: 0; width: 230px; background: var(--sidebar); padding: 12px 0; overflow-y: auto; z-index: 15; }
.sidebar a { display: block; padding: 9px 20px; color: var(--sidebar-text); text-decoration: none; }
.sidebar a:hover { background: rgba(255,255,255,.06); }
.sidebar a.active { background: var(--primary); color: #fff; }
.nav-title { padding: 14px 20px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: #7d93a8; }
main { margin: 56px 0 0 230px; padding: 24px; max-width: 1400px; }

@media (max-width: 860px) {
  .burger { display: block; }
  .sidebar { transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: none; }
  main { margin-left: 0; padding: 16px; }
  #azienda-nome { display: none; }
}

/* Components */
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; margin-bottom: 18px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-bottom: 18px; }
.kpi { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.kpi .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; }
.kpi .value { font-size: 24px; font-weight: 700; margin-top: 4px; }
.grid2 { display: grid; grid-template-columns: 2fr 1fr; gap: 18px; }
@media (max-width: 1000px) { .grid2 { grid-template-columns: 1fr; } }

.btn { display: inline-block; padding: 7px 14px; border-radius: 6px; border: 1px solid var(--border); background: var(--panel);
  color: var(--text); cursor: pointer; text-decoration: none; font: inherit; white-space: nowrap; }
.btn:hover { border-color: var(--primary); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.danger { color: var(--err); }
.btn.sm { padding: 3px 8px; font-size: 12px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.toolbar .spacer { flex: 1; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; color: var(--muted); font-weight: 600; background: #fafbfc; }
td.num, th.num { text-align: right; white-space: nowrap; }
tr.clickable { cursor: pointer; }
tr.clickable:hover { background: #f0f6fd; }
.table-wrap { overflow-x: auto; }
.empty { text-align: center; padding: 30px; color: var(--muted); }

input, select, textarea { font: inherit; padding: 7px 9px; border: 1px solid var(--border); border-radius: 6px; background: #fff; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid #b8d6f5; border-color: var(--primary); }
label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.form-grid .wide { grid-column: 1 / -1; }
.toolbar input, .toolbar select { width: auto; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 12px; font-weight: 600; }
.badge.bozza { background: #eceff3; color: #52606d; }
.badge.da_inviare { background: #fff4d6; color: #8a5a00; }
.badge.inviata { background: #dceeff; color: #0b4f94; }
.badge.accettata { background: #dcf5e6; color: #186b3d; }
.badge.rifiutata { background: #fde2e2; color: #a12b2b; }
.badge.pagata { background: #1f9d55; color: #fff; }

.rows { table-layout: fixed; min-width: 900px; } /* la descrizione resta leggibile; su schermi stretti si scorre */
.rows input, .rows select { padding: 5px 7px; width: 100%; }
.rows input.num { text-align: right; }
.rows input:invalid { border-color: var(--err); background: #fdecec; }
.rows td, .rows th { padding: 4px 6px; }
.totals { margin-left: auto; max-width: 460px; }
.totals td { border: 0; padding: 4px 10px; }
.totals .grand td { font-size: 18px; font-weight: 700; border-top: 2px solid var(--text); }

.chart { display: flex; align-items: flex-end; gap: 6px; height: 180px; padding-top: 10px; }
.chart .bar { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; }
.chart .bar div { width: 100%; background: var(--primary); border-radius: 4px 4px 0 0; min-height: 2px; }
.chart .bar span { font-size: 11px; color: var(--muted); margin-top: 4px; }

.toast { position: fixed; bottom: 20px; right: 20px; background: var(--sidebar); color: #fff; padding: 10px 16px; border-radius: 6px;
  opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 50; }
.toast.show { opacity: 1; }
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: flex; align-items: center; justify-content: center; z-index: 40; padding: 16px; }
.modal-box { background: #fff; border-radius: var(--radius); padding: 20px; width: 100%; max-width: 640px; max-height: 90vh; overflow-y: auto; }
.notice { background: #fff8e1; border: 1px solid #f3d27a; padding: 10px 14px; border-radius: 6px; margin-bottom: 14px; }

/* Intro / caricamento */
.splash { position: fixed; inset: 0; z-index: 100; background: #fff; display: flex; flex-direction: column;
  align-items: center; justify-content: center; padding: 16px; transition: opacity .5s; }
.splash.fade { opacity: 0; pointer-events: none; }
.splash-logo { width: min(420px, 80vw); animation: splash-in .8s ease-out both; }
.splash-bar { width: min(380px, 80vw); height: 8px; background: #e6edf5; border-radius: 4px; overflow: hidden; margin-top: 28px; }
.splash-bar div { height: 100%; width: 0; background: linear-gradient(90deg, #0a4fd6, #0b8af0, #12d0f5); border-radius: 4px; transition: width .25s; }
.splash-info { width: min(380px, 80vw); display: flex; justify-content: space-between; margin-top: 8px; font-size: 13px; color: var(--muted); }
.splash-ver { position: absolute; bottom: 20px; font-size: 12px; color: #9aa7b4; }
@keyframes splash-in { from { opacity: 0; transform: scale(.92); } to { opacity: 1; transform: none; } }
@media print { .splash { display: none !important; } }

.allegato { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 10px; border: 1px dashed var(--border); border-radius: 6px; background: #fafbfc; }
#esci { color: var(--err); }
.login-box { width: min(340px, 90vw); display: flex; flex-direction: column; gap: 12px; margin-top: 24px; }
.login-box .btn { padding: 10px; }
.login-err { color: var(--err); font-size: 13px; min-height: 1em; }
.pwd-wrap { position: relative; }
.pwd-wrap input { padding-right: 44px; }
.pwd-occhio, .login-box .pwd-occhio { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); background: none; border: 0;
  padding: 4px 8px; font-size: 18px; cursor: pointer; opacity: .55; line-height: 1; }
.pwd-occhio:hover, .pwd-occhio.attivo { opacity: 1; }
.login-caps { color: var(--warn); font-size: 12px; margin-top: 4px; }

/* Logo */
.topbar-logo { height: 34px; max-width: 120px; object-fit: contain; }
.logo-edit { display: flex; flex-wrap: wrap; gap: 18px; align-items: center; }
.logo-preview { width: 240px; height: 100px; border: 1px dashed var(--border); border-radius: 6px; display: flex;
  align-items: center; justify-content: center; background: #fafbfc; }
.logo-preview img { max-width: 100%; max-height: 100%; object-fit: contain; }
.doc-logo { max-height: 80px; max-width: 260px; margin-bottom: 10px; }

/* Documento stampabile */
.doc { background: #fff; max-width: 820px; margin: 0 auto; padding: 40px; border: 1px solid var(--border); }
.doc-head { display: flex; justify-content: space-between; gap: 20px; margin-bottom: 30px; }
.doc-head h2 { font-size: 24px; color: var(--primary); }
.doc-parties { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 24px; }
.doc-parties .box { border: 1px solid var(--border); padding: 12px; border-radius: 6px; }

.doc-legale { margin-top: 32px; font-size: 12px; line-height: 1.45; font-weight: 600; color: var(--text); }
.doc-piede { margin-top: 40px; padding-top: 10px; border-top: 1px solid var(--border); font-size: 10.5px; line-height: 1.4; color: var(--muted); }

/* Anteprima bozza a tutto schermo */
#anteprima { position: fixed; inset: 0; z-index: 60; background: #e9edf2; overflow-y: auto; padding: 0 16px 40px; }
.anteprima-bar { position: sticky; top: 0; z-index: 1; display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  max-width: 820px; margin: 0 auto 16px; padding: 12px 0; background: #e9edf2; }
.anteprima-bar .spacer { flex: 1; }
body.con-anteprima { overflow: hidden; }
.doc-bozza { position: relative; overflow: hidden; }
.doc-bozza::before { content: 'BOZZA'; position: absolute; top: 45%; left: 50%; transform: translate(-50%, -50%) rotate(-30deg);
  font-size: 140px; font-weight: 800; letter-spacing: 12px; color: rgba(214, 69, 69, .10); pointer-events: none; white-space: nowrap; }

@media print {
  .no-print, .topbar, .sidebar, .toolbar { display: none !important; }
  main { margin: 0; padding: 0; }
  body { background: #fff; }
  .doc { border: 0; padding: 0; }
  /* Con l'anteprima aperta si stampa solo la bozza */
  body.con-anteprima > *:not(#anteprima) { display: none !important; }
  body.con-anteprima { overflow: visible; }
  #anteprima { position: static; background: #fff; padding: 0; overflow: visible; }
  .doc-bozza::before { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
