:root {
    --bg: #f6f4ef;
    --surface: #ffffff;
    --ink: #1f2430;
    --muted: #6b7280;
    --line: #e6e2d8;
    --brand: #c2410c;
    --brand-ink: #ffffff;
    --side: #1f2430;
    --side-ink: #e8e6e1;
    --in: #15803d;
    --in-bg: #e8f5ec;
    --out: #b91c1c;
    --out-bg: #fbeaea;
    --warn: #b45309;
    --radius: 12px;
    --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
    :root {
        --bg: #14161c; --surface: #1d2028; --ink: #ecebe7; --muted: #9aa0ab; --line: #2c303a;
        --side: #0f1116; --in: #4ade80; --in-bg: #16301f; --out: #f87171; --out-bg: #3a1a1a; --warn: #fbbf24;
    }
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--ink); }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.6rem; margin: 0; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 1rem 0 .5rem; }
code { background: var(--bg); padding: 1px 5px; border-radius: 4px; font-size: .9em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.up { color: var(--in); }
.down { color: var(--out); }
.warn { color: var(--warn); }
.center { text-align: center; }
.mt { margin-top: 1.5rem; }

/* Layout */
.app { display: flex; min-height: 100vh; }
.sidebar { width: 250px; background: var(--side); color: var(--side-ink); flex-shrink: 0; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { display: flex; gap: .75rem; align-items: center; padding: 1.25rem 1rem; border-bottom: 1px solid rgba(255,255,255,.08); }
.brand small { display: block; opacity: .65; font-size: .78rem; }
.logo { display: inline-grid; place-items: center; width: 38px; height: 38px; border-radius: 10px; background: var(--brand); color: #fff; font-size: 1.2rem; }
.sidebar nav { padding: .75rem .5rem; display: flex; flex-direction: column; gap: 2px; }
.sidebar nav a { color: var(--side-ink); padding: .55rem .75rem; border-radius: 8px; display: flex; gap: .6rem; align-items: center; }
.sidebar nav a:hover { background: rgba(255,255,255,.07); text-decoration: none; }
.sidebar nav a.active { background: var(--brand); color: #fff; }
.sidebar .ico { width: 1.2rem; text-align: center; opacity: .9; }
.sidebar .ro { margin-left: auto; font-size: .75rem; opacity: .6; }
.nav-sep { margin: 1rem .75rem .25rem; font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; opacity: .5; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 1rem; padding: .6rem 1.5rem; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.event-switch { display: flex; align-items: center; gap: .5rem; }
.event-switch label { color: var(--muted); font-size: .85rem; }
.user-menu { margin-left: auto; display: flex; gap: .75rem; align-items: center; }
.menu-btn { display: none; background: none; border: 0; font-size: 1.4rem; color: var(--ink); cursor: pointer; }
.content { padding: 1.5rem; max-width: 1400px; width: 100%; }
.guest-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }

/* Components */
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; margin-bottom: 1.25rem; flex-wrap: wrap; }
.page-head p { margin: .25rem 0 0; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.25rem; margin-bottom: 1.25rem; box-shadow: var(--shadow); overflow-x: auto; }
.card.empty { text-align: center; padding: 2.5rem 1.25rem; }
details.card > summary, .card details > summary { cursor: pointer; font-weight: 600; color: var(--brand); }
details[open] > summary { margin-bottom: 1rem; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1.25rem; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1.25rem; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 1rem; margin-bottom: 1.25rem; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.25rem; box-shadow: var(--shadow); display: flex; flex-direction: column; justify-content: center; }
.kpi span { color: var(--muted); font-size: .85rem; }
.kpi strong { font-size: 1.5rem; font-variant-numeric: tabular-nums; }
.kpi small { color: var(--muted); }
.kpi.in { border-left: 4px solid var(--in); }
.kpi.out { border-left: 4px solid var(--out); }
.kpis.small .kpi strong { font-size: 1.2rem; }
.sticky-kpis { position: sticky; bottom: 0; background: var(--bg); padding: .75rem 0; }

.btn { display: inline-block; background: var(--brand); color: var(--brand-ink); border: 0; padding: .55rem 1rem; border-radius: 8px; font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; }
.btn:hover { filter: brightness(1.08); text-decoration: none; }
.btn.ghost { background: transparent; color: var(--brand); border: 1px solid var(--line); }
.btn.in { background: var(--in); }
.btn.out { background: var(--out); }
.btn:disabled { opacity: .6; cursor: wait; }
.btn-icon { background: none; border: 1px solid var(--line); border-radius: 6px; width: 30px; height: 30px; display: inline-grid; place-items: center; cursor: pointer; color: var(--ink); font-size: .9rem; }
.btn-icon:hover { background: var(--bg); text-decoration: none; }
.btn-icon.danger:hover { color: var(--out); border-color: var(--out); }
.link { background: none; border: 0; color: var(--brand); cursor: pointer; font: inherit; padding: 0; }
.inline { display: inline; }

.table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table th, .table td { padding: .55rem .6rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.table th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
.table tfoot th { color: var(--ink); font-size: .9rem; text-transform: none; border-top: 2px solid var(--line); }
.table .num { text-align: right; white-space: nowrap; }
.table.compact td, .table.compact th { padding: .35rem .5rem; }
.table tr.disabled { opacity: .55; }
.row-actions { white-space: nowrap; text-align: right; }

.form label { display: flex; flex-direction: column; gap: .3rem; font-size: .88rem; color: var(--muted); }
.form label.check { flex-direction: row; align-items: center; gap: .5rem; color: var(--ink); }
input, select, textarea { font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: .5rem .65rem; width: 100%; }
input[type=checkbox], input[type=radio] { width: auto; accent-color: var(--brand); }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; }
.grid-form { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .9rem 1rem; }
.span-2 { grid-column: span 2; }
.span-3 { grid-column: 1 / -1; }
.form-actions { display: flex; gap: .5rem; align-items: center; margin-top: .5rem; }
.form > label + label, .form > label + button { margin-top: .8rem; }

.filters { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin-bottom: 1rem; }
.filters select, .filters input { width: auto; }
.filters label { flex-direction: row; align-items: center; display: flex; gap: .5rem; }
.chip { border: 1px solid var(--line); background: var(--surface); color: var(--ink); border-radius: 999px; padding: .3rem .8rem; font: inherit; font-size: .88rem; cursor: pointer; }
.chip:hover { text-decoration: none; border-color: var(--brand); }
.chip.on { background: var(--brand); color: #fff; border-color: var(--brand); }

.flash { padding: .75rem 1rem; border-radius: 8px; margin-bottom: 1rem; }
.flash-success { background: var(--in-bg); color: var(--in); }
.flash-error { background: var(--out-bg); color: var(--out); }

.badge { font-size: .75rem; padding: .15rem .55rem; border-radius: 999px; background: var(--bg); border: 1px solid var(--line); white-space: nowrap; }
.role-superadmin { background: var(--brand); color: #fff; border-color: var(--brand); }
.role-admin { background: #1d4ed8; color: #fff; border-color: #1d4ed8; }
.status, .tier { font-size: .8rem; padding: .1rem .5rem; border-radius: 6px; background: var(--bg); white-space: nowrap; }
.sp-pagato, .st-svolta, .st-confermata, .sp-confermato { background: var(--in-bg); color: var(--in); }
.sp-rifiutato, .st-annullata { background: var(--out-bg); color: var(--out); }
.tier-oro { background: #fef3c7; color: #92400e; }
.tier-argento { background: #e5e7eb; color: #374151; }
.tier-bronzo { background: #fde7d6; color: #9a3412; }
.status-btn { font: inherit; font-size: .8rem; border: 0; border-radius: 6px; padding: .15rem .5rem; cursor: pointer; white-space: nowrap; }
.status-btn.ok { background: var(--in-bg); color: var(--in); }
.status-btn.todo { background: var(--out-bg); color: var(--out); }

.bar { height: 8px; background: var(--bg); border-radius: 99px; overflow: hidden; flex: 1; }
.bar span { display: block; height: 100%; background: var(--brand); border-radius: 99px; }
.bar.in span { background: var(--in); }
.bar.out span { background: var(--out); }
.bar-row { display: grid; grid-template-columns: minmax(110px, 38%) 1fr auto; gap: .75rem; align-items: center; padding: .3rem 0; font-size: .9rem; }
.bar-row b { font-variant-numeric: tabular-nums; min-width: 90px; text-align: right; }

.lvl { font-size: .8rem; padding: .1rem .5rem; border-radius: 6px; }
.lvl-2 { color: var(--in); }
.lvl-1 { color: #2563eb; }
.lvl-0 { color: var(--muted); opacity: .45; }
.perm-dots .dot { display: inline-block; width: 22px; text-align: center; }
.perm-table td, .perm-table th { padding: .45rem; }

.forecast-table input { width: 110px; text-align: right; padding: .3rem .4rem; }
.forecast-table input.pct { width: 80px; }
.forecast-table td:last-child input { width: 100%; min-width: 160px; text-align: left; }

.login-card { width: 100%; max-width: 420px; }
.login-brand { display: flex; gap: 1rem; align-items: center; margin-bottom: 1rem; }
.login-brand p { margin: 0; }

.presets { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: 1rem; }
.chat { display: flex; flex-direction: column; gap: 1rem; }
.msg { max-width: 85%; padding: .8rem 1rem; border-radius: 12px; }
.msg .who { font-size: .78rem; color: var(--muted); margin-bottom: .25rem; }
.msg-user { align-self: flex-end; background: var(--brand); color: #fff; }
.msg-user .who { color: rgba(255,255,255,.8); }
.msg-ai { align-self: flex-start; background: var(--bg); border: 1px solid var(--line); }
.msg-ai .body p:first-child { margin-top: 0; }
.msg-ai .body table { margin: .5rem 0; }
.ask-form textarea { resize: vertical; }

@media (max-width: 1000px) {
    .grid-3 { grid-template-columns: 1fr; }
    .grid-2 { grid-template-columns: 1fr; }
    .grid-form { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 760px) {
    .sidebar { position: fixed; left: -260px; z-index: 20; transition: left .2s; }
    body.nav-open .sidebar { left: 0; box-shadow: 0 0 40px rgba(0,0,0,.4); }
    .menu-btn { display: block; }
    .topbar { padding: .5rem 1rem; flex-wrap: wrap; }
    .event-switch label, .user-menu .badge { display: none; }
    .content { padding: 1rem; }
    .grid-form { grid-template-columns: 1fr; }
    .span-2 { grid-column: auto; }
    .msg { max-width: 100%; }
    h1 { font-size: 1.3rem; }
}
@media print {
    .sidebar, .topbar, .actions, .filters, details, .row-actions, .btn { display: none !important; }
    .card { box-shadow: none; break-inside: avoid; }
    body { background: #fff; }
}
