/**
 * Chat Blocks Widget V2 - Modern Styles
 * Mobile-first responsive design with CSS variables
 *
 * BACKDROP-FILTER GLITCH FIX:
 * This CSS works with chat-blocks-v2.js to prevent visual glitches
 * when animating glassmorphism effects. Backdrop-filter is applied
 * AFTER opacity animations complete to ensure smooth rendering.
 */

:root {
  --chat-border-radius: 18px;
  --chat-border-radius-mobile: 14px;
  --chat-animation-duration: 0.3s;
  --chat-animation-easing: cubic-bezier(0.4, 0, 0.2, 1);
  --chat-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  --chat-backdrop-blur: blur(10px);
  --chat-gap-mobile: 10px;
  --chat-gap-tablet: 12px;
  --chat-gap-desktop: 15px;
  --chat-backdrop-transition: 0.2s ease-out;
}

/* Container */
.chat-container-v2 {
  width: 100%;
  margin: 0 auto;
  display: flex;
  justify-content: flex-end;
  align-items: flex-start;
}

/* Messages wrapper */
.messages-wrapper {
  display: flex;
  flex-direction: column;
  gap: var(--chat-gap-mobile);
  width: 100%;
  max-width: 100%;
}

@media (min-width: 768px) {
  .messages-wrapper {
    gap: var(--chat-gap-tablet);
  }
}

@media (min-width: 1024px) {
  .messages-wrapper {
    gap: var(--chat-gap-desktop);
  }
}

/* Base message styles */
.message {
  display: flex;
  width: 100%;
  /* CRITICAL: Start with opacity 0 to avoid glassmorphism glitch */
  opacity: 0;
  transform: translateY(30px);
  visibility: hidden;
  /* Force hardware acceleration for smooth animations */
  will-change: opacity, transform;
  transform: translate3d(0, 0, 0);
}

/* Animation state management for backdrop-filter glitch fix */
.message.animating {
  /* During animation, no backdrop-filter to prevent glitch */
  backdrop-filter: none !important;
  -webkit-backdrop-filter: none !important;
  /* Optimize animation performance */
  will-change: opacity, transform;
}

.message.animation-complete {
  /* Clean up will-change after animation */
  will-change: auto;
}

.message.animation-complete .message-bubble {
  /* Apply backdrop-filter only after animation is complete */
  backdrop-filter: var(--chat-backdrop-blur);
  -webkit-backdrop-filter: var(--chat-backdrop-blur);
  transition: backdrop-filter var(--chat-backdrop-transition);
  /* Create proper stacking context for backdrop-filter */
  position: relative;
  z-index: 1;
}

/* Firefox fallback - backdrop-filter support is limited */
@supports not (backdrop-filter: blur(10px)) {
  .message.animation-complete .message-user .message-bubble {
    background: rgba(255, 255, 255, 0.25) !important;
  }

  .message.animation-complete .message-bot .message-bubble {
    background: rgba(139, 92, 246, 0.35) !important;
  }
}

