/**
 * Chat Modal Styles
 * AI Concierge chat interface styling
 */

/* Modal Overlay */
.chat-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 10002;
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  padding: 20px;
  animation: fade-in 0.3s ease;
}

@keyframes fade-in {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

/* Modal Container */
.chat-modal-container {
  background: white;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  width: 420px;
  max-width: 100%;
  height: 600px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: slide-up 0.3s ease;

  /* Prevent text selection during swipe */
  user-select: none;
}

@keyframes slide-up {
  from {
    transform: translateY(100%);
    opacity: 0;
  }

  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.chat-modal-container.minimized {
  height: 60px;
}

.chat-modal-container.minimized .chat-modal-messages,
.chat-modal-container.minimized .chat-modal-input {
  display: none;
}

/* Header */
.chat-modal-header {
  background: linear-gradient(135deg, #0dae9c 0%, #5fd4c4 100%);
  color: white;
  padding: 16px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
  border-radius: 16px 16px 0 0;
}

.chat-header-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  font-size: 16px;
}

.chat-header-title i {
  font-size: 20px;
}

/* Context Badge */
.chat-context-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 500;
  margin-left: 8px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.chat-context-badge.anonymous-context {
  background: rgba(255, 255, 255, 0.2);
  color: rgba(255, 255, 255, 0.9);
}

.chat-context-badge.authenticated-context {
  background: rgba(72, 187, 120, 0.3);
  color: white;
}

.chat-context-badge.booking-context {
  background: rgba(251, 191, 36, 0.3);
  color: white;
}

.chat-context-badge.flight-context {
  background: rgba(59, 130, 246, 0.3);
  color: white;
}

.chat-header-actions {
  display: flex;
  gap: 8px;
}

.chat-header-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}

.chat-header-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* Messages Area */
.chat-modal-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  background: #f8f9fa;
}

.chat-modal-messages::-webkit-scrollbar {
  width: 6px;
}

.chat-modal-messages::-webkit-scrollbar-thumb {
  background: #cbd5e0;
  border-radius: 3px;
}

.chat-modal-messages::-webkit-scrollbar-thumb:hover {
  background: #a0aec0;
}

/* Messages */
.user-message,
.assistant-message {
  display: flex;
  gap: 12px;
  animation: message-slide 0.3s ease;
}

@keyframes message-slide {
  from {
    opacity: 0;
    transform: translateY(10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.user-message {
  justify-content: flex-end;
}

.assistant-message {
  justify-content: flex-start;
}

.message-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg, #0dae9c 0%, #5fd4c4 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 18px;
}

.message-content {
  max-width: 75%;
  padding: 12px 16px;
  border-radius: 12px;
  line-height: 1.5;
  font-size: 14px;
  word-wrap: break-word;
  overflow-wrap: break-word;
  word-break: break-word;
}

.user-message .message-content {
  background: #0dae9c;
  color: white;
  border-bottom-right-radius: 4px;
}

.assistant-message .message-content {
  background: white;
  color: #2d3748;
  border-bottom-left-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

/* System Messages */
.system-message {
  margin: 16px 0;
  text-align: center;
  animation: message-slide 0.3s ease;
}

.system-message-content {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: rgba(13, 174, 156, 0.1);
  color: #0dae9c;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
}

.system-message-content i {
  font-size: 14px;
}

/* Message Context Badges */
.message-context-badge {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 10px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 4px;
  margin-right: 8px;
}

.message-context-badge.booking-context {
  background: rgba(251, 191, 36, 0.15);
  color: #d97706;
}

.message-context-badge.flight-context {
  background: rgba(59, 130, 246, 0.15);
  color: #2563eb;
}

.message-context-badge.header-context {
  background: rgba(107, 114, 128, 0.15);
  color: #6b7280;
}

/* Markdown Styling in Messages */
.message-content h2 {
  font-size: 18px;
  font-weight: 600;
  margin: 12px 0 8px;
  color: #1a202c;
}

.message-content h3 {
  font-size: 16px;
  font-weight: 600;
  margin: 10px 0 6px;
  color: #2d3748;
}

.message-content p {
  margin: 8px 0;
}

.message-content ul,
.message-content ol {
  margin: 8px 0;
  padding-left: 20px;
}

.message-content li {
  margin: 4px 0;
}

.message-content code {
  background: #f1f5f9;
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'Monaco', 'Courier New', monospace;
  font-size: 13px;
  color: #e53e3e;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message-content code.chat-copyable-code {
  cursor: pointer;
}

.message-content code.chat-copyable-code:hover {
  background: #e2e8f0;
}

.message-content code.chat-copyable-code.chat-code-copied {
  background: #dcfce7;
  color: #16a34a;
}

.message-content pre {
  background: #1a202c;
  color: #e2e8f0;
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 12px 0;
}

.message-content pre code {
  background: transparent;
  padding: 0;
  color: #e2e8f0;
  font-size: 13px;
}

.message-content a {
  color: #0dae9c;
  text-decoration: underline;
}

.message-content a:hover {
  color: #0b9987;
}

.message-content .chat-action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 118px;
  height: 32px;
  padding: 0 12px;
  margin: 0;
  border-radius: 999px;
  border: 1px solid #e2e8f0;
  background: #fff;
  color: #0f172a;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
}

.message-content .chat-action-btn i {
  font-size: 12px;
}

.message-content .chat-action-btn:hover {
  background: #f8fafc;
}

.message-content .chat-action-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.message-content .chat-action-book {
  border-color: rgba(13, 174, 156, 0.35);
}

.message-content .chat-action-favorite.favorited {
  border-color: rgba(229, 62, 62, 0.35);
  background: #fff5f5;
}

.message-content .chat-action-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin: 6px 0 10px;
}

.message-content strong {
  font-weight: 600;
  color: #1a202c;
}

/* Markdown Tables */
.chat-markdown-table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 13px;
}

