/* Naty — assistente in-app. Usa solo i token di tokens.css (light + dark). */

.ella-root {
  --ella-gap: 16px;
  --ella-bottom: calc(104px + var(--safe-b, env(safe-area-inset-bottom, 0px)));
  position: fixed;
  right: calc(var(--ella-gap) + env(safe-area-inset-right, 0px));
  bottom: var(--ella-bottom);
  z-index: 1250; /* sopra barra e banner di installazione, sotto i dialog modali */
  font-family: var(--font-family);
  pointer-events: none;
}
.ella-root[hidden] { display: none; }
.ella-root > * { pointer-events: auto; }

.ella-sr {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap;
}

/* ── Avatar ── */
.ella-avatar { display: block; border-radius: 50%; object-fit: cover; user-select: none; -webkit-user-drag: none; }

/* Avatar: posa di riposo sempre piena sotto, animazioni (WebP animate) sopra. */
.naty-face { position: relative; overflow: hidden; }
.naty-still, .naty-clip {
  position: absolute; inset: 0; width: 100%; height: 100%;
  display: block; border-radius: 50%; object-fit: cover;
  user-select: none; -webkit-user-drag: none;
}
.naty-clip { opacity: 0; }

/* ── Pulsante ── */
.ella-launcher {
  position: relative;
  display: grid; place-items: center;
  width: 58px; height: 58px; padding: 0;
  border: 0; border-radius: 50%;
  background: transparent;
  box-shadow: var(--shadow-cta);
  cursor: pointer;
  transition: transform .18s ease, box-shadow .18s ease;
  -webkit-tap-highlight-color: transparent;
}
.ella-launcher .ella-avatar { width: 58px; height: 58px; }
.ella-launcher:hover { transform: translateY(-2px); }
.ella-launcher:active { transform: scale(.96); }
.ella-launcher:focus-visible { outline: 3px solid var(--primary-mid); outline-offset: 3px; }
.ella-root.is-open .ella-launcher { transform: scale(.92); opacity: .0; pointer-events: none; }

.ella-badge {
  position: absolute; top: -3px; right: -3px;
  min-width: 20px; height: 20px; padding: 0 5px;
  border-radius: var(--radius-pill);
  border: 2px solid var(--bg);
  background: var(--red); color: #fff;
  font-size: 11px; font-weight: 800; line-height: 16px; text-align: center;
}
.ella-badge[hidden] { display: none; }

/* ── Anteprima messaggio proattivo ── */
.ella-peek {
  position: absolute; right: 0; bottom: 70px;
  display: flex; align-items: flex-start; gap: 4px;
  width: min(300px, calc(100vw - 2 * var(--ella-gap)));
  padding: 4px 4px 4px 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg) var(--radius-lg) 6px var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  opacity: 0; transform: translateY(14px) scale(.55); transform-origin: calc(100% - 26px) 100%;
  transition: opacity .2s ease, transform .5s cubic-bezier(.34, 1.56, .64, 1);
}
.ella-peek[hidden] { display: none; }
.ella-peek.is-in { opacity: 1; transform: none; }
.ella-peek .ella-peek-text { opacity: 0; transform: translateY(4px); transition: opacity .25s ease .18s, transform .25s ease .18s; }
.ella-peek.is-in .ella-peek-text { opacity: 1; transform: none; }
.ella-peek-body {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column; gap: 2px;
  padding: 10px 4px 10px 14px;
  border: 0; background: none; text-align: left; cursor: pointer;
  color: var(--text); font: inherit;
}
.ella-peek-name { font-size: var(--text-xs); font-weight: 800; color: var(--primary); }
.ella-peek-text { font-size: var(--text-sm); line-height: 1.4; color: var(--text); }

.ella-icon-btn, .ella-peek-close {
  flex: 0 0 auto;
  display: grid; place-items: center;
  width: 36px; height: 36px; padding: 0;
  border: 0; border-radius: 50%;
  background: transparent; color: var(--hint);
  cursor: pointer;
}
.ella-icon-btn:hover, .ella-peek-close:hover { background: var(--surface2); color: var(--text); }
.ella-icon-btn:focus-visible, .ella-peek-close:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
.ella-icon-btn svg, .ella-peek-close svg {
  width: 20px; height: 20px;
  fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round;
}
.ella-mute[aria-pressed="true"] { color: var(--orange); }

