mtk-rs 0.1.0-beta.4

Muse Toolkit
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
use parley::layout::Alignment;
use parley::style::{FontStyle, FontWeight, OverflowWrap};
use std::borrow::Cow;

pub use mtk_layout::{
    AbsoluteBuilder, AlignItems, AlignSelf, Computed, Constraints, Edges, FlexDirection, FlexWrap,
    IntoPositionStrategy, JustifyContent, Overflow, PositionStrategy, Rect, Size, Vector2,
};

use crate::animation::Curve;
use crate::clr;
use crate::colors::Color;
use crate::effects::{BoxShadow, Effects, Filter, Radius};

#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub enum LineHeight {
    Relative(f32),
    #[default]
    Auto,
}

impl LineHeight {
    pub fn resolve(&self) -> f32 {
        match self {
            LineHeight::Relative(f) => *f,
            LineHeight::Auto => 1.2, // I think this was 1.2
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum VerticalAlignment {
    Top,
    Center,
    Bottom,
}

#[derive(Clone, Debug, PartialEq)]
pub struct TextStyle {
    pub font_size: f32,
    pub line_height: LineHeight,
    pub color: Color,
    pub font_family: Cow<'static, str>,
    pub font_weight: FontWeight,
    pub font_style: FontStyle,
    pub alignment: Alignment,
    pub vertical_alignment: VerticalAlignment,
    pub wrap: bool,
    pub overflow_wrap: OverflowWrap,
    pub selection_color: Color,
    pub selection_bg: Color,
    pub caret_color: Color,
    pub strikethrough: bool,
    pub underline: bool,
}

impl Default for TextStyle {
    fn default() -> Self {
        Self {
            font_size: 16.0,
            line_height: LineHeight::Auto,
            color: clr!(black),
            font_family: Cow::Borrowed("system-ui"),
            font_weight: FontWeight::default(),
            font_style: FontStyle::default(),
            alignment: Alignment::Start,
            vertical_alignment: VerticalAlignment::Top,
            wrap: false,
            overflow_wrap: OverflowWrap::default(),
            selection_color: clr!(white),
            selection_bg: clr!(ll_blue),
            caret_color: clr!(black),
            strikethrough: false,
            underline: false,
        }
    }
}

/// Target properties that can be transitioned.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TransitionProperty {
    /// Transitions all changing properties simultaneously.
    All,
    Padding,
    Border,
    Width,
    Height,
    Size,
    Gap,
    BackgroundColor,
    BorderColor,
    CornerRadius,
    Scale,
    Opacity,
    BoxShadow,
    TextColor,
    FontSize,
    Scrollbar,
}

/// Backwards compatibility alias for [`TransitionProperty`].
pub type AnimationTarget = TransitionProperty;

/// Configuration for a smooth property transition.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Transition {
    pub property: TransitionProperty,
    pub duration_ms: f64,
    pub curve: Curve,
}

impl Transition {
    pub fn new(property: TransitionProperty, duration_ms: f64, curve: Curve) -> Self {
        Self {
            property,
            duration_ms,
            curve,
        }
    }
}

impl Effects {
    /// Merges non-default visual effects from `other` into `self`.
    pub fn merge(&mut self, other: &Effects) {
        if other.background_color != Color::transparent {
            self.background_color = other.background_color;
        }
        if other.border.color != Color::transparent {
            self.border.color = other.border.color;
        }
        if other.border.radius != crate::effects::Radius::all(0.0) {
            self.border.radius = other.border.radius;
        }
        if other.box_shadow != BoxShadow::default() {
            self.box_shadow = other.box_shadow;
        }
        if !other.additional_shadows.is_empty() {
            self.additional_shadows = other.additional_shadows.clone();
        }
        if !other.filters.is_empty() {
            self.filters = other.filters.clone();
        }
        if other.explicit_opacity || (other.opacity - 1.0).abs() > 1e-4 {
            self.opacity = other.opacity;
            self.explicit_opacity = true;
        }
        if other.explicit_scale || (other.scale - 1.0).abs() > 1e-4 {
            self.scale = other.scale;
            self.explicit_scale = true;
        }
    }
}

impl TextStyle {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn font_size(mut self, size: f32) -> Self {
        self.font_size = size;
        self
    }

    pub fn size(self, size: f32) -> Self {
        self.font_size(size)
    }

    pub fn line_height(mut self, line_height: LineHeight) -> Self {
        self.line_height = line_height;
        self
    }

    pub fn color(mut self, color: Color) -> Self {
        self.color = color;
        self
    }

    pub fn font_family(mut self, family: impl Into<Cow<'static, str>>) -> Self {
        self.font_family = family.into();
        self
    }

    pub fn family(self, family: impl Into<Cow<'static, str>>) -> Self {
        self.font_family(family)
    }

    pub fn font_weight(mut self, weight: FontWeight) -> Self {
        self.font_weight = weight;
        self
    }

    pub fn weight(self, weight: FontWeight) -> Self {
        self.font_weight(weight)
    }

    pub fn font_style(mut self, style: FontStyle) -> Self {
        self.font_style = style;
        self
    }

    pub fn italic(self) -> Self {
        self.font_style(FontStyle::Italic)
    }

    pub fn alignment(mut self, alignment: Alignment) -> Self {
        self.alignment = alignment;
        self
    }

    pub fn align(self, alignment: Alignment) -> Self {
        self.alignment(alignment)
    }

    pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self {
        self.vertical_alignment = alignment;
        self
    }

    pub fn vertical_align(self, alignment: VerticalAlignment) -> Self {
        self.vertical_alignment(alignment)
    }

    pub fn wrap(mut self, wrap: bool) -> Self {
        self.wrap = wrap;
        self
    }

    pub fn overflow_wrap(mut self, overflow_wrap: OverflowWrap) -> Self {
        self.overflow_wrap = overflow_wrap;
        self
    }

    pub fn selection_color(mut self, color: Color) -> Self {
        self.selection_color = color;
        self
    }

    pub fn selection_bg(mut self, color: Color) -> Self {
        self.selection_bg = color;
        self
    }

    pub fn caret_color(mut self, color: Color) -> Self {
        self.caret_color = color;
        self
    }

    pub fn strikethrough(mut self, strikethrough: bool) -> Self {
        self.strikethrough = strikethrough;
        self
    }

    pub fn underline(mut self, underline: bool) -> Self {
        self.underline = underline;
        self
    }

    /// Merges non-default typography styling from `other` into `self`.
    pub fn merge(&mut self, other: &TextStyle) {
        if (other.font_size - 16.0).abs() > 1e-4 {
            self.font_size = other.font_size;
        }
        if other.line_height != LineHeight::Auto {
            self.line_height = other.line_height;
        }
        if other.color != clr!(black) {
            self.color = other.color;
        }
        if other.font_family != "system-ui" {
            self.font_family = other.font_family.clone();
        }
        if other.font_weight != FontWeight::default() {
            self.font_weight = other.font_weight;
        }
        if other.font_style != FontStyle::default() {
            self.font_style = other.font_style;
        }
        if other.alignment != Alignment::Start {
            self.alignment = other.alignment;
        }
        if other.vertical_alignment != VerticalAlignment::Top {
            self.vertical_alignment = other.vertical_alignment;
        }
        if other.wrap {
            self.wrap = other.wrap;
        }
        if other.strikethrough {
            self.strikethrough = other.strikethrough;
        }
        if other.underline {
            self.underline = other.underline;
        }
        if other.caret_color != clr!(black) {
            self.caret_color = other.caret_color;
        }
    }
}

/// Visibility mode for a container's scrollbar.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum ScrollbarVisibility {
    /// Shown automatically when content overflows the viewport.
    #[default]
    Auto,
    /// Always visible even if content fits without overflowing.
    Always,
    /// Never visible (disables rendering and hit-testing; same as `no_scrollbar`).
    Never,
}

