burin 0.1.2

A Rust GUI framework built on the auralis reactive signal kernel
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
//! Paint tree traversal: surface, text, decoration, subtree caching, and scrollbar rendering.
//! Extracted from `platform/window.rs` (audit 2026-07-21).

use glam::Affine2;
use std::rc::Rc;

use crate::core::config::StateFlags;
use crate::core::element::{Element, ElementArena};
use crate::core::ElementId;
use crate::render::painter::{ClipInfo, Painter};
use crate::render::wgpu::glyphon_bridge::create_buffer;
use crate::style::resolve_style;
use crate::style::{Color, Rect};

// ── Paint ─────────────────────────────────────────────────────────

fn intersect_rects(a: Rect, b: Rect) -> Rect {
    let x = a.x.max(b.x);
    let y = a.y.max(b.y);
    let r = (a.x + a.width).min(b.x + b.width);
    let btm = (a.y + a.height).min(b.y + b.height);
    if r > x && btm > y {
        Rect::new(x, y, r - x, btm - y)
    } else {
        Rect::new(x, y, 0.0, 0.0)
    }
}

/// Universal slack absorbing small decor overhang (underline strokes,
/// slider thumbs, 1-2px AA bleed) that paints slightly outside
/// `screen_bounds` without a dedicated style field.
const AABB_GROW_BASE: f32 = 8.0;

/// TextInput error text paints at `x + w + 8` with a shaped width of up to
/// 400px (see the decor pass) — extend the AABB rightwards to cover it.
const AABB_ERROR_TEXT_EXTEND: f32 = 408.0;

/// Layout-space visual AABB of `eid`'s subtree, memoized per element and
/// keyed by `subtree_generation` (audit 2026-07-17 L2 paint culling).
///
/// Covers: own rect + position_offset/size_scale + shadow/outline growth +
/// error-text overflow, unioned with all descendant AABBs, then passed
/// through the element's own transform (corner union, damage-tracker
/// semantics — descendants compose about this element's pivot).
///
/// Soundness notes:
/// - Nested scroll offsets move descendants UP/LEFT of their layout rects,
///   but a scroll offset only exists on Scroll/Clip containers whose own
///   clip (⊆ own rect ⊆ this AABB) bounds those descendants.
/// - slot_inactive children are included (conservative superset).
fn subtree_visual_aabb(
    arena: &ElementArena,
    ct: &crate::ecs::tables::ComponentTables,
    eid: ElementId,
) -> Rect {
    let Some(el) = arena.get(eid) else {
        return Rect::ZERO;
    };
    let gen = el.subtree_generation.get();
    if let Some((g, r)) = el.subtree_aabb.get() {
        if g == gen {
            return r;
        }
    }

    let sb = el.screen_bounds;
    let mut x = sb.x;
    let mut y = sb.y;
    let mut w = sb.width.max(1.0);
    let mut h = sb.height.max(1.0);

    let xform = ct.xform.get(&eid);
    if let Some(xf) = xform {
        let off = xf.position_offset.get();
        x += off.x;
        y += off.y;
        let sc = xf.size_scale.get();
        w *= sc.x;
        h *= sc.y;
    }

    let mut grow = AABB_GROW_BASE;
    if let Some(s) = ct.style.get(&eid) {
        if let Some(ref sh) = s.shadow {
            grow = grow.max(sh.blur + sh.offset_x.abs().max(sh.offset_y.abs()));
        }
        if s.outline_width > 0.0 {
            grow = grow.max(s.outline_width + 2.0);
        }
        // StateStyle overrides can add shadows on hover/press — include them.
        if let Some(ref st) = s.state_style {
            for variant in [
                &st.animated,
                &st.hovered,
                &st.pressed,
                &st.focused,
                &st.disabled,
                &st.checked,
                &st.loading,
                &st.invalid,
                &st.indeterminate,
                &st.drag_over,
            ] {
                if let Some(ref sh) = variant.shadow {
                    grow = grow.max(sh.blur + sh.offset_x.abs().max(sh.offset_y.abs()));
                }
            }
        }
    }
    let mut aabb = Rect::new(x - grow, y - grow, w + 2.0 * grow, h + 2.0 * grow);

    if ct.lc.get(&eid).is_some_and(|l| l.error_text.is_some()) {
        aabb.width += AABB_ERROR_TEXT_EXTEND;
    }

    for &cid in &el.children {
        let ca = subtree_visual_aabb(arena, ct, cid);
        if ca.width > 0.0 && ca.height > 0.0 {
            aabb = aabb.union(&ca);
        }
    }

    // Own transform: descendants compose about this element's pivot, so
    // applying it to the whole subtree union is the exact composition.
    // Union with the untransformed rect (mid-animation safety, mirrors
    // the damage tracker).
    if let Some(t) = xform.and_then(|xf| xf.transform) {
        let tx = glam::Affine2::from_cols_array(&t);
        let ox = xform.map_or(0.5, |xf| xf.transform_origin_x) * w;
        let oy = xform.map_or(0.5, |xf| xf.transform_origin_y) * h;
        let to_origin = glam::Affine2::from_translation(glam::Vec2::new(-(x + ox), -(y + oy)));
        let from_origin = glam::Affine2::from_translation(glam::Vec2::new(x + ox, y + oy));
        let m = from_origin * tx * to_origin;
        let c = [
            m.transform_point2(glam::Vec2::new(aabb.x, aabb.y)),
            m.transform_point2(glam::Vec2::new(aabb.x + aabb.width, aabb.y)),
            m.transform_point2(glam::Vec2::new(aabb.x + aabb.width, aabb.y + aabb.height)),
            m.transform_point2(glam::Vec2::new(aabb.x, aabb.y + aabb.height)),
        ];
        let min_x = c.iter().map(|p| p.x).fold(f32::MAX, f32::min);
        let min_y = c.iter().map(|p| p.y).fold(f32::MAX, f32::min);
        let max_x = c.iter().map(|p| p.x).fold(f32::MIN, f32::max);
        let max_y = c.iter().map(|p| p.y).fold(f32::MIN, f32::max);
        aabb = aabb.union(&Rect::new(min_x, min_y, max_x - min_x, max_y - min_y));
    }

    el.subtree_aabb.set(Some((gen, aabb)));
    aabb
}

/// Return children sorted by z_index, with generation-based caching.
/// Cache is invalidated by add_child/remove_child/clear_children and z_index changes.
fn sorted_children(arena: &ElementArena, eid: ElementId) -> Rc<Vec<ElementId>> {
    let Some(el) = arena.get(eid) else {
        return Rc::new(Vec::new());
    };
    // Check cache: valid if children list matches
    if let Some(ref cached) = *el.sorted_children.borrow() {
        if cached.len() == el.children.len() {
            let match_ids = cached.iter().zip(&el.children).all(|(a, b)| a == b);
            if match_ids {
                return Rc::clone(cached);
            }
        }
    }
    // Rebuild: sort children by z_index
    let children = &el.children;
    let mut sorted: Vec<(ElementId, i32)> = children
        .iter()
        .map(|&cid| (cid, arena.get(cid).map_or(0, |c| c.z_index)))
        .collect();
    sorted.sort_by_key(|&(_, z)| z);
    let result: Rc<Vec<ElementId>> = Rc::new(sorted.into_iter().map(|(id, _)| id).collect());
    *el.sorted_children.borrow_mut() = Some(Rc::clone(&result));
    result
}

