/**
 * Accessibility and User Experience Enhancements for UfuqLMS
 * WCAG 2.1 AAA compliant styles and UX improvements
 */

/* =================================================================
   1. FOCUS MANAGEMENT
   ================================================================= */

/* Ensure all interactive elements have proper focus styles */
a,
button,
input,
select,
textarea,
[tabindex]:not([tabindex="-1"]),
[contenteditable="true"] {
  outline-offset: 2px;
}

/* Enhanced focus styles */
.focus-ring:focus,
a:focus,
button:focus,
input:focus,
select:focus,
textarea:focus,
[tabindex]:not([tabindex="-1"]):focus,
[contenteditable="true"]:focus {
  outline: 2px solid var(--primary-500);
  outline-offset: 2px;
}

/* Focus visible for better keyboard navigation */
.focus-visible:focus-visible,
button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--primary-500);
  outline-offset: 2px;
  box-shadow: 0 0 0 4px rgba(14, 165, 233, 0.15);
}

/* Remove outline for mouse users */
.focus-visible:focus:not(:focus-visible),
button:focus:not(:focus-visible),
a:focus:not(:focus-visible),
input:focus:not(:focus-visible),
select:focus:not(:focus-visible),
textarea:focus:not(:focus-visible) {
  outline: none;
  box-shadow: none;
}

/* Skip links for keyboard navigation */
.skip-link {
  position: absolute;
  left: -10000px;
  top: auto;
  width: 1px;
  height: 1px;
  overflow: hidden;
  background-color: var(--primary-600);
  color: var(--neutral-0);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-lg);
  text-decoration: none;
  font-weight: var(--font-medium);
  z-index: 9999;
}

.skip-link:focus {
  position: fixed;
  left: var(--space-4);
  top: var(--space-4);
  width: auto;
  height: auto;
  overflow: visible;
}

/* Focus trap for modals */
.modal-open {
  overflow: hidden;
}

.modal-focus-trap {
  position: fixed;
  inset: 0;
  z-index: var(--z-50);
}

/* =================================================================
   2. SCREEN READER SUPPORT
   ================================================================= */

/* Screen reader only text */
.sr-only {
  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;
}

/* Temporary show for debugging */
.sr-only.debug {
  position: static !important;
  width: auto !important;
  height: auto !important;
  margin: 0 !important;
  overflow: visible !important;
  clip: auto !important;
  white-space: normal !important;
  background: yellow;
  color: black;
  padding: var(--space-2);
  border: 1px solid black;
}

/* ARIA live regions */
.sr-live {
  position: absolute;
  left: -10000px;
  top: auto;
  width: 1px;
  height: 1px;
  overflow: hidden;
}

/* Announce important changes */
.announcement {
  position: absolute !important;
  left: -10000px !important;
  width: 1px !important;
  height: 1px !important;
  overflow: hidden !important;
}

/* =================================================================
   3. HIGH CONTRAST MODE SUPPORT
   ================================================================= */

@media (prefers-contrast: high) {
  /* Enhance borders and outlines in high contrast mode */
  .btn,
  .card,
  .form-input,
  .form-select,
  .form-textarea,
  .nav-link,
  .badge {
    border-width: 2px !important;
    border-color: currentColor !important;
  }

  /* Ensure text has sufficient contrast */
  .text-muted,
  .text-secondary {
    color: var(--neutral-900) !important;
  }

  /* Remove subtle shadows and backgrounds */
  .card,
  .modal-content,
  .nav {
    box-shadow: none !important;
    background: var(--neutral-0) !important;
  }

  /* Make focus states more prominent */
  *:focus {
    outline: 3px solid currentColor !important;
    outline-offset: 2px !important;
  }

  /* Ensure interactive elements are clearly distinguished */
  a {
    text-decoration: underline !important;
    text-underline-offset: 2px !important;
  }

  button {
    border: 2px solid currentColor !important;
  }
}

/* =================================================================
   4. REDUCED MOTION SUPPORT
   ================================================================= */

@media (prefers-reduced-motion: reduce) {
  /* Remove all animations and transitions */
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }

  /* Remove transform effects */
  .card:hover,
  .feature-card:hover,
  .btn:hover {
    transform: none !important;
  }

  /* Keep focus transitions for accessibility */
  *:focus {
    transition: outline 0.1s ease !important;
  }

  /* Disable parallax and auto-playing content */
  .parallax {
    transform: none !important;
  }

  video {
    animation: none !important;
  }

  /* Disable smooth scrolling */
  html {
    scroll-behavior: auto !important;
  }
}

/* =================================================================
   5. COLOR ACCESSIBILITY
   ================================================================= */

/* Ensure sufficient color contrast */
.text-low-contrast {
  color: var(--neutral-800);
}

