/**
 * Accessibility Styles
 * Focus indicators, screen reader utilities, keyboard navigation
 */

/* ==========================================================================
   Screen Reader Only Content
   ========================================================================== */

.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:active,
.sr-only-focusable:focus {
    position: static;
    width: auto;
    height: auto;
    overflow: visible;
    clip: auto;
    white-space: normal;
}

/* ==========================================================================
   Skip Links
   ========================================================================== */

.skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    background: var(--bs-primary);
    color: white;
    padding: 0.75rem 1.5rem;
    text-decoration: none;
    z-index: 10000;
    border-radius: 0 0 0.25rem 0;
    font-weight: 600;
}

.skip-link:focus {
    top: 0;
    outline: 3px solid var(--bs-warning);
    outline-offset: 2px;
}

/* ==========================================================================
   Focus Indicators (Keyboard Navigation)
   ========================================================================== */

body:not(.using-keyboard) *:focus {
    outline: none;
}

body.using-keyboard *:focus {
    outline: 3px solid var(--bs-primary);
    outline-offset: 2px;
    border-radius: 0.25rem;
}

/* Enhanced focus for interactive elements */
body.using-keyboard button:focus,
body.using-keyboard a:focus,
body.using-keyboard input:focus,
body.using-keyboard select:focus,
body.using-keyboard textarea:focus {
    outline: 3px solid var(--bs-primary);
    outline-offset: 3px;
    box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25);
}

/* Focus for cards and interactive containers */
body.using-keyboard .card:focus-within {
    outline: 2px solid var(--bs-primary);
    outline-offset: 2px;
}

/* ==========================================================================
   High Contrast Mode Support
   ========================================================================== */

@media (prefers-contrast: high) {
    body.using-keyboard *:focus {
        outline-width: 4px;
        outline-offset: 3px;
    }

    .btn {
        border-width: 2px;
    }

    .form-control {
        border-width: 2px;
    }
}

/* ==========================================================================
   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;
    }

    .loading-overlay {
        animation: none;
    }

    .history-event-card {
        animation: none;
    }

    .error-icon {
        animation: none;
    }
}

/* ==========================================================================
   Keyboard Navigation Indicators
   ========================================================================== */

[role="button"]:not(button):not(a) {
    cursor: pointer;
}

[role="button"][tabindex="0"]:hover {
    text-decoration: underline;
}

/* Arrow key navigable lists */
[role="listbox"],
[role="menu"] {
    list-style: none;
    padding: 0;
}

[role="option"],
[role="menuitem"] {
    cursor: pointer;
    padding: 0.5rem 1rem;
}

[role="option"]:focus,
[role="menuitem"]:focus {
    background-color: var(--bs-primary);
    color: white;
}

[role="option"][aria-selected="true"],
[role="menuitem"][aria-selected="true"] {
    background-color: var(--bs-light);
    font-weight: 600;
}

/* ==========================================================================
   ARIA Live Regions
   ========================================================================== */

[role="alert"],
[role="status"] {
    padding: 1rem;
    margin: 1rem 0;
}

[aria-live="polite"],
[aria-live="assertive"] {
    position: relative;
}

/* ==========================================================================
   Form Accessibility
   ========================================================================== */

label {
    cursor: pointer;
}

.required::after {
    content: " *";
    color: var(--bs-danger);
    font-weight: bold;
}

.form-text {
    font-size: 0.875rem;
    margin-top: 0.25rem;
}

/* Error states */
.is-invalid {
    border-color: var(--bs-danger) !important;
}

.invalid-feedback {
    display: none;
    color: var(--bs-danger);
    font-size: 0.875rem;
    margin-top: 0.25rem;
}

.invalid-feedback.d-block {
    display: block !important;
}

/* ==========================================================================
   Modal Accessibility
   ========================================================================== */

.modal[aria-hidden="true"] {
    display: none;
}

.modal[aria-hidden="false"] {
    display: block;
}

.modal-backdrop {
    background-color: rgba(0, 0, 0, 0.5);
}

/* ==========================================================================
   Tooltips and Popovers
   ========================================================================== */

[data-bs-toggle="tooltip"],
[data-bs-toggle="popover"] {
    cursor: help;
}

/* ==========================================================================
   Loading States for Screen Readers
   ========================================================================== */

.loading-element[aria-busy="true"]::after {
    content: "Loading...";
    position: absolute;
    left: -9999px;
}

/* ==========================================================================
   Link Styling for Accessibility
   ========================================================================== */

a {
    text-decoration: underline;
    text-decoration-skip-ink: auto;
}

a:hover,
a:focus {
    text-decoration-thickness: 2px;
}

/* Button links should not have underline */
a.btn {
    text-decoration: none;
}

/* ==========================================================================
   Table Accessibility
   ========================================================================== */

table {
    caption-side: top;
}

table caption {
    font-weight: 600;
    padding: 0.5rem;
    text-align: left;
}

th[scope="col"] {
    text-align: left;
}

/* ==========================================================================
   Color Contrast Enhancements
   ========================================================================== */

.text-muted {
    color: #6c757d !important;
}

@media (prefers-contrast: high) {
    .text-muted {
        color: #495057 !important;
    }
}

/* ==========================================================================
   Print Accessibility
   ========================================================================== */

@media print {
    .skip-link,
    .loading-overlay,
    button,
    [role="navigation"] {
        display: none !important;
    }

    a[href]::after {
        content: " (" attr(href) ")";
        font-size: 0.875rem;
        color: #666;
    }

    a.btn[href]::after {
        content: none;
    }
}

/* ==========================================================================
   Touch Target Size (Mobile Accessibility)
   ========================================================================== */

@media (max-width: 768px) {
    button,
    a,
    input[type="checkbox"],
    input[type="radio"],
    select {
        min-height: 44px;
        min-width: 44px;
    }

    .btn {
        padding: 0.75rem 1.5rem;
    }
}

/* ==========================================================================
   Dark Mode Accessibility - Support both manual and auto themes
   ========================================================================== */

/* Manual dark mode (user toggled) */
[data-theme="dark"] body.using-keyboard *:focus {
    outline-color: #6ea8fe;
    box-shadow: 0 0 0 0.25rem rgba(110, 168, 254, 0.25);
}

[data-theme="dark"] .skip-link {
    background: #0d6efd;
}

[data-theme="dark"] [role="option"]:focus,
[data-theme="dark"] [role="menuitem"]:focus {
    background-color: #0d6efd;
}

/* Auto mode with system dark preference */
@media (prefers-color-scheme: dark) {
    html:not([data-theme="light"]) body.using-keyboard *:focus {
        outline-color: #6ea8fe;
        box-shadow: 0 0 0 0.25rem rgba(110, 168, 254, 0.25);
    }

    html:not([data-theme="light"]) .skip-link {
        background: #0d6efd;
    }

    html:not([data-theme="light"]) [role="option"]:focus,
    html:not([data-theme="light"]) [role="menuitem"]:focus {
        background-color: #0d6efd;
    }
}
