/* ========================================
   APEX Utilities
   Responsive, accessibility, mobile, print, and utility classes
   Consolidated from: responsive.css, mobile-enhancements.css,
   accessibility.css, utilities.css, focus-print.css
   ======================================== */


/* ========================================
   1. ACCESSIBILITY — Focus States (WCAG 2.4.7)
   ======================================== */

:focus {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
}

:focus:not(:focus-visible) {
    outline: none;
}

:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
    box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.15);
}

[tabindex]:focus {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
}

/* Bootstrap form overrides */
.form-control:focus,
.form-select:focus {
    border-color: var(--color-primary);
    box-shadow: 0 0 0 0.2rem rgba(37, 99, 235, 0.25);
}

.btn:focus-visible,
.nav-link:focus-visible,
.form-control:focus-visible,
.form-select:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
}

/* Skip to main content link (WCAG 2.4.1) */
.skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    padding: var(--space-3) var(--space-4);
    background: var(--color-primary);
    color: var(--color-text-inverse);
    z-index: 10000;
    transition: top var(--transition-fast);
    border-radius: 0 0 var(--radius-md) 0;
    font-weight: var(--font-medium);
}

.skip-link:focus {
    top: 0;
}


/* ========================================
   2. ACCESSIBILITY — Screen Reader Utilities
   ======================================== */

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.sr-only-focusable:focus,
.sr-only-focusable:active {
    position: static;
    width: auto;
    height: auto;
    padding: inherit;
    margin: inherit;
    overflow: visible;
    clip: auto;
    white-space: normal;
}

.visually-hidden-alert {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}


/* ========================================
   3. ACCESSIBILITY — High Contrast
   ======================================== */

@media (prefers-contrast: high) {
    :root {
        --color-primary: #0044AA;
        --color-success: #006600;
        --color-warning: #CC6600;
        --color-error: #CC0000;
        --neutral-200: #666666;
        --neutral-300: #333333;
        --text-secondary: #333333;
        --text-muted: #555555;
    }

    .apex-card,
    .apex-stat-card {
        border-width: 2px;
    }

    .apex-btn {
        border-width: 2px;
    }
}


/* ========================================
   4. ACCESSIBILITY — Reduced Motion
   ======================================== */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }

    .apex-sidebar {
        transition: none;
    }

    .nav-link:hover,
    .apex-card:hover,
    .feature-card:hover {
        transform: none;
    }
}


/* ========================================
   5. ACCESSIBILITY — Text Scaling & Touch
   ======================================== */

/* Large text mode */
.text-scale-large {
    font-size: 125%;
}

.text-scale-large h1 { font-size: 2.5rem; }
.text-scale-large h2 { font-size: 2rem; }
.text-scale-large h3 { font-size: 1.75rem; }
.text-scale-large h4 { font-size: 1.5rem; }

/* Minimum 44x44px touch targets (WCAG 2.5.5) */
.apex-btn,
.nav-link,
.header-btn,
.sidebar-toggle,
.logout-btn {
    min-height: 44px;
    min-width: 44px;
}

@media (pointer: coarse) {
    .apex-btn-sm {
        min-height: 44px;
        padding: var(--space-3) var(--space-4);
    }

    .apex-input {
        min-height: 48px;
    }

    .apex-table td,
    .apex-table th {
        padding: var(--space-4);
    }
}

/* Focus trap indicator */
.focus-trap-active {
    position: relative;
}

.focus-trap-active::before {
    content: '';
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: var(--z-modal-backdrop);
}


/* ========================================
   6. TOUCH INTERACTIONS
   ======================================== */

/* Remove tap delay */
a,
button,
input,
select,
textarea {
    touch-action: manipulation;
}

/* Active states for touch */
@media (hover: none) and (pointer: coarse) {
    .apex-btn:active {
        transform: scale(0.98);
    }

    .apex-card:active {
        transform: scale(0.99);
    }

    .nav-link:active {
        background: rgba(255, 255, 255, 0.15);
    }
}

/* Swipe indicators */
.swipe-indicator {
    position: absolute;
    bottom: var(--space-2);
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: var(--space-2);
}

.swipe-dot {
    width: 8px;
    height: 8px;
    border-radius: var(--radius-full);
    background: var(--neutral-300);
    transition: all var(--transition-fast);
}

.swipe-dot.active {
    width: 24px;
    background: var(--color-primary);
}


