/* ===================================
   CHATBOT PAGE STYLES
   =================================== */

.chatbot-layout {
  display: flex;
  gap: 0;
  height: calc(100vh - var(--header-height) - 56px);
  background: var(--surface);
  border-radius: var(--radius-xl);
  border: 1px solid var(--border-light);
  overflow: hidden;
  animation: fadeIn 500ms ease forwards;
}

/* Conversations Sidebar */
.chat-sidebar {
  width: 280px;
  background: var(--accent);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.chat-sidebar-header {
  padding: 20px;
  border-bottom: 1px solid var(--border);
}

.chat-sidebar-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 12px;
}

.chat-new-btn {
  width: 100%;
  padding: 10px;
  background: var(--primary);
  color: var(--text-inverse);
  border-radius: var(--radius-md);
  font-size: 13px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  border: none;
  font-family: inherit;
  transition: all var(--transition-fast);
}

.chat-new-btn:hover {
  background: var(--primary-light);
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.chat-list-item {
  padding: 12px 14px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
  margin-bottom: 4px;
}

.chat-list-item:hover {
  background: var(--surface);
}

.chat-list-item.active {
  background: var(--surface);
  box-shadow: var(--shadow-xs);
}

.chat-list-item-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-list-item-date {
  font-size: 11px;
  color: var(--text-tertiary);
}

/* Chat Main Area */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.chat-header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border-light);
  display: flex;
  align-items: center;
  gap: 12px;
}

.chat-header-avatar {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.chat-header-avatar .material-icons-round {
  font-size: 22px;
  color: var(--text-inverse);
}

.chat-header-info {
  flex: 1;
}

.chat-header-name {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
}

.chat-header-status {
  font-size: 12px;
  color: var(--secondary);
  display: flex;
  align-items: center;
  gap: 4px;
}

.chat-header-status::before {
  content: '';
  width: 8px;
  height: 8px;
  border-radius: var(--radius-full);
  background: var(--secondary);
  display: inline-block;
}

/* Messages Area */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.chat-message {
  display: flex;
  gap: 12px;
  max-width: 80%;
  animation: fadeInUp 300ms ease forwards;
}

.chat-message.user {
  align-self: flex-start;
  flex-direction: row;
}

.chat-message.bot {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.chat-msg-avatar {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 14px;
  font-weight: 700;
}

.chat-message.user .chat-msg-avatar {
  background: var(--primary-bg);
  color: var(--primary);
}

.chat-message.bot .chat-msg-avatar {
  background: linear-gradient(135deg, var(--secondary) 0%, var(--secondary-light) 100%);
  color: var(--text-inverse);
}

.chat-msg-content {
  flex: 1;
}

.chat-msg-bubble {
  padding: 14px 18px;
  border-radius: var(--radius-lg);
  font-size: 14px;
  line-height: 1.9;
  position: relative;
}

.chat-message.user .chat-msg-bubble {
  background: var(--primary);
  color: var(--text-inverse);
  border-top-right-radius: 4px;
}

.chat-message.bot .chat-msg-bubble {
  background: var(--accent);
  color: var(--text-primary);
  border-top-left-radius: 4px;
}

.chat-msg-bubble strong {
  display: block;
  margin-bottom: 4px;
}

.chat-msg-time {
  font-size: 11px;
  color: var(--text-tertiary);
  margin-top: 6px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.chat-message.user .chat-msg-time {
  text-align: right;
}

.chat-message.bot .chat-msg-time {
  text-align: left;
}

/* Typing Indicator */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 12px 18px;
  background: var(--accent);
  border-radius: var(--radius-lg);
  width: fit-content;
}

.typing-dot {
  width: 8px;
  height: 8px;
  border-radius: var(--radius-full);
  background: var(--text-tertiary);
  animation: typingBounce 1.4s infinite ease-in-out;
}

.typing-dot:nth-child(1) { animation-delay: 0ms; }
.typing-dot:nth-child(2) { animation-delay: 200ms; }
.typing-dot:nth-child(3) { animation-delay: 400ms; }

@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-8px); opacity: 1; }
}

/* Chat Input */
.chat-input-area {
  padding: 16px 24px;
  border-top: 1px solid var(--border-light);
  background: var(--surface);
}

.chat-input-wrapper {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--accent);
  border: 2px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 8px 16px;
  transition: border-color var(--transition-fast);
}

.chat-input-wrapper:focus-within {
  border-color: var(--primary);
}

