/**
 * CECCM Accessibility Styles
 *
 * Comprehensive accessibility improvements including:
 * - Improved color contrast ratios (WCAG 2.1 AA compliant)
 * - Focus states for keyboard navigation
 * - Screen reader utilities
 * - Reduced motion support
 * - High contrast mode support
 *
 * @version 1.0.0
 * @date 2026-03-13
 */

/* ============================================
   COLOR CONTRAST IMPROVEMENTS
   WCAG 2.1 AA requires 4.5:1 for normal text, 3:1 for large text
   ============================================ */

/* Primary text colors - improved contrast */
:root {
  /* Text colors with improved contrast */
  --color-text-primary-accessible: #f8fafc; /* 15.3:1 on dark bg */
  --color-text-secondary-accessible: #cbd5e1; /* 9.2:1 on dark bg */
  --color-text-muted-accessible: #94a3b8; /* 6.1:1 on dark bg */

  /* Light theme text colors */
  --color-text-light-accessible: #1e293b; /* 14.8:1 on white */
  --color-text-light-secondary-accessible: #475569; /* 7.1:1 on white */
  --color-text-light-muted-accessible: #64748b; /* 4.7:1 on white */

  /* Accent colors with better contrast */
  --color-accent-cyan-accessible: #22d3ee; /* 8.2:1 on dark bg */
  --color-accent-violet-accessible: #a78bfa; /* 7.1:1 on dark bg */

  /* Error/Success colors with better contrast */
  --color-error-accessible: #fca5a5; /* 8.5:1 on dark bg */
  --color-success-accessible: #86efac; /* 9.1:1 on dark bg */
  --color-warning-accessible: #fcd34d; /* 10.2:1 on dark bg */

  /* Focus ring colors */
  --focus-ring-color: #22d3ee;
  --focus-ring-color-light: #0891b2;
  --focus-ring-width: 3px;
  --focus-ring-offset: 2px;
}

/* ============================================
   FOCUS STATES - KEYBOARD NAVIGATION
   ============================================ */

/* Remove default focus outline for mouse users */
:focus:not(:focus-visible) {
  outline: none;
}

/* Global focus-visible styles */
:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: var(--focus-ring-offset);
}

/* Button focus */
.btn:focus-visible,
button:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: var(--focus-ring-offset);
  box-shadow: 0 0 0 6px rgba(34, 211, 238, 0.25);
}

/* Link focus */
a:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: var(--focus-ring-offset);
  text-decoration: underline;
  text-decoration-thickness: 2px;
}

/* Form input focus */
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: 0;
  border-color: var(--focus-ring-color);
  box-shadow: 0 0 0 6px rgba(34, 211, 238, 0.15);
}

/* Form input error focus */
.form-input.error:focus-visible,
input.error:focus-visible {
  outline-color: var(--color-error-accessible);
  border-color: var(--color-error-accessible);
  box-shadow: 0 0 0 6px rgba(252, 165, 165, 0.15);
}

/* Form input success focus */
.form-input.success:focus-visible,
input.success:focus-visible {
  outline-color: var(--color-success-accessible);
  border-color: var(--color-success-accessible);
  box-shadow: 0 0 0 6px rgba(134, 239, 172, 0.15);
}

/* Skip link for keyboard navigation */
.skip-link {
  position: absolute;
  top: -100%;
  left: 50%;
  transform: translateX(-50%);
  background: var(--color-accent-cyan-accessible);
  color: var(--color-bg-dark);
  padding: var(--space-3) var(--space-6);
  border-radius: var(--radius-lg);
  font-weight: var(--font-weight-semibold);
  text-decoration: none;
  z-index: var(--z-max);
  transition: top 0.2s ease;
}

.skip-link:focus {
  top: var(--space-4);
  outline: var(--focus-ring-width) solid var(--color-bg-dark);
  outline-offset: var(--focus-ring-offset);
}

/* ============================================
   SCREEN READER UTILITIES
   ============================================ */

/* Visually hidden but accessible to screen readers */
.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;
}

/* Show element when focused */
.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;
}

