autumn-web 0.10.0

An opinionated, convention-over-configuration web framework for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
//! Framework-owned widget component CSS (issue #1215).
//!
//! Every `autumn-*` semantic class emitted by [`crate::form`], [`crate::widgets`],
//! [`crate::wizard`], [`crate::ui::pagination`], [`crate::storage::form_helper`],
//! and [`crate::job_tracking`] is backed by a rule here, so widgets render
//! styled out of the box — Tailwind or not — instead of shipping an unbacked
//! class hook the app must style itself.
//!
//! # Example
//!
//! Link the bundle from your base layout to style every widget with a single
//! `<link>`, no Tailwind build required:
//!
//! ```html
//! <link rel="stylesheet" href="/static/css/autumn-widgets.css">
//! ```
//!
//! Re-theme by overriding the [`crate::ui::tokens`] custom properties on
//! `:root` in your own stylesheet (loaded after this one) — not by forking
//! these rules.

// One macro for each opt-in widget family (#3408). It gives the family's CSS
// file when its feature is on and `""` when it is off. `concat!` expands
// these, so the bundles stay `const`.
#[cfg(feature = "widgets-search")]
macro_rules! search_css {
    () => {
        include_str!("widgets/search.css")
    };
}
#[cfg(not(feature = "widgets-search"))]
macro_rules! search_css {
    () => {
        ""
    };
}
#[cfg(feature = "widgets-social")]
macro_rules! social_css {
    () => {
        include_str!("widgets/social.css")
    };
}
#[cfg(not(feature = "widgets-social"))]
macro_rules! social_css {
    () => {
        ""
    };
}
#[cfg(feature = "widgets-workflow")]
macro_rules! workflow_css {
    () => {
        include_str!("widgets/workflow.css")
    };
}
#[cfg(not(feature = "widgets-workflow"))]
macro_rules! workflow_css {
    () => {
        ""
    };
}
#[cfg(feature = "widgets-charts")]
macro_rules! charts_css {
    () => {
        include_str!("widgets/charts.css")
    };
}
#[cfg(not(feature = "widgets-charts"))]
macro_rules! charts_css {
    () => {
        ""
    };
}

/// The core rules, then the CSS of each enabled widget family.
macro_rules! component_css {
    () => {
        concat!(
            include_str!("widgets.css"),
            search_css!(),
            social_css!(),
            workflow_css!(),
            charts_css!()
        )
    };
}

/// Component rules only: `.autumn-field`, `.autumn-nav`, `.autumn-modal`, etc.
/// It holds the rules of each enabled widget family (#3408).
///
/// References the [`crate::ui::tokens`] custom properties (`var(--primary)`,
/// `var(--border)`, `var(--radius)`, …) rather than hard-coded colors, so an
/// app re-themes by overriding tokens. Does not itself define those
/// variables — see [`WIDGETS_CSS`] for the self-contained bundle.
pub const WIDGETS_COMPONENT_CSS: &str = component_css!();

/// Self-contained bundle: [`WIDGETS_COMPONENT_CSS`] prefixed with the shared
/// design tokens ([`crate::ui::tokens::TOKENS_CSS`]).
///
/// A single `<link>` to [`WIDGETS_CSS_PATH`] styles every widget with no
/// other stylesheet — Tailwind or otherwise — required.
pub const WIDGETS_CSS: &str = concat!(include_str!("tokens.css"), "\n", component_css!());

