/* ==========================================================
   تمام — موقع مراسلة خاص
   تصميم مستوحى من WhatsApp
   ملاحظة: استيراد الخط انتقل إلى <link> داخل <head> في كل صفحة
   لأن @import هنا كان يؤخّر أول رسم للصفحة.
   ========================================================== */

/* ==========================================
   Reset & Base
   ========================================== */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary: #075E54;
    --primary-dark: #054d44;
    --secondary: #128C7E;
    --accent: #25D366;
    --accent-hover: #1eba57;
    --chat-bg: #0b141a;
    --bubble-out: #005c4b;
    --bubble-in: #1f2c34;
    --header-bg: #1f2c34;
    --text-primary: #e9edef;
    --text-secondary: #8696a0;
    --text-light: #667781;
    --border-color: #2a3942;
    --danger: #ea4335;
    --danger-hover: #d33426;
    --white: #ffffff;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    --radius: 12px;
    --radius-sm: 8px;
}

html {
    font-size: 16px;
    scroll-behavior: smooth;
}

body {
    font-family: 'Tajawal', 'Segoe UI', system-ui, sans-serif;
    background: var(--chat-bg);
    color: var(--text-primary);
    direction: rtl;
    min-height: 100vh;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

a {
    color: var(--accent);
    text-decoration: none;
    transition: color 0.2s;
}

a:hover {
    color: var(--accent-hover);
}

/* إظهار واضح لعنصر التركيز — مهم للتنقل بلوحة المفاتيح */
:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* احترام تفضيل تقليل الحركة في نظام المستخدم */
@media (prefers-reduced-motion: reduce) {

    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* ==========================================
   Auth Pages (Login / Register / Forgot PW)
   ========================================== */
.auth-wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    min-height: 100dvh;
    padding: 20px;
    background: linear-gradient(135deg, #0b141a 0%, #1a2e38 100%);
}

.auth-card {
    background: var(--header-bg);
    border-radius: 16px;
    padding: 40px 36px;
    width: 100%;
    max-width: 420px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
    animation: fadeInUp 0.4s ease;
}

.auth-logo {
    text-align: center;
    margin-bottom: 32px;
}

.auth-logo .logo-icon {
    width: 72px;
    height: 72px;
    background: var(--accent);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 16px;
    font-size: 32px;
    box-shadow: 0 4px 20px rgba(37, 211, 102, 0.3);
}

.auth-logo h1 {
    font-size: 26px;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 6px;
}

.auth-logo p {
    color: var(--text-secondary);
    font-size: 14px;
}

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

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
}

.form-group label .optional {
    font-size: 12px;
    color: var(--text-light);
    font-weight: 400;
}

.form-input {
    width: 100%;
    padding: 14px 16px;
    background: var(--chat-bg);
    border: 1.5px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 16px;
    /* 16px يمنع تكبير الشاشة تلقائياً على iOS عند الكتابة */
    transition: border-color 0.2s, box-shadow 0.2s;
    direction: rtl;
}

.form-input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(37, 211, 102, 0.1);
}

.form-input::placeholder {
    color: var(--text-light);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 14px 28px;
    border: none;
    border-radius: var(--radius-sm);
    font-family: inherit;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
}

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

.btn-primary {
    width: 100%;
    background: var(--accent);
    color: var(--white);
}

.btn-primary:hover:not(:disabled) {
    background: var(--accent-hover);
    transform: translateY(-1px);
    box-shadow: 0 4px 15px rgba(37, 211, 102, 0.3);
}

.btn-danger {
    background: var(--danger);
    color: var(--white);
}

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

.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
    padding: 8px 16px;
    font-size: 13px;
    border: 1px solid var(--border-color);
}

.btn-ghost:hover:not(:disabled) {
    color: var(--text-primary);
    background: rgba(255, 255, 255, 0.05);
}

.auth-footer {
    text-align: center;
    margin-top: 24px;
    font-size: 14px;
    color: var(--text-secondary);
}

.auth-footer a {
    font-weight: 600;
}

.alert {
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    margin-bottom: 20px;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 8px;
    line-height: 1.7;
}

.alert-error {
    background: rgba(234, 67, 53, 0.15);
    color: #ff6b6b;
    border: 1px solid rgba(234, 67, 53, 0.2);
}

