Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

142 KB
Raw Blame
# allow: no-sut-import — guardian; statically parses the shipped theme and
# component CSS rather than exercising Python production code
"""Labels on filled accent controls must be readable in every theme.

Outline buttons (hover, keyboard focus and active, since Bootstrap's
``.btn:focus-visible`` reuses the hover colors), the skip link, primary
buttons, the selected settings tab, active filters and accent badges draw
``var(--text-on-accent)`` on ``var(--accent-primary)`` and/or
``var(--accent-secondary)`` (``test_accent_fills_use_accent_ink`` sweeps
every stylesheet, template ``<style>`` block and inline ``style``
attribute in templates and scripts; ``test_filled_anchors_keep_readable_ink``
replays the cascade for every filled control class on a link -- accent,
status and other colour fills). White is too faint on every
shipped dark accent, so each theme sets the token next to its accent; the
styles.css ``:root`` palette (used alone by the login pages) keeps white on
its own accent.
"""

import functools
import hashlib
import re
from pathlib import Path

import pytest

WEB_ROOT = (
    Path(__file__).resolve().parents[2] / "src" / "local_deep_research" / "web"
)
CSS_ROOT = WEB_ROOT / "static" / "css"
THEME_FILES = sorted((WEB_ROOT / "themes").rglob("*.css"))

# Generated (git-ignored) sheets under ``static/css`` that the page-sheet
# scan must skip. ``themes.css`` is the concatenation of ``themes/*/*.css``
# that the FastAPI lifespan (``web/fastapi_app.py``, via
# ``theme_registry.get_combined_css()``) writes on every boot, so it exists
# only after the app (or a test that boots it) has started -- e.g. in the
# CI pytest run, never in a clean checkout. Read as a plain page sheet, its
# ``[data-theme="x"]`` rules lose their scope and get judged against every
# other palette. The theme sources themselves are checked via THEME_FILES.
# test_generated_sheet_exclusions_are_generated keeps this list honest.
GENERATED_SHEETS = {"themes.css"}

ACCENT_INK = "var(--text-on-accent)"


def _strip_comments(css):
    return re.sub(r"/\*.*?\*/", "", css, flags=re.DOTALL)


def _custom_properties(css):
    """``{token: value}`` for every ``--token: value`` declaration, the value
    kept as written. Nothing is dropped here: ``_resolve`` reads the value
    when a check needs the token as a colour, and fails on one it cannot
    read, so an unreadable theme value never falls back to the ``:root``
    default."""
    return {
        name: " ".join(value.split())
        for name, value in re.findall(
            r"(?:^|[{;])\s*--([\w-]+)\s*:\s*([^;{}]*?)\s*(?=[;}])",
            _strip_comments(css),
            flags=re.MULTILINE,
        )
    }


_HEX = re.compile(r"#([\da-fA-F]{3,4}|[\da-fA-F]{6}|[\da-fA-F]{8})")
_NUMBER = r"\s*([\d.]+)(%?)\s*"
_RGB_LITERAL = re.compile(
    rf"rgba?\({_NUMBER},{_NUMBER},{_NUMBER}(?:,{_NUMBER})?\)"
    r"|rgba?\(\s*([\d.]+)(%?)\s+([\d.]+)(%?)\s+([\d.]+)(%?)\s*"
    r"(?:/\s*([\d.]+)(%?)\s*)?\)",
    re.IGNORECASE,
)
_KEYWORDS = {
    "white": ((255, 255, 255), 1.0),
    "black": ((0, 0, 0), 1.0),
    "transparent": ((0, 0, 0), 0.0),
}


def _parse_color(value):
    """``(rgb, alpha)`` for a literal colour the checks read: 3/4/6/8-digit
    hex, ``rgb()``/``rgba()`` with literal numbers (comma or space
    syntax), ``white``, ``black`` and ``transparent``; None for anything
    else (``hsl()``, a named colour, ``var()``, ``color-mix()``...)."""
    value = value.strip()
    hex_match = _HEX.fullmatch(value)
    if hex_match:
        digits = hex_match.group(1)
        if len(digits) <= 4:
            digits = "".join(c * 2 for c in digits)
        channels = [
            int(digits[i : i + 2], 16) for i in range(0, len(digits), 2)
        ]
        alpha = channels[3] / 255 if len(channels) == 4 else 1.0
        return tuple(channels[:3]), alpha
    if value.lower() in _KEYWORDS:
        return _KEYWORDS[value.lower()]
    rgb_match = _RGB_LITERAL.fullmatch(value)
    if rgb_match:
        groups = [g for g in rgb_match.groups()]
        groups = groups[:8] if groups[0] is not None else groups[8:]
        channels = []
        for number, percent in zip(groups[0:6:2], groups[1:6:2]):
            channel = float(number) * 2.55 if percent else float(number)
            if channel > 255:
                return None
            channels.append(channel)
        alpha = 1.0
        if groups[6] is not None:
            alpha = float(groups[6]) / (100 if groups[7] else 1)
        if alpha > 1:
            return None
        return tuple(channels), alpha
    return None


def _root_defaults():
    css = _strip_comments((CSS_ROOT / "styles.css").read_text(encoding="utf-8"))
    match = re.search(r"(?:^|\})\s*:root\s*\{([^{}]*)\}", css)
    assert match, "styles.css has no :root block"
    return _custom_properties(match.group(1))


def _resolve(token, props, seen=()):
    """The opaque colour ``--token`` draws under ``props``, following
    ``var()`` references. Fails (AssertionError) when the token is
    undefined, a ``var()`` cycle, translucent, or a value ``_parse_color``
    does not read."""
    value = props.get(token)
    assert value is not None, f"--{token} is not defined"
    ref = re.fullmatch(r"var\(\s*--([\w-]+)\s*(?:,.*)?\)", value)
    if ref:
        assert ref.group(1) not in seen, f"--{token} is a var() cycle"
        return _resolve(ref.group(1), props, (*seen, token))
    parsed = _parse_color(value)
    assert parsed is not None, f"--{token}: {value!r} is not a colour read here"
    rgb, alpha = parsed
    assert alpha == 1, f"--{token}: {value!r} is translucent"
    return rgb


def _luminance(rgb):
    linear = [
        c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4
        for c in (channel / 255 for channel in rgb)
    ]
    return 0.2126 * linear[0] + 0.7152 * linear[1] + 0.0722 * linear[2]


def _contrast(a, b):
    dark, light = sorted((_luminance(a), _luminance(b)))
    return (light + 0.05) / (dark + 0.05)


def test_all_themes_are_discovered():
    """An empty glob would make the parametrized check skip, not fail."""
    stems = {path.stem for path in THEME_FILES}
    assert {"hashed", "high-contrast", "sepia", "vesper"} <= stems
    assert len(THEME_FILES) >= 30


def test_generated_sheet_exclusions_are_generated():
    """Every sheet the scan skips must be a git-ignored build output with a
    named generator, so the exclusion can't silently hide a hand-written
    sheet."""
    ignored = {
        line.strip()
        for line in (REPO_ROOT / ".gitignore")
        .read_text(encoding="utf-8")
        .splitlines()
    }
    generator = (WEB_ROOT / "fastapi_app.py").read_text(encoding="utf-8")
    for rel in GENERATED_SHEETS:
        assert f"src/local_deep_research/web/static/css/{rel}" in ignored, (
            f"{rel} is excluded from the scan but not git-ignored"
        )
        assert f'"css" / "{rel}"' in generator, (
            f"{rel} is excluded from the scan but fastapi_app.py no "
            "longer writes it"
        )
    assert "theme_registry.get_combined_css()" in generator


@pytest.mark.parametrize("path", THEME_FILES, ids=lambda path: path.stem)
@pytest.mark.parametrize("fill", ["accent-primary", "accent-secondary"])
def test_accent_ink_contrast(path, fill):
    props = _root_defaults()
    props.update(_custom_properties(path.read_text(encoding="utf-8")))
    ink = _resolve("text-on-accent", props)
    background = _resolve(fill, props)
    ratio = _contrast(ink, background)
    assert ratio >= 4.5, (
        f"{path.stem}: --text-on-accent on --{fill} is {ratio:.2f}:1"
    )


def test_root_palette_accent_ink_contrast():
    """Pages that load styles.css without themes.css (login, register), or
    after it (change password), draw the :root pair together.

    Known gap, not asserted (pre-existing): white on the :root
    --accent-secondary (#9179f0, the .btn-primary:hover fill) is 3.39:1.
    """
    props = _root_defaults()
    ink = _resolve("text-on-accent", props)
    ratio = _contrast(ink, _resolve("accent-primary", props))
    assert ratio >= 4.5, (
        f":root --text-on-accent on --accent-primary {ratio:.2f}:1"
    )


@pytest.mark.parametrize("path", THEME_FILES, ids=lambda path: path.stem)
def test_theme_sets_accent_ink_beside_its_accent(path):
    """Inheriting the :root ink would pair it with this theme's accent, which
    the :root value was never chosen for."""
    props = _custom_properties(path.read_text(encoding="utf-8"))
    assert "text-on-accent" in props, f"{path.stem} must set --text-on-accent"


def _rules(filename):
    return _parse_rules((CSS_ROOT / filename).read_text(encoding="utf-8"))


def _parse_rules(css):
    css = _strip_comments(css)
    for match in re.finditer(r"([^{}]*)\{([^{}]*)\}", css):
        decls = {}
        for decl in match.group(2).split(";"):
            name, sep, value = decl.partition(":")
            if sep:
                decls[name.strip().lower()] = " ".join(value.split())
        yield " ".join(match.group(1).split()), decls


def test_outline_primary_hover_focus_active_use_accent_ink():
    """Bootstrap's .btn:focus-visible reuses the hover pair, so this covers
    keyboard focus as well as hover and active."""
    rules = [d for s, d in _rules("styles.css") if s == ".btn-outline-primary"]
    assert rules, "styles.css no longer styles .btn-outline-primary"
    decls = rules[-1]
    for state in ("hover", "active"):
        assert decls.get(f"--bs-btn-{state}-bg") == "var(--accent-primary)"
        assert decls.get(f"--bs-btn-{state}-color") == ACCENT_INK


def test_accent_ink_only_sits_on_accent_fills():
    """The palette check above only holds where the ink is drawn on an accent
    fill; a rule pairing it with another background needs its own token."""
    offenders = []
    for name, rules in _sheets():
        for selector, decls in rules:
            if decls.get("color") != ACCENT_INK:
                continue
            background = decls.get("background-color", decls.get("background"))
            if background is not None and not _is_accent_fill(background):
                offenders.append(f"{name} {selector}: {background}")
    assert not offenders, offenders


# A fill counts as an accent fill when every colour stop in it is the
# primary or secondary accent: the two fills the contrast check above
# covers. Gradients mixing in --accent-tertiary or a literal colour are not
# covered by that check, so they are out of scope here.
_COLOR_STOP = re.compile(
    r"var\(--[\w-]+\)|rgba?\([^()]*(?:\([^()]*\)[^()]*)*\)"
    r"|#[\da-fA-F]{3,8}\b|\b(?:white|black|transparent)\b"
)
# ``var(--accent-primary, #6e4ff6)`` draws the token whenever it is defined,
# so a fallback is dropped before the stops and the ink are compared.
_VAR_FALLBACK = re.compile(
    r"var\(\s*(--[\w-]+)\s*,[^()]*(?:\([^()]*\)[^()]*)*\)"
)


def _drop_fallbacks(value):
    return _VAR_FALLBACK.sub(r"var(\1)", value)


_RGB_STOP = re.compile(r"rgba?\(\s*var\(--([\w-]+)\)\s*(?:,\s*([\d.]+)\s*)?\)")
_VAR_STOP = re.compile(r"var\(--([\w-]+)\)")
_ACCENT_TOKENS = frozenset({"accent-primary", "accent-secondary"})
_ACCENT_RGB_TOKENS = frozenset({"accent-primary-rgb", "accent-secondary-rgb"})
# The last class or pseudo-class of a compound selector: ``.a.b:hover``
# inherits its fill or ink from ``.a.b`` and then ``.a``.
_LAST_SIMPLE = re.compile(r"(\.[\w-]+|:[\w-]+(\([^()]*\))?)$")
_PSEUDO_ELEMENT = re.compile(r"::?(?:before|after)$")

# Rules on an accent fill that keep another ink because no template, script
# or Python module emits their class or id (checked with git grep, including
# template-literal class names); fix them if they are ever used again.
DEAD_ACCENT_RULES = {
    # document_text.html's .ldr-text-actions only holds .ldr-btn-secondary.
    ("document_details.css", ".ldr-text-actions .ldr-btn-primary"),
    ("document_details.css", ".ldr-text-actions .ldr-btn-primary:hover"),
    # Only link_analytics.html has a modal header, and it does not load
    # collections.css.
    ("collections.css", ".ldr-modal-header"),
    ("link_analytics.css", ".ldr-domain-count"),
    # No template or script renders the mobile sheet's badge.
    ("mobile-navigation.css", ".ldr-mobile-sheet-badge"),
    ("news.css", ".ldr-filter-chip.active"),
    ("styles.css", ".ldr-notification-group .btn-sm:hover"),
    ("styles.css", ".ldr-settings-card .btn:hover"),
    ("styles.css", ".ldr-sidebar-nav li.active .ldr-nav-shortcut"),
    ("styles.css", ".ldr-sidebar-nav li a:focus .ldr-nav-shortcut"),
    ("styles.css", ".ldr-sidebar-nav li:hover .ldr-nav-shortcut"),
    ("styles.css", "#try-again-btn"),
    ("styles.css", "#try-again-btn:hover"),
    ("settings.css", ".ldr-editor-lang"),
}


def _custom_property_definitions():
    """Every custom-property definition in shipped CSS (stylesheets, theme
    files and template ``<style>`` blocks), whatever its selector:
    ``{token: [(file, selector, value), ...]}``."""
    definitions = {}
    for name, css in _css_sources():
        for selector, decls in _parse_rules(css):
            for prop, value in decls.items():
                if prop.startswith("--"):
                    definitions.setdefault(prop[2:], []).append(
                        (name, selector, value)
                    )
    return definitions


def _css_sources():
    yield from _stylesheets()
    for path in THEME_FILES:
        yield path.name, path.read_text(encoding="utf-8")


def _stops_are_accent(value, accent, accent_rgb):
    stops = _COLOR_STOP.findall(
        _drop_fallbacks(value.replace("!important", ""))
    )
    if not stops:
        return False
    for stop in stops:
        rgb = _RGB_STOP.fullmatch(stop)
        if rgb:
            if rgb.group(1) not in accent_rgb:
                return False
            if rgb.group(2) is not None and float(rgb.group(2)) != 1:
                return False
            continue
        token = _VAR_STOP.fullmatch(stop)
        if not token or token.group(1) not in accent:
            return False
    return True


def _accent_aliases():
    """Tokens that draw the primary or secondary accent, followed through
    ``--x: var(--accent-primary)`` aliases (``--primary-color``,
    ``--news-gradient-1``) and their ``-rgb`` twins, transitively.

    A token counts when any of its definitions resolves to the accent, so a
    per-theme override with a literal colour (``--mobile-nav-active``) still
    makes its fills accent fills: conservative, since the theme where it
    resolves to the accent needs the accent ink. Returns
    ``(accent, accent_rgb, mixed)``; ``mixed`` maps the tokens whose other
    definitions do not resolve to the accent to those definitions."""
    definitions = _custom_property_definitions()
    accent, accent_rgb = set(_ACCENT_TOKENS), set(_ACCENT_RGB_TOKENS)
    changed = True
    while changed:
        changed = False
        for token, values in definitions.items():
            if token in accent or token in accent_rgb:
                continue
            for _, _, value in values:
                plain = _drop_fallbacks(value.replace("!important", "")).strip()
                ref = _VAR_STOP.fullmatch(plain)
                if ref and ref.group(1) in accent_rgb:
                    accent_rgb.add(token)
                elif _stops_are_accent(value, accent, accent_rgb):
                    accent.add(token)
                else:
                    continue
                changed = True
                break
    mixed = {}
    for token in (accent | accent_rgb) - _ACCENT_TOKENS - _ACCENT_RGB_TOKENS:
        for name, selector, value in definitions[token]:
            plain = _drop_fallbacks(value.replace("!important", "")).strip()
            ref = _VAR_STOP.fullmatch(plain)
            if not (
                _stops_are_accent(value, accent, accent_rgb)
                or (ref and ref.group(1) in accent_rgb)
            ):
                mixed.setdefault(token, []).append((name, selector, value))
    return accent, accent_rgb, mixed


@functools.cache
def _aliases():
    return _accent_aliases()


def _is_accent_fill(background):
    accent, accent_rgb, _ = _aliases()
    return _stops_are_accent(background, accent, accent_rgb)


# Raw-text scan, independent of the rule parser the sweep uses.
_ALIAS_DEFINITION = re.compile(
    r"--([\w-]+)\s*:\s*var\(\s*--accent-(?:primary|secondary)(-rgb)?\s*[,)]"
)


