/* ---------- Chat ---------- */

/* ---------- S128 — Chat shell (Claude-style) ----------
   Full-screen edge-to-edge. No chat-local sidebar; history lives in the
   global drawer. Header carries the model pill (smart router identity) on
   the left and action icons on the right. Hairline hair separator on
   hover, nothing else. */

.chat-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  /* Edge-to-edge horizontally over main-content side padding. Vertical
     padding removed from main-content S255, no neg margin needed top/bottom. */
  margin: 0 -40px;
  background: var(--bg-primary);
  position: relative;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  /* Left pad leaves room for the fixed drawer-toggle (top:18px, left:18px)
     so the hamburger never overlaps the model pill. */
  padding: 8px 16px 8px 62px;
  flex-shrink: 0;
  position: relative;
  min-height: 48px;
}

.chat-system-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 6px 16px;
  background: rgba(201, 168, 76, 0.08);
  border-bottom: 1px solid rgba(201, 168, 76, 0.15);
  font-size: 0.76rem;
  color: var(--text-secondary);
  flex-shrink: 0;
  flex-wrap: wrap;
  text-align: center;
}
.chat-system-banner.hidden { display: none; }
/* S584 — la variante di allarme: stesso posto e stessa forma della striscia
   che invita, tinta diversa perché dice un'altra cosa. Quella è un invito,
   questa è «una cosa che credevi accesa è spenta». */
.chat-system-banner--allarme {
  background: var(--red-dim, rgba(200, 80, 80, 0.10));
  border-bottom-color: rgba(200, 80, 80, 0.25);
}
.chat-system-banner-link {
  color: var(--gold);
  text-decoration: underline;
  text-decoration-style: dotted;
  white-space: nowrap;
}

.chat-header::after {
  content: '';
  position: absolute;
  left: 20px;
  right: 20px;
  bottom: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--border) 15%, var(--border) 85%, transparent);
  pointer-events: none;
}

/* Desktop: when drawer is visible, no need to reserve hamburger space. */
@media (min-width: 769px) {
  body:not(.drawer-collapsed) .chat-header {
    padding-left: 20px;
  }
}

.chat-header-model {
  position: relative;
  min-width: 0;
  flex: 1;
}

.chat-header-model-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px 8px 12px;
  border-radius: 999px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-primary);
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
  max-width: 100%;
  min-width: 0;
}

.chat-header-model-btn:hover {
  background: rgba(201, 168, 76, 0.05);
  border-color: var(--border-subtle);
}

.chat-header-model-btn.open {
  background: rgba(201, 168, 76, 0.08);
  border-color: var(--gold-dim);
}

.chat-header-model-icon {
  font-size: 0.95rem;
  color: var(--gold);
  line-height: 1;
  filter: drop-shadow(0 0 6px rgba(201, 168, 76, 0.4));
  flex-shrink: 0;
}

.chat-header-model-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: var(--font-numbers, var(--font-body));
  font-size: 0.88rem;
  font-weight: 500;
  letter-spacing: 0.01em;
  color: var(--text-primary);
}

.chat-header-model-chevron {
  width: 14px;
  height: 14px;
  color: var(--text-muted);
  transition: transform 0.15s ease;
  flex-shrink: 0;
}

.chat-header-model-btn.open .chat-header-model-chevron {
  transform: rotate(180deg);
  color: var(--gold);
}

.chat-header-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.chat-header-title { display: none; }

.chat-header-icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all var(--transition);
}

.chat-header-icon-btn:hover {
  border-color: var(--gold-dim);
  color: var(--gold);
  background: rgba(201, 168, 76, 0.05);
}

.chat-header-icon-btn svg {
  width: 16px;
  height: 16px;
}

