/* ==========================================================================
   MOBILE RESPONSIVE - Tach rieng de khong anh huong desktop
   Load SAU tat ca CSS khac trong base.html
   ========================================================================== */

/* ===== MOBILE DETECT HELPER ===== */
:root {
    --taskbar-h: 50px;
    --titlebar-h: 40px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ==========================================================================
   BREAKPOINT 768px - TABLET & MOBILE
   ========================================================================== */
@media (max-width: 768px) {

    /* ----- BODY: cho phep scroll tren mobile ----- */
    body {
        overflow: auto !important;
        height: auto !important;
        min-height: 100vh;
        -webkit-overflow-scrolling: touch;
    }

    /* ----- DESKTOP AREA ----- */
    .desktop {
        position: relative !important;
        height: auto !important;
        min-height: calc(100vh - var(--taskbar-h));
        overflow: visible !important;
    }

    #desktop-icons {
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 15px;
        padding: 15px;
        justify-items: center;
    }

    .desktop-icon {
        width: 85px;
    }

    .desktop-icon .icon {
        width: 52px;
        height: 52px;
        font-size: 26px;
    }

    .desktop-icon .label {
        font-size: 11px;
    }

    /* ----- WINDOWS: luon full-screen tren mobile ----- */
    #windows-container {
        position: fixed !important;
        top: 0;
        left: 0;
        width: 100% !important;
        height: 100% !important;
        pointer-events: none;
        z-index: 100;
    }

    .window {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 100% !important;
        height: calc(100vh - var(--taskbar-h)) !important;
        max-width: none !important;
        transform: none !important;
        border-radius: 0 !important;
        margin: 0 !important;
    }

    .window.show {
        animation: mobileWindowOpen 0.25s ease forwards;
    }

    @keyframes mobileWindowOpen {
        from {
            opacity: 0;
            transform: translateY(30px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    .window.maximized {
        height: calc(100vh - var(--taskbar-h)) !important;
    }

    /* An resize handle & tat drag */
    .window-resize-handle {
        display: none !important;
    }

    .window-titlebar {
        cursor: default !important;
        height: 44px;
        padding: 0 10px;
    }

    .window-title {
        font-size: 13px;
    }

    /* An nut minimize & maximize (mobile luon full) */
    .window-controls button.minimize-btn,
    .window-controls button.maximize-btn {
        display: none;
    }

    /* Fallback: An 2 nut dau, chi giu nut Close */
    .window-controls button:first-child,
    .window-controls button:nth-child(2) {
        display: none;
    }
    .window-controls button:last-child {
        display: flex !important;
    }

    /* Window content */
    .window-content {
        padding: 12px !important;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .window-content h2 {
        font-size: 18px;
        margin-bottom: 12px;
    }

    .window-content h3 {
        font-size: 15px;
        margin: 12px 0 10px 0;
    }

    /* ----- TASKBAR ----- */
    .taskbar {
        height: var(--taskbar-h);
        padding: 0 5px;
        gap: 5px;
        padding-bottom: var(--safe-bottom);
    }

    /* An search box tren taskbar (da co trong Start Menu) */
    .search-box {
        display: none !important;
    }

    /* Thu gon taskbar apps */
    .taskbar-apps {
        gap: 2px;
    }

    .taskbar-app {
        min-width: 40px;
        height: 40px;
        padding: 0 6px;
    }

    /* An ten app, chi hien icon */
    .taskbar-app .app-name {
        display: none !important;
    }

    /* An close btn tren taskbar app (dung nut X trong window) */
    .taskbar-app .close-btn {
        display: none !important;
    }

    /* System tray */
    .system-tray {
        gap: 8px;
        padding: 0 5px;
        font-size: 14px;
    }

    /* An cac icon khong can thiet tren mobile */
    .system-tray > i.fa-wifi,
    .system-tray > i.fa-volume-up {
        display: none;
    }

    /* User info trong system tray */
    #userInfo {
        padding: 3px 6px !important;
        font-size: 11px;
    }

    #userInfo span {
        display: none;
    }

    #clock {
        font-size: 12px;
        min-width: 40px;
    }

    /* ----- STATS GRID ----- */
    .stats-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px !important;
        margin-bottom: 15px !important;
    }

    .stat-card {
        padding: 15px !important;
    }

    .stat-card h3 {
        font-size: 24px !important;
        margin: 5px 0 !important;
    }

    .stat-card p {
        font-size: 11px !important;
    }

    .stat-card i {
        font-size: 20px;
    }

    /* ----- INLINE GRID OVERRIDES (filter bars, stat summaries) ----- */
    [style*="grid-template-columns: 2fr 1fr 1fr 1fr auto"],
    [style*="grid-template-columns: 2fr 1fr 1fr 1fr 1fr"],
    [style*="grid-template-columns: repeat(4"],
    [style*="grid-template-columns: repeat(5"],
    [style*="grid-template-columns: repeat(3"] {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
    }

    /* 2-column cho summary stats */
    [style*="grid-template-columns: repeat(3, 1fr)"] {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    /* ----- TABLES: scroll ngang + compact ----- */
    [style*="overflow-x: auto"],
    .table-responsive,
    .table-wrapper {
        -webkit-overflow-scrolling: touch;
        margin: 0 -12px;
        padding: 0 12px;
    }

    #studentsTable,
    #usersTable,
    .grades-table {
        font-size: 12px;
    }

    #studentsTable th,
    #studentsTable td,
    #usersTable th,
    #usersTable td {
        padding: 8px 6px !important;
        white-space: nowrap;
    }

    /* An cot khong quan trong tren mobile: Email, Phone */
    #studentsTable th:nth-child(7),
    #studentsTable td:nth-child(7),
    #studentsTable th:nth-child(8),
    #studentsTable td:nth-child(8) {
        display: none;
    }

    /* ----- BUTTONS ----- */
    .btn {
        padding: 8px 12px;
        font-size: 12px;
    }

    .btn-sm {
        padding: 5px 8px;
        font-size: 11px;
    }

    /* Button rows: wrap */
    .window-content > div > .btn,
    [style*="display: flex"][style*="gap"] > .btn {
        margin-bottom: 5px;
    }

    /* ----- PAGINATION ----- */
    [style*="justify-content: space-between"] {
        flex-wrap: wrap;
        gap: 8px;
    }

    /* ----- MODALS: full-screen tren mobile ----- */

    /* Student add/edit modal (inline 700px) */
    #studentModal > div > div,
    #userModal > div > div {
        width: 95vw !important;
        max-width: none !important;
        max-height: 90vh !important;
        border-radius: 12px !important;
        margin: auto;
    }

    /* Login modal (inline 450px) */
    #loginModal > div > div {
        width: 95vw !important;
        max-width: none !important;
        border-radius: 16px !important;
    }

    #loginModal [style*="padding: 40px"] {
        padding: 25px 20px !important;
    }

    #loginModal [style*="font-size: 64px"] {
        font-size: 40px !important;
    }

    /* Student profile modal (inline ~1000px) */
    .modal-container {
        width: 100% !important;
        height: 100% !important;
        max-width: none !important;
        max-height: none !important;
        border-radius: 0 !important;
    }

    .modal-body {
        flex-direction: column !important;
    }

    .profile-sidebar {
        width: 100% !important;
        max-height: 180px;
        border-right: none !important;
        border-bottom: 1px solid #e0e0e0;
        overflow-y: auto;
    }

    .profile-sidebar .student-avatar {
        width: 60px;
        height: 60px;
        font-size: 24px;
    }

    .tab-buttons {
        overflow-x: auto;
        flex-wrap: nowrap !important;
        -webkit-overflow-scrolling: touch;
        gap: 0 !important;
    }

    .tab-btn {
        padding: 8px 6px !important;
        font-size: 11px !important;
        white-space: nowrap;
        flex-shrink: 0;
    }

    .tab-btn i {
        display: block;
        margin: 0 0 2px 0 !important;
    }

    /* Tab content */
    .tab-content {
        padding: 12px !important;
    }

    /* Info cards grid trong profile */
    .info-grid,
    [style*="grid-template-columns"][style*="info"] {
        grid-template-columns: 1fr !important;
    }

    .info-card {
        padding: 10px !important;
    }

    /* Grades table trong modal */
    .grades-table th,
    .grades-table td {
        padding: 6px 4px;
        font-size: 11px;
    }

    /* Step indicator cho multi-step form */
    .step-indicator {
        gap: 5px;
    }

    .step-indicator .step-label {
        display: none;
    }

    /* Form inputs */
    #studentForm input,
    #studentForm select,
    #studentForm textarea,
    #userForm input,
    #userForm select {
        font-size: 16px !important; /* Prevent iOS zoom on focus */
    }

    /* ----- TOUCH-FRIENDLY TARGETS ----- */
    button, .btn, a, select, input[type="checkbox"], input[type="radio"] {
        min-height: 36px;
    }

    /* Active state cho touch feedback */
    .btn:active,
    .taskbar-app:active,
    .start-btn:active,
    .desktop-icon:active {
        opacity: 0.7;
        transform: scale(0.95);
    }

    /* ----- START MENU: full-screen overlay tren mobile ----- */
    .start-menu {
        position: fixed !important;
        bottom: var(--taskbar-h) !important;
        left: 0 !important;
        width: 100% !important;
        height: calc(100vh - var(--taskbar-h)) !important;
        max-height: none !important;
        border-radius: 0 !important;
    }

    .start-apps-grid {
        grid-template-columns: repeat(3, 1fr) !important;
    }

    .start-search {
        margin: 8px 12px;
    }

    .start-search input {
        font-size: 16px !important;
    }

    /* ==========================================================
       GRADEBOOK (So diem) - Mobile Responsive
       ========================================================== */

    /* --- NUT TOGGLE BO LOC --- */
    #gbFilterToggle {
        display: flex !important;
    }

    /* --- AN BO LOC mac dinh tren mobile --- */
    #gbFilterPanel {
        display: none !important;
        padding: 10px 12px !important;
    }

    #gbFilterPanel.gb-filter-open {
        display: block !important;
    }

    /* Bo loc khi mo: xep doc */
    #gbFilterPanel > div:first-child {
        flex-direction: column !important;
        gap: 8px !important;
    }

    #gbFilterPanel > div:first-child > div {
        width: 100%;
    }

    #gbFilterPanel select {
        width: 100% !important;
        min-width: unset !important;
        padding: 10px 12px !important;
        font-size: 14px !important;
    }

    #gbFilterPanel label {
        font-size: 12px !important;
        min-width: 55px;
    }

    /* Hang 2: si so + reset */
    #gbFilterPanel > div:nth-child(2) {
        margin-top: 8px !important;
        padding-top: 8px !important;
        gap: 8px !important;
    }

    #gbAdminStats {
        margin-left: 0 !important;
        padding-left: 0 !important;
        border-left: none !important;
    }

    /* Info bar */
    #gbInfoBar {
        margin-top: 6px !important;
        padding-top: 6px !important;
        font-size: 11px !important;
    }

    #gbInfoBar span + span {
        margin-left: 0 !important;
    }

    /* --- BANG DIEM MOBILE: Split-table approach --- */
    /* JS se tach gbTable thanh 2 bang: trai (co dinh) + phai (cuon ngang) */

    /* ===== SPLIT-TABLE LAYOUT ===== */

    /* Wrapper: chi la flex container, KHONG scroll */
    .gb-split-wrapper {
        display: flex !important;
        width: 100%;
    }

    /* Bang trai: co dinh, KHONG scroll */
    .gb-split-left {
        flex-shrink: 0;
        box-shadow: 3px 0 8px rgba(0,0,0,0.15);
        z-index: 2;
    }

    .gb-split-left table {
        border-collapse: collapse;
        font-size: 12px;
    }

    .gb-split-left th {
        background: #2c3e50;
        color: white;
        padding: 10px 6px;
        border-bottom: 1px solid #e0e0e0;
    }

    .gb-split-left td {
        padding: 8px 6px;
        border-bottom: 1px solid #e0e0e0;
        background: white;
        white-space: nowrap;
    }

    /* Bang phai: CHI cuon ngang */
    .gb-split-right {
        flex: 1;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .gb-split-right table {
        border-collapse: collapse;
        font-size: 12px;
    }

    .gb-split-right th {
        padding: 10px 5px;
        border-bottom: 1px solid #e0e0e0;
        white-space: nowrap;
    }

    .gb-split-right td {
        padding: 8px 5px;
        border-bottom: 1px solid #e0e0e0;
        white-space: nowrap;
    }

    /* An bang goc khi split active */
    #gbTableContainer.gb-split-active > #gbTableScroll {
        display: none !important;
    }

    /* O nhap diem */
    .gb-split-right input[type="text"],
    .gb-split-right input[type="number"] {
        width: 45px !important;
        padding: 6px 2px !important;
        font-size: 13px !important;
        text-align: center;
    }

    /* Vung chua bang: cuon DOC binh thuong */
    #gradebookWindow [style*="flex: 1; overflow: auto"] {
        padding: 4px !important;
    }

    /* --- ANALYSIS DASHBOARD --- */
    #gbAnalysisDashboard {
        padding: 10px 12px 0 12px !important;
    }

    #gbAnalysisDashboard > div:first-child {
        flex-wrap: wrap !important;
        gap: 8px !important;
    }

    #gbAnalysisDashboard > div:nth-child(2) {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
    }

    #gbAnalysisDashboard > div:nth-child(2) > div {
        padding: 12px !important;
    }

    #gbAnalysisDashboard > div:nth-child(2) > div > div:nth-child(3) {
        font-size: 22px !important;
    }

    #analysisChartsGrid {
        grid-template-columns: 1fr !important;
    }

    /* --- FOOTER NUT CHUC NANG --- */
    #gradebookWindow > .window-content [style*="background: white"][style*="border-top"] {
        padding: 8px 10px !important;
        gap: 6px !important;
        flex-wrap: wrap !important;
    }

    #gradebookWindow > .window-content [style*="background: white"][style*="border-top"] button {
        padding: 8px 12px !important;
        font-size: 12px !important;
        flex: 1;
        min-width: 0;
        justify-content: center;
    }

    #gradebookWindow > .window-content [style*="background: white"][style*="border-top"] > div:last-child {
        width: 100%;
        flex-wrap: wrap !important;
        gap: 8px !important;
        justify-content: center;
        font-size: 11px !important;
    }

    /* Placeholder */
    #gbPlaceholder i {
        font-size: 50px !important;
    }

    #gbPlaceholder h3 {
        font-size: 14px !important;
        text-align: center;
        padding: 0 10px;
    }
}

