uzor 1.5.38

Core UI engine — geometry, interaction, input state
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
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
//! A small DOM: a tree of elements styled by a [`Sheet`](crate::ui::style::Sheet), laid out
//! with [`flow`](crate::ui::flow) (`display:flex` needs the `flow-flex` feature, on by default)
//! and painted as CSS boxes (background, border, radius,
//! `box-shadow`, text). Text keeps one line, ellipsized when it does not fit, unless
//! `white-space: normal` lets it wrap at spaces and after inner hyphens (as browsers break). Screens describe *what*
//! is on them (`El::new("div.card").child(...)`) and the stylesheet decides *how it looks
//! and where it goes*: no per-screen pixel math. [`render`] returns every element's border
//! box by `id`, so the app hit-tests, registers and focuses by id as in a browser.
//!
//! `opacity` dims an element with its subtree; [`El::svg`] puts an inline icon in an element.
//! Element kinds with built-in content: `select` draws its chevron in the right padding
//! (`--chevron` colour, default the text colour). Everything else is a plain box.
//! `display:none` removes an element; `:hover` / `:focus-visible` / `:disabled` /
//! `:active` rules apply through the state callback given to [`render`].
//!
//! Inline rich text: [`El::runs`] puts styled [`Run`]s (bold, italic, code chips, links)
//! in one text flow that wraps across them; each run is styled by the sheet as an element
//! of its own (`b`, `code`, `a.link`, ...) inheriting the element's font, and a run with an
//! id is a hit of its own, one rect per wrapped piece.
//!
//! `position: absolute` takes an element out of the flow and places it against its nearest
//! positioned ancestor (`top` / `right` / `bottom` / `left`); positioned elements paint and
//! hit-test above the flow, ordered by `z-index`. Corners may differ (`border-radius: 12px
//! 12px 0 0`, elliptical `20px / 10px`, `border-top-left-radius`) and so may border sides
//! (`border-width: 0 0 1px`, `border-left: 3px solid red`). `shape-rendering:
//! geometricPrecision` draws an element's icon exactly, without pixel snapping.
//!
//! ```
//! use uzor::ui::dom::{lay_out, El, Run};
//! use uzor::ui::style::{Sheet, State};
//! fn card(ctx: &mut dyn uzor::render::RenderContext) {
//! let sheet = Sheet::parse(r#"
//!     div.card { position: relative; width: 240px; padding: 8px; white-space: normal;
//!                border-radius: 12px 12px 4px 12px; border-bottom: 2px solid #3b82f6 }
//!     b { font-weight: 700 }
//!     code { font-family: monospace; background: #222; padding: 0 6px; border-radius: 6px }
//!     a.link { color: #60a5fa }
//!     div.badge { position: absolute; top: -6px; right: -6px; width: 16px; height: 16px; z-index: 1 }
//! "#).unwrap();
//! let card = El::new("div.card").id("card")
//!     .runs([Run::text("Run "), Run::new("code", "cargo test"), Run::text(" and see "),
//!            Run::new("a.link", "the docs").id("url:docs"), Run::new("b", " now")])
//!     .child(El::new("div.badge").id("badge"));
//! let dom = lay_out(ctx, &sheet, &card, 0.0, 0.0, 800.0, &|_| State::NONE);
//! // The badge hangs over the card's corner and is the topmost hit there.
//! let (badge, card) = (dom.rect("badge").unwrap(), dom.rect("card").unwrap());
//! assert_eq!((badge.x + badge.width, badge.y), (card.x + card.width + 6.0, card.y - 6.0));
//! // The link run is a hit of its own.
//! assert!(dom.hits().any(|(id, _)| id == "url:docs"));
//! }
//! ```
//!
//! Animation nodes ([`El::anim`]) are boxes like any other (flex sizes them, an `id` makes
//! them hits, document order is their z-order) whose content is painted every frame by a
//! named [`AnimScript`](crate::render::AnimScript) from an
//! [`AnimRegistry`](crate::render::AnimRegistry): typically a canvas-style JS function
//! (`uzor-anim-js`: the page's engine on web, QuickJS on native) with a Rust or baked
//! fallback. [`paint_with`] / [`paint_anim`] paint them; an animation outside the visible
//! rect, fully transparent, or in a paused frame does not run.

use crate::render::{AnimParams, AnimRegistry, RenderContext, TextAlign};
use crate::types::Rect;
use crate::ui::css_box::ellipsize;
use crate::ui::css_text::{fill_text_css, INTER};
use crate::ui::flow::{layout_in, LBox, Laid};
use crate::ui::style::{apply_decl, Node, Sheet, State, Style, StyleField};
use crate::ui::widgets::composite::dialog::{fill_round_rect, path_round_rect, Shadow};

/// One element: `spec` is `kind#id.class.class` as in a selector (`div.card`, `button.pbtn`).
#[derive(Clone, Debug, Default, PartialEq)]
pub struct El {
    pub spec: String,
    /// Hit / focus id (also matched by `#id` selectors only if written in `spec`).
    pub id: Option<String>,
    pub text: Option<String>,
    /// Inline declarations (`style="..."`), over the sheet.
    pub css: Option<String>,
    /// A Tab stop (focusable control).
    pub tab: bool,
    /// Accessible name; defaults to the text.
    pub name: Option<String>,
    /// Inline `<svg>` markup drawn into the content box (stroke = the text colour).
    pub svg: Option<String>,
    /// Animated content ([`El::anim`]).
    pub anim: Option<AnimNode>,
    pub children: Vec<El>,
    /// Inline rich text ([`El::runs`]); when set it replaces `text`.
    pub runs: Vec<Run>,
    /// Built-in control content ([`El::control`]): switch, checkbox, spinner, text input.
    pub control: Option<Control>,
    /// An embed slot ([`El::slot`]): the app paints something else (a kit widget, a
    /// canvas) into the element's content box, found with [`Dom::slots`].
    pub slot: Option<String>,
    /// Vertical scroll offset of an overflow container ([`El::scroll_top`]).
    pub scroll_top: f64,
    /// The element takes its fingers whole ([`El::paint_slot`]): `touch-action: none`
    /// unless the style sets `touch-action` itself.
    pub touch_capture: bool,
}

/// Built-in control content of an element, painted into its content box after the
/// element's own CSS box (so the track, frame and colours stay in the style sheet).
///
/// | control | paints | CSS hooks |
/// |---|---|---|
/// | [`Control::Switch`] | a round knob in the content box, left (off) to right (on) | `--knob` (`#fff`), `:checked` on the element when on |
/// | [`Control::Checkbox`] | a tick (or a dash when mixed) | `--check` (default `color`), `--check-width` (2px), `:checked` |
/// | [`Control::Spinner`] | a rotating 270° arc | `color`, `--spin-track`, `--spin-width`, `--spin-speed` (turns/s, 1) |
/// | [`Control::Ring`] | a meter ring: the full track, then a `frac` arc clockwise from 12 o'clock | `--ring-color` (else `color`) for the arc, `--ring-track`, `--ring-width` |
/// | [`Control::Input`] | value or placeholder, selection, caret, IME composition | `color`, `font-*`, `--placeholder`, `--selection`, `--caret`, `:focus` while it has a caret |
#[derive(Clone, Debug, PartialEq)]
pub enum Control {
    /// A switch: `pos` 0 (off) … 1 (on) places the knob (animate it for a slide).
    Switch { on: bool, pos: f64 },
    /// A checkbox tick; `mixed` draws the indeterminate dash.
    Checkbox { on: bool, mixed: bool },
    /// A busy spinner at time `t` (seconds).
    Spinner { t: f64 },
    /// A meter ring filled to `frac` (0 … 1), e.g. a limit or quota.
    Ring { frac: f64 },
    /// A single-line text input showing the edit state (the app or the framework's text
    /// store owns editing; the DOM lays it out and paints it).
    Input(Box<Edit>),
}

impl Control {
    /// A switch at rest.
    pub fn switch(on: bool) -> Self {
        Control::Switch { on, pos: if on { 1.0 } else { 0.0 } }
    }
    pub fn checkbox(on: bool) -> Self {
        Control::Checkbox { on, mixed: false }
    }
    pub fn spinner(t: f64) -> Self {
        Control::Spinner { t }
    }
    /// A meter ring filled to `frac`, clamped to 0 … 1.
    pub fn ring(frac: f64) -> Self {
        Control::Ring { frac: if frac.is_finite() { frac.clamp(0.0, 1.0) } else { 0.0 } }
    }
    pub fn input(e: Edit) -> Self {
        Control::Input(Box::new(e))
    }
    /// The ARIA role an element with this control exposes.
    pub fn role(&self) -> &'static str {
        match self {
            Control::Switch { .. } => "switch",
            Control::Checkbox { .. } => "checkbox",
            Control::Spinner { .. } => "progressbar",
            Control::Ring { .. } => "meter",
            Control::Input(_) => "textbox",
        }
    }
    /// On (switch / checkbox).
    pub fn checked(&self) -> Option<bool> {
        match self {
            Control::Switch { on, .. } | Control::Checkbox { on, .. } => Some(*on),
            _ => None,
        }
    }
}

/// The edit state a DOM text input shows ([`Control::Input`]). Indices are chars.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Edit {
    pub value: String,
    /// Shown while `value` is empty and nothing is being composed.
    pub placeholder: String,
    /// The caret; `Some` = focused (the element gets `:focus`).
    pub caret: Option<usize>,
    /// The other end of a selection (a selection is `anchor..caret`).
    pub anchor: Option<usize>,
    /// IME composition shown at the caret, underlined.
    pub preedit: Option<String>,
    /// Bullets instead of the text (passwords).
    pub masked: bool,
    /// The caret is in the visible half of its blink (`false` hides it).
    pub caret_on: bool,
}

impl Edit {
    /// An unfocused input showing `value`.
    pub fn new(value: impl Into<String>) -> Self {
        Edit { value: value.into(), caret_on: true, ..Default::default() }
    }
    pub fn placeholder(mut self, p: impl Into<String>) -> Self {
        self.placeholder = p.into();
        self
    }
    /// Focused with the caret at char `at` (the value's length at most).
    pub fn caret(mut self, at: usize) -> Self {
        self.caret = Some(at.min(self.value.chars().count()));
        self
    }
    /// Select `anchor..caret` (call after [`Edit::caret`]).
    pub fn select_from(mut self, anchor: usize) -> Self {
        self.anchor = Some(anchor.min(self.value.chars().count()));
        self
    }
    pub fn preedit(mut self, p: impl Into<String>) -> Self {
        self.preedit = Some(p.into());
        self
    }
    pub fn masked(mut self, on: bool) -> Self {
        self.masked = on;
        self
    }
    pub fn caret_on(mut self, on: bool) -> Self {
        self.caret_on = on;
        self
    }
    /// What is painted: the value (bullets when masked) with the composition at the caret.
    pub fn display(&self) -> String {
        let v: String = if self.masked { "\u{2022}".repeat(self.value.chars().count()) } else { self.value.clone() };
        match (&self.preedit, self.caret) {
            (Some(p), Some(c)) => {
                let at = v.char_indices().nth(c).map_or(v.len(), |(i, _)| i);
                format!("{}{}{}", &v[..at], p, &v[at..])
            }
            _ => v,
        }
    }
    /// The caret in [`Edit::display`] chars (after the composition).
    pub fn display_caret(&self) -> Option<usize> {
        self.caret.map(|c| c + self.preedit.as_ref().map_or(0, |p| p.chars().count()))
    }
    fn shows_placeholder(&self) -> bool {
        self.value.is_empty() && self.preedit.as_deref().is_none_or(str::is_empty)
    }
}

/// One styled run of inline text ([`El::runs`]).
///
/// `spec` styles the run as an element of its own (`span`, `b`, `i`, `code`,
/// `a.link`), resolved by the sheet as a child of the element holding the runs, so it
/// inherits that element's font and colour. A run paints its `background` as a chip
/// behind each of its pieces (with its `border-radius`, its left / right `padding` at its
/// first / last piece, and its vertical padding around a `1.2em` content height), and
/// takes its own `color`, `font-*` and `font-style`.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Run {
    pub spec: String,
    pub text: String,
    /// Hit id: each of the run's pieces is a hit ([`Dom::hits`], [`Dom::run_at`]).
    pub id: Option<String>,
    /// Inline declarations over the sheet.
    pub css: Option<String>,
}

impl Run {
    /// A run styled as `spec` (`"code"`, `"a.link"`).
    pub fn new(spec: impl Into<String>, text: impl Into<String>) -> Self {
        Self { spec: spec.into(), text: text.into(), ..Self::default() }
    }
    /// A plain run (a `span`: the element's own font and colour).
    pub fn text(text: impl Into<String>) -> Self {
        Self::new("span", text)
    }
    pub fn id(mut self, id: impl Into<String>) -> Self {
        self.id = Some(id.into());
        self
    }
    pub fn css(mut self, decls: impl Into<String>) -> Self {
        self.css = Some(decls.into());
        self
    }
}

/// A run's resolved look on a placed element.
#[derive(Clone, Debug, PartialEq)]
pub struct PlacedRun {
    pub id: Option<String>,
    pub style: Style,
    pub text: String,
}

/// One piece of a run on one line. Coordinates are relative to the element's content
/// box, so they survive [`Dom::moved`].
#[derive(Clone, Debug, PartialEq)]
pub struct RunPiece {
    /// Index into [`Inline::runs`].
    pub run: usize,
    /// Left edge of the piece's box (a chip's left edge) and its width (text plus padding).
    pub x: f64,
    pub w: f64,
    /// Where the text starts inside the box (a chip's left padding on its first piece).
    pub pad: f64,
    pub text: String,
    /// Char offset of the piece's first char in [`Inline::text`].
    pub start: usize,
    /// X of each char boundary from the text start (`text.chars().count() + 1` values),
    /// for carets and selections.
    pub bounds: Vec<f64>,
}

/// One wrapped line of inline runs.
#[derive(Clone, Debug, PartialEq)]
pub struct RunLine {
    /// Top of the line box, relative to the content box.
    pub y: f64,
    pub height: f64,
    /// Width up to the end of its last non-space piece.
    pub width: f64,
    pub pieces: Vec<RunPiece>,
}

/// Laid-out inline runs of a placed element ([`Placed::inline`]).
#[derive(Clone, Debug, PartialEq)]
pub struct Inline {
    pub runs: Vec<PlacedRun>,
    pub lines: Vec<RunLine>,
    /// Every run's text, concatenated (the element's text content).
    pub text: String,
}

/// What an animation node paints: the script `name` in an [`AnimRegistry`] and its
/// per-instance params (the [`AnimParams`] besides the size and the pointer).
#[derive(Clone, Debug, PartialEq)]
pub struct AnimNode {
    pub name: String,
    pub seed: u32,
    pub args: [f64; crate::render::anim_script::ANIM_ARGS],
    /// Frames per second while it moves; `None`: a still frame (painted, no next frame asked).
    pub fps: Option<f64>,
    /// The script gets the window pointer wherever it is (eyes that follow it), not only
    /// while this node is the topmost thing under it.
    pub track_pointer: bool,
    /// Paint outside the content box (CSS `overflow: visible`); by default the frame is
    /// clipped to the content box, like a `<canvas>`.
    pub overflow: bool,
}