.alert-success {
    background: rgba(37, 211, 102, 0.15);
    color: var(--accent);
    border: 1px solid rgba(37, 211, 102, 0.2);
}

.alert-warning {
    background: rgba(255, 193, 7, 0.12);
    color: #ffc107;
    border: 1px solid rgba(255, 193, 7, 0.2);
}

.email-hint {
    background: rgba(37, 211, 102, 0.08);
    border: 1px solid rgba(37, 211, 102, 0.15);
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 20px;
    line-height: 1.7;
}

/* ==========================================
   Chat Layout
   ========================================== */
.chat-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    /* 100dvh يمنع اختفاء مربع الكتابة خلف شريط المتصفح على الجوال */
    height: 100dvh;
    max-width: 900px;
    margin: 0 auto;
    background: var(--chat-bg);
}

/* Chat Header */
.chat-header {
    background: var(--header-bg);
    padding: 10px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
    z-index: 10;
    min-height: 60px;
    flex-shrink: 0;
}

.chat-header .avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--accent);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    font-weight: 700;
    color: var(--white);
    flex-shrink: 0;
}

.chat-header .user-info {
    flex: 1;
    min-width: 0;
}

.chat-header .user-info h3 {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}

.chat-header .user-info p {
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-header .header-actions {
    display: flex;
    gap: 4px;
}

.chat-header .header-btn {
    width: 40px;
    height: 40px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: all 0.2s;
    text-decoration: none;
    flex-shrink: 0;
}

.chat-header .header-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
}

/* Chat Messages Area */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 16px;
    background: var(--chat-bg);
    background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.02'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}

.chat-messages::-webkit-scrollbar {
    width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

/* زر تحميل الرسائل الأقدم */
.load-older-wrap {
    display: flex;
    justify-content: center;
    margin-bottom: 14px;
}

/* Date Separator */
.date-separator {
    text-align: center;
    margin: 16px 0;
}

.date-separator span {
    background: var(--bubble-in);
    color: var(--text-secondary);
    padding: 5px 16px;
    border-radius: 8px;
    font-size: 12px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

/* Message Bubbles */
.message {
    display: flex;
    margin-bottom: 4px;
    animation: msgIn 0.2s ease;
}

/* في اتجاه RTL تعني flex-start الجهة اليمنى */
.message.sent {
    justify-content: flex-start;
    /* رسائلي — يمين */
}

.message.received {
    justify-content: flex-end;
    /* رسائل الطرف الآخر — يسار */
}

.message .bubble {
    max-width: 65%;
    padding: 8px 12px;
    border-radius: 8px;
    position: relative;
    overflow-wrap: anywhere;
    word-break: break-word;
    line-height: 1.5;
    font-size: 14.5px;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15);
}

/* الزاوية الحادة تشير إلى حافة الشاشة التي تلتصق بها الفقاعة
   (كانت معكوسة: الذيل يشير إلى داخل الشاشة) */
.message.sent .bubble {
    background: var(--bubble-out);
    color: var(--text-primary);
    border-top-right-radius: 0;
}

.message.received .bubble {
    background: var(--bubble-in);
    color: var(--text-primary);
    border-top-left-radius: 0;
}

.message .bubble .msg-text {
    margin-bottom: 2px;
}

.message .bubble .msg-time {
    font-size: 11px;
    color: rgba(255, 255, 255, 0.5);
    margin-top: 2px;
    display: flex;
    align-items: center;
    gap: 4px;
    justify-content: flex-end;
    direction: ltr;
}

.message .bubble .msg-time .read-indicator {
    font-size: 14px;
}

.message .bubble .msg-time .read-indicator.read {
    color: #53bdeb;
}

/* Chat Input Area */
.chat-input-area {
    background: var(--header-bg);
    padding: 10px 16px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
    display: flex;
    align-items: flex-end;
    gap: 10px;
    flex-shrink: 0;
}

.chat-input-wrapper {
    flex: 1;
    background: var(--chat-bg);
    border-radius: 24px;
    padding: 4px 16px;
    display: flex;
    align-items: center;
    border: 1px solid var(--border-color);
}

.chat-input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 16px;
    padding: 10px 0;
    resize: none;
    max-height: 120px;
    min-height: 20px;
    direction: rtl;
    line-height: 1.4;
}

.chat-input:focus {
    outline: none;
}

.chat-input::placeholder {
    color: var(--text-light);
}

