/**
 * Ghiras Soft — how every screen fits the device in front of it.
 *
 * Widths are CSS pixels: the size a device reports to the page, not its
 * physical resolution.
 *
 *   phone upright          < 576     iPhone SE 375 · iPhone 15 393 · iPhone Pro Max 430 · Android 360–412
 *   phone on its side,     576–767   phones in landscape (640–932 wide, under 500 tall) · iPad mini 744
 *   iPad mini upright
 *   tablet upright         768–991   iPad / iPad Air 820 · iPad Pro 11" 834 · Android tablets 800
 *   tablet on its side     992–1199  iPad mini 1133 · iPad Air 1180 · iPad Pro 11" 1194 · iPad Pro 12.9" upright 1024
 *   laptop and desktop     ≥ 1200    1280 · 1366 · 1440 · 1920
 *
 * Below 992 the sidebar is a drawer. An upright tablet cannot spare a third of
 * its width to a menu that is open all the time, so it gets the phone's menu
 * button and slide-out drawer instead. responsive-touch.js uses the same line.
 */

/* ============================================================================
 * 1. Every device
 * ============================================================================ */
body {
    overflow-x: hidden;
    -webkit-tap-highlight-color: transparent;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

/* clip, where supported, stops sideways scroll without breaking sticky headers. */
@supports (overflow-x: clip) {
    body { overflow-x: clip; }
}

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

.fl-scrolls-hoverable,
.table-responsive,
.overflow-auto,
.overflow-x-auto {
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
}

/* ============================================================================
 * 2. The drawer menu — phones and upright tablets (< 992px)
 * ============================================================================ */
@media (max-width: 991.98px) {
    .page-content {
        width: 100% !important;
        overflow-x: hidden !important;
    }

    .content-wrapper {
        width: 100% !important;
        min-width: 0 !important;
    }

    .sidebar-main {
        position: fixed !important;
        top: 0 !important;
        bottom: 0 !important;
        width: 290px !important;
        max-width: 85vw !important;
        z-index: 1060 !important;
        transition: transform 0.3s cubic-bezier(0.25, 0.8, 0.25, 1), box-shadow 0.3s ease !important;
        box-shadow: none !important;
        overflow-y: auto !important;
        overscroll-behavior: contain;
    }

    [dir="rtl"] .sidebar-main {
        right: 0 !important;
        left: auto !important;
        transform: translateX(105%) !important;
    }

    [dir="ltr"] .sidebar-main {
        left: 0 !important;
        right: auto !important;
        transform: translateX(-105%) !important;
    }

    body.sidebar-mobile-main .sidebar-main,
    [dir="rtl"] body.sidebar-mobile-main .sidebar-main,
    [dir="ltr"] body.sidebar-mobile-main .sidebar-main {
        transform: translateX(0) !important;
        box-shadow: 0 0 35px rgba(0, 0, 0, 0.55) !important;
    }

    body.sidebar-mobile-main::after {
        content: '';
        position: fixed;
        inset: 0;
        background: rgba(10, 15, 20, 0.65);
        backdrop-filter: blur(4px);
        -webkit-backdrop-filter: blur(4px);
        z-index: 1055;
        cursor: pointer;
        animation: drawerBackdropIn 0.25s ease-out forwards;
    }

    body.sidebar-mobile-main {
        overflow: hidden !important;
    }

    @keyframes drawerBackdropIn {
        from { opacity: 0; }
        to { opacity: 1; }
    }

    .sidebar-mobile-toggler {
        display: flex !important;
        padding: 0.85rem 1rem !important;
        background: rgba(0, 0, 0, 0.15) !important;
        font-weight: 700 !important;
    }

    /* Touch-sized buttons in the top bar. */
    .top-navbar-glass > .d-flex.d-md-none a,
    .top-navbar-glass > .d-flex.d-md-none button {
        min-width: 44px !important;
        min-height: 44px !important;
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
        padding: 0.25rem !important;
        border-radius: 10px !important;
        margin: 0 2px !important;
    }
}

/* Upright tablets: Bootstrap shows the desktop top bar from 768px, so bring
 * back the phone's menu and account buttons, and fold the rest away. */
@media (min-width: 768px) and (max-width: 991.98px) {
    .top-navbar-glass.navbar-expand-md {
        flex-wrap: wrap;
    }

    .top-navbar-glass.navbar-expand-md > .d-flex.d-md-none {
        display: flex !important;
    }

    .top-navbar-glass.navbar-expand-md .navbar-collapse:not(.show):not(.collapsing) {
        display: none !important;
    }

    .top-navbar-glass.navbar-expand-md .navbar-collapse {
        flex-basis: 100%;
    }

    /* The desktop button that shrinks a docked sidebar has nothing to shrink. */
    .top-navbar-glass .sidebar-main-toggle {
        display: none !important;
    }
}

/* ============================================================================
 * 3. Phones (< 768px)
 * ============================================================================ */
@media (max-width: 767.98px) {
    .page-content {
        flex-direction: column !important;
    }

    .content-wrapper {
        padding: 0 !important;
    }

    .content {
        padding: 0.75rem 0.65rem !important;
    }

    /* A card header with a badge or buttons beside its title squeezed the title
       into a one-word-per-line column (the driver's map). Let the side part drop
       under the title, and let long badges wrap instead of running off. */
    .card-header.header-elements-inline {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .card-header.header-elements-inline > .card-title {
        flex: 1 1 12rem;
        min-width: 0;
    }

    .card-header .header-elements .badge {
        white-space: normal;
        text-align: start;
        line-height: 1.5;
    }

    .navbar.top-navbar-glass {
        padding: 0.5rem 0.75rem !important;
        min-height: 56px;
    }

    .navbar .brand-logo-badge {
        width: 36px !important;
        height: 36px !important;
    }

    .navbar .brand-logo-badge i {
        font-size: 20px !important;
    }

    .navbar .d-flex.d-md-none button.navbar-toggler {
        background: rgba(255, 255, 255, 0.08) !important;
        border: 1px solid rgba(255, 255, 255, 0.12) !important;
    }

    .dashboard-hero-banner {
        padding: 1.15rem 1rem !important;
        border-radius: 16px !important;
        margin-bottom: 1rem !important;
    }

    .dashboard-hero-banner .hero-avatar-ring {
        width: 52px !important;
        height: 52px !important;
    }

    .dashboard-hero-banner h3 {
        font-size: 1.15rem !important;
    }

    .quick-action-grid {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
        margin-top: 1rem !important;
    }

    .quick-action-card {
        min-height: 52px !important;
        padding: 8px 12px !important;
    }

    .quick-action-title {
        font-size: 0.85rem !important;
    }

    .stat-card-glass {
        margin-bottom: 0.75rem !important;
    }

    .card .table {
        margin-bottom: 0;
    }

    .table-responsive,
    .dataTables_wrapper {
        width: 100% !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch !important;
        border-radius: 12px;
    }

    .table th,
    .table td {
        white-space: nowrap !important;
        padding: 0.65rem 0.75rem !important;
        font-size: 0.82rem !important;
    }

    .dataTables_filter,
    .dataTables_length,
    .dt-buttons {
        float: none !important;
        text-align: right !important;
        width: 100% !important;
        margin-bottom: 0.5rem !important;
    }

    .dataTables_filter input {
        width: 100% !important;
        margin: 0 !important;
    }

    .dt-buttons {
        display: flex !important;
        flex-wrap: wrap !important;
        gap: 4px !important;
    }

    .dt-button {
        flex: 1 1 auto !important;
        text-align: center !important;
    }

    /* 16px stops iOS Safari zooming the page when a field is tapped. */
    .form-control,
    .custom-select,
    .select2-selection {
        font-size: 16px !important;
        min-height: 44px !important;
        border-radius: 10px !important;
    }

    input.form-control,
    select.form-control,
    .custom-select {
        height: 44px !important;
    }

    .input-group-text {
        height: 44px !important;
    }

    .btn:not(.btn-sm):not(.btn-xs) {
        min-height: 44px !important;
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
        font-size: 0.9rem !important;
        padding: 0.5rem 1rem !important;
        border-radius: 10px !important;
    }

    /* Header tabs scroll sideways instead of wrapping into a stack. */
    .nav-tabs {
        flex-wrap: nowrap !important;
        overflow-x: auto;
        overflow-y: hidden;
        scrollbar-width: none;
    }

    .nav-tabs .nav-link {
        white-space: nowrap;
    }

    /* Dialogs rise from the bottom, within thumb reach. */
    .modal-dialog {
        margin: 0 !important;
        position: fixed !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        max-width: 100% !important;
        width: 100% !important;
        transform: translateY(100%) !important;
        transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1) !important;
    }

    .modal.show .modal-dialog {
        transform: translateY(0) !important;
    }

    .modal-content {
        border-bottom-left-radius: 0 !important;
        border-bottom-right-radius: 0 !important;
        border-top-left-radius: 22px !important;
        border-top-right-radius: 22px !important;
        max-height: 85vh !important;
        overflow-y: auto !important;
        box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.35) !important;
    }

    .modal-content::before {
        content: '';
        display: block;
        width: 44px;
        height: 4px;
        background: rgba(150, 150, 150, 0.4);
        border-radius: 4px;
        margin: 10px auto 4px auto;
    }
}

