/**
 * PWA & Responsive Styles
 * GES Enrollment System
 * Works on Desktop, Tablet, and Mobile
 */

/* ==========================================
   MOBILE SCROLLING FIX - Allow vertical & horizontal scroll ONLY in content areas
   ========================================== */
html {
    height: auto !important;
}

body {
    height: auto !important;
    overflow-y: auto !important;
    overflow-x: hidden !important; /* Prevent page-wide horizontal scroll */
    -webkit-overflow-scrolling: touch;
}

/* Don't override flex display on login page body */
body:has(.login-container) {
    display: flex !important;
    justify-content: center !important;
    align-items: center !important;
    height: 100vh !important;
    overflow: hidden !important;
}

/* Form containers MUST be scrollable */
.form-container,
.page-container,
.modal-content {
    height: auto !important;
    max-height: none !important;
    overflow: visible !important;
    display: block !important;
    -webkit-overflow-scrolling: touch;
}

/* Login container should maintain its flex-ready state */
.login-container {
    height: auto !important;
    max-height: none !important;
    overflow: visible !important;
    -webkit-overflow-scrolling: touch;
}

/* Main content area scrollable VERTICALLY ONLY */
.main-content {
    overflow-y: auto !important;
    overflow-x: hidden !important; /* Prevent page-wide horizontal scroll */
    height: auto !important;
    -webkit-overflow-scrolling: touch;
}

/* ONLY tables and form-heavy sections scroll horizontally */
table,
.table-wrapper,
.enrollment-form,
.enrollment-container,
.form-wrapper-horizontal {
    overflow-x: auto !important;
    overflow-y: visible !important;
    -webkit-overflow-scrolling: touch;
}

/* ==========================================
   STICKY SAVE BUTTONS - Always visible
   ========================================== */
.btn-group.sticky-save-buttons,
.sticky-save-buttons {
    position: sticky;
    bottom: 0;
    background: linear-gradient(to top, white 70%, rgba(255,255,255,0.9));
    padding: 15px 20px;
    margin: 20px -20px -20px -20px;
    border-top: 1px solid #e0e0e0;
    z-index: 100;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
    box-shadow: 0 -4px 20px rgba(0,0,0,0.08);
}

.sticky-save-buttons .btn {
    flex-shrink: 0;
}

.sticky-save-buttons .auto-save-indicator {
    padding: 10px 15px;
    background: #e8f5e9;
    border-radius: 8px;
    color: #2e7d32;
    font-size: 13px;
    flex: 1;
    min-width: 200px;
    text-align: center;
}

/* Float action button for mobile save */
.floating-save-btn {
    position: fixed;
    bottom: 80px;
    right: 20px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
    font-size: 24px;
    cursor: pointer;
    z-index: 999;
    display: none;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
}

.floating-save-btn:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 25px rgba(102, 126, 234, 0.5);
}

.floating-save-btn:active {
    transform: scale(0.95);
}

.floating-save-btn.saving {
    animation: pulse-save 1s infinite;
}

@keyframes pulse-save {
    0%, 100% { box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4); }
    50% { box-shadow: 0 4px 25px rgba(46, 125, 50, 0.6); }
}

/* ==========================================
   PWA Install Banner - Works on ALL devices  
   ========================================== */
#pwa-install-banner,
#ios-install-banner {
    position: fixed;
    bottom: -120px;
    left: 50%;
    transform: translateX(-50%);
    width: calc(100% - 40px);
    max-width: 600px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 18px 25px;
    z-index: 9999;
    box-shadow: 0 -4px 30px rgba(0,0,0,0.25), 0 0 0 1px rgba(255,255,255,0.1);
    transition: bottom 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
    border-radius: 16px;
    margin-bottom: 20px;
}

#pwa-install-banner.show,
#ios-install-banner.show {
    bottom: 20px;
}

/* Desktop positioning - bottom right corner */
@media screen and (min-width: 769px) {
    #pwa-install-banner,
    #ios-install-banner {
        left: auto;
        right: 20px;
        transform: none;
        width: auto;
        min-width: 400px;
        max-width: 500px;
        bottom: -150px;
    }
    
    #pwa-install-banner.show,
    #ios-install-banner.show {
        bottom: 20px;
    }
}

.pwa-banner-content {
    display: flex;
    align-items: center;
    gap: 15px;
    max-width: 600px;
    margin: 0 auto;
}

.pwa-banner-icon {
    font-size: 40px;
    flex-shrink: 0;
}

.pwa-banner-text {
    flex: 1;
}

.pwa-banner-text strong {
    display: block;
    font-size: 16px;
    margin-bottom: 3px;
}

.pwa-banner-text p {
    font-size: 13px;
    opacity: 0.9;
    margin: 0;
}

.pwa-banner-actions {
    display: flex;
    gap: 10px;
    flex-shrink: 0;
}

.pwa-btn-install {
    background: white;
    color: #667eea;
    border: none;
    padding: 10px 20px;
    border-radius: 25px;
    font-weight: 600;
    cursor: pointer;
    font-size: 14px;
    transition: transform 0.2s;
}

