/* ═══════════════════════════════════════════════════════════
   تاجر تيك | TAJER TECH - Live Chats Styles
   ═══════════════════════════════════════════════════════════ */

.chats-layout {
    display: grid;
    grid-template-columns: 320px 1fr;
    grid-template-rows: minmax(0, 1fr);  /* CRITICAL: forces row to fill container height */
    background: var(--color-bg-card);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: var(--shadow-lg);
    /* Let the parent flex control height — no fixed calc */
    flex: 1;
    min-height: 0;
}

/* Sidebar List */
.chats-sidebar {
    border-left: 1px solid var(--color-border);
    display: flex;
    flex-direction: column;
    background: rgba(15, 23, 42, 0.3);
    min-height: 0;
}

.chats-sidebar-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--color-border);
    font-weight: 700;
    font-size: 1.05rem;
    color: var(--color-text-primary);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
}

.chats-tabs {
    display: flex;
    border-bottom: 1px solid var(--color-border);
    background: rgba(0, 0, 0, 0.1);
    flex-shrink: 0;
}

.chats-tab-btn {
    flex: 1;
    background: none;
    border: none;
    padding: 12px 8px;
    font-family: inherit;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--color-text-secondary);
    cursor: pointer;
    transition: var(--transition-fast);
    position: relative;
    text-align: center;
}

.chats-tab-btn:hover {
    color: var(--color-text-primary);
}

.chats-tab-btn.active {
    color: var(--color-primary);
}

.chats-tab-btn.active::after {
    content: '';
    position: absolute;
    bottom: -1px;
    left: 0;
    right: 0;
    height: 2px;
    background: var(--color-primary);
}

.chats-list-container {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-height: 0;
}

/* Chat Item */
.chat-item {
    display: flex;
    flex-direction: column;
    padding: 12px;
    background: rgba(255, 255, 255, 0.02);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: var(--transition-fast);
    position: relative;
}

.chat-item:hover {
    background: rgba(255, 255, 255, 0.05);
    border-color: var(--color-border-light);
}

.chat-item.active {
    background: rgba(16, 185, 129, 0.08);
    border-color: var(--color-primary);
}

.chat-item-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 6px;
}

.chat-item-name {
    font-weight: 700;
    font-size: 0.88rem;
    color: var(--color-text-primary);
}

.chat-item-time {
    font-size: 0.7rem;
    color: var(--color-text-muted);
}

.chat-item-details {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chat-item-phone {
    font-size: 0.75rem;
    color: var(--color-text-secondary);
    direction: ltr;
}

.chat-item-badge {
    background: var(--color-primary);
    color: #fff;
    font-size: 0.65rem;
    font-weight: 700;
    min-width: 16px;
    height: 16px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
}

/* Pending Request Item Specifics */
.pending-request-item {
    border-color: rgba(245, 158, 11, 0.3);
    background: rgba(245, 158, 11, 0.02);
}

.pending-request-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-top: 10px;
}

.pending-btn {
    padding: 6px 12px;
    border: none;
    border-radius: 6px;
    font-size: 0.78rem;
    font-weight: 700;
    cursor: pointer;
    font-family: inherit;
    transition: var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
}

.pending-btn-accept {
    background: var(--color-primary);
    color: #fff;
}

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

.pending-btn-reject {
    background: rgba(239, 68, 68, 0.1);
    color: var(--color-red);
    border: 1px solid rgba(239, 68, 68, 0.2);
}

.pending-btn-reject:hover {
    background: rgba(239, 68, 68, 0.2);
}

/* ═══════════════════════════════════════════════════════════
   Right Content Area — CRITICAL: Full flex column layout
   Header + Messages + Input all in a strict column
   ═══════════════════════════════════════════════════════════ */
.chats-content {
    display: flex;
    flex-direction: column;
    background: rgba(15, 23, 42, 0.1);
    position: relative;
    /* The grid cell gives us height — we must not overflow */
    min-height: 0;
    overflow: hidden;
}

.chats-empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--color-text-secondary);
    padding: 40px;
    text-align: center;
    gap: 16px;
}

.chats-empty-icon {
    font-size: 3.5rem;
}

.chats-empty-title {
    font-weight: 700;
    font-size: 1.15rem;
    color: var(--color-text-primary);
}

