/**
 * AgentHub Chat Widget — frontend styles
 *
 * Visual system follows the AIQuinta Brand Guidelines 1.0.0:
 *   ElectricBlue  #234EFF   · primary actions, user turns
 *   AquaPulse     #00E4F7   · gradient partner ("IQ Wave"), accents
 *   MidnightCore  #080834   · text, scrim
 *   Type: Tektur (headings) + Quicksand (body)
 *
 * Everything is namespaced under .agenthub-* / #agenthub-* and resets the few
 * properties themes commonly bleed in (box-sizing, font, line-height, margins).
 */

/* ── Design tokens ─────────────────────────────────────────────────────────── */
:root {
  /* brand */
  --ah-electric: #234eff;
  --ah-aqua: #00e4f7;
  --ah-midnight: #080834;

  /* semantic (--agenthub-primary-color is injected from the admin setting) */
  --ah-primary: var(--agenthub-primary-color, #234eff);
  --ah-gradient: linear-gradient(135deg, #234eff 0%, #00e4f7 100%);

  --ah-surface: #ffffff;
  --ah-surface-sunken: #f5f7fc;

  --ah-text: #080834;
  --ah-text-soft: #4b5170;   /* ~7:1 on white  */
  --ah-text-muted: #6b7190;  /* ~4.8:1 on white */

  --ah-border: #e3e7f2;
  --ah-border-strong: #cfd6e8;

  --ah-scrim: rgba(8, 8, 52, 0.55);

  /* shape + rhythm (4/8 scale) */
  --ah-r-sm: 8px;
  --ah-r-md: 12px;
  --ah-r-lg: 16px;
  --ah-r-xl: 22px;
  --ah-measure: 46rem;          /* ≈ 70 characters — the readable line length */

  --ah-shadow-1: 0 1px 2px rgba(8, 8, 52, 0.06), 0 2px 8px rgba(8, 8, 52, 0.06);
  --ah-shadow-2: 0 12px 40px rgba(8, 8, 52, 0.18), 0 2px 8px rgba(8, 8, 52, 0.10);

  --ah-font-head: "Tektur", "Quicksand", ui-sans-serif, system-ui, sans-serif;
  --ah-font-body: "Quicksand", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;

  --ah-ease: cubic-bezier(0.16, 1, 0.3, 1);
}

.agenthub-chat-trigger,
.agenthub-chat-backdrop,
.agenthub-chat-backdrop * {
  box-sizing: border-box;
}

/* ── Launcher ──────────────────────────────────────────────────────────────── */
.agenthub-chat-trigger {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 99998;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;                 /* ≥44px touch target */
  padding: 12px 20px;
  border: 0;
  border-radius: 999px;
  background: var(--ah-gradient);
  color: #fff;
  font-family: var(--ah-font-body);
  font-size: 15px;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(35, 78, 255, 0.35);
  transition: transform 180ms var(--ah-ease), box-shadow 180ms var(--ah-ease);
}
.agenthub-chat-trigger:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 28px rgba(35, 78, 255, 0.45);
}
.agenthub-chat-trigger:focus-visible {
  outline: 3px solid var(--ah-aqua);
  outline-offset: 2px;
}
.agenthub-chat-trigger svg { flex: none; }

/* ── Scrim + modal shell ───────────────────────────────────────────────────── */
.agenthub-chat-backdrop {
  position: fixed;
  inset: 0;
  z-index: 99999;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--ah-scrim);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  font-family: var(--ah-font-body);
  font-weight: 400;                 /* inherited baseline — themes often push 500/600 */
  font-style: normal;
  letter-spacing: normal;
  text-transform: none;
}
.agenthub-chat-backdrop.open {
  display: flex;
  animation: ah-fade 160ms var(--ah-ease);
}

.agenthub-chat-modal {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 960px;                 /* was near-full-bleed — cap it */
  height: min(86vh, 780px);
  overflow: hidden;
  background: var(--ah-surface);
  border-radius: var(--ah-r-xl);
  box-shadow: var(--ah-shadow-2);
  animation: ah-rise 220ms var(--ah-ease);
}