.pwa-btn-install:hover {
    transform: scale(1.05);
}

.pwa-btn-dismiss {
    background: transparent;
    color: white;
    border: 1px solid rgba(255,255,255,0.5);
    padding: 10px 15px;
    border-radius: 25px;
    cursor: pointer;
    font-size: 13px;
}

.ios-banner .pwa-btn-dismiss {
    padding: 5px 10px;
    border: none;
    font-size: 18px;
}

/* Mobile-First Responsive Design */
@media screen and (max-width: 768px) {
    /* Root variables for mobile */
    :root {
        --sidebar-width: 280px;
        --header-height: 60px;
        --mobile-padding: 12px;
    }
    
    /* Sticky save buttons for mobile */
    .sticky-save-buttons {
        position: fixed;
        bottom: env(safe-area-inset-bottom, 0);
        left: 0;
        right: 0;
        margin: 0;
        border-radius: 0;
        padding: 12px 15px;
        padding-bottom: max(12px, env(safe-area-inset-bottom));
        flex-direction: row;
        justify-content: center;
        gap: 8px;
        background: white;
        box-shadow: 0 -4px 20px rgba(0,0,0,0.15);
        z-index: 999 !important;
        width: 100%;
        display: flex !important;
        flex-wrap: wrap;
    }
    
    .sticky-save-buttons .btn {
        flex: 1 1 calc(50% - 4px);
        min-width: 120px;
        padding: 12px 15px;
        font-size: 14px;
        font-weight: 500;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        border: none;
        border-radius: 6px;
        transition: all 0.2s ease;
        -webkit-user-select: none;
        user-select: none;
        -webkit-appearance: none;
        appearance: none;
    }
    
    .sticky-save-buttons .btn:active {
        transform: scale(0.98);
        opacity: 0.9;
    }
    
    .sticky-save-buttons .auto-save-indicator {
        display: none;
    }
    
    /* Show floating save button on mobile instead */
    .floating-save-btn {
        display: none;
    }
    
    /* Body adjustments */
    body {
        font-size: 14px;
        -webkit-text-size-adjust: 100%;
        -webkit-tap-highlight-color: transparent;
        overflow-y: auto !important;
        min-height: 100vh;
    }
    
    /* Form and Container Scrolling */
    .login-container {
        display: flex !important;
        flex-direction: column !important;
        grid-template-columns: 1fr !important;
        max-height: none !important;
        height: auto !important;
        overflow: visible !important;
        min-height: 100vh;
    }
    
    .login-box {
        padding: 20px 15px !important;
        overflow: visible !important;
        height: auto !important;
    }
    
    .form-container,
    .page-container {
        height: auto !important;
        max-height: none !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
    }
    
    form {
        overflow: visible !important;
    }
    
    /* Dashboard layout */
    .dashboard {
        flex-direction: column;
    }
    
    /* Sidebar mobile */
    .sidebar {
        position: fixed;
        width: var(--sidebar-width);
        height: 100vh;
        height: calc(var(--vh, 1vh) * 100);
        transform: translateX(-100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: 1000;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .sidebar.active {
        transform: translateX(0);
    }
    
    /* Main content mobile */
    .main-content {
        margin-left: 0 !important;
        padding: var(--mobile-padding);
        padding-top: 70px;
        padding-bottom: 80px; /* Space for fixed save buttons */
        min-height: 100vh;
        min-height: calc(var(--vh, 1vh) * 100);
        width: 100%;
    }
    
    /* PWA Banner mobile positioning */
    #pwa-install-banner.show,
    #ios-install-banner.show {
        bottom: 70px; /* Above fixed save buttons */
    }
    
    /* Page header mobile */
    .page-header,
    .header {
        padding: 15px;
        margin-bottom: 15px;
        border-radius: 10px;
    }
    
    .page-header h1,
    .header h1 {
        font-size: 20px !important;
        margin-bottom: 5px;
    }
    
    .header-actions {
        margin-top: 10px;
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }
    
    .header-actions .btn {
        padding: 8px 12px;
        font-size: 12px;
        flex: 1;
        min-width: 100px;
        text-align: center;
    }
    
    /* Stats grid mobile */
    .stats-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px;
    }
    
    .stat-card {
        padding: 15px;
        border-radius: 10px;
    }
    
    .stat-card h3 {
        font-size: 11px;
        margin-bottom: 5px;
    }
    
    .stat-card .stat-value,
    .stat-card p.stat-value {
        font-size: 22px !important;
    }
    
    .stat-card .stat-label {
        font-size: 10px;
    }
    
    /* Cards mobile */
    .card {
        border-radius: 10px;
        margin-bottom: 15px;
        overflow: hidden;
    }
    
    .card-header {
        padding: 12px 15px;
        flex-direction: column;
        gap: 10px;
    }
    
    .card-header h2,
    .card-header h3 {
        font-size: 16px;
    }
    
    .card-body {
        padding: 15px;
        padding-bottom: calc(15px + 100px);
    }
    
    /* Tables mobile */
    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: 0 -15px;
        padding: 0 15px;
    }
    
    table {
        font-size: 12px;
        min-width: 600px;
    }
    
    table th,
    table td {
        padding: 10px 8px;
        white-space: nowrap;
    }
    
    /* Data table mobile view */
    .data-table-mobile {
        display: block;
    }
    
    .data-table-mobile thead {
        display: none;
    }
    
    .data-table-mobile tbody tr {
        display: block;
        margin-bottom: 15px;
        background: white;
        border-radius: 10px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        padding: 15px;
    }
    
    .data-table-mobile tbody td {
        display: flex;
        justify-content: space-between;
        padding: 8px 0;
        border-bottom: 1px solid #f0f0f0;
    }
    
    .data-table-mobile tbody td:last-child {
        border-bottom: none;
    }
    
    .data-table-mobile tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #666;
    }
    
    /* Forms mobile */
    .form-group {
        margin-bottom: 15px;
    }
    
    .form-group label {
        font-size: 13px;
        margin-bottom: 5px;
    }
    
    .form-control,
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="tel"],
    select,
    textarea {
        padding: 12px 15px;
        font-size: 16px !important; /* Prevents zoom on iOS */
        border-radius: 8px;
        width: 100%;
        -webkit-appearance: none;
        appearance: none;
    }
    
    .form-row {
        grid-template-columns: 1fr !important;
        gap: 15px;
    }
    
    /* Horizontal scrolling ONLY for tables - wrap them */
    .table-wrapper {
        overflow-x: auto !important;
        overflow-y: visible !important;
        -webkit-overflow-scrolling: touch;
        width: 100%;
        margin-bottom: 15px;
        border-radius: 8px;
        border: 1px solid #e0e0e0;
        display: block !important;
    }
    
    .table-wrapper table {
        width: 100% !important;
    }
    
    table {
        width: 100%;
        min-width: 800px !important;
        border-collapse: collapse;
    }
    
    table thead,
    table tbody,
    table tfoot {
        display: table;
        width: 100%;
    }
    
    table th,
    table td {
        padding: 12px 8px;
        min-width: 80px;
    }
    
    /* Enrollment form specific */
    .enrollment-form,
    .enrollment-container {
        width: 100%;
        overflow-x: auto !important;
    }
    
    .enrollment-form table {
        min-width: 900px !important;
    }
    
    /* Form inputs in tables must be visible */
    .table-input {
        width: 100% !important;
        min-width: 70px !important;
        padding: 8px 4px !important;
        font-size: 14px !important;
    }
    
    .enrollment-fields {
        display: grid;
        grid-template-columns: 1fr;
        gap: 15px;
    }
    
    /* Buttons mobile */
    .btn {
        padding: 12px 20px;
        font-size: 14px;
        border-radius: 8px;
        min-height: 44px; /* Apple's recommended touch target */
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
    }
    
    .btn-primary,
    .btn-success,
    .btn-danger {
        width: 100%;
        margin-bottom: 10px;
    }
    
    /* Sidebar toggle button */
    .sidebar-toggle {
        display: flex !important;
        position: fixed;
        top: 12px;
        left: 12px;
        z-index: 1001;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border: none;
        border-radius: 10px;
        width: 44px;
        height: 44px;
        min-width: 44px;
        min-height: 44px;
        font-size: 20px;
        cursor: pointer;
        box-shadow: 0 4px 15px rgba(0,0,0,0.2);
        align-items: center;
        justify-content: center;
        padding: 0 !important;
        margin: 0 !important;
    }
    
    .sidebar-toggle:active {
        transform: scale(0.95);
    }
    
    /* Sidebar overlay */
    .sidebar-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0,0,0,0.5);
        z-index: 999;
        opacity: 0;
        transition: opacity 0.3s ease;
    }
    
    .sidebar-overlay.active {
        display: block;
        opacity: 1;
    }
    
    /* Menu items mobile */
    .menu-item {
        padding: 14px 20px;
        font-size: 14px;
    }
    
    .menu-item i {
        font-size: 18px;
        margin-right: 12px;
    }
    
    /* Ownership filter mobile */
    .ownership-filter-sidebar {
        padding: 12px 15px !important;
    }
    
    .ownership-filter-sidebar select {
        padding: 10px 12px !important;
        font-size: 14px !important;
    }
    
    /* Charts mobile */
    .chart-container {
        padding: 15px;
        min-height: 250px;
    }
    
    /* Modals mobile */
    .modal-content {
        margin: 10px;
        max-height: 90vh;
        max-height: calc(var(--vh, 1vh) * 90);
        overflow-y: auto;
        border-radius: 15px;
    }
    
    .modal-header {
        padding: 15px;
        position: sticky;
        top: 0;
        background: white;
        z-index: 1;
    }
    
    .modal-body {
        padding: 15px;
    }
    
    .modal-footer {
        padding: 15px;
        position: sticky;
        bottom: 0;
        background: white;
    }
    
    /* Alerts mobile */
    .alert {
        padding: 12px 15px;
        font-size: 13px;
        border-radius: 8px;
        margin-bottom: 15px;
    }
    
    /* Badges */
    .badge {
        padding: 4px 10px;
        font-size: 11px;
        border-radius: 12px;
    }
    
    /* Login page mobile */
    .login-page {
        padding: 15px;
    }
    
    .login-container {
        border-radius: 15px;
    }
    
    .login-box {
        padding: 30px 20px;
    }
    
    .login-header h1 {
        font-size: 22px;
    }
    
    /* PWA Banner adjustments */
    .pwa-banner-content {
        flex-wrap: wrap;
    }
    
    .pwa-banner-actions {
        width: 100%;
        justify-content: center;
        margin-top: 10px;
    }
}

