/**
 * AI Chatbot Platform - Widget Styles
 *
 * RTL-first design for Arabic sites with full LTR support.
 * Scoped to .aicp-widget to prevent host site conflicts.
 * Uses CSS custom properties for easy theming.
 *
 * @version 1.0.0
 */

/* ==========================================================================
   CSS Custom Properties (Theming)
   ========================================================================== */

.aicp-widget {
  --aicp-primary: #6C5CE7;
  --aicp-primary-dark: #5A4BD1;
  --aicp-primary-light: #a29bfe;
  --aicp-bg: #ffffff;
  --aicp-bg-alt: #f4f5f7;
  --aicp-bg-hover: #eef0f2;
  --aicp-text: #2d3436;
  --aicp-text-muted: #636e72;
  --aicp-text-light: #b2bec3;
  --aicp-border: #dfe6e9;
  --aicp-user-bubble: var(--aicp-primary);
  --aicp-user-text: #ffffff;
  --aicp-bot-bubble: #f1f2f6;
  --aicp-bot-text: #2d3436;
  --aicp-staff-bubble: #dfe6fd;
  --aicp-staff-text: #2d3436;
  --aicp-error: #d63638;
  --aicp-success: #00a32a;
  --aicp-shadow: 0 5px 40px rgba(0, 0, 0, 0.16);
  --aicp-radius: 16px;
  --aicp-radius-sm: 10px;
  --aicp-font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  --aicp-toggle-size: 60px;
  --aicp-z: 999998;
}

/* ==========================================================================
   Widget Root
   ========================================================================== */

.aicp-widget {
  position: fixed;
  bottom: 20px;
  z-index: var(--aicp-z);
  font-family: var(--aicp-font);
  font-size: 14px;
  line-height: 1.5;
  direction: rtl;
  box-sizing: border-box;
}

.aicp-widget *,
.aicp-widget *::before,
.aicp-widget *::after {
  box-sizing: border-box;
}

/* Position variants */
.aicp-position-right {
  right: 20px;
  left: auto;
}

.aicp-position-left {
  left: 20px;
  right: auto;
}

/* ==========================================================================
   Toggle Button
   ========================================================================== */

.aicp-toggle-btn {
  width: var(--aicp-toggle-size);
  height: var(--aicp-toggle-size);
  border-radius: 50%;
  background: var(--aicp-primary);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transition: transform 0.3s ease, background 0.3s ease, box-shadow 0.3s ease;
  position: relative;
  outline: none;
  padding: 0;
}

.aicp-toggle-btn:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}

.aicp-toggle-btn:focus-visible {
  outline: 3px solid var(--aicp-primary-light);
  outline-offset: 3px;
}

/* Pulse animation for attention */
.aicp-toggle-btn::before {
  content: '';
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  background: var(--aicp-primary);
  opacity: 0;
  animation: aicp-pulse 2s ease-in-out infinite;
  z-index: -1;
}

.aicp-toggle-active::before {
  animation: none;
  opacity: 0;
}

@keyframes aicp-pulse {
  0%, 100% { transform: scale(1); opacity: 0; }
  50% { transform: scale(1.15); opacity: 0.3; }
}

/* Icon toggle */
.aicp-toggle-btn .aicp-icon-close {
  display: none;
}

.aicp-toggle-active .aicp-icon-chat {
  display: none;
}

.aicp-toggle-active .aicp-icon-close {
  display: block;
}

/* Notification badge */
.aicp-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  background: var(--aicp-error);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid #fff;
  animation: aicp-bounce-in 0.3s ease;
}

@keyframes aicp-bounce-in {
  0% { transform: scale(0); }
  60% { transform: scale(1.2); }
  100% { transform: scale(1); }
}

/* ==========================================================================
   Chat Container
   ========================================================================== */

.aicp-chat-container {
  position: absolute;
  bottom: calc(var(--aicp-toggle-size) + 14px);
  width: 380px;
  height: 550px;
  background: var(--aicp-bg);
  border-radius: var(--aicp-radius);
  box-shadow: var(--aicp-shadow);
  display: none;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(20px) scale(0.95);
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.aicp-position-right .aicp-chat-container {
  right: 0;
  left: auto;
}

.aicp-position-left .aicp-chat-container {
  left: 0;
  right: auto;
}

.aicp-chat-open {
  opacity: 1;
  transform: translateY(0) scale(1);
}

/* ==========================================================================
   Header
   ========================================================================== */

.aicp-chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: linear-gradient(135deg, var(--aicp-primary), var(--aicp-primary-dark));
  color: #fff;
  flex-shrink: 0;
}

.aicp-header-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.aicp-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.aicp-avatar svg {
  color: #fff;
}

.aicp-header-text {
  display: flex;
  flex-direction: column;
}

.aicp-header-name {
  font-weight: 600;
  font-size: 15px;
  line-height: 1.3;
}

.aicp-header-status {
  font-size: 12px;
  opacity: 0.85;
  line-height: 1.2;
}

.aicp-header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.aicp-header-btn {
  width: 32px;
  height: 32px;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s ease;
  padding: 0;
}

.aicp-header-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.aicp-header-btn:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 1px;
}

