/* ==========================================================================
   PARKINSON ECOSYSTEM (tluparkinson.vn) - COMPREHENSIVE MOBILE RESPONSIVE CSS
   Tối ưu toàn diện cho màn hình điện thoại (360px - 768px, iOS & Android)
   Không làm ảnh hưởng bố cục Desktop (>= 1024px)
   ========================================================================== */

/* 1. GLOBAL BOX-SIZING & CHỐNG TRÀN NGANG */
*, *::before, *::after {
    box-sizing: border-box;
}

html, body {
    overflow-x: hidden;
    max-width: 100%;
    -webkit-text-size-adjust: 100%;
}

img, video, canvas, svg, iframe {
    max-width: 100%;
}

.table-responsive {
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* Ẩn các thành phần chỉ dành cho mobile trên Desktop (>= 769px) */
.shell-mobile-topbar,
.shell-sidebar-overlay,
.drawer-close-btn {
    display: none;
}

/* Nút tải/quay video & chụp ảnh MRI dùng chung */
.mobile-upload-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin-top: 12px;
}

.mobile-upload-actions .btn,
.mobile-upload-actions label.btn {
    flex: 1 1 180px;
    min-height: 44px;
}

/* ==========================================================================
   2. TABLET & MOBILE BREAKPOINT (<= 768px)
   ========================================================================== */
