/* ============================================================
   HỌC XANH — Mobile App Shell (một app thống nhất)
   Chỉ áp dụng ≤768px khi đã đăng nhập (.app-shell)
   ============================================================ */

@media (max-width: 768px) {
    body.app-shell {
        display: flex;
        flex-direction: column;
        min-height: var(--hx-app-height, 100dvh);
        background: #eef2f6;
        overflow-x: clip;
    }

    body.app-shell .duo-wrapper {
        flex: 1;
        width: 100%;
        min-height: 0;
        min-width: 0;
    }

    body.app-shell .duo-main-container {
        margin-left: 0 !important;
        width: 100%;
        max-width: 100%;
        min-width: 0;
        overflow-x: clip;
        flex-direction: column;
    }

    /* ── Top bar trắng — chữ xanh Học Xanh ── */
    .hx-topbar {
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-shrink: 0;
        position: sticky;
        top: 0;
        z-index: 200;
        min-height: 50px;
        padding: 10px 14px;
        padding-top: calc(10px + env(safe-area-inset-top));
        background: #ffffff;
        border-bottom: 2px solid var(--duo-border-color, #e5e5e5);
        box-shadow: 0 1px 8px rgba(0, 0, 0, 0.04);
    }

    .hx-topbar-left {
        display: flex;
        align-items: center;
        gap: 10px;
        min-width: 0;
        flex: 1;
    }

    .hx-topbar-logo {
        width: 34px;
        height: 34px;
        object-fit: contain;
        flex-shrink: 0;
    }

    .hx-topbar-title {
        font-size: 17px;
        font-weight: 900;
        color: var(--duo-dark, #3c3c3c);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .hx-topbar-points {
        flex-shrink: 0;
        background: var(--duo-green-light, #d7ffb8);
        color: var(--duo-green-border, #46a302);
        border: 2px solid var(--duo-green, #58cc02);
        border-radius: 999px;
        padding: 6px 14px;
        font-size: 14px;
        font-weight: 900;
    }

    /* ── Nội dung chính ── */
    body.app-shell .duo-content-col {
        width: 100%;
        max-width: 100% !important;
        min-width: 0;
        padding: 12px 12px calc(76px + var(--hx-safe-bottom)) 12px !important;
        overflow-x: clip;
    }

    /* Carousel / tab ngang — không tràn khung */
    body.app-shell .dashboard-mobile-carousel-wrap {
        width: 100%;
        max-width: 100%;
        margin-left: 0;
        margin-right: 0;
        overflow-x: clip;
    }

    body.app-shell .duo-subject-tabs {
        max-width: 100%;
        box-sizing: border-box;
    }

    /* Ẩn banner trùng tiêu đề — header đã có tên trang */
    body.app-shell .duo-map-header {
        display: none;
    }

    body.app-shell.page-dashboard .duo-map-header {
        display: block;
        background: linear-gradient(135deg, #1cb0f6, #1899d6) !important;
        box-shadow: 0 4px 0 #1482ba !important;
        border-radius: 16px;
        padding: 16px !important;
        margin-bottom: 12px !important;
    }

    body.app-shell.page-dashboard .duo-map-header h1 {
        font-size: 17px !important;
    }

    body.app-shell.page-dashboard .duo-map-header p {
        font-size: 12px !important;
        margin-top: 4px !important;
        opacity: 0.95;
    }

    body.app-shell .duo-card {
        border-radius: 16px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
    }

    /* ── Tab bar: 1 hàng, vuốt ngang ── */
    .hx-tabbar {
        display: block;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 500;
        background: #fff;
        border-top: 1px solid #e5e5e5;
        padding-bottom: env(safe-area-inset-bottom);
        box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.07);
    }

    .hx-tabbar-track {
        display: flex;
        flex-wrap: nowrap;
        overflow-x: auto;
        overflow-y: hidden;
        scroll-snap-type: x proximity;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        padding: 0 6px;
    }

    .hx-tabbar-track::-webkit-scrollbar {
        display: none;
    }

    .hx-tab {
        flex: 0 0 auto;
        min-width: 28%;
        max-width: 34%;
        min-height: 48px;
        scroll-snap-align: start;
        display: flex;
        align-items: center;
        justify-content: center;
        text-align: center;
        text-decoration: none;
        color: #777;
        font-size: 12px;
        font-weight: 800;
        text-transform: uppercase;
        line-height: 1.25;
        padding: 14px 8px 12px;
        border: none;
        background: none;
        font-family: inherit;
        position: relative;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
        cursor: pointer;
    }

    .hx-tab::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 20%;
        right: 20%;
        height: 3px;
        border-radius: 3px 3px 0 0;
        background: transparent;
        transition: background 0.2s;
    }

    .hx-tab.active {
        color: #1cb0f6;
    }

    .hx-tab.active::after {
        background: #1cb0f6;
    }

    #bnav-aitutor.hx-tab {
        font-size: 11px;
    }

    /* Ẩn menu lưới cũ, dùng hx-tabbar 1 hàng */
    body.app-shell .duo-mobile-header,
    body.app-shell .duo-bottom-nav {
        display: none !important;
    }

    body.app-shell .hx-tabbar {
        display: block !important;
    }

    body.immersive-open .hx-tabbar,
    body.immersive-open .hx-topbar {
        display: none !important;
    }

    body.keyboard-open .hx-tabbar,
    body.keyboard-open .hx-topbar {
        display: none !important;
    }

    body.keyboard-open .duo-content-col {
        padding-bottom: 12px !important;
    }

    #floating-tutor-btn {
        bottom: calc(72px + var(--hx-safe-bottom)) !important;
        right: calc(12px + var(--hx-safe-right)) !important;
    }

    /* Gia sư Lyco — full màn hình mobile, không tràn viền */
    #tutor-chat-panel {
        padding: var(--hx-safe-top) var(--hx-safe-right) 0 var(--hx-safe-left);
        box-sizing: border-box;
    }

    #tutor-chat-panel .duo-card,
    #tutor-chat-panel .hx-tutor-sheet {
        width: 100% !important;
        max-width: 100% !important;
        height: min(85%, calc(var(--hx-app-height, 100dvh) - var(--hx-safe-top))) !important;
        max-height: none !important;
        right: 0 !important;
        left: 0 !important;
        border-radius: 20px 20px 0 0 !important;
        box-sizing: border-box;
    }

    #tutor-chat-panel .hx-tutor-input-row {
        padding-bottom: calc(12px + var(--hx-safe-bottom));
    }
}