/* ==========================================================================
   Messages Area
   ========================================================================== */

.aicp-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--aicp-bg-alt);
}

/* Styled scrollbar */
.aicp-messages::-webkit-scrollbar {
  width: 5px;
}

.aicp-messages::-webkit-scrollbar-track {
  background: transparent;
}

.aicp-messages::-webkit-scrollbar-thumb {
  background: var(--aicp-text-light);
  border-radius: 10px;
}

.aicp-messages::-webkit-scrollbar-thumb:hover {
  background: var(--aicp-text-muted);
}

/* ==========================================================================
   Message Bubbles
   ========================================================================== */

.aicp-message {
  display: flex;
  flex-direction: column;
  max-width: 85%;
  animation: aicp-fade-in 0.25s ease;
}

@keyframes aicp-fade-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.aicp-bubble {
  padding: 10px 14px;
  border-radius: var(--aicp-radius-sm);
  word-wrap: break-word;
  overflow-wrap: break-word;
  line-height: 1.55;
  font-size: 14px;
}

.aicp-bubble a {
  color: inherit;
  text-decoration: underline;
}

.aicp-bubble a:hover {
  opacity: 0.8;
}

/* User messages (aligned to start in RTL = right side) */
.aicp-message-user {
  align-self: flex-start;
}

.aicp-message-user .aicp-bubble {
  background: var(--aicp-user-bubble);
  color: var(--aicp-user-text);
  border-bottom-right-radius: 4px;
}

/* Bot messages (aligned to end in RTL = left side) */
.aicp-message-bot {
  align-self: flex-end;
}

.aicp-message-bot .aicp-bubble {
  background: var(--aicp-bot-bubble);
  color: var(--aicp-bot-text);
  border-bottom-left-radius: 4px;
}

/* Staff messages */
.aicp-message-staff {
  align-self: flex-end;
}

.aicp-message-staff .aicp-bubble {
  background: var(--aicp-staff-bubble);
  color: var(--aicp-staff-text);
  border-bottom-left-radius: 4px;
}

/* System messages (centered, muted) */
.aicp-message-system {
  align-self: center;
  max-width: 90%;
}

.aicp-system-text {
  font-size: 12px;
  color: var(--aicp-text-muted);
  text-align: center;
  padding: 6px 14px;
  background: rgba(0, 0, 0, 0.04);
  border-radius: 12px;
  display: inline-block;
}

/* Timestamp */
.aicp-time {
  font-size: 11px;
  color: var(--aicp-text-light);
  margin-top: 3px;
  padding: 0 4px;
}

.aicp-message-user .aicp-time {
  text-align: right;
}

.aicp-message-bot .aicp-time,
.aicp-message-staff .aicp-time {
  text-align: left;
}

/* ==========================================================================
   Typing Indicator
   ========================================================================== */

.aicp-typing-bubble {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 12px 18px !important;
  min-height: 40px;
}

.aicp-typing-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--aicp-text-muted);
  animation: aicp-typing-bounce 1.4s ease-in-out infinite;
}

.aicp-typing-dot:nth-child(2) {
  animation-delay: 0.16s;
}

.aicp-typing-dot:nth-child(3) {
  animation-delay: 0.32s;
}

@keyframes aicp-typing-bounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-6px);
    opacity: 1;
  }
}

/* ==========================================================================
   Pre-Chat Form
   ========================================================================== */

.aicp-prechat-form {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--aicp-bg);
}

.aicp-prechat-inner {
  width: 100%;
  max-width: 320px;
}

.aicp-prechat-title {
  text-align: center;
  color: var(--aicp-text);
  font-size: 15px;
  font-weight: 500;
  margin: 0 0 20px;
  line-height: 1.5;
}

.aicp-prechat-field {
  margin-bottom: 12px;
}

.aicp-prechat-field input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--aicp-border);
  border-radius: 8px;
  font-size: 14px;
  font-family: var(--aicp-font);
  color: var(--aicp-text);
  background: var(--aicp-bg);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  outline: none;
}

.aicp-prechat-field input:focus {
  border-color: var(--aicp-primary);
  box-shadow: 0 0 0 3px rgba(108, 92, 231, 0.12);
}

.aicp-prechat-field input.aicp-input-error {
  border-color: var(--aicp-error);
  box-shadow: 0 0 0 3px rgba(214, 54, 56, 0.1);
}

.aicp-prechat-submit {
  width: 100%;
  padding: 12px;
  background: var(--aicp-primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 600;
  font-family: var(--aicp-font);
  cursor: pointer;
  transition: background 0.2s ease;
  margin-top: 4px;
}

.aicp-prechat-submit:hover {
  background: var(--aicp-primary-dark);
}

.aicp-prechat-submit:focus-visible {
  outline: 3px solid var(--aicp-primary-light);
  outline-offset: 2px;
}

/* ==========================================================================
   Input Area
   ========================================================================== */

.aicp-input-area {
  border-top: 1px solid var(--aicp-border);
  background: var(--aicp-bg);
  padding: 10px 12px;
  flex-shrink: 0;
}

.aicp-input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.aicp-input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--aicp-border);
  border-radius: var(--aicp-radius-sm);
  font-size: 14px;
  font-family: var(--aicp-font);
  color: var(--aicp-text);
  background: var(--aicp-bg-alt);
  resize: none;
  outline: none;
  max-height: 120px;
  min-height: 42px;
  line-height: 1.4;
  transition: border-color 0.2s ease;
}

