/* Design system unificato dai mockup lista + dettaglio. Zero JS, solo htmx. */
:root {
  --bg: #F2F4F8;
  --surface: #FFFFFF;
  --ink: #0F1F38;
  --ink2: #5B6B84;
  --line: #E6EBF2;
  --brand: #0A2A5E;
  --brand2: #1D4ED8;
  --accent: #FF4D6D;
  --ok: #0E7A3D;
  --okbg: #E4F5EA;
  --warn: #8A5A00;
  --warnbg: #FFF3D6;
  --bad: #C81E1E;
  --badbg: #FDE7E7;
  --white: #FFFFFF;
  --nav-link: #DCE6FF;
  --id-bg: #EAF0FF;
  --th-bg: #EEF2F9;
  --field-bg: #F8FAFD;
  --input-bg: #FAFAFA;
  --input-border: #E0E0E0;
  --logo-bg: #F0F2F5;
  --logo-border: #D0D0D0;
  --del-border: #F3C2C2;
  --white-92: rgba(255,255,255,.92);
  --white-94: rgba(255,255,255,.94);
  --white-96: rgba(255,255,255,.96);
  --white-97: rgba(255,255,255,.97);
  --white-12: rgba(255,255,255,.12);
  --white-14: rgba(255,255,255,.14);
  --white-15: rgba(255,255,255,.15);
  --white-25: rgba(255,255,255,.25);
  --white-28: rgba(255,255,255,.28);
  --scrim: rgba(10,42,94,.45);
  --fab-shadow: rgba(29,78,216,.4);
  --btn-shadow: rgba(29,78,216,.35);
  --chip-n: rgba(0,0,0,.08);
  --r: 18px;
  --shadow: 0 10px 30px rgba(10, 42, 94, .10);
  --shadow-sm: 0 2px 10px rgba(10, 42, 94, .08);
  --tap: 48px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg); color: var(--ink); -webkit-font-smoothing: antialiased;
}
a { text-decoration: none; color: inherit; }
.muted { color: var(--ink2); }

/* Drawer checkbox (CSS-only, no JS) */
.drawer-toggle { display: none; }
.scrim { display: none; }
.shell { max-width: 1280px; margin: 0 auto; display: flex; align-items: stretch; }

/* Sidebar */
.sidebar {
  width: 264px; flex: 0 0 264px;
  background: linear-gradient(180deg, var(--brand), var(--brand2));
  color: var(--white); border-radius: 18px; margin: 14px 0 14px 14px; padding: 18px 14px;
  box-shadow: var(--shadow); position: sticky; top: 14px; height: calc(100vh - 28px);
  display: flex; flex-direction: column; gap: 6px;
  transition: width .25s ease, flex-basis .25s ease, padding .25s ease;
}
.brandrow { display: flex; align-items: center; gap: 10px; padding: 2px 6px 14px; }
.brandmark {
  width: 42px; height: 42px; border-radius: 13px; background: var(--white); color: var(--brand);
  font-weight: 900; display: flex; align-items: center; justify-content: center; font-size: 1.2rem;
}
.brandrow b { display: block; font-size: 1.02rem; }
.brandrow small { opacity: .75; font-size: .75rem; }
.snav { display: flex; flex-direction: column; gap: 6px; margin-top: 4px; }
.snav a {
  display: flex; align-items: center; gap: 12px; color: var(--nav-link);
  padding: 12px; border-radius: 14px; font-weight: 700; font-size: .92rem;
}
.snav a i { font-style: normal; font-size: 1.2rem; width: 26px; text-align: center; }
.snav a.on { background: var(--white); color: var(--brand); }
.snav a:not(.on):hover { background: var(--white-14); color: var(--white); }
.snav a.off { opacity: .45; pointer-events: none; }
.sfoot { margin-top: auto; background: var(--white-12); border-radius: 14px; padding: 12px; font-size: .8rem; }
.sfoot b { display: block; }
.collapse-btn {
  display: none; align-self: flex-end; width: 36px; height: 36px; border-radius: 10px;
  background: var(--white-15); color: var(--white); align-items: center; justify-content: center;
  cursor: pointer; font-size: 1.2rem; line-height: 1;
}
.collapse-btn:hover { background: var(--white-28); }
.hamb {
  display: none; width: 44px; height: 44px; border-radius: 14px;
  border: 1px solid var(--line); background: var(--white); font-size: 1.2rem;
  align-items: center; justify-content: center; cursor: pointer;
}

