/* TurboBot widget v1 — vanilla CSS, zero deps, zero build step */

:root {
  --tb-primary: #f59e0b;
  --tb-primary-hover: #d97706;
  --tb-bg: #ffffff;
  --tb-fg: #1f2937;
  --tb-muted: #6b7280;
  --tb-border: #e5e7eb;
  --tb-bubble-user: #fef3c7;
  --tb-bubble-bot: #f3f4f6;
  --tb-shadow-fab: 0 4px 12px rgba(245,158,11,.4), 0 2px 4px rgba(0,0,0,.08);
  --tb-shadow-drawer: 0 12px 48px rgba(0,0,0,.18), 0 4px 12px rgba(0,0,0,.06);
  --tb-radius: 16px;
  --tb-radius-bubble: 14px;
}
[data-theme="dark"] {
  --tb-bg: #1f2937;
  --tb-fg: #f3f4f6;
  --tb-muted: #9ca3af;
  --tb-border: #374151;
  --tb-bubble-user: #92400e;
  --tb-bubble-bot: #374151;
}

/* === FAB ============================================== */
/* Wrapper: positioning anchor + badge container (NON applica clip iOS) */
.tb-fab {
  position: fixed; bottom: 24px; right: 24px;
  width: 56px; height: 56px;
  z-index: 9990;
  /* No background, no border-radius here — quelle vivono sul __btn interno */
  /* TF-TURBOBOT-FAB-AVOID-2026-05-21 v2: il translateY del lift è applicato
     inline da JS (valore dinamico); qui solo l'easing della transizione */
  transition: transform .3s cubic-bezier(.34,1.4,.5,1);
  will-change: transform;
}
.tb-fab[data-stacked="true"] { bottom: 96px; }
@media (prefers-reduced-motion: reduce) { .tb-fab { transition: none; } }

