*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
    --bg: #f6f6f7;
    --bg-card: #ffffff;
    --bg-nav: #ffffff;
    --bg-hover: #f1f1f3;
    --border: #d6d6db;
    --border-strong: #bfbfc6;
    --text: #101012;
    --text-muted: #63636b;
    --text-dim: #6e6e76;
    --primary: #101012;
    --primary-fg: #ffffff;
    --accent: #101012;
    --danger: #c0392b;
    --danger-bg: #fdecea;
    --danger-border: #f5c6c3;
    --success: #1e7e34;
    --success-bg: #eafaea;
    --success-border: #b2dfb2;
    --warning: #8a5c0a;
    --warning-bg: #fef8e7;
    --warning-border: #f5dfa8;
    --info: #2040a0;
    --info-bg: #f0f4ff;
    --info-border: #c0d0ff;
    --font: 'Montserrat', -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
    --radius: 10px;
    --radius-sm: 8px;
    --nav-h: 64px;
    --shadow-sm: 0 1px 2px rgba(16,16,18,.05), 0 1px 1px rgba(16,16,18,.04);
    --shadow-md: 0 10px 30px rgba(16,16,18,.08);
    --focus-ring: rgba(16,16,18,.12);
    --input-h: 42px;
    color-scheme: light;
}

:root[data-theme="dark"] {
    --bg: #0b0b0d;
    --bg-card: #1a1a1d;
    --bg-nav: #0b0b0d;
    --bg-hover: #1e1e21;
    --border: rgba(255,255,255,.16);
    --border-strong: rgba(255,255,255,.22);
    --text: #f2f2f4;
    --text-muted: #a3a3ab;
    --text-dim: #8c8c94;
    --primary: #ffffff;
    --primary-fg: #0b0b0d;
    --accent: #ffffff;
    --danger: #ff7a6b;
    --danger-bg: rgba(255,107,92,.12);
    --danger-border: rgba(255,107,92,.3);
    --success: #52c45a;
    --success-bg: rgba(76,175,80,.14);
    --success-border: rgba(76,175,80,.35);
    --warning: #e5b45c;
    --warning-bg: rgba(224,172,74,.12);
    --warning-border: rgba(224,172,74,.32);
    --info: #7d9aff;
    --info-bg: rgba(64,96,192,.16);
    --info-border: rgba(64,96,192,.4);
    --shadow-sm: 0 1px 2px rgba(0,0,0,.4);
    --shadow-md: 0 10px 30px rgba(0,0,0,.5);
    --focus-ring: rgba(255,255,255,.16);
    color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #0b0b0d;
        --bg-card: #1a1a1d;
        --bg-nav: #0b0b0d;
        --bg-hover: #1e1e21;
        --border: rgba(255,255,255,.16);
        --border-strong: rgba(255,255,255,.22);
        --text: #f2f2f4;
        --text-muted: #a3a3ab;
        --text-dim: #8c8c94;
        --primary: #ffffff;
        --primary-fg: #0b0b0d;
        --accent: #ffffff;
        --danger: #ff7a6b;
        --danger-bg: rgba(255,107,92,.12);
        --danger-border: rgba(255,107,92,.3);
        --success: #52c45a;
        --success-bg: rgba(76,175,80,.14);
        --success-border: rgba(76,175,80,.35);
        --warning: #e5b45c;
        --warning-bg: rgba(224,172,74,.12);
        --warning-border: rgba(224,172,74,.32);
        --info: #7d9aff;
        --info-bg: rgba(64,96,192,.16);
        --info-border: rgba(64,96,192,.4);
        --shadow-sm: 0 1px 2px rgba(0,0,0,.4);
        --shadow-md: 0 10px 30px rgba(0,0,0,.5);
        --focus-ring: rgba(255,255,255,.16);
        color-scheme: dark;
    }
}

html { font-size: 15px; }
body { font-family: var(--font); background: var(--bg); color: var(--text); line-height: 1.6; transition: background-color .15s ease, color .15s ease; }
a { color: var(--text); }