/// Declarative styling parameters for Material-style segmented scrollbars.
#[derive(Clone, Debug, PartialEq)]
pub struct ScrollbarStyle {
    /// Thickness of the scrollbar thumb and track in logical pixels.
    pub width: f32,
    /// Inset margin from the viewport edge in logical pixels.
    pub margin: f32,
    /// Air gap between the thumb and the top/bottom track segments.
    pub gap: f32,
    /// Color of the draggable thumb pill.
    pub thumb_color: Color,
    /// Optional color for the top/bottom track segments (None = no track rendered).
    pub track_color: Option<Color>,
    /// Corner border radii for the thumb and track pieces.
    pub radius: Radius,
    /// Minimum thumb length in logical pixels.
    pub min_thumb_len: f32,
    /// Visibility mode.
    pub visibility: ScrollbarVisibility,
}

impl Default for ScrollbarStyle {
    fn default() -> Self {
        Self {
            width: 4.0,
            margin: 2.0,
            gap: 2.0,
            thumb_color: Color::new(102, 102, 102, 128),
            track_color: None,
            radius: Radius::all(2.0),
            min_thumb_len: 20.0,
            visibility: ScrollbarVisibility::Auto,
        }
    }
}

impl ScrollbarStyle {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn merge(&mut self, other: &ScrollbarStyle) {
        if (other.width - 4.0).abs() > 1e-4 {
            self.width = other.width;
        }
        if (other.margin - 2.0).abs() > 1e-4 {
            self.margin = other.margin;
        }
        if (other.gap - 2.0).abs() > 1e-4 {
            self.gap = other.gap;
        }
        if other.thumb_color != Color::new(102, 102, 102, 128) {
            self.thumb_color = other.thumb_color;
        }
        if other.track_color.is_some() {
            self.track_color = other.track_color;
        }
        if other.radius != Radius::all(2.0) {
            self.radius = other.radius;
        }
        if (other.min_thumb_len - 20.0).abs() > 1e-4 {
            self.min_thumb_len = other.min_thumb_len;
        }
        if other.visibility != ScrollbarVisibility::Auto {
            self.visibility = other.visibility;
        }
    }
}

/// Compact bitmask tracking which properties have been explicitly set on a [`Style`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct StyleFlags(pub u64);

impl StyleFlags {
    pub const EMPTY: Self = Self(0);
    pub const WIDTH: Self = Self(1 << 0);
    pub const HEIGHT: Self = Self(1 << 1);
    pub const MIN_WIDTH: Self = Self(1 << 2);
    pub const MAX_WIDTH: Self = Self(1 << 3);
    pub const MIN_HEIGHT: Self = Self(1 << 4);
    pub const MAX_HEIGHT: Self = Self(1 << 5);
    pub const PADDING: Self = Self(1 << 6);
    pub const BORDER_WIDTH: Self = Self(1 << 7);
    pub const GAP: Self = Self(1 << 8);
    pub const FLEX_GROW: Self = Self(1 << 9);
    pub const FLEX_SHRINK: Self = Self(1 << 10);
    pub const FLEX_BASIS: Self = Self(1 << 11);
    pub const FLEX_DIRECTION: Self = Self(1 << 12);
    pub const FLEX_WRAP: Self = Self(1 << 13);
    pub const JUSTIFY_CONTENT: Self = Self(1 << 14);
    pub const ALIGN_ITEMS: Self = Self(1 << 15);
    pub const ALIGN_SELF: Self = Self(1 << 16);
    pub const ASPECT_RATIO: Self = Self(1 << 17);
    pub const OVERFLOW: Self = Self(1 << 18);
    pub const POSITIONING: Self = Self(1 << 19);
    pub const Z_INDEX: Self = Self(1 << 20);

    pub const BG_COLOR: Self = Self(1 << 21);
    pub const BORDER_COLOR: Self = Self(1 << 22);
    pub const BORDER_RADIUS: Self = Self(1 << 23);
    pub const BOX_SHADOW: Self = Self(1 << 24);
    pub const OPACITY: Self = Self(1 << 25);
    pub const SCALE: Self = Self(1 << 26);
    pub const FILTERS: Self = Self(1 << 27);

    pub const FONT_SIZE: Self = Self(1 << 28);
    pub const FONT_FAMILY: Self = Self(1 << 29);
    pub const FONT_WEIGHT: Self = Self(1 << 30);
    pub const FONT_STYLE: Self = Self(1 << 31);
    pub const TEXT_COLOR: Self = Self(1 << 32);
    pub const TEXT_ALIGN: Self = Self(1 << 33);
    pub const VERTICAL_ALIGN: Self = Self(1 << 34);
    pub const WRAP: Self = Self(1 << 35);
    pub const LINE_HEIGHT: Self = Self(1 << 36);
    pub const UNDERLINE: Self = Self(1 << 37);
    pub const STRIKETHROUGH: Self = Self(1 << 38);
    pub const CARET_COLOR: Self = Self(1 << 39);
    pub const SELECTION_COLOR: Self = Self(1 << 40);
    pub const SELECTION_BG: Self = Self(1 << 41);

    pub const SCROLLBAR: Self = Self(1 << 42);

    #[inline]
    pub const fn is_empty(self) -> bool {
        self.0 == 0
    }

    #[inline]
    pub const fn contains(self, other: Self) -> bool {
        (self.0 & other.0) == other.0
    }

    #[inline]
    pub fn insert(&mut self, other: Self) {
        self.0 |= other.0;
    }

    #[inline]
    pub fn remove(&mut self, other: Self) {
        self.0 &= !other.0;
    }
}

impl std::ops::BitOr for StyleFlags {
    type Output = Self;
    #[inline]
    fn bitor(self, rhs: Self) -> Self {
        Self(self.0 | rhs.0)
    }
}

impl std::ops::BitOrAssign for StyleFlags {
    #[inline]
    fn bitor_assign(&mut self, rhs: Self) {
        self.0 |= rhs.0;
    }
}

impl std::ops::BitAnd for StyleFlags {
    type Output = Self;
    #[inline]
    fn bitand(self, rhs: Self) -> Self {
        Self(self.0 & rhs.0)
    }
}

/// Declarative styling overrides for interactive pseudo-states (hover, active, focus, disabled).
#[derive(Clone, Debug, Default, PartialEq)]
pub struct InteractiveStyles {
    pub hover: Option<Style>,
    pub active: Option<Style>,
    pub focus: Option<Style>,
    pub disabled: Option<Style>,
}

/// Declarative styling container defining layout constraints, visual effects, typography, pseudo-states, and transitions.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Style {
    pub base_constraints: Constraints,
    pub base_effects: Effects,
    pub base_text_style: TextStyle,
    pub flex_direction: Option<FlexDirection>,
    pub scrollbar: Option<Box<ScrollbarStyle>>,
    pub flags: StyleFlags,
    pub interaction: Option<Box<InteractiveStyles>>,
    pub transitions: Vec<Transition>,
}

impl Style {
    pub fn new() -> Self {
        Self::default()
    }

    #[inline]
    pub fn hover(&self) -> Option<&Style> {
        self.interaction.as_ref().and_then(|i| i.hover.as_ref())
    }

    #[inline]
    pub fn active(&self) -> Option<&Style> {
        self.interaction.as_ref().and_then(|i| i.active.as_ref())
    }

    #[inline]
    pub fn focus(&self) -> Option<&Style> {
        self.interaction.as_ref().and_then(|i| i.focus.as_ref())
    }

    #[inline]
    pub fn disabled(&self) -> Option<&Style> {
        self.interaction.as_ref().and_then(|i| i.disabled.as_ref())
    }

