/* ── Variabili tema chiaro ───────────────────────────────────────────── */
:root {
  --bg:        #f0f2f7;
  --bg-2:      #ffffff;
  --panel:     #ffffff;
  --accent:    #2563eb;
  --accent-2:  #1d4ed8;
  --accent-lt: #eff6ff;
  --text:      #1e2530;
  --muted:     #64748b;
  --border:    #e2e8f0;
  --user-bg:   #eff6ff;
  --code-bg:   #f1f5f9;
  --pill-bg:   #2563eb;
  --pill-fg:   #ffffff;
  --shadow:    0 1px 4px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

body {
  display: flex;
  flex-direction: row;
  min-height: 100vh;
}

/* ── Sidebar (multi-chat) ────────────────────────────────────────── */
.sidebar {
  width: 260px;
  flex-shrink: 0;
  background: #1e293b;
  color: #cbd5e1;
  display: flex;
  flex-direction: column;
  border-right: 1px solid #334155;
  transition: margin-left 0.25s ease;
}
.sidebar[hidden] {
  margin-left: -260px;
  /* hidden attribute resets default display:none on flex containers */
  display: flex !important;
}
.sidebar-header {
  padding: 14px 12px;
  border-bottom: 1px solid #334155;
}
.new-chat-btn {
  width: 100%;
  background: transparent;
  border: 1px solid #475569;
  color: #f1f5f9;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 0.92em;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: background 0.12s, border-color 0.12s;
}
.new-chat-btn:hover {
  background: #334155;
  border-color: #64748b;
}
.new-chat-btn .plus {
  font-size: 1.2em;
  line-height: 1;
  font-weight: 400;
}
.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 6px;
}
.chat-list-item {
  padding: 9px 12px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 0.86em;
  line-height: 1.35;
  color: #cbd5e1;
  margin-bottom: 2px;
  transition: background 0.1s;
  display: flex;
  align-items: center;
  gap: 6px;
  position: relative;
}
.chat-list-item:hover {
  background: #334155;
}
.chat-list-item.active {
  background: #475569;
  color: #f8fafc;
  font-weight: 600;
}
.chat-list-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-list-delete {
  visibility: hidden;
  background: none;
  border: none;
  color: #94a3b8;
  cursor: pointer;
  font-size: 1em;
  padding: 0 4px;
  border-radius: 4px;
}
.chat-list-item:hover .chat-list-delete {
  visibility: visible;
}
.chat-list-delete:hover {
  background: #dc2626;
  color: #fff;
}
.sidebar-footer {
  padding: 10px 14px;
  border-top: 1px solid #334155;
  color: #64748b;
  font-size: 0.75em;
}

/* ── Wrapper main (a destra della sidebar) ───────────────────────── */
.main-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-width: 0;   /* permette al figlio di shrinkare correttamente */
}

/* ── Bottone toggle sidebar nella topbar ─────────────────────────── */
.sidebar-toggle-btn {
  background: none;
  border: none;
  font-size: 1.3em;
  cursor: pointer;
  color: var(--muted);
  padding: 4px 10px;
  border-radius: 6px;
  margin-right: 8px;
  transition: background 0.1s;
}
.sidebar-toggle-btn:hover {
  background: var(--bg);
  color: var(--text);
}

/* Titolo chat corrente nella topbar */
.chat-title-display {
  flex: 1;
  text-align: center;
  color: var(--muted);
  font-size: 0.9em;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 0 20px;
}

/* Indicatore di stato del router (memoria vs nuova ricerca) */
.router-status {
  margin: 16px auto;
  max-width: 860px;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 0.88em;
  display: flex;
  align-items: center;
  gap: 8px;
}
.router-status--memory {
  background: #ecfdf5;
  border: 1px solid #a7f3d0;
  color: #065f46;
}
.router-status--search {
  background: #fef3c7;
  border: 1px solid #fde68a;
  color: #78350f;
}
.router-icon { font-size: 1.1em; }

/* Responsive: sidebar a scomparsa su schermi piccoli */
@media (max-width: 768px) {
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; z-index: 100; }
}

/* ── Topbar ─────────────────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 28px;
  border-bottom: 1px solid var(--border);
  background: #ffffff;
  box-shadow: var(--shadow);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 17px;
  color: var(--text);
}

.brand-logo {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
  font-size: 16px;
  letter-spacing: -0.5px;
}

.sub { color: var(--muted); font-size: 12px; }

/* ── Area chat ──────────────────────────────────────────────────────── */
.chat {
  flex: 1;
  padding: 32px 8vw;
  overflow-y: auto;
  scroll-behavior: smooth;
}