/* ── Theme toggle ── */
.theme-toggle {
    display: inline-flex; align-items: center; justify-content: center;
    width: 36px; height: 36px; border-radius: 50%;
    border: 1px solid var(--border-strong); background: transparent; color: var(--text);
    cursor: pointer; transition: background-color .15s, border-color .15s, transform .15s;
    flex-shrink: 0;
}
.theme-toggle:hover { background: var(--bg-hover); transform: rotate(12deg); }
.theme-toggle:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--focus-ring); }
.theme-icon { width: 18px; height: 18px; display: block; }
.theme-icon-sun { display: none; }
:root[data-theme="dark"] .theme-icon-moon { display: none; }
:root[data-theme="dark"] .theme-icon-sun { display: block; }
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .theme-icon-moon { display: none; }
    :root:not([data-theme="light"]) .theme-icon-sun { display: block; }
}
.theme-toggle-fixed { position: fixed; top: 16px; right: 16px; z-index: 200; background: var(--bg-card); box-shadow: var(--shadow-sm); }

/* ── Theme-aware logo swap ── */
.logo-light { display: inline-block; }
.logo-dark { display: none; }
:root[data-theme="dark"] .logo-light { display: none; }
:root[data-theme="dark"] .logo-dark { display: inline-block; }
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .logo-light { display: none; }
    :root:not([data-theme="light"]) .logo-dark { display: inline-block; }
}

/* ── Admin Nav ── */
.admin-nav {
    height: var(--nav-h);
    background: var(--bg-nav);
    color: var(--text);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    position: sticky;
    top: 0;
    z-index: 100;
    border-bottom: 1px solid var(--border);
}
.nav-brand { display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; font-weight: 700; font-size: 15px; }
.nav-brand img { height: 24px; }
.nav-links { display: flex; align-items: center; gap: 22px; }
.nav-links a { color: var(--text-muted); text-decoration: none; font-size: 14px; font-weight: 500; transition: color .15s; border-radius: 4px; }
.nav-links a:hover, .nav-links a.active { color: var(--text); }
.nav-links a:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--focus-ring); }
.nav-logout { color: var(--text-dim) !important; font-size: 13px; }

/* ── Admin Layout ── */
.admin-page { min-height: 100vh; }
.admin-main { max-width: 1100px; margin: 0 auto; padding: 32px 24px; }
.admin-header { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 28px; flex-wrap: wrap; gap: 12px; }
.admin-header h1 { font-size: 22px; font-weight: 700; letter-spacing: -.01em; }

/* ── Buttons ── */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    height: 42px; padding: 0 20px; font-size: 13px; font-weight: 600;
    border: 1px solid transparent; border-radius: var(--radius-sm);
    cursor: pointer; text-decoration: none;
    transition: background-color .15s, border-color .15s, color .15s, transform .15s, box-shadow .15s, opacity .15s;
    font-family: var(--font); white-space: nowrap;
    text-transform: uppercase; letter-spacing: .04em;
}
.btn:hover { transform: translateY(-1px); box-shadow: var(--shadow-sm); }
.btn:active { transform: translateY(0); box-shadow: none; }
.btn:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--focus-ring); }
.btn-primary { background: var(--primary); color: var(--primary-fg); border-color: var(--primary); }
.btn-primary:hover { opacity: .88; }
.btn-outline { background: transparent; color: var(--text); border-color: var(--border-strong); }
.btn-outline:hover { border-color: var(--text); background: var(--bg-hover); }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: var(--bg-card); }
.btn-sm { height: 32px; padding: 0 14px; font-size: 11px; letter-spacing: .03em; }
.btn-full { width: 100%; }
.btn-link { background: none; border: none; height: auto; color: var(--text-muted); cursor: pointer; font-size: 13px; padding: 0; text-decoration: underline; text-transform: none; letter-spacing: 0; font-weight: 500; }
.btn-link:hover { color: var(--text); transform: none; box-shadow: none; }
.btn-link:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--focus-ring); border-radius: 3px; }

