/* ============================================================
   Mobile app-like single-screen UI (≤768px).
   Desktop is untouched: everything lives behind media queries.
   Shell: fixed compact top bar + fixed bottom tab bar,
   content pane in between (internal scroll only where content
   is inherently long). Task page = segmented Description/Code/
   Result panes, each fitting one screen.
   ============================================================ */

/* Mobile-only elements, hidden on desktop */
@media (min-width: 769px) {
    .tabbar,
    .mobile-actions,
    .mseg {
        display: none !important;
    }
}

@media (max-width: 768px) {
    :root {
        --m-top: 52px;
        --m-tab: calc(58px + env(safe-area-inset-bottom, 0px));
    }

    /* ---------- App shell: nothing outside panes ever moves ----- */
    html, body {
        height: 100%;
    }
    body {
        position: fixed;
        inset: 0;
        width: 100%;
        overflow: hidden;
    }

    .header {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: var(--m-top);
        z-index: 90;
        padding: 0 12px;
        display: flex;
        align-items: center;
    }
    .nav {
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: row; /* style.css mobile rule makes it a column */
        align-items: center;
        justify-content: space-between;
        padding: 0;
        flex-wrap: nowrap;
    }
    .nav-brand a {
        font-size: 1.02rem;
        white-space: nowrap;
    }
    .nav-links {
        display: none !important;
    }
    .mobile-actions {
        display: flex;
        align-items: center;
        gap: 10px;
    }
    .mobile-actions .language-select {
        background: rgba(255, 255, 255, 0.18);
        color: #fff;
        border: 1px solid rgba(255, 255, 255, 0.35);
        border-radius: 8px;
        padding: 4px 6px;
        font-size: 0.85rem;
    }
    .mobile-actions .language-select option {
        color: #2D3748;
    }
    .mlogout {
        color: #fff;
        text-decoration: none;
        font-size: 1.25rem;
        line-height: 1;
        opacity: 0.9;
    }
    /* second dark-mode toggle injected by mobile.js */
    .mobile-actions .dark-mode-toggle {
        width: 32px;
        height: 32px;
        font-size: 1.05rem;
        flex: none;
    }

    .main-content {
        position: fixed;
        top: var(--m-top);
        bottom: var(--m-tab);
        left: 0;
        right: 0;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        margin: 0;
        max-width: none;
        padding: 12px 14px 16px;
    }

    .footer {
        display: none;
    }

    /* ---------- Bottom tab bar ---------------------------------- */
    .tabbar {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: var(--m-tab);
        padding-bottom: env(safe-area-inset-bottom, 0px);
        background: rgba(255, 255, 255, 0.96);
        -webkit-backdrop-filter: blur(14px);
        backdrop-filter: blur(14px);
        border-top: 1px solid var(--border-color);
        z-index: 95;
    }
    .dark-mode .tabbar {
        background: rgba(26, 32, 44, 0.96);
        border-top-color: rgba(255, 255, 255, 0.12);
    }
    .tabbar a {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        min-width: 0;
        text-decoration: none;
        color: var(--text-muted);
        -webkit-tap-highlight-color: transparent;
    }
    .dark-mode .tabbar a {
        color: #A0AEC0;
    }
    .tabbar .ticon {
        font-size: 1.35rem;
        line-height: 1.1;
    }
    .tabbar .tlabel {
        font-size: 0.62rem;
        font-weight: 700;
        max-width: 100%;
        padding: 0 3px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .tabbar a.active {
        color: var(--primary-color);
    }
    .tabbar a.active .ticon {
        transform: translateY(-1px) scale(1.12);
    }
    .tabbar a.tab-vip {
        color: #b8860b;
    }
    .dark-mode .tabbar a.tab-vip {
        color: #FFD43B;
    }

    /* generic compaction */
    .page-header {
        margin-bottom: 12px;
    }
    .page-header h1 {
        font-size: 1.35rem;
        line-height: 1.25;
    }
    .page-header p {
        font-size: 0.9rem;
    }
    .messages {
        position: sticky;
        top: 0;
        z-index: 50;
    }

    /* ---------- Home: fits one screen ---------------------------- */
    .hero {
        padding: 18px 14px 16px;
        margin-bottom: 12px;
    }
    .hero h1 {
        font-size: 1.45rem;
        line-height: 1.25;
        margin-bottom: 6px;
    }
    .hero-subtitle {
        font-size: 0.92rem;
        margin-bottom: 14px;
    }
    .hero-stats {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 8px;
        margin-bottom: 16px;
    }
    .stat-card {
        padding: 10px 6px;
    }
    .stat-card .stat-number {
        font-size: 1.05rem;
    }
    .stat-card .stat-label {
        font-size: 0.68rem;
    }
    .hero-features {
        display: grid;
        grid-template-columns: 1fr;
        gap: 8px;
        margin-bottom: 14px;
    }
    .hero-features .feature {
        display: flex;
        align-items: center;
        text-align: left;
        gap: 12px;
        padding: 10px 12px;
    }
    .hero-features .feature-icon {
        font-size: 1.6rem;
        margin: 0;
        flex: none;
    }
    .hero-features .feature h3 {
        font-size: 0.95rem;
        margin: 0 0 2px;
    }
    .hero-features .feature p {
        font-size: 0.78rem;
        margin: 0;
        line-height: 1.4;
    }
    .hero-buttons {
        display: flex;
        flex-direction: column;
        gap: 8px;
    }
    /* "How does it work" doesn't fit the single screen — app keeps it desktop-only */
    .info-section {
        display: none;
    }

    /* ---------- Levels: compact app list ------------------------- */
    .levels-grid {
        display: flex;
        flex-direction: column;
        gap: 10px;
    }
    .level-card {
        padding: 12px 14px;
    }
    .level-card .level-description {
        display: none;
    }
    .level-card .level-header {
        margin-bottom: 8px;
        gap: 10px;
    }
    .level-card .level-header h3 {
        font-size: 1rem;
    }
    .level-card .level-number {
        width: 30px;
        height: 30px;
        font-size: 0.9rem;
        flex: none;
    }
    .level-card .level-progress {
        margin-bottom: 8px;
    }
    .level-card .btn {
        padding: 9px 12px;
        font-size: 0.9rem;
    }
    .level-locked p {
        display: none;
    }
    .vip-upsell-banner {
        flex-direction: column;
        gap: 10px;
        padding: 14px;
        text-align: center;
    }

    /* ---------- Level page --------------------------------------- */
    .theory-section,
    .tasks-section {
        padding: 14px;
    }
    .task-card {
        flex-direction: row;
        align-items: center;
        padding: 10px 12px;
        gap: 8px;
    }
    .task-card .task-details p {
        display: none;
    }
    .task-card .task-details h3 {
        font-size: 0.95rem;
    }
    .task-card .btn {
        padding: 8px 12px;
        font-size: 0.85rem;
        white-space: nowrap;
    }

    /* ---------- Task page: segmented single-screen panes --------- */
    .page-task .main-content {
        overflow: hidden;
        display: flex;
        flex-direction: column;
        padding: 8px 12px 10px;
    }
    .page-task .task-page {
        flex: 1;
        min-height: 0;
        display: flex;
        flex-direction: column;
    }
    .page-task .page-header {
        margin-bottom: 8px;
    }
    .page-task .page-header h1 {
        font-size: 1.05rem;
        margin: 2px 0;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .page-task .back-link {
        font-size: 0.78rem;
    }
    .page-task .page-header .task-meta {
        gap: 8px;
        font-size: 0.74rem;
    }

    .mseg {
        display: flex;
        gap: 4px;
        padding: 4px;
        border-radius: 12px;
        background: rgba(108, 99, 255, 0.10);
        margin-bottom: 8px;
        flex: none;
    }
    .dark-mode .mseg {
        background: rgba(255, 255, 255, 0.08);
    }
    .mseg button {
        flex: 1;
        border: none;
        background: transparent;
        color: var(--text-muted);
        font: inherit;
        font-size: 0.78rem;
        font-weight: 700;
        padding: 8px 4px;
        border-radius: 9px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        -webkit-tap-highlight-color: transparent;
    }
    .dark-mode .mseg button {
        color: #A0AEC0;
    }
    .mseg button.active {
        background: var(--primary-color);
        color: #fff;
        box-shadow: 0 2px 10px rgba(108, 99, 255, 0.35);
    }

    .page-task .task-content {
        flex: 1;
        min-height: 0;
        display: flex;
        flex-direction: column;
    }
    /* pane switching: body carries mpane-desc / mpane-code / mpane-out */
    .page-task [data-mpane] {
        display: none !important;
    }
    body.mpane-desc [data-mpane="desc"],
    body.mpane-out [data-mpane="out"] {
        display: block !important;
    }
    body.mpane-code [data-mpane="code"] {
        display: flex !important;
    }
    /* desc + out panes scroll internally if long */
    body.mpane-desc .task-content,
    body.mpane-out .task-content {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    .page-task .task-description,
    .page-task .result-section,
    .page-task .run-output {
        padding: 12px 14px;
        margin-bottom: 10px;
    }
    .page-task .task-description h2,
    .page-task .result-section h2,
    .page-task .code-editor-section h2 {
        font-size: 0.95rem;
        margin-bottom: 6px;
    }
    .page-task .description-text {
        font-size: 0.92rem;
        line-height: 1.55;
    }
    .page-task .task-navigation {
        display: flex !important;
        gap: 8px;
        justify-content: space-between;
        margin-top: 4px;
    }
    body.mpane-code .task-navigation,
    body.mpane-out .task-navigation {
        display: none !important;
    }

    /* code pane fills the remaining screen, buttons pinned below */
    .page-task .code-editor-section {
        flex: 1;
        min-height: 0;
        flex-direction: column;
        padding: 10px 12px;
        margin-bottom: 0;
    }
    .page-task .code-editor-section form {
        flex: 1;
        min-height: 0;
        display: flex;
        flex-direction: column;
    }
    .page-task .code-editor-wrapper {
        flex: 1;
        min-height: 0;
        margin-bottom: 8px;
    }
    .page-task .CodeMirror {
        height: 100% !important;
        font-size: 16px; /* prevents iOS auto-zoom on focus */
        border-radius: 10px;
    }
    .page-task .form-actions {
        flex: none;
        display: flex;
        gap: 8px;
        margin: 0;
    }
    .page-task .form-actions .btn {
        flex: 1;
        padding: 11px 8px;
        font-size: 0.95rem;
    }

    /* ---------- VIP: swipeable tier cards ------------------------- */
    .vip-hero {
        padding: 14px;
    }
    .vip-hero h1 {
        font-size: 1.25rem;
    }
    .vip-hero-sub {
        font-size: 0.85rem;
    }
    .region-switch {
        gap: 6px;
    }
    .region-tab {
        font-size: 0.8rem;
        padding: 8px 10px;
    }
    .vip-tiers {
        display: flex;
        flex-direction: row;
        overflow-x: auto;
        scroll-snap-type: x mandatory;
        gap: 12px;
        padding: 6px 2px 12px;
        -webkit-overflow-scrolling: touch;
    }
    .vip-tiers .tier-card {
        flex: 0 0 84%;
        scroll-snap-align: center;
        min-width: 0;
    }
    .vip-faq-grid {
        grid-template-columns: 1fr;
        gap: 8px;
    }

    /* ---------- Leaderboard / profile / achievements -------------- */
    .user-rank-card {
        flex-direction: column;
        gap: 10px;
        padding: 12px;
    }
    .leaderboard-table-wrapper {
        font-size: 0.85rem;
    }
    .achievements-grid {
        grid-template-columns: 1fr;
        gap: 8px;
    }
    .achievement-card {
        padding: 10px 12px;
    }

    /* ---------- Auth: one centered screen ------------------------- */
    .page-login .main-content,
    .page-register .main-content {
        display: flex;
        align-items: center;
        justify-content: center;
        overflow: hidden;
    }
    .auth-container {
        width: 100%;
        margin: 0;
        padding: 0;
    }
    .auth-card {
        padding: 20px 18px;
    }

    /* floating helpers from effects.js must not cover the tab bar */
    .dark-mode-toggle {
        z-index: 96;
    }
}