    /// Merges `other` into `self`, overriding conflicting properties while preserving non-conflicting base styles.
    pub fn merge(mut self, other: Style) -> Self {
        if other.base_constraints != Constraints::default() {
            self.base_constraints.merge(&other.base_constraints);
        }
        if other.base_effects != Effects::default() {
            self.base_effects.merge(&other.base_effects);
        }
        if other.base_text_style != TextStyle::default() {
            self.base_text_style.merge(&other.base_text_style);
        }

        if let Some(dir) = other.flex_direction {
            self.flex_direction = Some(dir);
            self.base_constraints.flex_direction = dir;
        }

        if !other.flags.is_empty() {
            if other.flags.contains(StyleFlags::WIDTH) {
                self.base_constraints.width = other.base_constraints.width;
            }
            if other.flags.contains(StyleFlags::HEIGHT) {
                self.base_constraints.height = other.base_constraints.height;
            }
            if other.flags.contains(StyleFlags::MIN_WIDTH) {
                self.base_constraints.min_width = other.base_constraints.min_width;
            }
            if other.flags.contains(StyleFlags::MAX_WIDTH) {
                self.base_constraints.max_width = other.base_constraints.max_width;
            }
            if other.flags.contains(StyleFlags::MIN_HEIGHT) {
                self.base_constraints.min_height = other.base_constraints.min_height;
            }
            if other.flags.contains(StyleFlags::MAX_HEIGHT) {
                self.base_constraints.max_height = other.base_constraints.max_height;
            }
            if other.flags.contains(StyleFlags::ASPECT_RATIO) {
                self.base_constraints.aspect_ratio = other.base_constraints.aspect_ratio;
            }
            if other.flags.contains(StyleFlags::PADDING) {
                self.base_constraints.padding = other.base_constraints.padding;
            }
            if other.flags.contains(StyleFlags::BORDER_WIDTH) {
                self.base_constraints.border = other.base_constraints.border;
            }
            if other.flags.contains(StyleFlags::GAP) {
                self.base_constraints.gap = other.base_constraints.gap;
            }
            if other.flags.contains(StyleFlags::FLEX_GROW) {
                self.base_constraints.flex_grow = other.base_constraints.flex_grow;
            }
            if other.flags.contains(StyleFlags::FLEX_SHRINK) {
                self.base_constraints.flex_shrink = other.base_constraints.flex_shrink;
            }
            if other.flags.contains(StyleFlags::FLEX_BASIS) {
                self.base_constraints.flex_basis = other.base_constraints.flex_basis;
            }
            if other.flags.contains(StyleFlags::FLEX_DIRECTION) {
                self.flex_direction = other.flex_direction;
                if let Some(dir) = other.flex_direction {
                    self.base_constraints.flex_direction = dir;
                }
            }
            if other.flags.contains(StyleFlags::FLEX_WRAP) {
                self.base_constraints.flex_wrap = other.base_constraints.flex_wrap;
            }
            if other.flags.contains(StyleFlags::JUSTIFY_CONTENT) {
                self.base_constraints.justify_content = other.base_constraints.justify_content;
            }
            if other.flags.contains(StyleFlags::ALIGN_ITEMS) {
                self.base_constraints.align_items = other.base_constraints.align_items;
            }
            if other.flags.contains(StyleFlags::ALIGN_SELF) {
                self.base_constraints.align_self = other.base_constraints.align_self;
            }
            if other.flags.contains(StyleFlags::OVERFLOW) {
                self.base_constraints.overflow = other.base_constraints.overflow;
            }
            if other.flags.contains(StyleFlags::POSITIONING) {
                self.base_constraints.positioning = other.base_constraints.positioning;
            }
            if other.flags.contains(StyleFlags::Z_INDEX) {
                self.base_constraints.z_index = other.base_constraints.z_index;
            }

            if other.flags.contains(StyleFlags::BG_COLOR) {
                self.base_effects.background_color = other.base_effects.background_color;
            }
            if other.flags.contains(StyleFlags::BORDER_COLOR) {
                self.base_effects.border.color = other.base_effects.border.color;
            }
            if other.flags.contains(StyleFlags::BORDER_RADIUS) {
                self.base_effects.border.radius = other.base_effects.border.radius;
            }
            if other.flags.contains(StyleFlags::BOX_SHADOW) {
                self.base_effects.box_shadow = other.base_effects.box_shadow;
                self.base_effects.additional_shadows = other.base_effects.additional_shadows;
            }
            if other.flags.contains(StyleFlags::OPACITY) {
                self.base_effects.opacity = other.base_effects.opacity;
                self.base_effects.explicit_opacity = true;
            }
            if other.flags.contains(StyleFlags::SCALE) {
                self.base_effects.scale = other.base_effects.scale;
                self.base_effects.explicit_scale = true;
            }
            if other.flags.contains(StyleFlags::FILTERS) {
                self.base_effects.filters = other.base_effects.filters;
            }

            if other.flags.contains(StyleFlags::FONT_SIZE) {
                self.base_text_style.font_size = other.base_text_style.font_size;
            }
            if other.flags.contains(StyleFlags::FONT_FAMILY) {
                self.base_text_style.font_family = other.base_text_style.font_family;
            }
            if other.flags.contains(StyleFlags::FONT_WEIGHT) {
                self.base_text_style.font_weight = other.base_text_style.font_weight;
            }
            if other.flags.contains(StyleFlags::FONT_STYLE) {
                self.base_text_style.font_style = other.base_text_style.font_style;
            }
            if other.flags.contains(StyleFlags::TEXT_COLOR) {
                self.base_text_style.color = other.base_text_style.color;
            }
            if other.flags.contains(StyleFlags::TEXT_ALIGN) {
                self.base_text_style.alignment = other.base_text_style.alignment;
            }
            if other.flags.contains(StyleFlags::VERTICAL_ALIGN) {
                self.base_text_style.vertical_alignment = other.base_text_style.vertical_alignment;
            }
            if other.flags.contains(StyleFlags::WRAP) {
                self.base_text_style.wrap = other.base_text_style.wrap;
            }
            if other.flags.contains(StyleFlags::LINE_HEIGHT) {
                self.base_text_style.line_height = other.base_text_style.line_height;
            }
            if other.flags.contains(StyleFlags::UNDERLINE) {
                self.base_text_style.underline = other.base_text_style.underline;
            }
            if other.flags.contains(StyleFlags::STRIKETHROUGH) {
                self.base_text_style.strikethrough = other.base_text_style.strikethrough;
            }
            if other.flags.contains(StyleFlags::CARET_COLOR) {
                self.base_text_style.caret_color = other.base_text_style.caret_color;
            }
            if other.flags.contains(StyleFlags::SELECTION_COLOR) {
                self.base_text_style.selection_color = other.base_text_style.selection_color;
            }
            if other.flags.contains(StyleFlags::SELECTION_BG) {
                self.base_text_style.selection_bg = other.base_text_style.selection_bg;
            }
            self.flags |= other.flags;
        }

        if let Some(sb) = other.scrollbar {
            self.scrollbar = Some(match self.scrollbar {
                Some(mut existing) => {
                    existing.merge(&sb);
                    existing
                }
                None => sb,
            });
            self.flags.insert(StyleFlags::SCROLLBAR);
        }

        if let Some(other_inter) = other.interaction {
            let mut my_inter = self.interaction.take().unwrap_or_default();
            if let Some(h) = other_inter.hover {
                my_inter.hover = Some(match my_inter.hover {
                    Some(existing) => existing.merge(h),
                    None => h,
                });
            }
            if let Some(a) = other_inter.active {
                my_inter.active = Some(match my_inter.active {
                    Some(existing) => existing.merge(a),
                    None => a,
                });
            }
            if let Some(f) = other_inter.focus {
                my_inter.focus = Some(match my_inter.focus {
                    Some(existing) => existing.merge(f),
                    None => f,
                });
            }
            if let Some(d) = other_inter.disabled {
                my_inter.disabled = Some(match my_inter.disabled {
                    Some(existing) => existing.merge(d),
                    None => d,
                });
            }
            self.interaction = Some(my_inter);
        }

        self.transitions.extend(other.transitions);
        self
    }

    /// Applies a reusable style mixin function `f`.
    pub fn apply(self, f: impl FnOnce(Style) -> Style) -> Self {
        f(self)
    }