/* JavaScript-detected fallback for browsers without backdrop-filter */
.no-backdrop-filter .message.animation-complete .message-user .message-bubble {
  background: rgba(255, 255, 255, 0.25) !important;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.no-backdrop-filter .message.animation-complete .message-bot .message-bubble {
  background: rgba(139, 92, 246, 0.35) !important;
  border: 1px solid rgba(139, 92, 246, 0.4);
}

/* Enhanced styling for browsers with backdrop-filter support */
.supports-backdrop-filter .message.animation-complete .message-bubble {
  backdrop-filter: var(--chat-backdrop-blur);
  -webkit-backdrop-filter: var(--chat-backdrop-blur);
}

/* When GSAP is active, it will animate these properties */
.chat-container-v2.gsap-ready .message {
  /* GSAP will handle all animations from opacity 0 to 1 */
}

/* Fallback for non-JS users - include backdrop-filter */
.no-js .message {
  opacity: 1;
  transform: none;
  visibility: visible;
}

.no-js .message-user .message-bubble {
  backdrop-filter: var(--chat-backdrop-blur);
  -webkit-backdrop-filter: var(--chat-backdrop-blur);
}

.no-js .message-bot .message-bubble {
  backdrop-filter: var(--chat-backdrop-blur);
  -webkit-backdrop-filter: var(--chat-backdrop-blur);
}

/* Improved scrolling for card images */
.card-images.scrolling {
  cursor: grabbing;
  user-select: none;
}

.card-images {
  cursor: grab;
}

/* User messages (right aligned) */
.message-user {
  justify-content: flex-end;
}

.message-user .message-bubble {
  background: rgba(255, 255, 255, 0.1);
  /* Backdrop-filter will be applied after animation via JS */
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #ffffff;
  align-self: flex-end;
  /* Force layer creation for performance */
  will-change: opacity, transform;
  transform: translate3d(0, 0, 0);
  /* Prepare for smooth backdrop-filter transition */
  transition: backdrop-filter 0.2s ease-out;
}

/* Bot messages (left aligned) */
.message-bot {
  justify-content: flex-start;
}

.message-bot .message-bubble {
  background: rgba(139, 92, 246, 0.2);
  /* Backdrop-filter will be applied after animation via JS */
  border: 1px solid rgba(139, 92, 246, 0.3);
  color: #ffffff;
  align-self: flex-start;
  /* Force layer creation for performance */
  will-change: opacity, transform;
  transform: translate3d(0, 0, 0);
  /* Prepare for smooth backdrop-filter transition */
  transition: backdrop-filter 0.2s ease-out;
}

/* Message bubble */
.message-bubble {
  max-width: 90%;
  padding: 12px 16px;
  border-radius: var(--chat-border-radius-mobile);
  word-wrap: break-word;
  overflow-wrap: break-word;
  box-shadow: var(--chat-shadow);
  transition: transform var(--chat-animation-duration) var(--chat-animation-easing);
  /* Optimize rendering performance */
  will-change: opacity, transform;
  transform: translateZ(0); /* Force GPU acceleration */
  backface-visibility: hidden; /* Prevent flicker */
  /* Create stacking context for backdrop-filter */
  isolation: isolate;
}

.message-bubble:hover {
  transform: translateY(-2px);
}

@media (min-width: 768px) {
  .message-bubble {
    max-width: 85%;
    padding: 14px 18px;
    border-radius: var(--chat-border-radius);
  }
}

@media (min-width: 1024px) {
  .message-bubble {
    max-width: 70%;
  }
}

/* Message text */
.message-text {
  margin: 0;
  line-height: 1.4;
  font-size: 14px;
  font-weight: 400;
}

@media (min-width: 768px) {
  .message-text {
    font-size: 15px;
  }
}

@media (min-width: 1024px) {
  .message-text {
    font-size: 16px;
  }
}

/* Card message with images */
.message-card {
  max-width: 95% !important;
}

@media (min-width: 768px) {
  .message-card {
    max-width: 90% !important;
  }
}

@media (min-width: 1024px) {
  .message-card {
    max-width: 80% !important;
  }
}

.card-images {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  overflow-x: auto;
  scroll-behavior: smooth;
  padding: 5px 0;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.card-images::-webkit-scrollbar {
  display: none;
}

@media (min-width: 768px) {
  .card-images {
    gap: 10px;
    margin-top: 15px;
  }
}

.card-image {
  flex-shrink: 0;
  width: 60px;
  height: 75px;
  border-radius: 8px;
  overflow: hidden;
  transition: transform var(--chat-animation-duration) var(--chat-animation-easing);
}

/* Remove hover for decorative images */
.card-image:hover {
  transform: none;
}

@media (min-width: 768px) {
  .card-image {
    width: 70px;
    height: 88px;
  }
}

@media (min-width: 1024px) {
  .card-image {
    width: 79px;
    height: 100px;
  }
}

.card-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
}

/* Action Button */
.message-card .action-button {
  width: 100%;
  margin-top: 16px;
  display: flex !important;
  flex-direction: row !important;
}

.action-button {
  display: flex !important;
  flex-direction: row !important;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 12px 24px;
  background: linear-gradient(135deg, #AF7DC5 0%, #EA9ACC 100%);
  color: #ffffff;
  text-decoration: none;
  border-radius: 26px;
  font-weight: 600;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  box-shadow: 0 4px 15px rgba(175, 125, 197, 0.3);
  min-width: 200px;
  border: none;
  cursor: default;
  pointer-events: none;
}

/* Remove hover effect for decorative button */
.action-button:hover {
  transform: none;
  box-shadow: 0 4px 15px rgba(175, 125, 197, 0.3);
  background: linear-gradient(135deg, #AF7DC5 0%, #EA9ACC 100%);
  text-decoration: none;
  color: #ffffff;
}

.action-button:active {
  transform: translateY(0);
}

@media (min-width: 768px) {
  .action-button {
    padding: 14px 28px;
    font-size: 15px;
    gap: 12px;
    min-width: 250px;
  }
}

@media (min-width: 1024px) {
  .action-button {
    padding: 15px 30px;
    font-size: 16px;
    min-width: 300px;
  }
}

.button-icon {
  display: inline-flex !important;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  flex-shrink: 0;
}

@media (min-width: 768px) {
  .button-icon {
    width: 26px;
    height: 26px;
  }
}

@media (min-width: 1024px) {
  .button-icon {
    width: 28px;
    height: 28px;
  }
}

.button-icon svg {
  width: 16px;
  height: 16px;
}

.action-button:hover .button-icon {
  background: rgba(255, 255, 255, 0.3);
}

.button-text {
  font-weight: 600;
  letter-spacing: 0.5px;
}

/* Specific preset styles */
.chat-container-v2[data-preset="skincare"] .message-bot.message:nth-child(2) .message-bubble {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: 15px;
  max-width: 95% !important;
}

@media (min-width: 768px) {
  .chat-container-v2[data-preset="skincare"] .message-bot.message:nth-child(2) .message-bubble {
    max-width: 85% !important;
  }
}

@media (min-width: 1024px) {
  .chat-container-v2[data-preset="skincare"] .message-bot.message:nth-child(2) .message-bubble {
    max-width: 75% !important;
  }
}

.chat-container-v2[data-preset="skincare"] .message-bot.message:nth-child(2) .message-text {
  flex: 1;
}

.chat-container-v2[data-preset="skincare"] .message-bot.message:nth-child(2) .card-images {
  flex-shrink: 0;
  margin-top: 0;
  order: 2;
}

/* Skincare product image sizing */
.chat-container-v2[data-preset="skincare"] .message-bot.message:nth-child(2) .card-images {
  overflow: visible;
  padding: 5px;
}

.chat-container-v2[data-preset="skincare"] .message-bot.message:nth-child(2) .card-image {
  width: 70px;
  height: 85px;
}

@media (min-width: 768px) {
  .chat-container-v2[data-preset="skincare"] .message-bot.message:nth-child(2) .card-image {
    width: 85px;
    height: 105px;
  }
}

@media (min-width: 1024px) {
  .chat-container-v2[data-preset="skincare"] .message-bot.message:nth-child(2) .card-image {
    width: 100px;
    height: 125px;
  }
}

/* Remove hover for skincare preset - decorative images */
.chat-container-v2[data-preset="skincare"] .message-bot.message:nth-child(2) .card-image:hover {
  transform: none;
}

/* Yoga fitness preset - wider bubble for 4 images */
.chat-container-v2[data-preset="yoga_fitness"] .message-card {
  max-width: 100% !important; /* Allow full width on mobile */
  padding: 14px !important;
}

/* First message bubble (sports bra) - width and alignment */
.chat-container-v2[data-preset="yoga_fitness"] .message-user:first-child .message-bubble {
  max-width: 280px !important;
  width: auto;
  align-self: flex-end;
}

@media (min-width: 480px) {
  .chat-container-v2[data-preset="yoga_fitness"] .message-user:first-child .message-bubble {
    max-width: 280px;
    width: 280px;
  }
}

@media (min-width: 768px) {
  .chat-container-v2[data-preset="yoga_fitness"] .message-user:first-child .message-bubble {
    max-width: 325px;
    width: 325px;
  }
}

/* Ensure sports bra image shows in first user message */
.chat-container-v2[data-preset="yoga_fitness"] .message-user:first-child .card-images {
  display: flex !important;
  justify-content: center;
  margin-top: 12px;
  width: 100%;
  max-width: 250px;
}

.chat-container-v2[data-preset="yoga_fitness"] .message-user:first-child .card-image {
  width: 100%;
  height: 120px;
  overflow: hidden;
  border-radius: 8px;
  cursor: default;
}

.chat-container-v2[data-preset="yoga_fitness"] .message-user:first-child .card-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  cursor: default;
}

@media (min-width: 480px) {
  .chat-container-v2[data-preset="yoga_fitness"] .message-user:first-child .card-images {
    max-width: 100%;
  }

  .chat-container-v2[data-preset="yoga_fitness"] .message-user:first-child .card-image {
    height: 138px;
  }
}

/* Yoga preset decorative elements - no interactions */
.chat-container-v2[data-preset="yoga_fitness"] .card-image {
  cursor: default;
}

.chat-container-v2[data-preset="yoga_fitness"] .action-button {
  cursor: default;
  pointer-events: none;
  display: flex !important;
  flex-direction: row !important;
  width: 100% !important;
  margin-top: 20px !important;
}

/* Desktop: Add right padding to bot messages in yoga preset */
@media (min-width: 1024px) {
  .chat-container-v2[data-preset="yoga_fitness"] .message-bot .message-bubble {
    margin-right: 80px;
  }
}

@media (min-width: 1440px) {
  .chat-container-v2[data-preset="yoga_fitness"] .message-bot .message-bubble {
    margin-right: 120px;
  }
}

.chat-container-v2[data-preset="yoga_fitness"] .card-images {
  display: flex;
  justify-content: flex-start;
  flex-wrap: nowrap;
  gap: 8px;
  overflow-x: auto;
  max-width: 100%;
  padding: 0;
  margin-top: 12px;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.chat-container-v2[data-preset="yoga_fitness"] .card-images::-webkit-scrollbar {
  display: none;
}

.chat-container-v2[data-preset="yoga_fitness"] .card-image {
  flex: 0 0 auto;
  width: 60px;
  height: 75px;
  border-radius: 8px;
  overflow: hidden;
}

@media (min-width: 480px) {
  .chat-container-v2[data-preset="yoga_fitness"] .message-card {
    max-width: 95% !important;
  }

  .chat-container-v2[data-preset="yoga_fitness"] .card-images {
    gap: 10px;
  }

  .chat-container-v2[data-preset="yoga_fitness"] .card-image {
    width: 70px;
    height: 88px;
  }
}

@media (min-width: 768px) {
  .chat-container-v2[data-preset="yoga_fitness"] .message-card {
    max-width: 450px !important; /* Fixed width to accommodate 4 images */
    padding: 16px !important;
  }

  .chat-container-v2[data-preset="yoga_fitness"] .card-images {
    overflow-x: visible;
    justify-content: flex-start;
    gap: 12px;
  }

  .chat-container-v2[data-preset="yoga_fitness"] .card-image {
    width: 85px;
    height: 107px;
    border-radius: 10px;
  }
}

@media (min-width: 1024px) {
  .chat-container-v2[data-preset="yoga_fitness"] .message-card {
    max-width: 500px !important; /* Wider for desktop */
    padding: 18px !important;
  }

  .chat-container-v2[data-preset="yoga_fitness"] .card-images {
    gap: 15px;
  }

  .chat-container-v2[data-preset="yoga_fitness"] .card-image {
    width: 95px;
    height: 119px;
    border-radius: 12px;
  }
}

/* Better button spacing for all presets - using more compatible selector */
.chat-container-v2 .message-bubble {
  text-align: left;
}

/* Removed conflicting display: block that was causing vertical layout */

/* Fallback for browsers without :has() support - removed width override */

@media (min-width: 768px) {
  .chat-container-v2 .message-bubble:has(.action-button),
  .chat-container-v2 .message-bubble .action-button {
    padding: 12px;
  }
}

/* Typing indicator styles */
.typing-indicator .message-bubble {
  min-width: 60px;
  padding: 12px 16px;
}

.typing-dots {
  display: flex;
  align-items: center;
  gap: 4px;
}

.typing-dots span {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.4;
}

/* Animation keyframes */
@keyframes messageSlideIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Loading animation */
.message-loading {
  opacity: 0.6;
}

.message-loading::after {
  content: '';
  display: inline-block;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: currentColor;
  animation: loadingDots 1.4s infinite ease-in-out;
  animation-fill-mode: both;
  margin-left: 4px;
}

@keyframes loadingDots {
  0%, 80%, 100% {
    opacity: 0;
    transform: scale(0);
  }
  40% {
    opacity: 1;
    transform: scale(1);
  }
}

/* Accessibility improvements */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
  .message-bubble {
    border: 2px solid;
  }

  .action-button {
    border: 2px solid #ffffff;
  }
}

/* Enhanced focus styles for accessibility */
.action-button:focus-visible {
  outline: 2px solid #ffffff;
  outline-offset: 2px;
  box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.3);
}

.card-image:focus-visible {
  outline: 2px solid #ffffff;
  outline-offset: 2px;
}

/* Skip link for keyboard navigation */
.chat-container-v2 .skip-link {
  position: absolute;
  top: -40px;
  left: 6px;
  background: #000;
  color: #fff;
  padding: 8px;
  text-decoration: none;
  border-radius: 4px;
  font-size: 14px;
  z-index: 1000;
}

.chat-container-v2 .skip-link:focus {
  top: 6px;
}

/* Enhanced loading states */
.chat-container-v2.loading .message {
  opacity: 0.3;
  pointer-events: none;
}

.chat-container-v2.loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 40px;
  height: 40px;
  border: 4px solid rgba(255, 255, 255, 0.3);
  border-top: 4px solid #ffffff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: translate(-50%, -50%) rotate(0deg); }
  100% { transform: translate(-50%, -50%) rotate(360deg); }
}

