/**
 * AITRAIN LMS - Accessibility Styles
 * Comprehensive accessibility enhancements for WCAG 2.1 compliance
 * Version: 1.0
 */

/* ============================================
   SKIP NAVIGATION LINKS
   ============================================ */

.skip-links {
  position: absolute;
  top: 0;
  left: 0;
  z-index: var(--z-max, 10000);
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 0.5rem;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: auto;
  width: 1px;
  height: 1px;
  overflow: hidden;
  padding: 0.75rem 1.25rem;
  background: var(--brand-primary, #F26522);
  color: #ffffff;
  font-weight: 600;
  font-size: 0.875rem;
  text-decoration: none;
  border-radius: 0 0 0.5rem 0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  transition: all 0.2s ease;
  z-index: var(--z-max, 10000);
}

.skip-link:focus {
  position: fixed;
  left: 0;
  top: 0;
  width: auto;
  height: auto;
  overflow: visible;
  outline: 3px solid #ffffff;
  outline-offset: 2px;
}

.skip-link:focus-visible {
  position: fixed;
  left: 0;
  top: 0;
  width: auto;
  height: auto;
  overflow: visible;
}

.skip-link:hover {
  background: var(--brand-primary-dark, #D55A1B);
}

/* Multiple skip links stacking */
.skip-link + .skip-link:focus {
  top: 3rem;
}

.skip-link + .skip-link + .skip-link:focus {
  top: 6rem;
}

/* ============================================
   FOCUS STATES - ENHANCED VISIBILITY
   ============================================ */

/* Default focus styles */
*:focus {
  outline: 2px solid var(--brand-primary, #F26522);
  outline-offset: 2px;
}

/* Focus-visible for keyboard navigation only */
*:focus:not(:focus-visible) {
  outline: none;
}

*:focus-visible {
  outline: 2px solid var(--brand-primary, #F26522);
  outline-offset: 2px;
}

/* High contrast focus ring for interactive elements */
button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[role="button"]:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--brand-primary, #F26522);
  outline-offset: 2px;
  box-shadow: 0 0 0 4px rgba(242, 101, 34, 0.25);
}

/* Dark background focus adjustments */
[data-theme="dark"] button:focus-visible,
[data-theme="dark"] a:focus-visible,
[data-theme="dark"] [role="button"]:focus-visible {
  outline-color: #FF8C4B;
  box-shadow: 0 0 0 4px rgba(255, 140, 75, 0.3);
}

/* Card and interactive container focus */
.card:focus-visible,
.course-card:focus-visible,
.nav-item:focus-visible,
.dropdown-item:focus-visible,
.badge-card:focus-visible,
.leaderboard-item:focus-visible {
  outline: 2px solid var(--brand-primary, #F26522);
  outline-offset: 2px;
  transform: translateY(-2px);
}

/* Form input focus enhancements */
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  border-color: var(--brand-primary, #F26522);
  box-shadow: 0 0 0 3px rgba(242, 101, 34, 0.15);
}

/* Remove default outlines but keep accessible */
button,
a,
input,
select,
textarea {
  outline-offset: 2px;
}

/* ============================================
   SCREEN READER ONLY CONTENT
   ============================================ */

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

/* Allow sr-only content to be focusable */
.sr-only-focusable:focus,
.sr-only-focusable:active {
  position: static !important;
  width: auto !important;
  height: auto !important;
  padding: inherit !important;
  margin: inherit !important;
  overflow: visible !important;
  clip: auto !important;
  white-space: normal !important;
}

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

[aria-live="polite"],
[aria-live="assertive"],
.live-region {
  /* Ensure live regions are accessible */
}

/* Loading announcement styling */
.loading-announcement {
  position: absolute;
  left: -9999px;
}

/* Status message styling */
.status-message[role="status"] {
  padding: 0.75rem 1rem;
  border-radius: 0.5rem;
  margin-bottom: 1rem;
}

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

@media (prefers-contrast: high) {
  :root {
    --brand-primary: #E04600;
    --color-success: #007F3B;
    --color-error: #CC0000;
    --color-warning: #B35900;
    --color-info: #0047B3;
  }

  /* Increase border visibility */
  .card,
  .btn,
  input,
  select,
  textarea,
  .nav-item,
  .dropdown-menu {
    border-width: 2px !important;
  }

  /* Ensure text has sufficient contrast */
  .text-muted,
  .text-secondary {
    color: var(--text-primary) !important;
    opacity: 0.85;
  }

  /* Make focus indicators more prominent */
  *:focus-visible {
    outline-width: 3px !important;
    outline-offset: 3px !important;
  }

  /* Increase button contrast */
  .btn-primary {
    background: #000000;
    color: #ffffff;
  }

  .btn-secondary {
    background: #ffffff;
    color: #000000;
    border-color: #000000;
  }

  /* Links must be clearly distinguishable */
  a {
    text-decoration: underline !important;
  }

  a:hover,
  a:focus {
    text-decoration-thickness: 3px !important;
  }
}

/* Forced colors mode (Windows High Contrast) */
@media (forced-colors: active) {
  .skip-link:focus {
    outline: 3px solid CanvasText;
    background: Canvas;
    color: CanvasText;
  }

  .btn-primary {
    border: 2px solid ButtonText;
  }

  .progress-bar {
    border: 1px solid CanvasText;
  }

  *:focus-visible {
    outline: 3px solid Highlight !important;
  }
}

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

  /* Disable specific animations */
  .skeleton,
  .spinner,
  .loading-spinner,
  .flame,
  [class*="animate"],
  [class*="pulse"],
  [class*="bounce"],
  [class*="shake"],
  [class*="slide"],
  [class*="fade"] {
    animation: none !important;
  }

  /* Remove hover transforms */
  .card:hover,
  .course-card:hover,
  .btn:hover,
  .nav-item:hover {
    transform: none !important;
  }

  /* Disable scroll animations */
  html {
    scroll-behavior: auto !important;
  }

  /* Keep essential state changes visible but instant */
  .modal-backdrop,
  .dropdown-menu,
  .sidebar {
    transition: opacity 0.01ms !important;
  }
}

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

/* Keyboard help modal trigger */
.keyboard-help-trigger {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  width: 40px;
  height: 40px;
  background: var(--bg-tertiary, #334155);
  border: 1px solid var(--border-light, rgba(255, 255, 255, 0.1));
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  color: var(--text-secondary, #94A3B8);
  cursor: pointer;
  z-index: 100;
  transition: all 0.2s ease;
}

.keyboard-help-trigger:hover,
.keyboard-help-trigger:focus-visible {
  background: var(--brand-primary, #F26522);
  color: white;
  transform: scale(1.1);
}

/* Keyboard shortcuts modal */
.keyboard-help-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: var(--z-max, 10000);
  padding: 1rem;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
}

.keyboard-help-modal.open {
  opacity: 1;
  visibility: visible;
}

.keyboard-help-content {
  background: var(--card-bg, #1E293B);
  border-radius: 1rem;
  max-width: 500px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}

.keyboard-help-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.25rem;
  border-bottom: 1px solid var(--border-light, rgba(255, 255, 255, 0.1));
}

.keyboard-help-header h2 {
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--text-primary, #F8FAFC);
}

.keyboard-help-close {
  width: 32px;
  height: 32px;
  background: none;
  border: none;
  border-radius: 0.5rem;
  color: var(--text-muted, #64748B);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.keyboard-help-close:hover,
.keyboard-help-close:focus-visible {
  background: var(--bg-hover, rgba(255, 255, 255, 0.1));
  color: var(--text-primary, #F8FAFC);
}

.keyboard-help-body {
  padding: 1.25rem;
}

.keyboard-shortcut-group {
  margin-bottom: 1.5rem;
}

.keyboard-shortcut-group:last-child {
  margin-bottom: 0;
}

.keyboard-shortcut-group h3 {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted, #64748B);
  margin-bottom: 0.75rem;
}

.keyboard-shortcut {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border-light, rgba(255, 255, 255, 0.05));
}

.keyboard-shortcut:last-child {
  border-bottom: none;
}

.keyboard-shortcut-label {
  font-size: 0.875rem;
  color: var(--text-secondary, #94A3B8);
}

.keyboard-shortcut-keys {
  display: flex;
  gap: 0.25rem;
}

.keyboard-key {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.75rem;
  height: 1.75rem;
  padding: 0 0.5rem;
  background: var(--bg-tertiary, #334155);
  border: 1px solid var(--border-medium, #475569);
  border-radius: 0.375rem;
  font-family: var(--font-mono, monospace);
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-primary, #F8FAFC);
}

/* ============================================
   COLOR CONTRAST PATTERNS
   Add patterns to color-only indicators
   ============================================ */

/* Success indicators with pattern */
.status-success,
.badge-success,
.progress-bar.success {
  background-image: url("data:image/svg+xml,%3Csvg width='6' height='6' viewBox='0 0 6 6' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='%23ffffff' fill-opacity='0.1' fill-rule='evenodd'%3E%3Cpath d='M5 0h1L0 6V5zM6 5v1H5z'/%3E%3C/g%3E%3C/svg%3E");
}

/* Warning indicators with pattern */
.status-warning,
.badge-warning,
.progress-bar.warning {
  background-image: url("data:image/svg+xml,%3Csvg width='6' height='6' viewBox='0 0 6 6' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='%23000000' fill-opacity='0.1' fill-rule='evenodd'%3E%3Cpath d='M3 0h3v3H3z'/%3E%3Cpath d='M0 3h3v3H0z'/%3E%3C/g%3E%3C/svg%3E");
}

/* Error indicators with pattern */
.status-error,
.badge-error,
.progress-bar.error {
  background-image: url("data:image/svg+xml,%3Csvg width='8' height='8' viewBox='0 0 8 8' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='%23ffffff' fill-opacity='0.1' fill-rule='evenodd'%3E%3Cpath d='M0 0h4v4H0V0zm4 4h4v4H4V4z'/%3E%3C/g%3E%3C/svg%3E");
}

/* Locked/disabled indicators */
.status-locked,
.locked {
  background-image: url("data:image/svg+xml,%3Csvg width='4' height='4' viewBox='0 0 4 4' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 3h1v1H1V3zm2-2h1v1H3V1z' fill='%23ffffff' fill-opacity='0.15' fill-rule='evenodd'/%3E%3C/svg%3E");
}

/* ============================================
   ACCESSIBLE FORM HINTS AND ERRORS
   ============================================ */

/* Form field with hint text */
.form-group .form-hint {
  display: block;
  margin-top: 0.25rem;
  font-size: 0.8125rem;
  color: var(--text-muted, #64748B);
}

/* Form field with error */
.form-group.has-error .form-input,
.form-group.has-error .form-select,
.form-group.has-error .form-textarea {
  border-color: var(--color-error, #EF4444);
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.15);
}

.form-group .form-error {
  display: block;
  margin-top: 0.25rem;
  font-size: 0.8125rem;
  color: var(--color-error, #EF4444);
}

/* Required field indicator */
.form-label .required,
.form-label[data-required]::after {
  content: " *";
  color: var(--color-error, #EF4444);
}

/* ============================================
   ACCESSIBLE BUTTONS AND LINKS
   ============================================ */

/* Icon-only button accessibility */
.btn-icon[aria-label],
button[aria-label] {
  position: relative;
}

/* Tooltip for icon buttons on focus */
.btn-icon[aria-label]:focus-visible::after {
  content: attr(aria-label);
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  padding: 0.5rem 0.75rem;
  background: var(--bg-tertiary, #334155);
  color: var(--text-primary, #F8FAFC);
  font-size: 0.75rem;
  white-space: nowrap;
  border-radius: 0.375rem;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  z-index: 100;
}

/* Ensure link text is descriptive */
a[href]:empty::before {
  content: "[Empty link]";
  color: var(--color-error);
}

/* ============================================
   ACCESSIBLE MODALS AND DIALOGS
   ============================================ */

/* Modal trap focus styling */
[role="dialog"],
[role="alertdialog"] {
  /* Focus trap container */
}

[role="dialog"]:focus,
[role="alertdialog"]:focus {
  outline: none;
}

/* First and last focusable element indicators */
.modal [data-focus-guard] {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

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

table[role="grid"] th,
table[role="grid"] td {
  position: relative;
}

table[role="grid"] th:focus-within,
table[role="grid"] td:focus-within {
  outline: 2px solid var(--brand-primary, #F26522);
  outline-offset: -2px;
}

/* ============================================
   ACCESSIBLE COLLAPSIBLE ELEMENTS
   ============================================ */

[aria-expanded="false"] + .collapsible-content {
  display: none;
}

[aria-expanded="true"] + .collapsible-content {
  display: block;
}

/* Expand/collapse indicator */
[aria-expanded]::after {
  content: "";
  display: inline-block;
  width: 0.5rem;
  height: 0.5rem;
  margin-left: 0.5rem;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(45deg);
  transition: transform 0.2s ease;
}

[aria-expanded="true"]::after {
  transform: rotate(-135deg);
}

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

@media print {
  /* Show URLs for links */
  a[href^="http"]::after {
    content: " (" attr(href) ")";
    font-size: 0.75em;
    color: #666;
  }

  /* Hide interactive elements */
  .skip-links,
  .keyboard-help-trigger,
  button:not(.print-btn),
  .btn:not(.print-btn) {
    display: none !important;
  }

  /* Ensure sufficient contrast */
  * {
    color: #000 !important;
    background: #fff !important;
  }

  /* Show hidden content that's relevant */
  .sr-only.print-visible {
    position: static !important;
    width: auto !important;
    height: auto !important;
    padding: inherit !important;
    margin: inherit !important;
    overflow: visible !important;
    clip: auto !important;
    white-space: normal !important;
  }
}

/* ============================================
   TOUCH ACCESSIBILITY
   ============================================ */

@media (pointer: coarse) {
  /* Larger touch targets */
  button,
  a,
  input,
  select,
  [role="button"],
  .nav-item,
  .dropdown-item {
    min-height: 44px;
    min-width: 44px;
  }

  /* More spacing between touch targets */
  .nav-item,
  .dropdown-item,
  .btn {
    margin-bottom: 0.25rem;
  }
}
