/* ═══════════════════════════════════════════════════════════
   AI Chatbot widget

   Scoped entirely under #ag-chat, so it cannot leak into the page
   or be leaked into. --ag-chat-c is set inline from the colour in
   Admin; everything tinted derives from it.
   ═══════════════════════════════════════════════════════════ */

#ag-chat {
  --ag-chat-c: #6a00ff;
  --ag-chat-ink: #16181d;
  --ag-chat-muted: #6b7280;
  --ag-chat-line: #e6e8ee;
  --ag-chat-bg: #fff;
  --ag-chat-in: #f3f4f8;
  --ag-chat-radius: 16px;
  position: fixed;
  z-index: 9998;
  bottom: 20px;
  font-family: var(--font-body), -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* The right edge is already a column: .stt-btn sits at bottom 28px and
   .whatsapp-float at 90px (includes/layout-footer.php). A launcher at 20px
   would land on top of the scroll-to-top button, so the right-hand placement
   takes the next slot up rather than overlapping. Left is clear, which is why
   it is the default in Admin. */
#ag-chat.ag-right { right: 22px; bottom: 152px; }
#ag-chat.ag-left  { left: 20px; }

/* ── Launcher ─────────────────────────────────────────── */
.ag-chat-launcher {
  width: 48px; height: 48px;
  border: 0; border-radius: 50%;
  background: var(--ag-chat-c);
  color: #fff;
  cursor: pointer;
  display: grid; place-items: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .22);
  transition: transform .18s ease, box-shadow .18s ease;
}
#ag-chat.ag-right .ag-chat-launcher { margin-left: auto; }
.ag-chat-launcher:hover  { transform: translateY(-2px); box-shadow: 0 12px 28px rgba(0, 0, 0, .28); }
.ag-chat-launcher:active { transform: translateY(0); }
.ag-chat-launcher svg { width: 22px; height: 22px; fill: currentColor; }
.ag-chat-launcher .ag-ic-close { display: none; }
#ag-chat.ag-open .ag-chat-launcher .ag-ic-open  { display: none; }
#ag-chat.ag-open .ag-chat-launcher .ag-ic-close { display: block; }

/* Unread dot on the launcher before the panel has ever been opened */
.ag-chat-ping {
  position: absolute; top: 0; right: 0;
  width: 13px; height: 13px; border-radius: 50%;
  background: #ff3b5c; border: 2px solid #fff;
}
#ag-chat.ag-open .ag-chat-ping, #ag-chat.ag-seen .ag-chat-ping { display: none; }

/* ── Panel ────────────────────────────────────────────── */
.ag-chat-panel {
  position: absolute; bottom: 62px;
  width: 310px; max-width: calc(100vw - 32px);
  /* The panel grows upward from the launcher, so its ceiling has to know how
     high the launcher is sitting. When the cookie banner raises it, subtract
     that too — otherwise the header runs off the top of the screen and the
     close button goes with it. */
  height: 430px;
  max-height: calc(100vh - 120px - var(--ag-cookie-h, 0px));
  background: var(--ag-chat-bg);
  border: 1px solid var(--ag-chat-line);
  border-radius: var(--ag-chat-radius);
  box-shadow: 0 24px 60px rgba(16, 18, 24, .22);
  display: flex; flex-direction: column;
  overflow: hidden;
  /* Hidden by default without display:none, so the open transition runs. */
  opacity: 0; visibility: hidden;
  transform: translateY(12px) scale(.98);
  transition: opacity .18s ease, transform .18s ease, visibility .18s,
              bottom .3s cubic-bezier(.4, 0, .2, 1);
}
#ag-chat.ag-right .ag-chat-panel { right: 0; }
#ag-chat.ag-left  .ag-chat-panel { left: 0; }
#ag-chat.ag-open  .ag-chat-panel {
  opacity: 1; visibility: visible;
  transform: translateY(0) scale(1);
}

/* ── Header ───────────────────────────────────────────── */
.ag-chat-head {
  background: var(--ag-chat-c);
  color: #fff;
  padding: 10px 12px;
  display: flex; align-items: center; gap: 9px;
  flex-shrink: 0;
}
.ag-chat-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: rgba(255, 255, 255, .2);
  display: grid; place-items: center;
  font-weight: 700; font-size: 14px;
  flex-shrink: 0;
}
.ag-chat-title { font-size: 13.5px; font-weight: 700; line-height: 1.2; }
.ag-chat-status {
  font-size: 10.5px; opacity: .85; margin-top: 2px;
  display: flex; align-items: center; gap: 5px;
}
.ag-chat-dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: #4ade80; display: inline-block;
}
.ag-chat-close {
  margin-left: auto;
  background: rgba(255, 255, 255, .15);
  border: 0; border-radius: 8px;
  color: #fff; cursor: pointer;
  width: 24px; height: 24px;
  display: grid; place-items: center;
  font-size: 15px; line-height: 1;
}
.ag-chat-close:hover { background: rgba(255, 255, 255, .28); }

