/* -------------------------------------------------------------- */
/* كافيه الفضاء - ملف التنسيق الشامل (Final v4.1) */
/* تم التحديث لإصلاح الشات والفوتر */
/* -------------------------------------------------------------- */

:root {
    /* الألوان الافتراضية (النهارية) */
    --primary: #2563eb;
    --primary-dark: #1e40af;
    --secondary: #64748b;
    --accent: #0ea5e9;
    --bg-body: #f1f5f9;
    --bg-card: #ffffff;
    --text-main: #0f172a;
    --text-muted: #64748b;
    --border-color: #e2e8f0;
    
    /* ألوان الشات (مطابقة للواتساب) */
    --msg-in: #ffffff;
    --msg-out: #dcf8c6; 
    
    /* الظلال والأبعاد */
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.1);
    --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.1);
    --radius: 12px;
    --radius-sm: 8px;
    --transition: 0.3s ease;
}

/* متغيرات الوضع الليلي */
[data-theme="dark"] {
    --primary: #3b82f6;
    --primary-dark: #60a5fa;
    --bg-body: #0f172a;       
    --bg-card: #1e293b;       
    --text-main: #f8fafc;     
    --text-muted: #94a3b8;
    --border-color: #334155;
    
    /* ألوان الشات الليلي */
    --msg-in: #334155;        
    --msg-out: #056049;       
    
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.5);
}

/* ---------------------------------- */
/* 1. أساسيات الصفحة */
/* ---------------------------------- */
* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: 'Tajawal', 'Segoe UI', sans-serif;
    background-color: var(--bg-body);
    color: var(--text-main);
    direction: rtl;
    text-align: right;
    line-height: 1.6;
    overflow-x: hidden;
    padding-bottom: 0; 
}

a { text-decoration: none; color: inherit; transition: 0.2s; }
ul { list-style: none; }
.hidden { display: none !important; }