/* Content */
.phone { max-width: 480px; margin: 0 auto; min-height: 100vh; position: relative; padding-bottom: 120px; }
.appbar {
  position: sticky; top: 0; z-index: 15; background: var(--white-92);
  backdrop-filter: blur(12px); border-bottom: 1px solid var(--line); border-radius: 14px;
  display: flex; align-items: center; gap: 12px; padding: 16px 20px;
}
.avatar {
  width: 40px; height: 40px; border-radius: 50%; background: var(--brand); color: var(--white);
  display: flex; align-items: center; justify-content: center; font-weight: 800;
}
.appbar .who { flex: 1; }
.who b { display: block; font-size: .95rem; }
.who small { color: var(--ink2); }
.icon-btn {
  width: 44px; height: 44px; border-radius: 14px; border: 1px solid var(--line);
  background: var(--white); font-size: 1.2rem; cursor: pointer; position: relative;
}
.hero { padding: 18px 16px 8px; }
.hero h1 { font-size: 1.65rem; letter-spacing: -.02em; }
.hero h1 span { color: var(--brand2); }
.hero p { color: var(--ink2); font-size: .92rem; margin-top: 4px; }
.hero-card {
  margin: 14px 16px; background: linear-gradient(135deg, var(--brand), var(--brand2));
  color: var(--white); border-radius: 22px; padding: 18px; box-shadow: var(--shadow);
}
.hero-card small { opacity: .8; font-size: .75rem; text-transform: uppercase; letter-spacing: .08em; font-weight: 800; }
.hero-card h1 { font-size: 1.5rem; margin: 4px 0; }
.hero-card .tot { font-size: 2.1rem; font-weight: 900; letter-spacing: -.02em; }
.hero-card .row { display: flex; justify-content: space-between; align-items: end; margin-top: 6px; }

/* KPI / cards */
.kpis { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; padding: 14px 16px; }
.kpi {
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px;
  padding: 12px; box-shadow: var(--shadow-sm); font-weight: 600; color: var(--brand);
}
.kpi small { color: var(--ink2); font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; font-weight: 700; display: block; }
.kpi b, .kpi .v { font-size: 1.25rem; display: block; margin-top: 2px; }
.kpi.ok .v, .kpi.ok b { color: var(--ok); }
.kpi.warn .v, .kpi.warn b { color: var(--warn); }
.kpi.bad .v, .kpi.bad b { color: var(--bad); }
.kpi.dark .v { color: var(--brand); }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r);
  box-shadow: var(--shadow); margin: 0 16px 12px; padding: 6px 16px 14px;
}
.card h2 {
  font-size: .78rem; text-transform: uppercase; letter-spacing: .07em;
  color: var(--ink2); padding: 12px 0 6px;
}
.chart-card svg { display: block; max-width: 100%; height: auto; }

/* Search + chips + sort */
.sticky-tools {
  position: sticky; top: 65px; z-index: 15;
  background: linear-gradient(var(--bg) 85%, transparent); padding: 8px 16px 10px;
}
.search {
  display: flex; align-items: center; gap: 10px; background: var(--white);
  border: 1px solid var(--line); border-radius: 999px; padding: 0 6px 0 14px;
  height: var(--tap); box-shadow: var(--shadow-sm);
}
.search input { flex: 1; border: 0; outline: 0; font-size: 1rem; background: transparent; min-width: 0; }
.search button {
  height: 40px; border: 0; border-radius: 999px; padding: 0 16px;
  background: var(--brand); color: var(--white); font-weight: 700;
}
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 10px 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; border: 1px solid var(--line); background: var(--white); border-radius: 999px;
  padding: 10px 14px; font-size: .85rem; font-weight: 700; color: var(--ink2);
}
.chip.on { background: var(--brand); border-color: var(--brand); color: var(--white); }
.chip .n { background: var(--chip-n); border-radius: 999px; padding: 1px 7px; margin-left: 6px; font-size: .75rem; }
.chip.on .n { background: var(--white-25); }
.sortrow {
  display: flex; align-items: center; justify-content: space-between;
  padding: 2px 18px 8px; color: var(--ink2); font-size: .85rem; gap: 10px;
}

