:root {
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-light: #eef2ff;
  --primary-alpha: rgba(79, 70, 229, 0.12);
  --accent: #06b6d4;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;

  --bg-page: #f8fafc;
  --bg-surface: #ffffff;
  --bg-card: #ffffff;
  --bg-input: #f1f5f9;
  --bg-hover: #e2e8f0;

  --text-main: #0f172a;
  --text-secondary: #334155;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --border-focus: #4f46e5;

  --user-bg: #4f46e5;
  --user-text: #ffffff;
  --model-bg: #ffffff;
  --model-text: #0f172a;

  --code-bg: #1e1e2e;
  --code-text: #cdd6f4;

  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.08), 0 2px 4px -2px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.05);

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-full: 9999px;

  --font-sans: "Cairo", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-mono: "Fira Code", monospace;
}

[data-theme="dark"] {
  --bg-page: #0f172a;
  --bg-surface: #1e293b;
  --bg-card: #1e293b;
  --bg-input: #334155;
  --bg-hover: #334155;

  --text-main: #f8fafc;
  --text-secondary: #cbd5e1;
  --text-muted: #94a3b8;
  --border: #334155;

  --user-bg: #4f46e5;
  --user-text: #ffffff;
  --model-bg: #1e293b;
  --model-text: #f8fafc;

  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.6);
}

/* التصفير والقواعد العامة */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  font-family: var(--font-sans);
  background-color: var(--bg-page);
  color: var(--text-main);
  line-height: 1.6;
  direction: rtl;
  text-align: right;
  overflow: hidden;
}

/* هيكلية التطبيق */
.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 1000px;
  margin: 0 auto;
  background-color: var(--bg-surface);
  box-shadow: var(--shadow-lg);
  position: relative;
}

/* وضع التضمين iFrame / Widget */
body.is-embedded .app-container {
  max-width: 100%;
  height: 100%;
  box-shadow: none;
  border-radius: 0;
}

/* ترويسة التطبيق (Header) */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background-color: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  z-index: 10;
  gap: 12px;
}

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

.bot-avatar-wrap {
  position: relative;
  width: 44px;
  height: 44px;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--primary), var(--accent));
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 22px;
  box-shadow: var(--shadow-sm);
  flex-shrink: 0;
}

.online-dot {
  position: absolute;
  bottom: -2px;
  left: -2px;
  width: 12px;
  height: 12px;
  background-color: var(--success);
  border: 2px solid var(--bg-surface);
  border-radius: 50%;
}

.bot-meta {
  display: flex;
  flex-direction: column;
}

.bot-title {
  font-size: 17px;
  font-weight: 700;
  color: var(--text-main);
  display: flex;
  align-items: center;
  gap: 8px;
}

.persona-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  background-color: var(--primary-alpha);
  color: var(--primary);
  border-radius: var(--radius-full);
}

.bot-status {
  font-size: 12px;
  color: var(--text-muted);
}

/* شريط الأدوات بالترويسة */
.header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn-icon {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-muted);
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-icon:hover {
  background-color: var(--bg-hover);
  color: var(--text-main);
}

.persona-select-wrap select {
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background-color: var(--bg-input);
  color: var(--text-main);
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s;
}

.persona-select-wrap select:focus {
  border-color: var(--primary);
}

/* مساحة المحادثة */
.chat-main {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  scroll-behavior: smooth;
}

/* شاشة الترحيب والمقترحات */
.welcome-card {
  background: linear-gradient(135deg, var(--bg-surface), var(--primary-alpha));
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
  text-align: center;
  margin: 10px 0 20px 0;
  box-shadow: var(--shadow-sm);
}

.welcome-icon {
  font-size: 40px;
  margin-bottom: 10px;
}

.welcome-card h2 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 8px;
  color: var(--text-main);
}

.welcome-card p {
  color: var(--text-muted);
  font-size: 14px;
  max-width: 500px;
  margin: 0 auto 18px auto;
}

.prompt-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
}