/* Enhanced error states */
.message.error .message-bubble {
  background: rgba(255, 59, 48, 0.2) !important;
  border-color: rgba(255, 59, 48, 0.3) !important;
  color: #ff3b30 !important;
}

.message.error .message-text::before {
  content: '⚠️ ';
  margin-right: 8px;
}

/* Marquee Card Styles */
.marquee-card {
  max-width: 95% !important;
  padding: 0 !important;
  overflow: hidden;
}

.marquee-card.marquee-enabled {
  padding: 20px 0 !important;
}

.card-images-marquee {
  width: 100%;
  overflow: hidden;
  position: relative;
  padding: 10px 0;
}

.marquee-wrapper {
  width: 100%;
  overflow: hidden;
}

.marquee-content {
  display: flex;
  gap: 12px;
  width: fit-content;
  padding: 5px 0;
}

/* Marquee animation styles */
.marquee-enabled .marquee-content {
  display: flex;
  gap: 12px;
  animation: marquee-left var(--marquee-duration, 20s) linear infinite;
}

.marquee-enabled.marquee-right .marquee-content {
  animation: marquee-right var(--marquee-duration, 20s) linear infinite;
}

/* Sección Especial preset specific styles */
.chat-container-v2[data-preset="seccion_especial"] .message-bubble:not(.marquee-card) {
  padding: 16px !important;
}

