mgui-core 0.2.0

mgui 框架核心:Element/RenderObject/Widget 三树、布局与绘制协议、帧管线与响应式原语
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
//! 阶段 4-A:Theme + Direction — 第一个 InheritedWidget 用例
//!
//! 设计文档 § 6 + § 19.Theme:全局视觉风格沿树向下传递。
//!
//! ```ignore
//! ThemeWidget::new(ThemeData::dark())
//!     .direction(Direction::Ltr)
//!     .child(MyApp())
//! ```
//!
//! 任何后代 widget 在 `state.build(ctx)` 中调 `ctx.depend_on::<ThemeWidget>()` 或
//! 便捷的 `ctx.theme()` / `ctx.direction()` 拿当前主题。

use crate::inherited::InheritedWidget;
use crate::widget::{Widget, WidgetNode};
use crate::{Color, Point, Size};
use std::any::Any;
use std::rc::Rc;

// =================================================================
// ThemeData — 纯数据,无 Widget 概念
// =================================================================

/// Text style defaults (theme-level).
/// Individual TextWidget can override via TextStyle.
#[derive(Debug, Clone, PartialEq)]
pub struct TextTheme {
    pub headline: TextStyleTheme,
    pub title: TextStyleTheme,
    pub body: TextStyleTheme,
    pub caption: TextStyleTheme,
}

#[derive(Debug, Clone, PartialEq)]
pub struct TextStyleTheme {
    pub font_size: f32,
    pub color: Color,
}

/// Button style defaults.
#[derive(Debug, Clone, PartialEq)]
pub struct ButtonStyle {
    pub background: Color,
    pub foreground: Color,
    pub radius: f32,
    /// 全局焦点环颜色(button / checkbox / input / nav 等复用同一令牌)
    pub focus_ring_color: Color,
    /// 全局焦点环宽度
    pub focus_ring_width: f32,
}

/// 按钮语义变体(对应组件侧 `ButtonType`)。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ButtonVariant {
    /// 主操作
    Primary,
    /// 中性 / 次要操作(可有描边)
    Normal,
    /// 三级 / 低强调
    Tertiary,
    /// 成功
    Success,
    /// 警告
    Warning,
    /// 危险
    Error,
    /// 信息
    Info,
}

/// 单个按钮变体的五态配色。
#[derive(Debug, Clone, PartialEq)]
pub struct ButtonVariantStyle {
    pub background: Color,
    pub foreground: Color,
    /// `Color::TRANSPARENT` = 无描边
    pub border: Color,
    pub hover_background: Color,
    pub pressed_background: Color,
}

/// 全部按钮变体。
#[derive(Debug, Clone, PartialEq)]
pub struct ButtonVariants {
    pub primary: ButtonVariantStyle,
    pub normal: ButtonVariantStyle,
    pub tertiary: ButtonVariantStyle,
    pub success: ButtonVariantStyle,
    pub warning: ButtonVariantStyle,
    pub error: ButtonVariantStyle,
    pub info: ButtonVariantStyle,
}

impl ButtonVariants {
    /// 按语义变体取样式。
    pub fn get(&self, variant: ButtonVariant) -> &ButtonVariantStyle {
        match variant {
            ButtonVariant::Primary => &self.primary,
            ButtonVariant::Normal => &self.normal,
            ButtonVariant::Tertiary => &self.tertiary,
            ButtonVariant::Success => &self.success,
            ButtonVariant::Warning => &self.warning,
            ButtonVariant::Error => &self.error,
            ButtonVariant::Info => &self.info,
        }
    }
}

/// Avatar 样式(首字头像)。
#[derive(Debug, Clone, PartialEq)]
pub struct AvatarStyle {
    /// 按名称哈希轮换的底色池
    pub palette: [Color; 6],
    pub foreground: Color,
}

/// 表单控件(Checkbox / Radio / Toggle / Slider)的 Cloudscape 令牌。
#[derive(Debug, Clone, PartialEq)]
pub struct ControlStyle {
    /// 未选中控件底色(color-background-control-default)
    pub background: Color,
    /// 控件描边(color-border-control-default)
    pub border: Color,
    /// 选中底色(color-background-control-checked)
    pub checked_background: Color,
    /// 选中前景(对勾 / 圆点颜色)
    pub checked_foreground: Color,
    /// 禁用底色(color-background-control-disabled)
    pub disabled_background: Color,
    /// 禁用描边
    pub disabled_border: Color,
    /// Toggle 关闭态轨道(color-background-toggle-default)
    pub toggle_off: Color,
    /// Slider 轨道底色(color-background-slider-track-default)
    pub slider_track: Color,
    /// Slider 拖拽中已填充段(color-background-slider-range-active)
    pub slider_range_active: Color,
}