.send-btn {
    width: 48px;
    height: 48px;
    border: none;
    background: var(--accent);
    color: var(--white);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    transition: all 0.2s;
    flex-shrink: 0;
}

.send-btn:hover:not(:disabled) {
    background: var(--accent-hover);
    transform: scale(1.05);
}

.send-btn:active:not(:disabled) {
    transform: scale(0.95);
}

.send-btn:disabled {
    background: var(--border-color);
    cursor: not-allowed;
    transform: none;
}

/* Empty Chat State */
.empty-chat {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-secondary);
    text-align: center;
    padding: 40px;
}

.empty-chat .empty-icon {
    font-size: 64px;
    margin-bottom: 20px;
    opacity: 0.5;
}

.empty-chat h3 {
    font-size: 20px;
    color: var(--text-primary);
    margin-bottom: 8px;
}

.empty-chat p {
    font-size: 14px;
    max-width: 300px;
    line-height: 1.7;
}

/* ==========================================
   إشعار عائم (فشل الإرسال، انقطاع الشبكة…)
   ========================================== */
.chat-notice {
    position: fixed;
    bottom: 90px;
    left: 50%;
    transform: translateX(-50%);
    max-width: min(420px, calc(100% - 32px));
    background: rgba(234, 67, 53, 0.95);
    color: var(--white);
    padding: 12px 42px 12px 16px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    line-height: 1.6;
    box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
    z-index: 200;
    animation: noticeIn 0.2s ease;
}

@keyframes noticeIn {
    from {
        opacity: 0;
        transform: translate(-50%, 16px);
    }

    to {
        opacity: 1;
        transform: translate(-50%, 0);
    }
}

.chat-notice.info {
    background: rgba(18, 140, 126, 0.95);
}

.chat-notice-close {
    position: absolute;
    top: 6px;
    left: 8px;
    background: transparent;
    border: none;
    color: inherit;
    font-size: 14px;
    cursor: pointer;
    padding: 4px 6px;
    line-height: 1;
}

/* ==========================================
   Admin — Conversations List
   ========================================== */
.conversation-list {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.conversation-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    cursor: pointer;
    transition: background 0.15s;
    border-bottom: 1px solid rgba(255, 255, 255, 0.03);
    text-decoration: none;
    color: inherit;
}

.conversation-item:hover,
.conversation-item.active {
    background: rgba(255, 255, 255, 0.05);
}

.conversation-item .avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    font-weight: 700;
    color: var(--white);
    flex-shrink: 0;
}

.conversation-item .conv-info {
    flex: 1;
    min-width: 0;
}

.conversation-item .conv-top {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
    margin-bottom: 4px;
}

.conversation-item .conv-name {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conversation-item .conv-time {
    font-size: 12px;
    color: var(--text-light);
    flex-shrink: 0;
}

.conversation-item .conv-bottom {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

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

.conversation-item .conv-you {
    color: var(--text-light);
}

.conversation-item .unread-badge {
    background: var(--accent);
    color: var(--white);
    font-size: 12px;
    font-weight: 700;
    min-width: 22px;
    height: 22px;
    padding: 0 6px;
    border-radius: 11px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    /* خاصية منطقية تعمل صحيحاً في RTL و LTR */
    margin-inline-start: 8px;
}

/* ==========================================
   Dropdown Menu
   ========================================== */
.dropdown {
    position: relative;
}

.dropdown-menu {
    display: none;
    position: absolute;
    top: 100%;
    inset-inline-start: 0;
    background: var(--header-bg);
    border-radius: var(--radius-sm);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
    min-width: 180px;
    z-index: 100;
    overflow: hidden;
    border: 1px solid var(--border-color);
}

.dropdown-menu.show {
    display: block;
    animation: fadeInUp 0.15s ease;
}

.dropdown-menu a {
    display: block;
    padding: 12px 16px;
    color: var(--text-primary);
    font-size: 14px;
    transition: background 0.15s;
}

.dropdown-menu a:hover {
    background: rgba(255, 255, 255, 0.05);
}

/* ==========================================
   Responsive
   ========================================== */
@media (max-width: 768px) {
    .message .bubble {
        max-width: 85%;
    }

    .auth-card {
        padding: 30px 24px;
    }
}

/* ==========================================
   Animations
   ========================================== */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes msgIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}
