/* WebG AI v2.1 — Clean */
*, *::before, *::after { box-sizing: border-box; }

:root {
  --c:    #0066cc;
  --cd:   #004fa3;
  --bg:   #ffffff;
  --sur:  #f4f7fb;
  --brd:  #e2e8f0;
  --txt:  #1a202c;
  --muted:#718096;
}

/* ── LAUNCHER ── */
#webg-launcher {
  position:fixed; bottom:20px; right:20px;
  width:64px; height:64px; border-radius:50%;
  background:#ffffff; border:none; cursor:pointer;
  z-index:999998; display:flex; align-items:center; justify-content:center;
  box-shadow:0 4px 20px rgba(0,0,0,.25);
  transition:transform .2s; outline:none; overflow:visible;
  padding:0;
}
#webg-launcher:hover { transform:scale(1.08); }
#webg-launcher svg { width:28px; height:28px; transition:opacity .2s,transform .2s; }
#webg-launcher img.ico-chat { width:64px; height:64px; border-radius:50%; object-fit:cover; transition:opacity .2s,transform .2s; }
#webg-launcher .ico-chat  { opacity:1; transform:scale(1); position:absolute; }
#webg-launcher .ico-close { opacity:0; transform:scale(.6) rotate(-90deg); position:absolute; background:rgba(0,0,0,0.5); border-radius:50%; padding:4px; }
#webg-launcher.open .ico-chat  { opacity:0; transform:scale(.6) rotate(90deg); }
#webg-launcher.open .ico-close { opacity:1; transform:scale(1); }
.webg-badge {
  position:absolute; top:0px; right:0px;
  background:#e53e3e; color:#fff; font-size:9px; font-weight:700;
  width:18px; height:18px; border-radius:50%;
  display:flex; align-items:center; justify-content:center;
  border:2px solid #fff; z-index:2;
  pointer-events:none;
}

/* ── PANEL desktop ── */
#webg-panel {
  position:fixed; bottom:96px; right:24px;
  width:360px; max-width:calc(100vw - 32px);
  height:480px; max-height:calc(100vh - 120px);
  background:#ffffff; border-radius:18px;
  box-shadow:0 8px 40px rgba(0,0,0,.18);
  z-index:999997; display:flex; flex-direction:column;
  overflow:hidden; border:1px solid var(--brd);
  opacity:0; transform:translateY(16px) scale(.97); pointer-events:none;
  transition:opacity .25s,transform .25s;
}
#webg-panel.open { opacity:1; transform:translateY(0) scale(1); pointer-events:all; }

/* ── HEADER ── */
#webg-header {
  background:var(--c); color:#fff;
  padding:14px 16px; display:flex; align-items:center; gap:10px; flex-shrink:0;
}
.webg-avatar {
  width:40px; height:40px; border-radius:50%;
  background:rgba(255,255,255,.15); border:2px solid rgba(255,255,255,.3);
  display:flex; align-items:center; justify-content:center; flex-shrink:0; overflow:hidden;
}
.webg-avatar svg { width:38px; height:38px; }
.webg-header-info { flex:1; min-width:0; }
.webg-name { font-weight:700; font-size:14px; }
.webg-status { font-size:11px; opacity:.85; display:flex; align-items:center; gap:4px; margin-top:2px; }
.webg-dot { width:6px; height:6px; border-radius:50%; background:#48bb78; animation:blink 2s infinite; }
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:.4} }

/* ── QUICK REPLIES ── */
#webg-quick {
  display:flex; flex-wrap:wrap; gap:6px;
  padding:10px 12px 8px; border-bottom:1px solid #e2e8f0;
  flex-shrink:0; width:100%; overflow:hidden;
}
.webg-qr {
  font-size:12px; padding:6px 12px; border-radius:20px;
  border:1.5px solid #e2e8f0; color:#1a202c; background:#f4f7fb;
  cursor:pointer; white-space:nowrap; flex-shrink:0;
  transition:background .15s,color .15s,border-color .15s;
}
.webg-qr:hover { background:#0066cc; color:#fff; border-color:#0066cc; }

/* ── MESSAGES ── */
#webg-messages {
  flex:1; overflow-y:auto; overflow-x:hidden;
  padding:14px 12px; display:flex; flex-direction:column; gap:10px; width:100%;
}
#webg-messages::-webkit-scrollbar { width:4px; }
#webg-messages::-webkit-scrollbar-thumb { background:var(--brd); border-radius:4px; }

