/**
 * Accessibility Utilities
 * Supports WCAG 2.1 Level AA compliance
 */

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

/* Focus Visible Styles */
:focus-visible {
  outline: 3px solid var(--color-primary-500);
  outline-offset: 2px;
  border-radius: var(--border-radius-base);
}

button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible {
  outline: 3px solid var(--color-primary-500);
  outline-offset: 2px;
}

/* Reduced Motion Support */
@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 Support */
@media (prefers-contrast: more) {
  :root {
    --color-border-primary: #000;
    --color-text-primary: #000;
    --color-text-secondary: #333;
  }

  @media (prefers-color-scheme: dark) {
    :root {
      --color-border-primary: #fff;
      --color-text-primary: #fff;
      --color-text-secondary: #ccc;
    }
  }
}

/* Forced Colors Mode (Windows High Contrast) */
@media (forced-colors: active) {
  button {
    border: 1px solid ButtonBorder;
    background-color: ButtonFace;
    color: ButtonText;
  }

  button:focus-visible {
    outline: 3px solid Highlight;
  }

  a {
    color: LinkText;
  }

  a:visited {
    color: VisitedText;
  }
}

/* Skip Navigation Link */
.skip-to-content {
  position: absolute;
  top: -40px;
  left: 0;
  background: var(--color-primary-500);
  color: white;
  padding: var(--spacing-3) var(--spacing-4);
  text-decoration: none;
  z-index: var(--z-index-tooltip);
}

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

/* Visible Label Requirement */
/* Ensures all form inputs have associated labels */
input[type="text"]:not([aria-label]):not([aria-labelledby]),
input[type="email"]:not([aria-label]):not([aria-labelledby]),
input[type="password"]:not([aria-label]):not([aria-labelledby]),
input[type="number"]:not([aria-label]):not([aria-labelledby]),
textarea:not([aria-label]):not([aria-labelledby]),
select:not([aria-label]):not([aria-labelledby]) {
  /* Warn in dev that inputs need labels */
}

/* Color Contrast Checker */
/* Ensures text meets WCAG AA standards */
/* Light mode: 4.5:1 for normal text, 3:1 for large text */
.text-primary {
  color: var(--color-text-primary);
  /* Contrast: ~12:1 (AAA) */
}

.text-secondary {
  color: var(--color-text-secondary);
  /* Contrast: ~8:1 (AAA) */
}

.text-tertiary {
  color: var(--color-text-tertiary);
  /* Contrast: ~4.5:1 (AA) */
}

/* Warning: Low contrast zone */
.text-quaternary {
  color: var(--color-neutral-400);
  /* Contrast: ~2.5:1 (FAILS AA) - use sparingly */
}

/* Active/Focus States for Keyboard Navigation */
[role="menuitem"]:focus,
[role="menuitem"][aria-selected="true"] {
  background-color: var(--color-background-hover);
  outline: 2px solid var(--color-primary-500);
  outline-offset: -2px;
}

[role="tab"]:focus,
[role="tab"][aria-selected="true"] {
  background-color: var(--color-primary-50);
  outline: 2px solid var(--color-primary-500);
  outline-offset: -2px;
}

/* Tooltip Accessibility */
[role="tooltip"] {
  max-width: 250px;
  padding: var(--spacing-2) var(--spacing-3);
  background-color: var(--color-neutral-900);
  color: white;
  border-radius: var(--border-radius-base);
  font-size: var(--font-size-sm);
  pointer-events: none;
}

/* Dialog/Modal Accessibility */
[role="dialog"] {
  position: fixed;
  z-index: var(--z-index-modal);
}

[role="dialog"]::backdrop {
  background-color: rgba(0, 0, 0, 0.5);
}

/* Combobox/Listbox Accessibility */
[role="listbox"] {
  max-height: 300px;
  overflow-y: auto;
}

[role="option"] {
  padding: var(--spacing-2) var(--spacing-3);
  cursor: pointer;
}

[role="option"]:hover,
[role="option"][aria-selected="true"] {
  background-color: var(--color-background-hover);
}

[role="option"]:focus {
  outline: 2px solid var(--color-primary-500);
  outline-offset: -2px;
}

/* Loading State Accessibility */
[aria-busy="true"] {
  opacity: 0.6;
  pointer-events: none;
}

/* Disabled State Accessibility */
[aria-disabled="true"],
:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Error/Alert Accessibility */
[role="alert"],
[aria-live="assertive"] {
  padding: var(--spacing-3);
  background-color: var(--color-danger-50);
  border-left: 4px solid var(--color-danger-500);
  border-radius: var(--border-radius-base);
}

[role="alert"] .sr-only {
  position: static;
  width: auto;
  height: auto;
  clip: auto;
}

/* Progress Bar Accessibility */
[role="progressbar"] {
  height: 20px;
  background-color: var(--color-background-secondary);
  border-radius: var(--border-radius-base);
  overflow: hidden;
}

[role="progressbar"]::after {
  content: attr(aria-valuenow) "%";
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
}