@media (max-width: 768px) {
    /* Chống tràn ngang trên mọi container chính */
    body,
    .dashboard-body,
    .shell,
    .main-content,
    .main,
    .card,
    .dashboard-grid,
    .dashboard-grid > *,
    .auth-body,
    .auth-container {
        max-width: 100%;
        min-width: 0;
    }

    /* Chống cắt chữ / chữ đè trên màn hình nhỏ */
    h1, h2, h3, h4, p, span, a, li, td, th, label {
        overflow-wrap: break-word;
        word-wrap: break-word;
    }

    /* ----------------------------------------------------------------------
       A. MOBILE TOPBAR & HAMBURGER DRAWER (CÁC TRANG DASHBOARD CHÍNH)
       ---------------------------------------------------------------------- */
    .mobile-topbar {
        display: flex !important;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: calc(60px + env(safe-area-inset-top, 0px));
        padding-top: env(safe-area-inset-top, 0px);
        padding-left: max(12px, env(safe-area-inset-left, 0px));
        padding-right: max(12px, env(safe-area-inset-right, 0px));
        gap: 8px;
        z-index: 1050;
    }

    .mobile-topbar .mobile-nav-toggle,
    .mobile-topbar .sidebar-toggle-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        min-width: 44px;
        min-height: 44px;
        background: var(--bg-card);
        border: 2px solid var(--border-color);
        border-radius: var(--radius-sm);
        color: var(--text-primary);
        font-size: 1.2rem;
        cursor: pointer;
        flex-shrink: 0;
    }

    .mobile-topbar-title {
        font-weight: 800;
        font-size: 1rem;
        color: var(--text-primary);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .mobile-topbar > div:nth-child(2) {
        min-width: 0;
        overflow: hidden;
    }

    .mobile-topbar > div:nth-child(2) span {
        font-size: 0.98rem !important;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .mobile-topbar .a11y-toolbar {
        flex-shrink: 0;
        padding: 2px;
    }

    .mobile-topbar .a11y-btn {
        min-width: 36px;
        min-height: 36px;
        padding: 4px 8px;
    }

    /* Sidebar trượt mượt (Drawer) nằm trên cùng */
    .sidebar {
        position: fixed !important;
        top: 0;
        left: 0;
        bottom: 0;
        width: min(82vw, 300px) !important;
        height: 100dvh;
        height: 100vh;
        z-index: 1100 !important;
        transform: translateX(-105%);
        transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1);
        padding-top: env(safe-area-inset-top, 0px);
        padding-bottom: env(safe-area-inset-bottom, 0px);
        box-shadow: 0 12px 36px rgba(15, 23, 42, 0.3);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .sidebar.open {
        transform: translateX(0) !important;
    }

    .sidebar-header,
    .sidebar-brand {
        padding: 16px 16px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
        border-bottom: 1px solid var(--border-color);
        font-weight: 800;
        color: var(--text-primary);
    }

    .sidebar-brand {
        font-size: 1.1rem;
    }

    .sidebar-brand i {
        color: var(--primary-600);
        font-size: 1.4rem;
    }

    .sidebar-header h2 {
        font-size: 1.15rem;
        flex: 1;
    }

    .drawer-close-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        min-width: 44px;
        min-height: 44px;
        border-radius: var(--radius-sm);
        border: 1px solid var(--border-color);
        background: var(--bg-card-subtle);
        color: var(--text-primary);
        font-size: 1.2rem;
        cursor: pointer;
        flex-shrink: 0;
    }

    .sidebar-overlay {
        position: fixed !important;
        inset: 0;
        background: rgba(15, 23, 42, 0.58);
        backdrop-filter: blur(3px);
        -webkit-backdrop-filter: blur(3px);
        z-index: 1090 !important;
        display: none;
        opacity: 0;
        transition: opacity 0.25s ease;
    }

    .sidebar-overlay.active {
        display: block !important;
        opacity: 1;
    }

    .sidebar .nav-links ul {
        list-style: none;
        padding: 0;
        margin: 0;
    }

    .sidebar .nav-links li a,
    .sidebar .nav-link {
        min-height: 48px;
        padding: 12px 14px;
        font-size: 1rem;
    }

    .sidebar-footer {
        padding: 14px 16px calc(16px + env(safe-area-inset-bottom, 0px));
    }

    /* Thanh điều hướng đáy iOS/Android Safe Area */
    .mobile-bottom-nav {
        display: flex !important;
        height: calc(62px + env(safe-area-inset-bottom, 0px));
        padding-bottom: env(safe-area-inset-bottom, 0px);
        padding-left: env(safe-area-inset-left, 0px);
        padding-right: env(safe-area-inset-right, 0px);
        z-index: 1040;
    }

    .mobile-nav-item {
        min-width: 0;
        padding: 6px 2px;
        font-size: 0.66rem;
        gap: 3px;
    }

    .mobile-nav-item i {
        font-size: 1.1rem;
    }

    .mobile-nav-item span {
        max-width: 100%;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* Main Content Padding & Safe Area */
    .main-content {
        margin-left: 0 !important;
        width: 100% !important;
        padding-top: calc(60px + 14px + env(safe-area-inset-top, 0px)) !important;
        padding-left: max(12px, env(safe-area-inset-left, 0px)) !important;
        padding-right: max(12px, env(safe-area-inset-right, 0px)) !important;
        padding-bottom: calc(82px + env(safe-area-inset-bottom, 0px)) !important;
    }

    .top-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 12px;
        margin-bottom: 16px;
    }

    .top-header h1 {
        font-size: 1.38rem !important;
        line-height: 1.3;
    }

    .top-header p {
        font-size: 0.92rem !important;
    }

    .header-actions {
        width: 100%;
        justify-content: space-between;
        gap: 10px;
    }

    .header-actions .a11y-toolbar {
        max-width: 100%;
        overflow-x: auto;
    }

    .user-profile {
        padding: 6px 12px;
        gap: 10px;
        max-width: 100%;
    }

    .user-profile .avatar {
        width: 36px;
        height: 36px;
        font-size: 0.95rem;
        flex-shrink: 0;
    }

    /* ----------------------------------------------------------------------
       B. MOBILE HAMBURGER DRAWER CHO TRANG MONITOR (.shell / .side / .main)
          (eeg_monitor.html & heart_emg_monitor.html)
       ---------------------------------------------------------------------- */
    .shell {
        display: block !important;
        min-height: 100vh;
    }

    .shell-mobile-topbar {
        display: flex !important;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: calc(58px + env(safe-area-inset-top, 0px));
        padding-top: env(safe-area-inset-top, 0px);
        padding-left: max(12px, env(safe-area-inset-left, 0px));
        padding-right: max(12px, env(safe-area-inset-right, 0px));
        background: linear-gradient(135deg, #172554 0%, #1e3a8a 100%);
        color: #ffffff;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
        z-index: 1050;
        box-shadow: 0 2px 12px rgba(15, 23, 42, 0.2);
    }

    .shell-mobile-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        min-width: 44px;
        min-height: 44px;
        border-radius: 8px;
        border: 1px solid rgba(255, 255, 255, 0.28);
        background: rgba(255, 255, 255, 0.12);
        color: #ffffff;
        font-size: 1.2rem;
        cursor: pointer;
    }

    .shell-mobile-brand {
        display: flex;
        align-items: center;
        gap: 8px;
        font-weight: 700;
        font-size: 1rem;
        color: #ffffff;
        min-width: 0;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .shell-mobile-brand i {
        color: #93c5fd;
        font-size: 1.25rem;
        flex-shrink: 0;
    }

    .shell-mobile-home {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        min-width: 44px;
        min-height: 44px;
        border-radius: 8px;
        color: #dbeafe;
        text-decoration: none;
        background: rgba(255, 255, 255, 0.1);
    }

    .shell .side {
        position: fixed !important;
        top: 0;
        left: 0;
        bottom: 0;
        width: min(82vw, 280px) !important;
        height: 100dvh;
        height: 100vh;
        padding: calc(18px + env(safe-area-inset-top, 0px)) 14px calc(20px + env(safe-area-inset-bottom, 0px)) !important;
        z-index: 1100 !important;
        transform: translateX(-105%);
        transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        box-shadow: 0 12px 36px rgba(15, 23, 42, 0.45);
    }

    .shell .side.open {
        transform: translateX(0) !important;
    }

    .shell .side .brand {
        display: flex !important;
        justify-content: space-between;
        align-items: center;
        padding: 0 6px 16px !important;
        margin-bottom: 8px;
        border-bottom: 1px solid rgba(255, 255, 255, 0.15);
    }

    .shell .side .drawer-close-btn {
        display: inline-flex !important;
        background: rgba(255, 255, 255, 0.12);
        border-color: rgba(255, 255, 255, 0.25);
        color: #ffffff;
    }

    .shell .side .nav {
        display: block !important;
        overflow: visible !important;
    }

    .shell .side .nav li {
        margin: 4px 0 !important;
    }

    .shell .side .nav a {
        min-height: 46px;
        padding: 11px 12px !important;
        font-size: 0.95rem !important;
    }

    .shell .side .nav span {
        display: inline !important;
    }

    .shell-sidebar-overlay {
        position: fixed;
        inset: 0;
        background: rgba(15, 23, 42, 0.6);
        backdrop-filter: blur(3px);
        -webkit-backdrop-filter: blur(3px);
        z-index: 1090;
        display: none;
    }

    .shell-sidebar-overlay.active {
        display: block !important;
    }

    .shell .main {
        padding-top: calc(58px + 16px + env(safe-area-inset-top, 0px)) !important;
        padding-left: max(12px, env(safe-area-inset-left, 0px)) !important;
        padding-right: max(12px, env(safe-area-inset-right, 0px)) !important;
        padding-bottom: calc(28px + env(safe-area-inset-bottom, 0px)) !important;
    }

    .shell header {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 12px !important;
        margin-bottom: 16px !important;
    }

    .shell .connect {
        width: 100% !important;
        max-width: 100%;
        justify-content: space-between;
        flex-wrap: wrap;
        white-space: normal !important;
        margin-top: 0 !important;
    }

    /* ----------------------------------------------------------------------
       C. BỐ CỤC LƯỚI 1 CỘT & THẺ THÔNG SỐ (GRID -> 1fr)
       ---------------------------------------------------------------------- */
    .dashboard-grid,
    .metrics,
    .bottom,
    .charts-grid,
    .research-stats-grid,
    .rehab-grid,
    .posture-selector-grid,
    .team-members-grid,
    .research-cards-grid,
    .card [style*="grid-template-columns"] {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }

    .card {
        padding: 16px 14px !important;
        border-radius: var(--radius-md);
    }

    .card h2,
    .card h3 {
        font-size: 1.1rem;
        gap: 8px;
        padding-bottom: 10px;
        margin-bottom: 14px;
    }

    .monitor,
    .info,
    .controls,
    .metric {
        padding: 14px 12px !important;
    }

    .controls {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 10px !important;
    }

    .control-group {
        width: 100%;
        display: flex;
        flex-direction: column;
        gap: 8px !important;
    }

    .control-group .btn,
    .control-group .baud-pill {
        width: 100%;
        justify-content: center;
        text-align: center;
    }

    .research-hero {
        padding: 20px 16px !important;
        margin-bottom: 16px;
    }

    .research-hero h2 {
        font-size: 1.35rem !important;
    }

    .research-stat-num {
        font-size: 1.45rem;
    }

    /* Research Hub Tabs cuộn ngang mượt */
    [role="tablist"] {
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    [role="tablist"]::-webkit-scrollbar {
        display: none;
    }

    .category-tab-btn {
        flex-shrink: 0;
        white-space: nowrap;
        min-height: 44px;
        padding: 10px 14px;
        font-size: 0.9rem;
    }

    /* ----------------------------------------------------------------------
       D. TỐI ƯU THAO TÁC CHẠM (TOUCH-FRIENDLY >= 44px, FONT >= 16px)
       Chống lỗi iOS Safari tự zoom khi bấm vào ô nhập
       ---------------------------------------------------------------------- */
    input:not([type="checkbox"]):not([type="radio"]):not([type="file"]),
    select,
    textarea {
        min-height: 44px !important;
        font-size: 16px !important;
    }

    button,
    .btn,
    .submit-btn,
    .tab-btn,
    .tool-toggle,
    .tool-select,
    .faq-question,
    a.btn {
        min-height: 44px !important;
    }

    .tool-select {
        font-size: 16px !important;
        padding: 8px 10px !important;
    }

    .tool-toggle {
        padding: 8px 12px !important;
        font-size: 0.85rem !important;
    }

    /* Bảng hỏi lâm sàng MDS-UPDRS (clinical_form.html) */
    .question-card {
        padding: 16px 14px !important;
        margin-bottom: 14px !important;
    }

    .question-card h3 {
        font-size: 1.05rem !important;
        margin-bottom: 12px !important;
    }

    .radio-group {
        display: flex !important;
        flex-direction: column !important;
        gap: 10px !important;
    }

    .radio-label {
        min-height: 48px !important;
        padding: 12px 14px !important;
        font-size: 16px !important;
        line-height: 1.45;
        gap: 12px !important;
        align-items: flex-start;
    }

    .radio-label input[type="radio"] {
        width: 22px !important;
        height: 22px !important;
        min-width: 22px !important;
        margin-top: 2px;
        flex-shrink: 0;
    }

    /* Trang đăng nhập / đăng ký (index.html) */
    .auth-body {
        padding: calc(74px + env(safe-area-inset-top, 0px)) 14px calc(24px + env(safe-area-inset-bottom, 0px)) !important;
        align-items: flex-start !important;
    }

    .auth-body > .a11y-toolbar {
        top: calc(12px + env(safe-area-inset-top, 0px)) !important;
        right: 12px !important;
    }

    .auth-header {
        padding: 26px 18px 20px !important;
    }

    .auth-header h1 {
        font-size: 1.38rem !important;
    }

    .auth-form {
        padding: 22px 16px !important;
    }

    .form-group {
        margin-bottom: 16px;
    }

    .form-group input {
        min-height: 48px !important;
        font-size: 16px !important;
    }

    .password-toggle-btn {
        min-width: 44px !important;
        min-height: 44px !important;
    }

    /* ----------------------------------------------------------------------
       E. CAMERA, VIDEO, ẢNH MRI & BIỂU ĐỒ SÓNG
       ---------------------------------------------------------------------- */
    /* Khung video/ảnh tự co giãn đúng tỷ lệ */
    #imageareview,
    #modalChartImg,
    #latestChartContainer img,
    #resultBox img,
    #uploadedVideoPreview {
        width: 100% !important;
        height: auto !important;
        max-height: 60vh;
        object-fit: contain;
    }

    /* Tremor camera overlay text gọn gàng trên màn hình hẹp */
    #overlayInstruction {
        font-size: 0.92rem !important;
        padding: 5px 12px !important;
        max-width: 92%;
    }

    #countdownText {
        font-size: 2.8rem !important;
    }

    #startBtn,
    #switchCamBtn {
        width: 100%;
        min-width: 0 !important;
        flex: 1 1 100% !important;
    }

    /* MRI Dropzone */
    #dropZone {
        min-height: 190px !important;
        padding: 18px 14px !important;
    }

    /* Biểu đồ sóng EEG & Heart/EMG */
    .shell .chart {
        width: 100% !important;
        height: 220px !important;
    }

    .shell .chart canvas,
    .shell .chart svg {
        width: 100% !important;
        height: 100% !important;
        display: block;
    }

    .shell .top {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 8px;
    }

    .shell .top-right,
    .shell .chart-toolbar {
        width: 100%;
        justify-content: flex-start;
    }

    .shell .legend-bar {
        flex-direction: column;
        align-items: flex-start;
        gap: 6px;
    }

    .shell .serial-log {
        max-width: 100%;
        font-size: 0.7rem;
    }

    /* ----------------------------------------------------------------------
       F. BẢNG DỮ LIỆU, MODAL & TOAST
       ---------------------------------------------------------------------- */
    .history-table th,
    .history-table td {
        padding: 10px 12px !important;
        font-size: 0.86rem !important;
    }

    .history-table th {
        min-width: 120px !important;
        white-space: nowrap;
    }

    /* Modal trên mobile */
    #resultModal,
    #chartModal {
        padding: 12px !important;
    }

    #resultModal > div,
    #chartModal > div {
        padding: 20px 16px !important;
        max-height: 90dvh;
        overflow-y: auto;
    }

    #resultModal h3,
    #chartModal h3 {
        font-size: 1.15rem !important;
    }

    .toast-container {
        left: 12px !important;
        right: 12px !important;
        bottom: calc(74px + env(safe-area-inset-bottom, 0px)) !important;
        max-width: none !important;
    }

    /* ----------------------------------------------------------------------
       G. TRANG PREVIEW MOBILE APP (mobile_app.html)
       ---------------------------------------------------------------------- */
    .mobile-preview-page .preview-header {
        flex-wrap: wrap;
        gap: 10px;
        padding: 12px 14px;
    }

    .mobile-preview-page .preview-main {
        display: grid;
        grid-template-columns: 1fr !important;
        gap: 20px;
        padding: 16px 14px calc(24px + env(safe-area-inset-bottom, 0px));
    }

    .mobile-preview-page .phone-frame {
        width: 100%;
        max-width: 360px;
        margin: 0 auto;
    }
}

/* Màn hình siêu nhỏ (360px - 400px) */
@media (max-width: 400px) {
    .mobile-nav-item {
        font-size: 0.6rem;
    }

    .mobile-nav-item i {
        font-size: 1rem;
    }

    .card {
        padding: 14px 12px !important;
    }

    .top-header h1 {
        font-size: 1.25rem !important;
    }
}