/* Inner button: il vero "FAB" che vedi (cerchio amber con icona) */
.tb-fab__btn {
  width: 100%; height: 100%; border-radius: 50%;
  background: var(--tb-primary); border: none; cursor: pointer;
  box-shadow: var(--tb-shadow-fab);
  display: flex; align-items: center; justify-content: center;
  transition: transform .15s ease, background .15s ease;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  -webkit-appearance: none;
  appearance: none;
}
.tb-fab__btn:hover  { background: var(--tb-primary-hover); transform: scale(1.05); }
.tb-fab__btn:active { transform: scale(.97); }
.tb-fab__btn:focus-visible { outline: 3px solid var(--tb-primary); outline-offset: 3px; }
.tb-fab__btn svg { width: 26px; height: 26px; color: #fff; }

@media (max-width: 767px) {
  .tb-fab { bottom: 86px; right: 16px; width: 52px; height: 52px; }
  .tb-fab[data-stacked="true"] { bottom: 154px; }
}

/* === FAB vs toast: separazione STATICA (TF-TURBOBOT-FAB-AVOID v3 2026-05-22) ===
   I toast standard dell'app (#toast, #fattura-toast) sono fixed bottom-right,
   stessa zona del FAB del chatbot. Li alziamo SOPRA il FAB (stack verticale)
   così non si sovrappongono mai — il FAB resta SEMPRE fermo. Niente JS/polling.
   Il selettore class-combo colpisce solo i toast bottom-right, mai i bottom-center
   (privi di .right-6) né il FAB (.tb-fab, senza queste classi Tailwind). */
.fixed.bottom-6.right-6 { bottom: 96px; }
@media (max-width: 767px) {
  .fixed.bottom-6.right-6 { bottom: 150px; }
}

/* Pulse ring — sul wrapper, dietro al button, perfettamente concentrico */
.tb-fab[data-pulse="true"]::after {
  content: ''; position: absolute; inset: 0;
  border-radius: 50%; border: 2px solid var(--tb-primary);
  pointer-events: none;
  animation: tb-pulse 2.4s ease-out infinite;
}
@keyframes tb-pulse {
  0% { transform: scale(1); opacity: .7; }
  100% { transform: scale(1.6); opacity: 0; }
}

/* Badge "Nuovo" — sibling del button, position relativa al wrapper.
   Non più clipped da iOS overflow rendering del <button>. */
.tb-fab-badge {
  position: absolute;
  top: -6px; right: -8px;
  background: #ef4444; color: #fff;
  font: 600 10px/1 -apple-system, system-ui, sans-serif;
  padding: 3px 7px; border-radius: 10px;
  border: 2px solid var(--tb-bg);
  pointer-events: none;
  z-index: 1;
  white-space: nowrap;
  box-shadow: 0 2px 6px rgba(239, 68, 68, .3);
}
@media (max-width: 767px) {
  .tb-fab-badge { top: -5px; right: -6px; font-size: 9.5px; padding: 2.5px 6px; }
}

/* === DRAWER =========================================== */
.tb-drawer {
  position: fixed; bottom: 92px; right: 24px;
  width: 380px; height: 620px; max-height: calc(100vh - 120px);
  background: var(--tb-bg); border-radius: var(--tb-radius);
  box-shadow: var(--tb-shadow-drawer);
  display: flex; flex-direction: column; z-index: 9995; overflow: hidden;
  transform: translateY(20px) scale(.95); opacity: 0; pointer-events: none;
  transition: transform .25s cubic-bezier(.16,1,.3,1), opacity .2s ease;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  color: var(--tb-fg);
}
.tb-drawer[data-open="true"] {
  transform: translateY(0) scale(1); opacity: 1; pointer-events: auto;
}

@media (max-width: 767px) {
  .tb-drawer {
    inset: 0; width: 100%; height: 100dvh; max-height: 100dvh;
    border-radius: 0; transform: translateY(100%);
  }
  .tb-drawer[data-open="true"] { transform: translateY(0); }
}

/* Header */
.tb-header {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 16px; border-bottom: 1px solid var(--tb-border);
  background: linear-gradient(180deg, var(--tb-bg) 0%, color-mix(in srgb, var(--tb-bg) 96%, var(--tb-primary)) 100%);
}
.tb-header__avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--tb-primary); flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
.tb-header__avatar svg { width: 20px; height: 20px; color: #fff; }
.tb-header__title { display: flex; flex-direction: column; flex-grow: 1; min-width: 0; }
.tb-header__name { font-size: 15px; font-weight: 600; line-height: 1; letter-spacing: -0.1px; }
.tb-header__status {
  font-size: 10.5px; color: var(--tb-muted);
  margin-top: 3px; letter-spacing: 0.3px;
  font-weight: 400;
  display: inline-flex; align-items: center; gap: 6px;
  text-transform: lowercase;
}
.tb-status-live {
  width: 14px; height: 14px;
  flex-shrink: 0;
  display: inline-block;
}
.tb-header__close {
  background: transparent; border: none; cursor: pointer;
  width: 32px; height: 32px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  color: var(--tb-muted);
}
.tb-header__close:hover { background: var(--tb-border); color: var(--tb-fg); }
.tb-header__close svg { width: 18px; height: 18px; }

/* Body */
.tb-body {
  flex-grow: 1; overflow-y: auto; overflow-x: hidden;
  padding: 16px; display: flex; flex-direction: column; gap: 10px;
  scroll-behavior: smooth;
}
.tb-body::-webkit-scrollbar { width: 6px; }
.tb-body::-webkit-scrollbar-thumb { background: var(--tb-border); border-radius: 3px; }

/* Empty state */
.tb-empty {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; height: 100%; padding: 24px; gap: 14px;
}
.tb-empty__icon { width: 56px; height: 56px; color: var(--tb-primary); }
.tb-empty__title { font-size: 17px; font-weight: 600; }
.tb-empty__subtitle { font-size: 13px; color: var(--tb-muted); margin: -10px 0 8px; }
.tb-chips { display: flex; flex-direction: column; gap: 8px; width: 100%; }
.tb-chip {
  background: var(--tb-bubble-bot); border: 1px solid var(--tb-border);
  border-radius: 12px; padding: 10px 14px; font-size: 13px;
  color: var(--tb-fg); cursor: pointer; text-align: left;
  font-family: inherit;
  transition: background .15s ease, border-color .15s ease;
}
.tb-chip:hover {
  background: color-mix(in srgb, var(--tb-bubble-bot) 70%, var(--tb-primary) 12%);
  border-color: var(--tb-primary);
}

/* Bubbles */
.tb-msg {
  display: flex; max-width: 85%;
  animation: tb-msg-in .25s ease-out;
}
.tb-msg[data-role="user"] { align-self: flex-end; flex-direction: row-reverse; }
.tb-msg[data-role="assistant"] { align-self: flex-start; }
.tb-msg__bubble {
  padding: 10px 14px; border-radius: var(--tb-radius-bubble);
  font-size: 14px; line-height: 1.5; word-wrap: break-word; overflow-wrap: anywhere;
}
.tb-msg[data-role="user"] .tb-msg__bubble {
  background: var(--tb-bubble-user); border-bottom-right-radius: 4px;
}
.tb-msg[data-role="assistant"] .tb-msg__bubble {
  background: var(--tb-bubble-bot); border-bottom-left-radius: 4px;
}
.tb-msg__bubble p { margin: 0 0 8px; }
.tb-msg__bubble p:last-child { margin-bottom: 0; }
.tb-msg__bubble strong { font-weight: 600; }
.tb-msg__bubble code.tb-code {
  background: rgba(0,0,0,.06); padding: 1px 5px; border-radius: 4px;
  font-family: ui-monospace, monospace; font-size: 12.5px;
}
.tb-msg__bubble pre.tb-pre {
  background: rgba(0,0,0,.06); padding: 10px 12px; border-radius: 8px;
  overflow-x: auto; margin: 6px 0; font-size: 12.5px;
}
.tb-msg__bubble pre.tb-pre code { background: none; padding: 0; }
.tb-msg__bubble a { color: var(--tb-primary-hover); text-decoration: underline; }
.tb-msg__bubble ul, .tb-msg__bubble ol { margin: 6px 0; padding-left: 22px; }
.tb-msg__bubble li { margin: 2px 0; }
.tb-msg__bubble h2 { font-size: 15px; margin: 8px 0 4px; font-weight: 600; }
.tb-msg__bubble h3 { font-size: 14.5px; margin: 8px 0 4px; font-weight: 600; }
.tb-msg__bubble h4 { font-size: 14px; margin: 8px 0 4px; font-weight: 600; }

/* Typing */
.tb-typing {
  display: flex; gap: 4px; align-items: center;
  padding: 12px 14px; align-self: flex-start;
  background: var(--tb-bubble-bot);
  border-radius: var(--tb-radius-bubble); border-bottom-left-radius: 4px;
}
.tb-typing span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--tb-muted);
  animation: tb-typing 1.2s ease-in-out infinite;
}
.tb-typing span:nth-child(2) { animation-delay: .2s; }
.tb-typing span:nth-child(3) { animation-delay: .4s; }
@keyframes tb-typing {
  0%, 60%, 100% { opacity: .3; transform: scale(.85); }
  30% { opacity: 1; transform: scale(1); }
}