def test_accent_aliases_are_recognised():
    """Every ``--x: var(--accent-primary|secondary...)`` definition in
    shipped CSS, under any selector, names a token the fill sweep treats
    as the accent, so a rule filled with ``var(--primary-color)`` or
    ``var(--news-gradient-1)`` is held to the accent ink like one filled
    with ``var(--accent-primary)``."""
    accent, accent_rgb, _ = _aliases()
    assert {"primary-color", "news-gradient-1", "mobile-nav-active"} <= accent
    assert "primary-color-rgb" in accent_rgb
    missed, found = [], 0
    for name, css in _css_sources():
        for match in _ALIAS_DEFINITION.finditer(_strip_comments(css)):
            found += 1
            pool = accent_rgb if match.group(2) else accent
            if match.group(1) not in pool:
                missed.append(f"{name}: {match.group(0)}")
    assert found >= 33, "alias scan found too few definitions"
    assert not missed, missed


# Aliases that resolve to the accent in one scope and to another colour in
# another. Their fills are still held to the accent ink (conservative); a
# new entry needs a check that the ink also reads on the other colour.
MIXED_ACCENT_ALIASES = {
    # Literal per-theme colours in mobile-navigation.css; its only text fill
    # (.ldr-mobile-sheet-badge) is dead, the others are stripes and ripples.
    "mobile-nav-active": "per-theme literal in mobile-navigation.css",
    "mobile-nav-text-active": "per-theme literal; used as a text colour",
    # Bootstrap button variables, set per button variant; no shipped rule
    # fills with them directly.
    "bs-btn-active-bg": "Bootstrap variant variable",
    "bs-btn-active-border-color": "Bootstrap variant variable",
    "bs-btn-border-color": "Bootstrap variant variable",
    "bs-btn-color": "Bootstrap variant variable",
    "bs-btn-hover-bg": "Bootstrap variant variable",
    "bs-btn-hover-border-color": "Bootstrap variant variable",
}


def test_mixed_accent_aliases_are_reviewed():
    """A token defined as the accent in one place and as something else
    elsewhere is treated as an accent fill everywhere; listing it makes a
    new one visible for review."""
    _, _, mixed = _aliases()
    assert set(mixed) == set(MIXED_ACCENT_ALIASES), {
        token: mixed.get(token)
        for token in set(mixed) ^ set(MIXED_ACCENT_ALIASES)
    }


def _compound_chain(selector):
    chain = [selector]
    while True:
        match = _LAST_SIMPLE.search(selector)
        if not match or match.start() == 0:
            return chain
        if selector[match.start() - 1] in " >+~":
            return chain
        selector = selector[: match.start()]
        chain.append(selector)


def _selector_chain(selector):
    """``.a .b.c:hover`` -> itself, ``.a .b.c``, ``.a .b``, then the subject
    compound alone (``.b.c:hover``, ``.b.c``, ``.b``): a rule on the subject
    alone matches the same element."""
    chain = _compound_chain(selector)
    subject = re.split(r"\s*[ >+~]\s*", selector)[-1]
    if subject != selector:
        chain += [s for s in _compound_chain(subject) if s not in chain]
    return chain


def _stylesheets():
    for path in sorted(CSS_ROOT.rglob("*.css")):
        if path.relative_to(CSS_ROOT).as_posix() in GENERATED_SHEETS:
            continue
        yield path.name, path.read_text(encoding="utf-8")
    for name, path in _markup_files():
        if path.suffix != ".html":
            continue
        html = path.read_text(encoding="utf-8")
        for css in re.findall(r"<style[^>]*>(.*?)</style>", html, flags=re.S):
            yield name, css


# --- Inline styles ---------------------------------------------------------
#
# Inline styles are read from the templates (``templates/`` and the HTML
# fragments script fetches from ``static/templates/``) and the scripts
# (``static/js/``):
#   - ``style=`` attributes in any case (``STYLE=``), quoted with ``"``,
#     ``'``, a backtick or, inside a script string, an escaped ``\"`` or
#     ``\'``, or unquoted, and ``el.style = ...`` assignments;
#   - ``el.style.cssText = ...`` (and ``+=``) and
#     ``el.setAttribute('style', ...)`` writes.
# A ``style =`` right after a standalone ``const``, ``let`` or ``var``
# whose right-hand side is a call declares a script variable and is
# skipped. A style whose value is all literal text becomes
# a pseudo-rule ``style@L<line>`` of its file. The scanner does not
# interpret script: every ``${...}`` expression and server-side template
# tag in a style's value -- in any property, since an expression can also
# add declarations (``width: ${w}`` with ``w = '1px; background: red'``)
# -- fails ``test_dynamic_inline_styles_are_allowlisted`` unless its exact
# (file, line, property) is listed in DYNAMIC_INLINE_STYLES with the same
# expression text. The values listed there, read off the source by a
# reviewer, replace the expression: one pseudo-rule ``style@L<line>#<n>``
# per combination, checked like any other rule. A style the scanner
# cannot read as ``property: value`` declarations fails with no
# allowlist for application UI: an attribute that is itself an expression
# (``style=${st}``, ``style="${st}"``); an expression or tag in a property name or in a part
# with no ``:``; a quote, backtick, backslash or brace left in a value
# once its expressions, a ``font-family`` list of plain quoted names
# (``'Courier New'``) and plain ``url()``s are set aside, so a string
# concatenation such as ``'style="color: ' + c + '"'`` fails; a write whose right-hand side is
# not a single string literal; and a quote, expression or tag that does
# not close. At a site it finds, the scanner fails rather than skipping
# what it cannot read. Sites spelled other ways are not found: styles set
# one property at a time (``el.style.color = ...``,
# ``style.setProperty(...)``, ``Object.assign(el.style, ...)``),
# ``el['style']``, an attribute name built by script, and ``<style>`` text
# built in script. The one sanitized document-authored KaTeX write is
# classified separately below, with its exact guarded source pinned.
_MAX_VARIANTS = 64
# A server-side template tag, which renders before the browser parses the
# attribute.
_JINJA = re.compile(r"\{\{.*?\}\}|\{%.*?%\}|\{#.*?#\}", re.S)
# An attribute named ``style`` (not ``data-style``, not ``el.style``).
_STYLE_ATTR = re.compile(r"(?<![\w$.-])style\s*=(?![=>])\s*", re.I)
# A write to an element's whole inline style.
_STYLE_WRITE = re.compile(
    r"\.style\s*=(?![=>])\s*"
    r"|\.style\.cssText\s*\+?=\s*"
    r"|\.setAttribute\(\s*(['\"`])style\1\s*,\s*",
    re.I,
)
# ``const style = getComputedStyle(...)``: a script variable set from a
# call. The keyword must stand alone (not ``data-var``), and a string
# right-hand side is read like any other style.
_VARIABLE_DECLARATION = re.compile(r"(?<![\w$.-])(?:const|let|var)\s+$")
_CALL = re.compile(r"[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\s*\(")
_UNQUOTED = re.compile(r"[^\s>]+")
_PROPERTY_NAME = re.compile(r"\s*-*[A-Za-z][\w-]*\s*")
_STRAY = re.compile(r"['\"`\\{}]|\$\{")
# Quotes in a value are read in two shapes only, so script glue between
# string pieces (``', c, '`` in ``['<i style="background:', c,
# '">'].join('')``) is never taken for CSS: a ``url()`` whose quoted
# argument holds no quote, comma, space, ``$``, ``+``, brace, backslash or
# backtick, and a ``font-family`` list of quoted plain names
# (``'Courier New'``) followed by bare names (``monospace``). Any other
# quote leaves the style unread.
_CSS_URL = re.compile(
    r"url\(\s*(?:'[^'\"`\\$+{}, ]*'|\"[^'\"`\\$+{}, ]*\"|[^'\"`\\$+{}()\s]*)\s*\)"
)
_FONT_NAME = r"(?:'[\w-]+(?: [\w-]+)*'|\"[\w-]+(?: [\w-]+)*\")"
_BARE_NAME = r"-?[A-Za-z_][\w-]*"
_FONT_FAMILY = re.compile(
    rf"\s*(?:{_FONT_NAME}(?:\s*,\s*{_FONT_NAME})*(?:\s*,\s*{_BARE_NAME})*"
    rf"|{_BARE_NAME}(?:\s*,\s*{_BARE_NAME})*)\s*"
)
# A value DYNAMIC_INLINE_STYLES may give for an expression that always
# yields a number (``width: ${pct}%``); read as ``0``, and never allowed in
# a property that takes a colour.
NUMBER = "<number>"
_COLOUR_PROPERTY = re.compile(
    r"color|background|border|outline|shadow|fill|stroke"
)


def _skip_string(text, start):
    """The index after the JavaScript string literal opening at
    ``text[start]`` (``'``, ``"`` or a template literal, whose ``${...}``
    are skipped), or None when it does not close."""
    quote, index = text[start], start + 1
    while index < len(text):
        char = text[index]
        if char == "\\":
            index += 2
            continue
        if char == quote:
            return index + 1
        if quote == "`" and text.startswith("${", index):
            index = _skip_expression(text, index)
            if index is None:
                return None
            continue
        if char == "\n" and quote != "`":
            return None
        index += 1
    return None


def _skip_expression(text, start):
    """The index after the ``${...}`` opening at ``text[start]``, counting
    nested braces and skipping string and template literals, or None when
    it does not close. Only used to find where an expression ends; what
    the expression evaluates to is never read."""
    depth, index = 1, start + 2
    while index < len(text):
        char = text[index]
        if char in "'\"`":
            index = _skip_string(text, index)
            if index is None:
                return None
            continue
        if char == "{":
            depth += 1
        elif char == "}":
            depth -= 1
            if depth == 0:
                return index + 1
        index += 1
    return None


def _read_attribute(text, start):
    """``(offset, value)`` of the quoted attribute value opening at
    ``text[start]`` (``${...}`` expressions and template tags inside it
    skipped whole, whatever quotes they hold), or None when it does not
    close."""
    close = text[start : start + 2] if text[start] == "\\" else text[start]
    index = begin = start + len(close)
    while index < len(text):
        if text.startswith("${", index):
            index = _skip_expression(text, index)
            if index is None:
                return None
            continue
        tag = _JINJA.match(text, index)
        if tag:
            index = tag.end()
            continue
        if text.startswith(close, index):
            return begin, text[begin:index]
        index += 1
    return None


def _read_write(text, start):
    """``(offset, value)`` of a style write's right-hand side when it is a
    single string literal (a template literal's ``${...}`` kept in the
    value), followed by ``;``, ``,``, ``)``, ``}`` or the end of the file;
    None for anything else (a variable, a call, a concatenation)."""
    if start >= len(text) or text[start] not in "'\"`":
        return None
    end = _skip_string(text, start)
    if end is None or not re.match(r"\s*(?:[;,)}]|$)", text[end:]):
        return None
    return start + 1, text[start + 1 : end - 1]


def _style_sites(text):
    """``(offset, read)`` for each inline style in ``text`` (see the comment
    above): ``read`` is ``(value offset, raw value)``, or None when the
    style cannot be read."""
    for match in _STYLE_ATTR.finditer(text):
        if _VARIABLE_DECLARATION.search(
            text[max(0, match.start() - 12) : match.start()]
        ) and _CALL.match(text, match.end()):
            continue
        start = match.end()
        if text.startswith(("${", "{{", "{%"), start):
            yield match.start(), None
        elif text.startswith(('"', "'", "`", '\\"', "\\'"), start):
            yield match.start(), _read_attribute(text, start)
        else:
            bare = _UNQUOTED.match(text, start)
            yield match.start(), bare and (start, bare.group())
    for match in _STYLE_WRITE.finditer(text):
        yield match.start(), _read_write(text, match.end())


def _expression_spans(value):
    """``[(start, end, source)]`` for each ``${...}`` and template tag in
    ``value`` (``source`` with its whitespace collapsed), or None when an
    expression does not close."""
    found, index = [], 0
    while index < len(value):
        if value.startswith("${", index):
            end = _skip_expression(value, index)
            if end is None:
                return None
            found.append(
                (index, end, " ".join(value[index + 2 : end - 1].split()))
            )
            index = end
            continue
        tag = _JINJA.match(value, index)
        if tag:
            found.append((index, tag.end(), " ".join(tag.group().split())))
            index = tag.end()
            continue
        index += 1
    return found


def _split_style(value):
    """``[(offset, part)]``: ``value`` split on the ``;`` outside
    expressions, template tags and parentheses, or None when an expression
    does not close."""
    parts, depth, index, begin = [], 0, 0, 0
    while index < len(value):
        if value.startswith("${", index):
            index = _skip_expression(value, index)
            if index is None:
                return None
            continue
        tag = _JINJA.match(value, index)
        if tag:
            index = tag.end()
            continue
        char = value[index]
        depth += char == "("
        depth -= char == ")"
        if char == ";" and depth == 0:
            parts.append((begin, value[begin:index]))
            begin = index + 1
        index += 1
    parts.append((begin, value[begin:]))
    return parts


def _style_declarations(text, offset, value):
    """``[(property, value, spans)]`` for an inline style's raw ``value``
    starting at ``text[offset]``, or None when it cannot be read (see the
    comment above). ``spans`` holds ``(start, end, source, line)`` for each
    expression or template tag in the declaration's value."""
    parts = _split_style(value)
    if parts is None:
        return None
    decls = []
    for begin, part in parts:
        if not part.strip():
            continue
        name, sep, rest = part.partition(":")
        if not sep or not _PROPERTY_NAME.fullmatch(name):
            return None
        spans = _expression_spans(rest)
        if spans is None:
            return None
        literal = rest
        for start, end, _ in reversed(spans):
            literal = literal[:start] + " " + literal[end:]
        plain = _CSS_URL.sub(" ", literal)
        if name.strip().lower() == "font-family" and _FONT_FAMILY.fullmatch(
            plain
        ):
            plain = ""
        if _STRAY.search(plain):
            return None
        base = offset + begin + len(name) + 1
        decls.append(
            (
                name.strip().lower(),
                rest,
                [
                    (start, end, source, text.count("\n", 0, base + start) + 1)
                    for start, end, source in spans
                ],
            )
        )
    return decls


def _merge_gradient(decls):
    """A gradient ``background-image`` paints over the background colour:
    both are the fill, read as ``background-color``."""
    image = decls.get("background-image")
    if image and _COLOR_STOP.search(re.sub(r"url\([^()]*\)", " ", image)):
        under = decls.get("background-color", decls.get("background"))
        decls["background-color"] = f"{image} {under or ''}".strip()
    return decls


def _style_variants(name, decls, allowlist):
    """The declaration sets an inline style's listed expressions can
    produce, or None when one of its expressions is not listed with that
    exact text (``test_dynamic_inline_styles_are_allowlisted`` reports it)
    or the combinations exceed _MAX_VARIANTS (reported as unread). Each
    listed (file, line, property) is one axis; listed entries sharing a
    pair label are taken value by value."""
    axes, seen = {}, set()
    for prop, _, spans in decls:
        for _, _, source, line in spans:
            key = (name, line, prop)
            entry = allowlist.get(key)
            if entry is None or entry[0] != source:
                return None
            if key not in seen:
                seen.add(key)
                axes.setdefault(entry[2] or key, []).append(key)
    combos = [{}]
    for keys in axes.values():
        widths = {len(allowlist[key][1]) for key in keys}
        if len(widths) != 1:
            return None
        width = widths.pop()
        combos = [
            {**combo, **{key: allowlist[key][1][i] for key in keys}}
            for combo in combos
            for i in range(width)
        ]
        if len(combos) > _MAX_VARIANTS:
            return []
    variants = []
    for combo in combos:
        variant = {}
        for prop, rest, spans in decls:
            value = rest
            for start, end, _, line in reversed(spans):
                chosen = combo[(name, line, prop)]
                value = (
                    value[:start]
                    + ("0" if chosen == NUMBER else chosen)
                    + value[end:]
                )
            variant[prop] = " ".join(value.split())
        variants.append(_merge_gradient(variant))
    return variants


# This write preserves document-authored KaTeX colors, not application
# palette choices. Its CSSOM declarations pass the positive property/value
# policy tested in tests/js/security/markdown-render-xss.test.js and the
# real-browser reparse suite. Do not pretend these arbitrary author colors
# are finite theme variants. Only this exact guarded source block is outside
# the application-palette sweep; new or changed whole-style writes still fail.
_AUTHORED_MATH_STYLE_SITE = ("static/js/services/ui.js", 850)
_AUTHORED_MATH_STYLE_CONTEXT = (
    "const safeStyle = getSafeKatexInlineStyle(el);",
    "if (!safeStyle) {",
    "el.removeAttribute('style');",
    "} else {",
    "el.setAttribute('style', safeStyle);",
    "}",
)


def _is_authored_math_style(text, name, line):
    if (name, line) != _AUTHORED_MATH_STYLE_SITE:
        return False
    end = line + 1  # the closing brace directly after the reviewed write
    context = text.splitlines()[end - len(_AUTHORED_MATH_STYLE_CONTEXT) : end]
    return (
        tuple(part.strip() for part in context) == _AUTHORED_MATH_STYLE_CONTEXT
    )


