/* ==========================================================================
   Ability Beyond Vision — shared accessibility layer
   Include on EVERY page of the platform, before any page-specific CSS:
     <link rel="stylesheet" href="/assets/a11y.css">
     <script src="/assets/a11y.js" defer></script>
   Targets WCAG 2.2 level AA.
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. Colour schemes
   Every pairing below is checked against WCAG 1.4.3 (4.5:1 body text,
   3:1 large text) and 1.4.11 (3:1 for UI components and focus indicators).
   -------------------------------------------------------------------------- */

*, *::before, *::after { box-sizing: border-box; }

/* Brand colours: dark teal #0D3B38, gold #D79412, ivory #F7F3E9.
   All three are used, but not interchangeably, because the measured contrast
   decides what each one is allowed to do:

     teal on ivory  11.15:1  — body text, links
     ivory on teal  11.15:1  — reversed text
     gold on teal    4.79:1  — passes as text on dark
     teal on gold    4.79:1  — passes as text ON a gold fill
     gold on ivory   2.33:1  — FAILS text (4.5) and even borders (3.0)

   So gold is never text, a link or an outline on the light background. It is
   a fill that dark text sits on, and the accent of the dark scheme. Getting
   this wrong on a sight loss organisation's own website would be the whole
   joke, so the numbers win over the brand sheet every time. */

:root {
  --bg: #f7f3e9;          /* ivory */
  --fg: #0d3b38;          /* teal — 11.15:1 on ivory */
  --muted: #3f5a56;       /* 6.75:1 on ivory, 6.17:1 on surface */
  --accent: #0d3b38;      /* links: teal, and always underlined */
  --on-accent: #f7f3e9;   /* ivory on teal — 11.15:1 */
  --surface: #efe9da;     /* a deeper ivory for panels */
  --rule: #4a6663;        /* 5.62:1 on ivory — passes non-text contrast */
  --focus: #b8003a;       /* 6.12:1 on ivory */
  --logo-panel: #000000;  /* the logo artwork is on black */

  /* Gold, for fills that dark text sits on. Never for text on ivory. */
  --brand-gold: #d79412;
  --on-brand-gold: #0d3b38;

  --text-scale: 1;
  --zoom: 1;
}

/* Follow the operating system when the user has not chosen a scheme. */
/* Dark: the teal becomes the page and the gold finally gets to be the accent,
   which is where it belongs — 5.65:1 on the background. The accent is
   lightened slightly to #e0a62e so it still passes on the raised surface
   (#d79412 lands at 4.46:1 there, a hair under 4.5). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #0a2e2c;        /* deep teal */
    --fg: #f7f3e9;        /* ivory — 13.16:1 */
    --muted: #b9c9c6;     /* 8.49:1 */
    --accent: #e0a62e;    /* gold — 6.71:1 on bg, 5.3:1 on surface */
    --on-accent: #0a2e2c;
    --surface: #10403d;
    --rule: #7fa8a4;      /* 5.58:1 */
    --focus: #ffb000;     /* 7.96:1 */
    --logo-panel: #000000;

    --brand-gold: #d79412;
    --on-brand-gold: #0d3b38;
  }
}

:root[data-theme="light"] {
  --bg: #f7f3e9;
  --fg: #0d3b38;
  --muted: #3f5a56;
  --accent: #0d3b38;
  --on-accent: #f7f3e9;
  --surface: #efe9da;
  --rule: #4a6663;
  --focus: #b8003a;
  --logo-panel: #000000;

  --brand-gold: #d79412;
  --on-brand-gold: #0d3b38;
}

:root[data-theme="dark"] {
  --bg: #0a2e2c;
  --fg: #f7f3e9;
  --muted: #b9c9c6;
  --accent: #e0a62e;
  --on-accent: #0a2e2c;
  --surface: #10403d;
  --rule: #7fa8a4;
  --focus: #ffb000;
  --logo-panel: #000000;

  --brand-gold: #d79412;
  --on-brand-gold: #0d3b38;
}

/* Maximum contrast: pure black and white, 21:1 throughout. */
:root[data-theme="contrast"] {
  --bg: #000000;
  --fg: #ffffff;
  --muted: #ffffff;
  --accent: #ffffff;
  --on-accent: #000000;
  --surface: #000000;
  --rule: #ffffff;
  --focus: #ffff00;
  --logo-panel: #000000;

  /* Brand gold is deliberately dropped here. These two schemes exist because
     someone needs maximum contrast; a brand colour is not more important than
     that. Gold collapses into the scheme's own palette. */
  --brand-gold: #ffffff;
  --on-brand-gold: #000000;
}

/* Yellow on black — widely preferred by people with low vision. 19.6:1 */
:root[data-theme="yellow"] {
  --bg: #000000;
  --fg: #ffd400;
  --muted: #ffd400;
  --accent: #ffd400;
  --on-accent: #000000;
  --surface: #141414;
  --rule: #ffd400;
  --focus: #ffffff;
  --logo-panel: #000000;

  --brand-gold: #ffd400;
  --on-brand-gold: #000000;
}

/* --------------------------------------------------------------------------
   2. Text size and zoom
   Scale is applied at the root so every rem-based measurement follows,
   satisfying 1.4.4 (resize to 200%) without a separate layout.
   -------------------------------------------------------------------------- */

