/* =====================================================
   MOBILE-FIRST RESPONSIVE ENHANCEMENTS
   Enhanced mobile experience for all devices
   ===================================================== */

/* Root variables for mobile optimization */
:root {
    /* Touch target sizes */
    --touch-target: 44px;
    --touch-target-sm: 36px;
    
    /* Mobile spacing */
    --mobile-padding: 1rem;
    --mobile-gap: 0.75rem;
    
    /* Font sizing for mobile readability */
    --font-base-mobile: 16px;
    --font-sm-mobile: 13px;
}

/* =====================================================
   1. BASE MOBILE TYPOGRAPHY
   ===================================================== */

html {
    font-size: var(--font-base-mobile);
}

/* Improved readability on small screens */
@media (max-width: 640px) {
    html {
        font-size: 15px;
    }
    
    body {
        line-height: 1.6;
        letter-spacing: 0.3px;
    }
    
    h1 { font-size: 1.8rem; line-height: 1.2; margin: 1rem 0 0.75rem 0; }
    h2 { font-size: 1.5rem; line-height: 1.3; margin: 0.875rem 0 0.625rem 0; }
    h3 { font-size: 1.2rem; line-height: 1.4; margin: 0.75rem 0 0.5rem 0; }
    h4 { font-size: 1.05rem; line-height: 1.4; margin: 0.625rem 0 0.375rem 0; }
    
    p { margin: 0.5rem 0; }
    
    small, .text-sm { font-size: var(--font-sm-mobile); }
}

/* =====================================================
   2. TOUCH-FRIENDLY BUTTONS & INTERACTIVE ELEMENTS
   ===================================================== */

@media (max-width: 768px) {
    button,
    .btn,
    a.btn,
    input[type="button"],
    input[type="submit"],
    input[type="reset"] {
        min-height: var(--touch-target);
        min-width: var(--touch-target);
        padding: 0.65rem 1.2rem;
        font-size: 16px;
        border-radius: 10px;
        tap-highlight-color: transparent;
        -webkit-tap-highlight-color: transparent;
    }
    
    /* Button spacing in mobile layout */
    .btn + .btn {
        margin-left: 0.5rem;
    }
    
    /* Full-width buttons on small screens */
    .btn-full,
    .form-group button,
    .form-group .btn {
        width: 100%;
        margin-left: 0;
        margin-right: 0;
    }
    
    /* Button group stacking */
    .button-group {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .button-group .btn {
        width: 100%;
    }
}

/* =====================================================
   3. FORM OPTIMIZATION FOR MOBILE
   ===================================================== */

@media (max-width: 768px) {
    /* Larger form inputs for easier tapping */
    input,
    textarea,
    select,
    .form-control {
        min-height: var(--touch-target-sm);
        padding: 0.75rem 1rem;
        font-size: 16px;
        border: 1px solid var(--border);
        border-radius: 8px;
    }
    
    input[type="checkbox"],
    input[type="radio"] {
        min-width: 18px;
        min-height: 18px;
        margin-right: 0.5rem;
        cursor: pointer;
    }
    
    /* Label sizing */
    label {
        display: block;
        margin-bottom: 0.5rem;
        font-weight: 600;
        font-size: 0.95rem;
    }
    
    /* Form group spacing */
    .form-group {
        margin-bottom: 1.25rem;
    }
    
    /* Inline forms become stacked */
    .form-inline,
    .inline-form {
        flex-direction: column;
    }
    
    .form-inline .form-group,
    .inline-form .form-group {
        width: 100%;
    }
}

/* =====================================================
   4. TABLE OPTIMIZATION FOR MOBILE
   ===================================================== */

@media (max-width: 768px) {
    .table-card,
    .data-table-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        border-radius: 8px;
    }
    
    .data-table {
        min-width: 100%;
        font-size: 0.9rem;
    }
    
    .data-table th,
    .data-table td {
        padding: 0.75rem 0.5rem;
        white-space: nowrap;
    }
    
    .data-table th {
        font-size: 0.75rem;
        font-weight: 700;
    }
    
    /* Sticky table header */
    .data-table thead {
        position: sticky;
        top: 0;
        background: var(--panel-soft);
        z-index: 5;
    }
    
    /* Action buttons in tables */
    .table-actions {
        white-space: nowrap;
    }
    
    .table-actions .btn {
        padding: 0.5rem 0.75rem;
        font-size: 0.85rem;
        min-height: 32px;
    }
}