.chat-input {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 14px;
  color: var(--text-primary);
  padding: 8px 0;
  resize: none;
  min-height: 24px;
  max-height: 100px;
  font-family: inherit;
  line-height: 1.5;
}

.chat-input::placeholder {
  color: var(--text-tertiary);
}

.chat-send-btn {
  width: 42px;
  height: 42px;
  border-radius: var(--radius-md);
  background: var(--primary);
  color: var(--text-inverse);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border: none;
  transition: all var(--transition-fast);
  flex-shrink: 0;
}

.chat-send-btn:hover {
  background: var(--primary-light);
  transform: scale(1.05);
}

.chat-send-btn .material-icons-round {
  font-size: 20px;
  transform: rotate(180deg);
}

/* Chat toggle sidebar button for mobile */
.chat-toggle-sidebar {
  display: none;
}

/* زر الرجوع — مخفي على الديسك توب */
.chat-back-btn {
  display: none;
  width: 38px;
  height: 38px;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.15);
  border: none;
  border-radius: var(--radius-md);
  color: #ffffff;
  cursor: pointer;
  flex-shrink: 0;
  transition: background var(--transition-fast);
  -webkit-tap-highlight-color: transparent;
}

.chat-back-btn:active {
  background: rgba(255, 255, 255, 0.25);
}

/* ============================================
   MOBILE CHATBOT — تجربة ملء الشاشة الكاملة
   ============================================ */

/* ── Drawer Overlay (الخلفية الداكنة) ─────────── */
.chat-drawer-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0);
  z-index: 1199;
  transition: background 300ms ease;
  pointer-events: none;
}

.chat-drawer-overlay.active {
  background: rgba(0, 0, 0, 0.55);
  pointer-events: all;
}

/* ── Chat Sidebar الأساسي (ديسك توب) ───────────── */
.chat-sidebar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.chat-sidebar-close {
  display: none;
  width: 34px;
  height: 34px;
  border: none;
  background: rgba(255,255,255,0.08);
  border-radius: 8px;
  color: rgba(255,255,255,0.8);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  transition: background 200ms ease;
  -webkit-tap-highlight-color: transparent;
}

.chat-sidebar-close:hover {
  background: rgba(255,255,255,0.15);
}

.chat-list-icon {
  font-size: 18px !important;
  color: rgba(255,255,255,0.45);
  flex-shrink: 0;
}

.chat-list-item-body {
  flex: 1;
  min-width: 0;
}

.chat-list-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 40px 20px;
  color: rgba(255,255,255,0.35);
  text-align: center;
}

.chat-list-empty .material-icons-round {
  font-size: 40px;
  opacity: 0.3;
}

.chat-list-empty p {
  font-size: 13px;
  line-height: 1.6;
}

.chat-list-empty small {
  font-size: 11px;
  opacity: 0.7;
}

/* ── Tablet (968px) ─────────────────────────────── */
@media (max-width: 968px) {
  /* السايدبار يختفي ويصبح drawer */
  .chat-sidebar {
    position: fixed;
    top: 0;
    right: 0;
    height: 100%;
    width: min(320px, 85vw);
    z-index: 1200;
    /* يبدأ خارج الشاشة من اليمين */
    transform: translateX(100%);
    transition: transform 300ms cubic-bezier(0.4, 0, 0.2, 1);
    /* تصميم داكن كـ ChatGPT */
    background: #1a2332;
    border-left: 1px solid rgba(255,255,255,0.08);
    box-shadow: none;
    display: flex;
  }

  .chat-sidebar.open {
    transform: translateX(0);
    box-shadow: -8px 0 40px rgba(0,0,0,0.4);
  }

  /* إظهار الـ overlay */
  .chat-drawer-overlay {
    display: block;
  }

  /* إظهار زر الإغلاق */
  .chat-sidebar-close {
    display: flex;
  }

  /* تحديث ألوان عناصر السايدبار الداكن */
  .chat-sidebar-header {
    background: #1a2332;
    border-bottom-color: rgba(255,255,255,0.08);
  }

  .chat-sidebar-title {
    color: rgba(255,255,255,0.9);
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .chat-new-btn {
    background: rgba(255,255,255,0.1);
    color: rgba(255,255,255,0.9);
    border: 1px solid rgba(255,255,255,0.15);
    border-radius: 10px;
    transition: all 200ms ease;
    padding: 12px;
  }

  .chat-new-btn:hover,
  .chat-new-btn:active {
    background: rgba(255,255,255,0.18);
  }

  .chat-list {
    background: #1a2332;
    padding: 8px;
  }

  .chat-list-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    border-radius: 10px;
    color: rgba(255,255,255,0.75);
    transition: background 150ms ease;
    -webkit-tap-highlight-color: transparent;
  }

  .chat-list-item:hover {
    background: rgba(255,255,255,0.08);
    color: #ffffff;
  }

  .chat-list-item.active {
    background: rgba(255,255,255,0.13);
    color: #ffffff;
    box-shadow: none;
  }

  .chat-list-item-title {
    color: inherit;
    font-size: 14px;
  }

  .chat-list-item-date {
    color: rgba(255,255,255,0.35);
    font-size: 11px;
    margin-top: 3px;
  }

  .chat-toggle-sidebar {
    display: flex;
  }

  .chat-message {
    max-width: 90%;
  }
}

