/* ============================================================================
 * Mobile / Responsive layer
 * ----------------------------------------------------------------------------
 * Single place for cross-portal mobile adjustments. Loaded after style.css so
 * rules here win on specificity ties. Keeps the base template intact while
 * polishing the custom components added throughout the portal (dashboard
 * cards, idea/download toolbars, admin tables, modals, etc.).
 *
 * Bootstrap 5 breakpoints used as reference:
 *   xs: <576px   sm: ≥576px   md: ≥768px   lg: ≥992px   xl: ≥1200px
 * ==========================================================================*/

/* ───────────────────────────────────────────────────────────────────────────
 * 1) Global baseline — safe on every viewport
 * ──────────────────────────────────────────────────────────────────────────*/

html { -webkit-text-size-adjust: 100%; }

/* Prevent horizontal scroll caused by accidental overflow on narrow screens */
html, body { overflow-x: hidden; }

/* Anything visually labelled a button should be at least 40px tall (Apple HIG
 * recommends 44px, Material 48dp — 40 is a balanced minimum). Exclude toolbar
 * buttons that use btn-sm by design. */
@media (max-width: 767.98px) {
    .btn:not(.btn-sm):not(.btn-xs) { min-height: 40px; }
}

/* Inputs: same deal — 40px tall is comfortable to tap */
@media (max-width: 767.98px) {
    .form-control:not([type="hidden"]),
    .form-select { min-height: 40px; }
}

/* Select2 drops its own sizing — align with Bootstrap on mobile */
@media (max-width: 767.98px) {
    .select2-container--default .select2-selection--single { height: 40px !important; }
    .select2-container--default .select2-selection--single .select2-selection__rendered { line-height: 38px !important; }
    .select2-container--default .select2-selection--single .select2-selection__arrow { height: 38px !important; }
    .select2-container { width: 100% !important; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 2) Page container and page-header
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    .main-container.container-fluid,
    .app-content > .side-app { padding-left: 10px; padding-right: 10px; }

    /* Stack the page title and the breadcrumb on small screens */
    .page-header {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 6px;
    }
    .page-header .ms-auto { margin-left: 0 !important; }

    .page-header .page-title { font-size: 1.35rem; line-height: 1.25; }

    .page-header .breadcrumb {
        flex-wrap: wrap;
        font-size: .78rem;
        margin-bottom: 0;
    }
    /* Keep breadcrumb compact — collapse with CSS-only ellipsis when very wide */
    .page-header .breadcrumb .breadcrumb-item { white-space: nowrap; }
}