    /// Applies this style's base constraints, effects, and text style directly to a layout node.
    pub fn apply_to_node(&self, ctx: &mut crate::Context, node: crate::Node) {
        node.update_constraints(ctx, |c| {
            let overflow = c.overflow;
            let scroll = c.scroll;
            let flex_dir = self.flex_direction.unwrap_or(c.flex_direction);
            *c = self.base_constraints;
            c.flex_direction = flex_dir;
            if self.base_constraints.overflow == Overflow::Visible && overflow != Overflow::Visible
            {
                c.overflow = overflow;
            }
            c.scroll = scroll;
        });

        node.set_effects(ctx, self.base_effects.clone());

        if let Some(text) = node.get_text(ctx) {
            let text_owned = text.to_string();
            if node
                .get_text_userdata::<crate::TextRenderInfo>(ctx)
                .is_none()
                && node.get_text_userdata::<TextStyle>(ctx).is_none()
            {
                node.set_text_with_userdata(ctx, &text_owned, self.base_text_style.clone());
            }
        }
    }

    /// Conditionally applies style modifications when `condition` is `true`.
    pub fn when(self, condition: bool, f: impl FnOnce(Style) -> Style) -> Self {
        if condition { f(self) } else { self }
    }

    pub fn padding(mut self, val: f32) -> Self {
        self.base_constraints.padding = Edges::all(val);
        self.flags.insert(StyleFlags::PADDING);
        self
    }

    /// Sets symmetric horizontal (`x`) and vertical (`y`) padding edges.
    pub fn padding_xy(mut self, x: f32, y: f32) -> Self {
        self.base_constraints.padding = Edges {
            top: y,
            right: x,
            bottom: y,
            left: x,
        };
        self.flags.insert(StyleFlags::PADDING);
        self
    }

    pub fn padding_edges(mut self, edges: Edges) -> Self {
        self.base_constraints.padding = edges;
        self.flags.insert(StyleFlags::PADDING);
        self
    }

    pub fn border(mut self, width: f32, color: Color) -> Self {
        self.base_constraints.border = Edges::all(width);
        self.base_effects.border.color = color;
        self.flags
            .insert(StyleFlags::BORDER_WIDTH | StyleFlags::BORDER_COLOR);
        self
    }

    pub fn border_color(mut self, color: Color) -> Self {
        self.base_effects.border.color = color;
        self.flags.insert(StyleFlags::BORDER_COLOR);
        self
    }

    pub fn flex_wrap(mut self, wrap: FlexWrap) -> Self {
        self.base_constraints.flex_wrap = wrap;
        self.flags.insert(StyleFlags::FLEX_WRAP);
        self
    }

    pub fn wrap(self) -> Self {
        self.flex_wrap(FlexWrap::Wrap)
    }

    pub fn border_edges(mut self, edges: Edges, color: Color) -> Self {
        self.base_constraints.border = edges;
        self.base_effects.border.color = color;
        self.flags
            .insert(StyleFlags::BORDER_WIDTH | StyleFlags::BORDER_COLOR);
        self
    }

    pub fn border_bottom(mut self, width: f32, color: Color) -> Self {
        self.base_constraints.border.bottom = width;
        self.base_effects.border.color = color;
        self.flags
            .insert(StyleFlags::BORDER_WIDTH | StyleFlags::BORDER_COLOR);
        self
    }

    pub fn border_top(mut self, width: f32, color: Color) -> Self {
        self.base_constraints.border.top = width;
        self.base_effects.border.color = color;
        self.flags
            .insert(StyleFlags::BORDER_WIDTH | StyleFlags::BORDER_COLOR);
        self
    }

    pub fn border_left(mut self, width: f32, color: Color) -> Self {
        self.base_constraints.border.left = width;
        self.base_effects.border.color = color;
        self.flags
            .insert(StyleFlags::BORDER_WIDTH | StyleFlags::BORDER_COLOR);
        self
    }

    pub fn border_right(mut self, width: f32, color: Color) -> Self {
        self.base_constraints.border.right = width;
        self.base_effects.border.color = color;
        self.flags
            .insert(StyleFlags::BORDER_WIDTH | StyleFlags::BORDER_COLOR);
        self
    }

    pub fn corner_radius(mut self, radius: f32) -> Self {
        self.base_effects.border.radius = Radius::all(radius);
        self.flags.insert(StyleFlags::BORDER_RADIUS);
        self
    }

    pub fn corner_radius_top(mut self, radius: f32) -> Self {
        self.base_effects.border.radius.tl = radius;
        self.base_effects.border.radius.tr = radius;
        self.flags.insert(StyleFlags::BORDER_RADIUS);
        self
    }

    pub fn corner_radius_bottom(mut self, radius: f32) -> Self {
        self.base_effects.border.radius.bl = radius;
        self.base_effects.border.radius.br = radius;
        self.flags.insert(StyleFlags::BORDER_RADIUS);
        self
    }

    pub fn corner_radius_precise(mut self, radius: Radius) -> Self {
        self.base_effects.border.radius = radius;
        self.flags.insert(StyleFlags::BORDER_RADIUS);
        self
    }

    pub fn box_shadow(mut self, shadow: BoxShadow) -> Self {
        self.base_effects.box_shadow = shadow;
        self.flags.insert(StyleFlags::BOX_SHADOW);
        self
    }

    pub fn add_box_shadow(mut self, shadow: BoxShadow) -> Self {
        self.base_effects.additional_shadows.push(shadow);
        self.flags.insert(StyleFlags::BOX_SHADOW);
        self
    }

    pub fn box_shadows(mut self, shadows: impl IntoIterator<Item = BoxShadow>) -> Self {
        let mut iter = shadows.into_iter();
        if let Some(first) = iter.next() {
            self.base_effects.box_shadow = first;
            self.base_effects.additional_shadows = iter.collect();
        } else {
            self.base_effects.box_shadow = BoxShadow::default();
            self.base_effects.additional_shadows.clear();
        }
        self.flags.insert(StyleFlags::BOX_SHADOW);
        self
    }

    pub fn blur(mut self, vibrancy: f32) -> Self {
        self.base_effects.filters.push(Filter::Blur {
            vibrancy,
            vibrancy_darkness: 0.2,
            passes: 4.0,
        });
        self.flags.insert(StyleFlags::FILTERS);
        self
    }

    pub fn opacity(mut self, opacity: f32) -> Self {
        self.base_effects.opacity = opacity;
        self.base_effects.explicit_opacity = true;
        self.flags.insert(StyleFlags::OPACITY);
        self
    }

    pub fn z_index(mut self, z_index: i32) -> Self {
        self.base_constraints.z_index = z_index;
        self.flags.insert(StyleFlags::Z_INDEX);
        self
    }

    pub fn absolute(mut self, left: f32, top: f32) -> Self {
        self.base_constraints.positioning = PositionStrategy::Absolute {
            left,
            top,
            right: f32::NAN,
            bottom: f32::NAN,
        };
        self.flags.insert(StyleFlags::POSITIONING);
        self
    }

    pub fn position(mut self, positioning: impl IntoPositionStrategy) -> Self {
        self.base_constraints.positioning = positioning.into_strategy();
        self.flags.insert(StyleFlags::POSITIONING);
        self
    }

    pub fn width(mut self, size: Size) -> Self {
        self.base_constraints.width = size;
        self.flags.insert(StyleFlags::WIDTH);
        self
    }

    pub fn min_width(mut self, min_w: f32) -> Self {
        self.base_constraints.min_width = min_w;
        self.flags.insert(StyleFlags::MIN_WIDTH);
        self
    }

    pub fn max_width(mut self, max_w: f32) -> Self {
        self.base_constraints.max_width = max_w;
        self.flags.insert(StyleFlags::MAX_WIDTH);
        self
    }

    pub fn height(mut self, size: Size) -> Self {
        self.base_constraints.height = size;
        self.flags.insert(StyleFlags::HEIGHT);
        self
    }