.agenthub-chat-close {
  position: absolute;
  top: 14px;
  right: 14px;
  z-index: 5;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ah-text-muted);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  transition: background 150ms var(--ah-ease), color 150ms var(--ah-ease);
}
.agenthub-chat-close:hover { background: var(--ah-surface-sunken); color: var(--ah-text); }
.agenthub-chat-close:focus-visible { outline: 3px solid var(--ah-primary); outline-offset: 1px; }

@keyframes ah-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes ah-rise {
  from { opacity: 0; transform: translateY(12px) scale(0.985); }
  to   { opacity: 1; transform: none; }
}

/* ── Welcome view ──────────────────────────────────────────────────────────── */
.agenthub-welcome-view {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 100%;
  padding: 40px 32px;
  overflow-y: auto;
  text-align: center;
}

.agenthub-brand { margin-bottom: 4px; }
.agenthub-brand-logo { max-height: 52px; max-width: 220px; object-fit: contain; }
.agenthub-brand-text {
  font-family: var(--ah-font-head);
  font-size: 30px;
  font-weight: 700;
  color: var(--ah-text);
  letter-spacing: -0.01em;
}
.agenthub-brand-desc {
  max-width: 34rem;
  margin: 0 0 24px;
  color: var(--ah-text-soft);
  font-size: 15px;
  font-weight: 400;
  line-height: 1.6;
}

.agenthub-search-wrap {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  width: 100%;
  max-width: 40rem;
  padding: 6px 6px 6px 18px;
  background: var(--ah-surface);
  border: 1.5px solid var(--ah-border-strong);
  border-radius: var(--ah-r-lg);
  box-shadow: var(--ah-shadow-1);
  transition: border-color 150ms var(--ah-ease), box-shadow 150ms var(--ah-ease);
}
.agenthub-search-wrap:focus-within {
  border-color: var(--ah-primary);
  box-shadow: 0 0 0 4px rgba(35, 78, 255, 0.14);
}

#agenthub-chat-modal .agenthub-search-input,
#agenthub-chat-modal .agenthub-conv-input {
  flex: 1;
  border: 0;
  outline: 0;
  padding: 12px 0;
  margin: 0;
  background: transparent;
  color: var(--ah-text);
  font-family: var(--ah-font-body);
  font-size: 16px;                  /* ≥16px: no iOS auto-zoom */
  line-height: 1.5;
  resize: none;
  max-height: 120px;
  box-shadow: none;
}
#agenthub-chat-modal .agenthub-search-input::placeholder,
#agenthub-chat-modal .agenthub-conv-input::placeholder { color: var(--ah-text-muted); }

.agenthub-search-send,
.agenthub-conv-send {
  flex: none;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 12px;
  background: var(--ah-gradient);
  color: #fff;
  cursor: pointer;
  transition: transform 150ms var(--ah-ease), opacity 150ms var(--ah-ease);
}
.agenthub-search-send:hover,
.agenthub-conv-send:hover { transform: scale(1.05); }
.agenthub-search-send:focus-visible,
.agenthub-conv-send:focus-visible { outline: 3px solid var(--ah-aqua); outline-offset: 2px; }
.agenthub-search-send:disabled,
.agenthub-conv-send:disabled { opacity: 0.45; cursor: default; transform: none; }

/* Trending questions */
.agenthub-trending-section { width: 100%; max-width: 40rem; margin-top: 28px; }
.agenthub-trending-label {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin-bottom: 12px;
  color: var(--ah-text-muted);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
}
/* Wrap onto as many rows as needed — every question stays fully readable and
   one click away (a horizontal scroller clipped them mid-word). */
