/* =============================================================================
   Accessibility Styles - WCAG 2.1 AA Compliance
   ============================================================================= */

/* Skip Links */
.skip-link {
    position: absolute;
    top: -40px;
    left: 6px;
    background: var(--primary);
    color: white;
    padding: 8px 12px;
    text-decoration: none;
    border-radius: var(--radius-sm);
    z-index: 9999;
    font-weight: 600;
    font-size: var(--text-sm);
    transition: top 0.3s ease;
}

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

/* Focus Management */
*:focus {
    outline: 3px solid var(--focus);
    outline-offset: 2px;
}

/* Enhanced focus for interactive elements */
button:focus,
a:focus,
input:focus,
select:focus,
textarea:focus,
[tabindex]:focus {
    outline: 3px solid var(--focus);
    outline-offset: 2px;
    box-shadow: 0 0 0 6px rgba(14, 165, 233, 0.1);
}

/* Remove default focus outline when our custom one is present */
button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible {
    outline: 3px solid var(--focus);
    outline-offset: 2px;
}

/* 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:focus {
    position: static;
    width: auto;
    height: auto;
    padding: initial;
    margin: initial;
    overflow: visible;
    clip: auto;
    white-space: normal;
}

/* Live Regions for Dynamic Content */
.live-region {
    position: absolute;
    left: -10000px;
    top: auto;
    width: 1px;
    height: 1px;
    overflow: hidden;
}

[aria-live="polite"],
[aria-live="assertive"] {
    speak: normal;
}

/* High Contrast Mode */
@media (prefers-contrast: high), .high-contrast {
    :root {
        --ink: #000000;
        --muted: #333333;
        --bg: #FFFFFF;
        --bg-soft: #F5F5F5;
        --primary: #0066CC;
        --accent: #FF8800;
        --success: #006600;
        --danger: #CC0000;
        --focus: #0066FF;
        --shadow: 0 4px 8px rgba(0, 0, 0, 0.5);
    }
    
    .card,
    .btn,
    .input,
    .select {
        border: 2px solid var(--ink);
    }
    
    .btn-ghost {
        border: 2px solid var(--primary);
        background: var(--bg);
    }
    
    .btn-ghost:hover {
        background: var(--primary);
        color: var(--bg);
    }
}

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

.reduced-motion *,
.reduced-motion *::before,
.reduced-motion *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
}

/* Touch Target Sizes (minimum 44x44px) */
button,
.btn,
a,
input[type="button"],
input[type="submit"],
input[type="reset"],
[role="button"],
[tabindex="0"] {
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* Exception for small buttons in specific contexts */
.btn-sm,
.tooltip-close,
.modal-close {
    min-width: 36px;
    min-height: 36px;
}

/* Text inputs */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="search"],
input[type="url"],
input[type="tel"],
textarea,
select {
    min-height: 44px;
    padding: 8px 12px;
}