.chip {
  background-color: var(--bg-surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 8px 14px;
  border-radius: var(--radius-full);
  font-size: 13px;
  font-family: var(--font-sans);
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 6px;
  box-shadow: var(--shadow-sm);
}

.chip:hover {
  background-color: var(--primary);
  color: white;
  border-color: var(--primary);
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

/* عناصر الرسائل */
.message-row {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  animation: fadeIn 0.3s ease;
}

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

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

.msg-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
}

.user-row .msg-avatar {
  background-color: var(--text-muted);
  color: white;
}

.model-row .msg-avatar {
  background: linear-gradient(135deg, var(--primary), var(--accent));
  color: white;
}

.msg-content-wrap {
  max-width: 82%;
  display: flex;
  flex-direction: column;
}

.user-row .msg-content-wrap {
  align-items: flex-end;
}

.msg-sender-name {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 4px;
  padding: 0 4px;
}

.message-bubble {
  border-radius: var(--radius-lg);
  padding: 12px 18px;
  font-size: 15px;
  line-height: 1.7;
  position: relative;
  word-break: break-word;
}

.user-row .message-bubble {
  background-color: var(--user-bg);
  color: var(--user-text);
  border-top-left-radius: 4px;
  box-shadow: var(--shadow-sm);
}

.model-row .message-bubble {
  background-color: var(--model-bg);
  color: var(--model-text);
  border: 1px solid var(--border);
  border-top-right-radius: 4px;
  box-shadow: var(--shadow-sm);
}

/* رسالة المستخدم المرفقة بصورة */
.attached-image-preview {
  margin-bottom: 8px;
}

.attached-image-preview img {
  max-width: 240px;
  max-height: 200px;
  border-radius: var(--radius-md);
  object-fit: cover;
  border: 2px solid rgba(255, 255, 255, 0.4);
}

/* تنسيق Markdown والرموز */
.message-body h1, .message-body h2, .message-body h3, .message-body h4 {
  margin: 12px 0 6px 0;
  color: var(--text-main);
}
.message-body h1 { font-size: 18px; }
.message-body h2 { font-size: 16px; }
.message-body h3 { font-size: 15px; }

.message-body p {
  margin-bottom: 8px;
}
.message-body p:last-child {
  margin-bottom: 0;
}

.message-body ul, .message-body ol {
  padding-right: 22px;
  margin-bottom: 10px;
}

.message-body li {
  margin-bottom: 4px;
}

.message-body blockquote {
  border-right: 4px solid var(--primary);
  background-color: var(--primary-alpha);
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  margin: 10px 0;
  font-style: italic;
}

.message-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 14px;
  overflow-x: auto;
  display: block;
}

.message-body th, .message-body td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: right;
}

.message-body th {
  background-color: var(--bg-input);
  font-weight: 700;
}

/* تنسيق كتل الأكواد البرمجية */
.message-body pre {
  background-color: var(--code-bg);
  border-radius: var(--radius-md);
  padding: 14px 14px 14px 14px;
  margin: 12px 0;
  overflow-x: auto;
  direction: ltr;
  text-align: left;
  position: relative;
  box-shadow: var(--shadow-sm);
}

.message-body code {
  font-family: var(--font-mono);
  font-size: 13.5px;
}

.message-body p code {
  background-color: var(--bg-hover);
  color: var(--primary);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 13px;
  direction: ltr;
  display: inline-block;
}

/* زر نسخ الكود */
.code-copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  background-color: rgba(255, 255, 255, 0.15);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 11px;
  font-family: var(--font-sans);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
  transition: all 0.2s ease;
  backdrop-filter: blur(4px);
}

.code-copy-btn:hover {
  background-color: rgba(255, 255, 255, 0.3);
}

.code-copy-btn.copied {
  background-color: var(--success);
}

/* شريط إجراءات الرسالة (نسخ، صوت، تقييم) */
.msg-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
  padding: 0 4px;
  opacity: 0.8;
  transition: opacity 0.2s;
}

.msg-actions:hover {
  opacity: 1;
}

.msg-act-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  font-family: var(--font-sans);
  transition: all 0.15s ease;
}

.msg-act-btn:hover {
  background-color: var(--bg-hover);
  color: var(--primary);
}

.msg-act-btn.active {
  color: var(--primary);
  background-color: var(--primary-alpha);
}

/* مؤشر الكتابة (Typing Indicator) */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 0;
}

.typing-indicator span {
  width: 8px;
  height: 8px;
  background-color: var(--primary);
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out both;
}

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

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* منطقة الإدخال السفلية (Input Area) */
.chat-footer {
  padding: 12px 18px 16px 18px;
  background-color: var(--bg-surface);
  border-top: 1px solid var(--border);
  position: relative;
}

/* معاينة الصورة المختارة قبل الإرسال */
.image-upload-preview {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background-color: var(--bg-input);
  border-radius: var(--radius-md);
  margin-bottom: 10px;
  animation: fadeIn 0.2s ease;
}

.preview-thumb {
  width: 48px;
  height: 48px;
  border-radius: 6px;
  object-fit: cover;
}

.preview-info {
  flex: 1;
  font-size: 12px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preview-remove {
  background: none;
  border: none;
  color: var(--danger);
  cursor: pointer;
  padding: 4px;
}

/* نموذج الإرسال وحاوية النص */
.input-box-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background-color: var(--bg-input);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 6px 10px;
  transition: all 0.2s ease;
}

.input-box-wrapper:focus-within {
  border-color: var(--primary);
  background-color: var(--bg-surface);
  box-shadow: 0 0 0 3px var(--primary-alpha);
}

.chat-textarea {
  flex: 1;
  border: none;
  background: transparent;
  outline: none;
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.5;
  color: var(--text-main);
  resize: none;
  max-height: 140px;
  min-height: 38px;
  padding: 6px 4px;
}

.chat-textarea::placeholder {
  color: var(--text-muted);
}