/* Extra small devices */
@media screen and (max-width: 480px) {
    .stats-grid {
        grid-template-columns: 1fr !important;
    }
    
    .stat-card {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
    
    .stat-card h3 {
        margin-bottom: 0;
    }
    
    .stat-card .stat-value {
        font-size: 24px !important;
    }
    
    .header-actions {
        flex-direction: column;
    }
    
    .header-actions .btn {
        width: 100%;
    }
    
    .card-header {
        text-align: center;
    }
    
    .card-header input[type="text"] {
        width: 100%;
    }
}

/* Landscape orientation */
@media screen and (max-width: 768px) and (orientation: landscape) {
    .sidebar {
        width: 250px;
    }
    
    .main-content {
        padding-top: 60px;
    }
    
    .stats-grid {
        grid-template-columns: repeat(4, 1fr) !important;
    }
    
    .stat-card {
        padding: 10px;
    }
    
    .stat-card .stat-value {
        font-size: 18px !important;
    }
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {
    /* Larger touch targets */
    .btn,
    .menu-item,
    input,
    select,
    textarea {
        min-height: 44px;
    }
    
    /* Remove hover effects that don't work on touch */
    .btn:hover,
    .menu-item:hover,
    .card:hover {
        transform: none;
    }
    
    /* Add active states instead */
    .btn:active {
        transform: scale(0.98);
        opacity: 0.9;
    }
    
    .menu-item:active {
        background: #f0f0f0;
    }
    
    /* Smooth scrolling */
    * {
        -webkit-overflow-scrolling: touch;
    }
    
    /* Prevent text selection on buttons */
    .btn,
    .menu-item {
        -webkit-user-select: none;
        user-select: none;
    }
}

/* Safe area insets for notched phones */
@supports (padding: max(0px)) {
    .main-content {
        padding-left: max(var(--mobile-padding), env(safe-area-inset-left));
        padding-right: max(var(--mobile-padding), env(safe-area-inset-right));
        padding-bottom: max(var(--mobile-padding), env(safe-area-inset-bottom));
    }
    
    #pwa-install-banner,
    #ios-install-banner {
        padding-bottom: max(15px, env(safe-area-inset-bottom));
    }
    
    .sidebar {
        padding-bottom: env(safe-area-inset-bottom);
    }
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
    /* Add dark mode styles if needed */
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Print styles - hide mobile elements */
@media print {
    .sidebar-toggle,
    .sidebar-overlay,
    #pwa-install-banner,
    #ios-install-banner {
        display: none !important;
    }
}

/* ==========================================
   ENHANCED MOBILE OPTIMIZATION
   ========================================== */

/* Mobile-first responsive design for tablets and phones */
@media screen and (max-width: 768px) {
    /* Hamburger menu button positioning */
    .sidebar-toggle {
        display: block !important;
        position: fixed;
        top: 15px;
        left: 15px;
        z-index: 1001;
        width: 50px;
        height: 50px;
        background: white;
        border: none;
        border-radius: 8px;
        cursor: pointer;
        box-shadow: 0 2px 10px rgba(0,0,0,0.15);
        padding: 10px;
        font-size: 24px;
    }
    
    .sidebar-toggle:active {
        background: #f5f5f5;
    }
    
    /* Sidebar overlay */
    .sidebar-overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0,0,0,0.5);
        display: none;
        z-index: 998;
    }
    
    .sidebar-overlay.active {
        display: block;
    }
    
    /* Sidebar positioning */
    .sidebar {
        width: 280px;
        max-width: 80vw;
        position: fixed;
        left: 0;
        top: 0;
        height: 100vh;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        z-index: 999;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .sidebar.active {
        transform: translateX(0);
    }
    
    /* Main content adjustment */
    .main-content {
        margin-left: 0 !important;
        padding: 70px 15px 20px !important;
    }
    
    /* Header adjustment */
    .header {
        padding: 15px 0;
        margin-bottom: 15px;
    }
    
    .header h1 {
        font-size: 20px;
    }
    
    .header p {
        font-size: 12px;
    }
    
    .header-actions {
        margin-top: 15px;
        width: 100%;
    }
    
    .header-actions .btn {
        width: 100%;
    }
    
    /* Stats grid */
    .stats-grid {
        display: grid;
        grid-template-columns: 1fr;
        gap: 12px;
        margin-bottom: 20px;
    }
    
    .stat-card {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 15px;
        background: white;
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    }
    
    .stat-info {
        flex: 1;
    }
    
    .stat-info h3 {
        font-size: 12px;
        margin-bottom: 4px;
        color: #666;
    }
    
    .stat-info p {
        font-size: 22px;
        font-weight: bold;
        margin: 0;
    }
    
    .stat-icon {
        width: 50px;
        height: 50px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 24px;
        flex-shrink: 0;
    }
    
    /* Cards */
    .card {
        border-radius: 8px;
        margin-bottom: 15px;
        background: white;
        box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    }
    
    .card-header {
        padding: 15px;
        border-bottom: 1px solid #e0e0e0;
    }
    
    .card-header h2,
    .card-header h3 {
        font-size: 16px;
        margin: 0;
    }
    
    .card-body {
        padding: 15px;
    }
    
    /* Forms */
    .form-row {
        display: grid;
        grid-template-columns: 1fr;
        gap: 12px;
        margin-bottom: 12px;
    }
    
    .form-group {
        margin-bottom: 12px;
    }
    
    .form-group label {
        display: block;
        font-size: 13px;
        font-weight: 500;
        margin-bottom: 6px;
        color: #333;
    }
    
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="date"],
    input[type="number"],
    input[type="tel"],
    select,
    textarea {
        font-size: 16px !important; /* Prevents iOS zoom */
        padding: 12px !important;
        width: 100% !important;
        border: 1px solid #ddd;
        border-radius: 6px;
        box-sizing: border-box;
    }
    
    select {
        appearance: none;
        background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
        background-repeat: no-repeat;
        background-position: right 10px center;
        background-size: 20px;
        padding-right: 35px !important;
    }
    
    textarea {
        min-height: 100px;
        resize: vertical;
    }
    
    /* Tables */
    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: -15px -15px 0 -15px;
    }
    
    table {
        font-size: 12px;
        width: 100%;
    }
    
    table th {
        background: #f5f5f5;
        font-weight: 600;
        padding: 10px 8px;
        text-align: left;
        border-bottom: 2px solid #e0e0e0;
    }
    
    table td {
        padding: 10px 8px;
        border-bottom: 1px solid #e0e0e0;
        white-space: nowrap;
    }
    
    /* Buttons */
    .btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 10px 15px;
        font-size: 12px;
        min-height: 44px;
        border-radius: 6px;
        border: none;
        cursor: pointer;
        transition: all 0.2s ease;
        white-space: nowrap;
    }
    
    .btn-sm {
        padding: 6px 10px;
        font-size: 11px;
        min-height: 36px;
    }
    
    .btn-block,
    .btn-full {
        width: 100%;
    }
    
    /* Filters */
    .filters-section {
        background: white;
        padding: 12px;
        border-radius: 8px;
        margin-bottom: 15px;
    }
    
    .filters-section form {
        display: flex;
        flex-direction: column;
        gap: 10px;
    }
    
    .filters-section input,
    .filters-section select {
        width: 100% !important;
    }
    
    /* Modals */
    .modal {
        padding: 0;
    }
    
    .modal-content {
        max-width: 95vw;
        max-height: 95vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        border-radius: 12px;
        margin: auto;
    }
    
    .modal-content h2 {
        font-size: 18px;
    }
    
    .close {
        font-size: 28px;
    }
    
    /* Badges */
    .badge {
        padding: 4px 8px;
        font-size: 11px;
        border-radius: 4px;
        display: inline-block;
    }
    
    /* Alerts */
    .alert {
        padding: 12px;
        margin-bottom: 12px;
        border-radius: 6px;
        font-size: 13px;
    }
}