.chat-markdown-table th,
.chat-markdown-table td {
  padding: 8px 12px;
  border: 1px solid #e2e8f0;
  text-align: left;
}

.chat-markdown-table th {
  background: #f7fafc;
  font-weight: 600;
  color: #2d3748;
}

.chat-markdown-table tr:nth-child(even) {
  background: #f8f9fa;
}

.chat-markdown-table tr:hover {
  background: #edf2f7;
}

.chat-markdown-table code {
  background: #f1f5f9;
  padding: 2px 4px;
  border-radius: 3px;
  font-size: 12px;
}

/* Typing Animation - Wave Dots in Message Bubble */
.typing-bubble .message-content {
  min-height: 44px;
  display: flex;
  align-items: center;
}

.typing-wave {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 0;
}

.typing-wave span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #0dae9c;
  animation: wave 1.3s ease-in-out infinite;
}

.typing-wave span:nth-child(2) {
  animation-delay: 0.15s;
}

.typing-wave span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes wave {
  0%, 60%, 100% {
    transform: translateY(0);
  }

  30% {
    transform: translateY(-8px);
  }
}

/* Fade out animation for typing dots */
.typing-wave.fade-out {
  animation: fade-out 0.2s ease-out forwards;
}

@keyframes fade-out {
  to {
    opacity: 0;
  }
}

/* Input Area */
.chat-modal-input {
  padding: 16px 20px;
  background: white;
  border-top: 1px solid #e2e8f0;
  display: flex;
  gap: 12px;
  align-items: flex-end;
  flex-shrink: 0;
  position: relative;

  /* Ensure input area doesn't get covered */
  z-index: 100;
}

.chat-input-textarea {
  flex: 1;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 14px;
  font-family: inherit;
  resize: none;
  max-height: 120px;
  transition: border-color 0.2s;
  overflow-y: auto;

  /* Hide scrollbar for all browsers */
  scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* IE and Edge */
}

/* Hide scrollbar for Chrome, Safari and Opera */
.chat-input-textarea::-webkit-scrollbar {
  display: none;
}

.chat-input-textarea:focus {
  outline: none;
  border-color: #0dae9c;
}

.chat-input-textarea::placeholder {
  color: #a0aec0;
}

.chat-input-textarea:disabled {
  background: #f1f5f9;
  cursor: not-allowed;
  opacity: 0.7;
}