/* Ledger table */
.table-wrap {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r);
  box-shadow: var(--shadow); overflow: hidden; margin: 0 16px;
}
table.ledger { width: 100%; border-collapse: collapse; }
.ledger thead th {
  background: var(--brand); color: var(--white); text-align: left; font-size: .78rem;
  text-transform: uppercase; letter-spacing: .05em; padding: 14px;
}
.ledger thead th a { color: var(--white); }
.ledger thead th.num, .ledger td.num { text-align: right; }
.ledger tbody td { padding: 14px; border-bottom: 1px solid var(--line); font-size: .95rem; }
.ledger tbody tr:last-child td { border-bottom: 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; font-weight: 700; }
.badge {
  display: inline-flex; align-items: center; gap: 6px; font-weight: 800;
  font-size: .78rem; padding: 6px 10px; border-radius: 999px;
}
.badge::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.b-open, .b-aperto { background: var(--warnbg); color: var(--warn); }
.b-ok, .b-accettato { background: var(--okbg); color: var(--ok); }
.b-ko, .b-rifiutato { background: var(--badbg); color: var(--bad); }
.row-actions { display: flex; gap: 8px; }
.mini {
  border: 1px solid var(--line); background: var(--white); border-radius: 10px;
  padding: 9px 10px; font-size: 1rem; min-height: 40px; min-width: 44px;
  display: inline-flex; align-items: center; justify-content: center;
}

/* Mobile cards reflow */
.cards { display: none; flex-direction: column; gap: 12px; padding: 0 16px; }
.pcard { background: var(--white); border: 1px solid var(--line); border-radius: var(--r); box-shadow: var(--shadow-sm); overflow: hidden; }
.pcard-top { display: flex; gap: 12px; padding: 14px 14px 10px; align-items: center; cursor: pointer; }
.pcard-top .id {
  width: 48px; height: 48px; border-radius: 14px; background: var(--id-bg); color: var(--brand);
  font-weight: 900; display: flex; align-items: center; justify-content: center;
  flex-direction: column; line-height: 1; font-size: .8rem;
}
.pcard-top .id b { font-size: 1rem; }
.pcard-top .meta { flex: 1; min-width: 0; }
.meta b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meta small { color: var(--ink2); }
.amount { text-align: right; }
.amount b { font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.amount small { display: block; color: var(--ink2); font-size: .75rem; }
.reflow {
  display: grid; grid-template-columns: 1fr 1fr; margin: 0; padding: 10px 14px;
  border-top: 1px dashed var(--line); gap: 10px;
}
.reflow div small { display: block; font-size: .7rem; text-transform: uppercase; letter-spacing: .07em; color: var(--ink2); font-weight: 800; margin-bottom: 2px; }
.reflow div span { font-weight: 600; font-size: .92rem; }
.pcard-foot { display: flex; gap: 8px; padding: 12px 14px 14px; }
.chev { margin-left: 2px; color: var(--ink2); font-size: 1.4rem; font-weight: 800; line-height: 1; transition: transform .2s; align-self: center; }
.xpand { display: none; }
.xbody { display: none; }
.xpand:checked ~ .xbody { display: block; }
.xpand:checked ~ .pcard-top .chev { transform: rotate(90deg); }

/* Buttons */
.btn {
  flex: 1; min-height: var(--tap); border-radius: 14px; border: 1px solid var(--line);
  background: var(--white); font-weight: 800; display: inline-flex; align-items: center;
  justify-content: center; gap: 8px; color: var(--brand); font-size: .92rem; padding: 0 18px;
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--white); }
.btn.accent { background: var(--brand2); border-color: var(--brand2); color: var(--white); }
.btn.danger { color: var(--bad); }
.btn.slim { flex: 0 0 auto; min-height: 40px; }
.new-desktop { display: none; }
.fab {
  position: fixed; bottom: 86px; right: calc(50% - 220px); z-index: 31;
  background: var(--brand2); color: var(--white); border: 0; border-radius: 999px; height: 56px;
  padding: 0 20px; font-weight: 800; font-size: .95rem;
  box-shadow: 0 12px 30px var(--fab-shadow); display: flex; align-items: center; gap: 8px;
}

/* Pager / tabbar */
.pager { display: flex; align-items: center; justify-content: center; gap: 14px; padding: 16px; }
.pager .pg-btn {
  width: 48px; height: 48px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--white); font-size: 1.3rem; display: inline-flex; align-items: center; justify-content: center;
}
.pager .pg-btn.off { opacity: .35; }
.tabbar {
  position: fixed; bottom: 0; left: 50%; transform: translateX(-50%); width: 100%; max-width: 480px;
  background: var(--white-96); backdrop-filter: blur(10px); border-top: 1px solid var(--line);
  display: flex; padding: 8px 8px calc(10px + env(safe-area-inset-bottom)); z-index: 30;
}
.tabbar a {
  flex: 1; color: var(--ink2); font-size: .68rem; font-weight: 700;
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 0; border-radius: 12px;
}
.tabbar a.on { color: var(--brand); }
.tabbar a i { font-size: 1.3rem; font-style: normal; }

