/**
 * MobiDaS Tutorial - Accessibility Styles
 * WCAG 2.1 AA Compliant
 * Integrated with design-system.css variables
 */

/* ═══════════════════════════════════════════════════════════════════════════
   SKIP NAVIGATION LINK
   ═══════════════════════════════════════════════════════════════════════════ */

.skip-link {
  position: absolute;
  top: -100px;
  left: 0;
  z-index: var(--z-toast);
  padding: var(--space-3) var(--space-5);
  background-color: var(--color-text);
  color: var(--color-text-inverse);
  font-family: var(--font-body);
  font-weight: var(--font-semibold);
  text-decoration: none;
  border-radius: 0 0 var(--radius-md) 0;
  transition: top var(--transition-fast);
}

.skip-link:focus {
  top: 0;
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: var(--focus-ring-offset);
}

/* Multiple skip links for complex layouts */
.skip-links {
  position: absolute;
  top: 0;
  left: 0;
  z-index: var(--z-toast);
}

.skip-links a {
  position: absolute;
  top: -100px;
  left: 0;
  padding: var(--space-3) var(--space-5);
  background-color: var(--color-text);
  color: var(--color-text-inverse);
  font-weight: var(--font-semibold);
  text-decoration: none;
  white-space: nowrap;
  border-radius: var(--radius-md);
  transition: top var(--transition-fast);
}

.skip-links a:focus {
  top: var(--space-2);
  left: var(--space-2);
}

/* ═══════════════════════════════════════════════════════════════════════════
   FOCUS STYLES
   Using :focus-visible for keyboard-only focus indication (WCAG 2.4.7)
   ═══════════════════════════════════════════════════════════════════════════ */

/* Remove default outline on :focus, apply on :focus-visible */
*:focus {
  outline: none;
}

/* Keyboard focus ring using design system variables */
*:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: var(--focus-ring-offset);
}

/* Alternative: box-shadow focus for elements where outline doesn't work well */
.focus-shadow:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

/* Specific focus styles for interactive elements */
a:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: var(--focus-ring-offset);
  border-radius: var(--radius-sm);
}

button:focus-visible,
.btn:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: var(--focus-ring-offset);
}

/* Form elements - use border + shadow instead of outline */
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-100);
}

/* Tab buttons focus - inset for visual consistency */
.tab-button:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: -2px;
}

/* Accordion header focus - inset to stay within bounds */
.accordion-header:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: -2px;
}

/* Cards and interactive containers */
.card-interactive:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: var(--focus-ring-offset);
}

/* Checkbox and radio focus */
input[type="checkbox"]:focus-visible,
input[type="radio"]:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: var(--focus-ring-offset);
}

/* Rating scale focus */
.rubric-rating-btn:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: var(--focus-ring-offset);
}

/* Module list link focus */
.module-list a:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: var(--focus-ring-offset);
}

/* Quiz option focus */
.quiz-option:focus-within {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: var(--focus-ring-offset);
}

/* ═══════════════════════════════════════════════════════════════════════════
   MINIMUM TARGET SIZE (WCAG 2.5.5 - Level AAA, 2.5.8 - Level AA)
   44x44px minimum for Level AA, recommended 48x48 for better usability
   ═══════════════════════════════════════════════════════════════════════════ */

/* Base interactive elements - 44px minimum */
button,
.btn,
[role="button"],
.tab-button,
.accordion-header {
  min-height: 44px;
}

/* Ensure adequate touch targets without distorting layout */
.touch-target {
  position: relative;
}

.touch-target::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: max(100%, 44px);
  height: max(100%, 44px);
}

/* Checkbox with adequate hit area */
input[type="checkbox"],
input[type="radio"] {
  width: 20px;
  height: 20px;
  cursor: pointer;
}

/* Self-check items - entire row is clickable */
.self-check-item {
  min-height: 44px;
  cursor: pointer;
}

