/* ===== MOBILE OPTIMIZATIONS ===== */
/* Additional mobile-specific enhancements */

/* Ensure no horizontal overflow */
html,
body {
    overflow-x: hidden !important;
    max-width: 100vw;
}

.main-content {
    overflow-x: hidden !important;
}

/* ===== MOBILE TABLES ===== */
@media (max-width: 768px) {

    /* Make all tables scrollable horizontally */
    .table-responsive {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        margin-bottom: 1rem;
    }

    table:not(.responsive-mobile) {
        min-width: 600px;
        /* Prevent table collapse */
        font-size: 0.85rem;
    }

    table th,
    table td {
        padding: 8px !important;
        white-space: nowrap;
    }

    /* Smaller buttons in tables */
    table .btn-group-sm .btn {
        padding: 4px 8px;
        font-size: 0.75rem;
    }

    table .btn-group-sm i {
        font-size: 0.85rem;
    }
}

/* ===== MOBILE FORMS ===== */
@media (max-width: 576px) {
    .form-grid {
        grid-template-columns: 1fr !important;
    }

    .form-group {
        margin-bottom: 1rem;
    }

    input,
    select,
    textarea {
        font-size: 16px !important;
        /* Prevent zoom on iOS */
        padding: 12px !important;
    }

    label {
        font-size: 0.9rem;
        margin-bottom: 0.5rem;
    }

    /* Stack form buttons */
    .form-actions {
        flex-direction: column;
        gap: 10px;
    }

    .form-actions .btn {
        width: 100%;
    }
}

/* ===== MOBILE BUTTONS & TOUCH TARGETS ===== */
@media (max-width: 768px) {

    /* Minimum touch target size: 44x44px (Apple HIG) */
    .btn {
        min-height: 44px;
        padding: 12px 20px;
    }

    .btn-sm {
        min-height: 38px;
        padding: 8px 16px;
    }

    .btn-icon {
        min-width: 44px;
        min-height: 44px;
        padding: 10px;
    }

    /* Stack button groups on mobile */
    .btn-group {
        flex-direction: column;
        width: 100%;
    }

    .btn-group .btn {
        width: 100%;
        border-radius: 8px !important;
        margin-bottom: 8px;
    }

    .top-controls {
        flex-wrap: wrap;
        width: 100%;
    }

    .top-controls .btn {
        flex: 1 1 auto;
        min-width: 120px;
    }
}

/* ===== MOBILE MODALS ===== */
@media (max-width: 576px) {
    .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100% - 1rem);
    }

    .modal-lg,
    .modal-xl {
        max-width: calc(100% - 1rem);
    }

    .modal-body {
        padding: 1rem;
    }

    .modal-header {
        padding: 1rem;
    }

    .modal-footer {
        padding: 1rem;
        flex-direction: column;
        gap: 10px;
    }

    .modal-footer .btn {
        width: 100%;
    }
}

/* ===== MOBILE CARDS ===== */
@media (max-width: 576px) {
    .card {
        margin-bottom: 1rem;
    }

    .card-body {
        padding: 1rem;
    }

    .kpi-card {
        padding: 15px;
    }

    .kpi-icon {
        width: 50px;
        height: 50px;
        font-size: 1.5rem;
    }

    .kpi-value {
        font-size: 1.8rem;
    }
}

/* ===== MOBILE SEARCH BOXES ===== */
@media (max-width: 576px) {
    .search-box {
        width: 100%;
        margin-bottom: 1rem;
    }

    .search-box input {
        width: 100%;
        font-size: 16px;
        /* Prevent zoom */
    }
}

/* ===== MOBILE PAGE TITLES ===== */
@media (max-width: 576px) {
    .page-title h1 {
        font-size: 1.5rem;
    }

    .page-title h1 i {
        font-size: 1.5rem;
    }

    .subtitle {
        font-size: 0.85rem;
    }
}

/* ===== MOBILE SIDEBAR OVERLAY ===== */
.sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 4999;
    display: none;
    transition: opacity 0.3s ease;
}

.sidebar-overlay.active {
    display: block;
}

@media (min-width: 992px) {
    .sidebar-overlay {
        display: none !important;
    }
}

/* ===== MOBILE PRODUCT GRID (Portal Cliente) ===== */
@media (max-width: 768px) {
    #product-grid {
        grid-template-columns: 1fr !important;
    }

    .product-card {
        max-width: 100%;
    }

    .product-card img {
        max-height: 150px;
    }
}

/* ===== MOBILE CART ===== */
@media (max-width: 576px) {
    .cart-item {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }

    .cart-item-actions {
        width: 100%;
        justify-content: space-between;
    }

    .cart-summary {
        position: static;
        margin-top: 1rem;
    }
}

/* ===== MOBILE ADMIN PANEL ===== */
@media (max-width: 768px) {
    #admin-clientes-container .table {
        font-size: 0.8rem;
    }

    #admin-clientes-container .btn-group {
        flex-direction: row;
        /* Keep horizontal for icon buttons */
        gap: 4px;
    }

    #admin-clientes-container .btn-group .btn {
        width: auto;
        padding: 6px 10px;
    }
}

/* ===== MOBILE NAVIGATION FIX ===== */
@media (max-width: 991px) {

    /* Ensure sidebar doesn't push content */
    body {
        position: relative;
    }

    .sidebar {
        position: fixed;
        z-index: 99999 !important;
    }

    .main-content {
        margin-left: 0 !important;
        width: 100%;
    }

    /* Prevent body scroll when sidebar is open */
    body.sidebar-open {
        overflow: hidden;
    }
}