.bg-low-contrast {
  background-color: var(--neutral-100);
  color: var(--neutral-900);
}

/* Color blind friendly indicators */
.status-indicator {
  position: relative;
  padding-left: var(--space-6);
}

.status-indicator::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: var(--space-4);
  height: var(--space-4);
  border-radius: 50%;
}

.status-indicator.success::before {
  background-color: var(--success-500);
}

.status-indicator.warning::before {
  background-color: var(--warning-500);
}

.status-indicator.error::before {
  background-color: var(--error-500);
}

/* Use shapes in addition to colors */
.success-indicator::after {
  content: "✓";
  color: var(--success-600);
  font-weight: bold;
  margin-left: var(--space-1);
}

.warning-indicator::after {
  content: "⚠";
  color: var(--warning-600);
  margin-left: var(--space-1);
}

.error-indicator::after {
  content: "✗";
  color: var(--error-600);
  font-weight: bold;
  margin-left: var(--space-1);
}

/* =================================================================
   6. FONT AND TYPOGRAPHY ACCESSIBILITY
   ================================================================= */

/* Ensure minimum font sizes for accessibility */
.text-accessible-sm {
  font-size: max(14px, 0.875rem);
}

.text-accessible-base {
  font-size: max(16px, 1rem);
}

/* Improve line height for better readability */
.text-readable {
  line-height: 1.6;
  max-width: 70ch;
}

/* Better spacing for Arabic text */
[lang="ar"],
.lang-ar {
  letter-spacing: 0.02em;
  word-spacing: 0.1em;
}

/* Improve readability for body text */
p,
li,
.body-text {
  font-size: max(16px, 1rem);
  line-height: 1.6;
  margin-bottom: var(--space-4);
}

/* Heading hierarchy for screen readers */
.heading-1 { font-size: clamp(1.875rem, 4vw, 3rem); }
.heading-2 { font-size: clamp(1.5rem, 3vw, 2.25rem); }
.heading-3 { font-size: clamp(1.25rem, 2.5vw, 1.875rem); }
.heading-4 { font-size: clamp(1.125rem, 2vw, 1.5rem); }
.heading-5 { font-size: clamp(1rem, 1.5vw, 1.25rem); }
.heading-6 { font-size: max(1rem, 1.125rem); }

/* =================================================================
   7. KEYBOARD NAVIGATION
   ================================================================= */

/* Improve keyboard navigation */
.keyboard-nav a,
.keyboard-nav button {
  position: relative;
}

/* Show keyboard navigation hints */
.keyboard-hint {
  position: absolute;
  top: -40px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--neutral-900);
  color: var(--neutral-0);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-lg);
  font-size: var(--text-sm);
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 1000;
}

.keyboard-nav *:focus .keyboard-hint {
  opacity: 1;
}

/* Tab order indicators (for debugging) */
.debug-tab-order [tabindex]::after {
  content: attr(tabindex);
  position: absolute;
  top: -10px;
  right: -10px;
  background: red;
  color: white;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: bold;
}

/* =================================================================
   8. FORM ACCESSIBILITY
   ================================================================= */

/* Required field indicators */
.required-field::after {
  content: " *";
  color: var(--error-500);
  font-weight: bold;
}

.required-field .sr-only-required::after {
  content: " (required)";
}

/* Form validation feedback */
.form-field-valid {
  border-color: var(--success-500) !important;
}

.form-field-valid:focus {
  box-shadow: 0 0 0 2px var(--success-500) !important;
}

.form-field-invalid {
  border-color: var(--error-500) !important;
}

.form-field-invalid:focus {
  box-shadow: 0 0 0 2px var(--error-500) !important;
}

/* Error messages */
.error-message {
  color: var(--error-600);
  font-size: var(--text-sm);
  margin-top: var(--space-1);
  display: flex;
  align-items: flex-start;
  gap: var(--space-2);
}

.error-message::before {
  content: "⚠";
  flex-shrink: 0;
  margin-top: 2px;
}

.success-message {
  color: var(--success-600);
  font-size: var(--text-sm);
  margin-top: var(--space-1);
  display: flex;
  align-items: flex-start;
  gap: var(--space-2);
}

.success-message::before {
  content: "✓";
  flex-shrink: 0;
  margin-top: 2px;
}

/* Form instructions */
.form-instructions {
  background: var(--info-50);
  border: 1px solid var(--info-200);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  margin-bottom: var(--space-6);
  color: var(--info-800);
}

.form-instructions::before {
  content: "ℹ";
  display: inline-block;
  margin-right: var(--space-2);
  color: var(--info-600);
  font-weight: bold;
}

/* =================================================================
   9. LOADING STATES AND FEEDBACK
   ================================================================= */

