/**
 * MSA Chat Widget — Component styles.
 *
 * Depends on: css/tokens.css (--color-*, --text-*, --radius-*, --gap-*)
 *
 * Structure:
 *   .msa-chat           — root wrapper
 *   .msa-chat__fab      — fixed FAB button + pulse animation
 *   .msa-chat__panel    — chat window
 *   .msa-chat__header   — panel header
 *   .msa-chat__body     — scrollable messages area
 *   .msa-chat__bubble   — individual message bubbles
 *   .msa-chat__typing   — "…" typing indicator
 *   .msa-chat__footer   — input area
 *   .msa-chat__ticket-form — email fallback form
 *   .msa-chat__rate-limit  — 429 warning bar
 */

/* ── Body lock (mobile) ───────────────────────────────────────────────────── */
body.msa-chat-locked {
  overflow: hidden !important;
  /* Prevent scroll leak on iOS */
  position: fixed;
  width: 100%;
  height: 100%;
}

.msa-chat {
  --chat-z: 99999;
  --chat-fab-size: 52px;
  --chat-panel-w: 380px;
  --chat-panel-h: 580px;
  --chat-offset-x: 24px;
  --chat-offset-y: 24px;
  --chat-accent: var(--color-accent-2);
  --chat-accent-hover: #d32f2f;
  --chat-bg: var(--color-white);
  --chat-border: var(--color-border);
  --chat-shadow: 0 8px 32px rgba(0, 0, 0, 0.14), 0 2px 8px rgba(0, 0, 0, 0.08);
  font-family: var(--font-family);
}

/* ── FAB button ───────────────────────────────────────────────────────────── */

.msa-chat__fab-wrapper {
  position: fixed;
  bottom: 0;
  right: var(--chat-offset-x);
  transform: translateY(calc(var(--chat-offset-y) * -1));
  z-index: var(--chat-z);
  display: flex;
  align-items: center;
  gap: 8px;
  transition:
    transform 0.3s ease,
    opacity 0.3s ease;
}

.msa-chat__popover {
  background: #ffffff;
  border: 1px solid #004b8c;
  border-radius: 56px;
  height: 52px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 8px;
  box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.08);
  opacity: 1;
  transition:
    opacity 0.3s ease,
    transform 0.3s ease;
  transform-origin: right center;
}

.msa-chat__popover[hidden] {
  display: none !important;
}

.msa-chat__popover--hidden-scroll {
  opacity: 0;
  transform: scaleX(0.8);
  pointer-events: none;
}

.msa-chat__popover-text {
  font-family: 'Montserrat', sans-serif;
  font-weight: 500;
  font-size: 12px;
  line-height: 16px;
  color: #1a1e26;
}

.msa-chat__popover-close {
  background: none;
  border: none;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  width: 16px;
  height: 16px;
}

.msa-chat__fab {
  width: var(--chat-fab-size);
  height: var(--chat-fab-size);
  border-radius: var(--radius-full);
  background: rgba(255, 255, 255, 0.4);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border: 1px solid #004b8c;
  color: #004b8c;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition:
    transform 0.2s ease,
    background 0.2s ease;
  box-shadow: none;
  flex-shrink: 0;
  position: relative;
}

.msa-chat__fab svg {
  position: relative;
  z-index: 2;
}

.msa-chat__fab:hover {
  transform: scale(1.07);
  background: rgba(255, 255, 255, 0.6);
}

.msa-chat__fab:focus-visible {
  outline: 3px solid rgba(245, 67, 54, 0.4);
  outline-offset: 3px;
}

/* Pulse rings */
.msa-chat__fab::before,
.msa-chat__fab::after {
  content: '';
  position: absolute;
  inset: -1px;
  border-radius: var(--radius-full);
  background: transparent;
  border: 1px solid #004b8c;
  opacity: 0;
  animation: msa-chat-pulse 2.5s ease-out infinite;
  pointer-events: none;
}

.msa-chat__fab::after {
  animation-delay: 0.8s;
}

@keyframes msa-chat-pulse {
  0% {
    transform: scale(1);
    opacity: 0.8;
  }
  80% {
    transform: scale(1.4);
    opacity: 0;
  }
  100% {
    transform: scale(1.4);
    opacity: 0;
  }
}

/* Pause pulse when chat is open or user has seen it */
.msa-chat--open .msa-chat__fab::before,
.msa-chat--open .msa-chat__fab::after,
.msa-chat--seen .msa-chat__fab::before,
.msa-chat--seen .msa-chat__fab::after {
  animation: none;
  opacity: 0;
}

/* Hide chat when any real popup/modal is open.
   All project popups share .msa-popup + .is-open (varPopup, changeRegPopup,
   sameRegPopup, msaQuizPopup). Cart drawer uses .cart-drawer.is-open. */