/* Responsive table cards - stack on mobile */
@media (max-width: 640px) {
    .table-card {
        border-radius: 8px;
        overflow: hidden;
    }
    
    /* Alternative: card layout for very small screens */
    .responsive-table {
        display: block;
    }
    
    .responsive-table thead {
        display: none;
    }
    
    .responsive-table tbody,
    .responsive-table tr,
    .responsive-table td {
        display: block;
        width: 100%;
    }
    
    .responsive-table tr {
        margin-bottom: 1rem;
        border: 1px solid var(--border);
        border-radius: 8px;
        padding: 1rem;
    }
    
    .responsive-table td {
        text-align: right;
        padding-left: 50%;
        position: relative;
        padding-bottom: 0.5rem;
    }
    
    .responsive-table td::before {
        content: attr(data-label);
        position: absolute;
        left: 1rem;
        font-weight: 600;
        text-transform: uppercase;
        font-size: 0.75rem;
        color: var(--muted);
    }
}

/* =====================================================
   5. GRID SYSTEM MOBILE OPTIMIZATION
   ===================================================== */

@media (max-width: 768px) {
    /* Grid responsive */
    .grid-2,
    .grid-3,
    .grid-4 {
        grid-template-columns: 1fr;
        gap: 1rem;
    }
    
    .stats-grid,
    .feature-grid,
    .company-grid,
    .card-grid {
        grid-template-columns: 1fr;
        gap: 1rem;
    }
    
    /* Maintain 2-column on tablets */
    @media (min-width: 641px) and (max-width: 768px) {
        .grid-3,
        .grid-4 {
            grid-template-columns: repeat(2, 1fr);
        }
    }
}

/* =====================================================
   6. CARD & PANEL MOBILE OPTIMIZATION
   ===================================================== */

@media (max-width: 768px) {
    .card,
    .panel,
    .form-card,
    .table-card {
        border-radius: 12px;
        padding: var(--mobile-padding);
        margin-bottom: var(--mobile-gap);
    }
    
    .card-header {
        padding: var(--mobile-padding);
        margin: calc(-1 * var(--mobile-padding));
        margin-bottom: var(--mobile-padding);
        border-bottom: 1px solid var(--border);
    }
    
    .card-body {
        padding: 0;
    }
    
    .card-footer {
        padding: var(--mobile-padding);
        margin: calc(-1 * var(--mobile-padding));
        margin-top: var(--mobile-padding);
        border-top: 1px solid var(--border);
    }
}

/* =====================================================
   7. SIDEBAR & NAVIGATION MOBILE
   ===================================================== */

@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        width: 280px;
        height: 100vh;
        transform: translateX(-100%);
        transition: transform 0.3s ease-out;
        z-index: 1000;
    }
    
    .sidebar.open,
    .sidebar[data-mobile-open="true"] {
        transform: translateX(0);
    }
    
    .mobile-menu-overlay {
        display: block;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease;
        z-index: 999;
    }
    
    .mobile-menu-overlay.show {
        opacity: 1;
        visibility: visible;
    }
    
    .mobile-app-bar {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0.75rem 1rem;
        background: var(--panel);
        border-bottom: 1px solid var(--border);
        position: sticky;
        top: 0;
        z-index: 50;
    }
    
    .mobile-menu-toggle {
        background: transparent;
        border: none;
        font-size: 1.5rem;
        cursor: pointer;
        padding: 0.5rem;
        color: var(--text);
    }
    
    .mobile-logo {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        color: var(--text);
        text-decoration: none;
        font-weight: 700;
    }
}

