/* ============================================
   LAYOUT FIXES - CENTERED & BALANCED CONTENT
   Ensures all pages use consistent, centered layouts
   ============================================ */

/* ============================================
   MODAL & DROPDOWN DEPTH SYSTEM
   Ensures overlays are always on top and not clipped
   ============================================ */
.dropdown-menu {
    z-index: 1060 !important;
}

.modal {
    z-index: 1070 !important;
}

.modal-backdrop {
    z-index: 1065 !important;
}

/* Ensure table-responsive doesn't clip dropdown items when rows are few 
   BUT keep horizontal scroll for mobile! */
.table-responsive {
    overflow-x: auto !important;
    overflow-y: visible !important;
    -webkit-overflow-scrolling: touch;
}

.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 20px;
}

.nxl-content {
    flex: 1;
    width: 100%;
    padding: 0;
    position: relative;
}

/* ============================================
   CONTENT CENTERING & MAX-WIDTH
   ============================================ */
.layout {
    display: flex;
    min-height: 100vh;
    width: 100%;
}

.sidebar {
    width: 250px;
    flex-shrink: 0;
    transition: width 0.3s ease;
}

.sidebar.collapsed {
    width: 70px;
}

/* Hide text and non-essential elements when collapsed */
.sidebar.collapsed .nxl-mtext,
.sidebar.collapsed .nxl-caption label,
.sidebar.collapsed .group-icon {
    display: none !important;
}

/* Center icons when collapsed */
.sidebar.collapsed .nxl-micon {
    margin-right: 0 !important;
    justify-content: center !important;
}

/* Adjust padding for link when collapsed */
.sidebar.collapsed .nxl-link {
    padding-left: 0 !important;
    padding-right: 0 !important;
    justify-content: center !important;
}

/* Handle logo switching or hiding when collapsed */
.sidebar.collapsed .b-brand img.logo-lg {
    display: none !important;
}

/* If logo-sm is hidden by default, show it */
.sidebar.collapsed .b-brand img.logo-sm {
    display: block !important;
    margin: 0 auto !important;
}

/* When expanded, make sure logo-sm is hidden if needed */
.sidebar:not(.collapsed) .b-brand img.logo-sm {
    display: none !important;
}

.nxl-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    transition: margin-left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    margin-left: 270px !important; /* Unified sidebar width */
    margin-top: 64px !important; /* Professional space for fixed header */
}

/* Response for collapsed desktop sidebar */
.nxl-navigation.collapsed + .nxl-container {
    margin-left: 80px;
}

/* Adjust header to match container margin */
.nxl-header {
    transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    left: 270px !important;
    width: calc(100% - 270px) !important;
    z-index: 1000 !important; /* Ensure content-level elements are above it if needed, but usually below sidebar */
}

.nxl-navigation.collapsed ~ .nxl-container .nxl-header,
.nxl-navigation.collapsed + .nxl-container .nxl-header {
    left: 80px !important;
    width: calc(100% - 80px) !important;
}

.nxl-navigation.collapsed + .nxl-container {
    margin-left: 80px !important;
}

.main-content {
    width: 100%;
    max-width: 1600px; /* Executive max-width for focus */
    margin-left: auto !important;
    margin-right: auto !important;
    padding: 2rem;
    position: relative;
}

/* Ensure content area uses proper grid/flex */
.main-content .row {
    margin-left: 0;
    margin-right: 0;
}

.main-content .row > [class*="col-"] {
    padding-left: 0.75rem;
    padding-right: 0.75rem;
}

/* ============================================
   RESPONSIVE CONTAINER SYSTEM
   ============================================ */
@media (min-width: 1200px) {
    .main-content {
        max-width: 100%;
        margin: 0 auto;
        padding: 2rem 3rem;
    }
}

@media (min-width: 1600px) {
    .main-content {
        max-width: 100%;
    }
}

