/*
 * CDD AI Assistant — Widget styles
 *
 * Side drawer that slides in from the right edge. Light theme matching
 * collaborativedrug.com (Montserrat, navy text #020B31, brand blue #1262B3).
 *
 * Scoped under #cdd-ai-assistant — every selector is prefixed so styles
 * never leak onto the host page. The host page is HubSpot, which has its
 * own CSS that we must not interfere with.
 *
 * Designed for desktop and mobile (responsive breakpoint at 768px).
 */

#cdd-ai-assistant {
  /* CDD brand palette — easy to override */
  --cdd-text: #020B31;
  --cdd-text-muted: #5a607a;
  --cdd-bg: #ffffff;
  --cdd-bg-soft: #f6f7fb;
  --cdd-border: #e3e6ee;
  --cdd-brand: #1262B3;
  --cdd-brand-hover: #0e4f93;
  --cdd-brand-soft: rgba(18, 98, 179, 0.08);
  --cdd-success: #1f8a5a;
  --cdd-error: #c2424b;
  --cdd-warn-bg: #fff8e1;
  --cdd-warn-border: #d4a72c;

  /* Sizing */
  --cdd-drawer-width: 420px;
  --cdd-bubble-size: 56px;
  --cdd-radius: 8px;

  /* Typography */
  --cdd-font: Montserrat, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --cdd-font-size: 14px;
  --cdd-line-height: 1.55;

  /* Z-index — high enough to clear HubSpot's own elements but not the universe */
  --cdd-z: 2147483000;

  font-family: var(--cdd-font);
  font-size: var(--cdd-font-size);
  line-height: var(--cdd-line-height);
  color: var(--cdd-text);
}

/* All children inherit our font/color rather than HubSpot's defaults */
#cdd-ai-assistant,
#cdd-ai-assistant * {
  box-sizing: border-box;
}

#cdd-ai-assistant button {
  font-family: inherit;
  font-size: inherit;
  cursor: pointer;
}

/* ---------------- Bubble (closed state) ---------------- */

#cdd-ai-assistant .cdd-bubble {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: var(--cdd-bubble-size);
  height: var(--cdd-bubble-size);
  border-radius: 50%;
  background: var(--cdd-brand);
  color: #fff;
  border: none;
  box-shadow: 0 4px 16px rgba(2, 11, 49, 0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: var(--cdd-z);
  transition: transform 0.15s ease, background 0.15s ease;
}
#cdd-ai-assistant .cdd-bubble:hover {
  background: var(--cdd-brand-hover);
  transform: translateY(-2px);
}
#cdd-ai-assistant .cdd-bubble:focus-visible {
  outline: 3px solid var(--cdd-brand);
  outline-offset: 3px;
}
#cdd-ai-assistant .cdd-bubble svg {
  width: 24px;
  height: 24px;
  fill: currentColor;
}
#cdd-ai-assistant .cdd-bubble[aria-hidden="true"] {
  display: none;
}

/* ---------------- Drawer ---------------- */

#cdd-ai-assistant .cdd-drawer {
  position: fixed;
  top: 0;
  right: 0;
  /* Use 100dvh on browsers that support it so iOS keyboard doesn't
     push the composer below the visible area. Falls back to 100vh
     when dvh isn't supported. */
  height: 100vh;
  height: 100dvh;
  width: var(--cdd-drawer-width);
  max-width: 100vw;
  background: var(--cdd-bg);
  border-left: 1px solid var(--cdd-border);
  box-shadow: -8px 0 24px rgba(2, 11, 49, 0.08);
  display: flex;
  flex-direction: column;
  z-index: var(--cdd-z);
  transform: translateX(100%);
  transition: transform 0.25s ease;
}
#cdd-ai-assistant .cdd-drawer[data-open="true"] {
  transform: translateX(0);
}

/* Drawer header */
#cdd-ai-assistant .cdd-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--cdd-border);
  background: var(--cdd-bg);
}
#cdd-ai-assistant .cdd-title {
  font-weight: 600;
  font-size: 15px;
  margin: 0;
}
#cdd-ai-assistant .cdd-subtitle {
  font-size: 12px;
  color: var(--cdd-text-muted);
  margin: 2px 0 0;
}
#cdd-ai-assistant .cdd-close {
  background: transparent;
  border: none;
  color: var(--cdd-text-muted);
  padding: 6px;
  border-radius: var(--cdd-radius);
  display: flex;
  align-items: center;
  justify-content: center;
}
#cdd-ai-assistant .cdd-close:hover {
  background: var(--cdd-bg-soft);
  color: var(--cdd-text);
}
#cdd-ai-assistant .cdd-close svg {
  width: 18px;
  height: 18px;
}

