/* Contenuti generati: markdown, ragionamento, grafici, allegati, lightbox.
   Condiviso da chat agenti, briefing e Rete di Sicurezza. */

/* ── Markdown ──────────────────────────────────────── */
.md {
  font-size: var(--text-md);
  line-height: 1.65;
  overflow-wrap: anywhere;
}
.md > * + * { margin-top: var(--space-3); }
.md .md-heading { margin-top: var(--space-6); font-family: var(--font-sans); }
.md .md-heading:first-child { margin-top: 0; }
.md h2.md-heading { font-size: var(--text-xl); }
.md h3.md-heading { font-size: var(--text-lg); }
.md h4.md-heading, .md h5.md-heading, .md h6.md-heading { font-size: var(--text-md); }
.md strong { font-weight: 600; }
.md code {
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--surface-2);
  font-size: 0.88em;
}
.md .md-code {
  margin: 0;
  padding: var(--space-3) var(--space-4);
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  font-size: var(--text-sm);
  line-height: 1.5;
}
.md .md-code code { padding: 0; background: none; }
.md .md-list { margin: 0; padding-left: 1.4em; }
.md .md-list li + li { margin-top: var(--space-1); }
.md .md-list .md-list { margin-top: var(--space-1); }
.md .md-quote {
  margin: 0;
  padding: var(--space-1) var(--space-3);
  border-left: 3px solid var(--border-strong);
  color: var(--text-muted);
  font-size: var(--text-sm);
}
.md .md-quote > * + * { margin-top: var(--space-1); }
.md .md-hr { border: 0; border-top: 1px solid var(--border); margin: var(--space-6) 0; }
.md a { text-decoration: underline; text-underline-offset: 2px; }
.md-badge { vertical-align: 1px; }