/* ============================================
   CARD & TABLE LAYOUTS
   ============================================ */
.card {
    width: 100%;
    margin-bottom: 1.5rem;
}

.card-body {
    width: 100%;
}

.table-responsive {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* Ensure tables don't break layout */
table {
    width: 100%;
    table-layout: auto;
}

/* ============================================
   FIX ONE-SIDED CONTENT ISSUES
   ============================================ */
/* Remove any hardcoded left/right positioning that causes drift */
.nxl-content > * {
    position: relative;
}

/* Ensure flex containers are balanced */
.tw-flex {
    display: flex;
}

.tw-flex-col {
    flex-direction: column;
}

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

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

.tw-justify-between {
    justify-content: space-between;
}

/* ============================================
   GRID SYSTEM FIXES
   ============================================ */
/* Ensure Bootstrap grid works properly */
.row {
    display: flex;
    flex-wrap: wrap;
    margin-left: -0.75rem;
    margin-right: -0.75rem;
}

.row > * {
    flex-shrink: 0;
    width: 100%;
    max-width: 100%;
    padding-left: 0.75rem;
    padding-right: 0.75rem;
}

/* ============================================
   MOBILE RESPONSIVE FIXES
   ============================================ */
@media (max-width: 1024px) {
    .nxl-container {
        margin-left: 0 !important;
    }
    
    .nxl-header {
        left: 0 !important;
        width: 100% !important;
    }

    .main-content {
        width: 100%;
        max-width: 100%;
        padding: 1rem;
    }
    
    /* Stack columns on mobile */
    .row > [class*="col-"] {
        flex: 0 0 100%;
        max-width: 100%;
    }
    
    /* Ensure cards are full width on mobile */
    .card {
        margin-left: 0;
        margin-right: 0;
    }
}

/* ============================================
   TABLET RESPONSIVE FIXES
   ============================================ */
@media (min-width: 769px) and (max-width: 1199px) {
    .main-content {
        max-width: 100%;
        padding: 1.5rem;
    }
}

/* ============================================
   PREVENT LAYOUT SHIFT
   ============================================ */
/* Reserve space for dynamic content */
#ajax-content {
    min-height: 200px;
    position: relative;
}

/* Prevent content jump on load */
.table-responsive {
    min-height: 100px;
}

/* ============================================
   CENTERED EMPTY STATES
   ============================================ */
.empty-state,
.error-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    max-width: 600px;
    margin: 2rem auto;
    text-align: center;
}

/* ============================================
   FIX ABSOLUTE POSITIONING ISSUES
   ============================================ */
/* Ensure absolute positioned elements don't break layout */
.nxl-content [style*="position: absolute"] {
    position: relative !important;
}

/* Only allow absolute positioning in specific contexts */
.dropdown-menu,
.tooltip {
    position: absolute;
}

.modal {
    position: fixed !important;
}

/* ============================================
   OVERFLOW FIXES
   ============================================ */
body {
    overflow-x: hidden;
}

.nxl-container {
    overflow-x: hidden;
}

.nxl-content {
    overflow-x: hidden;
}

/* Allow horizontal scroll only for tables */
.table-responsive {
    overflow-x: auto;
    overflow-y: visible;
}

/* ============================================
   DARK MODE COMPATIBILITY
   ============================================ */
