/**
 * Mobile-first responsive enhancements (AFRIPAYCBT)
 * Loaded after app.css — breakpoint lg: 991px, md: 768px, sm: 480px
 */

/* Touch-friendly controls */
@media (max-width: 991px) {
    .btn {
        min-height: 44px;
    }
    .btn-sm,
    .btn-xs {
        min-height: 36px;
        padding: 0.4rem 0.75rem;
    }
    .sidebar-link {
        min-height: 44px;
        padding: 0.65rem 1rem;
    }
    .palette-btn {
        width: 40px;
        height: 40px;
    }
    .option-card {
        padding: 1rem;
        min-height: 48px;
    }
}

/* ——— App shell: off-canvas sidebar ——— */
.menu-toggle {
    display: none;
    flex-direction: column;
    justify-content: center;
    gap: 5px;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    cursor: pointer;
    flex-shrink: 0;
}
.menu-toggle-bar {
    display: block;
    width: 18px;
    height: 2px;
    margin: 0 auto;
    background: var(--text);
    border-radius: 1px;
    transition: transform 0.2s, opacity 0.2s;
}
.menu-toggle[aria-expanded="true"] .menu-toggle-bar:nth-child(1) {
    transform: translateY(7px) rotate(45deg);
}
.menu-toggle[aria-expanded="true"] .menu-toggle-bar:nth-child(2) {
    opacity: 0;
}
.menu-toggle[aria-expanded="true"] .menu-toggle-bar:nth-child(3) {
    transform: translateY(-7px) rotate(-45deg);
}

.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1090;
    background: rgba(15, 20, 25, 0.55);
    opacity: 0;
    transition: opacity 0.25s ease;
    border: none;
    padding: 0;
    cursor: pointer;
}
.sidebar-backdrop.is-visible {
    display: block;
    opacity: 1;
}

body.nav-open,
body.public-nav-open {
    overflow: hidden;
    touch-action: none;
}

.topbar-start {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    flex: 1;
    min-width: 0;
}

