/* Pagina agente: conversazione a tutta altezza con cronologia laterale. */

.content--fill { display: flex; min-height: 0; }

.chat {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  flex: 1;
  height: calc(100dvh - var(--topbar-height));
  min-height: 0;
  overflow: hidden;
  /* la colonna della cronologia si stringe mentre il pannello scorre via */
  transition: grid-template-columns var(--duration-slow) var(--ease-out);
}
.chat[data-history="closed"] { grid-template-columns: minmax(0, 1fr) 0px; }

.chat__main {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.chat__header {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-6) var(--space-3);
}
.chat__avatar {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  flex: none;
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--agent-color) 14%, transparent);
  color: var(--agent-color);
}
.chat__avatar svg { width: 22px; height: 22px; }
.chat__identity { flex: 1; min-width: 0; }
.chat__name { font-family: var(--font-sans); font-size: var(--text-lg); }
.chat__sub { overflow: hidden; color: var(--text-muted); font-size: var(--text-sm); text-overflow: ellipsis; white-space: nowrap; }
.chat__actions { display: flex; align-items: center; gap: var(--space-1); }

.mode-bar { padding: 0 var(--space-6) var(--space-3); border-bottom: 1px solid var(--border); }

.chat__scroll { flex: 1; min-height: 0; overflow-y: auto; scroll-behavior: auto; }
.chat__thread {
  display: grid;
  gap: var(--space-6);
  max-width: 860px;
  margin: 0 auto;
  padding: var(--space-6) var(--space-6) var(--space-8);
}

/* ── Benvenuto (conversazione vuota) ──────────────── */
.welcome {
  display: grid;
  gap: var(--space-4);
  padding: var(--space-6);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
}
.welcome__mode { font-size: var(--text-lg); font-weight: 600; }
.welcome__desc { color: var(--text-muted); }
.welcome__about { padding-top: var(--space-4); border-top: 1px solid var(--border); color: var(--text-muted); font-size: var(--text-sm); }
.welcome__actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); }
.welcome__actions p { color: var(--text-subtle); font-size: var(--text-sm); }

/* ── Messaggi ──────────────────────────────────────── */
.msg { display: grid; gap: var(--space-2); }
.msg--user { justify-items: end; }
.msg__bubble {
  max-width: min(640px, 92%);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-lg) var(--radius-lg) 4px var(--radius-lg);
  background: var(--accent-soft);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg__attachments { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: var(--space-1); }
.msg__edit { display: grid; gap: var(--space-2); width: min(640px, 100%); }
.msg__edit-actions { display: flex; justify-content: flex-end; gap: var(--space-2); }

.msg--assistant { grid-template-columns: 32px minmax(0, 1fr); column-gap: var(--space-3); }
.msg__avatar {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--agent-color) 14%, transparent);
  color: var(--agent-color);
}
.msg__avatar svg { width: 18px; height: 18px; }
.msg__content { min-width: 0; display: grid; gap: var(--space-3); }
.msg__status {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--text-muted);
  font-size: var(--text-sm);
}
.msg__status[data-tone="warning"] { color: var(--warning); }
.msg__status[data-tone="danger"] { color: var(--danger); }
.msg__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1);
  opacity: 0.85;
}
.msg__actions:hover, .msg__actions:focus-within { opacity: 1; }
.msg--user .msg__actions { justify-content: flex-end; }

.typing { display: inline-flex; gap: 4px; padding: var(--space-2) 0; }
.typing span { width: 6px; height: 6px; border-radius: 50%; background: var(--text-subtle); animation: pulse 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }

/* ── Area di scrittura ─────────────────────────────── */
.chat__bottom {
  position: relative;
  display: grid;
  gap: var(--space-2);
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
  padding: 0 var(--space-6) var(--space-4);
}
/* centrato con margin auto e non con transform: lo :active dei chip usa transform
   (scale) e lo sostituiva, il pulsante scattava a destra e il clic andava perso */
