/* Tema chiaro/scuro automatico tramite variabili e prefers-color-scheme */
:root {
  color-scheme: light dark;
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1c2330;
  --muted: #5d6878;
  --border: #d5dae2;
  --accent: #1f5fbf;
  --accent-text: #ffffff;
  --accent-soft: #e4edfb;
  --danger: #b3261e;
  --mark: #ffe58a;
  --mark-text: #1c2330;
  --warn-bg: #fff4d6;
  --warn-border: #e0b84a;
  --error-bg: #fde8e6;
  --shadow: 0 8px 30px rgb(0 0 0 / 0.18);
  --radius: 8px;
  --topbar-h: 60px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151a;
    --surface: #1c2027;
    --text: #e7eaf0;
    --muted: #a0a9b8;
    --border: #353c48;
    --accent: #7fb0ff;
    --accent-text: #0d1522;
    --accent-soft: #233450;
    --danger: #ff8a80;
    --mark: #8a6d00;
    --mark-text: #ffffff;
    --warn-bg: #3a3112;
    --warn-border: #8a6d00;
    --error-bg: #43201d;
    --shadow: 0 8px 30px rgb(0 0 0 / 0.6);
  }
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
a { color: var(--accent); }
[hidden] { display: none !important; }
mark { background: var(--mark); color: var(--mark-text); border-radius: 2px; padding: 0 1px; }
.muted { color: var(--muted); }
.spacer { flex: 1; }

/* Focus sempre visibile per chi usa la tastiera */
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip-path: inset(50%); white-space: nowrap;
}
.skip-link { position: absolute; left: -999px; top: 0; background: var(--surface); padding: 8px 12px; z-index: 100; }
.skip-link:focus { left: 8px; top: 8px; }

