/* ==========================================================================
 * Unified chat widget
 * --------------------------------------------------------------------------
 * Shared stylesheet consumed by:
 *   - Dashboard web-chat preview (frontends/app/js/pages/web-chat/)
 *   - Onboarding wizard chat preview (frontends/agent_onboarding.html)
 *   - Embed widget shipped to third-party sites (frontends/embed/embed-widget.js)
 *
 * The embed widget lives in a shadow DOM and must stay self-contained, so
 * its script inlines a copy of this block. Treat this file as the
 * source-of-truth: if you change a rule here, mirror it in embed-widget.js
 * CSS() (a comment at the top of that function points here).
 *
 * Theming: the widget takes a `--cw-brand` custom property from its
 * parent (set inline, per-instance). Light/dark is driven by a
 * `data-theme="dark"` attribute on the .chat-widget root. Everything
 * else is derived so a host page only has to set the brand colour.
 * ========================================================================== */

.chat-widget {
  /* theme tokens — overridable per-instance */
  --cw-brand: #111;
  --cw-brand-contrast: #fff;
  --cw-bg: #ffffff;
  --cw-body-bg: #fafaf8;
  --cw-border: #e8e4de;
  --cw-text: #1f1f1f;
  --cw-text-muted: #6b6b6b;
  --cw-bubble-agent-bg: #ffffff;
  --cw-bubble-agent-text: #1f1f1f;
  --cw-bubble-agent-border: #e8e4de;
  --cw-bubble-user-bg: #111111;
  --cw-bubble-user-text: #ffffff;
  --cw-input-bg: #ffffff;
  --cw-input-text: #1f1f1f;
  --cw-shadow: 0 18px 48px rgba(17, 17, 17, 0.14), 0 2px 8px rgba(17, 17, 17, 0.06);
  --cw-radius: 20px;
  --cw-radius-bubble: 18px;

  display: flex;
  flex-direction: column;
  background: var(--cw-bg);
  border: 1px solid var(--cw-border);
  border-radius: var(--cw-radius);
  overflow: hidden;
  box-shadow: var(--cw-shadow);
  font-family: "Avenir Next", "Helvetica Neue", "Segoe UI", system-ui, sans-serif;
  color: var(--cw-text);
  min-width: 0;
}

.chat-widget[data-theme="dark"] {
  --cw-bg: #1a1a1a;
  --cw-body-bg: #141414;
  --cw-border: #2c2c2c;
  --cw-text: #f0f0f0;
  --cw-text-muted: #a0a0a0;
  --cw-bubble-agent-bg: #242424;
  --cw-bubble-agent-text: #f0f0f0;
  --cw-bubble-agent-border: #333333;
  --cw-bubble-user-bg: #f0f0f0;
  --cw-bubble-user-text: #0f0f0f;
  --cw-input-bg: #1e1e1e;
  --cw-input-text: #f0f0f0;
  --cw-shadow: 0 20px 56px rgba(0, 0, 0, 0.55), 0 2px 10px rgba(0, 0, 0, 0.4);
}

/* ── Header ───────────────────────────────────────────────────── */

.chat-widget__header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--cw-border);
  background: var(--cw-bg);
  flex-shrink: 0;
}

.chat-widget__avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--cw-brand);
  color: var(--cw-brand-contrast);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: .02em;
  flex-shrink: 0;
}

.chat-widget__info {
  flex: 1;
  min-width: 0;
}