/* Test mode banner */
#cdd-ai-assistant .cdd-test-banner {
  background: var(--cdd-warn-bg);
  border-bottom: 1px solid var(--cdd-warn-border);
  padding: 8px 18px;
  font-size: 12px;
  color: #5a4500;
}
#cdd-ai-assistant .cdd-test-banner[hidden] {
  display: none;
}

/* Messages area */
#cdd-ai-assistant .cdd-messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

#cdd-ai-assistant .cdd-msg {
  max-width: 90%;
  padding: 10px 14px;
  border-radius: 12px;
  word-wrap: break-word;
}
#cdd-ai-assistant .cdd-msg-user {
  align-self: flex-end;
  background: var(--cdd-brand);
  color: #fff;
  border-bottom-right-radius: 4px;
}
#cdd-ai-assistant .cdd-msg-bot {
  align-self: flex-start;
  background: var(--cdd-bg-soft);
  color: var(--cdd-text);
  border-bottom-left-radius: 4px;
  max-width: 100%;
}
#cdd-ai-assistant .cdd-msg-error {
  background: #fbeaec;
  color: var(--cdd-error);
  border: 1px solid #f3c5cb;
}
#cdd-ai-assistant .cdd-msg p {
  margin: 0 0 8px;
}
#cdd-ai-assistant .cdd-msg p:last-child {
  margin-bottom: 0;
}

/* Citation superscripts inside answer text */
#cdd-ai-assistant .cdd-cite {
  display: inline-block;
  margin: 0 1px 0 2px;
  padding: 1px 5px;
  font-size: 0.72em;
  line-height: 1.2;
  vertical-align: super;
  color: var(--cdd-brand);
  background: var(--cdd-brand-soft);
  border-radius: 4px;
  text-decoration: none;
  cursor: pointer;
  font-weight: 600;
}
#cdd-ai-assistant .cdd-cite:hover {
  background: var(--cdd-brand);
  color: #fff;
}

/* Tooltip on hover (desktop) / tap (mobile) */
#cdd-ai-assistant .cdd-cite-tooltip {
  position: absolute;
  max-width: 320px;
  padding: 10px 12px;
  background: #ffffff;
  border: 1px solid var(--cdd-border);
  border-radius: var(--cdd-radius);
  box-shadow: 0 4px 16px rgba(2, 11, 49, 0.12);
  font-size: 12px;
  line-height: 1.5;
  color: var(--cdd-text);
  z-index: calc(var(--cdd-z) + 1);
  pointer-events: auto;
}
#cdd-ai-assistant .cdd-cite-tooltip[hidden] {
  display: none;
}
#cdd-ai-assistant .cdd-cite-tooltip-title {
  font-weight: 600;
  margin: 0 0 4px;
  color: var(--cdd-brand);
}
#cdd-ai-assistant .cdd-cite-tooltip-domain {
  display: inline-block;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--cdd-text-muted);
  background: var(--cdd-bg-soft);
  padding: 1px 6px;
  border-radius: 3px;
  margin-left: 6px;
}
#cdd-ai-assistant .cdd-cite-tooltip-text {
  margin: 4px 0 6px;
  color: var(--cdd-text-muted);
}
#cdd-ai-assistant .cdd-cite-tooltip-link {
  color: var(--cdd-brand);
  text-decoration: underline;
  font-size: 11px;
}

/* Sources footer (after answer) */
#cdd-ai-assistant .cdd-sources {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--cdd-border);
}
#cdd-ai-assistant .cdd-sources h4 {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--cdd-text-muted);
  margin: 0 0 8px;
}
#cdd-ai-assistant .cdd-sources ol {
  list-style: none;
  padding: 0;
  margin: 0;
  counter-reset: src;
}
#cdd-ai-assistant .cdd-sources li {
  counter-increment: src;
  padding: 4px 0;
  font-size: 12px;
}
#cdd-ai-assistant .cdd-sources li::before {
  content: counter(src) ".";
  color: var(--cdd-text-muted);
  margin-right: 6px;
  font-weight: 600;
}
#cdd-ai-assistant .cdd-sources a {
  color: var(--cdd-brand);
  text-decoration: none;
}
#cdd-ai-assistant .cdd-sources a:hover {
  text-decoration: underline;
}
#cdd-ai-assistant .cdd-sources-domain {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--cdd-text-muted);
  background: var(--cdd-bg-soft);
  padding: 1px 6px;
  border-radius: 3px;
  margin-left: 6px;
}