/* ── Message list ─────────────────────────────────────── */
.ag-chat-log {
  flex: 1; overflow-y: auto;
  padding: 12px 12px;
  display: flex; flex-direction: column; gap: 8px;
  background: #fbfbfd;
  overscroll-behavior: contain;
}
.ag-chat-log::-webkit-scrollbar { width: 6px; }
.ag-chat-log::-webkit-scrollbar-thumb { background: #d4d7e0; border-radius: 3px; }

.ag-msg {
  max-width: 84%;
  padding: 7px 11px;
  border-radius: 12px;
  font-size: 12.8px; line-height: 1.48;
  color: var(--ag-chat-ink);
  white-space: pre-wrap; word-wrap: break-word;
  animation: ag-msg-in .2s ease;
}
@keyframes ag-msg-in { from { opacity: 0; transform: translateY(6px); } }
.ag-msg-bot {
  background: var(--ag-chat-in);
  align-self: flex-start;
  border-bottom-left-radius: 5px;
}
.ag-msg-user {
  background: var(--ag-chat-c);
  color: #fff;
  align-self: flex-end;
  border-bottom-right-radius: 5px;
}
.ag-msg-err {
  background: #fff1f2; color: #b42340;
  align-self: flex-start;
  border: 1px solid #ffd9df;
}

/* Typing indicator */
.ag-typing { display: flex; gap: 4px; padding: 12px 14px; }
.ag-typing span {
  width: 6px; height: 6px; border-radius: 50%;
  background: #9aa1ae;
  animation: ag-bounce 1.2s infinite;
}
.ag-typing span:nth-child(2) { animation-delay: .18s; }
.ag-typing span:nth-child(3) { animation-delay: .36s; }
@keyframes ag-bounce { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-5px); opacity: 1; } }

/* ── Quick actions ────────────────────────────────────── */
.ag-chat-chips {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 0 12px 8px;
  background: #fbfbfd;
}
.ag-chip {
  background: #fff;
  border: 1px solid var(--ag-chat-line);
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 11.8px; color: var(--ag-chat-ink);
  cursor: pointer;
  font-family: inherit;
  transition: border-color .15s ease, color .15s ease;
}
.ag-chip:hover { border-color: var(--ag-chat-c); color: var(--ag-chat-c); }
.ag-chip-cta { background: var(--ag-chat-c); border-color: var(--ag-chat-c); color: #fff; font-weight: 600; }
.ag-chip-cta:hover { color: #fff; opacity: .92; }

/* ── Composer ─────────────────────────────────────────── */
.ag-chat-form {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 10px;
  border-top: 1px solid var(--ag-chat-line);
  background: #fff;
  flex-shrink: 0;
}
.ag-chat-input {
  flex: 1;
  border: 1px solid var(--ag-chat-line);
  border-radius: 11px;
  padding: 7px 10px;
  font-size: 12.8px;
  font-family: inherit;
  color: var(--ag-chat-ink);
  resize: none;
  max-height: 96px;
  line-height: 1.45;
  outline: 0;
  transition: border-color .15s ease;
}
.ag-chat-input:focus { border-color: var(--ag-chat-c); }
.ag-chat-input::placeholder { color: #a3a9b5; }
.ag-chat-send {
  width: 34px; height: 34px;
  border: 0; border-radius: 10px;
  background: var(--ag-chat-c);
  color: #fff; cursor: pointer;
  display: grid; place-items: center;
  flex-shrink: 0;
  transition: opacity .15s ease;
}
.ag-chat-send:disabled { opacity: .45; cursor: not-allowed; }
.ag-chat-send svg { width: 15px; height: 15px; fill: currentColor; }

.ag-chat-foot a {
  color: var(--ag-chat-c);
  text-decoration: none;
  font-weight: 600;
}
.ag-chat-foot a:hover { text-decoration: underline; }
.ag-chat-foot {
  font-size: 10.5px; color: var(--ag-chat-muted);
  text-align: center; padding: 0 0 8px;
  background: #fff;
}

/* ── Small screens ────────────────────────────────────── */
@media (max-width: 480px) {
  #ag-chat { bottom: 16px; }
  /* responsive.css pulls .whatsapp-float down to 76px on small screens, so
     the right-hand slot moves with it. */
  #ag-chat.ag-right { right: 16px; bottom: 134px; }
  #ag-chat.ag-left  { left: 16px; }
  .ag-chat-panel {
    /* Anchored to the viewport, not to the launcher, so the panel is not
       pushed off-screen by however high the launcher sits. */
    position: fixed;
    inset: auto 8px 84px 8px;
    width: auto; max-width: none;
    height: min(74vh, 540px);
  }
}

/* ── Cookie banner ────────────────────────────────────
   js/main.js publishes the banner's height as --ag-cookie-h and puts
   .ag-cookie-open on <body>; includes/enquiry-popup.php already steps the
   other floats over it on phones. The banner sits bottom-LEFT on desktop
   (left:20px, up to 420px wide), so unlike those right-hand floats this
   launcher is covered at every width, not only on a phone — hence no media
   query on the first rule. */
body.ag-cookie-open #ag-chat.ag-left {
  bottom: calc(20px + var(--ag-cookie-h, 0px) + 12px);
}
@media (max-width: 560px) {
  /* Full-width down here, so the right-hand placement is covered too. */
  body.ag-cookie-open #ag-chat.ag-right {
    bottom: calc(134px + var(--ag-cookie-h, 0px) + 12px);
  }
  body.ag-cookie-open #ag-chat.ag-left {
    bottom: calc(16px + var(--ag-cookie-h, 0px) + 12px);
  }
  /* The panel is anchored to the viewport at this size rather than to the
     launcher, so it does not ride up with it — move it too, or the banner
     covers the composer. */
  body.ag-cookie-open .ag-chat-panel {
    bottom: calc(84px + var(--ag-cookie-h, 0px) + 12px);
  }
}
/* Same easing the other floats use, so they slide together. The panel
   declares `bottom` inside its own transition list instead — a second rule
   naming only `bottom` here would replace that list and cost it its fade. */