/* ==========================================================================
   BREAKPOINT 480px - SMALL PHONES
   ========================================================================== */
@media (max-width: 480px) {

    #desktop-icons {
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 10px;
        padding: 10px;
    }

    .desktop-icon .icon {
        width: 46px;
        height: 46px;
        font-size: 22px;
    }

    .desktop-icon .label {
        font-size: 10px;
    }

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

    .stat-card h3 {
        font-size: 20px !important;
    }

    /* Summary stats: 1 column */
    [style*="grid-template-columns: repeat(3, 1fr)"] {
        grid-template-columns: 1fr !important;
    }

    #studentsTable,
    #usersTable {
        font-size: 11px;
    }

    #studentsTable th,
    #studentsTable td,
    #usersTable th,
    #usersTable td {
        padding: 6px 4px !important;
    }

    /* An them cot tren dien thoai nho */
    #studentsTable th:nth-child(6),
    #studentsTable td:nth-child(6),
    #studentsTable th:nth-child(10),
    #studentsTable td:nth-child(10) {
        display: none;
    }

    .window-content {
        padding: 8px !important;
    }

    .window-content h2 {
        font-size: 16px;
    }

    .btn {
        padding: 7px 10px;
        font-size: 11px;
        gap: 4px;
    }

    /* Login modal nho hon */
    #loginModal > div > div {
        width: 98vw !important;
    }

    .start-apps-grid {
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 10px !important;
    }

    /* Gradebook 480px */
    #gbTable {
        min-width: 600px !important;
        font-size: 11px !important;
    }

    #gbTable th,
    #gbTable td {
        padding: 6px 3px !important;
    }

    #gbTable input[type="text"],
    #gbTable input[type="number"] {
        width: 40px !important;
        font-size: 12px !important;
    }

    .gb-split-left table,
    .gb-split-right table {
        font-size: 11px !important;
    }

    .gb-split-right input[type="text"],
    .gb-split-right input[type="number"] {
        width: 40px !important;
        font-size: 12px !important;
    }

    /* Footer buttons: 2 per row */
    #gradebookWindow > .window-content [style*="background: white"][style*="border-top"] button {
        flex: 0 0 calc(50% - 4px) !important;
        font-size: 11px !important;
        padding: 7px 8px !important;
    }
}