impl AnimNode {
    pub fn new(name: impl Into<String>) -> Self {
        Self { name: name.into(), seed: 0, args: [0.0; crate::render::anim_script::ANIM_ARGS], fps: Some(30.0), track_pointer: false, overflow: false }
    }
    pub fn seed(mut self, seed: u32) -> Self {
        self.seed = seed;
        self
    }
    /// The first `args` (up to [`ANIM_ARGS`](crate::render::anim_script::ANIM_ARGS); the rest 0).
    pub fn args<const N: usize>(mut self, args: [f64; N]) -> Self {
        let n = N.min(self.args.len());
        self.args = Default::default();
        self.args[..n].copy_from_slice(&args[..n]);
        self
    }
    /// `None`: still.
    pub fn fps(mut self, fps: Option<f64>) -> Self {
        self.fps = fps;
        self
    }
    pub fn track_pointer(mut self, on: bool) -> Self {
        self.track_pointer = on;
        self
    }
    pub fn overflow_visible(mut self, on: bool) -> Self {
        self.overflow = on;
        self
    }
}

impl El {
    pub fn new(spec: impl Into<String>) -> Self {
        Self { spec: spec.into(), ..Self::default() }
    }
    pub fn id(mut self, id: impl Into<String>) -> Self {
        self.id = Some(id.into());
        self
    }
    pub fn text(mut self, t: impl Into<String>) -> Self {
        self.text = Some(t.into());
        self
    }
    pub fn css(mut self, decls: impl Into<String>) -> Self {
        self.css = Some(decls.into());
        self
    }
    /// A focusable control (`id` required to be useful).
    pub fn tab(mut self) -> Self {
        self.tab = true;
        self
    }
    pub fn name(mut self, n: impl Into<String>) -> Self {
        self.name = Some(n.into());
        self
    }
    /// Inline `<svg>` content (an icon), drawn in the content box in `color`.
    pub fn svg(mut self, markup: impl Into<String>) -> Self {
        self.svg = Some(markup.into());
        self
    }
    /// Animated content: `node`'s script paints the content box every frame
    /// (see [`paint_with`]).
    pub fn anim(mut self, node: AnimNode) -> Self {
        self.anim = Some(node);
        self
    }
    pub fn child(mut self, c: El) -> Self {
        self.children.push(c);
        self
    }
    /// Inline rich text: styled runs in one text flow (replacing [`El::text`]). Under
    /// `white-space: normal` / `pre-wrap` the runs wrap together at spaces, a run's chip
    /// padding counting at its ends; `overflow-wrap: anywhere` breaks a word that does
    /// not fit a line. `\n` always starts a line. `line-clamp` keeps the first lines.
    pub fn runs(mut self, runs: impl IntoIterator<Item = Run>) -> Self {
        self.runs.extend(runs);
        self
    }
    pub fn children(mut self, cs: impl IntoIterator<Item = El>) -> Self {
        self.children.extend(cs);
        self
    }
    /// Built-in control content ([`Control`]).
    pub fn control(mut self, c: Control) -> Self {
        self.control = Some(c);
        self
    }
    /// An embed slot named `key`: the element is laid out and painted as a box like any
    /// other, and [`Dom::slots`] gives its content box so the app paints a kit widget
    /// (or anything else) there.
    pub fn slot(mut self, key: impl Into<String>) -> Self {
        self.slot = Some(key.into());
        self
    }
    /// A custom-paint slot for a chart, an order book, a map: id and slot key `key`,
    /// and it captures touch (`touch-action: none` unless the style says otherwise),
    /// so its pans, pinches, long-press drags and double-taps are delivered to it as
    /// gestures and never scroll the page. Size it with CSS like any box (`flex-grow:
    /// 1; min-height: 0` to take the rest of a column); the host paints into its
    /// content box, clipped, in paint order (`uzor_framework::widgets::kit::dom`
    /// with `paint_slots`).
    pub fn paint_slot(mut self, key: impl Into<String>) -> Self {
        let key = key.into();
        self.id = Some(key.clone());
        self.slot = Some(key);
        self.touch_capture = true;
        self
    }
    /// Scroll the element's content up by `y` px, as CSS `scrollTop`: its descendants
    /// (in flow and positioned, not `fixed`) move, the element itself does not. With
    /// `overflow: hidden | auto | scroll` they are clipped to its padding box, so
    /// nothing scrolled out paints or hits outside it; [`Placed::scroll_height`]
    /// gives the content height to clamp `y` against.
    pub fn scroll_top(mut self, y: f64) -> Self {
        self.scroll_top = y;
        self
    }
    /// Append `c` when `cond` (conditional markup).
    pub fn child_if(self, cond: bool, c: impl FnOnce() -> El) -> Self {
        if cond {
            Self::push_child(self, c())
        } else {
            self
        }
    }
    fn push_child(this: Self, c: El) -> Self {
        this.child(c)
    }
}

/// One laid-out element.
#[derive(Clone, Debug, PartialEq)]
pub struct Placed {
    pub id: Option<String>,
    pub kind: String,
    pub rect: Rect,
    pub style: Style,
    pub text: Option<String>,
    pub tab: bool,
    pub name: Option<String>,
    pub svg: Option<String>,
    pub anim: Option<AnimNode>,
    /// Laid-out inline runs ([`El::runs`]).
    pub inline: Option<Box<Inline>>,
    /// Built-in control content ([`El::control`]).
    pub control: Option<Control>,
    /// Embed slot key ([`El::slot`]).
    pub slot: Option<String>,
    /// In a `position: fixed` subtree: placed against the viewport and not moved by
    /// [`Dom::moved`] (an overlay that does not scroll with the page).
    pub fixed: bool,
    /// The clip from `overflow: hidden` ancestors (their padding boxes intersected),
    /// in the same coordinates as [`Placed::rect`]; paint and hits stay inside it.
    pub clip: Option<Rect>,
    /// A text input's char boundaries: x of each boundary of [`Edit::display`] from the
    /// text start (`chars + 1` values).
    pub bounds: Vec<f64>,
    /// An overflow container's content height (CSS `scrollHeight`): from its padding
    /// box top to the lowest descendant's bottom plus its bottom padding, at least its
    /// padding box height. `0` for other boxes.
    pub scroll_height: f64,
}

impl Default for Placed {
    fn default() -> Self {
        Self {
            id: None,
            kind: "div".into(),
            rect: Rect::default(),
            style: Style::default(),
            text: None,
            tab: false,
            name: None,
            svg: None,
            anim: None,
            inline: None,
            control: None,
            slot: None,
            fixed: false,
            clip: None,
            bounds: Vec::new(),
            scroll_height: 0.0,
        }
    }
}

impl Placed {
    /// CSS `touch-action: none`: a finger that lands on this element is its own (a
    /// chart, a map, a signature pad). A host registers it with `Sense::touch`, so
    /// its pans, pinches, long-press drags and double-taps are delivered to it as
    /// gestures and never scroll an ancestor.
    pub fn captures_touch(&self) -> bool {
        self.style.touch_action.as_deref() == Some("none")
    }
    /// The ARIA role: a control's ([`Control::role`]), else from the kind (`button`,
    /// `a` → `link`, `select` → `combobox`, `input` → `textbox`), else `generic`.
    pub fn role(&self) -> &'static str {
        if let Some(c) = &self.control {
            return c.role();
        }
        match self.kind.as_str() {
            "button" => "button",
            "a" => "link",
            "select" => "combobox",
            "input" | "textarea" => "textbox",
            "img" => "img",
            "li" => "listitem",
            "ul" | "ol" => "list",
            "h1" | "h2" | "h3" | "h4" => "heading",
            _ if self.tab => "button",
            _ => "generic",
        }
    }
    /// The accessible name: [`El::name`] when set, else the element's text (or inline
    /// runs' text, or a text input's value / placeholder).
    pub fn accessible_name(&self) -> Option<&str> {
        self.name.as_deref().or(self.text.as_deref()).or(self.inline.as_ref().map(|i| i.text.as_str())).or(match &self.control {
            Some(Control::Input(e)) if !e.masked && !e.value.is_empty() => Some(e.value.as_str()),
            Some(Control::Input(e)) => Some(e.placeholder.as_str()).filter(|p| !p.is_empty()),
            _ => None,
        })
    }
    /// A text input's horizontal scroll: enough to keep the caret inside the content box.
    pub fn input_scroll(&self) -> f64 {
        let Some(Control::Input(e)) = &self.control else { return 0.0 };
        let c = content_box(self);
        let at = e.display_caret().and_then(|i| self.bounds.get(i)).copied().unwrap_or(0.0);
        (at - c.width + 1.0).max(0.0)
    }
    /// A text input's char index (in [`Edit::display`]) nearest to window `x`: where a
    /// click puts the caret.
    pub fn input_index_at(&self, x: f64) -> Option<usize> {
        let Some(Control::Input(_)) = &self.control else { return None };
        let c = content_box(self);
        let lx = x - c.x + self.input_scroll();
        self.bounds.iter().enumerate().min_by(|a, b| (a.1 - lx).abs().total_cmp(&(b.1 - lx).abs())).map(|(i, _)| i)
    }
    /// A text input's caret rect (1px wide, the line height, window coordinates): for
    /// painting and for the OS IME candidate window.
    pub fn caret_rect(&self) -> Option<Rect> {
        let Some(Control::Input(e)) = &self.control else { return None };
        let i = e.display_caret()?;
        let c = content_box(self);
        let lh = font_of(&self.style).2;
        let x = c.x + self.bounds.get(i).copied().unwrap_or(0.0) - self.input_scroll();
        Some(Rect::new(x.round(), c.y + ((c.height - lh) / 2.0).max(0.0), 1.0, lh.min(c.height.max(lh))))
    }
    /// Every inline run piece as `(run index, rect)` in window coordinates (the piece's
    /// box over its full line height), line by line.
    pub fn run_boxes(&self) -> Vec<(usize, Rect)> {
        let Some(inl) = &self.inline else { return Vec::new() };
        let c = content_box(self);
        inl.lines
            .iter()
            .flat_map(|l| l.pieces.iter().map(move |p| (p.run, Rect::new(c.x + p.x, c.y + l.y, p.w, l.height))))
            .collect()
    }
    /// The char offset (in [`Inline::text`]) of the inline text boundary nearest to
    /// `(x, y)`: the line under `y` (clamped to the first / last), the nearest boundary
    /// on it. For carets and drag selections.
    pub fn char_at(&self, x: f64, y: f64) -> Option<usize> {
        let inl = self.inline.as_ref()?;
        let c = content_box(self);
        let line = inl.lines.iter().find(|l| y < c.y + l.y + l.height).or(inl.lines.last())?;
        let mut best: Option<(f64, usize)> = None;
        for p in &line.pieces {
            for (i, bx) in p.bounds.iter().enumerate() {
                let d = (c.x + p.x + p.pad + bx - x).abs();
                if best.is_none_or(|(bd, _)| d < bd) {
                    best = Some((d, p.start + i));
                }
            }
        }
        best.map(|b| b.1).or_else(|| line.pieces.first().map(|p| p.start))
    }
}

/// A rendered tree, in paint (document) order.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Dom {
    pub boxes: Vec<Placed>,
    /// The root's border box.
    pub root: Rect,
}

impl Dom {
    /// The border box of element `id`.
    pub fn rect(&self, id: &str) -> Option<Rect> {
        self.boxes.iter().find(|b| b.id.as_deref() == Some(id)).map(|b| b.rect)
    }
    /// `(id, rect)` of every element with an id, in paint order (later = on top): document
    /// order, positioned elements after the flow by `z-index`. An element's inline runs
    /// with ids follow it, one rect per piece.
    pub fn hits(&self) -> impl Iterator<Item = (&str, Rect)> {
        self.boxes.iter().flat_map(|b| {
            let cut = |r: Rect| match b.clip {
                Some(c) => c.intersect(r),
                None => r,
            };
            let own = b.id.as_deref().map(|i| (i, cut(b.rect)));
            let runs: Vec<(&str, Rect)> = match &b.inline {
                Some(inl) if inl.runs.iter().any(|r| r.id.is_some()) => {
                    b.run_boxes().into_iter().filter_map(|(ri, r)| Some((inl.runs[ri].id.as_deref()?, cut(r)))).collect()
                }
                _ => Vec::new(),
            };
            own.into_iter().chain(runs).filter(|(_, r)| r.width > 0.0 && r.height > 0.0)
        })
    }
    /// The topmost element with inline runs under `(x, y)` and the run there (a piece of
    /// it contains the point): `(element, run index)`.
    pub fn run_at(&self, x: f64, y: f64) -> Option<(&Placed, usize)> {
        self.boxes.iter().rev().filter(|b| b.inline.is_some()).find_map(|b| {
            b.run_boxes()
                .into_iter()
                .find(|(_, r)| x >= r.x && x < r.x + r.width && y >= r.y && y < r.y + r.height)
                .map(|(ri, _)| (b, ri))
        })
    }
    /// The Tab stops in document order.
    pub fn tabbable(&self) -> impl Iterator<Item = &str> {
        self.boxes.iter().filter(|b| b.tab).filter_map(|b| b.id.as_deref())
    }
    /// The tree shifted by `(dx, dy)`: lay out once to learn a size, then place it.
    /// `position: fixed` boxes stay where they are (they are placed against the
    /// viewport, not the page).
    pub fn moved(mut self, dx: f64, dy: f64) -> Self {
        for b in self.boxes.iter_mut().filter(|b| !b.fixed) {
            b.rect = Rect::new(b.rect.x + dx, b.rect.y + dy, b.rect.width, b.rect.height);
            if let Some(c) = &mut b.clip {
                *c = Rect::new(c.x + dx, c.y + dy, c.width, c.height);
            }
        }
        self.root = Rect::new(self.root.x + dx, self.root.y + dy, self.root.width, self.root.height);
        self
    }
    /// `(id, accessible name)` for elements with an id and a name or text
    /// ([`Placed::accessible_name`]: [`El::name`] first).
    pub fn names(&self) -> impl Iterator<Item = (&str, &str)> {
        self.boxes.iter().filter_map(|b| Some((b.id.as_deref()?, b.accessible_name()?)))
    }
    /// The accessibility nodes of the tree: every element with an id, with its role,
    /// accessible name ([`El::name`] wins over text), checked state and rect, in paint
    /// order. What a host hands its platform accessibility tree.
    pub fn a11y(&self) -> Vec<A11yNode> {
        self.boxes
            .iter()
            .filter_map(|b| {
                Some(A11yNode {
                    id: b.id.clone()?,
                    role: b.role(),
                    name: b.accessible_name().unwrap_or_default().to_string(),
                    checked: b.control.as_ref().and_then(Control::checked),
                    disabled: b.kind == "button" && b.style.opacity.is_some_and(|o| o < 0.6),
                    rect: b.rect,
                    focusable: b.tab || matches!(b.control, Some(Control::Input(_))),
                })
            })
            .collect()
    }
    /// The elements with an id that capture touch (CSS `touch-action: none`), with
    /// what is visible of them, in paint order.
    pub fn touch_targets(&self) -> impl Iterator<Item = (&str, Rect)> {
        self.boxes.iter().filter(|b| b.captures_touch()).filter_map(|b| Some((b.id.as_deref()?, b.clip.map_or(b.rect, |c| c.intersect(b.rect)))))
    }
    /// `(slot key, content box)` of every embed slot ([`El::slot`]), in paint order.
    pub fn slots(&self) -> impl Iterator<Item = (&str, Rect)> {
        self.boxes.iter().filter_map(|b| Some((b.slot.as_deref()?, content_box(b))))
    }
    /// The placed element `id`.
    pub fn get(&self, id: &str) -> Option<&Placed> {
        self.boxes.iter().find(|b| b.id.as_deref() == Some(id))
    }
    /// The topmost element with an id containing `(x, y)`.
    pub fn hit(&self, x: f64, y: f64) -> Option<&str> {
        let mut found = None;
        for (id, r) in self.hits() {
            if x >= r.x && x < r.x + r.width && y >= r.y && y < r.y + r.height {
                found = Some(id);
            }
        }
        found
    }
}

