* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  /* iOS Safari keyboard fix - prevent zoom/scale */
  height: 100%;
  height: -webkit-fill-available;
  overflow: hidden;
  touch-action: manipulation;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: #f5f5f5;
  min-height: 100vh;
  min-height: -webkit-fill-available;
  overflow: hidden;
  /* iOS Safari keyboard fix */
  position: fixed;
  width: 100%;
  height: 100%;
  -webkit-overflow-scrolling: touch;
  touch-action: manipulation;
}

/* === FAB (Floating Action Button) === */
.chat-fab {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  color: white;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  z-index: 1000;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.chat-fab:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.5);
}

.chat-fab:active {
  transform: scale(0.95);
}

.fab-label {
  font-size: 10px;
  font-weight: 600;
  margin-top: 2px;
}

.badge {
  position: absolute;
  top: 4px;
  right: 4px;
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  background: #ff3b30;
  color: white;
  font-size: 11px;
  font-weight: bold;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 0 6px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.chat-fab.has-unread .badge {
  display: flex;
}

/* === Chat Widget === */
.chat-widget {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 400px;
  max-width: calc(100vw - 40px);
  height: 600px;
  max-height: calc(100vh - 40px);
  background: white;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 1001;
  transition: opacity 0.3s ease, transform 0.3s ease, visibility 0.3s;
}

.chat-hidden {
  opacity: 0;
  visibility: hidden;
  transform: scale(0.9) translateY(20px);
  pointer-events: none;
}

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

.chat-title {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.chat-title strong {
  font-size: 16px;
  font-weight: 600;
}

.subtitle {
  font-size: 12px;
  opacity: 0.9;
  display: flex;
  align-items: center;
  gap: 6px;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

.dot-green {
  background: #34c759;
  box-shadow: 0 0 8px rgba(52, 199, 89, 0.6);
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

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

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

.icon-btn:active {
  background: rgba(255, 255, 255, 0.4);
}

/* === Chat Messages Area === */
.chat {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  background: #f8f9fa;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

.msg {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: 16px;
  word-wrap: break-word;
  line-height: 1.4;
  font-size: 15px;
  animation: slideIn 0.3s ease-out;
}

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

.msg.user {
  align-self: flex-end;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-bottom-right-radius: 4px;
}

.msg.bot {
  align-self: flex-start;
  background: white;
  color: #1a1a1a;
  border: 1px solid #e5e7eb;
  border-bottom-left-radius: 4px;
}

/* === Composer (CRITICAL FOR MOBILE) === */
.composer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: white;
  border-top: 1px solid #e5e7eb;
  flex-shrink: 0;
  /* IMPORTANT: Prevent the composer from shrinking */
  min-height: 64px;
}

.composer input {
  flex: 1;
  min-width: 0; /* Allow input to shrink if needed */
  padding: 10px 14px;
  border: 1px solid #e5e7eb;
  border-radius: 20px;
  font-size: 16px; /* Prevent iOS zoom */
  outline: none;
  background: #f8f9fa;
  transition: border-color 0.2s ease, background 0.2s ease;
  /* IMPORTANT: Set max-width to ensure button has space */
  max-width: calc(100% - 90px);
}

.composer input:focus {
  border-color: #667eea;
  background: white;
}

.composer button {
  /* CRITICAL: Prevent button from shrinking */
  flex-shrink: 0;
  min-width: 70px;
  height: 40px;
  padding: 0 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 20px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s ease, box-shadow 0.2s ease;
  white-space: nowrap;
}

.composer button:hover {
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.composer button:active {
  transform: scale(0.95);
}

.composer button:disabled {
  background: #d1d5db;
  cursor: not-allowed;
  opacity: 0.6;
}

/* === Footer === */
.chat-footer {
  padding: 8px 16px;
  background: #f8f9fa;
  border-top: 1px solid #e5e7eb;
  text-align: center;
  font-size: 12px;
  color: #6b7280;
  flex-shrink: 0;
}

/* === Mobile Optimizations === */
@media (max-width: 768px) {
  .chat-widget {
    position: fixed;
    bottom: 0;
    right: 0;
    left: 0;
    top: 0;
    width: 100%;
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height - adjusts with keyboard */
    max-width: 100%;
    max-height: 100vh;
    max-height: 100dvh;
    border-radius: 0;
    margin: 0;
    /* Prevent zoom/scale on iOS */
    touch-action: manipulation;
  }

  .chat-header {
    border-radius: 0;
    padding: 12px 16px;
    /* Handle notch on iOS */
    padding-top: max(12px, env(safe-area-inset-top));
  }

  .chat-fab {
    bottom: 16px;
    right: 16px;
    width: 56px;
    height: 56px;
  }

  /* CRITICAL: Mobile composer optimization */
  .composer {
    padding: 10px 12px;
    /* Handle home indicator on iOS */
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    gap: 8px;
    min-height: 60px;
    /* Prevent iOS scaling/zooming */
    touch-action: manipulation;
  }

  .composer input {
    font-size: 16px; /* Prevent iOS zoom */
    padding: 11px 14px;
    /* IMPORTANT: Ensure button always visible */
    max-width: calc(100% - 85px);
    min-height: 42px;
    /* iOS keyboard fix */
    touch-action: manipulation;
    -webkit-appearance: none;
    appearance: none;
  }

  .composer button {
    /* CRITICAL: Fixed width ensures visibility */
    min-width: 70px;
    width: 70px;
    height: 42px;
    padding: 0 16px;
    font-size: 14px;
    flex-shrink: 0;
  }

  .msg {
    max-width: 85%;
  }

  .chat {
    padding: 12px;
  }

  .chat-footer {
    padding: 6px 12px;
    padding-bottom: max(6px, calc(env(safe-area-inset-bottom) / 2));
  }
}

/* === Extra small screens (iPhone SE, etc.) === */
@media (max-width: 375px) {
  .composer {
    padding: 8px 10px;
    gap: 6px;
  }

  .composer input {
    padding: 10px 12px;
    font-size: 16px;
    /* Reduce max-width to give button more room */
    max-width: calc(100% - 75px);
    min-height: 40px;
  }

  .composer button {
    min-width: 65px;
    width: 65px;
    height: 40px;
    padding: 0 12px;
    font-size: 14px;
  }

  .chat-title strong {
    font-size: 15px;
  }

  .icon-btn {
    width: 32px;
    height: 32px;
  }
}

/* === iOS Safari keyboard fix === */
@supports (-webkit-touch-callout: none) {
  /* iOS-specific fix for keyboard viewport issues */
  body {
    min-height: -webkit-fill-available;
  }

  @media (max-width: 768px) {
    .chat-widget {
      height: -webkit-fill-available;
      max-height: -webkit-fill-available;
    }
  }
}

/* === Landscape mobile optimization === */
@media (max-width: 768px) and (orientation: landscape) {
  .chat-widget {
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
  }

  .composer {
    padding: 8px 12px;
    min-height: 56px;
  }

  .composer input {
    min-height: 38px;
    padding: 9px 12px;
  }

  .composer button {
    height: 38px;
    min-width: 65px;
    width: 65px;
  }
}

/* === Scrollbar === */
.chat::-webkit-scrollbar {
  width: 6px;
}

.chat::-webkit-scrollbar-track {
  background: transparent;
}

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

.chat::-webkit-scrollbar-thumb:hover {
  background: #9ca3af;
}

/* === Focus states for accessibility === */
.composer input:focus-visible,
.composer button:focus-visible,
.icon-btn:focus-visible,
.chat-fab:focus-visible {
  outline: 2px solid #667eea;
  outline-offset: 2px;
}

