:root {
  --bg: #f5f6f8; --card: #ffffff; --ink: #1d2330; --muted: #6b7385; --line: #e2e5eb;
  --brand: #1f3a5f; --brand-2: #2d5a8f; --accent: #f4b942;
  --pos: #1e8a4c; --neg: #c0392b; --warn: #b7791f;
  --pos-bg: #e6f4ec; --neg-bg: #fbe9e7; --warn-bg: #fdf3e1;
  --radius: 12px; --shadow: 0 1px 3px rgba(20,30,50,.08), 0 4px 14px rgba(20,30,50,.05);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161d; --card: #1b212b; --ink: #e8ebf1; --muted: #98a1b3; --line: #2c3442;
    --brand: #8fb4e6; --brand-2: #a9c6ee; --pos: #4cc27f; --neg: #ff7b6b; --warn: #f0b75a;
    --pos-bg: #173626; --neg-bg: #3b1d1a; --warn-bg: #3a2e17; --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--ink); }
a { color: var(--brand-2); }
h1 { font-size: 1.45rem; margin: .2rem 0 1rem; }
h2 { font-size: 1.15rem; margin: 1.4rem 0 .6rem; }
h3 { font-size: 1rem; margin: 1rem 0 .4rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }

.topbar { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 1rem; padding: .6rem 1rem;
  background: #1f3a5f; color: #fff; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: .5rem; color: #fff; text-decoration: none; font-weight: 700; }
.topbar nav { display: flex; gap: .2rem; margin-left: auto; flex-wrap: wrap; }
.topbar nav a, .topbar nav button { color: #dfe8f5; text-decoration: none; padding: .35rem .6rem; border-radius: 8px; background: none; border: 0; font: inherit; cursor: pointer; }
.topbar nav a.active, .topbar nav a:hover, .topbar nav button:hover { background: rgba(255,255,255,.14); color: #fff; }
.menu-btn { display: none; margin-left: auto; background: none; border: 0; color: #fff; font-size: 1.4rem; cursor: pointer; }
@media (max-width: 760px) {
  .menu-btn { display: block; }
  .topbar nav { display: none; width: 100%; flex-direction: column; }
  .topbar nav.open { display: flex; }
}

main { max-width: 1180px; margin: 0 auto; padding: 1rem 16px 4rem; }
.loading { padding: 3rem; text-align: center; color: var(--muted); }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem; margin-bottom: 1rem; }
.grid { display: grid; gap: .8rem; }
.grid.kpi { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.grid.cards { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.kpi .card { margin: 0; padding: .8rem 1rem; }
.kpi .label { font-size: .8rem; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.kpi .value { font-size: 1.35rem; font-weight: 700; margin-top: .15rem; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: .8rem; color: var(--muted); }
.pos { color: var(--pos); } .neg { color: var(--neg); }

.row { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; }
.row.between { justify-content: space-between; }
.spacer { flex: 1; }

.btn { display: inline-flex; align-items: center; gap: .35rem; padding: .55rem .95rem; border-radius: 9px; border: 1px solid var(--line);
  background: var(--card); color: var(--ink); font: inherit; cursor: pointer; text-decoration: none; }
.btn:hover { border-color: var(--brand-2); }
.btn.primary { background: #1f3a5f; color: #fff; border-color: #1f3a5f; }
.btn.primary:hover { background: #2d5a8f; }
.btn.wa { background: #1fa855; color: #fff; border-color: #1fa855; }
.btn.danger { color: var(--neg); }
.btn.sm { padding: .3rem .6rem; font-size: .85rem; }
.btn[disabled] { opacity: .55; cursor: wait; }

label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .2rem; }
input, select, textarea { width: 100%; padding: .55rem .65rem; border: 1px solid var(--line); border-radius: 8px; font: inherit;
  background: var(--card); color: var(--ink); }
textarea { min-height: 90px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-2); outline-offset: -1px; }
.form-grid { display: grid; gap: .7rem; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.form-grid .full { grid-column: 1 / -1; }
.check { display: flex; align-items: center; gap: .4rem; color: var(--ink); font-size: .95rem; }
.check input { width: auto; }

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: .5rem .55rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { font-size: .78rem; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; font-weight: 600; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr.clickable { cursor: pointer; } tr.clickable:hover td { background: rgba(45,90,143,.06); }
tfoot td { font-weight: 700; }

.tabs { display: flex; gap: .3rem; border-bottom: 1px solid var(--line); margin-bottom: 1rem; overflow-x: auto; }
.tabs button { background: none; border: 0; padding: .6rem .9rem; font: inherit; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button.active { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }

.badge { display: inline-block; padding: .1rem .5rem; border-radius: 99px; font-size: .75rem; background: var(--line); color: var(--ink); }
.badge.costo { background: var(--neg-bg); color: var(--neg); }
.badge.ricavo { background: var(--pos-bg); color: var(--pos); }
.badge.alto { background: var(--neg-bg); color: var(--neg); }
.badge.medio { background: var(--warn-bg); color: var(--warn); }
.badge.basso { background: var(--line); }

.bar { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--brand-2); }
.bar.inc > span { background: var(--pos); }

.md { line-height: 1.55; }
.md h2 { border-bottom: 1px solid var(--line); padding-bottom: .25rem; }
.md table { margin: .5rem 0; }
.md ul { padding-left: 1.2rem; }
.md li { margin: .2rem 0; }

.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }
.auth { max-width: 400px; margin: 8vh auto; }
.auth h1 { text-align: center; }
.auth .logo { display: block; margin: 0 auto .6rem; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1d2330; color: #fff; padding: .7rem 1.1rem;
  border-radius: 10px; z-index: 50; max-width: calc(100% - 32px); box-shadow: 0 6px 24px rgba(0,0,0,.25); }
.toast.err { background: #c0392b; }

dialog { border: 0; border-radius: 14px; padding: 0; width: min(640px, calc(100% - 24px)); background: var(--card); color: var(--ink); box-shadow: 0 10px 40px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(10,15,25,.45); }
dialog form { padding: 1.1rem; }
dialog h2 { margin-top: 0; }
.modal-actions { display: flex; gap: .5rem; justify-content: flex-end; margin-top: 1rem; flex-wrap: wrap; }

.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
pre.code { background: var(--bg); padding: .7rem; border-radius: 8px; overflow: auto; font-size: .8rem; }