def _scan_styles(text, name, allowlist=None):
    """``(rules, unread, found)`` for the inline styles in one file's
    ``text``: the pseudo-rules (``style@L<line>``, or ``style@L<line>#<n>``
    per combination of listed expression values), ``{"style@L<line>":
    reason}`` for each style that cannot be read, and ``{(name, line,
    property): {expression, ...}}`` for every expression found."""
    allowlist = DYNAMIC_INLINE_STYLES if allowlist is None else allowlist
    rules, unread, found = [], {}, {}
    for offset, read in _style_sites(text):
        line = text.count(chr(10), 0, offset) + 1
        label = f"style@L{line}"
        if read is None and _is_authored_math_style(text, name, line):
            continue
        decls = read and _style_declarations(text, *read)
        if decls is None:
            unread[label] = "cannot be read as literal declarations"
            continue
        for prop, _, spans in decls:
            for _, _, source, line in spans:
                found.setdefault((name, line, prop), set()).add(source)
        if not any(spans for _, _, spans in decls):
            static = {prop: " ".join(rest.split()) for prop, rest, _ in decls}
            rules.append((label, _merge_gradient(static)))
            continue
        variants = _style_variants(name, decls, allowlist)
        if variants == []:
            unread[label] = f"more than {_MAX_VARIANTS} combinations"
        elif variants:
            rules += [(f"{label}#{n}", v) for n, v in enumerate(variants)]
    return rules, unread, found


def _markup_files():
    """``[(key, path)]`` for the templates and scripts the inline-style and
    anchor sweeps read, each keyed by its path under the web root
    (``templates/pages/metrics.html``,
    ``static/templates/followup_modal.html``, ``static/js/pages/news.js``):
    file names repeat across directories (``components/news.js`` and
    ``pages/news.js``, a fragment and a Jinja template both named
    ``followup_modal.html``), and a key shared by two files would let one
    file's styles, expressions or anchors overwrite the other's."""
    return [
        (path.relative_to(WEB_ROOT).as_posix(), path)
        for root, pattern in (
            (WEB_ROOT / "templates", "*.html"),
            (WEB_ROOT / "static" / "templates", "*.html"),
            (WEB_ROOT / "static" / "js", "*.js"),
        )
        for path in sorted(root.rglob(pattern))
    ]


@functools.lru_cache(maxsize=None)
def _inline_scan():
    """``(sheets, unread, found)``: the inline-style pseudo-rules of each
    template and script, ``{(file, "style@L<line>"): reason}`` for each
    style that cannot be read, and ``{(file, line, property):
    {expression, ...}}`` for every expression in an inline style."""
    sheets, unread, found = [], {}, {}
    for name, path in _markup_files():
        rules, missing, expressions = _scan_styles(
            path.read_text(encoding="utf-8"), name
        )
        unread.update(((name, label), why) for label, why in missing.items())
        found.update(expressions)
        if rules:
            sheets.append((name, rules))
    return sheets, unread, found


def _inline_styles():
    """``style`` attributes and whole-style writes in templates and scripts,
    one pseudo-rule per style named after its line (one per combination of
    listed expression values, ``style@L<line>#<n>``)."""
    yield from _inline_scan()[0]


def _allow_key(name, selector):
    """The allowlist key of a rule: an expanded inline style
    (``style@L12#1``) is listed by its attribute (``style@L12``)."""
    return name, re.sub(r"^(style@L\d+)#\d+$", r"\1", selector)


# Every expression in an inline style: (file, line of the ``${``,
# property) -> (expression text, whitespace collapsed; every value it can
# take; pair label or None). Entries on one style sharing a pair label are
# taken value by value (the fill and ink of one ternary); all others are
# combined. NUMBER marks an expression that always yields a number. Each
# entry names the source its values were read from; a reviewer re-reads
# it whenever the line or the expression changes (the entry then fails as
# unlisted or stale). An entry pins the use site only: a change to what the
# expression evaluates to, made elsewhere (a new branch in the helper or
# ternary it names), fails only when a listed value disappears from the
# file (``test_dynamic_inline_style_values_are_literals``).
_STATE_COLORS = (
    "var(--success-color, #2ecc71)",
    "var(--error-color, #fa5c7c)",
    "var(--accent-primary, #0b84ff)",
    "var(--bg-tertiary, #444)",
)
DYNAMIC_INLINE_STYLES = {
    # benchmark.html, createExpandableText: the two halves of an
    # expandable answer, one shown and one hidden (keys carry the lines).
    ("templates/pages/benchmark.html", 1444, "display"): (
        "isExpanded ? 'none' : 'inline'",
        ("none", "inline"),
        None,
    ),
    ("templates/pages/benchmark.html", 1445, "display"): (
        "isExpanded ? 'inline' : 'none'",
        ("inline", "none"),
        None,
    ),
    # utilColor selects one of three fixed CSS variables. utilPct uses
    # Math.round(...) or 0, and truncationPercent is a finite number before
    # either value is interpolated into these styles.
    ("static/js/components/context-overflow.js", 582, "color"): (
        "utilColor",
        ("var(--error-color)", "var(--warning-color)", "var(--success-color)"),
        None,
    ),
    ("static/js/components/context-overflow.js", 585, "background"): (
        "utilColor",
        ("var(--error-color)", "var(--warning-color)", "var(--success-color)"),
        None,
    ),
    ("static/js/components/context-overflow.js", 585, "width"): (
        "Math.min(utilPct, 100)",
        (NUMBER,),
        None,
    ),
    ("static/js/components/context-overflow.js", 590, "width"): (
        "Math.min(truncationPercent, 100)",
        (NUMBER,),
        None,
    ),
    # details.js L712: an inline ternary of two literals.
    ("static/js/components/details.js", 712, "color"): (
        "item.success_status === 'success' ? 'var(--success-color)' "
        ": 'var(--error-color)'",
        ("var(--success-color)", "var(--error-color)"),
        None,
    ),
    # journal_quality.html L712-713: ``const pillBg = ok ?
    # 'var(--success-color)' : 'var(--error-color)'`` and ``const pillColor
    # = ok ? 'var(--text-on-success)' : 'var(--text-on-error)'`` in the same
    # arrow function, on the same ``ok``: paired.
    ("templates/pages/journal_quality.html", 723, "background"): (
        "pillBg",
        ("var(--success-color)", "var(--error-color)"),
        "pill",
    ),
    ("templates/pages/journal_quality.html", 723, "color"): (
        "pillColor",
        ("var(--text-on-success)", "var(--text-on-error)"),
        "pill",
    ),
    # journal_quality.html L814 ``function _stateColor(state)``: four
    # ``return`` literals (success, error, running, pending). L821-825
    # sets ``width`` to 0, 100, a number ``row.percent`` or 5.
    ("templates/pages/journal_quality.html", 870, "color"): (
        "_stateColor(row.state)",
        _STATE_COLORS,
        None,
    ),
    ("templates/pages/journal_quality.html", 873, "background"): (
        "_stateColor(row.state)",
        _STATE_COLORS,
        None,
    ),
    ("templates/pages/journal_quality.html", 873, "width"): (
        "width",
        (NUMBER,),
        None,
    ),
    # link_analytics.html L281: ``colors.background[index]`` from
    # generateChartColors (L453), which only copies its ten baseColors.
    ("templates/pages/link_analytics.html", 284, "border-left"): (
        "cardColor",
        tuple(
            f"rgba({rgb}, 0.8)"
            for rgb in (
                "107, 70, 193",
                "245, 158, 11",
                "59, 130, 246",
                "16, 185, 129",
                "239, 68, 68",
                "139, 69, 19",
                "255, 192, 203",
                "128, 128, 128",
                "255, 165, 0",
                "75, 0, 130",
            )
        ),
        None,
    ),
    # metrics.html L989: an inline ternary of two literals. Keyed at L975
    # until PR 7330 inserted a 14-line metric-write-health warning block
    # above it; the expression itself is unchanged.
    ("templates/pages/metrics.html", 989, "color"): (
        "item.success_status === 'success' ? 'green' : 'red'",
        ("green", "red"),
        None,
    ),
    # news.js: ``Math.max(0, Math.min(100, ...))`` and ``Number(...) ||
    # 10`` are numbers.
    ("static/js/pages/news.js", 1286, "width"): (
        "Math.max(0, Math.min(100, (Number(item.impact_score) || 0) * 10))",
        (NUMBER,),
        None,
    ),
    ("static/js/pages/news.js", 2949, "width"): (
        "Number(statusData.progress) || 10",
        (NUMBER,),
        None,
    ),
}


def test_markup_files_have_distinct_keys():
    """Each scanned template and script has its own key, so no file's
    inline styles, expressions (``_inline_scan``), text (``_markup_text``)
    or anchors (``_anchor_class_sets``) are overwritten by another file's:
    the scripts include file names that repeat across directories
    (``components/news.js`` and ``pages/news.js``)."""
    files = _markup_files()
    keys = [key for key, _ in files]
    assert len(keys) == len(set(keys)), sorted(
        key for key in set(keys) if keys.count(key) > 1
    )
    names = [path.name for _, path in files]
    assert len(set(names)) < len(names), "no repeated file name is scanned"
    assert {
        "static/js/components/news.js",
        "static/js/pages/news.js",
    } <= set(keys)
    assert _markup_text("static/js/pages/news.js") != _markup_text(
        "static/js/components/news.js"
    )


def _markup_text(name):
    return dict(_markup_files())[name].read_text(encoding="utf-8")


def test_dynamic_inline_styles_are_allowlisted():
    """Every inline style is read as literal declarations or fails, and
    every expression or template tag in one is listed in
    DYNAMIC_INLINE_STYLES at its exact (file, line, property) with the same
    expression text; a listed entry no longer found fails as stale."""
    _, unread, found = _inline_scan()
    assert not unread, unread
    unlisted = {
        key: sorted(sources)
        for key, sources in found.items()
        if len(sources) != 1
        or DYNAMIC_INLINE_STYLES.get(key, (None,))[0] not in sources
    }
    assert not unlisted, f"list these with their values: {unlisted}"
    stale = {
        key: entry[0]
        for key, entry in DYNAMIC_INLINE_STYLES.items()
        if found.get(key) != {entry[0]}
    }
    assert not stale, f"entries no longer found: {stale}"


def test_dynamic_inline_style_values_are_literals():
    """Each DYNAMIC_INLINE_STYLES entry: its source line still holds the
    expression; each value is plain CSS text that cannot add a
    declaration (no ``;``, brace, quote, backslash or expression) and,
    unless it is NUMBER, still appears as a string literal in that file;
    NUMBER never stands for a colour; and entries sharing a pair label
    have the same number of values. The values' completeness is the
    reviewer's reading of the named source, not something this checks."""
    problems = []
    pairs = {}
    for (name, line, prop), (
        expression,
        values,
        pair,
    ) in DYNAMIC_INLINE_STYLES.items():
        text = _markup_text(name)
        source_line = " ".join(text.splitlines()[line - 1].split())
        if f"${{{expression}}}" not in source_line:
            problems.append(f"{name}:{line} no longer holds ${{{expression}}}")
        if not values:
            problems.append(f"{name}:{line} {prop}: no values")
        for value in values:
            if value == NUMBER:
                if _COLOUR_PROPERTY.search(prop):
                    problems.append(f"{name}:{line} {prop}: NUMBER colour")
                continue
            if re.search(r"[;{}'\"`\\$<>]", value) or not value.strip():
                problems.append(f"{name}:{line} {prop}: {value!r} not plain")
            if f"'{value}'" not in text and f'"{value}"' not in text:
                problems.append(f"{name}:{line} {prop}: {value!r} not in file")
        if pair:
            pairs.setdefault((name, pair), set()).add(len(values))
    problems += [
        f"{key}: pair widths {w}" for key, w in pairs.items() if len(w) != 1
    ]
    assert not problems, problems


def test_authored_math_style_write_matches_reviewed_source():
    name, line = _AUTHORED_MATH_STYLE_SITE
    source = _markup_text(name)
    assert _is_authored_math_style(source, name, line)
    assert any(
        source.count("\n", 0, offset) + 1 == line and read is None
        for offset, read in _style_sites(source)
    )
    assert f"style@L{line}" not in _scan_styles(source, name)[1]


@pytest.mark.parametrize(
    "change", ["validator", "value", "second-write", "file"]
)
def test_authored_math_style_exception_rejects_unreviewed_writes(change):
    name, line = _AUTHORED_MATH_STYLE_SITE
    source = _markup_text(name)
    if change == "validator":
        source = source.replace(
            "const safeStyle = getSafeKatexInlineStyle(el);",
            "const safeStyle = el.getAttribute('style');",
        )
    elif change == "value":
        source = source.replace(
            "el.setAttribute('style', safeStyle);",
            "el.setAttribute('style', unfilteredStyle);",
        )
    elif change == "second-write":
        source += "\nel.setAttribute('style', safeStyle);\n"
        line = len(source.splitlines())
    else:
        name = "static/js/services/other.js"
    assert f"style@L{line}" in _scan_styles(source, name)[1]


def test_inline_style_scanner_over_reports():
    """Every shape the scanner does not model fails rather than being read
    as a style without a fill, and every expression is reported for the
    allowlist whatever property it sits in; literal styles and listed
    expressions still become rules."""
    unread_cases = {
        "whole attribute": '<i style="${st}">',
        "bare expression": "<i style=${st}>",
        "jinja attribute": "<i style={{ st }}>",
        "declaration from expression": '<i style="color: red; ${st}">',
        "expression in name": "<i style=\"${'back' + 'ground'}: red\">",
        "concatenation": "'<i style=\"background: ' + c + '; color: #fff\">'",
        "escaped concatenation": '"<i style=\\"background: " + c + "\\">"',
        "unquoted concatenation": "'<i style=background:' + c + '>'",
        "unclosed expression": '<i style="background: ${c; color: white">',
        "unclosed attribute": '<i style="background: red',
        "cssText variable": "el.style.cssText = css;",
        "cssText concatenation": "el.style.cssText = 'background: ' + c;",
        "cssText template concatenation": "el.style.cssText = `a: b` + c;",
        "style property assigned": "el.style = make();",
        "setAttribute variable": "el.setAttribute('style', css);",
        "setAttribute uppercase": 'el.setAttribute("STYLE", css);',
        # Script glue between string pieces is not a CSS string.
        "array join": "['<i style=\"background:', c, '\">'].join('')",
        "array join, spaced": "['<i style=\"background: ' , c , '\">']",
        "array join, member": "['<i style=\"background:', s.c, '\">']",
        "multi-argument call": (
            "fmt('<i style=\"background: ', c, '; color: white\">')"
        ),
        "multi-argument escaped quote": (
            'fmt("<i style=\\"background: ", c, "; color: white\\">")'
        ),
        "multi-argument escaped single quote": (
            "fmt('<i style=\\'background: ', c, '\\'>')"
        ),
        "name between strings": "<i style=\"font-family: 'a', c, 'b'\">",
        "font-family glue": "['<i style=\"font-family: ', c, '\">']",
        "glue inside url()": "['<i style=\"background: url(', c, ')\">']",
        "glue inside url(), double quotes": (
            'fmt("<i style=\'background: url(", c, ")\'>")'
        ),
        "glue after url()": (
            "['<i style=\"background: url(a.png) ', c, '\">']"
        ),
        "quote outside font-family": "<i style=\"content: 'a'\">",
        # An attribute ending in "var" is not a variable declaration.
        "attribute named data-var": '<i data-var style="${st}">',
        "data-var, call-shaped value": "<i data-var style=f(x)>",
    }
    for case, markup in unread_cases.items():
        rules, unread, _ = _scan_styles(markup, "case.js", {})
        assert unread and not rules, (case, rules)
    dynamic_cases = {
        "fill": ('<i style="background: ${pick({ ok })}">', 1, "background"),
        "non-fill property": (
            '<i style="width: ${w}; color: red">',
            1,
            "width",
        ),
        "uppercase": ('<i STYLE="color: ${c}">', 1, "color"),
        "escaped quote": ('"<i style=\\"color: ${c}\\">"', 1, "color"),
        "single quote": ("`<i style='color: ${c}'>`", 1, "color"),
        "element style": ("el.style = `color: ${c}`;", 1, "color"),
        "cssText": ("el.style.cssText = `\n color: ${c};`;", 2, "color"),
        "cssText append": ("el.style.cssText += `color: ${c}`;", 1, "color"),
        "setAttribute": (
            "el.setAttribute('style', `color: ${c}`);",
            1,
            "color",
        ),
        "jinja value": ('<i style="color: {{ c }}">', 1, "color"),
        "nested quote": (
            '<i style="background: ${ok ? "var(--a)" : "var(--b)"}">',
            1,
            "background",
        ),
        "after data-var": ('<i data-var style="color: ${c}">', 1, "color"),
        "after an attribute named var": (
            '<i var style="color: ${c}">',
            1,
            "color",
        ),
    }
    for case, (markup, line, prop) in dynamic_cases.items():
        rules, unread, found = _scan_styles(markup, "case.js", {})
        assert not rules and not unread, (case, rules, unread)
        assert set(found) == {("case.js", line, prop)}, (case, found)
    # A variable named style set from a call is a script variable, not a
    # style.
    for markup in (
        "const style = getComputedStyle(el);",
        "\tlet style = document.createElement('style');",
    ):
        assert _scan_styles(markup, "x.js", {}) == ([], {}, {}), markup
    # Literal styles are read, writes included.
    rules, unread, _ = _scan_styles(
        '<b style="background-image: linear-gradient(#000, #111); '
        "color: white; background-image: url('a.png') \">\n"
        "el.style.cssText = 'background: #000; color: #fff';",
        "x.js",
        {},
    )
    assert not unread, unread
    assert [label for label, _ in rules] == ["style@L1", "style@L2"]
    assert rules[1][1] == {"background": "#000", "color": "#fff"}
    # Quoted font names stay literal CSS strings.
    rules, unread, _ = _scan_styles(
        "<pre style=\"font-family: 'Courier New', monospace\">\n"
        "<pre style=\"font-family: 'Monaco', 'Menlo', 'Ubuntu Mono'\">",
        "x.js",
        {},
    )
    assert not unread, unread
    assert [d["font-family"] for _, d in rules] == [
        "'Courier New', monospace",
        "'Monaco', 'Menlo', 'Ubuntu Mono'",
    ]
    # Listed expressions expand: paired entries value by value, the rest
    # combined, and an entry with other expression text does not match.
    markup = '<i style="background: ${bg}; color: ${ink}; width: ${w}%">'
    listed = {
        ("x.js", 1, "background"): ("bg", ("var(--a)", "var(--b)"), "p"),
        ("x.js", 1, "color"): ("ink", ("var(--on-a)", "var(--on-b)"), "p"),
        ("x.js", 1, "width"): ("w", (NUMBER,), None),
    }
    rules, unread, _ = _scan_styles(markup, "x.js", listed)
    assert not unread
    assert [
        (label, d["background"], d["color"], d["width"]) for label, d in rules
    ] == [
        ("style@L1#0", "var(--a)", "var(--on-a)", "0%"),
        ("style@L1#1", "var(--b)", "var(--on-b)", "0%"),
    ]
    listed[("x.js", 1, "color")] = ("ink2", ("var(--on-a)", "var(--on-b)"), "p")
    assert _scan_styles(markup, "x.js", listed)[0] == []
    listed[("x.js", 1, "color")] = ("ink", ("var(--on-a)", "var(--on-b)"), None)
    assert len(_scan_styles(markup, "x.js", listed)[0]) == 4