/* Extra small screens (phones 480px and below) */
@media screen and (max-width: 480px) {
    html {
        font-size: 14px;
        height: auto !important;
    }
    
    body {
        font-size: 14px;
        overflow: auto !important;
        height: auto !important;
        display: block !important;
    }
    
    .login-container,
    .form-container,
    .page-container {
        height: auto !important;
        max-height: none !important;
        overflow: visible !important;
        display: block !important;
    }
    
    .main-content {
        padding: 65px 10px 15px !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        height: auto !important;
    }
    
    /* Tables MUST be wrapped for horizontal scroll on small phones */
    .table-wrapper {
        overflow-x: auto !important;
        overflow-y: hidden !important;
        -webkit-overflow-scrolling: touch;
        width: 100%;
        margin-bottom: 15px;
        border: 1px solid #e0e0e0;
        border-radius: 6px;
        background: white;
    }
    
    table {
        width: 100%;
        min-width: 650px;
        font-size: 12px;
    }
    
    table thead,
    table tbody,
    table tfoot {
        display: table;
        width: 100%;
    }
    
    table th,
    table td {
        padding: 8px 6px;
        white-space: nowrap;
    }
    
    /* Enrollment forms on small phones */
    .enrollment-container {
        width: 100%;
    }
    
    .enrollment-form table {
        min-width: 700px;
    }
    
    .header {
        padding: 10px 0;
    }
    
    .header h1 {
        font-size: 18px;
    }
    
    .header p {
        font-size: 11px;
    }
    
    .stats-grid {
        gap: 10px;
    }
    
    .stat-card {
        padding: 12px;
        gap: 10px;
    }
    
    .stat-icon {
        width: 45px;
        height: 45px;
        font-size: 20px;
    }
    
    .card {
        margin-bottom: 12px;
        border-radius: 6px;
    }
    
    .card-header {
        padding: 12px;
    }
    
    .card-header h2 {
        font-size: 15px;
    }
    
    .card-body {
        padding: 12px;
    }
    
    .form-group label {
        font-size: 12px;
        margin-bottom: 4px;
    }
    
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="date"],
    input[type="number"],
    input[type="tel"],
    select,
    textarea {
        font-size: 14px !important;
        padding: 10px !important;
    }
    
    table {
        font-size: 11px;
    }
    
    table th,
    table td {
        padding: 8px 6px;
    }
    
    .btn {
        padding: 8px 12px;
        font-size: 12px;
        min-height: 40px;
    }
    
    .btn-sm {
        padding: 6px 8px;
        font-size: 10px;
    }
    
    .sidebar {
        width: 100%;
    }
}

