/* Chat Widget Styles - Production Ready v2 */

/* CSS Variables */
:root {
  --primary-color: #2f5061;
  --primary-hover: #4297a0;
  --secondary-color: #e57f84;
  --success-color: #10b981;
  --background: #f4eae6;
  --surface: #f8fafc;
  --border: #e2e8f0;
  --text-primary: #1e293b;
  --text-secondary: #64748b;
  --shadow: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
}

/* Base Reset */
* {
  box-sizing: border-box;
}

html, body {
  margin: 0 !important;
  padding: 0 !important;
  background: transparent !important;
  overflow: visible !important;
}

body.chat-open-mobile {
  position: fixed;
  width: 100%;
  height: 100%;
  overflow: hidden !important;
}

/* Widget Container - CRITICAL FIX */
.chat-widget {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: auto;
  height: auto;
  z-index: 10000;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  pointer-events: none;
}

.chat-widget > * {
  pointer-events: auto;
}

/* Toggle Button */
.chat-toggle {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary-color), var(--primary-hover));
  border: none;
  cursor: pointer;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  position: relative;
}

.chat-toggle:hover {
  transform: scale(1.05);
  box-shadow: 0 25px 50px -12px rgb(0 0 0 / 0.25);
}

.chat-toggle .chat-icon,
.chat-toggle .close-icon {
  width: 32px;
  height: 32px;
  color: white;
  border-radius: 50%;
  object-fit: cover;
  transition: all 0.3s ease;
}

.hidden {
  display: none !important;
}

/* Chat Window - NO TRANSFORM CONFLICTS */
.chat-window {
  position: absolute;
  bottom: 80px;
  right: 0;
  width: 380px;
  height: 600px;
  background: var(--background);
  border-radius: 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

.chat-window.show {
  opacity: 1;
  visibility: visible;
}

/* Header */
.chat-header {
  background: linear-gradient(135deg, var(--primary-color), var(--primary-hover));
  color: white;
  padding: 16px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.header-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}

.header-text h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

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

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--success-color);
  animation: pulse 2s infinite;
}

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

.minimize-btn {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: background 0.2s ease;
}

.minimize-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.minimize-btn svg {
  width: 20px;
  height: 20px;
}

/* Messages Container */
.messages-container {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.messages-container::-webkit-scrollbar {
  width: 4px;
}

.messages-container::-webkit-scrollbar-track {
  background: transparent;
}

.messages-container::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

/* Messages */
.message {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.user-message {
  flex-direction: row-reverse;
}

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 16px;
}

.user-message .message-avatar {
  background: var(--primary-color);
  color: white;
}

.message-content {
  flex: 1;
  max-width: calc(100% - 44px);
}

.message-text {
  background: var(--surface);
  padding: 12px 16px;
  border-radius: 18px;
  color: var(--text-primary);
  word-wrap: break-word;
  line-height: 1.8;
  white-space: pre-wrap;
}

.user-message .message-text {
  background: var(--primary-color);
  color: white;
  border-bottom-right-radius: 4px;
}

.bot-message .message-text {
  border-bottom-left-radius: 4px;
}

.message-text a {
  color: var(--primary-hover);
  text-decoration: underline;
}

.user-message .message-text a {
  color: white;
}

.message-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.quick-action {
  background: var(--background);
  border: 1px solid var(--border);
  padding: 8px 12px;
  border-radius: 20px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
  color: var(--text-secondary);
  white-space: nowrap;
}

.quick-action:hover {
  background: var(--primary-color);
  color: white;
  border-color: var(--primary-color);
  transform: translateY(-1px);
}

/* Typing Indicator */
.typing-indicator {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 0 20px 16px;
}

.typing-animation {
  display: flex;
  gap: 4px;
  align-items: center;
  background: var(--surface);
  padding: 12px 16px;
  border-radius: 18px;
  border-bottom-left-radius: 4px;
}

.typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-secondary);
  animation: typing 1.4s infinite ease-in-out;
}

.typing-dot:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-dot:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typing {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-10px);
    opacity: 1;
  }
}

/* Input Area */
.input-area {
  border-top: 1px solid var(--border);
  background: var(--background);
  flex-shrink: 0;
}

.input-container {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  padding: 16px 20px;
}

#message-input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 12px 16px;
  font-family: inherit;
  font-size: 14px;
  resize: none;
  outline: none;
  max-height: 100px;
  min-height: 44px;
  height: 44px;
  transition: border-color 0.2s ease;
  background: white;
  line-height: 1.5;
}

#message-input:focus {
  border-color: var(--primary-color);
}

#message-input::placeholder {
  color: var(--text-secondary);
}

.send-button {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--primary-color);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  flex-shrink: 0;
}

.send-button:hover {
  background: var(--primary-hover);
  transform: scale(1.05);
}

.send-button:disabled {
  background: var(--border);
  cursor: not-allowed;
  transform: none;
}

