/**
 * إصلاحات التصميم على الجوال
 * Mobile Design Fixes
 */

/* ==================== منع Overflow الأفقي ==================== */
html {
    overflow-x: hidden;
    max-width: 100vw;
}

body {
    overflow-x: hidden;
    max-width: 100vw;
    position: relative;
}

/* ==================== Container المتجاوب ==================== */
.container {
    width: 100%;
    max-width: 100%;
    padding-left: 1rem;
    padding-right: 1rem;
}

@media (min-width: 640px) {
    .container {
        padding-left: 1.5rem;
        padding-right: 1.5rem;
    }
}

@media (min-width: 768px) {
    .container {
        max-width: 768px;
        margin-left: auto;
        margin-right: auto;
    }
}

@media (min-width: 1024px) {
    .container {
        max-width: 1024px;
    }
}

@media (min-width: 1280px) {
    .container {
        max-width: 1280px;
    }
}

/* ==================== الصور المتجاوبة ==================== */
img {
    max-width: 100%;
    height: auto;
}

/* ==================== الجداول المتجاوبة ==================== */
@media (max-width: 768px) {
    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        display: block;
        width: 100%;
    }

    table {
        width: 100%;
        max-width: 100%;
    }

    /* تصغير حجم الخط في الجداول */
    table td,
    table th {
        font-size: 0.875rem;
        padding: 0.5rem;
    }
}

/* ==================== النصوص الطويلة ==================== */
.text-overflow-ellipsis {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

@media (max-width: 640px) {
    /* منع النصوص الطويلة من التجاوز */
    h1 {
        font-size: 1.875rem;
        line-height: 2.25rem;
        word-break: break-word;
    }

    h2 {
        font-size: 1.5rem;
        line-height: 2rem;
        word-break: break-word;
    }

    h3 {
        font-size: 1.25rem;
        line-height: 1.75rem;
        word-break: break-word;
    }
}

/* ==================== الأزرار المتجاوبة ==================== */
@media (max-width: 640px) {
    .btn-responsive {
        width: 100%;
        padding: 0.75rem 1rem;
        font-size: 0.875rem;
    }

    /* الأزرار في الصفوف */
    .flex-col-mobile {
        flex-direction: column;
        gap: 0.5rem;
    }

    .flex-col-mobile > * {
        width: 100%;
    }
}

/* ==================== البطاقات (Cards) المتجاوبة ==================== */
@media (max-width: 640px) {
    .card-mobile {
        padding: 1rem;
        margin-bottom: 1rem;
    }

    /* Grid Cards */
    .grid-cols-mobile-1 {
        grid-template-columns: repeat(1, minmax(0, 1fr));
    }

    .grid-cols-mobile-2 {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

/* ==================== القائمة المنسدلة (Dropdown) ==================== */
@media (max-width: 640px) {
    .dropdown-menu {
        position: fixed;
        left: 0;
        right: 0;
        top: auto;
        bottom: 0;
        max-width: 100vw;
        border-radius: 1rem 1rem 0 0;
        max-height: 80vh;
        overflow-y: auto;
    }
}

/* ==================== النماذج (Forms) المتجاوبة ==================== */
@media (max-width: 640px) {
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="tel"],
    input[type="number"],
    select,
    textarea {
        font-size: 16px; /* منع zoom تلقائي في iOS */
        width: 100%;
    }

    /* الحقول في صف واحد */
    .form-row-mobile {
        flex-direction: column;
        gap: 1rem;
    }

    .form-row-mobile > * {
        width: 100%;
    }
}

/* ==================== الهيدر المتجاوب ==================== */
@media (max-width: 768px) {
    header {
        position: sticky;
        top: 0;
        z-index: 50;
    }

    .header-nav {
        padding: 0.75rem 1rem;
    }
}

/* ==================== القائمة المحمولة ==================== */
@media (max-width: 768px) {
    #mobile-menu {
        max-height: calc(100vh - 80px);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    #mobile-menu a {
        display: flex;
        align-items: center;
        gap: 0.75rem;
        padding: 0.75rem 1rem;
        border-radius: 0.5rem;
        transition: background-color 0.2s;
    }

    #mobile-menu a:active {
        background-color: rgba(255, 255, 255, 0.1);
    }
}

/* ==================== الصور في البطاقات ==================== */
@media (max-width: 640px) {
    .item-card-image {
        height: 200px;
        object-fit: cover;
    }

    .item-card {
        margin-bottom: 1rem;
    }
}

/* ==================== Grid المتجاوب للعروض ==================== */
@media (max-width: 640px) {
    .items-grid {
        grid-template-columns: repeat(1, minmax(0, 1fr));
        gap: 1rem;
    }
}

@media (min-width: 640px) and (max-width: 768px) {
    .items-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 1rem;
    }
}

@media (min-width: 768px) and (max-width: 1024px) {
    .items-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 1.5rem;
    }
}

@media (min-width: 1024px) {
    .items-grid {
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 1.5rem;
    }
}

