/* ── Reset & base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --c-bg: #f7f6f2;
  --c-surface: #ffffff;
  --c-border: rgba(0,0,0,.1);
  --c-border-md: rgba(0,0,0,.2);
  --c-text: #1a1a18;
  --c-text-2: #555550;
  --c-text-3: #888880;
  --c-blue: #185FA5;
  --c-blue-bg: #E6F1FB;
  --c-blue-text: #0C447C;
  --c-green: #3B6D11;
  --c-green-bg: #EAF3DE;
  --c-green-text: #27500A;
  --c-amber: #854F0B;
  --c-amber-bg: #FAEEDA;
  --c-amber-text: #633806;
  --c-red: #A32D2D;
  --c-red-bg: #FCEBEB;
  --c-red-text: #791F1F;
  --c-purple: #3C3489;
  --c-purple-bg: #EEEDFE;
  --c-purple-text: #26215C;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow: 0 1px 4px rgba(0,0,0,.08);
}
html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; font-size: 14px; color: var(--c-text); background: var(--c-bg); }
a { text-decoration: none; color: inherit; cursor: pointer; }
button { cursor: pointer; font-family: inherit; font-size: inherit; }
input, select, textarea { font-family: inherit; font-size: inherit; }
.hidden { display: none !important; }
.mt-6 { margin-top: 6px; }

/* ── Login ────────────────────────────────────────────────────────────────── */
.screen-login { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--c-bg); }
.login-card { background: var(--c-surface); border: 0.5px solid var(--c-border); border-radius: var(--radius-lg); padding: 2rem; width: 100%; max-width: 360px; }
.login-logo { display: flex; align-items: center; gap: 10px; margin-bottom: 1.5rem; }
.login-logo span { font-size: 16px; font-weight: 500; }
.login-card h1 { font-size: 20px; font-weight: 500; margin-bottom: 1.25rem; }

/* ── App layout ───────────────────────────────────────────────────────────── */
.app-layout { display: flex; height: 100vh; overflow: hidden; }

/* ── Sidebar ──────────────────────────────────────────────────────────────── */
.sidebar { width: 210px; background: var(--c-surface); border-right: 0.5px solid var(--c-border); display: flex; flex-direction: column; flex-shrink: 0; overflow-y: auto; }
.sidebar-header { padding: 14px 12px; border-bottom: 0.5px solid var(--c-border); }
.app-brand { display: flex; align-items: center; gap: 9px; }
.brand-name { font-size: 13px; font-weight: 500; }
.brand-sub { font-size: 10px; color: var(--c-text-3); }
.patient-switcher { margin: 8px; padding: 8px; background: var(--c-bg); border-radius: var(--radius); border: 0.5px solid var(--c-border); }
.pat-btn { display: flex; align-items: center; gap: 7px; padding: 6px 7px; border-radius: var(--radius); font-size: 12px; color: var(--c-text-2); cursor: pointer; border: none; background: none; width: 100%; text-align: left; }
.pat-btn.active { background: var(--c-surface); color: var(--c-text); font-weight: 500; border: 0.5px solid var(--c-border-md); }
.pat-avatar { width: 22px; height: 22px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 9px; font-weight: 600; }
.pa-b { background: var(--c-blue-bg); color: var(--c-blue-text); }
.pa-g { background: var(--c-green-bg); color: var(--c-green-text); }
.nav-section { padding: 8px 6px 2px; }
.nav-label { font-size: 10px; color: var(--c-text-3); padding: 4px 8px; text-transform: uppercase; letter-spacing: .05em; }
.nav-item { display: flex; align-items: center; gap: 8px; padding: 7px 8px; border-radius: var(--radius); font-size: 12px; color: var(--c-text-2); cursor: pointer; margin-bottom: 1px; border: none; background: none; width: 100%; text-align: left; }
.nav-item:hover { background: var(--c-bg); }
.nav-item.active { background: var(--c-blue-bg); color: var(--c-blue-text); font-weight: 500; }
.nav-icon { width: 15px; height: 15px; flex-shrink: 0; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.nav-badge { margin-left: auto; background: var(--c-bg); color: var(--c-text-3); font-size: 10px; padding: 1px 6px; border-radius: 10px; border: 0.5px solid var(--c-border); }
.sidebar-footer { margin-top: auto; padding: 10px; border-top: 0.5px solid var(--c-border); }
.user-info { display: flex; align-items: center; gap: 8px; }
.user-avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--c-purple-bg); color: var(--c-purple-text); display: flex; align-items: center; justify-content: center; font-size: 11px; font-weight: 500; flex-shrink: 0; }
.user-name { font-size: 12px; font-weight: 500; }
.user-role { font-size: 10px; color: var(--c-text-3); }
.btn-icon { margin-left: auto; background: none; border: none; color: var(--c-text-3); padding: 4px; border-radius: var(--radius); display: flex; align-items: center; gap: 4px; }
.btn-icon:hover { background: var(--c-bg); color: var(--c-text); }
/* Etichetta "Esci" visibile solo nella PWA installata (standalone) */
.logout-label { display: none; font-size: 12px; }
@media (display-mode: standalone) {
  .logout-label { display: inline; }
}

