/* ============================================================
   MOBILE STYLES — ca-foundation-app
   Loaded after styles.css; overrides apply at ≤768px / ≤480px
   ============================================================ */

/* Prevent horizontal overflow globally.
   overflow-x: clip (unlike hidden) does NOT create a scroll container,
   so child elements like the filter pills can still scroll independently. */
html, body {
    overflow-x: clip;
    max-width: 100%;
}

@media (max-width: 768px) {

    /* ── Sidebar: hide entirely on mobile ── */
    .sidebar {
        display: none;
    }

    /* ── Mobile top bar ── */
    .mobile-topbar:not(.hidden) {
        display: flex;
        align-items: center;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: 52px;
        background: rgba(255, 255, 255, 0.98);
        backdrop-filter: blur(10px);
        border-bottom: 1px solid var(--border);
        padding: 0 1rem;
        z-index: 1000;
    }

    .mobile-topbar-brand {
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .mobile-topbar-logo {
        height: 32px;
        width: auto;
        object-fit: contain;
    }

    .mobile-topbar-name {
        font-family: 'Crimson Pro', serif;
        font-size: 1.2rem;
        font-weight: 600;
        color: var(--accent);
    }

    .main-content,
    .main-content.expanded {
        margin-left: 0 !important;
        padding-top: 52px;    /* push content below mobile top bar */
        padding-bottom: 70px; /* room for bottom nav */
        max-width: 100vw;
        /* Do NOT set overflow-x: hidden here — it breaks child scroll containers (filter pills) */
    }

    /* ── Bottom navigation bar ── */
    .bottom-nav {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 60px;
        background: rgba(255, 255, 255, 0.98);
        backdrop-filter: blur(10px);
        border-top: 1px solid var(--border);
        z-index: 1000;
        align-items: stretch;
    }

    .bottom-nav-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 3px;
        color: var(--text-light);
        text-decoration: none;
        cursor: pointer;
        font-size: 0.62rem;
        font-family: 'JetBrains Mono', monospace;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        transition: color 0.2s;
        border: none;
        background: none;
        padding: 0;
    }

    .bottom-nav-item svg {
        width: 20px;
        height: 20px;
        stroke-width: 1.8px;
    }

    .bottom-nav-item.active {
        color: var(--accent-dark);
    }

    .bottom-nav-item:hover {
        color: var(--accent-dark);
    }

    /* ── Container ── */
    .container {
        padding: 1.25rem 0.9rem;
    }

    /* ── Page header ── */
    .page-header {
        margin-bottom: 1rem;
        padding-bottom: 0.75rem;
    }

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

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

    /* ── View controls: stack filters + hide grid/list toggle ── */
    .view-controls {
        flex-direction: column;
        gap: 0.5rem;
        margin-bottom: 1rem;
    }

    .view-toggle {
        display: none; /* force list on mobile, no toggle needed */
    }

    /* ── Part filter pills: horizontal scroll ── */
    .part-filters {
        flex-wrap: nowrap;
        overflow-x: scroll; /* scroll (not auto) is more reliable on Android */
        -webkit-overflow-scrolling: touch;
        padding-bottom: 4px;
        gap: 0.4rem;
        scrollbar-width: none;
        width: 100%; /* must be constrained to trigger overflow */
    }

    .part-filters::-webkit-scrollbar {
        display: none;
    }

    .part-filter {
        white-space: nowrap;
        flex-shrink: 0;
        padding: 0.35rem 0.75rem;
        font-size: 0.72rem;
    }

    /* ── Force list view on mobile ── */
    .chapters-grid.active,
    .chapters-grid {
        display: none !important;
    }

    .chapters-list,
    .chapters-list.inactive {
        display: flex !important;
        flex-direction: column;
        gap: 0;
    }

    /* ── Compact chapter list items: stacked layout ── */
    .chapter-list-item {
        padding: 0.75rem 1rem !important;
        border-radius: 0 !important;
        border-left: none !important;
        border-right: none !important;
        border-top: none !important;
        transform: none !important;
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 0.3rem !important;
        width: 100% !important;
        box-sizing: border-box !important;
    }

    .chapter-list-item:first-child {
        border-top: 1px solid var(--border);
    }

    .chapter-list-item:hover {
        transform: none !important;
        box-shadow: none;
        background: rgba(199, 169, 107, 0.05);
    }

    .chapter-list-info {
        width: 100%;
    }

    .chapter-list-title {
        font-size: 1rem;
        margin-bottom: 0.1rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        width: 100%;
    }

    .chapter-list-part {
        font-size: 0.68rem;
    }

    /* Stats row below the title — compact, hide verbose labels */
    .chapter-list-stats {
        display: flex;
        gap: 0.75rem;
        flex-shrink: 0;
        font-size: 0.8rem;
    }

    /* Hide "solved" and "accuracy" text labels, keep numbers only */
    .chapter-list-stats .stat-label {
        display: none;
    }

    .chapter-list-stats .stat {
        display: flex;
        align-items: center;
        gap: 0.2rem;
        font-size: 0.78rem;
    }

    /* Add "/" prefix back visually for the question count stat */
    .chapter-list-stats .stat:first-child::before {
        content: 'Q: ';
        color: var(--text-light);
        font-size: 0.72rem;
    }

    .chapter-list-stats .stat:last-child::after {
        content: ' acc';
        color: var(--text-light);
        font-size: 0.72rem;
    }

    /* ── Modal footer: stack buttons on mobile ── */
    .modal-footer {
        flex-direction: column-reverse;
        gap: 0.5rem;
    }

    .modal-footer .btn {
        width: 100%;
        padding: 0.75rem 1rem;
        font-size: 0.85rem;
    }

    .modal-footer .btn-outline {
        width: 100%;
        padding: 0.75rem 1rem;
        font-size: 0.85rem;
    }

    .modal-footer .btn-secondary {
        /* Cancel — least prominent, at bottom after reversing */
        border: none;
        color: var(--text-light);
        font-size: 0.8rem;
        padding: 0.4rem;
        background: none;
    }

    /* ── Stats grid: 2×2 compact ── */
    .stats-grid {
        grid-template-columns: 1fr 1fr;
        gap: 0.6rem;
        margin-bottom: 1.5rem;
    }

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

    .stat-number {
        font-size: 1.8rem;
        margin-bottom: 0.25rem;
    }

    .stat-title {
        font-size: 0.7rem;
        letter-spacing: 0.06em;
    }

    /* ── Test cards on mobile ── */
    .test-card {
        border-left: none;
        border-right: none;
        border-radius: 0;
        margin-bottom: 0;
        border-bottom: none;
        border-top: 1px solid var(--border);
    }

    .test-card:first-of-type {
        border-top: 1px solid var(--border);
    }

    /* ── Test creation: strip card styling on mobile, match chapters page ── */
    .test-config {
        background: none;
        border: none;
        box-shadow: none;
        padding: 0;
        max-width: 100%;
    }

    .chapter-checklist {
        border-left: none;
        border-right: none;
        border-radius: 0;
    }

    .chapter-check-item {
        padding: 0.75rem 0.9rem;
    }

    .chapter-check-part {
        display: none; /* save space on mobile */
    }

    .checkbox-group {
        grid-template-columns: 1fr 1fr;
        gap: 0.6rem;
    }

    .checkbox-label {
        padding: 0.7rem 0.8rem;
        font-size: 0.9rem;
        gap: 0.5rem;
    }

    .stepper {
        width: 100%;
        justify-content: center;
    }

    .stepper-btn {
        flex: 1;
        height: 50px;
        font-size: 1.6rem;
    }

    .stepper input[type="number"] {
        flex: 1;
        height: 50px;
        font-size: 1.2rem;
    }

    /* ── MCQ page ── */
    .question-card {
        padding: 1.25rem;
    }

    .question-text {
        font-size: 1.1rem;
        margin-bottom: 1.25rem;
    }

    .option {
        padding: 0.9rem 1rem;
    }

    .mcq-actions {
        gap: 0.5rem;
    }

    /* ── KaTeX overflow: make wide equations scroll horizontally ── */
    .katex-display {
        overflow-x: auto;
        overflow-y: hidden;
        padding-bottom: 4px; /* avoid clipping descenders */
    }

    .explanation-box {
        overflow-x: auto;
    }

    /* ── Login page ── */
    .login-container {
        margin: 2rem auto;
        padding: 2rem 1.5rem;
    }

    .login-title {
        font-size: 2rem;
    }

    /* ── Hide desktop inline select button on mobile ── */
    .multi-select-inline-btn {
        display: none;
    }

    /* ── Long-press hint banner ── */
    .multiselect-hint {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        background: rgba(199, 169, 107, 0.1);
        border: 1px solid rgba(199, 169, 107, 0.35);
        color: var(--accent-dark);
        font-size: 0.72rem;
        font-family: 'JetBrains Mono', monospace;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        padding: 0.5rem 0.9rem;
        border-radius: 20px;
        margin-bottom: 0.75rem;
        cursor: pointer;
        transition: opacity 0.4s;
    }

    /* ── List item in select mode: row layout so checkbox sits on left ── */
    .chapter-list-item.select-mode {
        flex-direction: row !important;
        align-items: center !important;
        gap: 0.75rem !important;
    }

    .chapter-list-item.select-mode .chapter-list-info {
        flex: 1;
        min-width: 0;
    }

    /* ── Multi-chapter selection bar: sit above bottom nav ── */
    .chapter-selection-bar {
        bottom: 60px; /* height of bottom-nav */
        flex-direction: column;
        align-items: stretch;
        padding: 0.65rem 1rem;
        gap: 0.5rem;
    }

    .selection-bar-info {
        flex-direction: row;
        align-items: center;
        gap: 0.75rem;
    }

    .selection-bar-actions {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .selection-status-filters {
        flex: 1;
        gap: 0.75rem;
    }

    /* Selection bar pushes content up — extra bottom padding when bar is shown */
    .main-content.selection-active {
        padding-bottom: 140px;
    }
}

@media (max-width: 480px) {

    .container {
        padding: 1rem 0.75rem;
    }

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

    .chapter-list-title {
        font-size: 0.95rem;
    }

    .stats-grid {
        gap: 0.4rem;
    }

    .stat-card {
        padding: 0.85rem 0.5rem;
    }

    .stat-number {
        font-size: 1.5rem;
    }

    .stat-title {
        font-size: 0.65rem;
    }

    .checkbox-group {
        grid-template-columns: 1fr;
    }

    .bottom-nav-item {
        font-size: 0.58rem;
    }
}