@media (max-width: 991px) {
    .menu-toggle {
        display: inline-flex;
    }

    .app-shell {
        flex-direction: row;
    }

    .sidebar {
        position: fixed;
        top: max(1rem, env(safe-area-inset-top));
        left: 50%;
        bottom: auto;
        width: min(320px, calc(100vw - 2rem));
        max-height: calc(100dvh - max(1rem, env(safe-area-inset-top)) - max(1rem, env(safe-area-inset-bottom)));
        z-index: 1100;
        transform: translateX(-50%) translateY(-12px) scale(0.97);
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
        transition: transform 0.25s ease, opacity 0.25s ease, visibility 0.25s;
        box-shadow: 0 16px 48px rgba(0, 0, 0, 0.28);
        border-radius: var(--radius);
        min-height: auto;
        overflow: hidden;
        display: flex;
        flex-direction: column;
    }

    .sidebar.is-open {
        transform: translateX(-50%) translateY(0) scale(1);
        opacity: 1;
        visibility: visible;
        pointer-events: auto;
    }

    .sidebar-nav {
        display: block;
        padding: 0.5rem 0 1rem;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        flex: 1;
        min-height: 0;
    }

    .sidebar-group {
        width: 100%;
        margin-bottom: 0.35rem;
    }

    .sidebar-group-label {
        display: block;
    }

    .sidebar-link {
        margin: 0.1rem 0.5rem;
    }

    .main-content {
        width: 100%;
        min-width: 0;
    }

    .topbar {
        position: sticky;
        top: 0;
        z-index: 900;
        flex-wrap: wrap;
        gap: 0.5rem;
        padding: 0.65rem 1rem;
        padding-top: max(0.65rem, env(safe-area-inset-top));
    }

    .topbar h1 {
        font-size: 1.05rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        flex: 1;
        min-width: 0;
    }

    .topbar-user {
        width: 100%;
        flex-wrap: wrap;
        justify-content: flex-end;
        gap: 0.4rem 0.65rem;
        font-size: 0.8rem;
    }

    .topbar-user-name {
        display: none;
    }

    .institution-switcher {
        width: 100%;
        margin-right: 0 !important;
    }

    .institution-switcher select {
        width: 100%;
        max-width: 100%;
    }

    .page-body {
        padding: 1rem;
        padding-bottom: max(1rem, env(safe-area-inset-bottom));
    }

    .stats-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.75rem;
    }

    .stat-card {
        padding: 1rem;
    }

    .stat-card h3 {
        font-size: 1.5rem;
    }

    .dash-hero {
        padding: 1.15rem 1.25rem;
        margin-bottom: 1.25rem;
    }

    .dash-hero h2 {
        font-size: 1.15rem;
    }

    .dash-quick-actions {
        width: 100%;
    }

    .dash-quick-actions .btn {
        flex: 1;
        min-width: calc(50% - 0.25rem);
    }

    .dash-nav-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.5rem;
    }

    .dash-nav-card-desc {
        -webkit-line-clamp: 1;
    }

    .exam-cards-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.65rem;
    }

    .exam-card {
        padding: 1rem;
    }

    .card-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.75rem;
        padding: 1rem;
    }

    .card-body {
        padding: 1rem;
    }

    .data-table {
        min-width: 560px;
        font-size: 0.85rem;
    }

    .data-table th,
    .data-table td {
        padding: 0.6rem 0.75rem;
    }

    .form-row-3 {
        grid-template-columns: 1fr;
    }

    .grid-2 {
        grid-template-columns: 1fr;
    }

    .tabs {
        overflow-x: auto;
        flex-wrap: nowrap;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 0.25rem;
        margin-bottom: 1rem;
    }

    .tabs a {
        flex-shrink: 0;
    }

    .filter-bar,
    .filter-inline {
        width: 100%;
    }

    .filter-bar select,
    .filter-inline select {
        flex: 1;
        min-width: 0;
    }

    .actions {
        width: 100%;
    }

    .inline-form {
        display: block;
        margin-top: 0.35rem;
    }

    .detail-grid {
        grid-template-columns: 1fr 1fr;
    }

    .toast-container {
        left: 1rem;
        right: 1rem;
        top: max(1rem, env(safe-area-inset-top));
    }

    .allocate-banner {
        padding: 0.65rem 1rem;
        font-size: 0.82rem;
    }

    /* Forms — single column, iOS-friendly input size */
    .form-row {
        grid-template-columns: 1fr;
    }

    .form-group input,
    .form-group select,
    .form-group textarea,
    .filter-bar input,
    .filter-bar select,
    .filter-inline input,
    .filter-inline select,
    .institution-switcher select {
        font-size: 16px;
    }

    /* Scrollable tables inside cards */
    .card-body:has(.data-table),
    .card-body-flush {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .breadcrumbs ol {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 0.2rem;
    }

    .breadcrumbs li {
        flex-shrink: 0;
        white-space: nowrap;
    }

    .pagination {
        justify-content: center;
        flex-wrap: wrap;
        padding: 0.75rem 1rem;
    }

    .pagination-meta {
        width: 100%;
        text-align: center;
        margin-left: 0;
        margin-top: 0.35rem;
    }

    .card-header {
        gap: 0.5rem;
    }

    .card-header > .btn,
    .card-header > .actions,
    .card-header > form,
    .dash-section-head > .btn,
    .dash-section-head > a.btn {
        width: 100%;
        justify-content: center;
    }

    .card-header .actions {
        display: flex;
        flex-direction: column;
        gap: 0.35rem;
        width: 100%;
    }

    .filter-bar,
    .filter-inline {
        flex-direction: column;
        align-items: stretch;
    }

    .maintenance-banner a {
        margin-left: 0;
        flex: 1 1 100%;
        text-align: center;
    }

    .empty-state {
        padding: 2rem 1rem;
        font-size: 0.9rem;
    }

    .dash-hero {
        flex-direction: column;
    }

    .dash-quick-actions {
        justify-content: stretch;
    }

    .hint,
    .form-required-legend {
        font-size: 0.82rem;
    }
}