/* Quiz options - ensure clickable area */
.quiz-option {
  min-height: 44px;
  cursor: pointer;
}

/* Small buttons - maintain minimum with proper padding */
.btn-sm {
  min-height: 36px;
  padding: var(--space-2) var(--space-4);
}

/* Icon-only buttons need explicit sizing */
.btn-icon {
  width: 44px;
  height: 44px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ═══════════════════════════════════════════════════════════════════════════
   REDUCED MOTION PREFERENCE (WCAG 2.3.3)
   ═══════════════════════════════════════════════════════════════════════════ */

@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;
  }

  /* Progress bar fills instantly */
  .progress-fill {
    transition: none !important;
  }

  /* Accordion opens instantly */
  .accordion-content {
    transition: none !important;
  }

  /* Sidebar slides instantly */
  .sidebar {
    transition: none !important;
  }

  /* Tooltips appear instantly */
  .glossary-tooltip {
    transition: none !important;
  }

  /* Toast notifications appear instantly */
  .toast {
    animation: none !important;
  }

  /* Chart animations */
  canvas {
    animation: none !important;
  }

  /* Modal transitions */
  .modal,
  .modal-backdrop {
    transition: none !important;
  }

  /* Freytag pyramid stage transitions */
  .freytag-stage {
    transition: none !important;
  }

  /* Decision gate transitions */
  .decision-gate {
    transition: none !important;
  }
}

/* ═══════════════════════════════════════════════════════════════════════════
   HIGH CONTRAST MODE (Windows High Contrast / Forced Colors)
   ═══════════════════════════════════════════════════════════════════════════ */

@media (forced-colors: active) {
  /* Use system colors for buttons */
  .btn,
  button {
    border: 2px solid ButtonText;
    forced-color-adjust: none;
  }

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

  .btn-primary:hover,
  .btn-primary:focus {
    background: Highlight;
    color: HighlightText;
  }

  /* Ensure visible borders for all containers */
  .card,
  .callout,
  .callout-info,
  .callout-tip,
  .callout-warning,
  .callout-danger,
  .worked-example,
  .comparison-side,
  .quiz-question,
  .decision-gate,
  .rubric,
  .self-check,
  .exercise-section,
  .accordion,
  .persona-card {
    border: 2px solid CanvasText;
  }

  /* Focus visible */
  *:focus-visible {
    outline: 3px solid Highlight;
    outline-offset: 2px;
  }

  /* Links */
  a {
    color: LinkText;
    text-decoration: underline;
  }

  a:visited {
    color: VisitedText;
  }

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

  .progress-fill {
    background: Highlight;
  }

  /* Form elements */
  input,
  textarea,
  select {
    border: 2px solid CanvasText;
  }

  input:focus,
  textarea:focus,
  select:focus {
    outline: 3px solid Highlight;
  }

  /* Checkboxes and radios */
  input[type="checkbox"],
  input[type="radio"] {
    border: 2px solid CanvasText;
  }

  input[type="checkbox"]:checked,
  input[type="radio"]:checked {
    background: Highlight;
    border-color: Highlight;
  }

  /* Accordion */
  .accordion-item {
    border-bottom: 1px solid CanvasText;
  }

  .accordion-header[aria-expanded="true"] {
    background: Highlight;
    color: HighlightText;
  }

  /* Tabs */
  .tab-button[aria-selected="true"] {
    background: Highlight;
    color: HighlightText;
    border-color: Highlight;
  }

  /* Tables */
  table {
    border: 1px solid CanvasText;
  }

  th,
  td {
    border: 1px solid CanvasText;
  }

  /* Badges */
  .badge,
  .module-badge {
    border: 1px solid CanvasText;
  }

  /* Decision gate states */
  .decision-gate--committed {
    border-color: Highlight;
  }
}

/* ═══════════════════════════════════════════════════════════════════════════
   PRINT STYLES
   ═══════════════════════════════════════════════════════════════════════════ */