pub(crate) fn paint_children_sorted(
    fcx: &crate::core::frame_context::FrameContext,
    ct: &crate::ecs::tables::ComponentTables,
    arena: &mut ElementArena,
    eid: ElementId,
    painter: &mut Painter,
    text_areas: &mut Vec<crate::render::wgpu::glyphon_bridge::TextAreaDesc>,
    scale_factor: f32,
    scroll_offset: (f32, f32),
    parent_should_paint: bool,
    default_bg: Color,
    default_fg: Color,
    accum_clip: Rect,
    accum_clip_radius: crate::style::CornerRadii,
    accum_xform: Affine2,
    parent_floor: i32,
    must_paint: &std::collections::HashSet<ElementId>,
    accum_opacity: f32,
    highlight_mode: crate::event::FocusHighlightMode,
) {
    let child_ids: Rc<Vec<ElementId>> = sorted_children(arena, eid);

    let parent_scroll_gen = arena
        .get(eid)
        .and_then(|el| el.get_user_data::<crate::widgets::bundle::ScrollGeneration>())
        .map(|sg| sg.0.get())
        .unwrap_or(0);

    for &cid in child_ids.iter() {
        // `child_ids` is a cached sorted-children snapshot (Rc<Vec>). An element
        // can be removed mid-frame (e.g. a submenu portal trimmed by a menu
        // interaction), leaving a stale id in this snapshot. Skip gracefully
        // instead of unwrapping — the removal already invalidated the cache, so
        // the next frame rebuilds it correctly.
        let child = match arena.get(cid) {
            Some(c) => c,
            None => continue,
        };
        if child.slot_inactive.get()
            || !ct
                .lc
                .get(&cid)
                .and_then(|l| l.reactive_visible.as_ref())
                .is_none_or(|v| v.get())
        {
            continue;
        }
        // ── Subtree AABB cull (audit 2026-07-17 L2): skip whole child
        // subtrees fully outside the accumulated clip without descending.
        // Turns scroll-frame paint from O(N_content) into O(visible).
        // Gate: accum_xform must be translation-only (scroll containers fold
        // their -offset translation into it; that shift is mirrored by
        // scroll_offset in clip space, matching the per-leaf cull's
        // `rect - scroll_offset ∩ accum_clip` semantics). True rotation /
        // scale transforms fall through to the per-leaf cull, same as today.
        if accum_xform.matrix2 == glam::Mat2::IDENTITY {
            let aabb = subtree_visual_aabb(arena, ct, cid);
            let vis_x = aabb.x - scroll_offset.0;
            let vis_y = aabb.y - scroll_offset.1;
            if vis_x + aabb.width <= accum_clip.x
                || vis_y + aabb.height <= accum_clip.y
                || vis_x >= accum_clip.x + accum_clip.width
                || vis_y >= accum_clip.y + accum_clip.height
            {
                continue;
            }
        }
        // Ticking elements are cache-eligible: every frame_tick site self-
        // reports changes via register_dirty / gen bumps (audited 2026-07-16,
        // Layer 3-3), so needs_repaint + must_paint fully cover invalidation.
        if !must_paint.contains(&cid)
            && !child.needs_repaint()
            && try_skip_subtree(
                fcx,
                ct,
                arena,
                cid,
                painter,
                text_areas,
                scroll_offset,
                parent_should_paint,
                parent_scroll_gen,
            )
        {
            continue;
        }

        let cmd_start = painter.snapshot_len();
        let ta_start = text_areas.len();
        let bd_start = painter.backdrop_regions.len();
        let root_x = child.screen_bounds.x;
        let root_y = child.screen_bounds.y;
        let _ = child;

        paint_element_tree(
            fcx,
            ct,
            arena,
            cid,
            painter,
            text_areas,
            scale_factor,
            scroll_offset,
            parent_should_paint,
            default_bg,
            default_fg,
            accum_clip,
            accum_clip_radius,
            accum_xform,
            parent_floor,
            must_paint,
            accum_opacity,
            highlight_mode,
        );

        let gen_after = crate::core::dirty_registry::content_gen_of(cid);
        let layout_after = crate::core::dirty_registry::layout_gen_of(cid);
        {
            let mut new_commands: Vec<crate::render::DrawCommand> =
                painter.commands_since(cmd_start).to_vec();
            let mut new_ta: Vec<crate::render::wgpu::glyphon_bridge::TextAreaDesc> =
                text_areas[ta_start..].to_vec();
            if new_ta.is_empty()
                && arena.get(cid).is_some_and(|el| {
                    el.children.iter().any(|&ccid| {
                        arena.get(ccid).is_some_and(|_gc| {
                            ct.text
                                .get(&ccid)
                                .and_then(|t| t.text_buffer.clone())
                                .is_some()
                        })
                    })
                })
            {
                new_commands.clear();
                new_ta.clear();
            }
            {
                let c = fcx.subtree_cache;
                let new_backdrops: Vec<crate::render::BackdropRegion> =
                    painter.backdrop_regions[bd_start..].to_vec();
                c.borrow_mut().insert(
                    cid,
                    Rc::new(crate::render::CachedSubtree {
                        commands: new_commands,
                        text_areas: new_ta,
                        backdrop_regions: new_backdrops,
                        root_x,
                        root_y,
                        scroll_ox: scroll_offset.0,
                        scroll_oy: scroll_offset.1,
                        content_gen: gen_after,
                        layout_gen: layout_after,
                        scroll_gen: parent_scroll_gen,
                    }),
                );
            }
        }
    }
}

/// Try to skip painting a child subtree by replaying cached draw commands.
/// Returns `true` if the subtree was successfully replayed from cache.
/// Cache validity requires `content_gen` match (draw commands unchanged)
/// AND scroll position match (clip rects are scroll-dependent).
fn try_skip_subtree(
    fcx: &crate::core::frame_context::FrameContext,
    ct: &crate::ecs::tables::ComponentTables,
    arena: &ElementArena,
    child_id: ElementId,
    painter: &mut Painter,
    text_areas: &mut Vec<crate::render::wgpu::glyphon_bridge::TextAreaDesc>,
    scroll_offset: (f32, f32),
    _parent_should_paint: bool,
    parent_scroll_gen: u64,
) -> bool {
    let child = match arena.get(child_id) {
        Some(c) => c,
        None => return true,
    };
    if child.slot_inactive.get()
        || !ct
            .lc
            .get(&child_id)
            .and_then(|l| l.reactive_visible.as_ref())
            .is_none_or(|v| v.get())
    {
        return true;
    }
    if child.needs_repaint() {
        crate::core::frame_pipeline::bump_subtree_cache_miss();
        return false;
    }
    let current_content = crate::core::dirty_registry::content_gen_of(child_id);
    let cache_hit = {
        let c = fcx.subtree_cache;
        let cache = c.borrow();
        cache.get(&child_id).is_some_and(|cs| {
            cs.content_gen == current_content
                && cs.scroll_gen == parent_scroll_gen
                && cs.scroll_ox == scroll_offset.0
                && cs.scroll_oy == scroll_offset.1
                && (!cs.commands.is_empty() || !cs.text_areas.is_empty())
                && !(cs.text_areas.is_empty()
                    && child.children.iter().any(|&ccid| {
                        arena.get(ccid).is_some_and(|_gc| {
                            ct.text
                                .get(&ccid)
                                .and_then(|t| t.text_buffer.clone())
                                .is_some()
                        })
                    }))
        })
    };
    if cache_hit {
        crate::core::frame_pipeline::bump_subtree_cache_hit();
        replay_subtree(fcx, arena, child_id, painter, text_areas, scroll_offset);
        return true;
    }
    crate::core::frame_pipeline::bump_subtree_cache_miss();
    false
}

fn replay_subtree(
    fcx: &crate::core::frame_context::FrameContext,
    arena: &ElementArena,
    child_id: ElementId,
    painter: &mut Painter,
    text_areas: &mut Vec<crate::render::wgpu::glyphon_bridge::TextAreaDesc>,
    scroll_offset: (f32, f32),
) {
    let child = match arena.get(child_id) {
        Some(c) => c,
        None => return,
    };
    {
        let c = fcx.subtree_cache;
        if let Some(cs) = c.borrow().get(&child_id) {
            // Screen position delta (layout change)
            let pos_dx = child.screen_bounds.x - cs.root_x;
            let pos_dy = child.screen_bounds.y - cs.root_y;
            // Scroll delta: cached scroll minus current scroll
            let scroll_dx = cs.scroll_ox - scroll_offset.0;
            let scroll_dy = cs.scroll_oy - scroll_offset.1;

            let scroll_xform =
                glam::Affine2::from_translation(glam::Vec2::new(scroll_dx, scroll_dy));

            for cmd in &cs.commands {
                let mut c = cmd.offset(pos_dx, pos_dy);
                c.adjust_transform(scroll_xform);
                painter.commands.push(c);
            }
            text_areas.extend(cs.text_areas.iter().map(|ta| {
                let ta = ta.offset(pos_dx, pos_dy);
                ta.offset(scroll_dx, scroll_dy)
            }));
            // Re-emit backdrop-blur regions (offset like commands) so the effect
            // persists on cached/static frames.
            for br in &cs.backdrop_regions {
                let mut r = br.clone();
                r.rect = r.rect.offset(pos_dx, pos_dy);
                r.transform = scroll_xform * r.transform;
                painter.backdrop_regions.push(r);
            }
        }
    }
}

