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

  :root {
    --blue:      #2563eb;
    --blue-lt:   #eff6ff;
    --blue-mid:  #bfdbfe;
    --green:     #16a34a;
    --green-lt:  #f0fdf4;
    --purple:    #7c3aed;
    --purple-lt: #f5f3ff;
    --gray-50:   #f8fafc;
    --gray-100:  #f1f5f9;
    --gray-200:  #e2e8f0;
    --gray-400:  #94a3b8;
    --gray-600:  #475569;
    --gray-700:  #334155;
    --gray-900:  #0f172a;
    --radius:    6px;
    --shadow:    0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
    --shadow-md: 0 4px 6px -1px rgba(0,0,0,.1), 0 2px 4px -2px rgba(0,0,0,.1);
  }

  html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif; font-size: 14px; color: var(--gray-900); background: var(--gray-100); }

  /* ── MODAL LOGIN ───────────────────────────────── */
  #login-overlay {
    position: fixed; inset: 0; background: rgba(15,23,42,.6);
    display: flex; align-items: center; justify-content: center; z-index: 1000;
  }
  #login-overlay.hidden { display: none; }
  .login-box {
    background: #fff; border-radius: 12px; padding: 40px;
    width: 360px; box-shadow: var(--shadow-md);
  }
  .login-box h2 { font-size: 20px; margin-bottom: 4px; color: var(--gray-900); }
  .login-box p  { color: var(--gray-600); margin-bottom: 24px; font-size: 13px; }
  .form-field { margin-bottom: 16px; }
  .form-field label { display: block; font-weight: 500; margin-bottom: 6px; font-size: 13px; }
  .form-field input {
    width: 100%; padding: 9px 12px; border: 1px solid var(--gray-200);
    border-radius: var(--radius); font-size: 14px; outline: none;
    transition: border-color .15s;
  }
  .form-field input:focus { border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-lt); }
  .btn-primary {
    width: 100%; padding: 10px; background: var(--blue); color: #fff;
    border: none; border-radius: var(--radius); font-size: 14px; font-weight: 600;
    cursor: pointer; transition: background .15s;
  }
  .btn-primary:hover { background: #1d4ed8; }
  .login-error { color: #dc2626; font-size: 13px; margin-top: 10px; text-align: center; min-height: 20px; }

  /* ── APP SHELL ─────────────────────────────────── */
  #app { display: flex; flex-direction: column; height: 100vh; }

  /* ── HEADER ────────────────────────────────────── */
  header {
    display: flex; align-items: center; gap: 16px;
    padding: 0 20px; height: 54px; background: #fff;
    border-bottom: 1px solid var(--gray-200); box-shadow: var(--shadow);
    flex-shrink: 0; z-index: 10;
  }
  .logo { font-weight: 700; font-size: 16px; color: var(--blue); white-space: nowrap; }
  .logo span { color: var(--gray-600); font-weight: 400; }

  .sede-bar { display: flex; gap: 6px; margin-left: 8px; }
  .sede-btn {
    padding: 5px 14px; border-radius: 20px; border: 1.5px solid var(--gray-200);
    background: #fff; font-size: 13px; font-weight: 500; cursor: pointer;
    transition: all .15s; color: var(--gray-700); white-space: nowrap;
  }
  .sede-btn:hover { border-color: var(--gray-400); background: var(--gray-50); }
  .sede-btn.active-roma   { background: var(--blue-lt);   border-color: var(--blue);   color: var(--blue); }
  .sede-btn.active-pont   { background: var(--green-lt);  border-color: var(--green);  color: var(--green); }
  .sede-btn.active-lat    { background: var(--purple-lt); border-color: var(--purple); color: var(--purple); }

  .header-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
  .user-badge { font-size: 12px; color: var(--gray-600); }
  .btn-logout {
    padding: 4px 10px; border: 1px solid var(--gray-200); border-radius: var(--radius);
    background: #fff; font-size: 12px; cursor: pointer; color: var(--gray-600);
  }
  .btn-logout:hover { background: var(--gray-100); }

  /* ── MAIN 3-COLUMN LAYOUT ──────────────────────── */
  .main-layout {
    display: flex; flex: 1; overflow: hidden;
  }

  /* ── LEFT: LIBRERIA ────────────────────────────── */
  .libreria-panel {
    width: 260px; min-width: 240px; background: #fff;
    border-right: 1px solid var(--gray-200);
    display: flex; flex-direction: column; overflow: hidden; flex-shrink: 0;
  }
  .libreria-search {
    padding: 12px; border-bottom: 1px solid var(--gray-200);
  }
  .libreria-search input {
    width: 100%; padding: 7px 10px; border: 1px solid var(--gray-200);
    border-radius: var(--radius); font-size: 13px; outline: none;
    background: var(--gray-50);
  }
  .libreria-search input:focus { border-color: var(--blue); background: #fff; }
  .libreria-scroll { flex: 1; overflow-y: auto; padding: 6px 0; }
  .libreria-scroll::-webkit-scrollbar { width: 4px; }
  .libreria-scroll::-webkit-scrollbar-thumb { background: var(--gray-200); border-radius: 4px; }

  .cat-section {}
  .cat-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 8px 14px; cursor: pointer; user-select: none;
    font-weight: 600; font-size: 12px; text-transform: uppercase;
    letter-spacing: .05em; color: var(--gray-600);
    transition: background .1s;
  }
  .cat-header:hover { background: var(--gray-50); }
  .cat-header .arrow { transition: transform .2s; font-size: 10px; }
  .cat-section.open .cat-header .arrow { transform: rotate(90deg); }
  .cat-count { background: var(--gray-100); color: var(--gray-600); border-radius: 10px; padding: 1px 7px; font-size: 11px; }

  .cat-body { display: none; }
  .cat-section.open .cat-body { display: block; }

  .esame-item {
    padding: 7px 18px; cursor: pointer; font-size: 13px; color: var(--gray-700);
    border-left: 3px solid transparent; transition: all .1s; line-height: 1.3;
  }
  .esame-item:hover { background: var(--gray-50); color: var(--gray-900); }
  .esame-item.active { background: var(--blue-lt); border-left-color: var(--blue); color: var(--blue); font-weight: 500; }

  .no-results { padding: 20px 14px; font-size: 13px; color: var(--gray-400); text-align: center; }

  /* ── CENTER: EDITOR ────────────────────────────── */
  .editor-panel {
    flex: 1; display: flex; flex-direction: column; overflow: hidden;
    background: var(--gray-50);
  }

  .exam-bar {
    display: flex; align-items: center; gap: 12px;
    padding: 10px 16px; background: #fff;
    border-bottom: 1px solid var(--gray-200); flex-shrink: 0;
  }
  .exam-label {
    font-size: 13px; font-weight: 600; color: var(--gray-900);
    flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .exam-label.placeholder { color: var(--gray-400); font-weight: 400; font-style: italic; }
  .exam-date {
    padding: 5px 10px; border: 1px solid var(--gray-200); border-radius: var(--radius);
    font-size: 13px; outline: none; color: var(--gray-700);
  }
  .exam-date:focus { border-color: var(--blue); }

  .editor-scroll { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 14px; }
  .editor-scroll::-webkit-scrollbar { width: 4px; }
  .editor-scroll::-webkit-scrollbar-thumb { background: var(--gray-200); border-radius: 4px; }

  /* Varianti chips */
  .varianti-block {
    background: #fff; border: 1px solid var(--gray-200); border-radius: var(--radius);
    padding: 12px 14px;
  }
  .varianti-block.hidden { display: none; }
  .block-label {
    font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em;
    color: var(--gray-400); margin-bottom: 10px;
  }
  .chips-wrap { display: flex; flex-wrap: wrap; gap: 6px; }
  .variante-chip {
    padding: 5px 12px; border: 1.5px solid var(--gray-200); border-radius: 20px;
    background: #fff; font-size: 12px; font-weight: 500; cursor: pointer;
    color: var(--gray-700); transition: all .15s;
  }
  .variante-chip:hover { border-color: var(--blue); color: var(--blue); background: var(--blue-lt); }
  .variante-chip.active { border-color: var(--blue); background: var(--blue); color: #fff; }

  /* Text editors */
  .editor-block {
    background: #fff; border: 1px solid var(--gray-200); border-radius: var(--radius);
    padding: 12px 14px;
  }
  .editor-block-header {
    display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px;
  }
  .editor-block textarea {
    width: 100%; resize: vertical; border: none; outline: none;
    font-size: 13px; line-height: 1.6; color: var(--gray-900);
    font-family: inherit; background: transparent;
  }
  .editor-block textarea::placeholder { color: var(--gray-400); }
  .char-count { font-size: 11px; color: var(--gray-400); }

  .btn-clear {
    padding: 2px 8px; border: 1px solid var(--gray-200); border-radius: var(--radius);
    background: #fff; font-size: 11px; cursor: pointer; color: var(--gray-500);
  }
  .btn-clear:hover { background: #fee2e2; border-color: #fca5a5; color: #dc2626; }

  /* Actions bar */
  .actions-bar {
    display: flex; gap: 8px; padding: 12px 16px;
    background: #fff; border-top: 1px solid var(--gray-200); flex-shrink: 0;
  }
  .btn-save {
    padding: 8px 20px; background: var(--blue); color: #fff; border: none;
    border-radius: var(--radius); font-size: 13px; font-weight: 600;
    cursor: pointer; transition: background .15s;
  }
  .btn-save:hover { background: #1d4ed8; }
  .btn-save:disabled { background: var(--gray-200); color: var(--gray-400); cursor: default; }
  .btn-secondary {
    padding: 8px 16px; background: #fff; color: var(--gray-700);
    border: 1px solid var(--gray-200); border-radius: var(--radius);
    font-size: 13px; cursor: pointer; transition: all .15s;
  }
  .btn-secondary:hover { background: var(--gray-50); border-color: var(--gray-400); }
  .status-msg { font-size: 13px; color: var(--gray-600); align-self: center; }

  /* ── RIGHT: PREVIEW ────────────────────────────── */
  .preview-panel {
    width: 320px; min-width: 280px; background: var(--gray-50);
    border-left: 1px solid var(--gray-200);
    display: flex; flex-direction: column; overflow: hidden; flex-shrink: 0;
  }
  .preview-title {
    padding: 10px 16px; font-size: 12px; font-weight: 600;
    text-transform: uppercase; letter-spacing: .06em; color: var(--gray-400);
    background: #fff; border-bottom: 1px solid var(--gray-200); flex-shrink: 0;
    display: flex; align-items: center; justify-content: space-between;
  }
  .preview-scroll { flex: 1; overflow-y: auto; padding: 16px; }
  .preview-scroll::-webkit-scrollbar { width: 4px; }
  .preview-scroll::-webkit-scrollbar-thumb { background: var(--gray-200); border-radius: 4px; }

  .preview-doc {
    background: #fff; border: 1px solid var(--gray-200); border-radius: var(--radius);
    padding: 20px; font-size: 12px; line-height: 1.65; color: var(--gray-700);
    box-shadow: var(--shadow);
  }
  .preview-studio { text-align: center; margin-bottom: 14px; }
  .preview-studio .studio-name { font-weight: 700; font-size: 14px; color: var(--gray-900); }
  .preview-studio .medico-name { font-weight: 600; font-size: 13px; margin-top: 2px; }
  .preview-studio .medico-info { color: var(--gray-500); font-size: 11px; margin-top: 2px; }
  .preview-sede-badge {
    display: inline-block; font-size: 11px; font-weight: 600;
    padding: 2px 8px; border-radius: 10px; margin-top: 4px;
  }
  .sede-roma   .preview-sede-badge { background: var(--blue-lt);   color: var(--blue); }
  .sede-pont   .preview-sede-badge { background: var(--green-lt);  color: var(--green); }
  .sede-lat    .preview-sede-badge { background: var(--purple-lt); color: var(--purple); }

  .preview-divider { border: none; border-top: 1px solid var(--gray-200); margin: 12px 0; }
  .preview-exam-title { font-weight: 700; font-size: 13px; color: var(--gray-900); }
  .preview-exam-date  { font-size: 11px; color: var(--gray-400); margin-top: 2px; margin-bottom: 10px; }
  .preview-body { white-space: pre-wrap; font-size: 12px; color: var(--gray-700); }
  .preview-conclusioni { margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--gray-200); }
  .preview-conclusioni strong { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--gray-500); display: block; margin-bottom: 4px; }
  .preview-empty { text-align: center; color: var(--gray-400); font-style: italic; padding: 40px 0; font-size: 13px; }

  /* Toast */
  #toast {
    position: fixed; bottom: 24px; right: 24px; z-index: 999;
    background: var(--gray-900); color: #fff; padding: 10px 18px;
    border-radius: var(--radius); font-size: 13px; box-shadow: var(--shadow-md);
    opacity: 0; transition: opacity .25s; pointer-events: none;
  }
  #toast.show { opacity: 1; }
  #toast.error { background: #dc2626; }
  #toast.success { background: var(--green); }

  /* ── NAV TABS ──────────────────────────────────── */
  .nav-tabs { display: flex; gap: 2px; margin-left: 12px; }
  .nav-tab {
    padding: 5px 14px; border-radius: var(--radius); border: none;
    background: transparent; font-size: 13px; font-weight: 500; cursor: pointer;
    color: var(--gray-600); transition: all .15s;
  }
  .nav-tab:hover { background: var(--gray-100); color: var(--gray-900); }
  .nav-tab.active { background: var(--blue-lt); color: var(--blue); font-weight: 600; }

  /* ── PAZIENTI VIEW ─────────────────────────────── */
  .pazienti-view { display: flex; flex: 1; overflow: hidden; }
  .pazienti-view.hidden { display: none; }

  .paz-list-panel {
    width: 300px; min-width: 260px; background: #fff;
    border-right: 1px solid var(--gray-200);
    display: flex; flex-direction: column; overflow: hidden; flex-shrink: 0;
  }
  .paz-list-header {
    padding: 10px 12px; border-bottom: 1px solid var(--gray-200);
    display: flex; gap: 8px; align-items: center;
  }
  .paz-search {
    flex: 1; padding: 7px 10px; border: 1px solid var(--gray-200);
    border-radius: var(--radius); font-size: 13px; outline: none;
    background: var(--gray-50);
  }
  .paz-search:focus { border-color: var(--blue); background: #fff; }
  .btn-new-paz {
    padding: 7px 12px; background: var(--blue); color: #fff;
    border: none; border-radius: var(--radius); font-size: 13px;
    font-weight: 600; cursor: pointer; white-space: nowrap;
  }
  .btn-new-paz:hover { background: #1d4ed8; }
  .paz-list-scroll { flex: 1; overflow-y: auto; }
  .paz-list-scroll::-webkit-scrollbar { width: 4px; }
  .paz-list-scroll::-webkit-scrollbar-thumb { background: var(--gray-200); border-radius: 4px; }

  .paz-card {
    padding: 10px 14px; border-bottom: 1px solid var(--gray-100);
    cursor: pointer; transition: background .1s; border-left: 3px solid transparent;
  }
  .paz-card:hover  { background: var(--gray-50); }
  .paz-card.active { background: var(--blue-lt); border-left-color: var(--blue); }
  .paz-name  { font-size: 14px; font-weight: 600; color: var(--gray-900); }
  .paz-cf    { font-size: 11px; color: var(--gray-400); font-family: monospace; margin-top: 1px; }
  .paz-phone { font-size: 12px; color: var(--gray-600); margin-top: 1px; }

  .paz-detail-panel { flex: 1; display: flex; flex-direction: column; overflow: hidden; background: var(--gray-50); }
  .paz-detail-empty { display: flex; align-items: center; justify-content: center; flex: 1; color: var(--gray-400); font-style: italic; font-size: 14px; }

  .paz-detail-header {
    padding: 14px 20px; background: #fff; border-bottom: 1px solid var(--gray-200);
    display: flex; align-items: flex-start; justify-content: space-between; flex-shrink: 0;
  }
  .paz-detail-name  { font-size: 18px; font-weight: 700; color: var(--gray-900); }
  .paz-detail-meta  { font-size: 12px; color: var(--gray-600); margin-top: 5px; display: flex; gap: 16px; flex-wrap: wrap; }
  .paz-detail-note  { margin-top: 6px; font-size: 12px; color: var(--gray-600); font-style: italic; }
  .paz-detail-actions { display: flex; gap: 8px; flex-shrink: 0; }
  .paz-detail-scroll { flex: 1; overflow-y: auto; padding: 16px 20px; }
  .paz-detail-scroll::-webkit-scrollbar { width: 4px; }
  .paz-detail-scroll::-webkit-scrollbar-thumb { background: var(--gray-200); border-radius: 4px; }

  /* Referto history item */
  .referto-hist-item {
    background: #fff; border: 1px solid var(--gray-200); border-radius: var(--radius);
    padding: 12px 16px; margin-bottom: 10px;
  }
  .referto-hist-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
  .referto-hist-title  { font-size: 14px; font-weight: 600; color: var(--gray-900); }
  .referto-hist-date   { font-size: 12px; color: var(--gray-500); }
  .referto-hist-body   {
    font-size: 12px; color: var(--gray-600); line-height: 1.5;
    max-height: 56px; overflow: hidden;
    display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical;
  }
  .referto-hist-footer { display: flex; gap: 8px; margin-top: 10px; align-items: center; }
  .sede-badge { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 10px; }
  .sede-badge.roma { background: var(--blue-lt);   color: var(--blue); }
  .sede-badge.pont { background: var(--green-lt);  color: var(--green); }
  .sede-badge.lat  { background: var(--purple-lt); color: var(--purple); }
  .btn-xs {
    padding: 3px 10px; border: 1px solid var(--gray-200); border-radius: var(--radius);
    background: #fff; font-size: 11px; cursor: pointer; color: var(--gray-600); transition: all .1s;
  }
  .btn-xs:hover { background: var(--gray-100); }
  .btn-xs.sms { background: var(--purple-lt); border-color: var(--purple); color: var(--purple); }
  .btn-xs.sms:hover { background: var(--purple); color: #fff; }

  /* ── MODALS ─────────────────────────────────────── */
  .modal-overlay {
    position: fixed; inset: 0; background: rgba(15,23,42,.5);
    display: flex; align-items: center; justify-content: center; z-index: 500;
  }
  .modal-overlay.hidden { display: none; }
  .modal-box {
    background: #fff; border-radius: 10px; padding: 28px 32px;
    width: 500px; max-width: 95vw; box-shadow: var(--shadow-md);
    max-height: 90vh; overflow-y: auto;
  }
  .modal-box.sm { width: 380px; }
  .modal-title { font-size: 17px; font-weight: 700; color: var(--gray-900); margin-bottom: 20px; }
  .modal-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
  .modal-grid .full { grid-column: 1 / -1; }
  .modal-field { display: flex; flex-direction: column; gap: 4px; }
  .modal-field label { font-size: 11px; font-weight: 600; color: var(--gray-700); text-transform: uppercase; letter-spacing: .04em; }
  .modal-field input, .modal-field textarea {
    padding: 8px 10px; border: 1px solid var(--gray-200);
    border-radius: var(--radius); font-size: 13px; outline: none; font-family: inherit;
  }
  .modal-field input:focus, .modal-field textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-lt); }
  .modal-field textarea { resize: vertical; }
  .modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }
  .modal-error { font-size: 12px; color: #dc2626; margin-top: 8px; min-height: 18px; }

  .otp-result {
    background: var(--green-lt); border: 1px solid var(--green);
    border-radius: var(--radius); padding: 14px; margin-top: 14px; text-align: center;
  }
  .otp-result.dev { background: var(--blue-lt); border-color: var(--blue); }
  .otp-code { font-size: 32px; font-weight: 700; letter-spacing: .25em; color: var(--green); font-family: monospace; }
  .otp-result.dev .otp-code { color: var(--blue); }
  .otp-result p { font-size: 12px; color: var(--gray-600); margin-top: 4px; }

  /* ── EXAM NOTIFICATION BAR ─────────────────────── */
  .exam-notification {
    display: flex; align-items: center; gap: 10px;
    padding: 8px 14px; background: #fffbeb;
    border-bottom: 1px solid #fde68a; flex-shrink: 0;
    animation: slideDown .2s ease;
  }
  .exam-notification.hidden { display: none; }
  @keyframes slideDown { from { opacity:0; transform:translateY(-6px); } to { opacity:1; transform:none; } }
  .exam-notif-dot {
    width: 8px; height: 8px; border-radius: 50%;
    background: #f59e0b; flex-shrink: 0;
    box-shadow: 0 0 0 3px #fde68a;
    animation: pulse 1.8s ease-in-out infinite;
  }
  @keyframes pulse { 0%,100%{box-shadow:0 0 0 3px #fde68a} 50%{box-shadow:0 0 0 6px #fde68a} }
  .exam-notif-text { flex: 1; font-size: 13px; color: #92400e; min-width: 0; }
  .exam-notif-text strong { color: #78350f; }
  .exam-notif-count {
    font-size: 11px; background: #f59e0b; color: #fff;
    border-radius: 10px; padding: 1px 7px; font-weight: 600; flex-shrink: 0;
  }
  .btn-notif-load {
    padding: 4px 12px; background: #f59e0b; color: #fff;
    border: none; border-radius: var(--radius); font-size: 12px;
    font-weight: 600; cursor: pointer; white-space: nowrap; flex-shrink: 0;
    transition: background .15s;
  }
  .btn-notif-load:hover { background: #d97706; }
  .btn-notif-load.loading { opacity: .6; cursor: default; }
  .btn-notif-dismiss {
    background: none; border: none; color: #a78bfa; font-size: 16px;
    cursor: pointer; padding: 0 4px; line-height: 1; flex-shrink: 0;
    opacity: .6;
  }
  .btn-notif-dismiss:hover { opacity: 1; }

  /* ── EXAM CONTEXT BADGE (in exam-bar) ──────────── */
  .exam-folder-badge {
    font-size: 11px; background: #fffbeb; border: 1px solid #fde68a;
    color: #92400e; border-radius: 10px; padding: 2px 8px;
    font-weight: 600; white-space: nowrap; flex-shrink: 0;
  }

  /* ── GENERA PDF BUTTON ──────────────────────────── */
  .btn-genera-pdf {
    padding: 8px 16px; background: var(--green); color: #fff;
    border: none; border-radius: var(--radius); font-size: 13px; font-weight: 600;
    cursor: pointer; transition: background .15s;
    display: flex; align-items: center; gap: 6px;
  }
  .btn-genera-pdf:hover:not(:disabled) { background: #15803d; }
  .btn-genera-pdf:disabled { background: var(--gray-200); color: var(--gray-400); cursor: default; }
  .btn-genera-pdf.hidden { display: none; }
  .btn-genera-pdf .spinner { width:12px; height:12px; border:2px solid rgba(255,255,255,.4); border-top-color:#fff; border-radius:50%; animation:spin .6s linear infinite; display:none; }
  .btn-genera-pdf.loading .spinner { display:block; }
  .btn-genera-pdf.loading .gp-label { opacity:.7; }

  /* ── PDF PRONTO PANEL (inside actions-bar) ──────── */
  .pdf-pronto-bar {
    display: flex; align-items: center; gap: 8px;
    padding: 8px 14px; background: var(--green-lt);
    border-top: 1px solid #bbf7d0; flex-shrink: 0;
    animation: slideDown .2s ease;
  }
  .pdf-pronto-bar.hidden { display: none; }
  .pdf-pronto-label { font-size: 13px; font-weight: 600; color: var(--green); flex: 1; }

  /* ── AI BUTTON ──────────────────────────────────── */
  .btn-ai {
    padding: 8px 16px; background: var(--purple); color: #fff;
    border: none; border-radius: var(--radius); font-size: 13px; font-weight: 600;
    cursor: pointer; transition: background .15s; display: flex; align-items: center; gap: 6px;
  }
  .btn-ai:hover:not(:disabled) { background: #6d28d9; }
  .btn-ai:disabled { background: var(--gray-200); color: var(--gray-400); cursor: default; }
  .btn-ai .spinner {
    width: 12px; height: 12px; border: 2px solid rgba(255,255,255,.4);
    border-top-color: #fff; border-radius: 50%;
    animation: spin .6s linear infinite; display: none;
  }
  .btn-ai.loading .spinner { display: block; }
  .btn-ai.loading .btn-ai-label { opacity: .7; }
  @keyframes spin { to { transform: rotate(360deg); } }

  /* ── WORKLIST VIEW ──────────────────────────────── */
  .worklist-view { display: flex; flex: 1; overflow: hidden; background: var(--gray-100); }
  .worklist-view.hidden { display: none; }
  .wl-main { display: flex; flex-direction: column; flex: 1; overflow: hidden; }
  .wl-thumbnails-sidebar {
    width: 280px; min-width: 280px; flex-shrink: 0; background: #fff; border-left: 1px solid var(--gray-200);
    display: flex; flex-direction: column; overflow: hidden;
  }
  .wl-thumbnails-title { padding: 12px; font-size: 12px; font-weight: 600; color: var(--gray-700); border-bottom: 1px solid var(--gray-100); }
  .wl-thumbnails-list { flex: 1; overflow-y: auto; padding: 8px; display: grid; grid-template-columns: 1fr 1fr; gap: 4px; }
  .wl-thumb { aspect-ratio: 1; cursor: pointer; border-radius: 4px; overflow: hidden; border: 2px solid transparent; background: var(--gray-100); transition: border-color 0.2s; }
  .wl-thumb:hover { border-color: var(--blue); }
  .wl-thumb img { width: 100%; height: 100%; object-fit: cover; }
  .wl-thumbnails-sidebar.hidden { display: none; }

  .wl-toolbar {
    display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
    padding: 10px 16px; background: #fff;
    border-bottom: 1px solid var(--gray-200); flex-shrink: 0;
  }
  .wl-filter-input {
    padding: 6px 10px; border: 1px solid var(--gray-200);
    border-radius: var(--radius); font-size: 13px; outline: none;
    background: var(--gray-50); color: var(--gray-700);
  }
  .wl-filter-input:focus { border-color: var(--blue); background: #fff; }

  .wl-table-wrap { flex: 1; overflow-y: auto; }
  .wl-table-wrap::-webkit-scrollbar { width: 4px; }
  .wl-table-wrap::-webkit-scrollbar-thumb { background: var(--gray-200); border-radius: 4px; }

  .wl-table { width: 100%; border-collapse: collapse; font-size: 13px; }
  .wl-table thead th {
    padding: 9px 14px; text-align: left; background: #fff;
    border-bottom: 2px solid var(--gray-200);
    font-weight: 600; font-size: 11px; text-transform: uppercase;
    letter-spacing: .05em; color: var(--gray-500);
    position: sticky; top: 0; z-index: 1;
  }
  .wl-table tbody tr { border-bottom: 1px solid var(--gray-100); background: #fff; }
  .wl-table tbody tr:hover { background: var(--gray-50); }
  .wl-table tbody td { padding: 9px 14px; vertical-align: middle; }
  .wl-empty { text-align: center; color: var(--gray-400); padding: 60px 20px; font-style: italic; font-size: 13px; }

  /* Stato badges */
  .stato-badge {
    display: inline-flex; align-items: center;
    padding: 3px 9px; border-radius: 12px;
    font-size: 11px; font-weight: 600; white-space: nowrap;
  }
  .stato-da_refertare { background: #fee2e2; color: #dc2626; }
  .stato-bozza        { background: #fef3c7; color: #d97706; }
  .stato-refertato    { background: #ffedd5; color: #ea580c; }
  .stato-firmato      { background: var(--green-lt); color: var(--green); }
  .stato-consegnato   { background: var(--blue-lt);  color: var(--blue); }

  /* WL action buttons */
  .wl-actions-cell { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; }
  .btn-wl { padding: 4px 10px; border: 1px solid var(--gray-200); border-radius: var(--radius); background: #fff; font-size: 11px; cursor: pointer; color: var(--gray-600); white-space: nowrap; transition: all .1s; }
  .btn-wl:hover { background: var(--gray-100); }
  .btn-wl.primary  { background: var(--blue);      border-color: var(--blue);    color: #fff; }
  .btn-wl.primary:hover  { background: #1d4ed8; }
  .btn-wl.green    { background: var(--green-lt);  border-color: var(--green);   color: var(--green); }
  .btn-wl.green:hover    { background: var(--green);  color: #fff; }
  .btn-wl.purple   { background: var(--purple-lt); border-color: var(--purple);  color: var(--purple); }
  .btn-wl.purple:hover   { background: var(--purple); color: #fff; }
  .btn-wl.orange   { background: #fff7ed; border-color: #f97316; color: #ea580c; }
  .btn-wl.orange:hover   { background: #f97316; color: #fff; }

  /* Back button in header */
  .btn-rp-back {
    padding: 5px 12px; border: 1px solid var(--gray-200); border-radius: var(--radius);
    background: #fff; font-size: 13px; cursor: pointer; color: var(--gray-600);
    display: flex; align-items: center; gap: 4px; white-space: nowrap;
  }
  .btn-rp-back:hover { background: var(--gray-100); }

  /* ── BUTTONS ──────────────────────────────────── */
  .btn {
    padding: 8px 16px; border: 1px solid var(--gray-200); border-radius: var(--radius);
    background: #fff; font-size: 13px; cursor: pointer; color: var(--gray-700);
    transition: all .2s; font-weight: 500;
  }
  .btn:hover { background: var(--gray-100); border-color: var(--gray-300); }
  .btn:active { opacity: 0.85; }
  .btn.primary { background: var(--blue); border-color: var(--blue); color: #fff; }
  .btn.primary:hover { background: #1d4ed8; border-color: #1d4ed8; }
  .btn.orange { background: #f97316; border-color: #f97316; color: #fff; }
  .btn.orange:hover { background: #ea580c; border-color: #ea580c; }
  .btn.green { background: var(--green); border-color: var(--green); color: #fff; }
  .btn.green:hover { background: #15803d; border-color: #15803d; }
  .btn.full-width { width: 100%; }

  /* ── REFERTO EDITOR ────────────────────────────────────── */
  .editor-toolbar {
    padding: 12px 16px; background: #fff; border-bottom: 1px solid var(--gray-200);
    display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  }
  .editor-toolbar select, .editor-toolbar input[type="date"] {
    padding: 6px 10px; border: 1px solid var(--gray-200); border-radius: var(--radius);
    font-size: 13px; background: #fff;
  }
  .editor-form {
    flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 16px;
  }
  .editor-form fieldset {
    border: none; padding: 0; margin: 0;
  }
  .editor-form legend {
    font-size: 13px; font-weight: 600; color: var(--gray-700); margin-bottom: 8px;
    text-transform: uppercase; letter-spacing: .05em;
  }
  .editor-form textarea {
    width: 100%; padding: 10px; border: 1px solid var(--gray-200); border-radius: var(--radius);
    font-family: inherit; font-size: 13px; color: var(--gray-800); resize: vertical;
  }
  .editor-form textarea:focus {
    border-color: var(--blue); outline: none; box-shadow: 0 0 0 3px var(--blue-lt);
  }
  .editor-form textarea::placeholder { color: var(--gray-400); }

  /* ── LIBRERIA TOOLBAR ────────────────────────────────────── */
  .libreria-toolbar {
    padding: 8px; border-bottom: 1px solid var(--gray-200); flex-shrink: 0;
  }
  .libreria-toolbar input {
    width: 100%; padding: 6px 10px; border: 1px solid var(--gray-200); border-radius: var(--radius);
    font-size: 12px; background: #fff;
  }
  .libreria-toolbar input:focus { border-color: var(--blue); outline: none; }

  /* ── PAZIENTI VIEW ────────────────────────────────────── */
  .pazienti-view { display: flex; flex-direction: column; flex: 1; overflow: hidden; padding: 16px; }
  .pazienti-view.hidden { display: none; }
  .pv-toolbar { display: flex; gap: 8px; margin-bottom: 16px; }
  .pv-search { flex: 1; padding: 8px 12px; border: 1px solid var(--gray-200); border-radius: var(--radius); font-size: 13px; }
  .pv-list { flex: 1; overflow-y: auto; display: grid; gap: 8px; }

  /* ── MODAL INPUTS ────────────────────────────────────── */
  .modal-box input, .modal-box textarea {
    width: 100%; padding: 10px; border: 1px solid var(--gray-200); border-radius: var(--radius);
    font-size: 13px; margin-bottom: 10px; font-family: inherit;
  }
  .modal-box input:focus, .modal-box textarea:focus {
    border-color: var(--blue); outline: none; box-shadow: 0 0 0 3px var(--blue-lt);
  }
  .modal-box textarea { resize: vertical; }

  /* ── FORM ERRORS ────────────────────────────────────── */
  .login-error, .np-error, .otp-error {
    color: #dc2626; font-size: 12px; margin: 8px 0; min-height: 18px; text-align: center;
  }

  /* ── TOAST ────────────────────────────────────── */
  .toast {
    position: fixed; bottom: 20px; right: 20px; padding: 12px 16px;
    background: var(--green); color: #fff; border-radius: var(--radius);
    font-size: 13px; opacity: 0; transition: opacity .3s;
    z-index: 1000; max-width: 280px; word-wrap: break-word;
  }
  .toast.show { opacity: 1; }
  .toast.error { background: #dc2626; }
  .toast.warning { background: #f97316; }

  /* ── PATIENT CARD ────────────────────────────────────── */
  .paz-card {
    background: #fff; border: 1px solid var(--gray-200); border-radius: var(--radius);
    padding: 12px; cursor: pointer; transition: all .2s;
  }
  .paz-card:hover { border-color: var(--blue); box-shadow: 0 2px 8px rgba(0,0,0,0.08); }
  .paz-card-name { font-weight: 600; font-size: 14px; color: var(--gray-900); margin-bottom: 4px; }
  .paz-card-info { font-size: 12px; color: var(--gray-600); }

  /* ── REFERTO HISTORY ────────────────────────────────────── */
  .referto-hist {
    background: var(--gray-50); border-radius: var(--radius); padding: 10px;
    border-left: 3px solid var(--gray-300); margin: 4px 0; font-size: 12px;
  }
  .referto-hist-header { display: flex; justify-content: space-between; margin-bottom: 4px; }
  .referto-hist-tipo { font-weight: 600; color: var(--gray-900); }
  .referto-hist-data { color: var(--gray-600); font-size: 11px; }
  .referto-hist-text { color: var(--gray-700); margin: 4px 0; }
  .referto-hist-footer {
    display: flex; gap: 8px; align-items: center; margin-top: 8px; padding-top: 8px;
    border-top: 1px solid var(--gray-200);
  }
  .referto-hist-footer span { flex: 1; }
  .referto-hist-footer .btn-xs {
    padding: 3px 8px; font-size: 11px; border: 1px solid var(--gray-200);
    border-radius: 3px; background: #fff; cursor: pointer; color: var(--gray-700);
  }
  .referto-hist-footer .btn-xs:hover { background: var(--gray-100); }
  .referto-hist-footer .btn-xs.sms { color: var(--blue); }

  /* ── STATUS BADGE ────────────────────────────────────── */
  .stato-badge {
    display: inline-block; padding: 3px 9px; border-radius: 12px;
    font-size: 11px; font-weight: 600; white-space: nowrap;
  }
  .stato-da_refertare { background: #fee2e2; color: #dc2626; }
  .stato-bozza        { background: #fef3c7; color: #d97706; }
  .stato-refertato    { background: #ffedd5; color: #ea580c; }
  .stato-firmato      { background: var(--green-lt); color: var(--green); }
  .stato-consegnato   { background: var(--blue-lt);  color: var(--blue); }

  /* ── NO-RESULTS ────────────────────────────────────── */
  .no-results { text-align: center; color: var(--gray-400); padding: 16px; font-size: 13px; }

  /* ── LOADING ────────────────────────────────────── */
  .loading { color: var(--gray-400); font-size: 13px; }

  /* ── SEDE BADGE ────────────────────────────────────── */
  .sede-badge-roma { padding: 2px 6px; background: #dbeafe; color: #1e40af; font-size: 11px; border-radius: 3px; }
  .sede-badge-pont { padding: 2px 6px; background: #fce7f3; color: #be185d; font-size: 11px; border-radius: 3px; }
  .sede-badge-lat  { padding: 2px 6px; background: #d1e7ee; color: #0c4a6e; font-size: 11px; border-radius: 3px; }

  /* ── STATUS MESSAGE ────────────────────────────────────── */
  #status-msg {
    font-size: 12px; color: var(--gray-600); padding: 8px 0; min-height: 20px; text-align: center;
  }

  /* ── FLEX UTILITIES ────────────────────────────────────── */
  .flex { display: flex; }
  .flex-1 { flex: 1; }
  .flex-column { flex-direction: column; }
  .gap-4 { gap: 4px; }
  .gap-8 { gap: 8px; }
  .items-center { align-items: center; }
  .justify-between { justify-content: space-between; }

  /* ── HIDDEN ────────────────────────────────────── */
  .hidden { display: none !important; }

  /* ── WLROW CLICK ────────────────────────────────────── */
  .wl-tbody tr { cursor: pointer; transition: background .1s; }
  .wl-tbody tr:hover { background: var(--gray-50); }
  .btn-rp-back.hidden { display: none; }