/* ========================================
   7. RESPONSIVE — Mobile (max 575px)
   ======================================== */

@media (max-width: 575.98px) {
    .sidebar {
        width: 100%;
        transform: translateX(-100%);
        z-index: 1050;
        position: fixed;
        top: 0;
        left: 0;
        height: 100vh;
    }

    .sidebar.show {
        transform: translateX(0);
        box-shadow: 0 0 30px rgba(0, 0, 0, 0.3);
    }

    .main-content {
        margin-left: 0;
        width: 100%;
        min-height: 100vh;
        padding: 0.5rem;
    }

    .container-fluid {
        padding: 0.5rem;
    }

    /* Mobile card adjustments */
    .card {
        margin-bottom: 1rem;
        border-radius: var(--radius-lg);
    }

    .card-body {
        padding: 1rem;
    }

    /* Mobile button adjustments */
    .btn {
        font-size: 0.8125rem;
        padding: 0.5rem 1rem;
    }

    .btn-lg {
        padding: 0.75rem 1.5rem;
        font-size: 0.9375rem;
    }

    /* Mobile table adjustments */
    .table-responsive {
        font-size: 0.875rem;
    }

    /* Mobile form adjustments - Prevents zoom on iOS */
    .form-control, .form-select {
        font-size: 16px;
        padding: 0.75rem;
    }

    /* Mobile typography */
    h1, .h1 { font-size: 1.75rem; }
    h2, .h2 { font-size: 1.5rem; }
    h3, .h3 { font-size: 1.25rem; }
    h4, .h4 { font-size: 1.125rem; }
    h5, .h5 { font-size: 1rem; }
    h6, .h6 { font-size: 0.875rem; }

    /* Mobile grid adjustments */
    .col, .col-1, .col-2, .col-3, .col-4, .col-5, .col-6,
    .col-7, .col-8, .col-9, .col-10, .col-11, .col-12 {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }

    .stat-card-compact {
        padding: 1rem;
        margin-bottom: 1rem;
    }

    .dashboard-grid {
        grid-template-columns: 1fr;
        gap: 1rem;
    }
}

/* Small devices (landscape phones, 576–767px) */
@media (min-width: 576px) and (max-width: 767.98px) {
    .sidebar {
        width: 100%;
        transform: translateX(-100%);
        position: fixed;
        z-index: 1050;
    }

    .sidebar.show {
        transform: translateX(0);
    }

    .main-content {
        margin-left: 0;
        width: 100%;
        padding: 1rem;
    }

    .dashboard-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 1rem;
    }
}

/* Medium devices (tablets, 768–991px) */
@media (min-width: 768px) and (max-width: 991.98px) {
    .sidebar {
        width: 250px;
        transform: translateX(0);
        position: fixed;
    }

    .main-content {
        margin-left: 250px;
        width: calc(100% - 250px);
        padding: 1.25rem;
    }

    .sidebar.collapsed {
        width: 80px;
    }

    .main-content.expanded {
        margin-left: 80px;
        width: calc(100% - 80px);
    }

    .dashboard-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 1.5rem;
    }
}

/* Large devices (desktops, 992–1199px) */
@media (min-width: 992px) and (max-width: 1199.98px) {
    .sidebar {
        width: 280px;
    }

    .main-content {
        margin-left: 280px;
        width: calc(100% - 280px);
        padding: 1.5rem;
    }

    .sidebar.collapsed {
        width: 80px;
    }

    .main-content.expanded {
        margin-left: 80px;
        width: calc(100% - 80px);
    }

    .dashboard-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 2rem;
    }
}

/* Extra large (1200px+) */
@media (min-width: 1200px) {
    .sidebar {
        width: 300px;
    }

    .main-content {
        margin-left: 300px;
        width: calc(100% - 300px);
        padding: 2rem;
    }

    .sidebar.collapsed {
        width: 80px;
    }

    .main-content.expanded {
        margin-left: 80px;
        width: calc(100% - 80px);
    }

    .dashboard-grid {
        grid-template-columns: repeat(4, 1fr);
        gap: 2rem;
    }

    .container-fluid {
        max-width: 1400px;
        margin: 0 auto;
    }
}

/* Ultra-wide (1400px+) */
@media (min-width: 1400px) {
    .container-fluid {
        max-width: 1600px;
        padding-left: 2rem;
        padding-right: 2rem;
    }
}


/* ========================================
   8. RESPONSIVE — Height-based
   ======================================== */