/// One accessibility node of a DOM tree ([`Dom::a11y`]).
#[derive(Clone, Debug, PartialEq)]
pub struct A11yNode {
    pub id: String,
    pub role: &'static str,
    pub name: String,
    pub checked: Option<bool>,
    pub disabled: bool,
    pub rect: Rect,
    pub focusable: bool,
}

struct Resolved {
    kind: String,
    id: Option<String>,
    style: Style,
    text: Option<String>,
    tab: bool,
    name: Option<String>,
    svg: Option<String>,
    anim: Option<AnimNode>,
    /// Measured text: break units and line height.
    measured: Option<(Units, f64)>,
    /// Width of a space in the element's font (wrapping text).
    space: f64,
    /// Measured inline runs.
    runs: Option<std::rc::Rc<RunsMeasured>>,
    control: Option<Control>,
    slot: Option<String>,
    /// A text input's display char boundaries.
    bounds: Vec<f64>,
    scroll_top: f64,
    children: Vec<Resolved>,
}

/// Token kinds of inline runs.
#[derive(Clone, Copy, Debug, PartialEq)]
enum Tok {
    Word,
    Space,
    Newline,
}

/// A measured token of inline runs: a word, a run of spaces, or a newline.
#[derive(Clone, Debug)]
struct RunTok {
    run: usize,
    kind: Tok,
    text: String,
    /// Char offset in the whole text.
    start: usize,
    /// Chip padding before / after (the run's first / last token).
    lp: f64,
    rp: f64,
    /// Char boundary x from the token start; the last is its text width.
    bounds: Vec<f64>,
}

/// Inline runs measured at resolve time (layout and placing need no render context).
#[derive(Debug)]
struct RunsMeasured {
    toks: Vec<RunTok>,
    runs: Vec<PlacedRun>,
    lh: f64,
    wrap: bool,
    anywhere: bool,
    clamp: usize,
    text: String,
}

fn measure_runs(ctx: &mut dyn RenderContext, sheet: &Sheet, el: &El, s: &Style, up: &[Node<'_>]) -> RunsMeasured {
    let mut toks = Vec::new();
    let mut runs = Vec::new();
    let mut text = String::new();
    let mut off = 0usize;
    for (ri, run) in el.runs.iter().enumerate() {
        let mut rs = if up.is_empty() { sheet.resolve(&Node::parse(&run.spec), Some(s)) } else { sheet.resolve_in(&Node::parse(&run.spec), up, Some(s)) };
        if let Some(css) = &run.css {
            for d in css.split(';') {
                if let Some((k, v)) = d.split_once(':') {
                    apply_decl(&mut rs, k.trim(), v.trim());
                }
            }
        }
        ctx.set_font(&font_str(&rs));
        let pad = rs.pad();
        let first = toks.len();
        // Some backends trim spaces when measuring: measure spaces between two glyphs.
        let xx = ctx.measure_text("xx");
        let mut push = |ctx: &mut dyn RenderContext, kind: Tok, t: &str, start: usize| {
            let n = t.chars().count();
            let bounds: Vec<f64> = match kind {
                Tok::Newline => vec![0.0],
                Tok::Space => {
                    let w = ctx.measure_text(&format!("x{t}x")) - xx;
                    (0..=n).map(|i| w * i as f64 / n.max(1) as f64).collect()
                }
                Tok::Word => {
                    let mut b = vec![0.0];
                    let mut acc = String::new();
                    for ch in t.chars() {
                        acc.push(ch);
                        b.push(ctx.measure_text(&acc));
                    }
                    b
                }
            };
            toks.push(RunTok { run: ri, kind, text: t.to_string(), start, lp: 0.0, rp: 0.0, bounds });
        };
        let mut cur = String::new();
        let mut cur_start = off;
        let mut cur_sp = false;
        for (ci, ch) in run.text.chars().enumerate() {
            let at = off + ci;
            if ch == '\n' {
                if !cur.is_empty() {
                    push(ctx, if cur_sp { Tok::Space } else { Tok::Word }, &std::mem::take(&mut cur), cur_start);
                }
                push(ctx, Tok::Newline, "", at);
                cur_start = at + 1;
                continue;
            }
            let sp = ch == ' ';
            if !cur.is_empty() && sp != cur_sp {
                push(ctx, if cur_sp { Tok::Space } else { Tok::Word }, &std::mem::take(&mut cur), cur_start);
            }
            if cur.is_empty() {
                cur_start = at;
            }
            cur_sp = sp;
            cur.push(ch);
        }
        if !cur.is_empty() {
            push(ctx, if cur_sp { Tok::Space } else { Tok::Word }, &cur, cur_start);
        }
        if toks.len() > first {
            toks[first].lp = pad[3];
            toks.last_mut().unwrap().rp = pad[1];
        }
        off += run.text.chars().count();
        text.push_str(&run.text);
        runs.push(PlacedRun { id: run.id.clone(), style: rs, text: run.text.clone() });
    }
    RunsMeasured {
        toks,
        runs,
        lh: font_of(s).2,
        wrap: wraps(s),
        anywhere: matches!(s.overflow_wrap.as_deref(), Some("anywhere" | "break-word")),
        clamp: clamp_of(s),
        text,
    }
}

/// Greedy wrap of measured runs at `limit` (`None` or not wrapping: only newlines break).
/// Spaces at a line start are dropped, spaces before a wrapped word too; with `anywhere`
/// a word too long for a line breaks between chars.
fn wrap_runs(m: &RunsMeasured, limit: Option<f64>) -> Vec<RunLine> {
    let max_w = if m.wrap { limit.unwrap_or(f64::INFINITY) } else { f64::INFINITY };
    let mut lines: Vec<Vec<RunPiece>> = vec![Vec::new()];
    let mut x = 0.0;
    let piece = |t: &RunTok, a: usize, b: usize, x: f64, lp: f64, rp: f64| {
        let base = t.bounds[a];
        let bounds: Vec<f64> = t.bounds[a..=b].iter().map(|v| v - base).collect();
        let w = bounds[b - a] + lp + rp;
        RunPiece { run: t.run, x, w, pad: lp, text: t.text.chars().skip(a).take(b - a).collect(), start: t.start + a, bounds }
    };
    for t in &m.toks {
        let n = t.bounds.len() - 1;
        let w = t.bounds[n] + t.lp + t.rp;
        match t.kind {
            Tok::Newline => {
                lines.push(Vec::new());
                x = 0.0;
                continue;
            }
            Tok::Space => {
                if x > 0.0 && x + w <= max_w {
                    lines.last_mut().unwrap().push(piece(t, 0, n, x, t.lp, t.rp));
                    x += w;
                } else if x > 0.0 {
                    lines.push(Vec::new());
                    x = 0.0;
                }
                continue;
            }
            Tok::Word => {}
        }
        if x + w <= max_w {
            lines.last_mut().unwrap().push(piece(t, 0, n, x, t.lp, t.rp));
            x += w;
            continue;
        }
        if x > 0.0 && (w <= max_w || !m.anywhere) {
            if let Some(l) = lines.last_mut() {
                while l.last().is_some_and(|p| p.text.trim().is_empty()) {
                    l.pop();
                }
            }
            lines.push(vec![piece(t, 0, n, 0.0, t.lp, t.rp)]);
            x = w;
            continue;
        }
        if !m.anywhere {
            lines.last_mut().unwrap().push(piece(t, 0, n, x, t.lp, t.rp));
            x += w;
            continue;
        }
        // Break anywhere: fill the line char by char, then carry on below.
        let (mut a, mut lp) = (0, t.lp);
        loop {
            let mut b = a;
            while b < n && x + lp + (t.bounds[b + 1] - t.bounds[a]) <= max_w {
                b += 1;
            }
            if b == a {
                if x > 0.0 {
                    lines.push(Vec::new());
                    x = 0.0;
                    continue;
                }
                b = a + 1;
            }
            let last = b == n;
            let p = piece(t, a, b, x, lp, if last { t.rp } else { 0.0 });
            x += p.w;
            lines.last_mut().unwrap().push(p);
            if last {
                break;
            }
            lines.push(Vec::new());
            x = 0.0;
            a = b;
            lp = 0.0;
        }
    }
    lines.truncate(m.clamp.max(1));
    lines
        .into_iter()
        .enumerate()
        .map(|(i, pieces)| {
            let width = pieces.iter().rev().find(|p| !p.text.trim().is_empty()).map_or(0.0, |p| p.x + p.w);
            RunLine { y: m.lh * i as f64, height: m.lh, width, pieces }
        })
        .collect()
}

fn font_of(s: &Style) -> (u32, f64, f64, String) {
    let px = s.font_size.unwrap_or(13.0);
    let lh = s.line_height.unwrap_or((px * 1.5 * 100.0).round() / 100.0);
    (s.font_weight.unwrap_or(400), px, lh, s.font_family.clone().unwrap_or_else(|| "Inter, sans-serif".into()))
}

fn font_str(s: &Style) -> String {
    let (w, px, _, fam) = font_of(s);
    match s.font_style.as_deref() {
        Some(st @ ("italic" | "oblique")) => format!("{st} {w} {px}px {fam}"),
        _ => format!("{w} {px}px {fam}"),
    }
}

/// `up`: the ancestors' selector nodes (root first) when the sheet has descendant /
/// child selectors (`None` otherwise, to skip the bookkeeping).
fn resolve<'e>(
    ctx: &mut dyn RenderContext,
    sheet: &Sheet,
    el: &'e El,
    parent: Option<&Style>,
    state: &dyn Fn(&str) -> State,
    up: &mut Option<Vec<Node<'e>>>,
) -> Option<Resolved> {
    let mut st = el.id.as_deref().map_or(State::NONE, state);
    match &el.control {
        Some(c) if c.checked() == Some(true) => st = st | State::CHECKED,
        Some(Control::Input(e)) if e.caret.is_some() => st = st | State::FOCUS,
        _ => {}
    }
    let node = Node::parse(&el.spec).state(st);
    // Inline declarations: custom properties take part in the element's own cascade (its
    // sheet rules' `var()` see them, and they reach the subtree like sheet ones), then
    // every other inline value is applied with `var()` resolved against them.
    let decls: Vec<(&str, &str)> = el.css.as_deref().map_or_else(Vec::new, |css| {
        css.split(';').filter_map(|d| d.split_once(':')).map(|(k, v)| (k.trim(), v.trim())).collect()
    });
    let inline_vars = decls.iter().any(|(k, _)| k.starts_with("--"));
    let ups: &[Node<'_>] = up.as_deref().unwrap_or(&[]);
    let mut s = if inline_vars {
        sheet.resolve_with_inline(&node, ups, parent, &decls)
    } else if !ups.is_empty() {
        sheet.resolve_in(&node, ups, parent)
    } else {
        sheet.resolve(&node, parent)
    };
    if !decls.is_empty() {
        for (k, v) in decls.iter().filter(|(k, _)| !k.starts_with("--")) {
            let v = sheet.subst_with(v, &s.custom);
            apply_decl(&mut s, k, &v);
        }
    }
    if el.touch_capture && s.touch_action.is_none() {
        s.touch_action = Some("none".into());
    }
    if s.display.as_deref() == Some("none") {
        return None;
    }
    let runs = (!el.runs.is_empty()).then(|| std::rc::Rc::new(measure_runs(ctx, sheet, el, &s, up.as_deref().unwrap_or(&[]))));
    let text = if runs.is_some() || matches!(el.control, Some(Control::Input(_))) { None } else { el.text.clone() };
    let mut measured = text.as_ref().map(|t| {
        ctx.set_font(&font_str(&s));
        (units(ctx, t, wraps(&s)), font_of(&s).2)
    });
    let mut bounds = Vec::new();
    if let Some(Control::Input(e)) = &el.control {
        // One unwrapped line: the shown text (or the placeholder) sizes an auto width.
        ctx.set_font(&font_str(&s));
        let shown = e.display();
        bounds.push(0.0);
        let mut acc = String::new();
        for ch in shown.chars() {
            acc.push(ch);
            bounds.push(ctx.measure_text(&acc));
        }
        let t = if e.shows_placeholder() { e.placeholder.clone() } else { shown };
        let w = ctx.measure_text(&t).ceil();
        measured = Some((vec![(t, w, false, false)], font_of(&s).2));
    }
    let space = if text.is_some() && wraps(&s) { ctx.measure_text(" ") } else { 0.0 };
    let children = if let Some(u) = up.as_mut() {
        u.push(node);
        let kids: Vec<Resolved> = el.children.iter().filter_map(|c| resolve(ctx, sheet, c, Some(&s), state, up)).collect();
        if let Some(u) = up.as_mut() {
            u.pop();
        }
        kids
    } else {
        el.children.iter().filter_map(|c| resolve(ctx, sheet, c, Some(&s), state, up)).collect()
    };
    let kind = el.spec.split(['.', '#', ':']).next().unwrap_or("div").to_string();
    Some(Resolved {
        kind,
        id: el.id.clone(),
        style: s,
        text,
        tab: el.tab,
        name: el.name.clone(),
        svg: el.svg.clone(),
        anim: el.anim.clone(),
        measured,
        space,
        runs,
        control: el.control.clone(),
        slot: el.slot.clone(),
        bounds,
        scroll_top: el.scroll_top,
        children,
    })
}

/// The most lines wrapping text may take (`line-clamp`).
fn clamp_of(s: &Style) -> usize {
    s.line_clamp.map_or(usize::MAX, usize::from)
}

fn wraps(s: &Style) -> bool {
    matches!(s.white_space.as_deref(), Some("normal") | Some("pre-wrap"))
}

/// Text cut at its break opportunities: `(segment, its width, a space before it, it starts a
/// new line)` per unit; unwrapped text is one unit.
type Units = Vec<(String, f64, bool, bool)>;

fn units(ctx: &mut dyn RenderContext, t: &str, wrap: bool) -> Units {
    if !wrap {
        return vec![(t.to_string(), ctx.measure_text(t).ceil(), false, false)];
    }
    let mut out = Vec::new();
    for (pi, para) in t.split('\n').enumerate() {
        for (wi, word) in para.split(' ').enumerate() {
            for (si, seg) in crate::ui::widgets::atomic::bubble::hyphen_segments(word).into_iter().enumerate() {
                out.push((seg.to_string(), ctx.measure_text(seg), si == 0 && wi > 0, si == 0 && wi == 0 && pi > 0));
            }
        }
    }
    out
}

/// Greedy line breaking of `u` at `limit` (`None` = no limit): the lines' unit ranges and
/// widths. `space` is the width of one space.
fn break_lines(u: &Units, space: f64, limit: Option<f64>) -> Vec<(usize, usize, f64)> {
    let mut lines = Vec::new();
    let (mut start, mut w) = (0, 0.0);
    for (i, (_, sw, sp, newline)) in u.iter().enumerate() {
        let add = if i == start { *sw } else { w + if *sp { space } else { 0.0 } + sw };
        if i > start && (*newline || limit.is_some_and(|l| add > l + 0.5)) {
            lines.push((start, i, w));
            start = i;
            w = *sw;
        } else {
            w = add;
        }
    }
    if start < u.len() {
        lines.push((start, u.len(), w));
    }
    lines
}

fn line_text(u: &Units, a: usize, b: usize) -> String {
    let mut s = String::new();
    for (i, (seg, _, sp, _)) in u[a..b].iter().enumerate() {
        if i > 0 && *sp {
            s.push(' ');
        }
        s.push_str(seg);
    }
    s
}

fn lbox<'a>(r: &Resolved, measures: &'a [Box<dyn Fn(Option<f64>) -> (f64, f64)>], next: &mut usize) -> LBox<'a> {
    let mut b = LBox::new(r.style.clone());
    if r.measured.is_some() || r.runs.is_some() {
        b = b.text(measures[*next].as_ref());
        *next += 1;
    }
    for c in &r.children {
        b = b.child(lbox(c, measures, next));
    }
    b
}