@media (max-width: 575.98px) {
    .page-header .page-title { font-size: 1.15rem; }
    /* Only show the active breadcrumb item on tiny screens; parents collapse */
    .page-header .breadcrumb .breadcrumb-item:not(.active) { display: none; }
    .page-header .breadcrumb .breadcrumb-item.active::before { content: none; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 3) Cards — less padding, no double gutters
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    .card { margin-bottom: 12px; border-radius: 10px; }
    .card > .card-body { padding: 12px 14px; }
    .card > .card-header { padding: 10px 14px; }
    .card > .card-header .card-title { font-size: 1rem; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 4) Toolbars — the combo of "search + filter dropdowns" pattern used on
 *    Downloads, Ideas, Tickets, etc.
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    .downloads-toolbar,
    .ideas-toolbar {
        flex-direction: column;
        align-items: stretch !important;
        gap: 8px;
    }
    .downloads-toolbar .search-box,
    .ideas-toolbar .search-box,
    .downloads-toolbar .filter-select,
    .ideas-toolbar .filter-select {
        width: 100%;
        min-width: 0;
    }

    /* Button groups inside toolbars wrap cleanly */
    .toolbar .btn-group,
    .page-header + .card .btn-group { flex-wrap: wrap; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 5) Tables — force horizontal scroll and sensible min-widths, so columns
 *    don't crush and stay legible. Also shrink cell padding on xs.
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 991.98px) {
    .table-responsive > .table { min-width: 640px; }
}
@media (max-width: 767.98px) {
    .table-responsive {
        -webkit-overflow-scrolling: touch;
        border-radius: 8px;
    }
    .table-responsive > .table th,
    .table-responsive > .table td {
        padding: 8px 10px;
        font-size: .85rem;
        vertical-align: middle;
    }
    .table-responsive > .table td .btn-sm { padding: 4px 8px; font-size: .78rem; }

    /* DataTables paging and length controls wrap rather than stretching */
    .dataTables_wrapper .dt-buttons { flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
    .dataTables_wrapper .dataTables_paginate .pagination { flex-wrap: wrap; }
    .dataTables_wrapper .dataTables_info,
    .dataTables_wrapper .dataTables_paginate { float: none !important; text-align: left; padding-top: 6px; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 6) Forms — labels above, inputs full-width, actions stack
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    .row.g-3 > [class^="col-"] { margin-bottom: 4px; }

    /* Action rows (Save / Cancel pairs) stack on xs */
    .d-flex.gap-2:has(> .btn + .btn) { flex-wrap: wrap; }
    .d-flex.gap-2 > .btn { flex: 1 1 auto; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 7) Modals — full-viewport on small screens so long content is usable
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    .modal-dialog:not(.modal-fullscreen):not(.modal-sm) {
        margin: 8px;
        max-width: calc(100vw - 16px);
    }
    .modal-content { border-radius: 12px; }
    .modal-body { padding: 16px; }
    .modal-footer { flex-wrap: wrap; gap: 8px; padding: 12px 16px; }
    .modal-footer > .btn { flex: 1 1 calc(50% - 4px); margin: 0 !important; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 8) Dashboard — idea/download/ticket cards and the "dash-section" blocks
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    /* Dashboard section outer spacing */
    .dash-section { margin-bottom: 12px; }
    .dash-section .card { margin-bottom: 0; }

    /* KPI rows: tighter gutter, and allow 2 per row on phones */
    .dash-section .row.mb-3 > .col-6 { margin-bottom: 8px; }

    /* The sticky "Make it yours" / "Getting Started" hero cards often clip
     * on narrow screens — allow them to wrap gracefully. */
    .dash-section [style*="border-radius: 16px"],
    .dash-section [style*="border-radius:16px"] {
        padding: 16px 18px !important;
    }
}

/* Download card (custom component on downloads.php) */
@media (max-width: 575.98px) {
    .download-card .download-header {
        flex-direction: column;
        gap: 8px !important;
    }
    .download-card .download-actions {
        width: 100%;
        justify-content: flex-start;
    }
    .download-card .download-actions .btn { flex: 0 1 auto; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 9) Sidebar polish — the Bootstrap template already handles off-canvas
 *    behaviour on mobile. We only need to ensure the main content padding
 *    matches when the sidebar collapses.
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 991.98px) {
    body.sidebar-mini .app-sidebar { z-index: 1030; }
    .app-content { padding-top: 0; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 10) Typography scaling
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    h1, .h1 { font-size: 1.5rem; }
    h2, .h2 { font-size: 1.25rem; }
    h3, .h3 { font-size: 1.1rem; }
    h4, .h4 { font-size: 1rem; }
    .fs-6 { font-size: .92rem !important; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 11) FullCalendar — make toolbar wrap and fit narrow screens
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    .fc .fc-toolbar.fc-header-toolbar {
        flex-direction: column;
        align-items: stretch;
        gap: 8px;
    }
    .fc .fc-toolbar-title { font-size: 1.1em !important; text-align: center; }
    .fc .fc-button-group { justify-content: center; }
    .fc .fc-daygrid-day-number { font-size: .8em; padding: 2px; }
    .fc-event { font-size: .72em; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 12) Image and media fluidity — nothing should blow out its container
 * ──────────────────────────────────────────────────────────────────────────*/

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

/* Allow iframe to keep its aspect ratio if explicitly set via style */
iframe[width][height] { height: auto; aspect-ratio: attr(width) / attr(height); }

/* ───────────────────────────────────────────────────────────────────────────
 * 13) Utility: hide-on-xs / show-on-xs companion helpers
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    .hide-on-xs { display: none !important; }
}
@media (min-width: 576px) {
    .show-on-xs-only { display: none !important; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 14) Landscape phones — subtle density tweaks
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) and (orientation: landscape) {
    .page-header { margin-bottom: 8px; }
    .card > .card-body { padding: 12px; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 15) Accessibility: focus ring always visible on mobile (since no hover)
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    .btn:focus-visible,
    .form-control:focus-visible,
    .form-select:focus-visible {
        outline: 2px solid #cd1316;
        outline-offset: 2px;
    }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 17) Dashboard KPI grid — 2 per row on phones, tighter gaps on tablet
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    /* Dashboard KPI rows use col-6 already on mobile; ensure internal padding
     * stays sane and cards don't cram text. */
    .dash-section .row > [class*="col-"] .card .card-body { padding: 10px 12px; }
    .dash-section .row > [class*="col-"] .card h3,
    .dash-section .row > [class*="col-"] .card .h3 { font-size: 1.15rem; margin-bottom: 2px; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 18) "Become a Xiting Hero" step progress bar — prevent the step chips and
 *     the adjacent "Open" button from crushing into each other.
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    .steps-progress,
    [class*="step"][class*="progress"] {
        flex-wrap: wrap;
        gap: 4px;
    }
    .step-chip,
    .step-circle { font-size: .75rem; min-width: 28px; height: 28px; line-height: 28px; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 19) Filter/search rows inside card headers (Partner Products, Partner
 *     Education, etc.) — keep label and input from overlapping.
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    .card-header {
        flex-wrap: wrap;
        gap: 8px;
    }
    .card-header > * { min-width: 0; }
    .card-header .dataTables_filter label {
        display: flex;
        flex-direction: column;
        align-items: stretch;
        gap: 4px;
        margin: 0;
        width: 100%;
    }
    .card-header .dataTables_filter input {
        margin-left: 0 !important;
        width: 100% !important;
    }
    .card-header .dataTables_length label {
        display: flex;
        align-items: center;
        gap: 6px;
        margin: 0;
    }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 20) Quick Links grid — keep tiles readable on phones
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    .xr-fav-tile,
    .xr-quick-link {
        font-size: .82rem;
        padding: 8px 10px !important;
    }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 21) FullCalendar list view — cleaner when used as default on phones
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    .fc-list-day-cushion,
    .fc-list-table td { padding: 8px 10px !important; }
    .fc-list-event-time { white-space: nowrap; min-width: 80px; font-size: .78rem; }
    .fc-list-event-title { font-size: .86rem; }
    .fc-list-empty-cushion { margin: 2em 0; font-size: .9rem; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 22) Admin pages — scope toggle and filter row wrap
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    .btn-group {
        flex-wrap: wrap;
    }
    .btn-group > .btn { flex: 1 1 auto; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 23) Mobile quick action bar (used on partner and customer dashboards) —
 *     ensure it never covers page footer content.
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    body.has-mobile-quick-bar,
    body { padding-bottom: env(safe-area-inset-bottom); }
    .main-container.container-fluid { padding-bottom: 72px; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 24) Partner Products — Downloads filter pills row
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    #pp-downloads .card-header > .d-flex { flex-direction: column; align-items: stretch !important; }
    #pp-downloads .card-header .d-flex.gap-1 {
        width: 100%;
        justify-content: flex-start;
        overflow-x: auto;
        padding-bottom: 4px;
        -webkit-overflow-scrolling: touch;
    }
    #pp-downloads .pp-filter-btn { white-space: nowrap; flex: 0 0 auto; }

    /* Downloads table gets a generous min-width so columns stay legible */
    #ppFilesTable { min-width: 720px; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 25) Partner Marketing — grid of campaign/benefit cards
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    /* Marketing page tends to use wide hero blocks with inline styles — pull
     * padding back so they breathe on phones */
    .pm-hero,
    .pm-section > [style*="padding: 40px"],
    .pm-section > [style*="padding:40px"] {
        padding: 20px !important;
    }
    .pm-card + .pm-card { margin-top: 12px; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 26) Partner Education — course cards and filter row
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    .eps-filters,
    .course-filters {
        flex-direction: column;
        gap: 8px;
    }
    .eps-filters > *,
    .course-filters > * { width: 100%; }

    .course-card .card-body { padding: 14px; }
    .course-card .card-title { font-size: 1rem; }
    .course-card .badge { font-size: .7rem; padding: 3px 8px; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 27) Partner XR design system (xr-card) — tighten padding on phones
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    .xr-card .card-body { padding: 12px; }
    .xr-card .card-header { padding: 10px 12px; }
    .xr-card .card-footer { padding: 10px 12px; }
    /* Partner-Manager avatar cap */
    .xr-card img.rounded-circle { max-width: 72px !important; height: auto !important; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 28) App header (top bar) — make search + icons usable on tiny screens
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    .app-header,
    .main-header { padding-left: 8px !important; padding-right: 8px !important; }
    .app-header .header-brand { transform: scale(.9); transform-origin: left center; }
    .navsearch .input-group { font-size: .85rem; }
    .navsearch .form-control { min-height: 36px; padding: .25rem .5rem; }

    /* Shrink header icon tray spacing */
    .header-right .dropdown > a,
    .header .nav-link { padding: .35rem .5rem !important; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 29) Chatbot / floating buttons — keep them reachable with one thumb
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    .chatbot-toggle,
    .floating-ai-btn,
    #chatbotToggle,
    [id*="chatbot-btn"] {
        bottom: 80px !important; /* avoid mobile quick-action bar overlap */
        right: 14px !important;
        width: 52px !important;
        height: 52px !important;
    }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 30) Ticket cards and status pill rows
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    .ticket-card,
    .ticket-row { flex-direction: column; align-items: flex-start !important; gap: 6px; }
    .ticket-card .badge,
    .ticket-row .badge { align-self: flex-start; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 31) Knowledge Base article — limit max-width of inline images and tables
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    .kb-article,
    .kb-content,
    .confluence-content {
        font-size: .95rem;
        line-height: 1.55;
    }
    .kb-article img,
    .kb-content img,
    .confluence-content img {
        max-width: 100% !important;
        height: auto !important;
    }
    .kb-article table,
    .kb-content table,
    .confluence-content table {
        display: block;
        overflow-x: auto;
        max-width: 100%;
    }
    .kb-article pre,
    .kb-content pre,
    .confluence-content pre {
        overflow-x: auto;
        font-size: .8rem;
    }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 32) Consulting / partner profile — avatar + details stack on xs
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    .profile-hero,
    .consulting-hero {
        flex-direction: column;
        text-align: center;
        gap: 12px;
    }
    .profile-hero .avatar,
    .consulting-hero .avatar,
    .profile-hero img,
    .consulting-hero img {
        margin: 0 auto;
    }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 33) Admin forms with many columns — stack gracefully
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    .card .row.g-3 > [class*="col-md-"] { width: 100% !important; }
    .card .row.g-3 > [class*="col-lg-"] { width: 100% !important; }

    /* File upload buttons and progress bars fit fully on screen */
    #uploadProgressContainer .progress { height: 22px; }
    #uploadProgressContainer small { font-size: .72rem; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 34) Pagination/breadcrumbs/alerts
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    .pagination { flex-wrap: wrap; gap: 4px; }
    .pagination .page-item .page-link { padding: 4px 10px; font-size: .82rem; }

    .alert { padding: 10px 12px; font-size: .9rem; }
    .alert .btn-close { padding: 10px; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 35) Tabs — allow horizontal scroll instead of wrapping weirdly
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 767.98px) {
    .nav-tabs,
    .nav-pills {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }
    .nav-tabs::-webkit-scrollbar,
    .nav-pills::-webkit-scrollbar { display: none; }
    .nav-tabs .nav-link,
    .nav-pills .nav-link { white-space: nowrap; flex-shrink: 0; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 36) Notifications, toasts and popovers
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    .toast, .notification { max-width: calc(100vw - 24px) !important; right: 12px !important; }
    .popover { max-width: calc(100vw - 24px); }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 37) Calendar event modal used on dashboards
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    #partnerCalendarModal .modal-body,
    #custCalEventModal .modal-body {
        padding: 16px;
    }
    #partnerCalendarModal .modal-title,
    #custCalEventModal .modal-title {
        font-size: 1.05rem;
    }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 38) Video Library card tiles — more compact on mobile
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    .video-tile,
    .news-tile,
    .latest-news-item {
        margin-bottom: 10px;
    }
    .video-tile img,
    .news-tile img {
        max-height: 160px;
        object-fit: cover;
        width: 100%;
    }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 39) Hero banners with inline gradient backgrounds (Make it yours /
 *     Getting Started) — consistent padding on xs
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    [style*="linear-gradient"][style*="border-radius"] {
        padding: 16px !important;
    }
    [style*="linear-gradient"][style*="border-radius"] h5,
    [style*="linear-gradient"][style*="border-radius"] .h5 {
        font-size: .95rem;
    }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 40) Login / auth pages — ensure the centered card is never wider than
 *     the viewport and the form is thumb-friendly
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 575.98px) {
    .login-card,
    .auth-card,
    .login-box {
        width: calc(100vw - 20px) !important;
        max-width: calc(100vw - 20px) !important;
        margin: 10px auto !important;
    }
    .login-card .card-body,
    .auth-card .card-body { padding: 18px; }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 41) Sidebar off-canvas polish (Bootstrap template)
 * ──────────────────────────────────────────────────────────────────────────*/

@media (max-width: 991.98px) {
    /* Prevent content from being hidden under the hamburger area */
    .app-content { padding-top: 6px; }
    /* Smooth backdrop darkening when sidebar opens */
    body.active .app-sidebar { box-shadow: 0 0 24px rgba(0,0,0,.25); }
}

/* ───────────────────────────────────────────────────────────────────────────
 * 16) Print: keep the portal usable when printed
 * ──────────────────────────────────────────────────────────────────────────*/

@media print {
    .app-sidebar,
    .app-header,
    .page-header .pageheader-btn,
    .no-print { display: none !important; }
    .app-content, .side-app, .main-container { padding: 0 !important; margin: 0 !important; }
    .card { border: 1px solid #ddd !important; box-shadow: none !important; page-break-inside: avoid; }
}
