/* Widget iframe UI. Self-contained — the host page's CSS can't reach in here,
   and nothing here leaks out (iframe boundary). Accent set via --accent from
   the ?color= query param (validated in embed.js). */
:root {
  --accent: #7c5cff;
  --bg: #ffffff;
  --ink: #1a1d24;
  --muted: #6b7280;
  --bubble-bot: #f1f2f6;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
}

#shell { display: flex; flex-direction: column; height: 100%; }

/* ---- Header ---- */
#header {
  position: relative;
  display: flex; align-items: center; gap: 10px;
  padding: 14px 16px;
  background: var(--accent); color: #fff;
  background-size: cover; background-position: center;
}
/* Banner image (M43/M47): when a header_image is set, embed.js adds .has-banner
   and sets the background image. Legibility over an ARBITRARY photo (incl. light
   or busy ones) is guaranteed by a dark scrim + a soft accent tint at the bottom
   + text/icon shadows — not by hoping the image is dark. Header grows taller like
   the reference screenshots. */
#header.has-banner {
  padding: 18px 16px 20px;
  align-items: flex-start;
}
#header.has-banner::before {
  content: ""; position: absolute; inset: 0; z-index: 0;
  /* Darker at the top where the name/actions sit, easing downward. Strong enough
     that white text passes contrast even on a pure-white image. Single plain
     gradient → valid in every browser (no color-mix that could void the rule). */
  background: linear-gradient(180deg,
    rgba(0,0,0,.52) 0%, rgba(0,0,0,.40) 50%, rgba(0,0,0,.26) 100%);
}
#header > * { position: relative; z-index: 1; }
/* Drop-shadows so text + SVG icons stay crisp regardless of what's behind them. */
#header.has-banner #botName,
#header.has-banner #botSub { text-shadow: 0 1px 4px rgba(0,0,0,.6); }
#header.has-banner #headerActions button,
#header.has-banner .dot,
#header.has-banner #demoTag {
  filter: drop-shadow(0 1px 2px rgba(0,0,0,.55));
}
.dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: #4ade80; flex: 0 0 auto; margin-top: 5px;
}
.dot.off { background: #f59e0b; }
#who { flex: 1; min-width: 0; }
#botName { font-weight: 700; font-size: 15px; }
#header.has-banner #botName { font-size: 18px; text-shadow: 0 1px 3px rgba(0,0,0,.4); }
#botSub { font-size: 12px; opacity: .85; }
#demoTag {
  font-size: 11px; font-weight: 800; letter-spacing: .06em;
  background: rgba(255,255,255,.22); border-radius: 6px; padding: 3px 7px;
  align-self: center;
}
/* Header action buttons (speaker toggle, restart, close) */
#headerActions { display: flex; align-items: center; gap: 2px; flex: 0 0 auto; }
#headerActions button {
  border: 0; background: transparent; color: #fff; cursor: pointer;
  padding: 6px; line-height: 0; opacity: .85; border-radius: 8px;
  display: inline-flex; align-items: center; justify-content: center;
}
#headerActions button:hover { opacity: 1; background: rgba(255,255,255,.16); }
#closeBtn { font-size: 16px; line-height: 1; }
#speakerBtn[hidden] { display: none; }
/* Speaker toggle shows the "off/muted" icon by default; .on swaps to the
   sound-waves icon once the visitor opts into audio. */
#speakerBtn .ic-on { display: none; }
#speakerBtn .ic-off { display: inline; }
#speakerBtn.on .ic-on { display: inline; }
#speakerBtn.on .ic-off { display: none; }
#speakerBtn.on { opacity: 1; background: rgba(255,255,255,.22); }