.app-skin-dark .nxl-content,
.dark-theme .nxl-content {
    background: var(--bg-page, #1a1d29) !important;
}

.app-skin-dark .main-content,
.dark-theme .main-content {
    color: var(--text-main, #e4e6eb);
}

.app-skin-dark .card,
.dark-theme .card {
    background: var(--bg-card, #252836);
    border-color: var(--border-color, #2d3142);
}

.app-skin-dark .table,
.dark-theme .table {
    color: var(--text-main, #e4e6eb);
}

.app-skin-dark .table thead th,
.dark-theme .table thead th {
    color: var(--text-muted, #9ca3af);
    border-color: var(--border-color, #2d3142);
}

.app-skin-dark .table tbody td,
.dark-theme .table tbody td {
    border-color: var(--border-color, #2d3142);
}

/* ============================================
   AJAX CONTENT CENTERING FIXES
   ============================================ */
/* Ensure AJAX-loaded content maintains proper layout */
.nxl-content {
    box-sizing: border-box;
    margin-left: 0;
    margin-right: 0;
    padding: 0;
}

/* Prevent content from drifting to one side after AJAX load */
.nxl-content .main-content {
    margin-left: auto;
    margin-right: auto;
    padding-left: 0;
    padding-right: 0;
}

/* Ensure all direct children of .nxl-content are properly aligned */
.nxl-content > .main-content,
.nxl-content > .row,
.nxl-content > .card {
    margin-left: auto;
    margin-right: auto;
}

/* Fix for content that might have hardcoded positioning */
.nxl-content [style*="left"],
.nxl-content [style*="right"] {
    position: relative !important;
    left: auto !important;
    right: auto !important;
}

/* Ensure flex containers maintain center alignment */
.nxl-content .tw-flex {
    justify-content: flex-start;
}

.nxl-content .tw-flex.tw-justify-center {
    justify-content: center;
}

.nxl-content .tw-flex.tw-justify-between {
    justify-content: space-between;
}

/* Prevent width issues after AJAX injection */
.nxl-content * {
    max-width: 100%;
    box-sizing: border-box;
}

/* ============================================
   ROBUST PRINT SYSTEM
   Ensures professional output for manifests, invoices, and reports
   ============================================ */
@media print {
    /* Hide non-essential UI elements */
    .nxl-navigation,
    .nxl-header,
    .page-header,
    footer,
    .btn,
    .dt-buttons,
    .card-header .tw-flex.tw-gap-3,
    #batch-form,
    #search-form,
    .tw-mb-10,
    .dataTables_length,
    .dataTables_filter,
    .dataTables_info,
    .dataTables_paginate {
        display: none !important;
    }

    /* Reset layout for print */
    .nxl-container {
        margin-top: 0 !important;
        padding: 0 !important;
        min-height: auto !important;
    }

    .nxl-content {
        padding: 0 !important;
        margin: 0 !important;
    }

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

    /* Table Print Optimizations */
    .table-responsive {
        overflow: visible !important;
        margin-bottom: 0 !important;
    }

    table {
        width: 100% !important;
        border-collapse: collapse !important;
        font-size: 9pt !important; /* Slightly smaller for professional look */
        font-family: 'Helvetica', 'Arial', sans-serif !important;
    }

    th, td {
        border: 1px solid #000 !important; /* Proper black borders for contrast */
        padding: 6px 8px !important;
        color: #000 !important;
        vertical-align: middle !important;
    }

    th {
        background-color: transparent !important; /* Default transparent for title/meta rows */
        font-weight: bold !important;
        text-transform: uppercase !important;
        font-size: 8pt !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    /* Apply background only to the actual column headers */
    thead tr.manifest-header-row th {
        background-color: #f0f0f0 !important;
    }

    /* Remove any UI-specific colored backgrounds for print */
    [class*="bg-"], [class*="tw-bg-"] {
        background-color: transparent !important;
    }
    
    /* Ensure text alignment works */
    .text-end, .text-right, .tw-text-right { text-align: right !important; }
    .text-center, .tw-text-center { text-align: center !important; }

    /* Force black text for better legibility */
    body, h1, h2, h3, h4, h5, h6, p, sp, div, a {
        color: #000 !important; 
        text-decoration: none !important;
    }

    /* Professional Header/Footer spacing */
    h1 { font-size: 18pt !important; margin-bottom: 5px !important; }
    h2 { font-size: 14pt !important; }
    p { font-size: 10pt !important; }

    /* Page Breaks */
    tr {
        page-break-inside: avoid !important;
    }

    thead {
        display: table-header-group !important;
    }

    /* Hide UI elements within table if necessary (like buttons) */
    td .btn, td a.btn {
        display: none !important;
    }
    
    /* Ensure no shadows */
    * {
        box-shadow: none !important;
        text-shadow: none !important;
    }

    .card {
        border: none !important;
        margin: 0 !important;
    }

    .card-body {
        padding: 0 !important;
    }

    /* Remove bootstrap zebra striping backgrounds if they look messy */
    .table-striped > tbody > tr:nth-of-type(odd) > * {
        --bs-table-accent-bg: transparent !important;
        background-color: transparent !important; 
    }
}

/* ============================================
   MODAL BLUR FIX
   Ensures modals are crisp and not affected by global container filters
   ============================================ */
body.modal-open .nxl-container,
body.modal-open .nxl-header,
body.modal-open .nxl-navigation,
body.modal-open .page-header {
    filter: none !important;
}

/* Add premium backdrop blur to the modal backdrop itself instead */
.modal-backdrop.show {
    backdrop-filter: blur(5px) !important;
    -webkit-backdrop-filter: blur(5px) !important;
    background-color: rgba(15, 23, 42, 0.5) !important; /* Elegant slate overlay */
}

/* Ensure modal content is sharp */
.modal-content {
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5) !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
}

/* ============================================
   STRICT MODAL & DROPDOWN FIXES
   Mandatory overrides for specific user requirements
   ============================================ */
.modal {
    z-index: 1060 !important;
}

.modal-backdrop {
    z-index: 1055 !important;
}

/* Ensure no parent container blocks the modal */
body.modal-open {
    overflow: hidden; /* Only body should handle overflow when modal is open */
}

/* Fix z-index for Select2 inside modals */
.select2-container--open {
    z-index: 1061 !important; /* Above modal */
}

/* Ensure datepicker is above modal */
.daterangepicker {
    z-index: 1062 !important;
}

/* ============================================
   DATA TABLES PAGINATION FIXES
   ============================================ */
/* Hide DataTables redundant Info & Pagination if manual pagination exists */
body:has(.pagination) .dataTables_paginate,
body:has(.pagination) .dataTables_info {
    display: none !important;
}

/* Ensure manual pagination buttons are responsive and don't wrap text ugly */
.pagination, .pagination-common-style {
    display: flex !important;
    flex-direction: row !important;
    align-items: center !important;
    justify-content: flex-end !important;
    list-style: none !important;
    padding: 0 !important;
    margin: 0 !important;
    gap: 8px !important;
}

.pagination .page-item, .pagination-common-style li {
    margin: 0 !important;
    display: flex !important;
}

.pagination .page-link, .pagination-common-style li a {
    white-space: nowrap;
    display: flex !important;
    align-items: center;
    justify-content: center;
    width: 40px !important;
    height: 40px !important;
    padding: 0 !important;
    border-radius: 12px !important; /* tw-rounded-xl */
    transition: all 0.2s ease;
}

/* Ensure pagination container is properly balanced */
nav[aria-label="Page navigation"] {
    display: flex !important;
    align-items: center !important;
    justify-content: space-between !important;
    width: 100% !important;
    gap: 20px !important;
}

@media (max-width: 576px) {
    .pagination {
        flex-wrap: wrap;
        justify-content: center !important;
        gap: 5px;
    }
    
    .pagination .page-item {
        margin: 2px;
    }

    /* Hide "Page X of Y" text on very small screens to save space */
    .pagination .page-item.disabled span.page-link {
        font-size: 11px;
        padding: 0.5rem 0.5rem;
    }
}

/* Ensure DataTables buttons container is responsive */
.dt-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
    margin-bottom: 15px;
}

/* Fix table width in DataTables wrapper */
.dataTables_wrapper {
    width: 100%;
    overflow-x: auto;
}