/// Stretch anchored-dropdown (Select/ComboBox) portal children to the portal's
/// content width. taffy leaf sizing gives text options a fixed content width
/// that defeats `align_items: STRETCH`, so we fill them to the portal width
/// here. Portal-native replacement for the old `overlay_layer` stretch pass.
/// Shared by the window frame loop and the TestHarness.
// (stretch_visible_anchored_portals / stretch_children_to_width moved to
// platform/portal.rs — audit round 3, ② phase 1)

fn paint_element_surface(
    element: &Element,
    style_comp: Option<&crate::ecs::components::StyleComponent>,
    lc_comp: Option<&crate::ecs::components::LifecycleComponent>,
    painter: &mut Painter,
    _x: f32,
    _y: f32,
    w: f32,
    h: f32,
    _element_clip: ClipInfo,
    _element_xform: Affine2,
    ez: i32,
    _accum_clip: ClipInfo,
    _opacity: f32,
    default_bg: Color,
    highlight_mode: crate::event::FocusHighlightMode,
) {
    let style_default;
    let s = match style_comp {
        Some(s) => s,
        None => {
            style_default = crate::ecs::components::StyleComponent::default();
            &style_default
        }
    };
    let resolved = resolve_style(element.state.get(), s);
    let corners = s.corners();

    if resolved.backdrop {
        let bg = resolved.background.unwrap_or(Color::rgba8(0, 0, 0, 160));
        let screen = Rect::new(0.0, 0.0, painter.viewport.width, painter.viewport.height);
        painter.fill_rect(screen, bg, screen.into(), glam::Affine2::IDENTITY, ez);
    }

    let focused = element.state.get().contains(StateFlags::FOCUSED);
    let show_auto_ring = focused
        && resolved.outline_width == 0.0
        && highlight_mode.show_focus_ring()
        && !resolved.backdrop; // backdrop paints full-screen gap fill that would erase the scrim
    let ow = if show_auto_ring {
        2.0
    } else {
        resolved.outline_width
    };
    if ow > 0.0 && (show_auto_ring || resolved.outline_width > 0.0) && !resolved.backdrop {
        let oc = resolved
            .outline_color
            .unwrap_or(Color::rgba8(59, 130, 246, 255));
        let gap = 0.0;
        painter.push_local_outline(ow, gap, oc, default_bg, w, h, corners);
    }

    if let Some(ref sh) = resolved.shadow {
        painter.push_local_shadow(*sh, w, h, corners);
    }

    // Compute the final interior fill colour BEFORE deciding border method,
    // so Outlined buttons (raw bg=TRANSPARENT → interior=default_bg) correctly
    // use BorderFill instead of falling back to thin StrokeRect.
    let mut interior: Option<Color> = None;
    if let Some(_grad) = resolved.gradient {
        // Gradient is pushed separately; interior remains None for now.
    } else if !resolved.backdrop {
        let bg = resolved.background;
        interior = match bg {
            Some(Color::TRANSPARENT)
                if resolved.border_width > 0.0 && resolved.border_color.is_some() =>
            {
                // Explicit TRANSPARENT + border (Outlined buttons, etc.) — fill
                // interior with default_bg so the border is visible against it.
                Some(default_bg)
            }
            other => other,
        };
    }

    if resolved.border_width > 0.0 {
        if let Some(bc) = if lc_comp
            .and_then(|l| l.invalid_hint.as_ref())
            .is_some_and(|h| h.get())
        {
            Some(Color::rgba8(220, 38, 38, 255))
        } else {
            resolved.border_color
        } {
            let hw = resolved.border_width;
            // Use BorderFill (large rect clipped to border) only when the
            // final interior is opaque (covers the center).  Otherwise
            // BorderFill would paint the whole element in bc, hiding parent
            // backgrounds (e.g. Table row zebra stripes).
            let has_interior = interior.is_some_and(|c| c != Color::TRANSPARENT)
                || resolved.gradient.is_some()
                || resolved.backdrop;
            if has_interior {
                let rh = hw;
                let border_corners = crate::style::CornerRadii {
                    top_left: corners.top_left + rh,
                    top_right: corners.top_right + rh,
                    bottom_right: corners.bottom_right + rh,
                    bottom_left: corners.bottom_left + rh,
                };
                painter.push_local_border_fill(
                    Rect::new(-hw, -hw, w + hw * 2.0, h + hw * 2.0),
                    bc,
                    border_corners,
                );
            } else {
                painter.push_local_stroke_rect(Rect::new(0.0, 0.0, w, h), bc, hw, corners);
            }
        }
    }

    // Push interior fill.
    if let Some(grad) = resolved.gradient {
        painter.push_local_linear_gradient(Rect::new(0.0, 0.0, w, h), grad, corners);
    } else if let Some(c) = interior {
        if resolved.blend_mode != 0 {
            painter.push_local_fill_rect_blend(
                Rect::new(0.0, 0.0, w, h),
                c,
                corners,
                resolved.blend_mode,
            );
        } else {
            painter.push_local_fill_rect(Rect::new(0.0, 0.0, w, h), c, corners);
        }
    }
}

fn record_element_text(
    element: &Element,
    text_comp: Option<&crate::ecs::components::TextComponent>,
    style_comp: Option<&crate::ecs::components::StyleComponent>,
    layout_comp: Option<&crate::ecs::components::LayoutComponent>,
    scroll_comp: Option<&crate::ecs::components::ScrollComponent>,
    local_ta: &mut Vec<crate::render::LocalTextArea>,
    _x: f32,
    _y: f32,
    w: f32,
    h: f32,
    scale_factor: f32,
    default_fg: Color,
) {
    let buf = text_comp.and_then(|t| t.text_buffer.clone());
    if let Some(ref buf) = buf {
        let scroll_x = scroll_comp.as_ref().map_or(0.0, |s| s.text_scroll_x.get());
        let scroll_y = scroll_comp.as_ref().map_or(0.0, |s| s.text_scroll_y.get());
        let is_ph = text_comp.is_some_and(|t| t.is_placeholder.get());
        let fg = if is_ph {
            text_comp
                .and_then(|t| t.placeholder_color)
                .unwrap_or(Color::rgba8(150, 150, 165, 255))
        } else {
            let style_default;
            let s = match style_comp {
                Some(s) => s,
                None => {
                    style_default = crate::ecs::components::StyleComponent::default();
                    &style_default
                }
            };
            let resolved = resolve_style(element.state.get(), s);
            resolved.foreground.unwrap_or(default_fg)
        };
        let lazy_fp = text_comp.and_then(|t| t.lazy_font_params.clone());
        let (eff_fs, eff_lh) = if let Some(ref fp) = lazy_fp {
            (fp.font_size, fp.line_height)
        } else {
            (
                text_comp.map_or(18.0, |t| t.font_size),
                text_comp.map_or(1.5, |t| t.line_height),
            )
        };
        let pad = layout_comp.map_or(crate::style::Padding::ZERO, |l| l.padding);
        let vcenter = text_comp.is_none_or(|t| t.text_vertical_center);
        let local_y_offset = if vcenter {
            let content_h = h - pad.top - pad.bottom;
            let text_h = eff_fs * eff_lh;
            pad.top + (content_h - text_h).max(0.0) / 2.0
        } else {
            pad.top
        };
        let tov = style_comp.map_or(crate::style::styled::TextOverflow::Clip, |s| {
            s.text_overflow
        });
        let (cl, ct_, cw, ch) = if tov == crate::style::styled::TextOverflow::Ellipsis {
            (
                pad.left,
                pad.top,
                (w - pad.left - pad.right).max(1.0),
                (h - pad.top - pad.bottom).max(1.0),
            )
        } else {
            (
                pad.left,
                pad.top,
                (w - pad.left - pad.right).max(1.0),
                (h - pad.top - pad.bottom).max(1.0),
            )
        };
        let mut local_left = pad.left;
        {
            let buf_ref = buf.borrow();
            let has_emoji = buf_ref
                .lines
                .iter()
                .any(|l| l.text().chars().any(|c| c as u32 > 0x1F000));
            if has_emoji
                && text_comp.map_or(crate::style::TextAlign::Start, |t| t.text_align)
                    == crate::style::TextAlign::Center
            {
                let content_w = (w - pad.left - pad.right).max(1.0);
                let mut glyph_w: f32 = 0.0;
                for run in buf_ref.layout_runs() {
                    for g in run.glyphs {
                        glyph_w = glyph_w.max(g.x + g.w);
                    }
                }
                if glyph_w > 0.0 && glyph_w < content_w {
                    local_left += (content_w - glyph_w) / 2.0;
                }
            }
        }
        local_ta.push(crate::render::LocalTextArea {
            buffer: buf.clone(),
            generation: text_comp.map_or(0, |t| t.text_generation.get()),
            scale: scale_factor,
            color: fg,
            scroll_x,
            scroll_y,
            local_left,
            local_top: local_y_offset,
            clip_local_x: cl,
            clip_local_y: ct_,
            clip_w: cw,
            clip_h: ch,
        });
    }
}