    pub fn min_height(mut self, min_h: f32) -> Self {
        self.base_constraints.min_height = min_h;
        self.flags.insert(StyleFlags::MIN_HEIGHT);
        self
    }

    pub fn max_height(mut self, max_h: f32) -> Self {
        self.base_constraints.max_height = max_h;
        self.flags.insert(StyleFlags::MAX_HEIGHT);
        self
    }

    pub fn aspect_ratio(mut self, ratio: f32) -> Self {
        self.base_constraints.aspect_ratio = ratio;
        self.flags.insert(StyleFlags::ASPECT_RATIO);
        self
    }

    pub fn justify_content(mut self, j: JustifyContent) -> Self {
        self.base_constraints.justify_content = j;
        self.flags.insert(StyleFlags::JUSTIFY_CONTENT);
        self
    }

    pub fn align_items(mut self, a: AlignItems) -> Self {
        self.base_constraints.align_items = a;
        self.flags.insert(StyleFlags::ALIGN_ITEMS);
        self
    }

    pub fn align_self(mut self, a: AlignSelf) -> Self {
        self.base_constraints.align_self = a;
        self.flags.insert(StyleFlags::ALIGN_SELF);
        self
    }

    pub fn flex_shrink(mut self, shrink: f32) -> Self {
        self.base_constraints.flex_shrink = shrink;
        self.flags.insert(StyleFlags::FLEX_SHRINK);
        self
    }

    pub fn flex_basis(mut self, basis: Size) -> Self {
        self.base_constraints.flex_basis = basis;
        self.flags.insert(StyleFlags::FLEX_BASIS);
        self
    }

    pub fn gap(mut self, val: f32) -> Self {
        self.base_constraints.gap = val;
        self.flags.insert(StyleFlags::GAP);
        self
    }

    pub fn overflow(mut self, overflow: Overflow) -> Self {
        self.base_constraints.overflow = overflow;
        self.flags.insert(StyleFlags::OVERFLOW);
        self
    }

    pub fn bg_color(mut self, color: Color) -> Self {
        self.base_effects.background_color = color;
        self.flags.insert(StyleFlags::BG_COLOR);
        self
    }

    pub fn scale(mut self, s: f32) -> Self {
        self.base_effects.scale = s;
        self.base_effects.explicit_scale = true;
        self.flags.insert(StyleFlags::SCALE);
        self
    }

    pub fn flex_direction(mut self, dir: FlexDirection) -> Self {
        self.base_constraints.flex_direction = dir;
        self.flex_direction = Some(dir);
        self.flags.insert(StyleFlags::FLEX_DIRECTION);
        self
    }

    /// Sets flex grow factor for layout flexing inside flex containers.
    pub fn flex_grow(mut self, val: f32) -> Self {
        self.base_constraints.flex_grow = val;
        self.flags.insert(StyleFlags::FLEX_GROW);
        self
    }

    pub fn set_constraints(mut self, c: Constraints) -> Self {
        self.base_constraints = c;
        self
    }

    pub fn update_constraints(mut self, f: impl FnOnce(&mut Constraints)) -> Self {
        f(&mut self.base_constraints);
        self
    }

    pub fn set_effects(mut self, e: Effects) -> Self {
        self.base_effects = e;
        self
    }

    pub fn update_effects(mut self, f: impl FnOnce(&mut Effects)) -> Self {
        f(&mut self.base_effects);
        self
    }

    pub fn set_text_style(mut self, t: TextStyle) -> Self {
        self.base_text_style = t;
        self
    }

    pub fn update_text_style(mut self, f: impl FnOnce(&mut TextStyle)) -> Self {
        f(&mut self.base_text_style);
        self
    }

    pub fn font_size(mut self, size: f32) -> Self {
        self.base_text_style.font_size = size;
        self.flags.insert(StyleFlags::FONT_SIZE);
        self
    }

    pub fn font_family(mut self, family: impl Into<Cow<'static, str>>) -> Self {
        self.base_text_style.font_family = family.into();
        self.flags.insert(StyleFlags::FONT_FAMILY);
        self
    }

    pub fn text_color(mut self, color: Color) -> Self {
        self.base_text_style.color = color;
        self.flags.insert(StyleFlags::TEXT_COLOR);
        self
    }

    /// Sets whether text should wrap across multiple lines when width is constrained.
    pub fn text_wrap(mut self, wrap: bool) -> Self {
        self.base_text_style.wrap = wrap;
        self.flags.insert(StyleFlags::WRAP);
        self
    }

    /// Disables text wrapping, forcing text to render on a single line.
    pub fn text_nowrap(self) -> Self {
        self.text_wrap(false)
    }

    /// Declares style overrides applied when the mouse cursor hovers over the element.
    pub fn on_hover(mut self, hover_fn: impl FnOnce(Style) -> Style) -> Self {
        let hover_style = hover_fn(Style::new());
        let mut inter = self.interaction.take().map(|b| *b).unwrap_or_default();
        inter.hover = Some(hover_style);
        self.interaction = Some(Box::new(inter));
        self
    }

    /// Declares style overrides applied when the mouse button is pressed over the element (active state).
    pub fn on_active(mut self, active_fn: impl FnOnce(Style) -> Style) -> Self {
        let active_style = active_fn(Style::new());
        let mut inter = self.interaction.take().map(|b| *b).unwrap_or_default();
        inter.active = Some(active_style);
        self.interaction = Some(Box::new(inter));
        self
    }

    /// Declares style overrides applied when the element receives focus.
    pub fn on_focus(mut self, focus_fn: impl FnOnce(Style) -> Style) -> Self {
        let focus_style = focus_fn(Style::new());
        let mut inter = self.interaction.take().map(|b| *b).unwrap_or_default();
        inter.focus = Some(focus_style);
        self.interaction = Some(Box::new(inter));
        self
    }

    /// Declares style overrides applied when the element is disabled.
    pub fn on_disabled(mut self, disabled_fn: impl FnOnce(Style) -> Style) -> Self {
        let disabled_style = disabled_fn(Style::new());
        let mut inter = self.interaction.take().map(|b| *b).unwrap_or_default();
        inter.disabled = Some(disabled_style);
        self.interaction = Some(Box::new(inter));
        self
    }

    /// Smoothly transitions all changing properties over `duration_ms` with easing/spring `curve`.
    pub fn transition_all(mut self, duration_ms: f64, curve: Curve) -> Self {
        self.transitions
            .push(Transition::new(TransitionProperty::All, duration_ms, curve));
        self
    }

    /// Smoothly transitions a specific property over `duration_ms` with easing/spring `curve`.
    pub fn transition(
        mut self,
        property: TransitionProperty,
        duration_ms: f64,
        curve: Curve,
    ) -> Self {
        self.transitions
            .push(Transition::new(property, duration_ms, curve));
        self
    }

    /// Backwards-compatible animation transition builder.
    pub fn animate(mut self, target: TransitionProperty, duration_ms: f64, curve: Curve) -> Self {
        self.transitions
            .push(Transition::new(target, duration_ms, curve));
        self
    }

    pub fn scrollbar(mut self, style: ScrollbarStyle) -> Self {
        if style.visibility == ScrollbarVisibility::Never {
            self.base_constraints.scrollbar_visible = false;
        }
        self.scrollbar = Some(Box::new(style));
        self.flags.insert(StyleFlags::SCROLLBAR);
        self
    }

    pub fn scrollbar_width(mut self, width: f32) -> Self {
        let mut sb = self.scrollbar.take().map(|b| *b).unwrap_or_default();
        sb.width = width;
        self.scrollbar = Some(Box::new(sb));
        self.flags.insert(StyleFlags::SCROLLBAR);
        self
    }

    pub fn scrollbar_margin(mut self, margin: f32) -> Self {
        let mut sb = self.scrollbar.take().map(|b| *b).unwrap_or_default();
        sb.margin = margin;
        self.scrollbar = Some(Box::new(sb));
        self.flags.insert(StyleFlags::SCROLLBAR);
        self
    }