@media print {
  /* Hide interactive and navigation elements */
  .navbar,
  .sidebar,
  .sidebar-backdrop,
  .mobile-menu-btn,
  .theme-toggle,
  .font-size-controls,
  .module-navigation,
  .decision-gate-actions,
  .export-progress-btn,
  .btn:not(.print-visible),
  button:not(.print-visible),
  .skip-link,
  .skip-links,
  .toast,
  .modal-backdrop {
    display: none !important;
  }

  /* Reset layout */
  .main-content {
    margin: 0 !important;
    padding: 0 !important;
    max-width: 100% !important;
  }

  body {
    background: white !important;
    color: black !important;
    font-size: 12pt;
    line-height: 1.5;
  }

  /* Typography reset */
  h1, h2, h3, h4, h5, h6 {
    color: black !important;
    page-break-after: avoid;
  }

  /* Show URLs after links */
  a[href^="http"]::after {
    content: " (" attr(href) ")";
    font-size: 0.8em;
    font-weight: normal;
    color: #666;
  }

  /* Internal links - don't show URL */
  a[href^="#"]::after {
    content: none;
  }

  /* Page breaks */
  .worked-example,
  .callout,
  .exercise-section,
  .self-check,
  .comparison-panel,
  .persona-card,
  .chart-container {
    page-break-inside: avoid;
  }

  section,
  article {
    page-break-before: auto;
  }

  /* Simplify styling */
  .card,
  .callout,
  .callout-info,
  .callout-tip,
  .callout-warning,
  .callout-danger,
  .worked-example,
  .self-check,
  .exercise-section,
  .comparison-side,
  .quiz-question,
  .decision-gate,
  .rubric {
    border: 1px solid #333 !important;
    background: white !important;
    box-shadow: none !important;
  }

  /* Ensure all text is readable */
  * {
    color: black !important;
    background: white !important;
    text-shadow: none !important;
  }

  /* Keep callout border colors for distinction */
  .callout-info {
    border-left: 4px solid #0284c7 !important;
  }

  .callout-tip {
    border-left: 4px solid #059669 !important;
  }

  .callout-warning {
    border-left: 4px solid #d97706 !important;
  }

  .callout-danger {
    border-left: 4px solid #dc2626 !important;
  }

  /* Show expandable content */
  .accordion-content {
    display: block !important;
    max-height: none !important;
    padding: 1rem !important;
  }

  .tab-content {
    display: block !important;
  }

  .tab-list {
    display: none !important;
  }

  /* Tables */
  table {
    border-collapse: collapse;
    width: 100%;
  }

  th,
  td {
    border: 1px solid #333;
    padding: 0.5rem;
  }

  /* Images */
  img {
    max-width: 100%;
    page-break-inside: avoid;
  }

  /* Charts - show placeholder text */
  .chart-container canvas {
    display: none;
  }

  .chart-container::after {
    content: "[Chart - See digital version]";
    display: block;
    text-align: center;
    font-style: italic;
    padding: 2rem;
    border: 1px dashed #999;
  }

  /* Hide chart-specific print message if canvas is visible */
  .chart-container:has(canvas[data-print="true"])::after {
    display: none;
  }
}

/* ═══════════════════════════════════════════════════════════════════════════
   LIVE REGION ANNOUNCER (WCAG 4.1.3)
   ═══════════════════════════════════════════════════════════════════════════ */