fn paint_element_decor(
    _element: &Element,
    text_comp: Option<&crate::ecs::components::TextComponent>,
    style_comp: Option<&crate::ecs::components::StyleComponent>,
    layout_comp: Option<&crate::ecs::components::LayoutComponent>,
    scroll_comp: Option<&crate::ecs::components::ScrollComponent>,
    cursor_comp: Option<&crate::ecs::components::CursorComponent>,
    lc_comp: Option<&crate::ecs::components::LifecycleComponent>,
    painter: &mut Painter,
    decor_ta: &mut Vec<crate::render::LocalTextArea>,
    x: f32,
    y: f32,
    w: f32,
    h: f32,
    element_clip: ClipInfo,
    element_xform: Affine2,
    ez: i32,
    accum_clip: ClipInfo,
    opacity: f32,
    _default_fg: Color,
    scale_factor: f32,
) {
    let fade = |c: Color| -> Color {
        if opacity < 1.0 {
            c.with_alpha(c.a * opacity)
        } else {
            c
        }
    };

    if let Some(ref sr) = cursor_comp.map(|c| c.selection_rect.clone()) {
        let rects = sr.replace(Vec::new());
        if !rects.is_empty() {
            let scroll_x = scroll_comp.as_ref().map_or(0.0, |s| s.text_scroll_x.get());
            let scroll_y = scroll_comp.as_ref().map_or(0.0, |s| s.text_scroll_y.get());
            let highlight = text_comp
                .and_then(|t| t.selection_color)
                .unwrap_or(Color::rgba8(59, 130, 246, 128));
            let pad = layout_comp.map_or(crate::style::Padding::ZERO, |l| l.padding);
            let vis = Rect::new(x + pad.left, y, (w - pad.left - pad.right).max(1.0), h);
            let fs = text_comp.map_or(18.0, |t| t.font_size);
            let lh = text_comp.map_or(1.5, |t| t.line_height);
            let vcenter_off = if text_comp.is_none_or(|t| t.text_vertical_center) {
                let content_h = h - pad.top - pad.bottom;
                let text_h = fs * lh;
                (content_h - text_h).max(0.0) / 2.0
            } else {
                0.0
            };
            for sel_rect in rects.iter() {
                let sel = Rect::new(
                    x + sel_rect.x - scroll_x,
                    y + pad.top + vcenter_off + sel_rect.y - scroll_y,
                    sel_rect.width,
                    sel_rect.height,
                );
                let cx = vis.x.max(sel.x);
                let cy = vis.y.max(sel.y);
                let cr = (vis.x + vis.width).min(sel.x + sel.width);
                let cb = (vis.y + vis.height).min(sel.y + sel.height);
                if cr > cx && cb > cy {
                    painter.fill_rect(
                        Rect::new(cx, cy, cr - cx, cb - cy),
                        fade(highlight),
                        element_clip,
                        element_xform,
                        ez,
                    );
                }
            }
        }
        sr.set(rects);
    }

    if let Some(ref cx) = cursor_comp.map(|c| c.cursor_x.clone()) {
        if let Some(ref cv) = cursor_comp.map(|c| c.cursor_visible.clone()) {
            if cv.get() {
                let cx_val = cx.get();
                let scroll_x = scroll_comp.as_ref().map_or(0.0, |s| s.text_scroll_x.get());
                let scroll_y = scroll_comp.as_ref().map_or(0.0, |s| s.text_scroll_y.get());
                let line = cursor_comp.as_ref().map_or(0, |c| c.cursor_line.get());
                let font_size = text_comp.map_or(18.0, |t| t.font_size);
                let line_h = text_comp.map_or(1.5, |t| t.line_height);
                let line_h_val = font_size * line_h;
                let cursor_color = text_comp.and_then(|t| t.caret_color).unwrap_or_else(|| {
                    style_comp
                        .and_then(|s| s.foreground)
                        .unwrap_or(Color::rgba8(0, 0, 0, 255))
                });
                let cursor_w = 2.0;
                let pad = layout_comp.map_or(crate::style::Padding::ZERO, |l| l.padding);
                let vcenter_off2 = if text_comp.is_none_or(|t| t.text_vertical_center) {
                    let content_h = h - pad.top - pad.bottom;
                    let text_h = line_h_val;
                    (content_h - text_h).max(0.0) / 2.0
                } else {
                    0.0
                };
                let cursor_y = y + pad.top + vcenter_off2 + line as f32 * line_h_val
                    - font_size * 0.15
                    - scroll_y;
                let cursor_rect = Rect::new(
                    x + pad.left + cx_val - scroll_x,
                    cursor_y,
                    cursor_w,
                    font_size * 1.8,
                );
                painter.fill_rect(
                    cursor_rect,
                    fade(cursor_color),
                    element_clip,
                    element_xform,
                    ez,
                );
            }
        }
    }

    // IME composition underline — same transform family as the selection
    // rects above (rect carries pad-baked local coords from
    // composition_underline_rect; splice P0: computed-but-never-painted).
    if let Some(ref ul) = cursor_comp.map(|c| c.composition_underline_rect.clone()) {
        if let Some(r) = ul.get() {
            let scroll_x = scroll_comp.as_ref().map_or(0.0, |s| s.text_scroll_x.get());
            let scroll_y = scroll_comp.as_ref().map_or(0.0, |s| s.text_scroll_y.get());
            let pad = layout_comp.map_or(crate::style::Padding::ZERO, |l| l.padding);
            let fs = text_comp.map_or(18.0, |t| t.font_size);
            let lh = text_comp.map_or(1.5, |t| t.line_height);
            let vcenter_off = if text_comp.is_none_or(|t| t.text_vertical_center) {
                let content_h = h - pad.top - pad.bottom;
                ((content_h - fs * lh).max(0.0)) / 2.0
            } else {
                0.0
            };
            let color = text_comp.and_then(|t| t.caret_color).unwrap_or_else(|| {
                style_comp
                    .and_then(|s| s.foreground)
                    .unwrap_or(Color::rgba8(0, 0, 0, 255))
            });
            let ur = Rect::new(
                x + r.x - scroll_x,
                y + pad.top + vcenter_off + r.y - scroll_y,
                r.width,
                r.height,
            );
            let vis = Rect::new(x + pad.left, y, (w - pad.left - pad.right).max(1.0), h);
            let cx = vis.x.max(ur.x);
            let cy = vis.y.max(ur.y);
            let cr = (vis.x + vis.width).min(ur.x + ur.width);
            let cb = (vis.y + vis.height).min(ur.y + ur.height);
            if cr > cx && cb > cy {
                painter.fill_rect(
                    Rect::new(cx, cy, cr - cx, cb - cy),
                    fade(color),
                    element_clip,
                    element_xform,
                    ez,
                );
            }
        }
    }

    let text_dec = style_comp.map_or(crate::style::styled::TextDecoration::None, |s| {
        s.text_decoration
    });
    if text_dec != crate::style::styled::TextDecoration::None
        && text_comp.and_then(|t| t.text_buffer.clone()).is_some()
    {
        let font_size = text_comp.map_or(18.0, |t| t.font_size);
        let scroll_y = scroll_comp.as_ref().map_or(0.0, |s| s.text_scroll_y.get());
        let pad = layout_comp.map_or(crate::style::Padding::ZERO, |l| l.padding);
        let start_y = y + pad.top - scroll_y;
        let dec_color = style_comp
            .and_then(|s| s.foreground)
            .unwrap_or(Color::rgba8(200, 200, 210, 255));
        let dec_y = match text_dec {
            crate::style::styled::TextDecoration::Underline => start_y + font_size * 1.35,
            crate::style::styled::TextDecoration::Strikethrough => start_y - font_size * -0.8,
            crate::style::styled::TextDecoration::Overline => start_y - font_size * 0.8,
            _ => start_y,
        };
        let text_w = (w - pad.left - pad.right).max(10.0);
        painter.fill_rect(
            Rect::new(x + pad.left, dec_y, text_w, 2.0),
            fade(dec_color),
            element_clip,
            element_xform,
            ez,
        );
    }

    if let Some(ref et) = lc_comp.and_then(|l| l.error_text.clone()) {
        let guard = et.borrow();
        if let Some(ref err) = *guard {
            let font_size = text_comp.map_or(18.0, |t| t.font_size);
            let err_fs = font_size * 0.85;
            let err_local_left = w + 8.0;
            let err_local_top = (h.max(1.0) - font_size) * 0.5 - font_size * 0.25;
            let err_color = fade(Color::rgba8(220, 38, 38, 255));
            let buf = std::rc::Rc::new(std::cell::RefCell::new(create_buffer(
                err,
                err_fs,
                1.5,
                400,
                None,
                None,
                crate::style::TextAlign::Start,
            )));
            decor_ta.push(crate::render::LocalTextArea {
                buffer: buf,
                generation: 1,
                scale: scale_factor,
                color: err_color,
                scroll_x: 0.0,
                scroll_y: 0.0,
                local_left: err_local_left,
                local_top: err_local_top,
                clip_local_x: accum_clip.rect.x - x,
                clip_local_y: accum_clip.rect.y - y,
                clip_w: accum_clip.rect.width,
                clip_h: accum_clip.rect.height,
            });
        }
    }

    // Custom paint extension: third-party widgets can set element.paint_fn
    // to inject custom painting without forking paint_element_tree.
    if let Some(ref pf) = _element.paint_fn {
        let mut f = pf.borrow_mut();
        f(painter, Rect::new(x, y, w, h));
    }
}