.aicp-input:focus {
  border-color: var(--aicp-primary);
}

.aicp-input::placeholder {
  color: var(--aicp-text-light);
}

/* Send button */
.aicp-send-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--aicp-primary);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.2s ease, opacity 0.2s ease, transform 0.15s ease;
  padding: 0;
}

.aicp-send-btn:hover:not(:disabled) {
  background: var(--aicp-primary-dark);
  transform: scale(1.05);
}

.aicp-send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.aicp-send-btn:focus-visible {
  outline: 3px solid var(--aicp-primary-light);
  outline-offset: 2px;
}

/* Loading spinner state */
.aicp-send-btn.aicp-sending svg {
  animation: aicp-spin 1s linear infinite;
}

@keyframes aicp-spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* RTL: flip the send icon */
[dir="rtl"] .aicp-send-btn svg {
  transform: scaleX(-1);
}

.aicp-send-btn.aicp-sending svg {
  transform: none;
}

/* Input actions row */
.aicp-input-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
  flex-wrap: wrap;
}

/* Handoff button */
.aicp-handoff-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  font-size: 12px;
  font-family: var(--aicp-font);
  color: var(--aicp-text-muted);
  background: var(--aicp-bg-alt);
  border: 1px solid var(--aicp-border);
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.aicp-handoff-btn:hover {
  color: var(--aicp-primary);
  border-color: var(--aicp-primary);
  background: rgba(108, 92, 231, 0.06);
}

/* WhatsApp transfer button */
.aicp-whatsapp-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  font-size: 12px;
  font-family: var(--aicp-font);
  color: #fff;
  background: #25D366;
  border: none;
  border-radius: 16px;
  cursor: pointer;
  transition: background 0.2s ease;
}

.aicp-whatsapp-btn:hover {
  background: #1da851;
}

.aicp-whatsapp-btn svg {
  flex-shrink: 0;
}

/* ==========================================================================
   Error Toast
   ========================================================================== */

.aicp-error-toast {
  position: absolute;
  top: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(-10px);
  background: var(--aicp-error);
  color: #fff;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 13px;
  font-family: var(--aicp-font);
  text-align: center;
  z-index: 10;
  opacity: 0;
  transition: opacity 0.3s ease, transform 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  max-width: 90%;
  pointer-events: none;
}

.aicp-toast-visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ==========================================================================
   Animations
   ========================================================================== */

@keyframes aicp-slide-up {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ==========================================================================
   Dark Mode Support
   ========================================================================== */

@media (prefers-color-scheme: dark) {
  .aicp-widget {
    --aicp-bg: #1e1e2e;
    --aicp-bg-alt: #2a2a3e;
    --aicp-bg-hover: #353550;
    --aicp-text: #e4e4e8;
    --aicp-text-muted: #a0a0b0;
    --aicp-text-light: #6c6c80;
    --aicp-border: #3a3a50;
    --aicp-bot-bubble: #2e2e42;
    --aicp-bot-text: #e4e4e8;
    --aicp-staff-bubble: #32325a;
    --aicp-staff-text: #e4e4e8;
    --aicp-shadow: 0 5px 40px rgba(0, 0, 0, 0.4);
  }
}

/* ==========================================================================
   Reduced Motion
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  .aicp-widget *,
  .aicp-widget *::before,
  .aicp-widget *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ==========================================================================
   Mobile Responsive
   ========================================================================== */

@media (max-width: 768px) {
  .aicp-widget {
    bottom: 12px;
  }

  .aicp-position-right {
    right: 12px;
  }

  .aicp-position-left {
    left: 12px;
  }

  .aicp-chat-container {
    position: absolute;
    bottom: calc(var(--aicp-toggle-size) + 10px);
    top: auto;
    width: min(92vw, 360px);
    height: min(72vh, 560px);
    max-height: calc(100vh - 96px);
    border-radius: 14px;
    z-index: calc(var(--aicp-z) + 1);
  }

  .aicp-chat-header {
    border-radius: 14px 14px 0 0;
  }

  .aicp-input-area {
    padding-bottom: 10px;
  }

  .aicp-toggle-btn {
    width: 56px;
    height: 56px;
  }

  .aicp-toggle-btn svg {
    width: 24px;
    height: 24px;
  }

  .aicp-messages {
    padding: 12px;
  }

  .aicp-message {
    max-width: 90%;
  }
}

/* Very small screens */
@media (max-width: 380px) {
  .aicp-position-right {
    right: 8px;
  }

  .aicp-position-left {
    left: 8px;
  }

  .aicp-chat-container {
    width: calc(100vw - 16px);
    max-height: calc(100vh - 86px);
  }

  .aicp-bubble {
    font-size: 13px;
    padding: 8px 12px;
  }

  .aicp-prechat-inner {
    padding: 0 4px;
  }
}
