/* ========================================
   Mobile Layout
   Activates when:
     1) Viewport is cramped AND portrait  (max-width:768px + orientation:portrait)
     2) Viewport is very narrow regardless (max-width:480px)
   "Request Desktop Site" inflates viewport to ~980px+,
   so these queries never match and the full desktop layout is shown (small but complete).
   ======================================== */

/* ── Shared mobile media query ── */
@media (max-width: 768px) and (orientation: portrait),
(max-width: 480px) {

    /* ---------- Body & chrome ---------- */
    body {
        overflow: hidden;
        /* Bottom nav sits on top */
        padding-bottom: 60px;
    }

    body.mobile-sheet-open {
        overflow: hidden;
    }

    /* ---------- Hide desktop-only elements ---------- */
    .tabs {
        display: none !important;
    }

    .tab-glider {
        display: none !important;
    }

    .swipe-hint {
        display: none !important;
    }

    .keyboard-hints {
        display: none !important;
    }

    .docs-link {
        display: none !important;
    }

    .pwa-install-btn {
        display: none !important;
    }

    .theme-toggle {
        display: none !important;
    }

    .profile-btn {
        display: none !important;
    }

    .separator {
        display: none !important;
    }

    /* ---------- Header (slim top bar) ---------- */
    header {
        padding: 0.6rem 1rem;
        position: sticky;
        top: 0;
        z-index: 200;
    }

    .header-content {
        gap: 0.5rem;
        flex-wrap: nowrap;
    }

    h1 {
        font-size: 1.1rem !important;
        white-space: nowrap;
    }

    .header-actions {
        gap: 0.5rem;
    }

    .auth-link {
        font-size: 0.7rem;
        padding: 0.35rem 0.6rem;
    }

    .profile-avatar {
        width: 30px;
        height: 30px;
    }

    /* ---------- Main container ---------- */
    main {
        flex: 1;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        padding: 0;
    }

    /* ---------- Sections (general) ---------- */
    section[id^="section-"] {
        padding: 0.75rem !important;
    }

    .active-section {
        height: auto !important;
        min-height: calc(100vh - 120px);
        overflow-y: visible !important;
    }

    /* ---------- VISUALIZER SECTION ---------- */
    #section-visualizer {
        padding: 0 !important;
    }

    /* Toolbar: collapsed into sheet on mobile */
    .toolbar {
        display: none !important;
    }

    /* Mini mobile toolbar (controlled by JS) */
    .mobile-mini-toolbar {
        display: flex;
        flex-direction: column;
        background: var(--bg-secondary);
        border-bottom: 1px solid var(--border-color);
        flex-shrink: 0;
    }

    /* Row 1: status bar */
    .mobile-toolbar-status {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        padding: 0.5rem 0.75rem;
    }

    .mobile-toolbar-status .mobile-algo-label {
        font-size: 0.8rem;
        font-weight: 600;
        color: var(--text-primary);
        white-space: nowrap;
        flex-shrink: 0;
    }

    .mobile-toolbar-status .mobile-stat {
        font-family: 'SF Mono', 'Consolas', monospace;
        font-size: 0.7rem;
        color: var(--text-secondary);
        white-space: nowrap;
    }

    .mobile-toolbar-status .mobile-stat strong {
        color: var(--text-primary);
    }

    .mobile-toolbar-toggle {
        margin-left: auto;
        flex-shrink: 0;
        background: none;
        border: none;
        color: var(--text-muted);
        font-size: 0.75rem;
        cursor: pointer;
        padding: 0.3rem 0.5rem;
        font-family: inherit;
        display: flex;
        align-items: center;
        gap: 0.25rem;
        -webkit-tap-highlight-color: transparent;
        transition: color 0.15s ease;
    }

    .mobile-toolbar-toggle:active {
        color: var(--text-primary);
    }

    .mobile-toolbar-toggle-icon {
        display: inline-block;
        transition: transform 0.2s ease;
        font-size: 0.6rem;
    }

    .mobile-mini-toolbar.expanded .mobile-toolbar-toggle-icon {
        transform: rotate(180deg);
    }

    .mobile-open-controls {
        flex-shrink: 0;
        background: var(--bg-tertiary);
        border: 1px solid var(--border-color);
        color: var(--text-secondary);
        padding: 0.3rem 0.6rem;
        border-radius: 6px;
        font-size: 0.7rem;
        font-family: inherit;
        cursor: pointer;
        white-space: nowrap;
        -webkit-tap-highlight-color: transparent;
    }

    /* Row 2: collapsible quick controls */
    .mobile-quick-controls {
        display: none;
        padding: 0.5rem 0.75rem 0.75rem;
        border-top: 1px solid var(--border-color);
        gap: 0.5rem;
        flex-direction: column;
        animation: mqc-slide-in 0.2s ease-out;
    }

    @keyframes mqc-slide-in {
        from {
            opacity: 0;
            transform: translateY(-6px);
        }

        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    .mobile-mini-toolbar.expanded .mobile-quick-controls {
        display: flex;
    }

    .mobile-qc-row {
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .mobile-qc-row label {
        font-size: 0.7rem;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: var(--text-muted);
        font-weight: 500;
        min-width: 56px;
        flex-shrink: 0;
    }

    .mobile-qc-row label strong {
        color: var(--text-primary);
        font-weight: 600;
    }

    .mobile-qc-row input[type="range"] {
        flex: 1;
        min-width: 0;
    }

    .mobile-qc-buttons {
        display: flex;
        gap: 0.4rem;
        margin-top: 0.15rem;
    }

    .mobile-qc-buttons .btn {
        flex: 1;
        min-height: 38px;
        font-size: 0.8rem;
        padding: 0.4rem 0.5rem;
    }

    /* Advanced options hidden on mobile (use sheet) */
    .advanced-options {
        display: none !important;
    }

    #visualizer-container {
        flex: 1;
        padding: 0.5rem 0.5rem 5rem !important;
        min-height: 55vh;
    }

    .progress-container {
        max-width: 100%;
        margin: 0.25rem 0.5rem;
    }

    #main-scrubber-wrap {
        left: 0.5rem;
        right: 0.5rem;
        bottom: 68px;
        z-index: 150;
    }

    /* ---------- STEP MODE ---------- */
    #section-step {
        padding: 0.75rem !important;
    }

    .step-header h2 {
        font-size: 1.1rem;
    }

    .step-header p {
        font-size: 0.75rem;
    }

    .step-controls {
        padding: 0.75rem;
        gap: 0.75rem;
        flex-direction: column;
    }

    .step-config {
        flex-direction: column;
        gap: 0.5rem;
        width: 100%;
    }

    .step-config .control-group {
        width: 100%;
    }

    .step-actions {
        width: 100%;
        justify-content: stretch;
    }

    .step-actions .btn {
        flex: 1;
        min-height: 40px;
    }

    .step-info {
        flex-direction: column;
        gap: 0.5rem;
        padding: 0.75rem;
        margin-bottom: 0.75rem;
        text-align: center;
    }

    .step-counter {
        font-size: 1rem;
    }

    .step-stats {
        gap: 0.75rem;
        font-size: 0.8rem;
    }

    .step-split-container {
        flex-direction: column !important;
        flex-wrap: nowrap !important;
        gap: 0.75rem;
    }

    .step-vis-pane {
        order: 1;
        min-height: 200px;
    }

    .step-visualizer {
        min-height: 200px !important;
    }

    .step-code-pane {
        order: 2;
        flex: none !important;
        max-width: 100% !important;
        max-height: 250px !important;
    }

    /* ---------- COMPARE MODE ---------- */
    .compare-header h2 {
        font-size: 1.1rem;
    }

    .compare-header p {
        font-size: 0.75rem;
    }

    .compare-controls {
        flex-direction: column !important;
        padding: 0.75rem !important;
        gap: 0.75rem !important;
    }

    .compare-config {
        flex-direction: column !important;
        gap: 0.5rem !important;
        width: 100% !important;
    }

    .compare-config .control-group {
        width: 100%;
    }

    .compare-actions {
        width: 100%;
        flex-wrap: wrap;
        justify-content: stretch;
    }

    .compare-actions .btn {
        flex: 1;
        min-width: 0;
        min-height: 40px;
    }

    .compare-grid {
        grid-template-columns: 1fr !important;
        gap: 0.75rem;
    }

    .compare-panel {
        height: 200px;
    }

    .race-count {
        font-size: 0.85rem;
    }

    .leaderboard {
        padding: 0.75rem;
    }

    .leaderboard-table {
        font-size: 0.75rem;
    }

    .leaderboard-table th,
    .leaderboard-table td {
        padding: 0.4rem 0.5rem;
    }

    /* ---------- BENCHMARK ---------- */
    #section-benchmark {
        padding-bottom: 80px !important;
    }

    #section-benchmark h2 {
        font-size: 1.1rem;
    }

    .benchmark-config {
        flex-direction: column;
        gap: 0.75rem;
        padding: 0.75rem;
    }

    .benchmark-config .config-item {
        width: 100%;
    }

    .benchmark-config input[type="number"] {
        width: 100%;
    }

    .algo-selection {
        padding: 0.75rem;
    }

    .checkbox-grid {
        grid-template-columns: 1fr 1fr !important;
    }

    .results-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .results-container table {
        font-size: 0.7rem;
    }

    .results-container th,
    .results-container td {
        padding: 0.4rem;
        white-space: nowrap;
    }

    .benchmark-export {
        flex-wrap: wrap;
        gap: 0.4rem;
    }

    .benchmark-export .btn {
        font-size: 0.75rem;
    }

    /* ---------- CUSTOM MODE ---------- */
    #section-custom {
        padding: 0.75rem !important;
    }

    .custom-header h2 {
        font-size: 1rem !important;
    }

    .custom-header p {
        font-size: 0.75rem;
    }

    .custom-workspace {
        grid-template-columns: 1fr !important;
        gap: 0.75rem;
    }

    /* Desktop notice for custom code editor on very small phones */
    .mobile-desktop-notice {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: 2rem 1rem;
        background: var(--bg-secondary);
        border: 1px solid var(--border-color);
        border-radius: 12px;
        text-align: center;
        gap: 0.75rem;
    }

    .mobile-desktop-notice h4 {
        font-size: 0.95rem;
        color: var(--text-primary);
    }

    .mobile-desktop-notice p {
        font-size: 0.8rem;
        color: var(--text-muted);
        max-width: 280px;
    }

    /* Editor on mobile: still usable but in a smaller viewport */
    .editor-wrapper {
        min-height: 200px;
    }

    #custom-code-editor {
        font-size: 12px !important;
    }

    .code-highlight-pre {
        font-size: 12px !important;
    }

    .editor-toolbar {
        flex-wrap: wrap;
        gap: 0.4rem;
    }

    .editor-actions {
        flex-wrap: wrap !important;
        gap: 0.35rem !important;
    }

    .custom-controls {
        flex-direction: column;
        gap: 0.75rem;
    }

    .custom-stats {
        margin-left: 0;
        gap: 0.75rem;
    }

    .custom-vis-box {
        min-height: 150px;
    }

    .custom-console-body {
        height: 80px;
    }

    /* ---------- ANALYTICS ---------- */
    #section-analytics {
        padding-bottom: 80px !important;
    }

    .analytics-header h2 {
        font-size: 1.1rem;
    }

    .analytics-header p {
        font-size: 0.75rem;
    }

    .analytics-grid {
        grid-template-columns: 1fr !important;
    }

    .analytics-card {
        grid-column: auto !important;
    }

    .session-stats {
        grid-template-columns: 1fr 1fr;
    }

    .upload-area {
        padding: 1rem !important;
    }

    /* ---------- Bottom Navigation ---------- */
    .mobile-nav {
        display: flex !important;
    }

    /* ---------- Floating Action Button ---------- */
    .mobile-fab {
        display: flex !important;
    }

    /* ---------- Enhanced touch targets ---------- */
    .btn {
        min-height: 40px;
        font-size: 0.85rem;
    }

    select,
    input[type="number"] {
        min-height: 40px;
        font-size: 0.9rem;
    }

    input[type="range"] {
        height: 8px;
        padding: 12px 0;
    }

    input[type="range"]::-webkit-slider-thumb {
        width: 26px;
        height: 26px;
        margin-top: -9px;
    }

    input[type="range"]::-moz-range-thumb {
        width: 26px;
        height: 26px;
    }

    /* ---------- Profile modal mobile ---------- */
    .profile-dropdown {
        position: fixed;
        top: auto;
        bottom: 70px;
        right: 0.5rem;
        left: 0.5rem;
        min-width: auto;
        max-width: none;
    }

    /* ---------- Toast on mobile (above bottom nav) ---------- */
    .toast-container {
        bottom: 72px !important;
    }
}

