
:root{
  --pio-chi-blue:#5297b2;
  --pio-chi-blue-dark:#286f8b;
  --pio-chi-orange:#ff914d;
  --pio-chi-ink:#173241;
  --pio-chi-muted:#6e838d;
  --pio-chi-bg:#f5f9fb;
  --pio-chi-line:#dce9ee;
}

.pio-ai-root{
  position:fixed;
  right:18px;
  bottom:18px;
  z-index:2147483000;
  font-family:inherit;
  color:var(--pio-chi-ink);
}

.pio-ai-root *{box-sizing:border-box}

.pio-ai-toggle{
  position:relative;
  width:68px;
  height:68px;
  padding:0;
  border:0;
  border-radius:50%;
  background:transparent;
  color:inherit;
  box-shadow:none;
  cursor:pointer;
  display:grid;
  place-items:center;
  transition:transform .2s ease;
  isolation:isolate;
}
.pio-ai-toggle:hover{transform:scale(1.05)}
.pio-ai-toggle img{
  position:relative;
  z-index:3;
  width:58px;
  height:58px;
  object-fit:contain;
  filter:drop-shadow(0 9px 16px rgba(31,73,91,.22));
}
.pio-ai-pulse-ring{
  position:absolute;
  z-index:1;
  inset:5px;
  border-radius:50%;
  border:2px solid rgba(82,151,178,.30);
  animation:pioChiPulseRing 2.4s ease-out infinite;
}
.pio-ai-pulse-ring-two{
  border-color:rgba(255,145,77,.25);
  animation-delay:1.2s;
}
@keyframes pioChiPulseRing{
  0%{transform:scale(.84);opacity:.72}
  70%{transform:scale(1.28);opacity:0}
  100%{transform:scale(1.28);opacity:0}
}
  50%{transform:scale(1.16);opacity:1}
}

.pio-ai-panel{
  position:absolute;
  right:0;
  bottom:72px;
  width:min(410px,calc(100vw - 24px));
  height:min(690px,calc(100vh - 110px));
  background:#fff;
  border:1px solid rgba(82,151,178,.18);
  border-radius:26px;
  box-shadow:0 28px 85px rgba(24,66,84,.28);
  overflow:hidden;
  display:grid;
  grid-template-rows:auto 1fr auto auto auto;
  transform-origin:bottom right;
  animation:pioChiOpen .22s ease-out;
}

@keyframes pioChiOpen{
  from{opacity:0;transform:translateY(12px) scale(.975)}
  to{opacity:1;transform:translateY(0) scale(1)}
}