fn collect_measures(r: &Resolved, out: &mut Vec<Box<dyn Fn(Option<f64>) -> (f64, f64)>>) {
    if let Some(m) = &r.runs {
        let m = m.clone();
        out.push(Box::new(move |lim: Option<f64>| {
            let ls = wrap_runs(&m, lim);
            (ls.iter().map(|l| l.width).fold(0.0, f64::max).ceil(), m.lh * ls.len().max(1) as f64)
        }));
    } else if let Some((u, lh)) = &r.measured {
        if wraps(&r.style) {
            // Wrapping: min-content is the widest unit, else the lines at the limit.
            let (u, lh, space, clamp) = (u.clone(), *lh, r.space, clamp_of(&r.style));
            out.push(Box::new(move |lim: Option<f64>| {
                let mut ls = break_lines(&u, space, lim);
                ls.truncate(clamp);
                (ls.iter().map(|l| l.2).fold(0.0, f64::max).ceil(), lh * ls.len().max(1) as f64)
            }));
        } else {
            // One line: min-content is the whole line (nowrap); a narrower box ellipsizes.
            let (w, lh) = (u[0].1, *lh);
            out.push(Box::new(move |lim: Option<f64>| (lim.filter(|l| *l > 0.0).map_or(w, |l| w.min(l)), lh)));
        }
    }
    for c in &r.children {
        collect_measures(c, out);
    }
}

/// `alpha`: the ancestors' opacity; a placed box carries its effective opacity (CSS
/// `opacity` applies to the whole subtree).
/// Positioned subtrees (absolute / relative) leave the flow's paint order: they are
/// collected in `layers` with their `z-index` and painted after it.
/// `fixed`: inside a `position: fixed` subtree. Fixed subtrees form the top layers
/// (above every non-fixed one, by `z-index` among themselves).
#[allow(clippy::too_many_arguments)]
fn place(r: Resolved, l: &Laid, out: &mut Vec<Placed>, layers: &mut Vec<(bool, i32, Vec<Placed>)>, alpha: f64, fixed: bool, clip: Option<Rect>) {
    let mut style = r.style;
    let inner = if style.clips() {
        let e = style.border_edges();
        let pb = Rect::new(l.rect.x + e[3], l.rect.y + e[0], (l.rect.width - e[1] - e[3]).max(0.0), (l.rect.height - e[0] - e[2]).max(0.0));
        Some(clip.map_or(pb, |c| c.intersect(pb)))
    } else {
        clip
    };
    let a = alpha * style.opacity.unwrap_or(1.0);
    style.opacity = (a < 1.0).then_some(a);
    let scroll_height = if style.clips() {
        let e = style.border_edges();
        let top = l.rect.y + e[0];
        let pad = style.padding.unwrap_or([0.0; 4])[2];
        let inner_h = (l.rect.height - e[0] - e[2]).max(0.0);
        (deepest(&l.children, top) + pad - top).max(inner_h)
    } else {
        0.0
    };
    // `scrollTop`: the descendants are placed `scroll_top` higher (the box stays put).
    let shifted: Option<Vec<Laid>> = (r.scroll_top != 0.0).then(|| l.children.iter().map(|c| shift_laid(c, -r.scroll_top)).collect());
    let kids: &[Laid] = shifted.as_deref().unwrap_or(&l.children);
    let inline = r.runs.map(|m| {
        let c = content_rect(&style, l.rect);
        let mut lines = wrap_runs(&m, Some(c.width));
        // A box taller than its lines centres them (as plain text); text-align moves each line.
        let top = ((c.height - m.lh * lines.len() as f64) / 2.0).max(0.0);
        for ln in &mut lines {
            ln.y += top;
            let dx = match own_text_align(&style) {
                Some("center") => (c.width - ln.width) / 2.0,
                Some("right") | Some("end") => c.width - ln.width,
                _ => 0.0,
            };
            if dx != 0.0 {
                for pc in &mut ln.pieces {
                    pc.x += dx;
                }
            }
        }
        let (runs, text) = std::rc::Rc::try_unwrap(m).map(|m| (m.runs, m.text)).unwrap_or_else(|rc| (rc.runs.clone(), rc.text.clone()));
        Box::new(Inline { runs, lines, text })
    });
    out.push(Placed {
        id: r.id,
        kind: r.kind,
        rect: l.rect,
        style,
        text: r.text,
        tab: r.tab,
        name: r.name,
        svg: r.svg,
        anim: r.anim,
        inline,
        control: r.control,
        slot: r.slot,
        fixed,
        clip,
        bounds: r.bounds,
        scroll_height,
    });
    for (c, cl) in r.children.into_iter().zip(kids) {
        if c.style.positioned() {
            let z = c.style.z_index.unwrap_or(0);
            let f = fixed || c.style.fixed();
            // Reserve the slot first: positioned descendants inside it paint above it.
            let at = layers.len();
            layers.push((f, z, Vec::new()));
            let mut sub = Vec::new();
            // A fixed box escapes its ancestors' overflow clips.
            let cc = if f && !fixed { None } else { inner };
            if c.style.z_index.is_some() {
                // A z-index makes a stacking context: the box's own positioned
                // descendants paint inside it (by their z-index), not among its siblings.
                let mut own = Vec::new();
                place(c, cl, &mut sub, &mut own, a, f, cc);
                stack(&mut sub, own);
            } else {
                place(c, cl, &mut sub, layers, a, f, cc);
            }
            layers[at].2 = sub;
        } else {
            place(c, cl, out, layers, a, fixed, inner);
        }
    }
}

/// Merge a stacking context's positioned `layers` into its painted `flow` (the context's
/// box first): negative z-index under the flow, the rest above it, ties in document order.
fn stack(flow: &mut Vec<Placed>, mut layers: Vec<(bool, i32, Vec<Placed>)>) {
    if layers.is_empty() {
        return;
    }
    crate::sort::sort_by_key(&mut layers, |l| l.1);
    let rest = if flow.is_empty() { Vec::new() } else { flow.split_off(1) };
    let split = layers.iter().position(|l| l.1 >= 0).unwrap_or(layers.len());
    let mut layers = layers.into_iter();
    for (_, _, l) in layers.by_ref().take(split) {
        flow.extend(l);
    }
    flow.extend(rest);
    for (_, _, l) in layers {
        flow.extend(l);
    }
}

/// The lowest bottom edge in `ls` and their descendants (`floor` when empty).
fn deepest(ls: &[Laid], floor: f64) -> f64 {
    ls.iter().fold(floor, |m, c| deepest(&c.children, m.max(c.rect.y + c.rect.height)))
}

/// `l` and its descendants moved down by `dy`.
fn shift_laid(l: &Laid, dy: f64) -> Laid {
    Laid { rect: Rect::new(l.rect.x, l.rect.y + dy, l.rect.width, l.rect.height), children: l.children.iter().map(|c| shift_laid(c, dy)).collect() }
}

/// Where an element's own text sits across its content box: `text-align`, else, in a
/// flex container, where its anonymous flex item goes — `justify-content` on a row,
/// `align-items` on a column (`center`, `flex-end` / `end` as `right`). A centred
/// `display: flex; justify-content: center` button centres its label as in a browser.
fn own_text_align(s: &Style) -> Option<&str> {
    if let Some(a) = s.text_align.as_deref() {
        return Some(a);
    }
    if !matches!(s.display.as_deref(), Some("flex") | Some("inline-flex")) {
        return None;
    }
    let column = matches!(s.flex_direction.as_deref(), Some("column") | Some("column-reverse"));
    let main = if column { s.align_items.as_deref() } else { s.justify_content.as_deref() };
    match main {
        // One item: `space-around` / `space-evenly` centre it, `space-between` keeps it at the start.
        Some("center") | Some("space-around") | Some("space-evenly") => Some("center"),
        Some("flex-end") | Some("end") => Some("right"),
        _ => None,
    }
}

/// Lay out `root` at `(x, y)` in a containing block `w` wide, without painting.
/// `state(id)` gives an element's interaction state (hover, focus-visible, ...).
pub fn lay_out(ctx: &mut dyn RenderContext, sheet: &Sheet, root: &El, x: f64, y: f64, w: f64, state: &dyn Fn(&str) -> State) -> Dom {
    lay_out_in(ctx, sheet, root, x, y, w, None, state)
}

/// [`lay_out`] in a `viewport` (window coordinates; the root's box when `None`):
/// `position: fixed` elements are placed against it and stay put when the tree
/// scrolls ([`Dom::moved`]); they paint above everything else (a popover, a toast, a
/// sticky composer over a scrolled page). `%` heights of the root resolve against it.
#[allow(clippy::too_many_arguments)]
pub fn lay_out_in(ctx: &mut dyn RenderContext, sheet: &Sheet, root: &El, x: f64, y: f64, w: f64, viewport: Option<Rect>, state: &dyn Fn(&str) -> State) -> Dom {
    let mut up = sheet.has_complex().then(Vec::new);
    let Some(r) = resolve(ctx, sheet, root, None, state, &mut up) else { return Dom::default() };
    let mut measures = Vec::new();
    collect_measures(&r, &mut measures);
    let laid = {
        let mut next = 0;
        let b = lbox(&r, &measures, &mut next);
        layout_in(&b, x, y, w, viewport)
    };
    drop(measures);
    let mut flow = Vec::new();
    let mut layers = Vec::new();
    let root_fixed = r.style.fixed();
    place(r, &laid, &mut flow, &mut layers, 1.0, root_fixed, None);
    if layers.is_empty() {
        return Dom { boxes: flow, root: laid.rect };
    }
    // Paint order: the root, negative z-index layers, the flow, then the other layers by
    // z-index (ties in document order).
    crate::sort::sort_by_key(&mut layers, |l| (l.0, l.1));
    let mut boxes = Vec::with_capacity(flow.len() + layers.iter().map(|l| l.2.len()).sum::<usize>());
    let mut flow = flow.into_iter();
    boxes.extend(flow.next());
    let split = layers.iter().position(|l| l.0 || l.1 >= 0).unwrap_or(layers.len());
    let mut layers = layers.into_iter();
    for (_, _, l) in layers.by_ref().take(split) {
        boxes.extend(l);
    }
    boxes.extend(flow);
    for (_, _, l) in layers {
        boxes.extend(l);
    }
    Dom { boxes, root: laid.rect }
}

/// Paint one placed box: shadow, background, border, text, kind content, inside the
/// clip of its `overflow` ancestors ([`Placed::clip`]): a box painted on its own stays
/// inside its scroll container as it does in [`paint`].
pub fn paint_box(ctx: &mut dyn RenderContext, b: &Placed) {
    with_clip(ctx, b, |ctx| paint_box_unclipped(ctx, b));
}

fn paint_box_unclipped(ctx: &mut dyn RenderContext, b: &Placed) {
    let s = &b.style;
    if let Some(a) = s.opacity {
        ctx.set_global_alpha(a);
    }
    let r = b.rect;
    if let Some(blur) = s.backdrop_blur {
        ctx.blur_backdrop(r.x, r.y, r.width, r.height, blur, s.border_radius.unwrap_or(0.0));
    }
    let shadow: Option<Shadow> = s.box_shadow.as_deref().and_then(<Option<Shadow>>::from_css).flatten();
    let sdx = if shadow.is_some() { shadow_dx(s.box_shadow.as_deref()) } else { 0.0 };
    let bg = s.background.as_deref().filter(|b| *b != "transparent" && *b != "none");
    if s.border_radii.is_some() || s.border_widths.is_some() || s.border_colors.is_some() || s.background_clip.is_some() {
        paint_frame(ctx, r, s, bg, shadow, sdx);
    } else {
        let rad = s.border_radius.unwrap_or(0.0);
        let bw = s.border_width.unwrap_or(0.0);
        let border = s.border_color.as_deref().filter(|_| bw > 0.0 && s.border_drawn());
        if let Some(sh) = shadow {
            ctx.set_shadow(sdx, sh.dy, sh.blur, sh.color);
        }
        if let Some(bg) = bg {
            fill_round_rect(ctx, r, rad, bg);
        } else if let Some(sh) = shadow {
            // A shadow needs something to cast it.
            fill_round_rect(ctx, r, rad, sh.color);
        }
        ctx.clear_shadow();
        if let Some(bc) = border {
            ctx.set_stroke_color(bc);
            ctx.set_stroke_width(bw);
            let dash = s.border_dash(bw);
            if let Some(d) = dash {
                ctx.set_line_dash(&d);
            }
            path_round_rect(ctx, Rect::new(r.x + bw / 2.0, r.y + bw / 2.0, r.width - bw, r.height - bw), (rad - bw / 2.0).max(0.0));
            ctx.stroke();
            if dash.is_some() {
                ctx.set_line_dash(&[]);
            }
        }
    }
    let p = s.pad();
    let be = s.border_edges();
    let content = content_rect(s, r);
    let color = s.color.as_deref().unwrap_or("#000000");
    if let Some(inl) = &b.inline {
        paint_runs(ctx, inl, content, color);
    }
    if let Some(t) = &b.text {
        let (_, px, lh, _) = font_of(s);
        ctx.set_font(&font_str(s));
        let lines: Vec<String> = if wraps(s) {
            let u = units(ctx, t, true);
            let space = ctx.measure_text(" ");
            let ls = break_lines(&u, space, Some(content.width));
            let clamp = clamp_of(s);
            let mut v: Vec<String> = ls.iter().take(clamp).map(|&(a, z, _)| line_text(&u, a, z)).collect();
            if ls.len() > clamp {
                // The clamp cuts after the last kept line and puts the ellipsis at its end.
                if let Some(l) = v.last_mut() {
                    let e = ellipsize(ctx, &format!("{}…", l.trim_end()), content.width + 0.5);
                    *l = if e.ends_with('…') { e } else { format!("{e}…") };
                }
            }
            v
        } else {
            vec![ellipsize(ctx, t, content.width + 0.5)]
        };
        let (align, x) = match own_text_align(s) {
            Some("center") => (TextAlign::Center, content.x + content.width / 2.0),
            Some("right") | Some("end") => (TextAlign::Right, content.x + content.width),
            _ => (TextAlign::Left, content.x),
        };
        // A box taller than its text centres it (flex `align-items:center`, the usual control).
        let top = content.y + ((content.height - lh * lines.len() as f64) / 2.0).max(0.0);
        ctx.set_fill_color(color);
        for (i, l) in lines.iter().enumerate() {
            fill_text_css(ctx, l, x, top + lh * i as f64, px, lh, INTER, align);
        }
    }
    if let Some(svg) = &b.svg {
        if s.shape_rendering.as_deref() == Some("geometricPrecision") {
            crate::render::draw_svg_icon_exact(ctx, svg, content.x, content.y, content.width, content.height, color);
        } else {
            crate::render::draw_svg_icon(ctx, svg, content.x, content.y, content.width, content.height, color);
        }
    }
    if let Some(ctl) = &b.control {
        paint_control(ctx, b, ctl, content, color);
    }
    if b.kind == "select" {
        let c = s.custom("--chevron").unwrap_or(color);
        let (ax, ay) = (r.x + r.width - be[1] - p[1].min(20.0).max(12.0) / 2.0 - 8.0, r.y + r.height / 2.0 - 2.0);
        chevron(ctx, ax.max(content.x + content.width + 2.0), ay, c);
    }
    if s.opacity.is_some() {
        ctx.set_global_alpha(1.0);
    }
}