def test_inline_style_scan_finds_the_known_expressions():
    """The scan reaches the expression styles reviewers found (an empty or
    narrowed scan would make the allowlist test pass vacuously), and
    expands the listed fills into rules the sweeps check."""
    sheets, _, found = _inline_scan()
    assert {
        ("templates/pages/journal_quality.html", 723, "background"),
        ("templates/pages/journal_quality.html", 873, "background"),
        ("static/js/components/context-overflow.js", 585, "background"),
        ("static/js/components/details.js", 712, "color"),
        ("templates/pages/metrics.html", 989, "color"),
    } <= set(found), sorted(found)
    expanded = {
        _allow_key(name, selector)
        for name, rules in sheets
        for selector, _ in rules
        if "#" in selector
    }
    assert {
        ("templates/pages/journal_quality.html", "style@L723"),
        ("templates/pages/journal_quality.html", "style@L873"),
        ("static/js/components/context-overflow.js", "style@L585"),
    } <= expanded, sorted(expanded)


def _sheets():
    for name, css in _stylesheets():
        yield name, list(_parse_rules(css))
    yield from _inline_styles()


def test_accent_fill_sweep_reads_css_and_templates():
    """An empty glob would make the sweep below pass vacuously."""
    names = {name for name, _ in _stylesheets()}
    assert {
        "news-enhanced.css",
        "templates/auth/login.html",
        "templates/pages/note_detail.html",
    } <= names
    inline = {name for name, _ in _inline_styles()}
    assert {
        "templates/pages/link_analytics.html",
        "static/js/collection_upload.js",
        "static/templates/followup_modal.html",
    } <= inline


# Accent fills that draw no text, so they need no ink.
_BAR = "progress or score bar; its label sits outside the fill"
_STRIPE = "decorative accent stripe; content: '' draws no text"
NON_TEXT_ACCENT_FILLS = {
    ("benchmark.css", ".ldr-progress-fill"): _BAR,
    ("templates/pages/benchmark_simple.html", ".ldr-progress-fill"): _BAR,
    ("library.css", ".ldr-progress-fill"): _BAR,
    ("semantic-search.css", ".ldr-progress-track .ldr-progress-bar"): _BAR,
    ("templates/pages/zotero.html", ".ldr-zotero-progress-fill"): _BAR,
    ("collection_details.css", ".ldr-progress-fill"): _BAR,
    ("link_analytics.css", ".ldr-source-type-fill.ldr-academic"): _BAR,
    ("news.css", ".ldr-active-research-card .ldr-progress-bar"): _BAR,
    ("star_reviews.css", ".ldr-rating-fill"): _BAR,
    ("styles.css", ".ldr-progress-fill"): _BAR,
    ("styles.css", ".ldr-detail-progress-fill"): _BAR,
    ("static/js/collection_upload.js", "style@L588"): _BAR,
    ("static/js/collection_upload.js", "style@L658"): _BAR,
    # _stateColor(): success, error, accent or the track colour.
    ("templates/pages/journal_quality.html", "style@L873"): _BAR,
    ("collection_details.css", ".ldr-stat-card::before"): _STRIPE,
    ("collection_details.css", ".ldr-document-item::before"): _STRIPE,
    ("news-enhanced.css", ".ldr-news-item::before"): _STRIPE,
    ("news.css", ".ldr-active-research-card::before"): _STRIPE,
    ("notes.css", ".ldr-note-card::before"): _STRIPE,
    ("mobile-navigation.css", ".ldr-mobile-nav-tab.active::before"): _STRIPE,
    (
        "mobile-navigation.css",
        ".ldr-mobile-nav-tab::after",
    ): "tap ripple; content: '' draws no text",
    (
        "mobile-navigation.css",
        ".ldr-mobile-sheet-item::after",
    ): "tap ripple; content: '' draws no text",
    (
        "collection_details.css",
        ".ldr-documents-list::-webkit-scrollbar-thumb",
    ): "scrollbar thumb",
    (
        "collections.css",
        ".ldr-collections-grid::-webkit-scrollbar-thumb",
    ): "scrollbar thumb",
    (
        "collections.css",
        ".ldr-collections-grid::-webkit-scrollbar-thumb:hover",
    ): "scrollbar thumb",
    (
        "document_details.css",
        ".ldr-content-preview::-webkit-scrollbar-thumb",
    ): "scrollbar thumb",
    (
        "news.css",
        ".ldr-subscriptions-horizontal::-webkit-scrollbar-thumb",
    ): "scrollbar thumb",
    (
        "settings.css",
        ".ldr-settings-tabs::-webkit-scrollbar-thumb",
    ): "scrollbar thumb",
    (
        "mobile-responsive.css",
        'input[type="range"]::-webkit-slider-thumb',
    ): "range slider thumb",
    (
        "mobile-responsive.css",
        'input[type="range"]::-moz-range-thumb',
    ): "range slider thumb",
    (
        "news.css",
        ".ldr-toggle-switch input:checked + .ldr-toggle-slider",
    ): "toggle track; its knob is the ::after with its own fill",
    (
        "benchmark.css",
        ".ldr-toggle-switch.ldr-active",
    ): "toggle track; its knob is the ::after with its own fill",
    # The checkmark is the ::after, checked above as ink on this fill.
    (
        "settings.css",
        '.ldr-checkbox-label input[type="checkbox"]:checked',
    ): "checked checkbox box",
    (
        "templates/auth/login.html",
        '.ldr-checkbox-label input[type="checkbox"]:checked',
    ): "checked native checkbox box (no glyph rule)",
    (
        "templates/auth/register.html",
        '.ldr-checkbox-label input[type="checkbox"]:checked',
    ): "checked native checkbox box (no glyph rule)",
}


def _ink(value):
    return _drop_fallbacks(value.replace("!important", "")).strip()


def test_accent_fills_use_accent_ink():
    """The converse of the check above: text drawn on an accent fill uses
    the accent ink. A state or modifier rule inherits the fill or the ink
    from its base selector in the same sheet (``.btn-primary:hover`` keeps
    the ``.btn-primary`` fill; ``.ldr-nav-link-btn.ldr-nav-accent`` keeps
    the ``.ldr-nav-link-btn`` ink), so the pair is resolved through that
    chain. An accent fill whose chain sets no ink fails too (its text would
    take an ink from a co-class or parent the sweep cannot see) unless it
    is listed in NON_TEXT_ACCENT_FILLS. Inline ``style`` attributes in
    templates and scripts count as rules of their own. A fill through an
    alias of the accent (``var(--primary-color)``, see ``_accent_aliases``)
    is an accent fill. Gradient text (background-clip: text) draws no
    fill.

    Limits: the chain only climbs from a selector to its own base, so a
    base-state rule that also matches a modifier (``.x:hover`` restyling a
    hovered ``.x.active``) is not applied to it, and rules from other sheets
    and type selectors (``a:hover``, a theme's ``[data-theme] a``) are not
    seen; ``test_filled_anchors_keep_readable_ink`` covers those for
    filled controls on links."""
    offenders = []
    allowed_seen = set()
    for name, rules in _sheets():
        fills, inks, selectors = {}, {}, []
        for selector_list, decls in rules:
            if selector_list.startswith("@"):
                continue
            clipped = {"background-clip", "-webkit-background-clip"} & set(
                decls
            )
            background = decls.get("background-color", decls.get("background"))
            for selector in (s.strip() for s in selector_list.split(",")):
                if background:
                    fills[selector] = None if clipped else background
                if "color" in decls:
                    inks[selector] = decls["color"]
                if background or "color" in decls:
                    selectors.append(selector)
        for selector in dict.fromkeys(selectors):
            chain = _selector_chain(selector)
            fill = next((fills[s] for s in chain if s in fills), None)
            ink = next((inks[s] for s in chain if s in inks), None)
            pseudo = _PSEUDO_ELEMENT.search(selector)
            if pseudo and fill is None and selector in inks:
                # An unfilled ::before/::after with its own ink draws on the
                # originating element's fill (the settings checkmark).
                origin = _selector_chain(selector[: pseudo.start()])
                fill = next((fills[s] for s in origin if s in fills), None)
            ancestor = re.match(r"(.*\S)\s*[ >]\s*[^ >+~]+$", selector)
            if ancestor and fill is None and selector in inks:
                # Unfilled text inside an accent-filled ancestor
                # (``.dropdown-item.active small``).
                origin = _selector_chain(ancestor.group(1))
                fill = next((fills[s] for s in origin if s in fills), None)
            if not fill or not _is_accent_fill(fill):
                continue
            if ink is not None and _ink(ink) == ACCENT_INK:
                continue
            for allowlist in (DEAD_ACCENT_RULES, NON_TEXT_ACCENT_FILLS):
                if _allow_key(name, selector) in allowlist:
                    allowed_seen.add(_allow_key(name, selector))
                    break
            else:
                offenders.append(f"{name} {selector}: {ink} on {fill}")
    assert not offenders, offenders
    stale = (DEAD_ACCENT_RULES | set(NON_TEXT_ACCENT_FILLS)) - allowed_seen
    assert not stale, f"allowlist entries no longer needed: {sorted(stale)}"


# Inks chosen for one filled token. A rule that pairs one of them with a
# fill, or a modifier or state rule that swaps the fill while keeping the
# ink of its base (``.ldr-log-indicator--has-error`` keeps
# ``.ldr-log-indicator``'s accent ink), must still read there.
FILL_INKS = frozenset(
    {
        ACCENT_INK,
        "var(--text-on-error)",
        "var(--text-on-warning)",
        "var(--text-on-success)",
    }
)
# ``.block--modifier`` is a modifier of ``.block``: script adds it beside
# the block's class, so it takes the block's ink.
_BEM_MODIFIER = re.compile(r"\.([\w-]+?)--[\w-]+")


def _palettes():
    """``[(name, props)]`` for the bare ``:root`` palette and every theme."""
    root = _root_defaults()
    palettes = [(":root", root)]
    for path in THEME_FILES:
        props = dict(root)
        props.update(_custom_properties(path.read_text(encoding="utf-8")))
        palettes.append((path.stem, props))
    return palettes


@pytest.mark.parametrize("kind", ["error", "warning", "success"])
def test_status_ink_contrast(kind):
    """``--text-on-<kind>`` reads on ``--<kind>-color`` in every palette
    (the :root error ink, black, is 4.13:1 on kanagawa's red, which sets
    white; light palettes set white on their darkened status colours)."""
    failures = []
    for name, props in _palettes():
        ratio = _contrast(
            _resolve(f"text-on-{kind}", props), _resolve(f"{kind}-color", props)
        )
        if ratio < 4.5:
            failures.append(f"{name}: {ratio:.2f}:1")
    assert not failures, failures


# A stop of a status colour: ``var(--error-color)`` or its ``-rgb`` twin.
_STATUS_STOP = re.compile(
    r"var\(--(error|warning|success)-color\)"
    r"|rgba?\(\s*var\(--(error|warning|success)-color-rgb\)"
    r"\s*(?:,\s*([\d.]+)\s*)?\)"
)


def _direct_status_kinds(value, aliases):
    kinds = set()
    for match in _STATUS_STOP.finditer(value):
        alpha = float(match.group(3)) if match.group(3) else 1.0
        if alpha >= 0.5:
            kinds.add(match.group(1) or match.group(2))
    for token in _VAR_STOP.findall(value):
        kinds |= aliases.get(token, set())
    return kinds


@functools.cache
def _status_aliases():
    """``{token: kinds}`` for the custom properties that draw a status
    colour solidly (as ``_status_fill_kinds`` reads it) under any of their
    definitions in shipped CSS, followed transitively: a plain alias or a
    gradient (``--news-gradient-2``, accent secondary to error red)."""
    definitions = _custom_property_definitions()
    aliases = {}
    changed = True
    while changed:
        changed = False
        for token, values in definitions.items():
            if token in ("error-color", "warning-color", "success-color"):
                continue
            kinds = set()
            for _, _, value in values:
                kinds |= _direct_status_kinds(_ink(value), aliases)
            if kinds - aliases.get(token, set()):
                aliases[token] = aliases.get(token, set()) | kinds
                changed = True
    return aliases


def _status_fill_kinds(fill):
    """The status colours (``error``, ``warning``, ``success``) a fill draws
    solidly: as a plain stop, through a ``var()`` fallback, as a gradient
    stop, as an ``-rgb`` twin at alpha 0.5 or more, or through a custom
    property that draws one (``_status_aliases``). Fainter tints are left
    to the rule's own ink and the page behind them."""
    return _direct_status_kinds(_ink(fill), _status_aliases())


# Status fills that draw no text, so they need no ink.
_DOT = "status dot; draws no text"
_STRENGTH = "password strength bar; draws no text"
NON_TEXT_STATUS_FILLS = {
    ("context_overflow.css", ".ldr-progress-fill"): _BAR,
    ("link_analytics.css", ".ldr-source-type-fill.ldr-news"): _BAR,
    ("link_analytics.css", ".ldr-source-type-fill.ldr-general"): _BAR,
    ("news.css", ".ldr-impact-fill"): _BAR,
    # The same bar restyled with --news-gradient-2 (accent to error red).
    ("news-enhanced.css", ".ldr-impact-fill"): _BAR,
    ("subscriptions.css", ".ldr-status-indicator.active"): _DOT,
    ("subscriptions.css", ".ldr-status-indicator.ldr-inactive"): _DOT,
    ("subscriptions.css", ".ldr-status-indicator.ldr-checking"): _DOT,
    ("templates/auth/change_password.html", ".ldr-strength-weak"): _STRENGTH,
    ("templates/auth/change_password.html", ".ldr-strength-medium"): _STRENGTH,
    ("templates/auth/change_password.html", ".ldr-strength-strong"): _STRENGTH,
    ("templates/auth/register.html", ".ldr-strength-weak"): _STRENGTH,
    ("templates/auth/register.html", ".ldr-strength-medium"): _STRENGTH,
    ("templates/auth/register.html", ".ldr-strength-strong"): _STRENGTH,
    # The utilColor and _stateColor() bars; their figures sit beside them.
    ("static/js/components/context-overflow.js", "style@L585"): _BAR,
    ("templates/pages/journal_quality.html", "style@L873"): _BAR,
}