.pio-ai-header{
  position:relative;
  display:flex;
  justify-content:space-between;
  align-items:center;
  min-height:76px;
  padding:12px 14px 12px 16px;
  background:
    radial-gradient(circle at 86% -30%,rgba(255,145,77,.42),transparent 42%),
    linear-gradient(135deg,#2d7894,#5297b2);
  color:#fff;
}

.pio-ai-header:after{
  content:"";
  position:absolute;
  left:0;right:0;bottom:-1px;
  height:15px;
  background:#fff;
  clip-path:ellipse(58% 40% at 50% 100%);
}

.pio-ai-brand{display:flex;align-items:center;gap:11px;min-width:0}
.pio-ai-brand-avatar{
  position:relative;
  flex:0 0 50px;
  width:50px;height:50px;
  display:grid;place-items:center;
  border-radius:50%;
  background:rgba(255,255,255,.92);
  box-shadow:0 5px 14px rgba(12,58,76,.18);
}
.pio-ai-brand-avatar img{width:45px;height:45px;object-fit:contain}
.pio-ai-online{
  position:absolute;right:0;bottom:1px;
  width:12px;height:12px;border-radius:50%;
  background:#2bc477;border:2px solid #fff;
}
.pio-ai-brand-title{font-size:17px;font-weight:900;white-space:nowrap}
.pio-ai-brand-title span{font-weight:650;opacity:.9}
.pio-ai-brand small{display:block;font-size:11px;opacity:.82;margin-top:3px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.pio-ai-close{
  flex:0 0 38px;width:38px;height:38px;
  display:grid;place-items:center;
  border:1px solid rgba(255,255,255,.16);
  border-radius:12px;
  background:rgba(255,255,255,.11);
  color:#fff;font-size:25px;line-height:1;cursor:pointer
}

.pio-ai-messages{
  overflow-y:auto;
  overscroll-behavior:contain;
  padding:20px 15px 14px;
  background:
    linear-gradient(rgba(245,249,251,.91),rgba(245,249,251,.91)),
    radial-gradient(circle at 30% 20%,rgba(82,151,178,.13),transparent 34%);
  scrollbar-width:thin;
  scrollbar-color:#cbdde4 transparent;
}

.pio-ai-welcome{
  text-align:center;
  padding:16px 10px 8px;
}
.pio-ai-welcome-logo{
  width:86px;height:86px;
  margin:0 auto 10px;
  display:grid;place-items:center;
  border-radius:50%;
  background:#fff;
  box-shadow:0 12px 28px rgba(45,120,148,.13);
  animation:pioChiFloat 4.6s ease-in-out infinite;
}
.pio-ai-welcome-logo img{width:76px;height:76px;object-fit:contain}
@keyframes pioChiFloat{0%,100%{transform:translateY(0)}50%{transform:translateY(-5px)}}

.pio-ai-welcome-kicker{
  color:var(--pio-chi-orange);
  font-size:12px;
  font-weight:900;
  letter-spacing:.08em;
  text-transform:uppercase;
}
.pio-ai-welcome h3{
  margin:5px 0 8px;
  font-size:25px;
  line-height:1.08;
  color:var(--pio-chi-ink);
}
.pio-ai-welcome p{
  max-width:330px;
  margin:0 auto;
  color:var(--pio-chi-muted);
  font-size:13px;
  line-height:1.52;
}
.pio-ai-suggestions{
  display:grid;
  grid-template-columns:1fr 1fr;
  gap:8px;
  margin-top:18px;
}
.pio-ai-suggestions button{
  min-height:45px;
  padding:9px 10px;
  border:1px solid var(--pio-chi-line);
  border-radius:13px;
  background:#fff;
  color:var(--pio-chi-blue-dark);
  font:inherit;
  font-size:12px;
  font-weight:750;
  cursor:pointer;
  transition:.18s ease;
}
.pio-ai-suggestions button:hover{
  border-color:rgba(82,151,178,.45);
  background:#edf7fa;
  transform:translateY(-1px);
}

.pio-ai-row{display:flex;align-items:flex-end;gap:8px;margin:9px 0}
.pio-ai-row.user{justify-content:flex-end}
.pio-ai-avatar{
  flex:0 0 30px;width:30px;height:30px;
  display:grid;place-items:center;
  border-radius:50%;background:#fff;border:1px solid var(--pio-chi-line)
}
.pio-ai-avatar img{width:27px;height:27px;object-fit:contain}

.pio-ai-msg{
  max-width:82%;
  padding:11px 13px;
  border-radius:16px;
  line-height:1.52;
  font-size:13.5px;
  white-space:pre-wrap;
  overflow-wrap:anywhere;
}
.pio-ai-msg.bot{
  background:#fff;
  color:var(--pio-chi-ink);
  border:1px solid #e0ebef;
  border-bottom-left-radius:5px;
  box-shadow:0 5px 14px rgba(37,77,93,.045);
}
.pio-ai-msg.user{
  color:#fff;
  background:linear-gradient(135deg,#438da9,#5297b2);
  border-bottom-right-radius:5px;
  box-shadow:0 5px 14px rgba(52,124,151,.13);
}

.pio-ai-src{
  margin:5px 0 12px 38px;
  max-width:82%;
  padding:9px 11px;
  border:1px solid var(--pio-chi-line);
  border-radius:13px;
  background:rgba(255,255,255,.78);
  font-size:11.5px;
}
.pio-ai-src strong{display:block;margin-bottom:4px;color:var(--pio-chi-muted);font-size:10px;text-transform:uppercase;letter-spacing:.06em}
.pio-ai-src a,.pio-ai-src div{
  display:block;
  color:var(--pio-chi-blue-dark)!important;
  text-decoration:none;
  margin:4px 0;
  line-height:1.35
}

.pio-ai-thinking{
  display:flex;
  align-items:center;
  gap:10px;
  padding:8px 15px 10px;
  background:var(--pio-chi-bg);
  color:var(--pio-chi-muted);
  border-top:1px solid rgba(220,233,238,.7);
}
.pio-ai-thinking-avatar{
  width:34px;height:34px;display:grid;place-items:center;border-radius:50%;
  background:#fff;border:1px solid var(--pio-chi-line);
  animation:pioChiThink 1.35s ease-in-out infinite;
}
.pio-ai-thinking-avatar img{width:30px;height:30px;object-fit:contain}
@keyframes pioChiThink{0%,100%{transform:scale(.94)}50%{transform:scale(1.06)}}
.pio-ai-thinking strong,.pio-ai-thinking small{display:block}
.pio-ai-thinking strong{font-size:12px;color:var(--pio-chi-blue-dark)}
.pio-ai-thinking small{font-size:10px;margin-top:1px}
.pio-ai-dots{display:flex;gap:3px;margin-left:auto}
.pio-ai-dots i{display:block;width:5px;height:5px;border-radius:50%;background:var(--pio-chi-orange);animation:pioChiDot 1s infinite ease-in-out}
.pio-ai-dots i:nth-child(2){animation-delay:.14s}.pio-ai-dots i:nth-child(3){animation-delay:.28s}
@keyframes pioChiDot{0%,70%,100%{transform:translateY(0);opacity:.45}35%{transform:translateY(-4px);opacity:1}}

.pio-ai-form{
  padding:10px 12px 5px;
  background:#fff;
  border-top:1px solid #e4edf0;
}
.pio-ai-compose{
  display:flex;
  align-items:flex-end;
  gap:7px;
  padding:6px 6px 6px 12px;
  background:#f7fafb;
  border:1px solid #dbe8ed;
  border-radius:17px;
  transition:border-color .18s,box-shadow .18s;
}
.pio-ai-compose:focus-within{
  border-color:rgba(82,151,178,.65);
  box-shadow:0 0 0 3px rgba(82,151,178,.09);
}
.pio-ai-compose textarea{
  flex:1;
  min-height:36px;
  max-height:110px;
  padding:8px 0;
  border:0!important;
  outline:0!important;
  resize:none;
  background:transparent!important;
  box-shadow:none!important;
  color:var(--pio-chi-ink);
  font:inherit;
  font-size:14px;
  line-height:1.45;
}
.pio-ai-compose textarea::placeholder{color:#8ba0a8}
.pio-ai-compose button{
  flex:0 0 40px;width:40px;height:40px;
  display:grid;place-items:center;
  border:0;border-radius:13px;
  background:linear-gradient(135deg,var(--pio-chi-orange),#ffa366);
  color:#fff;
  cursor:pointer;
  box-shadow:0 7px 16px rgba(255,145,77,.26);
}
.pio-ai-compose button svg{width:19px;height:19px;fill:currentColor}
.pio-ai-compose button:disabled{opacity:.55;cursor:wait}
.pio-ai-note{
  display:block;
  padding:3px 14px 9px;
  background:#fff;
  color:#8a9ba2;
  font-size:9.5px;
  line-height:1.3;
  text-align:center;
}

body.pio-ai-mobile-open{overflow:hidden!important;touch-action:none}

@media(max-width:600px){
  .pio-ai-root{
    right:12px;
    bottom:max(12px,env(safe-area-inset-bottom));
  }

  .pio-ai-toggle{
    width:58px;height:58px;min-height:58px;
    padding:5px;
    justify-content:center;
    border-radius:50%;
  }
  .pio-ai-toggle img{width:47px;height:47px}
  .pio-ai-toggle-halo{left:4px;top:4px;width:49px;height:49px}
  .pio-ai-toggle-label{display:none}

  .pio-ai-panel{
    position:fixed;
    z-index:2147483001;
    left:0;right:0;top:0;bottom:0;
    width:100vw;
    height:100dvh;
    max-width:none;
    max-height:none;
    border:0;
    border-radius:0;
    grid-template-rows:auto 1fr auto auto auto;
    transform-origin:bottom center;
    animation:pioChiMobileOpen .24s ease-out;
  }

  @keyframes pioChiMobileOpen{
    from{opacity:.65;transform:translateY(18px)}
    to{opacity:1;transform:translateY(0)}
  }

  .pio-ai-header{
    min-height:calc(72px + env(safe-area-inset-top));
    padding:calc(10px + env(safe-area-inset-top)) 12px 10px;
  }
  .pio-ai-header:after{height:10px}
  .pio-ai-brand-avatar{width:46px;height:46px;flex-basis:46px}
  .pio-ai-brand-avatar img{width:41px;height:41px}
  .pio-ai-brand-title{font-size:16px}
  .pio-ai-brand small{font-size:10px;max-width:230px}
  .pio-ai-close{width:40px;height:40px}

  .pio-ai-messages{
    padding:14px 12px;
    -webkit-overflow-scrolling:touch;
  }
  .pio-ai-welcome{padding:10px 5px 5px}
  .pio-ai-welcome-logo{width:78px;height:78px}
  .pio-ai-welcome-logo img{width:69px;height:69px}
  .pio-ai-welcome h3{font-size:23px}
  .pio-ai-welcome p{font-size:13px}
  .pio-ai-suggestions{grid-template-columns:1fr 1fr;gap:7px;margin-top:15px}
  .pio-ai-suggestions button{min-height:43px;font-size:11.5px;padding:8px}

  .pio-ai-msg{max-width:85%;font-size:14px;padding:10px 12px}
  .pio-ai-avatar{width:29px;height:29px;flex-basis:29px}
  .pio-ai-avatar img{width:26px;height:26px}
  .pio-ai-src{margin-left:37px;max-width:84%}

  .pio-ai-thinking{padding:7px 12px}
  .pio-ai-form{
    padding:8px 10px 4px;
  }
  .pio-ai-compose{
    border-radius:16px;
    padding-left:11px;
  }
  .pio-ai-compose textarea{
    font-size:16px; /* evita zoom automatico su iPhone */
  }
  .pio-ai-note{
    padding-bottom:max(8px,env(safe-area-inset-bottom));
  }
}

@media(max-width:370px){
  .pio-ai-brand small{max-width:185px}
  .pio-ai-suggestions{grid-template-columns:1fr}
}

.pio-ai-msg.bot a{
  color:var(--pio-chi-blue-dark)!important;
  font-weight:800;
  text-decoration:underline;
  text-underline-offset:2px;
}
.pio-ai-action-link{
  display:inline-flex;
  align-items:center;
  justify-content:center;
  margin-top:8px;
  padding:9px 12px;
  border-radius:11px;
  background:linear-gradient(135deg,var(--pio-chi-orange),#ffa363);
  color:#fff!important;
  text-decoration:none!important;
  font-weight:800;
  box-shadow:0 7px 16px rgba(255,145,77,.20);
}
@media(max-width:600px){
  .pio-ai-toggle{
    width:64px!important;
    height:64px!important;
  }
  .pio-ai-toggle img{
    width:56px!important;
    height:56px!important;
  }
  .pio-ai-panel{
    background:#fff;
  }
  .pio-ai-messages{
    padding-bottom:18px;
  }
}


/* === CHI 0.7.1 FIX === */

/* Il tema non deve disegnare rettangoli/outline intorno al launcher. */
#pio-ai-root #pio-ai-toggle,
#pio-ai-root #pio-ai-toggle:hover,
#pio-ai-root #pio-ai-toggle:active,
#pio-ai-root #pio-ai-toggle:focus{
  appearance:none!important;
  -webkit-appearance:none!important;
  border:0!important;
  outline:0!important;
  background:transparent!important;
  box-shadow:none!important;
  text-decoration:none!important;
  -webkit-tap-highlight-color:transparent!important;
}

#pio-ai-root #pio-ai-toggle::before,
#pio-ai-root #pio-ai-toggle::after{
  content:none!important;
  display:none!important;
}

#pio-ai-root #pio-ai-toggle:focus-visible{
  outline:0!important;
}

#pio-ai-root #pio-ai-toggle:focus-visible img{
  border-radius:50%;
  filter:
    drop-shadow(0 0 0 rgba(0,0,0,0))
    drop-shadow(0 0 6px rgba(82,151,178,.55));
}

/* La chat non deve crescere con le risposte: il centro della grid scorre. */
#pio-ai-root .pio-ai-panel{
  grid-template-rows:auto minmax(0,1fr) auto auto auto!important;
  overflow:hidden!important;
  contain:layout paint;
}

#pio-ai-root .pio-ai-messages{
  min-height:0!important;
  height:auto!important;
  max-height:none!important;
  overflow-y:auto!important;
  overflow-x:hidden!important;
  overscroll-behavior:contain!important;
  scrollbar-gutter:stable;
  -webkit-overflow-scrolling:touch;
}