#a11y-announcer,
[role="status"],
.announcer {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Polite announcements */
.announcer-polite {
  aria-live: polite;
  aria-atomic: true;
}

/* Assertive announcements for errors */
.announcer-assertive {
  aria-live: assertive;
  aria-atomic: true;
}

/* ═══════════════════════════════════════════════════════════════════════════
   SCREEN READER ONLY TEXT
   ═══════════════════════════════════════════════════════════════════════════ */

.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 elements to be focusable when needed */
.sr-only-focusable:active,
.sr-only-focusable:focus {
  position: static !important;
  width: auto !important;
  height: auto !important;
  overflow: visible !important;
  clip: auto !important;
  white-space: normal !important;
  margin: 0 !important;
}

/* ═══════════════════════════════════════════════════════════════════════════
   COLOR CONTRAST HELPERS (WCAG 1.4.3, 1.4.6)
   4.5:1 for normal text, 3:1 for large text (18pt or 14pt bold)
   ═══════════════════════════════════════════════════════════════════════════ */

/* Ensure muted text still meets contrast requirements */
.text-muted {
  color: var(--color-text-muted);
  /* Verified: 4.5:1 against --color-bg */
}

/* Text on colored backgrounds */
.on-primary,
.on-success,
.on-error,
.on-warning,
.on-info {
  color: var(--color-text-inverse);
}

/* Ensure links are distinguishable from text (WCAG 1.4.1) */
a:not(.btn):not(.nav-link) {
  text-decoration: underline;
  text-underline-offset: 2px;
}

a:not(.btn):not(.nav-link):hover,
a:not(.btn):not(.nav-link):focus {
  text-decoration-thickness: 2px;
}

/* Navigation links - distinguishable by context/position */
.navbar-nav a,
.module-list a,
.nav-link {
  text-decoration: none;
}

.navbar-nav a:hover,
.module-list a:hover,
.nav-link:hover {
  text-decoration: underline;
}

/* ═══════════════════════════════════════════════════════════════════════════
   KEYBOARD NAVIGATION INDICATORS
   ═══════════════════════════════════════════════════════════════════════════ */

/* Show keyboard navigation mode indicator */
body.keyboard-nav *:focus {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: var(--focus-ring-offset);
}

/* Remove focus ring on mouse interaction when not using keyboard */
body:not(.keyboard-nav) *:focus:not(:focus-visible) {
  outline: none;
}

/* ═══════════════════════════════════════════════════════════════════════════
   ARIA STATE INDICATORS
   ═══════════════════════════════════════════════════════════════════════════ */

/* Expanded state indicator */
[aria-expanded="false"] .collapse-icon,
[aria-expanded="false"] .accordion-icon {
  transform: rotate(0deg);
}

[aria-expanded="true"] .collapse-icon,
[aria-expanded="true"] .accordion-icon {
  transform: rotate(180deg);
}

/* Invalid state styling (WCAG 3.3.1) */
[aria-invalid="true"] {
  border-color: var(--color-error) !important;
}

[aria-invalid="true"]:focus {
  box-shadow: 0 0 0 3px var(--color-error-light) !important;
}

/* Error messages should be associated via aria-describedby */
.field-error {
  color: var(--color-error);
  font-size: var(--text-sm);
  margin-top: var(--space-1);
}

/* Disabled state styling */
[aria-disabled="true"],
[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

/* Current page indicator (WCAG 2.4.8) */
[aria-current="page"] {
  font-weight: var(--font-semibold);
  color: var(--color-primary);
}

[aria-current="step"] {
  font-weight: var(--font-semibold);
}

/* Selected state */
[aria-selected="true"] {
  font-weight: var(--font-medium);
}

/* ═══════════════════════════════════════════════════════════════════════════
   LOADING STATE INDICATOR
   ═══════════════════════════════════════════════════════════════════════════ */

[aria-busy="true"] {
  position: relative;
  opacity: 0.7;
  pointer-events: none;
}

[aria-busy="true"]::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 24px;
  height: 24px;
  margin: -12px 0 0 -12px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: a11y-spin 1s linear infinite;
}

@keyframes a11y-spin {
  to {
    transform: rotate(360deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  [aria-busy="true"]::after {
    animation: none;
    /* Show static indicator instead */
    border-top-color: var(--color-primary);
    border-right-color: var(--color-primary);
  }
}

/* ═══════════════════════════════════════════════════════════════════════════
   TEXT SPACING (WCAG 1.4.12)
   Support user text spacing adjustments without breaking layout
   ═══════════════════════════════════════════════════════════════════════════ */

/* Ensure layout doesn't break with increased text spacing */
body {
  /* These are the WCAG requirements - layout should handle: */
  /* Line height: 1.5x font size */
  /* Paragraph spacing: 2x font size */
  /* Letter spacing: 0.12x font size */
  /* Word spacing: 0.16x font size */
  line-height: var(--leading-relaxed);
}

/* Containers should use min-height, not fixed height */
.content-container {
  min-height: auto;
}

/* ═══════════════════════════════════════════════════════════════════════════
   REFLOW (WCAG 1.4.10)
   Content should reflow at 320px width without horizontal scrolling
   ═══════════════════════════════════════════════════════════════════════════ */

@media (max-width: 320px) {
  /* Ensure content fits at 320px */
  body {
    overflow-x: hidden;
  }

  .main-content {
    padding: var(--space-3);
  }

  /* Tables should scroll horizontally if necessary */
  .table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Images should scale down */
  img {
    max-width: 100%;
    height: auto;
  }

  /* Code blocks should scroll */
  pre {
    overflow-x: auto;
  }
}

/* ═══════════════════════════════════════════════════════════════════════════
   CONTENT ON HOVER OR FOCUS (WCAG 1.4.13)
   Tooltips and similar content
   ═══════════════════════════════════════════════════════════════════════════ */

/* Tooltips must be: dismissible, hoverable, and persistent */
.glossary-tooltip,
[role="tooltip"] {
  /* Hoverable: can hover over tooltip without it disappearing */
  pointer-events: auto;
}

/* Dismissible: can be dismissed without moving focus */
.tooltip-dismissible {
  position: relative;
}

.tooltip-dismissible::before {
  /* Invisible close target */
  content: '';
  position: absolute;
  top: -10px;
  right: -10px;
  width: 30px;
  height: 30px;
}

/* Persistent: remains visible until dismissed or trigger loses focus */
.glossary-term:hover .glossary-tooltip,
.glossary-term:focus .glossary-tooltip,
.glossary-tooltip:hover {
  opacity: 1;
  visibility: visible;
}

/* ═══════════════════════════════════════════════════════════════════════════
   HEADINGS AND LABELS (WCAG 2.4.6)
   ═══════════════════════════════════════════════════════════════════════════ */

/* Ensure form labels are always visible */
.form-label {
  display: block;
  margin-bottom: var(--space-2);
}

/* Required field indicator */
.form-label .required {
  color: var(--color-error);
  margin-left: var(--space-1);
}

.form-label .required::before {
  content: '*';
}

/* Optional field indicator */
.form-label .optional {
  color: var(--color-text-muted);
  font-weight: var(--font-normal);
  font-size: var(--text-sm);
}

/* ═══════════════════════════════════════════════════════════════════════════
   ERROR IDENTIFICATION (WCAG 3.3.1)
   ═══════════════════════════════════════════════════════════════════════════ */

.form-error,
.error-message {
  color: var(--color-error);
  font-size: var(--text-sm);
  margin-top: var(--space-1);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.form-error::before,
.error-message::before {
  content: '';
  width: 16px;
  height: 16px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23dc2626' stroke-width='2'%3E%3Ccircle cx='12' cy='12' r='10'/%3E%3Cpath d='M12 8v4M12 16h.01'/%3E%3C/svg%3E");
  background-size: contain;
  flex-shrink: 0;
}

/* Success message styling */
.form-success,
.success-message {
  color: var(--color-success-dark);
  font-size: var(--text-sm);
  margin-top: var(--space-1);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.form-success::before,
.success-message::before {
  content: '';
  width: 16px;
  height: 16px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23059669' stroke-width='2'%3E%3Cpath d='M20 6 9 17l-5-5'/%3E%3C/svg%3E");
  background-size: contain;
  flex-shrink: 0;
}