/* Hide from screen readers but visible visually */
[aria-hidden="true"] {
  /* Already hidden from screen readers via attribute */
}

/* ============================================
   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 parallax effects */
  .parallax,
  [data-parallax] {
    transform: none !important;
  }

  /* Disable auto-playing videos */
  video[autoplay] {
    autoplay: false;
  }

  /* Disable skeleton loading animations */
  .skeleton {
    animation: none !important;
    background: var(--color-bg-tertiary) !important;
  }

  /* Disable floating animations */
  .auth-orb,
  .page-orb {
    animation: none !important;
  }

  /* Keep focus transitions for accessibility */
  :focus-visible {
    transition: outline 0.1s ease !important;
  }
}

/* ============================================
   HIGH CONTRAST MODE SUPPORT
   ============================================ */

@media (prefers-contrast: high) {
  :root {
    --color-text-primary: #ffffff;
    --color-text-secondary: #e2e8f0;
    --color-text-muted: #cbd5e1;
    --color-border: rgba(255, 255, 255, 0.5);
    --color-border-hover: rgba(255, 255, 255, 0.7);
  }

  /* Increase border visibility */
  .btn,
  .card,
  .form-input,
  input,
  textarea,
  select {
    border-width: 2px;
  }

  /* Ensure buttons have visible borders */
  .btn-primary,
  .btn-accent {
    border: 2px solid currentColor;
  }

  /* Increase focus ring visibility */
  :focus-visible {
    outline-width: 4px;
  }

  /* Make error states more visible */
  .error-message,
  .form-error-message {
    border: 2px solid var(--color-error-accessible);
    padding: var(--space-3);
  }
}

/* Forced colors mode (Windows High Contrast) */
@media (forced-colors: active) {
  /* Ensure buttons are visible */
  .btn {
    border: 2px solid currentColor;
  }

  /* Ensure links are visible */
  a {
    text-decoration: underline;
  }

  /* Ensure focus is visible */
  :focus-visible {
    outline: 3px solid Highlight;
  }

  /* Ensure icons inherit color */
  .btn i,
  .nav-link i {
    forced-color-adjust: none;
  }

  /* Disable background images that may interfere */
  .auth-background,
  .page-hero-bg {
    background: none;
  }
}

/* ============================================
   FORM ACCESSIBILITY
   ============================================ */

/* Form labels - ensure they're properly associated */
.form-group label {
  display: block;
  margin-bottom: var(--space-2);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-primary-accessible);
}

/* Required field indicator */
.form-group label.required::after,
.form-group label[for][data-required]::after {
  content: " *";
  color: var(--color-error-accessible);
  font-weight: var(--font-weight-bold);
}

/* Form error messages */
.form-error-message {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-top: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: rgba(252, 165, 165, 0.1);
  border: 1px solid rgba(252, 165, 165, 0.3);
  border-radius: var(--radius-md);
  color: var(--color-error-accessible);
  font-size: var(--text-sm);
}

.form-error-message i {
  flex-shrink: 0;
}

/* Form helper text */
.form-helper-text {
  margin-top: var(--space-2);
  font-size: var(--text-sm);
  color: var(--color-text-muted-accessible);
}

/* Invalid input styling */
input[aria-invalid="true"],
textarea[aria-invalid="true"],
select[aria-invalid="true"] {
  border-color: var(--color-error-accessible) !important;
  box-shadow: 0 0 0 3px rgba(252, 165, 165, 0.15);
}

/* Valid input styling */
input[aria-invalid="false"]:not(:placeholder-shown),
textarea[aria-invalid="false"]:not(:placeholder-shown) {
  border-color: var(--color-success-accessible);
}

/* ============================================
   BUTTON ACCESSIBILITY
   ============================================ */

/* Ensure buttons have sufficient size */
.btn {
  min-height: 44px; /* WCAG touch target size */
  min-width: 44px;
}

.btn-sm {
  min-height: 36px;
  min-width: 36px;
}

/* Button loading state */
.btn[aria-busy="true"] {
  position: relative;
  pointer-events: none;
}