# Pre-existing gaps on status fills: (sheet, selector) -> (worst palette,
# worst ratio, number of palettes under 4.5:1), as measured by
# test_fill_inks_read_on_swapped_fills. Only that exact measurement is
# exempt; any change to it (better or worse) fails as stale so the list
# stays exact. A fill whose only status colour is the error red is never
# exempt: --text-on-error reads on it in every palette.
KNOWN_STATUS_FILL_GAPS = {
    # The warning-to-error gradient: --text-on-error reads on its red end
    # everywhere, but kanagawa's error ink (white) is 1.68:1 on its yellow
    # start. No single ink token reads on both ends in every palette.
    ("metrics.css", ".ldr-nav-link-btn.ldr-nav-warning"): ("kanagawa", 1.68, 1),
    ("styles.css", ".ldr-nav-link-btn.ldr-nav-warning"): ("kanagawa", 1.68, 1),
}


def _fill_ink_pairs():
    """``(sheet, selector, ink, fill)`` for each rule with a non-accent fill
    whose ink, set by the rule or inherited along its selector chain (and
    from a BEM block to its ``--modifier``), is one of FILL_INKS, and for
    each rule with a status fill (``_status_fill_kinds``) whatever its ink;
    ``ink`` is None there when the chain sets none."""
    pairs = []
    for name, rules in _sheets():
        fills, inks, filled = {}, {}, []
        for selector_list, decls in rules:
            if selector_list.startswith("@"):
                continue
            clipped = {"background-clip", "-webkit-background-clip"} & set(
                decls
            )
            background = decls.get("background-color", decls.get("background"))
            for selector in (s.strip() for s in selector_list.split(",")):
                if background:
                    fills[selector] = None if clipped else background
                    filled.append(selector)
                if "color" in decls:
                    inks[selector] = decls["color"]
        for selector in dict.fromkeys(filled):
            fill = fills[selector]
            if not fill or _PSEUDO_ELEMENT.search(selector):
                continue
            if _is_accent_fill(_ink(fill)):
                continue
            chain = _selector_chain(selector)
            for link in list(chain):
                block = _BEM_MODIFIER.sub(lambda m: "." + m.group(1), link)
                if block != link:
                    chain += [
                        s for s in _selector_chain(block) if s not in chain
                    ]
            ink = next((inks[s] for s in chain if s in inks), None)
            if _status_fill_kinds(fill):
                pairs.append((name, selector, ink and _ink(ink), fill))
            elif ink is not None and _ink(ink) in FILL_INKS:
                pairs.append((name, selector, _ink(ink), fill))
    return pairs


def test_fill_inks_read_on_swapped_fills():
    """A rule pairing a fill ink (FILL_INKS) with a fill other than the
    accent, directly or by inheriting the ink of its base selector, keeps
    4.5:1 on every stop of that fill, composited over the page and card
    backgrounds (a transparent fill leaves the ink on those), in every
    palette. So does any ink, literal colours included, on a status fill
    (``--error-color``, ``--warning-color`` or ``--success-color``, see
    ``_status_fill_kinds``); a status fill whose chain sets no ink fails
    (its text would take an ink from a co-class or parent the sweep cannot
    see) unless it is listed in NON_TEXT_STATUS_FILLS. A fill or ink that
    does not resolve fails rather than being skipped, including a fill in
    a colour syntax the parser does not read (``_fill_stops``). The only
    exemptions are the exact pre-existing gaps in KNOWN_STATUS_FILL_GAPS.

    Limits: the ink is inherited within one sheet only (along the selector
    chain and from a ``.block`` to its ``.block--modifier``); an inline
    style built from a template expression is checked as each value
    DYNAMIC_INLINE_STYLES lists for it (an unlisted expression fails in
    ``test_dynamic_inline_styles_are_allowlisted``); pseudo-element rules,
    fills and inks set one property at a time (``element.style.color``),
    ``filter`` on hover and
    cross-sheet type selectors (``a:hover``, see
    ``test_filled_anchors_keep_readable_ink``) are not checked."""
    pairs = _fill_ink_pairs()
    found = {(name, selector) for name, selector, _, _ in pairs}
    assert {
        ("styles.css", ".ldr-log-indicator--has-error"),
        ("styles.css", ".ldr-console-log-entry.ldr-log-error .ldr-log-badge"),
        ("styles.css", ".ldr-badge-danger"),
        ("templates/pages/journal_quality.html", ".ldr-quality-dangerous"),
        ("static/js/components/context-overflow.js", "style@L257"),
    } <= found
    assert len(pairs) >= 80, "too few fill/ink pairs were found"
    palettes = _palettes()
    offenders, gaps, allowed_seen = [], {}, set()
    for name, selector, ink, fill in pairs:
        key = _allow_key(name, selector)
        if ink is None:
            if key in NON_TEXT_STATUS_FILLS:
                allowed_seen.add(key)
            else:
                offenders.append(f"{name} {selector}: no ink on {fill}")
            continue
        failing = []
        for palette, props in palettes:
            ink_rgb = _resolve_color(ink, props)
            stops = _fill_stops(fill, props)
            if ink_rgb is None or stops is None:
                offenders.append(f"{name} {selector} [{palette}]: unresolved")
                continue
            # A transparent fill leaves the ink on the page or card.
            backdrops = [_resolve(token, props) for token in _BACKDROPS]
            worst = min(
                _contrast(ink_rgb, surface)
                for surface in _over(backdrops, stops)
            )
            if worst < 4.5:
                failing.append((worst, palette))
        if not failing:
            continue
        if key in KNOWN_STATUS_FILL_GAPS and _status_fill_kinds(fill) - {
            "error"
        }:
            worst, palette = min(failing, key=lambda item: item[0])
            gaps[key] = (palette, round(worst, 2), len(failing))
            continue
        offenders += [
            f"{name} {selector} [{palette}]: {ink} on {fill} {worst:.2f}:1"
            for worst, palette in failing
        ]
    assert not offenders, offenders
    stale = {
        key: (reason, None)
        for key, reason in NON_TEXT_STATUS_FILLS.items()
        if key not in allowed_seen
    }
    stale.update(
        (key, (recorded, gaps.get(key)))
        for key, recorded in KNOWN_STATUS_FILL_GAPS.items()
        if gaps.get(key) != recorded
    )
    assert not stale, f"entries to update (recorded, now): {stale}"


# A custom property whose value names a status colour, read from the raw
# text independently of the rule parser.
_STATUS_DEFINITION = re.compile(
    r"--([\w-]+)\s*:\s*([^;{}]*(?:error|warning|success)-color[^;{}]*)"
)


def test_status_colour_aliases_are_recognised():
    """The status-fill sweeps recognise the status tokens by name, so every
    custom property that names one must be followed: a ``--x:
    var(--error-color)`` alias or a gradient of status stops
    (``--news-gradient-2``) is a status fill through ``_status_aliases``.
    The only other definitions allowed are faint tints and shadows (every
    status stop an ``-rgb`` twin under alpha 0.5), which no sweep treats as
    a status fill; anything else, such as a bare ``-rgb`` twin alias
    (``--x-rgb: var(--error-color-rgb)``), fails."""
    aliases = _status_aliases()
    assert {"news-gradient-2", "news-gradient-3", "news-gradient-4"} <= set(
        aliases
    ), aliases
    assert _status_fill_kinds("var(--news-gradient-4)") == {"error", "warning"}
    missed, found = [], 0
    for name, css in _css_sources():
        for match in _STATUS_DEFINITION.finditer(_strip_comments(css)):
            token, value = match.group(1), _ink(match.group(2))
            if token in ("error-color", "warning-color", "success-color"):
                continue
            found += 1
            if token in aliases:
                continue
            faint = [
                float(alpha)
                for alpha in re.findall(
                    r"rgba?\(\s*var\(--(?:error|warning|success)-color-rgb\)"
                    r"\s*,\s*([\d.]+)\s*\)",
                    value,
                )
            ]
            mentions = len(
                re.findall(r"(?:error|warning|success)-color", value)
            )
            if faint and len(faint) == mentions and max(faint) < 0.5:
                continue
            missed.append(f"{name}: --{token}: {value}")
    assert found >= 3, "status alias scan found too few definitions"
    assert not missed, missed


# --- Accent-filled anchors against the cascade ----------------------------
#
# The sweep above resolves fill and ink within one sheet and through class
# chains only. An accent-filled class on an ``<a>`` also meets type
# selectors from other sheets: styles.css ``a`` and ``a:hover`` (0,1,1) and
# a theme's ``[data-theme="..."] a`` (0,1,1) and ``a:hover`` (0,2,1) outrank
# a single-class ink rule (0,1,0), so the label would take the link colour.
# The check below rebuilds that cascade statically.

# Classes JavaScript toggles at run time, so an anchor need not carry them
# in its markup to take a fill rule that requires them.
_STATE_CLASSES = frozenset(
    {"active", "selected", "show", "open", "disabled", "current"}
)
# The interaction states checked, as the pseudo-classes each one matches.
_STATES = {
    "rest": frozenset(),
    "hover": frozenset({"hover"}),
    "focus": frozenset({"focus", "focus-visible", "focus-within"}),
    "visited": frozenset({"visited"}),
    "pressed": frozenset(
        {"active", "hover", "focus", "focus-visible", "focus-within"}
    ),
}
_INTERACTIVE = frozenset(
    {"hover", "focus", "focus-visible", "focus-within", "active", "visited"}
)
# Pseudo-classes an anchor never matches.
_NEVER = frozenset(
    {"disabled", "checked", "indeterminate", "placeholder-shown"}
)
_SIMPLE = re.compile(
    r"\*|[a-zA-Z][\w-]*|\.[\w-]+|#[\w-]+|\[[^\]]*\]"
    r"|::?[\w-]+(?:\((?:[^()]|\([^()]*\))*\))?"
)
_SCOPE = re.compile(
    r"(?:html|:root|body)?(?:\[data-theme(?:=[\"']?[\w-]+[\"']?)?\])?"
    r"(?:\s+(?:html|body))?"
)


def _split_top(text, sep):
    parts, depth, start = [], 0, 0
    for i, char in enumerate(text):
        depth += char == "("
        depth -= char == ")"
        if char == sep and depth == 0:
            parts.append(text[start:i].strip())
            start = i + 1
    parts.append(text[start:].strip())
    return parts


def _subject_split(selector):
    """``.a > .b:hover`` -> (``.a``, ``.b:hover``), ignoring combinators
    inside ``:not(...)``."""
    depth, cut = 0, -1
    for i, char in enumerate(selector):
        depth += char == "("
        depth -= char == ")"
        if depth == 0 and char in " >+~":
            cut = i
    if cut < 0:
        return "", selector
    return selector[:cut].strip(" >+~"), selector[cut + 1 :].strip()


def _simples(compound):
    tokens, pos = [], 0
    while pos < len(compound):
        match = _SIMPLE.match(compound, pos)
        if not match:
            return None
        tokens.append(match.group(0))
        pos = match.end()
    return tokens


def _specificity(selector):
    ids = classes = types = 0
    for compound in re.split(r"\s*[ >+~]\s*", selector.strip()):
        for token in _simples(compound) or []:
            if token.startswith("#"):
                ids += 1
            elif token.startswith(("[", ".")):
                classes += 1
            elif token.startswith("::"):
                types += 1
            elif token.startswith(":"):
                name, _, args = token[1:].partition("(")
                if name == "where":
                    continue
                if name in ("not", "is", "has"):
                    inner = [
                        _specificity(s) for s in _split_top(args[:-1], ",")
                    ]
                    best = max(inner, default=(0, 0, 0))
                    ids, classes, types = (
                        ids + best[0],
                        classes + best[1],
                        types + best[2],
                    )
                else:
                    classes += 1
            elif token != "*":
                types += 1
    return ids, classes, types


@functools.cache
def _compound_matches(compound, classes, state, tag="a"):
    """Whether a ``tag`` element (an ``<a>`` unless stated) with ``classes``
    in ``state`` matches one compound selector. Ids are never matched
    (elements are tracked by class), other attributes and structural
    pseudo-classes are assumed to match."""
    tokens = _simples(compound)
    if not tokens:
        return False
    for token in tokens:
        if token.startswith("::"):
            return False
        if token.startswith("#"):
            return False
        if token.startswith("."):
            if token[1:] not in classes:
                return False
        elif token.startswith(":"):
            name, _, args = token[1:].partition("(")
            if name in ("before", "after"):
                return False
            if name == "not":
                if any(
                    _compound_matches(arg, classes, state, tag)
                    for arg in _split_top(args[:-1], ",")
                ):
                    return False
            elif name in ("is", "where"):
                if not any(
                    _compound_matches(arg, classes, state, tag)
                    for arg in _split_top(args[:-1], ",")
                ):
                    return False
            elif name in ("link", "any-link"):
                if name == "link" and "visited" in state:
                    return False
            elif name in _INTERACTIVE:
                if name not in state:
                    return False
            elif name in _NEVER:
                return False
        elif token.startswith("["):
            continue
        elif token not in ("*", tag):
            return False
    return True


def _anchor_class_sets():
    """The static classes of every ``<a>`` in templates and scripts (markup
    in HTML and template literals, and ``createElement('a')`` elements given
    a ``className`` or ``classList.add``), with template expressions
    dropped: ``{location: classes}``."""
    found = {}
    expression = re.compile(r"\$\{[^}]*\}|\{\{.*?\}\}|\{%.*?%\}", re.S)
    for name, path in _markup_files():
        text = path.read_text(encoding="utf-8")
        for match in re.finditer(
            r"<a\b[^>]*?\bclass\s*=\s*([\"'])(.*?)\1", text, flags=re.S
        ):
            line = text.count("\n", 0, match.start()) + 1
            classes = frozenset(expression.sub(" ", match.group(2)).split())
            if classes:
                found[f"{name}:{line}"] = classes
        for match in re.finditer(
            r"(?:const|let|var)\s+(\w+)\s*=\s*document\.createElement\("
            r"\s*['\"]a['\"]\s*\)",
            text,
        ):
            var = re.escape(match.group(1))
            names = set()
            for cls in re.finditer(
                rf"\b{var}\.className\s*=\s*(['\"`])(.*?)\1"
                rf"|\b{var}\.classList\.add\(([^)]*)\)",
                text,
            ):
                value = (
                    cls.group(2) if cls.group(2) is not None else cls.group(3)
                )
                names |= set(re.findall(r"[\w-]+", expression.sub(" ", value)))
            if names:
                line = text.count("\n", 0, match.start()) + 1
                found[f"{name}:{line}"] = frozenset(names)
    return found


def _decl(decls, prop):
    value = decls.get(prop)
    if value is None:
        return None
    important = value.endswith("!important")
    return important, value.replace("!important", "").strip()


# The properties the cascade below tracks: the ink, the fill, the focus
# outline, and Bootstrap's button variables, which its .btn state rules
# draw as the ink and the fill.
_TRACKED = frozenset(
    {"color", "background", "background-color", "outline", "outline-color"}
)
_BS_BTN_VAR = re.compile(r"var\(\s*--(bs-btn-[\w-]+)\s*\)")


def _prepare(rank, css):
    """``(rank, [(ancestors, subject, specificity)], decls)`` for each rule
    of ``css`` that sets a colour, a background, an outline,
    ``pointer-events`` or a ``--bs-btn-*`` variable."""
    prepared = []
    for selector_list, decls in _parse_rules(css):
        if selector_list.startswith("@") or not (
            _TRACKED & set(decls)
            or "pointer-events" in decls
            or any(prop.startswith("--bs-btn-") for prop in decls)
        ):
            continue
        selectors = [
            (*_subject_split(selector), _specificity(selector))
            for selector in _split_top(selector_list, ",")
        ]
        prepared.append((rank, selectors, decls))
    return prepared


def _cascade_winners(rules, classes, state, context, theme, tag="a"):
    """Winning values of the tracked properties for one element:
    ``{"color"|"background"|"outline"|"pointer-events"|"--bs-btn-*":
    value}``. ``rules`` come
    from ``_prepare`` in load order; a selector counts when its subject
    compound matches and its ancestors are empty, a theme scope, or the
    given ancestors (``context``)."""
    winners = {}
    for rank, selectors, decls in rules:
        best = None
        for ancestors, subject, spec in selectors:
            if ancestors and ancestors != context:
                scope = _SCOPE.fullmatch(ancestors)
                if not scope or not scope.group(0):
                    continue
                named = re.search(r"data-theme=[\"']?([\w-]+)", ancestors)
                if named and named.group(1) != theme:
                    continue
            if not _compound_matches(subject, classes, state, tag):
                continue
            best = spec if best is None or spec > best else best
        if best is None:
            continue
        clipped = {"background-clip", "-webkit-background-clip"} & set(decls)
        pairs = [
            ("color", "color"),
            ("background", "background"),
            ("background-color", "background"),
            ("outline", "outline"),
            ("outline-color", "outline"),
            ("pointer-events", "pointer-events"),
        ]
        pairs += [
            (prop, prop) for prop in decls if prop.startswith("--bs-btn-")
        ]
        for prop, key in pairs:
            found = _decl(decls, prop)
            if found is None:
                continue
            important, value = found
            if key == "background" and clipped:
                value = "transparent"
            order = (important, best, rank)
            if key not in winners or order >= winners[key][0]:
                winners[key] = (order, value)
    values = {key: value for key, (_, value) in winners.items()}
    for key in ("color", "background", "outline"):
        value = values.get(key)
        for _ in range(5):
            if value is None or not _BS_BTN_VAR.search(value):
                break
            missing = [
                ref
                for ref in _BS_BTN_VAR.findall(value)
                if f"--{ref}" not in values
            ]
            if missing:
                # An undefined variable makes the declaration invalid at
                # computed-value time: the ink is inherited (unknown here)
                # and the fill is transparent.
                value = "transparent" if key == "background" else None
                break
            value = _BS_BTN_VAR.sub(
                lambda match: values[f"--{match.group(1)}"], value
            )
        values[key] = value
    return values


