:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e3e2dd;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #85847f;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --good: #0ca30c;
  --good-ink: #006300;
  --warning: #fab219;
  --critical: #d03b3b;
  --critical-ink: #b42323;
  --grid: #e9e8e3;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 2px 8px rgba(0,0,0,.04);
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #2f2f2c;
    --text: #ffffff; --text-2: #c3c2b7; --muted: #8d8c85;
    --accent: #3987e5; --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70;
    --good-ink: #4cc94c; --critical-ink: #ef7c7c; --grid: #2a2a28;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #2f2f2c;
  --text: #ffffff; --text-2: #c3c2b7; --muted: #8d8c85;
  --accent: #3987e5; --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70;
  --good-ink: #4cc94c; --critical-ink: #ef7c7c; --grid: #2a2a28;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 22px; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
a { color: var(--accent); }
.num { font-variant-numeric: tabular-nums; }

/* layout */
.app { display: grid; grid-template-columns: 228px 1fr; min-height: 100vh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); padding: 18px 12px; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.logo { display: flex; align-items: center; gap: 10px; font-weight: 650; padding: 4px 10px 18px; }
.logo-mark { width: 22px; height: 22px; border-radius: 6px; background: var(--accent); }
#nav { display: flex; flex-direction: column; gap: 2px; }
#nav a { color: var(--text-2); text-decoration: none; padding: 8px 10px; border-radius: 8px; white-space: nowrap; }
#nav a:hover { background: var(--surface-2); color: var(--text); }
#nav a.active { background: var(--surface-2); color: var(--text); font-weight: 600; }
.main { min-width: 0; }
.topbar { position: sticky; top: 0; z-index: 5; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; padding: 12px 28px; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--border); }
.brand-pick { display: flex; align-items: center; gap: 8px; }
.brand-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex: none; }
.brand-pick select { font-weight: 600; min-width: 190px; }
#curSelect { font-weight: 600; }
.fx-form { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.period { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.custom-dates { display: inline-flex; gap: 6px; align-items: center; color: var(--muted); }
#themeToggle { margin-left: auto; }
.page { padding: 24px 28px 64px; display: flex; flex-direction: column; gap: 20px; max-width: 1400px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; color: var(--text-2); }

/* controls */
select, input, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 10px;
}
textarea { min-height: 70px; resize: vertical; }
input[type=color] { padding: 2px; height: 36px; width: 60px; }
select:focus, input:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 40%, transparent); outline-offset: 1px; }
.btn { font: inherit; border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 8px; padding: 7px 12px; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--critical-ink); }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn.icon { width: 36px; padding: 6px 0; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 6px 12px; font: inherit; color: var(--text-2); cursor: pointer; }
.seg button.on { background: var(--surface-2); color: var(--text); font-weight: 600; }

/* cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; min-width: 0; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.card-head .sub { color: var(--muted); font-size: 12px; font-weight: 400; margin-left: 6px; }
.card-head .actions { display: flex; gap: 8px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 20px; }
.grid-3 { display: grid; grid-template-columns: 2fr 1fr; gap: 20px; }

/* KPI tiles */
.kpi-group { display: flex; flex-direction: column; gap: 8px; }
.kpi-group > .label { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.kpi .k-label { color: var(--text-2); font-size: 12.5px; }
.kpi .k-value { font-size: 24px; font-weight: 650; margin-top: 4px; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.kpi .k-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.kpi.hero { border-color: color-mix(in srgb, var(--accent) 35%, var(--border)); }
.pos { color: var(--good-ink); }
.neg { color: var(--critical-ink); }

/* tables */
.tbl-wrap { overflow-x: auto; margin: 0 -18px -18px; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 9px 12px; border-top: 1px solid var(--border); white-space: nowrap; }
th:first-child, td:first-child { padding-left: 18px; }
th:last-child, td:last-child { padding-right: 18px; }
th { font-size: 12px; font-weight: 600; color: var(--muted); background: var(--surface-2); border-top: 0; }
td.r, th.r { text-align: right; }
tbody tr:hover { background: color-mix(in srgb, var(--surface-2) 60%, transparent); }
td .row-actions { visibility: hidden; display: inline-flex; gap: 4px; }
tr:hover .row-actions, tr:focus-within .row-actions { visibility: visible; }
td.wrap { white-space: normal; min-width: 200px; }
.empty { color: var(--muted); padding: 28px 18px; text-align: center; }
tfoot td { font-weight: 650; background: var(--surface-2); }

/* badges */
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 500; background: var(--surface-2); color: var(--text-2); }
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.badge.good::before { background: var(--good); }
.badge.warn::before { background: var(--warning); }
.badge.bad::before { background: var(--critical); }
.badge.info::before { background: var(--accent); }
.badge.plain::before { display: none; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }

/* bars (funnel, breakdowns) */
.bars { display: flex; flex-direction: column; gap: 12px; }
.bar-row .bar-top { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; margin-bottom: 5px; }
.bar-row .bar-top .v { font-weight: 600; font-variant-numeric: tabular-nums; }
.bar-track { height: 10px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 4px; background: var(--series-1); min-width: 2px; }
.bar-conv { font-size: 12px; color: var(--muted); margin: 4px 0 0 2px; }
.chart-box { position: relative; height: 280px; }

/* kanban */
.kanban { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(240px, 1fr); gap: 14px; overflow-x: auto; padding-bottom: 6px; }
.col { background: var(--surface-2); border-radius: var(--radius); padding: 10px; min-height: 160px; display: flex; flex-direction: column; gap: 8px; }
.col.drop { outline: 2px dashed var(--accent); outline-offset: -2px; }
.col-head { display: flex; justify-content: space-between; align-items: center; padding: 2px 4px 4px; font-weight: 600; font-size: 13px; }
.col-head .count { color: var(--muted); font-weight: 500; margin-left: 6px; }
.kcard { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; cursor: grab; display: flex; flex-direction: column; gap: 6px; }
.kcard:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }
.kcard .t { font-weight: 550; }
.kcard .meta { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; font-size: 12px; color: var(--muted); }
.kcard.done .t { text-decoration: line-through; color: var(--muted); }
.overdue { color: var(--critical-ink); font-weight: 600; }

/* modal */
dialog { border: 1px solid var(--border); border-radius: 16px; padding: 0; background: var(--surface); color: var(--text); width: min(540px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(0,0,0,.35); }
.modal-head { padding: 18px 20px 0; }
.form { padding: 16px 20px; display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; max-height: 70vh; overflow-y: auto; }
.form label { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--text-2); }
.form label.full, .form .full { grid-column: 1 / -1; }
.form .hint { grid-column: 1 / -1; font-size: 12px; color: var(--muted); margin: -4px 0 0; }
.modal-foot { display: flex; justify-content: space-between; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); }
.modal-foot .right { display: flex; gap: 8px; margin-left: auto; }

.onboard { text-align: center; padding: 48px 24px; }
.onboard p { color: var(--text-2); max-width: 520px; margin: 10px auto 20px; }
.onboard .actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.note { color: var(--text-2); font-size: 13px; }
.list-plain { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; }
.list-plain li { display: flex; justify-content: space-between; gap: 10px; padding: 9px 0; border-top: 1px solid var(--border); align-items: center; }
.list-plain li:first-child { border-top: 0; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 50; font-weight: 500; }

/* sidebar: separatori, utente */
.sidebar { display: flex; flex-direction: column; }
.nav-sep { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; padding: 16px 10px 6px; }
#userBox { margin-top: auto; padding-top: 14px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 6px; }
#userBox a { display: flex; gap: 10px; align-items: center; color: var(--text); text-decoration: none; padding: 6px 8px; border-radius: 8px; line-height: 1.25; }
#userBox a:hover, #userBox a.active { background: var(--surface-2); }
#userBox .btn { align-self: flex-start; margin-left: 8px; }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-weight: 650; flex: none; }
#menuToggle { display: none; }

/* login */
#login { display: none; }
body.logged-out .app { display: none; }
body.logged-out #login { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card { width: min(360px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: var(--shadow); padding: 26px 24px; display: flex; flex-direction: column; gap: 14px; }
.login-card .logo { padding: 0 0 6px; font-size: 18px; }
.login-card label { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--text-2); }
.login-card p { margin: 0; font-size: 13px; }