    pub fn scrollbar_gap(mut self, gap: f32) -> Self {
        let mut sb = self.scrollbar.take().map(|b| *b).unwrap_or_default();
        sb.gap = gap;
        self.scrollbar = Some(Box::new(sb));
        self.flags.insert(StyleFlags::SCROLLBAR);
        self
    }

    pub fn scrollbar_thumb(mut self, color: Color) -> Self {
        let mut sb = self.scrollbar.take().map(|b| *b).unwrap_or_default();
        sb.thumb_color = color;
        self.scrollbar = Some(Box::new(sb));
        self.flags.insert(StyleFlags::SCROLLBAR);
        self
    }

    pub fn scrollbar_thumb_color(self, color: Color) -> Self {
        self.scrollbar_thumb(color)
    }

    pub fn scrollbar_track(mut self, color: Color) -> Self {
        let mut sb = self.scrollbar.take().map(|b| *b).unwrap_or_default();
        sb.track_color = Some(color);
        self.scrollbar = Some(Box::new(sb));
        self.flags.insert(StyleFlags::SCROLLBAR);
        self
    }

    pub fn scrollbar_track_color(self, color: Color) -> Self {
        self.scrollbar_track(color)
    }

    pub fn scrollbar_radius(mut self, radius: Radius) -> Self {
        let mut sb = self.scrollbar.take().map(|b| *b).unwrap_or_default();
        sb.radius = radius;
        self.scrollbar = Some(Box::new(sb));
        self.flags.insert(StyleFlags::SCROLLBAR);
        self
    }

    pub fn scrollbar_visibility(mut self, visibility: ScrollbarVisibility) -> Self {
        let mut sb = self.scrollbar.take().map(|b| *b).unwrap_or_default();
        sb.visibility = visibility;
        if visibility == ScrollbarVisibility::Never {
            self.base_constraints.scrollbar_visible = false;
        } else {
            self.base_constraints.scrollbar_visible = true;
        }
        self.scrollbar = Some(Box::new(sb));
        self.flags.insert(StyleFlags::SCROLLBAR);
        self
    }

    pub fn no_scrollbar(mut self) -> Self {
        let mut sb = self.scrollbar.take().map(|b| *b).unwrap_or_default();
        sb.visibility = ScrollbarVisibility::Never;
        self.scrollbar = Some(Box::new(sb));
        self.base_constraints.scrollbar_visible = false;
        self.flags.insert(StyleFlags::SCROLLBAR);
        self
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;

    #[test]
    pub(crate) fn test_row_flex_direction_preserved_when_styled() {
        let style = Style::new();
        assert_eq!(style.base_constraints.flex_direction, FlexDirection::Column);
    }

    #[test]
    fn test_flexbox_new_properties() {
        let style = Style::new()
            .align_self(AlignSelf::End)
            .flex_grow(2.0)
            .flex_shrink(0.5)
            .flex_basis(Size::Fixed(100));

        assert_eq!(style.base_constraints.align_self, AlignSelf::End);
        assert_eq!(style.base_constraints.flex_grow, 2.0);
        assert_eq!(style.base_constraints.flex_shrink, 0.5);
        assert_eq!(style.base_constraints.flex_basis, Size::Fixed(100));
    }

    #[test]
    fn test_root_node_size_fill() {
        let mut ctx = crate::Context::new();
        let root = ctx.create_node();
        root.update_constraints(&mut ctx, |c| {
            c.width = Size::Fill;
            c.height = Size::Fill;
        });
        ctx.root_attach(root);
        ctx.compute_layout(1024.0, 768.0);

        let bounds = root.get_computed(&ctx).unwrap();
        assert_eq!(bounds.w, 1024.0);
        assert_eq!(bounds.h, 768.0);
    }

    #[test]
    fn test_flex_shrink_distribution() {
        let mut ctx = crate::Context::new();
        let root = ctx.create_node();
        root.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(100);
            c.height = Size::Fixed(50);
            c.flex_direction = FlexDirection::Row;
        });