def _cascade(rules, classes, state, context, theme, tag="a"):
    """Winning ``(color, background)`` values for one element, with
    ``var(--bs-btn-*)`` resolved against the variables the same cascade
    gives it."""
    values = _cascade_winners(rules, classes, state, context, theme, tag)
    return values.get("color"), values.get("background")


def _resolve_color(value, props):
    """The opaque colour an ink or a stop draws, or None when it does not
    resolve (an unread syntax, a translucent literal, or a token
    ``_resolve`` rejects)."""
    value = _drop_fallbacks(value).strip()
    token = _VAR_STOP.fullmatch(value)
    if token:
        try:
            return _resolve(token.group(1), props)
        except AssertionError:
            return None
    parsed = _parse_color(value)
    if parsed is None or parsed[1] != 1:
        return None
    return parsed[0]


def _readable(ink, fill, props):
    """Accent ink always reads on an accent fill (``test_accent_ink_contrast``
    checks the pair); any other ink must resolve to at least 4.5:1 on every
    colour stop of the fill under this theme's palette."""
    if _ink(ink) == ACCENT_INK:
        return True
    ink_rgb = _resolve_color(ink, props)
    stops = _COLOR_STOP.findall(_drop_fallbacks(fill))
    if ink_rgb is None or not stops:
        return False
    for stop in stops:
        stop_rgb = _resolve_color(stop, props)
        if stop_rgb is None or _contrast(ink_rgb, stop_rgb) < 4.5:
            return False
    return True


# Bootstrap's button rules that set a button's ink or fill, and the
# .btn-primary variables they read, transcribed from bootstrap.css 5.3.8
# (app.js imports bootstrap.min.css before styles.css, so they load first).
# ``test_bootstrap_transcription_matches_the_locked_release`` ties this to
# the locked version.
BOOTSTRAP_VERSION = "5.3.8"
BOOTSTRAP_BTN_CSS = """
.btn {
  --bs-btn-color: var(--bs-body-color);
  --bs-btn-bg: transparent;
  color: var(--bs-btn-color);
  background-color: var(--bs-btn-bg);
}
.btn:hover {
  color: var(--bs-btn-hover-color);
  background-color: var(--bs-btn-hover-bg);
}
.btn-check + .btn:hover {
  color: var(--bs-btn-color);
  background-color: var(--bs-btn-bg);
}
.btn:focus-visible {
  color: var(--bs-btn-hover-color);
  background-color: var(--bs-btn-hover-bg);
}
.btn-check:checked + .btn, :not(.btn-check) + .btn:active, .btn:first-child:active, .btn.active, .btn.show {
  color: var(--bs-btn-active-color);
  background-color: var(--bs-btn-active-bg);
}
.btn:disabled, .btn.disabled, fieldset:disabled .btn {
  color: var(--bs-btn-disabled-color);
  background-color: var(--bs-btn-disabled-bg);
}
.btn-primary {
  --bs-btn-color: #fff;
  --bs-btn-bg: #0d6efd;
  --bs-btn-hover-color: #fff;
  --bs-btn-hover-bg: #0b5ed7;
  --bs-btn-active-color: #fff;
  --bs-btn-active-bg: #0a58ca;
  --bs-btn-disabled-color: #fff;
  --bs-btn-disabled-bg: #0d6efd;
}
"""
REPO_ROOT = WEB_ROOT.parents[2]


def test_bootstrap_transcription_matches_the_locked_release():
    """The cascade check models Bootstrap from BOOTSTRAP_BTN_CSS, so a
    Bootstrap upgrade must revisit it; where the package is installed,
    every transcribed declaration is in the shipped stylesheet."""
    lock = (REPO_ROOT / "package-lock.json").read_text(encoding="utf-8")
    locked = re.search(
        r'"node_modules/bootstrap":\s*\{\s*"version":\s*"([^"]+)"', lock
    )
    assert locked, "package-lock.json no longer locks bootstrap"
    assert locked.group(1) == BOOTSTRAP_VERSION, (
        f"bootstrap {locked.group(1)} is locked; re-transcribe "
        "BOOTSTRAP_BTN_CSS from its dist/css/bootstrap.css"
    )
    shipped = REPO_ROOT / "node_modules/bootstrap/dist/css/bootstrap.css"
    if not shipped.exists():
        pytest.skip("bootstrap is not installed (npm ci)")
    rules = {}
    for selector, decls in _parse_rules(shipped.read_text(encoding="utf-8")):
        rules.setdefault(selector, {}).update(decls)
    for selector, decls in _parse_rules(BOOTSTRAP_BTN_CSS):
        assert selector in rules, f"bootstrap.css has no {selector!r} rule"
        for prop, value in decls.items():
            assert rules[selector].get(prop) == value, (selector, prop)


def _theme_name(path):
    match = re.search(
        r"\[data-theme=[\"']?([\w-]+)", path.read_text(encoding="utf-8")
    )
    return match.group(1) if match else path.stem


def _anchor_fill_trigger(background, ink):
    """Whether a fill rule is one the anchor replay checks: every filled
    control class. That is a fill with a colour stop (an accent or status
    token or an alias of one, its ``-rgb`` twin at alpha 0.5 or more, or a
    literal colour), a fill in a syntax the stop pattern does not read
    (replayed, so it fails as unresolved), or any fill whose ink -- set by
    the rule or inherited along its selector chain in the sheet -- is an
    on-fill ink (FILL_INKS) or a literal colour. A neutral surface
    (``--bg-tertiary``) under a theme text ink is not a filled control."""
    value = _ink(background)
    bare = re.sub(r"url\([^()]*\)|var\(--[\w-]+", " ", value)
    if _UNREAD_COLOR.search(bare):
        return True
    if _is_accent_fill(value) or _status_fill_kinds(value):
        return True
    accent, _, _ = _aliases()
    for stop in _COLOR_STOP.findall(value):
        token = _VAR_STOP.fullmatch(stop)
        rgba = _RGBA_TOKEN.fullmatch(stop)
        literal = _parse_color(stop)
        if token and (token.group(1) in _STATUS_TOKENS | accent):
            return True
        if rgba and rgba.group(1) in _STATUS_TOKENS | accent:
            if rgba.group(2) is None or float(rgba.group(2)) >= 0.5:
                return True
        if literal and literal[1] >= 0.5:
            return True
    if ink is None:
        return False
    ink = _ink(ink)
    return (
        ink in FILL_INKS
        or _parse_color(ink) is not None
        or bool(_UNREAD_COLOR.search(ink))
    )


_TEMPLATES = WEB_ROOT / "templates"


def _template_key(path):
    return path.relative_to(WEB_ROOT).as_posix()


@functools.cache
def _page_closure():
    """``{page key: (stylesheet names, template keys, script references)}``
    for each full page (a template that ``{% extends %}`` another), keyed
    like ``_markup_files``: every ``*.css`` and ``*.js`` named in its
    markup, the templates it extends and the ones they ``{% include %}`` or
    import from, transitively, and those templates themselves (whose
    ``<style>`` blocks and markup the page renders). Templates that extend
    nothing (fragments, components, standalone pages) are left out, and so
    is a page with a reference it cannot read: their sheets are
    unknown."""
    reference = re.compile(
        r"\{%-?\s*(extends|include|import|from)\s+([\"'])(.+?)\2"
    )
    pages = {}
    for path in sorted(_TEMPLATES.rglob("*.html")):
        if not re.search(
            r"\{%-?\s*extends\b", path.read_text(encoding="utf-8")
        ):
            continue
        sheets, scripts, todo, seen = set(), set(), [path], set()
        while todo:
            current = todo.pop()
            if current in seen:
                continue
            seen.add(current)
            if not current.is_file():
                # A template it cannot read: the page's sheets are unknown.
                sheets = None
                break
            body = current.read_text(encoding="utf-8")
            sheets |= set(re.findall(r"([\w.-]+\.css)\b", body))
            scripts |= set(re.findall(r"([\w./-]+\.js)\b", body))
            # A commented-out {# {% include %} #} loads nothing.
            live = re.sub(r"\{#.*?#\}", " ", body, flags=re.S)
            todo += [
                _TEMPLATES / target for _, _, target in reference.findall(live)
            ]
        if sheets is not None:
            templates = frozenset(_template_key(p) for p in seen)
            pages[_template_key(path)] = (sheets, templates, scripts)
    return pages


def _page_sheets():
    """``{page key: stylesheet names}`` (``_page_closure``)."""
    return {page: sheets for page, (sheets, _, _) in _page_closure().items()}


@functools.cache
def _sheets_named_in_code():
    """Stylesheet names that script or Python code mentions (a sheet it may
    load into any page): never treated as absent from a page."""
    named = set()
    for path in [
        *(WEB_ROOT / "static" / "js").rglob("*.js"),
        *(REPO_ROOT / "src").rglob("*.py"),
    ]:
        named |= set(
            re.findall(r"([\w.-]+\.css)\b", path.read_text(encoding="utf-8"))
        )
    return named


def _sheet_off_page(sheet, where):
    """Whether the fill sheet ``sheet`` is known not to reach the anchor at
    ``where``: the anchor is in a full page (``_page_sheets``) that names
    neither the sheet nor anything that loads it. styles.css and sheets
    named in code reach every page, a template's own ``<style>`` block
    (named after the template) reaches that template's anchors, and
    anchors built in script, and in templates whose page is unknown, are
    reached by every sheet."""
    page = where.rpartition(":")[0]
    if sheet in ("styles.css", page) or sheet in _sheets_named_in_code():
        return False
    loaded = _page_sheets().get(page)
    return loaded is not None and sheet not in loaded


def _gap_exemptable(background):
    """A KNOWN_ANCHOR_FILL_GAPS entry only covers a fill with a stop outside
    the primary/secondary accent (and its aliases) and the status tokens:
    ``--accent-tertiary``, a neutral token or a literal colour."""
    accent, accent_rgb, _ = _aliases()
    status = {"error-color", "warning-color", "success-color"}
    for stop in _COLOR_STOP.findall(_ink(background)):
        token = _VAR_STOP.fullmatch(stop) or _RGB_STOP.fullmatch(stop)
        name = token and token.group(1)
        if not name or name not in accent | accent_rgb | status | {
            f"{s}-rgb" for s in status
        }:
            return True
    return False


# Pre-existing gaps on filled anchors outside the accent and status fills
# (sheet, selector) -> (worst palette, worst ratio, number of failing
# scenarios, digest of every failing scenario and its ratio), as measured
# by test_filled_anchors_keep_readable_ink (``_gap_measurement``). A
# scenario is one (classes, context, state, palette) replay. Only a fill
# with a stop outside the primary/secondary accent and the status colours
# can be listed (``_gap_exemptable``); an unresolved ink or fill is never
# exempt, and any change to any failing scenario -- one more or one fewer,
# or a ratio that moves either way, even when the worst case and the count
# stay the same -- fails as stale, printing every failing scenario, so the
# list stays exact.
KNOWN_ANCHOR_FILL_GAPS = {
    # White on the --accent-tertiary to --accent-primary gradient: the
    # Metrics "Link Analytics" nav link and the document page's "View
    # Chunks" link. Mixed gradients are out of scope (see _COLOR_STOP):
    # no single ink token reads on both ends. High Contrast's a:hover green
    # on the green end is the 1.00:1 worst case.
    ("metrics.css", ".ldr-nav-link-btn.ldr-nav-info"): (
        "high-contrast",
        1.0,
        125,
        "eaa179253f4115a3",
    ),
    ("styles.css", ".ldr-nav-link-btn.ldr-nav-info"): (
        "high-contrast",
        1.0,
        125,
        "eaa179253f4115a3",
    ),
    ("document_details.css", ".ldr-btn-info"): (
        "high-contrast",
        1.0,
        141,
        "99d509c948be137e",
    ),
    # White on --accent-tertiary: the Library "View Text" action link.
    ("library.css", ".ldr-action-btn-txt"): (
        "high-contrast",
        1.0,
        115,
        "22f243fdfd560c22",
    ),
    # White on --btn-secondary-bg (--text-muted): the Document Chunks back
    # link, 3.37:1 with the :root palette.
    ("document_details.css", ".ldr-back-button"): (
        "high-contrast",
        1.33,
        115,
        "a93388242b77096d",
    ),
}


def _gap_measurement(found):
    """A KNOWN_ANCHOR_FILL_GAPS measurement from its failing scenarios
    ``[(ratio, palette, state, classes, context)]``: the worst palette and
    ratio, the number of failing scenarios, and a digest of every failing
    scenario with its ratio, so that a scenario that starts or stops
    failing, or any failing ratio that moves (worse or better), changes
    the measurement even when the worst case and the count do not."""
    lines = sorted(f"{ratio:.2f}|" + "|".join(rest) for ratio, *rest in found)
    digest = hashlib.sha256("\n".join(lines).encode()).hexdigest()[:16]
    worst = min(found)
    return worst[1], round(worst[0], 2), len(found), digest


@functools.cache
def _filled_anchor_offenders():
    """``(offenders, scenarios, failing, gaps)`` for the filled-anchor
    replay (``test_filled_anchors_keep_readable_ink``); ``failing`` holds
    the failing scenarios of each KNOWN_ANCHOR_FILL_GAPS entry and ``gaps``
    their measurements (``_gap_measurement``)."""
    anchors = _anchor_class_sets()
    base = _prepare(-1, BOOTSTRAP_BTN_CSS) + _prepare(
        0, (CSS_ROOT / "styles.css").read_text(encoding="utf-8")
    )
    root = _root_defaults()
    themes = [(None, "", [], root)]
    for path in THEME_FILES:
        css = path.read_text(encoding="utf-8")
        props = dict(root)
        props.update(_custom_properties(css))
        themes.append((path.stem, _theme_name(path), _prepare(1, css), props))
    offenders, scenarios, failing = [], set(), {}
    for name, css in _stylesheets():
        own = [] if name == "styles.css" else _prepare(2, css)
        # A template's <style> block only reaches that template's anchors.
        local = name.endswith(".html")
        parsed = list(_parse_rules(css))
        inks = {
            selector: decls["color"]
            for selector_list, decls in parsed
            if "color" in decls and not selector_list.startswith("@")
            for selector in _split_top(selector_list, ",")
        }
        for selector_list, decls in parsed:
            background = decls.get("background-color", decls.get("background"))
            if selector_list.startswith("@") or not background:
                continue
            if {"background-clip", "-webkit-background-clip"} & set(decls):
                continue
            for selector in _split_top(selector_list, ","):
                if (name, selector) in DEAD_ACCENT_RULES:
                    continue
                chain_ink = next(
                    (inks[s] for s in _selector_chain(selector) if s in inks),
                    None,
                )
                if not _anchor_fill_trigger(background, chain_ink):
                    continue
                context, subject = _subject_split(selector)
                tokens = _simples(subject)
                if not tokens or any(
                    t.startswith(("::", "#")) or t in (":before", ":after")
                    for t in tokens
                ):
                    continue
                if tokens[0][0].isalpha() and tokens[0] != "a":
                    continue
                fill_classes = {t[1:] for t in tokens if t.startswith(".")}
                if not fill_classes - _STATE_CLASSES:
                    continue
                needed = {
                    t[1:].partition("(")[0] for t in tokens if t.startswith(":")
                } & _INTERACTIVE
                status_gap = KNOWN_STATUS_FILL_GAPS.get((name, selector))
                mixed = _status_fill_kinds(background) - {"error"}
                for where, classes in anchors.items():
                    if local and not where.startswith(f"{name}:"):
                        continue
                    if not fill_classes - _STATE_CLASSES <= classes:
                        continue
                    if _sheet_off_page(name, where):
                        continue
                    element = classes | fill_classes
                    for state_name, state in _STATES.items():
                        if not needed <= state:
                            continue
                        for stem, theme, rules, props in themes:
                            key = (name, element, state_name, context, stem)
                            if key in scenarios:
                                continue
                            scenarios.add(key)
                            values = _cascade_winners(
                                base + rules + own,
                                element,
                                state,
                                context,
                                theme,
                            )
                            if state & {"hover", "active"} and (
                                values.get("pointer-events") == "none"
                            ):
                                # Not hoverable or pressable
                                # (``.ldr-page-link.disabled``).
                                continue
                            ink = values.get("color")
                            fill = values.get("background")
                            if not fill:
                                continue
                            if (
                                ink is not None
                                and _ink(ink) == ACCENT_INK
                                and _is_accent_fill(fill)
                            ):
                                # The pair test_accent_ink_contrast checks.
                                continue
                            # Whatever ink and fill win must read: the fill
                            # composited over the page and card backgrounds
                            # (a translucent or transparent winner lets them
                            # show through). An ink or fill that does not
                            # resolve fails.
                            ink_rgb = ink and _resolve_color(_ink(ink), props)
                            stops = _fill_stops(fill, props)
                            where_text = (
                                f"{name} {selector} on <a> {where} "
                                f"[{state_name}, {stem or ':root'}]: "
                                f"{ink} on {fill}"
                            )
                            if not ink_rgb or stops is None:
                                offenders.append(f"{where_text} unresolved")
                                continue
                            backdrops = [
                                _resolve(token, props) for token in _BACKDROPS
                            ]
                            worst = min(
                                _contrast(ink_rgb, surface)
                                for surface in _over(backdrops, stops)
                            )
                            if worst >= 4.5 or (
                                status_gap
                                and mixed
                                and round(worst, 2) >= status_gap[1]
                            ):
                                continue
                            if (
                                name,
                                selector,
                            ) in KNOWN_ANCHOR_FILL_GAPS and _gap_exemptable(
                                background
                            ):
                                failing.setdefault((name, selector), []).append(
                                    (
                                        worst,
                                        stem or ":root",
                                        state_name,
                                        " ".join(sorted(element)),
                                        context,
                                    )
                                )
                                continue
                            offenders.append(f"{where_text} {worst:.2f}:1")
    gaps = {key: _gap_measurement(found) for key, found in failing.items()}
    return offenders, scenarios, failing, gaps