/* ---------- S129 — Page shell (Claude-style for non-chat views) ----------
   Unified shell for Portfolio / Predictions (and, later, Settings / Admin /
   News / Jungla). Edge-to-edge like chat but scroll-native instead of
   full-height flex. Header mirrors the chat-header geometry so the drawer
   hamburger never overlaps the title — same 62px left-pad on mobile /
   collapsed desktop, 20px when the drawer is docked. Thin gradient
   separator at the bottom matches chat. */

.page-shell {
  /* Edge-to-edge horizontally over main-content side padding.
     Vertical padding lives in .page-shell-header (sticky) and .page-shell-body
     so sticky top:0 anchors at viewport top without margin-collapsing artifacts. */
  margin: 0 -40px;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg-primary);
  display: flex;
  flex-direction: column;
}

.page-shell-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 20px 10px 62px;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 12;
  background: var(--bg-primary);
  min-height: 48px;
}

.page-shell-header::after {
  content: '';
  position: absolute;
  left: 20px;
  right: 20px;
  bottom: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--border) 15%, var(--border) 85%, transparent);
  pointer-events: none;
}

@media (min-width: 769px) {
  body:not(.drawer-collapsed) .page-shell-header {
    padding-left: 20px;
  }
}

.page-shell-title {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 500;
  letter-spacing: 0.01em;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.page-shell-title .page-shell-back {
  color: var(--text-muted);
  text-decoration: none;
  font-size: 1.1rem;
  line-height: 1;
  padding: 4px 6px;
  border-radius: 6px;
  transition: all var(--transition);
}

.page-shell-title .page-shell-back:hover {
  color: var(--gold);
  background: rgba(201, 168, 76, 0.06);
}

.page-shell-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.page-shell-body {
  padding: 24px 40px 40px;
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

/* Empty-state polish (S130): when the body has only an empty state
   (optionally wrapped in a single card), give it viewport breathing room
   so it reads as "intentional void", not "tiny forgotten box at top". */
.page-shell-body > .state-empty:only-child {
  flex: 1;
  min-height: calc(100vh - 200px);
  justify-content: center;
}

.page-shell-body > .card:only-child {
  min-height: 240px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

@media (max-width: 768px) {
  .page-shell {
    margin: 0 -16px;
    min-height: 100dvh;
    overflow-x: clip;
  }

  .page-shell-header {
    padding: 10px 14px 10px 60px;
    gap: 8px;
  }

  .page-shell-title {
    font-size: 1rem;
    max-width: calc(100vw - 140px);
  }

  .page-shell-body {
    padding: 18px 16px 28px;
  }

  .page-shell-body > .state-empty:only-child {
    min-height: calc(100dvh - 180px);
  }

  .page-shell-body > .card:only-child {
    min-height: 200px;
  }

  .page-shell-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
    flex-shrink: 1;
    gap: 6px;
  }

  .page-shell-actions .btn-sm {
    padding: 6px 10px;
    font-size: 0.72rem;
  }
}

/* Model dropdown popover (drops DOWN from the header pill) */
.chat-model-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  min-width: 260px;
  max-width: 340px;
  background:
    linear-gradient(180deg, rgba(201, 168, 76, 0.025) 0%, transparent 30%),
    var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.55), 0 2px 8px rgba(201, 168, 76, 0.05);
  padding: 6px;
  z-index: 60;
  animation: dropdown-down 0.15s ease;
  max-height: 60vh;
  overflow-y: auto;
}

@keyframes dropdown-down {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0;
  scroll-behavior: smooth;
}

.chat-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  text-align: center;
  padding: 40px 24px;
  animation: chat-welcome-in 0.6s var(--transition-slow);
}