#pio-ai-root .pio-ai-row,
#pio-ai-root .pio-ai-msg,
#pio-ai-root .pio-ai-src{
  min-width:0;
}

#pio-ai-root .pio-ai-msg{
  overflow-wrap:anywhere!important;
  word-break:normal!important;
}

#pio-ai-root .pio-ai-msg img,
#pio-ai-root .pio-ai-msg iframe,
#pio-ai-root .pio-ai-msg video{
  max-width:100%!important;
  height:auto!important;
}

@media (min-width:601px){
  #pio-ai-root .pio-ai-panel{
    height:min(690px,calc(100dvh - 110px))!important;
    max-height:calc(100dvh - 110px)!important;
  }
}

@media (max-width:600px){
  #pio-ai-root .pio-ai-panel{
    width:100vw!important;
    height:100dvh!important;
    min-height:0!important;
    max-height:100dvh!important;
  }

  #pio-ai-root .pio-ai-messages{
    min-height:0!important;
    flex:1 1 auto!important;
  }
}


/* =========================================================
   CHI 0.7.5 — LAYOUT DEFINITIVO DESKTOP + MOBILE
   ========================================================= */

/* DESKTOP: finestra fissa, non cresce mai con il testo */
#pio-ai-root .pio-ai-panel{
  position:absolute !important;
  right:0 !important;
  bottom:82px !important;
  width:420px !important;
  height:680px !important;
  max-width:calc(100vw - 28px) !important;
  max-height:calc(100dvh - 120px) !important;

  display:grid !important;
  grid-template-rows:auto minmax(0,1fr) auto auto auto !important;

  overflow:hidden !important;
  border-radius:24px !important;
  background:#fff !important;
  contain:layout paint !important;
}