/* ── Pannello ── */
.ella-scrim {
  position: fixed; inset: 0; z-index: 0;
  background: var(--scrim);
}
.ella-scrim[hidden] { display: none; }

/* Telefono: pannello che sale dal basso, alto quasi tutto lo schermo (sotto la barra di
   stato), angoli arrotondati e maniglia come gli altri pannelli dell'app; si chiude anche
   trascinandolo giù. L'altezza segue l'area visibile, tastiera inclusa. */
.ella-panel {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1;
  top: calc(env(safe-area-inset-top, 0px) + 8px);
  display: flex; flex-direction: column;
  border-radius: 26px 26px 0 0;
  background: var(--bg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  transform: translateY(100%);
  transition: transform 320ms cubic-bezier(.05, .7, .1, 1);
  will-change: transform;
}
.ella-panel.is-closing { transition: transform 240ms cubic-bezier(.3, 0, .8, .15); }
.ella-panel.is-dragging { transition: none; }
.ella-grab {
  position: absolute; top: 9px; left: 50%; z-index: 3;
  width: 48px; height: 4px; border-radius: 999px;
  background: #dedfe9; transform: translateX(-50%);
  pointer-events: none;
}
.ella-panel .ella-head { padding-top: 26px; touch-action: none; cursor: grab; }
.ella-panel.is-compact .ella-head { padding-top: 22px; }
.ella-panel.is-dragging .ella-head { cursor: grabbing; }
.ella-scrim { transition: opacity 240ms ease; }
.ella-panel[hidden] { display: none; }
.ella-panel.is-in { transform: none; }

.ella-head {
  display: flex; align-items: center; gap: 14px;
  padding: 14px 10px 14px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  transition: padding .25s ease;
}
/* Avatar grande: l'animazione di Naty deve vedersi bene appena si apre la chat. */
.ella-head-avatar { flex: 0 0 auto; display: block; }
.ella-head-avatar .ella-avatar {
  width: 96px; height: 96px; border-radius: 50%;
  box-shadow: 0 6px 20px rgba(102, 84, 241, 0.22);
  transition: width .25s ease, height .25s ease;
}
.ella-head-text { flex: 1; min-width: 0; }
.ella-head-text h2 {
  margin: 0; font-size: var(--text-xl); font-weight: 800; letter-spacing: var(--tracking-tight);
  color: var(--text); transition: font-size .25s ease;
}
.ella-head-text p { margin: 2px 0 0; font-size: var(--text-sm); color: var(--hint); }
/* Tastiera aperta su schermi bassi: intestazione compatta per lasciare spazio ai messaggi. */
.ella-panel.is-compact .ella-head { padding-top: 10px; padding-bottom: 10px; }
.ella-panel.is-compact .ella-head-avatar .ella-avatar { width: 44px; height: 44px; }
.ella-panel.is-compact .ella-head-text h2 { font-size: var(--text-lg); }

.ella-list {
  flex: 1; min-height: 0;
  overflow-y: auto; overscroll-behavior: contain;
  padding: 16px 14px 8px;
  display: flex; flex-direction: column; gap: 10px;
}

.ella-msg { display: flex; flex-direction: column; max-width: 86%; }
.ella-msg.is-ella { align-self: flex-start; align-items: flex-start; }
.ella-msg.is-user { align-self: flex-end; align-items: flex-end; }

.ella-bubble {
  padding: 10px 14px;
  border-radius: 18px;
  font-size: var(--text-md); line-height: 1.45;
  overflow-wrap: anywhere;
}
.ella-msg.is-ella .ella-bubble {
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
  background: var(--surface); color: var(--text);
}
.ella-msg.is-user .ella-bubble {
  border-bottom-right-radius: 6px;
  background: var(--primary); color: var(--on-primary);
}
.ella-bubble strong { font-weight: 800; }

.ella-rate { display: flex; gap: 2px; margin-top: 4px; }
.ella-rate-btn {
  width: 32px; height: 28px; padding: 0;
  border: 0; border-radius: var(--radius-pill);
  background: transparent; font-size: 14px; line-height: 1;
  cursor: pointer; filter: grayscale(1); opacity: .55;
  transition: opacity .15s ease, filter .15s ease, background .15s ease;
}
.ella-rate-btn:hover { opacity: 1; background: var(--surface2); }
.ella-rate-btn.is-on { filter: none; opacity: 1; background: var(--primary-soft); }
.ella-rate-btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }

.ella-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.ella-chip {
  min-height: 36px; padding: 7px 14px;
  border: 1px solid var(--primary-mid);
  border-radius: var(--radius-pill);
  background: var(--surface); color: var(--primary);
  font: inherit; font-size: var(--text-sm); font-weight: 700;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease, transform .1s ease;
}
.ella-chip:hover { background: var(--primary-soft); }
.ella-chip:active { transform: scale(.97); }
.ella-chip:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.ella-chip:disabled { opacity: .5; cursor: default; }
.ella-chip.is-team { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.ella-chip.is-team:hover { background: var(--primary-dark); }
.ella-chip.is-link::after { content: " →"; }

.ella-typing .ella-bubble { display: inline-flex; gap: 4px; padding: 14px 16px; }
.ella-typing .ella-bubble span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--hint); opacity: .4;
  animation: ella-dot 1.1s infinite ease-in-out;
}
.ella-typing .ella-bubble span:nth-child(2) { animation-delay: .15s; }
.ella-typing .ella-bubble span:nth-child(3) { animation-delay: .3s; }
@keyframes ella-dot { 0%, 80%, 100% { opacity: .3; transform: none; } 40% { opacity: 1; transform: translateY(-3px); } }

.ella-empty { display: flex; flex-direction: column; align-items: center; text-align: center; padding: 18px 8px; gap: 10px; }
.ella-empty-avatar { display: none; }
.ella-empty p { margin: 0; color: var(--text-2); font-size: var(--text-md); line-height: 1.5; max-width: 300px; }
.ella-empty strong { color: var(--text); }
.ella-empty .ella-actions { justify-content: center; }

