/* ==========================================================================
   GLOBAL MATTRESS - IN-PAGE CHAT WIDGET
   Self-contained. Every selector is namespaced `.gmc-` so it can never
   collide with styles.css. Reads brand tokens from :root when available and
   falls back to literals so the widget also works on pages that load late.
   ========================================================================== */

.gmc-root {
  --gmc-red: var(--color-primary-red, #D0191F);
  --gmc-red-dark: var(--color-primary-dark, #B01217);
  --gmc-red-hover: var(--color-primary-hover, #9E0F14);
  --gmc-ink: var(--color-text-main, #18181B);
  --gmc-ink-soft: var(--color-text-secondary, #4B5563);
  --gmc-ink-muted: var(--color-text-muted, #6B7280);
  --gmc-surface: var(--color-bg-white, #FFFFFF);
  --gmc-canvas: var(--color-bg-light, #F8F9FA);
  --gmc-line: var(--color-border-light, #E5E7EB);
  --gmc-green: var(--color-success-green, #10B981);
  --gmc-font-head: var(--font-heading, 'Roboto', -apple-system, BlinkMacSystemFont, sans-serif);
  --gmc-font-body: var(--font-body, 'Inter', -apple-system, BlinkMacSystemFont, sans-serif);
  --gmc-ease: cubic-bezier(0.4, 0, 0.2, 1);

  position: fixed;
  bottom: 32px;
  right: 32px;
  z-index: 9998;
  font-family: var(--gmc-font-body);
  line-height: 1.5;
}

.gmc-root *,
.gmc-root *::before,
.gmc-root *::after {
  box-sizing: border-box;
}

/* --------------------------------------------------------------------------
   LAUNCHER (replaces the old WhatsApp FAB)
   -------------------------------------------------------------------------- */

.gmc-launcher {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 62px;
  height: 62px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--gmc-red);
  color: #FFFFFF;
  cursor: pointer;
  box-shadow: 0 6px 24px rgba(208, 25, 31, 0.45);
  transition: transform 0.25s var(--gmc-ease), background-color 0.2s ease;
}

.gmc-launcher:hover {
  background: var(--gmc-red-dark);
  transform: scale(1.08);
}

.gmc-launcher:focus-visible {
  outline: 3px solid rgba(208, 25, 31, 0.35);
  outline-offset: 3px;
}

.gmc-launcher-icon {
  font-size: 1.6rem;
  transition: transform 0.25s var(--gmc-ease), opacity 0.18s ease;
}

/* Swap the bubble for a chevron while the panel is open */
.gmc-root.is-open .gmc-launcher-icon--open { transform: scale(0.4); opacity: 0; }
.gmc-root.is-open .gmc-launcher-icon--close { transform: scale(1); opacity: 1; }
.gmc-launcher-icon--close { position: absolute; transform: scale(0.4); opacity: 0; }

.gmc-pulse-ring {
  position: absolute;
  inset: -6px;
  border: 2px solid var(--gmc-red);
  border-radius: 50%;
  pointer-events: none;
  animation: gmcPulseRing 2s cubic-bezier(0.24, 0, 0.38, 1) infinite;
}

.gmc-root.is-open .gmc-pulse-ring { display: none; }

@keyframes gmcPulseRing {
  0%   { transform: scale(0.95); opacity: 0.8; }
  50%  { transform: scale(1.3);  opacity: 0; }
  100% { transform: scale(1.3);  opacity: 0; }
}

.gmc-unread {
  position: absolute;
  top: -2px;
  right: -2px;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border: 2px solid #FFFFFF;
  border-radius: 999px;
  background: var(--gmc-ink);
  color: #FFFFFF;
  font-family: var(--gmc-font-head);
  font-size: 0.68rem;
  font-weight: 700;
  line-height: 16px;
  text-align: center;
}

.gmc-root.is-open .gmc-unread,
.gmc-unread[hidden] { display: none; }

/* --------------------------------------------------------------------------
   TOOLTIP
   -------------------------------------------------------------------------- */

.gmc-tooltip {
  position: absolute;
  right: calc(100% + 14px);
  top: 50%;
  transform: translate(10px, -50%);
  padding: 10px 18px;
  border-radius: 24px;
  background: #111827;
  color: #FFFFFF;
  font-family: var(--gmc-font-head);
  font-size: 0.82rem;
  font-weight: 700;
  white-space: nowrap;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.25s var(--gmc-ease), transform 0.25s var(--gmc-ease), visibility 0.25s;
}

.gmc-tooltip::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 100%;
  transform: translateY(-50%);
  border: 6px solid transparent;
  border-left-color: #111827;
}

.gmc-root:not(.is-open) .gmc-launcher:hover ~ .gmc-tooltip,
.gmc-root:not(.is-open) .gmc-launcher:focus-visible ~ .gmc-tooltip {
  opacity: 1;
  visibility: visible;
  transform: translate(0, -50%);
}

/* --------------------------------------------------------------------------
   PANEL
   -------------------------------------------------------------------------- */

.gmc-panel {
  position: absolute;
  right: 0;
  bottom: calc(100% + 18px);
  display: flex;
  flex-direction: column;
  width: 380px;
  height: 600px;
  max-height: calc(100vh - 120px);
  overflow: hidden;
  border-radius: 18px;
  background: var(--gmc-surface);
  box-shadow: 0 24px 60px -12px rgba(0, 0, 0, 0.3), 0 8px 20px -8px rgba(0, 0, 0, 0.15);
  opacity: 0;
  visibility: hidden;
  transform: translateY(16px) scale(0.97);
  transform-origin: bottom right;
  transition: opacity 0.28s var(--gmc-ease), transform 0.28s var(--gmc-ease), visibility 0.28s;
}

.gmc-root.is-open .gmc-panel {
  opacity: 1;
  visibility: visible;
  transform: translateY(0) scale(1);
}

/* --------------------------------------------------------------------------
   HEADER
   -------------------------------------------------------------------------- */

.gmc-header {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
  padding: 16px 16px 16px 18px;
  background: linear-gradient(135deg, var(--gmc-red) 0%, var(--gmc-red-dark) 100%);
  color: #FFFFFF;
}

.gmc-avatar {
  position: relative;
  flex-shrink: 0;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.18);
  border: 2px solid rgba(255, 255, 255, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--gmc-font-head);
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.gmc-avatar-status {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 12px;
  height: 12px;
  border: 2px solid var(--gmc-red-dark);
  border-radius: 50%;
  background: var(--gmc-green);
}

.gmc-header-text {
  flex: 1;
  min-width: 0;
}

.gmc-header-name {
  display: block;
  font-family: var(--gmc-font-head);
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.gmc-header-sub {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
  font-size: 0.75rem;
  color: rgba(255, 255, 255, 0.85);
  white-space: nowrap;
}

.gmc-header-sub .gmc-dot {
  flex-shrink: 0;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--gmc-green);
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.25);
}

.gmc-mode-badge {
  flex-shrink: 0;
  padding: 3px 9px;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.14);
  font-family: var(--gmc-font-head);
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  white-space: nowrap;
}

/* Phase 3 flips this to the human-agent state */
.gmc-mode-badge.is-human {
  border-color: rgba(255, 255, 255, 0.55);
  background: rgba(255, 255, 255, 0.28);
}

.gmc-header-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}

.gmc-icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: rgba(255, 255, 255, 0.9);
  font-size: 0.9rem;
  cursor: pointer;
  transition: background-color 0.18s ease, color 0.18s ease, transform 0.3s var(--gmc-ease);
}

.gmc-icon-btn:hover {
  background: rgba(255, 255, 255, 0.18);
  color: #FFFFFF;
}

.gmc-icon-btn:focus-visible {
  outline: 2px solid rgba(255, 255, 255, 0.7);
  outline-offset: 1px;
}

.gmc-icon-btn.is-spinning { transform: rotate(-360deg); }

/* Escritorio minimiza a la esquina (chevron); móvil cierra (X). */
.gmc-close-mobile { display: none; }

@media (max-width: 767px) {
  .gmc-close-desktop { display: none; }
  .gmc-close-mobile { display: inline-block; }
}

/* --------------------------------------------------------------------------
   MESSAGE LIST
   -------------------------------------------------------------------------- */

.gmc-body {
  flex: 1;
  min-height: 0;
  padding: 18px 16px 8px;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--gmc-canvas);
  scrollbar-width: thin;
  scrollbar-color: #D1D5DB transparent;
}

.gmc-body::-webkit-scrollbar { width: 6px; }
.gmc-body::-webkit-scrollbar-track { background: transparent; }
.gmc-body::-webkit-scrollbar-thumb { background: #D1D5DB; border-radius: 3px; }
.gmc-body::-webkit-scrollbar-thumb:hover { background: #9CA3AF; }

.gmc-daysep {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 4px 0 18px;
  color: var(--gmc-ink-muted);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.gmc-daysep::before,
.gmc-daysep::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--gmc-line);
}

.gmc-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin-bottom: 4px;
  animation: gmcRowIn 0.32s var(--gmc-ease) both;
}

.gmc-row:last-child { margin-bottom: 0; }

/* Una fila que se repinta porque cambió su hora ya estaba en pantalla:
   animarla otra vez es exactamente el parpadeo que queríamos quitar. */
.gmc-row.is-settled { animation: none; }

/* Extra breathing room between speakers */
.gmc-row.is-group-start { margin-top: 14px; }
.gmc-row.is-group-start:first-child { margin-top: 0; }

@keyframes gmcRowIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.gmc-row--customer { flex-direction: row-reverse; }

.gmc-row-avatar {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--gmc-red);
  color: #FFFFFF;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--gmc-font-head);
  font-size: 0.68rem;
  font-weight: 700;
}

.gmc-row-avatar.is-spacer { background: transparent; }

/* The timestamp lives inside the stack, so on rows that show one the avatar
   would sink to the timestamp's baseline. Lift it back to the bubble. */
.gmc-row.has-meta .gmc-row-avatar { margin-bottom: 21px; }

.gmc-row-stack {
  display: flex;
  flex-direction: column;
  max-width: 76%;
}

.gmc-row--customer .gmc-row-stack { align-items: flex-end; }

.gmc-bubble {
  padding: 10px 14px;
  font-size: 0.875rem;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.gmc-row--agent .gmc-bubble {
  border-radius: 16px;
  background: var(--gmc-surface);
  color: var(--gmc-ink);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 4px 12px -6px rgba(0, 0, 0, 0.12);
}

.gmc-row--customer .gmc-bubble {
  border-radius: 16px;
  background: var(--gmc-red);
  color: #FFFFFF;
}

/* The tail belongs to the last bubble of a run, not to every one of them. */
.gmc-row--agent.has-meta .gmc-bubble { border-bottom-left-radius: 4px; }
.gmc-row--customer.has-meta .gmc-bubble { border-bottom-right-radius: 4px; }

.gmc-meta {
  display: flex;
  align-items: center;
  gap: 4px;
  margin: 4px 2px 0;
  color: var(--gmc-ink-muted);
  font-size: 0.69rem;
}

.gmc-meta .gmc-check { color: #34B7F1; font-size: 0.7rem; }

/* --------------------------------------------------------------------------
   PRODUCT CARD (inside an agent bubble)
   -------------------------------------------------------------------------- */

.gmc-product {
  display: block;
  width: 240px;
  margin-top: 8px;
  overflow: hidden;
  border: 1px solid var(--gmc-line);
  border-radius: 14px;
  background: var(--gmc-surface);
  color: inherit;
  text-decoration: none;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 6px 16px -8px rgba(0, 0, 0, 0.16);
  transition: transform 0.2s var(--gmc-ease), box-shadow 0.2s var(--gmc-ease);
  animation: gmcRowIn 0.32s var(--gmc-ease) both;
}

.gmc-product:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.08), 0 12px 24px -10px rgba(0, 0, 0, 0.22);
}

.gmc-product-top {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
}

.gmc-product-img {
  flex-shrink: 0;
  width: 64px;
  height: 64px;
  border-radius: 10px;
  object-fit: cover;
  background: var(--gmc-canvas);
}

.gmc-product-info { min-width: 0; }

.gmc-product-tag {
  display: inline-block;
  margin-bottom: 3px;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--color-red-tint, #FFF0F1);
  color: var(--gmc-red);
  font-family: var(--gmc-font-head);
  font-size: 0.58rem;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
}

.gmc-product-name {
  font-family: var(--gmc-font-head);
  font-size: 0.8rem;
  font-weight: 700;
  line-height: 1.25;
  color: var(--gmc-ink);
}

.gmc-product-price {
  display: flex;
  align-items: baseline;
  gap: 6px;
  margin-top: 4px;
}

.gmc-product-now {
  color: var(--gmc-red);
  font-family: var(--gmc-font-head);
  font-size: 0.92rem;
  font-weight: 700;
}

.gmc-product-was {
  color: var(--gmc-ink-muted);
  font-size: 0.72rem;
  text-decoration: line-through;
}

.gmc-product-cta {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px;
  border-top: 1px solid var(--gmc-line);
  background: var(--gmc-canvas);
  color: var(--gmc-red);
  font-family: var(--gmc-font-head);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.gmc-product:hover .gmc-product-cta {
  background: var(--gmc-red);
  color: #FFFFFF;
}

/* --------------------------------------------------------------------------
   TYPING INDICATOR
   -------------------------------------------------------------------------- */

.gmc-typing-bubble {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 13px 14px;
  border-radius: 16px 16px 16px 4px;
  background: var(--gmc-surface);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 4px 12px -6px rgba(0, 0, 0, 0.12);
}

.gmc-typing-bubble span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #9CA3AF;
  animation: gmcTypingDot 1.3s infinite ease-in-out;
}

.gmc-typing-bubble span:nth-child(2) { animation-delay: 0.18s; }
.gmc-typing-bubble span:nth-child(3) { animation-delay: 0.36s; }

@keyframes gmcTypingDot {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.45; }
  30%           { transform: translateY(-5px); opacity: 1; }
}

/* --------------------------------------------------------------------------
   COMPOSER
   -------------------------------------------------------------------------- */

.gmc-composer-wrap {
  flex-shrink: 0;
  padding: 12px 14px calc(10px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--gmc-line);
  background: var(--gmc-surface);
}

.gmc-composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 6px 6px 6px 14px;
  border: 1px solid var(--gmc-line);
  border-radius: 24px;
  background: var(--gmc-canvas);
  transition: border-color 0.18s ease, box-shadow 0.18s ease;
}