.chat__jump { position: absolute; top: calc(-1 * var(--space-10)); left: 0; right: 0; width: fit-content; margin: 0 auto; box-shadow: var(--shadow); z-index: 2; }
.chat__jump:not([hidden]) { animation: pop-in var(--duration) var(--ease-out); }

.banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2) var(--space-3);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius);
  background: var(--info-soft);
  color: var(--info);
  font-size: var(--text-sm);
}
.banner .link-btn { margin-left: auto; }

.composer {
  display: grid;
  gap: var(--space-2);
  padding: var(--space-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow);
  transition: border-color var(--duration-fast), box-shadow var(--duration-fast);
}
.composer:focus-within { border-color: var(--focus); box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 18%, transparent), var(--shadow); }
.composer[data-dragging] { border-style: dashed; border-color: var(--accent); background: var(--accent-soft); }
.composer[aria-disabled="true"] { opacity: 0.6; }
.composer__input {
  width: 100%;
  min-height: 24px;
  max-height: 40vh;
  padding: var(--space-1) var(--space-1);
  border: 0;
  background: transparent;
  resize: none;
  outline: none;
  line-height: var(--leading);
}
.composer__bar { display: flex; align-items: center; gap: var(--space-2); }
.composer__hint { flex: 1; min-width: 0; color: var(--text-subtle); font-size: var(--text-xs); }
.composer__send { min-width: 96px; }
.composer__send[data-mode="stop"] { background: var(--text); color: var(--bg); }
.select--sm { width: auto; min-height: 32px; padding: 0 var(--space-2); font-size: var(--text-sm); }
.menu__panel--above { top: auto; bottom: calc(100% + var(--space-2)); left: 0; right: auto; transform-origin: bottom left; min-width: 280px; }