.ella-form {
  display: flex; align-items: flex-end; gap: 8px;
  margin: 0; padding: 10px 12px 4px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.ella-form textarea {
  flex: 1; min-height: 44px; max-height: 120px; resize: none;
  overflow-y: hidden; scrollbar-width: none; -ms-overflow-style: none;
  padding: 11px 14px;
  border: 1px solid var(--border-strong);
  border-radius: 22px;
  background: var(--surface2); color: var(--text);
  font: inherit; font-size: 16px; line-height: 1.35;
}
.ella-form textarea::placeholder { color: var(--hint); }
.ella-form textarea:focus,
.ella-form textarea:focus-visible { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.ella-send {
  flex: 0 0 auto;
  display: grid; place-items: center;
  width: 44px; height: 44px; padding: 0;
  border: 0; border-radius: 50%;
  background: var(--gradient-cta); color: var(--on-primary);
  cursor: pointer;
}
.ella-send:disabled { background: var(--surface3); color: var(--hint); cursor: default; }
.ella-send:focus-visible { outline: 3px solid var(--primary-mid); outline-offset: 2px; }
.ella-send svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

.ella-foot {
  margin: 0; padding: 4px 16px calc(10px + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  color: var(--hint); font-size: 11px; text-align: center;
}

/* ── Desktop: scheda flottante, niente scrim ── */
@media (min-width: 1024px) {
  .ella-root { --ella-bottom: 28px; --ella-gap: 28px; }
  .ella-scrim { display: none !important; }
  .ella-head-avatar .ella-avatar { width: 88px; height: 88px; }
  .ella-grab { display: none; }
  .ella-panel .ella-head, .ella-panel.is-compact .ella-head { padding-top: 14px; touch-action: auto; cursor: default; }
  .ella-panel {
    top: auto;
    opacity: 0; transform: translateY(24px);
    transition: opacity .22s ease, transform .26s cubic-bezier(.2, .8, .2, 1);
    left: auto; right: 28px; bottom: 28px;
    width: 390px; height: min(640px, calc(100dvh - 56px));
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
  }
  .ella-panel.is-in { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .ella-launcher, .ella-peek, .ella-panel, .ella-chip { transition: none; }
  .ella-typing .ella-bubble span { animation: none; opacity: .6; }
}

/* Card del Profilo evidenziata quando Naty ci porta l'utente. */
.ella-highlight {
  animation: ella-highlight 2.2s ease;
  border-radius: var(--radius-lg);
}
@keyframes ella-highlight {
  0%, 60% { box-shadow: 0 0 0 3px var(--primary), 0 0 0 8px var(--primary-soft); }
  100% { box-shadow: 0 0 0 0 transparent; }
}

/* Nessuna barra di scorrimento nel campo di testo (si scorre comunque col dito o la rotellina). */
.ella-form textarea::-webkit-scrollbar { display: none; width: 0; height: 0; }

/* Il focus globale dell'app aggiunge un outline: qui basta l'anello morbido. */
#ella-root .ella-form textarea:focus,
#ella-root .ella-form textarea:focus-visible { outline: none !important; }

/* ── Animazioni della bolla ── */
/* Ingresso: compare dal basso con un rimbalzo, poi l'avatar saluta. */
.ella-launcher.is-entering { animation: naty-pop .6s cubic-bezier(.34, 1.56, .64, 1) both; }
@keyframes naty-pop {
  0%   { opacity: 0; transform: translateY(24px) scale(.3); }
  60%  { opacity: 1; transform: translateY(-4px) scale(1.08); }
  100% { opacity: 1; transform: none; }
}
/* Naty ha qualcosa da dirti: un anello che pulsa intorno alla bolla. */
.ella-launcher::before {
  content: ""; position: absolute; inset: -3px; border-radius: 50%;
  border: 2px solid var(--primary); opacity: 0; pointer-events: none;
}
.ella-launcher.is-calling::before { animation: naty-ring 1.5s ease-out 3; }
@keyframes naty-ring {
  0%   { opacity: .7; transform: scale(1); }
  100% { opacity: 0; transform: scale(1.55); }
}

/* Scheda prodotto: la bolla resta sopra il pulsante Salva. */
.ella-root.is-product { --ella-bottom: calc(108px + var(--safe-b, env(safe-area-inset-bottom, 0px))); }

@media (prefers-reduced-motion: reduce) {
  .ella-launcher.is-entering,
  .ella-launcher.is-calling::before { animation: none; }
  .ella-peek, .ella-peek .ella-peek-text { transition: opacity .2s ease; transform: none; }
}

/* Saluto all'apparizione: «Ciao! 👋» corto, che sparisce da solo. */
.ella-peek.is-greeting { width: auto; }
.ella-peek.is-greeting .ella-peek-text { font-size: var(--text-md); font-weight: 700; white-space: nowrap; }

/* Emoji Resella: sticker dettagliati, quindi più grandi di un'emoji normale. */
.naty-emoji {
  display: inline-block; width: 2.15em; height: 2.15em;
  margin: -0.3em .08em; vertical-align: middle;
  object-fit: contain; user-select: none; -webkit-user-drag: none;
}
.ella-chip .naty-emoji { width: 1.7em; height: 1.7em; margin: -0.35em .25em -0.35em -0.2em; }
.ella-peek .naty-emoji { width: 2em; height: 2em; }
/* Messaggio fatto solo di emoji: sticker grande, senza fumetto. */
.ella-bubble.is-emoji-only { background: transparent !important; border-color: transparent !important; padding: 0; }
.ella-bubble.is-emoji-only .naty-emoji { width: 72px; height: 72px; margin: 0 2px; }

:root[data-theme="dark"] .ella-grab { background: var(--border-strong); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .ella-grab { background: var(--border-strong); } }
