:root {
  --widget-primary-color: #FF8C42;
  --widget-secondary-color: #667eea;
}

[data-widget-root] {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 9999;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* Minimized chat bubble */
.chat-bubble {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: var(--primary-color, var(--widget-primary-color));
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  border: 4px solid var(--secondary-color, var(--widget-secondary-color));
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s, box-shadow 0.2s;
}

.chat-bubble:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

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

.bubble-avatar {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
}

.bubble-icon {
  width: 32px;
  height: 32px;
  color: white;
}

/* Expanded chat widget */
.chat-widget {
  width: 400px;
  max-width: calc(100vw - 40px);
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  background: white;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  display: none;
}

.chat-widget.active {
  display: flex;
  flex-direction: column;
}

.chat-bubble.hidden {
  display: none;
}

.chat-header {
  background: var(--primary-color, var(--widget-primary-color));
  color: white;
  padding: 16px;
  border-bottom: 1px solid #e5e7eb;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chat-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  flex: 1;
}

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

.header-button,
.close-button {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  padding: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: background 0.2s;
}

.header-button:hover,
.close-button:hover {
  background: rgba(255, 255, 255, 0.1);
}

.header-button svg,
.close-button svg {
  width: 20px;
  height: 20px;
}

.chat-messages {
  height: 400px;
  overflow-y: auto;
  padding: 16px;
  background: #f9fafb;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 12px;
  align-items: flex-start;
}

.message {
  padding: 10px 14px;
  border-radius: 8px;
  max-width: 80%;
  width: fit-content;
  word-wrap: break-word;
  line-height: 1.5;
}

.message p {
  margin: 0;
}

.message p + p {
  margin-top: 0.75em;
}

.message-user {
  background: var(--primary-color, var(--widget-primary-color));
  color: white;
  align-self: flex-end;
}

.message-assistant {
  background: white;
  color: #1f2937;
  border: 1px solid #e5e7eb;
  align-self: flex-start;
}

.message-system {
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
  align-self: center;
  font-size: 14px;
}

.chat-widget form {
  padding: 16px;
  background: white;
  border-top: 1px solid #e5e7eb;
}

.chat-input-wrapper {
  display: flex;
  gap: 8px;
}

.chat-widget input[type="text"] {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
}

.chat-widget input[type="text"]:focus {
  border-color: var(--primary-color, var(--widget-primary-color));
}

.chat-widget input[type="text"]:disabled {
  background: #f3f4f6;
  cursor: not-allowed;
}

.chat-widget button[type="submit"] {
  padding: 10px 20px;
  background: var(--primary-color, var(--widget-primary-color));
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s;
  opacity: 1;
}

.chat-widget button[type="submit"]:hover {
  opacity: 0.9;
}

.chat-widget button[type="submit"]:active {
  opacity: 0.8;
}

/* Typing indicator */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 10px 14px !important;
}

.typing-indicator span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #9ca3af;
  animation: typing 1.4s infinite;
}

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

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

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

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

.chat-messages::-webkit-scrollbar-track {
  background: #f1f1f1;
}

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

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

/* Mobile responsiveness */
@media (max-width: 640px) {
  [data-widget-root] {
    bottom: 10px;
    right: 10px;
  }

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

  .bubble-icon {
    width: 28px;
    height: 28px;
  }

  .chat-widget {
    width: calc(100vw - 20px);
    max-width: calc(100vw - 20px);
    max-height: 80dvh;
    border-radius: 12px;
    transition: max-height 0.3s ease;
  }

  .chat-widget.keyboard-visible {
    max-height: 65dvh;
  }

  .chat-header {
    padding: 12px;
    flex-shrink: 0;
  }

  .chat-header h3 {
    font-size: 16px;
  }

  .chat-messages {
    flex: 1;
    min-height: 150px;
    padding: 12px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

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

  .chat-widget form {
    padding: 12px;
    flex-shrink: 0;
  }

  .chat-widget input[type="text"] {
    padding: 10px 12px;
    font-size: 16px; /* Prevents zoom on iOS */
  }

  .chat-widget button[type="submit"] {
    padding: 10px 16px;
    font-size: 14px;
  }
}

/* Small mobile devices */
@media (max-width: 375px) {
  .chat-bubble {
    width: 52px;
    height: 52px;
  }

  .bubble-icon {
    width: 26px;
    height: 26px;
  }

  .message {
    padding: 8px 12px;
    font-size: 13px;
  }
}

/* Landscape mobile - full screen already handled by mobile styles */
@media (max-width: 900px) and (max-height: 500px) {
  .chat-header {
    padding: 8px 12px;
  }

  .chat-header h3 {
    font-size: 14px;
  }
}