/* ═══════════════════════════════════════════════════════════
   Assistant — finto chatbot di navigazione (side panel)
   Prefisso: as-   |   IIFE: assistant.js (window.asRender)
   LAB feature sperimentale: icona dedicata, facile da rimuovere.
   Rispetta i design token di global.css. Accent VIOLA/BLU
   (volutamente diverso dal gold dell'AI Report: questo è un
   navigatore, non l'analisi AI vera).
   Tema-aware: tutte le tinte testo/bolle passano da variabili
   sovrascritte sotto :root[data-theme="light"].
   ═══════════════════════════════════════════════════════════ */

:root {
  --as-accent: #7c8cff;
  --as-accent-2: #59c2ff;
  --as-grad: linear-gradient(135deg, #7c8cff 0%, #59c2ff 100%);
  /* dark defaults */
  --as-text: rgba(255, 255, 255, .92);
  --as-text-dim: rgba(255, 255, 255, .5);
  --as-text-strong: #fff;
  --as-bubble-bg: rgba(255, 255, 255, .045);
  --as-bubble-border: rgba(255, 255, 255, .09);
  --as-hover: rgba(255, 255, 255, .08);
  --as-dot: rgba(255, 255, 255, .55);
}
:root[data-theme="light"] {
  --as-accent: #5566f0;
  --as-accent-2: #2f7fe0;
  --as-grad: linear-gradient(135deg, #5566f0 0%, #2f7fe0 100%);
  --as-text: #1c2030;
  --as-text-dim: #5b6276;
  --as-text-strong: #0e1220;
  --as-bubble-bg: rgba(20, 30, 60, .035);
  --as-bubble-border: rgba(20, 30, 60, .12);
  --as-hover: rgba(20, 30, 60, .055);
  --as-dot: rgba(20, 30, 60, .45);
}

/* ── Root ─────────────────────────────────────────────────── */
.as-root {
  display: flex;
  flex-direction: column;
  height: 100%;
  font-family: var(--font-ui);
  overflow: hidden;
}

/* ── Header ───────────────────────────────────────────────── */
.as-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px 12px;
  border-bottom: 1px solid var(--as-bubble-border);
  flex: 0 0 auto;
}
.as-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--as-grad);
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, .08), 0 4px 14px rgba(85, 102, 240, .28);
}
.as-avatar svg { width: 18px; height: 18px; fill: #fff; stroke: #fff; }
.as-head-meta { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.as-head-name { font-size: .94rem; font-weight: 600; color: var(--as-text-strong); }
.as-head-sub { font-size: .82rem; color: var(--as-text-dim); }
.as-head-back {
  margin-left: auto;
  display: none;
  align-items: center;
  gap: 5px;
  padding: 5px 10px;
  font-family: var(--font-ui);
  font-size: .82rem;
  color: var(--as-text-dim);
  background: var(--as-bubble-bg);
  border: 1px solid var(--as-bubble-border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--ease), color var(--ease);
}
.as-head-back svg { width: 13px; height: 13px; }
.as-head-back:hover { background: var(--as-hover); color: var(--as-text); }
.as-head-back.show { display: inline-flex; }

/* ── Stream ───────────────────────────────────────────────── */
.as-stream {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 18px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
  position: relative;   /* offsetTop dei figli relativo allo stream (anchorTop) */
}
.as-sentinel { flex: 0 0 auto; height: 8px; margin: -4px 0; }
.as-stream::-webkit-scrollbar { width: 8px; }
.as-stream::-webkit-scrollbar-thumb { background: var(--as-bubble-border); border-radius: 4px; }

/* ── Rows (assistant left / user right) ───────────────────── */
.as-row { display: flex; gap: 9px; align-items: flex-start; animation: as-fade-in .25s var(--ease); }
.as-row-user { justify-content: flex-end; }
.as-row-avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--as-grad);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 2px;
}
.as-row-avatar svg { width: 13px; height: 13px; fill: #fff; stroke: #fff; }

/* ── Bubbles ──────────────────────────────────────────────── */
.as-bubble {
  max-width: 84%;
  padding: 10px 13px;
  border-radius: var(--radius-md);
  font-size: .92rem;
  line-height: 1.5;
  color: var(--as-text);
}
.as-bubble-ai {
  background: var(--as-bubble-bg);
  border: 1px solid var(--as-bubble-border);
  border-top-left-radius: 4px;
}
.as-bubble-user {
  background: var(--as-grad);
  color: #fff;
  font-weight: 500;
  border-top-right-radius: 4px;
  box-shadow: 0 4px 14px rgba(85, 102, 240, .22);
}
.as-bubble b, .as-bubble strong { color: var(--as-text-strong); font-weight: 600; }
.as-bubble-user b, .as-bubble-user strong { color: #fff; }
.as-bubble p { margin: 0; }
.as-bubble p + p { margin-top: 8px; }

/* ── Aforisma: citazione celebre (feature "simpatica") ────── */
.as-aph { position: relative; padding: 6px 2px 2px 6px; }
.as-aph::before {
  content: '\201C';                 /* virgoletta decorativa */
  position: absolute; top: -12px; left: -4px;
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 2.6rem; line-height: 1;
  color: var(--as-accent); opacity: .32;
  pointer-events: none;
}
.as-aph-text {
  font-style: italic;
  font-size: 1rem;
  line-height: 1.55;
  color: var(--as-text-strong);
}
.as-aph-by {
  margin-top: 9px;
  font-size: .85rem;
  font-weight: 600;
  color: var(--as-accent-2);
  text-align: right;
}

/* ── Card iniezione: ospita la sezione REALE del report ──── */
.as-row-card { align-items: flex-start; }
.as-row-card .as-card {
  flex: 1 1 auto;
  min-width: 0;
  max-width: 100%;
  background: var(--as-bubble-bg);
  border: 1px solid var(--as-bubble-border);
  border-radius: var(--radius-md);
  border-top-left-radius: 4px;
  padding: 10px 12px;
  overflow: hidden;
}
.as-card-loading { padding: 12px 13px; }
.as-card-in { animation: as-fade-in .3s var(--ease); }
/* il contenuto rpt-* eredita gli stili del report (report.css); qui solo fit */
.as-card .as-rpt { font-size: .9rem; }
.as-card .as-rpt .rpt-section { margin: 0 0 14px; }
.as-card .as-rpt .rpt-section:last-child { margin-bottom: 0; }
/* Predittori Annuali: card incolonnate nella chat (nel report sono a 3 colonne) */
.as-card .yr-ins-grid { display: grid !important; grid-template-columns: 1fr !important; gap: 12px; }
/* messaggio inline quando l'iniezione non ha dati (no navigazione automatica) */
.as-card .as-rpt .as-empty { margin: 0; padding: 4px 0; opacity: .75; }

/* banner verdetto sincrono in cima al "quadro completo" */
.as-verdict {
  display: flex; align-items: center; gap: 8px;
  margin-bottom: 12px; padding: 8px 11px;
  border-radius: var(--radius-sm); font-weight: 600;
}
.as-verdict-lbl { font-size: .8rem; color: var(--as-text-dim); text-transform: uppercase; letter-spacing: .04em; }
.as-verdict-val { margin-left: auto; font-size: .95rem; font-weight: 700; }
.as-verdict-pos { background: rgba(31, 175, 99, .12); }  .as-verdict-pos .as-verdict-val { color: #1faf63; }
.as-verdict-neg { background: rgba(224, 64, 94, .12); }  .as-verdict-neg .as-verdict-val { color: #e0405e; }
.as-verdict-neu { background: rgba(192, 138, 46, .12); } .as-verdict-neu .as-verdict-val { color: #c08a2e; }
:root[data-theme="light"] .as-verdict-pos .as-verdict-val { color: #0f9d52; }
:root[data-theme="light"] .as-verdict-neg .as-verdict-val { color: #d12f4d; }
:root[data-theme="light"] .as-verdict-neu .as-verdict-val { color: #a9761f; }

/* nota "timeframe corrente" sotto la lettura daily del trend */
.as-trend-tf {
  margin-top: 12px; padding-top: 10px;
  border-top: 1px solid var(--as-border, rgba(255,255,255,.08));
}
.as-trend-tf-h {
  font-size: .82rem; font-weight: 700; color: var(--as-text-dim);
  text-transform: uppercase; letter-spacing: .04em; margin-bottom: 6px;
}

/* istogramma distribuzione durate trend */
.as-hist-wrap { margin: 10px 0 6px; }
.as-hist { display: block; width: 100%; height: auto; overflow: visible; }
.as-hist-cap { display: flex; gap: 16px; margin-top: 4px; font-size: .82rem; color: var(--as-text-dim); }
.as-hist-cap span { display: inline-flex; align-items: center; }
.as-hist-cap i { width: 11px; height: 11px; border-radius: 2px; margin-right: 6px; }
.as-hist-cap i.as-hist-k-med {
  width: 0; height: 12px; border-radius: 0; background: none !important;
  border-left: 2px dashed var(--as-text-dim);
}

/* matrice di allineamento multi-timeframe */
.as-mtf {
  margin-top: 14px; padding-top: 12px;
  border-top: 1px solid var(--as-border, rgba(255,255,255,.08));
}
.as-mtf-h {
  font-size: .82rem; font-weight: 700; color: var(--as-text-dim);
  text-transform: uppercase; letter-spacing: .04em; margin-bottom: 8px;
}
.as-mtf-grid { display: flex; flex-direction: column; gap: 5px; }
.as-mtf-row {
  display: grid; grid-template-columns: 2.4em auto 1fr; gap: 9px; align-items: center;
}
.as-mtf-tf { font-weight: 700; font-size: .9rem; color: var(--as-text-dim); }
.as-mtf-badge {
  justify-self: start; padding: 2px 8px; border-radius: var(--radius-sm);
  font-size: .82rem; font-weight: 700; letter-spacing: .02em; white-space: nowrap;
}
.as-mtf-pos { background: rgba(31,175,99,.14); color: #1faf63; }
.as-mtf-neg { background: rgba(224,64,94,.14); color: #e0405e; }
.as-mtf-neu { background: rgba(192,138,46,.14); color: #c08a2e; }
:root[data-theme="light"] .as-mtf-pos { color: #0f9d52; }
:root[data-theme="light"] .as-mtf-neg { color: #d12f4d; }
:root[data-theme="light"] .as-mtf-neu { color: #a9761f; }
.as-mtf-dur { justify-self: end; text-align: right; font-size: .9rem; color: var(--as-text); opacity: .85; }
.as-mtf-sum {
  margin-top: 10px; padding: 7px 11px; border-radius: var(--radius-sm);
  font-size: .9rem; font-weight: 600;
}
.as-mtf-sum-pos { background: rgba(31,175,99,.12); color: #1faf63; }
.as-mtf-sum-neg { background: rgba(224,64,94,.12); color: #e0405e; }
.as-mtf-sum-neu { background: rgba(192,138,46,.12); color: #c08a2e; }
:root[data-theme="light"] .as-mtf-sum-pos { color: #0f9d52; }
:root[data-theme="light"] .as-mtf-sum-neg { color: #d12f4d; }
:root[data-theme="light"] .as-mtf-sum-neu { color: #a9761f; }

/* CTA sotto la card */
.as-cta { border-color: var(--as-accent); }
.as-cta .as-opt-ico { background: var(--as-grad); color: #fff; }
.as-cta .as-opt-chev { color: var(--as-accent-2); font-weight: 700; }

/* "Torna alle domande" — bottone primario a fine risposta */
.as-back2q {
  margin-top: 4px;
  background: linear-gradient(135deg, rgba(124, 140, 255, .16), rgba(89, 194, 255, .12));
  border-color: var(--as-accent);
}
.as-back2q:hover { background: linear-gradient(135deg, rgba(124, 140, 255, .26), rgba(89, 194, 255, .2)); }
.as-back2q .as-opt-ico { background: var(--as-grad); color: #fff; }
.as-back2q .as-opt-label { font-weight: 600; color: var(--as-text); }

/* typing caret */
.as-caret {
  display: inline-block;
  width: 2px;
  height: 1em;
  margin-left: 1px;
  background: var(--as-accent-2);
  vertical-align: text-bottom;
  animation: as-blink 1s step-end infinite;
}
@keyframes as-blink { 50% { opacity: 0; } }

/* typing dots */
.as-dots { display: inline-flex; gap: 4px; padding: 3px 2px; }
.as-dots span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--as-dot);
  animation: as-dot 1.2s infinite ease-in-out both;
}
.as-dots span:nth-child(2) { animation-delay: .18s; }
.as-dots span:nth-child(3) { animation-delay: .36s; }
@keyframes as-dot {
  0%, 80%, 100% { transform: translateY(0); opacity: .35; }
  40% { transform: translateY(-4px); opacity: 1; }
}

/* ── Options ──────────────────────────────────────────────── */
.as-opts {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 2px 0 4px 35px; /* indent under assistant avatar */
}
.as-opt {
  display: flex;
  align-items: center;
  gap: 11px;
  width: 100%;
  text-align: left;
  padding: 11px 13px;
  font-family: var(--font-ui);
  background: var(--as-bubble-bg);
  border: 1px solid var(--as-bubble-border);
  border-radius: var(--radius-md);
  cursor: pointer;
  color: var(--as-text);
  opacity: 0;
  transform: translateY(7px);
  transition: background var(--ease), border-color var(--ease), transform var(--ease), opacity var(--ease);
}
.as-opt.as-in { opacity: 1; transform: translateY(0); }
.as-opt:hover { background: var(--as-hover); border-color: var(--as-accent); }
.as-opt:active { transform: scale(.985); }
.as-opt:disabled { cursor: default; opacity: .4; pointer-events: none; }

.as-opt-ico {
  width: 30px; height: 30px;
  flex: 0 0 auto;
  border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  color: var(--as-accent-2);
  background: rgba(85, 102, 240, .12);
}
.as-opt-ico svg { width: 17px; height: 17px; }
.as-opt-txt { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1 1 auto; }
.as-opt-label { font-size: .92rem; font-weight: 500; line-height: 1.25; color: var(--as-text); }
.as-opt-sub { font-size: .82rem; color: var(--as-text-dim); line-height: 1.25; }
.as-opt-chev { color: var(--as-text-dim); font-size: 1.1rem; flex: 0 0 auto; }

/* domanda già chiesta: spuntata e attenuata (ma ancora ri-cliccabile) */
.as-opt-done { opacity: .62; }
.as-opt-done .as-opt-label { color: var(--as-text-dim); }
.as-opt-done:hover { opacity: 1; }
.as-opt-check { color: #1faf63; font-size: 1rem; font-weight: 700; flex: 0 0 auto; }
:root[data-theme="light"] .as-opt-check { color: #0f9d52; }

/* bottone "Torna indietro" dentro il menu delle sotto-domande */
.as-opt-back { background: transparent; border-style: dashed; }
.as-opt-back .as-opt-ico { background: transparent; color: var(--as-text-dim); }
.as-opt-back .as-opt-label { color: var(--as-text-dim); }
.as-opt-back:hover .as-opt-label { color: var(--as-text); }

/* etichetta del menu che ricompare in basso dopo ogni risposta */
.as-cont-label {
  margin: 6px 0 -4px 35px;
  font-size: .8rem;
  font-weight: 600;
  color: var(--as-text-dim);
  letter-spacing: .01em;
}

/* leaf buttons: icona accent ciano */
.as-opt-leaf .as-opt-ico { background: rgba(47, 127, 224, .1); }

/* ── Footer teaser (paid) ─────────────────────────────────── */
.as-foot { flex: 0 0 auto; padding: 10px 16px 14px; border-top: 1px solid var(--as-bubble-border); }
.as-teaser {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 10px 12px;
  font-family: var(--font-ui);
  font-size: .86rem;
  color: var(--as-text);
  background: linear-gradient(135deg, rgba(85, 102, 240, .12), rgba(47, 127, 224, .08));
  border: 1px solid rgba(85, 102, 240, .3);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: border-color var(--ease), background var(--ease);
}
.as-teaser:hover { border-color: var(--as-accent); background: linear-gradient(135deg, rgba(85, 102, 240, .2), rgba(47, 127, 224, .14)); }
.as-teaser-star { display: inline-flex; color: var(--as-accent); }
.as-teaser-star svg { width: 15px; height: 15px; }
.as-teaser-soon {
  margin-left: auto;
  font-size: .72rem;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--as-accent-2);
  background: rgba(47, 127, 224, .12);
  padding: 2px 7px;
  border-radius: 999px;
}

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

/* ── Strip icon (dedicata, sperimentale) ──────────────────── */
.rsb-icon-as.active svg .as-spark,
.rsb-icon-as:hover svg .as-spark { fill: var(--as-accent-2); }

/* ── FAB fluttuante: riapre l'assistant da qualsiasi punto ── */
.as-fab {
  position: fixed;
  bottom: 22px;
  right: calc(var(--rsb-strip-w, 48px) + 16px);
  width: 50px;
  height: 50px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  z-index: 90;
  background: var(--as-grad);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 6px 20px rgba(85, 102, 240, .4), 0 0 0 1px rgba(255, 255, 255, .1) inset;
  transition: transform .18s var(--ease), opacity .18s var(--ease), box-shadow .18s var(--ease);
}
.as-fab svg { width: 22px; height: 22px; fill: #fff; stroke: #fff; position: relative; z-index: 1; }
.as-fab:hover { transform: translateY(-2px) scale(1.05); box-shadow: 0 10px 26px rgba(85, 102, 240, .5); }
.as-fab:active { transform: scale(.96); }
.as-fab-hidden { opacity: 0; pointer-events: none; transform: scale(.6); }
.as-fab-glow {
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  background: var(--as-grad);
  opacity: .55;
  z-index: 0;
  animation: as-fab-pulse 2.6s ease-out infinite;
}
@keyframes as-fab-pulse {
  0% { transform: scale(.85); opacity: .5; }
  70% { transform: scale(1.5); opacity: 0; }
  100% { transform: scale(1.5); opacity: 0; }
}

/* ── Reduce motion ────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  .as-row, .as-opt, .as-dots span, .as-caret, .as-fab-glow { animation: none !important; }
  .as-opt { transition: background var(--ease), border-color var(--ease); }
  .as-stream { scroll-behavior: auto; }
}
