/* ============================================================
   AI ChatBot — design system (white-label)
   Palette neutra di default: i colori del cliente arrivano da
   /branding/theme.css, generato da client/config.json ("colors"),
   che sovrascrive le variabili qui sotto. Gli accenti (--accent-*)
   colorano lo spinner di caricamento e i dettagli.
   ============================================================ */
* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --ink: #2a2e33;            /* testo principale */
  --paper: #f4f6f8;          /* fondo dell'app */
  --surface: #ffffff;
  --surface-2: #fafbfc;
  --border: #e3e6ea;
  --border-strong: #d2d7dd;
  --muted: #6b7480;
  --primary: #4a6fa5;          /* colore del brand (accessibile su bianco) */
  --primary-dark: #3c5c8a;
  --primary-tint: #eaf0f7;
  --accent-1: #f2af0d;
  --accent-2: #74c4eb;
  --accent-3: #d84242;
  --danger: #b3261e;
  --radius: 10px;
  --shadow-sm: 0 1px 2px rgba(35, 40, 43, .06);
  --shadow-md: 0 6px 24px rgba(35, 40, 43, .08);
  --shadow-lg: 0 18px 50px rgba(35, 40, 43, .18);
}

html { font-size: 15px; }

body {
  font-family: "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--paper);
  color: var(--ink);
  height: 100vh;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { font-weight: 650; letter-spacing: -0.015em; }

.ic { display: inline-block; vertical-align: -3px; flex-shrink: 0; }

::selection { background: color-mix(in srgb, var(--primary) 22%, white); }

*:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: 4px;
}

/* Scrollbar discreta */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 6px; border: 2px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-thumb:hover { background-color: var(--muted); }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Login ---------- */
.auth-wrap {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2.4rem 2.2rem;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-md);
}
.auth-logo { display: block; height: var(--logo-h-auth, 26px); margin: 0 auto 1.6rem; }
.auth-card h1 { font-size: 1.15rem; margin-bottom: .3rem; text-align: center; }
.auth-card p.subtitle { color: var(--muted); font-size: .88rem; margin-bottom: 1.5rem; text-align: center; }
.auth-card label { display: block; font-size: .82rem; font-weight: 500; margin: .9rem 0 .3rem; color: var(--muted); }
.auth-card input {
  width: 100%;
  padding: .62rem .75rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  font-size: .93rem;
  background: var(--surface);
  transition: border-color .15s, box-shadow .15s;
}
.auth-card input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-tint); }
.auth-card button {
  width: 100%;
  margin-top: 1.4rem;
  padding: .7rem;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: .95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s;
}
.auth-card button:hover { background: var(--primary-dark); }
.auth-toggle { margin-top: 1.1rem; text-align: center; font-size: .84rem; color: var(--muted); }
.auth-toggle a { color: var(--primary-dark); cursor: pointer; font-weight: 500; }
.error-msg { color: var(--danger); font-size: .84rem; margin-top: .8rem; min-height: 1.2em; }
.ok-msg { color: var(--primary-dark); font-size: .84rem; margin-top: .4rem; }