.agenthub-trending-cards {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}
.agenthub-trending-card {
  flex: 0 1 auto;
  max-width: 100%;
  min-height: 44px;
  padding: 10px 16px;
  border: 1px solid var(--ah-border);
  border-radius: 999px;
  background: var(--ah-surface-sunken);
  color: var(--ah-text-soft);
  font-family: var(--ah-font-body);
  font-size: 13.5px;
  font-weight: 600;
  line-height: 1.4;
  text-align: center;
  white-space: normal;              /* a long question wraps instead of being cut */
  cursor: pointer;
  transition: border-color 150ms var(--ah-ease), color 150ms var(--ah-ease),
              background 150ms var(--ah-ease), transform 150ms var(--ah-ease);
}
.agenthub-trending-card:hover {
  border-color: var(--ah-primary);
  background: #fff;
  color: var(--ah-primary);
  transform: translateY(-1px);
}
.agenthub-trending-card:focus-visible { outline: 3px solid var(--ah-primary); outline-offset: 2px; }

.agenthub-trending-card:disabled { opacity: 0.45; cursor: default; transform: none; }

/* ── Conversation view ─────────────────────────────────────────────────────── */
.agenthub-conv-view {
  display: flex;
  flex-direction: column;           /* header + panel (the old empty sidebar is gone) */
  height: 100%;
  min-height: 0;
}

.agenthub-conv-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex: none;
  padding: 14px 64px 14px 24px;     /* right padding clears the close button */
  border-bottom: 1px solid var(--ah-border);
  background: var(--ah-surface);
}
.agenthub-conv-brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.agenthub-conv-logo { max-height: 28px; max-width: 130px; object-fit: contain; }
.agenthub-conv-title {
  font-family: var(--ah-font-head);
  font-size: 15px;
  font-weight: 700;
  color: var(--ah-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Ghost action — reads as a header control, not a stray outlined pill */
.agenthub-new-chat {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  padding: 8px 14px;
  border: 0;
  border-radius: var(--ah-r-md);
  background: transparent;
  color: var(--ah-primary);
  font-family: var(--ah-font-body);
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background 150ms var(--ah-ease);
}
.agenthub-new-chat:hover { background: rgba(35, 78, 255, 0.09); }
.agenthub-new-chat:focus-visible { outline: 3px solid var(--ah-primary); outline-offset: 2px; }
.agenthub-new-chat svg { flex: none; }

.agenthub-main-panel {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.agenthub-chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 28px 24px 8px;
  scroll-behavior: smooth;
}
/* THE readability fix: cap the measure and centre the thread */
.agenthub-chat-messages > * {
  max-width: var(--ah-measure);
  margin-left: auto;
  margin-right: auto;
}

/* Timestamp — quiet, never competes with the message */
.agenthub-msg-time {
  color: var(--ah-text-muted);
  font-size: 11px;
  font-weight: 500;
  line-height: 1.4;
  font-variant-numeric: tabular-nums;
}

/* User turn — right-aligned brand bubble (no avatar; the bubble carries identity) */
.agenthub-user-message {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  margin: 0 0 20px;
}
.agenthub-user-avatar { display: none; }
.agenthub-user-text {
  max-width: 80%;
  padding: 11px 16px;
  border-radius: var(--ah-r-lg) var(--ah-r-lg) 4px var(--ah-r-lg);
  background: var(--ah-primary);
  color: #fff;
  font-size: 15px;
  font-weight: 500;
  line-height: 1.55;
  overflow-wrap: anywhere;
}
.agenthub-user-message .agenthub-msg-time { margin: 4px 2px 0 0; }

/* Bot turn — assistant avatar + soft card */
.agenthub-bot-message {
  position: relative;
  margin: 0 0 26px;
  padding-left: 44px;               /* room for the avatar gutter */
}
.agenthub-bot-message::before {
  /* The configured avatar image (--ah-avatar, injected inline from the setting);
     falls back to the brand IQ Wave gradient when none is set. */
  content: "";
  position: absolute;
  left: 0;
  top: 2px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background-color: #fff;           /* keeps transparent logos legible */
  background-image: var(--ah-avatar, var(--ah-gradient));
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  box-shadow: 0 0 0 1px rgba(8, 8, 52, 0.08), 0 2px 6px rgba(8, 8, 52, 0.12);
}
.agenthub-bot-message .agenthub-msg-time { margin: 6px 0 0 2px; }

#agenthub-chat-modal .agenthub-bot-content {
  padding: 14px 18px;
  border-radius: 4px var(--ah-r-lg) var(--ah-r-lg) var(--ah-r-lg);
  background: var(--ah-surface-sunken);
  color: var(--ah-text);
  font-family: var(--ah-font-body);
  font-size: 15px;
  font-weight: 400;                 /* themes often set 500/600 on body/p — pin it */
  line-height: 1.65;
  overflow-wrap: anywhere;
}

/* Markdown inside a bot answer.
   These are prefixed with the modal's id on purpose: the host theme styles bare
   `p`/`li`/`ul` too, and at equal specificity whichever loads last wins. The id
   (1,0,0) puts the widget's own typography beyond reach of that cascade — without
   it the theme was making every answer bold and stripping the list bullets. */
#agenthub-chat-modal .agenthub-bot-content p,
#agenthub-chat-modal .agenthub-bot-content li,
#agenthub-chat-modal .agenthub-bot-content td,
#agenthub-chat-modal .agenthub-bot-content blockquote {
  font-weight: 400;
}
#agenthub-chat-modal .agenthub-bot-content ul { list-style: disc outside; }
#agenthub-chat-modal .agenthub-bot-content ol { list-style: decimal outside; }
#agenthub-chat-modal .agenthub-bot-content li::marker { color: var(--ah-text-muted); }