.chat-container-v2[data-preset="seccion_especial"] .marquee-card {
  padding: 20px 0 !important;
  max-width: 100% !important;
  overflow: hidden;
}

.chat-container-v2[data-preset="seccion_especial"] .card-images-marquee {
  padding: 15px 0;
  margin: 0;
  overflow: hidden;
  width: 100%;
}

.chat-container-v2[data-preset="seccion_especial"] .marquee-content {
  gap: 15px;
  padding: 0;
}

.chat-container-v2[data-preset="seccion_especial"] .card-image {
  width: 78px;
  height: 78px;
  border-radius: 10px;
  overflow: hidden;
  flex-shrink: 0;
}

@media (min-width: 768px) {
  .chat-container-v2[data-preset="seccion_especial"] .card-image {
    width: 90px;
    height: 113px;
  }
}

@media (min-width: 1024px) {
  .chat-container-v2[data-preset="seccion_especial"] .card-image {
    width: 100px;
    height: 125px;
  }
}

.marquee-enabled .card-image {
  flex-shrink: 0;
  width: 60px;
  height: 75px;
}

@media (min-width: 768px) {
  .marquee-enabled .card-image {
    width: 70px;
    height: 88px;
  }
}

@media (min-width: 1024px) {
  .marquee-enabled .card-image {
    width: 79px;
    height: 100px;
  }
}

/* Marquee keyframe animations */
@keyframes marquee-left {
  0% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(-33.333%);
  }
}

@keyframes marquee-right {
  0% {
    transform: translateX(-33.333%);
  }
  100% {
    transform: translateX(0);
  }
}

/* Pause marquee on hover */
.marquee-enabled:hover .marquee-content {
  animation-play-state: paused;
}

/* Print styles */
@media print {
  .chat-container-v2 {
    background: none !important;
    color: black !important;
  }

  .message-bubble {
    background: #f0f0f0 !important;
    color: black !important;
    box-shadow: none !important;
    border: 1px solid #ccc !important;
  }

  .action-button {
    background: #333 !important;
    color: white !important;
    border: 2px solid #000 !important;
  }

  .card-images {
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 8px !important;
  }

  .card-image {
    break-inside: avoid !important;
  }
}