/**
 * Accessibility Utilities
 *
 * WCAG 2.1 AA compliant utilities and features for improved accessibility.
 * Includes focus management, screen reader utilities, and responsive enhancements.
 */

/* ==================== FOCUS MANAGEMENT ==================== */

/**
 * Visible focus indicator for keyboard navigation
 * Meets WCAG 2.1 AA contrast requirements (3:1)
 */
:focus-visible {
  outline: var(--focus-ring-width, 3px) solid currentColor;
  outline-offset: var(--focus-ring-offset, 2px);
  box-shadow: 0 0 0 var(--focus-ring-width, 3px)
              rgba(0, 0, 0, var(--focus-ring-opacity, 0.5));
}

/**
 * Enhanced focus for interactive elements
 */
button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: var(--focus-ring-width, 3px) solid currentColor;
  outline-offset: var(--focus-ring-offset, 2px);
}

/**
 * Skip to main content link (for keyboard users)
 */
.skip-link {
  position: absolute;
  top: -100%;
  left: 0;
  padding: var(--spacing-sm) var(--spacing-md);
  background-color: var(--gray-900, #202124);
  color: var(--white, #ffffff);
  font-weight: var(--font-weight-bold);
  text-decoration: none;
  z-index: var(--z-max);
}

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

/* ==================== SCREEN READER UTILITIES ==================== */

/**
 * Screen reader only (visually hidden, accessible to AT)
 * Use for:
 * - Descriptive text for icons
 * - Form labels when visual label not desired
 * - Additional context for screen readers
 */
.sr-only,
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

/**
 * Restore visibility when focused (for skip links)
 */
.sr-only:focus,
.sr-only:active,
.visually-hidden:focus,
.visually-hidden:active {
  position: static;
  width: auto;
  height: auto;
  padding: inherit;
  margin: inherit;
  overflow: visible;
  clip: auto;
  white-space: normal;
}

/**
 * Screen reader focusable (hidden until focused)
 * Allows tabbing to hidden elements
 */
.sr-only-focusable:not(:focus):not(:focus-within) {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

/* ==================== TOUCH TARGET SIZING ==================== */

/**
 * Minimum touch target size per WCAG 2.1 AA (44x44px)
 * Applies to all interactive elements
 */
button,
a,
input[type="checkbox"],
input[type="radio"],
select,
[role="button"],
[role="link"] {
  min-height: var(--touch-target-min, 44px);
  min-width: var(--touch-target-min, 44px);
}

/**
 * Touch target utility class
 */
.touch-target {
  min-height: var(--touch-target-min, 44px);
  min-width: var(--touch-target-min, 44px);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* ==================== COLOR CONTRAST ==================== */

/**
 * High contrast text utilities
 * Ensures WCAG AA contrast ratio (4.5:1 for normal text, 3:1 for large)
 */
.text-high-contrast {
  color: var(--gray-950, #000000);
  font-weight: var(--font-weight-semibold);
}

.bg-high-contrast {
  background-color: var(--gray-950, #000000);
  color: var(--white, #ffffff);
}

/* ==================== REDUCED MOTION ==================== */

/**
 * Respect user's motion preferences
 * Removes animations for users who prefer 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;
  }

  /* Utility to explicitly opt-in to motion */
  .motion-safe {
    animation-duration: inherit !important;
    transition-duration: inherit !important;
  }
}

/* ==================== DYSLEXIA-FRIENDLY FEATURES ==================== */

/**
 * Dyslexia-friendly text styling
 * Improves readability for users with dyslexia
 */
.dyslexia-friendly {
  font-family: var(--font-accessible-dyslexic);
  letter-spacing: var(--letter-spacing-wider);
  word-spacing: 0.16em;
  line-height: var(--line-height-relaxed);
  font-weight: var(--font-weight-medium);
}

/* ==================== KEYBOARD NAVIGATION HELPERS ==================== */

/**
 * Highlight focusable elements on keyboard navigation
 */
body.keyboard-nav *:focus {
  outline: var(--focus-ring-width, 3px) dashed currentColor;
  outline-offset: var(--focus-ring-offset, 2px);
}

/**
 * Focus within container indicator
 */
.focus-within:focus-within {
  outline: var(--border-width-medium) solid currentColor;
  outline-offset: var(--spacing-xs);
}

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

/**
 * Styles for ARIA live regions
 * Screen readers announce content changes
 */
[aria-live] {
  position: relative;
}

[aria-live="polite"]::before {
  content: '';
  /* Live region indicator (optional visual cue) */
}

[aria-live="assertive"] {
  /* Higher priority announcements */
  font-weight: var(--font-weight-semibold);
}

/* ==================== DISABLED STATE ==================== */

/**
 * Consistent disabled styling
 * Maintains WCAG contrast requirements
 */
[disabled],
[aria-disabled="true"] {
  opacity: 0.6;
  cursor: not-allowed;
  pointer-events: none;
}

button:disabled,
input:disabled,
select:disabled,
textarea:disabled {
  background-color: var(--gray-200, #e8eaed);
  color: var(--gray-600, #80868b);
  border-color: var(--gray-300, #dadce0);
}

/* ==================== LOADING & BUSY STATES ==================== */

/**
 * Loading state indicator
 */
[aria-busy="true"] {
  cursor: wait;
  opacity: 0.7;
}

[aria-busy="true"]::after {
  content: '...';
  animation: ellipsis 1.5s infinite;
}

@keyframes ellipsis {
  0%, 20% { content: '.'; }
  40% { content: '..'; }
  60%, 100% { content: '...'; }
}

/* ==================== LANDMARK REGIONS ==================== */

/**
 * Visual distinction for landmark regions (development/testing)
 * Remove in production or use only in debug mode
 */
body.show-landmarks [role="banner"],
body.show-landmarks [role="navigation"],
body.show-landmarks [role="main"],
body.show-landmarks [role="complementary"],
body.show-landmarks [role="contentinfo"] {
  outline: 2px dashed rgba(255, 0, 0, 0.3);
  outline-offset: -2px;
}

/* ==================== TEXT SELECTION ==================== */

/**
 * Accessible text selection colors
 * Maintains readability when text is selected
 */
::selection {
  background-color: var(--gray-800, #3c4043);
  color: var(--white, #ffffff);
}

::-moz-selection {
  background-color: var(--gray-800, #3c4043);
  color: var(--white, #ffffff);
}

/* ==================== FORCED COLORS MODE ==================== */

/**
 * Windows High Contrast Mode support
 */
@media (forced-colors: active) {
  * {
    border-color: currentColor !important;
  }

  button,
  a,
  input,
  select,
  textarea {
    border: var(--border-width-medium) solid currentColor !important;
  }

  :focus-visible {
    outline: var(--border-width-thick) solid currentColor !important;
    outline-offset: var(--spacing-xs) !important;
  }
}

/* ==================== PRINT ACCESSIBILITY ==================== */

/**
 * Ensure print accessibility
 */
@media print {
  /* Show URLs after links */
  a[href]::after {
    content: " (" attr(href) ")";
    font-size: 0.875em;
    font-weight: normal;
  }

  /* Don't show URLs for fragment identifiers or javascript */
  a[href^="#"]::after,
  a[href^="javascript:"]::after {
    content: "";
  }

  /* Show abbreviation titles */
  abbr[title]::after {
    content: " (" attr(title) ")";
  }
}

/* ==================== UTILITY CLASSES ==================== */

/**
 * Focusable utility
 * Makes non-interactive elements focusable
 */
.focusable {
  tabindex: 0;
}

/**
 * Non-focusable utility
 * Removes from tab order (use sparingly)
 */
.no-focus {
  tabindex: -1;
}

/**
 * Semantic hidden
 * Hides both visually and from screen readers
 */
.hidden {
  display: none !important;
}

/**
 * Visible utility (undoes .hidden)
 */
.visible {
  display: block !important;
}

.inline-visible {
  display: inline !important;
}

.flex-visible {
  display: flex !important;
}

/**
 * Accessible color contrast indicators
 * For development/testing only
 */
.contrast-check-fail {
  outline: 3px solid red;
  outline-offset: 2px;
}

.contrast-check-warn {
  outline: 3px solid orange;
  outline-offset: 2px;
}

.contrast-check-pass {
  outline: 3px solid green;
  outline-offset: 2px;
}

/* ==================== RESPONSIVE ACCESSIBILITY ==================== */

/**
 * Mobile-specific accessibility improvements
 */
@media (max-width: 48em) {
  /* Larger touch targets on mobile */
  button,
  a,
  input[type="checkbox"],
  input[type="radio"] {
    min-height: 48px;
    min-width: 48px;
  }

  /* Larger text for better readability */
  body {
    font-size: calc(var(--font-size-base) * 1.1);
  }

  /* Increased line height for easier reading */
  p {
    line-height: var(--line-height-relaxed);
  }
}

/* ==================== LANGUAGE-SPECIFIC ADJUSTMENTS ==================== */

/**
 * Right-to-left language support
 */
[dir="rtl"] {
  direction: rtl;
  text-align: right;
}

[dir="rtl"] .sr-only:focus,
[dir="rtl"] .visually-hidden:focus {
  right: 0;
  left: auto;
}

/**
 * Vertical writing mode support
 */
[data-writing-mode="vertical-rl"] {
  writing-mode: vertical-rl;
}

[data-writing-mode="vertical-lr"] {
  writing-mode: vertical-lr;
}