/* ── Bottom Navigation (hidden by default, shown by media query above) ── */
.mobile-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 60px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    z-index: 500;
    align-items: stretch;
    justify-content: space-around;
    padding: 0;
    padding-bottom: env(safe-area-inset-bottom, 0);
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.25);
}

.mobile-nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    background: none;
    border: none;
    color: var(--text-muted);
    font-family: inherit;
    font-size: 0.6rem;
    font-weight: 500;
    cursor: pointer;
    transition: color 0.15s ease;
    padding: 0.25rem 0;
    position: relative;
    -webkit-tap-highlight-color: transparent;
}

.mobile-nav-item svg {
    width: 22px;
    height: 22px;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
    flex-shrink: 0;
}

.mobile-nav-item span {
    letter-spacing: 0.02em;
}

.mobile-nav-item.active {
    color: var(--text-primary);
}

.mobile-nav-item.active::before {
    content: '';
    position: absolute;
    top: 0;
    left: 25%;
    right: 25%;
    height: 2px;
    background: var(--text-primary);
    border-radius: 0 0 2px 2px;
}

/* ── Mobile More Menu ── */
.mobile-more-menu {
    display: none;
    position: fixed;
    bottom: 60px;
    right: 0;
    left: 0;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    z-index: 499;
    padding: 0.5rem 0;
    box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.3);
    animation: mobile-menu-slide-up 0.2s ease-out;
    padding-bottom: env(safe-area-inset-bottom, 0);
}