/// URL of the framework-served widget stylesheet.
///
/// The default Autumn server mounts this asset automatically. Link it from
/// your base layout — `link rel="stylesheet" href=(autumn_web::ui::WIDGETS_CSS_PATH);`
/// — so every `autumn-*` widget class renders styled with zero app-authored CSS.
pub const WIDGETS_CSS_PATH: &str = "/static/css/autumn-widgets.css";

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ui::tokens::test_support::{Scheme, color, contrast};
    use std::fmt::Write as _;

    /// The core rules and every widget family file, whatever the features
    /// (#3408). The rule tests read this, so they cover each family file.
    const ALL_CSS: &str = concat!(
        include_str!("widgets.css"),
        include_str!("widgets/search.css"),
        include_str!("widgets/social.css"),
        include_str!("widgets/workflow.css"),
        include_str!("widgets/charts.css")
    );

    /// Each CSS file that the server can join into the bundle (#3408).
    const CSS_FILES: [(&str, &str); 5] = [
        ("widgets.css", include_str!("widgets.css")),
        ("widgets/search.css", include_str!("widgets/search.css")),
        ("widgets/social.css", include_str!("widgets/social.css")),
        ("widgets/workflow.css", include_str!("widgets/workflow.css")),
        ("widgets/charts.css", include_str!("widgets/charts.css")),
    ];

    #[test]
    fn widgets_css_bundles_tokens_and_components() {
        assert!(WIDGETS_CSS.contains(crate::ui::tokens::TOKENS_CSS));
        assert!(WIDGETS_CSS.contains(WIDGETS_COMPONENT_CSS));
    }

    #[test]
    fn component_css_references_tokens_not_hardcoded_colors() {
        for token_var in ["var(--primary)", "var(--border)", "var(--radius)"] {
            assert!(
                ALL_CSS.contains(token_var),
                "the component CSS should reference {token_var}"
            );
        }
    }

    /// Issue #2354: the renamed widget classes must each have a rule in the
    /// component stylesheet. The selector boundary check keeps `.autumn-card`
    /// from passing on the strength of `.autumn-card__header` alone.
    #[test]
    fn renamed_widget_classes_are_backed_by_component_css() {
        fn has_rule(css: &str, class: &str) -> bool {
            let needle = format!(".{class}");
            css.match_indices(&needle).any(|(i, _)| {
                matches!(
                    css[i + needle.len()..].chars().next(),
                    Some('{' | ' ' | ',' | ':' | '\n' | '\t')
                )
            })
        }

        for class in [
            "autumn-alert",
            "autumn-alert--info",
            "autumn-alert__dismiss-toggle",
            "autumn-badge",
            "autumn-badge--neutral",
            "autumn-wizard-progress",
            "autumn-wizard-step",
            "autumn-wizard-step__number",
            "autumn-theme-toggle",
            "autumn-theme-toggle__option",
            "autumn-card",
            "autumn-card__header",
            "autumn-card__title",
            "autumn-card__body",
            "autumn-card__footer",
            "autumn-stat-card",
            "autumn-stat-card__label",
            "autumn-stat-card__value",
            "autumn-stat-card__link",
            "autumn-search-empty",
            "autumn-autocomplete-empty",
            "autumn-alert__icon-svg",
            "autumn-active",
        ] {
            assert!(
                has_rule(ALL_CSS, class),
                "widget emits .{class} but the component CSS has no rule for it"
            );
        }
    }

    /// Wayfinder audit (keyboard walkthrough, tier 1): four widget rules
    /// set `outline: none` and signalled focus with `border-color` +
    /// `box-shadow` only. Forced-colors mode (Windows High Contrast) strips
    /// `box-shadow`, so keyboard users lost the focus indicator (WCAG 2.4.7).
    /// A transparent outline is repainted in the system focus colour under
    /// forced-colors and is invisible otherwise — but only on the `:focus`
    /// state: on the base selector every unfocused field would be outlined
    /// too, and focus would be indistinguishable.
    #[test]
    fn widget_focus_states_keep_an_outline_and_base_rules_have_none() {
        assert!(
            !ALL_CSS.contains("outline: none"),
            "`outline: none` leaves no focus indicator under forced-colors mode"
        );
        for selector in [
            ".autumn-field__input:focus {",
            ".autumn-submit:focus {",
            ".autumn-search__input:focus {",
            ".autumn-autocomplete__input:focus {",
        ] {
            let start = ALL_CSS
                .find(selector)
                .unwrap_or_else(|| panic!("missing `{selector}` rule"));
            let end = ALL_CSS[start..].find('}').unwrap() + start;
            assert!(
                ALL_CSS[start..end].contains("outline: 2px solid transparent;"),
                "`{selector}` must declare a transparent outline"
            );
        }
        assert_eq!(
            ALL_CSS.matches("outline: 2px solid transparent;").count(),
            4,
            "the transparent outline belongs on the four :focus rules only"
        );
    }
    /// Resolve `#hex` / `var(--x)` / `var(--x, #hex)` / `color-mix(...)`
    /// against the shipped tokens in `scheme`, the way a browser does: the
    /// token wins, and the fallback applies only when the token is undefined.
    fn resolve(value: &str, scheme: Scheme) -> String {
        let value = value.trim();
        if let Some(args) = value
            .strip_prefix("color-mix(in srgb,")
            .and_then(|v| v.strip_suffix(')'))
        {
            // `<color> <pct>%, <color>`
            let (first, second) = args.split_once("%,").unwrap();
            let (a, pct) = first.trim().rsplit_once(' ').unwrap();
            let weight = pct.parse::<u32>().unwrap();
            let (a, b) = (resolve(a, scheme), resolve(second, scheme));
            let channel =
                |h: &str, i: usize| u32::from_str_radix(&h[1 + i * 2..3 + i * 2], 16).unwrap();
            let mixed = (0..3).fold(String::from("#"), |mut out, i| {
                let c = (channel(&a, i) * weight + channel(&b, i) * (100 - weight) + 50) / 100;
                write!(out, "{c:02x}").unwrap();
                out
            });
            return mixed;
        }
        let Some(inner) = value.strip_prefix("var(").and_then(|v| v.strip_suffix(')')) else {
            return value.to_owned();
        };
        let (name, fallback) = inner
            .split_once(',')
            .map_or((inner, None), |(n, f)| (n, Some(f)));
        color(name.trim(), scheme)
            .or_else(|| fallback.map(|f| resolve(f, scheme)))
            .unwrap_or_else(|| panic!("`{name}` is neither a token nor has a fallback"))
    }

    fn rule_body(selector: &str) -> &'static str {
        let at = ALL_CSS
            .find(&format!("{selector} {{"))
            .unwrap_or_else(|| panic!("missing `{selector}` rule"));
        let body = &ALL_CSS[at..];
        &body[..body.find('}').unwrap()]
    }

    fn declared(selector: &str, prop: &str, scheme: Scheme) -> String {
        let line = rule_body(selector)
            .lines()
            .filter_map(|l| l.trim().strip_prefix(&format!("{prop}:")))
            .next_back()
            .unwrap_or_else(|| panic!("`{selector}` has no `{prop}`"));
        resolve(line.trim_end_matches(';'), scheme)
    }

    /// Status chips must not derive their text from `--text`: a dark scheme
    /// makes `--text` light. Each chip uses its own `-text` token.
    #[test]
    fn status_chip_text_is_independent_of_the_theme_foreground() {
        for selector in [
            ".autumn-badge--success",
            ".autumn-badge--warning",
            ".autumn-alert--success",
            ".autumn-alert--warning",
        ] {
            assert!(
                !rule_body(selector).contains("var(--text"),
                "{selector} reads --text"
            );
        }
    }

    /// Wayfinder audit (contrast, tier 1), extended by #3396 to both schemes:
    /// computed text/background pairs against the shipped tokens. WCAG 1.4.3
    /// needs 4.5:1 for normal-size text.
    #[test]
    fn widget_text_meets_aa_contrast_against_its_own_background() {
        let mut failures = Vec::new();
        for scheme in Scheme::BOTH {
            for (selector, bg) in [
                (".autumn-badge--neutral", "background"),
                (".autumn-badge--info", "background"),
                (".autumn-badge--success", "background"),
                (".autumn-badge--warning", "background"),
                (".autumn-badge--danger", "background"),
                (".autumn-alert--info", "background"),
                (".autumn-alert--success", "background"),
                (".autumn-alert--warning", "background"),
                (".autumn-alert--error", "background"),
                (".autumn-job-status__success", ""),
                (".autumn-job-status__error", ""),
                (".autumn-submit", "background-color"),
                (".autumn-pager__current", "background-color"),
                (".autumn-modal__confirm", "background-color"),
                (".autumn-modal__confirm--danger", "background-color"),
                (".autumn-hero__cta--primary", "background-color"),
                (
                    ".autumn-theme-toggle__option[aria-pressed=\"true\"]",
                    "background",
                ),
                (".autumn-wizard-step__number", "background-color"),
                (
                    ".autumn-wizard-step--completed .autumn-wizard-step__number",
                    "background-color",
                ),
                (
                    ".autumn-wizard-step--current .autumn-wizard-step__number",
                    "background-color",
                ),
                (
                    ".autumn-wizard-step--upcoming .autumn-wizard-step__number",
                    "background-color",
                ),
            ] {
                let bg = if bg.is_empty() {
                    resolve("var(--surface)", scheme)
                } else {
                    declared(selector, bg, scheme)
                };
                let ratio = contrast(&declared(selector, "color", scheme), &bg);
                if ratio < 4.5 {
                    failures.push(format!("{scheme:?} {selector}: {ratio:.2}:1"));
                }
            }
        }
        assert!(failures.is_empty(), "below 4.5:1 — {failures:?}");
    }

    /// Issue #3396: a dark scheme makes `--primary` and `--danger` light, so
    /// text on a token fill must use `--on-primary` / `--on-danger`, not white.
    #[test]
    fn no_white_text_on_a_token_fill() {
        for rule in ALL_CSS.split('}') {
            let white = ["color: #fff;", "color: #ffffff;", "color: white;"]
                .iter()
                .any(|w| rule.contains(w));
            let token_fill = rule
                .lines()
                .any(|l| l.trim().starts_with("background") && l.contains("var(--"));
            assert!(
                !(white && token_fill),
                "white text on a token fill:{rule}}}"
            );
        }
    }

    /// The alert icon glyph sits on a `currentColor` circle. It must stand
    /// out from the circle in both schemes (WCAG 1.4.11, 3:1).
    #[test]
    fn alert_icon_glyph_is_visible_in_both_schemes() {
        let glyph = declared(".autumn-alert__icon-glyph", "fill", Scheme::Light);
        assert!(glyph.starts_with('#'), "{glyph}");
        for scheme in Scheme::BOTH {
            for variant in ["info", "success", "warning", "error"] {
                let circle = declared(&format!(".autumn-alert--{variant}"), "color", scheme);
                let glyph = declared(".autumn-alert__icon-glyph", "fill", scheme);
                let ratio = contrast(&glyph, &circle);
                assert!(ratio >= 3.0, "{scheme:?} {variant}: {ratio:.2}:1");
            }
        }
    }

    /// A widget that paints a token surface also sets its text color.
    /// Otherwise it inherits the page color, which can be the other scheme.
    #[test]
    fn a_token_surface_sets_its_own_text_color() {
        let mut missing = Vec::new();
        let mut rest = ALL_CSS;
        while let Some(open) = rest.find('{') {
            let selector = rest[..open].rsplit(['}', '/']).next().unwrap_or("").trim();
            let close = open + rest[open..].find('}').unwrap();
            let body = &rest[open..close];
            let paints = body.lines().any(|l| {
                let l = l.trim();
                l.starts_with("background") && l.contains("var(--surface")
            });
            let colors = body.lines().any(|l| l.trim().starts_with("color:"));
            if paints && !colors && !selector.contains(':') {
                missing.push(selector.to_owned());
            }
            rest = &rest[close + 1..];
        }
        assert!(missing.is_empty(), "surface without color: {missing:?}");
    }

    /// The modal paints the token surface. The browser's dialog colors are
    /// not the tokens, and in dark mode they hide the dialog edge.
    #[test]
    fn modal_uses_the_token_surface() {
        let body = rule_body(".autumn-modal");
        for declaration in [
            "background: var(--surface);",
            "color: var(--text);",
            "border: 1px solid var(--border);",
        ] {
            assert!(body.contains(declaration), "missing `{declaration}`:{body}");
        }
    }

    /// An unclosed block turns every later rule into part of it, for example
    /// an `@media` query. Braces must balance, and never go below zero.
    #[test]
    fn component_css_braces_balance() {
        for (file, text) in CSS_FILES {
            let mut css = String::new();
            let mut rest = text;
            while let Some(start) = rest.find("/*") {
                css.push_str(&rest[..start]);
                let end = rest[start..]
                    .find("*/")
                    .map_or(rest.len(), |e| start + e + 2);
                rest = &rest[end..];
            }
            css.push_str(rest);
            let mut depth = 0_i32;
            for c in css.chars() {
                match c {
                    '{' => depth += 1,
                    '}' => depth -= 1,
                    _ => {}
                }
                assert!(depth >= 0, "a `}}` has no matching `{{` in {file}");
            }
            assert_eq!(depth, 0, "{depth} unclosed `{{` in {file}");
        }
    }

    /// The drawer paints the token surface and has a rule for each edge,
    /// size and close button.
    #[test]
    fn drawer_uses_tokens_and_styles_every_variant() {
        let body = rule_body(".autumn-drawer");
        for declaration in ["background: var(--surface);", "color: var(--text);"] {
            assert!(body.contains(declaration), "missing `{declaration}`:{body}");
        }
        for selector in [
            ".autumn-drawer--left",
            ".autumn-drawer--right",
            ".autumn-drawer--top",
            ".autumn-drawer--bottom",
            ".autumn-drawer--sm",
            ".autumn-drawer--lg",
            ".autumn-drawer--full",
            ".autumn-modal--sm",
            ".autumn-modal--lg",
            ".autumn-modal--full",
            ".autumn-modal__close",
            ".autumn-drawer__close",
        ] {
            assert!(ALL_CSS.contains(selector), "no rule: {selector}");
        }
    }

    /// The exit animation cancels the region gap, which density scales.
    #[test]
    fn toast_exit_follows_the_density_gap() {
        assert!(rule_body(".autumn-toast-region").contains("gap: var(--space-2);"));
        let at = ALL_CSS.find("@keyframes autumn-toast-out").unwrap();
        let frames = &ALL_CSS[at..at + 400];
        assert!(
            frames.contains("margin-top: calc(-1 * var(--space-2));"),
            "{frames}"
        );
    }

    /// The region is a popover. The UA popover box (centred, bordered,
    /// `Canvas` background) is reset, and `display` wins over the UA
    /// `display: none`, so toasts also show without JS (#3399).
    #[test]
    fn toast_region_resets_the_popover_box() {
        let body = rule_body(".autumn-toast-region");
        for declaration in [
            "display: flex;",
            "inset: auto 1rem 1rem auto;",
            "margin: 0;",
            "padding: 0;",
            "border: 0;",
            "background: transparent;",
            "color: inherit;",
            "overflow: visible;",
        ] {
            assert!(body.contains(declaration), "missing `{declaration}`:{body}");
        }
        assert!(rule_body(".autumn-toast-region::backdrop").contains("display: none;"));
    }

    /// The runtime sets `--autumn-toast-duration`. The default is 5 s.
    #[test]
    fn toast_dwell_reads_the_duration_property() {
        assert!(
            rule_body(".autumn-toast").contains(
                "autumn-toast-out 400ms ease-in var(--autumn-toast-duration, 5s) forwards"
            ),
            "{}",
            rule_body(".autumn-toast")
        );
    }

    /// The stylesheet with LF line ends. A Windows checkout can have CRLF.
    fn lf_css() -> String {
        ALL_CSS.replace("\r\n", "\n")
    }

    /// WCAG 2.2.1: hover or focus on a toast pauses every toast timer.
    #[test]
    fn toast_timer_pauses_on_hover_and_focus() {
        let list = ".autumn-toast-region:hover .autumn-toast,\n\
                    .autumn-toast-region:focus-within .autumn-toast,\n\
                    .autumn-toast:hover,\n\
                    .autumn-toast:focus-within {";
        assert!(lf_css().contains(list), "missing pause selectors");
        // Only the exit animation pauses. The entrance of a new toast runs.
        assert!(
            rule_body(".autumn-toast:focus-within")
                .contains("animation-play-state: running, paused;")
        );
    }

    /// A persistent toast fades in and never fades out.
    #[test]
    fn persistent_toast_has_no_exit_animation() {
        let body = rule_body(".autumn-toast--persistent");
        assert!(
            body.contains("animation: autumn-toast-in 200ms ease-out;"),
            "{body}"
        );
        assert!(!body.contains("autumn-toast-out"), "{body}");
    }

    /// Reduced motion: no slide, the same dwell, and no animation at all on
    /// a persistent toast.
    #[test]
    fn reduced_motion_keeps_the_dwell_and_the_persistent_toast() {
        let css = lf_css();
        let at = css
            .find("@media (prefers-reduced-motion: reduce) {\n    /* A 0s entrance")
            .expect("reduced-motion toast block");
        let rest = &css[at..];
        let block = &rest[..rest.find("\n}\n").expect("block end")];
        assert!(
            block.contains(
                "autumn-toast-in 0s,\n            \
                 autumn-toast-out 0s linear var(--autumn-toast-duration, 5s) forwards;"
            ),
            "{block}"
        );
        assert!(
            block.contains(".autumn-toast--persistent {\n        animation: none;"),
            "{block}"
        );
    }

    /// The close button stays hidden until the runtime shows it, and its
    /// target is at least 24 px (WCAG 2.5.8).
    #[test]
    fn toast_close_button_is_hidden_without_js_and_large_enough() {
        assert!(rule_body(".autumn-toast__close[hidden]").contains("display: none;"));
        // The toast sets `display: flex`, which beats the UA `[hidden]` rule.
        assert!(rule_body(".autumn-toast[hidden]").contains("display: none;"));
        let body = rule_body(".autumn-toast__close");
        assert!(body.contains("min-width: 1.5rem;"), "{body}");
        assert!(body.contains("min-height: 1.5rem;"), "{body}");
        assert!(
            rule_body(".autumn-toast__close:focus-visible")
                .contains("outline: 2px solid var(--primary);")
        );
    }

    // ── tooltip (#3404) ──────────────────────────────────────────────

    /// The block that starts at `head`, up to its closing brace.
    fn css_block(head: &str) -> String {
        let css = lf_css();
        let at = css.find(head).unwrap_or_else(|| panic!("missing `{head}`"));
        let end = css[at..].find("\n}\n").expect("block end");
        css[at..at + end].to_owned()
    }

    /// The text is `display: none` until hover or keyboard focus, so a
    /// hidden text adds no scroll overflow. `aria-describedby` still reads
    /// it. A popover shows only when the browser opens it.
    #[test]
    fn tooltip_is_hidden_until_hover_or_keyboard_focus() {
        let body = rule_body(".autumn-tooltip__bubble");
        assert!(body.contains("display: none;"), "{body}");
        assert!(body.contains("pointer-events: auto;"), "{body}");
        // One rule for each selector. A browser without `:has()` or
        // `:popover-open` drops a whole selector list, so a list would lose
        // the hover baseline too.
        for selector in [
            ".autumn-tooltip:hover > .autumn-tooltip__bubble:not([popover])",
            ".autumn-tooltip:has(:focus-visible) > .autumn-tooltip__bubble:not([popover])",
            ".autumn-tooltip > .autumn-tooltip__bubble[popover]:popover-open",
        ] {
            assert!(
                lf_css().contains(&format!("\n{selector} {{\n    display: block;\n}}")),
                "`{selector}` must be a rule of its own"
            );
        }
        // Browsers without `:has()` show on any focus.
        let block =
            css_block("@supports not selector(:has(a)) {\n    .autumn-tooltip:focus-within");
        assert!(block.contains("display: block;"), "{block}");
        // A long link trigger stays in the text flow.
        assert!(rule_body(".autumn-tooltip").contains("display: inline;"));
    }

    /// WCAG 1.4.13 (hoverable): a transparent bridge covers the gap between
    /// the trigger and the text, so the pointer can move onto the text.
    #[test]
    fn tooltip_has_a_hover_bridge_over_the_gap() {
        let body = rule_body(".autumn-tooltip__bubble::before");
        assert!(body.contains("content: \"\";"), "{body}");
        assert!(body.contains("position: absolute;"), "{body}");
        assert!(
            body.contains("inset: calc(-1 * var(--autumn-tooltip-gap));"),
            "{body}"
        );
    }

    /// The dismiss helper sets `hidden`. An empty text never shows. Both win
    /// over each show rule.
    #[test]
    fn a_dismissed_or_empty_tooltip_does_not_show() {
        assert!(
            lf_css().contains(
                ".autumn-tooltip__bubble[hidden],\n.autumn-tooltip__bubble:empty {\n    \
                 display: none !important;"
            ),
            "missing the [hidden] / :empty rule"
        );
    }

    /// Anchor positioning applies only with `anchor-scope`. Without it, all
    /// tooltips on a page share one anchor name.
    #[test]
    fn tooltip_anchor_positioning_needs_anchor_scope() {
        let root = rule_body(".autumn-tooltip");
        assert!(root.contains("anchor-name: --autumn-tooltip;"), "{root}");
        assert!(root.contains("anchor-scope: --autumn-tooltip;"), "{root}");
        let block = css_block("@supports (anchor-scope: all) {");
        for declaration in [
            "position: fixed;",
            "position-anchor: --autumn-tooltip;",
            "position-try-fallbacks: flip-block;",
            // Start and end try the other side, then above and below, so
            // the text never covers its trigger in a narrow viewport.
            "position-try-fallbacks: flip-inline, --autumn-tooltip-above, --autumn-tooltip-below;",
            // `self-*` follows the tooltip's direction in an RTL subtree.
            "position-area: self-inline-start;",
            "position-area: self-inline-end;",
            // The text stays off its trigger when no block area fits.
            "align-self: unsafe end;",
            "align-self: unsafe start;",
        ] {
            assert!(
                block.contains(declaration),
                "missing `{declaration}`:{block}"
            );
        }
        for name in ["--autumn-tooltip-above", "--autumn-tooltip-below"] {
            assert!(
                lf_css().contains(&format!("@position-try {name} {{")),
                "missing @position-try {name}"
            );
        }
        for placement in ["top", "bottom", "start", "end"] {
            assert!(
                lf_css().contains(&format!(
                    ".autumn-tooltip--{placement} > .autumn-tooltip__bubble"
                )),
                "no rule for the {placement} placement"
            );
        }
    }

    /// On Chromium the runtime makes the text a `popover="hint"`. The CSS
    /// resets the UA popover box and places it at the implicit anchor.
    #[test]
    fn tooltip_popover_resets_the_ua_box() {
        let body = rule_body(".autumn-tooltip__bubble[popover]");
        for declaration in [
            "position: fixed;",
            "position-anchor: auto;",
            "inset: auto;",
            "overflow: visible;",
        ] {
            assert!(body.contains(declaration), "missing `{declaration}`:{body}");
        }
    }

    /// Forced colors paint a transparent border, so the text keeps an edge.
    #[test]
    fn tooltip_keeps_an_edge_in_forced_colors() {
        assert!(rule_body(".autumn-tooltip__bubble").contains("border: 1px solid transparent;"));
    }

    #[test]
    fn tooltip_sits_above_toasts_and_has_no_motion_when_reduced() {
        assert!(rule_body(".autumn-tooltip__bubble").contains("z-index: var(--z-tooltip);"));
        let block =
            css_block("@media (prefers-reduced-motion: reduce) {\n    .autumn-tooltip__bubble");
        assert!(block.contains("transition: none;"), "{block}");
    }

    #[test]
    fn tooltip_text_meets_aa_contrast_in_both_schemes() {
        for scheme in Scheme::BOTH {
            let ratio = contrast(
                &declared(".autumn-tooltip__bubble", "color", scheme),
                &declared(".autumn-tooltip__bubble", "background", scheme),
            );
            assert!(ratio >= 4.5, "{scheme:?}: {ratio:.2}:1");
        }
    }

    /// WCAG 1.4.11: a text input's border is its only visible boundary, so
    /// it uses `--control-border` (3:1), not the decorative `--border`.
    #[test]
    fn text_inputs_use_the_control_border() {
        for selector in [
            ".autumn-field__input",
            ".autumn-search__input",
            ".autumn-autocomplete__input",
            ".autumn-comment-input",
        ] {
            let body = rule_body(selector);
            assert!(
                body.contains("border: 1px solid var(--control-border);"),
                "{selector}:{body}"
            );
        }
    }

    // ── Issue #3406: the small display set ────────────────────────────────

    /// One style rule: the at-rules around it, its selector and its body.
    struct Rule {
        at: String,
        selector: String,
        body: String,
    }

    impl Rule {
        /// The `(property, value)` pairs of the body.
        fn declarations(&self) -> Vec<(&str, &str)> {
            self.body
                .split(';')
                .filter_map(|d| d.split_once(':'))
                .map(|(p, v)| (p.trim(), v.trim()))
                .filter(|(p, _)| !p.is_empty())
                .collect()
        }
    }

    /// Every style rule, with `@media` and `@supports` context. Comments
    /// are removed. `@keyframes` and `@starting-style` blocks are skipped.
    fn rules() -> Vec<Rule> {
        fn walk(css: &str, at: &str, out: &mut Vec<Rule>) {
            let mut rest = css;
            while let Some(open) = rest.find('{') {
                let head = rest[..open].trim();
                let mut depth = 0_i32;
                let mut close = rest.len();
                for (i, c) in rest[open..].char_indices() {
                    match c {
                        '{' => depth += 1,
                        '}' => {
                            depth -= 1;
                            if depth == 0 {
                                close = open + i;
                                break;
                            }
                        }
                        _ => {}
                    }
                }
                let inner = &rest[open + 1..close];
                if head.starts_with("@media") || head.starts_with("@supports") {
                    walk(inner, &format!("{at} {head}"), out);
                } else if !head.starts_with('@') {
                    out.push(Rule {
                        at: at.trim().to_owned(),
                        selector: head.to_owned(),
                        body: inner.to_owned(),
                    });
                }
                rest = rest.get(close + 1..).unwrap_or("");
            }
        }
        let mut css = String::new();
        let mut rest = lf_css();
        while let Some(start) = rest.find("/*") {
            css.push_str(&rest[..start]);
            let end = rest[start..]
                .find("*/")
                .map_or(rest.len(), |e| start + e + 2);
            rest = rest[end..].to_owned();
        }
        css.push_str(&rest);
        let mut out = Vec::new();
        walk(&css, "", &mut out);
        out
    }

    /// The class prefixes the #3406 widgets own.
    const DISPLAY_SET: &[&str] = &[
        ".autumn-button",
        ".autumn-spinner",
        ".autumn-skeleton",
        ".autumn-progress",
        ".autumn-meter",
        ".autumn-separator",
        ".autumn-kbd",
        ".autumn-empty-state",
        ".autumn-avatar-group",
        ".autumn-auto-grow",
        ".autumn-indicator",
    ];

    fn display_set_rules() -> Vec<Rule> {
        rules()
            .into_iter()
            .filter(|r| DISPLAY_SET.iter().any(|p| r.selector.contains(p)))
            .collect()
    }

    /// Each class that the #3406 widgets emit has a rule.
    #[test]
    fn display_set_classes_are_backed_by_component_css() {
        let rules = rules();
        for class in [
            "autumn-button",
            "autumn-button--primary",
            "autumn-button--secondary",
            "autumn-button--ghost",
            "autumn-button--destructive",
            "autumn-button--link",
            "autumn-button--sm",
            "autumn-button--md",
            "autumn-button--lg",
            "autumn-button--icon",
            "autumn-button__icon",
            "autumn-button__label",
            "autumn-button__spinner",
            "autumn-button-group",
            "autumn-button-group--vertical",
            "autumn-spinner",
            "autumn-spinner--sm",
            "autumn-spinner--md",
            "autumn-spinner--lg",
            "autumn-spinner__icon",
            "autumn-indicator",
            "autumn-skeleton",
            "autumn-skeleton--line",
            "autumn-skeleton--block",
            "autumn-skeleton--circle",
            "autumn-skeleton__item",
            "autumn-progress",
            "autumn-progress-field",
            "autumn-progress-field__header",
            "autumn-progress-field__label",
            "autumn-progress-field__value",
            "autumn-meter",
            "autumn-separator",
            "autumn-separator--vertical",
            "autumn-kbd",
            "autumn-kbd-combo",
            "autumn-empty-state",
            "autumn-empty-state__icon",
            "autumn-empty-state__title",
            "autumn-empty-state__body",
            "autumn-empty-state__action",
            "autumn-avatar-group",
            "autumn-avatar-group__more",
            "autumn-auto-grow",
        ] {
            let needle = format!(".{class}");
            let backed = rules.iter().any(|r| {
                r.selector.match_indices(&needle).any(|(i, _)| {
                    !r.selector[i + needle.len()..]
                        .starts_with(|c: char| c.is_ascii_alphanumeric() || c == '-' || c == '_')
                })
            });
            assert!(backed, "no rule for .{class}");
        }
    }

    /// The #3406 rules style only with tokens: no literal colors, and
    /// spacing, type and radius come from the token scales.
    ///
    /// Two exceptions: a `forced-colors` block must use system colors, and
    /// a `-1px` margin puts two borders on one line (it is not spacing).
    #[test]
    fn display_set_rules_style_only_with_tokens() {
        const LITERAL_COLORS: &[&str] = &[
            "#", "rgb(", "rgba(", "hsl(", "hsla(", "hwb(", "lab(", "lch(", "oklch(", "oklab(",
            "white", "black", "gray", "grey", "red", "green", "blue",
        ];
        const KEYWORDS: &[&str] = &[
            "0",
            "auto",
            "inherit",
            "none",
            "transparent",
            "currentColor",
        ];
        let mut failures = Vec::new();
        let rules = display_set_rules();
        assert!(
            rules.len() >= 40,
            "display set scan looks broken: {}",
            rules.len()
        );
        for rule in rules.iter().filter(|r| !r.at.contains("forced-colors")) {
            for (prop, value) in rule.declarations() {
                let color = matches!(
                    prop,
                    "color"
                        | "background"
                        | "background-color"
                        | "border-color"
                        | "outline-color"
                        | "fill"
                        | "stroke"
                        | "accent-color"
                        | "box-shadow"
                ) || prop.starts_with("border")
                    || prop.starts_with("outline");
                if color {
                    let lower = value.to_ascii_lowercase();
                    if LITERAL_COLORS.iter().any(|c| lower.contains(c)) {
                        failures.push(format!("{}: {prop}: {value}", rule.selector));
                    }
                }
                let scale = if prop.starts_with("padding")
                    || prop.starts_with("margin")
                    || prop.ends_with("gap")
                {
                    Some("var(--space-")
                } else if prop == "font-size" {
                    Some("var(--font-size-")
                } else if prop == "font-weight" {
                    Some("var(--font-weight-")
                } else if prop == "line-height" {
                    Some("var(--line-height-")
                } else if prop == "border-radius" {
                    Some("var(--radius")
                } else if prop == "font-family" {
                    Some("var(--font-")
                } else {
                    None
                };
                if let Some(token) = scale
                    && !value.contains(token)
                    && value != "-1px"
                    && !value.split_whitespace().all(|v| KEYWORDS.contains(&v))
                {
                    failures.push(format!("{}: {prop}: {value} (no {token}…)", rule.selector));
                }
            }
        }
        assert!(failures.is_empty(), "not token-based: {failures:#?}");
    }

    /// The spinner turns only with `prefers-reduced-motion: no-preference`.
    /// With `reduce`, it fades: an opacity change, not motion.
    #[test]
    fn spinner_turns_only_without_reduced_motion() {
        let rules = rules();
        let animation = |at: &str| {
            rules
                .iter()
                .filter(|r| r.selector == ".autumn-spinner__icon" && r.at.contains(at))
                .flat_map(Rule::declarations)
                .find(|(p, _)| *p == "animation")
                .map(|(_, v)| v.to_owned())
        };
        let turn = animation("prefers-reduced-motion: no-preference").expect("turn animation");
        assert!(turn.contains("autumn-spin"), "{turn}");
        let fade = animation("prefers-reduced-motion: reduce").expect("reduced animation");
        assert!(!fade.contains("autumn-spin"), "{fade}");
        let keyframes = lf_css();
        let at = keyframes
            .find("@keyframes autumn-pulse")
            .expect("pulse keyframes");
        let frames = &keyframes[at..at + keyframes[at..].find("\n}").unwrap()];
        assert!(
            frames.contains("opacity") && !frames.contains("transform"),
            "{frames}"
        );
        // No animation outside the motion queries.
        assert!(
            rules
                .iter()
                .filter(|r| r.selector.contains("autumn-spinner") && r.at.is_empty())
                .all(|r| !r.body.contains("animation")),
            "a spinner animation outside a prefers-reduced-motion query"
        );
    }

    /// The skeleton pulses only with `prefers-reduced-motion: no-preference`.
    #[test]
    fn skeleton_pulses_only_without_reduced_motion() {
        for rule in rules()
            .iter()
            .filter(|r| r.selector.contains("autumn-skeleton"))
        {
            if rule.body.contains("animation") {
                assert!(
                    rule.at.contains("prefers-reduced-motion: no-preference"),
                    "{} animates in `{}`",
                    rule.selector,
                    rule.at
                );
            }
        }
        assert!(
            rules()
                .iter()
                .any(|r| r.selector.contains("autumn-skeleton__item")
                    && r.body.contains("animation")),
            "the skeleton has no pulse"
        );
    }

    /// A busy button hides its icon and label with `opacity` only. They
    /// stay in the accessibility tree, so the button keeps its name.
    #[test]
    fn a_busy_button_keeps_its_label_in_the_accessibility_tree() {
        let mut hides = 0;
        for rule in display_set_rules() {
            let busy =
                rule.selector.contains("aria-busy") || rule.selector.contains("htmx-request");
            if !busy || !rule.selector.contains("autumn-button") {
                continue;
            }
            assert!(
                !rule.body.contains("visibility: hidden") && !rule.body.contains("display: none"),
                "{} removes content from the accessibility tree",
                rule.selector
            );
            if rule.body.contains("opacity: 0;") {
                hides += 1;
                assert!(
                    rule.selector.contains("autumn-button__spinner ~ *"),
                    "{}",
                    rule.selector
                );
            }
        }
        assert_eq!(hides, 1, "one rule hides the content of a busy button");
    }

    /// Class-level specificity: classes, attributes and pseudo-classes.
    /// The selectors it reads have no ids and no `:not()`.
    fn class_specificity(selector: &str) -> usize {
        selector.matches('.').count()
            + selector.matches('[').count()
            + selector.matches(':').count()
            - 2 * selector.matches("::").count()
    }

    /// Codex review (#3406): `.autumn-spinner__icon` comes later in the
    /// file and sets `display`. The rule that hides the button spinner must
    /// beat it, and each busy rule must beat the hide rule. Then the order
    /// of the rules does not matter.
    #[test]
    fn button_spinner_is_hidden_unless_the_button_is_busy() {
        const HIDE: &str = ".autumn-button > .autumn-button__spinner";
        let rules = rules();
        let sets_display = |r: &&Rule| r.declarations().iter().any(|(p, _)| *p == "display");
        let hide = rules
            .iter()
            .find(|r| r.selector == HIDE)
            .expect("the hide rule");
        assert!(hide.body.contains("display: none;"), "{}", hide.body);
        let floor = class_specificity(HIDE);
        let icon = rules
            .iter()
            .filter(sets_display)
            .filter(|r| r.selector == ".autumn-spinner__icon")
            .count();
        assert!(icon > 0, "no `.autumn-spinner__icon` display rule");
        assert!(class_specificity(".autumn-spinner__icon") < floor);
        let show = rules
            .iter()
            .filter(sets_display)
            .find(|r| {
                r.selector
                    .contains(r#".autumn-button[aria-busy="true"] > .autumn-button__spinner"#)
            })
            .expect("the busy rule");
        assert!(show.body.contains("display: block;"), "{}", show.body);
        let selectors: Vec<&str> = show.selector.split(',').map(str::trim).collect();
        assert_eq!(selectors.len(), 2, "{selectors:?}");
        for selector in selectors {
            assert!(
                class_specificity(selector) > floor,
                "{selector} does not beat {HIDE}"
            );
        }
    }

    /// An indicator is hidden until htmx marks it, or an element around
    /// it, with `htmx-request`.
    #[test]
    fn indicator_is_hidden_until_an_htmx_request() {
        let rule = rules()
            .into_iter()
            .find(|r| {
                r.selector
                    .contains(".autumn-indicator:not(.autumn-spinner):not(")
            })
            .expect("indicator rule");
        assert!(rule.at.is_empty(), "{}", rule.at);
        assert!(rule.body.contains("display: none;"), "{}", rule.body);
        // A spinner hides its content only. Its live region stays in the
        // tree, so the label that shows is read.
        assert!(
            rule.selector.contains(
                ".autumn-spinner.autumn-indicator:not(.htmx-request, .htmx-request *) > *"
            ),
            "{}",
            rule.selector
        );
    }

    /// `field-sizing: content` grows the textarea. A minimum keeps it from
    /// a one-line height, and a maximum keeps it on the screen.
    #[test]
    fn auto_grow_uses_field_sizing_content() {
        let body = rule_body(".autumn-auto-grow");
        for declaration in [
            "inline-size: 100%;",
            "field-sizing: content;",
            "min-block-size: 3lh;",
            "max-block-size: 50vh;",
        ] {
            assert!(body.contains(declaration), "missing `{declaration}`:{body}");
        }
    }

    /// WCAG 1.4.3: button, kbd, `+N` item and empty-state text in both
    /// schemes.
    /// A transparent background reads the surface.
    #[test]
    fn display_set_text_meets_aa_contrast() {
        let mut failures = Vec::new();
        for scheme in Scheme::BOTH {
            for (fg, bg, prop) in [
                (
                    ".autumn-button--primary",
                    ".autumn-button--primary",
                    "background-color",
                ),
                (
                    ".autumn-button--primary",
                    ".autumn-button--primary:hover",
                    "background-color",
                ),
                (
                    ".autumn-button--secondary",
                    ".autumn-button--secondary",
                    "background-color",
                ),
                (
                    ".autumn-button--secondary",
                    ".autumn-button--secondary:hover",
                    "background-color",
                ),
                (".autumn-button--ghost", "", ""),
                (
                    ".autumn-button--ghost",
                    ".autumn-button--ghost:hover",
                    "background-color",
                ),
                (
                    ".autumn-button--destructive",
                    ".autumn-button--destructive",
                    "background-color",
                ),
                (
                    ".autumn-button--destructive",
                    ".autumn-button--destructive:hover",
                    "background-color",
                ),
                (".autumn-button--link", "", ""),
                (".autumn-kbd", ".autumn-kbd", "background-color"),
                (
                    ".autumn-avatar-group__more",
                    ".autumn-avatar-group__more",
                    "background-color",
                ),
                (".autumn-empty-state__body", "", ""),
                (".autumn-progress-field__value", "", ""),
            ] {
                let fg_color = declared(fg, "color", scheme);
                let bg_color = if bg.is_empty() {
                    resolve("var(--surface)", scheme)
                } else {
                    declared(bg, prop, scheme)
                };
                let ratio = contrast(&fg_color, &bg_color);
                if ratio < 4.5 {
                    failures.push(format!("{scheme:?} {fg} on {bg}: {ratio:.2}:1"));
                }
            }
        }
        assert!(failures.is_empty(), "below 4.5:1 — {failures:?}");
    }

    /// WCAG 1.4.11: each meter fill stands out from the track (3:1), and the
    /// spinner ring from the surface, in both schemes.
    #[test]
    fn meter_fills_and_spinner_meet_non_text_contrast() {
        let mut failures = Vec::new();
        for scheme in Scheme::BOTH {
            let track = declared(".autumn-meter::-webkit-meter-bar", "background", scheme);
            for fill in [
                ".autumn-meter::-webkit-meter-optimum-value",
                ".autumn-meter::-webkit-meter-suboptimum-value",
                ".autumn-meter::-webkit-meter-even-less-good-value",
                ".autumn-meter:-moz-meter-optimum::-moz-meter-bar",
                ".autumn-meter:-moz-meter-sub-optimum::-moz-meter-bar",
                ".autumn-meter:-moz-meter-sub-sub-optimum::-moz-meter-bar",
            ] {
                let ratio = contrast(&declared(fill, "background", scheme), &track);
                if ratio < 3.0 {
                    failures.push(format!("{scheme:?} {fill}: {ratio:.2}:1"));
                }
            }
            let ring = declared(".autumn-spinner", "color", scheme);
            let ratio = contrast(&ring, &resolve("var(--surface)", scheme));
            if ratio < 3.0 {
                failures.push(format!("{scheme:?} spinner: {ratio:.2}:1"));
            }
        }
        assert!(failures.is_empty(), "below 3:1 — {failures:?}");
    }

    /// A skeleton paints only a background. Forced colors removes it, so a
    /// transparent border keeps an edge.
    #[test]
    fn skeleton_keeps_an_edge_in_forced_colors() {
        assert!(
            rule_body(".autumn-skeleton__item").contains("border: 1px solid transparent;"),
            "{}",
            rule_body(".autumn-skeleton__item")
        );
    }

    /// `hsl(h, s%, l%)` as `#rrggbb`.
    #[allow(
        clippy::cast_possible_truncation,
        clippy::cast_sign_loss,
        clippy::many_single_char_names,
        clippy::suboptimal_flops,
        reason = "the CSS Color 4 HSL formula, with its own names; a channel is rounded and in 0..=255"
    )]
    fn hsl_to_hex(hsl: &str) -> String {
        let inner = hsl
            .trim()
            .strip_prefix("hsl(")
            .and_then(|v| v.strip_suffix(')'))
            .unwrap_or_else(|| panic!("not hsl(): {hsl}"));
        let parts: Vec<f64> = inner
            .split(',')
            .map(|p| p.trim().trim_end_matches('%').parse().expect("number"))
            .collect();
        let (h, s, l) = (parts[0] / 360.0, parts[1] / 100.0, parts[2] / 100.0);
        let q = if l < 0.5 {
            l * (1.0 + s)
        } else {
            l + s - l * s
        };
        let p = 2.0 * l - q;
        let channel = |t: f64| {
            let t = t.rem_euclid(1.0);
            let c = if t < 1.0 / 6.0 {
                p + (q - p) * 6.0 * t
            } else if t < 0.5 {
                q
            } else if t < 2.0 / 3.0 {
                p + (q - p) * (2.0 / 3.0 - t) * 6.0
            } else {
                p
            };
            format!("{:02x}", (c * 255.0).round() as u8)
        };
        format!(
            "#{}{}{}",
            channel(h + 1.0 / 3.0),
            channel(h),
            channel(h - 1.0 / 3.0)
        )
    }

    /// axe (#3406 avatar group story): white initials on each palette
    /// color, and on the fallback, meet 4.5:1.
    #[test]
    fn avatar_initials_meet_aa_contrast_on_every_palette_color() {
        assert_eq!(hsl_to_hex("hsl(0, 100%, 50%)"), "#ff0000");
        let mut failures = Vec::new();
        for class in (0..12)
            .map(|i| format!(".autumn-avatar--c{i}"))
            .chain([".autumn-avatar--initials".to_owned()])
        {
            let body = rule_body(&class);
            let value = body
                .lines()
                .filter_map(|l| l.trim().strip_prefix("background:"))
                .chain(
                    body.split_once("background:")
                        .map(|(_, v)| v.split(';').next().unwrap_or("")),
                )
                .next()
                .unwrap_or_else(|| panic!("{class} has no background"));
            let bg = hsl_to_hex(value.trim().trim_end_matches(';'));
            let ratio = contrast("#ffffff", &bg);
            if ratio < 4.5 {
                failures.push(format!("{class} {bg}: {ratio:.2}:1"));
            }
        }
        assert!(failures.is_empty(), "below 4.5:1 — {failures:?}");
    }
}