/* ── Main content ─────────────────────────────────────────────────────────── */
.main-content { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

/* ── Topbar ───────────────────────────────────────────────────────────────── */
.topbar { padding: 12px 18px; background: var(--c-surface); border-bottom: 0.5px solid var(--c-border); display: flex; align-items: center; gap: 12px; flex-shrink: 0; }
.topbar-title { font-size: 14px; font-weight: 500; }
.topbar-sub { font-size: 11px; color: var(--c-text-3); margin-top: 1px; }
.topbar-actions { margin-left: auto; display: flex; gap: 6px; }
.search-wrap { flex: 1; max-width: 280px; display: flex; align-items: center; gap: 7px; background: var(--c-bg); border: 0.5px solid var(--c-border); border-radius: var(--radius); padding: 6px 10px; }
.search-wrap input { background: none; border: none; outline: none; font-size: 12px; color: var(--c-text); width: 100%; }

/* ── Buttons ──────────────────────────────────────────────────────────────── */
.btn { display: inline-flex; align-items: center; gap: 5px; padding: 6px 12px; border-radius: var(--radius); border: 0.5px solid var(--c-border-md); font-size: 12px; background: var(--c-surface); color: var(--c-text); }
.btn:hover { background: var(--c-bg); }
.btn-primary { background: var(--c-blue-bg); color: var(--c-blue-text); border-color: transparent; font-weight: 500; }
.btn-primary:hover { opacity: .9; }
.btn-danger { background: var(--c-red-bg); color: var(--c-red-text); border-color: transparent; }
.btn-success { background: var(--c-green-bg); color: var(--c-green-text); border-color: transparent; }
.btn-sm { padding: 4px 9px; font-size: 11px; }
.btn-full { width: 100%; justify-content: center; }

/* ── Forms ────────────────────────────────────────────────────────────────── */
.field-group { margin-bottom: 12px; }
.field-group label { display: block; font-size: 11px; color: var(--c-text-2); margin-bottom: 4px; }
input[type=text], input[type=email], input[type=password], input[type=date], select, textarea {
  width: 100%; padding: 7px 9px; border: 0.5px solid var(--c-border-md); border-radius: var(--radius);
  background: var(--c-bg); color: var(--c-text); outline: none; font-size: 12px;
}
input:focus, select:focus, textarea:focus { border-color: var(--c-blue); box-shadow: 0 0 0 2px rgba(24,95,165,.15); }
textarea { resize: vertical; line-height: 1.5; }

/* ── Alerts ───────────────────────────────────────────────────────────────── */
.alert { padding: 8px 10px; border-radius: var(--radius); font-size: 12px; margin-bottom: 10px; }
.alert-danger { background: var(--c-red-bg); color: var(--c-red-text); border: 0.5px solid #F09595; }
.alert-success { background: var(--c-green-bg); color: var(--c-green-text); }
.alert-info { background: var(--c-blue-bg); color: var(--c-blue-text); }
.alert-warning { background: var(--c-amber-bg); color: var(--c-amber-text); border: 0.5px solid #EF9F27; }

/* ── Cards e sezioni ──────────────────────────────────────────────────────── */
.section-wrap { padding: 16px; flex: 1; overflow-y: auto; }
.card { background: var(--c-surface); border: 0.5px solid var(--c-border); border-radius: var(--radius-lg); padding: 14px; margin-bottom: 12px; }
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 9999; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal-box { background: var(--c-bg); border-radius: var(--radius-lg); padding: 20px; max-width: 420px; width: 100%; box-shadow: var(--shadow-lg); display: flex; flex-direction: column; gap: 14px; }
.card-title { font-size: 13px; font-weight: 500; margin-bottom: 12px; display: flex; align-items: center; gap: 7px; }
.stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 16px; }
.stat-card { background: var(--c-bg); border-radius: var(--radius); padding: 10px 12px; }
.stat-label { font-size: 11px; color: var(--c-text-2); margin-bottom: 4px; }
.stat-value { font-size: 22px; font-weight: 500; }

/* ── Filtri ───────────────────────────────────────────────────────────────── */
.filter-row { display: flex; gap: 6px; margin-bottom: 12px; flex-wrap: wrap; }
.chip { padding: 4px 10px; border-radius: 14px; border: 0.5px solid var(--c-border-md); font-size: 11px; cursor: pointer; background: var(--c-surface); color: var(--c-text-2); }
.chip.active { background: var(--c-blue-bg); color: var(--c-blue-text); border-color: transparent; font-weight: 500; }

/* ── Lista documenti ──────────────────────────────────────────────────────── */
.doc-list { display: flex; flex-direction: column; gap: 6px; }
.doc-row { background: var(--c-surface); border: 0.5px solid var(--c-border); border-radius: var(--radius); padding: 10px 12px; display: flex; align-items: center; gap: 10px; cursor: pointer; }
.doc-row:hover { border-color: var(--c-border-md); background: var(--c-bg); }
.doc-icon { width: 32px; height: 32px; border-radius: var(--radius); display: flex; align-items: center; justify-content: center; font-size: 15px; flex-shrink: 0; }
.di-pdf { background: var(--c-red-bg); color: var(--c-red); }
.di-img { background: var(--c-blue-bg); color: var(--c-blue); }
.di-dcm { background: var(--c-green-bg); color: var(--c-green); }
.di-zip { background: var(--c-amber-bg); color: var(--c-amber); }
.di-def { background: var(--c-bg); color: var(--c-text-2); }
.doc-name { font-size: 12px; font-weight: 500; white---c-text-overflow: ellipsis; overflow: hidden; white-space: nowrap; }
.doc-meta { font-size: 10px; color: var(--c-text-3); margin-top: 1px; }
.doc-info { flex: 1; min-width: 0; }
.doc-actions { display: flex; gap: 5px; margin-left: auto; flex-shrink: 0; }
.icon-btn { width: 32px; height: 32px; display: flex; align-items: center; justify-content: center; border-radius: var(--radius); border: 0.5px solid var(--c-border); background: var(--c-bg); color: var(--c-text-2); font-size: 17px; }
.icon-btn:hover { border-color: var(--c-border-md); color: var(--c-text); }

/* ── Badge categorie ──────────────────────────────────────────────────────── */
.badge { padding: 2px 7px; border-radius: 10px; font-size: 10px; font-weight: 500; white-space: nowrap; }
.b-analisi { background: var(--c-green-bg); color: var(--c-green-text); }
.b-imaging { background: var(--c-blue-bg); color: var(--c-blue-text); }
.b-ricovero { background: var(--c-amber-bg); color: var(--c-amber-text); }
.b-visita { background: var(--c-purple-bg); color: var(--c-purple-text); }
.b-altro { background: var(--c-bg); color: var(--c-text-2); border: 0.5px solid var(--c-border); }
.b-rev { background: var(--c-amber-bg); color: var(--c-amber-text); }

/* ── Link condivisione ────────────────────────────────────────────────────── */
.link-card { background: var(--c-surface); border: 0.5px solid var(--c-border); border-radius: var(--radius); padding: 11px 13px; margin-bottom: 8px; }
.link-meta { display: flex; align-items: center; gap: 6px; margin-top: 7px; flex-wrap: wrap; }
.status-dot { width: 6px; height: 6px; border-radius: 50%; display: inline-block; }
.dot-on { background: #1D9E75; }
.dot-off { background: #E24B4A; }

/* ── Scan NAS ─────────────────────────────────────────────────────────────── */
.scan-row { display: flex; align-items: center; gap: 8px; padding: 7px 10px; background: var(--c-bg); border-radius: var(--radius); border: 0.5px solid var(--c-border); margin-bottom: 5px; }
.scan-row.warn { border-color: #EF9F27; background: var(--c-amber-bg); }
.scan-name { flex: 1; font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.scan-size { font-size: 10px; color: var(--c-text-3); white-space: nowrap; }

/* ── Log ──────────────────────────────────────────────────────────────────── */
.log-row { display: flex; gap: 10px; padding: 8px 0; border-bottom: 0.5px solid var(--c-border); font-size: 12px; }
.log-time { color: var(--c-text-3); white-space: nowrap; font-size: 10px; min-width: 110px; }

/* ── Viewer overlay ───────────────────────────────────────────────────────── */
.viewer-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 1000; display: flex; align-items: center; justify-content: center; }
.viewer-container { background: var(--c-surface); border-radius: var(--radius-lg); width: 96vw; height: 94vh; display: flex; flex-direction: column; overflow: hidden; }
.viewer-topbar { padding: 10px 14px; border-bottom: 0.5px solid var(--c-border); display: flex; align-items: center; gap: 8px; flex-shrink: 0; background: var(--c-surface); }
.viewer-title { font-size: 13px; font-weight: 500; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-actions { display: flex; align-items: center; gap: 5px; flex-shrink: 0; }
.viewer-page-info, .viewer-zoom-val { font-size: 11px; color: var(--c-text-2); min-width: 40px; text-align: center; }
.viewer-body { flex: 1; display: flex; overflow: hidden; }
.viewer-canvas-wrap { flex: 1; background: #2a2a2a; overflow: auto; display: flex; align-items: flex-start; justify-content: center; padding: 16px; }
#viewer-canvas { display: block; }
#viewer-img { max-width: 100%; height: auto; border-radius: 4px; }
.viewer-unsupported { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; height: 200px; color: #ccc; }
.viewer-sidebar { width: 240px; border-left: 0.5px solid var(--c-border); overflow-y: auto; flex-shrink: 0; }
.vs-section { padding: 12px; border-bottom: 0.5px solid var(--c-border); }
.vs-title { font-size: 11px; font-weight: 500; margin-bottom: 8px; color: var(--c-text-2); }
.vs-row { display: flex; justify-content: space-between; font-size: 10px; padding: 3px 0; }
.vs-lbl { color: var(--c-text-3); }
.vs-val { color: var(--c-text); font-weight: 500; text-align: right; max-width: 130px; word-break: break-word; }
.note-item { background: var(--c-bg); border-radius: var(--radius); padding: 7px 8px; margin-bottom: 6px; }
.note-who { font-size: 9px; color: var(--c-text-3); margin-bottom: 3px; }
.note-text { font-size: 11px; line-height: 1.4; }

/* ── Utenti ───────────────────────────────────────────────────────────────── */
.user-card { background: var(--c-surface); border: 0.5px solid var(--c-border); border-radius: var(--radius); padding: 11px 13px; margin-bottom: 7px; display: flex; align-items: center; gap: 10px; }
.perm-tags { display: flex; gap: 4px; flex-wrap: wrap; margin-left: auto; }
.perm-tag { padding: 2px 6px; border-radius: 6px; font-size: 10px; background: var(--c-bg); color: var(--c-text-2); border: 0.5px solid var(--c-border); }
.perm-tag.on { background: var(--c-blue-bg); color: var(--c-blue-text); border-color: transparent; }

/* ── Loading ──────────────────────────────────────────────────────────────── */
.loading { display: flex; align-items: center; justify-content: center; padding: 40px; color: var(--c-text-3); font-size: 13px; }
.empty { text-align: center; padding: 40px; color: var(--c-text-3); font-size: 13px; }

.nav-group { margin-bottom: 1px; }
.nav-group-header { display: flex; align-items: center; gap: 8px; padding: 7px 8px; border-radius: var(--radius); font-size: 12px; color: var(--c-text-2); cursor: pointer; border: none; background: none; width: 100%; text-align: left; font-family: inherit; }
.nav-group-header:hover { background: var(--c-bg); }
.nav-group-icon-wrap { position: relative; display: inline-flex; flex-shrink: 0; }
.nav-group-dot { position: absolute; top: -2px; right: -2px; width: 6px; height: 6px; border-radius: 50%; background: var(--c-blue-text); border: 1px solid var(--c-surface); }
.nav-group-title { flex: 1; }
.nav-chevron { width: 14px; height: 14px; flex-shrink: 0; color: var(--c-text-3); transition: transform 0.15s ease; }
.nav-group.open > .nav-group-header .nav-chevron { transform: rotate(90deg); }
.nav-subitems { max-height: 0; overflow: hidden; transition: max-height 0.2s ease; }
.nav-group.open > .nav-subitems { max-height: 200px; }
.nav-subitem { padding-left: 28px; }