/* Khóa vuốt ngang khi mở dropdown chọn lớp / tìm kiếm */
body.hx-select-open {
    overflow: hidden !important;
    touch-action: none;
}

body.hx-select-open .hx-tabbar-track,
body.hx-select-open #bottom-nav,
body.hx-select-open .dashboard-mobile-carousel,
body.hx-select-open .duo-subject-tabs {
    pointer-events: none;
}

@media (max-width: 768px) {
    body.app-shell .duo-select-container {
        width: 100%;
        max-width: 100%;
        min-width: 0;
        overflow: visible;
        padding: 0;
        margin-bottom: 16px;
    }

    body.app-shell .duo-custom-select {
        width: 100%;
        max-width: 100%;
    }

    body.app-shell .duo-custom-select-menu {
        position: fixed;
        left: 12px;
        right: 12px;
        width: auto;
        max-width: none;
        max-height: min(50vh, 320px);
        z-index: 5000;
    }

    body.app-shell .duo-custom-select-menu.show {
        transform: translateY(0);
    }
}

@media (min-width: 769px) {
    .hx-topbar,
    .hx-tabbar {
        display: none !important;
    }

    #tutor-chat-panel .hx-tutor-sheet {
        width: min(400px, calc(100% - 24px)) !important;
        max-width: 400px;
        height: 80% !important;
        max-height: 600px;
        right: 0;
        left: auto;
        border-radius: 24px 24px 0 24px !important;
    }
}

/* Màn hình rất nhỏ (iPhone SE, máy cũ) */
@media (max-width: 380px) {
    body.app-shell .hx-topbar-title {
        font-size: 15px;
    }

    body.app-shell .hx-topbar-points {
        padding: 5px 10px;
        font-size: 12px;
    }

    .duo-round-lessons .duo-map-node-wrapper:nth-child(8n+2) { margin-left: 32px !important; }
    .duo-round-lessons .duo-map-node-wrapper:nth-child(8n+3) { margin-left: 52px !important; }
    .duo-round-lessons .duo-map-node-wrapper:nth-child(8n+4) { margin-left: 32px !important; }
    .duo-round-lessons .duo-map-node-wrapper:nth-child(8n+6) { margin-left: -32px !important; }
    .duo-round-lessons .duo-map-node-wrapper:nth-child(8n+7) { margin-left: -52px !important; }
    .duo-round-lessons .duo-map-node-wrapper:nth-child(8n+8) { margin-left: -32px !important; }

    .duo-map-node-mascot {
        left: 58px !important;
        width: max-content;
        max-width: calc(100vw - 40px);
    }

    .duo-mascot-speech {
        white-space: nowrap;
        font-size: 10px;
        padding: 4px 8px;
    }

    .duo-choices-grid {
        grid-template-columns: 1fr !important;
    }

    .duo-feedback-bar {
        flex-wrap: wrap;
        gap: 10px;
        padding: 12px 14px calc(12px + var(--hx-safe-bottom));
    }

    .duo-feedback-trail {
        width: 100%;
        justify-content: flex-end;
    }
}

/* Capacitor / PWA standalone — tránh nhảy layout khi bàn phím */
html.capacitor-native,
html.pwa-standalone {
    height: 100%;
}

html.capacitor-native body.app-shell,
html.pwa-standalone body.app-shell {
    min-height: var(--hx-app-height, 100dvh);
}