@media (max-width: 480px) {
    /* Keep card grids at 2 columns on small phones — compact sizing */
    .dash-nav-grid,
    .stats-grid,
    .dash-health-grid,
    .exam-cards-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.4rem;
    }

    /* Home sections (not hero): stay 2 columns on small phones */
    .home-actions-grid,
    .portal-grid,
    .home-features {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.5rem;
    }

    .home-action-card,
    .portal-card {
        padding: 0.85rem;
    }

    .home-action-card h3,
    .portal-card h3 {
        font-size: 0.82rem;
        line-height: 1.25;
    }

    .home-action-card p,
    .portal-card p {
        font-size: 0.72rem;
        margin-bottom: 0.65rem;
        line-height: 1.35;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    .home-action-card .btn {
        font-size: 0.72rem;
        padding: 0.4rem 0.5rem;
        min-height: 36px;
    }

    .portal-card .portal-icon {
        font-size: 1.35rem;
        margin-bottom: 0.35rem;
    }

    .portal-card .badge {
        font-size: 0.65rem;
        padding: 0.15rem 0.35rem;
    }

    .home-feature {
        padding: 0.6rem 0.35rem;
    }

    .home-feature strong {
        font-size: 0.78rem;
    }

    .home-feature span {
        font-size: 0.68rem;
        line-height: 1.3;
    }

    .home-section-title {
        font-size: 1.15rem;
    }

    .dash-nav-card {
        padding: 0.55rem 0.5rem;
        min-height: 3.75rem;
    }

    .dash-nav-card-icon {
        width: 1.5rem;
        height: 1.5rem;
    }

    .dash-nav-card-icon svg {
        width: 0.85rem;
        height: 0.85rem;
    }

    .dash-nav-card-title {
        font-size: 0.72rem;
    }

    .dash-nav-card-desc {
        font-size: 0.65rem;
        -webkit-line-clamp: 1;
    }

    .dash-nav-card-badge {
        font-size: 0.62rem;
        padding: 0.1rem 0.3rem;
    }

    .stat-card {
        padding: 0.75rem 0.65rem;
    }

    .stat-card h3 {
        font-size: 1.25rem;
    }

    .stat-card p {
        font-size: 0.78rem;
    }

    .detail-grid {
        grid-template-columns: 1fr 1fr;
    }
}

/* Auth pages */
@media (max-width: 768px) {
    .auth-page {
        grid-template-columns: 1fr;
        min-height: 100vh;
        min-height: 100dvh;
    }

    .auth-brand {
        display: none;
    }

    .auth-form-wrap {
        padding: 1.25rem 1rem;
        min-height: 100vh;
        min-height: 100dvh;
        align-items: flex-start;
        padding-top: max(1.25rem, env(safe-area-inset-top));
        padding-bottom: max(1.25rem, env(safe-area-inset-bottom));
    }

    .auth-home-link {
        margin-bottom: 1.25rem;
        padding-bottom: 0.85rem;
    }

    .auth-form h2 {
        font-size: 1.45rem;
    }

    .auth-form .form-row {
        grid-template-columns: 1fr;
    }

    .auth-form .subtitle {
        margin-bottom: 1.25rem;
    }

    .form-required-legend {
        margin: -0.75rem 0 1.25rem;
    }
}

/* Public site mobile menu */
.public-menu-toggle {
    display: none;
    flex-direction: column;
    justify-content: center;
    gap: 5px;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    cursor: pointer;
    flex-shrink: 0;
    order: 2;
}
.public-menu-toggle .menu-toggle-bar {
    display: block;
    width: 18px;
    height: 2px;
    margin: 0 auto;
    background: var(--text);
    border-radius: 1px;
}