/* Tool indicator pill */
.tb-tool-pill {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 8px 12px; align-self: flex-start;
  background: color-mix(in srgb, var(--tb-primary) 12%, var(--tb-bg));
  border: 1px solid color-mix(in srgb, var(--tb-primary) 30%, var(--tb-border));
  border-radius: var(--tb-radius-bubble);
  border-bottom-left-radius: 4px;
  font-size: 12.5px; color: var(--tb-fg);
  animation: tb-msg-in .2s ease-out;
}
.tb-tool-pill__icon {
  display: inline-flex; width: 14px; height: 14px;
  color: var(--tb-primary-hover);
  animation: tb-tool-spin 1.6s linear infinite;
}
.tb-tool-pill__icon svg { width: 100%; height: 100%; }
.tb-tool-pill__label { font-weight: 500; }
@keyframes tb-tool-spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: reduce) {
  .tb-tool-pill__icon { animation: none; }
}

/* Error */
.tb-error {
  background: #fee2e2; color: #991b1b; align-self: stretch;
  padding: 10px 14px; border-radius: 10px; font-size: 13px;
  border: 1px solid #fecaca;
}
[data-theme="dark"] .tb-error {
  background: #7f1d1d; color: #fecaca; border-color: #b91c1c;
}

/* Footer */
.tb-footer {
  border-top: 1px solid var(--tb-border);
  padding: 10px 12px 12px; background: var(--tb-bg);
}
.tb-input-row {
  display: flex; align-items: flex-end; gap: 8px;
  background: color-mix(in srgb, var(--tb-bg) 96%, var(--tb-fg) 4%);
  border: 1px solid var(--tb-border); border-radius: 14px;
  padding: 6px 6px 6px 12px;
  transition: border-color .15s ease;
}
.tb-input-row:focus-within { border-color: var(--tb-primary); }
.tb-input {
  flex-grow: 1; border: none; background: transparent;
  font: 14px/1.4 inherit; resize: none; outline: none;
  color: var(--tb-fg); max-height: 120px; padding: 7px 0;
  /* iOS: kill blue tap highlight + native styling */
  -webkit-tap-highlight-color: transparent;
  -webkit-appearance: none;
  appearance: none;
  border-radius: 0;
}
.tb-input:focus,
.tb-input:focus-visible,
.tb-input:active {
  outline: none !important;
  box-shadow: none !important;
  -webkit-box-shadow: none !important;
  -webkit-tap-highlight-color: transparent;
}
.tb-input::placeholder { color: var(--tb-muted); }
.tb-send {
  background: var(--tb-primary); color: #fff; border: none;
  width: 34px; height: 34px; border-radius: 50%; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: background .15s ease, transform .1s ease;
}
.tb-send:hover { background: var(--tb-primary-hover); }
.tb-send:active { transform: scale(.95); }
.tb-send:disabled { background: var(--tb-muted); cursor: not-allowed; opacity: .5; }
.tb-send svg { width: 16px; height: 16px; }

.tb-meta {
  display: flex; justify-content: space-between; align-items: center;
  margin-top: 8px; padding: 0 6px;
  font-size: 10px; line-height: 1.3;
  color: var(--tb-muted);
  letter-spacing: 0.2px;
  font-feature-settings: 'tnum';
}
.tb-meta__hint {
  opacity: 0.55;
  font-size: 9.5px;
  font-style: italic;
  letter-spacing: 0.3px;
}
.tb-meta__remaining {
  font-weight: 500;
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .tb-fab[data-pulse="true"]::after { animation: none; }
  .tb-drawer { transition: opacity .15s ease; transform: none; }
  .tb-msg { animation: none; }
  .tb-typing span { animation: none; opacity: .7; }
  /* Live SVG: hide animated outer rings, keep only solid center dot */
  .tb-status-live circle:not(:last-of-type) { display: none; }
}

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