/* ── Cronologia ────────────────────────────────────── */
.chat__history {
  display: flex;
  flex-direction: column;
  width: 320px;
  min-height: 0;
  border-left: 1px solid var(--border);
  background: var(--surface);
  transition: transform var(--duration-slow) var(--ease-out), visibility 0s linear 0s;
}
/* chiuso: scorre verso destra, poi diventa invisibile (fuori dal Tab) */
.chat[data-history="closed"] .chat__history {
  transform: translateX(100%);
  visibility: hidden;
  transition: transform var(--duration-slow) var(--ease-out), visibility 0s linear var(--duration-slow);
}
.history__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-4) var(--space-4) var(--space-2);
}
.history__title { font-size: var(--text-md); }
.history__filters { display: flex; flex-wrap: wrap; gap: var(--space-1); padding: 0 var(--space-4) var(--space-3); border-bottom: 1px solid var(--border); }
.history__filters .chip { height: 26px; padding: 0 var(--space-2); font-size: var(--text-xs); }
.history__filters .chip[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.history__body { flex: 1; min-height: 0; overflow-y: auto; padding: var(--space-2) var(--space-2) var(--space-6); }
.history__section + .history__section { margin-top: var(--space-4); }
.history__label {
  padding: var(--space-2) var(--space-2) var(--space-1);
  color: var(--text-subtle);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}
.history__list { display: grid; gap: 1px; margin: 0; padding: 0; list-style: none; }
.history__list:empty::after { content: attr(data-empty); display: block; padding: var(--space-3) var(--space-2); color: var(--text-subtle); font-size: var(--text-sm); }
.h-item {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: var(--space-2);
  padding: var(--space-2);
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.h-item:hover { background: var(--surface-2); }
.h-item[aria-current="true"] { background: var(--accent-soft); }
.h-item__body { flex: 1; min-width: 0; }
.h-item__title {
  display: -webkit-box;
  overflow: hidden;
  color: var(--text);
  font-size: var(--text-sm);
  font-weight: 500;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
}
.h-item__title:hover { text-decoration: none; }
.h-item__meta { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-1); margin-top: 2px; color: var(--text-subtle); font-size: var(--text-xs); }
.h-item__actions { display: flex; opacity: 0; transition: opacity var(--duration-fast); }
.h-item:hover .h-item__actions, .h-item:focus-within .h-item__actions { opacity: 1; }
.h-item__actions .icon-btn { width: 28px; height: 28px; }
.h-item__actions .icon { width: 15px; height: 15px; }
.h-item .pulse { margin-top: 6px; color: var(--accent); }
.chat__scrim { display: none; }

@media (hover: none) {
  .h-item__actions { opacity: 1; }
}

@media (max-width: 1200px) {
  .chat, .chat[data-history="closed"] { grid-template-columns: minmax(0, 1fr); }
  .chat__history {
    position: fixed;
    inset: 0 0 0 auto;
    z-index: var(--z-drawer);
    width: min(360px, 90vw);
    transform: translateX(100%);
    visibility: visible;
    transition: transform var(--duration) var(--ease-out);
    box-shadow: var(--shadow-lg);
  }
  .chat__history[data-open="true"] { transform: none; }
  .chat__scrim:not([hidden]) { display: block; position: fixed; inset: 0; z-index: calc(var(--z-drawer) - 1); background: rgb(10 12 16 / 0.4); }
}

@media (max-width: 900px) {
  .chat { height: calc(100dvh - var(--topbar-height)); }
  .chat__header, .mode-bar { padding-left: var(--space-4); padding-right: var(--space-4); }
  .chat__thread { padding: var(--space-4) var(--space-4) var(--space-6); }
  .chat__bottom { padding: 0 var(--space-3) var(--space-3); }
  .mode-bar { overflow-x: auto; }
  .mode-bar .segmented { flex-wrap: nowrap; }
}

@media (max-width: 560px) {
  .hide-sm { display: none; }
  .msg--assistant { grid-template-columns: minmax(0, 1fr); }
  .msg__avatar { display: none; }
  .composer__send { min-width: 0; }
  .composer__send span { display: none; }
}

/* ── Galleria immagini ─────────────────────────────── */
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: var(--space-4); }
.gallery__item { display: grid; overflow: hidden; border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--surface); }
.gallery__img { aspect-ratio: 1; width: 100%; padding: 0; border: 0; background: var(--surface-2); cursor: zoom-in; }
.gallery__img img { width: 100%; height: 100%; object-fit: cover; }
.gallery__body { display: grid; gap: var(--space-2); padding: var(--space-3); }
.gallery__prompt { display: -webkit-box; overflow: hidden; font-size: var(--text-sm); -webkit-box-orient: vertical; -webkit-line-clamp: 3; line-clamp: 3; }
.gallery__foot { display: flex; align-items: center; justify-content: space-between; color: var(--text-subtle); font-size: var(--text-xs); }

/* ── Rete di Sicurezza ─────────────────────────────── */
/* Stato "intro": casella al centro sotto il titolo. Stato "chat": thread e
   casella in basso. Il passaggio lo anima la View Transition del browser:
   la casella ha un nome proprio e "vola" dalla posizione centrale al fondo. */
.verify-chat .composer { view-transition-name: verify-composer; }
.verify-intro__title { view-transition-name: verify-title; }

.verify-chat[data-state="intro"] .chat__scroll { flex: 0 0 auto; margin-top: auto; overflow: visible; }
.verify-chat[data-state="intro"] .chat__bottom { margin-bottom: auto; padding-bottom: var(--space-10); }
.verify-chat[data-state="intro"] .chat__thread { display: none; }
.verify-chat[data-state="chat"] .verify-intro { display: none; }
.verify-chat[data-state="chat"] .verify-legend { display: none; }

.verify-intro {
  display: grid;
  justify-items: center;
  gap: var(--space-3);
  max-width: 640px;
  margin: 0 auto;
  padding: var(--space-6) var(--space-6) var(--space-6);
  text-align: center;
}
.verify-intro__icon {
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border-radius: var(--radius-lg);
  background: var(--accent-soft);
  color: var(--accent);
}
.verify-intro__icon .icon { width: 28px; height: 28px; }
.verify-intro__title { font-family: var(--font-serif); font-size: var(--text-2xl); font-weight: 600; }
.verify-intro__lead { max-width: 52ch; color: var(--text-muted); }
.verify-chat[data-state="intro"] .composer__input { min-height: 72px; }