@keyframes mobile-menu-slide-up {
    from {
        transform: translateY(100%);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.mobile-more-menu.open {
    display: flex;
    flex-direction: column;
}

.mobile-more-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.85rem 1.25rem;
    background: none;
    border: none;
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 0.9rem;
    cursor: pointer;
    transition: background 0.15s ease;
    -webkit-tap-highlight-color: transparent;
}

.mobile-more-item:active {
    background: var(--bg-tertiary);
}

.mobile-more-item svg {
    width: 20px;
    height: 20px;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
    flex-shrink: 0;
}

/* ── Floating Action Button ── */
.mobile-fab {
    display: none;
    position: fixed;
    bottom: 76px;
    right: 16px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--text-primary);
    color: var(--bg-primary);
    border: none;
    font-family: inherit;
    font-size: 0.8rem;
    font-weight: 700;
    cursor: pointer;
    z-index: 300;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
    transition: transform 0.15s ease, background 0.15s ease;
    -webkit-tap-highlight-color: transparent;
}

.mobile-fab:active {
    transform: scale(0.92);
}

.mobile-fab.stop {
    background: #e74c3c;
    color: #fff;
}

.mobile-fab svg {
    width: 24px;
    height: 24px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* Only show FAB on applicable sections */
.mobile-fab[data-visible="false"] {
    display: none !important;
}

/* ── Mobile Bottom Sheet ── */
.mobile-sheet-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 599;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.25s ease, visibility 0.25s ease;
}