.md-table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.md-table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
.md-table th, .md-table td { padding: var(--space-2) var(--space-3); border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.md-table th { background: var(--surface-2); font-weight: 600; white-space: nowrap; }
.md-table tr:last-child td { border-bottom: 0; }

.md-verdict {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  font-size: var(--text-sm);
}
.md-verdict--success { background: var(--success-soft); color: var(--success); }
.md-verdict--warning { background: var(--warning-soft); color: var(--warning); }
.md-verdict--danger { background: var(--danger-soft); color: var(--danger); }
.md-verdict span { color: var(--text); }

.md-figure { margin: 0; }
.md-image {
  display: block;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  overflow: hidden;
  cursor: zoom-in;
}
.md-image img { max-height: 520px; width: auto; }

/* ── Grafici ───────────────────────────────────────── */
.md-chart {
  position: relative;
  margin: 0;
  padding: var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.md-chart__canvas { width: 100%; height: 380px; }
.md-chart--pending,
.md-chart[data-pending] .md-chart__canvas {
  display: grid;
  place-items: center;
  min-height: 120px;
  color: var(--text-subtle);
  font-size: var(--text-sm);
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 50%, var(--surface-2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
}
.md-chart__download { position: absolute; right: var(--space-2); top: var(--space-2); }
.md-chart__error { display: flex; align-items: center; gap: var(--space-2); color: var(--text-muted); font-size: var(--text-sm); }

/* ── Ragionamento ──────────────────────────────────── */
.reasoning {
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface-2);
  font-size: var(--text-sm);
}
.reasoning__summary {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  color: var(--text-muted);
  font-weight: 500;
  cursor: pointer;
  list-style: none;
}
.reasoning__summary::-webkit-details-marker { display: none; }
.reasoning__summary::before {
  content: "";
  width: 6px;
  height: 6px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(-45deg);
  transition: transform var(--duration-fast);
}
.reasoning[open] .reasoning__summary::before { transform: rotate(45deg); }
.reasoning__body {
  max-height: 320px;
  overflow-y: auto;
  padding: 0 var(--space-3) var(--space-3);
  color: var(--text-muted);
  white-space: pre-wrap;
}

/* ── Allegati (pillole) ────────────────────────────── */
.pills {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin: 0;
  padding: 0;
  list-style: none;
}
.pill {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  max-width: 280px;
  padding: var(--space-1) var(--space-1) var(--space-1) var(--space-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  font-size: var(--text-xs);
}
.pill__icon { display: grid; place-items: center; width: 22px; color: var(--text-muted); }
.pill__thumb { width: 28px; height: 28px; border-radius: 4px; object-fit: cover; }
.pill__name {
  display: grid;
  min-width: 0;
  padding: 0;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
}
.pill__name:disabled { cursor: default; }
.pill__label { overflow: hidden; font-weight: 500; text-overflow: ellipsis; white-space: nowrap; }
.pill__meta { color: var(--text-subtle); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pill__remove {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border: 0;
  border-radius: 4px;
  background: none;
  color: var(--text-subtle);
  cursor: pointer;
}
.pill__remove:hover { background: var(--surface-3); color: var(--text); }
.pill__remove .icon { width: 14px; height: 14px; }

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

.attachment-preview {
  max-height: 50vh;
  margin: 0;
  padding: var(--space-3);
  overflow: auto;
  border-radius: var(--radius);
  background: var(--surface-2);
  font-size: var(--text-xs);
  white-space: pre-wrap;
}

/* ── Lightbox ──────────────────────────────────────── */
.lightbox {
  width: 100vw;
  max-width: 100vw;
  height: 100dvh;
  max-height: 100dvh;
  padding: var(--space-4);
  border: 0;
  background: transparent;
}
.lightbox::backdrop { background: rgb(8 9 12 / 0.85); }
.lightbox[open] { display: grid; grid-template-rows: auto 1fr; gap: var(--space-3); place-items: center; }
.lightbox__bar { display: flex; gap: var(--space-2); justify-self: end; }
.lightbox__close { background: var(--surface); }
.lightbox__img { max-width: 100%; max-height: calc(100dvh - 96px); border-radius: var(--radius); object-fit: contain; }

/* ── Dialog: varianti usate dai contenuti ──────────── */
.dialog--wide { width: min(640px, calc(100vw - 32px)); }
.dialog__left { margin-right: auto; }
.fieldset { display: grid; gap: var(--space-2); margin: 0; padding: 0; border: 0; }
.check-list { display: grid; gap: var(--space-1); max-height: 180px; overflow-y: auto; }
.check { display: flex; align-items: center; gap: var(--space-2); font-size: var(--text-sm); cursor: pointer; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }
.check__meta { color: var(--text-subtle); font-size: var(--text-xs); }

.handoff__preview {
  max-height: 140px;
  margin: 0;
  padding: var(--space-3);
  overflow-y: auto;
  border-left: 3px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: var(--text-sm);
  white-space: pre-wrap;
}
.agent-pick-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: var(--space-2); }
.agent-pick {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: var(--text-sm);
  font-weight: 500;
  cursor: pointer;
}
.agent-pick[aria-pressed="true"] { border-color: var(--agent-color); box-shadow: inset 0 0 0 1px var(--agent-color); }
.agent-pick .nav__dot { --agent-color: inherit; background: var(--agent-color); margin: 0; }

.yt-result { display: grid; gap: var(--space-2); padding: var(--space-3); border-radius: var(--radius); background: var(--surface-2); }
.yt-result__title { font-weight: 600; }
.yt-result__actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }

/* ── Controlli segmentati ──────────────────────────── */
.segmented {
  position: relative;
  display: inline-flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 3px;
  border-radius: var(--radius);
  background: var(--surface-2);
}
.segmented > button,
.segmented > label {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-3);
  border: 0;
  border-radius: calc(var(--radius) - 3px);
  background: none;
  color: var(--text-muted);
  font-size: var(--text-sm);
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
}
.segmented > button:hover,
.segmented > label:hover { color: var(--text); }
.segmented > button,
.segmented > label { position: relative; z-index: 1; transition: color var(--duration) var(--ease-out); }
.segmented > button[aria-selected="true"] { color: var(--text); }
/* senza JS (o per i radio) lo sfondo resta sulla scheda stessa */
.segmented:not(:has(.segmented__indicator)) > button[aria-selected="true"],
.segmented > label:has(input:checked) { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }
.segmented__indicator {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 0;
  border-radius: calc(var(--radius) - 3px);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  pointer-events: none;
  transition: transform var(--duration-slow) var(--ease-out), width var(--duration-slow) var(--ease-out), height var(--duration-slow) var(--ease-out);
}
.segmented > label input { position: absolute; opacity: 0; pointer-events: none; }

/* ── Strumenti usati dall'agente ───────────────────── */
.tools {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: var(--text-sm);
  transition: border-color var(--duration) var(--ease-out), box-shadow var(--duration) var(--ease-out);
}
.tools.is-running { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 10%, transparent); }
.tools__summary {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  color: var(--text-muted);
  cursor: pointer;
  list-style: none;
  user-select: none;
}
.tools__summary::-webkit-details-marker { display: none; }
.tools__summary:hover { color: var(--text); }
.tools__summary > .icon:last-child { margin-left: auto; transition: transform var(--duration) var(--ease-out); }
.tools[open] > .tools__summary > .icon:last-child { transform: rotate(180deg); }
.tools__icons { display: flex; flex: none; }
.tools__badge {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  margin-left: -6px;
  border: 2px solid var(--surface);
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
}
.tools__badge:first-child { margin-left: 0; }
.tools__badge .icon { width: 12px; height: 12px; stroke-width: 2.2; }
.tools__title { min-width: 0; color: var(--text); font-weight: 500; overflow-wrap: anywhere; }
.tools__count { color: var(--text-subtle); font-weight: 400; }
.tools__count::before { content: " · "; }
.tools.is-running .tools__title { color: var(--accent); }