/* L'area messaggi è L'UNICA zona che scorre */
#pio-ai-root .pio-ai-messages{
  position:relative !important;
  min-height:0 !important;
  height:100% !important;
  max-height:none !important;

  overflow-y:auto !important;
  overflow-x:hidden !important;
  overscroll-behavior-y:contain !important;
  -webkit-overflow-scrolling:touch !important;

  touch-action:pan-y !important;
  scrollbar-width:thin !important;
  scrollbar-color:#bdd3dc transparent !important;
}

#pio-ai-root .pio-ai-messages::-webkit-scrollbar{
  width:6px;
}
#pio-ai-root .pio-ai-messages::-webkit-scrollbar-thumb{
  background:#bdd3dc;
  border-radius:999px;
}

/* Header e composer non devono mai restringersi */
#pio-ai-root .pio-ai-header,
#pio-ai-root .pio-ai-thinking,
#pio-ai-root .pio-ai-form,
#pio-ai-root .pio-ai-note{
  flex:0 0 auto !important;
  min-height:0;
}

/* Evita che testi/link allarghino o allarghino la chat */
#pio-ai-root .pio-ai-row,
#pio-ai-root .pio-ai-msg,
#pio-ai-root .pio-ai-src,
#pio-ai-root .pio-ai-welcome{
  min-width:0 !important;
  max-width:100%;
}
#pio-ai-root .pio-ai-msg{
  overflow-wrap:anywhere !important;
  word-break:break-word !important;
}