@media (max-height: 600px) {
    .sidebar {
        overflow-y: auto;
    }

    .sidebar-nav {
        max-height: calc(100vh - 200px);
        overflow-y: auto;
    }
}


/* ========================================
   9. MOBILE — Sidebar & Navigation
   ======================================== */

@media (max-width: 1024px) {
    .apex-sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100vh;
        width: var(--sidebar-width);
        transform: translateX(-100%);
        z-index: var(--z-modal);
        transition: transform var(--transition-base);
    }

    .apex-sidebar.open {
        transform: translateX(0);
    }

    .sidebar-backdrop {
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        opacity: 0;
        visibility: hidden;
        z-index: calc(var(--z-modal) - 1);
        transition: opacity var(--transition-base), visibility var(--transition-base);
    }

    .sidebar-backdrop.active {
        opacity: 1;
        visibility: visible;
    }

    .apex-main {
        margin-left: 0;
        width: 100%;
    }

    .mobile-menu-btn {
        display: flex;
    }
}


/* ========================================
   10. MOBILE — Header
   ======================================== */

@media (max-width: 768px) {
    html {
        font-size: 16px;
        -webkit-text-size-adjust: 100%;
    }

    body {
        -webkit-tap-highlight-color: transparent;
    }

    .apex-header {
        height: 56px;
        padding: 0 var(--space-3);
        gap: var(--space-2);
    }

    .header-search {
        position: fixed;
        top: 56px;
        left: 0;
        right: 0;
        padding: var(--space-3);
        background: var(--color-surface);
        border-bottom: 1px solid var(--neutral-200);
        transform: translateY(-100%);
        opacity: 0;
        visibility: hidden;
        transition: all var(--transition-base);
        z-index: var(--z-sticky);
    }

    .header-search.active {
        transform: translateY(0);
        opacity: 1;
        visibility: visible;
    }

    .header-search .search-input {
        width: 100%;
        max-width: none;
    }

    .search-toggle-btn {
        display: flex;
    }
}


/* ========================================
   11. MOBILE — Content & Cards
   ======================================== */

@media (max-width: 768px) {
    .apex-content {
        padding: var(--space-4);
    }

    .page-header .flex {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--space-3);
    }

    .page-actions {
        width: 100%;
        display: flex;
        gap: var(--space-2);
    }

    .page-actions .apex-btn {
        flex: 1;
        justify-content: center;
    }

    .grid-cols-2,
    .grid-cols-3,
    .grid-cols-4 {
        grid-template-columns: 1fr;
    }

    .apex-card {
        border-radius: var(--radius-lg);
        margin-bottom: var(--space-3);
    }

    .apex-card-header {
        padding: var(--space-4);
        flex-wrap: wrap;
        gap: var(--space-2);
    }

    .apex-card-body {
        padding: var(--space-4);
    }

    .apex-card-footer {
        padding: var(--space-3) var(--space-4);
    }

    .apex-stat-card {
        padding: var(--space-4);
    }

    .apex-stat-value {
        font-size: var(--text-2xl);
    }
}


/* ========================================
   12. MOBILE — Tables
   ======================================== */

@media (max-width: 768px) {
    .apex-table-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .apex-table {
        min-width: 600px;
    }

    /* Card-style table on mobile */
    .table-mobile-cards .apex-table {
        min-width: auto;
    }

    .table-mobile-cards .apex-table thead {
        display: none;
    }

    .table-mobile-cards .apex-table tbody tr {
        display: block;
        padding: var(--space-4);
        margin-bottom: var(--space-3);
        border: 1px solid var(--neutral-200);
        border-radius: var(--radius-lg);
        background: var(--color-surface);
    }

    .table-mobile-cards .apex-table tbody td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: var(--space-2) 0;
        border: none;
    }

    .table-mobile-cards .apex-table tbody td::before {
        content: attr(data-label);
        font-weight: var(--font-semibold);
        color: var(--text-secondary);
        font-size: var(--text-xs);
        text-transform: uppercase;
    }
}


/* ========================================
   13. MOBILE — Forms & Buttons
   ======================================== */