html {
  font-size: calc(18px * var(--text-scale));
  zoom: var(--zoom);
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: Arial, Helvetica, "Segoe UI", sans-serif;
  font-size: 1rem;
  line-height: 1.6;
}

/* 1.4.10 Reflow — a single long word must never force a horizontal
   scrollbar at a 320px viewport (worse still at 200% text). `anywhere`,
   not `break-word`, because only `anywhere` also reduces an element's
   min-content width, which is what flex and grid track sizing depend on.
   Set on body so it inherits to every element, including ones easy to
   forget: blockquote, cite, caption, figcaption, td. */
body {
  overflow-wrap: anywhere;
  word-break: normal;
}

/* --------------------------------------------------------------------------
   3. Focus, targets and motion
   -------------------------------------------------------------------------- */

:where(a, button, input, select, textarea, summary):focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

/* 2.4.11 Focus not obscured — nothing here is sticky, but guard anyway. */
:where(a, button, input, select, textarea, summary) {
  scroll-margin-top: 4rem;
}

/* 2.5.8 Target size — 44px comfortably exceeds the 24px minimum. */
.a11y-bar button,
.a11y-bar input[type="range"] {
  min-height: 44px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

/* --------------------------------------------------------------------------
   4. Skip link
   -------------------------------------------------------------------------- */

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 100;
  background: var(--accent);
  color: var(--on-accent);
  padding: 0.75rem 1.25rem;
  font-weight: bold;
}
.skip-link:focus { left: 0; }

/* --------------------------------------------------------------------------
   5. The accessibility toolbar
   Deliberately in the normal document flow, not fixed or floating, so it
   can never cover focused content (2.4.11) or trap a magnifier user.
   -------------------------------------------------------------------------- */

.a11y-bar {
  background: var(--surface);
  border-bottom: 2px solid var(--rule);
  color: var(--fg);
}

.a11y-bar__inner {
  max-width: 60rem;
  margin: 0 auto;
  padding: 0.5rem 14px;
}

/* The top line of the toolbar: accessibility settings at the leading end,
   docked extras (the sight loss simulator) at the trailing end. Both items
   set min-width:0 and break anywhere, so at 200% text they wrap onto
   separate lines instead of forcing the page sideways (1.4.10 Reflow). */
.a11y-bar__row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.a11y-bar__row > * { min-width: 0; }

.a11y-bar__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  /* Empty on the coming-soon page, where nothing docks here. */
  margin-left: auto;
}

.a11y-bar__actions:empty { display: none; }

/* Deliberately inline-block, not inline-flex. A flex container's minimum
   width is its longest unbreakable word, and at 200% text "Accessibility"
   alone is wider than a 320px screen — which broke reflow (1.4.10). */
.a11y-bar__toggle {
  display: inline-block;
  background: transparent;
  color: var(--fg);
  border: 2px solid var(--rule);
  border-radius: 0.25rem;
  padding: 0.5rem 1rem;
  font: inherit;
  font-weight: bold;
  cursor: pointer;
  max-width: 100%;
  text-align: left;
  /* `anywhere`, not `break-word`: only `anywhere` reduces the element's
     minimum content width, which is what reflow actually depends on. */
  overflow-wrap: anywhere;
}

/* Option buttons must also wrap rather than push the layout wide. */
.a11y-options button,
.a11y-bar__reset {
  max-width: 100%;
  overflow-wrap: anywhere;
}

.a11y-bar__toggle:hover { background: var(--bg); }

.a11y-bar__panel {
  padding: 1rem 0 0.5rem;
}

.a11y-bar__panel[hidden] { display: none; }

/* Horizontal padding in px, not rem: at 200% text plus 200% zoom the
   effective viewport is 160px and rem padding would break reflow (1.4.10). */
.a11y-group {
  border: 2px solid var(--rule);
  border-radius: 0.25rem;
  padding: 0.75rem 14px 1rem;
  margin: 0 0 1rem;
}

.a11y-group legend {
  font-weight: bold;
  padding: 0 0.4rem;
}

.a11y-options {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.a11y-options button {
  background: var(--bg);
  color: var(--fg);
  border: 2px solid var(--rule);
  border-radius: 0.25rem;
  padding: 0.5rem 1rem;
  font: inherit;
  cursor: pointer;
}

.a11y-options button[aria-pressed="true"] {
  background: var(--accent);
  color: var(--on-accent);
  border-color: var(--accent);
  font-weight: bold;
}

.a11y-slider {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

/* Widths are intentionally in px, not rem: the slider must not grow with
   the text-size setting, or the toolbar overflows at large sizes (1.4.10). */
.a11y-slider input[type="range"] {
  flex: 1 1 160px;
  min-width: 0;
  max-width: 100%;
  accent-color: var(--accent);
}

.a11y-slider output {
  font-weight: bold;
  min-width: 4ch;
}

.a11y-group,
.a11y-bar__inner {
  max-width: 100%;
}

.a11y-bar__reset {
  background: var(--bg);
  color: var(--fg);
  border: 2px solid var(--rule);
  border-radius: 0.25rem;
  padding: 0.5rem 1rem;
  font: inherit;
  cursor: pointer;
}

/* Screen-reader-only helper. */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* If scripting is unavailable the toolbar is hidden rather than broken;
   the page itself remains fully usable and the OS theme still applies. */
.no-js .a11y-bar { display: none; }
