/* =========================================================
   Chatbot belfarsi
   Cambia i colori qui sotto per adattarli al portfolio
   ========================================================= */
:root {
  --bf-primary: #5138ee;
  --bf-primary-2: #7b61ff;
  --bf-bg: #ffffff;
  --bf-surface: #f4f5fa;
  --bf-text: #1c1f2e;
  --bf-muted: #6b7085;
  --bf-border: #e7e8f0;
  --bf-online: #22c55e;
  --bf-radius: 22px;
  --bf-shadow: 0 24px 60px -12px rgba(28, 31, 46, .30), 0 8px 20px -8px rgba(28, 31, 46, .18);
}

.bf-root, .bf-root * { box-sizing: border-box; }
.bf-root { font-family: inherit; color: var(--bf-text); }
.bf-root button, .bf-root input, .bf-root textarea { font: inherit; }
.bf-root svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Foto (avatar) ---------- */
.bf-avatar {
  position: relative;
  display: inline-grid;
  place-items: center;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--bf-primary), var(--bf-primary-2));
  color: #fff;
  font-weight: 700;
  flex-shrink: 0;
}
.bf-avatar img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}
.bf-avatar--noimg img { display: none; }
.bf-avatar-initial { font-size: .9em; }

.bf-dot {
  position: absolute;
  right: 2px;
  bottom: 2px;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: var(--bf-online);
  border: 2px solid #fff;
}

/* ---------- Pulsante con la foto ---------- */
.bf-launcher {
  position: fixed;
  right: 20px;
  bottom: 84px; /* sopra il pulsante ↑ del sito */
  z-index: 9998;
  width: 66px;
  height: 66px;
  padding: 3px;
  border: 0;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--bf-primary), var(--bf-primary-2));
  box-shadow: var(--bf-shadow);
  cursor: pointer;
  transition: transform .25s ease, opacity .25s ease;
}
.bf-launcher::before {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  border: 2px solid var(--bf-primary);
  opacity: 0;
  animation: bfPulse 2.6s ease-out infinite;
}
.bf-launcher:hover { transform: translateY(-3px) scale(1.06); }
.bf-launcher { display: grid; place-items: center; padding: 0; }
.bf-launcher > .bf-dot { right: 2px; bottom: 2px; width: 15px; height: 15px; }

/* Fumetto bianco con i tre puntini animati */
.bf-bubble-icon {
  position: relative;
  width: 34px;
  height: 25px;
  display: grid;
  place-items: center;
  border-radius: 9px;
  background: #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, .15);
  animation: bfWiggle 5s ease-in-out infinite;
}
.bf-bubble-icon::after { /* codina del fumetto */
  content: "";
  position: absolute;
  left: 6px;
  bottom: -5px;
  border-left: 6px solid #fff;
  border-bottom: 6px solid transparent;
}
.bf-bdots { display: flex; gap: 4px; }
.bf-bdots i {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--bf-primary);
  animation: bfDots 1.2s infinite ease-in-out;
}
.bf-bdots i:nth-child(2) { animation-delay: .15s; }
.bf-bdots i:nth-child(3) { animation-delay: .3s; }

.bf-hint {
  position: absolute;
  right: 80px;
  bottom: 14px;
  white-space: nowrap;
  padding: 10px 14px;
  border-radius: 16px 16px 4px 16px;
  background: var(--bf-bg);
  color: var(--bf-text);
  font-size: 14px;
  font-weight: 500;
  box-shadow: var(--bf-shadow);
  opacity: 0;
  transform: translateY(6px);
  transition: opacity .3s ease, transform .3s ease;
  pointer-events: none;
}
.bf-launcher.show-hint .bf-hint { opacity: 1; transform: none; }

.bf-root.is-open .bf-launcher { opacity: 0; transform: scale(.6); pointer-events: none; }

/* ---------- Sfondo scuro quando è ingrandita ---------- */
.bf-backdrop {
  position: fixed;
  inset: 0;
  z-index: 9997;
  background: rgba(15, 17, 30, .45);
  backdrop-filter: blur(3px);
  opacity: 0;
  visibility: hidden;
  transition: opacity .3s ease, visibility 0s .3s;
}
.bf-root.is-open.is-expanded .bf-backdrop { opacity: 1; visibility: visible; transition: opacity .3s ease; }