/* Accesso con Google */
.google-btn {
  display: flex; align-items: center; justify-content: center; gap: .55rem;
  width: 100%; padding: .6rem .9rem; margin-top: .2rem;
  border: 1px solid var(--border); border-radius: 8px;
  background: #fff; color: var(--ink); font-size: .92rem; font-weight: 500;
  cursor: pointer; text-decoration: none;
}
.google-btn:hover { background: #f6f6f6; }
.auth-divider {
  display: flex; align-items: center; gap: .7rem;
  margin: .9rem 0 .4rem; color: var(--muted); font-size: .8rem;
}
.auth-divider::before, .auth-divider::after {
  content: ""; flex: 1; height: 1px; background: var(--border);
}

/* ---------- Layout con sidebar ---------- */
.app-layout { display: flex; height: 100vh; }

.sidebar {
  width: 264px;
  flex-shrink: 0;
  background: var(--surface-2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width .18s ease;
}
.sidebar.collapsed { width: 0; border-right: none; }
.sidebar-header, .conv-list { min-width: 264px; }  /* niente re-wrap durante l'animazione */
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .85rem .9rem;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: .88rem;
}
.conv-list { flex: 1; overflow-y: auto; padding: .5rem; }
.conv-item {
  display: flex;
  align-items: center;
  gap: .3rem;
  padding: .52rem .65rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: .86rem;
  color: var(--ink);
  transition: background .12s;
}
.conv-item:hover { background: #f0f3ec; }
.conv-item.active { background: var(--primary-tint); font-weight: 500; }
.conv-title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-del {
  border: none;
  background: none;
  cursor: pointer;
  color: var(--muted);
  opacity: 0;
  flex-shrink: 0;
  padding: 2px;
  border-radius: 5px;
  transition: opacity .12s, color .12s;
}
.conv-item:hover .conv-del { opacity: .7; }
.conv-del:hover { opacity: 1; color: var(--danger); }
.conv-empty { color: var(--muted); font-size: .85rem; padding: 1.2rem; text-align: center; }

/* ---------- Barra superiore: navigazione a icone ---------- */
/* Le voci sono quattro e sempre le stesse: una volta imparate, l'etichetta è
   rumore. Resta però raggiungibile passandoci sopra, e il nome per intero vive
   in aria-label/data-label per chi usa uno screen reader. */
.topbar-nav { display: flex; align-items: center; gap: .25rem; }
.topbar-nav .nav-link { border-color: transparent; background: transparent; }
.topbar-nav .nav-link:hover { background: var(--hover, rgba(0, 0, 0, .05)); }

/* Etichetta a comparsa: solo dove c'è un puntatore, perché su un touch
   l'hover resta "appiccicato" dopo il tocco e l'etichetta non se ne va più. */
@media (hover: hover) and (pointer: fine) {
  .topbar [data-label] { position: relative; }
  .topbar [data-label]::after {
    content: attr(data-label);
    position: absolute;
    top: calc(100% + 6px);
    left: 50%;
    transform: translateX(-50%) translateY(-3px);
    padding: .22rem .5rem;
    border-radius: 6px;
    background: var(--text, #1c1c1c);
    color: var(--surface, #fff);
    font-size: .74rem;
    line-height: 1.3;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity .13s ease, transform .13s ease;
    z-index: 40;
  }
  .topbar [data-label]:hover::after,
  .topbar [data-label]:focus-visible::after {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

/* Stato del collegamento a Google: un pallino sull'icona. Verde = collegato.
   Sostituisce il pulsante "Collega Google", che occupava la barra per
   un'operazione che si fa una volta sola. */
.drive-badge { position: relative; }
.drive-badge .drive-dot {
  position: absolute;
  right: 1px;
  bottom: 1px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--muted, #8a8a8a);
  border: 1.5px solid var(--surface, #fff);
}
/* Non collegato: il marchio è spento, così la differenza si coglie senza
   dover distinguere il colore di un pallino di nove pixel. */
.drive-badge .marchio { filter: grayscale(1); opacity: .5; }
.drive-badge.collegato .marchio { filter: none; opacity: 1; }
.drive-badge.collegato .drive-dot { background: #1e8e3e; }
/* Il segno di spunta: una "L" ruotata di 45°, che è il modo affidabile di
   disegnarlo. Col trucco dei due gradienti incrociati usciva una X — cioè il
   simbolo opposto a quello che deve dire. */
.drive-badge.collegato .drive-dot::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 45%;
  width: 2.5px;
  height: 5px;
  border: solid #fff;
  border-width: 0 1.5px 1.5px 0;
  transform: translate(-50%, -50%) rotate(45deg);
}

/* Menu Impostazioni: sempre a tendina, anche da computer. Le voci che
   contiene si usano di rado (collegamento, password, uscita) e tenerle in
   vista costava spazio a quelle che si usano ogni giorno. */
.topbar-extra {
  display: none;
  position: absolute;
  top: 52px;
  right: .8rem;
  z-index: 45;
  flex-direction: column;
  align-items: stretch;
  gap: .3rem;
  min-width: 210px;
  padding: .45rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .16);
}
header.topbar.menu-open .topbar-extra { display: flex; }
.topbar-extra .btn { justify-content: flex-start; border-color: transparent; }
.topbar-extra .btn:hover { background: var(--hover, rgba(0, 0, 0, .05)); }

/* ---------- Piè di risposta: fonti e voto ---------- */
.reply-footer {
  display: flex;
  align-items: center;
  gap: .6rem;
  flex-wrap: wrap;
  margin-top: .55rem;
  padding-top: .45rem;
  border-top: 1px solid var(--border);
}
.reply-footer .sources { flex: 1; min-width: 0; font-size: .78rem; color: var(--muted); }
.reply-footer .sources summary { cursor: pointer; list-style: none; }
.reply-footer .sources summary::-webkit-details-marker { display: none; }
.reply-footer .sources summary::before { content: '▸ '; }
.reply-footer .sources[open] summary::before { content: '▾ '; }
.reply-footer .sources ul { margin: .35rem 0 0; padding-left: 1.1rem; }
.reply-footer .sources li { margin: .12rem 0; }
.vote { display: flex; gap: .2rem; flex-shrink: 0; }
.vote-btn {
  border: 1px solid transparent;
  background: none;
  cursor: pointer;
  font-size: .9rem;
  line-height: 1;
  padding: .22rem .35rem;
  border-radius: 6px;
  opacity: .45;
  transition: opacity .12s, background .12s, border-color .12s;
}
.vote-btn:hover { opacity: 1; background: var(--surface-2); }
.vote-btn.on { opacity: 1; border-color: var(--border); background: var(--primary-tint); }

/* Pulsante invio che diventa "interrompi" mentre la risposta arriva */
#send-btn.stopping { background: var(--danger, #b3261e); }
#send-btn.stopping [data-ic], #send-btn.stopping svg { display: none; }
#send-btn.stopping::after {
  content: '';
  width: 11px;
  height: 11px;
  border-radius: 2px;
  background: currentColor;
}

/* Da telefono la sidebar diventa un pannello a scomparsa sopra la chat:
   nascondendola e basta, le conversazioni erano irraggiungibili da mobile. */
.sidebar-backdrop { display: none; }
@media (max-width: 760px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 40;
    width: min(84vw, 300px);
    box-shadow: 0 0 28px rgba(0, 0, 0, .22);
    transform: translateX(0);
    transition: transform .2s ease;
  }
  .sidebar.collapsed {
    width: min(84vw, 300px);
    transform: translateX(-102%);
    border-right: 1px solid var(--border);
  }
  /* Col pannello aperto il pulsante che lo apre resta sotto: la chiusura deve
     stare dentro il pannello stesso */
  #sidebar-close { display: inline-flex; }
  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 39;
    background: rgba(0, 0, 0, .38);
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s ease;
  }
  body.sidebar-open .sidebar-backdrop { opacity: 1; pointer-events: auto; }
  /* Barra superiore: il menu a tendina vale a ogni dimensione (vedi sopra),
     qui si stringono solo le spaziature e si toglie il nome dell'app, che è
     la cosa meno utile quando lo spazio è poco. */
  header.topbar { padding: 0 .5rem; gap: .25rem; }
  header.topbar .topbar-extra { right: .5rem; }
  header.topbar .brand-app { display: none; }
  .topbar-nav { gap: .1rem; }
}
@media (min-width: 761px) { #sidebar-close { display: none; } }

/* ---------- Struttura pagina chat ---------- */
.chat-layout { display: flex; flex-direction: column; height: 100vh; flex: 1; min-width: 0; }

header.topbar {
  display: flex;
  align-items: center;
  gap: .55rem;
  padding: 0 1.1rem;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  /* Il menu Impostazioni si posiziona rispetto a questa barra. Senza, si
     ancora al primo antenato posizionato — che oggi sarebbe la pagina, e
     tornerebbe giusto solo per la coincidenza che la barra sta in cima. */
  position: relative;
}
.brand { display: flex; align-items: center; gap: .7rem; flex: 1; min-width: 0; overflow: hidden; }
.brand img { height: var(--logo-h, 19px); display: block; flex-shrink: 0; }
.brand .brand-app {
  font-size: .76rem;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: .08em;
  text-transform: uppercase;
  padding-left: .7rem;
  border-left: 1px solid var(--border);
  white-space: nowrap;
  flex-shrink: 0;
}
@media (max-width: 1240px) { .brand .brand-app { display: none; } }
header.topbar h1 { font-size: 1rem; flex: 1; white-space: nowrap; }
header.topbar .user-name { color: var(--muted); font-size: .82rem; white-space: nowrap; margin-right: .2rem; }
@media (max-width: 1100px) { header.topbar .user-name { display: none; } }

.btn {
  display: inline-flex;
  align-items: center;
  gap: .42rem;
  padding: .46rem .8rem;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  border-radius: var(--radius);
  font-size: .82rem;
  font-weight: 500;
  cursor: pointer;
  color: var(--ink);
  text-decoration: none;      /* i link-bottone non sono sottolineati */
  white-space: nowrap;
  transition: background .13s, border-color .13s, color .13s;
}
.btn:hover { background: var(--surface-2); border-color: var(--muted); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn.danger { color: var(--danger); border-color: #e5c3c0; }
.btn.danger:hover { background: #fdf0ef; border-color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.icon-only { padding: .46rem .5rem; }
.btn[disabled] { opacity: .55; cursor: default; }

.badge-ok { color: var(--primary-dark); font-size: .8rem; display: inline-flex; align-items: center; gap: .3rem; }

.model-select {
  padding: .42rem .5rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  font-size: .78rem;
  background: var(--surface);
  color: var(--ink);
  max-width: 200px;
  cursor: pointer;
}

/* ---------- Corpo chat + anteprima ---------- */
.chat-body { display: flex; flex: 1; min-height: 0; }
.chat-main { display: flex; flex-direction: column; flex: 1; min-width: 0; }

main.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.3rem 1.3rem 1rem;
  display: flex;
  flex-direction: column;
  gap: .85rem;
  max-width: 920px;
  width: 100%;
  margin: 0 auto;
}
/* Con anteprima aperta la chat usa tutta la colonna disponibile */
.chat-body.with-preview main.messages { max-width: none; }

.msg {
  max-width: 82%;
  padding: .72rem 1rem;
  border-radius: 14px;
  line-height: 1.5;
  font-size: .92rem;
  word-wrap: break-word;
  flex-shrink: 0;
}
.msg.user {
  align-self: flex-end;
  background: var(--ink);
  color: #f4f6f1;
  border-bottom-right-radius: 5px;
  white-space: pre-wrap;
}
.msg.bot {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
  box-shadow: var(--shadow-sm);
}
.msg.user img {
  max-width: min(100%, 440px);
  max-height: 340px;
  border-radius: 8px;
  display: block;
  margin-bottom: .4rem;
  cursor: zoom-in;
}

/* --- Markdown nei messaggi del bot --- */
.msg.bot > *:first-child { margin-top: 0; }
.msg.bot > *:last-child { margin-bottom: 0; }
.msg.bot p { margin: .45rem 0; }
.msg.bot h1, .msg.bot h2, .msg.bot h3, .msg.bot h4 { margin: .85rem 0 .35rem; line-height: 1.3; }
.msg.bot h1 { font-size: 1.12rem; }
.msg.bot h2 { font-size: 1.04rem; }
.msg.bot h3 { font-size: .97rem; }
.msg.bot h4 { font-size: .92rem; }
.msg.bot ul, .msg.bot ol { margin: .45rem 0; padding-left: 1.4rem; }
.msg.bot li { margin: .16rem 0; }
.msg.bot li > ul, .msg.bot li > ol { margin: .1rem 0; }
.msg.bot img { max-width: 100%; border-radius: 8px; margin: .5rem 0; }
.msg.bot a { color: var(--primary-dark); font-weight: 500; }
.msg.bot hr { border: none; border-top: 1px solid var(--border); margin: .8rem 0; }
.msg.bot blockquote {
  margin: .5rem 0;
  padding: .25rem .85rem;
  border-left: 3px solid var(--primary);
  color: var(--muted);
  background: var(--surface-2);
  border-radius: 0 6px 6px 0;
}
.msg.bot code {
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: .05rem .32rem;
  font-size: .84em;
  font-family: "Cascadia Code", Consolas, monospace;
}
.msg.bot pre {
  background: #262b2e;
  color: #e9ede6;
  border-radius: 8px;
  padding: .7rem .9rem;
  margin: .5rem 0;
  overflow-x: auto;
}
.msg.bot pre code { background: none; border: none; padding: 0; color: inherit; font-size: .82rem; }
.msg.bot .table-wrap { overflow-x: auto; margin: .5rem 0 0; border: 1px solid var(--border); border-radius: 8px; }
.msg.bot table { border-collapse: collapse; width: max-content; min-width: 100%; }
.msg.bot th { background: var(--surface-2); font-weight: 600; text-align: left; }
.msg.bot td, .msg.bot th { border-bottom: 1px solid var(--border); padding: .4rem .65rem; font-size: .86rem; vertical-align: top; }
.msg.bot tr:last-child td { border-bottom: none; }
.msg.bot del { color: var(--muted); }

/* --- Avanzamento: anello "sphaera" + passi --- */
.msg.progress { color: var(--ink); min-width: 280px; }
.msg.progress .step { display: flex; align-items: center; gap: .55rem; padding: .14rem 0; font-size: .88rem; }
.msg.progress .step.done { color: var(--muted); }
.msg.progress .check { color: var(--primary); display: inline-flex; width: 15px; flex-shrink: 0; }
/* Il tempo sta a destra del passo in corso: si legge se lo cerchi, non pesa se no. */
.msg.progress .step-time { margin-left: auto; padding-left: .8rem; color: var(--muted); font-size: .8rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
.msg.progress .step-nota { margin-top: .5rem; padding-left: 1.55rem; color: var(--muted); font-size: .82rem; line-height: 1.45; }
.spinner {
  width: 15px;
  height: 15px;
  flex-shrink: 0;
  border-radius: 50%;
  background: conic-gradient(var(--primary), var(--accent-1), var(--accent-3), var(--accent-2), var(--primary));
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 2.6px), #000 calc(100% - 2.4px));
  mask: radial-gradient(farthest-side, transparent calc(100% - 2.6px), #000 calc(100% - 2.4px));
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2.5s; }
}

/* ---------- Composer ---------- */
footer.composer {
  padding: .8rem 1.3rem 1.1rem;
  background: transparent;
  flex-shrink: 0;
}
.composer-inner {
  display: flex;
  align-items: flex-end;
  gap: .5rem;
  max-width: 920px;
  margin: 0 auto;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  padding: .45rem;
  box-shadow: var(--shadow-sm);
  transition: border-color .15s, box-shadow .15s;
}
.composer-inner:focus-within { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-tint); }
.chat-body.with-preview .composer-inner { max-width: none; }
.composer-inner textarea {
  flex: 1;
  resize: none;
  padding: .5rem .55rem;
  border: none;
  font-size: .93rem;
  font-family: inherit;
  max-height: 140px;
  background: transparent;
}
.composer-inner textarea:focus { outline: none; }
.composer-inner textarea::placeholder {
  white-space: nowrap;        /* il suggerimento non va mai a capo: */
  overflow: hidden;           /* se non ci sta, viene troncato con i tre punti */
  text-overflow: ellipsis;
  color: var(--muted);
}
.composer-inner .btn { border-color: transparent; color: var(--muted); }
.composer-inner .btn:hover { background: var(--surface-2); color: var(--ink); }
.composer-inner .btn.primary { color: #fff; }
.composer-inner .btn.primary:hover { background: var(--primary-dark); }
.composer-inner .btn.toggle-on {
  background: var(--primary-tint);
  color: var(--primary-dark);
  border-color: var(--primary);
}

.attach-chip {
  display: flex;
  align-items: center;
  gap: .55rem;
  max-width: 920px;
  margin: 0 auto .55rem;
  padding: .4rem .65rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: .84rem;
  box-shadow: var(--shadow-sm);
}
.attach-chip img { height: 34px; width: 34px; object-fit: cover; border-radius: 6px; }
.attach-chip button { margin-left: auto; border: none; background: none; cursor: pointer; color: var(--muted); }
.attach-chip button:hover { color: var(--danger); }

/* --- Ricerca nelle conversazioni --- */
.conv-search {
  display: flex; align-items: center; gap: .4rem;
  margin: 0 .7rem .5rem; padding: .3rem .55rem;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--paper, #fff); color: var(--muted);
}
.conv-search input {
  flex: 1; border: none; outline: none; background: transparent;
  font-size: .84rem; color: var(--ink); min-width: 0;
}
.conv-snippet {
  font-size: .72rem; color: var(--muted); margin-top: .15rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.sidebar.collapsed .conv-search { display: none; }

/* --- Pannello di amministrazione --- */
/* Schede del pannello admin: una sola area alla volta invece di un lungo scroll */
.admin-tabs {
  display: flex; flex-wrap: wrap; gap: .3rem;
  border-bottom: 1px solid var(--border);
  margin: .4rem 0 1.4rem; position: sticky; top: 0;
  background: var(--paper); z-index: 5; padding-top: .2rem;
}
.admin-tab {
  display: inline-flex; align-items: center; gap: .4rem;
  border: none; background: none; cursor: pointer;
  font-size: .9rem; color: var(--muted);
  padding: .55rem .9rem; border-bottom: 2px solid transparent;
  margin-bottom: -1px; white-space: nowrap;
}
.admin-tab:hover { color: var(--ink); }
.admin-tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

.admin-section { margin: 1.6rem 0; }
.admin-section:first-of-type { margin-top: .4rem; }
.admin-section h2 {
  display: flex; align-items: center; gap: .5rem;
  font-size: 1.05rem; margin-bottom: .7rem; color: var(--ink);
}
.admin-warning {
  display: flex; align-items: center; gap: .5rem;
  background: #fff7e6; border: 1px solid #f0d9a8; color: #7a5a12;
  border-radius: 8px; padding: .55rem .8rem; margin: .4rem 0; font-size: .87rem;
}
.admin-cards { display: flex; flex-wrap: wrap; gap: .6rem; margin-bottom: .8rem; }
.admin-card {
  background: var(--paper, #fff); border: 1px solid var(--border);
  border-radius: 10px; padding: .55rem .9rem; min-width: 130px;
}
.admin-card .k { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.admin-card .v { font-size: .98rem; font-weight: 600; margin-top: .15rem; }
.admin-inline-form { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: .6rem; }
.admin-inline-form input[type="text"], .admin-inline-form input[type="email"] {
  flex: 1; min-width: 180px; padding: .5rem .7rem;
  border: 1px solid var(--border); border-radius: 8px; font-size: .9rem;
}
.admin-inline-form input[type="file"] { font-size: .85rem; align-self: center; }
.table-scroll { overflow-x: auto; }
.admin-table { width: 100%; border-collapse: collapse; font-size: .87rem; }
.admin-table th, .admin-table td {
  text-align: left; padding: .45rem .6rem; border-bottom: 1px solid var(--border);
}
.admin-table th { font-size: .74rem; text-transform: uppercase; color: var(--muted); letter-spacing: .03em; }
.admin-table tr.inactive td { opacity: .55; }
.admin-table .row-actions { display: flex; gap: .35rem; justify-content: flex-end; }
.btn.small { padding: .28rem .55rem; font-size: .78rem; }
.admin-details { margin-top: .6rem; }
.admin-details summary { cursor: pointer; font-size: .85rem; color: var(--muted); }

/* --- Cursore dello streaming --- */
.stream-cursor {
  display: inline-block;
  width: .55em;
  height: 1.05em;
  margin-left: 2px;
  vertical-align: text-bottom;
  background: var(--primary, #5e9e22);
  border-radius: 2px;
  animation: stream-blink 1s steps(2, start) infinite;
}
@keyframes stream-blink { to { visibility: hidden; } }

/* --- Player audio nei messaggi --- */
.msg .audio-player { display: block; width: min(320px, 100%); margin: .35rem 0 .2rem; }

/* --- Pulsanti di esportazione --- */
.export-bar { margin: .3rem 0 .5rem; }
.export-btn {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--muted);
  border-radius: 7px;
  padding: .22rem .6rem;
  font-size: .74rem;
  font-weight: 500;
  cursor: pointer;
  transition: color .12s, border-color .12s;
}
.export-btn:hover { color: var(--primary-dark); border-color: var(--primary); }

/* ---------- Anteprima app Canvas ---------- */
aside.preview {
  width: 46%;
  min-width: 340px;
  max-width: 78vw;
  border-left: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  position: relative;
}
.preview-resizer {
  position: absolute;
  left: -4px;
  top: 0;
  bottom: 0;
  width: 8px;
  cursor: col-resize;
  z-index: 5;
}
.preview-resizer:hover, body.resizing .preview-resizer { background: var(--primary-tint); }
body.resizing { user-select: none; cursor: col-resize; }
body.resizing .preview-frame { pointer-events: none; }  /* l'iframe non deve catturare il drag */
.preview-header {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .6rem .8rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
.preview-title { font-size: .88rem; font-weight: 600; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.preview-badge {
  font-size: .68rem;
  font-weight: 600;
  padding: .14rem .55rem;
  border-radius: 20px;
  background: #fdf2d2;
  color: #8a6100;
  border: 1px solid #ecd9a0;
  white-space: nowrap;
}
.preview-badge.published { background: var(--primary-tint); color: var(--primary-dark); border-color: #cbe3ae; }
.preview-frame { flex: 1; border: none; width: 100%; background: #fff; }
@media (max-width: 1000px) {
  aside.preview { position: fixed; inset: 56px 0 0 auto; width: min(94vw, 520px); z-index: 30; box-shadow: var(--shadow-lg); }
}

/* ---------- Card di autorizzazione ---------- */
.msg.confirm-card { border-left: 3px solid var(--accent-1); }
.confirm-head { display: flex; align-items: center; gap: .45rem; font-weight: 600; font-size: .9rem; }
.confirm-head code { background: var(--paper); border: 1px solid var(--border); border-radius: 4px; padding: 0 .35rem; font-size: .85em; }
.confirm-summary {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .55rem .75rem;
  margin: .55rem 0;
  font-size: .82rem;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: inherit;
  /* Niente tetto d'altezza: la scheda di una nuova azione va letta per intero
     prima di autorizzarla, e veniva tagliata a metà frase. Il codice, che è la
     parte lunga, sta in un <details> richiuso. */
  overflow-x: auto;
}
.confirm-buttons { display: flex; gap: .6rem; margin-top: .55rem; }
.confirm-done { margin-top: .55rem; font-size: .84rem; color: var(--muted); font-style: italic; }
.check-row { display: flex; align-items: flex-start; gap: .5rem; cursor: pointer; }
.check-row input { width: auto; margin-top: .2rem; }

/* ---------- Pagine elenco (Azioni, Canvas) ---------- */
.actions-page {
  flex: 1;
  overflow-y: auto;
  padding: 1.4rem 1.3rem;
  max-width: 1100px;
  width: 100%;
  margin: 0 auto;
}
.actions-intro { color: var(--muted); font-size: .89rem; margin-bottom: 1.2rem; max-width: 62rem; line-height: 1.55; }
.actions-intro code { background: var(--surface); border: 1px solid var(--border); border-radius: 4px; padding: 0 .3rem; }

/* Permessi delle capacità (cancello) */
.grant-card { border: 1px solid var(--border); border-radius: var(--radius); padding: .8rem 1rem; margin-bottom: .7rem; background: var(--surface); }
.grant-head { display: flex; align-items: baseline; gap: .6rem; flex-wrap: wrap; }
.grant-head strong { font-size: .92rem; }
.grant-desc { color: var(--muted); font-size: .82rem; }
.grant-chips { display: flex; flex-wrap: wrap; gap: .4rem; margin: .55rem 0; }
.grant-chip { display: inline-flex; align-items: center; gap: .35rem; background: var(--primary-tint); color: var(--primary-dark); border-radius: 999px; padding: .12rem .5rem .12rem .6rem; font-size: .8rem; }
.grant-chip button { border: none; background: none; color: inherit; cursor: pointer; font-size: 1rem; line-height: 1; padding: 0; }
.grant-add { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.grant-add select { font-size: .82rem; padding: .25rem .4rem; }
.muted { color: var(--muted); font-size: .84rem; }

/* Registro attività */
.riga-invasiva { background: color-mix(in srgb, var(--accent-1) 9%, transparent); }
.badge.ok { background: color-mix(in srgb, #16a34a 16%, transparent); color: #14803a; }
.badge.ko { background: color-mix(in srgb, var(--danger) 16%, transparent); color: var(--danger); }
.att-args { max-width: 32rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: ui-monospace, monospace; font-size: .78rem; color: var(--muted); }
.att-more { text-align: center; margin: 1rem 0; }
.actions-filters { display: flex; gap: .6rem; margin-bottom: 1.1rem; flex-wrap: wrap; }
.actions-filters input, .actions-filters select {
  padding: .5rem .7rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  font-size: .88rem;
  background: var(--surface);
}
.actions-filters input:focus, .actions-filters select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-tint); }
.actions-filters input { flex: 1; min-width: 200px; }
.actions-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: .85rem; }
.action-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: .95rem 1.05rem;
  cursor: pointer;
  transition: box-shadow .15s, border-color .15s, transform .15s;
}
.action-card:hover { box-shadow: var(--shadow-md); border-color: var(--border-strong); transform: translateY(-1px); }
.action-card-head { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; margin-bottom: .45rem; }
.action-name { font-weight: 600; font-family: "Cascadia Code", Consolas, monospace; font-size: .9rem; }
.badge {
  font-size: .68rem;
  font-weight: 600;
  padding: .13rem .55rem;
  border-radius: 20px;
  border: 1px solid var(--border);
  color: var(--muted);
  background: var(--surface-2);
}
.badge.badge-ok { color: #fff; background: var(--primary); border-color: var(--primary); }
.badge.badge-draft { color: #8a6100; background: #fdf2d2; border-color: #ecd9a0; }
.badge.badge-confirm { color: #7a4a00; background: #fef7e6; border-color: #ecd9a0; }
.action-desc { font-size: .86rem; line-height: 1.45; }
.action-meta { font-size: .75rem; color: var(--muted); margin-top: .45rem; }

/* ---------- Modali ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(28, 33, 30, .45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 1rem;
}
.modal {
  background: var(--surface);
  border-radius: 14px;
  width: 100%;
  max-width: 760px;
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-lg);
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .95rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { font-size: 1rem; font-family: "Cascadia Code", Consolas, monospace; }
.modal-close { border: none; background: none; cursor: pointer; color: var(--muted); padding: .3rem; border-radius: 6px; }
.modal-close:hover { color: var(--ink); background: var(--surface-2); }
.modal-body { padding: 1rem 1.25rem 1.35rem; overflow-y: auto; font-size: .89rem; }
.modal-body h3 { font-size: .9rem; margin: 1.05rem 0 .4rem; }
.modal-body label { display: block; font-size: .83rem; font-weight: 500; margin: .85rem 0 .28rem; color: var(--muted); }
.modal-body label small { font-weight: normal; }
.modal-body input, .modal-body select, .modal-body textarea {
  width: 100%;
  padding: .55rem .7rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  font-size: .89rem;
  font-family: inherit;
}
.modal-body input:focus, .modal-body select:focus, .modal-body textarea:focus {
  outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-tint);
}
.modal-body textarea#c-script, .modal-body textarea#c-params { font-family: "Cascadia Code", Consolas, monospace; font-size: .8rem; }
.modal-body form .btn { margin-top: 1.05rem; }
.detail-table { border-collapse: collapse; width: 100%; margin: .3rem 0; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.detail-table th, .detail-table td { border-bottom: 1px solid var(--border); padding: .38rem .6rem; font-size: .83rem; text-align: left; }
.detail-table th { background: var(--surface-2); }
.detail-table tr:last-child td { border-bottom: none; }
.code-view {
  background: #262b2e;
  color: #e9ede6;
  border-radius: 8px;
  padding: .7rem .9rem;
  overflow-x: auto;
  font-size: .78rem;
  max-height: 320px;
  font-family: "Cascadia Code", Consolas, monospace;
}
.pre-wrap { white-space: pre-wrap; }
.problems-box {
  background: #fdf2d2;
  border: 1px solid #ecd9a0;
  border-radius: var(--radius);
  padding: .6rem .9rem;
  margin: .6rem 0;
  font-size: .84rem;
}
.problems-box ul { margin: .3rem 0 0 1.1rem; }
.detail-buttons { display: flex; gap: .6rem; margin-top: 1.15rem; flex-wrap: wrap; }
.form-note { font-size: .84rem; color: var(--muted); margin-top: .8rem; }

/* ---------- Pagina Workflow ---------- */
.wf-desc { font-size: .85rem; margin: .1rem 0 .5rem; }
.wf-meta { display: flex; flex-direction: column; gap: .22rem; font-size: .78rem; color: var(--muted); margin-bottom: .55rem; }
.wf-meta [data-ic] { vertical-align: -2px; margin-right: .25rem; }
.wf-steps {
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: .74rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .5rem .6rem;
  white-space: pre-wrap;
  margin-bottom: .65rem;
  max-height: 7.5rem;
  overflow-y: auto;
}
.wf-buttons { display: flex; gap: .45rem; flex-wrap: wrap; }
.wf-empty { color: var(--muted); font-size: .9rem; }
.wf-bar {
  background: var(--primary-tint);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: .55rem .8rem;
  font-size: .85rem;
  margin-bottom: .9rem;
}
.wf-bar.error { background: #fdf0ef; border-color: var(--danger); color: var(--danger); }
.wf-run { border: 1px solid var(--border); border-radius: 8px; margin-bottom: .6rem; }
.wf-run summary { cursor: pointer; padding: .5rem .7rem; font-size: .84rem; }
.wf-run pre {
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: .76rem;
  white-space: pre-wrap;
  padding: .6rem .7rem;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
  max-height: 20rem;
  overflow-y: auto;
}
.run-ok { color: var(--primary-dark); }
.run-err { color: var(--danger); }

/* ---------- Pannello admin: opportunità e profilo aziendale ---------- */
.opp-card {
  border: 1px solid var(--border);
  border-left: 3px solid var(--primary);
  border-radius: 9px;
  padding: .8rem .95rem;
  margin-bottom: .7rem;
  background: var(--surface);
}
.opp-head {
  display: flex;
  align-items: center;
  gap: .5rem;
  flex-wrap: wrap;
  margin-bottom: .45rem;
}
.opp-saving {
  margin-left: auto;
  font-size: .8rem;
  font-weight: 600;
  color: var(--primary-dark);
  white-space: nowrap;
}
.opp-card p { margin: .25rem 0; font-size: .86rem; }
.opp-evidence { color: var(--muted); font-size: .8rem; }
.opp-card .row-actions { margin-top: .6rem; display: flex; gap: .4rem; flex-wrap: wrap; }
.admin-check { display: inline-flex; align-items: center; gap: .35rem; font-size: .84rem; color: var(--muted); }
.dept-input { width: 8.5rem; font-size: .82rem; padding: .25rem .4rem; }
.admin-table select { font-size: .82rem; padding: .25rem .3rem; }

/* Crediti */
.admin-sub { font-size: .95rem; margin: 1.4rem 0 .4rem; color: var(--ink); }
.muted-small { font-size: .8rem; color: var(--muted); }
#credits-models td:last-child { max-width: 32rem; }

/* Ritocchi mobile in coda: queste regole devono venire DOPO le definizioni base
   di .brand/.model-select, altrimenti a parità di specificità perdono. */
@media (max-width: 760px) {
  /* Il logo del cliente non deve essere tagliato a metà: su schermo stretto si
     rimpicciolisce il logo e si accorcia il selettore del modello */
  .model-select { max-width: 6.5rem; min-width: 0; }
  .brand { flex: 0 1 auto; }
  .brand img { height: 15px; max-width: 45vw; object-fit: contain; }
}
