/* =============================================================
   CareSpend — Mobile & Responsive Styles
   Breakpoints:
     Mobile  : ≤ 767px
     Tablet  : 768px – 991px
     Desktop : ≥ 992px
   ============================================================= */

/* -------------------------------------------------------------
   1. Global baseline — overflow control & text scaling
   ------------------------------------------------------------- */
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    overflow-x: hidden;
}

img,
video,
iframe,
embed,
object {
    max-width: 100%;
    height: auto;
}

* {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

/* Prevent any container from causing horizontal scroll */
.container-fluid,
.container {
    overflow-x: hidden;
}

/* -------------------------------------------------------------
   2. Sidebar off-canvas drawer  (mobile + tablet < 992px)
   ------------------------------------------------------------- */

/* Sidebar becomes a fixed off-canvas panel on small screens */
@media (max-width: 991.98px) {
    .sidebar {
        position: fixed !important;
        top: 0;
        left: -270px !important;
        width: 260px !important;
        min-width: 260px !important;
        height: 100vh !important;
        min-height: 100vh !important;
        z-index: 1050;
        overflow-y: auto;
        overflow-x: hidden;
        transition: left 0.3s ease-in-out;
    }

    /* Sidebar visible state */
    .sidebar.sidebar-open {
        left: 0 !important;
        box-shadow: 4px 0 28px rgba(0, 0, 0, 0.28);
    }

    /* Lock page scroll while sidebar is open */
    body.sidebar-open {
        overflow: hidden;
    }

    /* Main content occupies full width */
    .main-content {
        width: 100% !important;
        min-width: 0 !important;
        padding: 1rem !important;
        padding-bottom: 4rem !important;
    }

    /* Inline styles on the sidebar <nav> set width:220px — force override */
    nav.sidebar[style] {
        width: 260px !important;
    }
}

/* Desktop: sidebar is always visible in normal flow */
@media (min-width: 992px) {
    .sidebar-overlay,
    .sidebar-close-btn,
    .mobile-menu-toggle {
        display: none !important;
    }
}

/* -------------------------------------------------------------
   3. Sidebar backdrop overlay
   ------------------------------------------------------------- */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1040;
    cursor: pointer;
    /* Animate in */
    opacity: 0;
    transition: opacity 0.3s ease;
}

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

/* -------------------------------------------------------------
   4. Mobile hamburger / toggle button
   ------------------------------------------------------------- */
.mobile-menu-toggle {
    display: none; /* flex on mobile via media query below */
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border: none;
    background: transparent;
    color: #2a5298;
    font-size: 1.35rem;
    cursor: pointer;
    border-radius: 8px;
    padding: 0;
    flex-shrink: 0;
    margin-right: 0.5rem;
    transition: background 0.15s ease;
    -webkit-appearance: none;
}

.mobile-menu-toggle:hover,
.mobile-menu-toggle:focus {
    background: rgba(42, 82, 152, 0.1);
    outline: 2px solid rgba(42, 82, 152, 0.3);
    outline-offset: 2px;
}

@media (max-width: 991.98px) {
    .mobile-menu-toggle {
        display: flex !important;
    }
}

/* -------------------------------------------------------------
   5. Sidebar close button (shown inside the drawer on mobile)
   ------------------------------------------------------------- */
.sidebar-close-btn {
    display: none;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 0.65rem;
    right: 0.65rem;
    width: 34px;
    height: 34px;
    background: rgba(255, 255, 255, 0.15);
    border: none;
    border-radius: 50%;
    color: #fff;
    font-size: 1rem;
    cursor: pointer;
    z-index: 5;
    padding: 0;
    flex-shrink: 0;
    transition: background 0.15s ease;
}

.sidebar-close-btn:hover {
    background: rgba(255, 255, 255, 0.3);
}

@media (max-width: 991.98px) {
    .sidebar-close-btn {
        display: flex !important;
    }

    /* Add a little right padding to the logo so it doesn't overlap the close btn */
    .sidebar > a.sidebar-brand {
        padding-right: 2.5rem;
    }
}

/* -------------------------------------------------------------
   6. Header — compact on mobile
   ------------------------------------------------------------- */
@media (max-width: 767.98px) {
    header.mb-4 {
        margin-bottom: 0.75rem !important;
    }

    header h2,
    header .h2 {
        font-size: 1.05rem !important;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 55vw;
    }

    #userProfileDropdown {
        font-size: 0.78rem;
        max-width: 140px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
}

/* -------------------------------------------------------------
   7. Input anti-zoom
   iOS Safari auto-zooms when a focused input has font-size < 16px.
   Force 16px on mobile to suppress that behaviour.
   ------------------------------------------------------------- */
@media (max-width: 767.98px) {
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
    select,
    textarea,
    .form-control,
    .form-select {
        font-size: 16px !important;
        line-height: 1.4;
    }
}

/* -------------------------------------------------------------
   8. Dashboard KPI / stat cards
   ------------------------------------------------------------- */
