/* =====================================================
   Responsive overrides for Equipment Loan Management
   Supports: 320px / 390px / 768px / 1024px / 1440px+
   ===================================================== */

/* ---------------------------------------------------
   1. GLOBAL LAYOUT FIX — Prevent horizontal overflow
   --------------------------------------------------- */
*, *::before, *::after {
    box-sizing: border-box;
}

html, body {
    max-width: 100%;
    overflow-x: hidden;
}

/* The d-flex layout wrapper must not overflow */
.d-flex {
    min-width: 0;
}

/* Images and embedded content must stay within container */
img, video, iframe, canvas {
    max-width: 100%;
}

/* ---------------------------------------------------
   2. MOBILE TOP NAVIGATION BAR
   --------------------------------------------------- */
.mobile-header-bar {
    display: none;
    background-color: #ffffff;
    border-bottom: 1px solid var(--border-color, #e2e8f0);
    padding: 0.75rem 1.25rem;
    position: sticky;
    top: 0;
    z-index: 1020;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    width: 100%;
}

.mobile-header-bar .btn-toggle {
    border: none;
    background: transparent;
    font-size: 1.4rem;
    color: var(--text-primary, #1e293b);
    width: 44px;
    height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    transition: background-color 0.2s;
    flex-shrink: 0;
}

.mobile-header-bar .btn-toggle:active {
    background-color: var(--light, #f8fafc);
}

.mobile-header-bar .brand-text {
    font-weight: 700;
    font-size: 1rem;
    letter-spacing: -0.3px;
    color: var(--dark, #0f172a);
    margin-left: 0.5rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.mobile-header-bar .user-avatar {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: linear-gradient(135deg, var(--primary, #1a2e6b), var(--secondary, #2d5fbe));
    color: white;
    font-weight: 700;
    font-size: 0.8rem;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    border: 2px solid rgba(201,162,39,0.4);
}

/* ---------------------------------------------------
   3. OFFCANVAS SIDEBAR (Mobile drawer)
   --------------------------------------------------- */
.offcanvas-sidebar {
    background: var(--sidebar-bg, rgba(15, 23, 42, 0.98)) !important;
    backdrop-filter: blur(12px) !important;
    width: 280px !important;
    border-right: 1px solid rgba(255, 255, 255, 0.08) !important;
    max-width: 85vw !important; /* Never exceed 85% on very small screens */
}

.offcanvas-sidebar .offcanvas-header {
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    padding: 1.25rem 1.5rem;
}

.offcanvas-sidebar .btn-close {
    filter: invert(1) grayscale(1) brightness(2);
    opacity: 0.75;
}

.offcanvas-sidebar .offcanvas-body {
    padding: 0;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

/* ---------------------------------------------------
   4. TOUCH TARGET SIZES — Only for interactive items
      NOT applied globally to prevent layout inflation
   --------------------------------------------------- */
.mobile-header-bar .btn-toggle,
.offcanvas-sidebar .nav-link,
.sidebar-menu .nav-link {
    min-height: 44px;
}

/* Form controls should be touch-friendly */
.form-control,
.form-select {
    min-height: 44px;
}

/* ---------------------------------------------------
   5. FORM CHECK INPUTS — Larger for touch
   --------------------------------------------------- */
.form-check-input {
    width: 20px;
    height: 20px;
    margin-top: 0.25em;
    flex-shrink: 0;
}

/* ---------------------------------------------------
   6. MODALS — Ensure they scroll and fit on mobile
   --------------------------------------------------- */
.modal-content {
    border-radius: 20px;
}

.modal-dialog {
    margin: 0.5rem auto;
}

/* ---------------------------------------------------
   7. TABLES — Base overflow protection
   --------------------------------------------------- */
.table-responsive {
    border: none;
    margin-bottom: 0;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* ---------------------------------------------------
   8. BREAKPOINT: TABLET & MOBILE (< 992px)
      Hide desktop sidebar, show mobile header
   --------------------------------------------------- */
@media (max-width: 991.98px) {
    /* Hide the fixed desktop sidebar */
    .sidebar {
        display: none !important;
    }

    /* Remove sidebar left margin — layout is d-block on mobile */
    .main-content {
        margin-left: 0 !important;
        padding: 1rem !important;
        width: 100% !important;
        max-width: 100% !important;
        overflow-x: hidden !important;
    }

    /* Show mobile header */
    .mobile-header-bar {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    /* Page header inside main content */
    .navbar-custom {
        margin-top: 0.5rem;
        margin-bottom: 1rem;
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }

    .page-title {
        font-size: 1.4rem;
        line-height: 1.2;
    }

    .page-subtitle {
        font-size: 0.875rem;
    }

    /* Card headers stack vertically */
    .card-premium .card-header {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 0.75rem;
    }

    .card-premium .card-header .btn,
    .card-premium .card-header a.btn {
        width: 100%;
        justify-content: center;
        text-align: center;
    }

    /* Reduce card body padding */
    .card-premium .card-body {
        padding: 1rem;
    }

    /* DataTables Responsive child rows */
    .dtr-details {
        width: 100%;
        font-size: 0.875rem;
    }

    /* Report filter cards — allow vertical stacking */
    .reports-actions .btn {
        width: 100%;
    }

    /* Stat cards inner layout */
    .stat-card {
        padding: 1.25rem;
    }

    /* Prevent filter form overflow */
    .card-premium form.row {
        flex-direction: column;
    }

    /* Accordion items (loan create) touch friendly */
    .accordion-button {
        min-height: 52px;
        font-size: 0.95rem;
    }
}

/* ---------------------------------------------------
   9. BREAKPOINT: TABLET (768px – 1024px)
   --------------------------------------------------- */
@media (min-width: 768px) and (max-width: 1024px) {
    .main-content {
        padding: 1.5rem !important;
    }
    .stat-card {
        padding: 1.25rem;
    }
    .stat-card .stat-value {
        font-size: 1.5rem;
    }
}

/* ---------------------------------------------------
   10. BREAKPOINT: SMALL MOBILE (≤ 576px)
       Core layout compression
   --------------------------------------------------- */
@media (max-width: 576px) {
    .main-content {
        padding: 0.75rem !important;
    }

    .card-premium {
        border-radius: 12px;
        margin-bottom: 1rem;
    }

    .card-premium .card-body {
        padding: 0.875rem;
    }

    .page-title {
        font-size: 1.3rem;
    }

    /* Action button groups inside tables - keep small */
    .table td .btn,
    .table td .btn-action {
        min-height: 34px;
        padding: 0.3rem 0.5rem;
        font-size: 0.8rem;
    }

    /* Modals: take most of screen width */
    .modal-dialog {
        margin: 0.25rem;
        max-width: calc(100vw - 0.5rem);
    }

    .modal-content {
        border-radius: 16px;
    }

    /* Accordion collapse for loan creation */
    .accordion-body {
        padding: 0.75rem;
    }
    .accordion-body .row.g-2 {
        gap: 0.5rem;
    }

    /* Beneficiary form - force single column */
    .row.g-3 > [class*="col-md-"],
    .row.g-3 > [class*="col-lg-"] {
        flex: 0 0 100%;
        max-width: 100%;
    }

    /* Stat cards: ensure they fill width */
    .stat-card {
        padding: 1rem;
    }
    .stat-card .stat-value {
        font-size: 1.5rem;
    }
    .stat-card .stat-icon {
        width: 46px;
        height: 46px;
        font-size: 1.25rem;
    }
}

/* ---------------------------------------------------
   11. BREAKPOINT: VERY SMALL (≤ 390px)
   --------------------------------------------------- */
@media (max-width: 390px) {
    .mobile-header-bar {
        padding: 0.6rem 0.875rem;
    }
    .mobile-header-bar .brand-text {
        font-size: 0.95rem;
    }

    .main-content {
        padding: 0.625rem !important;
    }

    .card-premium .card-body {
        padding: 0.75rem;
    }

    .page-title {
        font-size: 1.2rem;
    }

    /* Prevent filter form selects/inputs from overflowing */
    .form-select, .form-control {
        font-size: 0.875rem;
    }
}

/* ---------------------------------------------------
   12. BREAKPOINT: MINIMUM (≤ 320px)
   --------------------------------------------------- */
@media (max-width: 320px) {
    .main-content {
        padding: 0.5rem !important;
    }
    .card-premium .card-body {
        padding: 0.625rem;
    }
    .card-premium .card-header {
        padding: 0.75rem;
    }
    .table-premium td,
    .table-premium th {
        padding: 8px 6px !important;
        font-size: 0.8rem;
    }
    .stat-card {
        flex-direction: column;
        gap: 0.5rem;
        text-align: center;
    }
    .stat-card .stat-icon {
        margin: 0 auto;
    }
}

/* ---------------------------------------------------
   13. DATATABLES RESPONSIVE — Child row styling
   --------------------------------------------------- */
table.dataTable.dtr-inline.collapsed > tbody > tr > td.child,
table.dataTable.dtr-inline.collapsed > tbody > tr > th.child {
    background-color: var(--light, #f8fafc) !important;
    padding: 1rem !important;
    border-radius: 8px;
}

table.dataTable.dtr-inline.collapsed > tbody > tr > td.dtr-control:before,
table.dataTable.dtr-inline.collapsed > tbody > tr > th.dtr-control:before {
    background-color: var(--primary, #6366f1) !important;
    border: none !important;
    box-shadow: none !important;
    line-height: 16px !important;
}

/* DataTables wrapper must not overflow */
.dataTables_wrapper {
    overflow-x: auto;
    width: 100%;
}

/* DataTables length/filter controls on mobile */
@media (max-width: 576px) {
    .dataTables_wrapper .dataTables_length,
    .dataTables_wrapper .dataTables_filter {
        width: 100%;
        text-align: left;
        margin-bottom: 0.5rem;
    }
    .dataTables_wrapper .dataTables_filter input {
        width: 100%;
    }
    .dataTables_wrapper .dataTables_info,
    .dataTables_wrapper .dataTables_paginate {
        width: 100%;
        text-align: center;
        margin-top: 0.5rem;
    }
}

/* ---------------------------------------------------
   14. PRINT ISOLATION — Never affect PDF/paper output
   --------------------------------------------------- */
@media print {
    .mobile-header-bar,
    .offcanvas,
    .offcanvas-backdrop,
    .no-print {
        display: none !important;
    }
    .main-content {
        margin-left: 0 !important;
        padding: 0 !important;
        width: 100% !important;
    }
    body {
        overflow-x: visible;
    }
}
