/* ===================================
   PWA Mobile Optimizations
   تحسينات خاصة للتطبيق المثبت
   =================================== */

/* إخفاء عناصر غير ضرورية في وضع Standalone */
@media all and (display-mode: standalone) {
    /* إخفاء زر التثبيت عند التشغيل كتطبيق */
    .pwa-install-btn {
        display: none !important;
    }
    
    /* تحسين الحواف العلوية في iOS */
    body {
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
    }
    
    /* تحسين الـ navbar */
    .modern-navbar {
        padding-top: max(20px, env(safe-area-inset-top));
    }
}

/* تحسينات عامة للهواتف */
@media (max-width: 768px) {
    /* تقليل المساحات العامة */
    .container, .container-fluid {
        padding-left: 0.75rem !important;
        padding-right: 0.75rem !important;
    }
    
    /* تقليل padding للبطاقات */
    .card {
        margin-bottom: 0.5rem !important;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.06) !important;
    }
    
    .card-body {
        padding: 0.5rem !important;
        min-height: auto !important;
    }
    
    .card-header {
        padding: 0.4rem 0.5rem !important;
        font-size: 0.8rem !important;
        line-height: 1.2 !important;
    }
    
    .card-footer {
        padding: 0.4rem 0.5rem !important;
        font-size: 0.8rem !important;
    }
    
    /* تقليل حجم العناوين */
    h1 { font-size: 1.4rem !important; }
    h2 { font-size: 1.3rem !important; }
    h3 { font-size: 1.2rem !important; }
    h4 { font-size: 1.1rem !important; }
    h5 { font-size: 1rem !important; }
    h6 { font-size: 0.9rem !important; }
    
    /* تقليل حجم الأزرار */
    .btn {
        padding: 0.5rem 0.75rem !important;
        font-size: 0.85rem !important;
        border-radius: 6px !important;
    }
    
    .btn-lg {
        padding: 0.6rem 1rem !important;
        font-size: 0.9rem !important;
    }
    
    .btn-sm {
        padding: 0.25rem 0.5rem !important;
        font-size: 0.75rem !important;
    }
    
    /* تقليل حجم بطاقات الإحصائيات */
    .dashboard-card .card-body {
        padding: 0.4rem 0.3rem !important;
        min-height: auto !important;
    }
    
    .dashboard-card .fs-1,
    .stat-card .stat-value,
    .display-6 {
        font-size: 0.9rem !important;
        line-height: 1.1 !important;
        margin: 0.1rem 0 !important;
    }
    
    .dashboard-card .fs-4 {
        font-size: 0.7rem !important;
        line-height: 1.2 !important;
        margin: 0.05rem 0 !important;
    }
    
    .dashboard-card .bi,
    .stat-card .bi {
        font-size: 0.8rem !important;
        margin-bottom: 0.1rem !important;
    }
    
    /* تقليل حجم المربعات في الشبكة */
    .row {
        margin: 0 -0.375rem !important;
    }
    
    .row > * {
        padding-left: 0.375rem !important;
        padding-right: 0.375rem !important;
    }
    
    /* تحسين الجداول */
    .table {
        font-size: 0.8rem !important;
    }
    
    .table th,
    .table td {
        padding: 0.4rem !important;
        vertical-align: middle !important;
    }
    
    /* تقليل مساحة الـ forms */
    .form-group,
    .mb-3 {
        margin-bottom: 0.75rem !important;
    }
    
    .form-control {
        padding: 0.5rem 0.75rem !important;
        font-size: 0.9rem !important;
    }
    
    .form-label {
        font-size: 0.85rem !important;
        margin-bottom: 0.25rem !important;
    }
    
    /* تقليل مساحة الـ modals */
    .modal-header {
        padding: 0.75rem 1rem !important;
    }
    
    .modal-body {
        padding: 0.75rem 1rem !important;
    }
    
    .modal-footer {
        padding: 0.5rem 1rem !important;
    }
    
    /* تحسين النصوص للقراءة */
    body {
        font-size: 14px !important; /* منع التكبير التلقائي في iOS */
        line-height: 1.4 !important;
        -webkit-text-size-adjust: 100%;
    }
    
    /* تحسين الأزرار للمس */
    .btn, button, a {
        min-height: 44px; /* معيار Apple للمس */
        min-width: 44px;
    }
    
    /* تحسين أزرار الهيدر خصيصاً */
    .navbar-action-btn, .sidebar-toggle-main, .lang-toggle, .user-profile-btn {
        min-height: 36px !important;
        min-width: 36px !important;
        touch-action: manipulation;
        -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
    }
    
    /* تحسين الحقول */
    input, select, textarea {
        font-size: 16px; /* منع التكبير عند التركيز */
    }
    
    /* تحسين الشات */
    .chat-container {
        height: calc(100vh - 80px);
        height: calc(100dvh - 80px); /* Dynamic viewport height */
    }
    
    /* إخفاء عناصر غير ضرورية على الموبايل */
    .navbar-search {
        display: none !important;
    }
    
    /* تحسين توزيع عناصر الهيدر */
    .modern-navbar {
        display: grid !important;
        grid-template-columns: auto 1fr auto;
        align-items: center;
        gap: 8px;
    }
    
    .sidebar-toggle-main {
        grid-column: 1;
        justify-self: start;
        position: relative !important;
        left: auto !important;
        right: auto !important;
        top: auto !important;
        transform: none !important;
    }
    
    .navbar-header-custom {
        grid-column: 2;
        justify-self: center;
        position: relative !important;
        margin: 0 !important;
    }
    
    .navbar-actions {
        grid-column: 3;
        justify-self: end;
        position: relative !important;
        left: auto !important;
        right: auto !important;
        top: auto !important;
        transform: none !important;
    }
    
    /* تحسين الـ cards */
    .card {
        border-radius: 16px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    }
}