/* Loading states with proper ARIA attributes */
.loading {
  position: relative;
  pointer-events: none;
  opacity: 0.7;
}

.loading::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: inherit;
}

.loading-spinner-accessible {
  display: inline-block;
  width: 2rem;
  height: 2rem;
  border: 3px solid var(--neutral-300);
  border-radius: 50%;
  border-top-color: var(--primary-600);
  animation: spin 1s ease-in-out infinite;
}

/* Announce loading state to screen readers */
.loading-spinner-accessible::before {
  content: "";
  position: absolute;
  left: -10000px;
}

.loading[aria-busy="true"]::before {
  content: "Loading...";
  position: absolute;
  left: -10000px;
}

/* Progress indicators */
.progress-accessible {
  background: var(--neutral-200);
  border-radius: var(--radius-full);
  height: 8px;
  overflow: hidden;
  position: relative;
}

.progress-bar-accessible {
  background: linear-gradient(90deg, var(--primary-500), var(--primary-600));
  height: 100%;
  border-radius: inherit;
  transition: width 0.3s ease;
  position: relative;
}

/* Announce progress to screen readers */
.progress-accessible[aria-valuenow]::after {
  content: attr(aria-valuenow) "% complete";
  position: absolute;
  left: -10000px;
}

/* =================================================================
   10. NOTIFICATION AND ALERT ACCESSIBILITY
   ================================================================= */

/* Accessible alerts */
.alert-accessible {
  position: relative;
  padding: var(--space-4);
  padding-left: var(--space-12);
  border-radius: var(--radius-lg);
  border: 2px solid;
  margin-bottom: var(--space-4);
}

.alert-accessible::before {
  position: absolute;
  left: var(--space-4);
  top: var(--space-4);
  width: var(--space-6);
  height: var(--space-6);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  font-weight: bold;
  font-size: var(--text-lg);
}

.alert-success-accessible {
  background: var(--success-50);
  border-color: var(--success-500);
  color: var(--success-800);
}

.alert-success-accessible::before {
  content: "✓";
  background: var(--success-500);
  color: white;
}

.alert-warning-accessible {
  background: var(--warning-50);
  border-color: var(--warning-500);
  color: var(--warning-800);
}

.alert-warning-accessible::before {
  content: "⚠";
  background: var(--warning-500);
  color: white;
}

.alert-error-accessible {
  background: var(--error-50);
  border-color: var(--error-500);
  color: var(--error-800);
}

.alert-error-accessible::before {
  content: "✗";
  background: var(--error-500);
  color: white;
}

.alert-info-accessible {
  background: var(--info-50);
  border-color: var(--info-500);
  color: var(--info-800);
}

.alert-info-accessible::before {
  content: "ℹ";
  background: var(--info-500);
  color: white;
}

/* Toast notifications */
.toast-container {
  position: fixed;
  top: var(--space-4);
  right: var(--space-4);
  z-index: 1000;
  max-width: 400px;
  width: 100%;
}

@media (max-width: 640px) {
  .toast-container {
    left: var(--space-4);
    right: var(--space-4);
    max-width: none;
  }
}

.toast {
  background: var(--neutral-0);
  border: 2px solid var(--neutral-200);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: var(--space-4);
  margin-bottom: var(--space-2);
  position: relative;
  animation: slideInRight 0.3s ease-out;
}