.webg-msg {
  display:flex; flex-direction:column;
  max-width:78%; width:fit-content;
  animation:msgin .2s ease-out;
}
@keyframes msgin { from{opacity:0;transform:translateY(6px)} to{opacity:1;transform:translateY(0)} }
.webg-msg.user { align-self:flex-end; align-items:flex-end; max-width:70%; }
.webg-msg.bot  { align-self:flex-start; align-items:flex-start; }

.webg-bubble {
  padding:10px 14px; border-radius:16px;
  font-size:13.5px; line-height:1.55;
  word-break:break-word; overflow-wrap:break-word; max-width:100%;
}
.webg-msg.user .webg-bubble { background:var(--c); color:#fff; border-bottom-right-radius:4px; }
.webg-msg.bot  .webg-bubble { background:#f0f4f8; color:var(--txt); border-bottom-left-radius:4px; }
.webg-time { font-size:10px; color:var(--muted); margin-top:3px; padding:0 4px; white-space:nowrap; }

.webg-typing .webg-bubble { display:flex; align-items:center; gap:4px; padding:12px 14px; }
.webg-typing .webg-bubble span { width:6px; height:6px; background:var(--muted); border-radius:50%; animation:dot 1.2s infinite; }
.webg-typing .webg-bubble span:nth-child(2){animation-delay:.2s}
.webg-typing .webg-bubble span:nth-child(3){animation-delay:.4s}
@keyframes dot { 0%,80%,100%{transform:scale(.7);opacity:.5} 40%{transform:scale(1);opacity:1} }

/* ── INPUT ── */
#webg-input-area { padding:10px 12px 12px; border-top:1px solid var(--brd); flex-shrink:0; }
.webg-input-wrap {
  display:flex; align-items:flex-end; gap:8px;
  background:var(--sur); border:1.5px solid var(--brd); border-radius:12px;
  padding:8px 8px 8px 12px; transition:border-color .2s;
}
.webg-input-wrap:focus-within { border-color:var(--c); }
#webg-input {
  flex:1; min-width:0; border:none; background:#ffffff; resize:none; outline:none;
  font-size:13.5px; line-height:1.5; color:var(--txt);
  max-height:90px; min-height:22px; font-family:inherit;
}
#webg-input::placeholder { color:var(--muted); }
#webg-send {
  width:34px; height:34px; border-radius:8px;
  background:var(--c); color:#fff; border:none; cursor:pointer;
  display:flex; align-items:center; justify-content:center; flex-shrink:0;
  transition:background .15s,transform .1s;
}
#webg-send:hover { background:var(--cd); }
#webg-send:active { transform:scale(.93); }
#webg-send:disabled { background:#a0aec0; cursor:not-allowed; }
#webg-send svg { width:16px; height:16px; }
.webg-powered { text-align:center; font-size:10px; color:var(--muted); padding:4px 0 0; opacity:.7; }
.webg-lead-ok { background:#f0fff4; border:1px solid #9ae6b4; color:#276749; font-size:12px; padding:8px 12px; border-radius:8px; }

/* ══════════════════════════════
   MOBILE
══════════════════════════════ */
@media (max-width: 600px) {

  /* launcher: piccolo, bottom-right, Z alto */
  #webg-launcher {
    width:64px !important; height:64px !important;
    bottom:20px !important; right:16px !important;
    z-index:999999 !important;
  }
  #webg-launcher svg { width:22px !important; height:22px !important; }

  /* panel: fullscreen, sotto la status bar */
  #webg-panel {
    position:fixed !important;
    top:0 !important; left:0 !important; right:0 !important; bottom:0 !important;
    width:100vw !important; max-width:100vw !important;
    height:100% !important; max-height:100% !important;
    border-radius:0 !important; border:none !important;
    z-index:999997 !important;
    background:#ffffff !important;
    -webkit-overflow-scrolling: touch !important;
  }

  /* header: padding per status bar iOS */
  #webg-header {
    padding:14px 56px 14px 16px !important;
    padding-top:max(14px, env(safe-area-inset-top, 14px)) !important;
    min-height:64px;
    position:relative !important;
  }
  .webg-avatar { width:46px !important; height:46px !important; }
  .webg-name { font-size:17px !important; }
  .webg-status { font-size:13px !important; }

  /* quick: riga unica scorrevole, colori neutri NON verdi */
  #webg-quick {
    flex-wrap:wrap !important;
    overflow-x:hidden !important;
    padding:10px 14px 8px !important;
    gap:7px !important;
    background:#ffffff !important;
    border-bottom:none !important;
  }
  .webg-qr {
    font-size:13px !important;
    padding:8px 12px !important;
    background:var(--sur) !important;
    color:var(--txt) !important;
    border-color:var(--brd) !important;
    flex:1 1 calc(50% - 4px) !important;
    text-align:center !important;
    white-space:nowrap !important;
    overflow:hidden !important;
    text-overflow:ellipsis !important;
  }
  .webg-qr:hover, .webg-qr:active {
    background:var(--c) !important; color:#fff !important; border-color:var(--c) !important;
  }

  /* messages */
  #webg-messages {
    flex:1 !important; overflow-y:auto !important; overflow-x:hidden !important;
    padding:16px 14px !important; width:100% !important;
  }

  /* bolle: max-width stretto per non uscire mai */
  .webg-msg { max-width:75% !important; }
  .webg-msg.user {
    max-width:62% !important;
    align-self:flex-end !important;
    margin-right:0 !important;
    overflow:hidden !important;
    word-break:break-word !important;
  }
  .webg-msg.bot  { max-width:82% !important; align-self:flex-start !important; }
  .webg-bubble { font-size:15px !important; line-height:1.6 !important; padding:11px 14px !important; }
  .webg-time { font-size:11px !important; }

  /* input: fisso in basso */
  #webg-input-area {
    flex-shrink:0 !important;
    padding:10px 14px !important;
    padding-bottom:max(14px, env(safe-area-inset-bottom, 14px)) !important;
    background:#ffffff !important;
    border-top:1px solid var(--brd) !important;
    width:100% !important;
    box-sizing:border-box !important;
  }
  .webg-input-wrap {
    border-radius:24px !important;
    padding:10px 10px 10px 16px !important;
    width:100% !important;
    overflow:hidden !important;
  }
  #webg-input { font-size:15px !important; }
  #webg-send {
    width:38px !important; height:38px !important; border-radius:50% !important;
  }
  #webg-send svg { width:17px !important; height:17px !important; }
  .webg-powered { padding:6px 0 4px !important; font-size:11px !important; }
}