/* ==========================================================================
   START MENU STYLES (hien dang thieu hoan toan)
   ========================================================================== */
.start-menu {
    position: fixed;
    bottom: 50px; /* taskbar height */
    left: 0;
    width: 400px;
    max-height: 500px;
    background: rgba(30, 30, 30, 0.96);
    backdrop-filter: blur(20px);
    border-radius: 12px 12px 0 0;
    color: white;
    display: none;
    flex-direction: column;
    z-index: 999;
    box-shadow: 0 -8px 40px rgba(0,0,0,0.5);
    overflow: hidden;
    animation: startMenuOpen 0.2s ease;
}

@keyframes startMenuOpen {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.start-menu.show {
    display: flex;
}

/* Start Header */
.start-header {
    padding: 20px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}

.start-header .user-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.start-header .user-avatar {
    font-size: 36px;
    opacity: 0.9;
}

.start-header .user-name {
    font-size: 16px;
    font-weight: 600;
}

/* Start Search */
.start-search {
    margin: 12px 16px;
    display: flex;
    align-items: center;
    gap: 10px;
    background: rgba(255,255,255,0.1);
    border-radius: 8px;
    padding: 8px 12px;
}

.start-search i {
    color: rgba(255,255,255,0.5);
}

.start-search input {
    flex: 1;
    background: none;
    border: none;
    color: white;
    outline: none;
    font-size: 14px;
}

.start-search input::placeholder {
    color: rgba(255,255,255,0.4);
}

/* Start Content */
.start-content {
    flex: 1;
    overflow-y: auto;
    padding: 12px 16px;
}

.start-content h4 {
    font-size: 13px;
    color: rgba(255,255,255,0.6);
    margin-bottom: 12px;
    font-weight: 500;
}

/* Start Apps Grid */
.start-apps-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
}