@keyframes slideInRight {
  from {
    transform: translateX(100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

/* =================================================================
   11. MOBILE ACCESSIBILITY
   ================================================================= */

/* Touch target improvements */
@media (hover: none) and (pointer: coarse) {
  /* Ensure minimum touch target size */
  button,
  a,
  input,
  select,
  textarea,
  [role="button"],
  [role="link"] {
    min-height: 44px;
    min-width: 44px;
  }

  /* Improve form controls for touch */
  input,
  select,
  textarea {
    font-size: 16px; /* Prevent zoom on iOS */
    padding: var(--space-3);
  }

  /* Better spacing for mobile forms */
  .form-group {
    margin-bottom: var(--space-6);
  }

  /* Improve modal interactions */
  .modal-close {
    min-width: 48px;
    min-height: 48px;
  }
}

/* =================================================================
   12. PRINT ACCESSIBILITY
   ================================================================= */

@media print {
  /* Ensure proper contrast for printing */
  * {
    background: transparent !important;
    color: black !important;
    text-shadow: none !important;
    box-shadow: none !important;
  }

  /* Show URLs for links */
  a[href]::after {
    content: " (" attr(href) ")";
    font-size: smaller;
    color: #666;
  }

  a[href^="javascript:"]::after,
  a[href^="#"]::after {
    content: "";
  }

  /* Hide interactive elements */
  .no-print,
  button,
  input,
  select,
  textarea,
  .btn {
    display: none !important;
  }

  /* Improve readability */
  .card,
  .alert {
    border: 1px solid black !important;
    margin-bottom: var(--space-4) !important;
  }

  /* Page breaks */
  .page-break-before {
    page-break-before: always;
  }

  .page-break-after {
    page-break-after: always;
  }

  .page-break-inside-avoid {
    page-break-inside: avoid;
  }
}

/* =================================================================
   13. LANGUAGE AND INTERNATIONALIZATION
   ================================================================= */

/* Improve Arabic text rendering */
[lang="ar"],
.lang-ar {
  text-align: right;
  direction: rtl;
  font-feature-settings: "liga", "calt", "kern";
}

/* English text in RTL context */
[lang="ar"] .lang-en,
.lang-ar .lang-en {
  direction: ltr;
  text-align: left;
  display: inline-block;
}

/* Numbers in Arabic context */
[lang="ar"] .numbers,
.lang-ar .numbers {
  direction: ltr;
  display: inline-block;
}

/* Proper quotation marks for different languages */
[lang="ar"] q::before { content: '"'; }
[lang="ar"] q::after { content: '"'; }
[lang="en"] q::before { content: '"'; }
[lang="en"] q::after { content: '"'; }

/* =================================================================
   14. SEMANTIC MARKUP SUPPORT
   ================================================================= */

/* Style semantic elements properly */
main {
  display: block;
  min-height: 60vh;
}

section {
  margin-bottom: var(--space-8);
}

article {
  margin-bottom: var(--space-6);
}

aside {
  background: var(--neutral-50);
  padding: var(--space-6);
  border-radius: var(--radius-lg);
  border: 1px solid var(--neutral-200);
}

figure {
  margin: var(--space-6) 0;
}

figcaption {
  font-size: var(--text-sm);
  color: var(--neutral-600);
  margin-top: var(--space-2);
  text-align: center;
  font-style: italic;
}

/* Definition lists */
dl {
  margin-bottom: var(--space-6);
}

dt {
  font-weight: var(--font-semibold);
  margin-top: var(--space-4);
  margin-bottom: var(--space-2);
}

dd {
  margin-left: var(--space-6);
  margin-bottom: var(--space-2);
}

[dir="rtl"] dd {
  margin-left: 0;
  margin-right: var(--space-6);
}

/* Code elements */
code {
  font-family: var(--font-family-mono);
  font-size: 0.9em;
  background: var(--neutral-100);
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
  border: 1px solid var(--neutral-200);
}

pre {
  font-family: var(--font-family-mono);
  background: var(--neutral-100);
  padding: var(--space-4);
  border-radius: var(--radius-lg);
  border: 1px solid var(--neutral-200);
  overflow-x: auto;
  margin: var(--space-4) 0;
}

pre code {
  background: none;
  border: none;
  padding: 0;
}

/* Table accessibility */
table {
  border-collapse: collapse;
  width: 100%;
  margin-bottom: var(--space-6);
}

th,
td {
  padding: var(--space-3);
  text-align: left;
  border: 1px solid var(--neutral-200);
  vertical-align: top;
}

[dir="rtl"] th,
[dir="rtl"] td {
  text-align: right;
}

th {
  background: var(--neutral-100);
  font-weight: var(--font-semibold);
}

caption {
  font-weight: var(--font-semibold);
  margin-bottom: var(--space-4);
  caption-side: top;
}

/* =================================================================
   15. DEBUGGING AND DEVELOPMENT HELPERS
   ================================================================= */

/* Debug mode styles (remove in production) */
.debug-accessibility [aria-hidden="true"] {
  outline: 2px dashed red !important;
}

.debug-accessibility [role] {
  outline: 1px solid blue !important;
}

.debug-accessibility .sr-only {
  position: static !important;
  width: auto !important;
  height: auto !important;
  margin: 0 !important;
  overflow: visible !important;
  clip: auto !important;
  white-space: normal !important;
  background: yellow;
  color: black;
  padding: var(--space-2);
  border: 1px solid black;
}

/* Show focus order for debugging */
.debug-focus [tabindex="0"]::before { content: "0"; }
.debug-focus [tabindex="1"]::before { content: "1"; }
.debug-focus [tabindex="2"]::before { content: "2"; }
.debug-focus [tabindex="3"]::before { content: "3"; }
.debug-focus [tabindex="4"]::before { content: "4"; }
.debug-focus [tabindex="5"]::before { content: "5"; }

.debug-focus [tabindex]::before {
  position: absolute;
  top: -10px;
  right: -10px;
  background: red;
  color: white;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: bold;
  z-index: 1000;
}