.placeholder {
  text-align: center;
  margin: 80px auto;
  max-width: 560px;
  color: var(--muted);
}
.placeholder h1 {
  color: var(--accent);
  font-size: 44px;
  font-weight: 800;
  margin-bottom: 10px;
  letter-spacing: -1px;
}
.placeholder p { font-size: 15px; line-height: 1.7; }

/* ── Messaggi ───────────────────────────────────────────────────────── */
.msg {
  max-width: 860px;
  margin: 0 auto 22px;
  padding: 18px 22px;
  border-radius: 14px;
  line-height: 1.65;
  word-wrap: break-word;
}

.msg.user {
  background: var(--user-bg);
  border: 1px solid #bfdbfe;
  margin-left: auto;
  max-width: 680px;
  color: var(--text);
}

.msg.bot {
  background: #ffffff;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

/* ══════════════════════════════════════════════════════════════════════
   Contenuto Markdown della risposta bot — stile document viewer
   ══════════════════════════════════════════════════════════════════════ */

/* ── RISPOSTA PRINCIPALE: Paragrafi ─────────────────────────────── */
.msg.bot .body p {
  margin: 0 0 1.1em;
  line-height: 1.82;
  color: var(--text);
}
.msg.bot .body > *:first-child { margin-top: 0 !important; }

/* Titoli — gerarchia visiva chiara */
.msg.bot .body h1,
.msg.bot .body h2,
.msg.bot .body h3,
.msg.bot .body h4,
.msg.bot .body h5,
.msg.bot .body h6 {
  font-weight: 800;
  line-height: 1.3;
  margin: 1.8em 0 0.7em;
}

/* H1 — titolo principale (es. Analisi Legale) */
.msg.bot .body h1 {
  font-size: 1.3em;
  color: #1e3a8a;
  padding: 8px 14px;
  background: linear-gradient(90deg, #dbeafe 0%, #eff6ff 70%, transparent 100%);
  border-left: 5px solid var(--accent);
  border-radius: 0 6px 6px 0;
  border-bottom: none;
}

/* H2 — sezione principale (Analisi Legale è spesso un h2) */
.msg.bot .body h2 {
  font-size: 1.18em;
  color: #1e3a8a;
  padding: 6px 12px;
  background: linear-gradient(90deg, #dbeafe 0%, #eff6ff 60%, transparent 100%);
  border-left: 4px solid var(--accent);
  border-radius: 0 5px 5px 0;
  border-bottom: none;
}

/* H3 — sottosezione */
.msg.bot .body h3 {
  font-size: 1.07em;
  color: #1e40af;
  padding: 4px 10px;
  border-left: 3px solid #93c5fd;
  border-radius: 0 4px 4px 0;
  background: #f0f7ff;
  border-bottom: none;
}

/* H4-H6 */
.msg.bot .body h4 { font-size: 1em; color: #1e40af; font-weight: 700; }
.msg.bot .body h5,
.msg.bot .body h6 { font-size: 0.96em; color: var(--muted); font-weight: 700; }

/* Fallback fuori da .body */
.msg.bot h1, .msg.bot h2, .msg.bot h3 {
  color: var(--accent); font-weight: 700;
  margin: 1.4em 0 0.5em;
}
.msg.bot h1 { font-size: 1.3em; }
.msg.bot h2 { font-size: 1.18em; }
.msg.bot h3 { font-size: 1.07em; }

/* Grassetto — nero quasi puro, peso 800, nessun colore speciale né sottolineatura */
.msg.bot .body strong,
.msg.bot .body b {
  font-weight: 800;
  color: #0f172a;
}

/* Corsivo */
.msg.bot .body em,
.msg.bot .body i {
  font-style: italic;
  color: #374151;
}

/* Liste */
.msg.bot ul, .msg.bot ol { padding-left: 1.6em; margin: 0 0 1.1em; }
.msg.bot .body ul, .msg.bot .body ol { padding-left: 1.6em; margin: 0 0 1.1em; }
.msg.bot li { margin-bottom: 0.5em; line-height: 1.72; }
.msg.bot .body li > p { margin: 0; }
/* Marcatori lista blu */
.msg.bot .body ul li::marker { color: var(--accent); }
.msg.bot .body ol li::marker { color: var(--accent); font-weight: 700; }

/* Blockquote — riferimento normativo evidenziato */
.msg.bot .body blockquote {
  border-left: 4px solid var(--accent);
  background: #eff6ff;
  padding: 10px 18px;
  margin: 1.1em 0;
  border-radius: 0 8px 8px 0;
  color: #1e40af;
  font-style: italic;
  font-size: 0.96em;
}

/* Linea orizzontale */
.msg.bot .body hr {
  border: none;
  border-top: 2px solid #dbeafe;
  margin: 1.4em 0;
}

.msg.bot code {
  background: var(--code-bg);
  color: #c026d3;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.91em;
  border: 1px solid var(--border);
}

.msg.bot pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  padding: 12px 16px;
  border-radius: 8px;
  overflow-x: auto;
}

.msg.bot a { color: var(--accent); text-decoration: underline; }

/* Tabelle Markdown nella risposta */
.msg.bot table {
  border-collapse: collapse;
  width: 100%;
  margin: 14px 0;
  font-size: 0.9em;
  overflow-x: auto;
  display: block;
}
.msg.bot th, .msg.bot td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
  vertical-align: top;
  line-height: 1.5;
}
.msg.bot th {
  background: var(--bg);
  font-weight: 700;
  color: var(--text);
  font-size: 0.88em;
  text-transform: none;
  white-space: nowrap;
}
.msg.bot tr:nth-child(even) td { background: #f8fafc; }
.msg.bot tr:hover td { background: var(--accent-lt); }

/* ── Pill citazione [N] — stile apice piccolo, non interrompe il testo ── */
.pill,
.pill--legal,
.pill--fiscal {
  display: inline-block;
  color: #fff;
  padding: 0px 5px;
  margin: 0 1px;
  border-radius: 3px;
  font-size: 0.62em;
  font-weight: 700;
  cursor: pointer;
  user-select: none;
  transition: background 0.1s;
  vertical-align: super;
  line-height: 1.3;
}

/* Default e variante legale: blu (accent) */
.pill,
.pill--legal {
  background: var(--accent);
}
.pill:hover,
.pill--legal:hover {
  background: var(--accent-2);
}

/* Variante fiscale: verde, per distinguere a colpo d'occhio le fonti
   dell'ambito tributario quando le pill appaiono insieme alle legali. */
.pill--fiscal {
  background: #16a34a;
}
.pill--fiscal:hover {
  background: #15803d;
}

/* Dialog inline "Risposta veloce / Risposta dettagliata" mostrato dentro
   il pending bubble quando il router decide ricerca e serve scelta utente. */
.search-choice {
  padding: 6px 4px;
}
.search-choice-title {
  margin: 0 0 14px;
  color: var(--text);
  font-size: 0.96em;
  line-height: 1.5;
}
.search-choice-buttons {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.search-choice-btn {
  flex: 1 1 220px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border: 2px solid var(--border);
  background: #fff;
  border-radius: 10px;
  cursor: pointer;
  text-align: left;
  transition: border-color 0.15s, background 0.15s, transform 0.08s;
}
.search-choice-btn:hover {
  transform: translateY(-1px);
}
.search-choice-btn--fast:hover {
  border-color: #d97706;
  background: #fffbeb;
}
.search-choice-btn--detailed:hover {
  border-color: var(--accent);
  background: var(--accent-lt);
}
.search-choice-btn .sc-icon {
  font-size: 1.6em;
  flex-shrink: 0;
}
.search-choice-btn .sc-label {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.search-choice-btn .sc-label strong {
  color: var(--text);
  font-size: 0.95em;
}
.search-choice-btn .sc-hint {
  color: var(--muted);
  font-size: 0.82em;
  font-weight: 400;
}

/* Menu di scelta che appare quando una pill [N] senza prefisso è ambigua
   (N esiste sia in legale che fiscale). L'utente sceglie quale aprire. */
.pill-choice-menu {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  padding: 4px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 130px;
}
.pill-choice-menu button {
  background: transparent;
  border: none;
  padding: 8px 12px;
  cursor: pointer;
  border-radius: 5px;
  font-size: 0.88em;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
}
.pill-choice-menu button:hover {
  background: var(--bg);
}
.pill-choice-menu .dot {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* ── Header messaggio bot ───────────────────────────────────────────── */
.bot-header {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.bot-header::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
}

/* ── Sistema a tabs per risposte legale / fiscale ───────────────────── */
.tab-bar {
  display: flex;
  border-bottom: 2px solid var(--border);
  margin-bottom: 16px;
  gap: 0;
}

.tab-btn {
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  padding: 9px 24px 9px 16px;
  cursor: pointer;
  font-size: 0.82em;
  font-weight: 700;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  letter-spacing: 0.4px;
  text-transform: uppercase;
  transition: color 0.15s, border-color 0.15s;
  margin-bottom: -2px;   /* sovrappone il border-bottom del .tab-bar */
  white-space: nowrap;
  user-select: none;
}
.tab-btn:hover { color: var(--text); }
.tab-btn[hidden] { display: none !important; }

/* Tab LEGALE attivo */
.tab-btn--legal.tab-btn--active {
  color: #1e3a8a;
  border-bottom-color: #1e3a8a;
}
/* Tab FISCALE attivo */
.tab-btn--fiscal.tab-btn--active {
  color: #166534;
  border-bottom-color: #166534;
}

/* Pallino animato "in caricamento" mostrato sul tab mentre la risposta
   è in fase di generazione. Sostituito dal check verde quando completata. */
.tab-loading-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #f59e0b;     /* arancione: in lavorazione */
  display: inline-block;
  flex-shrink: 0;
  animation: tab-pulse 1s ease-in-out infinite;
  margin-left: 6px;
  box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.55);
  vertical-align: middle;
}
@keyframes tab-pulse {
  0%   { opacity: 0.7; transform: scale(0.85); box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.55); }
  50%  { opacity: 1;   transform: scale(1.15); box-shadow: 0 0 0 6px rgba(245, 158, 11, 0); }
  100% { opacity: 0.7; transform: scale(0.85); box-shadow: 0 0 0 0 rgba(245, 158, 11, 0); }
}

/* Check verde "completato" mostrato sul tab dopo che la risposta è stata
   ricevuta in toto. Sostituisce il tab-loading-dot quando finalize() viene
   chiamato. L'utente vede a colpo d'occhio quale risposta è pronta. */
.tab-done-check {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  background: #16a34a;
  color: #fff;
  border-radius: 50%;
  font-size: 10px;
  font-weight: 700;
  margin-left: 6px;
  flex-shrink: 0;
  line-height: 1;
  animation: tab-done-pop 0.4s ease-out;
}
@keyframes tab-done-pop {
  0%   { transform: scale(0); opacity: 0; }
  60%  { transform: scale(1.3); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

/* Pannello contenuto del tab */
.tab-pane { width: 100%; }
.tab-pane[hidden] { display: none !important; }

/* ── Badge modulo Lexroom (es. "📚 Tributario") ─────────────────── */
.module-slot {
  min-height: 0;
}
.module-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75em;
  font-weight: 500;
  color: #1e40af;
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  padding: 4px 12px 4px 9px;
  border-radius: 20px;
  margin-bottom: 12px;
  user-select: none;
  line-height: 1.3;
}
.module-badge-icon {
  font-size: 1.05em;
  line-height: 1;
}
.module-badge-label strong {
  font-weight: 700;
  color: #1e3a8a;
}

/* ── Sezione fonti in fondo al messaggio ────────────────────────────── */
.sources {
  margin-top: 20px;
  border-top: 1px solid var(--border);
  padding-top: 14px;
  font-size: 0.91em;
}
.sources h4 {
  margin: 0 0 10px;
  color: var(--muted);
  font-weight: 700;
  font-size: 0.82em;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.sources ol { padding-left: 1.3em; margin: 0; }
.sources li {
  margin-bottom: 6px;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.12s;
  line-height: 1.5;
}
.sources li:hover { color: var(--accent); }
.sources li strong { color: var(--text); }
.sources li small { color: var(--muted); font-size: 0.88em; }

/* ── Cursore lampeggiante "sta ancora scrivendo" ────────────────────── */
.stream-cursor {
  display: inline-block;
  color: var(--accent);
  animation: cur-blink 0.7s steps(1) infinite;
  margin-left: 2px;
  font-size: 1.1em;
  line-height: 1;
  vertical-align: middle;
}
@keyframes cur-blink {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0; }
}

/* ── Animazione typing ──────────────────────────────────────────────── */
.typing {
  display: inline-flex;
  gap: 5px;
  padding: 10px 12px;
}

/* Contenitore stato progresso (typing dots + etichetta testuale) */
.typing-status {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}

/* Testo di stato sotto i tre pallini */
.typing-label {
  font-size: 0.82em;
  color: var(--muted);
  margin: 0;
  padding-left: 12px;
  font-style: italic;
  line-height: 1.4;
}
.typing span {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: bounce 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.35; }
  30% { transform: translateY(-6px); opacity: 1; }
}

/* ── Barra di input ─────────────────────────────────────────────────── */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 8vw 10px;
  border-top: 1px solid var(--border);
  background: #ffffff;
}

.composer textarea {
  flex: 1;
  resize: none;
  padding: 10px 14px;
  background: var(--bg);
  color: var(--text);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  min-height: 42px;
  max-height: 160px;
  line-height: 1.45;
  transition: border-color 0.15s;
}
.composer textarea::placeholder { color: var(--muted); }
.composer textarea:focus { border-color: var(--accent); background: #fff; }

.composer button {
  width: 42px; height: 42px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  transition: background 0.12s, transform 0.08s;
  flex-shrink: 0;
}
.composer button:hover { background: var(--accent-2); transform: translateY(-1px); }
.composer button:disabled { opacity: 0.45; cursor: progress; transform: none; }

/* ── Bottone "Torna su" ─────────────────────────────────────────────── */
.scroll-top-btn {
  position: fixed;
  bottom: 76px;
  right: 22px;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(37,99,235,0.35);
  display: grid;
  place-items: center;
  z-index: 250;
  opacity: 0.88;
  transition: opacity 0.2s, transform 0.15s, background 0.12s;
}
.scroll-top-btn:hover {
  opacity: 1;
  background: var(--accent-2);
  transform: translateY(-3px);
}
.scroll-top-btn[hidden] { display: none !important; }

/* ── Footer ─────────────────────────────────────────────────────────── */
.footer {
  text-align: center;
  color: var(--muted);
  padding: 6px 0 12px;
  font-size: 11px;
}

/* ── Errore ─────────────────────────────────────────────────────────── */
.error {
  background: #fef2f2;
  border: 1px solid #fca5a5;
  color: #b91c1c;
}

/* ── Blocco errore con bottone Riprova ──────────────────────────────── */
.error-block {
  background: #fef2f2;
  border: 1px solid #fca5a5;
  border-radius: 10px;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.error-msg {
  color: #991b1b;
  line-height: 1.55;
  font-size: 0.95em;
}
.error-icon {
  font-size: 1.1em;
  margin-right: 4px;
}
.error-text {
  color: #7f1d1d;
}
.retry-btn {
  align-self: flex-start;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 18px;
  font-size: 0.92em;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.12s, transform 0.08s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.retry-btn:hover { background: var(--accent-2); transform: translateY(-1px); }
.retry-btn:active { transform: translateY(0); }

/* ── Fonte non disponibile (caricamento documento fallito) ──────────── */
.src-unavailable {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 14px;
  margin-top: 4px;
  background: #f8fafc;
  border: 1px dashed #cbd5e1;
  border-radius: 8px;
  font-size: 0.85em;
  color: #475569;
  line-height: 1.55;
}
.src-unavailable-icon {
  font-size: 1.2em;
  flex-shrink: 0;
  margin-top: 1px;
}
.src-unavailable-text strong {
  color: #334155;
  font-weight: 600;
}
.retry-source-btn {
  margin-top: 8px;
  display: inline-block;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 5px 12px;
  font-size: 0.92em;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.12s;
}
.retry-source-btn:hover { background: var(--accent-2); }

/* ── Banner "Elaborazione in corso…" (durante streaming) ───────────── */
.progress-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  margin: 0 0 12px;
  background: linear-gradient(90deg, #eff6ff 0%, #f0f9ff 100%);
  border: 1px solid #bfdbfe;
  border-radius: 10px;
  font-size: 0.85em;
  color: #1e3a8a;
  font-style: italic;
  animation: progress-pulse 2s ease-in-out infinite;
}
.progress-spinner {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid #bfdbfe;
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
  display: inline-block;
}
@keyframes spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}
@keyframes progress-pulse {
  0%, 100% { opacity: 0.85; }
  50%       { opacity: 1; }
}

/* ── Avviso "risposta incompleta" (non bloccante) ───────────────────── */
.incomplete-notice {
  margin: 18px 0 8px;
  padding: 14px 16px;
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.incomplete-msg {
  color: #78350f;
  font-size: 0.92em;
  line-height: 1.55;
}
.incomplete-icon {
  font-size: 1.05em;
  margin-right: 4px;
}
.retry-btn--soft {
  background: #d97706;
  align-self: flex-start;
}
.retry-btn--soft:hover { background: #b45309; }

/* ══ Pannello dettaglio fonte ════════════════════════════════════════ */
.src-panel {
  position: fixed;
  top: 0; right: 0;
  width: 460px;
  max-width: 94vw;
  height: 100vh;
  background: #ffffff;
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 300;
  box-shadow: var(--shadow-lg);
  animation: src-slide-in 0.2s ease;
}
.src-panel[hidden] { display: none !important; }

@keyframes src-slide-in {
  from { transform: translateX(100%); opacity: 0; }
  to   { transform: translateX(0);    opacity: 1; }
}

/* Barra navigazione fonti */
.src-panel-nav {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}

.src-nav-btn {
  background: #ffffff;
  border: 1px solid var(--border);
  color: var(--text);
  width: 32px; height: 32px;
  border-radius: 7px;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.1s, border-color 0.1s;
}
.src-nav-btn:hover { background: var(--accent-lt); border-color: var(--accent); color: var(--accent); }
.src-nav-btn:disabled { opacity: 0.35; cursor: default; }

#src-counter {
  flex: 1;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  user-select: none;
}

.src-close-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 17px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 7px;
  transition: background 0.1s, color 0.1s;
  line-height: 1;
}
.src-close-btn:hover { background: #fee2e2; color: #dc2626; }

/* Corpo del pannello */
.src-panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px 20px 36px;
}

/* Badge fonte documento */
.src-doc-badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin-bottom: 12px;
}
.src-doc-badge::before {
  content: "JL";
  background: var(--accent);
  color: #fff;
  padding: 2px 7px;
  border-radius: 4px;
  font-size: 10px;
  letter-spacing: 0.3px;
}

/* Titolo documento — con icona libro come Sapient-IA */
.src-doc-title {
  display: flex;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 6px;
  line-height: 1.4;
}
.src-title-text {
  font-size: 0.97em;
  font-weight: 700;
  color: var(--text);
}
.src-title-text::before {
  content: "📋 ";
}

/* Breadcrumb */
.src-doc-path {
  font-size: 0.82em;
  color: var(--muted);
  line-height: 1.65;
  margin-bottom: 16px;
}

/* Separatore */
.src-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0 0 16px;
}

/* Testo corpo fonte */
.src-doc-text {
  font-size: 0.91em;
  color: var(--text);
  line-height: 1.75;
}

/* Etichetta "Contenuto del paragrafo" — sottile, grigio */
.src-content-label {
  display: block;
  font-size: 0.7em;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--muted);
  margin-bottom: 10px;
}

/* ══════════════════════════════════════════════════════════════════
   PANNELLO FONTE — stile Sapient-IA
   ══════════════════════════════════════════════════════════════════ */

/* Contenitore prosa principale */
.src-prose {
  font-size: 0.93em;
  color: var(--text);
  line-height: 1.82;
}

/* Paragrafo normale — testo pulito, giustificato, nessuno sfondo */
.src-prose-p {
  margin: 0 0 1em;
  padding: 0;
  background: none;
  border: none;
  border-radius: 0;
  text-align: justify;
}

/* Paragrafo vuoto / raccordo */
.src-prose-p.src-empty {
  color: var(--muted);
  font-style: italic;
}

/* Grassetti — solo peso, nessuno sfondo colorato */
.src-prose strong,
.src-prose b,
.src-prose-p strong,
.src-prose-p b {
  font-weight: 700;
  color: inherit;
  background: none;
  padding: 0;
}

/* ── Tabelle ────────────────────────────────────────────────────── */
.src-prose-table {
  border-collapse: collapse;
  width: 100%;
  margin: 0.8em 0 1.2em;
  font-size: 0.88em;
}
.src-prose-table td,
.src-prose-table th {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
  vertical-align: top;
  line-height: 1.6;
}
.src-prose-table th {
  background: #f1f5f9;
  font-weight: 700;
  color: var(--text);
}
.src-prose-table tr:nth-child(even) td { background: #f8fafc; }

/* ── Numero paragrafo — stile Sapient-IA: testo rosa, non badge ─── */
.src-para-num {
  display: block;
  color: #c2185b;
  font-weight: 700;
  font-size: 0.88em;
  margin: 18px 0 6px;
  padding: 0;
  background: none;
  border: none;
  border-radius: 0;
  letter-spacing: 0;
  text-transform: none;
}

.src-doc-text strong { font-weight: 700; }

/* ── Etichetta sezione (es. "PRECISAZIONI") — stile Sapient-IA ──── */
.src-section-title {
  display: inline-block;
  font-size: 0.7em;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: #c2185b;
  background: #fce4ec;
  margin: 14px 0 8px;
  padding: 2px 8px;
  border-radius: 3px;
}

/* ── Sottotitolo "Termine:" ─────────────────────────────────────── */
.src-subheading {
  display: block;
  font-size: 0.93em;
  font-weight: 700;
  color: var(--text);
  margin: 12px 0 4px;
  padding: 0;
  background: none;
  border: none;
  border-radius: 0;
}
.src-subheading::after {
  content: ":";
  color: var(--muted);
  margin-left: 1px;
}

/* ── Elenco puntato — trattino semplice come Sapient-IA ─────────── */
.src-list {
  margin: 0.5em 0 1em;
  padding: 0;
  list-style: none;
}
.src-list li {
  position: relative;
  padding: 2px 0 2px 16px;
  margin-bottom: 5px;
  line-height: 1.75;
  background: none;
  border-radius: 0;
}
.src-list li::before {
  content: "-";
  position: absolute;
  left: 0;
  top: 2px;
  color: var(--text);
  font-weight: normal;
  font-size: 1em;
}

/* ── Parola-chiave di avviso — solo grassetto ────────────────────── */
.src-kw-alert {
  font-weight: 700;
  color: inherit;
  background: none;
  border: none;
  border-radius: 0;
  padding: 0;
  font-size: inherit;
}

/* ── Numeri (%, €) — solo grassetto ─────────────────────────────── */
.src-num {
  font-weight: 700;
  color: inherit;
}

/* ── Anno — testo normale ────────────────────────────────────────── */
.src-year {
  color: inherit;
  font-weight: inherit;
}

/* ── Riferimenti normativi — testo blu semplice (nessun badge) ───── */
/* In Sapient-IA appaiono come link blu; qui testo blu senza sfondo  */
.src-ref-art,
.src-ref-norm,
.src-ref-sent,
.src-ref-circ {
  display: inline;
  color: #1d4ed8;
  font-weight: normal;
  background: none;
  border-radius: 0;
  padding: 0;
  font-size: inherit;
}

/* Sezione link normativi */
.src-links {
  margin-top: 20px;
  padding-top: 14px;
  border-top: 1px dashed var(--border);
}
.src-links-title {
  font-size: 0.76em;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--muted);
  margin-bottom: 8px;
}
/* Badge riferimento normativo — cliccabili per aprire il modal */
.src-link-item {
  display: inline-block;
  background: var(--accent-lt);
  color: var(--accent);
  border: 1px solid #bfdbfe;
  font-size: 0.82em;
  font-weight: 500;
  padding: 3px 10px;
  border-radius: 20px;
  margin: 0 4px 6px 0;
  cursor: pointer;
  user-select: none;
  transition: background 0.12s, border-color 0.12s;
}
.src-link-item:hover {
  background: #dbeafe;
  border-color: var(--accent);
}

/* ══ Modal documento normativo ═══════════════════════════════════════ */
#doc-modal {
  position: fixed;
  inset: 0;
  z-index: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}
