/* Chatbot widget styles (P4.4 / #1011, polish #1086, sidebar entry point + drag
 * / #1609).
 *
 * Plain Bootstrap 5 + Varial design tokens. No !important. No inline styles.
 * Tokens (--varial-*) are defined in css/branding.css.
 *
 * Layout: a fixed bottom-left widget host that contains the flyout panel.
 * The persistent open/close affordance lives in the sidebar
 * (#cms-chat-sidebar-toggle in templates/includes/sidebar.html).
 *
 * State classes (driven by chat-widget.js):
 *   .cms-chat-widget.is-open       — panel is visible
 *   .cms-chat-widget.is-loading    — request in flight
 *   .cms-chat-widget.is-dragging   — pointer drag in progress
 *   .cms-chat-loading              — typing-indicator row in the log
 *   .cms-chat-msg-error            — banner-styled error row in the log
 */

.cms-chat-widget {
  position: fixed;
  left: 1.25rem;
  bottom: 1.25rem;
  z-index: 1080;
  font-family: var(--bs-body-font-family, sans-serif);
}

.cms-chat-widget[hidden] {
  display: none;
}

.cms-chat-widget.is-dragging {
  cursor: grabbing;
  user-select: none;
}

/* Styleguide modifier — render the widget inline (not fixed) so it can
   live inside a card on /pages/styleguide/ for documentation purposes. */
.cms-chat-widget--inline {
  position: relative;
  left: auto;
  right: auto;
  top: auto;
  bottom: auto;
  z-index: auto;
  display: inline-block;
}