def test_filled_anchors_keep_readable_ink():
    """Every filled control class on an ``<a>`` (templates and script
    markup) keeps a readable ink at rest, on hover, on keyboard focus, when
    visited and when pressed, in every theme and with the bare ``:root``
    palette. A filled control is any fill rule ``_anchor_fill_trigger``
    picks: accent, error, warning and success fills, any other colour or
    literal fill, and any fill paired with an on-fill ink. The cascade is
    rebuilt from Bootstrap's button rules (``BOOTSTRAP_BTN_CSS``, rank -1),
    styles.css (rank 0), the theme file (rank 1) and the sheet declaring
    the fill (rank 2): by importance, specificity and load order, so
    ``a:hover`` (0,1,1) and ``[data-theme="high-contrast"] a`` (0,1,1) beat
    a bare ``.ldr-skip-link`` (0,1,0), High Contrast's ``a:hover`` (0,2,1)
    beats ``.ldr-nav-link-btn.ldr-nav-success`` (0,2,0), and Bootstrap's
    ``.btn:focus-visible`` (0,2,0) beats ``.btn-primary`` (0,1,0).
    ``var(--bs-btn-*)`` values are resolved against the variables the same
    cascade gives the anchor (an undefined one leaves the ink unknown and
    the fill transparent). Whatever ink and fill win must reach 4.5:1 on
    every stop of the fill, composited over the page and card backgrounds
    (a translucent or transparent winner lets them show through);
    ``var(--text-on-accent)`` on an accent fill passes, since
    ``test_accent_ink_contrast`` checks that pair. An ink or fill that does
    not resolve fails. The only exemptions are a mixed status fill down to
    its KNOWN_STATUS_FILL_GAPS floor and the exact pre-existing
    measurements in KNOWN_ANCHOR_FILL_GAPS; an entry whose measurement
    changes (a failing scenario added or dropped, or its ratio moved)
    fails as stale. A sheet is not applied to an anchor in a full page
    that does not load it (``_sheet_off_page``; a template's own
    ``<style>`` block always reaches its own anchors), and a hover or
    press is not replayed on an anchor whose cascade sets
    ``pointer-events: none``.

    Limits: rules whose ancestors are neither a theme scope nor the fill's
    own ancestors are not applied (``.ldr-alert a`` inside an alert, see
    ``test_links_in_filled_containers_stay_readable``;
    ``.ldr-sidebar-nav li a``; Bootstrap's ``.btn-check + .btn``), id
    selectors never match, classes set from JavaScript after render (other
    than the state classes in ``_STATE_CLASSES``) and inline styles on the
    anchor are not seen, rules from other page sheets the same page loads
    are not combined, ``@media`` blocks are applied
    unconditionally, and Bootstrap is modelled for ``.btn`` and
    ``.btn-primary`` only (other variants and reboot's ``a`` rules are not
    transcribed). Fill rules whose subject carries no class other than the
    state classes (``a.active``, ``[aria-current]``, a type selector) are
    skipped, since no static class ties them to an anchor, and a neutral
    surface fill under a theme text ink is not a filled control."""
    offenders, scenarios, failing, gaps = _filled_anchor_offenders()
    assert len(scenarios) >= 3000, "too few filled anchors were checked"
    assert not offenders, offenders
    stale = {
        key: (recorded, gaps.get(key), sorted(failing.get(key, [])))
        for key, recorded in KNOWN_ANCHOR_FILL_GAPS.items()
        if gaps.get(key) != recorded
    }
    assert not stale, f"entries to update (recorded, now, scenarios): {stale}"


def test_anchor_replay_covers_every_status_fill():
    """The replay reaches anchors filled with each status colour, so a
    narrowed trigger (accent and error only) fails here: the View Text
    link (success), the Metrics Cost Analytics link (success) and the
    Metrics warning-to-error link."""
    _, scenarios, _, _ = _filled_anchor_offenders()
    filled = {(name, frozenset(element)) for name, element, *_ in scenarios}
    for sheet, classes in (
        ("document_details.css", {"ldr-btn", "ldr-btn-success"}),
        ("metrics.css", {"ldr-nav-link-btn", "ldr-nav-success"}),
        ("styles.css", {"ldr-nav-link-btn", "ldr-nav-success"}),
        ("metrics.css", {"ldr-nav-link-btn", "ldr-nav-warning"}),
        ("library.css", {"ldr-action-btn", "ldr-action-btn-pdf"}),
    ):
        assert (sheet, frozenset(classes)) in filled, (sheet, classes)
    # The off-page filter only drops pairs it can prove: collection
    # details' .ldr-btn-success never reaches the document page's link.
    assert _sheet_off_page(
        "collection_details.css", "templates/pages/document_details.html:37"
    )
    assert not _sheet_off_page(
        "document_details.css", "templates/pages/document_details.html:37"
    )
    assert not _sheet_off_page(
        "collection_details.css", "static/js/collections.js:1"
    )
    assert not _sheet_off_page(
        "chat.css", "templates/pages/document_details.html:37"
    )
    # A template's own <style> block reaches its own anchors.
    assert not _sheet_off_page(
        "templates/pages/note_detail.html",
        "templates/pages/note_detail.html:1536",
    )


@functools.cache
def _script_pages():
    """``{script key: page keys}``: the full pages whose markup
    (``_page_closure``) names the script. A script no page names, or one
    whose file name another script or Python code mentions (so it may be
    imported or loaded from there), maps to None: its pages are
    unknown."""
    named_in_code = {}
    for path in [
        *(WEB_ROOT / "static" / "js").rglob("*.js"),
        *(REPO_ROOT / "src").rglob("*.py"),
    ]:
        for name in re.findall(r"([\w.-]+\.js)\b", path.read_text("utf-8")):
            if name != path.name:
                named_in_code[name] = True
    found = {}
    for key, path in _markup_files():
        if path.suffix != ".js":
            continue
        tail = key.removeprefix("static/")
        pages = {
            page
            for page, (_, _, scripts) in _page_closure().items()
            for ref in scripts
            if tail == ref.lstrip("./").removeprefix("static/")
            or tail.endswith("/" + ref.lstrip("./").removeprefix("static/"))
        }
        found[key] = None if not pages or path.name in named_in_code else pages
    return found


def _anchor_pages(where):
    """The full pages an anchor at ``where`` (``file key:line``) renders on:
    the pages whose template closure holds its template, or that load its
    script (``_script_pages``); None when they are unknown (a fragment
    fetched by script, a template no page includes, a script loaded from
    other code)."""
    key = where.rpartition(":")[0]
    if key.endswith(".js"):
        pages = _script_pages().get(key)
    else:
        pages = {
            page
            for page, (_, templates, _) in _page_closure().items()
            if key in templates
        }
    return sorted(pages) if pages else None


def _page_rules(page):
    """``_prepare(2, ...)`` rules of every stylesheet and ``<style>`` block
    that reaches ``page`` (``_page_closure``, plus the sheets named in
    code), or of every one when the page is unknown (None); styles.css is
    the replay's base and is left out."""
    if page is None:
        reach = None
    else:
        sheets, templates, _ = _page_closure()[page]
        reach = sheets | templates | _sheets_named_in_code()
    return [
        rule
        for name, css in _stylesheets()
        if name != "styles.css" and (reach is None or name in reach)
        for rule in _prepare(2, css)
    ]


def _classed_link_subjects(selectors):
    """The ``(ancestors, subject, class set)`` of each selector whose subject
    is a classed link (``.x``, ``a.x:hover``): only classes, an ``a`` and
    pseudo-classes, with a class outside ``_STATE_CLASSES``."""
    found = []
    for ancestors, subject, _ in selectors:
        tokens = _simples(subject) or []
        if any(t[0].isalpha() and t != "a" for t in tokens):
            continue
        classes = {t[1:] for t in tokens if t.startswith(".")}
        if classes - _STATE_CLASSES:
            found.append((ancestors, subject, classes))
    return found


@functools.cache
def _theme_override_offenders(replace=None):
    """``(offenders, scenarios)`` for
    ``test_theme_link_overrides_read_on_every_page``; ``replace`` is an
    optional ``(theme file stem, css)`` that stands in for that theme's
    file."""
    anchors = _anchor_class_sets()
    base = _prepare(-1, BOOTSTRAP_BTN_CSS) + _prepare(
        0, (CSS_ROOT / "styles.css").read_text(encoding="utf-8")
    )
    root = _root_defaults()
    page_rules = functools.cache(_page_rules)
    offenders, scenarios = [], set()
    for path in THEME_FILES:
        css = path.read_text(encoding="utf-8")
        if replace and replace[0] == path.stem:
            css = replace[1]
        props = dict(root)
        props.update(_custom_properties(css))
        match = re.search(r"\[data-theme=[\"']?([\w-]+)", css)
        theme = match.group(1) if match else path.stem
        theme_rules = _prepare(1, css)
        backdrops = [_resolve(token, props) for token in _BACKDROPS]
        # Theme rules that set the ink of a classed link, with the class
        # sets they need.
        overrides = [
            (index, [classes for _, _, classes in subjects])
            for index, (_, selectors, decls) in enumerate(theme_rules)
            if "color" in decls
            and (subjects := _classed_link_subjects(selectors))
        ]
        for index, _ in overrides:
            selectors = theme_rules[index][1]
            for ancestors, subject, classes in _classed_link_subjects(
                selectors
            ):
                scope = _SCOPE.fullmatch(ancestors)
                context = "" if scope and scope.group(0) else ancestors
                for where, anchor_classes in anchors.items():
                    if not classes - _STATE_CLASSES <= anchor_classes:
                        continue
                    element = anchor_classes | classes
                    # The ink before the overrides (the cascade without
                    # every theme override that can reach this anchor) is
                    # reported for context only; the winning ink is judged.
                    dropped = {
                        other
                        for other, needs in overrides
                        if any(
                            need - _STATE_CLASSES <= anchor_classes
                            for need in needs
                        )
                    }
                    others = [
                        rule
                        for other, rule in enumerate(theme_rules)
                        if other not in dropped
                    ]
                    for page in _anchor_pages(where) or [None]:
                        for state_name, state in _STATES.items():
                            if not _compound_matches(subject, element, state):
                                continue
                            key = (path.stem, subject, where, page, state_name)
                            scenarios.add(key)
                            sheet_rules = page_rules(page)
                            values = _cascade_winners(
                                base + theme_rules + sheet_rules,
                                element,
                                state,
                                context,
                                theme,
                            )
                            if state & {"hover", "active"} and (
                                values.get("pointer-events") == "none"
                            ):
                                continue
                            before = _cascade_winners(
                                base + others + sheet_rules,
                                element,
                                state,
                                context,
                                theme,
                            )
                            ink = values.get("color")
                            fill = values.get("background")
                            if (
                                ink is not None
                                and fill
                                and _ink(ink) == ACCENT_INK
                                and _is_accent_fill(fill)
                            ):
                                continue

                            def worst(ink, fill):
                                rgb = ink and _resolve_color(_ink(ink), props)
                                stops = _fill_stops(fill, props) if fill else []
                                if not rgb or stops is None:
                                    return None
                                return min(
                                    _contrast(rgb, surface)
                                    for surface in _over(backdrops, stops)
                                )

                            now = worst(ink, fill)
                            was = worst(
                                before.get("color"), before.get("background")
                            )
                            text = (
                                f"{path.name} {ancestors} {subject} on <a> "
                                f"{where} (page {page or 'unknown'}) "
                                f"[{state_name}]: {ink} on {fill or 'the page'}"
                            )
                            if now is None:
                                offenders.append(f"{text} unresolved")
                            elif now < 4.5:
                                offenders.append(
                                    f"{text} {now:.2f}:1 (was "
                                    f"{'unresolved' if was is None else f'{was:.2f}:1'})"
                                )
    return offenders, scenarios


def test_theme_link_overrides_read_on_every_page():
    """A theme rule that sets the ink of a classed link (High Contrast's
    keep-ink overrides, ``a.btn-primary:hover``) applies on every page,
    to every ``<a>`` carrying its classes, not only on the page whose
    sheet fills that class: a class name two sheets give different
    meanings (an accent chip on one page, a plain text link on another)
    gets the override's on-fill ink on both. For each such rule, each
    anchor it matches, each full page the anchor renders on
    (``_anchor_pages``; every sheet when unknown) and each interaction
    state, the cascade is replayed with that page's sheets combined
    (``_page_rules``); wherever the override decides the ink, the ink must
    reach 4.5:1 on the winning fill composited over the page and card
    backgrounds, or on those backgrounds alone when nothing fills the
    link. Wherever the override matches, the winning ink is judged, even
    when another override gives the same ink; there is no "no worse than
    before" exemption. An ink or fill
    that does not resolve fails.

    Limits: sheets combined for one page are applied in name order, not
    load order; an anchor whose pages are unknown gets every sheet at
    once, so another page's fill for the same class can stand in for the
    one it really renders on; a fill or link colour set on an ancestor container is not
    seen (the backdrops stand in for it); an override with an ancestor
    other than the theme scope is applied as if the ancestor matched."""
    offenders, scenarios = _theme_override_offenders()
    assert len(scenarios) >= 100, "too few theme overrides were replayed"
    assert not offenders, offenders


def test_theme_link_override_replay_judges_duplicate_overrides():
    """Two theme rules giving the same link the same on-fill ink both
    decide it: neither is skipped because the other already sets that
    ink. Moving High Contrast's chip override back onto the plain
    ``.ldr-research-link`` and adding a second, unqualified copy fails."""
    path = next(p for p in THEME_FILES if p.stem == "high-contrast")
    css = path.read_text(encoding="utf-8")
    assert "a.ldr-research-chip:hover" in css
    mutated = css.replace(
        "a.ldr-research-chip:hover", "a.ldr-research-link:hover"
    ) + (
        '\n[data-theme="high-contrast"] .ldr-research-link:hover {\n'
        "  color: var(--text-on-accent);\n}\n"
    )
    offenders, _ = _theme_override_offenders(("high-contrast", mutated))
    assert any("ldr-research-link:hover" in o for o in offenders)
    assert any(
        ".ldr-research-link:hover on" in o and "a.ldr-research" not in o
        for o in offenders
    )


def test_theme_override_replay_reaches_every_page_of_a_class():
    """The override replay sees an anchor on each page it renders on: the
    skip link (base.html) on the Metrics page, and a script-built anchor on
    the page that loads the script."""
    _, scenarios = _theme_override_offenders()
    seen = {(where, page) for _, _, where, page, _ in scenarios}
    pages = {page for _, page in seen}
    assert "templates/pages/metrics.html" in pages
    assert any(
        where.startswith("templates/base.html:")
        and page.endswith("metrics.html")
        for where, page in seen
        if page
    )
    assert _anchor_pages("static/js/pages/link_analytics_render.js:1") == [
        "templates/pages/link_analytics.html"
    ]
    assert _anchor_pages("templates/pages/cost_analytics.html:1") == [
        "templates/pages/cost_analytics.html"
    ]


