/* 
   MOBILE STYLESHEET
   Independent mobile overrides for Aelpy Web Store
   Target: Screens narrower than 768px (Tablets & Phones)
*/

@media (max-width: 768px) {

    /* --- NAVIGATION --- */
    /* Fix header overflow on small screens */
    .navbar-brand {
        max-width: 60% !important;
        /* Leave room for hamburger and cart */
        overflow: hidden !important;
    }

    .navbar-brand a {
        width: 100%;
    }

    .brand-text {
        font-size: 0.9rem !important;
        /* Scale down text */
        white-space: nowrap !important;
    }

    .mobile-menu-toggle {
        display: flex !important;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        flex-shrink: 0;
        /* Prevent squishing */
        border-radius: 50%;
        background: var(--bg-card);
        border: 1px solid var(--border-color);
        color: var(--text-primary);
        cursor: pointer;
        transition: all 0.2s ease;
    }

    .nav-links {
        display: none;
        /* Hidden by default */
        position: fixed;
        top: var(--header-height);
        left: 0;
        width: 100%;
        height: calc(100vh - var(--header-height));
        background: rgba(5, 5, 5, 0.98);
        /* Deep dark bg */
        backdrop-filter: blur(25px);
        -webkit-backdrop-filter: blur(25px);
        flex-direction: column;
        align-items: center;
        justify-content: flex-start;
        padding: 2rem 1.5rem;
        gap: 1.5rem;
        z-index: 9999;
        overflow-y: auto;
        border-top: 1px solid var(--border-color);
    }

    .nav-links.active {
        display: flex !important;
        animation: mobileMenuIn 0.3s cubic-bezier(0.16, 1, 0.3, 1) forwards;
    }

    @keyframes mobileMenuIn {
        from {
            opacity: 0;
            transform: translateY(10px);
        }

        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    .nav-link {
        font-size: 1.1rem;
        width: 100%;
        text-align: center;
        padding: 1rem;
        border-radius: var(--radius-sm);
        border: 1px solid transparent;
        color: var(--text-primary) !important;
    }

    .nav-link:active,
    .nav-link:hover {
        background: var(--bg-card-hover);
        border-color: var(--border-color);
        color: var(--accent-primary) !important;
    }

    .nav-auth-section {
        flex-direction: column;
        width: 100%;
        gap: 1rem;
        border-top: 1px solid var(--border-color);
        padding-top: 1.5rem;
        margin-top: auto;
        /* Push to bottom if space permits */
    }

    .nav-auth-section .btn {
        width: 100%;
        padding: 1rem;
        font-size: 1rem;
    }

    /* --- LAYOUT & TYPOGRAPHY --- */
    /* --- LAYOUT & TYPOGRAPHY --- */
    html,
    body {
        overflow-x: hidden !important;
        max-width: 100vw !important;
        overflow-anchor: none !important;
        /* Fix scroll jumping */
    }

    .container {
        padding-left: 1.5rem !important;
        padding-right: 1.5rem !important;
        margin-left: auto !important;
        margin-right: auto !important;
        max-width: 100% !important;
        width: 100% !important;
        box-sizing: border-box !important;
    }

    /* Optimize padding for smaller screens (iPhone SE, etc.) to give more room to grid */
    @media (max-width: 360px) {
        .container {
            padding-left: 1rem !important;
            padding-right: 1rem !important;
        }
    }

    /* Restore Padding for Navbar specifically */
    .navbar .container {
        padding: 0 1rem !important;
    }

    /* Restore Padding for Footer specifically */
    /* Safety padding for text elements outside grid */
    /* h1, h2, h3, p { Removed explicit padding } */

    h1 {
        font-size: 2rem;
    }

    h2 {
        font-size: 1.75rem;
    }

    h3 {
        font-size: 1.25rem;
    }

    /* Navbar Spacing */
    .navbar .container {
        padding: 0 1rem !important;
        /* Keep navbar tight but clean */
        gap: 0.5rem;
    }

    .hero {
        padding: 3rem 0;
    }

    /* Fixed WhatsApp Button position for mobile */
    .whatsapp-float {
        bottom: 20px !important;
        right: 20px !important;
        width: 50px !important;
        height: 50px !important;
    }

    .whatsapp-float img {
        width: 28px !important;
        height: 28px !important;
    }

    .hero-row {
        font-size: 2.5rem;
        flex-direction: column;
        gap: 0;
        line-height: 1.1;
    }

    .hero-buttons {
        flex-direction: column;
        width: 100%;
        gap: 1rem;
    }

    .hero-buttons .btn {
        width: 100%;
    }

    /* --- GRIDS & CARDS --- */

    .product-grid {
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        gap: 0.75rem !important;
        padding: 0 !important;
        /* Remove internal padding, rely on container */
        width: 100% !important;
        margin: 0 auto !important;
        /* CENTERING FORCE */
        justify-content: center !important;
        justify-items: center !important;
    }

    /* Ensure immediate parent of grid doesn't shift it */
    .product-grid-container,
    .col-md-9,
    .row {
        margin-left: 0 !important;
        margin-right: 0 !important;
        padding-left: 0 !important;
        padding-right: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
    }

    .product-card {
        border-radius: var(--radius-sm);
        margin-bottom: 0 !important;
        min-width: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        /* Ensure card doesn't overflow cell */
        display: flex;
        flex-direction: column;
    }

    /* Ratio is handled by global site.css now, but ensure width is full */
    .product-image-container {
        width: 100% !important;
        margin: 0 0 0.5rem 0 !important;
        border-radius: var(--radius-sm) var(--radius-sm) 0 0 !important;
        /* Force aspect ratio again just in case */
        aspect-ratio: 2/3 !important;
        height: auto !important;
    }

    /* Small font adjustments for 2-column mobile view */
    .product-card h3 {
        font-size: 0.85rem !important;
        margin-bottom: 0.25rem;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }

    .price-tag {
        font-size: 1rem !important;
    }

    .product-actions {
        display: flex !important;
        flex-direction: column !important;
        /* Stack vertically to save horizontal space in 2-col */
        gap: 0.5rem !important;
        margin-top: auto !important;
        /* Push to bottom */
    }

    .product-actions .btn {
        padding: 0.5rem 0 !important;
        font-size: 0.75rem !important;
        width: 100%;
        margin: 0 !important;
    }

    .old-price {
        font-size: 0.7rem !important;
    }

    /* Category Grid: 2 Columns */
    .category-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 0.8rem;
    }

    .category-banner {
        aspect-ratio: 4 / 3;
        /* More compact than 16:9 on mobile */
    }

    .category-overlay span {
        font-size: 0.9rem;
    }

    /* --- SLIDER --- */
    .main-slider {
        aspect-ratio: 16 / 9 !important;
        /* Taller on mobile for visibility */
        margin: 1rem 0;
        width: 100%;
    }

    .slider-arrow {
        padding: 12px;
        font-size: 14px;
    }

    /* --- SCROLLING WRAPPER (Best Sellers) --- */
    .scrolling-wrapper .card {
        min-width: 160px !important;
        max-width: 160px !important;
        margin-right: 0.8rem !important;
    }

    .scrolling-wrapper .product-image-container {
        height: 140px !important;
    }

    /* --- FOOTER --- */
    footer {
        padding: 3rem 0 2rem;
        margin-top: 3rem;
    }

    footer .container {
        grid-template-columns: 1fr !important;
        text-align: center;
        gap: 2.5rem !important;
    }

    footer h4 {
        margin-bottom: 1rem;
        font-size: 1.1rem;
    }

    footer ul li {
        justify-content: center;
    }

    .footer-brand-section {
        align-items: center;
    }

    footer .d-flex.gap-1 {
        justify-content: center;
    }

    footer img {
        margin: 0 auto;
    }

    /* --- ADMIN & TABLES --- */
    /* Hide specific admin columns on mobile if necessary, or scroll */
    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Page Specific Adjustments */
    #products-section {
        padding-top: 2rem !important;
        padding-bottom: 3rem !important;
    }

    /* --- CHECKOUT PAGE --- */
    .checkout-container>div {
        grid-template-columns: 1fr !important;
    }

    /* Make Summary appear valid in flow */
    .checkout-container .card {
        width: 100% !important;
    }

    /* Improve Credit Card Inputs */
    #creditCardSection {
        padding-bottom: 1rem;
    }

    /* Stack date/cvc if 3 cols is too tight (on very small screens) */
    @media (max-width: 380px) {
        #creditCardSection>div[style*="grid-template-columns"] {
            grid-template-columns: 1fr 1fr !important;
        }

        #creditCardSection .form-group:last-child {
            grid-column: span 2;
        }
    }

    /* --- PRODUCT PAGE FILTERS --- */
    /* By default hide sidebar on mobile, we will toggle it via JS/CSS class */
    .filter-sidebar {
        display: none !important;
        /* Force hidden unless active */
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100vh;
        background: var(--bg-primary);
        z-index: 10000;
        overflow-y: auto;
        padding: 1.5rem;
    }

    .filter-sidebar.active {
        display: block !important;
        animation: fadeIn 0.3s ease;
    }

    .filter-toggle-btn {
        display: flex;
        width: 100%;
        padding: 0.8rem;
        background: var(--bg-secondary);
        border: 1px solid var(--border-color);
        color: var(--text-primary);
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
        border-radius: var(--radius-sm);
        margin-bottom: 1rem;
        font-weight: 600;
    }

    /* Close button for filter sidebar */
    .filter-close-btn {
        display: block;
        width: 100%;
        padding: 1rem;
        background: var(--accent-primary);
        color: #000;
        font-weight: 800;
        text-align: center;
        border: none;
        margin-bottom: 2rem;
        border-radius: var(--radius-sm);
    }

    /* --- ADMIN PANEL --- */
    .admin-layout {
        flex-direction: column;
    }

    .admin-sidebar {
        width: 100% !important;
        height: auto !important;
        position: relative !important;
        border-right: none !important;
        border-bottom: 1px solid var(--border-color);
        padding: 1rem !important;
        display: none;
        /* Hidden by default, toggled via JS */
    }

    .admin-sidebar.active {
        display: flex;
    }

    .admin-main {
        margin-left: 0 !important;
        width: 100% !important;
        padding: 1rem !important;
    }

    .admin-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 1rem;
    }

    .admin-mobile-head {
        display: flex !important;
        /* OVERRIDE INLINE STYLE */
        align-items: center;
        justify-content: space-between;
        padding: 1rem;
        background: var(--bg-card);
        border-bottom: 1px solid var(--border-color);
        position: sticky;
        top: 0;
        z-index: 1001;
    }

    .admin-sidebar {
        width: 100% !important;
        height: 100vh !important;
        position: fixed !important;
        top: 60px;
        /* Below active header approx */
        left: 0;
        border-right: none !important;
        background: var(--bg-primary);
        z-index: 1000;
        padding: 1rem !important;
        display: none;
        overflow-y: auto;
    }

    .admin-sidebar.active {
        display: flex;
        animation: fadeIn 0.2s ease;
    }

    .dataTables_wrapper .row {
        flex-direction: column;
    }

    /* Admin Dashboard Mobile Grid Overrides */
    .admin-stats-grid,
    .admin-charts-grid,
    .admin-analytics-grid {
        display: flex !important;
        flex-direction: column !important;
        /* Stack everything vertically */
        grid-template-columns: 1fr !important;
        gap: 1rem !important;
    }

    /* Fix charts height on mobile */
    .admin-charts-grid canvas,
    .admin-analytics-grid canvas {
        max-height: 250px !important;
    }

    /* --- MOBILE TOOLBAR / FILTER BAR STACKING --- */
    .card.mb-4.p-3.d-flex.justify-between.align-center.flex-wrap.gap-2 {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 1rem !important;
        text-align: center;
    }

    .d-flex.align-center.gap-2 {
        justify-content: space-between !important;
        width: 100% !important;
    }

    .d-flex.align-center.gap-1 select.form-control {
        width: 100% !important;
        min-width: 120px;
    }

    /* --- SMALL MOBILE / COMPACT VIEW (< 525px) --- */
    @media (max-width: 525px) {
        .product-grid {
            grid-template-columns: 1fr 1fr !important;
            gap: 0.5rem !important;
            padding: 0 !important;
            margin: 0 auto !important;
            width: 100% !important;
            justify-content: center !important;
        }

        .product-card {
            margin-bottom: 0 !important;
            width: 100% !important;
            min-width: 0 !important;
            padding: 0 !important;
            /* Full bleed image */
            overflow: hidden !important;
        }

        .product-image-container {
            width: 100% !important;
            aspect-ratio: auto !important;
            /* Allow natural shape */
            height: auto !important;
            margin-bottom: 0 !important;
        }

        .product-details {
            padding: 0.5rem !important;
        }

        .product-card h3 {
            font-size: 0.8rem !important;
            white-space: normal !important;
            display: -webkit-box !important;
            -webkit-line-clamp: 2;
            line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden !important;
            height: 2.4em !important;
            line-height: 1.2 !important;
            margin-bottom: 0.25rem !important;
        }

        .price-tag {
            font-size: 0.9rem !important;
        }

        .product-actions {
            flex-direction: column !important;
            gap: 0.25rem !important;
            margin-top: auto !important;
        }

        .product-actions .btn {
            font-size: 0.7rem !important;
            padding: 0.35rem 0 !important;
            width: 100% !important;
        }

        footer ul li,
        footer .d-flex.gap-1 {
            justify-content: center !important;
        }
    }

    /* --- EMERGENCY FALLBACK FOR ULTRA NARROW SCREENS (< 320px) --- */
    /* Captures the scenario in user's screenshot (264px window) */
    @media (max-width: 319px) {
        .product-grid {
            grid-template-columns: 1fr !important;
        }

        .product-card {
            /* Make it look like a list item instead of a huge card */
            flex-direction: row !important;
            align-items: center !important;
            gap: 0.5rem;
        }

        .product-image-container {
            width: 80px !important;
            min-width: 80px !important;
            height: 120px !important;
            margin-bottom: 0 !important;
        }

        .product-card h3 {
            -webkit-line-clamp: 3;
            line-clamp: 3;
            height: auto !important;
        }

        .product-actions {
            display: none !important;
            /* Hide buttons on super tiny screens, user clicks card */
        }
    }
}