/// 线性混合两种颜色(`t` ∈ [0,1],向 `b` 靠拢)。
fn mix(a: Color, b: Color, t: f32) -> Color {
    Color { r: a.r + (b.r - a.r) * t, g: a.g + (b.g - a.g) * t, b: a.b + (b.b - a.b) * t, a: a.a + (b.a - a.a) * t }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ScrollbarStyle {
    pub thumb_color: Color,
    /// 指针悬停(或拖拽中)在 thumb 上时的高亮色
    pub thumb_hover_color: Color,
    pub thumb_radius: f32,
    pub thumb_width: f32,       // paint width (hit area is +2px)
    pub hit_width: f32,         // hit test area width
    pub thumb_min_size: f32,    // min thumb size in main axis px
    pub thumb_track_ratio: f32, // thumb_main = viewport_main * ratio
}

/// TextField / 输入框样式(Cloudscape:描边输入框 + 焦点蓝边)。
#[derive(Debug, Clone, PartialEq)]
pub struct InputStyle {
    pub background: Color,
    pub border: Color,
    pub focused_border: Color,
    pub text_color: Color,
    pub placeholder_color: Color,
    pub radius: f32,
}

/// Tooltip 浮层样式。
#[derive(Debug, Clone, PartialEq)]
pub struct TooltipStyle {
    pub background: Color,
    pub foreground: Color,
    pub border: Color,
    pub radius: f32,
}

/// 菜单 / 右键 ContextMenu / 下拉列表样式。
#[derive(Debug, Clone, PartialEq)]
pub struct MenuStyle {
    pub background: Color,
    pub foreground: Color,
    pub hover_background: Color,
    pub border: Color,
    pub radius: f32,
}

/// TabBar / TabView / TabbedPane 样式。
#[derive(Debug, Clone, PartialEq)]
pub struct TabStyle {
    pub background: Color,
    pub active_foreground: Color,
    pub inactive_foreground: Color,
    pub hover_background: Color,
    pub indicator_color: Color,
}

/// NavigationMenu 样式。
#[derive(Debug, Clone, PartialEq)]
pub struct NavStyle {
    pub active_background: Color,
    pub active_foreground: Color,
    pub foreground: Color,
    pub hover_background: Color,
    pub indicator_color: Color,
}

/// 反馈类组件样式(进度条 / Toast 等)。
#[derive(Debug, Clone, PartialEq)]
pub struct FeedbackStyle {
    pub progress_track: Color,
    pub progress_value: Color,
    pub success: Color,
    pub warning: Color,
    /// 信息态强调色(Toast Info 等)
    pub info: Color,
    /// 错误态强调色(与全局 `error` 同源,供状态容器统一取用)
    pub error: Color,
}

/// Dropdown 触发器 + 弹出面板样式。
#[derive(Debug, Clone, PartialEq)]
pub struct DropdownStyle {
    pub background: Color,
    pub foreground: Color,
    pub placeholder: Color,
    pub border: Color,
    pub open_border: Color,
    pub hover: Color,
    pub selected_background: Color,
    pub disabled: Color,
}

/// Dialog 遮罩 + 对话框样式。
#[derive(Debug, Clone, PartialEq)]
pub struct DialogStyle {
    pub barrier: Color,
    pub background: Color,
    pub radius: f32,
}

/// ListView / 列表类样式。
#[derive(Debug, Clone, PartialEq)]
pub struct ListStyle {
    pub hover_background: Color,
    pub selected_background: Color,
    pub foreground: Color,
}

/// 4pt 间距刻度 + 组件默认间距(Cloudscape spacing scale)。
#[derive(Debug, Clone, PartialEq)]
pub struct SpacingScale {
    /// 2 — 徽标/gap 微调
    pub xxs: f32,
    /// 4 — 图标与文字
    pub xs: f32,
    /// 8 — 控件内边距 / 紧凑 gap
    pub s: f32,
    /// 16 — 容器内边距 / 常规 gap
    pub m: f32,
    /// 24 — 区块间距
    pub l: f32,
    /// 32 — 页面级间距
    pub xl: f32,
    /// 叶子控件默认内边距
    pub control_padding: f32,
    /// 容器默认内边距
    pub container_padding: f32,
    /// 容器未显式指定 gap 时的默认子项间距
    pub container_gap: f32,
}

impl Default for SpacingScale {
    fn default() -> Self {
        Self {
            xxs: 2.0,
            xs: 4.0,
            s: 8.0,
            m: 16.0,
            l: 24.0,
            xl: 32.0,
            control_padding: 8.0,
            container_padding: 16.0,
            container_gap: 8.0,
        }
    }
}

/// 主题色板 + 子主题。
///
/// 所有 `update_should_notify` 走 `PartialEq` 派生自动比较。
///
/// 风格基准:**AWS Cloudscape Design System(Visual Refresh)**
/// (`cloudscape_dark()` / `cloudscape_light()`,`cloudscape(bool)` 按开关选择;
/// `dark()` / `light()` 为同一套主题的别名)。
///
/// 通用令牌(border / divider / hover)供全部组件共享,做到"一次改主题、全局换肤"。
#[derive(Debug, Clone, PartialEq)]
pub struct ThemeData {
    // --- ColorScheme (core) ---
    pub background: Color,
    pub foreground: Color,
    pub primary: Color,
    pub accent: Color,
    pub surface: Color,
    pub error: Color,
    /// 高对比度变体标记(由 [`ThemeData::high_contrast`] 设置)。
    pub high_contrast: bool,

    // --- 通用令牌(全组件共享) ---
    pub border: Color,    // 组件描边
    pub divider: Color,   // Divider / 分隔线
    pub hover: Color,     // hover 覆盖层(带 alpha)
    pub disabled: Color,  // 禁用态前景(或带 alpha 覆盖)
    pub selection: Color, // 文本/列表选中背景(primary 系 alpha)
    pub icon: Color,      // 图标默认 tint

    // --- Radius ---
    pub radius_sm: f32, // 4.0 — badge / checkbox
    pub radius_md: f32, // 8.0 — input / item
    pub radius_lg: f32, // 16.0 — dialog / menu / large container

    // --- Spacing ---
    pub spacing: f32, // 8.0 — default widget spacing
    /// 4pt 间距刻度与组件默认间距
    pub space: SpacingScale,

    // --- Sub-themes ---
    pub text: TextTheme,
    pub button: ButtonStyle,
    /// 按钮语义变体色板(Primary/Normal/Tertiary/Success/Warning/Error/Info)
    pub button_variants: ButtonVariants,
    /// Avatar 首字头像样式
    pub avatar: AvatarStyle,
    /// 表单控件令牌(Checkbox/Radio/Toggle/Slider)
    pub control: ControlStyle,
    pub scrollbar: ScrollbarStyle,
    pub input: InputStyle,
    pub tooltip: TooltipStyle,
    pub menu: MenuStyle,
    pub tab: TabStyle,
    pub nav: NavStyle,
    pub feedback: FeedbackStyle,
    pub dropdown: DropdownStyle,
    pub dialog: DialogStyle,
    pub list: ListStyle,
}

impl Default for ThemeData {
    fn default() -> Self {
        Self::cloudscape_dark()
    }
}

impl ThemeData {
    /// 深色主题(Cloudscape 别名)。
    pub fn dark() -> Self {
        Self::cloudscape_dark()
    }

    /// 浅色主题(Cloudscape 别名)。
    pub fn light() -> Self {
        Self::cloudscape_light()
    }

    /// AWS Cloudscape Design System(Visual Refresh)— 浅色。
    pub fn cloudscape_light() -> Self {
        Self::cloudscape(false)
    }

    /// AWS Cloudscape Design System(Visual Refresh)— 深色。
    pub fn cloudscape_dark() -> Self {
        Self::cloudscape(true)
    }

    /// 按 `dark` 选择 Cloudscape 主题。
    ///
    /// 色值取自 Cloudscape design-tokens(visual refresh)官方明/暗令牌:
    /// 主操作蓝 `#006CE0` / `#42B4FF`、正文 `#0F141A` / `#C6C6CD`、
    /// 容器 `#FFFFFF` / `#161D26`、分割线 `#C6C6CD` / `#424650`、
    /// 控件圆角 8、容器圆角 16、主按钮胶囊 20。
    pub fn cloudscape(dark: bool) -> Self {
        // ---- Cloudscape visual-refresh 色板 ----
        let (bg, surface, fg, caption, primary, accent, error) = if dark {
            (
                Color::from_rgb8(0x0F, 0x14, 0x1A), // color-background-layout-main
                Color::from_rgb8(0x16, 0x1D, 0x26), // color-background-container-default
                Color::from_rgb8(0xC6, 0xC6, 0xCD), // color-text-body-default
                Color::from_rgb8(0xA4, 0xA4, 0xAD), // color-text-status-inactive
                Color::from_rgb8(0x42, 0xB4, 0xFF), // color-border-item-focused
                Color::from_rgb8(0xFB, 0xD3, 0x32), // color-text-status-warning
                Color::from_rgb8(0xFF, 0x7A, 0x7A), // color-text-status-error
            )
        } else {
            (
                Color::from_rgb8(0xF3, 0xF3, 0xF7), // 画布(容器 #FFFFFF 之上留出层次)
                Color::from_rgb8(0xFF, 0xFF, 0xFF), // color-background-container-default
                Color::from_rgb8(0x0F, 0x14, 0x1A), // color-text-body-default
                Color::from_rgb8(0x42, 0x46, 0x50), // color-text-body-secondary
                Color::from_rgb8(0x00, 0x6C, 0xE0), // color-border-item-focused
                Color::from_rgb8(0x85, 0x59, 0x00), // color-text-status-warning
                Color::from_rgb8(0xDB, 0x00, 0x00), // color-text-status-error
            )
        };
        let (border, divider, input_border, disabled_token) = if dark {
            (
                Color::from_rgb8(0x42, 0x46, 0x50),
                Color::from_rgb8(0x42, 0x46, 0x50), // color-border-divider-default
                Color::from_rgb8(0x65, 0x68, 0x71), // color-border-input-default
                Color::from_rgb8(0x65, 0x68, 0x71), // color-text-input-disabled
            )
        } else {
            (
                Color::from_rgb8(0xC6, 0xC6, 0xCD),
                Color::from_rgb8(0xC6, 0xC6, 0xCD), // color-border-divider-default
                Color::from_rgb8(0x8C, 0x8C, 0x94), // color-border-input-default
                Color::from_rgb8(0xB4, 0xB4, 0xBB), // color-text-input-disabled
            )
        };
        let (hover_bg, hover_overlay, selected_bg, scrollbar_thumb, scrollbar_hover, menu_hover, nav_hover) = if dark {
            (
                Color::from_rgb8(0x1B, 0x23, 0x2D), // 下拉/行 hover
                Color::rgba(1.0, 1.0, 1.0, 0.06),
                Color::from_rgb8(0x00, 0x11, 0x29), // color-background-item-selected
                Color::rgba(0.396, 0.408, 0.443, 0.70), // #656871
                Color::rgba(0.549, 0.549, 0.580, 0.90), // #8C8C94
                Color::from_rgb8(0x1B, 0x23, 0x2D),
                Color::from_rgb8(0x1B, 0x23, 0x2D),
            )
        } else {
            (
                Color::from_rgb8(0xF3, 0xF3, 0xF7), // color-background-dropdown-item-hover
                Color::rgba(0.059, 0.078, 0.102, 0.05),
                Color::from_rgb8(0xF0, 0xFB, 0xFF), // color-background-item-selected
                Color::rgba(0.549, 0.549, 0.565, 0.55), // #8C8C94
                Color::rgba(0.259, 0.275, 0.314, 0.85), // #424650
                Color::from_rgb8(0xF3, 0xF3, 0xF7),
                Color::from_rgb8(0xF3, 0xF3, 0xF7),
            )
        };
        let (placeholder, tooltip_bg, tooltip_fg, tooltip_border) = if dark {
            (
                Color::from_rgb8(0xA4, 0xA4, 0xAD), // color-text-input-placeholder
                Color::from_rgb8(0x33, 0x38, 0x43),
                Color::from_rgb8(0xDE, 0xDE, 0xE3),
                Color::from_rgb8(0x42, 0x46, 0x50),
            )
        } else {
            (
                Color::from_rgb8(0x65, 0x68, 0x71), // color-text-input-placeholder
                Color::from_rgb8(0x0F, 0x14, 0x1A),
                Color::WHITE,
                Color::from_rgb8(0x0F, 0x14, 0x1A),
            )
        };
        let (success, warning, track) = if dark {
            (
                Color::from_rgb8(0x2B, 0xB5, 0x34), // color-text-status-success
                Color::from_rgb8(0xFB, 0xD3, 0x32), // color-text-status-warning
                Color::rgba(1.0, 1.0, 1.0, 0.12),
            )
        } else {
            (
                Color::from_rgb8(0x00, 0x80, 0x2F), // color-text-status-success
                Color::from_rgb8(0x85, 0x59, 0x00), // color-text-status-warning
                Color::rgba(0.0, 0.0, 0.0, 0.08),
            )
        };
        let (button_bg, button_fg) = if dark {
            (primary, Color::from_rgb8(0x0F, 0x14, 0x1A)) // 深色主按钮:浅蓝底 + 深字
        } else {
            (primary, Color::WHITE)
        };

        let text = TextTheme {
            headline: TextStyleTheme { font_size: 24.0, color: fg },
            title: TextStyleTheme { font_size: 18.0, color: fg },
            body: TextStyleTheme { font_size: 14.0, color: fg },
            caption: TextStyleTheme { font_size: 12.0, color: caption },
        };
        let button = ButtonStyle {
            background: button_bg,
            foreground: button_fg,
            radius: 20.0, // border-radius-button
            focus_ring_color: primary,
            focus_ring_width: 2.0,
        };
        // 按钮变体:Primary 蓝底;Normal 蓝描边;Tertiary 低强调;状态变体由状态色推导 hover/pressed
        let status_variant = |base: Color| ButtonVariantStyle {
            background: base,
            foreground: button_fg,
            border: Color::TRANSPARENT,
            hover_background: if dark { mix(base, Color::WHITE, 0.15) } else { mix(base, Color::BLACK, 0.12) },
            pressed_background: if dark { mix(base, Color::WHITE, 0.28) } else { mix(base, Color::BLACK, 0.22) },
        };
        let button_variants = ButtonVariants {
            primary: ButtonVariantStyle {
                background: primary,
                foreground: button_fg,
                border: Color::TRANSPARENT,
                hover_background: if dark {
                    mix(primary, Color::WHITE, 0.20)
                } else {
                    Color::from_rgb8(0x00, 0x2B, 0x66)
                },
                pressed_background: if dark {
                    mix(primary, Color::WHITE, 0.30)
                } else {
                    Color::from_rgb8(0x00, 0x2B, 0x66)
                },
            },
            normal: ButtonVariantStyle {
                background: surface,
                foreground: primary,
                border: primary,
                hover_background: if dark {
                    Color::from_rgb8(0x1B, 0x23, 0x2D)
                } else {
                    Color::from_rgb8(0xF0, 0xFB, 0xFF)
                },
                pressed_background: if dark {
                    Color::from_rgb8(0x33, 0x38, 0x43)
                } else {
                    Color::from_rgb8(0xD1, 0xF1, 0xFF)
                },
            },
            tertiary: ButtonVariantStyle {
                background: Color::TRANSPARENT,
                foreground: fg,
                border: Color::TRANSPARENT,
                hover_background: hover_bg,
                pressed_background: if dark {
                    Color::from_rgb8(0x33, 0x38, 0x43)
                } else {
                    Color::from_rgb8(0xDE, 0xDE, 0xE3)
                },
            },
            success: status_variant(success),
            warning: status_variant(warning),
            error: status_variant(error),
            info: status_variant(primary),
        };
        let avatar = AvatarStyle {
            palette: [
                Color::from_rgb8(0x00, 0x6C, 0xE0),
                Color::from_rgb8(0x00, 0x80, 0x2F),
                Color::from_rgb8(0xC2, 0x5A, 0x00),
                Color::from_rgb8(0x7B, 0x3F, 0xBF),
                Color::from_rgb8(0xDB, 0x00, 0x00),
                Color::from_rgb8(0x00, 0x7E, 0x87),
            ],
            foreground: Color::WHITE,
        };
        // 表单控件令牌(Cloudscape visual refresh)
        let control = if dark {
            ControlStyle {
                background: Color::from_rgb8(0x16, 0x1D, 0x26),
                border: Color::from_rgb8(0x8C, 0x8C, 0x94),
                // Cloudscape 深色 Checkbox/Radio:饱和蓝底 + 白色对勾/圆点
                checked_background: Color::from_rgb8(0x00, 0x6C, 0xE0),
                checked_foreground: Color::WHITE,
                disabled_background: Color::from_rgb8(0x33, 0x38, 0x43),
                disabled_border: Color::from_rgb8(0x65, 0x68, 0x71),
                toggle_off: Color::from_rgb8(0x8C, 0x8C, 0x94),
                slider_track: Color::from_rgb8(0x65, 0x68, 0x71),
                slider_range_active: Color::from_rgb8(0x75, 0xCF, 0xFF),
            }
        } else {
            ControlStyle {
                background: Color::WHITE,
                border: Color::from_rgb8(0x8C, 0x8C, 0x94),
                checked_background: primary,
                checked_foreground: Color::WHITE,
                disabled_background: Color::from_rgb8(0xDE, 0xDE, 0xE3),
                disabled_border: Color::from_rgb8(0xB4, 0xB4, 0xBB),
                toggle_off: Color::from_rgb8(0x42, 0x46, 0x50),
                slider_track: Color::from_rgb8(0x8C, 0x8C, 0x94),
                slider_range_active: Color::from_rgb8(0x00, 0x4A, 0x9E),
            }
        };
        let scrollbar = ScrollbarStyle {
            thumb_color: scrollbar_thumb,
            thumb_hover_color: scrollbar_hover,
            thumb_radius: 4.0,
            thumb_width: 8.0,
            hit_width: 10.0,
            thumb_min_size: 24.0,
            thumb_track_ratio: 0.32,
        };
        let input = InputStyle {
            background: surface,
            border: input_border,
            focused_border: primary,
            text_color: fg,
            placeholder_color: placeholder,
            radius: 8.0, // border-radius-input
        };
        let tooltip =
            TooltipStyle { background: tooltip_bg, foreground: tooltip_fg, border: tooltip_border, radius: 8.0 };
        let menu =
            MenuStyle { background: surface, foreground: fg, hover_background: menu_hover, border, radius: 16.0 };
        let tab = TabStyle {
            background: surface,
            active_foreground: fg,
            inactive_foreground: caption,
            hover_background: hover_bg,
            indicator_color: primary,
        };
        let nav = NavStyle {
            active_background: selected_bg,
            active_foreground: primary,
            foreground: fg,
            hover_background: nav_hover,
            indicator_color: primary,
        };
        let feedback =
            FeedbackStyle { progress_track: track, progress_value: primary, success, warning, info: primary, error };
        let dropdown = DropdownStyle {
            background: surface,
            foreground: fg,
            placeholder,
            border: input_border,
            open_border: primary,
            hover: hover_bg,
            selected_background: selected_bg,
            disabled: disabled_token,
        };
        let dialog = DialogStyle {
            barrier: if dark { Color::rgba(0.0, 0.0, 0.0, 0.60) } else { Color::rgba(0.0, 0.0, 0.0, 0.45) },
            background: surface,
            radius: 16.0, // border-radius-container
        };
        let list = ListStyle { hover_background: hover_bg, selected_background: selected_bg, foreground: fg };
        Self {
            background: bg,
            foreground: fg,
            primary,
            accent,
            surface,
            error,
            high_contrast: false,
            border,
            divider,
            hover: hover_overlay,
            disabled: disabled_token,
            selection: selected_bg,
            icon: fg,
            button_variants,
            avatar,
            control,
            radius_sm: 4.0,  // border-radius-badge
            radius_md: 8.0,  // border-radius-input / item
            radius_lg: 16.0, // border-radius-container
            spacing: 8.0,
            space: SpacingScale::default(),
            text,
            button,
            scrollbar,
            input,
            tooltip,
            menu,
            tab,
            nav,
            feedback,
            dropdown,
            dialog,
            list,
        }
    }

    /// 高对比度主题(Windows 高对比度/强制对比模式的通用替代)。
    ///
    /// 在 Cloudscape 色板骨架上强制:
    /// - 前景/背景 ≥ 7:1,边框/背景 ≥ 3:1,主色/背景 ≥ 4.5:1;
    /// - 关键文本使用纯黑/纯白,边框与分割线使用前景色;
    /// - 圆角归零、焦点环 3px,便于低视力用户辨识。
    ///
    /// `dark=true` 为黑底白字/金黄主色;`false` 为白底黑字/深蓝主色。
    /// 系统高对比度色板由平台提供时,应用仍可自行替换核心令牌。
    pub fn high_contrast(dark: bool) -> Self {
        let mut theme = Self::cloudscape(dark);
        let (bg, fg, dim, primary, on_primary, focus, accent, error, success, warning, hover, selection) = if dark {
            (
                Color::BLACK,
                Color::WHITE,
                Color::from_rgb8(0xB0, 0xB0, 0xB0),
                Color::from_rgb8(0xFF, 0xD7, 0x00),
                Color::BLACK,
                Color::WHITE,
                Color::from_rgb8(0x00, 0xFF, 0xFF),
                Color::from_rgb8(0xFF, 0x80, 0x80),
                Color::from_rgb8(0x7C, 0xFC, 0x7C),
                Color::from_rgb8(0xFF, 0xD7, 0x00),
                Color::rgba(1.0, 1.0, 1.0, 0.20),
                Color::rgba(1.0, 0.843, 0.0, 0.35),
            )
        } else {
            (
                Color::WHITE,
                Color::BLACK,
                Color::from_rgb8(0x50, 0x50, 0x50),
                Color::from_rgb8(0x00, 0x33, 0xCC),
                Color::WHITE,
                Color::BLACK,
                Color::from_rgb8(0x00, 0x5A, 0x5A),
                Color::from_rgb8(0xB0, 0x00, 0x20),
                Color::from_rgb8(0x00, 0x64, 0x00),
                Color::from_rgb8(0x7A, 0x4F, 0x00),
                Color::rgba(0.0, 0.0, 0.0, 0.12),
                Color::rgba(0.0, 0.2, 0.8, 0.18),
            )
        };

        theme.high_contrast = true;
        theme.background = bg;
        theme.surface = bg;
        theme.foreground = fg;
        theme.primary = primary;
        theme.accent = accent;
        theme.error = error;
        theme.border = fg;
        theme.divider = fg;
        theme.hover = hover;
        theme.disabled = dim;
        theme.selection = selection;
        theme.icon = fg;
        theme.radius_sm = 0.0;
        theme.radius_md = 0.0;
        theme.radius_lg = 0.0;

        for style in [&mut theme.text.headline, &mut theme.text.title, &mut theme.text.body] {
            style.color = fg;
        }
        theme.text.caption.color = dim;

        theme.button.background = primary;
        theme.button.foreground = on_primary;
        theme.button.radius = 0.0;
        theme.button.focus_ring_color = focus;
        theme.button.focus_ring_width = 3.0;

        theme.button_variants.primary = ButtonVariantStyle {
            background: primary,
            foreground: on_primary,
            border: Color::TRANSPARENT,
            hover_background: mix(primary, focus, 0.18),
            pressed_background: mix(primary, bg, 0.20),
        };
        theme.button_variants.normal = ButtonVariantStyle {
            background: bg,
            foreground: primary,
            border: primary,
            hover_background: selection,
            pressed_background: hover,
        };
        theme.button_variants.tertiary = ButtonVariantStyle {
            background: Color::TRANSPARENT,
            foreground: fg,
            border: Color::TRANSPARENT,
            hover_background: selection,
            pressed_background: hover,
        };
        let status_variant = |fill: Color| ButtonVariantStyle {
            background: fill,
            foreground: on_primary,
            border: Color::TRANSPARENT,
            hover_background: mix(fill, focus, 0.15),
            pressed_background: mix(fill, bg, 0.15),
        };
        theme.button_variants.success = status_variant(success);
        theme.button_variants.warning = status_variant(warning);
        theme.button_variants.error = status_variant(error);
        theme.button_variants.info = status_variant(accent);

        theme.avatar.palette = [
            Color::from_rgb8(0x00, 0x50, 0x80),
            Color::from_rgb8(0x00, 0x50, 0x00),
            Color::from_rgb8(0x6B, 0x2A, 0x00),
            Color::from_rgb8(0x3B, 0x00, 0x80),
            Color::from_rgb8(0x90, 0x00, 0x00),
            Color::from_rgb8(0x00, 0x4A, 0x4A),
        ];
        theme.avatar.foreground = Color::WHITE;

        theme.control.background = bg;
        theme.control.border = fg;
        theme.control.checked_background = primary;
        theme.control.checked_foreground = on_primary;
        theme.control.disabled_background = bg;
        theme.control.disabled_border = dim;
        theme.control.toggle_off = dim;
        theme.control.slider_track = dim;
        theme.control.slider_range_active = primary;

        theme.scrollbar.thumb_color = dim;
        theme.scrollbar.thumb_hover_color = primary;

        theme.input.background = bg;
        theme.input.border = fg;
        theme.input.focused_border = primary;
        theme.input.text_color = fg;
        theme.input.placeholder_color = dim;
        theme.input.radius = 0.0;

        theme.tooltip.background = bg;
        theme.tooltip.foreground = fg;
        theme.tooltip.border = fg;
        theme.tooltip.radius = 0.0;

        theme.menu.background = bg;
        theme.menu.foreground = fg;
        theme.menu.hover_background = selection;
        theme.menu.border = fg;
        theme.menu.radius = 0.0;

        theme.tab.background = bg;
        theme.tab.active_foreground = fg;
        theme.tab.inactive_foreground = dim;
        theme.tab.hover_background = selection;
        theme.tab.indicator_color = primary;

        theme.nav.active_background = selection;
        theme.nav.active_foreground = fg;
        theme.nav.foreground = fg;
        theme.nav.hover_background = selection;
        theme.nav.indicator_color = primary;

        theme.feedback.progress_track = dim;
        theme.feedback.progress_value = primary;
        theme.feedback.success = success;
        theme.feedback.warning = warning;
        theme.feedback.info = accent;
        theme.feedback.error = error;

        theme.dropdown.background = bg;
        theme.dropdown.foreground = fg;
        theme.dropdown.placeholder = dim;
        theme.dropdown.border = fg;
        theme.dropdown.open_border = primary;
        theme.dropdown.hover = selection;
        theme.dropdown.selected_background = selection;
        theme.dropdown.disabled = dim;

        theme.dialog.barrier = if dark { Color::rgba(0.0, 0.0, 0.0, 0.85) } else { Color::rgba(0.0, 0.0, 0.0, 0.60) };
        theme.dialog.background = bg;
        theme.dialog.radius = 0.0;

        theme.list.hover_background = selection;
        theme.list.selected_background = selection;
        theme.list.foreground = fg;

        theme
    }
}

// =================================================================
// Direction — 文本方向(Ltr / Rtl)
// =================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Direction {
    #[default]
    Ltr,
    Rtl,
}