.mobile-sheet-backdrop.visible {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

.mobile-sheet {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 80vh;
    background: var(--bg-secondary);
    border-radius: 16px 16px 0 0;
    z-index: 600;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.2, 0, 0.2, 1);
    overflow: hidden;
    padding-bottom: env(safe-area-inset-bottom, 0);
    visibility: hidden;
    pointer-events: none;
}

.mobile-sheet.open {
    transform: translateY(0);
    visibility: visible;
    pointer-events: auto;
}

.mobile-sheet-handle-bar {
    display: flex;
    justify-content: center;
    padding: 0.5rem 0;
    cursor: grab;
}

.mobile-sheet-handle-bar::after {
    content: '';
    width: 36px;
    height: 4px;
    background: var(--text-muted);
    border-radius: 4px;
    opacity: 0.5;
}

.mobile-sheet-title {
    padding: 0 1rem 0.5rem;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-primary);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    border-bottom: 1px solid var(--border-color);
}

.mobile-sheet-body {
    padding: 1rem;
    overflow-y: auto;
    max-height: calc(80vh - 60px);
    -webkit-overflow-scrolling: touch;
}

.mobile-sheet-body .control-group {
    width: 100%;
    margin-bottom: 0.75rem;
}

.mobile-sheet-body .control-group label {
    font-size: 0.8rem;
    margin-bottom: 0.25rem;
}

.mobile-sheet-body select,
.mobile-sheet-body input[type="number"],
.mobile-sheet-body input[type="text"] {
    width: 100%;
    min-height: 42px;
}

.mobile-sheet-body input[type="range"] {
    width: 100%;
}

.mobile-sheet-body .option-group {
    margin-bottom: 0.75rem;
    width: 100%;
    justify-content: space-between;
}

.mobile-sheet-body .option-group input[type="checkbox"] {
    width: 20px;
    height: 20px;
}

/* ── Mobile mini-toolbar (only shown in mobile) ── */
.mobile-mini-toolbar {
    display: none;
}

/* ── Mobile more menu overlay (click-away) ── */
.mobile-more-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 498;
}

.mobile-more-overlay.active {
    display: block;
}