/* ---------- Finestra ---------- */
.bf-chat {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 9999;
  width: 390px;
  height: min(640px, calc(100vh - 48px));
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bf-bg);
  border-radius: var(--bf-radius);
  box-shadow: var(--bf-shadow);
  opacity: 0;
  visibility: hidden;
  transform: translateY(18px) scale(.97);
  transform-origin: bottom right;
  transition: opacity .25s ease, transform .25s ease, visibility 0s .25s,
              width .35s cubic-bezier(.2, .8, .2, 1), height .35s cubic-bezier(.2, .8, .2, 1);
}
.bf-root.is-open .bf-chat {
  opacity: 1;
  visibility: visible;
  transform: none;
  transition: opacity .25s ease, transform .25s ease,
              width .35s cubic-bezier(.2, .8, .2, 1), height .35s cubic-bezier(.2, .8, .2, 1);
}
.bf-root.is-expanded .bf-chat {
  width: min(1040px, calc(100vw - 48px));
  height: calc(100vh - 48px);
}

/* ---------- Intestazione ---------- */
.bf-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 14px 16px 18px;
  background: linear-gradient(135deg, var(--bf-primary), var(--bf-primary-2));
  color: #fff;
}
.bf-avatar--header { width: 46px; height: 46px; border: 2px solid rgba(255, 255, 255, .85); font-size: 18px; }
.bf-avatar--header .bf-dot { right: -1px; bottom: -1px; }
.bf-header-info { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.bf-name { font-size: 16.5px; font-weight: 700; letter-spacing: .2px; }
.bf-status { font-size: 12.5px; opacity: .85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bf-header-actions { display: flex; gap: 6px; }

.bf-icon-btn {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 11px;
  background: rgba(255, 255, 255, .14);
  color: #fff;
  cursor: pointer;
  transition: background .2s ease;
}
.bf-icon-btn:hover { background: rgba(255, 255, 255, .26); }
.bf-icon-btn:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
.bf-ico-shrink { display: none; }
.bf-root.is-expanded .bf-ico-expand { display: none; }
.bf-root.is-expanded .bf-ico-shrink { display: block; }

/* ---------- Corpo ---------- */
.bf-body { position: relative; flex: 1; display: flex; min-height: 0; background: var(--bf-surface); }
.bf-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.bf-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}
.bf-root.is-expanded .bf-messages { padding: 28px 8%; }

/* ---------- Messaggi ---------- */
.bf-msg { display: flex; align-items: flex-end; gap: 8px; animation: bfIn .3s ease; }
.bf-msg--user { flex-direction: row-reverse; }
.bf-msg .bf-avatar { width: 30px; height: 30px; font-size: 13px; }

.bf-bubble {
  max-width: 82%;
  padding: 11px 14px;
  border-radius: 18px;
  font-size: 14.5px;
  line-height: 1.55;
  overflow-wrap: anywhere;
}
.bf-root.is-expanded .bf-bubble { max-width: 68%; font-size: 15px; }
.bf-msg--bot .bf-bubble {
  background: var(--bf-bg);
  border: 1px solid var(--bf-border);
  border-bottom-left-radius: 6px;
  box-shadow: 0 1px 2px rgba(28, 31, 46, .05);
}
.bf-msg--user .bf-bubble {
  background: linear-gradient(135deg, var(--bf-primary), var(--bf-primary-2));
  color: #fff;
  border-bottom-right-radius: 6px;
}
.bf-bubble a { color: var(--bf-primary); font-weight: 600; text-decoration: none; }
.bf-bubble a:hover { text-decoration: underline; }
.bf-time { display: block; margin-top: 4px; font-size: 11px; opacity: .6; }
.bf-msg--user .bf-time { text-align: right; }

.bf-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.bf-action {
  display: inline-flex;
  align-items: center;
  padding: 7px 13px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--bf-primary), var(--bf-primary-2));
  color: #fff !important;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none !important;
}
.bf-action--ghost { background: transparent; color: var(--bf-primary) !important; border-color: var(--bf-primary); }

/* Puntini "sta scrivendo" */
.bf-typing { display: inline-flex; gap: 4px; padding: 4px 2px; }
.bf-typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--bf-muted); animation: bfDots 1.2s infinite ease-in-out; }
.bf-typing span:nth-child(2) { animation-delay: .15s; }
.bf-typing span:nth-child(3) { animation-delay: .3s; }