/* Close button in header (mobile) */
#webg-close-btn {
  flex-shrink: 0;
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  margin-left: auto;
  background: rgba(255,255,255,.2);
  border: none;
  color: #fff;
  width: 34px; height: 34px;
  border-radius: 50%;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
  line-height: 1;
  padding: 0;
  transition: background .15s;
}
#webg-close-btn:hover { background: rgba(255,255,255,.35); }

@media (max-width: 600px) {
  /* bolla utente - fix overflow definitivo */
  #webg-messages {
    padding-right: 16px !important;
  }
  .webg-msg.user .webg-bubble {
    margin-right: 0 !important;
  }
  /* send button sempre blu */
  #webg-send {
    background: var(--c) !important;
  }
  #webg-send:not(:disabled) {
    background: var(--c) !important;
    opacity: 1 !important;
  }
  #webg-send:disabled {
    background: var(--c) !important;
    opacity: 0.45 !important;
  }
}

@media (max-width: 600px) {
  /* panel chiuso su mobile: nascosto completamente */
  #webg-panel:not(.open) {
    opacity: 0 !important;
    pointer-events: none !important;
    visibility: hidden !important;
  }
  /* panel aperto su mobile: visibile e solido */
  #webg-panel.open {
    opacity: 1 !important;
    visibility: visible !important;
    pointer-events: all !important;
    background: #ffffff !important;
  }
  /* launcher più grande */
  #webg-launcher {
    width: 58px !important;
    height: 58px !important;
    bottom: 24px !important;
    right: 20px !important;
  }
  #webg-launcher svg { width: 26px !important; height: 26px !important; }
}