#agenthub-chat-modal .agenthub-bot-content > *:first-child { margin-top: 0; }
#agenthub-chat-modal .agenthub-bot-content > *:last-child { margin-bottom: 0; }
#agenthub-chat-modal .agenthub-bot-content p { margin: 0 0 12px; }
#agenthub-chat-modal .agenthub-bot-content h2,
#agenthub-chat-modal .agenthub-bot-content h3,
#agenthub-chat-modal .agenthub-bot-content h4 {
  margin: 18px 0 8px;
  font-family: var(--ah-font-head);
  font-weight: 700;
  line-height: 1.3;
  color: var(--ah-text);
}
#agenthub-chat-modal .agenthub-bot-content h2 { font-size: 18px; }
#agenthub-chat-modal .agenthub-bot-content h3 { font-size: 16px; }
#agenthub-chat-modal .agenthub-bot-content h4 { font-size: 15px; }
#agenthub-chat-modal .agenthub-bot-content ul,
#agenthub-chat-modal .agenthub-bot-content ol { margin: 0 0 12px; padding-left: 22px; }
#agenthub-chat-modal .agenthub-bot-content li { margin: 0 0 5px; }
#agenthub-chat-modal .agenthub-bot-content a { color: var(--ah-primary); text-decoration: underline; text-underline-offset: 2px; }
#agenthub-chat-modal .agenthub-bot-content strong { font-weight: 700; color: var(--ah-text); }
#agenthub-chat-modal .agenthub-bot-content code {
  padding: 2px 6px;
  border-radius: 5px;
  background: rgba(8, 8, 52, 0.07);
  font-family: ui-monospace, "SFMono-Regular", Menlo, monospace;
  font-size: 0.88em;
}
#agenthub-chat-modal .agenthub-bot-content pre {
  margin: 0 0 12px;
  padding: 12px 14px;
  border-radius: var(--ah-r-md);
  background: var(--ah-midnight);
  color: #e7ecff;
  overflow-x: auto;
}
#agenthub-chat-modal .agenthub-bot-content pre code { background: none; padding: 0; color: inherit; }
#agenthub-chat-modal .agenthub-bot-content blockquote {
  margin: 0 0 12px;
  padding: 2px 0 2px 12px;
  border-left: 3px solid var(--ah-border-strong);
  color: var(--ah-text-soft);
}
#agenthub-chat-modal .agenthub-bot-content hr { margin: 16px 0; border: 0; border-top: 1px solid var(--ah-border); }
#agenthub-chat-modal .agenthub-bot-content table { width: 100%; border-collapse: collapse; margin: 0 0 12px; font-size: 14px; }
#agenthub-chat-modal .agenthub-bot-content th,
#agenthub-chat-modal .agenthub-bot-content td { padding: 8px 10px; border: 1px solid var(--ah-border); text-align: left; }
#agenthub-chat-modal .agenthub-bot-content th { background: rgba(35, 78, 255, 0.06); font-weight: 700; }