#doc-modal[hidden] { display: none !important; }

.doc-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(2px);
}

.doc-modal-box {
  position: relative;
  width: min(92vw, 900px);
  height: 82vh;
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: modal-in 0.18s ease;
}
@keyframes modal-in {
  from { transform: scale(0.95); opacity: 0; }
  to   { transform: scale(1);    opacity: 1; }
}

.doc-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}
.doc-modal-label {
  font-size: 0.88em;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.doc-modal-close {
  background: transparent;
  border: none;
  font-size: 17px;
  color: var(--muted);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  transition: background 0.1s, color 0.1s;
  line-height: 1;
}
.doc-modal-close:hover { background: #fee2e2; color: #dc2626; }

.doc-modal-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  flex: 1;
  color: var(--muted);
  font-size: 0.95em;
}
.doc-modal-loading p { margin: 0; }

.doc-modal-content {
  flex: 1;
  width: 100%;
  overflow-y: auto;
  padding: 24px 32px 32px;
  display: none;   /* nascosto finché non carica */
  font-size: 0.93em;
  line-height: 1.75;
  color: var(--text);
}

/* Stili per il contenuto estratto dal documento */
.doc-modal-content p   { margin: 0 0 0.9em; }
.doc-modal-content h1,
.doc-modal-content h2,
.doc-modal-content h3,
.doc-modal-content h4  { color: var(--accent); margin: 1.3em 0 0.5em; font-weight: 700; }
.doc-modal-content h1  { font-size: 1.15em; }
.doc-modal-content h2  { font-size: 1.05em; }
.doc-modal-content h3,
.doc-modal-content h4  { font-size: 0.97em; }
.doc-modal-content ul,
.doc-modal-content ol  { padding-left: 1.6em; margin: 0 0 0.9em; }
.doc-modal-content li  { margin-bottom: 0.3em; }
.doc-modal-content strong { font-weight: 700; }
.doc-modal-content em     { font-style: italic; }
/* Link nel documento: visibili come testo sottolineato ma NON cliccabili
   (l'utente vede il riferimento normativo ma non esce dal modale) */
.doc-modal-content a      {
  color: var(--accent);
  text-decoration: underline;
  pointer-events: none;   /* non cliccabile */
  cursor: text;
}
/* Riferimenti normativi convertiti da <a> a <span class="doc-ref"> */
.doc-modal-content .doc-ref {
  color: var(--accent);
  text-decoration: underline;
  font-style: normal;
}
.doc-modal-content table  { border-collapse: collapse; width: 100%; margin: 0.8em 0; font-size: 0.9em; }
.doc-modal-content td,
.doc-modal-content th     { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.doc-modal-content th     { background: var(--bg); font-weight: 700; }
.doc-modal-content hr     { border: none; border-top: 1px solid var(--border); margin: 1em 0; }
/* Numeri di paragrafo (4-6 cifre) in evidenza */
.doc-modal-content .src-para-num { color: #9333ea; font-weight: 700; font-size: 0.85em; }

/* ══ Pannello "Domanda ottimizzata" — uno per tab ═══════════════════════ */
/* Ogni tab (Fiscale / Legale) ha il proprio slot in cima, sopra il corpo
   della risposta. Così la domanda ottimizzata è contestuale al contenuto. */
.tab-enhanced-slot {
  margin-bottom: 12px;
}
.tab-enhanced-slot:empty { display: none; }

.enhanced-query-panel {
  margin: 0;
  border: 1px solid #c7d7f9;
  border-radius: 10px;
  background: #f0f5ff;
  overflow: hidden;
  font-size: 0.86em;
}

/* Variante Legale (blu) */
.enhanced-query-panel--legal {
  border-color: #c7d7f9;
  background: #f0f5ff;
}
.enhanced-query-panel--legal .enhanced-query-summary { color: #1e3a8a; }
.enhanced-query-panel--legal .enhanced-query-summary:hover { background: #dbeafe; }

/* Variante Fiscale (verde) — distingue visivamente i due prompt specializzati */
.enhanced-query-panel--fiscal {
  border-color: #bbf7d0;
  background: #f0fdf4;
}
.enhanced-query-panel--fiscal .enhanced-query-summary { color: #166534; }
.enhanced-query-panel--fiscal .enhanced-query-summary:hover { background: #dcfce7; }
.enhanced-query-panel--fiscal .enhanced-query-body {
  background: #f7fdf9;
  border-top-color: #bbf7d0;
}
.enhanced-query-panel--fiscal .enhanced-query-body strong {
  color: #14532d;
}

.enhanced-query-summary {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 9px 13px;
  cursor: pointer;
  list-style: none;
  user-select: none;
  color: #2563eb;
  font-weight: 600;
}
.enhanced-query-summary::-webkit-details-marker { display: none; }
.enhanced-query-summary::marker { display: none; }

.enhanced-query-summary:hover {
  background: #dbeafe;
}

.eq-icon { font-size: 1em; flex-shrink: 0; }

.eq-label { flex: 1; }

.eq-toggle-hint {
  font-size: 0.78em;
  font-weight: 400;
  color: #6b85be;
  font-style: italic;
}

/* Quando il details è aperto: nascondi "clicca per vedere" */
.enhanced-query-panel[open] .eq-toggle-hint { display: none; }

.enhanced-query-body {
  padding: 14px 18px 16px;
  border-top: 1px solid #c7d7f9;
  background: #f8faff;
  line-height: 1.65;
  color: var(--text);
  font-size: 0.93em;
  overflow-x: auto;
}

/* Stili Markdown nel body della domanda ottimizzata */
.enhanced-query-body h1,
.enhanced-query-body h2,
.enhanced-query-body h3,
.enhanced-query-body h4 {
  color: #1d4ed8;
  margin: 0.9em 0 0.4em;
  font-weight: 700;
}
.enhanced-query-body h1 { font-size: 1.1em; }
.enhanced-query-body h2 { font-size: 1.02em; }
.enhanced-query-body h3,
.enhanced-query-body h4 { font-size: 0.95em; }
.enhanced-query-body p  { margin: 0 0 0.7em; }
.enhanced-query-body ul,
.enhanced-query-body ol { padding-left: 1.5em; margin: 0 0 0.7em; }
.enhanced-query-body li { margin-bottom: 0.25em; }
.enhanced-query-body strong { font-weight: 700; color: #1e3a8a; }
.enhanced-query-body em { font-style: italic; color: #374151; }
.enhanced-query-body hr {
  border: none;
  border-top: 1px solid #bfdbfe;
  margin: 0.9em 0;
}
.enhanced-query-body code {
  background: #e0eaff;
  padding: 1px 5px;
  border-radius: 4px;
  font-family: monospace;
  font-size: 0.9em;
}