/* ── Forms ── */
.form-section { background: var(--bg-card); border: 1px solid var(--border-strong); border-radius: var(--radius); padding: 24px; margin-bottom: 20px; box-shadow: var(--shadow-sm); }

/* Shared "card eyebrow" header - identical across form sections and detail
   cards (.detail-card h3), so the same kind of label always looks the same. */
.section-title, .detail-card h3 {
    font-size: 13px; font-weight: 700; margin-bottom: 14px; padding-bottom: 10px;
    border-bottom: 1px solid var(--border); text-transform: uppercase;
    letter-spacing: .06em; color: var(--text-muted);
}
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px 20px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field-full { grid-column: 1 / -1; }
.field label { font-size: 13px; font-weight: 600; color: var(--text-muted); }

/* Base styling applies to every input/select/textarea in the app, wrapped in
   .field or not (e.g. bare tranche-row inputs), so nothing ever falls back
   to raw browser chrome. */
input, select, textarea {
    width: 100%; min-height: var(--input-h);
    padding: 9px 14px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
    font-size: 14px; font-family: var(--font); color: var(--text); background: var(--bg-card);
    line-height: 1.4;
    transition: border-color .15s, box-shadow .15s, background-color .15s;
}
select { appearance: none; -webkit-appearance: none; -moz-appearance: none; }
textarea { min-height: 0; width: 100%; resize: vertical; }
input::placeholder, textarea::placeholder { color: var(--text-dim); opacity: 1; }
input:hover, select:hover, textarea:hover { border-color: var(--accent); }
input:focus, select:focus, textarea:focus {
    outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--focus-ring);
}

/* Custom select chevron (light + dark variants - data-URIs can't read CSS vars) */
select {
    padding-right: 38px;
    text-overflow: ellipsis; white-space: nowrap; overflow: hidden;
    background-repeat: no-repeat;
    background-position: right 12px center;
    background-size: 11px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2363636b' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
}
:root[data-theme="dark"] select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23a3a3ab' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
}
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) select {
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23a3a3ab' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    }
}

/* ── Custom dropdown (custom-select.js) ──
   Progressively replaces the rule above: when the script runs it wraps each
   <select> in this markup so the OPEN option list is fully ours, not the
   browser/OS native popup. The native <select> above still handles no-JS
   fallback and screen-reader-hostile edge cases where JS fails to load. */
.cs { position: relative; width: 100%; }
.cs-native {
    position: absolute !important; width: 1px !important; height: 1px !important;
    padding: 0 !important; margin: -1px !important; overflow: hidden !important;
    clip: rect(0,0,0,0) !important; white-space: nowrap !important; border: 0 !important;
    opacity: 0; pointer-events: none;
}
.cs-trigger {
    width: 100%; min-height: var(--input-h);
    display: flex; align-items: center; justify-content: space-between; gap: 8px;
    padding: 9px 14px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
    background: var(--bg-card); color: var(--text); font-size: 14px; font-family: var(--font);
    cursor: pointer; text-align: left;
    transition: border-color .15s, box-shadow .15s;
}
.cs-trigger:disabled { cursor: not-allowed; opacity: .6; }
.cs-trigger-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cs-chevron { flex-shrink: 0; width: 11px; height: 11px; color: var(--text-muted); transition: transform .15s; }
.cs-chevron svg { display: block; width: 100%; height: 100%; }
.cs.cs-open .cs-chevron { transform: rotate(180deg); }
.cs-trigger:hover { border-color: var(--accent); }
.cs-trigger:focus-visible,
.cs.cs-open .cs-trigger { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--focus-ring); }