.chat-send-button {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  border: none;
  background: linear-gradient(135deg, #0dae9c 0%, #5fd4c4 100%);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  transition: transform 0.2s, opacity 0.2s;
  flex-shrink: 0;
}

.chat-send-button:hover:not(:disabled) {
  transform: scale(1.05);
}

.chat-send-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.chat-char-counter {
  position: absolute;
  bottom: 76px;
  right: 24px;
  background: white;
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 12px;
  color: #718096;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* Welcome Message */
.chat-welcome-message {
  animation: message-slide 0.3s ease;
}

/* Queued Message Styles */
.queued-message .chat-message {
  opacity: 0.7;
  position: relative;
}

.queue-indicator {
  display: block;
  font-size: 12px;
  color: #888;
  margin-top: 4px;
  font-style: italic;
}

.chat-send-button.queued {
  background: linear-gradient(135deg, #667eea 0%, #f7a800 100%);
  animation: pulse-queued 2s infinite;
}

@keyframes pulse-queued {
  0%, 100% {
    opacity: 0.8;
  }

  50% {
    opacity: 1;
  }
}

/* Better touch targets for mobile */
.chat-header-btn,
.chat-send-button {
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* Allow text selection in message content */
.user-message .message-content,
.assistant-message .message-content {
  user-select: text;
}

/* Mobile Styles */
@media (max-width: 768px) {
  .chat-modal-overlay {
    padding: 0;
    align-items: stretch;
    justify-content: stretch;
  }

  .chat-modal-container {
    --chat-mobile-input-gap: 14px;

    width: 100%;

    /* Use fixed positioning for better keyboard handling */
    position: fixed;
    inset: 0;

    /* Use dynamic viewport height for true fullscreen on mobile */
    /* This automatically accounts for browser chrome and keyboard */

    /* Fallback for browsers without dvh support */

    /* dvh override (browsers read this last if supported) */
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
    display: flex;
    flex-direction: column;

    /* Ensure the container doesn't go behind the keyboard */
    transform: translateZ(0); /* Enable hardware acceleration */

    /* Prevent accidental swipe-to-close */
    touch-action: pan-y;
    overscroll-behavior: contain;

    /* Smooth transitions */
    transition: transform 0.3s ease, opacity 0.3s ease;
  }

  .chat-modal-container.mobile {
    animation: slide-up-mobile 0.3s ease;
  }

  /* Adjust when keyboard is visible - height will be set via JavaScript */
  .chat-modal-container.keyboard-visible {
    /* dvh automatically handles keyboard visibility */
    height: 100dvh;
    max-height: 100dvh;
    transition: height 0.2s ease;
  }

  /* When keyboard is visible, fix input area at bottom with gap */
  .chat-modal-container.keyboard-visible .chat-modal-input {
    position: fixed;
    bottom: calc(var(--chat-mobile-input-gap) + env(safe-area-inset-bottom, 0px));
    left: 0;
    right: 0;
    background: white;
    z-index: 10001;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
  }

  /* Add extra bottom margin when keyboard is not visible for better UX */
  .chat-modal-container:not(.keyboard-visible) .chat-modal-input {
    margin-bottom: env(safe-area-inset-bottom, 0);
  }

  @keyframes slide-up-mobile {
    from {
      transform: translateY(100%);
    }

    to {
      transform: translateY(0);
    }
  }

  .chat-modal-header {
    border-radius: 0;
    padding: 12px 16px;
    flex-shrink: 0; /* Don't shrink when keyboard appears */
  }

  .chat-modal-messages {
    /* Allow messages area to shrink when keyboard appears */
    flex: 1 1 auto;
    min-height: 0; /* Important for flex shrinking */
    overflow: hidden auto;
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
    overscroll-behavior: none; /* Prevent overscroll bounce that shows gap */

    /* Prevent accidental swipe when scrolling */
    touch-action: pan-y;

    /* Add padding to prevent content from being hidden by absolute input */
    /* Input height: 16px top + 48px button + 20px bottom + safe-area ≈ 100px */
    padding-bottom: calc(100px + env(safe-area-inset-bottom, 0px) + var(--chat-mobile-input-gap));
  }

  .chat-modal-input {
    flex-shrink: 0;

    /* Add proper padding for mobile with safe area support */
    padding: var(--chat-mobile-input-gap) 20px;
    padding-bottom: calc(var(--chat-mobile-input-gap) + env(safe-area-inset-bottom, 0px));
    background: white;
    z-index: 10; /* Ensure input stays above other content */

    /* Use absolute positioning to ensure input stays at the bottom */
    /* This works better with mobile keyboards than sticky */
    position: absolute;
    bottom: env(safe-area-inset-bottom, 0);
    left: 0;
    right: 0;
  }

  #chat-minimize-btn {
    display: none;
  }

  .message-content {
    max-width: 85%;
    font-size: 15px;
  }

  .chat-header-btn {
    width: 36px;
    height: 36px;
    min-width: 44px; /* Touch target */
    min-height: 44px;
  }

  .chat-send-button {
    width: 48px;
    height: 48px;
    min-width: 44px;
    min-height: 44px;
  }

  .chat-input-textarea {
    font-size: 16px; /* Prevent zoom on iOS */

    /* Better touch handling */
    touch-action: manipulation;
  }

  /* Prevent body scroll when modal is open */
  body.chat-modal-open {
    overflow: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
    overscroll-behavior: none;
    touch-action: none;
  }
}

/* Dark mode support (optional) */
@media (prefers-color-scheme: dark) {
  .chat-modal-container {
    background: #1a202c;
  }

  .chat-modal-messages {
    background: #2d3748;
  }

  .assistant-message .message-content {
    background: #4a5568;
    color: #e2e8f0;
  }

  .chat-modal-input {
    background: #1a202c;
    border-top-color: #4a5568;
  }

  .chat-input-textarea {
    background: #2d3748;
    border-color: #4a5568;
    color: #e2e8f0;
  }

  .chat-input-textarea:focus {
    border-color: #0dae9c;
  }

  .message-content h2,
  .message-content h3,
  .message-content strong {
    color: #e2e8f0;
  }

  .message-content code {
    background: #2d3748;
    color: #fc8181;
  }

  .chat-markdown-table th,
  .chat-markdown-table td {
    border-color: #4a5568;
  }

  .chat-markdown-table th {
    background: #2d3748;
    color: #e2e8f0;
  }

  .chat-markdown-table tr:nth-child(even) {
    background: #2d3748;
  }

  .chat-markdown-table tr:hover {
    background: #4a5568;
  }
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {
  .chat-modal-overlay,
  .chat-modal-container,
  .user-message,
  .assistant-message,
  .chat-welcome-message {
    animation: none;
  }

  .chat-send-button:hover:not(:disabled) {
    transform: none;
  }
}

/* Print styles */
@media print {
  .chat-modal-overlay {
    display: none;
  }
}