.composer__alt span { white-space: nowrap; }
.legend { border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--surface); }
.legend__summary { padding: var(--space-3) var(--space-4); color: var(--text-muted); font-size: var(--text-sm); font-weight: 500; cursor: pointer; }
.legend__list { display: grid; gap: var(--space-2); margin: 0; padding: 0 var(--space-4) var(--space-4); }
.legend__list div { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); font-size: var(--text-sm); }
.legend__list dt, .legend__list dd { margin: 0; }
.legend__list dd { color: var(--text-muted); }

/* Durata e curva della trasformazione */
::view-transition-group(verify-composer) {
  animation-duration: 520ms;
  animation-timing-function: cubic-bezier(0.2, 0.9, 0.25, 1);
}
::view-transition-old(verify-title) { animation: 240ms ease-in both vt-fade-up; }
::view-transition-old(root),
::view-transition-new(root) { animation-duration: 320ms; }
@keyframes vt-fade-up { to { opacity: 0; transform: translateY(-16px) scale(0.98); } }

/* Messaggi che entrano nel thread */
.msg--enter { animation: msg-in 420ms cubic-bezier(0.2, 0.9, 0.25, 1) both; }
.msg--enter + .msg--enter { animation-delay: 120ms; }
@keyframes msg-in { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }

@media (max-width: 560px) {
  .composer__alt span { display: none; }
  .verify-chat[data-state="intro"] .chat__bottom { padding-bottom: var(--space-4); }
}

/* ── Telefono e touch (chat, Rete, galleria) ─────── */
@media (pointer: coarse) {
  .segmented > button, .segmented > label { min-height: 36px; }
  .history__filters .chip { height: 32px; padding: 0 var(--space-3); }
  .h-item { padding: var(--space-3) var(--space-2); }
}

/* Spazio per la barra home dell'iPhone sotto la casella */
.chat__bottom { padding-bottom: max(var(--space-4), env(safe-area-inset-bottom)); }

@media (max-width: 560px) {
  .chat__header { gap: var(--space-2); padding-top: var(--space-3); }
  .chat__avatar { width: 34px; height: 34px; }
  .chat__thread { gap: var(--space-5); padding: var(--space-4) var(--space-3) var(--space-5); }
  .chat__bottom { padding: 0 var(--space-2) max(var(--space-2), env(safe-area-inset-bottom)); }
  .msg__bubble { max-width: 100%; }
  .composer { padding: var(--space-2); }
  .composer__hint { display: none; }
  .md-chart__canvas { height: 280px; }
  .md-chart__download span, .md-chart__download { font-size: var(--text-xs); }
  .verify-intro { padding: var(--space-4) var(--space-3); }
  .verify-intro__title { font-size: var(--text-xl); }
  .gallery { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-2); }
  .gallery__body { padding: var(--space-2); }
}
@media (max-width: 900px), (pointer: coarse) {
  .select--sm { font-size: 16px; min-height: 36px; }
}

/* Cursore alla fine del testo mentre l'agente scrive */
.msg__body[data-streaming] > :last-child:not(.tools, details)::after {
  content: "";
  display: inline-block;
  width: 0.5em;
  height: 1em;
  margin-left: 2px;
  vertical-align: -0.12em;
  border-radius: 2px;
  background: var(--agent-color, var(--accent));
  animation: caret 1s steps(2, start) infinite;
}
@keyframes caret { to { visibility: hidden; } }

/* Messaggi nuovi nella chat agenti */
.msg--new { animation: msg-in 380ms cubic-bezier(0.2, 0.9, 0.25, 1) both; }

/* Frecce delle versioni di una domanda rigenerata */
.pager { display: inline-flex; align-items: center; gap: 2px; color: var(--text-muted); font-size: var(--text-sm); }
.pager__btn { width: 28px; height: 28px; }
.pager__btn:disabled { opacity: 0.35; cursor: default; }
.pager__btn:disabled:hover { background: none; }
.pager__label { min-width: 3.2em; text-align: center; font-variant-numeric: tabular-nums; }
@media (pointer: coarse) { .pager__btn { width: 36px; height: 36px; } }
.msg--old .msg__bubble { background: var(--surface-2); }
.banner--version { background: var(--warning-soft); color: var(--warning); }