/* Typing indicator */
#cdd-ai-assistant .cdd-typing {
  display: inline-flex;
  gap: 4px;
  padding: 10px 14px;
  background: var(--cdd-bg-soft);
  border-radius: 12px;
  border-bottom-left-radius: 4px;
  align-self: flex-start;
}
#cdd-ai-assistant .cdd-typing span {
  width: 6px;
  height: 6px;
  background: var(--cdd-text-muted);
  border-radius: 50%;
  animation: cdd-bounce 1.4s infinite ease-in-out;
}
#cdd-ai-assistant .cdd-typing span:nth-child(2) { animation-delay: 0.15s; }
#cdd-ai-assistant .cdd-typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes cdd-bounce {
  0%, 80%, 100% { transform: scale(0.7); opacity: 0.5; }
  40% { transform: scale(1); opacity: 1; }
}

/* Composer */
#cdd-ai-assistant .cdd-composer {
  border-top: 1px solid var(--cdd-border);
  padding: 12px 14px;
  background: var(--cdd-bg);
}
#cdd-ai-assistant .cdd-composer-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
#cdd-ai-assistant .cdd-input {
  flex: 1;
  min-height: 40px;
  max-height: 140px;
  padding: 10px 12px;
  border: 1px solid var(--cdd-border);
  border-radius: var(--cdd-radius);
  resize: none;
  font-family: inherit;
  font-size: inherit;
  line-height: 1.45;
  color: var(--cdd-text);
  background: var(--cdd-bg);
  outline: none;
}
#cdd-ai-assistant .cdd-input:focus {
  border-color: var(--cdd-brand);
  box-shadow: 0 0 0 3px var(--cdd-brand-soft);
}
#cdd-ai-assistant .cdd-input:disabled {
  background: var(--cdd-bg-soft);
  color: var(--cdd-text-muted);
}
#cdd-ai-assistant .cdd-send {
  height: 40px;
  padding: 0 16px;
  background: var(--cdd-brand);
  color: #fff;
  border: none;
  border-radius: var(--cdd-radius);
  font-weight: 600;
  transition: background 0.12s ease;
}
#cdd-ai-assistant .cdd-send:hover:not(:disabled) {
  background: var(--cdd-brand-hover);
}
#cdd-ai-assistant .cdd-send:disabled {
  background: var(--cdd-text-muted);
  cursor: not-allowed;
  opacity: 0.6;
}
#cdd-ai-assistant .cdd-footer-hint {
  font-size: 11px;
  color: var(--cdd-text-muted);
  margin-top: 6px;
  text-align: right;
}

/* Empty state — when conversation is brand new */
#cdd-ai-assistant .cdd-empty {
  text-align: center;
  padding: 32px 20px;
  color: var(--cdd-text-muted);
}
#cdd-ai-assistant .cdd-empty h3 {
  margin: 0 0 8px;
  font-size: 15px;
  font-weight: 600;
  color: var(--cdd-text);
}
#cdd-ai-assistant .cdd-empty p {
  margin: 0;
  font-size: 13px;
}

/* Mobile breakpoint */
@media (max-width: 768px) {
  #cdd-ai-assistant {
    --cdd-drawer-width: 100vw;
    --cdd-font-size: 15px;            /* slightly larger for thumbs */
  }
  #cdd-ai-assistant .cdd-drawer {
    border-left: none;
  }
  #cdd-ai-assistant .cdd-bubble {
    right: 16px;
    bottom: 16px;
  }
  /* Touch-friendly composer */
  #cdd-ai-assistant .cdd-input {
    min-height: 44px;                 /* Apple HIG minimum */
    font-size: 16px;                  /* prevents iOS auto-zoom on focus */
  }
  #cdd-ai-assistant .cdd-send {
    height: 44px;
    padding: 0 18px;
  }
  /* Tooltips can be huge on small screens — cap them to fit */
  #cdd-ai-assistant .cdd-cite-tooltip {
    max-width: calc(100vw - 24px);
    left: 12px !important;            /* override JS positioning on mobile */
    right: 12px;
    width: auto;
  }
  /* Prevent body scroll when drawer is open (iOS rubber-band fix is in JS) */
  body.cdd-drawer-open {
    overflow: hidden;
    position: fixed;
    width: 100%;
  }
}