/* تحسينات للوضع الأفقي */
@media (max-width: 768px) and (orientation: landscape) {
    .modern-navbar {
        padding: 10px 20px;
    }
    
    .chat-container {
        height: calc(100vh - 60px);
    }
}

/* تحسينات iOS خاصة */
@supports (-webkit-touch-callout: none) {
    /* منع التكبير عند النقر المزدوج */
    * {
        touch-action: manipulation;
    }
    
    /* تحسين الـ scroll */
    body, .chat-messages, .scroll-container {
        -webkit-overflow-scrolling: touch;
    }
    
    /* إزالة الـ highlight عند اللمس */
    * {
        -webkit-tap-highlight-color: transparent;
    }
}

/* Animation للتطبيق المثبت */
@media all and (display-mode: standalone) {
    @keyframes fadeInApp {
        from {
            opacity: 0;
            transform: scale(0.95);
        }
        to {
            opacity: 1;
            transform: scale(1);
        }
    }
    
    body {
        animation: fadeInApp 0.3s ease-out;
    }
}

/* تحسين الإشعارات للهواتف */
.notification-dropdown {
    max-height: calc(100vh - 80px);
    max-height: calc(100dvh - 80px);
}

@media (max-width: 576px) {
    .notification-dropdown {
        left: 0 !important;
        right: 0 !important;
        width: 100vw !important;
        border-radius: 0 !important;
        top: 70px !important;
    }
}

/* تحسين زر التثبيت المنبثق */
#install-app-btn {
    z-index: 9999;
}

@media (max-width: 576px) {
    #install-app-btn > div {
        bottom: 20px !important;
        right: 10px !important;
        left: 10px !important;
        max-width: none !important;
    }
}

/* تحسين الجداول للهواتف */
@media (max-width: 768px) {
    .table-responsive {
        border-radius: 8px !important;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin-bottom: 0.75rem !important;
    }
    
    table {
        min-width: 100%;
        font-size: 0.75rem !important;
    }
    
    .table th {
        padding: 0.4rem 0.3rem !important;
        font-size: 0.7rem !important;
        font-weight: 600 !important;
        background: #f8f9fa;
        border-bottom: 1px solid #dee2e6 !important;
    }
    
    .table td {
        padding: 0.4rem 0.3rem !important;
        font-size: 0.75rem !important;
        border-bottom: 1px solid #f1f3f4 !important;
    }
    
    /* تحسين عرض الأرقام والمبالغ */
    .table .text-end,
    .table .text-start {
        white-space: nowrap;
    }
    
    /* تحسين أزرار الجدول */
    .table .btn {
        padding: 0.2rem 0.4rem !important;
        font-size: 0.7rem !important;
        border-radius: 4px !important;
    }
}

/* تحسين المودال للهواتف */
@media (max-width: 576px) {
    .modal-dialog {
        margin: 0;
        max-width: 100%;
        height: 100vh;
        height: 100dvh;
    }
    
    .modal-content {
        height: 100%;
        border-radius: 0;
    }
}

/* Dark mode support for PWA */
@media (prefers-color-scheme: dark) {
    /* يمكن إضافة تحسينات للوضع الداكن هنا */
}

/* تحسين السرعة - Hardware Acceleration */
.modern-navbar,
.chat-container,
.notification-dropdown,
.modal {
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
    will-change: transform;
}

/* تحسين الفوكس للكيبورد */
@media (max-width: 768px) {
    input:focus,
    textarea:focus,
    select:focus {
        /* منع القفز عند ظهور الكيبورد */
        scroll-margin-bottom: 200px;
    }
}

/* Pull to refresh indicator */
@media all and (display-mode: standalone) {
    body {
        overscroll-behavior-y: contain;
    }
}

/* تحسين الصور للتطبيق */
img {
    max-width: 100%;
    height: auto;
}

/* تحسين الأيقونات */
.bi {
    vertical-align: middle;
}