@keyframes chat-welcome-in {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

/* S603 — il link alla guida sotto il benvenuto: staccato dalla riga di
   testo, come nel riquadro dei portafogli. Chi apre Pizia la prima volta
   ha davanti un campo vuoto e nessun appiglio. */
.chat-welcome .state-welcome-guide { margin-top: 18px; }

.chat-welcome-icon {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--gold-glow);
  border: 1px solid rgba(201, 168, 76, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
}

.chat-welcome-icon svg {
  width: 28px;
  height: 28px;
  color: var(--gold);
}

.chat-welcome-title {
  font-family: var(--font-titles);
  font-size: 1.8rem;
  font-weight: 500;
  color: var(--gold);
  letter-spacing: 0.06em;
  margin-bottom: 8px;
}

.chat-welcome-subtitle {
  font-size: 0.9rem;
  color: var(--text-secondary);
  max-width: 320px;
}

/* Messages */

.chat-msg {
  display: flex;
  gap: 12px;
  padding: 6px 24px;
  animation: msg-in 0.25s ease;
}

@keyframes msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.chat-msg--user {
  justify-content: flex-end;
}

.chat-msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-family: var(--font-numbers);
  font-weight: 600;
  margin-top: 2px;
}

.chat-msg--assistant .chat-msg-avatar {
  background: var(--gold-glow);
  border: 1px solid rgba(201, 168, 76, 0.2);
  color: var(--gold);
}

.chat-msg--user .chat-msg-avatar {
  display: none;
}

.chat-msg-body {
  max-width: 720px;
  min-width: 0;
}

.chat-msg--user .chat-msg-body {
  max-width: 600px;
}

.chat-msg-content {
  line-height: 1.6;
  font-size: 0.9rem;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.chat-msg--user .chat-msg-content {
  background: rgba(201, 168, 76, 0.08);
  border: 1px solid rgba(201, 168, 76, 0.15);
  border-radius: var(--radius) var(--radius) var(--radius-sm) var(--radius);
  padding: 10px 16px;
  color: var(--text-primary);
  white-space: pre-wrap;
}

.chat-msg--assistant .chat-msg-content {
  color: var(--text-primary);
}

/* Markdown inside assistant messages */

.chat-msg--assistant .chat-msg-content p {
  margin: 0 0 10px 0;
}

.chat-msg--assistant .chat-msg-content p:last-child {
  margin-bottom: 0;
}

.chat-msg--assistant .chat-msg-content strong {
  color: var(--text-primary);
  font-weight: 600;
}

.chat-msg--assistant .chat-msg-content em {
  font-style: italic;
  color: var(--text-secondary);
}

.chat-msg--assistant .chat-msg-content a {
  color: var(--gold);
  text-decoration: underline;
  text-decoration-color: rgba(201, 168, 76, 0.3);
  text-underline-offset: 2px;
}

.chat-msg--assistant .chat-msg-content a:hover {
  text-decoration-color: var(--gold);
}

.chat-msg--assistant .chat-msg-content ul,
.chat-msg--assistant .chat-msg-content ol {
  margin: 8px 0;
  padding-left: 20px;
}

.chat-msg--assistant .chat-msg-content ul {
  list-style: disc;
}

.chat-msg--assistant .chat-msg-content ol {
  list-style: decimal;
}

.chat-msg--assistant .chat-msg-content li {
  margin: 4px 0;
}

.chat-msg--assistant .chat-msg-content h1,
.chat-msg--assistant .chat-msg-content h2,
.chat-msg--assistant .chat-msg-content h3 {
  margin: 16px 0 8px 0;
  font-family: var(--font-titles);
}

.chat-msg--assistant .chat-msg-content h1 { font-size: 1.3rem; }
.chat-msg--assistant .chat-msg-content h2 { font-size: 1.15rem; }
.chat-msg--assistant .chat-msg-content h3 { font-size: 1rem; color: var(--text-secondary); }

.chat-msg--assistant .chat-msg-content hr {
  border: none;
  height: 1px;
  background: var(--border);
  margin: 16px 0;
}

/* Inline code */
.chat-msg--assistant .chat-msg-content code {
  font-family: var(--font-numbers);
  font-size: 0.82em;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 1px 5px;
  color: var(--gold);
}

/* Code blocks (artifact) */
.chat-artifact {
  position: relative;
  margin: 12px 0;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  overflow: hidden;
  background: #0c0c14;
}

.chat-artifact-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: rgba(255, 255, 255, 0.03);
  border-bottom: 1px solid var(--border);
}