.btn[aria-busy="true"]::after {
  content: "";
  position: absolute;
  width: 16px;
  height: 16px;
  border: 2px solid transparent;
  border-top-color: currentColor;
  border-radius: 50%;
  animation: btn-loading 0.8s linear infinite;
}

@keyframes btn-loading {
  to {
    transform: rotate(360deg);
  }
}

/* Disabled button styling */
.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.6;
  cursor: not-allowed;
  pointer-events: none;
}

/* ============================================
   NAVIGATION ACCESSIBILITY
   ============================================ */

/* Navigation landmark */
.nav {
  role: "navigation";
}

/* Navigation list */
.nav-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

/* Navigation links */
.nav-link {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  color: var(--color-text-primary-accessible);
  text-decoration: none;
  border-radius: var(--radius-md);
  transition:
    background 0.2s ease,
    color 0.2s ease;
}

.nav-link:hover {
  background: rgba(255, 255, 255, 0.1);
}

.nav-link:focus-visible {
  background: rgba(255, 255, 255, 0.15);
}

/* Current page indicator */
.nav-link[aria-current="page"] {
  background: rgba(34, 211, 238, 0.15);
  color: var(--color-accent-cyan-accessible);
  font-weight: var(--font-weight-medium);
}

/* ============================================
   MODAL ACCESSIBILITY
   ============================================ */

/* Modal backdrop */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  z-index: var(--z-modal-backdrop);
}

/* Modal container */
.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-width: 90vw;
  max-height: 90vh;
  overflow: auto;
  background: var(--color-bg-elevated);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-2xl);
  z-index: var(--z-modal);
}

/* Modal focus trap */
.modal:focus {
  outline: none;
}

/* Modal title */
.modal-title {
  font-size: var(--text-xl);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary-accessible);
}

/* Close button */
.modal-close {
  position: absolute;
  top: var(--space-4);
  right: var(--space-4);
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: var(--radius-md);
  color: var(--color-text-secondary-accessible);
  cursor: pointer;
  transition:
    background 0.2s ease,
    color 0.2s ease;
}

.modal-close:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--color-text-primary-accessible);
}

.modal-close:focus-visible {
  background: rgba(255, 255, 255, 0.15);
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: 0;
}

/* ============================================
   TOAST NOTIFICATION ACCESSIBILITY
   ============================================ */

.toast {
  position: fixed;
  bottom: var(--space-6);
  right: var(--space-6);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  background: var(--color-bg-elevated);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  z-index: var(--z-toast);
  animation: toast-slide-in 0.3s ease;
}

@keyframes toast-slide-in {
  from {
    transform: translateX(100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

.toast[role="alert"] {
  /* Ensure alert role is properly styled */
  border-left: 4px solid var(--color-accent-cyan-accessible);
}

.toast.toast-error[role="alert"] {
  border-left-color: var(--color-error-accessible);
}

.toast.toast-success[role="alert"] {
  border-left-color: var(--color-success-accessible);
}

.toast-close {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: var(--radius-md);
  color: var(--color-text-secondary-accessible);
  cursor: pointer;
}

.toast-close:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: 0;
}

/* ============================================
   TABLE ACCESSIBILITY
   ============================================ */

.table {
  width: 100%;
  border-collapse: collapse;
}

/* Table header cells */
.table th {
  text-align: left;
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary-accessible);
  background: var(--color-bg-secondary);
  border-bottom: 2px solid var(--color-border);
}

/* Table data cells */
.table td {
  color: var(--color-text-primary-accessible);
  border-bottom: 1px solid var(--color-border);
}

/* Sortable column headers */
.table th[aria-sort] {
  cursor: pointer;
  user-select: none;
}

.table th[aria-sort]::after {
  content: " ⇅";
  opacity: 0.5;
}

.table th[aria-sort="ascending"]::after {
  content: " ↑";
  opacity: 1;
}

.table th[aria-sort="descending"]::after {
  content: " ↓";
  opacity: 1;
}

/* ============================================
   IMAGE ACCESSIBILITY
   ============================================ */

/* Ensure images have alt text */
img:not([alt]) {
  outline: 3px solid var(--color-error-accessible);
}

/* Decorative images */
img[alt=""],
img[role="presentation"] {
  /* These are decorative and should be ignored by screen readers */
}

/* Lazy loaded images */
img[loading="lazy"] {
  /* Ensure proper loading behavior */
}

/* ============================================
   LOADING STATES
   ============================================ */

/* Loading spinner */
.loading-spinner {
  display: inline-block;
  width: 24px;
  height: 24px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-accent-cyan-accessible);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Loading placeholder */
.loading-placeholder {
  background: linear-gradient(
    90deg,
    var(--color-bg-tertiary) 25%,
    var(--color-bg-secondary) 50%,
    var(--color-bg-tertiary) 75%
  );
  background-size: 200% 100%;
  animation: loading-shimmer 1.5s ease-in-out infinite;
  border-radius: var(--radius-md);
}

@keyframes loading-shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

/* ============================================
   ERROR BOUNDARY ACCESSIBILITY
   ============================================ */

.error-boundary {
  padding: var(--space-8);
  text-align: center;
  background: var(--color-bg-secondary);
  border-radius: var(--radius-xl);
}

.error-boundary-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto var(--space-6);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-full);
  font-size: var(--text-3xl);
}

