/*
 * Accessibilità — Fase 6 (D4, §17)
 * Keyboard navigation, contrast AA, screen reader ARIA, focus visibile, reduced motion.
 */

/* === Skip links — raggiungibili da tastiera (§17.1) === */
.skip-link {
  position: absolute;
  top: -100%;
  left: var(--space-4);
  padding: var(--space-2) var(--space-4);
  background: var(--bg);
  color: var(--text);
  text-decoration: none;
  border-radius: var(--radius);
  z-index: 200;
  font-size: 0.875rem;
  font-weight: 600;
  border: 1px solid var(--border);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.skip-link:focus {
  top: var(--space-2);
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* === Focus visibile universale (§17.1) === */
*:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 2px;
}

a:focus-visible,
button:focus-visible {
  outline-offset: 4px;
}

/* === Navigazione tastiera — ordine semantico (§16.3) === */
nav {
  display: flex;
  align-items: center;
  gap: var(--space-4);
}

/* === Screen reader — contenuti nascosti visivamente (§17.1) === */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* === Live region per ricerca (D4) — announce a screen reader === */
.sr-live {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

/* === Contrasto AA verificati (§17.1) === */
/* Testo su sfondo ≥ 4.5:1, grande testo ≥ 3:1 */
a {
  color: var(--accent);
}

a:hover {
  opacity: 1; /* Unica regola hover (F39): mantiene il contrasto AA */
}

time,
.meta,
footer {
  color: var(--muted);
}

/* === Contrasto elevato — prefers-contrast: more (§17.2) === */
@media (prefers-contrast: more) {
  :root {
    --border: #444;
    --muted: #555;
    --accent: #0044cc;
  }

  a {
    color: var(--accent);
    text-decoration-thickness: 2px;
  }

  mark {
    background: var(--accent);
    color: white;
  }

  /* Focus ancora più visibile */
  *:focus-visible {
    outline-width: 3px;
  }
}

/* === Reduced motion — refined (§17.2) === */
@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto; /* Niente smooth scroll */
  }

  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    transition-delay: 0ms !important;
  }

  /* Palette non si anima */
  .palette-overlay {
    transition: none;
  }
}

/* === Tab order corretto (§16.3) ===
   Ordine DOM naturale: nav → main → footer.
   main ha tabindex="-1" nel DOM per la skip-link. */

/* === Focus trap per palette (D4) === */
.palette-overlay[aria-modal="true"] {
  outline: none;
}

/* === Stampa accessibile (§24) === */
@media print {
  .skip-link,
  .search-trigger,
  .palette-overlay,
  .sr-only,
  .sr-live {
    display: none;
  }

  a[href]::after {
    content: " (" attr(href) ")";
    font-size: 0.8em;
    word-break: break-word;
  }

  mark {
    background: transparent;
    border: 1px solid #000;
  }
}

/* === High contrast — prefers-contrast: more (D9, §17.2) === */
@media (prefers-contrast: more) {
  body {
    background: white;
    color: black;
  }

  a {
    text-decoration: underline;
    text-decoration-thickness: 2px;
  }

  code,
  pre {
    border: 1px solid #000;
  }

  .palette-overlay {
    background: rgba(0, 0, 0, 0.8);
  }

  .palette-container {
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
  }
}

/* === Reduced transparency — prefers-reduced-transparency (§17.2) === */
@media (prefers-reduced-transparency: reduce) {
  .palette-overlay {
    background: rgb(0, 0, 0);
  }

  nav,
  footer {
    background: var(--bg);
  }
}