def test_alert_wrapped_collection_links_keep_accent_ink():
    """The create and upload result links (collection_create.js,
    collection_upload.js) are ``.ldr-btn-collections-primary`` anchors inside
    an ``.ldr-alert``, whose ``.ldr-alert a`` (0,1,1) and ``.ldr-alert
    a:hover`` (0,2,1) colours the sweep above does not apply."""
    sources = {
        name: text
        for name, text in _stylesheets()
        if name in ("styles.css", "collection_details.css")
    }
    assert ".ldr-alert a:hover" in _strip_comments(sources["styles.css"])
    base = _prepare(0, sources["styles.css"])
    own = _prepare(2, sources["collection_details.css"])
    element = frozenset({"ldr-btn-collections", "ldr-btn-collections-primary"})
    offenders = []
    themes = [(None, "", [])] + [
        (
            path.stem,
            _theme_name(path),
            _prepare(1, path.read_text(encoding="utf-8")),
        )
        for path in THEME_FILES
    ]
    for state_name, state in _STATES.items():
        for stem, theme, theme_rules in themes:
            ink, fill = _cascade(
                base + theme_rules + own, element, state, ".ldr-alert", theme
            )
            assert fill and _is_accent_fill(fill), (state_name, stem, fill)
            if ink is None or _ink(ink) != ACCENT_INK:
                offenders.append(f"{state_name} {stem or ':root'}: {ink}")
    assert not offenders, offenders


def test_anchor_scan_finds_accent_controls():
    """An empty scan would make the cascade check pass vacuously."""
    anchors = _anchor_class_sets()
    seen = set().union(*anchors.values())
    assert {
        "ldr-skip-link",
        "ldr-btn-collections-primary",
        "ldr-action-btn-rag",
        "ldr-page-link",
        "btn-primary",
    } <= seen
    created = [
        where
        for where in anchors
        if where.startswith("static/js/components/annotation_surface.js")
    ]
    assert created, "createElement('a') anchors are not scanned"


# --- Links inside filled containers ----------------------------------------
#
# A container filled from a status token (an alert's warning tint, the news
# priority banner's solid warning fill) styles its links with ``.box a``
# (0,1,1), which a theme's ``a:hover`` (0,2,1) or the container's own
# ``.box a:hover`` can recolour. The check below replays the cascade for the
# links in every such container.

_STATUS_TOKENS = frozenset(
    {
        "accent-primary",
        "accent-secondary",
        "accent-tertiary",
        "warning-color",
        "success-color",
        "error-color",
    }
)
# The surfaces a container sits on: the page and a card.
_BACKDROPS = ("bg-primary", "bg-secondary")
_RGBA_TOKEN = re.compile(
    r"rgba?\(\s*var\(--([\w-]+)-rgb\)\s*(?:,\s*([\d.]+)\s*)?\)"
)
_LINK_SUBJECT = re.compile(r"a(?::[\w-]+(?:\([^()]*\))?)*")

# Classed links rendered inside a container (beside a bare ``<a>``, which
# every container is checked with): research_form.js's action link,
# collection_create.js's result buttons, news.js's progress link.
IN_CONTAINER_ANCHORS = {
    "ldr-alert": [
        frozenset({"ldr-alert-action"}),
        frozenset({"ldr-btn-collections", "ldr-btn-collections-primary"}),
        frozenset({"ldr-btn-collections", "ldr-btn-collections-secondary"}),
    ],
    "ldr-priority-alert": [frozenset({"ms-2"})],
}

# Pre-existing link gaps: (theme, container class or modifier, ink) ->
# the worst ratio measured for that ink over the container's fill (across
# the links, states, fills and backdrops checked). Only that exact triple
# is exempt, and only down to its recorded ratio; any other ink, container
# or theme, or a lower ratio, fails. An entry that is no longer observed,
# or whose ratio has changed, fails as stale so the list stays exact. The
# accent ink and the status inks (FILL_INKS) are never exempt.
KNOWN_LINK_GAPS = {
    # --text-primary is the lightest ink these themes define, so no token
    # reads better on their 10-20% status tints over --bg-secondary.
    ("everforest-dark", "ldr-alert-info", "var(--text-primary)"): 4.48,
    ("everforest-dark", "ldr-alert-success", "var(--text-primary)"): 4.38,
    ("everforest-dark", "ldr-alert-warning", "var(--text-primary)"): 4.28,
    ("one-dark", "ldr-alert-success", "var(--text-primary)"): 4.34,
    ("one-dark", "ldr-alert-warning", "var(--text-primary)"): 4.16,
    ("palenight", "ldr-alert-danger", "var(--text-primary)"): 4.40,
    ("palenight", "ldr-alert-info", "var(--text-primary)"): 3.42,
    ("palenight", "ldr-alert-success", "var(--text-primary)"): 3.33,
    ("palenight", "ldr-alert-warning", "var(--text-primary)"): 3.44,
    # settings.css .ldr-community-note a draws --accent-tertiary, which in
    # these themes is under 4.5:1 on the bare page already (2.13:1 in
    # midnight); the note's 8% accent tint lowers it a little further.
    ("everforest-dark", "ldr-community-note", "var(--accent-tertiary)"): 4.28,
    ("flexoki-dark", "ldr-community-note", "var(--accent-tertiary)"): 3.96,
    ("gruvbox-dark", "ldr-community-note", "var(--accent-tertiary)"): 3.83,
    ("lavender", "ldr-community-note", "var(--accent-tertiary)"): 2.25,
    ("midnight", "ldr-community-note", "var(--accent-tertiary)"): 1.73,
    ("nord", "ldr-community-note", "var(--accent-tertiary)"): 2.16,
    ("rose", "ldr-community-note", "var(--accent-tertiary)"): 3.10,
    ("solarized-dark", "ldr-community-note", "var(--accent-tertiary)"): 3.67,
}


# Colour syntax _COLOR_STOP does not read: colour functions other than
# rgb()/rgba(), currentColor, and the CSS named colours other than white,
# black and transparent. A fill using any of them is unresolved rather
# than read as drawing nothing.
_NAMED_COLORS = frozenset(
    """aliceblue antiquewhite aqua aquamarine azure beige bisque
    blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse
    chocolate coral cornflowerblue cornsilk crimson cyan darkblue darkcyan
    darkgoldenrod darkgray darkgreen darkgrey darkkhaki darkmagenta
    darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen
    darkslateblue darkslategray darkslategrey darkturquoise darkviolet
    deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite
    forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray green
    greenyellow grey honeydew hotpink indianred indigo ivory khaki lavender
    lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan
    lightgoldenrodyellow lightgray lightgreen lightgrey lightpink
    lightsalmon lightseagreen lightskyblue lightslategray lightslategrey
    lightsteelblue lightyellow lime limegreen linen magenta maroon
    mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen
    mediumslateblue mediumspringgreen mediumturquoise mediumvioletred
    midnightblue mintcream mistyrose moccasin navajowhite navy oldlace
    olive olivedrab orange orangered orchid palegoldenrod palegreen
    paleturquoise palevioletred papayawhip peachpuff peru pink plum
    powderblue purple rebeccapurple red rosybrown royalblue saddlebrown
    salmon sandybrown seagreen seashell sienna silver skyblue slateblue
    slategray slategrey snow springgreen steelblue tan teal thistle tomato
    turquoise violet wheat whitesmoke yellow yellowgreen""".split()
)
_UNREAD_COLOR = re.compile(
    r"(?<![\w-])(?:(?:hsla?|hwb|lab|lch|oklab|oklch|color|color-mix|"
    r"light-dark)\(|currentcolor(?![\w-])|(?:"
    + "|".join(sorted(_NAMED_COLORS))
    + r")(?![\w-]))",
    re.IGNORECASE,
)


def _fill_stops(value, props):
    """``[(rgb, alpha)]`` for each colour stop of a fill (the alternatives a
    gradient draws), ``[]`` when it draws nothing, ``None`` when a stop
    does not resolve under ``props`` or the fill uses a colour syntax the
    stop pattern does not read (``hsl()``, ``color-mix()``, a named
    colour; see ``_UNREAD_COLOR``)."""
    value = _ink(value)
    if value in ("transparent", "none", "inherit", "initial", "unset"):
        return []
    # url(...) and var() names cannot hold a colour the check would miss.
    bare = re.sub(r"url\([^()]*\)|var\(--[\w-]+", " ", value)
    if _UNREAD_COLOR.search(bare):
        return None
    stops = []
    for stop in _COLOR_STOP.findall(value):
        if stop == "transparent":
            continue
        rgba = _RGBA_TOKEN.fullmatch(stop)
        literal = _parse_color(stop)
        if rgba:
            rgb = _resolve_color(f"var(--{rgba.group(1)})", props)
            alpha = float(rgba.group(2)) if rgba.group(2) else 1.0
        elif literal:
            rgb, alpha = literal
        else:
            rgb, alpha = _resolve_color(stop, props), 1.0
        if rgb is None:
            return None
        stops.append((rgb, alpha))
    return stops


def _over(backgrounds, stops):
    if not stops:
        return backgrounds
    return [
        tuple(alpha * f + (1 - alpha) * b for f, b in zip(rgb, background))
        for background in backgrounds
        for rgb, alpha in stops
    ]


def _status_fill(value):
    tokens = re.findall(r"var\(--([\w-]+?)(?:-rgb)?\)", _ink(value))
    accent, _, _ = _aliases()
    return bool(tokens) and all(
        token in _STATUS_TOKENS or token in accent for token in tokens
    )


def _filled_link_containers():
    """``{container class: [(link sheet css, fill sheet css, modifier)]}``:
    the containers whose links a stylesheet styles (``.box a``,
    ``.box a:hover``) and whose fill, set on the container class or on a
    ``.box-*`` modifier, draws a status token."""
    sources = list(_stylesheets())
    link_sheets = {}
    for _, css in sources:
        for selector_list, decls in _parse_rules(css):
            if selector_list.startswith("@") or not _TRACKED & set(decls):
                continue
            for selector in _split_top(selector_list, ","):
                context, subject = _subject_split(selector)
                if re.fullmatch(
                    r"\.[\w-]+", context
                ) and _LINK_SUBJECT.fullmatch(subject):
                    link_sheets.setdefault(context[1:], []).append(css)
    found = {}
    for container, sheets in link_sheets.items():
        own = re.compile(rf"\.({re.escape(container)}(?:-[\w-]+)?)")
        for _, css in sources:
            for selector_list, decls in _parse_rules(css):
                background = decls.get(
                    "background-color", decls.get("background")
                )
                if not background or not _status_fill(background):
                    continue
                for selector in _split_top(selector_list, ","):
                    match = own.fullmatch(selector)
                    if not match:
                        continue
                    modifier = match.group(1)
                    for link_css in dict.fromkeys(sheets):
                        found.setdefault(container, []).append(
                            (link_css, css, modifier)
                        )
    return found


def _container_link_offenders():
    base = _prepare(0, (CSS_ROOT / "styles.css").read_text(encoding="utf-8"))
    styles = (CSS_ROOT / "styles.css").read_text(encoding="utf-8")
    root = _root_defaults()
    themes = [(None, "", [], root)]
    for path in THEME_FILES:
        css = path.read_text(encoding="utf-8")
        props = dict(root)
        props.update(_custom_properties(css))
        themes.append((path.stem, _theme_name(path), _prepare(1, css), props))
    sheets = [css for name, css in _stylesheets() if not name.endswith(".html")]
    offenders, gaps, checked = [], {}, 0
    for container, pairs in _filled_link_containers().items():
        links = [frozenset(), *IN_CONTAINER_ANCHORS.get(container, [])]
        for link_css, fill_css, modifier in dict.fromkeys(pairs):
            box = frozenset({container, modifier})
            for stem, theme, theme_rules, props in themes:
                backdrops = [_resolve(name, props) for name in _BACKDROPS]
                outer_rules = (
                    base
                    + theme_rules
                    + [
                        rule
                        for css in dict.fromkeys((fill_css, link_css))
                        if css != styles
                        for rule in _prepare(2, css)
                    ]
                )
                outer = _cascade_winners(
                    outer_rules, box, frozenset(), "", theme, "div"
                )
                box_ink = outer.get("color") or "var(--text-primary)"
                box_fill = outer.get("background") or ""
                box_stops = _fill_stops(box_fill, props)
                if box_stops is None:
                    offenders.append(
                        f".{modifier} [{stem or ':root'}]: unresolved fill "
                        f"{box_fill}"
                    )
                    continue
                if not box_stops:
                    # The cascade leaves this container unfilled here.
                    continue
                surfaces = _over(backdrops, box_stops)
                for classes in links:
                    # A classed link also takes the rules of the sheets that
                    # style its classes (collection_details.css's buttons).
                    rules = outer_rules + [
                        rule
                        for css in sheets
                        if css not in (styles, fill_css, link_css)
                        and any(
                            re.search(rf"\.{re.escape(c)}(?![\w-])", css)
                            for c in classes
                        )
                        for rule in _prepare(2, css)
                    ]
                    for state_name, state in _STATES.items():
                        values = _cascade_winners(
                            rules, classes, state, f".{container}", theme
                        )
                        ink = values.get("color")
                        if ink is None or _ink(ink) in (
                            "inherit",
                            "currentColor",
                        ):
                            ink = box_ink
                        where = (
                            f".{modifier} a.{'.'.join(sorted(classes))} "
                            f"[{state_name}, {stem or ':root'}]"
                        )
                        ink_rgb = _resolve_color(_ink(ink), props)
                        link_stops = _fill_stops(
                            values.get("background") or "", props
                        )
                        if ink_rgb is None or link_stops is None:
                            offenders.append(f"{where}: unresolved {ink}")
                            continue
                        checked += 1
                        own_fill = values.get("background")
                        if (
                            _ink(ink) == ACCENT_INK
                            and own_fill
                            and _is_accent_fill(_ink(own_fill))
                            and all(a == 1 for _, a in link_stops)
                        ):
                            # Accent ink on an opaque accent fill: the pair
                            # test_accent_ink_contrast checks.
                            worst = 21.0
                        else:
                            worst = min(
                                _contrast(ink_rgb, surface)
                                for surface in _over(surfaces, link_stops)
                            )
                        if worst < 4.5:
                            key = (stem, modifier, _ink(ink))
                            floor = KNOWN_LINK_GAPS.get(key)
                            if (
                                floor is not None
                                and _ink(ink) not in FILL_INKS
                                and round(worst, 2) >= floor
                            ):
                                gaps[key] = min(gaps.get(key, 21.0), worst)
                            else:
                                offenders.append(
                                    f"{where}: {ink} {worst:.2f}:1"
                                )
                        outline = values.get("outline")
                        if "focus" not in state or not outline:
                            continue
                        if "currentcolor" in outline.lower():
                            ring = ink_rgb
                        else:
                            stops = _COLOR_STOP.findall(_ink(outline))
                            ring = stops and _resolve_color(stops[0], props)
                        if not ring:
                            continue
                        worst = min(_contrast(ring, s) for s in surfaces)
                        if worst < 3:
                            offenders.append(
                                f"{where}: focus outline {outline} {worst:.2f}:1"
                            )
    return offenders, gaps, checked


def test_links_in_filled_containers_stay_readable():
    """Links inside a container filled from a status token (``.ldr-alert``
    and its tints, ``.ldr-priority-alert``) keep at least 4.5:1 against the
    container's fill, composited over the page and card backgrounds, at
    rest, on hover, on keyboard focus, when visited and when pressed, in
    every theme; a focus outline keeps 3:1. A link or container fill that
    does not resolve fails. The only exemptions are the exact pre-existing
    (theme, container, ink) gaps in KNOWN_LINK_GAPS, each down to its
    recorded ratio. The cascade is styles.css
    (rank 0), the theme (rank 1), and the sheets declaring the link rule
    and the fill (rank 2), so ``[data-theme="high-contrast"] a:hover``
    (0,2,1) meets ``.ldr-priority-alert a`` (0,1,1). An ``inherit`` ink
    takes the container's.

    Limits: containers are found from ``.box a`` rules with a single-class
    ancestor and a fill on ``.box`` or a ``.box-*`` modifier; the classed
    links checked besides a bare ``<a>`` are those in IN_CONTAINER_ANCHORS;
    nested containers and the page's own fills under the backdrop tokens
    are not modelled."""
    offenders, gaps, checked = _container_link_offenders()
    assert checked >= 1000, "too few container links were checked"
    assert not offenders, offenders
    stale = {
        key: (floor, round(gaps[key], 2) if key in gaps else None)
        for key, floor in KNOWN_LINK_GAPS.items()
        if key not in gaps or round(gaps[key], 2) != floor
    }
    assert not stale, (
        f"KNOWN_LINK_GAPS entries to update (recorded, now): {stale}"
    )


def test_container_link_scan_finds_the_alerts():
    """An empty scan would make the container check pass vacuously."""
    found = _filled_link_containers()
    assert {"ldr-alert", "ldr-priority-alert"} <= set(found)
    modifiers = {
        modifier for pairs in found.values() for _, _, modifier in pairs
    }
    assert {"ldr-alert-warning", "ldr-alert-danger", "ldr-priority-alert"} <= (
        modifiers
    )
    seen = set().union(*_anchor_class_sets().values())
    listed = set().union(
        *(set().union(*v) for v in IN_CONTAINER_ANCHORS.values())
    )
    assert listed <= seen, listed - seen