// =================================================================
// ThemeWidget + InheritedWidget impl
// =================================================================

/// 用户显式颜色优先,否则回退到主题/token 值。
///
/// P1-8:用 `Option` 表达"未覆盖",不再依赖与默认色相等的 sentinel 判断
/// (用户显式选择与哨兵相同的颜色时也能正确覆盖)。
pub const fn resolve_color(explicit: Option<Color>, themed: Color) -> Color {
    match explicit {
        Some(color) => color,
        None => themed,
    }
}

/// 数值版 `resolve_color`:显式尺寸/宽度优先,否则用主题/回退值。
pub const fn resolve_f32(explicit: Option<f32>, themed: f32) -> f32 {
    match explicit {
        Some(value) => value,
        None => themed,
    }
}

/// Theme 提供者 widget。挂到树中,所有后代可通过 `ctx.theme()` 访问。
///
/// `theme` 用 `Rc` 共享:State 缓存主题时只 clone 引用,不再复制整份 ThemeData。
#[derive(Debug)]
pub struct ThemeWidget {
    pub theme: Rc<ThemeData>,
    pub direction: Direction,
    pub child: Option<WidgetNode>,
}

impl ThemeWidget {
    pub fn new(theme: ThemeData) -> Self {
        Self::from_rc(Rc::new(theme))
    }