body:has(.msa-popup.is-open) .msa-chat,
body:has(.cart-drawer.is-open) .msa-chat,
body:has(.new-hdr__nav.active) .msa-chat,
.msa-chat--hidden {
  opacity: 0 !important;
  pointer-events: none !important;
  transition: opacity 0.3s ease;
}

/* Lift the chat above bottom sticky panels when they are visible.
   .cart-sticky toggles .is-visible (inc/global/cart-sticky-panel.php);
   .reg-sticky-panel is Alpine x-show (inline style="display: none;" when hidden). */
body:has(.cart-sticky.is-visible) .msa-chat,
body:has(.reg-sticky-panel:not([style*='display: none'])) .msa-chat {
  --chat-offset-y: calc(24px + 70px);
}

@media (max-width: 599px) {
  body:has(.cart-sticky.is-visible) .msa-chat,
  body:has(.reg-sticky-panel:not([style*='display: none'])) .msa-chat {
    --chat-offset-y: calc(24px + 80px);
  }
}

/* Notification dot */
.msa-chat__fab-dot {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 12px;
  height: 12px;
  border-radius: var(--radius-full);
  background: var(--color-warning);
  border: 2px solid #fff;
}

/* ── Panel ────────────────────────────────────────────────────────────────── */

.msa-chat__panel {
  position: fixed;
  bottom: calc(var(--chat-offset-y) + var(--chat-fab-size) + 12px);
  right: var(--chat-offset-x);
  z-index: var(--chat-z);
  width: var(--chat-panel-w);
  height: var(--chat-panel-h);
  max-height: calc(100vh - var(--chat-offset-y) - var(--chat-fab-size) - 24px);
  background: var(--chat-bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--chat-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform-origin: bottom right;
  transform: scale(0.92) translateY(12px);
  opacity: 0;
  transition:
    transform 0.22s cubic-bezier(0.34, 1.56, 0.64, 1),
    opacity 0.18s ease;
  pointer-events: none;
}

.msa-chat__panel[aria-hidden='false'] {
  transform: scale(1) translateY(0);
  opacity: 1;
  pointer-events: auto;
}

/* ── Header ───────────────────────────────────────────────────────────────── */

.msa-chat__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: #ffffff;
  border-bottom: 1px solid #e5e7eb;
  color: #1a1e26;
  flex-shrink: 0;
}

.msa-chat__header-info {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.msa-chat__header-avatar {
  width: 40px;
  height: 40px;
  border-radius: 20px;
  background: #fafafa;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  flex-shrink: 0;
}

.msa-chat__header-name {
  font-size: 16px;
  font-weight: 600;
  line-height: 24px;
  font-family: 'Montserrat', sans-serif;
  color: #1a1e26;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.msa-chat__close {
  background: none;
  border: none;
  color: #1a1e26;
  cursor: pointer;
  padding: 0;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition:
    color 0.15s ease,
    background 0.15s ease;
  flex-shrink: 0;
  width: 24px;
  height: 24px;
}

.msa-chat__close:hover {
  background: #fafafa;
}

.msa-chat__close:focus-visible {
  outline: 2px solid #e5e7eb;
  outline-offset: 2px;
}

/* ── Messages body ────────────────────────────────────────────────────────── */

.msa-chat__body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: var(--gap-sm) var(--space-md);
  display: flex;
  flex-direction: column;
  gap: var(--gap-sm);
  scroll-behavior: smooth;
  /* Prevent scroll chaining on mobile */
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.msa-chat__body::-webkit-scrollbar {
  width: 4px;
}
.msa-chat__body::-webkit-scrollbar-track {
  background: transparent;
}
.msa-chat__body::-webkit-scrollbar-thumb {
  background: var(--color-text-light-gray);
  border-radius: var(--radius-full);
}

/* ── Message row ──────────────────────────────────────────────────────────── */

.msa-chat__msg {
  display: flex;
  align-items: flex-end;
  gap: var(--gap-xs);
  max-width: 88%;
  animation: msa-chat-msg-in 0.2s ease both;
}

@keyframes msa-chat-msg-in {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.msa-chat__msg--ai {
  align-self: flex-start;
}

.msa-chat__msg--user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

/* AI avatar */
.msa-chat__msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-full);
  background: var(--chat-accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: var(--font-weight-bold);
  flex-shrink: 0;
}

/* ── Bubbles ──────────────────────────────────────────────────────────────── */

.msa-chat__bubble {
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 22px;
  word-break: break-word;
  white-space: pre-wrap;
}

.msa-chat__msg--ai .msa-chat__bubble {
  background: #fafafa;
  border: 1px solid #e5e7eb;
  color: #1a1e26;
  border-bottom-left-radius: 12px;
}

.msa-chat__msg--user .msa-chat__bubble {
  background: #004b8c;
  color: #fff;
  border-bottom-right-radius: 12px;
}

.msa-chat__bubble a {
  color: inherit;
  text-decoration: underline;
  font-weight: 600;
}

.msa-chat__bubble a:hover {
  text-decoration: none;
}

/* ── Inline Button Link ───────────────────────────────────────────────────── */
.msa-chat__btn-link {
  display: inline-block;
  background: #004b8c;
  color: #ffffff !important;
  text-decoration: none !important;
  padding: 8px 16px;
  border-radius: 8px;
  font-family: 'Montserrat', sans-serif;
  font-size: 14px;
  font-weight: 600;
  text-align: center;
  transition: background 0.15s ease;
  margin-top: 4px;
}

.msa-chat__btn-link:hover {
  background: #003666;
}

/* ── Suggestions ──────────────────────────────────────────────────────────── */

.msa-chat__suggestions {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  width: 100%;
  margin-top: 8px;
}

.msa-chat__suggestion {
  background: #fafafa;
  border: 1px solid #004b8c;
  border-radius: 12px;
  padding: 8px 12px;
  color: #004b8c;
  font-family: 'Montserrat', sans-serif;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition:
    background 0.15s ease,
    color 0.15s ease;
  white-space: normal;
  text-align: right;
  max-width: 88%;
}

.msa-chat__suggestion:hover {
  background: #004b8c;
  color: #ffffff;
}

/* ── Typing indicator ─────────────────────────────────────────────────────── */

.msa-chat__typing {
  display: flex;
  align-items: flex-end;
  gap: var(--gap-xs);
  align-self: flex-start;
}

.msa-chat__typing-dots {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--color-bg-tint);
  padding: 12px 16px;
  border-radius: var(--radius-md);
  border-bottom-left-radius: var(--radius-xs);
}