@media (max-width: 575.98px) {
    /* 2-up grid on small phones for col-md-3 cards */
    .row.g-4 > [class*="col-md-3"],
    .row.g-3 > [class*="col-md-3"],
    .row.g-2 > [class*="col-md-3"] {
        flex: 0 0 50%;
        max-width: 50%;
        width: 50%;
    }

    /* If there are col-md-4 3-up layouts, stack to full-width */
    .row.g-4 > [class*="col-md-4"],
    .row.g-3 > [class*="col-md-4"] {
        flex: 0 0 100%;
        max-width: 100%;
        width: 100%;
    }
}

/* -------------------------------------------------------------
   9. Cards — compact padding on mobile
   ------------------------------------------------------------- */
@media (max-width: 767.98px) {
    .card {
        border-radius: 8px;
    }

    .card-header {
        padding: 0.6rem 0.85rem;
        font-size: 0.9rem;
    }

    .card-body {
        padding: 0.75rem;
    }

    .card-footer {
        padding: 0.5rem 0.75rem;
    }

    /* Stat card numbers */
    .card-body h4,
    .card-body .h4 {
        font-size: 1.2rem;
        margin-bottom: 0.15rem;
    }

    .card-body span,
    .card-body small {
        font-size: 0.78rem;
    }
}

/* -------------------------------------------------------------
   10. Tables — horizontal scroll + compact font
   ------------------------------------------------------------- */
@media (max-width: 767.98px) {
    .table-responsive,
    .table-responsive-sm,
    .table-responsive-md,
    .table-responsive-lg {
        -webkit-overflow-scrolling: touch;
        border: 0;
    }

    .table th,
    .table td {
        font-size: 0.8rem;
        padding: 0.35rem 0.45rem;
        white-space: nowrap;
    }

    /* Allow wrapping for description / notes columns */
    .table th.text-wrap,
    .table td.text-wrap,
    .table-wrap th,
    .table-wrap td {
        white-space: normal !important;
        min-width: 100px;
    }
}

/* Optional: stacked card-style rows for very small screens.
   Opt in by adding class="table-stack-mobile" to the <table>. */
@media (max-width: 480px) {
    .table-stack-mobile thead {
        display: none;
    }

    .table-stack-mobile tbody tr {
        display: block;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        margin-bottom: 0.65rem;
        padding: 0.4rem 0.5rem;
        background: #fff;
    }

    .table-stack-mobile tbody td {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        padding: 0.25rem 0.35rem;
        border: none;
        white-space: normal;
        font-size: 0.82rem;
        gap: 0.5rem;
    }

    .table-stack-mobile tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #495057;
        flex: 0 0 42%;
        font-size: 0.75rem;
    }
}

/* Utility class for explicit horizontal scroll container */
.cs-table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    width: 100%;
}

/* -------------------------------------------------------------
   11. Buttons — minimum 44 × 44 touch target on mobile
   ------------------------------------------------------------- */
@media (max-width: 767.98px) {
    .btn:not(.btn-sm):not(.btn-link):not(.btn-close):not(.mobile-menu-toggle):not(.sidebar-close-btn) {
        min-height: 44px;
        padding-top: 0.5rem;
        padding-bottom: 0.5rem;
    }

    .btn-sm {
        min-height: 36px;
        font-size: 0.78rem;
        padding: 0.3rem 0.55rem;
    }

    /* Wrap button toolbars instead of overflowing */
    .d-flex.gap-2,
    .d-flex.gap-3 {
        flex-wrap: wrap;
    }

    /* Icon-only buttons */
    .btn .fa,
    .btn .fas,
    .btn .far {
        pointer-events: none;
    }
}

/* -------------------------------------------------------------
   12. Modals — scrollable, no viewport overflow
   ------------------------------------------------------------- */
@media (max-width: 767.98px) {
    .modal-dialog {
        margin: 0.75rem auto !important;
        max-width: calc(100vw - 1.5rem) !important;
    }

    .modal-dialog.modal-lg,
    .modal-dialog.modal-xl {
        margin: 0.5rem !important;
        max-width: calc(100vw - 1rem) !important;
        width: calc(100vw - 1rem) !important;
    }

    .modal-content {
        border-radius: 12px;
        /* Limit height so modal never overflows viewport */
        max-height: calc(100dvh - 2rem);
        max-height: calc(100vh - 2rem); /* fallback for browsers without dvh */
        display: flex;
        flex-direction: column;
    }

    .modal-body {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        flex: 1;
        min-height: 0;
    }

    .modal-header {
        padding: 0.75rem 1rem;
        flex-shrink: 0;
    }

    .modal-footer {
        padding: 0.65rem 1rem;
        flex-wrap: wrap;
        gap: 0.5rem;
        flex-shrink: 0;
    }

    .modal-footer .btn {
        flex: 1 1 auto;
        min-width: 90px;
    }
}

/* -------------------------------------------------------------
   13. Forms — prevent overflow, usable layout
   ------------------------------------------------------------- */