/* تحسين Loading States */
.loading-spinner {
    animation: spin 1s linear infinite;
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* Prevent zoom on double tap */
button, a, input, select {
    touch-action: manipulation;
}

/* تحسين الـ Focus للوصولية */
*:focus-visible {
    outline: 2px solid #667eea;
    outline-offset: 2px;
}

/* تحسين الانتقالات */
* {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* تحسينات إضافية للمساحات الصغيرة جداً */
@media (max-width: 576px) {
    /* تحسين أقصى للجوال الصغير */
    .container,
    .container-fluid {
        padding-left: 0.5rem !important;
        padding-right: 0.5rem !important;
    }
    
    /* تحسين البطاقات للشاشات الصغيرة */
    .card {
        border-radius: 12px !important;
        margin-bottom: 0.5rem !important;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06) !important;
    }
    
    .card-body {
        padding: 0.5rem !important;
    }
    
    .card-header {
        padding: 0.4rem 0.5rem !important;
        font-size: 0.8rem !important;
    }
    
    /* تصغير أكثر للعناوين */
    h1 { font-size: 1.1rem !important; }
    h2 { font-size: 1rem !important; }
    h3 { font-size: 0.95rem !important; }
    h4 { font-size: 0.9rem !important; }
    h5 { font-size: 0.85rem !important; }
    h6 { font-size: 0.8rem !important; }
    
    /* تصغير جميع الأيقونات */
    .card .bi,
    .dashboard-card .bi,
    .stat-card .bi {
        font-size: 0.75rem !important;
    }
    
    /* تصغير الأزرار أكثر */
    .btn {
        padding: 0.4rem 0.6rem !important;
        font-size: 0.8rem !important;
        border-radius: 6px !important;
        min-height: 36px !important;
    }
    
    .btn-sm {
        padding: 0.2rem 0.4rem !important;
        font-size: 0.7rem !important;
        min-height: 30px !important;
    }
    
    /* تقليل حجم الأيقونات */
    .bi {
        font-size: 0.8rem !important;
    }
    
    /* تحسين الإحصائيات */
    .dashboard-card .card-body {
        padding: 0.4rem 0.3rem !important;
    }
    
    .dashboard-card .fs-1,
    .display-6 {
        font-size: 1.1rem !important;
    }
    
    .dashboard-card .fs-4 {
        font-size: 0.75rem !important;
    }
    
    /* تحسين النماذج */
    .form-control {
        padding: 0.4rem 0.5rem !important;
        font-size: 0.85rem !important;
        min-height: 38px !important;
    }
    
    .form-label {
        font-size: 0.8rem !important;
        margin-bottom: 0.2rem !important;
    }
    
    /* تحسين المساحات */
    .py-4 { padding-top: 0.75rem !important; padding-bottom: 0.75rem !important; }
    .py-3 { padding-top: 0.5rem !important; padding-bottom: 0.5rem !important; }
    .py-2 { padding-top: 0.25rem !important; padding-bottom: 0.25rem !important; }
    .my-4 { margin-top: 0.75rem !important; margin-bottom: 0.75rem !important; }
    .my-3 { margin-top: 0.5rem !important; margin-bottom: 0.5rem !important; }
    .my-2 { margin-top: 0.25rem !important; margin-bottom: 0.25rem !important; }
    
    .mb-4 { margin-bottom: 0.75rem !important; }
    .mb-3 { margin-bottom: 0.5rem !important; }
    .mb-2 { margin-bottom: 0.25rem !important; }
    
    /* تحسين النصوص */
    body {
        font-size: 13px !important;
        line-height: 1.35 !important;
    }
    
    /* تحسين القوائم */
    .list-group-item {
        padding: 0.4rem 0.5rem !important;
        font-size: 0.8rem !important;
    }
    
    /* تحسين الـ alerts */
    .alert {
        padding: 0.4rem 0.5rem !important;
        font-size: 0.8rem !important;
        margin-bottom: 0.5rem !important;
    }
}

/* تحسينات للشاشات الصغيرة جداً (أقل من 400px) */
@media (max-width: 400px) {
    .container,
    .container-fluid {
        padding-left: 0.25rem !important;
        padding-right: 0.25rem !important;
    }
    
    .card {
        border-radius: 8px !important;
        margin-bottom: 0.4rem !important;
    }
    
    .card-body {
        padding: 0.4rem !important;
    }
    
    .btn {
        padding: 0.3rem 0.5rem !important;
        font-size: 0.75rem !important;
        min-height: 32px !important;
    }
    
    h1 { font-size: 1.1rem !important; }
    h2 { font-size: 1rem !important; }
    h3 { font-size: 0.95rem !important; }
    
    .dashboard-card .card-body {
        padding: 0.3rem 0.2rem !important;
    }
    
    .dashboard-card .fs-1 {
        font-size: 1rem !important;
    }
    
    .dashboard-card .fs-4 {
        font-size: 0.7rem !important;
    }
}