/* The smallest phones still sold or carried: iPhone SE (1st gen) 320, Galaxy Fold closed 280. */
@media (max-width: 374.98px) {
    .content {
        padding: 0.6rem 0.45rem !important;
    }

    .card-body {
        padding: 0.85rem !important;
    }

    .dashboard-hero-banner h3 {
        font-size: 1.05rem !important;
    }
}

/* Phones on their side: short screens, so the chrome gets out of the way. */
@media (max-width: 991.98px) and (max-height: 500px) and (orientation: landscape) {
    .navbar.top-navbar-glass {
        min-height: 48px !important;
        padding-top: 0.25rem !important;
        padding-bottom: 0.25rem !important;
    }

    .modal-content {
        max-height: 92vh !important;
    }
}

/* ============================================================================
 * 4. Tablets, upright and on their side (768px – 1199px)
 * ============================================================================ */
@media (min-width: 768px) and (max-width: 1199.98px) {
    .content {
        padding: 1.25rem 1rem !important;
    }

    .table th,
    .table td {
        padding: 0.65rem 0.85rem !important;
        font-size: 0.88rem !important;
    }

    .table-responsive {
        overflow-x: auto !important;
    }

    .bubble-scanner-container {
        display: flex !important;
        flex-direction: column !important;
        gap: 15px !important;
    }
}

