/* ==========================================================================
   GLOBAL MOBILE OPTIMIZATIONS
   Applies to all modules (Inventory, Clients, Employees, etc.)
   ========================================================================== */

/* --- MOBILE ONLY STYLES (< 768px) --- */
@media (max-width: 768px) {

    /* 1. GENERAL LAYOUT ADJUSTMENTS */
    /* Hide scrollbars for NATIVE APP feel */
    *::-webkit-scrollbar {
        display: none !important;
    }

    * {
        -ms-overflow-style: none !important;
        scrollbar-width: none !important;
    }

    .content-area {
        padding: 0 0 80px 0 !important;
        /* Edge to Edge */
    }

    .main-content {
        height: 100vh;
        overflow: hidden;
    }

    /* 2. MODALS MOBILE OPTIMIZATION */
    .modal {
        align-items: flex-end !important;
        padding: 0 !important;
    }

    .modal-content {
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        border-radius: 20px 20px 0 0 !important;
        max-height: 95vh !important;
        display: flex;
        flex-direction: column;
        animation: slideUp 0.3s ease-out;
    }

    .modal-content.large,
    .modal-content.extra-large {
        width: 100% !important;
        height: 100% !important;
        max-height: 100% !important;
        border-radius: 0 !important;
    }

    .modal-body {
        overflow-y: auto;
        padding: 16px !important;
        flex: 1;
    }

    .modal-header {
        padding: 16px !important;
        border-bottom: 1px solid var(--border-color);
        position: sticky;
        top: 0;
        background: var(--card-bg-color);
        z-index: 10;
        border-radius: 20px 20px 0 0;
    }

    .modal-footer {
        padding: 16px !important;
        background: var(--card-bg-color);
        border-top: 1px solid var(--border-color);
        position: sticky;
        bottom: 0;
        z-index: 10;
        flex-direction: column-reverse;
        gap: 10px;
    }

    .modal-footer button {
        width: 100%;
        margin: 0 !important;
        height: 48px;
        font-size: 1rem;
    }

    /* 2.1 MODAL TABS SCROLLABLE */
    .modal-tabs {
        display: flex !important;
        flex-wrap: nowrap !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        gap: 8px;
        padding-bottom: 8px;
        margin-bottom: 15px;
        border-bottom: 1px solid var(--border-color);
    }

    .modal-tab {
        flex: 0 0 auto !important;
        /* Don't shrink */
        white-space: nowrap;
        padding: 8px 16px;
    }

    /* Hide scrollbar for tabs */
    .modal-tabs::-webkit-scrollbar {
        height: 4px;
    }

    .modal-tabs::-webkit-scrollbar-thumb {
        background: #ccc;
        border-radius: 4px;
    }

    /* 3. GRIDS & FORMS */
    .form-grid-2,
    .form-grid-3,
    .dashboard-grid,
    .row,
    .form-row {
        /* Force single column for most grids, but be careful with .row generic */
        /* Better to rely on col-md-* stacking naturally, but enforce gap */
    }

    .form-row {
        flex-direction: column;
    }

    .form-group {
        margin-bottom: 16px;
        width: 100%;
    }

    input,
    select,
    textarea,
    button {
        font-size: 16px !important;
        min-height: 44px;
    }

    /* 4. DATA TABLES (Card View Transformation) */
    table,
    thead,
    tbody,
    th,
    td,
    tr {
        display: block;
    }

    thead tr {
        position: absolute;
        top: -9999px;
        left: -9999px;
    }

    tbody tr {
        margin-bottom: 16px;
        background: var(--card-bg-color);
        border-radius: 12px;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
        border: 1px solid var(--border-color);
        padding: 12px;
        position: relative;
    }

    tbody tr td {
        border: none;
        position: relative;
        padding-left: 45% !important;
        text-align: right;
        min-height: 35px;
        display: flex;
        align-items: center;
        justify-content: flex-end;
        border-bottom: 1px solid rgba(0, 0, 0, 0.03);
        font-size: 0.95rem;
    }

    tbody tr td:last-child {
        border-bottom: none;
        justify-content: center;
        padding-left: 0 !important;
        margin-top: 12px;
        padding-top: 12px;
        border-top: 1px dashed var(--border-color);
    }

    tbody tr td:before {
        position: absolute;
        left: 0;
        width: 40%;
        white-space: nowrap;
        text-align: left;
        font-weight: 600;
        color: var(--text-muted);
        content: attr(data-label);
        font-size: 0.85rem;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    td.no-label:before {
        content: none;
    }

    /* 5. TOP BAR & NAVIGATION */
    .top-bar {
        padding: 0 12px;
        height: 60px;
    }

    #page-title {
        font-size: 1.1rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 200px;
    }

    .top-bar-right .btn-icon {
        width: 40px;
        height: 40px;
    }

    /* 6. CLIENTS MODULE SPECIFIC - V5 "NO OVERLAP & CLEAN" */

    /* --- A. Layout & Container --- */
    #clients-submodule-content {
        padding: 0 4px 80px 4px;
        /* Minimal padding */
        background: #f8fafc;
    }

    /* --- B. Tabs (Fixing the Gray Background) --- */
    /* Target ALL containers involved in tabs to remove backgrounds */
    #clients-submodule-content .card-header,
    #clients-submodule-content .nav-pills,
    #clients-submodule-content .nav-item,
    #clients-submodule-content .card {
        background: transparent !important;
        background-color: transparent !important;
        box-shadow: none !important;
        border: none !important;
        padding: 0 !important;
        margin: 0 !important;
    }

    .submodule-tabs {
        display: flex;
        gap: 12px;
        padding: 4px 0 20px 0;
        /* Raised Up */
        margin: 0;
        width: 100%;
        background: transparent !important;
    }

    .submodule-tabs::-webkit-scrollbar {
        display: none;
    }

    .submodule-tab {
        flex: 1;
        /* 50/50 Split */
        background: white !important;
        color: #64748b;
        border: 1px solid #e2e8f0;
        padding: 16px 0;
        /* Thicker/Taller */
        border-radius: 16px;
        font-size: 1.05rem;
        font-weight: 800;
        white-space: nowrap;
        text-align: center;
        justify-content: center;
        box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
    }

    .submodule-tab.active {
        background: var(--primary-color) !important;
        color: white;
        border-color: var(--primary-color);
        box-shadow: 0 4px 12px rgba(var(--primary-rgb), 0.3);
    }

    /* --- C. Stats Dashboard --- */
    #clients-submodule-content .row.animate__fadeIn {
        display: flex !important;
        flex-wrap: nowrap !important;
        overflow-x: auto !important;
        gap: 16px;
        padding-bottom: 20px;
        margin: 0 !important;
        width: 100%;
        scroll-snap-type: x mandatory;
        padding-right: 50px;
        /* Allow peeking */
        scrollbar-width: none;
    }

    #clients-submodule-content .row.animate__fadeIn::-webkit-scrollbar {
        display: none;
    }

    #clients-submodule-content .col-md-3 {
        width: 85% !important;
        flex: 0 0 85% !important;
        max-width: 85% !important;
        padding: 0 !important;
        scroll-snap-align: start;
    }

    .cli-stats-card {
        display: flex !important;
        flex-direction: row !important;
        align-items: center !important;
        justify-content: space-between !important;
        padding: 16px 20px !important;
        height: auto !important;
        min-height: 90px;
        background: white;
        border-radius: 20px !important;
        border: none !important;
        box-shadow: 0 6px 15px rgba(0, 0, 0, 0.04) !important;
    }

    .cli-stats-card>div:first-child {
        text-align: left;
        flex: 1;
    }

    .cli-stats-card h2 {
        font-size: 1.8rem !important;
        /* Restore Big Size */
        margin: 0 !important;
        line-height: 1;
        font-weight: 800;
        color: #0f172a;
        letter-spacing: -0.5px;
    }

    .cli-stats-card p {
        font-size: 0.8rem !important;
        color: #64748b;
        font-weight: 700;
        text-transform: uppercase;
        margin-bottom: 5px !important;
        letter-spacing: 1px;
    }

    .cli-icon-circle {
        position: static !important;
        width: 54px;
        height: 54px;
        /* Big Icon */
        min-width: 54px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;
        font-size: 1.6rem;
        margin-left: 15px;
        opacity: 1 !important;
    }

    /* --- D. Header Tools --- */
    /* Restore Header Visibility cleanly */
    #clients-submodule-content>.card>.card-header {
        display: flex !important;
        padding-bottom: 15px !important;
        padding-top: 10px !important;
    }

    #clients-submodule-content .input-group {
        flex: 1;
        background: white !important;
        border-radius: 60px;
        /* Pill */
        height: 60px;
        /* Taller Search Bar */
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
        border: 1px solid #e2e8f0;
        overflow: hidden;
        align-items: center;
    }

    #clients-submodule-content .form-control,
    #clients-submodule-content .input-group-text {
        background: transparent !important;
        border: none !important;
        height: 100%;
        font-size: 1.1rem;
        /* Larger Text */
        color: #334155;
    }

    #clients-submodule-content .input-group-text i {
        font-size: 1.2rem;
        margin-left: 8px;
    }

    #new-client-btn {
        border: none !important;
    }

    #new-client-btn i {
        font-size: 1rem;
        color: white;
        margin: 0 !important;
    }

    /* --- E. Lista de Clientes (FLEXBOX FIX) --- */
    #clients-main-table tbody tr {
        display: flex;
        align-items: stretch;
        /* Full Height Children */
        justify-content: space-between;
        background: white;
        margin-bottom: 12px;
        padding: 0 !important;
        /* Zero Padding for Edge-to-Edge */
        border-radius: 16px;
        overflow: hidden;
        /* Clip Avatar */
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.02);
        border-left: 5px solid #e2e8f0;
    }

    #clients-main-table tbody tr[data-has-debt="true"] {
        border-left-color: #ef4444;
    }

    #clients-main-table tbody tr[data-has-debt="false"] {
        border-left-color: #10b981;
    }

    /* --- F. Column Alignment & Reset --- */

    /* Reset Generic TD styles (Remove padding-left: 45% and labels) */
    #clients-main-table tbody tr td {
        padding: 0 !important;
        border: none !important;
        min-height: auto !important;
        position: static !important;
        text-align: left !important;
        background: transparent !important;
        box-shadow: none !important;
        margin: 0 !important;
    }

    #clients-main-table tbody tr td:before {
        display: none !important;
        content: none !important;
    }

    /* Hide unused columns for Clean Mobile UI */
    #clients-main-table td:nth-child(1),
    #clients-main-table td:nth-child(3),
    #clients-main-table td:nth-child(4),
    #clients-main-table td:nth-child(6) {
        display: none !important;
    }

    /* Name Column (2) - Flex Grow */
    #clients-main-table tbody tr td:nth-child(2) {
        flex: 1;
        display: flex !important;
        align-items: center;
        padding-left: 0px !important;
    }

    /* Balance Column (5) - Right Align */
    #clients-main-table td:nth-child(5) {
        text-align: right !important;
        justify-content: center;
        display: flex !important;
        flex-direction: column;
        align-items: flex-end;
        padding-right: 12px !important;
    }

    /* 1. Avatar (Circle Token) */
    #clients-main-table td:nth-child(2) .cli-avatar {
        width: 48px;
        min-width: 48px;
        height: 48px !important;
        border-radius: 50%;
        /* Rounded */
        font-size: 1.1rem;
        background-color: #f1f5f9 !important;
        color: #475569 !important;
        margin: 0 10px 0 12px !important;
        /* Spacing */
        display: flex;
        align-items: center;
        justify-content: center;
        align-self: center;
        /* Center Vertically */
    }

    /* Text Truncation & PADDING */
    #clients-main-table td:nth-child(2)>div:last-child {
        min-width: 0;
        flex: 1;
        padding: 12px 10px;
        /* Padding for Text */
    }

    #clients-main-table td:nth-child(2) h6,
    #clients-main-table td:nth-child(2) small,
    #clients-main-table td:nth-child(2) .text-muted {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        display: block;
        max-width: 100%;
    }

    /* Duplicates removed */

    /* 7. ANIMATIONS */
    @keyframes slideUp {
        from {
            transform: translateY(100%);
        }

        to {
            transform: translateY(0);
        }
    }

    /* 8. PREMIUM MODAL OPTIMIZATIONS (Overrides Generic) */

    /* A. Header: Clean White & Minimal */
    .modal-header {
        background: white !important;
        border-bottom: 1px solid #f1f5f9 !important;
        color: #1e293b !important;
        padding: 20px 20px 10px 20px !important;
        border-radius: 24px 24px 0 0 !important;
    }

    .modal-header h2 {
        font-size: 1.25rem !important;
        font-weight: 800 !important;
        color: #0f172a !important;
        letter-spacing: -0.5px;
        margin: 0;
    }

    .modal-header .close-btn {
        background: #f1f5f9;
        color: #64748b;
        border-radius: 50%;
        width: 36px;
        height: 36px;
        display: flex;
        align-items: center;
        justify-content: center;
        opacity: 1;
        font-weight: normal;
        font-size: 1.5rem;
        line-height: 1;
        margin-left: 15px;
    }

    /* B. Tabs: Segmented Control (High-End) */
    .modal-tabs {
        background: #f1f5f9;
        padding: 4px;
        margin: 10px 0 20px 0;
        border-radius: 14px;
        display: flex;
        gap: 0;
        border: none !important;
        overflow-x: auto;
    }

    /* Force pills inside modal tabs */
    .modal-tab {
        flex: none;
        /* Let them scroll if many */
        background: transparent !important;
        border: none !important;
        color: #64748b !important;
        font-size: 0.9rem !important;
        font-weight: 600 !important;
        padding: 8px 16px !important;
        border-radius: 10px !important;
        transition: all 0.2s;
        box-shadow: none !important;
    }

    .modal-tab.active {
        background: white !important;
        color: var(--primary-color) !important;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05) !important;
        font-weight: 700 !important;
    }

    /* Remove icons from tabs to save space if needed, or keep them small */
    .modal-tab i {
        font-size: 0.8em;
        margin-right: 4px;
    }

    /* C. Form Fields: Spacious & Soft */
    .modal-body .form-group label {
        font-size: 0.8rem;
        font-weight: 700;
        color: #64748b;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        margin-bottom: 8px;
    }

    .modal-body .form-control,
    .modal-body select.form-control {
        height: 52px !important;
        border-radius: 14px !important;
        border: 1px solid #e2e8f0 !important;
        background-color: #f8fafc !important;
        font-size: 1rem !important;
        color: #1e293b !important;
        box-shadow: none !important;
        padding-left: 15px;
    }

    .modal-body .form-control:focus {
        border-color: var(--primary-color) !important;
        background-color: white !important;
        box-shadow: 0 0 0 3px rgba(var(--primary-rgb), 0.1) !important;
    }

    /* D. Footer: Fixed Bottom Actions */
    .modal-footer {
        background: white !important;
        border-top: 1px solid #f1f5f9 !important;
        padding: 16px 20px 30px 20px !important;
        /* Extra padding for iPhone X home bar */
    }

    .modal-footer .btn {
        height: 52px;
        border-radius: 14px;
        font-weight: 700;
        font-size: 1rem;
        text-transform: none;
        letter-spacing: 0;
    }

    .modal-footer .btn-secondary {
        background: #f1f5f9;
        border: none;
        color: #64748b;
    }

    .modal-footer .btn-primary {
        background: var(--primary-color);
        box-shadow: 0 4px 12px rgba(var(--primary-rgb), 0.3);
    }

    /* --- CXC Mobile Table Transformation --- */
    #cxc-table-container table,
    #cxc-table-container thead,
    #cxc-table-container tbody,
    #cxc-table-container th,
    #cxc-table-container td,
    #cxc-table-container tr {
        display: block;
    }

    #cxc-table-container thead tr {
        position: absolute;
        top: -9999px;
        left: -9999px;
    }

    #cxc-table-container tr {
        border: 1px solid #f1f5f9;
        border-radius: 16px;
        margin-bottom: 12px;
        padding: 16px;
        background: white;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.02);
        position: relative;
    }

    #cxc-table-container td {
        border: none;
        border-bottom: 1px solid #f8fafc;
        position: relative;
        padding-left: 0;
        padding-top: 8px;
        padding-bottom: 8px;
        text-align: left;
    }

    #cxc-table-container td:last-child {
        border-bottom: none;
    }

    /* Name Highlight */
    #cxc-table-container td:nth-child(2) {
        font-size: 1.1rem;
        font-weight: 800;
        color: #1e293b;
        padding-bottom: 4px;
        border-bottom: none;
    }

    /* Debt Highlight */
    #cxc-table-container td:nth-child(5) {
        font-size: 1.2rem;
        font-weight: 700;
        color: #ef4444 !important;
        text-align: right;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background: #fef2f2;
        padding: 12px;
        border-radius: 12px;
        margin-top: 8px;
        margin-bottom: 12px;
    }

    #cxc-table-container td:nth-child(5):before {
        content: "Pendiente";
        font-size: 0.8rem;
        color: #ef4444;
        font-weight: 600;
        text-transform: uppercase;
    }

    /* Hide ID */
    #cxc-table-container td:nth-child(1) {
        display: none;
    }

    /* Actions */
    #cxc-table-container td:nth-child(6) {
        display: flex;
        gap: 8px;
    }

    #cxc-table-container .btn {
        flex: 1;
        border-radius: 8px;
        height: 40px;
        font-size: 0.9rem;
        margin: 0;
    }

    @media (max-width: 768px) {

        /* Floating Action Button (Quotations) - Class Enforced */
        #new-quote-btn.mobile-fab-fixed {
            position: fixed !important;
            bottom: 85px !important;
            right: 20px !important;
            left: auto !important;
            transform: none !important;
            width: 60px !important;
            min-width: 0 !important;
            max-width: 60px !important;
            height: 60px !important;
            border-radius: 50% !important;
            padding: 0 !important;
            display: flex !important;
            justify-content: center !important;
            align-items: center !important;
            box-shadow: 0 4px 15px rgba(var(--primary-rgb), 0.4) !important;
            z-index: 9999 !important;
            background-color: var(--primary-color) !important;
            animation: fab-pulse 2s infinite ease-in-out !important;
        }

        #new-quote-btn.mobile-fab-fixed i {
            margin: 0 !important;
            font-size: 1.8rem !important;
            line-height: 1 !important;
        }

        #new-quote-btn.mobile-fab-fixed span {
            display: none !important;
        }

        @keyframes fab-pulse {
            0% {
                transform: scale(1);
                box-shadow: 0 4px 15px rgba(var(--primary-rgb), 0.4);
            }

            50% {
                transform: scale(1.15);
                box-shadow: 0 8px 30px rgba(var(--primary-rgb), 0.6);
            }

            100% {
                transform: scale(1);
                box-shadow: 0 4px 15px rgba(var(--primary-rgb), 0.4);
            }
        }
    }
}