/* Dettaglio */
.top {
  position: sticky; top: 0; z-index: 20; background: var(--white-94);
  backdrop-filter: blur(12px); border-bottom: 1px solid var(--line);
  padding: 10px 12px; display: flex; align-items: center; gap: 10px;
}
.back {
  width: 44px; height: 44px; border-radius: 14px; border: 1px solid var(--line);
  background: var(--white); font-size: 1.2rem; display: flex; align-items: center; justify-content: center;
}
.top .t { flex: 1; }
.top .t b { display: block; }
.top .t small { color: var(--ink2); }
.seg { display: flex; background: var(--white); border: 1px solid var(--line); border-radius: 999px; padding: 4px; margin: 12px 16px; gap: 4px; }
.seg a, .seg button { flex: 1; border: 0; background: transparent; border-radius: 999px; min-height: 44px; font-weight: 800; color: var(--ink2); display: flex; align-items: center; justify-content: center; }
.seg .on { background: var(--brand); color: var(--white); }
.field { padding: 8px 0; border-top: 1px solid var(--line); }
.field label { display: block; font-size: .72rem; font-weight: 800; text-transform: uppercase; letter-spacing: .06em; color: var(--brand); margin-bottom: 6px; }
.field input, .field select {
  width: 100%; min-height: 52px; border: 1.5px solid var(--line); border-radius: 14px;
  padding: 0 14px; font-size: 1rem; background: var(--field-bg); font-family: inherit;
}
.field input:focus, .field select:focus { outline: 2px solid var(--brand2); background: var(--white); }
.cli-grid-2 { display: grid; grid-template-columns: 1fr 1fr; column-gap: 16px; }
.cli-grid-2 .span { grid-column: 1 / -1; }
table.righe { width: 100%; border-collapse: collapse; }
.righe th { background: var(--th-bg); color: var(--brand); font-size: .75rem; text-transform: uppercase; text-align: left; padding: 10px; }
.righe td { border-top: 1px solid var(--line); padding: 8px; }
.righe input { width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 10px; font-size: .95rem; }
.riga { background: var(--field-bg); border: 1.5px solid var(--line); border-radius: 16px; padding: 14px; margin: 10px 0; }
.riga-head { display: flex; gap: 10px; align-items: center; margin-bottom: 8px; }
.riga-head .n {
  width: 34px; height: 34px; border-radius: 11px; background: var(--brand); color: var(--white);
  font-weight: 900; display: flex; align-items: center; justify-content: center;
}
.riga-head b { flex: 1; }
.del { width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--del-border); background: var(--white); color: var(--bad); font-size: 1.1rem; }
.riga label { display: block; font-size: .7rem; font-weight: 800; text-transform: uppercase; letter-spacing: .06em; color: var(--ink2); margin: 8px 0 4px; }
.riga input, .riga textarea {
  width: 100%; border: 1.5px solid var(--line); border-radius: 12px; padding: 12px;
  font-size: 1rem; background: var(--white); font-family: inherit; min-height: var(--tap);
}
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.totrow { display: flex; justify-content: space-between; align-items: center; background: var(--id-bg); border-radius: 12px; padding: 10px 12px; margin-top: 10px; font-weight: 900; }
.add {
  width: 100%; min-height: var(--tap); border: 2px dashed var(--brand2); background: var(--white);
  color: var(--brand2); border-radius: 14px; font-weight: 800; margin: 4px 0 2px;
}
.totals { background: var(--white); border: 1px solid var(--line); border-radius: var(--r); margin: 0 16px; padding: 14px 16px; box-shadow: var(--shadow); }
.totals div { display: flex; justify-content: space-between; padding: 5px 0; color: var(--ink2); }
.totals .grand { color: var(--ink); font-size: 1.25rem; font-weight: 900; border-top: 1px dashed var(--line); margin-top: 6px; padding-top: 10px; }
.sticky {
  position: fixed; bottom: 0; left: 50%; transform: translateX(-50%); width: 100%; max-width: 480px;
  background: var(--white-97); border-top: 1px solid var(--line);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); z-index: 30;
}
.sticky .sum { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.sticky .sum b { font-size: 1.3rem; }
.actions-bar { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; gap: 8px; }
.act {
  border: 1px solid var(--line); background: var(--white); border-radius: 14px; min-height: 56px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: .68rem; font-weight: 800; color: var(--brand); gap: 2px;
}
.act i { font-style: normal; font-size: 1.25rem; }
.act.save { background: var(--brand); border-color: var(--brand); color: var(--white); }

/* Settings */
.set-wrap { max-width: 1100px; margin: 0 auto; }
.set-head { text-align: center; margin-bottom: 2rem; }
.set-head h1 { color: var(--brand); }
.set-head p { color: var(--ink2); }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1.5rem; }
.set-card { background: var(--white); border-radius: 12px; padding: 1.5rem; box-shadow: var(--shadow); border: 1px solid var(--line); }
.set-card h2 { margin-bottom: 1rem; font-size: 1.1rem; }
.fg { margin-bottom: 1rem; }
.fg label { display: block; font-size: .8rem; color: var(--ink2); text-transform: uppercase; margin-bottom: .4rem; }
.fg input, .fg textarea { width: 100%; padding: .7rem; border: 1px solid var(--input-border); border-radius: 8px; background: var(--input-bg); font-family: inherit; }
.logo-prev {
  height: 130px; background: var(--logo-bg); border: 2px dashed var(--logo-border); border-radius: 8px;
  display: flex; align-items: center; justify-content: center; color: var(--ink2); margin-bottom: 1rem; font-weight: 700;
}
.logo-img { max-width: 200px; border-radius: 8px; }
.form-foot { margin-top: 1rem; display: flex; gap: 10px; }
.form-foot .btn { flex: 0 0 auto; }
.link-row { color: inherit; }