/* ---------- Suggerimenti rapidi ---------- */
.bf-chips { display: flex; gap: 8px; padding: 4px 16px 10px; overflow-x: auto; scrollbar-width: none; }
.bf-chips::-webkit-scrollbar { display: none; }
.bf-root.is-expanded .bf-chips { padding: 4px 8% 12px; flex-wrap: wrap; }
.bf-chip {
  flex: 0 0 auto;
  padding: 7px 14px;
  border: 1px solid var(--bf-border);
  border-radius: 999px;
  background: var(--bf-bg);
  color: var(--bf-primary);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background .2s ease, color .2s ease, border-color .2s ease;
}
.bf-chip:hover { background: var(--bf-primary); border-color: var(--bf-primary); color: #fff; }

/* ---------- Campo di scrittura ---------- */
.bf-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin: 0 12px 12px;
  padding: 6px 6px 6px 14px;
  background: var(--bf-bg);
  border: 1px solid var(--bf-border);
  border-radius: 18px;
  transition: border-color .2s ease, box-shadow .2s ease;
}
.bf-root.is-expanded .bf-form { margin: 0 8% 20px; }
.bf-form:focus-within { border-color: var(--bf-primary); box-shadow: 0 0 0 4px rgba(81, 56, 238, .12); }
.bf-form textarea {
  flex: 1;
  max-height: 130px;
  padding: 8px 0;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  color: var(--bf-text);
  font-size: 14.5px;
  line-height: 1.45;
}
.bf-send {
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 13px;
  background: linear-gradient(135deg, var(--bf-primary), var(--bf-primary-2));
  color: #fff;
  cursor: pointer;
  transition: opacity .2s ease, transform .2s ease;
}
.bf-send:disabled { opacity: .4; cursor: default; }
.bf-send:not(:disabled):hover { transform: scale(1.06); }

/* ---------- Pannello conversazioni ---------- */
.bf-history {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  background: var(--bf-bg);
  transform: translateX(-100%);
  visibility: hidden;
  transition: transform .3s ease, visibility 0s .3s;
}
.bf-history.is-visible { transform: none; visibility: visible; transition: transform .3s ease; }

.bf-history-head { display: flex; align-items: center; gap: 8px; padding: 14px 14px 10px; font-size: 15px; }
.bf-back-btn {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 10px;
  background: var(--bf-surface);
  color: var(--bf-text);
  cursor: pointer;
}

.bf-search { position: relative; margin: 0 14px 10px; }
.bf-search svg { position: absolute; top: 50%; inset-inline-start: 12px; transform: translateY(-50%); color: var(--bf-muted); }
.bf-search input {
  width: 100%;
  padding: 10px 12px;
  padding-inline-start: 38px;
  border: 1px solid var(--bf-border);
  border-radius: 12px;
  outline: 0;
  background: var(--bf-surface);
  color: var(--bf-text);
  font-size: 14px;
}
.bf-search input:focus { border-color: var(--bf-primary); background: var(--bf-bg); }

.bf-new-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin: 0 14px 8px;
  padding: 9px;
  border: 1px dashed var(--bf-primary);
  border-radius: 12px;
  background: transparent;
  color: var(--bf-primary);
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
}
.bf-new-btn:hover { background: rgba(81, 56, 238, .06); }