/// Paint inline runs in content box `c`: each run's chip (a run with a background) once per
/// line it is on, spanning its pieces there, then the text in the run's font and colour
/// (`fg` when the run sets none).
fn paint_runs(ctx: &mut dyn RenderContext, inl: &Inline, c: Rect, fg: &str) {
    for line in &inl.lines {
        let mut i = 0;
        while i < line.pieces.len() {
            let p = &line.pieces[i];
            let mut j = i + 1;
            while j < line.pieces.len() && line.pieces[j].run == p.run {
                j += 1;
            }
            let last = &line.pieces[j - 1];
            if let Some(rs) = inl.runs.get(p.run).map(|r| &r.style) {
                if let Some(bg) = rs.background.as_deref().filter(|b| *b != "transparent" && *b != "none") {
                    // The chip: a 1.2em content box plus the run's vertical padding, centred in the line.
                    let (_, px, _, _) = font_of(rs);
                    let rp = rs.pad();
                    let h = (px * 1.2 + rp[0] + rp[2]).round();
                    let chip = Rect::new(c.x + p.x, c.y + line.y + ((line.height - h) / 2.0).round(), last.x + last.w - p.x, h);
                    let rad = fit_radii(chip, rs.radii());
                    ctx.begin_path();
                    add_radii_path(ctx, chip, rad, false);
                    ctx.set_fill_color(bg);
                    ctx.fill();
                }
            }
            i = j;
        }
        for p in &line.pieces {
            let Some(run) = inl.runs.get(p.run) else { continue };
            let rs = &run.style;
            let (_, px, _, _) = font_of(rs);
            ctx.set_font(&font_str(rs));
            ctx.set_fill_color(rs.color.as_deref().unwrap_or(fg));
            fill_text_css(ctx, &p.text, c.x + p.x + p.pad, c.y + line.y, px, line.height, INTER, TextAlign::Left);
        }
    }
}

/// Paint a built-in control into content box `c` (`fg`: the element's text colour).
fn paint_control(ctx: &mut dyn RenderContext, b: &Placed, ctl: &Control, c: Rect, fg: &str) {
    let s = &b.style;
    let num = |k: &str, d: f64| s.custom(k).and_then(|v| v.trim().trim_end_matches("px").parse::<f64>().ok()).unwrap_or(d);
    match ctl {
        Control::Switch { pos, .. } => {
            let d = c.height.min(c.width);
            let x = c.x + (c.width - d) * pos.clamp(0.0, 1.0);
            ctx.set_fill_color(s.custom("--knob").unwrap_or("#ffffff"));
            ctx.begin_path();
            ctx.arc(x + d / 2.0, c.y + c.height / 2.0, d / 2.0, 0.0, std::f64::consts::TAU);
            ctx.fill();
        }
        Control::Checkbox { on, mixed } => {
            if !on && !mixed {
                return;
            }
            let k = s.custom("--check").unwrap_or(fg);
            let w = num("--check-width", 2.0);
            let (x, y, sz) = (c.x, c.y, c.width.min(c.height));
            ctx.set_stroke_color(k);
            ctx.set_stroke_width(w);
            ctx.set_line_cap("round");
            ctx.begin_path();
            if *mixed {
                ctx.move_to(x + sz * 0.2, y + sz * 0.5);
                ctx.line_to(x + sz * 0.8, y + sz * 0.5);
            } else {
                ctx.move_to(x + sz * 0.18, y + sz * 0.52);
                ctx.line_to(x + sz * 0.42, y + sz * 0.76);
                ctx.line_to(x + sz * 0.84, y + sz * 0.26);
            }
            ctx.stroke();
            ctx.set_line_cap("butt");
        }
        Control::Spinner { t } => {
            let r = c.width.min(c.height) / 2.0;
            let w = num("--spin-width", (r / 4.0).clamp(1.5, 4.0));
            let (cx, cy, rr) = (c.x + c.width / 2.0, c.y + c.height / 2.0, (r - w / 2.0).max(0.5));
            ctx.set_stroke_width(w);
            if let Some(tr) = s.custom("--spin-track") {
                ctx.set_stroke_color(tr);
                ctx.begin_path();
                ctx.arc(cx, cy, rr, 0.0, std::f64::consts::TAU);
                ctx.stroke();
            }
            let a = (t * num("--spin-speed", 1.0)).fract() * std::f64::consts::TAU;
            ctx.set_stroke_color(fg);
            ctx.set_line_cap("round");
            ctx.begin_path();
            ctx.arc(cx, cy, rr, a, a + std::f64::consts::PI * 1.5);
            ctx.stroke();
            ctx.set_line_cap("butt");
        }
        Control::Ring { frac } => {
            let r = c.width.min(c.height) / 2.0;
            let w = num("--ring-width", (r / 6.0).clamp(1.5, 6.0));
            let (cx, cy, rr) = (c.x + c.width / 2.0, c.y + c.height / 2.0, (r - w / 2.0).max(0.5));
            ctx.set_stroke_width(w);
            ctx.set_stroke_color(s.custom("--ring-track").unwrap_or("rgba(255,255,255,0.08)"));
            ctx.begin_path();
            ctx.arc(cx, cy, rr, 0.0, std::f64::consts::TAU);
            ctx.stroke();
            if *frac > 0.0 {
                let a = -std::f64::consts::FRAC_PI_2;
                ctx.set_stroke_color(s.custom("--ring-color").unwrap_or(fg));
                ctx.set_line_cap("round");
                ctx.begin_path();
                ctx.arc(cx, cy, rr, a, a + std::f64::consts::TAU * frac.min(1.0));
                ctx.stroke();
                ctx.set_line_cap("butt");
            }
        }
        Control::Input(e) => {
            let (_, px, lh, _) = font_of(s);
            let top = c.y + ((c.height - lh) / 2.0).max(0.0);
            let scroll = b.input_scroll();
            ctx.save();
            ctx.begin_path();
            ctx.rect(c.x, c.y, c.width, c.height);
            ctx.clip();
            ctx.set_font(&font_str(s));
            let x0 = c.x - scroll;
            if e.shows_placeholder() {
                ctx.set_fill_color(s.custom("--placeholder").unwrap_or("#8a8a8a"));
                fill_text_css(ctx, &e.placeholder, x0, top, px, lh, INTER, TextAlign::Left);
            } else {
                let at = |i: usize| b.bounds.get(i).copied().unwrap_or(0.0);
                if let (Some(a), Some(k)) = (e.anchor, e.caret) {
                    let (lo, hi) = (a.min(k), a.max(k));
                    if lo != hi && e.preedit.is_none() {
                        ctx.set_fill_color(s.custom("--selection").unwrap_or("rgba(59,130,246,0.35)"));
                        ctx.fill_rect(x0 + at(lo), top, at(hi) - at(lo), lh);
                    }
                }
                ctx.set_fill_color(fg);
                fill_text_css(ctx, &e.display(), x0, top, px, lh, INTER, TextAlign::Left);
                if let (Some(p), Some(k)) = (&e.preedit, e.caret) {
                    let (a, z) = (at(k), at(k + p.chars().count()));
                    ctx.set_stroke_color(fg);
                    ctx.set_stroke_width(1.0);
                    ctx.begin_path();
                    ctx.move_to(x0 + a, top + lh - 2.5);
                    ctx.line_to(x0 + z, top + lh - 2.5);
                    ctx.stroke();
                }
            }
            if let (true, Some(cr)) = (e.caret_on, b.caret_rect()) {
                ctx.set_fill_color(s.custom("--caret").unwrap_or(fg));
                ctx.fill_rect(cr.x, cr.y + 2.0, 1.0, (cr.height - 4.0).max(1.0));
            }
            ctx.restore();
        }
    }
}

/// The native `<select>` chevron (8×4, 2px round stroke) with its left end at `(x, y)`.
pub fn chevron(ctx: &mut dyn RenderContext, x: f64, y: f64, color: &str) {
    ctx.set_stroke_color(color);
    ctx.set_stroke_width(2.0);
    ctx.set_line_cap("round");
    ctx.begin_path();
    ctx.move_to(x, y);
    ctx.line_to(x + 4.0, y + 4.0);
    ctx.line_to(x + 8.0, y);
    ctx.stroke();
    ctx.set_line_cap("butt");
}

/// Paint a laid-out tree (document order: parents under children).
pub fn paint(ctx: &mut dyn RenderContext, dom: &Dom) {
    paint_slots(ctx, dom, &mut |_, _, _| {});
}

/// [`paint`], calling `slot(ctx, key, content box)` where each embed slot
/// ([`El::slot`]) is in paint order, so a kit widget drawn there sits under later
/// layers (a popover, a fixed panel) and inside `overflow: hidden` ancestors.
pub fn paint_slots(ctx: &mut dyn RenderContext, dom: &Dom, slot: &mut dyn FnMut(&mut dyn RenderContext, &str, Rect)) {
    for b in &dom.boxes {
        with_clip(ctx, b, |ctx| {
            paint_box_unclipped(ctx, b);
            if let Some(k) = &b.slot {
                slot(ctx, k, content_box(b));
            }
        });
    }
}

/// Run `f` inside `b`'s overflow clip ([`Placed::clip`]): for what a host paints in a
/// box itself (an embed, an animation), so it stays in the box's scroll container like
/// the box does. Nothing runs when the clip is empty (scrolled out of sight).
pub fn with_clip(ctx: &mut dyn RenderContext, b: &Placed, f: impl FnOnce(&mut dyn RenderContext)) {
    match b.clip {
        Some(c) => {
            if c.width <= 0.0 || c.height <= 0.0 {
                return;
            }
            ctx.save();
            ctx.begin_path();
            ctx.rect(c.x, c.y, c.width, c.height);
            ctx.clip();
            f(ctx);
            ctx.restore();
        }
        None => f(ctx),
    }
}

/// One frame of a tree's animation nodes ([`paint_with`], [`paint_anim`]).
pub struct AnimFrame<'a> {
    /// Seconds on the host's clock.
    pub t: f64,
    /// What can be seen, in the tree's coordinates (a scroll viewport, the window):
    /// nodes outside it do not run.
    pub visible: Rect,
    /// The window pointer, for nodes that track it.
    pub pointer: Option<(f64, f64)>,
    /// The pointer if `id` is the topmost hit under it (z-order safe, as the framework's
    /// `pointer_over`): what other nodes see, so a drawer above one hides the pointer
    /// from it.
    pub pointer_over: &'a dyn Fn(&str) -> Option<(f64, f64)>,
    /// Hidden page or reduced motion: nodes paint frame `t` but ask for no next frame.
    pub paused: bool,
}

impl<'a> AnimFrame<'a> {
    /// A frame at `t` showing `visible`, without a pointer.
    pub fn new(t: f64, visible: Rect) -> Self {
        Self { t, visible, pointer: None, pointer_over: &|_| None, paused: false }
    }
}

/// What the animation nodes of a frame need next.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct AnimReport {
    /// Nodes that ran.
    pub painted: usize,
    /// Seconds until the next frame some running node needs (`None`: all still).
    pub next: Option<f64>,
    /// A painted node tracks the pointer: repaint when it moves.
    pub tracks_pointer: bool,
}

/// The content box of a placed element.
pub fn content_box(b: &Placed) -> Rect {
    content_rect(&b.style, b.rect)
}

fn content_rect(s: &Style, r: Rect) -> Rect {
    let (e, p) = (s.border_edges(), s.pad());
    Rect::new(r.x + p[3] + e[3], r.y + p[0] + e[0], (r.width - p[1] - p[3] - e[1] - e[3]).max(0.0), (r.height - p[0] - p[2] - e[0] - e[2]).max(0.0))
}

/// Corner radii scaled down as CSS does when adjacent ones overflow a side.
fn fit_radii(r: Rect, rad: [[f64; 2]; 4]) -> [[f64; 2]; 4] {
    let mut f: f64 = 1.0;
    for (sum, len) in [
        (rad[0][0] + rad[1][0], r.width),
        (rad[3][0] + rad[2][0], r.width),
        (rad[0][1] + rad[3][1], r.height),
        (rad[1][1] + rad[2][1], r.height),
    ] {
        if sum > len && sum > 0.0 {
            f = f.min(len.max(0.0) / sum);
        }
    }
    rad.map(|c| [c[0].max(0.0) * f, c[1].max(0.0) * f])
}

/// A rounded rect outline with per-corner elliptical radii (top-left, top-right,
/// bottom-right, bottom-left; `[h, v]`, already fitted) as cubic segments from
/// `start`, clockwise.
fn radii_segments(r: Rect, rad: [[f64; 2]; 4]) -> ((f64, f64), Vec<[(f64, f64); 3]>) {
    const K: f64 = 0.5523;
    let (x0, y0, x1, y1) = (r.x, r.y, r.x + r.width, r.y + r.height);
    let [tl, tr, br, bl] = rad;
    let line = |a: (f64, f64), b: (f64, f64)| [a, b, b];
    let mut v = Vec::with_capacity(8);
    v.push(line((x0 + tl[0], y0), (x1 - tr[0], y0)));
    v.push([(x1 - tr[0] + tr[0] * K, y0), (x1, y0 + tr[1] - tr[1] * K), (x1, y0 + tr[1])]);
    v.push(line((x1, y0 + tr[1]), (x1, y1 - br[1])));
    v.push([(x1, y1 - br[1] + br[1] * K), (x1 - br[0] + br[0] * K, y1), (x1 - br[0], y1)]);
    v.push(line((x1 - br[0], y1), (x0 + bl[0], y1)));
    v.push([(x0 + bl[0] - bl[0] * K, y1), (x0, y1 - bl[1] + bl[1] * K), (x0, y1 - bl[1])]);
    v.push(line((x0, y1 - bl[1]), (x0, y0 + tl[1])));
    v.push([(x0, y0 + tl[1] - tl[1] * K), (x0 + tl[0] - tl[0] * K, y0), (x0 + tl[0], y0)]);
    ((x0 + tl[0], y0), v)
}

/// Add a per-corner rounded rect to the current path; `reverse` winds it
/// counter-clockwise (a hole under the non-zero rule).
fn add_radii_path(ctx: &mut dyn RenderContext, r: Rect, rad: [[f64; 2]; 4], reverse: bool) {
    let (start, segs) = radii_segments(r, rad);
    if !reverse {
        ctx.move_to(start.0, start.1);
        for [c1, c2, e] in segs {
            ctx.bezier_curve_to(c1.0, c1.1, c2.0, c2.1, e.0, e.1);
        }
    } else {
        ctx.move_to(start.0, start.1);
        let mut prev = start;
        // Walk backwards: each segment's end becomes the next start.
        let pts: Vec<((f64, f64), [(f64, f64); 3])> = segs
            .iter()
            .map(|s| {
                let from = prev;
                prev = s[2];
                (from, *s)
            })
            .collect();
        for (from, [c1, c2, _]) in pts.into_iter().rev() {
            ctx.bezier_curve_to(c2.0, c2.1, c1.0, c1.1, from.0, from.1);
        }
    }
    ctx.close_path();
}

/// Start a path along a placed box's border-box outline, its corner radii included
/// (per-corner, elliptical). For a shadow or fill the box style cannot express.
pub fn path_border_box(ctx: &mut dyn RenderContext, b: &Placed) {
    ctx.begin_path();
    add_radii_path(ctx, b.rect, fit_radii(b.rect, b.style.radii()), false);
}