/* ===== MOBILE TEXT SIZES ===== */
@media (max-width: 576px) {
    body {
        font-size: 14px;
    }

    h1 {
        font-size: 1.75rem;
    }

    h2 {
        font-size: 1.5rem;
    }

    h3 {
        font-size: 1.25rem;
    }

    h4 {
        font-size: 1.1rem;
    }

    h5 {
        font-size: 1rem;
    }

    h6 {
        font-size: 0.9rem;
    }
}

/* ===== MOBILE SPACING ===== */
@media (max-width: 576px) {
    .container-fluid {
        padding-left: 1rem;
        padding-right: 1rem;
    }

    .row {
        margin-left: -0.5rem;
        margin-right: -0.5rem;
    }

    .col,
    [class*="col-"] {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }

    .py-4 {
        padding-top: 1.5rem !important;
        padding-bottom: 1.5rem !important;
    }

    .mb-4 {
        margin-bottom: 1rem !important;
    }
}

/* ===== PREVENT ZOOM ON DOUBLE TAP (iOS) ===== */
button,
a {
    touch-action: manipulation;
}

/* ===== IMPROVE SCROLLING PERFORMANCE ===== */
.table-responsive,
.sidebar-menu,
.modal-body {
    -webkit-overflow-scrolling: touch;
}

/* ===== MOBILE LANDING SCREEN ===== */
@media (max-width: 576px) {
    #landing-screen h1 {
        font-size: 2.2rem !important;
        /* Slightly smaller for general mobile */
        line-height: 1.2;
        display: flex;
        flex-direction: column;
        /* Force wrap if needed */
        align-items: center;
        width: 100%;
        padding: 0 15px;
        word-break: break-word;
    }

    #landing-screen .text-secondary {
        color: var(--success) !important;
        text-shadow: 0 0 15px rgba(76, 201, 240, 0.4);
        font-size: 0.9em;
        /* Scale down the Portal part slightly */
    }

    @media (max-width: 420px) {

        /* Fits S20 Ultra (412px) and smaller */
        #landing-screen h1 {
            font-size: 1.8rem !important;
        }
    }

    #landing-screen .subtitle {
        font-size: 0.95rem;
        padding: 0 20px;
    }

    #landing-screen .auth-options {
        flex-direction: column;
        gap: 15px;
        width: 100%;
        max-width: 320px;
    }

    #landing-screen .auth-card {
        width: 100%;
        max-width: 100%;
    }
}

/* ===== MOBILE PRODUCT CARDS (CLIENT PORTAL) ===== */
@media (max-width: 768px) {
    .product-grid-mobile {
        display: flex;
        flex-direction: column;
        gap: 12px;
        padding: 10px;
    }

    .product-card-mobile {
        background: white !important;
        border-radius: 12px !important;
        box-shadow: 0 2px 5px rgba(0,0,0,0.05) !important;
        margin-bottom: 0 !important;
        padding: 12px !important;
        display: flex !important;
        flex-direction: row !important;
        align-items: flex-start !important;
        gap: 15px !important;
        border: 1px solid #f1f5f9 !important;
        height: auto;
    }

    .product-card-mobile .product-image {
        width: 90px !important;
        height: 90px !important;
        object-fit: contain !important;
        background: #fff !important;
        border-radius: 8px !important;
        padding: 4px;
        flex-shrink: 0;
        border: 1px solid #e2e8f0;
        align-self: center;
    }

    .product-card-mobile .product-info {
        flex: 1 !important;
        display: flex !important;
        flex-direction: column !important;
        gap: 4px !important;
        min-width: 0;
    }

    .product-card-mobile .product-name {
        font-weight: 700 !important;
        font-size: 0.95rem !important;
        line-height: 1.2 !important;
        color: #0f172a !important;
        margin-bottom: 2px;
        width: 100%;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .product-card-mobile .product-code {
        font-size: 0.75rem !important;
        color: #64748b !important;
        background: #f1f5f9;
        padding: 2px 8px;
        border-radius: 4px;
        display: inline-block;
        width: fit-content;
    }

    .product-card-mobile .product-price {
        font-size: 1.1rem !important;
        font-weight: 800 !important;
        color: var(--primary) !important;
        margin: 4px 0;
    }

    /* Improved Quantity Selector */
    .product-card-mobile .quantity-selector {
        display: flex !important;
        align-items: center !important;
        gap: 8px !important;
        margin-bottom: 8px;
        width: fit-content;
    }

    .product-card-mobile .quantity-selector button {
        width: 32px !important;
        height: 32px !important;
        min-width: 32px !important;
        padding: 0 !important;
        border-radius: 8px !important;
        background: #f1f5f9 !important;
        border: 1px solid #e2e8f0 !important;
        color: #334155 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        font-size: 1rem !important;
        box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    }

    .product-card-mobile .quantity-selector input {
        width: 40px !important;
        height: 32px !important;
        text-align: center !important;
        font-size: 1rem !important;
        font-weight: 700 !important;
        border: none !important;
        background: transparent !important;
        padding: 0 !important;
        color: #0f172a !important;
    }

    .product-card-mobile .add-to-cart-btn {
        height: 36px !important;
        border-radius: 8px !important;
        background: var(--primary) !important;
        color: white !important;
        font-size: 0.9rem !important;
        border: none !important;
        padding: 0 !important;
        width: 100% !important;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        font-weight: 600;
    }

    /* Banner Compact */
    .friparts-banner {
        max-height: 100px !important;
        margin-bottom: 1rem !important;
    }
    
    #portal-search {
        padding: 10px 15px !important;
        font-size: 14px !important;
    }
}