/* ---- Messages ---- */
#messages {
  flex: 1; overflow-y: auto; padding: 14px;
  display: flex; flex-direction: column; gap: 10px;
}
.msg {
  max-width: 82%; padding: 10px 13px; border-radius: 14px;
  line-height: 1.45; white-space: pre-wrap; word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg.user {
  align-self: flex-end;
  background: var(--accent); color: #fff;
  border-bottom-right-radius: 4px;
}
.msg.bot {
  align-self: flex-start;
  background: var(--bubble-bot); color: var(--ink);
  border-bottom-left-radius: 4px;
}

/* "Thinking" indicator — three bouncing dots shown from the moment the visitor
   sends until the first reply token arrives, so the wait never feels dead. */
.msg.dots { display: inline-flex; gap: 5px; padding: 13px 14px; }
.msg.dots span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--muted);
  opacity: .4; animation: bounce 1.2s ease-in-out infinite;
}
.msg.dots span:nth-child(2) { animation-delay: .18s; }
.msg.dots span:nth-child(3) { animation-delay: .36s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .4; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* Streaming cursor on the bot bubble currently being filled. */
.msg.bot.typing::after {
  content: "▍"; margin-left: 2px; color: var(--muted);
  animation: blink 1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* ---- Live caption (voice) ---- */
#liveCaption {
  margin: 0 14px 6px; padding: 8px 12px; border-radius: 10px;
  background: var(--bubble-bot); color: var(--muted);
  font-size: 13px; font-style: italic;
}

/* ---- Lead-capture card (M37) ---- */
.msg.lead-card {
  align-self: stretch; max-width: 100%;
  background: #fff; border: 1px solid #e0e3ea;
  display: flex; flex-direction: column; gap: 8px;
}
.lead-prompt { font-weight: 600; font-size: 14px; }
.lead-input {
  border: 1px solid #d8dbe2; border-radius: 8px;
  padding: 9px 11px; font-size: 16px; font-family: inherit; outline: none;
}
.lead-input:focus { border-color: var(--accent); }
.lead-err { color: #e5484d; font-size: 12px; }
.lead-actions { display: flex; gap: 8px; align-items: center; margin-top: 2px; }
.lead-submit {
  border: 0; background: var(--accent); color: #fff; cursor: pointer;
  border-radius: 999px; padding: 8px 18px; font-size: 14px; font-family: inherit;
}
.lead-skip {
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
  font-size: 13px; font-family: inherit;
}
.lead-skip:hover { color: var(--ink); }
.msg.lead-thanks { font-weight: 600; }
.lead-chip {
  border: 1px solid var(--accent); background: #fff; color: var(--accent);
  border-radius: 999px; padding: 7px 13px; font-size: 13px;
  cursor: pointer; font-family: inherit; font-weight: 600;
}

/* ---- Human-handoff header button (M41) ---- */
.handoff-btn {
  border: 1px solid rgba(255,255,255,.5); background: transparent; color: #fff;
  border-radius: 999px; padding: 4px 11px; font-size: 12px; cursor: pointer;
  font-family: inherit; white-space: nowrap;
}
.handoff-btn:hover { background: rgba(255,255,255,.16); }

/* ---- Clickable links in answers (M41) ---- */
.msg-link { color: var(--accent); text-decoration: underline; word-break: break-all; }
.row.user .msg-link, .msg.user .msg-link { color: #fff; }

/* ---- Branding footer (hidden for white-label tenants) ---- */
#brand {
  text-align: center; font-size: 11px; color: var(--muted);
  padding: 4px 0 7px;
}
#brand a { color: var(--muted); text-decoration: none; }
#brand a:hover { color: var(--accent); }

/* ---- Adaptive FAQ follow-up chips (M46), shown inline after an answer ---- */
.faq-suggest {
  align-self: flex-start; max-width: 100%;
  display: flex; flex-wrap: wrap; gap: 6px; margin-top: 2px;
}
.faq-suggest-label {
  width: 100%; font-size: 11px; color: var(--muted); margin-bottom: 1px;
}
.faq-chip {
  border: 1px solid var(--accent); background: #fff; color: var(--accent);
  border-radius: 999px; padding: 6px 12px; font-size: 12.5px; line-height: 1.25;
  cursor: pointer; font-family: inherit; text-align: left;
}
.faq-chip:hover { background: var(--accent); color: #fff; }
.faq-chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- Suggestion chips ---- */
#chips { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 14px 8px; }
#chips button {
  border: 1px solid #d8dbe2; background: #fff; color: var(--ink);
  border-radius: 999px; padding: 7px 13px; font-size: 13px;
  cursor: pointer; font-family: inherit;
}
#chips button:hover { border-color: var(--accent); color: var(--accent); }

/* ---- Composer ---- */
#composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 12px; border-top: 1px solid #e7e9ef;
}
/* Auto-growing multi-line input (M43): a <textarea> that starts one line tall
   and grows with the text (embed.js sets its height) up to a cap, then scrolls —
   so a long message is fully visible instead of hidden in a single line. */
#input {
  flex: 1; border: 1px solid #d8dbe2; border-radius: 20px;
  padding: 10px 15px; font-family: inherit; outline: none;
  /* 16px: under this, iOS Safari zooms the page on focus — jarring in an iframe. */
  font-size: 16px; line-height: 1.4;
  resize: none; overflow-y: auto;
  min-height: 42px; max-height: 132px;   /* ~1 to ~5 lines, then scroll */
}
#input:focus { border-color: var(--accent); }
#input:disabled { background: #f4f5f8; }

/* Visible keyboard focus — the host page's reset is unknown, so don't rely on
   the UA default outline. */
#input:focus-visible,
#chips button:focus-visible,
#sendBtn:focus-visible,
#micBtn:focus-visible,
.csat button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

/* Respect reduced-motion: kill the decorative animations for vestibular users. */
@media (prefers-reduced-motion: reduce) {
  .msg.bot.typing::after,
  .msg.dots span,
  #micBtn.recording { animation: none; }
}
#sendBtn, #micBtn {
  flex: 0 0 auto; width: 42px; height: 42px; border-radius: 50%;
  border: 0; cursor: pointer; display: inline-flex;
  align-items: center; justify-content: center;
}
#sendBtn { background: var(--accent); color: #fff; }
#sendBtn:disabled, #sendBtn.dim { opacity: .5; }
#sendBtn:disabled { cursor: default; }
#micBtn { background: #eef0f5; color: var(--ink); }
/* The display:inline-flex above would defeat the `hidden` attribute (hidden
   only applies display:none at UA-stylesheet priority) — restate it. */
#micBtn[hidden] { display: none; }
#micBtn.recording { background: #e5484d; color: #fff; animation: pulse 1.2s infinite; }
#micBtn:disabled { opacity: .5; cursor: default; }
@keyframes pulse { 50% { transform: scale(1.08); } }