.cs-menu {
    position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 60;
    max-height: 260px; overflow-y: auto;
    background: var(--bg-card); border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
    box-shadow: var(--shadow-md);
    list-style: none; margin: 0; padding: 4px;
}
.cs-menu.cs-menu-up { top: auto; bottom: calc(100% + 4px); }
.cs-option {
    padding: 8px 10px; border-radius: 6px; font-size: 14px; color: var(--text);
    cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.cs-option:hover, .cs-option.cs-active { background: var(--bg-hover); }
.cs-option[aria-selected="true"] { font-weight: 600; }
.cs-option[aria-disabled="true"] { color: var(--text-dim); cursor: not-allowed; }

/* Number input spinners: the native up/down arrows render wildly differently
   per browser (and Firefox can't be restyled at all) - hidden everywhere for
   one guaranteed-consistent look. Typing is the primary input method for
   money/percentage/day-count fields anyway. */
input[type="number"] { -moz-appearance: textfield; }
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none; margin: 0;
}
input[type="date"]::-webkit-calendar-picker-indicator { cursor: pointer; opacity: .65; }
input[type="date"]::-webkit-calendar-picker-indicator:hover { opacity: 1; }

.req { color: var(--danger); }
.form-actions { display: flex; gap: 12px; justify-content: flex-end; margin-top: 8px; }

/* Tranche rows */
.tranche-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.tranche-header label { font-size: 13px; font-weight: 600; color: var(--text-muted); }
.tranche-row { display: grid; grid-template-columns: 130px 1fr 1fr auto; gap: 8px; margin-bottom: 8px; align-items: center; }

/* "+ Aggiungi tranche" - a repeated, low-emphasis utility action, not a
   primary/secondary decision like the rest of the .btn family, so it
   deliberately doesn't borrow their bordered/uppercase treatment. */
.btn-add {
    display: inline-flex; align-items: center; gap: 5px;
    background: none; border: none; height: auto; padding: 4px 2px;
    color: var(--text-muted); font-size: 13px; font-weight: 600; font-family: var(--font);
    text-transform: none; letter-spacing: normal; cursor: pointer;
    border-radius: 4px; transition: color .15s;
}
.btn-add:hover { color: var(--accent); }
.btn-add:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--focus-ring); }
.btn-add::before { content: '+'; font-size: 15px; line-height: 1; }

/* ── Alerts ── */
.alert { padding: 12px 16px; border-radius: var(--radius-sm); margin-bottom: 20px; font-size: 14px; border: 1px solid; }
.alert-success { background: var(--success-bg); color: var(--success); border-color: var(--success-border); }
.alert-error   { background: var(--danger-bg); color: var(--danger); border-color: var(--danger-border); }
.alert-warning { background: var(--warning-bg); color: var(--warning); border-color: var(--warning-border); }
.alert-info    { background: var(--info-bg); color: var(--info); border-color: var(--info-border); }

/* ── Badge ── */
.badge { display: inline-block; padding: 4px 11px; border-radius: 20px; font-size: 11px; font-weight: 700; color: #fff; text-transform: uppercase; letter-spacing: .04em; }

/* ── Table ── */
.table-wrap { background: var(--bg-card); border: 1px solid var(--border-strong); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow-sm); }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
thead { background: var(--bg-hover); }
th { text-align: left; padding: 12px 16px; font-size: 11px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; letter-spacing: .06em; border-bottom: 1px solid var(--border); }
td { padding: 13px 16px; border-bottom: 1px solid var(--border); color: var(--text); }
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--bg-hover); }

/* ── Detail grid ── */
.detail-grid { display: grid; grid-template-columns: 1fr 340px; gap: 20px; align-items: start; }
.detail-card { background: var(--bg-card); border: 1px solid var(--border-strong); border-radius: var(--radius); padding: 20px; margin-bottom: 16px; box-shadow: var(--shadow-sm); }
.detail-card-success { border-color: var(--success-border); background: var(--success-bg); }
.detail-right .detail-card { margin-bottom: 12px; }

.info-list { display: grid; grid-template-columns: 110px 1fr; gap: 8px 12px; font-size: 14px; }
.info-list dt { color: var(--text-muted); font-weight: 700; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; padding-top: 2px; }
.info-list dd { color: var(--text); }