.cms-chat-panel {
  width: 22rem;
  max-width: calc(100vw - 2rem);
  height: 28rem;
  max-height: calc(100vh - 4rem);
  display: flex;
  flex-direction: column;
  background: var(--varial-bg, #fff);
  color: var(--varial-fg, #1c1c1c);
  border: 1px solid var(--varial-border, #d6d6d6);
  border-radius: 0.75rem;
  box-shadow: var(--varial-shadow-lg, 0 12px 32px rgba(0, 0, 0, 0.22));
  overflow: hidden;
  position: absolute;
  left: 0;
  bottom: 0;
  opacity: 0;
  transform: translateY(0.75rem) scale(0.98);
  transition:
    opacity 180ms ease,
    transform 220ms cubic-bezier(0.2, 0.8, 0.25, 1);
  pointer-events: none;
}

.cms-chat-panel.is-open {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

.cms-chat-panel[hidden] {
  display: none;
}

@media (prefers-reduced-motion: reduce) {
  .cms-chat-panel {
    transition: none;
  }
  .cms-chat-notice,
  .cms-chat-contact {
    transition: none;
  }
  .cms-chat-typing-dot {
    animation: none;
  }
}

.cms-chat-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 0.9rem;
  background: var(--varial-primary, #1B3AA0);
  color: var(--varial-on-primary, #fff);
}

.cms-chat-drag-handle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.25rem;
  height: 1.25rem;
  cursor: move;
  font-size: 0.85rem;
  line-height: 1;
  color: var(--varial-on-primary, #fff);
  opacity: 0.75;
  /* Prevent the browser hijacking the pointer for scroll/zoom while
     a drag is in progress (mainly relevant on touch/pen devices). */
  touch-action: none;
  user-select: none;
}

.cms-chat-drag-handle:hover {
  opacity: 1;
}

.cms-chat-title {
  font-size: 1rem;
  margin: 0;
  font-weight: 600;
  flex: 1 1 auto;
  /* Override branding.css h1–h6 dark color so the title stays readable */
  /* on the primary-blue header background (inherits from .cms-chat-header). */
  color: var(--varial-on-primary, #fff);
}

.cms-chat-close {
  background: transparent;
  border: 0;
  color: inherit;
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 0.25rem;
}

.cms-chat-close:hover,
.cms-chat-close:focus-visible {
  outline: 2px solid var(--varial-focus-ring, #88a4ff);
  outline-offset: 2px;
}

/* Sidebar entry-point button — render like the other nav-link items but
   without the underline/anchor affordance since it's a <button>. */
.cms-chat-sidebar-toggle {
  background: transparent;
  border: 0;
  display: block;
  /* Unlike a block <a>, a block <button> won't auto-stretch to fill its
     container — it shrink-wraps to its text. Force full width, but subtract
     the .nav-link horizontal margins (0 0.25rem) so the button's outer box
     lines up with the anchor nav-links instead of overflowing the rail. */
  width: calc(100% - 0.5rem);
  text-align: left;
  cursor: pointer;
}

.cms-chat-log {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 0.75rem;
  background: var(--varial-bg-muted, #f7f8fa);
}

.cms-chat-msg {
  margin: 0 0 0.6rem 0;
  padding: 0.5rem 0.75rem;
  border-radius: 0.5rem;
  max-width: 85%;
  word-wrap: break-word;
}

.cms-chat-msg-user {
  margin-left: auto;
  background: var(--varial-primary-soft, #e8edff);
  /* Real theme-flipping token, not the phantom --varial-fg (which always fell back
     to near-black #1c1c1c and left user text illegible on the dark bubble). */
  color: var(--varial-gray-900, #131210);
}

.cms-chat-msg-assistant {
  background: var(--varial-bg, #fff);
  border: 1px solid var(--varial-border, #d6d6d6);
}

/* Dark mode: --varial-fg is undefined (dark fallback) so message text is
   invisible on the dark bubbles — set readable light text + a dark assistant
   bubble. */
[data-bs-theme="dark"] .cms-chat-panel,
[data-bs-theme="dark"] .cms-chat-msg,
[data-bs-theme="dark"] .cms-chat-msg-user,
[data-bs-theme="dark"] .cms-chat-msg-assistant {
  color: var(--varial-gray-700, #F1F5F9);
}

[data-bs-theme="dark"] .cms-chat-msg-assistant {
  background: var(--varial-bg-subtle, #1E293B);
  border-color: var(--varial-border, #334155);
}

/* Rendered markdown content inside an assistant message — keep the
   internal spacing tight so the message bubble doesn't feel sparse. */
.cms-chat-msg-body > :first-child {
  margin-top: 0;
}
.cms-chat-msg-body > :last-child {
  margin-bottom: 0;
}
.cms-chat-msg-body p {
  margin: 0 0 0.4rem 0;
}
.cms-chat-msg-body h1,
.cms-chat-msg-body h2,
.cms-chat-msg-body h3,
.cms-chat-msg-body h4,
.cms-chat-msg-body h5,
.cms-chat-msg-body h6 {
  font-size: 0.95rem;
  font-weight: 600;
  margin: 0.4rem 0 0.3rem 0;
  line-height: 1.25;
}
.cms-chat-msg-body ul,
.cms-chat-msg-body ol {
  margin: 0.25rem 0 0.4rem 1.1rem;
  padding: 0;
}
.cms-chat-msg-body li {
  margin: 0 0 0.15rem 0;
}
.cms-chat-msg-body code {
  background: var(--varial-bg-muted, #f1f3f8);
  padding: 0.05rem 0.3rem;
  border-radius: 0.25rem;
  font-size: 0.85em;
  font-family: var(--bs-font-monospace, "SFMono-Regular", monospace);
}
.cms-chat-msg-body pre {
  background: var(--varial-bg-muted, #f1f3f8);
  padding: 0.5rem 0.6rem;
  border-radius: 0.4rem;
  overflow-x: auto;
  font-size: 0.85em;
  margin: 0.3rem 0;
}
.cms-chat-msg-body pre code {
  background: transparent;
  padding: 0;
}
.cms-chat-msg-body a {
  color: var(--varial-primary, #1B3AA0);
  text-decoration: underline;
}
.cms-chat-msg-body blockquote {
  margin: 0.3rem 0;
  padding: 0.25rem 0.6rem;
  border-left: 3px solid var(--varial-border, #d6d6d6);
  color: var(--varial-fg-muted, #555);
}

.cms-chat-msg-error {
  background: var(--varial-warning-soft, var(--varial-error-soft, #fde2e2));
  color: var(--varial-warning-fg, var(--varial-error-fg, #7a1212));
  border: 1px solid var(--varial-warning-border, var(--varial-error-border, #f4b8b8));
  max-width: 100%;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.35rem;
}

.cms-chat-retry {
  align-self: flex-start;
  background: var(--varial-bg, #fff);
  color: var(--varial-warning-fg, var(--varial-error-fg, #7a1212));
  border: 1px solid var(--varial-warning-border, var(--varial-error-border, #f4b8b8));
  border-radius: 0.4rem;
  padding: 0.2rem 0.6rem;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
}

.cms-chat-retry:hover,
.cms-chat-retry:focus-visible {
  background: var(--varial-warning-soft, var(--varial-error-soft, #fde2e2));
  outline: 2px solid var(--varial-focus-ring, #88a4ff);
  outline-offset: 1px;
}

/* Typing indicator — three dots with a staggered bounce. */
.cms-chat-loading {
  padding: 0.6rem 0.85rem;
  width: auto;
  display: inline-flex;
  align-items: center;
}

.cms-chat-typing {
  display: inline-flex;
  gap: 0.3rem;
  align-items: center;
}

.cms-chat-typing-dot {
  width: 0.45rem;
  height: 0.45rem;
  border-radius: 50%;
  background: var(--varial-fg-muted, #888);
  display: inline-block;
  animation: cms-chat-typing-bounce 1.2s infinite ease-in-out;
}

.cms-chat-typing-dot:nth-child(2) {
  animation-delay: 0.15s;
}
.cms-chat-typing-dot:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes cms-chat-typing-bounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.55;
  }
  30% {
    transform: translateY(-0.2rem);
    opacity: 1;
  }
}

.cms-chat-citations {
  margin: 0.4rem 0 0 0;
  padding: 0;
  list-style: none;
  font-size: 0.85rem;
}

.cms-chat-citations li {
  margin-bottom: 0.15rem;
}

.cms-chat-form {
  display: flex;
  gap: 0.4rem;
  padding: 0.5rem;
  border-top: 1px solid var(--varial-border, #d6d6d6);
  background: var(--varial-bg, #fff);
}

.cms-chat-input {
  flex: 1 1 auto;
  border: 1px solid var(--varial-border, #d6d6d6);
  border-radius: 0.4rem;
  padding: 0.4rem 0.6rem;
  font-size: 0.95rem;
  /* Pin to the panel's surface tokens so the field doesn't fall back to the OS
     `field` system colour (dark grey on a dark-mode OS) in light mode (#3323).
     Text uses the real theme-flipping token, not the phantom --varial-fg (which
     always fell back to near-black and was illegible on the dark field, #4066). */
  background: var(--varial-bg, #fff);
  color: var(--varial-gray-900, #131210);
}

.cms-chat-input::placeholder {
  color: var(--varial-gray-500, #6b7280);
  opacity: 1;
}

.cms-chat-input:disabled {
  background: var(--varial-bg-muted, #f1f3f8);
  cursor: not-allowed;
}

.cms-chat-submit {
  background: var(--varial-primary, #1B3AA0);
  color: var(--varial-on-primary, #fff);
  border: 0;
  border-radius: 0.4rem;
  padding: 0.4rem 0.9rem;
  font-weight: 600;
  cursor: pointer;
}

.cms-chat-submit:disabled {
  opacity: 0.6;
  cursor: progress;
}

.cms-chat-notice,
.cms-chat-contact {
  /* Collapsed once a question has been asked (.has-messages on the panel) —
     the disclaimer/contact footer matters most before the first message,
     and just eats log space once a conversation is underway. */
  transition:
    max-height 200ms ease,
    opacity 160ms ease,
    padding 200ms ease,
    border-top-width 200ms ease;
  max-height: 5rem;
  overflow: hidden;
}

.cms-chat-panel.has-messages .cms-chat-notice,
.cms-chat-panel.has-messages .cms-chat-contact {
  max-height: 0;
  opacity: 0;
  padding-top: 0;
  padding-bottom: 0;
  border-top-width: 0;
}

.cms-chat-notice {
  margin: 0;
  padding: 0.5rem 0.75rem;
  background: var(--varial-bg-muted, #f7f8fa);
  border-top: 1px solid var(--varial-border, #d6d6d6);
  text-align: center;
  font-size: 0.75rem;
  color: var(--varial-fg-muted, #555);
}

/* Contact-us CTA — a more human option below the AI disclaimer. */
.cms-chat-contact {
  margin: 0;
  padding: 0.4rem 0.75rem 0.6rem;
  background: var(--varial-bg-muted, #f7f8fa);
  text-align: center;
  font-size: 0.8rem;
}

.cms-chat-contact-link {
  color: var(--varial-fg-muted, #555);
  text-decoration: none;
}

.cms-chat-contact-link:hover,
.cms-chat-contact-link:focus-visible {
  color: var(--varial-primary, #1B3AA0);
  text-decoration: underline;
}

/* Auth prompt — shown when the user is not authenticated or unverified.
 * Replaces the input form so anonymous visitors still see the widget
 * chrome but get a clear sign-in CTA. See chat-widget.js showAuthPrompt(). */
.cms-chat-auth-prompt {
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  align-items: center;
  text-align: center;
  border-top: 1px solid var(--varial-border, #d6d6d6);
}

.cms-chat-auth-prompt-message {
  margin: 0;
  font-size: 0.875rem;
  /* Theme-aware text token (readable in light and dark) rather than the
     phantom --varial-fg token, which is never defined and so always resolves
     to a hardcoded dark fallback that is invisible on the dark chat panel. */
  color: var(--varial-gray-900);
}

.cms-chat-auth-prompt-action {
  text-decoration: none;
}

/* Mobile bottom-sheet fallback — on small viewports the drag handle is
   hidden and the widget is pinned to the full width of the viewport so
   the panel reads as a sheet rather than a floating card. */
@media (max-width: 768px) {
  .cms-chat-widget {
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
  }
  .cms-chat-drag-handle {
    display: none;
  }
  .cms-chat-panel {
    width: 100vw;
    max-width: none;
    border-radius: 0.75rem 0.75rem 0 0;
  }
}
