@import url('https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700&display=swap');

/* =========================================
   1. المتغيرات والألوان (Variables)
   ========================================= */
:root {
    /* الوضع النهاري (Light) */
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --bg-light: #f3f4f6;
    --text-light: #111827;
    --card-light: #ffffff;
    --border-light: #e5e7eb;
    --text-muted-light: #6b7280;
    --input-bg-light: #ffffff;
    
    /* الوضع الليلي (Dark) */
    --bg-dark: #0f172a;
    --card-dark: #1e293b;
    --text-dark: #f1f5f9;
    --text-muted-dark: #94a3b8;
    --input-dark: #334155;
    --border-dark: #475569;

    /* ألوان الحالة */
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
}

* { box-sizing: border-box; }

body {
    font-family: 'Tajawal', sans-serif;
    background-color: var(--bg-light);
    color: var(--text-light);
    margin: 0;
    padding: 20px;
    padding-bottom: 80px; /* مساحة للشريط السفلي في الجوال */
    transition: background 0.3s, color 0.3s;
}

/* =========================================
   2. إعدادات الوضع الداكن (Dark Mode)
   ========================================= */
body.dark-mode {
    background-color: var(--bg-dark);
    color: var(--text-dark);
}

body.dark-mode .card {
    background-color: var(--card-dark);
    color: var(--text-dark);
    box-shadow: 0 4px 6px rgba(0,0,0,0.5);
    border: 1px solid var(--border-dark);
}

/* العناوين في الوضع الليلي (عام) */
body.dark-mode h1, 
body.dark-mode h3, 
body.dark-mode h4, 
body.dark-mode h5, 
body.dark-mode h6, 
body.dark-mode strong { 
    color: #ffffff !important; 
}

/* استثناء: العناوين الرئيسية (H2) في الهيدر والبطاقات تأخذ اللون البنفسجي */
body.dark-mode .header-flex h2,
body.dark-mode .card h2 {
    color: #4f46e5 !important;
}

/* النصوص العادية والثانوية في البطاقات باللون الأبيض */
body.dark-mode .card p,
body.dark-mode .card span,
body.dark-mode .card div,
body.dark-mode .card .text-secondary {
    color: #ffffff !important;
}

/* استثناء للشارات والأزرار للحفاظ على ألوانها */
body.dark-mode .card .badge,
body.dark-mode .card .status-badge,
body.dark-mode .card .btn,
body.dark-mode .card .sub-stat strong {
    color: inherit !important;
}
body.dark-mode .card .btn { color: white !important; }

.text-secondary { color: var(--text-muted-light); }
body.dark-mode .text-secondary { color: var(--text-muted-dark) !important; }

/* =========================================
   3. المكونات الأساسية (Layout & Cards)
   ========================================= */
.header-flex {
    display: flex; justify-content: space-between; align-items: center;
    margin-bottom: 25px; flex-wrap: wrap; gap: 10px;
}

.grid-container {
    display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;
}

.card {
    background-color: var(--card-light);
    border-radius: 16px;
    padding: 24px;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    margin-bottom: 20px;
    transition: background 0.3s;
    break-inside: avoid;
    position: relative;
}

/* =========================================
   4. الأزرار (Buttons)
   ========================================= */
.btn {
    padding: 10px 20px;
    border: none; border-radius: 8px; cursor: pointer;
    font-weight: 700; text-decoration: none;
    display: inline-flex; align-items: center; justify-content: center;
    gap: 8px; transition: all 0.2s ease; font-family: inherit; font-size: 0.95rem;
    color: white !important;
}
.btn:active { transform: scale(0.98); }