.error-boundary-icon.network {
  background: rgba(245, 158, 11, 0.1);
  color: var(--color-warning-accessible);
}

.error-boundary-icon.database {
  background: rgba(239, 68, 68, 0.1);
  color: var(--color-error-accessible);
}

.error-boundary-icon.error {
  background: rgba(239, 68, 68, 0.1);
  color: var(--color-error-accessible);
}

.error-boundary-title {
  font-size: var(--text-xl);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary-accessible);
  margin-bottom: var(--space-3);
}

.error-boundary-message {
  font-size: var(--text-base);
  color: var(--color-text-secondary-accessible);
  max-width: 400px;
  margin: 0 auto var(--space-6);
  line-height: var(--leading-relaxed);
}

.error-boundary-actions {
  display: flex;
  gap: var(--space-3);
  justify-content: center;
  flex-wrap: wrap;
}

/* ============================================
   PRINT ACCESSIBILITY
   ============================================ */

@media print {
  /* Ensure text is readable when printed */
  body {
    color: #000 !important;
    background: #fff !important;
  }

  /* Show link URLs */
  a[href]::after {
    content: " (" attr(href) ")";
    font-size: 0.8em;
    color: #666;
  }

  /* Don't show link URLs for anchor links */
  a[href^="#"]::after,
  a[href^="javascript"]::after {
    content: "";
  }

  /* Ensure images are printed */
  img {
    max-width: 100% !important;
  }

  /* Hide non-essential elements */
  .nav,
  .footer,
  .toast,
  .modal-backdrop,
  .skip-link {
    display: none !important;
  }
}

/* ============================================
   RESPONSIVE ACCESSIBILITY
   ============================================ */

/* Ensure touch targets are large enough on mobile */
@media (max-width: 768px) {
  .btn,
  button,
  a,
  input,
  select,
  textarea {
    min-height: 44px;
    min-width: 44px;
  }

  /* Increase spacing for touch */
  .nav-link {
    padding: var(--space-3) var(--space-4);
  }

  /* Ensure form inputs are large enough */
  input,
  textarea,
  select {
    font-size: 16px; /* Prevents zoom on iOS */
  }
}

/* ============================================
   DARK MODE ACCESSIBILITY
   ============================================ */

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
  }

  /* Ensure sufficient contrast in dark mode */
  .card,
  .modal {
    background: var(--color-bg-elevated);
    border: 1px solid var(--color-border);
  }
}

/* ============================================
   LIGHT MODE ACCESSIBILITY
   ============================================ */

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
  }

  /* Adjust colors for light mode */
  body {
    --color-text-primary-accessible: #1e293b;
    --color-text-secondary-accessible: #475569;
    --color-text-muted-accessible: #64748b;
    --focus-ring-color: var(--focus-ring-color-light);
  }
}