.chat-artifact-lang {
  font-family: var(--font-numbers);
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.chat-artifact-copy {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-family: var(--font-numbers);
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  transition: all var(--transition);
  cursor: pointer;
}

.chat-artifact-copy:hover {
  color: var(--gold);
  background: var(--gold-glow);
}

.chat-artifact-copy.copied {
  color: var(--green);
}

.chat-artifact-copy svg {
  width: 13px;
  height: 13px;
}

.chat-artifact pre {
  margin: 0;
  padding: 14px 16px;
  overflow-x: auto;
  overflow-y: auto;
  max-height: 400px;
  font-family: var(--font-numbers);
  font-size: 0.82rem;
  line-height: 1.55;
  color: var(--text-primary);
}

.chat-artifact pre code {
  background: none;
  border: none;
  padding: 0;
  color: inherit;
  font-size: inherit;
}

/* Syntax coloring — minimal, covering common LLM output languages */
.chat-artifact .tok-kw { color: #c792ea; }
.chat-artifact .tok-str { color: #c3e88d; }
.chat-artifact .tok-num { color: #f78c6c; }
.chat-artifact .tok-cm { color: #546e7a; font-style: italic; }
.chat-artifact .tok-fn { color: #82aaff; }
.chat-artifact .tok-op { color: #89ddff; }
.chat-artifact .tok-var { color: #f07178; }
.chat-artifact .tok-type { color: #ffcb6b; }
.chat-artifact .tok-prop { color: #80cbc4; }
.chat-artifact .tok-bool { color: #ff5370; }

/* Tables inside chat */
.chat-msg--assistant .chat-msg-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 0.85rem;
  display: block;
  overflow-x: auto;
}

/* S504 — le celle NON spezzano più le parole.
   Segnalato dal Sacerdote: «le tabelle nella chat sono brutte». Sul
   telefono una tabella a sette colonne rendeva "MOD ELL O", "BA GA GL
   IA IO", "15. 800 – 19. 000 €": illeggibile. La colpa era di
   `word-break: break-word` sposato a `width: 100%`, che obbligava sette
   colonne dentro 360 pixel — cinquanta pixel a colonna, e a quel punto
   l'unico modo di starci è tagliare le parole a metà.
   Lo scroll orizzontale c'era già (`display:block; overflow-x:auto`) e
   non veniva mai usato, perché niente superava mai la larghezza. Ora
   ogni colonna ha un minimo, le parole restano intere, e quando non ci
   stanno la tabella scorre — che è il gesto che un telefono conosce. */
.chat-msg--assistant .chat-msg-content th,
.chat-msg--assistant .chat-msg-content td {
  padding: 8px 12px;
  border: 1px solid var(--border);
  text-align: left;
  word-break: normal;
  overflow-wrap: normal;
  min-width: 6.5rem;
}

/* L'intestazione non va a capo mai: è l'etichetta della colonna, e
   "BAGAGLIAIO" spezzato in cinque righe non è più un'etichetta. */
.chat-msg--assistant .chat-msg-content th {
  white-space: nowrap;
}

.chat-msg--assistant .chat-msg-content th {
  background: rgba(255, 255, 255, 0.03);
  font-family: var(--font-numbers);
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 500;
}

/* Message timestamp */
.chat-msg-time {
  font-size: 0.68rem;
  color: var(--text-muted);
  margin-top: 4px;
  font-family: var(--font-numbers);
  opacity: 0.6;
}

.chat-msg--user .chat-msg-time {
  text-align: right;
}

/* S548 (F157) — i gesti sul messaggio: copia, copia con la domanda, rifai.
   Su PUNTATORE restano in ombra e si accendono avvicinandosi: la chat si
   legge, e tre icone accese sotto ogni bolla sono rumore. Su TOCCO l'ombra
   non esiste — non c'e' hover su un telefono, e un gesto che si rivela solo
   passandoci sopra li' non si rivela mai. */
.chat-gesti {
  display: flex;
  gap: 2px;
  margin-top: 2px;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.chat-msg--user .chat-gesti {
  justify-content: flex-end;
}

.chat-msg:hover .chat-gesti,
.chat-gesti:focus-within {
  opacity: 1;
}

@media (hover: none) {
  .chat-gesti { opacity: 0.55; }
}

.chat-gesto {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: color 0.15s ease, background 0.15s ease;
}

.chat-gesto svg { width: 15px; height: 15px; }

.chat-gesto:hover {
  color: var(--gold);
  background: rgba(201, 168, 76, 0.09);
}

/* L'area che il dito colpisce e' piu' larga di quella che l'occhio vede:
   30px disegnati, 44px toccabili, senza allargare la riga. */
@media (hover: none) {
  .chat-gesto::after {
    content: '';
    position: absolute;
    width: 44px;
    height: 44px;
  }
  .chat-gesto { position: relative; }
}

.chat-gesto--fatto,
.chat-gesto--fatto:hover {
  color: var(--green);
  background: rgba(45, 212, 168, 0.12);
}

/* Token usage badge (BYOK only) */
.chat-msg-usage {
  font-size: 0.78rem;
  color: var(--text-primary);
  margin-top: 6px;
  font-family: var(--font-numbers);
  letter-spacing: 0.02em;
}

/* Interrupted response */
.chat-interrupted {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-style: italic;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

/* S533 — il turno che si è interrotto prima della risposta, e il tasto che
   lo fa ripartire. Non è una bolla di Pizia: Pizia non ha detto niente, e
   fingere una sua riga sarebbe metterle in bocca un'assenza. È un avviso
   del tempio, in mezzo alla conversazione, con il gesto già sotto le dita. */
.chat-interrotto {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 14px;
  margin: 14px auto 6px;
  padding: 14px 18px;
  max-width: 640px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--gold);
  border-radius: var(--radius-sm, 6px);
  background: rgba(212, 175, 55, 0.04);
}

.chat-interrotto-testo {
  font-size: 0.82rem;
  color: var(--text-secondary);
}

.chat-interrotto-btn { flex-shrink: 0; }

@media (max-width: 560px) {
  .chat-interrotto { flex-direction: column; gap: 10px; text-align: center; }
}

/* Thinking / typing indicator */
.chat-thinking {
  /* S544 — le fasi si impilano invece di sovrascriversi: sopra quello che ha
     già fatto, sotto quello che sta facendo adesso. */
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 8px 0;
  font-size: 0.8rem;
  color: var(--text-muted);
  font-style: italic;
}

/* Quello che ha già fatto: resta leggibile, ma non ruba l'occhio a quello
   che sta succedendo ora. */
.chat-fase-fatta {
  font-size: 0.74rem;
  line-height: 1.5;
  opacity: 0.55;
}

.chat-fase-fatta::before {
  content: '· ';
}

/* La fase viva: i puntini che si muovono e il tempo che scorre. */
.chat-fase-viva {
  display: flex;
  align-items: center;
  gap: 6px;
}

.chat-fase-hint {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.chat-fase-hint a {
  color: var(--gold);
}

.chat-thinking-dots {
  display: flex;
  gap: 3px;
}

.chat-thinking-dots span {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--gold-dim);
  animation: dot-pulse 1.4s ease-in-out infinite;
}

.chat-thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.chat-thinking-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes dot-pulse {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1); }
}

/* ---------- S128 — Composer (pill) ----------
   Single rounded pill containing attach + textarea + send. No surrounding
   chrome; sits on the same background as the messages surface. */

.chat-composer {
  padding: 10px 20px 16px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.chat-composer-pill {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  max-width: 780px;
  width: 100%;
  margin: 0 auto;
  padding: 6px 6px 6px 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 24px;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.chat-composer-pill:focus-within {
  border-color: var(--gold-dim);
  box-shadow: 0 0 0 2px var(--gold-glow), 0 0 16px rgba(201, 168, 76, 0.06);
}

.chat-composer-textarea {
  flex: 1;
  min-height: 40px;
  max-height: 200px;
  padding: 10px 8px;
  background: transparent;
  border: none;
  color: var(--text-primary);
  font-family: var(--font-body);
  font-size: 0.95rem;
  line-height: 1.45;
  resize: none;
  outline: none;
  overflow-y: auto;
}

.chat-composer-textarea::placeholder {
  color: var(--text-muted);
  font-style: italic;
}

.chat-composer-attach,
.chat-composer-send {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: all var(--transition);
}

.chat-composer-attach {
  background: transparent;
  border: none;
  color: var(--text-muted);
}
.chat-composer-attach:hover {
  background: rgba(255, 255, 255, 0.04);
  color: var(--text-primary);
}
.chat-composer-attach svg {
  width: 18px;
  height: 18px;
}

/* S128 polish — the send button shares the lightning's visual weight.
   Full gold glow so it reads as the primary action across the shell. */
.chat-composer-send {
  background: linear-gradient(135deg, var(--gold-bright) 0%, var(--gold) 60%, var(--gold-dim) 100%);
  border: 1px solid var(--gold-bright);
  color: var(--bg-primary);
  box-shadow:
    0 0 0 3px rgba(201, 168, 76, 0.12),
    0 0 18px rgba(201, 168, 76, 0.45),
    0 2px 10px rgba(201, 168, 76, 0.3);
  animation: send-pulse 2.8s ease-in-out infinite;
}
.chat-composer-send:hover:not(:disabled) {
  box-shadow:
    0 0 0 4px rgba(201, 168, 76, 0.18),
    0 0 28px rgba(201, 168, 76, 0.6),
    0 4px 14px rgba(201, 168, 76, 0.4);
  transform: translateY(-1px);
}
.chat-composer-send:active:not(:disabled) { transform: translateY(0); }
.chat-composer-send:disabled {
  opacity: 0.32;
  cursor: default;
  box-shadow: none;
  animation: none;
}
/* S436 — durante lo streaming il tasto è STOP: stesso oro, pulse fermo,
   quadrato dentro. Deve leggersi come "posso fermarlo", non come inattivo. */
.chat-composer-send.is-stop {
  animation: none;
  box-shadow:
    0 0 0 3px rgba(201, 168, 76, 0.18),
    0 0 14px rgba(201, 168, 76, 0.5);
}
.chat-composer-send svg {
  width: 16px;
  height: 16px;
  filter: drop-shadow(0 0 2px rgba(0, 0, 0, 0.25));
}

@keyframes send-pulse {
  0%, 100% {
    box-shadow:
      0 0 0 3px rgba(201, 168, 76, 0.12),
      0 0 18px rgba(201, 168, 76, 0.45),
      0 2px 10px rgba(201, 168, 76, 0.3);
  }
  50% {
    box-shadow:
      0 0 0 3px rgba(201, 168, 76, 0.18),
      0 0 26px rgba(201, 168, 76, 0.62),
      0 2px 12px rgba(201, 168, 76, 0.4);
  }
}

.chat-input-wrapper--drag {
  outline: 2px dashed var(--gold-dim);
  outline-offset: 3px;
  border-radius: 28px;
}

.chat-attachments-preview {
  max-width: 780px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 6px;
}
.chat-attach-blind-banner {
  flex-basis: 100%;
  padding: 8px 12px;
  background: rgba(201, 168, 76, 0.08);
  border: 1px solid var(--gold-dim);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.82rem;
  line-height: 1.35;
}
.chat-attach-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 30px 6px 6px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-width: 240px;
}
.chat-attach-thumb {
  width: 40px;
  height: 40px;
  border-radius: 4px;
  object-fit: cover;
  flex-shrink: 0;
}
.chat-attach-icon {
  width: 40px;
  height: 40px;
  border-radius: 4px;
  background: var(--bg-secondary);
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 600;
  flex-shrink: 0;
}
.chat-attach-meta {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.chat-attach-name {
  font-size: 0.82rem;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 140px;
}
.chat-attach-size {
  font-size: 0.72rem;
  color: var(--text-muted);
}
.chat-attach-badge {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 2px;
}
.chat-attach-badge--ok { color: var(--gold); }
.chat-attach-badge--err { color: var(--amber); }
.chat-attach-remove {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.chat-attach-remove:hover {
  background: var(--bg-secondary);
  color: var(--amber);
}

/* Attachments rendered inline inside a chat message */
.chat-msg-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}
.chat-msg-attachment {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  color: inherit;
  border-radius: 6px;
  overflow: hidden;
}
.chat-msg-attachment--img {
  position: relative;
  min-width: 80px;
  min-height: 80px;
}
.chat-msg-attachment--img img {
  max-width: 220px;
  max-height: 180px;
  display: block;
  border-radius: 6px;
  border: 1px solid var(--border);
  position: relative;
  z-index: 1;
  background: var(--bg-input);
}

/* S116 — placeholder "loading" pseudo-element. Mostra icona immagine
   grigia dietro l'<img> mentre hydrate (Bearer fetch) è in corso.
   Quando img.src arriva, l'img si piazza sopra (z-index: 1) e copre. */
.chat-msg-attachment--img-loading {
  width: 80px;
  height: 80px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.chat-msg-attachment--img-loading::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 0;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 34px 34px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='%23808080' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='3' y='3' width='18' height='18' rx='2'/%3E%3Ccircle cx='8.5' cy='8.5' r='1.5'/%3E%3Cpath d='M21 15l-5-5L5 21'/%3E%3C/svg%3E");
  opacity: 0.7;
}
.chat-msg-attachment--img-loading img {
  width: 80px;
  height: 80px;
  opacity: 0;
}
.chat-msg-attachment--file {
  padding: 6px 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  font-size: 0.82rem;
}
.chat-msg-attachment-icon {
  font-weight: 600;
  color: var(--text-muted);
}

/* S128 — Dropdown item styles (header popover shares these). */
.chat-model-dropdown-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition);
  font-size: 0.85rem;
  color: var(--text-primary);
}

.chat-model-dropdown-item:hover { background: var(--bg-hover); }
.chat-model-dropdown-item.active { color: var(--gold); background: var(--gold-glow); }
.chat-model-dropdown-item--auto { background: rgba(201, 168, 76, 0.03); }

.chat-model-dropdown-item .provider-name { font-weight: 500; }
.chat-model-dropdown-item .model-name {
  font-size: 0.74rem;
  color: var(--text-muted);
  font-family: var(--font-numbers, var(--font-body));
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 150px;
}
.chat-model-dropdown-item .check-icon {
  width: 14px;
  height: 14px;
  color: var(--gold);
  flex-shrink: 0;
}

.chat-model-dropdown-empty {
  padding: 14px;
  font-size: 0.78rem;
  color: var(--text-muted);
  text-align: center;
}

.chat-disclaimer {
  max-width: 780px;
  width: 100%;
  margin: 0 auto;
  padding: 0 12px;
  text-align: center;
  font-size: 0.68rem;
  font-style: italic;
  color: var(--text-muted);
  opacity: 0.55;
}

/* S128 — Chat mobile */

@media (max-width: 768px) {
  .chat-shell {
    height: 100dvh;
    max-height: -webkit-fill-available;
    /* main-content mobile padding is `0 16px` (vert zeroed S256 125f22f).
       Cancel only the side padding for edge-to-edge horizontal; vert margin
       must stay 0 or the chat-header (model selector / "router" Pizia) gets
       pushed above viewport top. */
    margin: 0 -16px;
  }

  .chat-header {
    padding: 10px 10px 10px 60px;  /* leave room for fixed drawer-toggle */
    gap: 6px;
  }

  .chat-header-model-label {
    font-size: 0.85rem;
    max-width: calc(100vw - 190px);
  }

  /* Bigger tap target for the "+" icon in chat header. */
  .chat-header-icon-btn { width: 44px; height: 44px; }
  .chat-header-icon-btn svg { width: 20px; height: 20px; stroke-width: 2; }

  .chat-msg {
    padding: 6px 16px;
  }

  .chat-msg-body {
    max-width: calc(100vw - 90px);
  }

  .chat-msg--user .chat-msg-body {
    max-width: calc(100vw - 90px);
  }

  .chat-composer {
    padding: 8px 12px 14px;
  }

  .chat-composer-textarea {
    font-size: 16px; /* prevent iOS zoom */
  }

  .chat-composer-textarea::placeholder {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .chat-artifact pre {
    font-size: 0.78rem;
    padding: 10px 12px;
  }

  .chat-welcome-title {
    font-size: 1.5rem;
  }

  .chat-disclaimer {
    font-size: 0.64rem;
    padding: 0 6px;
  }

  /* S128 — model dropdown opened from header goes almost full-width on
     mobile so long model names stay readable. */
  .chat-model-dropdown {
    min-width: calc(100vw - 32px);
    max-width: calc(100vw - 32px);
    left: 50%;
    transform: translateX(-50%);
  }
  @keyframes dropdown-down {
    from { opacity: 0; transform: translate(-50%, -4px); }
    to { opacity: 1; transform: translate(-50%, 0); }
  }
}

/* Settings mobile */

@media (max-width: 540px) {
  .settings-grid {
    gap: 10px;
  }

  .settings-card {
    padding: 14px 16px;
  }

  .settings-card--danger-zone {
    padding: 14px 16px;
  }

  .api-provider-row {
    flex-wrap: wrap;
    gap: 6px;
    padding: 10px 0;
  }

  .api-provider-info {
    flex-basis: 100%;
  }

  .api-provider-right {
    width: 100%;
    justify-content: space-between;
  }

  .api-provider-value {
    display: none;
  }

  .api-edit-form .form-input {
    font-size: 16px !important; /* prevent iOS zoom on focus */
  }

  .danger-zone-row {
    flex-wrap: wrap;
    gap: 10px;
  }

  .danger-zone-row .btn {
    width: 100%;
  }
}

.chat-providers-exhausted {
  background: rgba(224, 172, 58, 0.08);
  border: 1px solid var(--gold-dim);
  border-radius: 8px;
  padding: 16px;
  margin-top: 8px;
}
.chat-providers-exhausted p { margin: 0 0 8px; }
.chat-providers-exhausted p:last-of-type { margin-bottom: 12px; }
.chat-exhausted-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.chat-retry-btn:disabled {
  opacity: 0.5;
  cursor: wait;
}

.chat-delivery-card {
  background: var(--gold-glow);
  border-left: 3px solid var(--gold);
  border-radius: 6px;
  padding: 10px 14px;
  margin-bottom: 8px;
  font-size: 0.85rem;
  color: var(--text-primary);
}

/* S626 — le sue parole sotto il fatto, non dentro. Più spente: servono a
   riconoscere QUALE segnalazione, non a essere rilette. */
.chat-delivery-parole {
  margin-top: 4px;
  font-size: 0.8rem;
  font-style: italic;
  color: var(--text-secondary);
}

/* S463 — invito alle notifiche, mostrato una volta sola dopo che una
   risposta è stata recuperata al ritorno sull'app. Volutamente più spento
   della delivery card: è un'offerta, non una notizia. */
.chat-push-invite {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  justify-content: space-between;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 0.78rem;
  color: var(--text-muted);
}

.chat-push-invite-actions {
  display: flex;
  gap: 8px;
}