.btn-primary { background-color: var(--primary); }
.btn-primary:hover { background-color: var(--primary-hover); }
.btn-danger { background-color: var(--danger); }
.btn-whatsapp { background-color: #25D366; }

/* زر شفاف للأدوات (الإعدادات والوضع الليلي) */
.btn-transparent {
    background-color: #374151; 
    border: 1px solid #374151;
    color: #ffffff !important;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    width: 42px; height: 42px; padding: 0;
    border-radius: 10px;
}
.btn-transparent:hover { background-color: #1f2937; transform: translateY(-2px); }

body.dark-mode .btn-transparent {
    background-color: rgba(255,255,255,0.1);
    border-color: rgba(255,255,255,0.2);
    color: #fff !important;
}
body.dark-mode .btn-transparent:hover { background-color: rgba(255,255,255,0.2); }

/* أزرار خاصة بالاختبارات */
.btn-copy { background: var(--bg-light); color: var(--primary) !important; border: 1px solid var(--primary); flex: 1; padding: 8px; border-radius: 8px; cursor: pointer; font-weight: bold; transition: 0.2s; display:flex; justify-content:center; gap:5px; }
.btn-copy:hover { background: var(--primary); color: white !important; }
body.dark-mode .btn-copy { background: transparent; }
body.dark-mode .btn-copy:hover { background: var(--primary); }

.btn-icon { width: 42px; height: 42px; display: flex; align-items: center; justify-content: center; border-radius: 8px; cursor: pointer; border: none; color: white !important; }
.btn-results { background: #f59e0b; } .btn-results:hover { background: #d97706; }
.btn-delete { background: #ef4444; } .btn-delete:hover { background: #dc2626; }

/* زر القائمة (النقاط الثلاث) */
.btn-menu { background: white; color: #475569 !important; border: 1px solid #cbd5e1; }
.btn-menu:hover { background: #f1f5f9; color: #1e293b !important; }
/* إصلاح زر القائمة في الوضع الليلي */
body.dark-mode .btn-menu { background-color: #334155 !important; color: #ffffff !important; border-color: #475569 !important; }

/* زر الرفع في منشئ الاختبارات */
.btn-upload {
    background-color: #fff;
    color: var(--primary) !important;
    border: 2px dashed var(--primary);
    padding: 10px 20px;
    border-radius: 8px;
    cursor: pointer;
    font-weight: bold;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: all 0.3s ease;
}
.btn-upload:hover { background-color: #eef2ff; transform: translateY(-2px); }
body.dark-mode .btn-upload { background-color: var(--card-dark); color: #818cf8 !important; border-color: #818cf8; }
body.dark-mode .btn-upload:hover { background-color: rgba(129, 140, 248, 0.1); }

/* =========================================
   5. مبدل اللغة (الكبسولة)
   ========================================= */
.lang-wrapper {
    background-color: #f3f4f6;
    border: 1px solid #e5e7eb;
    border-radius: 30px;
    padding: 4px;
    display: flex; align-items: center; gap: 2px;
    height: 42px;
}
.lang-wrapper a {
    text-decoration: none; padding: 6px 12px; border-radius: 20px;
    font-size: 0.85rem; font-weight: 700; color: #6b7280;
    transition: all 0.3s ease; display: inline-block;
}
.lang-wrapper a:hover { color: var(--primary); }
.lang-wrapper a.active {
    background-color: #ffffff; color: var(--primary);
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

body.dark-mode .lang-wrapper { background-color: #1f2937; border-color: #374151; }
body.dark-mode .lang-wrapper a { color: #9ca3af; }
body.dark-mode .lang-wrapper a.active { background-color: #374151; color: #fff; box-shadow: 0 2px 5px rgba(0,0,0,0.5); }

/* =========================================
   6. النماذج والجداول (Forms & Tables)
   ========================================= */
.form-group { margin-bottom: 20px; }
.form-group label { display: block; margin-bottom: 5px; font-weight: 500; }

input, textarea, select {
    width: 100%; padding: 12px; border: 1px solid var(--border-light);
    border-radius: 8px; margin-top: 5px; 
    background-color: #fff; 
    color: #333;
    transition: all 0.3s;
}

select {
    appearance: none; 
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 1rem center;
    background-size: 1em;
    padding-right: 2.5rem;
}

html[dir="rtl"] select {
    background-position: left 1rem center;
    padding-right: 12px;
    padding-left: 2.5rem;
}

/* --- إصلاح حقول الإدخال في الوضع الليلي (تحديث هام) --- */
body.dark-mode input, 
body.dark-mode textarea, 
body.dark-mode select {
    background-color: var(--input-dark) !important; 
    border-color: var(--border-dark) !important;
    color: #f9fafb !important;
}

/* معالجة خاصة للحقول التي قد تحمل تنسيق مباشر */
body.dark-mode input[style], 
body.dark-mode textarea[style] {
    background-color: var(--input-dark) !important;
    color: #f9fafb !important;
}

/* إصلاح خيارات القائمة في الوضع الليلي */
body.dark-mode option {
    background-color: #1f2937; 
    color: white;
}

/* إصلاح لون Placeholder في الوضع الليلي */
body.dark-mode ::placeholder {
    color: #94a3b8;
    opacity: 1;
}

input:focus, select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.3); }

/* الجداول */
table { width: 100%; border-collapse: separate; border-spacing: 0; }
th, td { padding: 15px; text-align: start; border-bottom: 1px solid var(--border-light); }
th { font-weight: 600; color: #6b7280; text-transform: uppercase; background: #f9fafb; }

body.dark-mode th { background-color: #0f172a !important; color: #94a3b8; border-bottom-color: var(--border-dark); }
body.dark-mode td { border-bottom-color: var(--border-dark); color: #e2e8f0; }

/* الشارات (Badges) */
.badge { padding: 6px 12px; border-radius: 20px; font-size: 0.75rem; font-weight: 700; display: inline-block; }
.badge.paid { background: #d1fae5; color: #065f46; }
.badge.partial { background: #fef3c7; color: #92400e; }
.badge.unpaid { background: #fee2e2; color: #991b1b; }

a { text-decoration: none; color: inherit; }

/* =========================================
   7. تنسيقات خاصة للاختبارات (Quizzes)
   ========================================= */
.toolbar-sticky { position: sticky; top: 20px; z-index: 100; background: var(--card-light); padding: 15px; border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; border: 1px solid var(--border-light); margin-bottom: 20px; }
body.dark-mode .toolbar-sticky { background: var(--card-dark); border-color: var(--border-dark); }

.question-card { background: var(--card-light); border: 1px solid var(--border-light); border-radius: 12px; padding: 25px; margin-bottom: 20px; position: relative; border-right: 5px solid var(--primary); transition: 0.3s; }
body.dark-mode .question-card { background: var(--card-dark); border-color: var(--border-dark); }

.question-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; border-bottom: 1px solid var(--border-light); padding-bottom: 10px; }

.media-controls { margin-top: 10px; margin-bottom: 15px; padding: 12px; background: #f8fafc; border-radius: 8px; border: 1px dashed #cbd5e1; display: flex; justify-content: center; align-items: center; gap: 15px; }
body.dark-mode .media-controls { background: #1e293b; border-color: #475569; }

.btn-media { padding: 8px 15px; border-radius: 6px; border: 1px solid transparent; cursor: pointer; display: flex; align-items: center; gap: 5px; font-size: 0.9em; background: white; color: #333; box-shadow: 0 2px 4px rgba(0,0,0,0.05); transition: 0.2s; }
body.dark-mode .btn-media { background: #334155; color: white; }
.btn-media:hover { transform: translateY(-2px); box-shadow: 0 4px 6px rgba(0,0,0,0.1); }

.tf-wrapper { display: flex; gap: 20px; margin-top: 15px; justify-content: center; }
.tf-option { flex: 1; display: flex; align-items: center; justify-content: center; gap: 10px; padding: 15px; border-radius: 8px; cursor: pointer; border: 2px solid transparent; background: #f1f5f9; font-weight: bold; transition: 0.2s; }
body.dark-mode .tf-option { background: #334155; color: white; }
.tf-option:has(input:checked) { border-color: var(--primary); background: rgba(79, 70, 229, 0.1); color: var(--primary); }

.quiz-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 25px; padding: 10px; }
.quiz-card { background: var(--card-light); border-radius: 16px; padding: 25px; box-shadow: 0 4px 15px rgba(0,0,0,0.05); border: 1px solid var(--border-light); transition: transform 0.3s ease; display: flex; flex-direction: column; justify-content: space-between; }
body.dark-mode .quiz-card { background: var(--card-dark); border-color: var(--border-dark); box-shadow: 0 4px 15px rgba(0,0,0,0.2); }
.quiz-card:hover { transform: translateY(-5px); border-color: var(--primary); }

.quiz-icon-wrapper { width: 60px; height: 60px; background: linear-gradient(135deg, #e0e7ff 0%, #c7d2fe 100%); color: var(--primary); border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 1.8rem; margin-bottom: 15px; }
body.dark-mode .quiz-icon-wrapper { background: rgba(79, 70, 229, 0.2); color: #818cf8; }

.card-actions { display: flex; gap: 8px; margin-top: auto; }

/* =========================================
   8. Utils & Responsive
   ========================================= */
@media (max-width: 768px) {
    .portal-nav { display: none; }
    .bottom-nav { display: flex; }
    .header-flex { flex-direction: column; align-items: stretch; gap: 15px; }
    .grid-container { grid-template-columns: 1fr; }
    .toolbar-sticky { flex-direction: column; }
    .toolbar-sticky button { width: 100%; }
    .card { overflow-x: auto; }
    .fc-header-toolbar { flex-direction: column; gap: 10px; }
    .fc-toolbar-chunk { display: flex; justify-content: center; width: 100%; }
}

.bottom-nav { display: none; position: fixed; bottom: 0; left: 0; width: 100%; background: var(--card-light); border-top: 1px solid var(--border-light); justify-content: space-around; padding: 10px 0; z-index: 1000; box-shadow: 0 -2px 10px rgba(0,0,0,0.05); }
body.dark-mode .bottom-nav { background: var(--card-dark); border-color: var(--border-dark); }
.bottom-nav-item { text-align: center; color: var(--text-muted-light); font-size: 0.75rem; flex: 1; }
.bottom-nav-item.active { color: var(--primary); }

html[dir="rtl"] body { direction: rtl; text-align: right; }
html[dir="ltr"] body { direction: ltr; text-align: left; font-family: 'Segoe UI', sans-serif; }

/* إعدادات الطباعة A4 */
@media print {
    * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
    @page { size: A4; margin: 15mm; }
    body { background-color: white !important; color: black !important; padding: 0 !important; margin: 0 !important; font-size: 12pt; }
    .no-print, .btn, .header-flex button, .header-flex a, form, .alert, iframe, .status-btn, .toolbar-sticky, .bottom-nav, .portal-nav { display: none !important; }
    .card { box-shadow: none !important; border: 1px solid #ccc !important; margin-bottom: 20px !important; page-break-inside: avoid; background-color: white !important; color: black !important; }
    .grid-container { display: block !important; }
    table { width: 100% !important; border-collapse: collapse !important; }
    th, td { border-bottom: 1px solid #999 !important; color: black !important; }
    th { background-color: #f0f0f0 !important; }
    .print-header { display: block !important; text-align: center; margin-bottom: 30px; border-bottom: 2px solid #000; padding-bottom: 10px; }
    canvas { max-width: 100% !important; max-height: 300px !important; }
    
    body.dark-mode { background-color: white !important; color: black !important; }
    body.dark-mode .card { background-color: white !important; color: black !important; border: 1px solid #ccc !important; }
    body.dark-mode th { background-color: #f0f0f0 !important; color: black !important; }
    body.dark-mode td { color: black !important; }
}