/* ═══════════════════════════════════════════════════════════
   Chat Header — FIXED at top, never scrolls
   ═══════════════════════════════════════════════════════════ */
.chats-content-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 16px;
    border-bottom: 1px solid var(--color-border);
    background: rgba(15, 23, 42, 0.4);
    flex-shrink: 0;
    gap: 8px;
    min-height: 56px;
}

/* Right section: office name + available status */
.chats-header-right-section {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
    flex-shrink: 1;
}

.chats-header-marketer {
    display: flex;
    flex-direction: column;
    gap: 1px;
    min-width: 0;
}

.chats-header-title {
    font-weight: 700;
    font-size: 0.9rem;
    color: var(--color-text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chats-header-subtitle {
    font-size: 0.6rem;
    color: var(--color-primary);
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 4px;
}

/* Center section: customer name */
.chats-header-center-section {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.chats-header-customer-name {
    font-size: 0.82rem;
    color: var(--color-text-secondary);
    font-weight: 700;
    white-space: nowrap;
}

/* Left section: platform name + end button */
.chats-header-left-section {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.chats-header-platform-title {
    font-weight: 800;
    font-size: 1rem;
    color: #fff;
    letter-spacing: 0.5px;
    white-space: nowrap;
}

.chats-header-platform-title span {
    color: var(--color-primary);
}

.btn-end-chat {
    background: rgba(239, 68, 68, 0.1);
    color: var(--color-red);
    border: 1px solid rgba(239, 68, 68, 0.25);
    padding: 5px 10px;
    border-radius: 8px;
    font-size: 0.72rem;
    font-weight: 700;
    cursor: pointer;
    font-family: inherit;
    transition: var(--transition-fast);
    white-space: nowrap;
}

.btn-end-chat:hover {
    background: var(--color-red);
    color: white;
}

/* ═══════════════════════════════════════════════════════════
   Chat Messages — SCROLLABLE, takes all remaining space
   ═══════════════════════════════════════════════════════════ */
.chats-messages-wrapper {
    flex: 1;
    min-height: 0;       /* CRITICAL: allows flex to shrink */
    padding: 20px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    display: flex;
    flex-direction: column;
    gap: 14px;
    background: radial-gradient(circle at 50% 100%, rgba(16, 185, 129, 0.02) 0%, transparent 60%);
}

.chats-msg-row {
    display: flex;
    width: 100%;
}

.chats-msg-row.sent {
    justify-content: flex-start;
    flex-direction: row-reverse;
}

.chats-msg-row.received {
    justify-content: flex-start;
}

.chats-msg-bubble {
    max-width: 70%;
    padding: 10px 14px;
    border-radius: var(--radius-md);
    font-size: 0.88rem;
    line-height: 1.5;
    position: relative;
    word-break: break-word;
    box-shadow: var(--shadow-sm);
}

.chats-msg-row.sent .chats-msg-bubble {
    background: var(--color-primary);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.chats-msg-row.received .chats-msg-bubble {
    background: var(--color-bg-card);
    color: var(--color-text-primary);
    border-bottom-left-radius: 4px;
    border: 1px solid var(--color-border);
}

.chats-msg-time {
    display: block;
    font-size: 0.65rem;
    color: rgba(255, 255, 255, 0.5);
    margin-top: 4px;
    text-align: left;
}

.chats-msg-row.received .chats-msg-time {
    color: var(--color-text-muted);
    text-align: right;
}

.chats-media-content {
    border-radius: 8px;
    max-width: 100%;
    max-height: 180px;
    margin-top: 4px;
    display: block;
    cursor: pointer;
}

.chats-system-msg {
    align-self: center;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--color-border);
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 0.72rem;
    color: var(--color-text-secondary);
    text-align: center;
    max-width: 80%;
    margin: 6px 0;
}

/* ═══════════════════════════════════════════════════════════
   Chat Input — FIXED at bottom, never scrolls
   ═══════════════════════════════════════════════════════════ */
.chats-input-area {
    padding: 12px 16px;
    border-top: 1px solid var(--color-border);
    background: rgba(0, 0, 0, 0.2);
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.chats-attach-btn {
    background: var(--color-bg-card);
    border: 1px solid var(--color-border);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    color: var(--color-text-primary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition-fast);
    flex-shrink: 0;
}

.chats-attach-btn:hover {
    background: var(--color-bg-hover);
    border-color: var(--color-border-light);
}

.chats-input-field {
    flex: 1;
    background: var(--color-bg-main);
    border: 1px solid var(--color-border);
    border-radius: 20px;
    padding: 9px 16px;
    color: #fff;
    font-size: 0.88rem;
    outline: none;
    transition: var(--transition-fast);
    min-width: 0;
}

.chats-input-field:focus {
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.1);
}

.chats-send-btn {
    background: var(--color-primary);
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition-fast);
    box-shadow: 0 2px 6px rgba(16, 185, 129, 0.2);
    flex-shrink: 0;
}

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

/* Back button for mobile */
.btn-back-chats {
    background: none;
    border: none;
    color: var(--color-primary);
    font-size: 1.25rem;
    cursor: pointer;
    padding: 4px;
    display: none;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

/* ═══════════════════════════════════════════════════════════
   Chats Page Layout — makes .page-content a pure flex container
   ═══════════════════════════════════════════════════════════ */
.page-content.chats-page-active {
    overflow: hidden !important;
    padding: 16px !important;
    display: flex !important;
    flex-direction: column !important;
    min-height: 0 !important;
    /* Ensure the flex container doesn't grow beyond viewport */
    height: 0 !important;  /* flex:1 from base + this = exact remaining space */
    flex: 1 1 0% !important;
}

/* ═══════════════════════════════════════════════════════════
   MOBILE: Responsive Chats (max-width: 900px)
   ═══════════════════════════════════════════════════════════ */
@media (max-width: 900px) {
    /* إزالة الـ padding الزائد تماماً على صفحة الدردشة في الجوال */
    .page-content.chats-page-active {
        padding: 0 !important;
    }

    .chats-layout {
        grid-template-columns: 1fr;
        border-radius: 0;
        border: none;
        box-shadow: none;
    }
    
    .chats-sidebar {
        display: flex;
    }
    
    .chats-sidebar.hidden-mobile {
        display: none;
    }
    
    .chats-content {
        display: none;
    }
    
    .chats-content.active-mobile {
        display: flex;
    }

    .btn-back-chats {
        display: flex !important;
    }

    /* ═══ هيدر الجوال: سطرين بدلاً من سطر واحد ═══ */
    .chats-content-header {
        flex-wrap: wrap;
        padding: 10px 12px 8px;
        gap: 6px;
        min-height: auto;
    }

    /* السطر الأول: رجوع + اسم المكتب + اسم المنصة */
    .chats-header-right-section {
        flex: 1;
        min-width: 0;
        gap: 6px;
    }

    .chats-header-marketer {
        min-width: 0;
        flex: 1;
    }

    .chats-header-title {
        font-size: 0.78rem;
        white-space: normal;      /* السماح بالتفاف النص */
        overflow: visible;
        text-overflow: unset;
        line-height: 1.3;
        display: -webkit-box;
        -webkit-line-clamp: 2;    /* سطرين كحد أقصى */
        -webkit-box-orient: vertical;
        overflow: hidden;
    }
    
    .chats-header-subtitle {
        font-size: 0.55rem;
    }

    .chats-header-platform-title {
        font-size: 0.82rem;
        flex-shrink: 0;
    }

    /* إخفاء اسم المنصة من السطر الأول على الجوال — نضعه ضمنياً */
    .chats-header-left-section {
        flex-shrink: 0;
    }

    .chats-header-left-section .chats-header-platform-title {
        display: none;  /* إخفاء اسم المنصة لتوفير المساحة على الجوال */
    }

    /* السطر الثاني: اسم العميل + زر الإنهاء */
    .chats-header-center-section {
        order: 10;
        flex: 1;
        justify-content: flex-start;
    }

    .chats-header-left-section {
        order: 11;
    }

    .chats-header-customer-name {
        font-size: 0.72rem;
    }

    .btn-end-chat {
        font-size: 0.62rem;
        padding: 4px 8px;
        white-space: nowrap;
    }

    /* ═══ منطقة الرسائل ═══ */
    .chats-messages-wrapper {
        padding: 12px;
    }

    /* ═══ صندوق الكتابة — ثابت + safe area للآيفون ═══ */
    .chats-input-area {
        padding: 10px 12px;
        padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    }
}