.start-apps-grid .start-app {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    padding: 12px 8px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s;
    text-align: center;
}

.start-apps-grid .start-app:hover {
    background: rgba(255,255,255,0.1);
}

.start-apps-grid .start-app:active {
    background: rgba(255,255,255,0.15);
    transform: scale(0.95);
}

.start-apps-grid .start-app i {
    font-size: 24px;
}

.start-apps-grid .start-app span {
    font-size: 11px;
    opacity: 0.9;
    word-break: break-word;
}

/* Start Footer */
.start-footer {
    display: flex;
    justify-content: space-around;
    padding: 12px 16px;
    border-top: 1px solid rgba(255,255,255,0.1);
}

.start-footer button {
    background: none;
    border: none;
    color: rgba(255,255,255,0.7);
    cursor: pointer;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 12px;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 6px;
}

.start-footer button:hover {
    background: rgba(255,255,255,0.1);
    color: white;
}

.start-footer button:active {
    background: rgba(255,255,255,0.15);
}

/* ==========================================================================
   LANDSCAPE MOBILE (ngang)
   ========================================================================== */
@media (max-width: 768px) and (orientation: landscape) {
    .window {
        height: calc(100vh - var(--taskbar-h)) !important;
    }

    .start-menu {
        height: calc(100vh - var(--taskbar-h)) !important;
        max-height: none !important;
    }

    .profile-sidebar {
        max-height: 120px !important;
    }
}

/* ==========================================================================
   PRINT: khong in taskbar / start menu
   ========================================================================== */
@media print {
    .taskbar,
    .start-menu,
    #desktop-icons,
    .overlay {
        display: none !important;
    }

    .window {
        position: static !important;
        width: 100% !important;
        height: auto !important;
        transform: none !important;
        box-shadow: none !important;
    }

    .window-titlebar {
        background: #333 !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
}