.tools__list {
  position: relative;
  display: grid;
  gap: 2px;
  margin: 0;
  padding: 0 var(--space-3) var(--space-3);
  list-style: none;
}
/* linea che unisce i passaggi, come una piccola cronologia */
.tool:not(:last-child)::before {
  content: "";
  position: absolute;
  top: calc(var(--space-1) + 28px);
  bottom: calc(-1 * var(--space-1) - 2px);
  left: 13px;
  width: 2px;
  border-radius: 1px;
  background: var(--border);
}
.tool { position: relative; display: flex; align-items: flex-start; gap: var(--space-3); padding: var(--space-1) 0; animation: content-in 260ms var(--ease-out) both; }
.tool__icon {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  flex: none;
  border: 2px solid var(--surface);
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text-muted);
}
.tool__icon .icon { width: 14px; height: 14px; }
.tool[data-kind="search"] .tool__icon { background: var(--info-soft); color: var(--info); }
.tool[data-kind="read"] .tool__icon { background: var(--success-soft); color: var(--success); }
.tool[data-kind="archive"] .tool__icon { background: var(--warning-soft); color: var(--warning); }
.tool[data-kind="rss"] .tool__icon { background: var(--danger-soft); color: var(--danger); }
.tool[data-kind="social"] .tool__icon { background: var(--accent-soft); color: var(--accent); }
.tool[data-kind="mcp"] .tool__icon { background: var(--surface-3); color: var(--text); }
.tool__body { display: grid; gap: 1px; flex: 1; min-width: 0; padding-top: 4px; }
.tool__label { color: var(--text-muted); font-size: var(--text-xs); font-weight: 600; letter-spacing: 0.02em; }
.tool__query { color: var(--text); overflow-wrap: anywhere; }
.tool__link { display: flex; min-width: 0; color: var(--text); text-decoration: none; }
.tool__link:hover .tool__host { text-decoration: underline; }
.tool__host { flex: none; font-weight: 500; }
.tool__path { min-width: 0; overflow: hidden; color: var(--text-subtle); text-overflow: ellipsis; white-space: nowrap; }
.tool__state { display: flex; align-items: center; gap: var(--space-2); flex: none; padding-top: 6px; color: var(--accent); font-size: var(--text-xs); font-weight: 500; }
.tool.is-running .tool__icon { box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); animation: pulse 1.4s ease-in-out infinite; }

/* apertura e chiusura fluide dove il browser lo consente */
@supports selector(::details-content) {
  .tools { interpolate-size: allow-keywords; }
  .tools::details-content {
    block-size: 0;
    overflow: clip;
    opacity: 0;
    transition: block-size var(--duration-slow) var(--ease-out), opacity var(--duration) var(--ease-out), content-visibility var(--duration-slow) allow-discrete;
  }
  .tools[open]::details-content { block-size: auto; opacity: 1; }
}
@media (max-width: 640px) {
  .tools__summary { padding: var(--space-2); gap: var(--space-2); }
  .tools__icons { display: none; }
  .tools__count { display: block; font-size: var(--text-xs); }
  .tools__count::before { content: none; }
  .tools__list { padding: 0 var(--space-2) var(--space-2); }
  .tool__state { font-size: 0; gap: 0; }
}
@media (prefers-reduced-motion: reduce) {
  .tools::details-content, .tool, .tool.is-running .tool__icon { transition: none; animation: none; }
}