@media (max-width: 767.98px) {
    .form-label {
        font-size: 0.875rem;
        margin-bottom: 0.2rem;
    }

    /* Input groups must not overflow */
    .input-group {
        flex-wrap: nowrap;
    }

    .input-group > .form-control,
    .input-group > .form-select {
        min-width: 0;
    }

    /* Date / time pickers */
    input[type="date"],
    input[type="datetime-local"],
    input[type="time"],
    input[type="month"] {
        min-width: 0;
        width: 100%;
    }

    /* Prevent forms from overflowing their container */
    form {
        overflow-x: hidden;
    }
}

/* -------------------------------------------------------------
   14. Typography — responsive scale
   ------------------------------------------------------------- */
@media (max-width: 767.98px) {
    h1, .h1 { font-size: 1.5rem; }
    h2, .h2 { font-size: 1.25rem; }
    h3, .h3 { font-size: 1.1rem; }
    h4, .h4 { font-size: 1rem; }
    h5, .h5 { font-size: 0.95rem; }
    h6, .h6 { font-size: 0.875rem; }

    .display-4,
    .display-5,
    .display-6 {
        font-size: 1.5rem !important;
    }

    .lead {
        font-size: 0.95rem;
    }

    .badge {
        font-size: 0.68rem;
    }

    .alert {
        font-size: 0.875rem;
        padding: 0.5rem 0.75rem;
    }

    .breadcrumb {
        font-size: 0.8rem;
    }

    /* Compact list-group text */
    .list-group-item {
        font-size: 0.875rem;
        padding: 0.55rem 0.85rem;
    }
}

/* -------------------------------------------------------------
   15. Footer — static (not fixed) on mobile/tablet
   ------------------------------------------------------------- */
@media (max-width: 991.98px) {
    .footer {
        position: static !important;
        font-size: 0.8rem;
        padding: 0.65rem 0;
    }
}

/* -------------------------------------------------------------
   16. Collapsible filter / search panels
   Usage: add class="cs-filter-panel" to your filter card,
          class="cs-filter-toggle" to the button.
   ------------------------------------------------------------- */
.cs-filter-toggle {
    display: none;
}

@media (max-width: 767.98px) {
    .cs-filter-panel {
        overflow: hidden;
        max-height: 0;
        transition: max-height 0.3s ease, padding 0.3s ease;
        padding-top: 0 !important;
        padding-bottom: 0 !important;
    }

    .cs-filter-panel.cs-filter-panel--open {
        max-height: 1200px;
        padding-top: revert !important;
        padding-bottom: revert !important;
    }

    .cs-filter-toggle {
        display: inline-flex !important;
        align-items: center;
        gap: 0.4rem;
    }
}

/* -------------------------------------------------------------
   17. Login / auth page (standalone — no sidebar)
   ------------------------------------------------------------- */
@media (max-width: 480px) {
    .login-container {
        margin: 1.5rem auto !important;
        border-radius: 10px !important;
        padding: 1.25rem !important;
    }
}

/* -------------------------------------------------------------
   18. Dropdown menus — prevent viewport overflow
   ------------------------------------------------------------- */
@media (max-width: 767.98px) {
    .dropdown-menu {
        max-height: 60vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* -------------------------------------------------------------
   19. Pagination — compact on mobile
   ------------------------------------------------------------- */
@media (max-width: 767.98px) {
    .pagination .page-link {
        padding: 0.3rem 0.55rem;
        font-size: 0.82rem;
    }
}

/* -------------------------------------------------------------
   20. Receipt / image previews
   ------------------------------------------------------------- */
@media (max-width: 767.98px) {
    .receipt-preview img,
    .receipt-image,
    img.receipt-img {
        max-width: 100%;
        height: auto;
        border-radius: 6px;
    }
}

/* -------------------------------------------------------------
   21. Nav tabs / pills — scroll horizontally on mobile
   ------------------------------------------------------------- */
@media (max-width: 767.98px) {
    .nav-tabs,
    .nav-pills {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        -ms-overflow-style: none;
    }

    .nav-tabs::-webkit-scrollbar,
    .nav-pills::-webkit-scrollbar {
        display: none;
    }

    /* Sidebar pills are vertical so don't need the above — revert */
    .sidebar .nav-pills {
        flex-wrap: wrap;
        overflow-x: visible;
    }

    .nav-tabs .nav-link,
    .nav-pills .nav-link {
        white-space: nowrap;
        font-size: 0.82rem;
        padding: 0.4rem 0.7rem;
    }
}

/* -------------------------------------------------------------
   22. Utility classes (reusable)
   ------------------------------------------------------------- */

/* Explicit horizontal scroll wrapper */
.scroll-x {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* Touch-friendly 44px target wrapper */
.touch-target {
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* Visually hide on mobile */
@media (max-width: 767.98px) {
    .hide-mobile {
        display: none !important;
    }
}

/* Visually hide on desktop */
@media (min-width: 768px) {
    .hide-desktop {
        display: none !important;
    }
}

/* Text truncation helper */
.text-truncate-2 {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* -------------------------------------------------------------
   23. Print — preserve desktop layout
   ------------------------------------------------------------- */
@media print {
    .sidebar,
    .sidebar-overlay,
    .mobile-menu-toggle,
    .footer {
        display: none !important;
    }

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