@media (min-width: 768px) and (max-width: 991.98px) {
    .quick-action-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px !important;
    }

    .stat-cards-row > div[class*="col-"] {
        flex: 0 0 50% !important;
        max-width: 50% !important;
    }

    #fleet-live-map {
        height: 60vh !important;
        min-height: 480px !important;
    }
}

/* A tablet on its side docks the sidebar, leaving the content about 900px:
 * the greeting and three columns of shortcuts do not fit beside each other. */
@media (min-width: 992px) and (max-width: 1199.98px) {
    .quick-action-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 12px !important;
    }

    #fleet-live-map {
        height: 560px !important;
    }
}

/* ============================================================================
 * 5. Laptops and desktops (≥ 1200px)
 * ============================================================================ */
@media (min-width: 1200px) {
    .content {
        padding: 1.5rem 1.75rem !important;
        max-width: 1800px;
        margin: 0 auto;
    }

    .quick-action-grid {
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 12px !important;
    }

    #fleet-live-map {
        height: 680px !important;
    }
}

@media (min-width: 1400px) {
    .quick-action-grid {
        grid-template-columns: repeat(4, 1fr) !important;
    }
}

/* ============================================================================
 * 6. Any touch screen, whatever its width — iPads included
 * ============================================================================ */
@media (hover: none) and (pointer: coarse) {
    /* iPad Safari zooms small fields exactly as a phone does. */
    .form-control,
    .custom-select,
    .select2-selection,
    .dataTables_filter input {
        font-size: 16px !important;
    }

    .btn:not(.btn-sm):not(.btn-xs),
    .dropdown-item {
        min-height: 44px;
    }

    .dropdown-item {
        display: flex;
        align-items: center;
    }
}

/* ============================================================================
 * 7. The notch and the home indicator (viewport-fit=cover in the layouts)
 * ============================================================================ */
