@font-face {
    font-family: 'Inter';
    src: url('../fonts/InterVariable.woff2') format('woff2');
    font-weight: 100 900;
    font-style: normal;
    font-display: swap;
}

:root {
    --font: 'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif;
    --bg: #f3f5f9;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --line: #e5e9f0;
    --line-strong: #cdd5e1;
    --text: #0f172a;
    --text-2: #334155;
    --muted: #64748b;
    --brand: #2449c7;
    --brand-hover: #1d3ba6;
    --brand-50: #eef2fd;
    --brand-100: #dce4fb;
    --ok: #15803d;
    --ok-50: #ecfdf3;
    --ok-100: #c9f2d8;
    --ko: #c42b20;
    --ko-50: #fef3f2;
    --ko-100: #fbd5d0;
    --warn: #b45309;
    --warn-50: #fff7eb;
    --warn-100: #fde3bf;
    --radius-sm: 8px;
    --radius: 12px;
    --radius-lg: 16px;
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, .05);
    --shadow: 0 1px 2px rgba(15, 23, 42, .04), 0 4px 16px -6px rgba(15, 23, 42, .10);
    --shadow-lg: 0 2px 4px rgba(15, 23, 42, .04), 0 18px 40px -12px rgba(15, 23, 42, .22);
    --ring: 0 0 0 3px rgba(36, 73, 199, .18);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0; min-height: 100vh;
    font-family: var(--font); font-size: 14.5px; line-height: 1.5;
    font-feature-settings: 'cv11', 'ss01';
    background: var(--bg); color: var(--text);
    -webkit-font-smoothing: antialiased;
}
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 2px; }
h1 { font-size: 26px; line-height: 1.2; font-weight: 700; letter-spacing: -.022em; margin: 0; }
h2 { font-size: 16px; line-height: 1.35; font-weight: 650; letter-spacing: -.01em; margin: 0 0 4px; }
p { margin: 0 0 12px; }
ul { margin: 0 0 12px; padding-left: 20px; }
strong { font-weight: 650; }
.muted { color: var(--muted); }
.opt { color: var(--muted); font-weight: 450; }
[hidden] { display: none !important; }
.ico { width: 18px; height: 18px; flex: none; }
.ico-flip { transform: scaleX(-1); }

/* Barra in alto */
.topbar {
    position: sticky; top: 0; z-index: 30;
    background: rgba(255, 255, 255, .92); backdrop-filter: saturate(1.6) blur(10px);
    border-bottom: 1px solid var(--line);
}
.topbar-in { max-width: 1440px; margin: 0 auto; height: 62px; padding: 0 24px; display: flex; align-items: center; gap: 28px; }
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); font-weight: 700; font-size: 17px; letter-spacing: -.02em; }
.brand:hover { text-decoration: none; }
.brand img { width: 32px; height: 32px; border-radius: 9px; box-shadow: 0 4px 10px -4px rgba(36, 73, 199, .55); }
.nav { display: flex; gap: 2px; flex: 1; min-width: 0; overflow-x: auto; scrollbar-width: none; }
.nav a {
    display: flex; align-items: center; gap: 8px; white-space: nowrap;
    color: var(--text-2); font-weight: 550; padding: 8px 12px; border-radius: 9px;
}
.nav a .ico { color: var(--muted); }
.nav a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.nav a.is-on { background: var(--brand-50); color: var(--brand); }
.nav a.is-on .ico { color: var(--brand); }
.user { display: flex; align-items: center; gap: 10px; margin: 0 0 0 auto; }
.avatar {
    width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; flex: none;
    background: linear-gradient(135deg, #4069f0, #1f3fb4); color: #fff; font-weight: 650; font-size: 13px; letter-spacing: .02em;
}
.user-name { display: flex; flex-direction: column; line-height: 1.2; font-weight: 600; font-size: 13.5px; color: var(--text); white-space: nowrap; }
.user-name small { color: var(--muted); font-weight: 500; font-size: 12px; }

/* Pagina */
.page { max-width: 1440px; margin: 0 auto; padding: 28px 24px 56px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head p { margin: 6px 0 0; }
.page-head > div:last-child:not(:first-child) { display: flex; gap: 10px; align-items: center; }
.eyebrow { display: block; font-size: 12.5px; font-weight: 600; color: var(--brand); text-transform: uppercase; letter-spacing: .06em; margin-bottom: 6px; }

.card {
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-lg);
    padding: 22px 24px; margin-bottom: 18px; box-shadow: var(--shadow-sm);
}
.card > h2 + p.muted, .card > h2 + .muted { margin-top: 2px; }
.card > h2 { font-size: 17px; }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 16px; }
.card-head h2 { margin: 0; }
.card-head p { margin: 4px 0 0; }
.card.highlight { border-color: var(--brand); box-shadow: var(--ring), var(--shadow); }
.card-flush { padding: 0; overflow: hidden; }
.card-flush > .card-head { padding: 20px 24px 0; }
.card-foot { border-top: 1px solid var(--line); padding: 12px 24px; background: var(--surface-2); }
.code-big {
    display: inline-block; font-family: ui-monospace, 'Cascadia Mono', Consolas, monospace; font-size: 40px; font-weight: 700;
    letter-spacing: .14em; margin: 8px 0 14px; padding: 10px 20px; background: var(--brand-50); color: var(--brand); border-radius: var(--radius);
}

