:root {
  --ink: #0B1F3A;
  --ink-soft: #14305A;
  --paper: #EAF2FC;
  --paper-raised: #FFFFFF;
  --line: #D3E0F1;
  --muted: #576C8B;
  --accent: #1F6FEB;      /* blu — azione primaria */
  --accent-2: #19B4E6;    /* azzurro */
  --accent-ink: #0B2E66;
  --teal: #1B8F7A;        /* stato valido */
  --amber: #D9A017;       /* stato in scadenza / in attesa (avviso) */
  --red: #B23A2E;         /* stato scaduto / da sollecitare */
  --grey-dot: #8FA1BC;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(22,35,31,0.06), 0 6px 20px rgba(22,35,31,0.06);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--paper); color: var(--ink);
  font-family: 'IBM Plex Sans', -apple-system, sans-serif;
  font-size: 14.5px;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 {
  font-family: 'Space Grotesk', 'IBM Plex Sans', sans-serif;
  font-weight: 600; letter-spacing: -0.01em; margin: 0; color: var(--ink);
}

.mono { font-family: 'IBM Plex Mono', monospace; }
button, input, select, textarea { font-family: inherit; font-size: inherit; }
a { color: inherit; }

#app { display: flex; min-height: 100vh; }

/* ---------- Sidebar ---------- */

.sidebar {
  width: 232px; flex-shrink: 0; background: var(--ink); color: #E4EEFB;
  display: flex; flex-direction: column; padding: 22px 14px;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 26px 10px; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), #0B4FB8);
  flex-shrink: 0; display: flex; align-items: center; justify-content: center;
  font-family: 'Space Grotesk', sans-serif; font-weight: 700; color: #fff; font-size: 13px;
}
.brand-text { font-family: 'Space Grotesk', sans-serif; font-weight: 600; font-size: 15px; line-height: 1.15; }
.brand-text span { display: block; font-size: 11px; font-weight: 400; color: #8CA8CD; }

nav.nav { display: flex; flex-direction: column; gap: 2px; margin-top: 6px; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 8px;
  color: #B9CEEB; text-decoration: none; cursor: pointer; font-size: 13.5px; font-weight: 500;
  transition: background .12s ease, color .12s ease; border: none; background: transparent;
  text-align: left; width: 100%;
}
.nav-item:hover { background: rgba(255,255,255,0.06); color: #fff; }
.nav-item.active { background: var(--accent); color: #fff; }
.nav-item .ico { width: 16px; text-align: center; opacity: .9; font-size: 14px; }
.sidebar-foot { margin-top: auto; padding: 10px; font-size: 11px; color: #6B7F9E; line-height: 1.5; }

/* ---------- Main ---------- */

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 22px 32px; border-bottom: 1px solid var(--line); background: var(--paper);
  position: sticky; top: 0; z-index: 5;
}
.topbar h1 { font-size: 21px; }
.topbar .subtitle { color: var(--muted); font-size: 12.5px; margin-top: 3px; }
#view-root { padding: 26px 32px 60px 32px; flex: 1; min-width: 0; }

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex; align-items: center; gap: 7px; padding: 9px 16px; border-radius: 8px;
  border: 1px solid transparent; background: var(--accent); color: #fff; font-weight: 600;
  font-size: 13.5px; cursor: pointer; transition: filter .12s ease, transform .05s ease;
}
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(1px); }
.btn.secondary { background: var(--paper-raised); color: var(--ink); border-color: var(--line); }
.btn.secondary:hover { background: #F1F6FC; }
.btn.ghost { background: transparent; color: var(--muted); border: none; padding: 6px 8px; }
.btn.ghost:hover { color: var(--ink); }
.btn.danger { background: var(--red); }
.btn.small { padding: 6px 11px; font-size: 12.5px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- Cards / tables ---------- */

.card { background: var(--paper-raised); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow-x: auto; }
.card-pad { padding: 20px 22px; }

.grid-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin-bottom: 24px; }
.stat {
  background: var(--paper-raised); border: 1px solid var(--line); border-left: 4px solid var(--accent);
  border-radius: var(--radius); padding: 16px 18px;
}
.stat.teal { border-left-color: var(--teal); }
.stat.red { border-left-color: var(--red); }
.stat .num { font-family: 'Space Grotesk', sans-serif; font-size: 28px; font-weight: 700; line-height: 1; }
.stat .lbl { color: var(--muted); font-size: 12px; margin-top: 6px; }

table { width: 100%; border-collapse: collapse; }
thead th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); font-weight: 600; padding: 10px 14px; border-bottom: 1px solid var(--line);
}
tbody td { padding: 12px 14px; border-bottom: 1px solid #E8EFF9; font-size: 13.5px; vertical-align: middle; }
tbody tr:hover { background: #F6F9FE; }
tbody tr:last-child td { border-bottom: none; }

.empty-state { text-align: center; padding: 60px 20px; color: var(--muted); }
.empty-state .ico { font-size: 30px; margin-bottom: 10px; }
.empty-state h3 { color: var(--ink); margin-bottom: 6px; font-size: 16px; }
.empty-state p { margin: 0 0 16px 0; font-size: 13px; }

/* ---------- EER chip + semaforo ---------- */

.eer-chip {
  display: inline-flex; align-items: center; gap: 7px; background: #E3F0FD; border: 1px solid #BFDBF7;
  padding: 3px 9px 3px 7px; border-radius: 999px; font-family: 'IBM Plex Mono', monospace;
  font-size: 12px; font-weight: 600; color: var(--accent-ink);
}
.dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; display: inline-block; }
.dot.valida { background: var(--teal); }
.dot.in_scadenza { background: var(--amber); }
.dot.scaduta { background: var(--red); }
.dot.senza_scadenza { background: var(--grey-dot); }

.badge { display: inline-flex; align-items: center; gap: 6px; font-size: 11.5px; font-weight: 600; padding: 4px 9px; border-radius: 999px; }
.badge.valida { background: #DDF3EE; color: var(--teal); }
.badge.in_scadenza { background: #FFF1CC; color: #8A6200; }
.badge.scaduta { background: #F5E2DF; color: var(--red); }
.badge.senza_scadenza { background: #E6EEF9; color: var(--muted); }
.badge.critica { background: #F5E2DF; color: var(--red); }

.badge-stato.bozza { background: #E6EEF9; color: var(--muted); }
.badge-stato.inviata { background: #DCEBFF; color: #1B4FA3; }
.badge-stato.accettata { background: #DDF3EE; color: var(--teal); }
.badge-stato.rifiutata { background: #F5E2DF; color: var(--red); }

/* ---------- Rating cliente / marginalità ---------- */

.rating-badge { display: inline-flex; align-items: center; gap: 7px; font-size: 12px; font-weight: 700; padding: 5px 11px; border-radius: 999px; white-space: nowrap; }
.rating-badge.alta { background: #DDF3EE; color: var(--teal); }
.rating-badge.media { background: #E4F4FB; color: #0F7390; }
.rating-badge.bassa { background: #FFF1CC; color: #8A6200; }
.rating-badge.critica { background: #F5E2DF; color: var(--red); }
.rating-badge.senza_dati { background: #E6EEF9; color: var(--muted); }

.rating-dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.rating-dot.alta { background: var(--teal); }
.rating-dot.media { background: #19A8D8; }
.rating-dot.bassa { background: var(--amber); }
.rating-dot.critica { background: var(--red); box-shadow: 0 0 0 3px rgba(178,58,46,0.15); }
.rating-dot.senza_dati { background: var(--grey-dot); }

.trend-arrow { font-size: 12px; font-weight: 700; margin-left: 4px; }
.trend-arrow.in_calo { color: var(--red); }
.trend-arrow.in_crescita { color: var(--teal); }
.trend-arrow.stabile { color: var(--muted); }
.margin-perc { font-family: 'IBM Plex Mono', monospace; font-weight: 600; }

.margin-history { display: flex; flex-direction: column; gap: 8px; }
.margin-history-row { display: flex; align-items: center; gap: 12px; }
.margin-history-row .mh-label { width: 150px; flex-shrink: 0; font-size: 12.5px; }
.margin-history-row .mh-label .num { font-family: 'IBM Plex Mono', monospace; font-size: 12px; display: block; }
.margin-history-row .mh-label .date { color: var(--muted); font-size: 11px; }
.margin-history-row .mh-bar-wrap { flex: 1; background: #E8EFF9; border-radius: 6px; height: 20px; position: relative; overflow: hidden; }
.margin-history-row .mh-bar { height: 100%; border-radius: 6px; }
.margin-history-row .mh-val { width: 64px; text-align: right; font-size: 12.5px; flex-shrink: 0; }

.rischio-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 14px; border-bottom: 1px solid #EAF1FA; }
.rischio-row:last-child { border-bottom: none; }
.rischio-row:hover { background: #F6F9FE; }
.rischio-row .cname { font-weight: 600; font-size: 13.5px; }
.rischio-row .csub { font-size: 11.5px; color: var(--muted); }

/* ---------- Forms ---------- */

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12.5px; font-weight: 600; color: var(--ink-soft); margin-bottom: 5px; }
.field .hint { font-size: 11.5px; color: var(--muted); margin-top: 4px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 7px;
  background: var(--paper-raised); color: var(--ink);
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
.field textarea { min-height: 64px; resize: vertical; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 14px; }

/* ---------- Modal ---------- */

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(22,35,31,0.45); display: flex;
  align-items: flex-start; justify-content: center; padding: 40px 20px; overflow-y: auto; z-index: 100;
}
.modal { background: var(--paper-raised); border-radius: 12px; width: 100%; max-width: 560px; box-shadow: 0 20px 60px rgba(0,0,0,0.25); }
.modal.wide { max-width: 820px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 22px; border-bottom: 1px solid var(--line); }
.modal-body { padding: 22px; max-height: 70vh; overflow-y: auto; }
.modal-foot { padding: 16px 22px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 10px; }

/* ---------- Pannello quotazioni EER ---------- */

.eer-lookup { border: 1px solid var(--line); border-radius: 9px; overflow: hidden; margin-top: 8px; background: #F6F9FE; }
.eer-lookup-head { padding: 9px 12px; font-size: 11.5px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; border-bottom: 1px solid var(--line); background: #EEF4FB; }
.eer-lookup-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; border-bottom: 1px solid #EAF1FA; cursor: pointer; }
.eer-lookup-row:last-child { border-bottom: none; }
.eer-lookup-row:hover { background: #E9F1FB; }
.eer-lookup-row .left { display: flex; flex-direction: column; gap: 2px; }
.eer-lookup-row .impianto { font-weight: 600; font-size: 13px; }
.eer-lookup-row .meta { font-size: 11.5px; color: var(--muted); }
.eer-lookup-row .price { font-family: 'IBM Plex Mono', monospace; font-weight: 600; font-size: 14px; }
.eer-lookup-empty { padding: 14px 12px; font-size: 12.5px; color: var(--muted); }

/* ---------- Righe offerta ---------- */

.offer-line { border: 1px solid var(--line); border-radius: 10px; padding: 16px; margin-bottom: 14px; background: #F8FBFF; position: relative; }
.offer-line-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.offer-line-head .idx { font-family: 'Space Grotesk', sans-serif; font-weight: 700; color: var(--muted); font-size: 12px; }
.offer-line-total { text-align: right; margin-top: 8px; font-size: 13px; }
.offer-line-total b { font-family: 'IBM Plex Mono', monospace; font-size: 15px; }
.totals-card { display: flex; justify-content: flex-end; gap: 28px; padding: 16px 20px; margin-top: 4px; }
.totals-card .tot { text-align: right; }
.totals-card .tot .lbl { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.totals-card .tot .val { font-family: 'IBM Plex Mono', monospace; font-size: 18px; font-weight: 600; }

/* ---------- Pratiche (iter commerciale) ---------- */

.pratiche-summary { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.sum-chip {
  display: inline-flex; align-items: center; gap: 8px; padding: 8px 14px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--paper-raised); cursor: pointer;
  font-size: 13px; font-weight: 600; color: var(--ink);
}
.sum-chip .n { font-family: 'Space Grotesk', sans-serif; font-weight: 700; font-size: 15px; }
.sum-chip:hover { border-color: var(--ink); }
.sum-chip.active { background: var(--ink); color: #fff; border-color: var(--ink); }
.sum-chip.alert .n { color: var(--red); }
.sum-chip.alert.active .n { color: #FFB3AB; }

.board { display: grid; grid-template-columns: repeat(4, minmax(250px, 1fr)); gap: 14px; overflow-x: auto; padding-bottom: 10px; align-items: start; }
.board-col { background: #DDE8F6; border-radius: 10px; padding: 10px; min-height: 120px; }
.board-col-head { display: flex; justify-content: space-between; align-items: center; padding: 4px 6px 10px; font-family: 'Space Grotesk', sans-serif; font-weight: 600; font-size: 13.5px; }
.board-col-head .count { background: var(--paper-raised); border-radius: 999px; padding: 1px 9px; font-size: 12px; color: var(--muted); }
.board-col-body { display: flex; flex-direction: column; gap: 10px; }
.board-empty { font-size: 12px; color: var(--muted); text-align: center; padding: 18px 6px; }

.pratica-card {
  background: var(--paper-raised); border: 1px solid var(--line); border-left: 4px solid var(--line);
  border-radius: 9px; padding: 12px 13px; cursor: pointer; box-shadow: 0 1px 2px rgba(22,35,31,0.05);
}
.pratica-card:hover { box-shadow: var(--shadow); }
.pratica-card.waiting { border-left-color: var(--amber); }
.pratica-card.alert { border-left-color: var(--red); background: #FFFBFA; }
.pratica-card.won { border-left-color: var(--teal); }
.pratica-card.lost { opacity: .7; }
.pc-cliente { font-size: 11.5px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.pc-titolo { font-weight: 600; font-size: 14px; margin: 3px 0 8px; }
.pc-eer { display: flex; gap: 5px; flex-wrap: wrap; margin-bottom: 8px; }
.pc-meta { font-size: 11.5px; color: var(--muted); margin-top: 8px; }
.pc-note { font-size: 12px; color: var(--ink-soft); margin-top: 6px; font-style: italic; }
.pc-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }

.pstato { display: inline-flex; align-items: center; font-size: 11.5px; font-weight: 700; padding: 4px 9px; border-radius: 999px; line-height: 1.3; }
.pstato.corso { background: #E6EEF9; color: var(--muted); }
.pstato.attesa { background: #FFF1CC; color: #8A6200; }
.pstato.sollecito { background: #F5E2DF; color: var(--red); }
.pstato.vinta { background: #DDF3EE; color: var(--teal); }
.pstato.persa { background: #E6EEF9; color: var(--muted); }

.stepper { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 18px; }
.step {
  display: inline-flex; align-items: center; gap: 8px; padding: 8px 14px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--paper-raised); cursor: pointer;
  font-size: 13px; font-weight: 600; color: var(--muted);
}
.step .n { width: 20px; height: 20px; border-radius: 50%; background: #DDE8F6; display: inline-flex; align-items: center; justify-content: center; font-size: 11px; }
.step.done { color: var(--teal); }
.step.done .n { background: #BFE8DF; }
.step.current { background: var(--ink); color: #fff; border-color: var(--ink); }
.step.current .n { background: var(--accent); color: #fff; }

.attesa-box { border: 1px solid #F0DDA0; background: #FFF8E3; border-radius: 9px; padding: 14px 16px; margin-bottom: 16px; }
.attesa-box.alert { border-color: #E4B9B3; background: #FBEFED; }
.attesa-box .ab-title { font-weight: 700; margin-bottom: 4px; }
.attesa-box .ab-sub { font-size: 12.5px; color: var(--ink-soft); margin-bottom: 10px; }
.attesa-box .ab-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 20px; margin-bottom: 18px; }
.info-grid .k { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
.info-grid .v { font-size: 13.5px; margin-top: 2px; }

.timeline { display: flex; flex-direction: column; margin-top: 4px; }
.tl-item { display: flex; gap: 12px; padding-bottom: 14px; }
.tl-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--grey-dot); margin-top: 5px; flex-shrink: 0; }
.tl-item.sollecito .tl-dot { background: var(--red); }
.tl-item.fase .tl-dot { background: var(--accent); }
.tl-item.stato .tl-dot { background: var(--amber); }
.tl-item.nota .tl-dot { background: var(--teal); }
.tl-item.offerta .tl-dot { background: #1B4FA3; }
.tl-text { font-size: 13.5px; }
.tl-date { font-size: 11.5px; color: var(--muted); }
.note-form { display: flex; gap: 8px; margin: 10px 0 18px; }
.note-form input { flex: 1; min-width: 0; padding: 9px 11px; border: 1px solid var(--line); border-radius: 7px; background: var(--paper-raised); }
.detail-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line); }

/* ---------- Toast ---------- */

#toast-root { position: fixed; bottom: 24px; right: 24px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast { background: var(--ink); color: #fff; padding: 12px 16px; border-radius: 8px; font-size: 13px; box-shadow: 0 8px 24px rgba(0,0,0,0.25); max-width: 320px; animation: toast-in .18s ease; }
.toast.err { background: var(--red); }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

/* ---------- Misc ---------- */

.toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; gap: 12px; flex-wrap: wrap; }
.toolbar .filters { display: flex; gap: 8px; flex-wrap: wrap; }
.toolbar input[type="text"], .toolbar select { padding: 8px 11px; border: 1px solid var(--line); border-radius: 7px; background: var(--paper-raised); }
.actions-cell { display: flex; gap: 6px; justify-content: flex-end; }
.text-muted { color: var(--muted); }
.section-title { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); margin: 26px 0 10px 0; }


/* ---------- Accesso / primo accesso ---------- */

body.auth-mode #app { display: none; }
#auth-root { display: none; }
body.auth-mode #auth-root {
  display: flex; align-items: center; justify-content: center; min-height: 100vh; min-height: 100dvh;
  padding: 24px 16px; padding-top: max(24px, env(safe-area-inset-top)); padding-bottom: max(24px, env(safe-area-inset-bottom));
  background: var(--ink);
}
.auth-card { width: 100%; max-width: 400px; background: var(--paper-raised); border-radius: 16px; padding: 28px 24px; box-shadow: 0 24px 60px rgba(0,0,0,0.35); }
.auth-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 22px; }
.auth-brand .brand-mark { width: 40px; height: 40px; font-size: 16px; border-radius: 10px; }
.auth-brand .t { font-family: 'Space Grotesk', sans-serif; font-weight: 600; font-size: 16px; line-height: 1.2; }
.auth-brand .t span { display: block; font-weight: 400; font-size: 12px; color: var(--muted); }
.auth-card h2 { font-size: 20px; margin-bottom: 4px; }
.auth-card .lead { color: var(--muted); font-size: 13px; margin: 0 0 18px; line-height: 1.45; }
.auth-card .btn { width: 100%; justify-content: center; padding: 12px 16px; font-size: 15px; }
.auth-error { background: #F5E2DF; color: var(--red); border-radius: 8px; padding: 10px 12px; font-size: 13px; margin-bottom: 14px; }
.pw-wrap { position: relative; }
.pw-wrap input { padding-right: 70px; }
.pw-toggle { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); background: none; border: none; color: var(--muted); font-size: 12.5px; font-weight: 600; padding: 10px; cursor: pointer; }

/* ---------- Box utente (menu laterale) ---------- */

.user-box { margin-top: auto; padding: 14px 10px 4px; border-top: 1px solid rgba(255,255,255,0.1); }
.user-name { font-weight: 600; font-size: 13.5px; color: #fff; word-break: break-word; }
.user-role { font-size: 11.5px; color: #8CA8CD; margin-top: 2px; }
.user-actions { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; margin-top: 10px; }
.btn-link { background: none; border: none; color: #B9CEEB; font-size: 13px; padding: 6px 0; cursor: pointer; text-align: left; }
.btn-link:hover { color: #fff; text-decoration: underline; }

.role-chip { display: inline-block; font-size: 11px; font-weight: 700; padding: 3px 9px; border-radius: 999px; background: #DDF3EE; color: var(--teal); }
.role-chip.off { background: #E6EEF9; color: var(--muted); }

/* ---------- Barra di navigazione in basso (solo telefono) ---------- */

.bottom-nav { display: none; }
.sidebar-backdrop { display: none; }

/* ---------- Telefono e tablet ---------- */

@media (max-width: 860px) {
  html, body { font-size: 15px; }

  .sidebar {
    position: fixed; z-index: 60; left: 0; top: 0; bottom: 0; width: 280px; max-width: 86vw; height: auto;
    transform: translateX(-100%); transition: transform .22s ease;
    padding-top: max(22px, env(safe-area-inset-top)); padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
  .sidebar.open { transform: translateX(0); box-shadow: 8px 0 40px rgba(0,0,0,0.35); }
  .sidebar-backdrop { display: block; position: fixed; inset: 0; z-index: 55; background: rgba(22,35,31,0.5); opacity: 0; pointer-events: none; transition: opacity .2s ease; }
  .sidebar-backdrop.open { opacity: 1; pointer-events: auto; }
  .nav-item { padding: 13px 12px; font-size: 15px; }

  .topbar {
    position: static; padding: 12px 16px; padding-top: max(12px, env(safe-area-inset-top));
    flex-direction: column; align-items: stretch; gap: 10px;
  }
  .topbar h1 { font-size: 19px; }
  #page-actions { display: flex; gap: 8px; flex-wrap: wrap; }
  #page-actions .btn { flex: 1 1 auto; justify-content: center; margin: 0 !important; }
  #page-actions:empty { display: none; }

  #view-root { padding: 16px 14px calc(88px + env(safe-area-inset-bottom)); }

  .bottom-nav {
    display: flex; position: fixed; z-index: 50; left: 0; right: 0; bottom: 0;
    background: var(--ink); border-top: 1px solid rgba(255,255,255,0.08);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .bottom-nav a, .bottom-nav button {
    flex: 1 1 0; min-width: 0; min-height: 58px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    color: #8CA8CD; text-decoration: none; background: none; border: none; font-size: 10.5px; font-weight: 600; cursor: pointer; padding: 6px 2px;
    -webkit-tap-highlight-color: transparent;
  }
  .bottom-nav svg { width: 23px; height: 23px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
  .bottom-nav .active { color: #fff; }
  .bottom-nav .active svg { stroke: var(--accent); }

  /* campi comodi da toccare; 16px evita lo zoom automatico di iPhone */
  .field input, .field select, .field textarea,
  .toolbar input[type="text"], .toolbar select, .note-form input { font-size: 16px; min-height: 46px; }
  .field textarea { min-height: 84px; }
  .btn { min-height: 44px; padding: 10px 16px; }
  .btn.small { min-height: 40px; padding: 8px 13px; }
  .btn.ghost.small { min-height: 40px; }

  .row-2, .row-3, .info-grid { grid-template-columns: 1fr; gap: 0 14px; }
  .info-grid { gap: 12px; }
  .grid-stats { grid-template-columns: 1fr 1fr; gap: 10px; }
  .stat { padding: 13px 14px; }
  .stat .num { font-size: 24px; }
  .toolbar .filters { width: 100%; }
  .toolbar .filters select, .toolbar .filters input { flex: 1 1 100%; width: 100%; }

  .pratiche-summary { overflow-x: auto; flex-wrap: nowrap; margin-left: -14px; margin-right: -14px; padding: 0 14px 4px; -webkit-overflow-scrolling: touch; }
  .sum-chip { flex-shrink: 0; min-height: 42px; }

  /* bacheca: una colonna alla volta, si scorre di lato */
  .board { grid-template-columns: none; grid-auto-flow: column; grid-auto-columns: 86%; scroll-snap-type: x mandatory; margin: 0 -14px; padding: 0 14px 10px; scroll-padding: 0 14px; -webkit-overflow-scrolling: touch; }
  .board-col { scroll-snap-align: start; }

  .totals-card { flex-wrap: wrap; justify-content: space-between; gap: 14px; }
  .totals-card .tot { text-align: left; flex: 1 1 120px; }
  .margin-history-row .mh-label { width: 104px; }
  .margin-history-row .mh-val { width: 56px; }
  .eer-lookup-row { padding: 13px 12px; }

  /* finestre: a tutto schermo, con il pulsante sempre raggiungibile */
  .modal-backdrop { padding: 0; align-items: flex-end; }
  .modal, .modal.wide { max-width: none; width: 100%; border-radius: 18px 18px 0 0; max-height: 94vh; max-height: 94dvh; display: flex; flex-direction: column; }
  .modal-head { padding: 14px 16px; flex-shrink: 0; }
  .modal-head h3 { font-size: 16px; padding-right: 8px; }
  .modal-body { padding: 16px; max-height: none; flex: 1; -webkit-overflow-scrolling: touch; }
  .modal-foot { padding: 12px 16px; padding-bottom: max(12px, env(safe-area-inset-bottom)); flex-shrink: 0; }
  .modal-foot .btn { flex: 1; justify-content: center; }
  .detail-actions .btn { flex: 1 1 calc(50% - 8px); justify-content: center; }

  #toast-root { left: 12px; right: 12px; bottom: calc(72px + env(safe-area-inset-bottom)); }
  .toast { max-width: none; }

  /* tabelle -> schede */
  table, thead, tbody, tr, td { display: block; }
  thead { display: none; }
  .card { overflow-x: visible; }
  tbody tr { padding: 12px 14px; border-bottom: 1px solid #E8EFF9; }
  tbody tr:last-child { border-bottom: none; }
  tbody td { display: flex; justify-content: space-between; align-items: center; gap: 14px; padding: 5px 0; border: none; text-align: right; }
  tbody td::before { content: attr(data-label); font-family: 'IBM Plex Sans', -apple-system, sans-serif; flex-shrink: 0; max-width: 42%; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); text-align: left; }
  tbody td:not([data-label])::before { display: none; }
  tbody td:first-child { display: block; text-align: left; font-size: 15px; padding-bottom: 6px; }
  tbody td:first-child::before { display: none; }
  tbody td.actions-cell { justify-content: flex-end; padding-top: 8px; }
  tbody td[colspan] { display: block; text-align: left; }
  tbody td.vuota { display: none; }
}

@media (max-width: 380px) {
  .grid-stats { grid-template-columns: 1fr; }
  .board { grid-auto-columns: 92%; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* ===== Clienti: elenco di lavoro e scheda con cronostoria ===== */
.card.pad { padding: 18px 20px; }
.mono { font-family: 'IBM Plex Mono', monospace; font-size: 12.5px; }

.quick-add { display: grid; grid-template-columns: 2fr 1.2fr auto; gap: 10px; margin-bottom: 14px; }
.quick-add input, .toolbar input[type="search"] {
  padding: 11px 13px; border: 1px solid var(--line); border-radius: 8px; background: var(--paper-raised);
  font: inherit; color: var(--ink); min-width: 0;
}
.quick-add input:focus, .toolbar input[type="search"]:focus { outline: 2px solid var(--accent); border-color: var(--accent); }
.toolbar input[type="search"] { width: 100%; max-width: 420px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.chip {
  display: inline-flex; align-items: center; gap: 7px; padding: 8px 14px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--paper-raised); color: var(--ink-soft);
  font: inherit; font-size: 13.5px; font-weight: 600; cursor: pointer; -webkit-tap-highlight-color: transparent;
}
.chip b { font-weight: 700; color: var(--muted); }
.chip.on { background: var(--ink); border-color: var(--ink); color: #fff; }
.chip.on b { color: #fff; }
.chip.alert:not(.on) { border-color: var(--red); color: var(--red); }
.chip.alert:not(.on) b { color: var(--red); }
.chip.small { padding: 5px 11px; font-size: 12.5px; }
.chips.tipi { margin-bottom: 12px; }

.cl-list { padding: 0; overflow: hidden; }
.cl-row {
  display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 14px 18px;
  border-bottom: 1px solid var(--line); cursor: pointer; background: var(--paper-raised);
}
.cl-row:last-child { border-bottom: none; }
.cl-row:hover, .cl-row:focus-visible { background: #F2F7FD; outline: none; }
.cl-row.urgente { box-shadow: inset 4px 0 0 var(--red); background: #FFFAF9; }
.cl-main { min-width: 0; flex: 1; }
.cl-name { font-weight: 600; font-size: 15.5px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.cl-sub { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 4px; font-size: 12.5px; color: var(--muted); }
.cl-sub b { color: var(--ink-soft); font-weight: 600; }
.cl-side { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; text-align: right; flex-shrink: 0; }
.cl-tel { font-family: 'IBM Plex Mono', monospace; font-size: 13px; color: var(--teal); text-decoration: none; font-weight: 600; padding: 4px 0; }

.stato-cl { font-size: 11px; font-weight: 700; padding: 3px 9px; border-radius: 999px; white-space: nowrap; background: #E6EEF9; color: var(--muted); }
.stato-cl.da_contattare { background: #DCEBFF; color: #1B4FA3; }
.stato-cl.contattato { background: #E4F4FB; color: #0F7390; }
.stato-cl.in_trattativa { background: #FFF1CC; color: #8A6200; }
.stato-cl.cliente { background: #DDF3EE; color: var(--teal); }
.stato-cl.non_interessato { background: #E6EEF9; color: var(--grey-dot); }

.richiamo { font-size: 12.5px; font-weight: 600; color: var(--muted); }
.richiamo.scaduto { color: var(--red); background: #F5E2DF; padding: 4px 10px; border-radius: 999px; }

.scheda { display: grid; grid-template-columns: minmax(280px, 380px) 1fr; gap: 18px; align-items: start; }
.scheda-col { display: flex; flex-direction: column; gap: 18px; min-width: 0; }
.contatti { display: flex; flex-wrap: wrap; gap: 10px; }
.btn.call { background: var(--teal); border-color: var(--teal); color: #fff; text-decoration: none; }
.quick-days { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }

.cronos { display: flex; flex-direction: column; }
.cr-item { padding: 12px 0 12px 16px; border-left: 3px solid var(--line); margin-left: 4px; position: relative; }
.cr-item::before { content: ''; position: absolute; left: -7px; top: 17px; width: 11px; height: 11px; border-radius: 50%; background: var(--paper-raised); border: 3px solid var(--accent); }
.cr-head { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.cr-tipo { font-size: 11px; font-weight: 700; padding: 3px 9px; border-radius: 999px; background: #E6EEF9; color: var(--ink-soft); }
.cr-tipo.telefonata { background: #DDF3EE; color: var(--teal); }
.cr-tipo.visita { background: #FFF1CC; color: #8A6200; }
.cr-tipo.email { background: #DCEBFF; color: #1B4FA3; }
.cr-data { font-size: 12.5px; color: var(--muted); }
.cr-del { margin-left: auto; background: none; border: none; color: var(--grey-dot); font-size: 22px; line-height: 1; cursor: pointer; padding: 2px 8px; }
.cr-del:hover { color: var(--red); }
.cr-testo { white-space: pre-wrap; line-height: 1.5; font-size: 14.5px; }

.badge-count { display: inline-flex; align-items: center; justify-content: center; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px; background: var(--red); color: #fff; font-size: 11.5px; font-weight: 700; font-style: normal; margin-left: auto; }
.badge-count[hidden] { display: none !important; }

@media (max-width: 860px) {
  .quick-add { grid-template-columns: 1fr; }
  .quick-add input { font-size: 16px; min-height: 46px; }
  .quick-add .btn { justify-content: center; }
  .toolbar input[type="search"] { font-size: 16px; min-height: 46px; max-width: none; }
  .field input[type="date"] { font-size: 16px; min-height: 46px; }
  .chips { flex-wrap: nowrap; overflow-x: auto; margin-left: -14px; margin-right: -14px; padding: 2px 14px 6px; scrollbar-width: none; }
  .chips::-webkit-scrollbar { display: none; }
  .chips.tipi { flex-wrap: wrap; overflow: visible; margin: 0 0 12px; padding: 0; }
  .chip { white-space: nowrap; min-height: 40px; }
  .chip.small { min-height: 36px; }
  .cl-row { padding: 13px 14px; align-items: flex-start; }
  .cl-side { max-width: 46%; }
  .cl-tel { padding: 8px 0; }
  .scheda { grid-template-columns: 1fr; }
  .scheda-col.wide { order: -1; }
  .bottom-nav a { position: relative; }
  .bottom-nav .badge-count { position: absolute; top: 5px; left: calc(50% + 6px); margin: 0; min-width: 18px; height: 18px; font-size: 11px; }
  .contatti .btn { flex: 1 1 100%; justify-content: center; }
}

/* Richieste / iter documenti tecnici */
.stato-cl.lead_confermato { background: #E9E1F3; color: #5A3E8C; }
.richiesta { border: 1px solid var(--line, #D3E0F1); border-radius: 12px; padding: 12px 14px; margin-bottom: 10px; background: #fff; }
.richiesta > summary { cursor: pointer; display: flex; justify-content: space-between; align-items: center; gap: 10px; font-weight: 600; list-style: none; }
.richiesta > summary::-webkit-details-marker { display: none; }
.richiesta[open] > summary { margin-bottom: 12px; }
.r-titolo { overflow: hidden; text-overflow: ellipsis; }
.passi { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.passo { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.p-dot { width: 24px; height: 24px; border-radius: 50%; background: #E6EEF9; color: var(--muted); display: inline-flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700; flex: none; }
.passo.fatto .p-dot { background: var(--teal); color: #fff; }
.p-txt { flex: 1; min-width: 0; }
.link-btn { background: none; border: 0; color: var(--muted); text-decoration: underline; cursor: pointer; font-size: 12px; padding: 4px; }
.eer-row { display: grid; grid-template-columns: 110px 1fr 70px 60px 1fr 28px; gap: 6px; margin-bottom: 6px; }
.eer-row input, .eer-row select { min-width: 0; padding: 8px; border: 1px solid #C3D4EA; border-radius: 8px; background: #fff; }
.r-azioni { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
@media (max-width: 760px) {
  .eer-row { grid-template-columns: 1fr 70px 60px 28px; }
  .eer-row .e-desc, .eer-row .e-n { grid-column: 1 / -1; }
  .eer-row .e-cod { grid-column: 1 / 2; }
}


/* ================= Restyling blu / azzurro ================= */
:root { --radius: 14px; --shadow: 0 1px 2px rgba(11,31,58,0.06), 0 10px 28px rgba(31,111,235,0.08); }
html, body { background: linear-gradient(180deg, #EAF3FF 0%, #F4F8FD 280px, #F4F8FD 100%) fixed; }
h1, h2, h3 { color: var(--ink); }
.sidebar { background: linear-gradient(185deg, #0B1F3A 0%, #0E3A7A 100%); box-shadow: 4px 0 24px rgba(11,31,58,0.18); }
.brand-mark { background: linear-gradient(135deg, var(--accent-2), var(--accent)); box-shadow: 0 6px 16px rgba(25,180,230,0.35); border-radius: 10px; }
.nav-item { transition: background .15s, transform .15s; }
.nav-item:hover { background: rgba(255,255,255,0.08); }
.nav-item.active { background: linear-gradient(135deg, var(--accent), var(--accent-2)); box-shadow: 0 6px 16px rgba(31,111,235,0.4); }
.btn { background: linear-gradient(135deg, var(--accent), #2B8CF0); box-shadow: 0 4px 12px rgba(31,111,235,0.28); border-radius: 10px; transition: transform .12s, box-shadow .12s, filter .12s; }
.btn:hover { filter: brightness(1.06); transform: translateY(-1px); box-shadow: 0 8px 18px rgba(31,111,235,0.32); }
.btn.secondary, .btn.ghost { background: #fff; box-shadow: none; }
.btn.secondary { color: var(--accent); border: 1px solid #BFD6F6; }
.btn.secondary:hover { background: #F0F6FE; }
.card { border-radius: var(--radius); border: 1px solid #DCE8F7; box-shadow: var(--shadow); }
.card.pad, .card-pad { background: linear-gradient(180deg, #fff 0%, #FBFDFF 100%); }
.section-title { color: var(--accent-ink); }
.field input:focus, .field select:focus, .field textarea:focus { outline: 3px solid rgba(25,180,230,0.35); border-color: var(--accent-2); }
.page-head h1, #page-title { background: linear-gradient(90deg, #0B1F3A, #1F6FEB); -webkit-background-clip: text; background-clip: text; color: transparent; }
.cl-row { border-radius: 12px; transition: box-shadow .15s, transform .15s, border-color .15s; }
.cl-row:hover { box-shadow: 0 8px 22px rgba(31,111,235,0.14); transform: translateY(-1px); border-color: #BFD6F6; }
.chip.on { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; border-color: transparent; }
.richiesta { border-color: #CFE0F6; background: linear-gradient(180deg, #FFFFFF, #F6FAFF); box-shadow: 0 2px 10px rgba(31,111,235,0.06); }
.passo.fatto .p-dot { background: linear-gradient(135deg, var(--accent), var(--accent-2)); }
.passo .p-dot { background: #E1ECFA; color: #4D6A93; }
.stato-cl.cliente { background: #DDF3EE; color: #12705F; }
.stato-cl.lead_confermato { background: #DCEBFF; color: #1B4FA3; }
.cr-item::before { border-color: var(--accent-2); }
.bottom-nav { background: linear-gradient(180deg, #0E2B55, #0B1F3A); }
table thead th { background: #F0F6FE; color: var(--accent-ink); }
.btn, a.btn { text-decoration: none; }

/* Report */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 18px; }
.kpi { background: linear-gradient(135deg, #fff, #F1F7FF); border: 1px solid #DCE8F7; border-radius: 14px; padding: 14px 16px; box-shadow: var(--shadow); position: relative; overflow: hidden; }
.kpi::after { content: ''; position: absolute; right: -18px; top: -18px; width: 70px; height: 70px; border-radius: 50%; background: radial-gradient(circle, rgba(25,180,230,.22), transparent 70%); }
.kpi-l { font-size: 11.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.kpi-v { font-family: 'Space Grotesk', sans-serif; font-size: 26px; font-weight: 700; color: var(--accent-ink); margin: 4px 0 2px; }
.kpi-n { font-size: 12px; color: var(--muted); }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid-2 .span-2 { grid-column: 1 / -1; }
.chart { width: 100%; height: auto; display: block; }
.legenda { display: flex; gap: 16px; font-size: 12px; color: var(--muted); margin-top: 6px; }
.legenda i, .leg-list i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; }
.hbars { display: flex; flex-direction: column; gap: 10px; }
.hbar { display: grid; grid-template-columns: minmax(90px, 38%) 1fr auto; gap: 10px; align-items: center; font-size: 13px; }
.hb-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hb-track { background: #E6EEF9; border-radius: 999px; height: 10px; overflow: hidden; }
.hb-fill { height: 100%; border-radius: 999px; }
.hb-val { font-weight: 600; color: var(--accent-ink); font-size: 12.5px; }
.ciambella { display: flex; align-items: center; gap: 22px; flex-wrap: wrap; }
.leg-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
@media (max-width: 860px) { .grid-2 { grid-template-columns: 1fr; } }
@media print { .sidebar, .bottom-nav, #page-actions { display: none !important; } #app { display: block; } }

/* Documenti */
.dropzone { border: 2px dashed #A9C6EE; border-radius: 16px; padding: 28px 16px; text-align: center; background: linear-gradient(180deg, #F5FAFF, #EAF3FF); display: flex; flex-direction: column; align-items: center; gap: 6px; transition: border-color .15s, background .15s; }
.dropzone.over { border-color: var(--accent); background: #DDEBFF; }
.dz-ico { width: 46px; height: 46px; border-radius: 50%; background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; font-size: 22px; display: flex; align-items: center; justify-content: center; box-shadow: 0 6px 16px rgba(31,111,235,.3); }
.dz-link { color: var(--accent); text-decoration: underline; cursor: pointer; font-weight: 600; }
.toolbar-docs { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }
.toolbar-docs input[type=search] { max-width: 420px; padding: 10px 12px; border: 1px solid #C3D4EA; border-radius: 10px; background: #fff; }
.doc-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.doc-card { display: flex; gap: 14px; background: #fff; border: 1px solid #DCE8F7; border-radius: 14px; padding: 14px; box-shadow: var(--shadow); transition: transform .15s, box-shadow .15s; }
.doc-card:hover { transform: translateY(-2px); box-shadow: 0 12px 26px rgba(31,111,235,.14); }
.doc-ico { width: 46px; height: 56px; border-radius: 8px; color: #fff; font-weight: 700; font-size: 12px; display: flex; align-items: flex-end; justify-content: center; padding-bottom: 8px; flex: none; position: relative; }
.doc-ico::before { content: ''; position: absolute; right: 0; top: 0; border-width: 0 12px 12px 0; border-style: solid; border-color: transparent #fff rgba(255,255,255,.4) transparent; }
.doc-body { min-width: 0; flex: 1; }
.doc-title { font-weight: 600; color: var(--accent-ink); overflow-wrap: anywhere; }
.doc-meta { font-size: 12px; color: var(--muted); margin: 2px 0 6px; }
.doc-desc { font-size: 13px; margin-bottom: 8px; }
.doc-act { display: flex; gap: 6px; flex-wrap: wrap; }
.doc-act .btn { text-decoration: none; }