.chat-widget__name {
  font-weight: 700;
  font-size: 15px;
  color: var(--cw-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-widget__sub {
  font-size: 11px;
  color: var(--cw-text-muted);
  margin-top: 2px;
}

.chat-widget__close {
  background: transparent;
  border: none;
  color: var(--cw-text-muted);
  font-size: 22px;
  line-height: 1;
  padding: 4px 8px;
  cursor: pointer;
  border-radius: 8px;
  transition: background .15s ease, color .15s ease;
}

.chat-widget__close:hover { background: var(--cw-border); color: var(--cw-text); }

/* ── Body ─────────────────────────────────────────────────────── */

.chat-widget__body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 18px 20px;
  background: var(--cw-body-bg);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Subtle scrollbar styling — optional, progressive */
.chat-widget__body::-webkit-scrollbar { width: 8px; }
.chat-widget__body::-webkit-scrollbar-thumb { background: var(--cw-border); border-radius: 4px; }
.chat-widget__body::-webkit-scrollbar-track { background: transparent; }

/* ── Messages ─────────────────────────────────────────────────── */

.chat-widget__msg {
  display: flex;
  animation: cw-msg-in .28s ease-out both;
}

.chat-widget__msg--user { justify-content: flex-end; }
.chat-widget__msg--agent { justify-content: flex-start; }

.chat-widget__bubble {
  max-width: 82%;
  padding: 12px 16px;
  border-radius: var(--cw-radius-bubble);
  font-size: 14px;
  line-height: 1.55;
  word-wrap: break-word;
  white-space: pre-wrap;
}

.chat-widget__bubble--agent {
  background: var(--cw-bubble-agent-bg);
  color: var(--cw-bubble-agent-text);
  border: 1px solid var(--cw-bubble-agent-border);
  border-bottom-left-radius: 6px;
}

.chat-widget__bubble--user {
  background: var(--cw-bubble-user-bg);
  color: var(--cw-bubble-user-text);
  border-bottom-right-radius: 6px;
}

/* ── Typing dots ─────────────────────────────────────────────── */

.chat-widget__typing {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 2px;
}

.chat-widget__typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--cw-text-muted);
  opacity: .6;
  animation: cw-typing 1.2s ease-in-out infinite;
}

.chat-widget__typing span:nth-child(2) { animation-delay: .15s; }
.chat-widget__typing span:nth-child(3) { animation-delay: .3s; }

@keyframes cw-typing {
  0%, 60%, 100% { transform: translateY(0); opacity: .4; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* ── Input row ───────────────────────────────────────────────── */

.chat-widget__input-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-top: 1px solid var(--cw-border);
  background: var(--cw-bg);
  flex-shrink: 0;
}

.chat-widget__input {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--cw-border);
  border-radius: 12px;
  padding: 11px 16px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  background: var(--cw-input-bg);
  color: var(--cw-input-text);
  transition: border-color .15s ease, box-shadow .15s ease;
}

.chat-widget__input::placeholder { color: var(--cw-text-muted); }

.chat-widget__input:focus {
  border-color: var(--cw-brand);
  box-shadow: 0 0 0 3px rgba(0, 0, 0, .06);
}

.chat-widget__send {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  background: var(--cw-brand);
  color: var(--cw-brand-contrast);
  display: grid;
  place-items: center;
  flex-shrink: 0;
  transition: transform .18s ease, opacity .18s ease;
}

.chat-widget__send:hover { transform: scale(1.05); }
.chat-widget__send:active { transform: scale(.95); }
.chat-widget__send:disabled { opacity: .4; cursor: not-allowed; transform: none; }

/* ── Open/close animation (embed FAB panel) ───────────────────── */

.chat-widget--panel {
  transform-origin: bottom right;
  transition: transform .28s cubic-bezier(.2, .9, .3, 1.2), opacity .22s ease;
}

.chat-widget--panel.cw-hidden {
  opacity: 0;
  pointer-events: none;
  transform: scale(.85) translateY(24px);
}

/* ── Message append animation ────────────────────────────────── */

@keyframes cw-msg-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ── Reduced-motion: respect the user's wishes ────────────────── */

@media (prefers-reduced-motion: reduce) {
  .chat-widget__msg,
  .chat-widget--panel,
  .chat-widget__send,
  .chat-widget__close {
    animation: none !important;
    transition: none !important;
  }
  .chat-widget__typing span { animation: none !important; opacity: .7; }
}