/* ==================== الفلاتر المتجاوبة ==================== */
@media (max-width: 768px) {
    .filters-container {
        flex-direction: column;
        gap: 0.75rem;
    }

    .filter-button {
        width: 100%;
        justify-content: center;
    }

    /* Horizontal scroll للفئات */
    .categories-scroll {
        display: flex;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        gap: 0.5rem;
        padding-bottom: 0.5rem;
        scrollbar-width: none;
        -ms-overflow-style: none;
    }

    .categories-scroll::-webkit-scrollbar {
        display: none;
    }

    .category-button {
        flex-shrink: 0;
        min-width: fit-content;
        white-space: nowrap;
    }
}

/* ==================== المحادثات (Chat) المتجاوبة ==================== */
@media (max-width: 640px) {
    .chat-container {
        height: calc(100vh - 180px);
        min-height: 400px;
    }

    .chat-header {
        position: sticky;
        top: 0;
        z-index: 10;
        background: white;
    }

    .chat-input-container {
        position: sticky;
        bottom: 0;
        background: white;
        padding: 1rem;
    }

    .message-bubble {
        max-width: 85%;
        word-break: break-word;
    }
}

/* ==================== الملف الشخصي المتجاوب ==================== */
@media (max-width: 640px) {
    .profile-header {
        flex-direction: column;
        text-align: center;
    }

    .profile-avatar {
        width: 100px;
        height: 100px;
        margin: 0 auto;
    }

    .profile-stats {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.75rem;
    }
}

/* ==================== صفحة الطلبات المتجاوبة ==================== */
@media (max-width: 768px) {
    .order-card {
        padding: 1rem;
    }

    .order-details {
        flex-direction: column;
        gap: 1rem;
    }

    .order-image {
        width: 100%;
        height: 200px;
        object-fit: cover;
    }
}

/* ==================== Modal المتجاوب ==================== */
@media (max-width: 640px) {
    .modal-content {
        width: 95vw;
        max-width: 95vw;
        margin: 1rem;
        max-height: 90vh;
        overflow-y: auto;
    }

    .modal-header {
        padding: 1rem;
        font-size: 1.25rem;
    }

    .modal-body {
        padding: 1rem;
    }
}

/* ==================== الأيقونات المتجاوبة ==================== */
@media (max-width: 640px) {
    .icon-sm {
        width: 1rem;
        height: 1rem;
    }

    .icon-md {
        width: 1.25rem;
        height: 1.25rem;
    }

    .icon-lg {
        width: 1.5rem;
        height: 1.5rem;
    }
}

/* ==================== منع التكبير على input في iOS ==================== */
@media (max-width: 640px) {
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="tel"],
    select,
    textarea {
        font-size: 16px !important;
    }
}

/* ==================== Sticky Footer ==================== */
@media (max-width: 640px) {
    .sticky-footer {
        position: sticky;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 40;
        background: white;
        border-top: 2px solid #E5E7EB;
        padding: 1rem;
        box-shadow: 0 -4px 6px -1px rgba(0, 0, 0, 0.1);
    }
}

/* ==================== Safe Area للأجهزة الحديثة ==================== */
@supports (padding: max(0px)) {
    .safe-area-padding {
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-right: max(1rem, env(safe-area-inset-right));
        padding-top: max(0px, env(safe-area-inset-top));
        padding-bottom: max(0px, env(safe-area-inset-bottom));
    }
}

/* ==================== تحسين الأداء ==================== */
* {
    -webkit-tap-highlight-color: transparent;
}

a, button {
    touch-action: manipulation;
}

/* Smooth scrolling */
html {
    scroll-behavior: smooth;
}

/* ==================== إصلاح مشاكل خاصة ==================== */

/* منع horizontal scroll */
.no-horizontal-scroll {
    overflow-x: hidden;
    max-width: 100vw;
}

/* Truncate text with ellipsis */
.truncate {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Line clamp */
.line-clamp-2 {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.line-clamp-3 {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* ==================== تحسين المسافات ==================== */
@media (max-width: 640px) {
    .mobile-spacing-sm {
        padding: 0.5rem;
        margin-bottom: 0.5rem;
    }

    .mobile-spacing-md {
        padding: 1rem;
        margin-bottom: 1rem;
    }

    .mobile-spacing-lg {
        padding: 1.5rem;
        margin-bottom: 1.5rem;
    }
}

/* ==================== إصلاح Flexbox ==================== */
@media (max-width: 640px) {
    .flex-mobile-col {
        flex-direction: column;
    }

    .flex-mobile-wrap {
        flex-wrap: wrap;
    }

    .justify-mobile-center {
        justify-content: center;
    }

    .items-mobile-center {
        align-items: center;
    }
}

/* ==================== تحسين النقرات ==================== */
.clickable {
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
}

.clickable:active {
    opacity: 0.7;
}

/* ==================== Loading States ==================== */
.skeleton {
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: loading 1.5s infinite;
}

@keyframes loading {
    0% {
        background-position: 200% 0;
    }
    100% {
        background-position: -200% 0;
    }
}

/* ==================== إصلاح الظلال على الجوال ==================== */
@media (max-width: 640px) {
    .shadow-mobile-sm {
        box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    }

    .shadow-mobile-md {
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    }

    .shadow-mobile-lg {
        box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    }
}