    pub fn from_rc(theme: Rc<ThemeData>) -> Self {
        Self { theme, direction: Direction::Ltr, child: None }
    }

    pub fn dark() -> Self {
        Self::new(ThemeData::dark())
    }

    pub fn light() -> Self {
        Self::new(ThemeData::light())
    }

    pub fn with_direction(mut self, d: Direction) -> Self {
        self.direction = d;
        self
    }

    pub fn child(mut self, c: impl Widget) -> Self {
        self.child = Some(WidgetNode::new(c));
        self
    }

    pub fn child_node(mut self, c: WidgetNode) -> Self {
        self.child = Some(c);
        self
    }
}

impl InheritedWidget for ThemeWidget {
    fn update_should_notify(&self, old: &dyn InheritedWidget) -> bool {
        // downcast old 到 ThemeWidget 比较 theme + direction
        if let Some(old_theme) = old.as_any().downcast_ref::<ThemeWidget>() {
            if Rc::ptr_eq(&self.theme, &old_theme.theme) {
                return self.direction != old_theme.direction;
            }
            *self.theme != *old_theme.theme || self.direction != old_theme.direction
        } else {
            true
        }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl ThemeWidget {
    pub fn theme(&self) -> &ThemeData {
        &self.theme
    }

    pub fn direction(&self) -> Direction {
        self.direction
    }
}

impl Widget for ThemeWidget {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_inherited(&self) -> Option<&dyn crate::inherited::InheritedWidget> {
        Some(self)
    }

    fn create_render_object(&self) -> Box<dyn crate::render_object::RenderObject> {
        // 透传 child RO,但必须**包一层 ProxyRenderObject**:
        // 直接把 child widget 的 RO 树当自己的 RO,会让 element 树(1 个 child)
        // 与 RO 树(child 的整棵子树)不同构;propagate 只能按索引替换前 N 个 slot,
        // 其余 slot 成为陈旧副本(Theme 根节点会重复绘制 nested boundary)。
        match &self.child {
            Some(c) => Box::new(crate::render_object::ProxyRenderObject::new(c.0.create_render_object())),
            None => Box::new(crate::render_object::RRectRenderObject::new(Size::ZERO, Color::TRANSPARENT, 0.0)),
        }
    }

    fn children(&self) -> Vec<WidgetNode> {
        self.child.clone().into_iter().collect()
    }

    fn children_with_positions(&self) -> Vec<(Point, WidgetNode)> {
        self.child.clone().into_iter().map(|c| (Point::ZERO, c)).collect()
    }
}

// =================================================================
// DirectionWidget — 单独暴露 direction(如果只想用 Direction 不想用 Theme)
// =================================================================

/// 单独的 Direction 提供者,方便只想换方向不换主题的场景。
#[derive(Debug)]
pub struct DirectionWidget {
    pub direction: Direction,
    pub child: Option<WidgetNode>,
}

impl DirectionWidget {
    pub fn new(d: Direction) -> Self {
        Self { direction: d, child: None }
    }

    pub fn child(mut self, c: impl Widget) -> Self {
        self.child = Some(WidgetNode::new(c));
        self
    }
}

impl InheritedWidget for DirectionWidget {
    fn update_should_notify(&self, old: &dyn InheritedWidget) -> bool {
        if let Some(old_dir) = old.as_any().downcast_ref::<DirectionWidget>() {
            self.direction != old_dir.direction
        } else {
            true
        }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl DirectionWidget {
    pub fn direction(&self) -> Direction {
        self.direction
    }
}

impl Widget for DirectionWidget {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_inherited(&self) -> Option<&dyn crate::inherited::InheritedWidget> {
        Some(self)
    }

    fn create_render_object(&self) -> Box<dyn crate::render_object::RenderObject> {
        // 与 ThemeWidget 相同:透传 child 必须包 Proxy,保证 element/RO 同构。
        match &self.child {
            Some(c) => Box::new(crate::render_object::ProxyRenderObject::new(c.0.create_render_object())),
            None => Box::new(crate::render_object::RRectRenderObject::new(Size::ZERO, Color::TRANSPARENT, 0.0)),
        }
    }

    fn children(&self) -> Vec<WidgetNode> {
        self.child.clone().into_iter().collect()
    }

    fn children_with_positions(&self) -> Vec<(Point, WidgetNode)> {
        self.child.clone().into_iter().map(|c| (Point::ZERO, c)).collect()
    }
}

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

    #[test]
    fn cloudscape_palette_matches_design_tokens() {
        let light = ThemeData::cloudscape_light();
        assert_eq!(light.primary, Color::from_rgb8(0x00, 0x6C, 0xE0));
        assert_eq!(light.background, Color::from_rgb8(0xF3, 0xF3, 0xF7));
        assert_eq!(light.surface, Color::WHITE);
        assert_eq!(light.foreground, Color::from_rgb8(0x0F, 0x14, 0x1A));
        assert_eq!(light.button.foreground, Color::WHITE);
        assert_eq!(light.button.radius, 20.0, "Cloudscape 主按钮为胶囊形");
        assert_eq!(light.input.radius, 8.0);
        assert_eq!(light.dialog.radius, 16.0);

        let dark = ThemeData::cloudscape_dark();
        assert_eq!(dark.primary, Color::from_rgb8(0x42, 0xB4, 0xFF));
        assert_eq!(dark.background, Color::from_rgb8(0x0F, 0x14, 0x1A));
        assert_eq!(dark.surface, Color::from_rgb8(0x16, 0x1D, 0x26));
        assert_eq!(dark.foreground, Color::from_rgb8(0xC6, 0xC6, 0xCD));
        // 深色主题主按钮:浅蓝底 + 深色文字(Cloudscape 约定)
        assert_eq!(dark.button.foreground, dark.background);
    }

    #[test]
    fn cloudscape_selector_and_alias_consistency() {
        assert_eq!(ThemeData::cloudscape(false), ThemeData::cloudscape_light());
        assert_eq!(ThemeData::cloudscape(true), ThemeData::cloudscape_dark());
        assert_ne!(ThemeData::cloudscape_light(), ThemeData::cloudscape_dark());
    }

    #[test]
    fn high_contrast_palette_meets_wcag_and_uses_square_corners() {
        fn channel(value: f32) -> f32 {
            if value <= 0.04045 {
                value / 12.92
            } else {
                ((value + 0.055) / 1.055).powf(2.4)
            }
        }
        fn luminance(color: Color) -> f32 {
            0.2126 * channel(color.r) + 0.7152 * channel(color.g) + 0.0722 * channel(color.b)
        }
        fn contrast(a: Color, b: Color) -> f32 {
            let (la, lb) = (luminance(a), luminance(b));
            (la.max(lb) + 0.05) / (la.min(lb) + 0.05)
        }

        for dark in [false, true] {
            let theme = ThemeData::high_contrast(dark);
            assert!(theme.high_contrast);
            assert_eq!(theme.background, if dark { Color::BLACK } else { Color::WHITE });
            assert!(contrast(theme.foreground, theme.background) >= 7.0, "正文对比度不足");
            assert!(contrast(theme.primary, theme.background) >= 4.5, "主色对比度不足");
            assert!(contrast(theme.border, theme.background) >= 3.0, "边框对比度不足");
            assert!(contrast(theme.text.caption.color, theme.background) >= 4.5, "次要文字对比度不足");
            assert!(contrast(theme.button.foreground, theme.button.background) >= 4.5, "主按钮文字对比度不足");
            assert!(
                contrast(theme.button_variants.normal.foreground, theme.button_variants.normal.background) >= 4.5,
                "常规按钮文字对比度不足"
            );
            assert!(contrast(theme.input.text_color, theme.input.background) >= 7.0, "输入框文字对比度不足");
            assert!(contrast(theme.tooltip.foreground, theme.tooltip.background) >= 7.0, "Tooltip 文字对比度不足");
            assert_eq!(theme.radius_md, 0.0);
            assert_eq!(theme.button.radius, 0.0);
            assert_eq!(theme.dialog.radius, 0.0);
            assert!(theme.button.focus_ring_width >= 3.0);
        }
    }

    #[test]
    fn cloudscape_focus_and_emphasis_follow_primary() {
        for t in [ThemeData::cloudscape_light(), ThemeData::cloudscape_dark()] {
            assert_eq!(t.button.focus_ring_color, t.primary);
            assert_eq!(t.text.body.color, t.foreground);
            assert_eq!(t.tab.indicator_color, t.primary);
            assert_eq!(t.nav.indicator_color, t.primary);
            assert_eq!(t.feedback.progress_value, t.primary);
            assert_eq!(t.dropdown.open_border, t.primary);
            assert_eq!(t.input.focused_border, t.primary);
        }
    }

    #[test]
    fn cloudscape_light_and_dark_have_opposite_luminance() {
        let light = ThemeData::cloudscape_light();
        let dark = ThemeData::cloudscape_dark();
        let luma = |c: Color| c.r * 0.2126 + c.g * 0.7152 + c.b * 0.0722;
        assert!(luma(light.background) > 0.8, "浅色画布应接近白色");
        assert!(luma(dark.background) < 0.15, "深色画布应接近黑色");
        assert!(luma(light.foreground) < 0.2, "浅色主题正文应为深色");
        assert!(luma(dark.foreground) > 0.6, "深色主题正文应为浅色");
    }

    #[test]
    fn cloudscape_button_variants_and_avatar_are_themed() {
        let light = ThemeData::cloudscape_light();
        assert_eq!(light.button_variants.primary.background, light.primary);
        assert_eq!(light.button_variants.primary.foreground, Color::WHITE);
        assert_eq!(light.button_variants.normal.border, light.primary);
        assert_eq!(light.button_variants.normal.background, light.surface);
        assert_eq!(light.button_variants.tertiary.background, Color::TRANSPARENT);

        let dark = ThemeData::cloudscape_dark();
        // 深色主题主按钮:浅蓝底 + 深字
        assert_eq!(dark.button_variants.primary.foreground, dark.background);
        assert_eq!(dark.button_variants.normal.border, dark.primary);
        assert_eq!(dark.avatar.foreground, Color::WHITE);
        assert_ne!(dark.avatar.palette[0], dark.avatar.palette[1]);

        // 状态变体严格来自反馈状态色
        assert_eq!(dark.button_variants.success.background, dark.feedback.success);
        assert_eq!(dark.button_variants.warning.background, dark.feedback.warning);
        assert_eq!(dark.button_variants.error.background, dark.feedback.error);
        assert_eq!(dark.feedback.error, dark.error);
    }

    #[test]
    fn theme_variants_follow_primary_and_status_tokens() {
        let t = ThemeData::cloudscape_dark();
        assert_eq!(t.button_variants.primary.background, t.primary);
        assert_eq!(t.feedback.info, t.primary);
        assert_eq!(t.feedback.error, t.error);
        assert_eq!(t.avatar.foreground, Color::WHITE);
    }

    #[test]
    fn spacing_scale_defaults() {
        let s = ThemeData::cloudscape_dark().space;
        assert_eq!((s.xxs, s.xs, s.s, s.m, s.l, s.xl), (2.0, 4.0, 8.0, 16.0, 24.0, 32.0));
        assert_eq!(s.control_padding, 8.0, "控件默认内边距");
        assert_eq!(s.container_padding, 16.0, "容器默认内边距");
        assert_eq!(s.container_gap, 8.0, "容器默认子项间距");
    }
}