        let child1 = ctx.create_node();
        child1.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(80);
            c.height = Size::Fixed(50);
            c.flex_shrink = 1.0;
        });

        let child2 = ctx.create_node();
        child2.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(40);
            c.height = Size::Fixed(50);
            c.flex_shrink = 1.0;
        });

        root.append(&mut ctx, child1);
        root.append(&mut ctx, child2);
        ctx.root_attach(root);
        ctx.compute_layout(100.0, 50.0);

        let c1_bounds = child1.get_computed(&ctx).unwrap();
        let c2_bounds = child2.get_computed(&ctx).unwrap();

        // 80 / 120 * 20 = 13.333... -> 80 - 13.333 = 66.666...
        // 40 / 120 * 20 = 6.666... -> 40 - 6.666 = 33.333...
        assert!((c1_bounds.w - 66.666).abs() < 0.1);
        assert!((c2_bounds.w - 33.333).abs() < 0.1);
        assert!(((c1_bounds.w + c2_bounds.w) - 100.0).abs() < 0.1);
    }

    #[test]
    fn test_flex_grow_distribution() {
        let mut ctx = crate::Context::new();
        let root = ctx.create_node();
        root.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(200);
            c.height = Size::Fixed(50);
            c.flex_direction = FlexDirection::Row;
        });

        let child1 = ctx.create_node();
        child1.update_constraints(&mut ctx, |c| {
            c.flex_grow = 1.0;
            c.height = Size::Fixed(50);
        });

        let child2 = ctx.create_node();
        child2.update_constraints(&mut ctx, |c| {
            c.flex_grow = 3.0;
            c.height = Size::Fixed(50);
        });

        root.append(&mut ctx, child1);
        root.append(&mut ctx, child2);
        ctx.root_attach(root);
        ctx.compute_layout(200.0, 50.0);

        let c1_bounds = child1.get_computed(&ctx).unwrap();
        let c2_bounds = child2.get_computed(&ctx).unwrap();

        assert_eq!(c1_bounds.w, 50.0);
        assert_eq!(c2_bounds.w, 150.0);
    }

    #[test]
    fn test_flex_wrap_layout() {
        let mut ctx = crate::Context::new();
        let root = ctx.create_node();
        root.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(120);
            c.height = Size::Fit;
            c.flex_direction = FlexDirection::Row;
            c.flex_wrap = FlexWrap::Wrap;
            c.gap = 10.0;
        });

        let child1 = ctx.create_node();
        child1.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(50);
            c.height = Size::Fixed(30);
        });

        let child2 = ctx.create_node();
        child2.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(50);
            c.height = Size::Fixed(30);
        });

        let child3 = ctx.create_node();
        child3.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(50);
            c.height = Size::Fixed(30);
        });

        root.append(&mut ctx, child1);
        root.append(&mut ctx, child2);
        root.append(&mut ctx, child3);
        ctx.root_attach(root);
        ctx.compute_layout(120.0, 100.0);

        let c1_bounds = child1.get_computed(&ctx).unwrap();
        let c2_bounds = child2.get_computed(&ctx).unwrap();
        let c3_bounds = child3.get_computed(&ctx).unwrap();
        let root_bounds = root.get_computed(&ctx).unwrap();

        assert_eq!(c1_bounds.x, 0.0);
        assert_eq!(c1_bounds.y, 0.0);

        assert_eq!(c2_bounds.x, 60.0);
        assert_eq!(c2_bounds.y, 0.0);

        assert_eq!(c3_bounds.x, 0.0);
        assert_eq!(c3_bounds.y, 40.0);

        assert_eq!(root_bounds.h, 70.0);
    }

    #[test]
    fn test_flex_basis_with_grow() {
        let mut ctx = crate::Context::new();
        let root = ctx.create_node();
        root.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(200);
            c.height = Size::Fixed(50);
            c.flex_direction = FlexDirection::Row;
        });

        let child1 = ctx.create_node();
        child1.update_constraints(&mut ctx, |c| {
            c.flex_basis = Size::Fixed(40);
            c.flex_grow = 1.0;
            c.height = Size::Fixed(50);
        });

        let child2 = ctx.create_node();
        child2.update_constraints(&mut ctx, |c| {
            c.flex_basis = Size::Fixed(60);
            c.flex_grow = 1.0;
            c.height = Size::Fixed(50);
        });

        root.append(&mut ctx, child1);
        root.append(&mut ctx, child2);
        ctx.root_attach(root);
        ctx.compute_layout(200.0, 50.0);

        // Total basis = 40 + 60 = 100. Free space = 200 - 100 = 100.
        // Child 1 = 40 + (1/2)*100 = 90.
        // Child 2 = 60 + (1/2)*100 = 110.
        let c1_bounds = child1.get_computed(&ctx).unwrap();
        let c2_bounds = child2.get_computed(&ctx).unwrap();

        assert_eq!(c1_bounds.w, 90.0);
        assert_eq!(c2_bounds.w, 110.0);
    }

    #[test]
    fn test_pre_distribution_cross_axis_stretch() {
        let mut ctx = crate::Context::new();
        let root = ctx.create_node();
        root.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(300);
            c.height = Size::Fixed(400);
            c.flex_direction = FlexDirection::Column;
            c.align_items = AlignItems::Stretch;
        });

        let card = ctx.create_node();
        card.update_constraints(&mut ctx, |c| {
            c.height = Size::Fixed(80);
            c.flex_direction = FlexDirection::Row;
        });

        let card_inner_fill = ctx.create_node();
        card_inner_fill.update_constraints(&mut ctx, |c| {
            c.width = Size::Percent(1.0);
            c.height = Size::Fixed(30);
        });

        card.append(&mut ctx, card_inner_fill);
        root.append(&mut ctx, card);
        ctx.root_attach(root);
        ctx.compute_layout(300.0, 400.0);

        // Card is stretched by root column to 300px width.
        // Inner fill child resolves its 100% width against card's 300px width!
        let card_bounds = card.get_computed(&ctx).unwrap();
        let inner_bounds = card_inner_fill.get_computed(&ctx).unwrap();

        assert_eq!(card_bounds.w, 300.0);
        assert_eq!(inner_bounds.w, 300.0);
    }

    #[test]
    fn test_incremental_dirty_layout_speed() {
        let mut ctx = crate::Context::new();
        let root = ctx.create_node();
        root.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(1000);
            c.height = Size::Fixed(1000);
            c.flex_direction = FlexDirection::Column;
        });

        let mut leaf_nodes = Vec::new();
        for _ in 0..10 {
            let row = ctx.create_node();
            row.update_constraints(&mut ctx, |c| {
                c.width = Size::Percent(1.0);
                c.height = Size::Fixed(50);
                c.flex_direction = FlexDirection::Row;
            });
            for _ in 0..10 {
                let leaf = ctx.create_node();
                leaf.update_constraints(&mut ctx, |c| {
                    c.width = Size::Fixed(50);
                    c.height = Size::Fixed(50);
                });
                row.append(&mut ctx, leaf);
                leaf_nodes.push(leaf);
            }
            root.append(&mut ctx, row);
        }

        ctx.root_attach(root);
        ctx.compute_layout(1000.0, 1000.0);

        // Initial layout complete. Now modify only one single leaf
        let target_leaf = leaf_nodes[42];
        target_leaf.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(80);
        });

        // Incremental re-layout
        ctx.compute_layout(1000.0, 1000.0);

        let target_bounds = target_leaf.get_computed(&ctx).unwrap();
        assert_eq!(target_bounds.w, 80.0);

        // Untouched leaf retains its computed size
        let untouched_leaf = leaf_nodes[0];
        let untouched_bounds = untouched_leaf.get_computed(&ctx).unwrap();
        assert_eq!(untouched_bounds.w, 50.0);
    }

    #[test]
    fn test_aspect_ratio_resolution_modes() {
        let mut ctx = crate::Context::new();

        // 1. Fixed width + Fit height + Aspect Ratio 2.0
        let node1 = ctx.create_node();
        node1.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(100);
            c.height = Size::Fit;
            c.aspect_ratio = 2.0;
        });
        ctx.root_attach(node1);
        ctx.compute_layout(500.0, 500.0);
        let b1 = node1.get_computed(&ctx).unwrap();
        assert_eq!(b1.w, 100.0);
        assert_eq!(b1.h, 50.0);

        // 2. Fixed height + Fit width + Aspect Ratio 2.0
        let node2 = ctx.create_node();
        node2.update_constraints(&mut ctx, |c| {
            c.width = Size::Fit;
            c.height = Size::Fixed(50);
            c.aspect_ratio = 2.0;
        });
        ctx.root_attach(node2);
        ctx.compute_layout(500.0, 500.0);
        let b2 = node2.get_computed(&ctx).unwrap();
        assert_eq!(b2.w, 100.0);
        assert_eq!(b2.h, 50.0);

        // 3. Child with height: Fill + width: Fit in a Row (Cross-axis stretch)
        let row = ctx.create_node();
        row.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(400);
            c.height = Size::Fixed(40);
            c.flex_direction = FlexDirection::Row;
        });
        let child_fill_h = ctx.create_node();
        child_fill_h.update_constraints(&mut ctx, |c| {
            c.height = Size::Fill;
            c.width = Size::Fit;
            c.aspect_ratio = 1.5; // w = h * 1.5 = 40 * 1.5 = 60
        });
        row.append(&mut ctx, child_fill_h);
        ctx.root_attach(row);
        ctx.compute_layout(400.0, 400.0);
        let b3 = child_fill_h.get_computed(&ctx).unwrap();
        assert_eq!(b3.h, 40.0);
        assert_eq!(b3.w, 60.0);

        // 4. Child with width: Fill + height: Fit in a Column
        let col = ctx.create_node();
        col.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(300);
            c.height = Size::Fixed(500);
            c.flex_direction = FlexDirection::Column;
        });
        let child_fill_w = ctx.create_node();
        child_fill_w.update_constraints(&mut ctx, |c| {
            c.width = Size::Fill;
            c.height = Size::Fit;
            c.aspect_ratio = 1.5; // h = w / 1.5 = 300 / 1.5 = 200
        });
        col.append(&mut ctx, child_fill_w);
        ctx.root_attach(col);
        ctx.compute_layout(300.0, 500.0);
        let b4 = child_fill_w.get_computed(&ctx).unwrap();
        assert_eq!(b4.w, 300.0);
        assert_eq!(b4.h, 200.0);
    }

    #[test]
    fn test_scroll_clamping_on_content_shrink() {
        let mut ctx = crate::Context::new();

        let container = ctx.create_node();
        container.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(200);
            c.height = Size::Fixed(200);
            c.overflow = Overflow::Scroll;
            c.flex_direction = FlexDirection::Column;
        });

        let child = ctx.create_node();
        child.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(200);
            c.height = Size::Fixed(600);
        });
        container.append(&mut ctx, child);
        ctx.root_attach(container);

        // Scroll to 300px (max scroll is 400px)
        container.update_constraints(&mut ctx, |c| {
            c.scroll.y = 300.0;
        });

        ctx.compute_layout(400.0, 400.0);
        let cons = container.get_constraints(&ctx).unwrap();
        assert_eq!(cons.scroll.y, 300.0);

        // Shrink child to 100px (now fits in 200px container, max scroll is 0.0)
        child.update_constraints(&mut ctx, |c| {
            c.height = Size::Fixed(100);
        });

        ctx.compute_layout(400.0, 400.0);
        let cons = container.get_constraints(&ctx).unwrap();
        assert_eq!(cons.scroll.y, 0.0);

        let child_comp = child.get_computed(&ctx).unwrap();
        let container_comp = container.get_computed(&ctx).unwrap();
        assert_eq!(child_comp.y, container_comp.y);
    }

    #[test]
    fn test_flex_fill_hover_incremental_layout() {
        let mut ctx = crate::Context::new();

        let root = ctx.create_node();
        root.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(400);
            c.height = Size::Fixed(400);
            c.flex_direction = FlexDirection::Column;
        });

        let row = ctx.create_node();
        row.update_constraints(&mut ctx, |c| {
            c.width = Size::Percent(1.0);
            c.height = Size::Fill;
            c.gap = 10.0;
            c.flex_direction = FlexDirection::Row;
        });
        root.append(&mut ctx, row);

        let mut buttons = Vec::new();
        for _ in 0..4 {
            let btn = ctx.create_node();
            btn.update_constraints(&mut ctx, |c| {
                c.width = Size::Fill;
                c.height = Size::Fill;
            });
            row.append(&mut ctx, btn);
            buttons.push(btn);
        }

        ctx.root_attach(root);
        ctx.compute_layout(400.0, 400.0);

        // Expected button width: (400 - 3 * 10) / 4 = 370 / 4 = 92.5
        for &btn in &buttons {
            let comp = btn.get_computed(&ctx).unwrap();
            assert!((comp.w - 92.5).abs() < 1e-3, "Initial width was {}", comp.w);
        }

        // Simulate hover on button 0 by setting it dirty
        buttons[0].set_dirty(&mut ctx);
        ctx.compute_layout(400.0, 400.0);

        // Verify that incremental layout maintains exact geometries for all buttons
        for &btn in &buttons {
            let comp = btn.get_computed(&ctx).unwrap();
            assert!(
                (comp.w - 92.5).abs() < 1e-3,
                "Button {:?} width corrupted after hover on button 0: {}",
                btn,
                comp.w
            );
        }

        // Simulate hover on button 2
        buttons[2].set_dirty(&mut ctx);
        ctx.compute_layout(400.0, 400.0);

        for &btn in &buttons {
            let comp = btn.get_computed(&ctx).unwrap();
            assert!(
                (comp.w - 92.5).abs() < 1e-3,
                "Button {:?} width corrupted after hover on button 2: {}",
                btn,
                comp.w
            );
        }
    }

    #[test]
    fn test_scroll_offset_percentage_initial_position() {
        let mut ctx = crate::Context::new();

        let root = ctx.create_node();
        root.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(200);
            c.height = Size::Fixed(200);
            c.overflow = Overflow::Scroll;
            c.scroll.y = -1.0001;
        });

        let child = ctx.create_node();
        child.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(200);
            c.height = Size::Fixed(1000);
        });

        let grandchild = ctx.create_node();
        grandchild.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(200);
            c.height = Size::Fixed(50);
        });

        child.append(&mut ctx, grandchild);
        root.append(&mut ctx, child);
        ctx.root_attach(root);

        ctx.compute_layout(200.0, 200.0);

        let root_cons = root.get_constraints(&ctx).unwrap();
        let child_comp = child.get_computed(&ctx).unwrap();
        let grandchild_comp = grandchild.get_computed(&ctx).unwrap();

        assert!(
            (root_cons.scroll.y - 800.0).abs() < 1e-3,
            "scroll.y was {}",
            root_cons.scroll.y
        );
        assert!(
            (child_comp.y - (-800.0)).abs() < 1e-3,
            "child.y was {}",
            child_comp.y
        );
        assert!(
            (grandchild_comp.y - (-800.0)).abs() < 1e-3,
            "grandchild.y was {}",
            grandchild_comp.y
        );
    }

    #[test]
    fn test_scroll_offset_percentage_horizontal_and_partial() {
        let mut ctx = crate::Context::new();

        let root = ctx.create_node();
        root.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(200);
            c.height = Size::Fixed(200);
            c.overflow = Overflow::Scroll;
            c.scroll.y = -0.5001;
            c.scroll.x = -1.0001;
        });

        let child = ctx.create_node();
        child.update_constraints(&mut ctx, |c| {
            c.width = Size::Fixed(600);
            c.height = Size::Fixed(1000);
        });

        root.append(&mut ctx, child);
        ctx.root_attach(root);

        ctx.compute_layout(200.0, 200.0);

        let root_cons = root.get_constraints(&ctx).unwrap();
        let child_comp = child.get_computed(&ctx).unwrap();

        assert!(
            (root_cons.scroll.y - 400.0).abs() < 1e-3,
            "scroll.y was {}",
            root_cons.scroll.y
        );
        assert!(
            (child_comp.y - (-400.0)).abs() < 1e-3,
            "child.y was {}",
            child_comp.y
        );

        assert!(
            (root_cons.scroll.x - 400.0).abs() < 1e-3,
            "scroll.x was {}",
            root_cons.scroll.x
        );
        assert!(
            (child_comp.x - (-400.0)).abs() < 1e-3,
            "child.x was {}",
            child_comp.x
        );
    }

    #[test]
    fn test_style_box_shadows() {
        let s1 = BoxShadow::sm();
        let s2 = BoxShadow::md();
        let style = Style::new().box_shadow(s1).add_box_shadow(s2);

        assert_eq!(style.base_effects.box_shadow, s1);
        assert_eq!(style.base_effects.additional_shadows, vec![s2]);

        let s3 = BoxShadow::lg();
        let style2 = Style::new().box_shadows([s1, s2, s3]);
        assert_eq!(style2.base_effects.box_shadow, s1);
        assert_eq!(style2.base_effects.additional_shadows, vec![s2, s3]);
    }

    #[test]
    fn test_style_flags_and_explicit_sentinel_overrides() {
        // Transparent color override (previously broken by magic sentinel comparisons)
        let base = Style::new().bg_color(Color::new(255, 0, 0, 255));
        let override_style = Style::new().bg_color(Color::transparent);
        let merged = base.merge(override_style);
        assert_eq!(merged.base_effects.background_color, Color::transparent);

        // Explicit font size 16.0 override (previously ignored because 16.0 was default)
        let base = Style::new().font_size(24.0);
        let override_style = Style::new().font_size(16.0);
        let merged = base.merge(override_style);
        assert_eq!(merged.base_text_style.font_size, 16.0);

        // Explicit padding 0.0 override (previously ignored if checking default edges)
        let base = Style::new().padding(20.0);
        let override_style = Style::new().padding(0.0);
        let merged = base.merge(override_style);
        assert_eq!(merged.base_constraints.padding, Edges::all(0.0));
    }

    #[test]
    fn test_style_interaction_consolidation() {
        let style = Style::new()
            .bg_color(Color::new(10, 20, 30, 255))
            .on_hover(|s| s.bg_color(Color::new(40, 50, 60, 255)))
            .on_active(|s| s.scale(0.95))
            .on_focus(|s| s.border(2.0, Color::new(0, 100, 200, 255)))
            .on_disabled(|s| s.opacity(0.5));

        assert!(style.interaction.is_some());
        assert_eq!(
            style.hover().unwrap().base_effects.background_color,
            Color::new(40, 50, 60, 255)
        );
        assert_eq!(style.active().unwrap().base_effects.scale, 0.95);
        assert_eq!(
            style.focus().unwrap().base_constraints.border,
            Edges::all(2.0)
        );
        assert_eq!(style.disabled().unwrap().base_effects.opacity, 0.5);

        // Unconfigured style has None for interaction -> zero heap allocation
        let plain_style = Style::new().padding(10.0);
        assert!(plain_style.interaction.is_none());
        assert!(plain_style.hover().is_none());
    }

    #[test]
    fn test_style_memory_footprint() {
        use std::mem::size_of;
        // ScrollbarStyle is now boxed, saving 56 bytes
        assert_eq!(size_of::<Option<Box<ScrollbarStyle>>>(), 8);
        // Interaction is now consolidated into a single Option<Box>, saving 24 bytes on stack
        assert_eq!(size_of::<Option<Box<InteractiveStyles>>>(), 8);
        // Flags is a compact 8-byte bitmask
        assert_eq!(size_of::<StyleFlags>(), 8);
        println!("Style size: {} bytes", size_of::<Style>());
    }
}