/// Clip to a placed box's padding box with the inner corner radii CSS gives it (outer
/// radius less the border width on each axis): what `overflow: hidden` clips its
/// children to. Pair with `save` / `restore`.
///
/// ```
/// # use uzor::ui::dom::{clip_padding_box, paint_box, Dom};
/// # fn f(ctx: &mut dyn uzor::render::RenderContext, dom: &Dom) {
/// let (frame, kids) = dom.boxes.split_first().unwrap();
/// paint_box(ctx, frame);
/// ctx.save();
/// clip_padding_box(ctx, frame);
/// kids.iter().for_each(|b| paint_box(ctx, b));
/// ctx.restore();
/// # }
/// ```
pub fn clip_padding_box(ctx: &mut dyn RenderContext, b: &Placed) {
    let (r, e) = (b.rect, b.style.border_edges());
    let inner = Rect::new(r.x + e[3], r.y + e[0], (r.width - e[1] - e[3]).max(0.0), (r.height - e[0] - e[2]).max(0.0));
    ctx.begin_path();
    add_radii_path(ctx, inner, inner_radii(fit_radii(r, b.style.radii()), e), false);
    ctx.clip();
}

/// CSS inner (padding-box) radii: each outer radius less the adjacent border widths.
fn inner_radii(rad: [[f64; 2]; 4], e: [f64; 4]) -> [[f64; 2]; 4] {
    [
        [(rad[0][0] - e[3]).max(0.0), (rad[0][1] - e[0]).max(0.0)],
        [(rad[1][0] - e[1]).max(0.0), (rad[1][1] - e[0]).max(0.0)],
        [(rad[2][0] - e[1]).max(0.0), (rad[2][1] - e[2]).max(0.0)],
        [(rad[3][0] - e[3]).max(0.0), (rad[3][1] - e[2]).max(0.0)],
    ]
}

/// The x offset of a `box-shadow: <dx> <dy> <blur> <color>` (the first length;
/// 0 when it has fewer than three). [`Shadow`] keeps only dy and blur, so the DOM
/// reads dx here instead of widening that public struct.
fn shadow_dx(css: Option<&str>) -> f64 {
    let Some(v) = css else { return 0.0 };
    let mut it = v.split_whitespace().map(|t| t.trim_end_matches("px").parse::<f64>());
    match (it.next(), it.next(), it.next()) {
        (Some(Ok(dx)), Some(Ok(_)), Some(Ok(_))) => dx,
        _ => 0.0,
    }
}

/// Paint a CSS box's background and border with per-corner radii and per-side border
/// widths / colours (the uniform case keeps the classic single-radius path).
fn paint_frame(ctx: &mut dyn RenderContext, r: Rect, s: &Style, bg: Option<&str>, shadow: Option<Shadow>, sdx: f64) {
    let rad = fit_radii(r, s.radii());
    let e = s.border_edges();
    let colors: [Option<&str>; 4] = [0, 1, 2, 3].map(|i| s.border_side_color(i));
    if let Some(sh) = shadow {
        ctx.set_shadow(sdx, sh.dy, sh.blur, sh.color);
    }
    if let Some(fill) = bg.or(shadow.map(|sh| sh.color)) {
        ctx.begin_path();
        match s.background_inset() {
            // `background-clip`: the fill is the inset box with CSS's inner radii.
            Some(i) => {
                let inner = Rect::new(r.x + i[3], r.y + i[0], (r.width - i[1] - i[3]).max(0.0), (r.height - i[0] - i[2]).max(0.0));
                add_radii_path(ctx, inner, inner_radii(rad, i), false);
            }
            None => add_radii_path(ctx, r, rad, false),
        }
        ctx.set_fill_color(fill);
        ctx.fill();
    }
    ctx.clear_shadow();
    if e.iter().all(|w| *w <= 0.0) || colors.iter().all(Option::is_none) || !s.border_drawn() {
        return;
    }
    let uniform_w = e.iter().all(|w| *w == e[0]);
    let uniform_c = colors.iter().all(|c| *c == colors[0]);
    let dash = s.border_dash(e[0]);
    if uniform_w && uniform_c {
        if let Some(d) = dash {
            ctx.set_line_dash(&d);
        }
        // One stroke along the inset outline (radii shrunk by half the width).
        let w = e[0];
        let inner = Rect::new(r.x + w / 2.0, r.y + w / 2.0, r.width - w, r.height - w);
        let ir = rad.map(|c| [(c[0] - w / 2.0).max(0.0), (c[1] - w / 2.0).max(0.0)]);
        ctx.set_stroke_color(colors[0].unwrap_or("#000000"));
        ctx.set_stroke_width(w);
        ctx.begin_path();
        add_radii_path(ctx, inner, ir, false);
        ctx.stroke();
        if dash.is_some() {
            ctx.set_line_dash(&[]);
        }
        return;
    }
    // The border ring: the outer outline minus the padding-box outline (CSS inner radii).
    let inner = Rect::new(r.x + e[3], r.y + e[0], (r.width - e[1] - e[3]).max(0.0), (r.height - e[0] - e[2]).max(0.0));
    let ir = inner_radii(rad, e);
    let ring = |ctx: &mut dyn RenderContext, color: &str| {
        ctx.begin_path();
        add_radii_path(ctx, r, rad, false);
        add_radii_path(ctx, inner, ir, true);
        ctx.set_fill_color(color);
        ctx.fill();
    };
    if uniform_c {
        ring(ctx, colors[0].unwrap_or("#000000"));
        return;
    }
    // Each side clipped to its trapezoid (outer corners to inner corners).
    let (x0, y0, x1, y1) = (r.x, r.y, r.x + r.width, r.y + r.height);
    let (ix0, iy0, ix1, iy1) = (inner.x, inner.y, inner.x + inner.width, inner.y + inner.height);
    let quads = [
        [(x0, y0), (x1, y0), (ix1, iy0), (ix0, iy0)],
        [(x1, y0), (x1, y1), (ix1, iy1), (ix1, iy0)],
        [(x1, y1), (x0, y1), (ix0, iy1), (ix1, iy1)],
        [(x0, y1), (x0, y0), (ix0, iy0), (ix0, iy1)],
    ];
    for i in 0..4 {
        let Some(c) = colors[i].filter(|_| e[i] > 0.0) else { continue };
        ctx.save();
        ctx.begin_path();
        ctx.move_to(quads[i][0].0, quads[i][0].1);
        for q in &quads[i][1..] {
            ctx.line_to(q.0, q.1);
        }
        ctx.close_path();
        ctx.clip();
        ring(ctx, c);
        ctx.restore();
    }
}

/// Run `b`'s animation (if it has one) for `frame`: the script paints in content-box
/// coordinates (origin at its top-left, `p.w` × `p.h`), clipped to it unless
/// [`AnimNode::overflow`], under the element's opacity; the pointer is given in the same
/// local coordinates. Skipped when the box is outside `frame.visible` or transparent.
/// Returns whether it ran; its needs are added to `report`.
pub fn paint_anim(ctx: &mut dyn RenderContext, b: &Placed, reg: &mut AnimRegistry, frame: &AnimFrame<'_>, report: &mut AnimReport) -> bool {
    let Some(a) = &b.anim else { return false };
    let c = content_box(b);
    let v = frame.visible;
    let outside = c.x >= v.x + v.width || c.y >= v.y + v.height || c.x + c.width <= v.x || c.y + c.height <= v.y;
    let alpha = b.style.opacity.unwrap_or(1.0);
    if outside || c.width <= 0.0 || c.height <= 0.0 || alpha <= 0.0 {
        return false;
    }
    let ptr = if a.track_pointer { frame.pointer } else { b.id.as_deref().and_then(frame.pointer_over) };
    let params = AnimParams { w: c.width, h: c.height, seed: a.seed, mouse: ptr.map(|(x, y)| (x - c.x, y - c.y)), args: a.args };
    ctx.save();
    if !a.overflow {
        ctx.begin_path();
        ctx.rect(c.x, c.y, c.width, c.height);
        ctx.clip();
    }
    ctx.translate(c.x, c.y);
    if alpha < 1.0 {
        ctx.set_global_alpha(alpha);
    }
    let ok = reg.paint(ctx, &a.name, frame.t, &params).is_ok();
    ctx.restore();
    if alpha < 1.0 {
        ctx.set_global_alpha(1.0);
    }
    report.painted += 1;
    report.tracks_pointer |= a.track_pointer && ptr.is_some();
    if let (Some(fps), false, true) = (a.fps.filter(|f| *f > 0.0), frame.paused, ok) {
        let d = 1.0 / fps;
        report.next = Some(report.next.map_or(d, |n: f64| n.min(d)));
    }
    true
}

/// Paint a laid-out tree with its animation nodes (each box, then its animation, in
/// document order, so later boxes cover earlier animations).
pub fn paint_with(ctx: &mut dyn RenderContext, dom: &Dom, reg: &mut AnimRegistry, frame: &AnimFrame<'_>) -> AnimReport {
    let mut report = AnimReport::default();
    for b in &dom.boxes {
        with_clip(ctx, b, |ctx| {
            paint_box_unclipped(ctx, b);
            paint_anim(ctx, b, reg, frame, &mut report);
        });
    }
    report
}

/// Lay out and paint `root`.
pub fn render(ctx: &mut dyn RenderContext, sheet: &Sheet, root: &El, x: f64, y: f64, w: f64, state: &dyn Fn(&str) -> State) -> Dom {
    let dom = lay_out(ctx, sheet, root, x, y, w, state);
    paint(ctx, &dom);
    dom
}

#[cfg(all(test, feature = "flow-flex"))]
mod tests {
    use super::*;
    use crate::testing::NullRenderContext;

    #[test]
    fn box_shadow_dx_is_the_first_of_three_lengths() {
        assert_eq!(shadow_dx(Some("3px 4px 0 rgba(0,0,0,0.5)")), 3.0);
        assert_eq!(shadow_dx(Some("-6px 0 2px #000")), -6.0);
        assert_eq!(shadow_dx(Some("0 8px 16px #000")), 0.0);
        assert_eq!(shadow_dx(Some("4px 8px")), 0.0, "fewer than three lengths: no offset read");
        assert_eq!(shadow_dx(Some("none")), 0.0);
        assert_eq!(shadow_dx(None), 0.0);
    }

    #[test]
    fn background_clip_parses_and_insets() {
        let sh = Sheet::parse("div.a { border: 2px solid #000; padding: 3px; background-clip: padding-box } div.b { border: 2px solid #000; padding: 3px; background-clip: Content-Box } div.c { border: 2px solid #000 }").unwrap();
        let mut ctx = NullRenderContext::default();
        let page = El::new("div.r").child(El::new("div.a").id("a")).child(El::new("div.b").id("b")).child(El::new("div.c").id("c"));
        let dom = lay_out(&mut ctx, &sh, &page, 0.0, 0.0, 200.0, &|_| State::NONE);
        let st = |id: &str| dom.boxes.iter().find(|b| b.id.as_deref() == Some(id)).unwrap().style.clone();
        assert_eq!(st("a").background_inset(), Some([2.0; 4]));
        assert_eq!(st("b").background_inset(), Some([5.0; 4]));
        assert_eq!(st("c").background_inset(), None, "border-box is the default");
    }

    #[test]
    fn inline_custom_properties_reach_the_subtree() {
        let sh = Sheet::parse(":root { --acc: #111111 } span.t { color: var(--acc) } div.b { background: var(--acc) }").unwrap();
        let mut ctx = NullRenderContext::default();
        let page = El::new("div.r").css("--acc: #ff0000; border-color: var(--acc)").child(El::new("span.t").id("t").text("x")).child(El::new("div.b").id("b"));
        let dom = lay_out(&mut ctx, &sh, &page, 0.0, 0.0, 200.0, &|_| State::NONE);
        assert_eq!(dom.get("t").and_then(|p| p.style.color.as_deref()), Some("#ff0000"));
        assert_eq!(dom.get("b").and_then(|p| p.style.background.as_deref()), Some("#ff0000"));
        let plain = El::new("div.r").child(El::new("span.t").id("t").text("x"));
        let dom = lay_out(&mut ctx, &sh, &plain, 0.0, 0.0, 200.0, &|_| State::NONE);
        assert_eq!(dom.get("t").and_then(|p| p.style.color.as_deref()), Some("#111111"));
    }

    /// An element's own sheet rules see its inline custom properties (`<div class="root"
    /// style="--fg: …">` with `.root { color: var(--fg) }`), and inline ones beat the
    /// sheet's for the same element; text inheriting from it gets the inline value.
    #[test]
    fn inline_custom_properties_feed_the_elements_own_rules() {
        let sh = Sheet::parse(":root { --fg: #eeeeee } div.root { color: var(--fg); --edge: #222222; border-color: var(--edge) }").unwrap();
        let mut ctx = NullRenderContext::default();
        let page = El::new("div.root").id("r").css("--fg: #1d1f24; --edge: #ff0000").child(El::new("div").id("t").text("x"));
        let dom = lay_out(&mut ctx, &sh, &page, 0.0, 0.0, 200.0, &|_| State::NONE);
        assert_eq!(dom.get("r").and_then(|p| p.style.color.as_deref()), Some("#1d1f24"));
        assert_eq!(dom.get("t").and_then(|p| p.style.color.as_deref()), Some("#1d1f24"), "inherited text");
        assert_eq!(dom.get("r").and_then(|p| p.style.border_color.as_deref()), Some("#ff0000"), "inline beats the sheet's custom property");
    }