/* Landscape mode */
@media screen and (max-height: 500px) and (orientation: landscape) {
    .main-content {
        padding-top: 50px !important;
    }
    
    .sidebar {
        height: auto;
        max-height: 100vh;
        overflow-y: auto;
    }
}

/* Touch-friendly sizing and interactions */
@media (hover: none) and (pointer: coarse) {
    /* Increase minimum touch target size to 44x44 */
    .btn,
    .menu-item,
    .sidebar-toggle,
    input[type="checkbox"],
    input[type="radio"],
    a {
        min-height: 44px;
        min-width: 44px;
    }
    
    /* Remove hover effects */
    .menu-item:hover,
    .btn:hover {
        background: inherit;
        transform: none;
    }
    
    /* Disable callouts */
    button,
    a,
    input,
    select {
        -webkit-touch-callout: none;
    }
}

/* Safe area for notched devices (iPhone X, etc.) */
@supports (padding: max(0px)) {
    .sidebar {
        padding-left: max(0, env(safe-area-inset-left));
        padding-right: max(0, env(safe-area-inset-right));
    }
    
    .main-content {
        padding-top: max(70px, calc(60px + env(safe-area-inset-top))) !important;
        padding-bottom: max(15px, env(safe-area-inset-bottom)) !important;
    }
}