.send-button svg {
  width: 18px;
  height: 18px;
  color: white;
}

.input-footer {
  padding: 8px 20px 12px;
  text-align: center;
}

.powered-by {
  font-size: 11px;
  color: var(--text-secondary);
}

/* Lead Form */
.lead-form-card {
  background: var(--surface);
  border-radius: 12px;
  padding: 20px;
  margin-top: 8px;
}

.lead-form-card h4 {
  margin: 0 0 8px 0;
  color: var(--text-primary);
  font-size: 16px;
  font-weight: 600;
}

.lead-form-card p {
  margin: 0 0 16px 0;
  color: var(--text-secondary);
  font-size: 13px;
}

.lead-form-card form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.lead-form-card input,
.lead-form-card select,
.lead-form-card textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: 'Inter', sans-serif;
  font-size: 14px;
  transition: border-color 0.2s;
  box-sizing: border-box;
}

.lead-form-card input:focus,
.lead-form-card select:focus,
.lead-form-card textarea:focus {
  outline: none;
  border-color: var(--primary-color);
}

.lead-form-card textarea {
  resize: vertical;
  min-height: 60px;
}

.submit-lead-btn {
  background: var(--primary-color);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 12px 20px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.submit-lead-btn:hover:not(:disabled) {
  background: var(--primary-hover);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(47, 80, 97, 0.3);
}

.submit-lead-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-loader,
.btn-text {
  display: inline-block;
}

.btn-loader.hidden,
.btn-text.hidden {
  display: none;
}

/* Animations */
.streaming-cursor {
  animation: blink 1s infinite;
  color: var(--primary-color);
}

@keyframes blink {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0; }
}

.slide-in {
  animation: slideIn 0.3s ease-out;
}

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

/* ==================== RESPONSIVE ==================== */

/* Tablet (481px - 768px) */
@media (max-width: 768px) and (min-width: 481px) {
  .chat-widget {
    bottom: 20px;
    right: 20px;
  }

  .chat-toggle {
    width: 56px;
    height: 56px;
  }

  .chat-toggle .chat-icon,
  .chat-toggle .close-icon {
    width: 28px;
    height: 28px;
  }

  .chat-window {
    position: fixed;
    bottom: auto;
    right: auto;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 85%;
    max-width: 500px;
    height: 70vh;
    max-height: 600px;
    border-radius: 20px;
  }
}

/* Mobile (up to 480px) - KEYBOARD FIX */
@media (max-width: 480px) {
  html {
    height: 100%;
  }

  body {
    height: 100%;
    position: relative;
  }

  .chat-widget {
    inset: 0;
    z-index: 999999;
    bottom: 0;
    right: 0;
  }

  .chat-toggle {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 56px;
    height: 56px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    z-index: 999999;
  }

  .chat-toggle .chat-icon,
  .chat-toggle .close-icon {
    width: 28px;
    height: 28px;
  }

  .chat-window {
    position: fixed;
    inset: 0;
    width: 100%;
    max-width: 100%;
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
    bottom: 0;
    right: 0;
    z-index: 999999;
  }

  .chat-window.hidden {
    transform: translateY(100%);
    transition: transform 0.35s cubic-bezier(0.4, 0.0, 0.2, 1);
  }

  .chat-window.show {
    transform: translateY(0);
  }
  
  .chat-header {
    padding: 16px;
  }

  .messages-container {
    padding: 16px;
    padding-bottom: 8px;
    gap: 16px;
    overscroll-behavior: contain;
  }

  .message-avatar {
    width: 28px;
    height: 28px;
    font-size: 14px;
  }

  .message-text {
    padding: 10px 14px;
    font-size: 13px;
    line-height: 1.6;
  }

  .quick-action {
    font-size: 11px;
    padding: 7px 10px;
  }

  /* KEYBOARD FIX - Add safe area */
  .input-area {
    padding-bottom: env(safe-area-inset-bottom, 0);
    background: var(--background);
  }

  .input-container {
    padding: 12px 16px;
  }

  #message-input {
    font-size: 16px !important;
    padding: 12px 16px;
    min-height: 48px;
    height: 48px;
    border-radius: 24px;
    touch-action: manipulation;
  }

  .send-button {
    width: 48px;
    height: 48px;
    touch-action: manipulation;
  }

  .send-button svg {
    width: 18px;
    height: 18px;
  }

  .lead-form-card {
    padding: 16px;
  }

  .lead-form-card h4 {
    font-size: 15px;
  }

  .lead-form-card p {
    font-size: 12px;
  }

  .lead-form-card input,
  .lead-form-card select,
  .lead-form-card textarea {
    font-size: 16px;
    padding: 9px 11px;
  }
}

/* Very Small Mobile (up to 360px) */
@media (max-width: 360px) {
  .messages-container {
    padding: 12px;
  }

  #message-input {
    min-height: 40px;
    height: 40px;
  }

  .send-button {
    width: 40px;
    height: 40px;
  }
}