/* Read Aloud Styles */
.read-aloud-container {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.read-aloud-toggle {
    transition: all 0.2s ease;
}

.read-aloud-toggle:hover {
    transform: translateY(-1px);
}

.read-aloud-toggle .read-aloud-icon {
    margin-right: 0.25rem;
    font-size: 1.125rem;
}

.read-aloud-btn {
    display: none; /* Hidden by default, shown when read-aloud is enabled */
    align-items: center;
    gap: 0.25rem;
    font-size: 0.875rem;
    padding: 0.375rem 0.75rem;
}

.being-read {
    background: rgba(58, 134, 255, 0.1) !important;
    border: 2px solid var(--primary) !important;
    box-shadow: 0 0 0 4px rgba(58, 134, 255, 0.1) !important;
    animation: reading-pulse 2s ease-in-out infinite;
}

@keyframes reading-pulse {
    0%, 100% { 
        background: rgba(58, 134, 255, 0.1) !important;
    }
    50% { 
        background: rgba(58, 134, 255, 0.2) !important;
    }
}

/* Glossary Styles */
.glossary-term {
    background: rgba(255, 200, 87, 0.15);
    border-bottom: 1px dotted var(--accent);
    cursor: help;
    transition: all 0.2s ease;
    padding: 1px 2px;
    border-radius: 2px;
    position: relative;
}

.glossary-term:hover,
.glossary-term:focus {
    background: rgba(255, 200, 87, 0.3);
    outline: 2px solid var(--focus);
    outline-offset: 1px;
    text-decoration: underline;
}

.glossary-term.active {
    background: var(--accent);
    color: var(--ink);
    font-weight: 600;
}

.glossary-tooltip {
    position: fixed;
    background: var(--bg);
    border: 2px solid var(--primary);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    padding: 0;
    z-index: 9999;
    max-width: 300px;
    min-width: 200px;
    font-size: var(--text-sm);
    animation: tooltip-appear 0.2s ease-out;
}

@keyframes tooltip-appear {
    from {
        opacity: 0;
        transform: translateY(-8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.tooltip-header {
    background: var(--primary);
    color: white;
    padding: 0.75rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: var(--radius) var(--radius) 0 0;
}

.tooltip-header strong {
    font-weight: 600;
    font-size: var(--text-base);
}

.tooltip-close {
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    font-size: 1.25rem;
    line-height: 1;
    padding: 0;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: background 0.2s ease;
}

.tooltip-close:hover,
.tooltip-close:focus {
    background: rgba(255, 255, 255, 0.2);
    outline: 2px solid white;
}

.tooltip-content {
    padding: 1rem;
    color: var(--ink);
    line-height: 1.5;
}

.tooltip-actions {
    padding: 0.5rem 1rem 1rem;
    display: flex;
    justify-content: flex-end;
}

.tooltip-read {
    font-size: 0.875rem;
    padding: 0.375rem 0.75rem;
}

/* Accessibility Notifications */
.a11y-notification {
    animation: notification-slide-in 0.3s ease-out;
}

@keyframes notification-slide-in {
    from {
        opacity: 0;
        transform: translate(-50%, -20px);
    }
    to {
        opacity: 1;
        transform: translate(-50%, 0);
    }
}

/* ARIA States and Properties */
[aria-expanded="true"] .expand-icon {
    transform: rotate(180deg);
}

[aria-pressed="true"] {
    background: var(--primary);
    color: white;
}

[aria-current="page"] {
    font-weight: 600;
    color: var(--primary);
    text-decoration: underline;
}

[aria-current="step"] {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

[aria-disabled="true"] {
    opacity: 0.6;
    cursor: not-allowed;
    pointer-events: none;
}

[aria-hidden="true"] {
    display: none !important;
}

/* Loading and Error States */
[aria-busy="true"] {
    position: relative;
    pointer-events: none;
}

[aria-busy="true"]::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: inherit;
}

[aria-invalid="true"] {
    border-color: var(--danger);
    box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1);
}

/* Keyboard Navigation Enhancements */
.focus-trap {
    position: relative;
}

.focus-trap:focus-within {
    outline: 2px solid var(--primary);
    outline-offset: 4px;
}

/* Modal and Dialog Accessibility */
.modal[aria-hidden="false"] {
    display: flex !important;
}

.modal-content {
    position: relative;
}

.modal-content:focus {
    outline: none;
}

/* Form Accessibility */
.form-group {
    margin-bottom: 1rem;
}

.form-label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
    color: var(--ink);
}

.form-label.required::after {
    content: ' *';
    color: var(--danger);
}

.form-help {
    font-size: var(--text-sm);
    color: var(--muted);
    margin-top: 0.25rem;
}

.form-error {
    font-size: var(--text-sm);
    color: var(--danger);
    margin-top: 0.25rem;
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.form-error::before {
    content: '⚠️';
}

/* Table Accessibility */
table {
    border-collapse: collapse;
    width: 100%;
}

th {
    text-align: left;
    background: var(--bg-soft);
    font-weight: 600;
    padding: 0.75rem;
    border: 1px solid #E5E7EB;
}

td {
    padding: 0.75rem;
    border: 1px solid #E5E7EB;
}

caption {
    caption-side: top;
    text-align: left;
    font-weight: 600;
    margin-bottom: 0.5rem;
}

/* Progress and Status Indicators */
.progress[role="progressbar"] {
    background: var(--bg-soft);
    border: 1px solid #E5E7EB;
    border-radius: var(--radius);
    overflow: hidden;
}

.progress-bar {
    height: 100%;
    background: var(--primary);
    transition: width 0.3s ease;
}

.status-indicator {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius-sm);
    font-size: var(--text-sm);
    font-weight: 500;
}

.status-indicator.success {
    background: rgba(46, 196, 182, 0.1);
    color: var(--success);
}

.status-indicator.warning {
    background: rgba(255, 200, 87, 0.1);
    color: #D97706;
}

.status-indicator.error {
    background: rgba(239, 68, 68, 0.1);
    color: var(--danger);
}

/* Print Styles for Accessibility */
@media print {
    .skip-link,
    .read-aloud-toggle,
    .read-aloud-btn,
    .tooltip-close,
    button {
        display: none !important;
    }
    
    .glossary-term {
        background: none !important;
        border-bottom: 1px solid #000 !important;
    }
    
    .glossary-term::after {
        content: " [" attr(title) "]";
        font-style: italic;
        font-size: 0.9em;
    }
    
    a::after {
        content: " (" attr(href) ")";
        font-size: 0.8em;
        color: #666;
    }
}

/* Responsive Accessibility */
@media (max-width: 768px) {
    .read-aloud-toggle .read-aloud-text {
        display: none;
    }
    
    .glossary-tooltip {
        max-width: calc(100vw - 20px);
        left: 10px !important;
        right: 10px !important;
        width: auto !important;
    }
    
    .a11y-notification {
        max-width: calc(100vw - 20px);
        left: 10px !important;
        right: 10px !important;
        transform: translateX(0) !important;
    }
}

@media (max-width: 480px) {
    /* Increase touch targets on small screens */
    button,
    .btn,
    a,
    [role="button"] {
        min-width: 48px;
        min-height: 48px;
    }
    
    input[type="text"],
    input[type="email"],
    input[type="password"],
    textarea,
    select {
        min-height: 48px;
        font-size: 16px; /* Prevent zoom on iOS */
    }
}

/* Color Contrast Overrides */
@media (prefers-contrast: high) {
    .btn-ghost {
        border: 2px solid currentColor;
    }
    
    .card {
        border: 2px solid var(--ink);
    }
    
    .glossary-term {
        background: yellow;
        color: black;
        border-bottom: 2px solid black;
    }
}

/* Animation Preferences */
@media (prefers-reduced-motion: no-preference) {
    .focus-visible,
    .being-read,
    .glossary-term:hover {
        transition: all 0.2s ease;
    }
}

/* Dark Mode Accessibility (if implemented) */
@media (prefers-color-scheme: dark) {
    .glossary-tooltip {
        background: #1f2937;
        border-color: #374151;
        color: #f9fafb;
    }
    
    .tooltip-header {
        background: #374151;
    }
    
    .a11y-notification {
        background: #1f2937;
        color: #f9fafb;
        border-color: #374151;
    }
}

/* Voice Over and NVDA Specific Enhancements */
.nvda-speak {
    speak: always;
}

.jaws-speak {
    speak: always;
}