/* Home: riquadri degli stabilimenti */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 18px; margin-bottom: 18px; }
.tile {
    position: relative; overflow: hidden;
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-lg); padding: 22px 24px 18px;
    box-shadow: var(--shadow-sm);
}
.tile-head { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
.tile-head h2 { margin: 0; font-size: 17px; }
.tile-head small { display: block; color: var(--muted); font-size: 12.5px; font-weight: 500; }
.tile-ico { width: 40px; height: 40px; border-radius: 11px; display: grid; place-items: center; background: var(--brand-50); color: var(--brand); flex: none; }
.tile-ico .ico { width: 21px; height: 21px; }
.tile-nums { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.tile-num { background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.tile-num strong { display: block; font-size: 34px; line-height: 1.1; font-weight: 700; letter-spacing: -.03em; font-variant-numeric: tabular-nums; }
.tile-num span { color: var(--muted); font-size: 13px; font-weight: 500; }
.tile-num.is-live strong { color: var(--ok); }
.tile-links { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--line); }

/* Campi */
input, select, textarea {
    font: inherit; font-size: 14.5px; color: var(--text);
    width: 100%; min-height: 40px; padding: 8px 12px;
    border: 1px solid var(--line-strong); border-radius: 10px; background: var(--surface);
    box-shadow: var(--shadow-sm); transition: border-color .15s, box-shadow .15s;
}
input::placeholder { color: #94a3b8; }
input:hover, select:hover, textarea:hover { border-color: #b4bfcf; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--brand); box-shadow: var(--ring); }
select {
    appearance: none; padding-right: 36px; cursor: pointer;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat; background-position: right 10px center; background-size: 18px;
}
textarea { font-family: ui-monospace, 'Cascadia Mono', Consolas, monospace; font-size: 13px; line-height: 1.55; padding: 12px 14px; resize: vertical; }
input[type="checkbox"] { width: 18px; height: 18px; min-height: 0; padding: 0; accent-color: var(--brand); box-shadow: none; flex: none; cursor: pointer; }
label { display: block; font-weight: 600; font-size: 13px; color: var(--text-2); }
label > input, label > select, label > textarea { display: block; margin-top: 6px; }
label.check {
    display: flex; align-items: flex-start; gap: 12px; font-weight: 450; font-size: 14.5px; color: var(--text);
    padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface-2); cursor: pointer;
}
label.check input { margin-top: 2px; display: inline-block; }
.input-code { font-family: ui-monospace, 'Cascadia Mono', Consolas, monospace; font-size: 24px; letter-spacing: .14em; text-transform: uppercase; text-align: center; min-height: 56px; }

.stack { display: flex; flex-direction: column; gap: 14px; }
.grid-form { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px 18px; }
.grid-form .wide { grid-column: 1 / -1; }
.doc-form { display: grid; grid-template-columns: 1fr 1fr; gap: 16px 18px; }
.doc-form .wide { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 10px; align-items: center; padding-top: 4px; }
.filters {
    display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap;
    margin: 0 0 18px; padding: 14px; background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius);
}
.filters label { min-width: 140px; flex: 1 1 150px; max-width: 240px; }
.filters label.grow { flex: 2 1 240px; max-width: none; }
.filters-add { margin: 18px 0 0; }
.inline { display: inline; }

/* Pulsanti */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    min-height: 40px; padding: 8px 16px; border-radius: 10px; border: 1px solid transparent;
    font: inherit; font-size: 14px; font-weight: 600; line-height: 1.2; cursor: pointer; white-space: nowrap;
    transition: background-color .15s, border-color .15s, box-shadow .15s, color .15s;
}
.btn:hover { text-decoration: none; }
.btn:focus-visible { outline: none; box-shadow: var(--ring); }
.btn .ico { width: 17px; height: 17px; }
.btn-primary { background: var(--brand); color: #fff; box-shadow: 0 1px 2px rgba(15, 23, 42, .12), inset 0 1px 0 rgba(255, 255, 255, .12); }
.btn-primary:hover { background: var(--brand-hover); }
.btn-outline { background: var(--surface); color: var(--text-2); border-color: var(--line-strong); box-shadow: var(--shadow-sm); }
.btn-outline:hover { background: var(--surface-2); border-color: #b4bfcf; color: var(--text); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-danger { background: var(--surface); color: var(--ko); border-color: var(--ko-100); }
.btn-danger:hover { background: var(--ko-50); border-color: #f3b2a9; }
.btn-small { min-height: 32px; padding: 5px 11px; font-size: 13px; border-radius: 8px; }
.btn-icon { min-height: 38px; width: 38px; padding: 0; }
.btn-wide { width: 100%; min-height: 46px; font-size: 15px; }

/* Avvisi */
.alert {
    display: flex; align-items: flex-start; gap: 10px;
    padding: 12px 14px; border-radius: var(--radius); margin-bottom: 16px; border: 1px solid; font-weight: 500;
}
.alert .ico { margin-top: 1px; }
.alert-ok { background: var(--ok-50); border-color: var(--ok-100); color: #14532d; }
.alert-ok .ico { color: var(--ok); }
.alert-err { background: var(--ko-50); border-color: var(--ko-100); color: #7f1d1d; }
.alert-err .ico { color: var(--ko); }

/* Tabelle */
.table-wrap { overflow-x: auto; margin: 0 -24px; padding: 0 24px; }
.table { width: 100%; border-collapse: separate; border-spacing: 0; font-variant-numeric: tabular-nums; }
.table th {
    text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); letter-spacing: .02em;
    padding: 10px 12px; white-space: nowrap; background: var(--surface-2);
    border-top: 1px solid var(--line); border-bottom: 1px solid var(--line);
}
.table th:first-child { border-left: 1px solid var(--line); border-top-left-radius: 10px; border-bottom-left-radius: 10px; }
.table th:last-child { border-right: 1px solid var(--line); border-top-right-radius: 10px; border-bottom-right-radius: 10px; }
.table td { padding: 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover td { background: #fafbfd; }
.table-dense td { padding: 9px 10px; font-size: 13.5px; }
.table-dense th { padding: 9px 10px; }
.table tr.row-in td:first-child { box-shadow: inset 3px 0 0 var(--ok); }
.table td small { display: block; }
.cell-strong { font-weight: 600; }
.cell-mono { font-family: ui-monospace, 'Cascadia Mono', Consolas, monospace; font-size: 12.5px; }
.actions { display: flex; gap: 6px; justify-content: flex-end; }
.actions form { margin: 0; }
img.sig { height: 30px; max-width: 90px; background: #fff; border: 1px solid var(--line); border-radius: 6px; }
.table-foot { margin: 14px 0 0; color: var(--muted); font-size: 13px; }

.badge {
    display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px; border-radius: 999px;
    font-size: 12px; font-weight: 600; line-height: 1.35; white-space: nowrap;
    background: #eef1f5; color: var(--text-2); border: 1px solid transparent;
}
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .8; }
.badge-ok { background: var(--ok-50); color: var(--ok); border-color: var(--ok-100); }
.badge-wait { background: var(--brand-50); color: var(--brand); border-color: var(--brand-100); }
.badge-warn { background: var(--warn-50); color: var(--warn); border-color: var(--warn-100); }
.badge-muted { background: #f1f5f9; color: #94a3b8; }

.empty {
    display: flex; flex-direction: column; align-items: center; text-align: center; gap: 6px;
    padding: 34px 20px; color: var(--muted); border: 1px dashed var(--line-strong); border-radius: var(--radius); background: var(--surface-2);
}
.empty .ico { width: 28px; height: 28px; color: #94a3b8; margin-bottom: 4px; }
.empty strong { color: var(--text-2); }
.empty p { margin: 0; max-width: 56ch; }

.kv { list-style: none; padding: 0; margin: 12px 0 0; display: grid; gap: 8px; }
.kv li { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface-2); }
.kv li .ico { color: var(--muted); }
.kv li span { margin-left: auto; }

/* Ricerca del referente */
.field { display: flex; flex-direction: column; gap: 6px; position: relative; }
.field label { font-weight: 600; font-size: 13px; color: var(--text-2); }
.ref-list {
    position: absolute; top: calc(100% + 6px); left: 0; right: 0; z-index: 15; padding: 6px;
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow-lg);
    max-height: 300px; overflow-y: auto;
}
.ref-list button {
    display: flex; align-items: baseline; gap: 8px; width: 100%; text-align: left; border: 0; border-radius: 8px;
    background: transparent; padding: 9px 12px; cursor: pointer; font: inherit; font-weight: 550; color: var(--text);
}
.ref-list button:hover, .ref-list button:focus-visible { background: var(--brand-50); outline: none; }
.ref-list button:disabled { color: var(--muted); cursor: default; background: none; }
.ref-list small { color: var(--muted); font-weight: 450; }

.check-col { width: 76px; text-align: center; }
.box { display: inline-block; width: 18px; height: 18px; border: 1.5px solid var(--line-strong); border-radius: 5px; vertical-align: middle; }

/* Pagine senza login: accesso e abbinamento */
body.guest {
    background:
        radial-gradient(1200px 600px at 15% -10%, rgba(64, 105, 240, .16), transparent 60%),
        radial-gradient(900px 500px at 100% 110%, rgba(31, 63, 180, .12), transparent 60%),
        var(--bg);
}
body.guest .topbar { background: transparent; border-bottom: 0; backdrop-filter: none; position: static; }
body.guest .page { min-height: calc(100vh - 62px); display: flex; flex-direction: column; align-items: center; justify-content: center; padding-top: 12px; }
.auth-card {
    width: 100%; max-width: 440px; background: var(--surface); border: 1px solid var(--line); border-radius: 20px;
    padding: 34px 34px 30px; box-shadow: var(--shadow-lg);
}
.auth-card .auth-ico { width: 48px; height: 48px; border-radius: 14px; display: grid; place-items: center; background: var(--brand-50); color: var(--brand); margin-bottom: 18px; }
.auth-card .auth-ico .ico { width: 24px; height: 24px; }
.auth-card .auth-ico.is-ok { background: var(--ok-50); color: var(--ok); }
.auth-card h1 { font-size: 24px; margin-bottom: 8px; }
.auth-card > p { color: var(--muted); }
.auth-card form { margin-top: 18px; }
.auth-note { margin: 20px 0 0; padding-top: 16px; border-top: 1px solid var(--line); font-size: 13px; color: var(--muted); }
.auth-foot { margin-top: 22px; font-size: 12.5px; color: var(--muted); text-align: center; }

@media (max-width: 1100px) {
    .user-name { display: none; }
}
@media (max-width: 860px) {
    .topbar-in { gap: 14px; padding: 0 16px; }
    .brand span { display: none; }
    .nav a span { display: none; }
    .nav a { padding: 9px; }
    .page { padding: 20px 16px 40px; }
    .grid-form, .doc-form { grid-template-columns: 1fr; }
    .card { padding: 18px; }
    .table-wrap { margin: 0 -18px; padding: 0 18px; }
}

@media print {
    .topbar, .no-print, .alert { display: none !important; }
    body { background: #fff; font-size: 12px; }
    .page { padding: 0; max-width: none; }
    .card { box-shadow: none; border: 0; padding: 0; margin-bottom: 18px; }
    .print-block { break-inside: avoid; }
    .table th { background: #eee !important; color: #000; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    .table tbody tr:hover td { background: none; }
}