.msa-chat__typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: var(--radius-full);
  background: var(--color-text-tertiary);
  animation: msa-chat-dot-bounce 1.2s ease infinite both;
}

.msa-chat__typing-dots span:nth-child(2) {
  animation-delay: 0.2s;
}
.msa-chat__typing-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes msa-chat-dot-bounce {
  0%,
  80%,
  100% {
    transform: translateY(0);
    opacity: 0.45;
  }
  40% {
    transform: translateY(-5px);
    opacity: 1;
  }
}

/* ── Honeypot ─────────────────────────────────────────────────────────────── */

.msa-chat__hp {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  width: 0;
  height: 0;
  z-index: -1;
}

/* ── Footer ───────────────────────────────────────────────────────────────── */

.msa-chat__footer {
  flex-shrink: 0;
  border-top: none;
  padding: 12px 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* Input row */
.msa-chat__input-row {
  display: flex;
  align-items: center;
  gap: 12px;
  background: #fafafa;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 12px 16px;
  min-height: 48px;
  transition: border-color 0.15s ease;
  box-sizing: border-box;
}

.msa-chat__input-row:focus-within {
  border-color: #004b8c;
  background: #ffffff;
}

.msa-chat__input-row .msa-chat__char-count:empty {
  display: none;
}

.msa-chat__textarea {
  box-sizing: border-box;
  flex: 1 1 auto;
  resize: none;
  border: none;
  padding: 0;
  font-family: 'Montserrat', sans-serif;
  font-size: 14px;
  font-weight: 500;
  line-height: 22px;
  color: #1a1e26;
  background: transparent;
  max-height: 100px;
  overflow-y: auto;
}

.msa-chat__textarea:focus {
  outline: none;
}

.msa-chat__textarea::placeholder {
  color: #9ca2ad;
}

.msa-chat__textarea:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.msa-chat__send {
  width: 24px;
  height: 24px;
  background: transparent;
  color: #6a7282;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  padding: 0;
  transition:
    color 0.15s ease,
    opacity 0.15s ease;
}

.msa-chat__send:hover:not(:disabled) {
  color: #004b8c;
}

.msa-chat__send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.msa-chat__send:focus-visible {
  outline: 2px solid #004b8c;
  outline-offset: 2px;
  border-radius: 4px;
}

/* Character counter */
.msa-chat__char-count {
  font-size: var(--text-caption);
  line-height: var(--text-caption-lh);
  color: var(--color-text-tertiary);
  text-align: right;
  margin: 0;
  min-height: 16px;
}

.msa-chat__char-count--warn {
  color: var(--color-warning);
}

/* ── Products list ────────────────────────────────────────────────────────── */
.msa-chat__body .new-hdr__prod-card {
  padding: 10px;
}

.msa-chat__body .new-hdr__prod-bot .new-hdr__prod-view {
  display: none;
}

.msa-chat__body .new-hdr__prod-title {
  font-size: var(--text-body-sm, 14px);
  line-height: 22px;
}

.msa-chat__prod-text-link {
  font-family: 'Montserrat', sans-serif;
  font-size: 14px;
  font-weight: 600;
  color: #004b8c;
  text-decoration: underline;
  padding: 0 16px;
  display: block;
}

.msa-chat__prod-text-link:hover {
  text-decoration: none;
  color: #003666;
}

/* ── Ticket form ──────────────────────────────────────────────────────────── */

.msa-chat__ticket-form {
  display: flex;
  flex-direction: column;
  gap: var(--gap-xs);
}

.msa-chat__ticket-label {
  font-size: var(--text-body-sm);
  line-height: var(--text-body-sm-lh);
  color: var(--color-text-secondary);
  margin: 0;
}

.msa-chat__email-input {
  width: 100% !important;
  box-sizing: border-box;
  border: 1px solid #e5e7eb !important;
  border-radius: 12px !important;
  padding: 12px 16px !important;
  font-family: 'Montserrat', sans-serif;
  font-size: 14px;
  color: #1a1e26;
  background: #fafafa;
  transition: border-color 0.15s ease;
  min-height: 48px;
}

.msa-chat__email-input:focus {
  outline: none;
  border-color: #004b8c;
  background: #ffffff;
}

.msa-chat__email-input--error {
  border-color: #f54336 !important;
}

.msa-chat__email-error {
  font-size: 12px;
  color: #f54336;
  margin: -4px 0 0 0;
}

.msa-chat__email-input::placeholder {
  color: #9ca2ad;
}

.msa-chat__ticket-message {
  width: 100%;
  box-sizing: border-box;
  resize: vertical;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 12px 16px;
  font-family: 'Montserrat', sans-serif;
  font-size: 14px;
  line-height: 22px;
  color: #1a1e26;
  background: #fafafa;
  transition: border-color 0.15s ease;
}

.msa-chat__ticket-message:focus {
  outline: none;
  border-color: #004b8c;
  background: #ffffff;
}

.msa-chat__ticket-message::placeholder {
  color: #9ca2ad;
}

.msa-chat__ticket-btn {
  width: 100%;
  padding: 12px 16px;
  border-radius: 12px;
  background: #004b8c;
  color: #fff;
  border: none;
  cursor: pointer;
  font-family: 'Montserrat', sans-serif;
  font-size: 14px;
  font-weight: 600;
  transition:
    background 0.15s ease,
    opacity 0.15s ease;
}

.msa-chat__ticket-btn:hover:not(:disabled) {
  background: var(--chat-accent-hover);
}

.msa-chat__ticket-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ── Rate-limit bar ───────────────────────────────────────────────────────── */

.msa-chat__rate-limit {
  display: flex;
  align-items: center;
  gap: var(--gap-xs);
  padding: 8px 12px;
  background: #fff8ec;
  border: 1px solid #ffe4a0;
  border-radius: var(--radius-sm);
  font-size: var(--text-body-sm);
  color: var(--color-warning);
}

.msa-chat__rate-limit svg {
  flex-shrink: 0;
}

/* ── Mobile ───────────────────────────────────────────────────────────────── */

@media (max-width: 599px) {
  /* Dimmed backdrop when open */
  .msa-chat--open::before {
    content: '';
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: calc(var(--chat-z) - 1);
    animation: msa-chat-fade-in 0.2s ease;
  }

  @keyframes msa-chat-fade-in {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }

  .msa-chat__panel {
    --chat-panel-w: 100vw;
    bottom: 0;
    top: auto;
    left: 0;
    right: 0;
    width: 100%;
    height: 80vh;
    max-height: 80dvh;
    border-radius: 24px 24px 0 0;
    transform-origin: bottom center;
    transform: translateY(100%);
    overscroll-behavior: none;
    box-shadow:
      0px -6px 24px 0px rgba(5, 25, 39, 0.04),
      0px 20px 30px 0px rgba(5, 25, 39, 0.04);
  }

  .msa-chat__panel[aria-hidden='false'] {
    transform: translateY(0);
  }

  /* Prevent iOS Safari from zooming in on input focus (requires min 16px) */
  .msa-chat__textarea,
  .msa-chat__email-input,
  .msa-chat__ticket-message {
    font-size: 16px;
  }

  .msa-chat__fab-wrapper {
    bottom: 0;
    transform: translateY(calc(var(--chat-offset-y) * -1));
  }

  /* Hide FAB when panel is open on mobile */
  .msa-chat--open .msa-chat__fab {
    opacity: 0;
    pointer-events: none;
    transform: scale(0.8);
  }
}
