.mobile-only { display: none !important; }

@media (max-width: 768px) {
    body { padding-bottom: calc(76px + env(safe-area-inset-bottom)); height: auto; min-height: 100dvh; }
    .mobile-only { display: block !important; }
    button, input, select, textarea { touch-action: manipulation; }
    button:focus-visible, [role="button"]:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
        outline: 3px solid #d97706; outline-offset: 2px;
    }
    .btn, .sidebar-menu li, .context-menu-item { min-height: 44px; }
    .btn { white-space: normal; line-height: 1.45; }
    input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]):not([type="file"]), select, textarea {
        min-height: 44px; font-size: 16px !important; max-width: 100%; min-width: 0 !important;
    }
    input[type="checkbox"], input[type="radio"] { min-width: 22px; min-height: 22px; flex-shrink: 0; }
    .sidebar {
        display: none !important; position: fixed; top: 0; right: 0; bottom: 0;
        width: min(360px, 92vw) !important; min-width: 0 !important; max-width: 92vw !important;
        height: 100dvh; padding: 16px !important; z-index: 1100; overflow-y: auto;
        padding-bottom: calc(24px + env(safe-area-inset-bottom)) !important;
    }
    .mobile-menu-open { overflow-y: hidden; }
    .mobile-menu-open .sidebar { display: flex !important; }
    .sidebar .brand { padding: 12px 0 !important; justify-content: center; }
    .sidebar-menu { flex-direction: column; }
    .sidebar-menu li { padding: 12px !important; font-size: 0.95rem !important; }
    .sidebar-info-container { display: flex; flex: none; }
    .sidebar div[style*="margin-top: auto"] { flex-direction: column; margin-top: 16px !important; }
    .mobile-menu-close { align-self: flex-end; background: white; color: #0b59a1; border: 0; border-radius: 8px; padding: 12px; font: inherit; }
    .mobile-menu-backdrop {
        position: fixed; inset: 0; width: 100%; height: 100dvh; background: rgb(15 23 42 / 55%);
        border: 0; z-index: 1090;
    }
    .mobile-menu-backdrop[hidden] { display: none !important; }
    .mobile-admin-header {
        display: flex !important; align-items: center; justify-content: space-between; gap: 12px;
        padding: 12px 16px; background: #0b59a1; color: white; font-weight: 700;
    }
    .mobile-admin-header span { min-width: 0; overflow-wrap: anywhere; }
    .mobile-admin-header button { min-height: 44px; flex-shrink: 0; padding: 8px 12px; border: 1px solid #b6d4fe; border-radius: 8px; background: transparent; color: white; font: inherit; }
    .main-wrapper { width: 100%; min-width: 0; }
    .top-header { padding: 12px 16px; gap: 10px; }
    .top-header h3 { font-size: 1rem; line-height: 1.5; }
    .device-status-badge { max-width: 100%; white-space: normal; }
    .tab-content { padding: 12px; }
    .card { padding: 14px; min-width: 0; }
    .card-title { flex-wrap: wrap; gap: 8px; line-height: 1.6; }
    .mobile-stack-grid { grid-template-columns: minmax(0, 1fr) !important; height: auto !important; }
    .dashboard-top-card { height: auto !important; max-height: none !important; gap: 16px; }
    .dashboard-calendar-layout > div { border-right: 0 !important; padding-right: 0 !important; height: auto !important; }
    #today-notice-board { min-height: 80px; }
    #calendar-month-title { font-size: 1rem !important; }
    .cal-day-cell[data-date] { min-height: 44px !important; font-size: 0.9rem !important; }
    .cal-day-cell:focus-visible { outline: 3px solid #d97706; outline-offset: 1px; }
    .filter-bar { padding: 10px; }
    .filter-bar input, .filter-bar select { width: 100% !important; }
    .filter-bar div[style*="margin-left: auto"] { gap: 8px; }
    .btn-group, .template-bar { flex-wrap: wrap; }
    .btn-group { gap: 6px; }
    #tab-sms .sms-tag-row {
        display: flex; flex-wrap: nowrap; align-items: center; gap: 4px; width: 100%; min-width: 0;
        overflow-x: auto; padding: 2px 0 6px; -webkit-overflow-scrolling: touch;
    }
    #tab-sms .sms-tag-row > span { flex-shrink: 0; font-size: 0.72rem !important; white-space: nowrap; }
    #tab-sms .sms-tag-row .btn { flex-shrink: 0; padding: 6px; font-size: 0.72rem; white-space: nowrap; }
    .mobile-student-tools { display: flex !important; align-items: center; flex-wrap: wrap; gap: 8px; margin: 12px 0; }
    .mobile-student-tools select { flex: 1; }
    .student-table-scroll { max-height: none !important; overflow: visible; border: 0; }
    .student-table-scroll:has(#student-list-tbody) table { display: block; }
    .student-table-scroll:has(#student-list-tbody) thead { display: none; }
    #student-list-tbody, .mobile-card-table tbody { display: grid; gap: 12px; }
    #student-list-tbody tr, .mobile-card-table tbody tr {
        display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px;
        padding: 12px; border: 1px solid #dce5ef; border-radius: 12px; background: white;
    }
    #student-list-tbody td, .mobile-card-table tbody td {
        display: block; padding: 0 !important; min-width: 0 !important; max-width: none !important;
        width: auto !important; border: 0; white-space: normal !important; overflow-wrap: anywhere; text-align: left !important;
    }
    #student-list-tbody td::before, .mobile-card-table tbody td::before {
        content: attr(data-mobile-label); display: block; color: #64748b; font-size: 0.75rem; font-weight: 500; margin-bottom: 4px;
    }
    #student-list-tbody { gap: 8px; }
    #student-list-tbody tr { grid-template-columns: 28px minmax(0, 1fr); gap: 8px; padding: 12px; }
    #student-list-tbody td, #student-list-tbody tr:hover td { background: transparent !important; }
    #student-list-tbody td:first-child { display: flex; align-items: center; justify-content: center; min-height: 44px; }
    #student-list-tbody td:first-child::before, #student-list-tbody td:nth-child(2)::before { display: none; }
    #student-list-tbody td:nth-child(2) { display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: 8px; align-items: center; font-size: 1rem; }
    #student-list-tbody td:nth-child(2) > b { grid-column: 1; line-height: 1.4; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    #student-list-tbody .mobile-student-summary { grid-column: 1; color: #64748b; font-size: 0.78rem; line-height: 1.5; }
    #student-list-tbody .mobile-student-toggle { grid-column: 2; grid-row: 1 / 3; padding: 8px 12px !important; font-size: 0.8rem !important; background: #eef4fb; color: #0b59a1; }
    #student-list-tbody td:nth-child(12), #student-list-tbody td[colspan], .mobile-card-table td[colspan] { grid-column: 1 / -1; }
    #student-list-tbody td:nth-child(12) { display: flex; flex-wrap: wrap; gap: 6px; }
    #student-list-tbody td:nth-child(12)::before { flex-basis: 100%; }
    #student-list-tbody .mobile-only { display: inline-flex !important; align-items: center; justify-content: center; }
    #student-list-tbody td.mobile-student-detail { display: none; }
    #student-list-tbody .mobile-student-expanded .mobile-student-detail { display: block; grid-column: 1 / -1; padding-top: 8px !important; border-top: 1px solid #edf2f7; }
    #student-list-tbody .mobile-student-expanded td:nth-child(2) > b { white-space: normal; overflow-wrap: anywhere; }
    #student-list-tbody .mobile-student-expanded td:nth-child(12) { display: flex; }
    #student-list-tbody .mobile-student-expanded td:nth-child(12) .btn { flex: 1; }
    #student-list-tbody td[colspan]::before { display: none; }
    #student-list-tbody td:nth-child(13) { grid-column: 1 / -1; }
    #student-list-tbody td:nth-child(13) select { width: auto !important; flex: 1; }
    .mobile-card-table { display: block; width: 100% !important; min-width: 0 !important; table-layout: auto !important; }
    .mobile-card-table thead { display: block; margin-bottom: 10px; }
    .mobile-card-table thead tr { display: flex; flex-wrap: wrap; gap: 6px; }
    .mobile-card-table thead th { display: none; }
    .mobile-card-table thead th[onclick] { display: flex; align-items: center; min-height: 44px; border: 1px solid #dce5ef; border-radius: 8px; padding: 8px !important; position: static; white-space: normal; }
    #finance-list-tbody { gap: 8px; }
    #finance-list-tbody tr { grid-template-columns: minmax(0, 1fr) 38px auto; align-items: center; gap: 6px; padding: 8px 10px; }
    #finance-list-tbody td, #finance-list-tbody tr:hover td { background: transparent !important; font-size: 0.82rem; }
    #finance-list-tbody td::before { display: none; }
    #finance-list-tbody td:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap !important; }
    #finance-list-tbody td:nth-child(2) { color: #64748b; white-space: nowrap !important; }
    #finance-list-tbody td:nth-child(3) { display: flex; align-items: center; gap: 4px; white-space: nowrap !important; }
    #finance-list-tbody .mobile-finance-toggle {
        display: inline-flex !important; align-items: center; justify-content: center; min-height: 44px; min-width: 44px;
        border: 0; border-radius: 8px; background: #eef4fb; color: #0b59a1; font-size: 1rem;
    }
    #finance-list-tbody td:nth-child(4) { display: none; }
    #finance-list-tbody .mobile-finance-expanded td:nth-child(4) { display: block; grid-column: 1 / -1; padding-top: 10px !important; border-top: 1px solid #edf2f7; }
    #finance-list-tbody .mobile-finance-expanded td:nth-child(4)::before { display: block; }
    #finance-list-tbody .mobile-finance-expanded td:first-child { overflow: visible; white-space: normal !important; }
    #finance-list-tbody td:nth-child(4) .badge { display: inline-flex; align-items: center; min-height: 44px; padding: 8px 12px; }
    .table-responsive:has(.mobile-card-table) { max-height: none !important; overflow: visible; }
    .mobile-sms-retry { display: inline-block !important; margin: 6px 0 0 6px; }
    .modal-overlay { padding: 0; align-items: stretch; height: 100dvh; z-index: 1200; }
    .modal-content { width: 100% !important; max-width: 100% !important; max-height: 100dvh !important; height: 100%; border-radius: 0; padding: 16px !important; padding-bottom: calc(24px + env(safe-area-inset-bottom)) !important; overflow-x: hidden; }
    .modal-content input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]), .modal-content textarea { width: 100%; }
    .modal-content div[style*="display: flex"] { flex-wrap: wrap; }
    .modal-content div[style*="display:flex"] { flex-wrap: wrap; }
    .custom-context-menu {
        position: fixed !important; top: auto !important; bottom: calc(80px + env(safe-area-inset-bottom));
        left: 12px !important; right: 12px; min-width: 0; max-height: 65dvh; overflow-y: auto; z-index: 1080;
    }
    .context-menu-item { padding: 12px 14px; }
    .mobile-context-close { width: 100%; min-height: 44px; border: 0; background: #eef4fb; color: #0b59a1; font: inherit; font-weight: 700; }
    .mobile-bottom-nav {
        display: grid !important; grid-template-columns: repeat(5, minmax(0, 1fr));
        position: fixed; left: 0; right: 0; bottom: 0; z-index: 1000; background: white;
        border-top: 1px solid #dce5ef; padding: 8px 4px calc(8px + env(safe-area-inset-bottom));
        box-shadow: 0 -3px 12px rgb(15 23 42 / 6%);
    }
    .mobile-bottom-nav button { min-height: 48px; border: 0; border-radius: 10px; background: white; color: #52647a; font: inherit; font-size: 0.85rem; font-weight: 700; }
    .mobile-bottom-nav button[aria-current="page"], .mobile-bottom-nav button[aria-expanded="true"] { color: #0b59a1; background: #e8f3ff; }
    #toast-container { bottom: calc(84px + env(safe-area-inset-bottom)); }
    #toast-install-popup { bottom: calc(84px + env(safe-area-inset-bottom)); max-width: calc(100vw - 32px); }
    #tab-finance .card-title > div, #tab-finance .card-title > div > div { flex-wrap: wrap; gap: 8px !important; }
    #tab-finance .mobile-stack-grid > div > div[style*="display: flex"] { flex-wrap: wrap; gap: 8px; }
    #collection-payment-root .cp-filter-bar { flex-wrap: wrap; white-space: normal; }
    #collection-payment-root .cp-filter-bar input { width: 100%; min-width: 0; }
    #collection-payment-root .cp-filter-bar button { min-height: 44px; }
    #collection-payment-root .cp-grid { grid-template-columns: minmax(0, 1fr); }
    #collection-payment-root .cp-grid label.wide { grid-column: auto; }
    #collection-invoice-modal, #collection-application-status-modal, #collection-invoice-manager-modal { padding: 0 !important; }
    .mobile-collection-panel { width: 100% !important; max-width: 100% !important; height: 100dvh; max-height: 100dvh !important; border-radius: 0 !important; padding: 16px !important; overflow-x: hidden !important; }
    .mobile-collection-panel button { min-height: 44px; }
    .mobile-collection-panel > div[style*="display:flex"], .mobile-collection-panel > div[style*="display: flex"] { flex-wrap: wrap; }
    .mobile-collection-panel .mobile-stack-grid > div { overflow-wrap: anywhere; }
}

@media print {
    .mobile-only { display: none !important; }
}
