/* =========================================================
   ACCESSIBILITY ENHANCEMENTS — WCAG 2.1 LEVEL AA
   ========================================================= */

/* =========================================================
   FOCUS VISIBLE STATES
   ========================================================= */

/* Remove default browser outline, add visible focus state */
*:focus {
    outline: none;
}

*:focus-visible {
    outline: 3px solid var(--primary);
    outline-offset: 2px;
}

/* High contrast focus for better visibility */
@media (prefers-contrast: more) {
    *:focus-visible {
        outline-width: 4px;
        outline-offset: 3px;
    }
}

/* =========================================================
   LINK STATES
   ========================================================= */

a {
    /* Ensure links are recognizable */
    color: var(--primary);
    text-decoration: underline;
    text-decoration-thickness: 2px;
    text-underline-offset: 3px;
    cursor: pointer;
    transition: color 0.2s ease;
}

a:hover {
    color: var(--primary-hover);
    text-decoration-thickness: 3px;
}

a:visited {
    color: #7c3aed; /* Purple for visited links */
}

a:active {
    color: #5b21b6; /* Darker purple for active */
}

/* Focus state for links */
a:focus-visible {
    outline: 3px solid var(--primary-glow);
    border-radius: 2px;
}

/* =========================================================
   FORM ELEMENTS ACCESSIBILITY
   ========================================================= */

/* Ensure form labels are always associated */
label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 12px;
    color: var(--text);
    font-weight: 500;
    font-size: 14px;
}

/* Required field indicator */
.label-required::after {
    content: ' *';
    color: var(--danger);
    font-weight: 700;
    aria-label: 'required field';
}

/* Input/textarea focus */
input:focus,
textarea:focus,
select:focus {
    outline: none;
    box-shadow: 0 0 0 3px var(--primary-glow);
}

/* High contrast input focus */
@media (prefers-contrast: more) {
    input:focus,
    textarea:focus,
    select:focus {
        outline: 3px solid var(--primary);
        outline-offset: 2px;
    }
}

/* Placeholder text color */
::placeholder {
    color: var(--muted);
    opacity: 0.8;
}

/* =========================================================
   BUTTON ACCESSIBILITY
   ========================================================= */

button {
    cursor: pointer;
    min-height: 44px;
    min-width: 44px;
}

button:focus-visible {
    outline: 3px solid var(--primary);
    outline-offset: 2px;
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.6;
}

/* =========================================================
   COLOR CONTRAST FIXES (WCAG AA)
   ========================================================= */

/* Primary text must have 7:1 contrast on light background */
body {
    color: var(--text);
    background: var(--bg);
}

/* Ensure all text is readable */
.muted,
.text-muted {
    color: var(--muted);
    /* Ensure 4.5:1 contrast for AA */
}

/* Fix text on light backgrounds */
.light-bg {
    background: white;
    color: var(--text);
}

/* Fix text on dark backgrounds */
.dark-bg {
    background: var(--text);
    color: white;
}

/* =========================================================
   SKIP LINK — KEYBOARD NAVIGATION
   ========================================================= */

.skip-to-main {
    position: absolute;
    top: -40px;
    left: 0;
    background: var(--primary);
    color: white;
    padding: 8px 16px;
    text-decoration: none;
    z-index: 100;
    border-radius: 0 0 4px 0;
}

.skip-to-main:focus {
    top: 0;
}

/* =========================================================
   SEMANTIC HTML ELEMENTS
   ========================================================= */

/* Ensure headings are properly styled */
h1, h2, h3, h4, h5, h6 {
    margin-top: 0;
    font-weight: 600;
    line-height: 1.2;
}

h1 {
    font-size: 28px;
    margin-bottom: 16px;
}

h2 {
    font-size: 24px;
    margin-bottom: 14px;
}

h3 {
    font-size: 20px;
    margin-bottom: 12px;
}

h4 {
    font-size: 18px;
    margin-bottom: 10px;
}

/* =========================================================
   ARIA LIVE REGIONS
   ========================================================= */