/* ==========================================
   CHAT & MESSAGE CARDS MOBILE OPTIMIZATION
   TechLaw AI & School Chat - Professional mobile chat interface
   ========================================== */

@media screen and (max-width: 768px) {
    /* Chat Container - Stack on mobile */
    .chat-container {
        display: flex !important;
        flex-direction: column !important;
        height: auto !important;
        max-height: calc(100vh - 200px);
        background: white;
        border-radius: 12px;
        overflow: hidden;
        margin-bottom: 20px;
    }
    
    /* Chat Sidebar - Mobile stack */
    .chat-sidebar {
        display: none !important;
        width: 100% !important;
        height: auto !important;
        max-height: 300px !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
        padding: 12px !important;
        background: #f5f5f5;
        border-radius: 8px 8px 0 0;
        margin-bottom: 15px;
    }
    
    .chat-sidebar.mobile-visible {
        display: block !important;
    }
    
    /* Chat Main Area */
    .chat-main {
        flex: 1;
        display: flex !important;
        flex-direction: column !important;
        padding: 12px !important;
        background: white;
        border-radius: 12px;
        gap: 0;
        overflow: hidden;
    }
    
    /* Chat Header - Compact */
    .chat-header {
        padding: 12px 0 !important;
        margin-bottom: 10px !important;
        border-bottom: 1px solid #e0e0e0;
    }
    
    .chat-header h3 {
        font-size: 15px !important;
        margin: 0 0 4px 0 !important;
    }
    
    .admin-info,
    .officer-info {
        font-size: 11px !important;
        color: #999 !important;
        margin: 0 !important;
    }
    
    .officer-info {
        background: rgba(0, 212, 255, 0.08) !important;
        padding: 8px 12px !important;
        border-radius: 8px;
        display: flex !important;
        justify-content: space-between !important;
        align-items: center !important;
        margin: 10px 0 !important;
        flex-wrap: wrap;
        gap: 8px;
    }
    
    .officer-info .officer-details {
        display: flex !important;
        align-items: center !important;
        gap: 8px !important;
    }
    
    .officer-info .officer-avatar {
        width: 36px !important;
        height: 36px !important;
        font-size: 14px !important;
        border-radius: 50%;
    }
    
    .officer-info .officer-name {
        font-size: 13px !important;
        font-weight: 600;
    }
    
    .officer-info .officer-role {
        font-size: 10px !important;
    }
    
    .access-badge {
        padding: 3px 8px !important;
        font-size: 9px !important;
        border-radius: 12px;
        display: inline-block;
    }
    
    /* Chat Messages Container */
    .chat-messages {
        flex: 1;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        -webkit-overflow-scrolling: touch;
        padding: 12px 0 !important;
        background: #f9f9f9;
        border-radius: 8px;
        margin-bottom: 12px !important;
        min-height: 250px;
        max-height: 400px;
        display: flex;
        flex-direction: column;
        gap: 10px;
    }
    
    /* Individual Message Styling */
    .message {
        display: flex !important;
        flex-direction: column !important;
        margin-bottom: 10px !important;
        align-items: flex-start !important;
        gap: 6px;
        animation: slideIn 0.3s ease;
    }
    
    @keyframes slideIn {
        from {
            opacity: 0;
            transform: translateY(10px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }
    
    .message.sent {
        align-items: flex-end !important;
        flex-direction: column !important;
    }
    
    .message.user {
        align-items: flex-end !important;
    }
    
    .message.ai {
        align-items: flex-start !important;
    }
    
    /* Message Avatar */
    .message-avatar {
        width: 32px !important;
        height: 32px !important;
        border-radius: 50%;
        flex-shrink: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        font-weight: 600;
    }
    
    .message.user .message-avatar {
        background: linear-gradient(135deg, #667eea, #764ba2);
        color: white;
        order: 2;
    }
    
    .message.sent .message-avatar {
        background: linear-gradient(135deg, #667eea, #764ba2);
        color: white;
    }
    
    .message.ai .message-avatar {
        background: linear-gradient(135deg, #00d4ff, #00ff88);
        color: #1e3a5f;
        order: 1;
    }
    
    /* Message Content Bubble */
    .message-content {
        max-width: 85%;
        padding: 10px 12px !important;
        border-radius: 12px !important;
        font-size: 13px !important;
        line-height: 1.4;
        word-wrap: break-word;
        white-space: pre-wrap;
        overflow-wrap: break-word;
    }
    
    .message.sent .message-content {
        background: #2196f3 !important;
        color: white !important;
        border-radius: 12px 4px 12px 12px !important;
    }
    
    .message.user .message-content {
        background: #2196f3 !important;
        color: white !important;
        border-radius: 12px 4px 12px 12px !important;
    }
    
    .message.ai .message-content {
        background: white !important;
        color: #333 !important;
        border: 1px solid #e0e0e0;
        border-radius: 4px 12px 12px 12px !important;
        box-shadow: 0 1px 3px rgba(0,0,0,0.08);
    }
    
    /* Message Metadata */
    .message-sender {
        font-size: 11px !important;
        font-weight: 600;
        color: #666;
        margin: 0 !important;
    }
    
    .message.sent .message-sender {
        color: #999;
        text-align: right;
    }
    
    .message-time {
        font-size: 10px !important;
        color: #999 !important;
        margin-top: 2px !important;
    }
    
    .message.sent .message-time {
        text-align: right;
        color: #aaa;
    }
    
    .message-text {
        margin: 0 !important;
        word-break: break-word;
    }
    
    /* Message Reactions */
    .message-reactions {
        display: flex;
        flex-wrap: wrap;
        gap: 4px;
        margin-top: 4px;
    }
    
    .reaction {
        padding: 2px 6px;
        background: rgba(0,0,0,0.08);
        border-radius: 12px;
        font-size: 12px;
    }
    
    /* Message Actions */
    .message-actions {
        display: none;
        gap: 4px;
        font-size: 12px;
    }
    
    .message:hover .message-actions {
        display: flex;
    }
    
    .message-action-btn {
        background: none;
        border: none;
        cursor: pointer;
        color: #999;
        font-size: 12px;
        padding: 2px 4px;
    }
    
    .message-action-btn:hover {
        color: #333;
    }
    
    /* Message Attachment */
    .message-attachment {
        margin-top: 6px !important;
        padding: 6px 8px !important;
        background: rgba(0,0,0,0.05);
        border-radius: 4px;
        font-size: 11px !important;
    }
    
    .message-attachment a {
        color: #2196f3;
        text-decoration: none;
    }
    
    .message-attachment a:hover {
        text-decoration: underline;
    }
    
    /* Welcome Message - TechLaw AI */
    .welcome-message {
        text-align: center;
        padding: 30px 15px !important;
        color: #999;
    }
    
    .welcome-message i {
        font-size: 40px !important;
        color: #00d4ff;
        margin-bottom: 12px !important;
    }
    
    .welcome-message h3 {
        color: #333 !important;
        font-size: 16px !important;
        margin-bottom: 8px !important;
    }
    
    .welcome-message p {
        font-size: 12px !important;
        margin-bottom: 15px !important;
    }
    
    /* Sample Queries Grid */
    .sample-queries {
        display: grid !important;
        grid-template-columns: 1fr !important;
        gap: 8px !important;
        margin-top: 15px;
    }
    
    .sample-query {
        padding: 8px 12px !important;
        background: white;
        border: 1px solid #e0e0e0;
        border-radius: 6px;
        color: #333;
        font-size: 12px !important;
        cursor: pointer;
        transition: all 0.2s ease;
        text-align: center;
    }
    
    .sample-query:active {
        background: #f5f5f5;
        border-color: #2196f3;
    }
    
    /* Chat Input Container */
    .chat-input-container {
        display: flex !important;
        flex-direction: column !important;
        gap: 8px !important;
        padding: 12px 0 !important;
        margin: 0 !important;
        width: 100%;
    }
    
    .chat-input-wrapper {
        display: flex !important;
        gap: 6px !important;
        align-items: flex-end;
        width: 100%;
    }
    
    .chat-input {
        flex: 1;
        padding: 10px 12px !important;
        font-size: 14px !important;
        border: 1px solid #ddd !important;
        border-radius: 8px !important;
        resize: none !important;
        min-height: 40px;
        max-height: 100px;
        font-family: inherit;
    }
    
    .chat-input:focus {
        outline: none;
        border-color: #2196f3 !important;
        box-shadow: 0 0 0 2px rgba(33, 150, 243, 0.1);
    }
    
    /* Send Button */
    .send-btn,
    .chat-send-btn {
        padding: 10px 12px !important;
        background: #2196f3;
        color: white;
        border: none;
        border-radius: 8px;
        cursor: pointer;
        font-size: 14px;
        min-width: 40px;
        min-height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.2s ease;
        flex-shrink: 0;
    }
    
    .send-btn:active,
    .chat-send-btn:active {
        background: #1976d2;
        transform: scale(0.95);
    }
    
    .send-btn:disabled,
    .chat-send-btn:disabled {
        background: #ccc;
        cursor: not-allowed;
        opacity: 0.6;
    }
    
    /* School List Mobile */
    .school-list {
        list-style: none;
        padding: 0 !important;
        margin: 0 !important;
    }
    
    .school-list li {
        margin-bottom: 8px !important;
    }
    
    .school-list-item {
        padding: 10px 12px !important;
        background: white;
        border: 1px solid #e0e0e0;
        border-radius: 6px;
        cursor: pointer;
        transition: all 0.2s ease;
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 13px;
        gap: 8px;
    }
    
    .school-list-item:active {
        background: #f5f5f5;
        border-color: #2196f3;
    }
    
    .school-list-item.active {
        background: #2196f3;
        color: white;
        border-color: #2196f3;
    }
    
    .unread-badge {
        background: #ff4444;
        color: white;
        border-radius: 50%;
        width: 20px;
        height: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 11px;
        font-weight: 600;
        flex-shrink: 0;
    }
    
    .school-list-item.active .unread-badge {
        background: rgba(255,255,255,0.3);
    }
    
    /* Search/Filter in chat sidebar */
    .chat-sidebar input[type="text"] {
        width: 100% !important;
        padding: 8px !important;
        font-size: 13px !important;
        border: 1px solid #ddd;
        border-radius: 6px;
        margin-bottom: 8px;
    }
    
    /* Loading indicators */
    .loading-spinner {
        display: inline-block;
        width: 16px;
        height: 16px;
        border: 2px solid #f3f3f3;
        border-top: 2px solid #2196f3;
        border-radius: 50%;
        animation: spin 1s linear infinite;
    }
    
    @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
    }
    
    /* Typing indicator */
    .typing-indicator {
        display: flex;
        gap: 4px;
        padding: 8px 12px;
        background: #f5f5f5;
        border-radius: 8px;
        width: fit-content;
    }
    
    .typing-dot {
        width: 6px;
        height: 6px;
        border-radius: 50%;
        background: #999;
        animation: typing 1.4s infinite;
    }
    
    .typing-dot:nth-child(2) {
        animation-delay: 0.2s;
    }
    
    .typing-dot:nth-child(3) {
        animation-delay: 0.4s;
    }
    
    @keyframes typing {
        0%, 60%, 100% { opacity: 0.5; }
        30% { opacity: 1; }
    }
}

@media screen and (max-width: 480px) {
    /* Extra small devices - Compact chat */
    .chat-container {
        max-height: calc(100vh - 150px);
    }
    
    .chat-messages {
        min-height: 200px;
        max-height: 300px;
    }
    
    .message-content {
        max-width: 95%;
        font-size: 12px !important;
        padding: 8px 10px !important;
    }
    
    .message-avatar {
        width: 28px !important;
        height: 28px !important;
        font-size: 12px;
    }
    
    .chat-input {
        font-size: 14px !important;
        min-height: 36px;
        padding: 8px 10px !important;
    }
    
    .send-btn {
        min-width: 36px;
        min-height: 36px;
        padding: 8px 10px !important;
    }
    
    .welcome-message {
        padding: 20px 12px !important;
    }
    
    .welcome-message i {
        font-size: 32px !important;
        margin-bottom: 10px !important;
    }
    
    .welcome-message h3 {
        font-size: 14px !important;
    }
    
    .sample-queries {
        grid-template-columns: 1fr;
    }
    
    .sample-query {
        padding: 6px 10px !important;
        font-size: 11px !important;
    }
}

/* Touch device optimizations for chat */
@media (hover: none) and (pointer: coarse) {
    .message:hover {
        background: none;
    }
    
    .message:active {
        opacity: 0.9;
    }
    
    .send-btn:hover {
        background: #2196f3;
    }
    
    .send-btn:active {
        background: #1976d2;
        transform: scale(0.95);
    }
}
