:root {
    /* Palette del logo Color Villa: rosso (#C02828) e bianco, con grigi neutri */
    --bg: #f5f5f6;
    --surface: #ffffff;
    --text: #232326;
    --muted: #6c6c72;
    --line: #e3e3e6;
    --accent: #c02828;
    --accent-2: #9c1f1f;
    --accent-soft: #fbeaea;
    --accent-text: #ffffff;
    --danger: #9c1f1f;
    --ok-bg: #e4f7ec;
    --ok-text: #17603a;
    --err-bg: #fdecec;
    --err-text: #8a1c1c;
    --warn: #b86a00;
    --radius: 12px;
    --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 4px 16px rgba(0, 0, 0, .05);
    --shadow-lg: 0 8px 30px rgba(0, 0, 0, .25);
    --brand-gradient: linear-gradient(135deg, #d22f2f 0%, #b32222 100%);
}

* { box-sizing: border-box; }

html { font-size: 17px; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    line-height: 1.45;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }

h1 { font-size: 1.7rem; margin: 0 0 1rem; letter-spacing: -.02em; }
h2 { font-size: 1.15rem; margin: 0 0 .75rem; letter-spacing: -.01em; }
h3 { font-size: 1rem; margin: 1rem 0 .5rem; }

/* Barra in alto (bianca, per il logo aziendale) */
.topbar {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    padding: .45rem 1.5rem;
    background: #fff;
    color: var(--text);
    border-bottom: 3px solid var(--accent);
    box-shadow: 0 2px 12px rgba(0, 0, 0, .08);
    position: sticky;
    top: 0;
    z-index: 10;
}
.topbar .brand { display: flex; align-items: center; text-decoration: none; }
.topbar .brand img { display: block; height: 46px; width: auto; }
.topbar nav { display: flex; gap: .3rem; flex: 1; flex-wrap: wrap; }
.topbar nav a {
    color: var(--muted);
    text-decoration: none;
    padding: .4rem .9rem;
    border-radius: 999px;
    font-weight: 500;
    transition: background .15s, color .15s;
}
.topbar nav a:hover { background: var(--accent-soft); color: var(--accent); }
.topbar nav a.active { background: var(--accent); color: #fff; font-weight: 600; }
.topbar form { margin: 0; }
.topbar a.account { color: var(--muted); text-decoration: none; padding: .35rem .6rem; border-radius: 999px; }
.topbar a.account:hover, .topbar a.account.active { color: var(--accent); background: var(--accent-soft); }
button.link { background: none; border: 0; color: var(--muted); font: inherit; cursor: pointer; padding: .35rem .6rem; border-radius: 999px; box-shadow: none; }
button.link:hover { color: var(--accent); background: var(--accent-soft); border: 0; }

.page { padding: 1.5rem; max-width: 1500px; margin: 0 auto; }
.page.narrow { max-width: 900px; }

.title-row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.1rem; }
.title-row h1 { margin: 0; }
.actions { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.actions form { margin: 0; }

/* Pulsanti e campi */
button, .button {
    display: inline-block;
    font: inherit;
    font-weight: 500;
    padding: .5rem 1.05rem;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    text-decoration: none;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .05);
    transition: transform .1s, box-shadow .15s, border-color .15s, filter .15s;
}
button:hover, .button:hover { border-color: var(--accent); color: var(--accent); }
button:active, .button:active { transform: translateY(1px); }
button.primary, .button.primary {
    background: var(--brand-gradient);
    border-color: transparent;
    color: var(--accent-text);
    box-shadow: 0 4px 12px rgba(192, 40, 40, .3);
}
button.primary:hover, .button.primary:hover { filter: brightness(1.08); color: #fff; box-shadow: 0 6px 16px rgba(192, 40, 40, .4); }
button.danger { color: var(--danger); border-color: var(--danger); background: transparent; box-shadow: none; }
button.danger:hover { background: var(--danger); color: #fff; }

label { display: flex; flex-direction: column; gap: .3rem; font-size: .85rem; font-weight: 500; color: var(--muted); }
label.radio { flex-direction: row; align-items: center; gap: .5rem; color: var(--text); font-size: 1rem; font-weight: 400; margin: .35rem 0; }
input[type=text], input[type=search], input[type=password], input[type=date], select {
    font: inherit;
    color: var(--text);
    padding: .55rem .75rem;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #fff;
    width: 100%;
    min-width: 0;
    transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(192, 40, 40, .18); }
button:focus-visible, .button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input[readonly] { background: #f1f1f2; }
input[type=radio], input[type=checkbox] { accent-color: var(--accent); }

/* Messaggi */
.alert { padding: .75rem 1rem; border-radius: 10px; margin: 0 0 1rem; border-left: 4px solid transparent; }
.alert ul { margin: .4rem 0 0; padding-left: 1.2rem; }
.alert.success { background: var(--ok-bg); color: var(--ok-text); border-color: #2fb36b; }
.alert.error { background: var(--err-bg); color: var(--err-text); border-color: var(--danger); }
.warn { color: var(--warn); font-weight: 500; }
.hint { color: var(--muted); font-size: .9rem; }
.count { color: var(--muted); margin: 0 0 .5rem; font-size: .9rem; }

/* Filtri */
.filters {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    gap: .85rem;
    align-items: end;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.1rem;
    margin-bottom: 1.1rem;
}
.filters.search .search-field { grid-column: 1 / -1; }
label.check { flex-direction: row; align-items: center; gap: .5rem; color: var(--text); font-size: .95rem; font-weight: 400; white-space: nowrap; }
.filters label.check { padding-bottom: 0; }
.filters-foot { grid-column: 1 / -1; display: flex; justify-content: flex-end; align-items: center; gap: 1.5rem; flex-wrap: wrap; }
td.check, th.check { width: 2.5rem; text-align: center; }
tr.disabled td { color: var(--muted); }

/* Tabelle */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
table { border-collapse: collapse; width: 100%; background: var(--surface); }
th, td { padding: .65rem .85rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
thead th {
    background: var(--accent);
    color: #fff;
    font-size: .88rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .05em;
    white-space: nowrap;
}
table.sortable thead th { cursor: pointer; user-select: none; }
table.sortable thead th:hover { background: var(--accent-2); }
table.sortable thead th[aria-sort=ascending]::after { content: " ▲"; font-size: .7em; }
table.sortable thead th[aria-sort=descending]::after { content: " ▼"; font-size: .7em; }
td.color-code { font-weight: 700; }
.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
table.rows-link tbody tr[data-href] { cursor: pointer; transition: background .12s; }
table.rows-link tbody tr[data-href]:hover { background: #fdf3f3; }
td.empty { text-align: center; color: var(--muted); padding: 2.5rem; }
section > table, .card table { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }

/* Dettaglio */
.fields {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: .85rem 1.25rem;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.15rem 1.25rem;
    margin: 0 0 1.25rem;
}
.fields > div { min-width: 0; }
.fields .wide { grid-column: span 2; }
.fields dt { font-size: .72rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.fields dd { margin: .1rem 0 0; font-size: 1.1rem; font-weight: 600; overflow-wrap: anywhere; min-height: 1.4em; }
.fields.compact { grid-template-columns: 1fr; margin-bottom: .75rem; box-shadow: none; background: var(--accent-soft); border-color: transparent; }
.two-columns { display: grid; grid-template-columns: minmax(0, 1fr); gap: 1.5rem; align-items: start; }
.rescale-inputs { display: flex; gap: .75rem; align-items: end; margin-bottom: .75rem; flex-wrap: wrap; }
.rescale-inputs label { flex: 1; min-width: 9rem; }
.rescale-inputs input { font-size: 1.15rem; font-weight: 600; }
input.line-weight { width: 8.5rem; text-align: right; font-variant-numeric: tabular-nums; }
.ml { color: var(--muted); font-size: .9rem; margin-left: .35rem; }

/* Form */
fieldset { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); padding: 1.1rem; margin: 0 0 1.1rem; }
legend { font-weight: 700; padding: 0 .5rem; color: var(--accent); }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .85rem 1rem; }
.grid .wide { grid-column: span 2; }
.form-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 1.25rem; align-items: start; }
.form-layout aside { position: sticky; top: 5rem; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.1rem; }
.formula-input th { font-weight: 600; white-space: nowrap; }
.formula-input td, .formula-input th { border-bottom: 0; padding: .25rem .4rem; }
.formula-input thead th { background: none; color: var(--muted); }
.preview-errors { color: var(--err-text); font-size: .9rem; padding-left: 1.1rem; margin: 0 0 .75rem; }
.used-by { margin-top: 1.75rem; }

/* Scelta da elenco con possibilità di aggiungere una voce nuova */
.pick { min-width: 0; }
.pick details { margin-top: .35rem; font-size: .85rem; }
.pick summary { color: var(--accent); font-weight: 500; }
.pick details input { margin-top: .35rem; }
.names-columns { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 480px), 1fr)); gap: 1.25rem; align-items: start; }
.names-columns .card > form { margin-bottom: 1rem; }
.delete-form { margin-top: 2rem; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.1rem 1.35rem; margin-bottom: 1.25rem; }
.card form.inline-form { flex-direction: row; align-items: center; }
.card form { display: flex; flex-direction: column; gap: .6rem; align-items: flex-start; }

/* Accesso */
.login-page {
    display: grid;
    place-items: center;
    min-height: 100vh;
    background:
        radial-gradient(circle at 15% 15%, rgba(255, 255, 255, .22), transparent 45%),
        radial-gradient(circle at 90% 90%, rgba(0, 0, 0, .18), transparent 50%),
        linear-gradient(135deg, #d22f2f, #9c1f1f);
}
.login-box { width: min(92vw, 400px); background: var(--surface); border-radius: 20px; padding: 2.25rem 2rem; box-shadow: var(--shadow-lg); }
.login-box .login-logo { display: block; width: min(100%, 300px); height: auto; margin: 0 auto .5rem; }
.login-box h1 { text-align: center; font-size: 1.2rem; color: var(--muted); font-weight: 600; }
.login-box form { display: flex; flex-direction: column; gap: 1rem; }
.login-box button.primary { padding: .65rem 1rem; }

@media (max-width: 900px) {
    .topbar { gap: .75rem; padding: .4rem 1rem; }
    .topbar .brand img { height: 38px; }
    .two-columns, .form-layout { grid-template-columns: minmax(0, 1fr); }
    .fields { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .filters.search { grid-template-columns: minmax(0, 1fr); }
    .form-layout aside { position: static; }
}

/* Stampa della scheda colore */
@media print {
    html { font-size: 12px; }
    body { background: #fff; }
    .no-print { display: none !important; }
    .page { padding: 0; max-width: none; }
    .fields { border: 1px solid #000; border-radius: 0; box-shadow: none; background: #fff; gap: .4rem 1rem; }
    .two-columns { display: block; }
    body:not(.scaled) #rescaled, body.scaled #original { display: none; }
    section h2 { display: none; }
    table, th, td { border: 1px solid #000; border-radius: 0; }
    .table-wrap { box-shadow: none; border-radius: 0; }
    thead th { background: #fff; color: #000; }
    input.line-weight { border: 0; padding: 0; width: auto; font-size: inherit; box-shadow: none; }
}

[hidden] { display: none !important; }

/* Registro prezzi: dettaglio espandibile */
details > summary { cursor: pointer; }
details[open] > summary { margin-bottom: .5rem; }