pub(crate) fn paint_element_tree(
    fcx: &crate::core::frame_context::FrameContext,
    ct: &crate::ecs::tables::ComponentTables,
    arena: &mut ElementArena,
    eid: ElementId,
    painter: &mut Painter,
    text_areas: &mut Vec<crate::render::wgpu::glyphon_bridge::TextAreaDesc>,
    scale_factor: f32,
    scroll_offset: (f32, f32),
    parent_should_paint: bool,
    default_bg: Color,
    default_fg: Color,
    accum_clip: Rect,
    accum_clip_radius: crate::style::CornerRadii,
    accum_xform: Affine2,
    parent_floor: i32,
    must_paint: &std::collections::HashSet<ElementId>,
    accum_opacity: f32,
    highlight_mode: crate::event::FocusHighlightMode,
) {
    let Some(element) = arena.get(eid) else {
        return;
    };

    // Fast visibility check — pre-fetch lc before the full component batch below.
    let lc_comp = ct.lc.get(&eid);

    if element.slot_inactive.get()
        || !lc_comp
            .and_then(|l| l.reactive_visible.as_ref())
            .is_none_or(|v| v.get())
    {
        return;
    }

    let eff_floor = parent_floor.max(element.z_index_floor.unwrap_or(0));
    let mut ez = element.z_index.max(eff_floor);
    let should_paint = element.needs_repaint() || parent_should_paint;

    let sb = element.screen_bounds;
    let mut x = sb.x;
    let mut y = sb.y;
    let mut w = sb.width.max(1.0);
    let mut h = sb.height.max(1.0);

    // Pre-fetch all components once to avoid redundant HashMap lookups
    // throughout the paint path (audit 2026-07-17: ~72 → ~8 per element).
    let xform = ct.xform.get(&eid);
    let style_comp = ct.style.get(&eid);
    let text_comp = ct.text.get(&eid);
    let layout_comp = ct.layout.get(&eid);
    let scroll_comp = ct.scroll.get(&eid);
    let cursor_comp = ct.cursor.get(&eid);

    let element_xform_t = if let Some(ref t) = xform.and_then(|x| x.transform) {
        let tx = glam::Affine2::from_cols_array(t);
        let ox = xform.map_or(0.5, |x| x.transform_origin_x) * w;
        let oy = xform.map_or(0.5, |x| x.transform_origin_y) * h;
        let to_origin = glam::Affine2::from_translation(glam::Vec2::new(-(x + ox), -(y + oy)));
        let from_origin = glam::Affine2::from_translation(glam::Vec2::new(x + ox, y + oy));
        from_origin * tx * to_origin
    } else {
        glam::Affine2::IDENTITY
    };

    // Apply position_offset and size_scale BEFORE the cull so that the
    // raw_clip is computed from the correct visual rect.
    if let Some(ref off) = xform.map(|x| x.position_offset.clone()) {
        let o = off.get();
        x += o.x;
        y += o.y;
    }
    if let Some(ref sc) = xform.map(|x| x.size_scale.clone()) {
        let s = sc.get();
        w *= s.x;
        h *= s.y;
    }

    let element_xform = accum_xform * element_xform_t;

    // ── Early clip cull for leaves (audit 2026-07-17): compute raw_clip
    // before StyleComponent clone + resolve_style + opacity, saving ~40-60%
    // of the per-culled-leaf cost. Backdrop elements are never culled early
    // because their backdrop region must be emitted even when the element
    // rect is fully outside the clip.
    let raw_clip = intersect_rects(
        accum_clip,
        Rect::new(x - scroll_offset.0, y - scroll_offset.1, w, h),
    );
    let is_leaf = element.children.is_empty();
    if is_leaf && (raw_clip.width <= 0.0 || raw_clip.height <= 0.0) {
        // Backdrop-filter leaf whose rect is outside the clip still needs
        // its backdrop region emitted (it affects the visible area behind it).
        // This is extremely rare; pay the HashMap lookup rather than clone.
        if style_comp.and_then(|st| st.backdrop_filter).is_none() {
            if should_paint {
                element.clear_repaint();
            }
            return;
        }
        // Has backdrop_filter → fall through to the full path.
    }

    // ── Style borrow + resolve (only after cull check above; audit
    // 2026-07-17 follow-up: borrow instead of cloning the StyleComponent —
    // gradients/StateStyle carry heap data) ──
    let style_default;
    let s = match style_comp {
        Some(s) => s,
        None => {
            style_default = crate::ecs::components::StyleComponent::default();
            &style_default
        }
    };
    let resolved = resolve_style(element.state.get(), s);
    let opacity = resolved.opacity * accum_opacity;

    // Per-element outline z-offset (base style, matching the pre-audit gate).
    if s.outline_width > 0.0 {
        ez += crate::render::OUTLINE_Z_OFFSET;
    }

    // ── Backdrop-blur region (screen space) ──
    if let Some(bf) = resolved.backdrop_filter {
        painter
            .backdrop_regions
            .push(crate::render::BackdropRegion {
                rect: Rect::new(x, y, w, h),
                transform: element_xform,
                corner_radius: s.corners(),
                blur_radius: bf.blur_radius,
                tint: bf.tint,
                z_index: ez,
            });
    }

    let element_clip = ClipInfo::with_scroll(
        raw_clip,
        accum_clip_radius,
        [scroll_offset.0, scroll_offset.1],
    );

    // Cull non-leaf elements fully outside the viewport after scroll adjustment.
    // Never cull containers with children — their overflowing children may
    // extend into the visible area (e.g. Table body inside a scroll container).
    if !is_leaf && (raw_clip.width <= 0.0 || raw_clip.height <= 0.0) {
        // Container with all children outside clip: still paint it so that
        // its children are reached. The per-child clip will cull individual
        // leaf elements that are truly outside.
    } else if is_leaf && (raw_clip.width <= 0.0 || raw_clip.height <= 0.0) {
        if should_paint {
            element.clear_repaint();
        }
        return;
    }

    // Image / Progress / Slider
    let elem_cr = style_comp.map_or(4.0, |s| s.corner_radius);
    if elem_cr > 0.0 {
        // Override clip radius with element's own corner_radius so the
        // image is clipped to the element's shape (e.g. circular Avatar).
        let image_clip = ClipInfo::with_scroll(
            raw_clip,
            crate::style::CornerRadii::all(elem_cr),
            [scroll_offset.0, scroll_offset.1],
        );
        if let Some(img_data) = element.get_user_data::<crate::widgets::display::ImageData>() {
            let rect = Rect::new(x, y, w, h);
            painter.push_image(
                img_data.hash,
                rect,
                opacity,
                img_data.fit,
                image_clip,
                element_xform,
                ez,
            );
            if should_paint {
                element.clear_repaint();
            }
            return;
        }
    } else if let Some(img_data) = element.get_user_data::<crate::widgets::display::ImageData>() {
        let rect = Rect::new(x, y, w, h);
        painter.push_image(
            img_data.hash,
            rect,
            opacity,
            img_data.fit,
            element_clip,
            element_xform,
            ez,
        );
        if should_paint {
            element.clear_repaint();
        }
        return;
    }
    if let Some(bd) = element.get_user_data::<crate::widgets::display::BarChartData>() {
        crate::widgets::display::bar_chart::paint_bar_chart(
            bd,
            painter,
            x,
            y,
            w,
            h,
            element_clip,
            element_xform,
            ez,
        );
        if should_paint {
            element.clear_repaint();
        }
        return;
    }
    if let Some(ld) = element.get_user_data::<crate::widgets::display::LineChartData>() {
        crate::widgets::display::line_chart::paint_line_chart(
            ld,
            painter,
            x,
            y,
            w,
            h,
            element_clip,
            element_xform,
            ez,
        );
        if should_paint {
            element.clear_repaint();
        }
        return;
    }
    if let Some(pd) = element.get_user_data::<crate::widgets::display::ProgressData>() {
        crate::widgets::display::progress::paint_progress(
            element,
            pd,
            painter,
            x,
            y,
            w,
            h,
            element_clip,
            element_xform,
            ez,
        );
        if should_paint {
            element.clear_repaint();
        }
        return;
    }
    if let Some(sd) = element.get_user_data::<crate::widgets::input::SliderPaintData>() {
        crate::widgets::input::slider::paint_slider(
            sd,
            element,
            painter,
            x,
            y,
            w,
            h,
            element_clip,
            element_xform,
            ez,
        );
        if should_paint {
            element.clear_repaint();
        }
        return;
    }
    if let Some(cpd) = element.get_user_data::<crate::widgets::input::ColorPlanePaintData>() {
        crate::widgets::input::color_picker::paint_color_plane(
            cpd,
            element,
            painter,
            x,
            y,
            w,
            h,
            element_clip,
            element_xform,
            ez,
        );
        if should_paint {
            element.clear_repaint();
        }
        return;
    }
    if let Some(hpd) = element.get_user_data::<crate::widgets::input::HueBarPaintData>() {
        crate::widgets::input::color_picker::paint_hue_bar(
            hpd,
            element,
            painter,
            x,
            y,
            w,
            h,
            element_clip,
            element_xform,
            ez,
        );
        if should_paint {
            element.clear_repaint();
        }
        return;
    }
    if let Some(apd) = element.get_user_data::<crate::widgets::input::AlphaBarPaintData>() {
        crate::widgets::input::color_picker::paint_alpha_bar(
            apd,
            element,
            painter,
            x,
            y,
            w,
            h,
            element_clip,
            element_xform,
            ez,
        );
        if should_paint {
            element.clear_repaint();
        }
        return;
    }

    // ── Path rendering (Icon, etc.) ──
    if let Some(ipd) = element.get_user_data::<crate::widgets::display::IconPathData>() {
        // Checkbox / switch icons may have dynamic visibility + brush colour.
        let brush: crate::style::Brush = if let Some(cis) =
            element.get_user_data::<Rc<crate::widgets::input::CheckboxIconState>>()
        {
            if !cis.visible.get() {
                if should_paint {
                    element.clear_repaint();
                }
                return;
            }
            crate::style::Brush::Solid(cis.brush_color.get())
        } else {
            ipd.brush.clone()
        };

        let scale = (w / 24.0).min(h / 24.0);
        let icon_clip = ClipInfo::with_scroll(
            raw_clip,
            crate::style::CornerRadii::ZERO,
            [scroll_offset.0, scroll_offset.1],
        );
        let path_xform = glam::Affine2::from_translation(glam::Vec2::new(x, y))
            * glam::Affine2::from_scale(glam::Vec2::new(scale, scale));
        let mut stroke = ipd.stroke.clone();
        stroke.width = (stroke.width * scale as f64).max(0.5);
        painter.stroke_path(
            &ipd.path,
            stroke,
            brush,
            icon_clip,
            element_xform * path_xform,
            ez,
        );
        if should_paint {
            element.clear_repaint();
        }
        return;
    }

    // ── Menu item icon (left of label text) ──
    if let Some(mi) = element.get_user_data::<crate::widgets::overlay::MenuItemIcon>() {
        let ix = x + 10.0;
        let iy = y + (h - mi.size) * 0.5;
        let ik = mi.size / 24.0;
        let path_xform = glam::Affine2::from_translation(glam::Vec2::new(ix, iy))
            * glam::Affine2::from_scale(glam::Vec2::new(ik, ik));
        let icon_clip = ClipInfo::with_scroll(
            raw_clip,
            crate::style::CornerRadii::ZERO,
            [scroll_offset.0, scroll_offset.1],
        );
        if mi.filled {
            painter.fill_path(
                &mi.path,
                crate::style::Brush::Solid(mi.color),
                icon_clip,
                element_xform * path_xform,
                ez,
            );
        } else {
            let mut stroke = kurbo::Stroke::new(1.5);
            stroke.join = kurbo::Join::Round;
            stroke.start_cap = kurbo::Cap::Round;
            stroke.end_cap = kurbo::Cap::Round;
            painter.stroke_path(
                &mi.path,
                stroke,
                crate::style::Brush::Solid(mi.color),
                icon_clip,
                element_xform * path_xform,
                ez,
            );
        }
    }

    // ── Submenu indicator (context menu arrow) ──
    if let Some(ind) = element.get_user_data::<crate::widgets::overlay::SubmenuIndicator>() {
        let indicator_size = ind.size;
        let ax = x + w - indicator_size - 8.0;
        let ay = y + (h - indicator_size * 0.8) * 0.5;
        let path_xform = glam::Affine2::from_translation(glam::Vec2::new(ax, ay))
            * glam::Affine2::from_scale(glam::Vec2::new(
                indicator_size / 5.0,
                indicator_size / 8.0,
            ));
        let arrow_clip = ClipInfo::with_scroll(
            raw_clip,
            crate::style::CornerRadii::ZERO,
            [scroll_offset.0, scroll_offset.1],
        );
        let fg = style_comp
            .and_then(|s| s.foreground)
            .unwrap_or(crate::style::Color::rgba8(200, 200, 210, 255));
        let mut stroke = kurbo::Stroke::new(1.5);
        stroke.join = kurbo::Join::Round;
        stroke.start_cap = kurbo::Cap::Round;
        stroke.end_cap = kurbo::Cap::Round;
        painter.stroke_path(
            &ind.path,
            stroke,
            crate::style::Brush::Solid(fg),
            arrow_clip,
            element_xform * path_xform,
            ez,
        );
    }

    // ── Lazy label buffer rebuild: text_generation vs buffer_gen ──
    let lbl = text_comp.and_then(|t| t.lazy_label.clone());
    let buf = text_comp.and_then(|t| t.text_buffer.clone());
    if let (Some(ref lbl), Some(ref buf)) = (&lbl, &buf) {
        let text_gen = text_comp.map_or(0, |t| t.text_generation.get());
        let buf_gen_val = text_comp.map_or(0, |t| t.buffer_gen.get());
        if text_gen != 0 && text_gen != buf_gen_val {
            let text = lbl.take();
            let text_str: &str = &text;
            let lazy_fp = text_comp.and_then(|t| t.lazy_font_params.clone());
            let font_family = text_comp.and_then(|t| t.font_family.clone());
            let (fs, lh, fw, ff, mut pw, ta) = if let Some(ref fp) = lazy_fp {
                (
                    fp.font_size,
                    fp.line_height,
                    fp.font_weight,
                    fp.font_family.as_deref(),
                    fp.max_width,
                    fp.text_align,
                )
            } else {
                (
                    text_comp.map_or(18.0, |t| t.font_size),
                    text_comp.map_or(1.5, |t| t.line_height),
                    text_comp.map_or(400, |t| t.font_weight),
                    font_family.as_deref(),
                    layout_comp.and_then(|l| l.preferred_width),
                    text_comp.map_or(crate::style::TextAlign::Start, |t| t.text_align),
                )
            };
            let tov = style_comp.map_or(crate::style::styled::TextOverflow::Clip, |s| {
                s.text_overflow
            });
            if tov == crate::style::styled::TextOverflow::Clip
                || tov == crate::style::styled::TextOverflow::Ellipsis
            {
                let pad = layout_comp.map_or(crate::style::Padding::ZERO, |l| l.padding);
                let cw = (element.screen_bounds.width - pad.left - pad.right).max(4.0);
                pw = Some(cw);
            }
            // Reuse the existing Buffer allocation and shape once
            // (audit round 3, ③) — replaces the old create_buffer path
            // that allocated a fresh Buffer and shaped twice.
            crate::render::wgpu::glyphon_bridge::reuse_buffer(
                &mut buf.borrow_mut(),
                text_str,
                fs,
                lh,
                fw,
                ff,
                pw,
                ta,
            );
            // Intrinsic width from the just-shaped buffer when it laid out
            // as a single Start/Left-aligned line — skips the SECOND full
            // shaping pass that measure_text_width would run (audit
            // 2026-07-17 round 2). Wrapped/multi-line/aligned text falls
            // back to the fresh unconstrained measure, preserving semantics.
            let single_line_w = if text_str.is_empty() {
                Some(0.0) // measure_text_width's empty-text semantics
            } else if matches!(
                ta,
                crate::style::TextAlign::Start | crate::style::TextAlign::Left
            ) {
                crate::render::wgpu::glyphon_bridge::intrinsic_width_from_buffer(&buf.borrow(), fs)
            } else {
                None
            };
            let measured = single_line_w.unwrap_or_else(|| {
                crate::render::text::measure_text_width(text_str, fs, fw, ff.map(|s| s.to_string()))
            });
            vgen!(
                "[VGEN:PAINT_BUILD] eid={:?} text_generation={} buffer_gen: {} -> {} width={:.1} text=\"{}\"",
                eid, text_gen, buf_gen_val, text_gen, pw.unwrap_or(0.0),
                text_str.chars().take(40).collect::<String>()
            );
            lbl.set(text);
            if let Some(ref mtw) = text_comp.map(|t| t.measured_text_width.clone()) {
                let new_w = measured.max(fs * 2.0);
                if (mtw.get() - new_w).abs() > 0.5 {
                    mtw.set(new_w);
                    element.mark_measure();
                }
            }
            if let Some(ref bg) = text_comp.map(|t| t.buffer_gen.clone()) {
                bg.set(text_gen);
            }
        }
    }

    #[cfg(debug_assertions)]
    {
        let dbg_lbl = text_comp.and_then(|t| t.lazy_label.clone());
        let dbg_buf = text_comp.and_then(|t| t.text_buffer.clone());
        if dbg_lbl.is_some() && dbg_buf.is_none() {
            panic!(
                "Element {:?} has lazy_label but no text_buffer — buffer will never be built",
                eid
            );
        }
    }

    let surf_gen = element.surface_gen.get();
    let text_gen = text_comp.map_or(0, |t| t.text_generation.get());
    let deco_gen = element.decor_gen.get();
    let needs_rp = element.needs_repaint();

    let (cached, surf_ok, text_ok, deco_ok) = {
        let c = fcx.scene_cache;
        let cache = c.borrow();
        if let Some(cs) = cache.get(&eid) {
            let scroll_ok = (cs.scroll_x - scroll_offset.0).abs() < 0.5
                && (cs.scroll_y - scroll_offset.1).abs() < 0.5;
            (
                Some(Rc::clone(cs)),
                cs.surface_gen == surf_gen && scroll_ok,
                cs.text_gen == text_gen && scroll_ok,
                cs.decor_gen == deco_gen && scroll_ok,
            )
        } else {
            (None, false, false, false)
        }
    };

    if !needs_rp && surf_ok && text_ok && deco_ok {
        let cs = cached.as_ref().unwrap();
        painter.replay_local_accum(
            &cs.local_items,
            x,
            y,
            ClipInfo::with_scroll(
                accum_clip,
                accum_clip_radius,
                [scroll_offset.0, scroll_offset.1],
            ),
            element_clip,
            element_xform,
            ez,
            opacity,
        );
        painter.replay(&cs.commands);
        for lta in &cs.local_text_areas {
            text_areas.push(lta.to_world_clipped(
                x,
                y,
                scroll_offset.0,
                scroll_offset.1,
                ez,
                eid,
                Some(element_clip.rect),
            ));
        }
    } else {
        #[cfg(debug_assertions)]
        if needs_rp && surf_ok && text_ok && deco_ok {
            let is_root = arena.root_id == Some(eid);
            if !is_root {
                let ctx = format!(
                    "role={:?} depth={} label={:?} dirty={:?} kids={} in_mustpaint={}",
                    ct.a11y.get(&eid).and_then(|a| a.accessible_role),
                    element.depth,
                    ct.a11y.get(&eid).and_then(|a| a.accessible_label.clone()),
                    element.dirty.get(),
                    element.children.len(),
                    must_paint.contains(&eid),
                );
                crate::debug::check_over_render(eid, surf_gen, deco_gen, &ctx);
            }
        }

        let redo_surface = !surf_ok || cached.is_none() || needs_rp;
        let redo_text = needs_rp || !text_ok || cached.is_none();
        let redo_decor = needs_rp || !deco_ok || cached.is_none();

        let items_start = painter.local_items_len();
        let cmd_start = painter.snapshot_len();

        if redo_surface {
            paint_element_surface(
                element,
                style_comp,
                lc_comp,
                painter,
                x,
                y,
                w,
                h,
                element_clip,
                element_xform,
                ez,
                accum_clip.into(),
                opacity,
                default_bg,
                highlight_mode,
            );
        }
        let surface_items = if redo_surface {
            painter.drain_local_items_from(items_start)
        } else {
            cached.as_ref().unwrap().local_items.clone()
        };
        let surface_cmds: Vec<crate::render::DrawCommand> = if redo_surface {
            painter.drain_from(cmd_start)
        } else {
            let cs = cached.as_ref().unwrap();
            cs.commands[..cs.decor_start].to_vec()
        };

        let mut main_ta: Vec<crate::render::LocalTextArea> = Vec::new();
        if redo_text {
            record_element_text(
                element,
                text_comp,
                style_comp,
                layout_comp,
                scroll_comp,
                &mut main_ta,
                x,
                y,
                w,
                h,
                scale_factor,
                default_fg,
            );
        } else {
            let cs = cached.as_ref().unwrap();
            main_ta = cs.local_text_areas[..cs.decor_text_start].to_vec();
        }

        let deco_cmd_start = painter.snapshot_len();
        let mut decor_ta: Vec<crate::render::LocalTextArea> = Vec::new();
        if redo_decor && should_paint {
            paint_element_decor(
                element,
                text_comp,
                style_comp,
                layout_comp,
                scroll_comp,
                cursor_comp,
                lc_comp,
                painter,
                &mut decor_ta,
                x,
                y,
                w,
                h,
                element_clip,
                element_xform,
                ez,
                accum_clip.into(),
                opacity,
                default_fg,
                scale_factor,
            );
        } else if let Some(cs) = &cached {
            decor_ta = cs.local_text_areas[cs.decor_text_start..].to_vec();
        }
        let decor_cmds: Vec<crate::render::DrawCommand> = if redo_decor {
            painter.drain_from(deco_cmd_start)
        } else {
            let cs = cached.as_ref().unwrap();
            cs.commands[cs.decor_start..].to_vec()
        };

        let mut all_commands = surface_cmds;
        let new_decor_start = all_commands.len();
        all_commands.extend(decor_cmds);

        let mut local_ta = main_ta;
        let decor_text_start = local_ta.len();
        local_ta.extend(decor_ta);

        {
            let c = fcx.scene_cache;
            let cs = Rc::new(crate::render::CachedScene {
                local_items: surface_items,
                commands: all_commands,
                local_text_areas: local_ta,
                surface_gen: surf_gen,
                text_gen: text_gen,
                decor_gen: deco_gen,
                decor_start: new_decor_start,
                decor_text_start,
                scroll_x: scroll_offset.0,
                scroll_y: scroll_offset.1,
            });
            c.borrow_mut().insert(eid, Rc::clone(&cs));

            painter.replay_local_accum(
                &cs.local_items,
                x,
                y,
                ClipInfo::with_scroll(
                    accum_clip,
                    accum_clip_radius,
                    [scroll_offset.0, scroll_offset.1],
                ),
                element_clip,
                element_xform,
                ez,
                opacity,
            );
            painter.replay(&cs.commands);
            for lta in &cs.local_text_areas {
                text_areas.push(lta.to_world_clipped(
                    x,
                    y,
                    scroll_offset.0,
                    scroll_offset.1,
                    ez,
                    eid,
                    Some(element_clip.rect),
                ));
            }
        }
    }

    // ── Children ──
    // Extract all needed data before dropping element for arena recursion
    let is_scrollable = layout_comp.is_some_and(|l| {
        l.overflow == crate::core::config::Overflow::Scroll
            || l.overflow == crate::core::config::Overflow::Clip
    });
    let so_x = scroll_comp
        .as_ref()
        .map_or(0.0, |s| s.scroll_offset.get().x);
    let so_y = scroll_comp
        .as_ref()
        .map_or(0.0, |s| s.scroll_offset.get().y);
    let children_clone = element.children.clone();
    let overflow = layout_comp.map_or(crate::core::config::Overflow::Visible, |l| l.overflow);
    let scrollbar_policy = layout_comp.map_or(crate::core::config::ScrollbarPolicy::Auto, |l| {
        l.scrollbar_policy
    });
    let sb_w = layout_comp.map_or(10.0, |l| l.scrollbar_width);
    let leaf_content_bounds = scroll_comp.map(|sc| sc.content_bounds.get());
    let _ = element;

    if is_scrollable {
        let mut cb_w = w;
        let mut cb_h = h;
        // If the scrollable element stores List item IDs (via ListItemIds
        // user_data), compute content bounds from the actual items rather
        // than from the clip child — this supports clips that have
        // affected_by_child_size(false) for full-width item stretching.
        let list_items = arena
            .get(eid)
            .and_then(|el| el.get_user_data::<crate::widgets::display::list::ListItemIds>());
        let virtual_cb = arena
            .get(eid)
            .and_then(|el| {
                el.get_user_data::<crate::widgets::display::list::VirtualContentBounds>()
            })
            .map(|vcb| vcb.0.get());
        if let Some(vcb) = virtual_cb {
            cb_w = vcb.width.max(cb_w);
            cb_h = vcb.height.max(cb_h);
        } else if let Some(ids) = list_items {
            // screen_bounds from taffy is in document-root space. Convert
            // to container-local by subtracting the container's position,
            // otherwise parent-chain offsets inflate content_bounds.
            for &iid in &ids.0 {
                if let Some(item) = arena.get(iid) {
                    let ir = item.screen_bounds;
                    let local_x = ir.x - x;
                    let local_y = ir.y - y;
                    let right = local_x + ir.width;
                    let bottom = local_y + ir.height;
                    if right > cb_w {
                        cb_w = right;
                    }
                    if bottom > cb_h {
                        cb_h = bottom;
                    }
                }
            }
        } else {
            for &cid in &children_clone {
                if let Some(child) = arena.get(cid) {
                    let cr = child.screen_bounds;
                    let right = cr.x + cr.width;
                    let bottom = cr.y + cr.height;
                    if right > cb_w {
                        cb_w = right;
                    }
                    if bottom > cb_h {
                        cb_h = bottom;
                    }
                }
            }
            cb_w -= x;
            cb_h -= y;
        }
        // Leaf widgets (e.g. multiline TextInput) set content_bounds in local
        // space via ECS — apply after children's coordinate conversion.
        if let Some(b) = leaf_content_bounds {
            cb_w = cb_w.max(b.width);
            cb_h = cb_h.max(b.height);
        }
        let has_v = cb_h > h;
        let has_h = cb_w > w;

        let gutter_w = if has_v { sb_w + 2.0 } else { 0.0 };
        let gutter_h = if has_h { sb_w + 2.0 } else { 0.0 };
        let sx = x - scroll_offset.0;
        let sy = y - scroll_offset.1;
        let content_clip = Rect::new(sx, sy, (w - gutter_w).max(1.0), (h - gutter_h).max(1.0));

        let scroll_xform_t = glam::Affine2::from_translation(glam::Vec2::new(-so_x, -so_y));
        paint_children_sorted(
            fcx,
            ct,
            arena,
            eid,
            painter,
            text_areas,
            scale_factor,
            (scroll_offset.0 + so_x, scroll_offset.1 + so_y),
            should_paint,
            default_bg,
            default_fg,
            content_clip,
            style_comp.map_or(crate::style::CornerRadii::all(4.0), |s| s.corners()),
            element_xform * scroll_xform_t,
            eff_floor,
            must_paint,
            opacity,
            highlight_mode,
        );

        // Scrollbars — use unadjusted x,y because element_xform already
        // includes the accumulated scroll transform from ancestors.
        let Some(element) = arena.get(eid) else {
            return;
        };
        let show_bars = scrollbar_policy != crate::core::config::ScrollbarPolicy::Never;
        if show_bars && has_v {
            let thumb_h = (h / cb_h * h).max(20.0);
            let thumb_y = y + (so_y / (cb_h - h)) * (h - thumb_h);
            let thumb_rect = Rect::new(x + w - sb_w - 2.0, thumb_y, sb_w, thumb_h);
            painter.fill_rect(
                Rect::new(x + w - sb_w - 2.0, y, sb_w, h),
                Color::rgba8(60, 60, 70, 60),
                element_clip,
                element_xform,
                ez,
            );
            painter.fill_rounded_rect(
                thumb_rect,
                Color::rgba8(100, 100, 120, 255),
                crate::style::CornerRadii::all(sb_w * 0.5),
                element_clip,
                element_xform,
                ez,
            );
        }
        if show_bars && has_h {
            let h_gutter = if has_v { sb_w + 2.0 } else { 0.0 };
            let thumb_w = (w / cb_w * w).max(20.0);
            let thumb_x = x + (so_x / (cb_w - w)) * (w - thumb_w - h_gutter);
            let thumb_rect = Rect::new(thumb_x, y + h - sb_w - 2.0, thumb_w, sb_w);
            painter.fill_rect(
                Rect::new(x, y + h - sb_w - 2.0, w - h_gutter, sb_w),
                Color::rgba8(60, 60, 70, 60),
                element_clip,
                element_xform,
                ez,
            );
            painter.fill_rounded_rect(
                thumb_rect,
                Color::rgba8(100, 100, 120, 255),
                crate::style::CornerRadii::all(sb_w * 0.5),
                element_clip,
                element_xform,
                ez,
            );
        }

        if let Some(sc) = scroll_comp {
            let cb_cell = &sc.content_bounds;
            let old = cb_cell.get();
            // VirtualContentBounds is the authoritative source (set by list/table frame_tick);
            // always sync to it so the scrollbar shrinks when data is removed.
            if virtual_cb.is_some() {
                if (cb_h - old.height).abs() > 0.01 || (cb_w - old.width).abs() > 0.01 {
                    cb_cell.set(Rect::new(0.0, 0.0, cb_w, cb_h));
                }
            } else if cb_h > old.height || cb_w > old.width {
                // Non-virtual: only grow — paint-computed values may be
                // unreliable (coordinate-space issues in nested scroll).
                cb_cell.set(Rect::new(
                    0.0,
                    0.0,
                    cb_w.max(old.width),
                    cb_h.max(old.height),
                ));
            }
        }
        let _ = element;
    } else {
        let child_clip = if overflow == crate::core::config::Overflow::Visible {
            accum_clip
        } else {
            element_clip.rect
        };
        let child_clip_radius = if overflow == crate::core::config::Overflow::Visible {
            accum_clip_radius
        } else {
            style_comp.map_or(crate::style::CornerRadii::all(4.0), |s| s.corners())
        };
        paint_children_sorted(
            fcx,
            ct,
            arena,
            eid,
            painter,
            text_areas,
            scale_factor,
            scroll_offset,
            should_paint,
            default_bg,
            default_fg,
            child_clip,
            child_clip_radius,
            element_xform,
            eff_floor,
            must_paint,
            opacity,
            highlight_mode,
        );
    }

    if should_paint {
        if let Some(element) = arena.get(eid) {
            element.clear_repaint();
        }
    }
}