/* ---------- Controlli ---------- */
input, select, textarea, button { font: inherit; color: inherit; }
input[type="text"], input[type="search"], input[type="tel"], input[type="email"], select, textarea {
  width: 100%;
  padding: 8px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  min-height: 40px;
}
textarea { resize: vertical; }
input.invalid, [aria-invalid="true"] { border-color: var(--danger); box-shadow: 0 0 0 1px var(--danger); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 6px 14px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 32px; padding: 2px 10px; font-size: 0.875rem; }
.btn.icon-btn { min-width: 40px; padding: 0; border-color: transparent; background: transparent; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- Barra superiore con la ricerca sempre visibile ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  min-height: var(--topbar-h); padding: 8px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.search { position: relative; flex: 1; min-width: 0; max-width: 640px; }
.search input { padding-right: 40px; border-radius: 20px; padding-left: 16px; }
.search kbd {
  position: absolute; right: 12px; top: 50%; transform: translateY(-50%);
  font: 0.75rem ui-monospace, monospace; color: var(--muted);
  border: 1px solid var(--border); border-radius: 4px; padding: 0 6px; pointer-events: none;
}
.search input:focus + kbd, .search input:not(:placeholder-shown) + kbd { display: none; }
.actions { display: flex; gap: 8px; margin-left: auto; }
.only-mobile { display: none; }

/* ---------- Accesso ---------- */
body.logged-out .topbar, body.logged-out .layout, body.logged-out .skip-link { display: none; }
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-box {
  width: min(360px, 100%); padding: 24px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
}
.login-box h1 { font-size: 1.5rem; }
.login-box .btn { width: 100%; margin-top: 4px; }
input[type="password"] {
  width: 100%; padding: 8px 10px; min-height: 40px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
}
.user { align-self: center; font-size: 0.875rem; color: var(--muted); white-space: nowrap; }

/* ---------- Struttura ---------- */
.layout { display: grid; grid-template-columns: 280px minmax(0, 1fr); align-items: start; }
main { padding: 12px 16px 48px; max-width: 900px; width: 100%; }

.filters {
  position: sticky; top: var(--topbar-h);
  max-height: calc(100vh - var(--topbar-h)); overflow-y: auto;
  padding: 16px; border-right: 1px solid var(--border);
}
.filters-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.filters-head h2 { flex: 1; }
fieldset { border: 0; padding: 0; margin: 0 0 16px; min-width: 0; }
legend { padding: 0; font-weight: 600; margin-bottom: 6px; }
.facet legend { display: flex; align-items: center; justify-content: space-between; gap: 8px; width: 100%; }
.facet .mode { width: auto; min-height: 28px; padding: 2px 6px; font-size: 0.8rem; font-weight: 400; }
.facet-list { list-style: none; margin: 0; padding: 0; max-height: 220px; overflow-y: auto; }
.facet-list label { display: flex; align-items: center; gap: 8px; padding: 4px 2px; cursor: pointer; border-radius: 4px; }
.facet-list label:hover { background: var(--accent-soft); }
.facet-name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.count {
  font-size: 0.75rem; color: var(--muted); background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px; padding: 0 7px; min-width: 24px; text-align: center;
}
input[type="checkbox"], input[type="radio"] { width: 18px; height: 18px; accent-color: var(--accent); flex: none; }

.segmented { display: flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.segmented label { flex: 1; text-align: center; cursor: pointer; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: block; padding: 7px 8px; font-size: 0.9rem; }
.segmented label + label { border-left: 1px solid var(--border); }
.segmented input:checked + span { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.segmented input:focus-visible + span { outline: 3px solid var(--accent); outline-offset: -3px; }

/* ---------- Filtri attivi, stato, elenco ---------- */
.active-filters { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.active-filters:not(:empty) { margin-bottom: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  background: var(--accent-soft); border-radius: 14px; padding: 2px 4px 2px 10px; font-size: 0.875rem;
}
.chip button {
  border: 0; background: transparent; cursor: pointer; border-radius: 50%;
  width: 24px; height: 24px; line-height: 1; font-size: 0.75rem;
}
.chip button:hover { background: var(--border); }
.status { margin: 4px 0 8px; color: var(--muted); font-size: 0.9rem; }
.error { color: var(--danger); }

.list { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.list:empty { display: none; }
.row {
  position: relative; display: grid; grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 12px; align-items: center; padding: 10px 14px;
}
.row + .row { border-top: 1px solid var(--border); }
.row:hover { background: var(--accent-soft); }
.row-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.row-name {
  border: 0; background: none; padding: 0; text-align: left; cursor: pointer;
  font-weight: 600; font-size: 1rem; overflow-wrap: anywhere;
}
/* l'area cliccabile del nome copre tutta la riga */
.row-name::after { content: ""; position: absolute; inset: 0; }
.row-name:focus-visible { outline: none; }
.row-name:focus-visible::after { outline: 3px solid var(--accent); outline-offset: -3px; border-radius: var(--radius); }
.row-sub, .row-match { font-size: 0.85rem; color: var(--muted); overflow-wrap: anywhere; }
.row-sub::first-letter { text-transform: uppercase; }
.row-phone { position: relative; z-index: 1; white-space: nowrap; font-variant-numeric: tabular-nums; padding: 8px 4px; }

.icon { display: inline-flex; align-items: center; justify-content: center; width: 38px; height: 38px; border-radius: 50%; flex: none; }
.icon-persona { background: var(--accent-soft); color: var(--accent); }
.icon-ufficio { background: color-mix(in srgb, #e08a00 22%, var(--surface)); color: color-mix(in srgb, #e08a00 70%, var(--text)); border-radius: 8px; }

.tags { display: inline-flex; flex-wrap: wrap; gap: 4px; }
.tag { font-size: 0.75rem; padding: 1px 8px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); }
.tag.group { border-style: dashed; }
.badge { font-size: 0.7rem; font-weight: 700; padding: 1px 6px; border-radius: 4px; background: var(--accent); color: var(--accent-text); }

/* ---------- Finestra modale ---------- */
dialog {
  width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  padding: 0; border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
dialog.wide { width: min(760px, calc(100vw - 24px)); }
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
dialog > * { padding: 0 20px 20px; }
.dlg-header {
  position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 8px;
  margin: 0 -20px 12px; padding: 14px 12px 12px 20px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.dlg-header h2 { flex: 1; overflow-wrap: anywhere; }
.dlg-footer {
  position: sticky; bottom: 0; display: flex; gap: 8px; align-items: center;
  margin: 16px -20px -20px; padding: 12px 20px;
  background: var(--surface); border-top: 1px solid var(--border);
}

.detail-kind { display: flex; align-items: center; gap: 10px; margin: 0 0 12px; color: var(--muted); }
.detail-section { margin-bottom: 16px; }
.detail-section h3 { margin-bottom: 6px; }
.detail-section p { margin: 4px 0; }
.plain { list-style: none; margin: 0; padding: 0; }
.plain li { padding: 3px 0; overflow-wrap: anywhere; }
address { font-style: normal; margin-bottom: 4px; }
.notes { white-space: pre-wrap; overflow-wrap: anywhere; }

/* ---------- Form ---------- */
.field { margin-bottom: 12px; position: relative; }
.field > label, .group-row > label { display: block; font-size: 0.875rem; font-weight: 600; margin-bottom: 4px; }
.req { color: var(--danger); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-3 { display: grid; grid-template-columns: 110px 1fr 110px; gap: 12px; }
.rep-row { display: grid; grid-template-columns: 130px minmax(0, 1.4fr) minmax(0, 1fr) auto; gap: 8px; margin-bottom: 8px; align-items: center; }
.warning, .form .error {
  border: 1px solid var(--warn-border); background: var(--warn-bg);
  border-radius: 6px; padding: 8px 12px; margin-bottom: 12px; font-size: 0.9rem;
}
.form .error { border-color: var(--danger); background: var(--error-bg); color: var(--text); }
.warning ul, .error ul { margin: 4px 0; padding-left: 20px; }

.chip-input {
  display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
  padding: 4px 6px; min-height: 40px; cursor: text;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
}
.chip-input:focus-within { outline: 3px solid var(--accent); outline-offset: 2px; }
.chips-inline { display: contents; }
.chip-input input { flex: 1; min-width: 140px; border: 0; min-height: 30px; padding: 2px 4px; outline: none; background: transparent; }
.suggestions {
  position: absolute; left: 0; right: 0; z-index: 5; list-style: none; margin: 2px 0 0; padding: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px; box-shadow: var(--shadow);
  max-height: 220px; overflow-y: auto;
}
.suggestions li { padding: 6px 10px; border-radius: 4px; cursor: pointer; }
.suggestions li.active, .suggestions li:hover { background: var(--accent-soft); }

.group-list { list-style: none; padding: 0; margin: 12px 0 0; }
.group-row { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.group-row > label { margin: 0; white-space: nowrap; }
.group-row input { flex: 1; min-width: 0; }
.user-row { display: grid; grid-template-columns: minmax(0, 1fr) 150px minmax(0, 1fr) auto auto; gap: 8px; align-items: center; margin-bottom: 8px; }
.user-row strong { overflow-wrap: anywhere; }
@media (max-width: 800px) {
  .user-row { grid-template-columns: 1fr 1fr; padding-bottom: 8px; border-bottom: 1px dashed var(--border); }
  .user-row > :first-child:not(.visually-hidden), .user-row input[type="text"] { grid-column: 1 / -1; }
}
.import-result ul { padding-left: 20px; max-height: 200px; overflow-y: auto; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 1000;
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 20px; box-shadow: var(--shadow);
  max-width: calc(100vw - 32px);
}
.backdrop { display: none; }

/* ---------- Smartphone: filtri a scomparsa ---------- */
@media (max-width: 800px) {
  .only-mobile { display: inline-flex; }
  .topbar { flex-wrap: wrap; padding: 8px 12px; --gap: 8px; gap: 8px; }
  .topbar h1 { display: none; }
  .actions { flex-wrap: wrap; justify-content: flex-end; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .layout { display: block; }
  main { padding: 10px 10px 48px; }
  .filters {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 40;
    width: min(320px, 88vw); max-height: none;
    background: var(--surface); box-shadow: var(--shadow);
    transform: translateX(-105%); visibility: hidden;
    transition: transform 0.2s ease, visibility 0.2s;
  }
  .filters.open { transform: none; visibility: visible; }
  .backdrop { display: block; position: fixed; inset: 0; z-index: 30; background: rgb(0 0 0 / 0.45); }
  .row { grid-template-columns: auto minmax(0, 1fr); }
  .row-phone { grid-column: 2; justify-self: start; padding: 4px 0; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; gap: 0; }
  .rep-row { grid-template-columns: 1fr 1fr; padding-bottom: 8px; border-bottom: 1px dashed var(--border); }
  .rep-row [data-role="value"] { grid-column: 1 / -1; grid-row: 1; }
  dialog > * { padding: 0 14px 14px; }
  .dlg-header { margin: 0 -14px 12px; padding-left: 14px; }
  .dlg-footer { margin: 14px -14px -14px; padding: 10px 14px; }
}
@media (prefers-reduced-motion: reduce) {
  .filters { transition: none; }
}