#ag-chat { transition: bottom .3s cubic-bezier(.4, 0, .2, 1); }

/* Reduced motion: keep the state changes, drop the movement. */
@media (prefers-reduced-motion: reduce) {
  .ag-chat-panel, .ag-chat-launcher { transition: opacity .12s ease; }
  .ag-msg { animation: none; }
  .ag-typing span { animation-duration: 2s; }
}

/* ══════════════════════════════════════════════════════════
   Voice input
   The button ships hidden and the script reveals it only where the
   browser speech API exists, so [hidden] must win over the flex display.
   ══════════════════════════════════════════════════════════ */
.ag-chat-mic {
  width: 34px; height: 34px;
  border: 0; border-radius: 10px;
  background: rgba(0, 0, 0, .06);
  color: #475569; cursor: pointer;
  display: grid; place-items: center;
  flex-shrink: 0;
  transition: background .15s ease, color .15s ease;
}
.ag-chat-mic[hidden] { display: none; }
.ag-chat-mic:hover { background: rgba(0, 0, 0, .1); }
.ag-chat-mic svg   { width: 16px; height: 16px; fill: currentColor; }

/* Listening: the ring pulses so it is obvious the microphone is live. */
.ag-chat-mic.is-live {
  background: var(--ag-chat-c);
  color: #fff;
  animation: ag-mic-pulse 1.4s ease-out infinite;
}
@keyframes ag-mic-pulse {
  0%   { box-shadow: 0 0 0 0 color-mix(in srgb, var(--ag-chat-c) 55%, transparent); }
  100% { box-shadow: 0 0 0 10px transparent; }
}
@media (prefers-reduced-motion: reduce) {
  .ag-chat-mic.is-live { animation: none; outline: 2px solid var(--ag-chat-c); outline-offset: 2px; }
}

/* ══════════════════════════════════════════════════════════
   Admin-set appearance
   ══════════════════════════════════════════════════════════ */

/* Gradient header. --ag-chat-c2 equals --ag-chat-c when no second colour
   is set, so "gradient" on a single colour stays flat rather than fading
   into something the admin never chose. */
#ag-chat[data-head="gradient"] .ag-chat-head {
  background: linear-gradient(135deg, var(--ag-chat-c) 0%, var(--ag-chat-c2, var(--ag-chat-c)) 100%);
}
#ag-chat[data-head="gradient"] .ag-chat-launcher {
  background: linear-gradient(135deg, var(--ag-chat-c) 0%, var(--ag-chat-c2, var(--ag-chat-c)) 100%);
}

/* Uploaded imagery */
.ag-chat-avatar-img { object-fit: cover; background: rgba(255, 255, 255, .2); }
.ag-chat-launcher .ag-ic-img { width: 24px; height: 24px; object-fit: contain; border-radius: 6px; }

/* Launcher with a label beside the icon */
.ag-chat-launcher.has-label {
  width: auto; height: 48px;
  border-radius: 999px;
  padding: 0 18px 0 14px;
  display: inline-flex; align-items: center; gap: 9px;
}
.ag-chat-launcher-label {
  font-size: 14px; font-weight: 600; white-space: nowrap; line-height: 1;
}
#ag-chat.ag-open .ag-chat-launcher-label { display: none; }
#ag-chat.ag-open .ag-chat-launcher.has-label { width: 48px; padding: 0; }

/* Corner style */
#ag-chat[data-style="soft"]   .ag-chat-panel { border-radius: 12px; }
#ag-chat[data-style="square"] .ag-chat-panel { border-radius: 2px; }
#ag-chat[data-style="square"] .ag-chat-send,
#ag-chat[data-style="square"] .ag-chat-mic   { border-radius: 3px; }
#ag-chat[data-style="square"] .ag-chat-launcher:not(.has-label) { border-radius: 8px; }

/* Panel scale. Capped against the viewport so "large" cannot push the
   panel off a phone screen. */
#ag-chat[data-size="compact"]  .ag-chat-panel { width: min(320px, calc(100vw - 32px)); height: min(440px, calc(100vh - 140px)); }
#ag-chat[data-size="large"]    .ag-chat-panel { width: min(420px, calc(100vw - 32px)); height: min(620px, calc(100vh - 120px)); }