[aria-live="polite"],
[aria-live="assertive"] {
    /* Ensure live regions are not hidden */
    display: block;
    position: relative;
}

/* Status messages */
[role="status"] {
    position: relative;
    display: block;
}

/* Alerts */
[role="alert"] {
    position: relative;
    display: block;
    margin-bottom: 16px;
}

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

/* =========================================================
   HIGH CONTRAST MODE
   ========================================================= */

@media (prefers-contrast: more) {
    body {
        font-weight: 500;
    }
    
    button,
    a {
        border-width: 2px;
    }
    
    input,
    textarea,
    select {
        border-width: 2px;
    }
}

/* =========================================================
   COLOR BLIND FRIENDLY
   ========================================================= */

/* Use patterns in addition to colors */
.status-success::before {
    content: '✓ ';
}

.status-error::before {
    content: '✗ ';
}

.status-warning::before {
    content: '⚠ ';
}

.status-info::before {
    content: 'ℹ ';
}

/* =========================================================
   TEXT SIZING & RESPONSIVE
   ========================================================= */

body {
    font-size: 16px; /* 1rem base */
    line-height: 1.5;
}

/* Support browser zoom up to 200% */
@supports (zoom: 200%) {
    html {
        zoom: 1;
    }
}

/* =========================================================
   POINTER & HOVER STATES
   ========================================================= */

/* For devices that support hover */
@media (hover: hover) and (pointer: fine) {
    a:hover {
        text-decoration-thickness: 3px;
    }
    
    button:hover {
        background-color: var(--primary-hover);
    }
}

/* For touch devices */
@media (hover: none) and (pointer: coarse) {
    /* Make touch targets larger */
    button,
    a {
        min-height: 48px;
        min-width: 48px;
    }
    
    /* Reduce pointer:fine styles */
    a:hover {
        text-decoration-thickness: 2px;
    }
}

/* =========================================================
   SCREEN READER ONLY TEXT
   ========================================================= */

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

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

/* =========================================================
   IMAGES & ICONS
   ========================================================= */

img {
    max-width: 100%;
    height: auto;
    display: block;
}

/* Ensure images have alt text */
img[alt] {
    /* Good! Has alt text */
}

/* SVG icons should be hidden from screen readers if decorative */
svg.icon[role="img"] {
    /* Should have aria-label or title */
}

/* =========================================================
   LISTS
   ========================================================= */

ul, ol {
    margin: 12px 0;
    padding-left: 24px;
}

li {
    margin: 6px 0;
    line-height: 1.6;
}

/* =========================================================
   TABLES
   ========================================================= */

table {
    border-collapse: collapse;
    width: 100%;
    margin: 16px 0;
}

thead {
    background: var(--primary-soft);
    border-bottom: 2px solid var(--primary);
}

th {
    padding: 12px;
    text-align: left;
    font-weight: 700;
    color: var(--text);
}

td {
    padding: 12px;
    border-bottom: 1px solid var(--panel-border);
}

tr:focus-within {
    background: var(--primary-soft);
}

/* =========================================================
   DARK MODE ACCESSIBILITY
   ========================================================= */

[data-theme="dark"] {
    color-scheme: dark;
}

[data-theme="dark"] a {
    color: #60a5fa; /* Brighter blue for dark mode */
}

[data-theme="dark"] a:visited {
    color: #a78bfa; /* Brighter purple for dark mode */
}

[data-theme="dark"] a:focus-visible {
    outline-color: var(--primary);
}

/* =========================================================
   FOCUS ORDER & TABINDEX
   ========================================================= */

/* Never use tabindex > 0 */
[tabindex="1"],
[tabindex="2"],
[tabindex="3"] {
    /* Consider refactoring to use natural DOM order */
}

/* Use tabindex="-1" for programmatically focusable elements */
[tabindex="-1"] {
    outline: none;
}

[tabindex="-1"]:focus-visible {
    outline: 3px solid var(--primary);
    outline-offset: 2px;
}