/* MOBILE: vera schermata intera */
@media (max-width:600px){
  html.pio-ai-mobile-open,
  body.pio-ai-mobile-open{
    overflow:hidden !important;
    height:100% !important;
  }

  #pio-ai-root{
    right:10px !important;
    bottom:max(10px, env(safe-area-inset-bottom)) !important;
  }

  #pio-ai-root .pio-ai-panel{
    position:fixed !important;
    inset:0 !important;

    width:100vw !important;
    height:100dvh !important;
    min-width:100vw !important;
    min-height:100dvh !important;
    max-width:100vw !important;
    max-height:100dvh !important;

    border:0 !important;
    border-radius:0 !important;
    box-shadow:none !important;

    display:grid !important;
    grid-template-rows:
      auto
      minmax(0,1fr)
      auto
      auto
      auto !important;

    overflow:hidden !important;
    background:#fff !important;

    padding-top:0 !important;
    padding-bottom:0 !important;
  }

  #pio-ai-root .pio-ai-header{
    position:relative !important;
    z-index:4 !important;
    min-height:calc(68px + env(safe-area-inset-top)) !important;
    padding:
      calc(9px + env(safe-area-inset-top))
      12px
      9px !important;
  }

  /* Pulsante di chiusura sempre ben visibile e cliccabile */
  #pio-ai-root .pio-ai-close{
    position:relative !important;
    z-index:8 !important;
    width:42px !important;
    height:42px !important;
    min-width:42px !important;
    min-height:42px !important;
    border-radius:50% !important;
    font-size:26px !important;
    touch-action:manipulation !important;
  }

  #pio-ai-root .pio-ai-messages{
    min-height:0 !important;
    height:100% !important;
    overflow-y:auto !important;
    overflow-x:hidden !important;
    padding:14px 12px 18px !important;
    -webkit-overflow-scrolling:touch !important;
    overscroll-behavior-y:contain !important;
    touch-action:pan-y !important;
  }

  #pio-ai-root .pio-ai-thinking{
    padding:7px 11px !important;
  }

  /* Composer compatibile con tastiera software */
  #pio-ai-root .pio-ai-form{
    position:relative !important;
    z-index:5 !important;
    padding:8px 10px 5px !important;
    background:#fff !important;
  }

  #pio-ai-root .pio-ai-compose{
    min-height:52px !important;
  }

  #pio-ai-root .pio-ai-compose textarea{
    font-size:16px !important; /* evita zoom Safari */
    line-height:1.4 !important;
    max-height:96px !important;
  }

  #pio-ai-root .pio-ai-note{
    position:relative !important;
    z-index:5 !important;
    padding:
      2px
      12px
      max(8px, env(safe-area-inset-bottom)) !important;
    background:#fff !important;
  }

  /* Quando compare la tastiera, la viewport visuale può ridursi.
     La variabile viene aggiornata dal JS. */
  #pio-ai-root .pio-ai-panel{
    height:var(--pio-visual-height, 100dvh) !important;
    min-height:var(--pio-visual-height, 100dvh) !important;
    max-height:var(--pio-visual-height, 100dvh) !important;
  }
}


/* =========================================================
   CHI 0.7.6 — APERTURA/CHIUSURA
   Il selettore [hidden] deve vincere su display:grid!important.
   ========================================================= */
#pio-ai-root .pio-ai-panel[hidden]{
  display:none !important;
}

#pio-ai-root .pio-ai-thinking[hidden]{
  display:none !important;
}


/* =========================================================
   CHI 0.7.6 — FIX APERTURA / CHIUSURA
   Il selettore [hidden] deve vincere su display:grid!important.
   ========================================================= */
#pio-ai-root .pio-ai-panel[hidden],
#pio-ai-root [hidden].pio-ai-panel{
  display:none !important;
}

/* Quando JS rimuove hidden, torna alla struttura grid della chat. */
#pio-ai-root .pio-ai-panel:not([hidden]){
  display:grid !important;
}