.public-nav-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 199;
    background: rgba(15, 20, 25, 0.5);
    border: none;
    padding: 0;
    cursor: pointer;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.25s ease, visibility 0.25s;
}
.public-nav-backdrop.is-visible {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

@media (max-width: 768px) {
    .public-menu-toggle {
        display: inline-flex;
    }

    .public-header-inner {
        flex-wrap: nowrap;
        padding: 0.65rem 1rem;
        padding-top: max(0.65rem, env(safe-area-inset-top));
    }

    .public-header-menu {
        display: flex;
        align-items: center;
        flex-shrink: 0;
        gap: 0.35rem;
    }

    .public-brand {
        flex: 1;
        min-width: 0;
    }

    .public-brand strong {
        font-size: 1rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .public-nav {
        visibility: hidden;
        pointer-events: none;
        position: fixed;
        top: max(1rem, env(safe-area-inset-top));
        left: 50%;
        right: auto;
        bottom: auto;
        width: min(320px, calc(100vw - 2rem));
        max-height: calc(100dvh - max(1rem, env(safe-area-inset-top)) - max(1rem, env(safe-area-inset-bottom)));
        display: flex;
        flex-direction: column;
        align-items: stretch;
        gap: 0;
        padding: 1rem 1rem 1.5rem;
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: var(--radius);
        box-shadow: 0 16px 48px rgba(0, 0, 0, 0.18);
        z-index: 200;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        transform: translateX(-50%) translateY(-12px) scale(0.97);
        opacity: 0;
        transition: transform 0.25s ease, opacity 0.25s ease, visibility 0.25s;
    }

    .public-nav.is-open {
        visibility: visible;
        pointer-events: auto;
        transform: translateX(-50%) translateY(0) scale(1);
        opacity: 1;
    }

    .public-nav a {
        padding: 0.85rem 0.75rem;
        border-bottom: 1px solid var(--border);
        font-size: 1rem;
        min-height: 48px;
        display: flex;
        align-items: center;
    }

    .public-nav a.btn {
        justify-content: center;
        margin-top: 0.5rem;
        border-bottom: none;
    }

    .account-user-menu,
    .public-user-menu {
        order: 0;
        z-index: 210;
    }

    .public-header-menu .public-menu-toggle {
        order: 1;
        flex-shrink: 0;
    }

    .public-header-menu .theme-toggle {
        order: 2;
        flex-shrink: 0;
    }

    .public-user-dropdown {
        right: 0;
        left: auto;
        max-width: min(280px, calc(100vw - 1.5rem));
    }

    .public-container {
        padding-left: 1rem;
        padding-right: 1rem;
    }

    .public-header-inner {
        padding-left: 1rem;
        padding-right: 1rem;
    }

    .home-hero {
        padding: 2.25rem 0 2.75rem;
    }

    .home-hero-actions {
        justify-content: flex-start;
    }

    .home-hero-actions .home-hero-btn {
        flex: 1 1 calc(50% - 0.35rem);
        min-width: 0;
        white-space: normal;
        text-align: center;
    }

    .home-section {
        padding: 2rem 0;
    }

    .home-section-features {
        padding: 2rem 0;
    }

    /* Home sections below hero: 2 cards per row */
    .home-actions-grid,
    .portal-grid,
    .home-features {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.65rem;
        max-width: none;
    }

    .portals-page .portal-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .home-hero h1 {
        font-size: 1.65rem;
        max-width: none;
    }

    .home-hero-lead {
        font-size: 0.95rem;
    }

    .home-action-card {
        padding: 1.15rem;
    }

    .home-action-card .btn {
        width: 100%;
    }

    .portal-header .public-header-inner {
        flex-wrap: nowrap;
    }

    .portal-header .public-header-menu {
        display: flex;
        align-items: center;
        gap: 0.35rem;
        flex-shrink: 0;
    }

    .public-footer {
        padding: 1.5rem 1rem;
        padding-bottom: max(1.5rem, env(safe-area-inset-bottom));
    }

    .public-footer-links {
        flex-direction: column;
        gap: 0.5rem;
    }
}

/* Exam interface */
@media (max-width: 768px) {
    .exam-instructions-page {
        padding: 1rem;
        align-items: flex-start;
    }

    .exam-intro-card {
        padding: 1.25rem;
        margin: 1rem auto !important;
    }

    .exam-topbar {
        flex-wrap: wrap;
        padding: 0.65rem 1rem;
        gap: 0.5rem;
    }

    .exam-title-block {
        flex: 1 1 100%;
        order: 1;
    }

    .exam-timer {
        order: 2;
        flex: 1;
    }

    #submitExamBtn {
        order: 3;
        width: 100%;
        min-height: 44px;
    }

    .exam-body {
        flex-direction: column;
        max-height: none;
    }

    .question-palette {
        position: sticky;
        top: 0;
        z-index: 50;
        width: 100%;
        flex-direction: row;
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        border-right: none;
        border-bottom: 1px solid var(--border);
        padding: 0.5rem;
        gap: 0.35rem;
    }

    .exam-questions {
        padding: 1rem;
    }

    .q-nav {
        flex-wrap: wrap;
    }

    .q-nav .btn {
        flex: 1;
        min-width: calc(50% - 0.375rem);
    }

    .result-hero {
        padding: 1.5rem 1rem;
    }

    .score-pct {
        font-size: 2.25rem;
    }

    .score-details {
        flex-direction: column;
        gap: 0.5rem;
    }
}

@media (max-width: 768px) {
    .public-page-content {
        padding-top: 1.5rem;
        padding-bottom: 2rem;
    }

    .apply-page {
        min-height: auto;
    }

    .apply-card {
        padding: 1rem;
    }

    .grid-apply {
        grid-template-columns: 1fr;
    }

    .apply-layout .card-header {
        flex-direction: column;
        align-items: flex-start;
    }
}

@media (max-width: 768px) {
    .portal-hero {
        padding: 2rem 0 2.5rem;
    }

    .portal-hero h1 {
        font-size: 1.65rem;
    }

    .portal-hero-actions {
        flex-direction: column;
        align-items: stretch;
    }

    .portal-hero-actions .btn-portal-primary,
    .portal-hero-actions .btn-portal-outline {
        width: 100%;
        text-align: center;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .portal-steps {
        grid-template-columns: 1fr;
    }

    .portal-section {
        padding: 1.75rem 0;
    }

    .apply-hero {
        text-align: left;
    }

    .apply-hero h1 {
        font-size: 1.35rem;
    }

    .apply-footer {
        font-size: 0.85rem;
        line-height: 1.6;
    }

    .success-card {
        padding: 1.5rem 1rem;
    }

    .ref-box strong {
        font-size: 1.2rem;
        word-break: break-all;
    }
}

/* Standalone pages (PIN / instructions without app shell) */
@media (max-width: 768px) {
    body:not(.public-site) > .exam-instructions-page,
    body:not(.public-site) > .apply-page {
        padding-left: 1rem;
        padding-right: 1rem;
    }

    .exam-instructions-page .btn,
    .exam-intro-card .btn {
        width: 100%;
        margin-left: 0 !important;
        margin-top: 0.5rem;
    }

    .exam-instructions-page form .btn + .btn,
    .exam-intro-card form .btn + .btn {
        margin-top: 0.5rem;
    }

    .profile-avatar-img,
    .profile-avatar-placeholder {
        width: 72px;
        height: 72px;
        font-size: 1.35rem;
    }

    .student-name-cell {
        flex-wrap: wrap;
    }

    .review-q-head {
        flex-wrap: wrap;
    }

    .review-opt {
        font-size: 0.88rem;
    }
}

/* Print — unchanged on mobile */
@media print {
    .menu-toggle,
    .sidebar-backdrop,
    .public-menu-toggle,
    .public-nav-backdrop {
        display: none !important;
    }
}