@media (max-width: 768px) {
    .apex-form-group {
        margin-bottom: var(--space-4);
    }

    .apex-input {
        min-height: 48px;
        font-size: 16px; /* Prevents iOS zoom */
    }

    .apex-label {
        font-size: var(--text-sm);
        margin-bottom: var(--space-2);
    }

    .form-row {
        flex-direction: column;
    }

    .form-row > * {
        width: 100%;
    }

    .btn-group-mobile {
        display: flex;
        flex-direction: column;
        gap: var(--space-2);
    }

    .btn-group-mobile .apex-btn {
        width: 100%;
        justify-content: center;
    }

    .apex-btn {
        min-height: 48px;
        padding: var(--space-3) var(--space-4);
    }

    .apex-btn-sm {
        min-height: 40px;
        padding: var(--space-2) var(--space-3);
    }

    .btn-block-mobile {
        width: 100%;
        justify-content: center;
    }

    /* Floating action button */
    .fab {
        position: fixed;
        bottom: var(--space-4);
        right: var(--space-4);
        width: 56px;
        height: 56px;
        border-radius: var(--radius-full);
        background: var(--color-primary);
        color: var(--color-text-inverse);
        box-shadow: var(--shadow-lg);
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: var(--text-xl);
        z-index: var(--z-fixed);
        border: none;
        cursor: pointer;
    }

    .fab:active {
        transform: scale(0.95);
    }
}


/* ========================================
   14. MOBILE — Modals
   ======================================== */

@media (max-width: 768px) {
    .modal-dialog {
        margin: 0;
        max-width: 100%;
        min-height: 100vh;
    }

    .modal-content {
        border-radius: 0;
        min-height: 100vh;
    }

    /* Bottom sheet style modal */
    .modal-bottom-sheet .modal-dialog {
        margin: auto 0 0;
        min-height: auto;
        max-height: 90vh;
    }

    .modal-bottom-sheet .modal-content {
        border-radius: var(--radius-2xl) var(--radius-2xl) 0 0;
        min-height: auto;
    }

    .modal-bottom-sheet .modal-header {
        padding: var(--space-4);
    }

    .modal-bottom-sheet .modal-header::before {
        content: '';
        position: absolute;
        top: var(--space-2);
        left: 50%;
        transform: translateX(-50%);
        width: 40px;
        height: 4px;
        background: var(--neutral-300);
        border-radius: var(--radius-full);
    }
}


/* ========================================
   15. MOBILE — Bottom Navigation
   ======================================== */

.bottom-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 64px;
    background: var(--color-surface);
    border-top: 1px solid var(--neutral-200);
    z-index: var(--z-fixed);
    padding-bottom: env(safe-area-inset-bottom);
}

@media (max-width: 768px) {
    .bottom-nav {
        display: flex;
    }

    .apex-content {
        padding-bottom: calc(var(--space-6) + 64px);
    }
}

.bottom-nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-1);
    color: var(--text-muted);
    text-decoration: none;
    font-size: 10px;
    font-weight: var(--font-medium);
    transition: color var(--transition-fast);
}

.bottom-nav-item.active {
    color: var(--color-primary);
}

.bottom-nav-icon {
    font-size: 20px;
}


/* ========================================
   16. MOBILE — Touch Targets
   ======================================== */