/* varie */
.stack { display: flex; flex-direction: column; gap: 18px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
.clip { display: inline-block; max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; vertical-align: bottom; }
tr.clickable { cursor: pointer; }
.pad { padding: 12px 0 0; margin: 0; }
.tbl-wrap + .pad { padding: 30px 0 0; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.filters { flex-wrap: wrap; }
.filters input[type=search] { min-width: 180px; }
.narrow { max-width: 640px; }
.form.flat { padding: 0; max-height: none; overflow: visible; }
td.heat { background: color-mix(in srgb, var(--series-3) calc(var(--h) * 45%), transparent); }
td.heat.soft { background: color-mix(in srgb, var(--series-1) calc(var(--h) * 25%), transparent); }
.bar-track.goal { position: relative; overflow: visible; }
.bar-track.goal .pace { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--text); opacity: .55; border-radius: 1px; }
.steps { margin: 0 0 14px; padding-left: 20px; display: flex; flex-direction: column; gap: 6px; color: var(--text-2); }
code.secret { font-size: 14px; background: var(--surface-2); padding: 2px 6px; border-radius: 6px; user-select: all; }
.access-row { padding: 8px 0; border-top: 1px solid var(--border); }
.access-row:first-of-type { border-top: 0; }
.access-row ul { margin: 6px 0 0; padding-left: 20px; color: var(--text-2); }

/* modal grande: scheda lead, utenti */
dialog.wide { width: min(980px, calc(100vw - 32px)); }
.lead-grid { display: grid; grid-template-columns: 1fr 1.1fr; gap: 22px; padding: 14px 20px; max-height: 72vh; overflow-y: auto; }
.lead-grid h3 { margin: 4px 0 10px; }
.lead-grid h3:not(:first-child) { margin-top: 20px; }
.modal-head .note { margin: 4px 0 0; }
.timeline { list-style: none; margin: 0; padding: 0 0 0 14px; border-left: 2px solid var(--border); display: flex; flex-direction: column; gap: 10px; }
.timeline li { position: relative; display: flex; flex-direction: column; font-size: 13px; }
.timeline li::before { content: ""; position: absolute; left: -20px; top: 5px; width: 10px; height: 10px; border-radius: 50%; background: var(--accent); }
.timeline .when { color: var(--muted); font-size: 12px; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; margin: 0; font-size: 13px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; font-variant-numeric: tabular-nums; }
.chat { display: flex; flex-direction: column; gap: 6px; max-height: 60vh; overflow-y: auto; background: var(--surface-2); border-radius: 12px; padding: 12px; }
.msg { max-width: 82%; padding: 7px 10px; border-radius: 12px; background: var(--surface); border: 1px solid var(--border); font-size: 13px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg span { display: block; font-size: 11px; color: var(--muted); margin-top: 3px; }
.msg.out { align-self: flex-end; background: color-mix(in srgb, var(--accent) 14%, var(--surface)); }
.access-editor { display: flex; flex-direction: column; gap: 10px; }
.access-editor fieldset { border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px 12px; margin: 0; }
.access-editor fieldset.on { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }
.access-editor fieldset:not(.on) .access-body { display: none; }
.access-editor legend { padding: 0 4px; }
.access-editor .row { display: flex; gap: 8px; flex-wrap: wrap; margin: 6px 0 10px; }
.access-editor .row select { flex: 1; min-width: 200px; }
.perm-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px 14px; }
label.inline { display: flex !important; flex-direction: row !important; align-items: center; gap: 7px; font-size: 13px !important; color: var(--text) !important; }

@media (max-width: 1100px) { .grid-3 { grid-template-columns: 1fr; } }
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; z-index: 20; left: 0; top: 0; bottom: 0; width: 260px; height: 100vh; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 40px rgba(0,0,0,.2); }
  body.nav-open .sidebar { transform: none; }
  #menuToggle { display: inline-block; }
  .topbar, .page { padding-left: 16px; padding-right: 16px; }
  .grid-2 { grid-template-columns: 1fr; }
  .form, .lead-grid, .perm-grid { grid-template-columns: 1fr; }
  .brand-pick select { min-width: 0; max-width: 46vw; }
  .page-head { align-items: flex-start; }
}

[hidden] { display: none !important; }