.bf-hlist { flex: 1; overflow-y: auto; margin: 0; padding: 4px 8px 8px; list-style: none; }
.bf-hitem {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 10px 10px;
  border-radius: 12px;
  cursor: pointer;
  transition: background .15s ease;
}
.bf-hitem:hover { background: var(--bf-surface); }
.bf-hitem.is-active { background: rgba(81, 56, 238, .09); }
.bf-hitem-body { flex: 1; min-width: 0; }
.bf-hitem-top { display: flex; justify-content: space-between; gap: 8px; }
.bf-hitem-title { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bf-hitem-date { flex-shrink: 0; font-size: 11px; color: var(--bf-muted); }
.bf-hitem-snippet {
  margin-top: 2px;
  font-size: 12.5px;
  color: var(--bf-muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.bf-hitem mark { background: rgba(81, 56, 238, .2); color: inherit; border-radius: 3px; padding: 0 2px; }
.bf-hdel {
  width: 26px;
  height: 26px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--bf-muted);
  cursor: pointer;
  opacity: 0;
  transition: opacity .15s ease, background .15s ease;
}
.bf-hdel svg { width: 15px; height: 15px; }
.bf-hitem:hover .bf-hdel, .bf-hdel:focus-visible { opacity: 1; }
.bf-hdel:hover { background: rgba(239, 68, 68, .1); color: #ef4444; }
@media (hover: none) { .bf-hdel { opacity: .7; } }

.bf-hempty { padding: 30px 16px; text-align: center; color: var(--bf-muted); font-size: 13.5px; }
.bf-hnote { margin: 0; padding: 10px 14px 12px; border-top: 1px solid var(--bf-border); font-size: 11.5px; color: var(--bf-muted); text-align: center; }

/* Finestra ingrandita su schermo grande: le conversazioni diventano una colonna laterale */
@media (min-width: 761px) {
  .bf-root.is-expanded .bf-history {
    position: relative;
    inset: auto;
    width: 300px;
    flex: 0 0 300px;
    transform: none;
    visibility: visible;
    border-inline-end: 1px solid var(--bf-border);
    transition: none;
  }
  .bf-root.is-expanded .bf-back-btn { display: none; }
}

/* ---------- Arabo (da destra a sinistra) ---------- */
[dir="rtl"].bf-root .bf-launcher { right: auto; left: 20px; }
[dir="rtl"].bf-root .bf-hint { right: auto; left: 80px; border-radius: 16px 16px 16px 4px; }
[dir="rtl"].bf-root .bf-chat { right: auto; left: 24px; transform-origin: bottom left; }
[dir="rtl"].bf-root .bf-header { padding: 16px 18px 16px 14px; }
[dir="rtl"].bf-root .bf-msg--bot .bf-bubble { border-bottom-left-radius: 18px; border-bottom-right-radius: 6px; }
[dir="rtl"].bf-root .bf-msg--user .bf-bubble { border-bottom-right-radius: 18px; border-bottom-left-radius: 6px; }
[dir="rtl"].bf-root .bf-msg--user .bf-time { text-align: left; }
[dir="rtl"].bf-root .bf-form { padding: 6px 14px 6px 6px; }
[dir="rtl"].bf-root .bf-history { transform: translateX(100%); }
[dir="rtl"].bf-root .bf-history.is-visible { transform: none; }
[dir="rtl"].bf-root .bf-bubble-icon::after { left: auto; right: 6px; border-left: 0; border-right: 6px solid #fff; }
[dir="rtl"].bf-root .bf-back-btn svg, [dir="rtl"].bf-root .bf-send svg { transform: scaleX(-1); }

/* ---------- Telefono: chat a tutto schermo ---------- */
@media (max-width: 600px) {
  .bf-launcher { right: 16px; bottom: 80px; width: 60px; height: 60px; }
  [dir="rtl"].bf-root .bf-launcher { left: 16px; }
  .bf-hint { right: 72px; }
  [dir="rtl"].bf-root .bf-hint { left: 72px; }
  .bf-chat,
  .bf-root.is-expanded .bf-chat,
  [dir="rtl"].bf-root .bf-chat {
    inset: 0;
    width: 100%;
    height: 100%;
    border-radius: 0;
  }
  .bf-expand-btn { display: none; }
  .bf-root.is-expanded .bf-messages,
  .bf-root.is-expanded .bf-chips { padding-left: 16px; padding-right: 16px; }
  .bf-root.is-expanded .bf-form { margin: 0 12px 12px; }
  .bf-form textarea { font-size: 16px; } /* evita lo zoom automatico su iPhone */
}

/* ---------- Animazioni ---------- */
@keyframes bfIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes bfDots { 0%, 80%, 100% { transform: translateY(0); opacity: .45; } 40% { transform: translateY(-4px); opacity: 1; } }
@keyframes bfWiggle { 0%, 80%, 100% { transform: rotate(0) scale(1); } 84% { transform: rotate(-10deg) scale(1.08); } 88% { transform: rotate(8deg) scale(1.08); } 92% { transform: rotate(-5deg) scale(1.04); } 96% { transform: rotate(3deg); } }
@keyframes bfPulse { 0% { transform: scale(.95); opacity: .6; } 70%, 100% { transform: scale(1.25); opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  .bf-root *, .bf-root *::before { animation: none !important; transition: none !important; }
}