/* ── Sala Briefing ─────────────────────────────────── */
.msg__author { display: flex; align-items: baseline; gap: var(--space-1); color: var(--agent-color, var(--text)); font-size: var(--text-sm); font-weight: 600; }
.msg__author span { color: var(--text-subtle); font-weight: 400; }

/* Tappe del briefing: barra di avanzamento sotto la testata */
.pipeline { padding: 0 var(--space-6) var(--space-3); border-bottom: 1px solid var(--border); overflow-x: auto; scrollbar-width: none; }
.pipeline::-webkit-scrollbar { display: none; }
.pipeline__list { display: flex; gap: var(--space-1); margin: 0; padding: 0; list-style: none; }
.step {
  --c: var(--step-color, var(--accent));
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-3) var(--space-1) var(--space-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--surface);
  color: var(--text-subtle);
  font-size: var(--text-xs);
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  transition: color var(--duration) var(--ease-out), border-color var(--duration) var(--ease-out), background-color var(--duration) var(--ease-out);
}
.step__dot {
  position: relative;
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--surface-3);
  transition: background-color var(--duration) var(--ease-out), transform var(--duration) var(--ease-out);
}
.step__dot .icon { width: 12px; height: 12px; stroke-width: 2.5; color: #fff; opacity: 0; transform: scale(0.5); transition: opacity var(--duration), transform var(--duration) var(--ease-out); }
.step[data-state="running"] { border-color: var(--c); color: var(--text); }
.step[data-state="running"] .step__dot { background: color-mix(in srgb, var(--c) 25%, transparent); }
.step[data-state="running"] .step__dot::after {
  content: ""; position: absolute; inset: -3px; border-radius: 50%;
  border: 2px solid var(--c); border-right-color: transparent;
  animation: spin 0.9s linear infinite;
}
.step[data-state="done"] { color: var(--text-muted); }
.step[data-state="done"] .step__dot { background: var(--c); }
.step[data-state="done"] .step__dot .icon { opacity: 1; transform: none; }
.step[data-state="error"] { color: var(--danger); border-color: var(--danger-soft); }
.step[data-state="error"] .step__dot { background: var(--danger); }
.step[data-state="skipped"] { opacity: 0.5; }

/* Le tappe nella schermata d'ingresso */
.intro-steps { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--space-2); margin: var(--space-2) 0 0; padding: 0; list-style: none; }
.intro-steps li {
  display: flex; align-items: center; gap: var(--space-2);
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-full); background: var(--surface); border: 1px solid var(--border);
  font-size: var(--text-xs); color: var(--text-muted);
}
.intro-steps .nav__dot { margin: 0; }

/* "In breve" e testo lungo richiudibile */
.brief {
  padding: var(--space-3) var(--space-4);
  border-left: 3px solid var(--agent-color, var(--accent));
  border-radius: 0 var(--radius) var(--radius) 0;
  background: color-mix(in srgb, var(--agent-color, var(--accent)) 7%, var(--surface));
  font-size: var(--text-sm);
}
.brief__title { margin-bottom: var(--space-1); color: var(--text-muted); font-size: var(--text-xs); font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; }
.brief ul { margin: 0; padding-left: 1.1em; }
.brief li + li { margin-top: 2px; }
.msg__body[data-collapsed] {
  max-height: 420px;
  overflow: hidden;
  -webkit-mask-image: linear-gradient(180deg, #000 70%, transparent);
  mask-image: linear-gradient(180deg, #000 70%, transparent);
}
.msg__expand { justify-self: start; }
.msg--system { justify-items: center; color: var(--text-subtle); font-size: var(--text-sm); }

@media (max-width: 900px) {
  .pipeline { padding-left: var(--space-4); padding-right: var(--space-4); }
}