    const CSS: &str = r#"
        :root { --txt: #f5f5f5 }
        div.page { display: flex; gap: 10px; padding: 8px; color: var(--txt); font-size: 13px }
        div.nav { width: 100px }
        div.body { flex-grow: 1; display: flex; flex-direction: column; gap: 6px }
        div.row { display: flex; justify-content: space-between; align-items: center; padding: 4px 8px; background: #222; border-radius: 8px }
        select { padding: 5px 30px 5px 12px; background: #2a2a2a; border-radius: 8px }
        select:hover { background: #333 }
        div.gone { display: none }
    "#;

    fn page() -> El {
        El::new("div.page")
            .child(El::new("div.nav").id("nav"))
            .child(
                El::new("div.body")
                    .child(El::new("div.row").id("r1").child(El::new("span").text("Timezone")).child(El::new("select").id("sel:tz").text("Auto").tab()))
                    .child(El::new("div.row.gone").id("hidden"))
                    .child(El::new("div.row").id("r2").css("padding: 10px 8px").child(El::new("span").text("Accent"))),
            )
    }

    #[test]
    fn lays_out_by_css_and_reports_ids() {
        let mut ctx = NullRenderContext::default();
        let sh = Sheet::parse(CSS).unwrap();
        let d = lay_out(&mut ctx, &sh, &page(), 0.0, 0.0, 500.0, &|_| State::NONE);
        let nav = d.rect("nav").unwrap();
        assert_eq!((nav.x, nav.width), (8.0, 100.0));
        let r1 = d.rect("r1").unwrap();
        assert_eq!(r1.x, 118.0);
        assert_eq!(r1.width, 500.0 - 8.0 - 118.0);
        // The select sits at the row's right edge (space-between), inside its padding.
        let sel = d.rect("sel:tz").unwrap();
        assert!((sel.x + sel.width - (r1.x + r1.width - 8.0)).abs() < 0.01);
        assert!(d.rect("hidden").is_none());
        // Inline css wins: 10px vertical padding.
        let r2 = d.rect("r2").unwrap();
        assert_eq!(r2.y, r1.y + r1.height + 6.0);
        assert!(r2.height > r1.height - 0.01 || r2.height >= 19.5 + 20.0);
        assert_eq!(d.tabbable().collect::<Vec<_>>(), vec!["sel:tz"]);
        assert!(d.names().any(|(i, n)| i == "sel:tz" && n == "Auto"));
    }

    #[test]
    fn hover_state_restyles() {
        let mut ctx = NullRenderContext::default();
        let sh = Sheet::parse(CSS).unwrap();
        let d = render(&mut ctx, &sh, &page(), 0.0, 0.0, 500.0, &|id| if id == "sel:tz" { State::HOVER } else { State::NONE });
        let b = d.boxes.iter().find(|b| b.id.as_deref() == Some("sel:tz")).unwrap();
        assert_eq!(b.style.background.as_deref(), Some("#333"));
        assert_eq!(b.kind, "select");
    }

    #[test]
    fn white_space_normal_wraps_at_spaces_and_hyphens() {
        let u: Units = [("one", 30.0, false, false), ("two-", 40.0, true, false), ("three", 50.0, false, false), ("four", 40.0, true, false)]
            .iter()
            .map(|(t, w, sp, nl)| (t.to_string(), *w, *sp, *nl))
            .collect();
        // Unlimited: one line, spaces counted, no space at the hyphen break.
        assert_eq!(break_lines(&u, 5.0, None), vec![(0, 4, 30.0 + 5.0 + 40.0 + 50.0 + 5.0 + 40.0)]);
        // 100 px: "one two-" | "three four"; 80 px: "four" goes to a third line.
        assert_eq!(break_lines(&u, 5.0, Some(100.0)), vec![(0, 2, 75.0), (2, 4, 95.0)]);
        assert_eq!(break_lines(&u, 5.0, Some(80.0)).len(), 3);
        assert_eq!(line_text(&u, 0, 2), "one two-");
        assert_eq!(line_text(&u, 2, 4), "three four");
        // Laid out in a narrow box the text grows by whole lines; nowrap stays one line.
        let mut ctx = crate::testing::RecordingRenderContext::with_char_width(7.0);
        let sh = Sheet::parse("div.p { width: 60px; font-size: 10px; line-height: 15px } div.w { white-space: normal }").unwrap();
        let text = "aaaa bbbb cccc dddd eeee";
        let one = lay_out(&mut ctx, &sh, &El::new("div.p").child(El::new("div").id("t").text(text)), 0.0, 0.0, 500.0, &|_| State::NONE);
        let many = lay_out(&mut ctx, &sh, &El::new("div.p.w").child(El::new("div").id("t").text(text)), 0.0, 0.0, 500.0, &|_| State::NONE);
        assert_eq!(one.rect("t").unwrap().height, 15.0);
        let h = many.rect("t").unwrap().height;
        assert!(h > 15.0 && (h / 15.0).fract() == 0.0, "{h}");
        // line-clamp: at most two lines, the last ending in an ellipsis.
        let sh2 = Sheet::parse("div.p { width: 60px; font-size: 10px; line-height: 15px; white-space: normal } div.c { -webkit-line-clamp: 2 }").unwrap();
        let clamped = render(&mut ctx, &sh2, &El::new("div.p").child(El::new("div.c").id("t").text(text)), 0.0, 0.0, 500.0, &|_| State::NONE);
        assert_eq!(clamped.rect("t").unwrap().height, 30.0);
        assert_eq!(clamped.boxes.last().unwrap().style.line_clamp, Some(2));
    }

    #[test]
    fn anim_nodes_lay_out_paint_and_pause() {
        use crate::render::{AnimRegistry, FnAnim};
        use std::cell::RefCell;
        use std::rc::Rc;
        let mut ctx = NullRenderContext::default();
        let sh = Sheet::parse("div.row { display: flex; gap: 4px; padding: 2px } div.av { width: 40px; height: 40px; padding: 2px } div.grow { flex-grow: 1 }").unwrap();
        let root = El::new("div.row")
            .child(El::new("div.av").id("a").anim(AnimNode::new("spy").seed(7).args([1.0, 2.0, 3.0, 4.0])))
            .child(El::new("div.grow").id("drawer"))
            .child(El::new("div.av").id("b").anim(AnimNode::new("spy").fps(None).track_pointer(true)));
        let d = lay_out(&mut ctx, &sh, &root, 10.0, 20.0, 300.0, &|_| State::NONE);
        let (a, b) = (d.rect("a").unwrap(), d.rect("b").unwrap());
        assert_eq!((a.x, a.y, a.width), (12.0, 22.0, 40.0));
        assert_eq!(b.x + b.width, 10.0 + 300.0 - 2.0, "flex places the anim box like any other");
        let seen: Rc<RefCell<Vec<AnimParams>>> = Rc::default();
        let s2 = seen.clone();
        let mut reg = AnimRegistry::new();
        reg.fallback("spy", Box::new(FnAnim(move |_: &mut dyn RenderContext, _t: f64, p: &AnimParams| s2.borrow_mut().push(*p))));
        // The pointer is over `a` (topmost there), 5px into its content box.
        let over = |id: &str| (id == "a").then_some((a.x + 2.0 + 5.0, a.y + 2.0 + 6.0));
        let f = AnimFrame { t: 1.0, visible: Rect::new(0.0, 0.0, 1000.0, 1000.0), pointer: Some((0.0, 0.0)), pointer_over: &over, paused: false };
        let r = paint_with(&mut ctx, &d, &mut reg, &f);
        assert_eq!(r.painted, 2);
        assert_eq!(r.next, Some(1.0 / 30.0), "only `a` moves");
        assert!(r.tracks_pointer);
        let v = seen.borrow().clone();
        assert_eq!((v[0].w, v[0].h, v[0].seed, v[0].args), (36.0, 36.0, 7, [1.0, 2.0, 3.0, 4.0, 0.0, 0.0, 0.0, 0.0]));
        assert_eq!(v[0].mouse, Some((5.0, 6.0)), "local coordinates");
        assert_eq!(v[1].mouse, Some((-(b.x + 2.0), -(b.y + 2.0))), "a tracking node sees the window pointer anywhere");
        // Not topmost: no pointer for the hover-only node.
        seen.borrow_mut().clear();
        let f2 = AnimFrame { pointer_over: &|_| None, ..AnimFrame::new(2.0, Rect::new(0.0, 0.0, 1000.0, 1000.0)) };
        paint_with(&mut ctx, &d, &mut reg, &f2);
        assert_eq!(seen.borrow()[0].mouse, None);
        // Scrolled out of view: nothing runs, no frame asked; paused: runs, asks nothing.
        seen.borrow_mut().clear();
        let r = paint_with(&mut ctx, &d, &mut reg, &AnimFrame::new(3.0, Rect::new(0.0, 100.0, 1000.0, 100.0)));
        assert_eq!((r.painted, r.next, seen.borrow().len()), (0, None, 0));
        let r = paint_with(&mut ctx, &d, &mut reg, &AnimFrame { paused: true, ..AnimFrame::new(3.0, Rect::new(0.0, 0.0, 1000.0, 1000.0)) });
        assert_eq!((r.painted, r.next), (2, None));
    }

    #[test]
    fn anim_frame_is_clipped_to_its_content_box() {
        use crate::render::{AnimRegistry, FnAnim};
        let mut ctx = crate::testing::RecordingRenderContext::new();
        let sh = Sheet::parse("div.av { width: 20px; height: 10px }").unwrap();
        let mut reg = AnimRegistry::new();
        reg.fallback("dot", Box::new(FnAnim(|c: &mut dyn RenderContext, _t: f64, _p: &AnimParams| c.fill_rect(0.0, 0.0, 1.0, 1.0))));
        for overflow in [false, true] {
            let d = lay_out(&mut ctx, &sh, &El::new("div.av").anim(AnimNode::new("dot").overflow_visible(overflow)), 5.0, 6.0, 100.0, &|_| State::NONE);
            ctx.ops.clear();
            paint_with(&mut ctx, &d, &mut reg, &AnimFrame::new(0.0, Rect::new(0.0, 0.0, 100.0, 100.0)));
            let ops = format!("{:?}", ctx.ops);
            assert_eq!(ops.contains("Clip"), !overflow, "{ops}");
            assert!(ops.contains("Translate"), "{ops}");
        }
    }

    #[test]
    fn runs_wrap_across_styles_and_hit_per_piece() {
        // 7px per char in every font: easy arithmetic.
        let mut ctx = crate::testing::RecordingRenderContext::with_char_width(7.0);
        let sh = Sheet::parse(
            "div.m { width: 100px; font-size: 10px; line-height: 20px; white-space: pre-wrap; overflow-wrap: anywhere; color: #eee }
             code { font-family: monospace; background: #222; padding: 1px 6px; border-radius: 6px }
             a.link { color: #60a5fa } b { font-weight: 700 } i { font-style: italic }",
        )
        .unwrap();
        let el = El::new("div.m").id("m").runs([
            Run::text("see "),
            Run::new("code", "ab"),
            Run::text(" "),
            Run::new("a.link", "https://x.y/zzzz").id("url:1"),
            Run::new("b", " ok"),
            Run::new("i", "\nit"),
        ]);
        let d = render(&mut ctx, &sh, &el, 0.0, 0.0, 500.0, &|_| State::NONE);
        let b = d.boxes.iter().find(|b| b.id.as_deref() == Some("m")).unwrap();
        let inl = b.inline.as_ref().unwrap();
        assert_eq!(inl.text, "see ab https://x.y/zzzz ok\nit");
        // "see " 28 + chip 6+14+6 = 54, " " 61; the 16-char link (112px) breaks anywhere:
        // 5 chars fill line one (61 + 35 = 96), the rest wraps.
        let l0 = &inl.lines[0];
        let ab = l0.pieces.iter().find(|p| p.text == "ab").unwrap();
        assert_eq!((ab.x, ab.w, ab.pad, ab.start), (28.0, 26.0, 6.0, 4));
        assert_eq!(l0.pieces.last().unwrap().text, "https");
        assert_eq!(inl.lines[1].pieces[0].text, "://x.y/zzzz");
        // The forced newline starts the italic run on its own line.
        let last = inl.lines.last().unwrap();
        assert_eq!(last.pieces[0].text, "it");
        assert_eq!(b.rect.height, 20.0 * inl.lines.len() as f64);
        // The link is a hit on each of its pieces; the plain runs are not.
        let hits: Vec<_> = d.hits().filter(|(id, _)| *id == "url:1").collect();
        assert_eq!(hits.len(), 2);
        assert_eq!(hits[0].1, Rect::new(61.0, 0.0, 35.0, 20.0));
        assert_eq!(d.run_at(65.0, 5.0).map(|(_, r)| r), Some(3));
        assert_eq!(d.run_at(5.0, 5.0).map(|(_, r)| r), Some(0));
        // Char boundaries for carets: "see " is 4 chars, the chip text starts after its padding.
        assert_eq!(b.char_at(28.0 + 6.0 + 7.5, 5.0), Some(5));
        assert_eq!(b.char_at(0.0, 25.0), Some(inl.lines[1].pieces[0].start));
        // Painted: the chip, the italic and bold fonts, the link colour.
        let ops = format!("{:?}", ctx.ops);
        assert!(ops.contains("#222") && ops.contains("italic 400 10px") && ops.contains("700 10px") && ops.contains("#60a5fa"), "{ops}");
        assert!(d.names().any(|(id, n)| id == "m" && n.starts_with("see ab")));
    }

    #[test]
    fn a_multi_word_chip_is_one_box_per_line() {
        let mut ctx = crate::testing::RecordingRenderContext::with_char_width(7.0);
        let sh = Sheet::parse(
            "div.m { width: 100px; font-size: 10px; line-height: 20px; white-space: pre-wrap }
             code { background: #222; padding: 1px 6px; border-radius: 6px }",
        )
        .unwrap();
        // "x " then the chip "aa bb cc dd" (77px + 12px padding) does not fit after it: the
        // chip's words wrap, and each line gets one chip spanning its words.
        let el = El::new("div.m").id("m").runs([Run::text("x "), Run::new("code", "aa bb cc dd ee")]);
        let d = lay_out(&mut ctx, &sh, &el, 0.0, 0.0, 500.0, &|_| State::NONE);
        ctx.ops.clear();
        paint(&mut ctx, &d);
        let fills = ctx.ops.iter().filter(|o| format!("{o:?}").contains("#222")).count();
        let b = d.boxes.iter().find(|b| b.id.as_deref() == Some("m")).unwrap();
        let lines = &b.inline.as_ref().unwrap().lines;
        assert_eq!(lines.len(), 2);
        assert_eq!(fills, 2, "one chip per line, not one per word");
        // Line one's chip spans "aa bb cc " from after "x " (14px).
        let chip_line = &lines[0].pieces;
        let first = chip_line.iter().find(|p| p.run == 1).unwrap();
        assert_eq!((first.x, first.pad), (14.0, 6.0));
    }

    #[test]
    fn runs_size_their_box_like_text() {
        let mut ctx = crate::testing::RecordingRenderContext::with_char_width(7.0);
        let sh = Sheet::parse("div.row { display: flex } div.m { font-size: 10px; line-height: 20px; white-space: normal; max-width: 70px }").unwrap();
        let el = El::new("div.row").child(El::new("div.m").id("m").runs([Run::text("aaaa "), Run::new("b", "bbbb cc")]));
        let d = lay_out(&mut ctx, &sh, &el, 0.0, 0.0, 500.0, &|_| State::NONE);
        let r = d.rect("m").unwrap();
        // max-content 84 > 70: "aaaa bbbb" (63) | "cc".
        assert_eq!((r.width, r.height), (70.0, 40.0));
        // Wrapped spaces are dropped from the line's width.
        let b = d.boxes.iter().find(|b| b.id.as_deref() == Some("m")).unwrap();
        assert_eq!(b.inline.as_ref().unwrap().lines[0].width, 63.0);
        // Not wrapping (white-space unset): one line at max-content.
        let sh2 = Sheet::parse("div.row { display: flex } div.m { font-size: 10px; line-height: 20px }").unwrap();
        let d2 = lay_out(&mut ctx, &sh2, &el, 0.0, 0.0, 500.0, &|_| State::NONE);
        assert_eq!(d2.rect("m").unwrap(), Rect::new(0.0, 0.0, 84.0, 20.0));
    }

    #[test]
    fn absolute_boxes_leave_the_flow_and_paint_on_top() {
        let mut ctx = NullRenderContext::default();
        let sh = Sheet::parse(
            "div.card { position: relative; width: 200px; padding: 10px; border: 2px solid #333 }
             div.line { height: 30px }
             div.badge { position: absolute; top: -4px; right: -4px; width: 16px; height: 16px }
             div.under { position: absolute; inset: 0; z-index: -1 }
             div.cover { position: absolute; left: 10px; bottom: 0; width: 50px; height: 5px; z-index: 2 }
             div.wrap { padding: 4px }",
        )
        .unwrap();
        let el = El::new("div.card")
            .id("card")
            .child(El::new("div.cover").id("cover"))
            .child(El::new("div.line").id("l1"))
            .child(El::new("div.badge").id("badge"))
            .child(El::new("div.wrap").id("wrap").child(El::new("div.badge").id("deep")))
            .child(El::new("div.under").id("under"));
        let d = lay_out(&mut ctx, &sh, &el, 0.0, 0.0, 500.0, &|_| State::NONE);
        // The flow ignores the absolute boxes: l1 at the content top, the card as tall as l1 + wrap.
        assert_eq!(d.rect("l1").unwrap().y, 12.0);
        let card = d.rect("card").unwrap();
        assert_eq!(card.height, 2.0 + 10.0 + 30.0 + 8.0 + 10.0 + 2.0);
        // Against the padding box (inside the 2px border).
        assert_eq!(d.rect("badge").unwrap(), Rect::new(200.0 - 2.0 + 4.0 - 16.0, 2.0 - 4.0, 16.0, 16.0));
        // A static wrapper is no containing block: `deep` uses the card too.
        assert_eq!(d.rect("deep").unwrap(), d.rect("badge").unwrap());
        assert_eq!(d.rect("under").unwrap(), Rect::new(2.0, 2.0, 196.0, card.height - 4.0));
        assert_eq!(d.rect("cover").unwrap(), Rect::new(12.0, card.height - 2.0 - 5.0, 50.0, 5.0));
        // Paint (and hit) order: root, z<0, flow, then z>=0 layers by z-index.
        let order: Vec<&str> = d.boxes.iter().filter_map(|b| b.id.as_deref()).collect();
        assert_eq!(order, ["card", "under", "l1", "wrap", "badge", "deep", "cover"]);
        // A relative box shifts with its subtree, its flow slot unchanged.
        let sh2 = Sheet::parse("div.r { position: relative; top: 5px; left: -3px; height: 10px } div.n { height: 10px }").unwrap();
        let d2 = lay_out(&mut ctx, &sh2, &El::new("div").child(El::new("div.r").id("r")).child(El::new("div.n").id("n")), 0.0, 0.0, 100.0, &|_| State::NONE);
        assert_eq!(d2.rect("r").unwrap(), Rect::new(-3.0, 5.0, 100.0, 10.0));
        assert_eq!(d2.rect("n").unwrap().y, 10.0);
        // Shrink-to-fit width of an absolute box with text.
        let mut rc = crate::testing::RecordingRenderContext::with_char_width(7.0);
        let sh3 = Sheet::parse("div.p { position: relative; width: 300px; height: 50px } span.t { position: absolute; right: 0; bottom: 0; font-size: 10px; line-height: 20px }").unwrap();
        let d3 = lay_out(&mut rc, &sh3, &El::new("div.p").child(El::new("span.t").id("t").text("abc")), 0.0, 0.0, 500.0, &|_| State::NONE);
        assert_eq!(d3.rect("t").unwrap(), Rect::new(279.0, 30.0, 21.0, 20.0));
    }

    /// The own text of a flex container is its anonymous flex item: a row's
    /// `justify-content: center` (a column's `align-items: center`) centres it as in a
    /// browser, `text-align` still wins, and a plain block stays left.
    #[test]
    fn own_text_follows_the_flex_main_axis() {
        let sh = Sheet::parse(
            "button.b { display: flex; justify-content: center; width: 200px; height: 40px; font-size: 10px }
             button.c { display: flex; flex-direction: column; align-items: center; width: 200px; height: 40px; font-size: 10px }
             button.e { display: flex; justify-content: flex-end; width: 200px; height: 40px; font-size: 10px }
             button.l { display: flex; justify-content: center; text-align: left; width: 200px; height: 40px; font-size: 10px }
             div.p { width: 200px; height: 40px; font-size: 10px }",
        )
        .unwrap();
        let x_of = |spec: &str| {
            let mut ctx = crate::testing::RecordingRenderContext::with_char_width(7.0);
            render(&mut ctx, &sh, &El::new(spec).text("abc"), 0.0, 0.0, 500.0, &|_| State::NONE);
            let mut texts = ctx.fill_texts().into_iter().map(|(_, x, _)| x);
            texts.next().expect("the label")
        };
        let left = x_of("div.p");
        assert!(left < 1.0, "a block keeps its text at the left: {left}");
        assert!((x_of("button.b") - 100.0).abs() < 11.0, "row centre: {}", x_of("button.b"));
        assert!((x_of("button.c") - 100.0).abs() < 11.0, "column centre: {}", x_of("button.c"));
        assert!(x_of("button.e") > 170.0, "row end: {}", x_of("button.e"));
        assert!(x_of("button.l") < 1.0, "text-align wins: {}", x_of("button.l"));
    }

    #[test]
    fn per_corner_radii_and_per_side_borders() {
        let mut ctx = crate::testing::RecordingRenderContext::new();
        let sh = Sheet::parse(
            "div.top { width: 100px; height: 40px; background: #111; border-radius: 12px 12px 0 0 }
             div.ell { width: 100px; height: 40px; background: #111; border-radius: 20px / 10px }
             div.side { width: 100px; padding: 4px; border-width: 0 0 2px 3px; border-color: red; height: 20px }
             div.mix { width: 100px; height: 20px; border: 1px solid #333; border-left: 4px solid #3b82f6 }",
        )
        .unwrap();
        let s = sh.el("div.top");
        assert_eq!(s.radii(), [[12.0, 12.0], [12.0, 12.0], [0.0, 0.0], [0.0, 0.0]]);
        assert_eq!(sh.el("div.ell").radii()[2], [20.0, 10.0]);
        // Per-side widths move the content box.
        let d = render(&mut ctx, &sh, &El::new("div.side").id("s").child(El::new("div").id("in").css("height: 5px")), 0.0, 0.0, 500.0, &|_| State::NONE);
        assert_eq!(d.rect("in").unwrap(), Rect::new(3.0 + 4.0, 4.0, 100.0 - 3.0 - 8.0, 5.0));
        assert_eq!(d.rect("s").unwrap().height, 20.0);
        let mix = sh.el("div.mix");
        assert_eq!(mix.border_edges(), [1.0, 1.0, 1.0, 4.0]);
        assert_eq!(mix.border_side_color(3), Some("#3b82f6"));
        assert_eq!(mix.border_side_color(0), Some("#333"));
        // Painting a square-bottomed box: the bottom corners are plain line joins (zero
        // radius), the top ones curves; mixed side colours clip each side.
        ctx.ops.clear();
        render(&mut ctx, &sh, &El::new("div.top"), 0.0, 0.0, 500.0, &|_| State::NONE);
        let ops = format!("{:?}", ctx.ops);
        assert!(ops.contains("BezierCurveTo"), "{ops}");
        ctx.ops.clear();
        render(&mut ctx, &sh, &El::new("div.mix"), 0.0, 0.0, 500.0, &|_| State::NONE);
        let ops = format!("{:?}", ctx.ops);
        assert_eq!(ops.matches("Clip").count(), 4, "{ops}");
        assert!(ops.contains("#3b82f6") && ops.contains("#333"));
        // The classic single radius keeps its exact old path.
        let classic = Sheet::parse("div { width: 10px; height: 10px; background: #000; border-radius: 3px }").unwrap();
        assert_eq!(classic.el("div").border_radii, None);
    }

    #[test]
    fn geometric_precision_icons_are_not_snapped() {
        let icon = r#"<svg viewBox="0 0 24 24" stroke-width="1.5"><path d="M4 12H20"/></svg>"#;
        let mut ctx = crate::testing::RecordingRenderContext::new();
        let sh = Sheet::parse("i.ic { width: 24px; height: 24px } i.x { shape-rendering: geometricPrecision }").unwrap();
        for (spec, want) in [("i.ic", 2.0), ("i.ic.x", 1.5)] {
            ctx.ops.clear();
            render(&mut ctx, &sh, &El::new(spec).svg(icon), 0.3, 0.0, 100.0, &|_| State::NONE);
            let w = ctx.ops.iter().find_map(|o| if let crate::testing::DrawOp::StrokeWidth(w) = o { Some(*w) } else { None });
            assert_eq!(w, Some(want), "{spec}");
        }
    }

    #[test]
    fn padding_box_clip_uses_the_inner_radii() {
        let mut ctx = crate::testing::RecordingRenderContext::new();
        let sh = Sheet::parse("div { width: 100px; height: 50px; border-width: 0 0 1px 1px; border-color: #222; border-radius: 12px 0 0 12px }").unwrap();
        let d = lay_out(&mut ctx, &sh, &El::new("div"), 0.0, 0.0, 500.0, &|_| State::NONE);
        clip_padding_box(&mut ctx, &d.boxes[0]);
        let ops = format!("{:?}", ctx.ops);
        // Starts after the top-left corner: 1px border, 11px inner horizontal radius.
        assert!(ops.contains("MoveTo { x: 12.0, y: 0.0 }") && ops.contains("Clip"), "{ops}");
        ctx.ops.clear();
        path_border_box(&mut ctx, &d.boxes[0]);
        assert!(format!("{:?}", ctx.ops).contains("MoveTo { x: 12.0, y: 0.0 }"));
    }

    /// Each `fill_text` with the clip in force when it ran (every `rect` + `clip` under
    /// the open `save`s intersected; `None` = unclipped).
    fn texts_with_clips(ops: &[crate::testing::DrawOp]) -> Vec<(String, f64, Option<Rect>)> {
        use crate::testing::DrawOp as D;
        let (mut stack, mut cur, mut path, mut out) = (Vec::new(), None::<Rect>, None::<Rect>, Vec::new());
        for op in ops {
            match op {
                D::Save => stack.push(cur),
                D::Restore => cur = stack.pop().flatten(),
                D::BeginPath => path = None,
                D::Rect { x, y, w, h } => path = Some(Rect::new(*x, *y, *w, *h)),
                D::Clip => cur = path.map(|p| cur.map_or(p, |c: Rect| c.intersect(p))),
                D::FillText { text, x, .. } => out.push((text.clone(), *x, cur)),
                _ => {}
            }
        }
        out
    }

    #[test]
    fn scroll_container_clips_text_placeholders_and_inputs_painted_box_by_box() {
        // A dialog: a scrolling body over a footer. The body is scrolled so the second input
        // sits under the footer; a long value runs past the first input's right edge.
        let sh = Sheet::parse(
            "div.card { width: 200px; height: 120px; display: flex; flex-direction: column } \
             div.body { flex-grow: 1; min-height: 0; overflow: auto; padding-bottom: 4px; display: flex; flex-direction: column } \
             div.foot { height: 30px; flex-shrink: 0 } input { height: 24px; padding: 0 6px; font-size: 10px; line-height: 14px; flex-shrink: 0 } \
             div.t { height: 40px; flex-shrink: 0; font-size: 10px; line-height: 14px }",
        )
        .unwrap();
        let mut ctx = crate::testing::RecordingRenderContext::with_char_width(6.0);
        let long = "hsreq1.".to_string() + &"x".repeat(60);
        let tree = |y: f64| {
            El::new("div.card")
                .child(
                    El::new("div.body")
                        .id("body")
                        .scroll_top(y)
                        .child(El::new("div.t").text("Paste the block"))
                        .child(El::new("input").id("a").control(Control::input(Edit { caret: Some(67), ..Edit::new(long.clone()) })))
                        .child(El::new("div.t").text("Paste the request"))
                        .child(El::new("input").id("b").control(Control::input(Edit { placeholder: "hsreq1.…".into(), ..Edit::new("") }))),
                )
                .child(El::new("div.foot").id("foot").text("Refresh"))
        };
        let d0 = lay_out(&mut ctx, &sh, &tree(0.0), 0.0, 0.0, 400.0, &|_| State::NONE);
        let body = d0.get("body").unwrap();
        assert_eq!(body.rect, Rect::new(0.0, 0.0, 200.0, 90.0));
        assert_eq!(body.scroll_height, 40.0 + 24.0 + 40.0 + 24.0 + 4.0, "scrollHeight: content plus bottom padding");
        // scrollTop moves the descendants, not the container; the clip stays its padding box.
        let d = lay_out(&mut ctx, &sh, &tree(30.0), 0.0, 0.0, 400.0, &|_| State::NONE);
        assert_eq!(d.get("body").unwrap().rect, body.rect);
        let (b0, b) = (d0.get("b").unwrap().rect, d.get("b").unwrap().rect);
        assert_eq!(b.y, b0.y - 30.0);
        assert!(b.y < 90.0 && b.y + b.height > 90.0, "«b» straddles the footer's top: {b:?}");
        assert_eq!(d.get("b").unwrap().clip, Some(Rect::new(0.0, 0.0, 200.0, 90.0)));
        assert_eq!(d.get("foot").unwrap().clip, None, "the footer is not in the scroller");
        assert_eq!(d.hit(20.0, 100.0), Some("foot"), "the scrolled input does not take the footer's clicks");
        // Painted one box at a time (as a host that does its own per-box work): every text
        // inside the body stays in its clip, the input's own text inside its content box too.
        ctx.ops.clear();
        for p in &d.boxes {
            paint_box(&mut ctx, p);
        }
        let texts = texts_with_clips(&ctx.ops);
        for (t, _, c) in texts.iter().filter(|(t, ..)| t != "Refresh") {
            let c = c.unwrap_or_else(|| panic!("{t:?} painted unclipped"));
            assert!(c.y >= 0.0 && c.y + c.height <= 90.0 + 1e-9, "{t:?} leaks out of the body: {c:?}");
        }
        let (_, x, c) = texts.iter().find(|(t, ..)| *t == long).unwrap();
        let a = content_box(d.get("a").unwrap());
        assert!(c.unwrap().x >= a.x && c.unwrap().x + c.unwrap().width <= a.x + a.width, "the value is clipped to the input");
        assert!(*x < a.x, "scrolled left so the caret at the end shows");
        let caret = d.get("a").unwrap().caret_rect().unwrap();
        assert!(caret.x >= a.x && caret.x < a.x + a.width, "caret in view: {caret:?} in {a:?}");
        let (_, _, pc) = texts.iter().find(|(t, ..)| t == "hsreq1.…").unwrap();
        assert!(pc.unwrap().y + pc.unwrap().height <= 90.0, "placeholder cut at the footer");
        // Scrolled further, «b» is wholly under the footer: none of it paints.
        let d = lay_out(&mut ctx, &sh, &tree(38.0 + 40.0), 0.0, 0.0, 400.0, &|_| State::NONE);
        ctx.ops.clear();
        for p in &d.boxes {
            paint_box(&mut ctx, p);
        }
        let texts = texts_with_clips(&ctx.ops);
        assert!(texts.iter().filter(|(t, ..)| t == "hsreq1.…").all(|(_, _, c)| c.is_some_and(|c| c.height > 0.0 && c.y + c.height <= 90.0)), "{texts:?}");
    }

    #[test]
    fn overflow_hidden_clips_paint_and_hits_and_fixed_escapes() {
        let sh = Sheet::parse("div.v { width: 100px; height: 50px; overflow: hidden; border: 2px solid #333 } div.r { height: 40px } div.f { position: fixed; top: 0; left: 0; width: 10px; height: 10px } div.s { height: 20px }").unwrap();
        let mut ctx = NullRenderContext::default();
        let root = El::new("div.v")
            .child(El::new("div.r").id("a"))
            .child(El::new("div.r").id("b"))
            .child(El::new("div.f").id("fx"))
            .child(El::new("div.s").slot("@k"));
        let d = lay_out_in(&mut ctx, &sh, &root, 0.0, 0.0, 300.0, Some(Rect::new(0.0, 0.0, 300.0, 300.0)), &|_| State::NONE);
        let b = d.get("b").unwrap();
        assert_eq!(b.clip, Some(Rect::new(2.0, 2.0, 96.0, 46.0)));
        assert_eq!(d.hit(10.0, 45.0), Some("b"));
        assert_eq!(d.hit(10.0, 60.0), None, "clipped away");
        assert_eq!(d.get("fx").unwrap().clip, None, "fixed escapes the clip");
        // The slot callback runs in paint order, inside the clip.
        let mut seen = Vec::new();
        paint_slots(&mut ctx, &d, &mut |_, k, r| seen.push((k.to_string(), r)));
        assert_eq!(seen.len(), 1);
        let d = d.moved(0.0, -30.0);
        assert_eq!(d.get("b").unwrap().clip, Some(Rect::new(2.0, -28.0, 96.0, 46.0)));
    }
}