/* ============================================
   MOBILE RESPONSIVE ENHANCEMENTS
   ============================================ */

/* --- Prevent Horizontal Scroll --- */
html, body {
    overflow-x: hidden;
    width: 100%;
    max-width: 100vw;
}

* {
    box-sizing: border-box;
}

/* --- Touch Target Sizes --- */
@media (max-width: 768px) {
    /* Minimum 44x44px touch targets per Apple/Google guidelines */
    button, a, input[type="submit"], input[type="button"] {
        min-height: 44px;
        min-width: 44px;
        padding: 0.5rem 1rem;
    }
    
    /* Radio buttons and checkboxes */
    input[type="radio"], input[type="checkbox"] {
        min-width: 20px;
        min-height: 20px;
        margin-right: 8px;
    }
}

/* --- Form Elements --- */
@media (max-width: 640px) {
    /* Prevent zoom on input focus (iOS) */
    input, select, textarea {
        font-size: 16px !important;
    }
    
    /* Full-width forms on mobile */
    form {
        width: 100%;
    }
    
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="search"],
    select,
    textarea {
        width: 100%;
        max-width: 100%;
    }
}

/* --- Responsive Tables --- */
@media (max-width: 768px) {
    table {
        display: block;
        overflow-x: auto;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
    }
    
    /* Alternative: Stack table cells vertically */
    table.stack-mobile {
        display: table;
    }
    
    table.stack-mobile thead {
        display: none;
    }
    
    table.stack-mobile tbody,
    table.stack-mobile tr,
    table.stack-mobile td {
        display: block;
        width: 100%;
    }
    
    table.stack-mobile tr {
        margin-bottom: 1rem;
        border: 1px solid #e5e7eb;
        border-radius: 0.5rem;
        padding: 0.5rem;
    }
    
    table.stack-mobile td {
        text-align: right;
        padding: 0.5rem;
        position: relative;
        padding-left: 50%;
    }
    
    table.stack-mobile td:before {
        content: attr(data-label);
        position: absolute;
        left: 0.5rem;
        width: 45%;
        text-align: left;
        font-weight: 600;
    }
}

/* --- Cards and Containers --- */
@media (max-width: 640px) {
    .card, .bg-white.rounded-lg {
        border-radius: 0.5rem;
        margin: 0 -0.5rem; /* Negative margin for full bleed on small screens */
    }
}

/* --- Sidebar Overlay (Mobile) --- */
@media (max-width: 767px) {
    #main-sidebar {
        box-shadow: 2px 0 8px rgba(0, 0, 0, 0.3);
    }
    
    /* Prevent body scroll when sidebar is open */
    body.sidebar-open {
        overflow: hidden;
    }
}

/* --- Navigation --- */
@media (max-width: 768px) {
    nav ul {
        flex-direction: column;
    }
    
    nav li {
        width: 100%;
    }
    
    nav a {
        display: block;
        padding: 1rem;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    }
}

/* --- Typography --- */
@media (max-width: 640px) {
    h1 {
        font-size: 1.75rem;
        line-height: 2rem;
    }
    
    h2 {
        font-size: 1.5rem;
        line-height: 1.75rem;
    }
    
    h3 {
        font-size: 1.25rem;
        line-height: 1.5rem;
    }
    
    p, li, td {
        font-size: 0.875rem;
        line-height: 1.5;
    }
}

/* --- Buttons --- */
@media (max-width: 640px) {
    /* Stack buttons vertically on small screens */
    .button-group {
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .button-group button,
    .button-group a {
        width: 100%;
    }
    
    /* Smaller padding for compact buttons */
    .btn-sm {
        padding: 0.375rem 0.75rem;
        font-size: 0.875rem;
    }
}

/* --- Modals --- */
@media (max-width: 640px) {
    .modal {
        width: 95vw !important;
        max-height: 90vh;
        margin: 5vh auto;
    }
    
    .modal-content {
        padding: 1rem;
    }
}

/* --- Grid Layouts --- */
@media (max-width: 640px) {
    /* Force single column on mobile */
    .grid-cols-2,
    .grid-cols-3,
    .grid-cols-4 {
        grid-template-columns: 1fr !important;
    }
}

/* --- Images --- */
img {
    max-width: 100%;
    height: auto;
}

/* --- Fix for Safari Mobile Input Shadows --- */
input,
select,
textarea {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}

/* --- Prevent Text Selection on UI Elements --- */
@media (max-width: 768px) {
    button, .button, .nav-link {
        -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
        user-select: none;
    }
}

/* --- Loading Indicators --- */
@media (max-width: 640px) {
    .htmx-indicator {
        font-size: 0.875rem;
    }
}

/* --- Utility Classes for Mobile --- */
.mobile-only {
    display: none;
}

.desktop-only {
    display: block;
}

@media (max-width: 768px) {
    .mobile-only {
        display: block;
    }
    
    .desktop-only {
        display: none;
    }
}

/* --- Scrollbar Styling (Webkit) --- */
@media (max-width: 768px) {
    ::-webkit-scrollbar {
        width: 4px;
        height: 4px;
    }
    
    ::-webkit-scrollbar-track {
        background: transparent;
    }
    
    ::-webkit-scrollbar-thumb {
        background: rgba(0, 0, 0, 0.2);
        border-radius: 2px;
    }
}

/* --- Safe Area Insets (iPhone X+) --- */
@supports (padding: max(0px)) {
    body {
        padding-left: max(0px, env(safe-area-inset-left));
        padding-right: max(0px, env(safe-area-inset-right));
    }
    
    header {
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-right: max(1rem, env(safe-area-inset-right));
    }
}

/* --- Landscape Mode Fixes --- */
@media (max-width: 768px) and (orientation: landscape) {
    /* Reduce vertical padding in landscape */
    .container {
        padding-top: 1rem;
        padding-bottom: 1rem;
    }
    
    h1 {
        font-size: 1.5rem;
    }
}

/* --- Performance: Reduce Animations on Mobile --- */
@media (max-width: 768px) {
    * {
        animation-duration: 0.2s !important;
        transition-duration: 0.2s !important;
    }
}

/* --- Focus Styles for Accessibility --- */
@media (max-width: 768px) {
    *:focus {
        outline: 2px solid #10b981;
        outline-offset: 2px;
    }
    
    button:focus,
    a:focus,
    input:focus,
    select:focus {
        outline: 2px solid #10b981;
    }
}

/* --- Sticky Elements Mobile Fix --- */
@media (max-width: 768px) {
    .sticky {
        position: -webkit-sticky;
        position: sticky;
    }
}

/* --- Prevent Font Boosting (Android) --- */
@media (max-width: 768px) {
    * {
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
    }
}