.link-box {
    background: var(--bg-card); border: 1px dashed var(--border-strong); border-radius: var(--radius-sm);
    padding: 10px 12px; font-size: 12px; color: var(--text-muted); cursor: pointer; word-break: break-all;
    transition: background .15s, border-color .15s;
}
.link-box:hover { background: var(--bg-hover); border-color: var(--accent); }
.link-box:focus-visible { outline: none; border-style: solid; box-shadow: 0 0 0 3px var(--focus-ring); }
.link-box.copied { background: var(--success-bg); border-color: var(--success-border); color: var(--success); }
.link-box.copied::after { content: ' ✓ Copiato!'; font-weight: 600; }

/* Audit */
.audit-list { font-size: 13px; }
.audit-item { display: flex; justify-content: space-between; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--border); gap: 8px; flex-wrap: wrap; }
.audit-item:last-child { border-bottom: none; }
.audit-action { font-weight: 600; color: var(--text); font-family: monospace; font-size: 12px; }
.audit-time, .audit-ip { font-size: 11px; color: var(--text-dim); }

/* ── Auth ── */
.auth-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--bg); padding: 24px; }
.auth-box { background: var(--bg-card); border: 1px solid var(--border-strong); border-radius: var(--radius); padding: 40px; width: 360px; max-width: 95vw; box-shadow: var(--shadow-md); }
.auth-logo { display: flex; align-items: center; gap: 12px; margin-bottom: 28px; }
.auth-logo img { height: 30px; }
.auth-title { font-size: 17px; font-weight: 700; color: var(--text); }
.auth-form { display: flex; flex-direction: column; gap: 16px; }

/* ── PDF Preview Modal ──
   Always light/paper regardless of page theme - it's a document viewer,
   like .contract-card and .canvas-wrap on the sign page. Fixed (not
   variable-driven) colors so text and buttons stay legible even in dark
   mode, when the surrounding page's own variables would resolve too light. */
.pdf-modal-overlay {
    display: none; position: fixed; inset: 0; z-index: 1000;
    background: rgba(0,0,0,.65);
    align-items: center; justify-content: center;
}
.pdf-modal-overlay.is-open { display: flex; }
.pdf-modal {
    width: min(960px, 96vw); height: 92vh;
    background: #ffffff; border-radius: var(--radius);
    display: flex; flex-direction: column; overflow: hidden;
    box-shadow: 0 8px 40px rgba(0,0,0,.4);
}
.pdf-modal-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 12px 16px; border-bottom: 1px solid #e4e4e8; flex-shrink: 0;
}
.pdf-modal-title { font-weight: 600; font-size: 15px; color: #101012; }
.pdf-modal-actions { display: flex; gap: 8px; }
.pdf-modal-actions .btn-outline { color: #101012; border-color: #d3d3d9; }
.pdf-modal-actions .btn-outline:hover { background: #f1f1f3; border-color: #101012; }
.pdf-modal-actions .btn-danger { color: #c0392b; border-color: #c0392b; }
.pdf-modal-actions .btn-danger:hover { background: #c0392b; color: #ffffff; }
.pdf-modal-actions .btn:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(16,16,18,.15); }
.pdf-modal-frame { flex: 1; border: none; width: 100%; color-scheme: light; }

/* ── Empty state ── */
.empty-state { text-align: center; padding: 60px 20px; color: var(--text-muted); }

/* ── Helpers ── */
.text-muted { color: var(--text-muted); }
.hint-text { color: var(--text-dim); }
.subsection-divider { margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); }

@media (max-width: 900px) {
    .detail-grid { grid-template-columns: 1fr; }
    .field-grid { grid-template-columns: 1fr; }
    /* Stacked, not a 3-column squeeze: guarantees the same shape for every
       row regardless of whether it has a remove button (the first tranche
       row never does), instead of leaving an orphan empty grid cell. */
    .tranche-row { grid-template-columns: 1fr; gap: 6px; }
    .tranche-row .btn-danger { justify-self: end; }
}