.gmc-composer:focus-within {
  border-color: var(--gmc-red);
  box-shadow: 0 0 0 3px rgba(208, 25, 31, 0.12);
}

.gmc-input {
  flex: 1;
  max-height: 88px;
  padding: 8px 0;
  border: none;
  background: transparent;
  color: var(--gmc-ink);
  font-family: var(--gmc-font-body);
  font-size: 0.875rem;
  line-height: 1.45;
  resize: none;
  outline: none;
  scrollbar-width: none;
}

.gmc-input::-webkit-scrollbar { display: none; }
.gmc-input::placeholder { color: var(--color-text-light, #9CA3AF); }

.gmc-send {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--gmc-red);
  color: #FFFFFF;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background-color 0.18s ease, transform 0.18s var(--gmc-ease), opacity 0.18s ease;
}

.gmc-send:hover:not(:disabled) { background: var(--gmc-red-hover); transform: scale(1.06); }
.gmc-send:disabled { opacity: 0.4; cursor: not-allowed; }
.gmc-send:focus-visible { outline: 2px solid rgba(208, 25, 31, 0.4); outline-offset: 2px; }

.gmc-disclaimer {
  margin-top: 8px;
  color: var(--color-text-light, #9CA3AF);
  font-size: 0.66rem;
  text-align: center;
}

/* --------------------------------------------------------------------------
   RESPONSIVE - full screen below 768px
   -------------------------------------------------------------------------- */

@media (max-width: 767px) {
  .gmc-root {
    bottom: 20px;
    right: 20px;
  }

  .gmc-launcher { width: 56px; height: 56px; }
  .gmc-launcher-icon { font-size: 1.45rem; }
  .gmc-tooltip { display: none; }

  /*
   * Pantalla completa, siguiendo el VIEWPORT VISUAL.
   *
   * En un móvil no basta con inset:0. Cuando se abre el teclado, iOS encoge el
   * viewport visual pero deja igual el de layout, así que un panel anclado al
   * layout queda por detrás del teclado: el cuerpo se ve vacío y el composer
   * flota en mitad de la nada.
   *
   * Por eso el alto y la posición salen de dos variables que chat-widget.js
   * mantiene al día desde window.visualViewport. El 100dvh es el respaldo para
   * navegadores sin esa API.
   */
  .gmc-panel {
    position: fixed;
    top: var(--gmc-vv-top, 0px);
    left: 0;
    right: 0;
    bottom: auto;
    width: 100%;
    height: var(--gmc-vv-height, 100dvh);
    max-height: none;
    border-radius: 0;
    transform: translateY(16px);
    transform-origin: center;
  }

  .gmc-root.is-open .gmc-panel { transform: translateY(0); }

  /* The sheet covers the launcher's corner, so hide it while open.
     The chevron in the panel header closes it. */
  .gmc-root.is-open .gmc-launcher,
  .gmc-root.is-open .gmc-pulse-ring { display: none; }
  .gmc-row-stack { max-width: 82%; }
  .gmc-product { width: min(260px, 100%); }

  /* Con el teclado abierto cada píxel del cuerpo cuenta. */
  .gmc-header { padding: 12px 8px 12px 14px; }
  .gmc-avatar { width: 36px; height: 36px; font-size: 0.85rem; }
  .gmc-body { padding: 14px 12px 6px; }
  .gmc-composer-wrap { padding: 10px 12px; }
  .gmc-disclaimer { display: none; }

  /* La X de cerrar, más grande de lo que pide el diseño: en un móvil es el
     único modo de salir y fallar el toque es frustrante. */
  .gmc-icon-btn { width: 38px; height: 38px; font-size: 1rem; }

  /* The panel already clears the home indicator, so don't inset twice */
  .gmc-composer-wrap { padding-bottom: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  .gmc-root *,
  .gmc-root *::before,
  .gmc-root *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