/* ---------------------------------- */
/* 2. عناصر التحكم (Buttons, Forms) */
/* ---------------------------------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    padding: 10px 20px; border-radius: var(--radius-sm); font-weight: 600; cursor: pointer; border: none; font-size: 0.95rem; transition: 0.2s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-block { width: 100%; }

.form-group { margin-bottom: 20px; }
.form-group label { display: block; margin-bottom: 8px; font-weight: 600; color: var(--text-main); font-size: 0.9rem; }
.form-group input, .form-group select, .form-group textarea {
    width: 100%; padding: 12px 15px; border: 1px solid var(--border-color); border-radius: var(--radius-sm); font-size: 1rem; background: var(--bg-card); color: var(--text-main); transition: 0.2s;
}
.form-group input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,0.1); }

.alert { padding: 15px; border-radius: var(--radius-sm); margin-bottom: 20px; font-weight: 500; text-align: center; }
.success-alert { background: #d1fae5; color: #065f46; border: 1px solid #a7f3d0; }
.error-alert { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; }

/* ---------------------------------- */
/* 3. الهيدر (Header) */
/* ---------------------------------- */
.main-header {
    background: var(--bg-card);
    box-shadow: var(--shadow-sm);
    position: sticky; top: 0; z-index: 1000; height: 70px;
    border-bottom: 1px solid var(--border-color);
}
.header-container {
    max-width: 1300px; margin: 0 auto; padding: 0 20px; height: 100%;
    display: flex; justify-content: space-between; align-items: center;
}
.logo-link { font-size: 1.4rem; font-weight: 800; color: var(--primary); display: flex; align-items: center; gap: 10px; }
.main-nav ul { display: flex; gap: 25px; }
.main-nav a {
    color: var(--text-muted); font-weight: 500; font-size: 0.95rem; display: flex; align-items: center; gap: 8px;
}
.main-nav a:hover, .main-nav a.active { color: var(--primary); }
.admin-link { color: #ef4444 !important; }
.doctor-link { color: #10b981 !important; }

.user-menu { display: flex; align-items: center; gap: 15px; }
.user-link { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 0.9rem; }
.user-link img { width: 35px; height: 35px; border-radius: 50%; object-fit: cover; border: 2px solid var(--bg-body); }
.btn-logout { color: var(--text-muted); font-size: 1.2rem; transition: 0.2s; }
.btn-logout:hover { color: #ef4444; transform: translateX(-3px); }
.mobile-menu-btn { display: none; background: none; border: none; font-size: 1.5rem; color: var(--text-main); cursor: pointer; }

/* ---------------------------------- */
/* 4. البطاقات والتخطيط العام */
/* ---------------------------------- */
.content-wrapper { max-width: 1300px; margin: 30px auto; padding: 0 20px; min-height: calc(100vh - 150px); }

.page-header { margin-bottom: 30px; border-bottom: 2px solid var(--border-color); padding-bottom: 15px; }
.page-header h1 { font-size: 1.8rem; color: var(--primary); font-weight: 700; }

.card, .admin-card, .profile-card, .news-section-card {
    background: var(--bg-card); border-radius: var(--radius); box-shadow: var(--shadow-sm); padding: 25px; transition: 0.3s; border: 1px solid var(--border-color);
}
.card:hover, .profile-card:hover { box-shadow: var(--shadow-lg); transform: translateY(-2px); }

/* ---------------------------------- */
/* 5. تسجيل الدخول (Login) */
/* ---------------------------------- */
.login-body { display: flex; justify-content: center; align-items: center; min-height: 100vh; }
.login-container {
    background: var(--bg-card); padding: 40px; border-radius: 20px; box-shadow: var(--shadow-lg); width: 100%; max-width: 400px; text-align: center; border: 1px solid var(--border-color);
}
.logo-title { color: var(--primary); font-size: 2rem; margin-bottom: 10px; }

/* ---------------------------------- */
/* 6. الصفحة الرئيسية (Home) */
/* ---------------------------------- */
.home-layout { display: grid; gap: 25px; }
.welcome-box {
    background: linear-gradient(120deg, var(--primary), #4f46e5); color: white;
    padding: 30px; border-radius: var(--radius); display: flex; justify-content: space-between; align-items: center;
}
.batch-info .btn-class { background: var(--bg-card); color: var(--primary); padding: 8px 15px; border-radius: 20px; font-weight: bold; display: inline-block; margin-top: 10px; }
.news-section-card h3 { border-bottom: 2px solid var(--primary-light); padding-bottom: 10px; margin-bottom: 20px; color: var(--primary); font-size: 1.6rem; }
.news-item { background: var(--bg-body); padding: 15px; border-radius: var(--radius-sm); margin-bottom: 15px; border-right: 4px solid var(--primary); }
.news-item h4 { color: var(--primary); margin-bottom: 5px; }
.news-meta { font-size: 0.8rem; color: var(--text-muted); }

/* ---------------------------------- */
/* 7. صفحة المواد (Classes) */
/* ---------------------------------- */
.course-cards-container { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 25px; }
.course-card {
    background: var(--bg-card); border-radius: 15px; overflow: hidden; box-shadow: var(--shadow-sm); transition: transform 0.2s; display: flex; flex-direction: column; height: 100%; border: 1px solid var(--border-color);
}
.course-card:hover { transform: translateY(-5px); box-shadow: var(--shadow-lg); }
.card-header { background: var(--primary); color: #fff; padding: 20px; }
.card-body { padding: 20px; flex-grow: 1; color: var(--text-main); }
.card-footer { padding: 15px; background: var(--bg-body); text-align: center; color: var(--primary); font-weight: bold; }

/* ---------------------------------- */
/* 8. صفحة المادة والشات (Course View) */
/* ---------------------------------- */
.tabs-nav { display: flex; border-bottom: 1px solid var(--border-color); margin-bottom: 20px; gap: 5px; }
.tab-link { padding: 10px 20px; color: var(--text-muted); font-weight: bold; cursor: pointer; }
.tab-link.active { color: var(--primary); border-bottom: 3px solid var(--primary); }

/* شات المادة */
.chat-container {
    background: var(--bg-card); border-radius: var(--radius); box-shadow: var(--shadow-sm); height: 600px; display: flex; flex-direction: column; position: relative; border: 1px solid var(--border-color);
}
.messages-area {
    flex-grow: 1; padding: 20px; overflow-y: auto; background: var(--bg-body); display: flex; flex-direction: column; gap: 10px;
}

/* ---------------------------------- */
/* 9. نظام الرسائل الخاصة (Updated Fixed) */
/* ---------------------------------- */
/* ملاحظة: نستخدم اسم messaging-container في ملف PHP لذا سنقوم بتعريفه هنا */
.messaging-container, .messaging-layout {
    display: flex; /* تغيير من Grid إلى Flex للتحكم الأفضل */
    /* هذا الارتفاع يمنع تداخل الفوتر */
    height: calc(100dvh - 220px); 
    max-width: 1100px;
    margin: 20px auto;
    background: var(--bg-card);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--border-color);
    position: relative;
    z-index: 1;
}

/* القائمة الجانبية */
.conversations-sidebar, .conversations-list {
    width: 320px;
    display: flex; flex-direction: column; 
    border-left: 1px solid var(--border-color); background-color: var(--bg-card);
}
.sidebar-header, .list-header {
    flex-shrink: 0; height: 65px; padding: 0 20px; background: var(--bg-body); display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--border-color);
}
.conversations-list-body, .list-body {
    flex-grow: 1; overflow-y: auto;
}
.conv-item, .conversation-item {
    display: flex; align-items: center; padding: 15px; border-bottom: 1px solid var(--bg-body); cursor: pointer; transition: 0.2s; position: relative;
}
.conv-item:hover, .conv-item.active { background: var(--bg-body); }
.conv-avatar { width: 45px; height: 45px; border-radius: 50%; object-fit: cover; margin-left: 15px; border: 1px solid var(--border-color); }
.conv-details, .conv-info { flex: 1; overflow: hidden; }
.conv-name, .conv-info h4 { margin: 0; font-size: 0.95rem; font-weight: 600; color: var(--text-main); }
.conv-msg, .conv-info p { margin: 0; color: var(--text-muted); font-size: 0.8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.unread-badge { background-color: #ef4444; color: #fff; font-size: 0.75rem; padding: 2px 6px; border-radius: 10px; position: absolute; left: 15px; top: 15px; }

/* منطقة الشات النشطة */
.chat-area-wrapper, .active-chat-area {
    flex: 1;
    display: flex; flex-direction: column; height: 100%;
    position: relative;
}
.chat-header {
    flex-shrink: 0; height: 65px; padding: 0 20px; background: var(--bg-body); display: flex; align-items: center; justify-content: flex-end; gap: 10px; border-bottom: 1px solid var(--border-color); z-index: 10;
}
.header-avatar, .chat-header-avatar { width: 40px; height: 40px; border-radius: 50%; object-fit: cover; border: 1px solid var(--border-color); }
.header-info { text-align: right; }
.header-info h4 { margin: 0; font-size: 1rem; color: var(--text-main); }
.status-text { font-size: 0.75rem; color: var(--text-muted); }

.chat-box, .private-messages-box {
    flex-grow: 1; 
    overflow-y: auto; 
    padding: 20px; 
    display: flex; flex-direction: column; gap: 8px;
    background-color: #e5ddd5; /* لون خلفية الواتساب الافتراضي */
    background-image: url('https://user-images.githubusercontent.com/15075759/28719144-86dc0f70-73b1-11e7-911d-60d70fcded21.png');
}
/* تعديل الخلفية في الوضع الليلي */
[data-theme="dark"] .chat-box {
    background-color: #0b141a; 
    background-image: none; /* أو استبدالها بصورة داكنة */
}

/* --- تنسيق الفقاعات (Bubble System) الجديد --- */
.msg-container { display: flex; align-items: flex-end; gap: 8px; max-width: 75%; margin-bottom: 5px; }
/* رسائلي */
.msg-container.mine { align-self: flex-end; flex-direction: row-reverse; } 
.msg-container.mine .message { background-color: var(--msg-out); color: #000; border-bottom-right-radius: 0; text-align: left; }
/* رسائل الآخرين */
.msg-container.others { align-self: flex-start; flex-direction: row; }
.msg-container.others .message { background-color: var(--msg-in); color: #000; border-bottom-left-radius: 0; text-align: right; }

/* تنسيق الوضع الليلي للنصوص داخل الفقاعات */
[data-theme="dark"] .msg-container.mine .message { color: #fff; }
[data-theme="dark"] .msg-container.others .message { color: #fff; }

.msg-avatar { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; border: 1px solid #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.2); }

.message { 
    padding: 8px 12px; border-radius: 15px; position: relative; font-size: 0.95rem; box-shadow: 0 1px 2px rgba(0,0,0,0.1); line-height: 1.4; word-wrap: break-word; min-width: 80px; 
}

.msg-time { font-size: 0.65rem; color: #999; display: block; margin-top: 4px; text-align: right; }

/* الميديا داخل الشات */
.chat-img { max-width: 250px; max-height: 300px; border-radius: 8px; cursor: pointer; object-fit: cover; margin-top: 5px; display: block; }
.sticker-img { width: 100px; display: block; }

/* --- منطقة الكتابة (Input Area) --- */
.chat-input-area, .chat-input-form {
    flex-shrink: 0;
    padding: 10px 20px; 
    background: var(--bg-body); 
    display: flex; align-items: center; gap: 10px; 
    border-top: 1px solid var(--border-color); 
    z-index: 50;
    direction: rtl;
}
.chat-input-area input { flex-grow: 1; padding: 12px 20px; border-radius: 30px; border: 1px solid var(--border-color); outline: none; background-color: var(--bg-card); color: var(--text-main); }
.btn-send { width: 45px; height: 45px; border-radius: 50%; padding: 0; font-size: 1.2rem; background: var(--primary); color: #fff; display: flex; align-items: center; justify-content: center; transform: rotate(180deg); }
.btn-icon, .btn-emoji { background: none; border: none; font-size: 1.4rem; cursor: pointer; color: var(--secondary); padding: 5px; }

/* القوائم المنبثقة */
.popup-menu {
    position: absolute; 
    bottom: 75px; /* يرتفع فوق شريط الكتابة */
    right: 10px; 
    background: var(--bg-card); 
    padding: 10px; 
    border-radius: 12px;
    box-shadow: 0 -2px 20px rgba(0,0,0,0.15); 
    border: 1px solid var(--border-color);
    display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; 
    width: 280px; 
    z-index: 100;
}
.sticker-menu { grid-template-columns: repeat(3, 1fr); }
.popup-menu span { font-size: 1.5rem; cursor: pointer; text-align: center; }
.popup-menu img { width: 60px; cursor: pointer; transition: 0.2s; }
.popup-menu img:hover { transform: scale(1.1); }

/* الرد */
.reply-preview { 
    background: var(--bg-body); padding: 10px; border-right: 4px solid var(--primary); display: flex; justify-content: space-between; align-items: center; direction: rtl; 
}
.quoted-msg { border-right: 4px solid var(--primary); background: rgba(0,0,0,0.05); padding: 5px 10px; margin-bottom: 5px; border-radius: 5px; font-size: 0.8rem; text-align: right; cursor: pointer; }

/* ---------------------------------- */
/* 10. البروفايل (Profile) */
/* ---------------------------------- */
.profile-container { display: grid; grid-template-columns: 350px 1fr; gap: 30px; }
@media (max-width: 768px) { .profile-container { grid-template-columns: 1fr; } }
.profile-card { text-align: center; }
.avatar-wrapper { position: relative; width: 160px; height: 160px; margin: 0 auto 20px; }
.profile-avatar { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; border: 5px solid var(--bg-body); box-shadow: var(--shadow-md); }
.camera-btn {
    position: absolute; bottom: 5px; right: 5px; background: var(--primary); color: #fff; width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; border: 3px solid #fff; transition: 0.2s;
}
.profile-name { font-size: 1.5rem; font-weight: 800; margin-bottom: 5px; }
.profile-role { color: var(--primary); font-weight: 600; margin-bottom: 20px; background: var(--bg-body); display: inline-block; padding: 5px 15px; border-radius: 20px; font-size: 0.85rem; }
.info-grid { text-align: right; margin-top: 25px; }
.info-item { padding: 15px 0; border-bottom: 1px solid var(--border-color); display: flex; justify-content: space-between; }
.info-item.disabled { background: var(--bg-body); padding: 10px; border-radius: 8px; border: none; }
.profile-actions { display: flex; justify-content: center; gap: 10px; margin-top: 20px; }
.btn-edit-card { background: var(--bg-card); border: 2px solid var(--primary); color: var(--primary); border-radius: 20px; padding: 8px 20px; }
.btn-messages { background: #10b981; color: #fff; border-radius: 20px; padding: 10px 20px; display: inline-block; }

/* ---------------------------------- */
/* 11. الإشعارات */
/* ---------------------------------- */
.notifications-container { background: var(--bg-card); padding: 20px; border-radius: 15px; max-width: 800px; margin: 0 auto; }
.notif-item { display: flex; align-items: center; padding: 15px; border-bottom: 1px solid var(--border-color); transition: 0.2s; border-radius: 8px; }
.notif-item:hover { background: var(--bg-body); }
.notif-item.unread { background: var(--bg-body); font-weight: bold; border-right: 4px solid var(--primary); }
.notif-icon-box { width: 40px; height: 40px; background: var(--bg-body); border-radius: 50%; display: flex; align-items: center; justify-content: center; margin-left: 15px; }
.notif-content { flex-grow: 1; }
.new-badge { background-color: #ef4444; color: #fff; font-size: 0.7rem; padding: 2px 8px; border-radius: 10px; position: absolute; top: 15px; left: 15px; }

/* ---------------------------------- */
/* 12. الفوتر */
/* ---------------------------------- */
.main-footer {
    background-color: var(--bg-card); color: var(--text-muted); padding: 20px 0; text-align: center; font-size: 0.9rem; margin-top: 30px; border-top: 1px solid var(--border-color);
}

/* --- Responsive (معدل للموبايل) --- */
@media (max-width: 768px) {
    .header-container { padding: 10px; }
    .main-nav { display: none; position: absolute; top: 70px; left: 0; width: 100%; background: var(--bg-card); box-shadow: var(--shadow-lg); padding: 20px; flex-direction: column; }
    .main-nav.active { display: flex; }
    .main-nav ul { flex-direction: column; gap: 15px; }
    .mobile-menu-btn { display: block; }
    
    /* إصلاح الشات في الموبايل */
    .messaging-container, .messaging-layout { 
        flex-direction: column; 
        height: calc(100dvh - 180px); /* ارتفاع مناسب للموبايل */
        width: 95%;
        margin: 10px auto;
    }
    
    .conversations-sidebar.hidden-mobile { display: none; }
    .chat-area-wrapper.hidden-mobile { display: none; }
    .conversations-sidebar { width: 100%; height: 100%; }
    .back-btn-mobile { display: block !important; font-size: 1.5rem; margin-left: 10px; cursor: pointer; text-decoration: none; color: var(--text-main); }
    
    .profile-container { grid-template-columns: 1fr; }
}
.back-btn-mobile { display: none; }