@supports (padding: max(0px)) {
    /* Never less than the bar's own padding: the collapsed menu inside it is
     * pulled out by negative margins sized to that padding, and any less
     * pushes it past the edge of the screen. */
    .navbar.top-navbar-glass {
        padding-left: max(1.25rem, env(safe-area-inset-left)) !important;
        padding-right: max(1.25rem, env(safe-area-inset-right)) !important;
    }

    @media (max-width: 767.98px) {
        .navbar.top-navbar-glass {
            padding-left: max(0.75rem, env(safe-area-inset-left)) !important;
            padding-right: max(0.75rem, env(safe-area-inset-right)) !important;
        }
    }

    .sidebar-main {
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
    }

    @media (max-width: 767.98px) {
        .content {
            padding-left: max(0.65rem, env(safe-area-inset-left)) !important;
            padding-right: max(0.65rem, env(safe-area-inset-right)) !important;
            padding-bottom: max(0.75rem, env(safe-area-inset-bottom)) !important;
        }

        .modal-content {
            padding-bottom: env(safe-area-inset-bottom);
        }

        .map-floating-overlay {
            bottom: max(10px, env(safe-area-inset-bottom)) !important;
        }
    }
}

/* ============================================================================
 * 8. Screens with their own layout
 * ============================================================================ */

/* ---- Login ---- */
@media (max-width: 991.98px) {
    .login-cover {
        padding: 1.5rem 0.5rem !important;
        min-height: 100vh !important;
    }

    .login-cover .row {
        flex-direction: column !important;
    }

    .login-cover .col-lg-6 {
        text-align: center !important;
        margin-bottom: 1.5rem !important;
    }

    .login-cover .col-lg-6 .brand-hero-badge {
        display: inline-flex !important;
        margin: 0 auto 0.75rem auto !important;
    }

    .login-cover .col-lg-6 h1 {
        font-size: 1.85rem !important;
    }

    .login-cover .col-lg-6 p.lead {
        font-size: 0.95rem !important;
        margin-bottom: 1rem !important;
    }

    .login-glass-card {
        border-radius: 20px !important;
        box-shadow: 0 15px 35px -10px rgba(7, 130, 92, 0.12), 0 2px 8px rgba(0, 0, 0, 0.04) !important;
    }

    .login-glass-card .card-body {
        padding: 1.5rem 1.25rem !important;
    }
}

/* 100vh on iOS includes the address bar; dvh is the height actually visible. */
@supports (min-height: 100dvh) {
    @media (max-width: 991.98px) {
        .login-cover {
            min-height: 100dvh !important;
        }
    }
}

/* ---- Bubble sheet camera scanner ---- */
.scanner-viewport-wrap {
    transition: all 0.3s ease;
}

@media (max-width: 767.98px) {
    .scanner-viewport-wrap {
        border-radius: 12px !important;
        aspect-ratio: 1 / 1 !important;
        max-width: 100% !important;
        margin: 0 auto 12px auto !important;
    }

    .scanner-frame {
        width: 85% !important;
        height: 85% !important;
    }

    .scanner-controls-mobile {
        display: flex !important;
        gap: 8px !important;
        width: 100% !important;
        justify-content: center !important;
        margin-top: 10px !important;
    }

    .scanner-controls-mobile .btn {
        flex: 1 !important;
    }

    .result-card-box {
        padding: 18px 16px !important;
        border-radius: 18px !important;
    }

    .score-circle {
        width: 76px !important;
        height: 76px !important;
    }
}

/* ---- Live bus tracking ---- */
@media (max-width: 767.98px) {
    #fleet-live-map {
        height: 55vh !important;
        min-height: 380px !important;
        border-radius: 14px !important;
    }

    .map-floating-overlay {
        position: fixed !important;
        bottom: 10px !important;
        left: 10px !important;
        right: 10px !important;
        max-width: none !important;
        width: auto !important;
        z-index: 1050 !important;
        border-radius: 16px !important;
        box-shadow: 0 8px 30px rgba(0, 0, 0, 0.45) !important;
    }
}

/* ---- Attendance and marks ---- */
@media (max-width: 767.98px) {
    .attendance-btn-group {
        display: flex !important;
        width: 100% !important;
        gap: 4px !important;
    }

    .attendance-btn-group .btn {
        flex: 1 !important;
        padding: 0.4rem 0.2rem !important;
        font-size: 0.75rem !important;
    }

    .student-attendance-card {
        border-radius: 14px !important;
        padding: 10px 12px !important;
        margin-bottom: 8px !important;
    }
}

/* ============================================================================
 * 9. People who asked for less motion, and paper
 * ============================================================================ */
@media (prefers-reduced-motion: reduce) {
    .sidebar-main,
    .modal-dialog,
    body.sidebar-mobile-main::after {
        transition: none !important;
        animation: none !important;
    }
}

@media print {
    body.sidebar-mobile-main::after {
        display: none !important;
    }
}