@media (max-width: 767.98px) {
    .btn,
    .nav-link,
    .dropdown-item,
    .form-control,
    .form-select {
        min-height: 44px;
        min-width: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .nav-link {
        justify-content: flex-start;
    }
}

/* Two columns on larger phones */
@media (min-width: 480px) and (max-width: 768px) {
    .grid-cols-2 {
        grid-template-columns: repeat(2, 1fr);
    }
}


/* ========================================
   17. SAFE AREA SUPPORT
   ======================================== */

@supports (padding: env(safe-area-inset-bottom)) {
    .apex-content {
        padding-bottom: calc(var(--space-6) + env(safe-area-inset-bottom));
    }

    .bottom-nav {
        padding-bottom: env(safe-area-inset-bottom);
    }

    .modal-bottom-sheet .modal-footer {
        padding-bottom: calc(var(--space-4) + env(safe-area-inset-bottom));
    }
}


/* ========================================
   18. GRADIENT BACKGROUND UTILITIES
   ======================================== */

.bg-gradient-primary { background: var(--gradient-primary) !important; }
.bg-gradient-secondary { background: var(--gradient-secondary) !important; }
.bg-gradient-accent { background: var(--gradient-accent) !important; }
.bg-gradient-glass { background: var(--gradient-glass) !important; }
.bg-gradient-card { background: var(--gradient-card) !important; }


/* ========================================
   19. SHADOW UTILITIES
   ======================================== */

.shadow-none { box-shadow: none !important; }
.shadow-sm { box-shadow: var(--shadow-sm) !important; }
.shadow { box-shadow: var(--shadow-md) !important; }
.shadow-lg { box-shadow: var(--shadow-lg) !important; }
.shadow-glow { box-shadow: var(--shadow-glow) !important; }
.shadow-glow-purple { box-shadow: var(--shadow-glow-purple) !important; }


/* ========================================
   20. COLOR UTILITIES (APEX-specific)
   ======================================== */

.text-parker-blue { color: var(--parker-blue) !important; }
.text-parker-teal { color: var(--parker-teal) !important; }
.bg-parker-blue { background-color: var(--parker-blue) !important; }
.bg-parker-teal { background-color: var(--parker-teal) !important; }


/* ========================================
   21. PRINT STYLES
   ======================================== */

@media print {
    /* Hide non-essential elements */
    .apex-sidebar,
    .apex-header,
    .sidebar,
    .navbar,
    .apex-btn:not(.print-btn),
    .header-actions,
    .mobile-menu-btn,
    .no-print,
    .bottom-nav,
    nav,
    footer {
        display: none !important;
    }

    /* Reset layout for print */
    .apex-main,
    .main-content {
        margin-left: 0 !important;
        padding: 0 !important;
        width: 100% !important;
    }

    .apex-content {
        padding: 0 !important;
        max-width: 100% !important;
    }

    /* Optimize cards for print */
    .apex-card,
    .card {
        box-shadow: none !important;
        border: 1px solid #ccc !important;
        page-break-inside: avoid;
        break-inside: avoid;
    }

    /* Tables */
    .apex-table,
    .table {
        font-size: 10pt;
    }

    .apex-table th, .apex-table td,
    .table th, .table td {
        padding: 8px !important;
    }

    /* Page breaks */
    h1, h2, h3 {
        page-break-after: avoid;
    }

    .page-break {
        page-break-before: always;
    }

    /* Links */
    a[href^="http"]::after {
        content: " (" attr(href) ")";
        font-size: 0.8em;
        color: #666;
    }

    /* Force backgrounds off for print */
    * {
        background: transparent !important;
        color: #000 !important;
    }
}


/* ========================================
   22. PERFORMANCE OPTIMIZATIONS
   ======================================== */

.apex-sidebar,
.apex-main {
    will-change: transform;
}

.apex-card:hover,
.apex-btn:hover,
.nav-link:hover {
    will-change: transform, box-shadow;
}

/* Content visibility for long lists */
.lazy-content {
    content-visibility: auto;
    contain-intrinsic-size: auto 500px;
}

/* Reduce paint areas */
.apex-table tbody {
    contain: layout style paint;
}

/* Efficient scrolling */
.apex-sidebar .sidebar-nav {
    overscroll-behavior: contain;
}


/* ========================================
   23. LOADING STATES (Accessibility)
   ======================================== */

/* Loading spinner utility */
.loading {
    position: relative;
    overflow: hidden;
}

.loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin: -10px 0 0 -10px;
    border: 2px solid transparent;
    border-top: 2px solid var(--color-primary);
    border-radius: 50%;
    animation: apex-spin 1s linear infinite;
}

@keyframes apex-spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.apex-spinner {
    width: 24px;
    height: 24px;
    border: 3px solid var(--neutral-200);
    border-top-color: var(--color-primary);
    border-radius: 50%;
    animation: apex-spin 0.8s linear infinite;
}

.apex-spinner-lg {
    width: 40px;
    height: 40px;
    border-width: 4px;
}

/* Full page loader */
.page-loader {
    position: fixed;
    inset: 0;
    background: var(--color-surface);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

.page-loader.fade-out {
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-slow);
}

/* Skeleton loading */
.skeleton {
    background: linear-gradient(90deg,
            var(--neutral-100) 25%,
            var(--neutral-200) 50%,
            var(--neutral-100) 75%);
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s infinite;
    border-radius: var(--radius-md);
}

@keyframes skeleton-loading {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

.skeleton-text {
    height: 1em;
    margin-bottom: 0.5em;
}

.skeleton-heading {
    height: 1.5em;
    width: 60%;
    margin-bottom: 1em;
}

.skeleton-avatar {
    width: 40px;
    height: 40px;
    border-radius: var(--radius-full);
}

.skeleton-card {
    height: 200px;
}