/* =====================================================
   8. MODAL & DIALOG MOBILE
   ===================================================== */

@media (max-width: 768px) {
    .modal,
    .dialog,
    .modal-dialog {
        width: 90vw !important;
        max-width: none !important;
        margin: auto !important;
        border-radius: 12px;
        max-height: 90vh;
        overflow-y: auto;
    }
    
    .modal-header,
    .modal-footer {
        padding: 1rem;
    }
    
    .modal-body {
        padding: 1rem;
    }
    
    /* Full-width close button on mobile */
    .modal-close {
        min-height: var(--touch-target-sm);
        min-width: var(--touch-target-sm);
    }
}

/* =====================================================
   9. SPACING & PADDING OPTIMIZATION
   ===================================================== */

@media (max-width: 768px) {
    /* Reduce padding on mobile */
    .page-heading,
    .page-header {
        padding: 1rem var(--mobile-padding);
    }
    
    .content-area {
        padding: var(--mobile-padding);
    }
    
    .section-padding {
        padding: 1.5rem var(--mobile-padding);
    }
    
    /* Margin utilities */
    .mx-auto { margin-left: auto; margin-right: auto; }
    .my-2 { margin-top: 0.5rem; margin-bottom: 0.5rem; }
    .p-2 { padding: 0.5rem; }
    .p-3 { padding: 1rem; }
}

/* =====================================================
   10. UTILITY CLASSES FOR MOBILE
   ===================================================== */

/* Hide on mobile */
@media (max-width: 768px) {
    .hide-mobile,
    .hidden-mobile {
        display: none !important;
    }
}

/* Show only on mobile */
@media (min-width: 769px) {
    .show-mobile,
    .visible-mobile {
        display: none !important;
    }
}

/* Mobile text alignment */
@media (max-width: 768px) {
    .text-center-mobile { text-align: center; }
    .text-left-mobile { text-align: left; }
    .text-right-mobile { text-align: right; }
}

/* =====================================================
   11. OVERFLOW & SCROLL OPTIMIZATION
   ===================================================== */

@media (max-width: 768px) {
    /* Smooth scrolling */
    html {
        scroll-behavior: smooth;
    }
    
    /* Horizontal scroll containers */
    .horizontal-scroll,
    .overflow-x-auto {
        -webkit-overflow-scrolling: touch;
        overflow-x: auto;
    }
    
    /* Prevent text overflow */
    body, p, span, a, li {
        word-break: break-word;
        overflow-wrap: break-word;
    }
    
    code, pre {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* =====================================================
   12. KEYBOARD & ACCESSIBILITY
   ===================================================== */

@media (max-width: 768px) {
    /* Ensure proper focus states */
    a:focus,
    button:focus,
    input:focus,
    select:focus,
    textarea:focus {
        outline: 2px solid var(--primary);
        outline-offset: 2px;
    }
    
    /* Disable tap highlight */
    a,
    button,
    input,
    select,
    textarea {
        -webkit-tap-highlight-color: rgba(127, 23, 52, 0.1);
    }
}

/* =====================================================
   13. PRINT OPTIMIZATION
   ===================================================== */

@media print {
    .mobile-app-bar,
    .mobile-menu-toggle,
    .sidebar,
    .topbar,
    .nav-primary,
    .button-group,
    .hide-print {
        display: none !important;
    }
    
    body {
        background: white;
        color: black;
    }
}

/* =====================================================
   14. SAFE AREA FOR NOTCHED DEVICES
   ===================================================== */

@supports (padding: max(0px)) {
    @media (max-width: 768px) {
        body {
            padding-left: max(1rem, env(safe-area-inset-left));
            padding-right: max(1rem, env(safe-area-inset-right));
        }
        
        .sidebar {
            padding-bottom: env(safe-area-inset-bottom);
        }
    }
}
