:root {
  --bg: #f5f6f8; --panel: #ffffff; --ink: #14213d; --muted: #5b6474; --line: #e2e5ea; --accent: #0f766e; --accent-ink: #ffffff;
  --ok: #166534; --ok-bg: #dcfce7; --warn: #92400e; --warn-bg: #fef3c7; --bad: #991b1b; --bad-bg: #fee2e2; --info: #1e40af; --info-bg: #dbeafe; --gray-bg: #eef0f3;
  --radius: 10px; --shadow: 0 1px 2px rgba(20,33,61,.06), 0 2px 8px rgba(20,33,61,.05);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; color: var(--ink); background: var(--bg);
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); line-height: 1.45; font-size: 15px; }
a { color: var(--accent); }
h1, h2, h3 { margin: 0 0 .5rem; line-height: 1.2; }
h2 { font-size: 1.25rem; } h3 { font-size: 1.02rem; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(160deg,#14213d,#0f766e); }
.login-card { background: var(--panel); border-radius: 14px; padding: 28px; width: min(380px, 100%); box-shadow: var(--shadow); display: grid; gap: 10px; }
.login-card h1 { font-size: 1.15rem; letter-spacing: .02em; }
.brand-mark { font-size: 2rem; color: var(--accent); }
input, select { font: inherit; padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink); min-width: 0; }
input:focus, select:focus, button:focus-visible, a:focus-visible { outline: 3px solid #5eead4; outline-offset: 1px; }
.msg { min-height: 1.2em; color: var(--bad); margin: 0; }

/* layout */
.app { display: grid; grid-template-columns: 220px 1fr; grid-template-rows: auto 1fr; min-height: 100vh; }
.topbar { grid-column: 1 / -1; display: flex; align-items: center; gap: 12px; padding: 10px 18px; background: var(--ink); color: #fff; position: sticky; top: 0; z-index: 20; }
.topbar .title { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.topbar .title strong { letter-spacing: .03em; font-size: 1rem; }
.company { font-size: .85rem; color: #c9d1e0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; gap: 10px; align-items: center; }
.fresh { font-size: .8rem; color: #c9d1e0; }
.nav { background: var(--panel); border-right: 1px solid var(--line); padding: 12px 8px; display: flex; flex-direction: column; gap: 2px; position: sticky; top: 56px; height: calc(100vh - 56px); overflow: auto; }
.nav a { padding: 9px 12px; border-radius: 8px; color: var(--ink); text-decoration: none; font-size: .95rem; }
.nav a:hover { background: var(--gray-bg); }
.nav a.active { background: var(--ink); color: #fff; }
.main { padding: 20px; min-width: 0; max-width: 1400px; }
.only-mobile { display: none; }

/* componenti */
.btn { font: inherit; border: 1px solid var(--line); background: #fff; color: var(--ink); padding: 8px 12px; border-radius: 8px; cursor: pointer; white-space: nowrap; }
.btn:hover:not(:disabled) { border-color: #9aa3b2; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { background: var(--bad); color: #fff; border-color: var(--bad); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.topbar .btn { background: #fca311; border-color: #fca311; color: #14213d; font-weight: 600; }
.icon-btn { font: inherit; background: none; border: 0; color: #fff; font-size: 1.4rem; cursor: pointer; padding: 4px 8px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 16px; overflow-x: auto; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
.kpi .v { font-size: 1.8rem; font-weight: 700; }
.kpi .l { font-size: .8rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.row-right { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }
.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; margin-bottom: 12px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { font: inherit; border: 0; background: #fff; padding: 7px 12px; cursor: pointer; }
.seg button.on { background: var(--ink); color: #fff; }

/* tabelle -> card su mobile */
table.t { width: 100%; border-collapse: collapse; font-size: .92rem; }
table.t th { text-align: left; font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); border-bottom: 1px solid var(--line); padding: 8px; }
table.t td { border-bottom: 1px solid var(--line); padding: 9px 8px; vertical-align: top; }
table.t tr.click { cursor: pointer; }
table.t tr.click:hover td { background: #f8fafc; }
.title-cell { font-weight: 600; }
table.t-fonti td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.t-fonti td.act { width: 1%; white-space: nowrap; }
table.t-fonti td:nth-child(3) { min-width: 150px; }
.small { font-size: .82rem; }

/* badge di stato: sempre icona + testo */
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; font-size: .78rem; font-weight: 600; white-space: nowrap; border: 1px solid transparent; }
.b-ok { background: var(--ok-bg); color: var(--ok); border-color: #bbf7d0; }
.b-warn { background: var(--warn-bg); color: var(--warn); border-color: #fde68a; }
.b-bad { background: var(--bad-bg); color: var(--bad); border-color: #fecaca; }
.b-info { background: var(--info-bg); color: var(--info); border-color: #bfdbfe; }
.b-gray { background: var(--gray-bg); color: #374151; border-color: #d9dde3; }
.score { font-weight: 700; font-variant-numeric: tabular-nums; }
.urgent { color: var(--bad); font-weight: 700; }
.soon { color: var(--warn); font-weight: 600; }

/* dettaglio */
.detail-head { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; align-items: flex-start; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 12px 0; overflow-x: auto; }
.tabs button { font: inherit; background: none; border: 0; padding: 9px 12px; cursor: pointer; border-bottom: 3px solid transparent; white-space: nowrap; }
.tabs button.on { border-color: var(--accent); font-weight: 600; }
dl.kv { display: grid; grid-template-columns: minmax(140px, 220px) 1fr; gap: 6px 14px; margin: 0; }
dl.kv dt { color: var(--muted); font-size: .88rem; }
dl.kv dd { margin: 0; overflow-wrap: anywhere; }
.why { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .85rem; background: #f8fafc; border: 1px solid var(--line); border-radius: 8px; padding: 12px; white-space: pre-wrap; overflow-x: auto; }
.prov { font-size: .82rem; color: var(--muted); border-left: 3px solid var(--line); padding-left: 8px; margin: 4px 0; overflow-wrap: anywhere; }
.empty { padding: 28px; text-align: center; color: var(--muted); }
.error-box { background: var(--bad-bg); color: var(--bad); border: 1px solid #fecaca; border-radius: 8px; padding: 12px; }
.note { background: var(--warn-bg); color: var(--warn); border: 1px solid #fde68a; border-radius: 8px; padding: 10px 12px; font-size: .9rem; }
.loading { padding: 30px; text-align: center; color: var(--muted); }
.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 50; max-width: calc(100% - 32px); box-shadow: var(--shadow); }
.confirm { border: 1px solid var(--line); border-radius: 12px; padding: 18px; max-width: min(440px, calc(100% - 32px)); }
.confirm::backdrop { background: rgba(20,33,61,.4); }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .only-mobile { display: inline-block; }
  .nav { position: fixed; top: 56px; left: 0; bottom: 0; width: 260px; z-index: 30; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); height: auto; }
  .nav.open { transform: none; }
  .main { padding: 14px 16px; }
  .fresh { display: none; }
  table.t thead { display: none; }
  table.t, table.t tbody, table.t tr, table.t td { display: block; width: 100%; }
  table.t tr { border: 1px solid var(--line); border-radius: 10px; margin-bottom: 10px; padding: 6px 10px; background: #fff; }
  table.t td { border: 0; padding: 4px 0; }
  table.t td[data-l]::before { content: attr(data-l) ": "; color: var(--muted); font-size: .78rem; text-transform: uppercase; }
  dl.kv { grid-template-columns: 1fr; }
  dl.kv dt { margin-top: 6px; }
}
@media (max-width: 420px) {
  .topbar { padding: 8px 10px; gap: 8px; }
  .topbar .btn { padding: 7px 9px; font-size: .85rem; }
  .kpi .v { font-size: 1.5rem; }
}
@media print { .nav, .topbar, .row-right, .tabs { display: none !important; } .app { display: block; } }