/* Breakpoints */
@media(max-width:760px) {
  .shell { display: block; }
  .sidebar {
    position: fixed; z-index: 60; top: 0; left: 0; height: 100vh; margin: 0;
    border-radius: 0 18px 18px 0; transform: translateX(-105%); transition: transform .25s ease; width: 270px;
  }
  .drawer-toggle:checked ~ .shell .sidebar { transform: none; }
  .drawer-toggle:checked ~ .scrim {
    display: block; position: fixed; inset: 0; background: var(--scrim); z-index: 55;
  }
  .hamb { display: flex; }
  table.ledger, .table-wrap { display: none; }
  .cards { display: flex; }
  .fab { right: 16px; }
  .phone { padding-bottom: 190px; }
}
@media(min-width:761px) {
  .phone { max-width: 1100px; padding: 0 0 40px; }
  .shell .phone { flex: 1; min-width: 0; margin: 0; }
  .cards, .tabbar, .fab { display: none !important; }
  .scrim { display: none !important; }
  .hero, .kpis, .sticky-tools, .sortrow { padding-left: 24px; padding-right: 24px; }
  .table-wrap { margin: 0 24px; }
  .hero-card { margin: 14px 24px; }
  .card { margin: 0 24px 12px; }
  .totals { margin: 0 24px; }
  .seg { margin: 12px 24px; }
  .collapse-btn { display: flex; }
  .drawer-toggle:checked ~ .shell .sidebar { width: 76px; flex-basis: 76px; padding: 18px 10px; align-items: center; }
  .drawer-toggle:checked ~ .shell .brandrow { justify-content: center; padding: 2px 0 14px; }
  .drawer-toggle:checked ~ .shell .brandrow .brand-txt { display: none; }
  .drawer-toggle:checked ~ .shell .snav a { font-size: 0; justify-content: center; padding: 12px 0; }
  .drawer-toggle:checked ~ .shell .snav a i { font-size: 1.3rem; width: auto; }
  .drawer-toggle:checked ~ .shell .sfoot { display: none; }
  .drawer-toggle:checked ~ .shell .collapse-btn { align-self: center; font-size: 0; }
  .drawer-toggle:checked ~ .shell .collapse-btn::after { content: '\00BB'; font-size: 1.2rem; }
  .drawer-toggle:checked ~ .shell .phone { max-width: none; }
  .new-desktop {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    height: 48px; padding: 0 22px; border-radius: 999px; border: 0;
    background: var(--brand2); color: var(--white); font-weight: 800; font-size: .95rem;
    box-shadow: 0 8px 20px var(--btn-shadow); white-space: nowrap;
  }
  .new-desktop:hover { background: var(--brand); }
}