@media (max-width: 768px) {
  /* الشات بوت يأخذ كامل الشاشة */
  .chatbot-layout {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    height: 100dvh;
    height: 100vh; /* fallback للمتصفحات القديمة */
    border-radius: 0;
    border: none;
    z-index: 600;
    flex-direction: column;
    animation: none;
  }

  /* هيدر الشات بلون التطبيق الرئيسي */
  .chat-header {
    padding: 14px 16px;
    padding-top: max(14px, env(safe-area-inset-top));
    background: linear-gradient(135deg, var(--primary-dark) 0%, var(--primary) 100%);
    border-bottom: none;
    gap: 10px;
    flex-shrink: 0;
  }

  /* إظهار زر الرجوع على الموبايل */
  .chat-back-btn {
    display: flex;
  }

  /* إظهار زر قائمة المحادثات */
  .chat-toggle-sidebar {
    display: flex;
    background: rgba(255, 255, 255, 0.15) !important;
    color: #ffffff !important;
  }

  .chat-header-avatar {
    width: 38px;
    height: 38px;
    background: rgba(255, 255, 255, 0.2);
  }

  .chat-header-name {
    color: #ffffff;
    font-size: 16px;
  }

  .chat-header-status {
    color: rgba(255, 255, 255, 0.85);
  }

  .chat-header-status::before {
    background: #4ade80;
  }

  /* الإصلاح الجذري للسكرول داخل flexbox محدود الارتفاع */
  .chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0; /* ← هذا هو المفتاح! بدونه flex-child لن يتقلّص */
    overflow: hidden;
  }

  /* خلفية رمادية فاتحة كتطبيقات المراسلة */
  .chat-messages {
    flex: 1;
    min-height: 0; /* ← يسمح للـ flex-child بالتقلص والسكرول */
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px 12px;
    gap: 16px;
    background: #f0f2f5;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    /* تأكيد أن السكرول يعمل على iOS */
    scroll-behavior: smooth;
  }

  /* فقاعات أوسع */
  .chat-message {
    max-width: 88%;
  }

  .chat-msg-bubble {
    font-size: 15px;
    line-height: 1.75;
    padding: 12px 16px;
  }

  /* رسائل المستخدم */
  .chat-message.user .chat-msg-bubble {
    background: var(--primary);
    color: #ffffff;
    box-shadow: 0 1px 4px rgba(15, 76, 129, 0.25);
  }

  /* رسائل البوت — بيضاء كتطبيقات المراسلة */
  .chat-message.bot .chat-msg-bubble {
    background: #ffffff;
    color: var(--text-primary);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  }

  /* حقل الإدخال */
  .chat-input-area {
    padding: 10px 12px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    background: #ffffff;
    border-top: 1px solid var(--border);
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.06);
    flex-shrink: 0;
  }

  .chat-input-wrapper {
    border-radius: 24px;
    padding: 6px 6px 6px 14px;
    background: #f5f5f5;
    border-color: #e0e0e0;
    gap: 8px;
  }

  .chat-input-wrapper:focus-within {
    border-color: var(--primary);
    background: #ffffff;
  }

  .chat-input {
    font-size: 15px;
    padding: 8px 0;
  }

  .chat-send-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
  }
}

@media (max-width: 480px) {
  .chat-messages {
    padding: 12px 10px;
    gap: 14px;
  }

  .chat-input-area {
    padding: 8px 10px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  .chat-msg-bubble {
    font-size: 14px;
    padding: 10px 14px;
    line-height: 1.7;
  }
}