/* Typing indicator */
.agenthub-typing { display: inline-flex; gap: 5px; padding: 3px 0; }
.agenthub-typing-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--ah-primary);
  opacity: 0.4;
  animation: ah-blink 1.4s infinite both;
}
.agenthub-typing-dot:nth-child(2) { animation-delay: 0.2s; }
.agenthub-typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes ah-blink {
  0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

/* Follow-up suggestions */
.agenthub-suggestions { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0 0 44px; }
.agenthub-suggestion-btn {
  min-height: 40px;
  padding: 9px 14px;
  border: 1px solid var(--ah-border-strong);
  border-radius: 999px;
  background: var(--ah-surface);
  color: var(--ah-text-soft);
  font-family: var(--ah-font-body);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 150ms var(--ah-ease), color 150ms var(--ah-ease);
}
.agenthub-suggestion-btn:hover { border-color: var(--ah-primary); color: var(--ah-primary); }
.agenthub-suggestion-btn:focus-visible { outline: 3px solid var(--ah-primary); outline-offset: 2px; }

.agenthub-response-time { margin-top: 6px; color: var(--ah-text-muted); font-size: 11px; }

/* ── Composer ──────────────────────────────────────────────────────────────── */
.agenthub-conv-input-area {
  flex: none;
  padding: 12px 24px 16px;
  border-top: 1px solid var(--ah-border);
  background: var(--ah-surface);
}
.agenthub-conv-input-box {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: var(--ah-measure);
  margin: 0 auto;
  padding: 6px 6px 6px 18px;
  border: 1.5px solid var(--ah-border-strong);
  border-radius: var(--ah-r-lg);
  background: var(--ah-surface);
  transition: border-color 150ms var(--ah-ease), box-shadow 150ms var(--ah-ease);
}
.agenthub-conv-input-box:focus-within {
  border-color: var(--ah-primary);
  box-shadow: 0 0 0 4px rgba(35, 78, 255, 0.14);
}
.agenthub-footer-disclaimer {
  max-width: var(--ah-measure);
  margin: 8px auto 0;
  color: var(--ah-text-muted);
  font-size: 11px;
  font-weight: 400;
  line-height: 1.4;
  text-align: center;
}

/* ── Responsive ────────────────────────────────────────────────────────────── */
@media (max-width: 782px) {
  .agenthub-chat-backdrop { padding: 0; }
  .agenthub-chat-modal {
    max-width: none;
    height: 100%;
    height: 100dvh;
    border-radius: 0;
  }
  .agenthub-conv-header { padding: 12px 56px 12px 16px; }
  .agenthub-chat-messages { padding: 20px 16px 8px; }
  .agenthub-conv-input-area { padding: 10px 16px calc(12px + env(safe-area-inset-bottom)); }
  .agenthub-welcome-view { padding: 32px 20px; justify-content: flex-start; }
  .agenthub-brand-text { font-size: 24px; }
  .agenthub-user-text { max-width: 88%; }
  .agenthub-suggestions { margin-left: 0; }
  .agenthub-chat-trigger { right: 16px; bottom: 16px; }
}

/* ── Motion preference ─────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  .agenthub-chat-backdrop.open,
  .agenthub-chat-modal { animation: none; }
  .agenthub-chat-messages { scroll-behavior: auto; }
  .agenthub-typing-dot { animation: none; opacity: 0.55; }
  .agenthub-chat-trigger,
  .agenthub-trending-card,
  .agenthub-search-send,
  .agenthub-conv-send { transition: none; }
  .agenthub-chat-trigger:hover,
  .agenthub-trending-card:hover,
  .agenthub-search-send:hover,
  .agenthub-conv-send:hover { transform: none; }
}