.input-buttons {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-bottom: 2px;
}

.btn-circle {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background-color: transparent;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-circle:hover {
  background-color: var(--bg-hover);
  color: var(--text-main);
}

/* زر التسجيل الصوتي */
.btn-mic.recording {
  color: var(--danger);
  animation: pulseMic 1.2s infinite ease-in-out;
}

@keyframes pulseMic {
  0% { transform: scale(1); background-color: rgba(239, 68, 68, 0.15); }
  50% { transform: scale(1.1); background-color: rgba(239, 68, 68, 0.3); }
  100% { transform: scale(1); background-color: rgba(239, 68, 68, 0.15); }
}

/* زر الإرسال */
.btn-send {
  background-color: var(--primary);
  color: white;
}

.btn-send:hover {
  background-color: var(--primary-hover);
  color: white;
}

.btn-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* زر إيقاف التوليد */
.btn-stop {
  background-color: var(--danger);
  color: white;
}

.btn-stop:hover {
  background-color: #dc2626;
}

/* نافذة الإعدادات المنبثقة (Modal) */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(2px);
  padding: 16px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.modal-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  background-color: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 480px;
  padding: 24px;
  box-shadow: var(--shadow-lg);
  transform: translateY(10px);
  transition: transform 0.2s ease;
}

.modal-overlay.open .modal-card {
  transform: translateY(0);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}

.modal-title {
  font-size: 18px;
  font-weight: 700;
}

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text-secondary);
}

.form-input {
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background-color: var(--bg-input);
  color: var(--text-main);
  font-family: var(--font-sans);
  font-size: 14px;
  outline: none;
}

.form-input:focus {
  border-color: var(--primary);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

.btn-primary {
  background-color: var(--primary);
  color: white;
  border: none;
  padding: 8px 18px;
  border-radius: var(--radius-md);
  font-family: var(--font-sans);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-primary:hover {
  background-color: var(--primary-hover);
}

.btn-secondary {
  background-color: var(--bg-hover);
  color: var(--text-main);
  border: none;
  padding: 8px 18px;
  border-radius: var(--radius-md);
  font-family: var(--font-sans);
  font-size: 14px;
  cursor: pointer;
}

/* تنسيق KaTeX */
.katex-display {
  margin: 12px 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 6px 0;
  direction: ltr;
}

/* تجاوب الشاشات الصغيرة والهواتف */
@media (max-width: 640px) {
  .app-container {
    max-width: 100%;
    box-shadow: none;
  }

  .app-header {
    padding: 10px 14px;
  }

  .bot-title {
    font-size: 15px;
  }

  .chat-main {
    padding: 14px 10px;
  }

  .msg-content-wrap {
    max-width: 90%;
  }

  .welcome-card {
    padding: 18px 14px;
  }

  .prompt-chips {
    gap: 6px;
  }

}

/* درج المحادثات السابقة للمتعلم (History Drawer) */
.drawer-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(2px);
  z-index: 99;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.drawer-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.history-drawer {
  position: fixed;
  top: 0;
  right: -340px;
  bottom: 0;
  width: 320px;
  max-width: 85vw;
  background-color: var(--bg-surface);
  border-left: 1px solid var(--border);
  box-shadow: -5px 0 25px rgba(0, 0, 0, 0.15);
  z-index: 100;
  display: flex;
  flex-direction: column;
  transition: right 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  direction: rtl;
}

.history-drawer.open {
  right: 0;
}

.drawer-header {
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.drawer-title {
  font-size: 16px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-new-thread {
  margin: 14px 18px;
  padding: 10px;
  background-color: var(--primary-alpha);
  color: var(--primary);
  border: 1px dashed var(--primary);
  border-radius: var(--radius-md);
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: all 0.2s;
}

.btn-new-thread:hover {
  background-color: var(--primary);
  color: white;
}

.drawer-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 12px 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.history-item-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  border: 1px solid transparent;
  cursor: pointer;
  text-align: right;
  transition: all 0.15s;
}

.history-item-btn:hover {
  border-color: var(--border);
  background: var(--bg-hover);
}

.history-item-btn.active {
  border-color: var(--primary);
  background: var(--primary-alpha);
}

.history-item-info {
  flex: 1;
  overflow: hidden;
  margin-left: 8px;
}

.history-item-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-item-time {
  font-size: 11px;
  color: var(--text-muted);
}

.history-item-del {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  opacity: 0.6;
  transition: opacity 0.2s, color 0.2s;
}

.history-item-del:hover {
  opacity: 1;
  color: var(--danger);
}

/* شريط تنبيه تجاوز معدل الرسائل */
.rate-limit-banner {
  background: rgba(239, 68, 68, 0.12);
  border: 1px solid var(--danger);
  color: var(--danger);
  padding: 10px 14px;
  border-radius: var(--radius-md);
  margin: 10px 16px 0 16px;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 8px;
  animation: fadeIn 0.2s ease;
}