superlighttui 0.21.0

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

/// Spacing scale for consistent padding, margin, and gap values.
///
/// All values are in terminal cells. The scale is relative to [`Spacing::base`],
/// which defaults to 1 cell. Use [`Theme::spacing`] to access the active scale,
/// or [`crate::Context::spacing`] for convenience.
///
/// # Example
///
/// ```
/// use slt::Spacing;
///
/// let sp = Spacing::new(1);
/// assert_eq!(sp.sm(), 2);
/// assert_eq!(sp.md(), 3);
/// assert_eq!(sp.xl(), 6);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Spacing {
    /// Base unit in terminal cells. Defaults to 1.
    pub base: u32,
}

impl Spacing {
    /// Create a spacing scale with the given base unit.
    pub const fn new(base: u32) -> Self {
        Self { base }
    }

    /// Zero spacing.
    pub const fn none(&self) -> u32 {
        0
    }

    /// Extra-small spacing (1× base).
    pub const fn xs(&self) -> u32 {
        self.base
    }

    /// Small spacing (2× base).
    pub const fn sm(&self) -> u32 {
        self.base * 2
    }

    /// Medium spacing (3× base).
    pub const fn md(&self) -> u32 {
        self.base * 3
    }

    /// Large spacing (4× base).
    pub const fn lg(&self) -> u32 {
        self.base * 4
    }

    /// Extra-large spacing (6× base).
    pub const fn xl(&self) -> u32 {
        self.base * 6
    }

    /// Double extra-large spacing (8× base).
    pub const fn xxl(&self) -> u32 {
        self.base * 8
    }
}

impl Default for Spacing {
    fn default() -> Self {
        Self { base: 1 }
    }
}

/// Semantic color token that resolves against the active [`Theme`].
///
/// Use this when you want colors to automatically follow theme changes
/// without hardcoding specific [`Color`] values. Resolve with
/// [`Theme::resolve`] or [`crate::Context::color`].
///
/// # Example
///
/// ```
/// use slt::{Theme, ThemeColor};
///
/// let theme = Theme::dark();
/// let color = theme.resolve(ThemeColor::Primary);
/// assert_eq!(color, theme.primary);
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ThemeColor {
    /// Primary accent color.
    Primary,
    /// Secondary accent color.
    Secondary,
    /// Decorative accent color.
    Accent,
    /// Default text color.
    Text,
    /// Dimmed text color.
    TextDim,
    /// Border color for unfocused containers.
    Border,
    /// Background color.
    Bg,
    /// Success indicator color.
    Success,
    /// Warning indicator color.
    Warning,
    /// Error indicator color.
    Error,
    /// Selected item background.
    SelectedBg,
    /// Selected item foreground.
    SelectedFg,
    /// Surface background for elevated containers.
    Surface,
    /// Surface hover state.
    SurfaceHover,
    /// Text color readable on surface backgrounds.
    SurfaceText,
    /// Informational indicator (resolves to primary).
    Info,
    /// Hyperlink color (resolves to primary).
    Link,
    /// Focus ring outline color (resolves to primary).
    FocusRing,
    /// A literal color value, not resolved from the theme.
    Custom(Color),
}

/// Per-token-category colors for syntax highlighting, resolved through the [`Theme`].
///
/// Tree-sitter highlight capture names are mapped onto these nine categories so
/// that code blocks adopt the active theme's palette instead of a hardcoded
/// One Dark scheme. Neutral tokens (comments, operators, plain variables,
/// punctuation) continue to resolve through [`Theme::text`]/[`Theme::text_dim`]
/// and are not represented here.
///
/// # Example
///
/// ```
/// use slt::{SyntaxPalette, Theme};
///
/// let theme = Theme::nord();
/// // `keyword`-class tokens render with Nord's mauve, not One Dark purple.
/// let kw: slt::Color = theme.syntax.keyword;
/// assert_eq!(kw, SyntaxPalette::nord().keyword);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SyntaxPalette {
    /// Keywords (`fn`, `let`, `if`, `return`, ...).
    pub keyword: Color,
    /// String and char literals.
    pub string: Color,
    /// Numeric literals.
    pub number: Color,
    /// Function and method names.
    pub function: Color,
    /// Macro invocations (e.g. `println!`).
    pub macro_: Color,
    /// Type names, builtins, and constructors.
    pub type_: Color,
    /// Constants, builtin constants, and attribute values.
    pub constant: Color,
    /// Object/struct properties and fields.
    pub property: Color,
    /// Markup tags and tag-like builtin variables.
    pub tag: Color,
}

impl SyntaxPalette {
    /// One Dark (Atom) palette — the historical default used by
    /// [`Theme::dark`] and [`Theme::one_dark`].
    pub const fn one_dark() -> Self {
        Self {
            keyword: Color::Rgb(198, 120, 221),
            string: Color::Rgb(152, 195, 121),
            number: Color::Rgb(209, 154, 102),
            function: Color::Rgb(97, 175, 239),
            macro_: Color::Rgb(86, 182, 194),
            type_: Color::Rgb(229, 192, 123),
            constant: Color::Rgb(209, 154, 102),
            property: Color::Rgb(97, 175, 239),
            tag: Color::Rgb(224, 108, 117),
        }
    }

    /// One Light palette — the historical light-variant default used by
    /// [`Theme::light`] and [`Theme::solarized_light`].
    pub const fn one_light() -> Self {
        Self {
            keyword: Color::Rgb(166, 38, 164),
            string: Color::Rgb(80, 161, 79),
            number: Color::Rgb(152, 104, 1),
            function: Color::Rgb(64, 120, 242),
            macro_: Color::Rgb(1, 132, 188),
            type_: Color::Rgb(152, 104, 1),
            constant: Color::Rgb(152, 104, 1),
            property: Color::Rgb(64, 120, 242),
            tag: Color::Rgb(166, 38, 164),
        }
    }

    /// Dracula palette.
    pub const fn dracula() -> Self {
        Self {
            keyword: Color::Rgb(255, 121, 198),
            string: Color::Rgb(241, 250, 140),
            number: Color::Rgb(189, 147, 249),
            function: Color::Rgb(80, 250, 123),
            macro_: Color::Rgb(139, 233, 253),
            type_: Color::Rgb(139, 233, 253),
            constant: Color::Rgb(189, 147, 249),
            property: Color::Rgb(102, 217, 239),
            tag: Color::Rgb(255, 121, 198),
        }
    }

    /// Catppuccin Mocha palette.
    pub const fn catppuccin() -> Self {
        Self {
            keyword: Color::Rgb(203, 166, 247),
            string: Color::Rgb(166, 227, 161),
            number: Color::Rgb(250, 179, 135),
            function: Color::Rgb(137, 180, 250),
            macro_: Color::Rgb(245, 194, 231),
            type_: Color::Rgb(249, 226, 175),
            constant: Color::Rgb(250, 179, 135),
            property: Color::Rgb(137, 220, 235),
            tag: Color::Rgb(243, 139, 168),
        }
    }

    /// Nord palette.
    pub const fn nord() -> Self {
        Self {
            keyword: Color::Rgb(180, 142, 173),
            string: Color::Rgb(163, 190, 140),
            number: Color::Rgb(180, 142, 173),
            function: Color::Rgb(136, 192, 208),
            macro_: Color::Rgb(143, 188, 187),
            type_: Color::Rgb(143, 188, 187),
            constant: Color::Rgb(208, 135, 112),
            property: Color::Rgb(129, 161, 193),
            tag: Color::Rgb(191, 97, 106),
        }
    }

    /// Solarized Dark palette.
    pub const fn solarized_dark() -> Self {
        Self {
            keyword: Color::Rgb(133, 153, 0),
            string: Color::Rgb(42, 161, 152),
            number: Color::Rgb(211, 54, 130),
            function: Color::Rgb(38, 139, 210),
            macro_: Color::Rgb(203, 75, 22),
            type_: Color::Rgb(181, 137, 0),
            constant: Color::Rgb(211, 54, 130),
            property: Color::Rgb(38, 139, 210),
            tag: Color::Rgb(220, 50, 47),
        }
    }

    /// Solarized Light palette.
    pub const fn solarized_light() -> Self {
        Self {
            keyword: Color::Rgb(133, 153, 0),
            string: Color::Rgb(42, 161, 152),
            number: Color::Rgb(211, 54, 130),
            function: Color::Rgb(38, 139, 210),
            macro_: Color::Rgb(203, 75, 22),
            type_: Color::Rgb(181, 137, 0),
            constant: Color::Rgb(211, 54, 130),
            property: Color::Rgb(38, 139, 210),
            tag: Color::Rgb(220, 50, 47),
        }
    }

    /// Tokyo Night palette.
    pub const fn tokyo_night() -> Self {
        Self {
            keyword: Color::Rgb(187, 154, 247),
            string: Color::Rgb(158, 206, 106),
            number: Color::Rgb(255, 158, 100),
            function: Color::Rgb(122, 162, 247),
            macro_: Color::Rgb(125, 207, 255),
            type_: Color::Rgb(43, 178, 187),
            constant: Color::Rgb(255, 158, 100),
            property: Color::Rgb(115, 218, 202),
            tag: Color::Rgb(247, 118, 142),
        }
    }

    /// Gruvbox Dark palette.
    pub const fn gruvbox_dark() -> Self {
        Self {
            keyword: Color::Rgb(251, 73, 52),
            string: Color::Rgb(184, 187, 38),
            number: Color::Rgb(211, 134, 155),
            function: Color::Rgb(184, 187, 38),
            macro_: Color::Rgb(142, 192, 124),
            type_: Color::Rgb(250, 189, 47),
            constant: Color::Rgb(211, 134, 155),
            property: Color::Rgb(131, 165, 152),
            tag: Color::Rgb(251, 73, 52),
        }
    }
}

impl Default for SyntaxPalette {
    fn default() -> Self {
        Self::one_dark()
    }
}

/// A color theme that flows through all widgets automatically.
///
/// Construct with [`Theme::dark()`] or [`Theme::light()`], or use
/// [`Theme::builder()`] for custom themes. Pass via [`crate::RunConfig`]
/// and every widget picks up the colors without any extra wiring.
///
/// # Example
///
/// ```
/// use slt::{Color, Theme};
///
/// let theme = Theme::builder()
///     .primary(Color::Rgb(255, 107, 107))
///     .build();
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct Theme {
    /// Primary accent color, used for focused borders and highlights.
    pub primary: Color,
    /// Secondary accent color, used for less prominent highlights.
    pub secondary: Color,
    /// Accent color for decorative elements.
    pub accent: Color,
    /// Default foreground text color.
    pub text: Color,
    /// Dimmed text color for secondary labels and hints.
    pub text_dim: Color,
    /// Border color for unfocused containers.
    pub border: Color,
    /// Background color. Typically [`Color::Reset`] to inherit the terminal background.
    pub bg: Color,
    /// Color for success states (e.g., toast notifications).
    pub success: Color,
    /// Color for warning states.
    pub warning: Color,
    /// Color for error states.
    pub error: Color,
    /// Background color for selected list/table rows.
    pub selected_bg: Color,
    /// Foreground color for selected list/table rows.
    pub selected_fg: Color,
    /// Subtle surface color for card backgrounds and elevated containers.
    pub surface: Color,
    /// Hover/active surface color, one step brighter than `surface`.
    ///
    /// Used for interactive element hover states. Should be visually
    /// distinguishable from both `surface` and `border`.
    pub surface_hover: Color,
    /// Secondary text color guaranteed readable on `surface` backgrounds.
    ///
    /// Use this instead of `text_dim` when rendering on `surface`-colored
    /// containers. `text_dim` is tuned for the main `bg`; on `surface` it
    /// may lack contrast.
    pub surface_text: Color,
    /// Whether this theme is a dark theme. Used to initialize dark mode in Context.
    pub is_dark: bool,
    /// Spacing scale for consistent padding, margin, and gap values.
    pub spacing: Spacing,
    /// Per-token-category colors used to drive syntax highlighting.
    pub syntax: SyntaxPalette,
}

impl Theme {
    /// Resolve a [`ThemeColor`] token to a concrete [`Color`].
    pub fn resolve(&self, token: ThemeColor) -> Color {
        match token {
            ThemeColor::Primary => self.primary,
            ThemeColor::Secondary => self.secondary,
            ThemeColor::Accent => self.accent,
            ThemeColor::Text => self.text,
            ThemeColor::TextDim => self.text_dim,
            ThemeColor::Border => self.border,
            ThemeColor::Bg => self.bg,
            ThemeColor::Success => self.success,
            ThemeColor::Warning => self.warning,
            ThemeColor::Error => self.error,
            ThemeColor::SelectedBg => self.selected_bg,
            ThemeColor::SelectedFg => self.selected_fg,
            ThemeColor::Surface => self.surface,
            ThemeColor::SurfaceHover => self.surface_hover,
            ThemeColor::SurfaceText => self.surface_text,
            ThemeColor::Info | ThemeColor::Link | ThemeColor::FocusRing => self.primary,
            ThemeColor::Custom(c) => c,
        }
    }

    /// Return a text color with guaranteed contrast against the given background.
    ///
    /// Delegates to [`Color::contrast_fg`].
    pub fn contrast_text_on(&self, bg: Color) -> Color {
        Color::contrast_fg(bg)
    }

    /// Blend a color with the theme's background at the given alpha.
    ///
    /// `alpha = 0.0` returns `self.bg`, `alpha = 1.0` returns `color` unchanged.
    pub fn overlay(&self, color: Color, alpha: f32) -> Color {
        color.blend(self.bg, alpha)
    }

    /// Create a dark theme with cyan primary and white text.
    pub const fn dark() -> Self {
        Self {
            primary: Color::Cyan,
            secondary: Color::Blue,
            accent: Color::Magenta,
            text: Color::White,
            text_dim: Color::Indexed(245),
            border: Color::Indexed(240),
            bg: Color::Reset,
            success: Color::Green,
            warning: Color::Yellow,
            error: Color::Red,
            selected_bg: Color::Cyan,
            selected_fg: Color::Black,
            surface: Color::Indexed(236),
            surface_hover: Color::Indexed(238),
            surface_text: Color::Indexed(250),
            is_dark: true,
            spacing: Spacing::new(1),
            syntax: SyntaxPalette::one_dark(),
        }
    }

    /// Create a light theme with high-contrast dark text on light backgrounds.
    pub const fn light() -> Self {
        Self {
            primary: Color::Rgb(37, 99, 235),
            secondary: Color::Rgb(14, 116, 144),
            accent: Color::Rgb(147, 51, 234),
            text: Color::Rgb(15, 23, 42),
            text_dim: Color::Rgb(100, 116, 139),
            border: Color::Rgb(203, 213, 225),
            bg: Color::Rgb(248, 250, 252),
            success: Color::Rgb(22, 163, 74),
            warning: Color::Rgb(202, 138, 4),
            error: Color::Rgb(220, 38, 38),
            selected_bg: Color::Rgb(37, 99, 235),
            selected_fg: Color::White,
            surface: Color::Rgb(241, 245, 249),
            surface_hover: Color::Rgb(226, 232, 240),
            surface_text: Color::Rgb(51, 65, 85),
            is_dark: false,
            spacing: Spacing::new(1),
            syntax: SyntaxPalette::one_light(),
        }
    }

    /// Create a [`ThemeBuilder`] for configuring a custom theme.
    ///
    /// Unset fields fall back to [`Theme::dark()`] defaults. Use
    /// [`Theme::builder_from`] to start from a different base, or
    /// [`Theme::light_builder`] for a light-base shorthand.
    ///
    /// # Example
    ///
    /// ```
    /// use slt::{Color, Theme};
    ///
    /// let theme = Theme::builder()
    ///     .primary(Color::Rgb(255, 107, 107))
    ///     .accent(Color::Cyan)
    ///     .build();
    /// ```
    pub const fn builder() -> ThemeBuilder {
        ThemeBuilder {
            primary: None,
            secondary: None,
            accent: None,
            text: None,
            text_dim: None,
            border: None,
            bg: None,
            success: None,
            warning: None,
            error: None,
            selected_bg: None,
            selected_fg: None,
            surface: None,
            surface_hover: None,
            surface_text: None,
            is_dark: None,
            spacing: None,
            syntax: None,
        }
    }

    /// Create a [`ThemeBuilder`] pre-filled with every field from `base`.
    ///
    /// Only fields explicitly overridden via builder methods will differ
    /// from `base`; unset fields keep `base`'s value (rather than falling
    /// back to [`Theme::dark()`] defaults as plain [`Theme::builder`]
    /// does). Useful for deriving variants from any preset.
    ///
    /// # Example
    ///
    /// ```
    /// use slt::{Color, Theme};
    ///
    /// // Nord variant: keep all Nord colors but override primary.
    /// let custom_nord = Theme::builder_from(Theme::nord())
    ///     .primary(Color::Rgb(255, 0, 0))
    ///     .build();
    /// assert_eq!(custom_nord.bg, Theme::nord().bg);
    /// assert_eq!(custom_nord.primary, Color::Rgb(255, 0, 0));
    /// ```
    pub const fn builder_from(base: Theme) -> ThemeBuilder {
        ThemeBuilder {
            primary: Some(base.primary),
            secondary: Some(base.secondary),
            accent: Some(base.accent),
            text: Some(base.text),
            text_dim: Some(base.text_dim),
            border: Some(base.border),
            bg: Some(base.bg),
            success: Some(base.success),
            warning: Some(base.warning),
            error: Some(base.error),
            selected_bg: Some(base.selected_bg),
            selected_fg: Some(base.selected_fg),
            surface: Some(base.surface),
            surface_hover: Some(base.surface_hover),
            surface_text: Some(base.surface_text),
            is_dark: Some(base.is_dark),
            spacing: Some(base.spacing),
            syntax: Some(base.syntax),
        }
    }

    /// Convenience: builder pre-filled with all [`Theme::light()`] fields.
    ///
    /// Equivalent to `Theme::builder_from(Theme::light())`.
    ///
    /// # Example
    ///
    /// ```
    /// use slt::{Color, Theme};
    ///
    /// let my_light = Theme::light_builder()
    ///     .primary(Color::Rgb(0, 100, 200))
    ///     .build();
    /// assert!(!my_light.is_dark);
    /// assert_eq!(my_light.primary, Color::Rgb(0, 100, 200));
    /// // bg stays light (not dark()'s Reset)
    /// assert_eq!(my_light.bg, Theme::light().bg);
    /// ```
    pub const fn light_builder() -> ThemeBuilder {
        Self::builder_from(Self::light())
    }

    /// Dracula theme — purple primary on dark gray.
    pub fn dracula() -> Self {
        Self {
            primary: Color::Rgb(189, 147, 249),
            secondary: Color::Rgb(139, 233, 253),
            accent: Color::Rgb(255, 121, 198),
            text: Color::Rgb(248, 248, 242),
            text_dim: Color::Rgb(98, 114, 164),
            border: Color::Rgb(68, 71, 90),
            bg: Color::Rgb(40, 42, 54),
            success: Color::Rgb(80, 250, 123),
            warning: Color::Rgb(241, 250, 140),
            error: Color::Rgb(255, 85, 85),
            selected_bg: Color::Rgb(189, 147, 249),
            selected_fg: Color::Rgb(40, 42, 54),
            surface: Color::Rgb(68, 71, 90),
            surface_hover: Color::Rgb(98, 100, 120),
            surface_text: Color::Rgb(191, 194, 210),
            is_dark: true,
            spacing: Spacing::new(1),
            syntax: SyntaxPalette::dracula(),
        }
    }

    /// Catppuccin Mocha theme — lavender primary on dark base.
    pub fn catppuccin() -> Self {
        Self {
            primary: Color::Rgb(180, 190, 254),
            secondary: Color::Rgb(137, 180, 250),
            accent: Color::Rgb(245, 194, 231),
            text: Color::Rgb(205, 214, 244),
            text_dim: Color::Rgb(127, 132, 156),
            border: Color::Rgb(88, 91, 112),
            bg: Color::Rgb(30, 30, 46),
            success: Color::Rgb(166, 227, 161),
            warning: Color::Rgb(249, 226, 175),
            error: Color::Rgb(243, 139, 168),
            selected_bg: Color::Rgb(180, 190, 254),
            selected_fg: Color::Rgb(30, 30, 46),
            surface: Color::Rgb(49, 50, 68),
            surface_hover: Color::Rgb(69, 71, 90),
            surface_text: Color::Rgb(166, 173, 200),
            is_dark: true,
            spacing: Spacing::new(1),
            syntax: SyntaxPalette::catppuccin(),
        }
    }

    /// Nord theme — frost blue primary on polar night.
    pub fn nord() -> Self {
        Self {
            primary: Color::Rgb(136, 192, 208),
            secondary: Color::Rgb(129, 161, 193),
            accent: Color::Rgb(180, 142, 173),
            text: Color::Rgb(236, 239, 244),
            text_dim: Color::Rgb(216, 222, 233),
            border: Color::Rgb(59, 66, 82),
            bg: Color::Rgb(46, 52, 64),
            success: Color::Rgb(163, 190, 140),
            warning: Color::Rgb(235, 203, 139),
            error: Color::Rgb(191, 97, 106),
            selected_bg: Color::Rgb(136, 192, 208),
            selected_fg: Color::Rgb(46, 52, 64),
            surface: Color::Rgb(59, 66, 82),
            surface_hover: Color::Rgb(67, 76, 94),
            surface_text: Color::Rgb(216, 222, 233),
            is_dark: true,
            spacing: Spacing::new(1),
            syntax: SyntaxPalette::nord(),
        }
    }

    /// Solarized Dark theme — blue primary on dark base.
    pub fn solarized_dark() -> Self {
        Self {
            primary: Color::Rgb(38, 139, 210),
            secondary: Color::Rgb(42, 161, 152),
            accent: Color::Rgb(211, 54, 130),
            text: Color::Rgb(131, 148, 150),
            text_dim: Color::Rgb(101, 123, 131),
            border: Color::Rgb(7, 54, 66),
            bg: Color::Rgb(0, 43, 54),
            success: Color::Rgb(133, 153, 0),
            warning: Color::Rgb(181, 137, 0),
            error: Color::Rgb(220, 50, 47),
            selected_bg: Color::Rgb(38, 139, 210),
            selected_fg: Color::Rgb(253, 246, 227),
            surface: Color::Rgb(7, 54, 66),
            surface_hover: Color::Rgb(23, 72, 85),
            surface_text: Color::Rgb(147, 161, 161),
            is_dark: true,
            spacing: Spacing::new(1),
            syntax: SyntaxPalette::solarized_dark(),
        }
    }

    /// Solarized Light theme — warm ochre primary on light base.
    pub fn solarized_light() -> Self {
        Self {
            primary: Color::Rgb(38, 139, 210),
            secondary: Color::Rgb(42, 161, 152),
            accent: Color::Rgb(211, 54, 130),
            text: Color::Rgb(101, 123, 131),
            text_dim: Color::Rgb(88, 110, 117),
            border: Color::Rgb(238, 232, 213),
            bg: Color::Rgb(253, 246, 227),
            success: Color::Rgb(133, 153, 0),
            warning: Color::Rgb(181, 137, 0),
            error: Color::Rgb(220, 50, 47),
            selected_bg: Color::Rgb(38, 139, 210),
            selected_fg: Color::Rgb(253, 246, 227),
            surface: Color::Rgb(238, 232, 213),
            surface_hover: Color::Rgb(227, 221, 201),
            surface_text: Color::Rgb(88, 110, 117),
            is_dark: false,
            spacing: Spacing::new(1),
            syntax: SyntaxPalette::solarized_light(),
        }
    }

    /// Tokyo Night theme — blue primary on dark storm base.
    pub fn tokyo_night() -> Self {
        Self {
            primary: Color::Rgb(122, 162, 247),
            secondary: Color::Rgb(125, 207, 255),
            accent: Color::Rgb(187, 154, 247),
            text: Color::Rgb(169, 177, 214),
            text_dim: Color::Rgb(86, 95, 137),
            border: Color::Rgb(54, 58, 79),
            bg: Color::Rgb(26, 27, 38),
            success: Color::Rgb(158, 206, 106),
            warning: Color::Rgb(224, 175, 104),
            error: Color::Rgb(247, 118, 142),
            selected_bg: Color::Rgb(122, 162, 247),
            selected_fg: Color::Rgb(26, 27, 38),
            surface: Color::Rgb(36, 40, 59),
            surface_hover: Color::Rgb(41, 46, 66),
            surface_text: Color::Rgb(192, 202, 245),
            is_dark: true,
            spacing: Spacing::new(1),
            syntax: SyntaxPalette::tokyo_night(),
        }
    }

    /// Gruvbox Dark theme — warm, retro tones on dark background.
    pub fn gruvbox_dark() -> Self {
        Self {
            primary: Color::Rgb(215, 153, 33),
            secondary: Color::Rgb(69, 133, 136),
            accent: Color::Rgb(177, 98, 134),
            text: Color::Rgb(235, 219, 178),
            text_dim: Color::Rgb(146, 131, 116),
            border: Color::Rgb(80, 73, 69),
            bg: Color::Rgb(40, 40, 40),
            success: Color::Rgb(152, 151, 26),
            warning: Color::Rgb(250, 189, 47),
            error: Color::Rgb(204, 36, 29),
            selected_bg: Color::Rgb(215, 153, 33),
            selected_fg: Color::Rgb(40, 40, 40),
            surface: Color::Rgb(60, 56, 54),
            surface_hover: Color::Rgb(80, 73, 69),
            surface_text: Color::Rgb(189, 174, 147),
            is_dark: true,
            spacing: Spacing::new(1),
            syntax: SyntaxPalette::gruvbox_dark(),
        }
    }

    /// Compact density preset — base spacing = 1 (matches v0.19 default behavior).
    ///
    /// Use when terminal space is tight or you want maximum information
    /// density. Built on [`Theme::dark()`] colors with `Spacing::new(1)`.
    ///
    /// # Example
    ///
    /// ```
    /// use slt::Theme;
    ///
    /// let theme = Theme::compact();
    /// assert_eq!(theme.spacing.xs(), 1);
    /// ```
    pub const fn compact() -> Self {
        let base = Self::dark();
        Self {
            spacing: Spacing::new(1),
            ..base
        }
    }

    /// Comfortable density preset — base spacing = 2.
    ///
    /// Widgets use roughly twice the padding/margin of [`Theme::compact`],
    /// improving readability in spacious terminals at the cost of fitting
    /// less content per screen.
    ///
    /// # Example
    ///
    /// ```
    /// use slt::Theme;
    ///
    /// let theme = Theme::comfortable();
    /// assert_eq!(theme.spacing.xs(), 2);
    /// assert_eq!(theme.spacing.sm(), 4);
    /// ```
    pub const fn comfortable() -> Self {
        let base = Self::dark();
        Self {
            spacing: Spacing::new(2),
            ..base
        }
    }

    /// Spacious density preset — base spacing = 3.
    ///
    /// Tripled padding/margin compared to [`Theme::compact`] — best for
    /// presentations, demos, or very wide terminals where "breathing room"
    /// matters more than density.
    ///
    /// # Example
    ///
    /// ```
    /// use slt::Theme;
    ///
    /// let theme = Theme::spacious();
    /// assert_eq!(theme.spacing.xs(), 3);
    /// assert_eq!(theme.spacing.sm(), 6);
    /// ```
    pub const fn spacious() -> Self {
        let base = Self::dark();
        Self {
            spacing: Spacing::new(3),
            ..base
        }
    }

    /// Apply a [`Spacing`] scale on top of the current theme, returning a new theme.
    ///
    /// All other fields are preserved. Useful to adapt any preset (Nord,
    /// Dracula, custom) to a new density without rebuilding the colors.
    ///
    /// # Example
    ///
    /// ```
    /// use slt::{Spacing, Theme};
    ///
    /// let dense_nord = Theme::nord().with_spacing(Spacing::new(2));
    /// assert_eq!(dense_nord.spacing.xs(), 2);
    /// // Nord colors preserved.
    /// assert_eq!(dense_nord.bg, Theme::nord().bg);
    /// ```
    pub const fn with_spacing(mut self, spacing: Spacing) -> Self {
        self.spacing = spacing;
        self
    }

    /// One Dark theme (Atom) — cool blues and purples on dark gray.
    pub fn one_dark() -> Self {
        Self {
            primary: Color::Rgb(97, 175, 239),
            secondary: Color::Rgb(86, 182, 194),
            accent: Color::Rgb(198, 120, 221),
            text: Color::Rgb(171, 178, 191),
            text_dim: Color::Rgb(92, 99, 112),
            border: Color::Rgb(62, 68, 81),
            bg: Color::Rgb(40, 44, 52),
            success: Color::Rgb(152, 195, 121),
            warning: Color::Rgb(229, 192, 123),
            error: Color::Rgb(224, 108, 117),
            selected_bg: Color::Rgb(97, 175, 239),
            selected_fg: Color::Rgb(40, 44, 52),
            surface: Color::Rgb(50, 55, 65),
            surface_hover: Color::Rgb(62, 68, 81),
            surface_text: Color::Rgb(152, 159, 172),
            is_dark: true,
            spacing: Spacing::new(1),
            syntax: SyntaxPalette::one_dark(),
        }
    }

    /// Parse a [`Theme`] from a TOML document, ignoring any `[widgets]` block.
    ///
    /// The colors live under a top-level `[theme]` table; missing fields fall
    /// back to [`Theme::dark()`]. Color values accept `#rrggbb`/`#rgb` hex,
    /// named colors (`"cyan"`), or `indexed:N` palette indices. To also read
    /// per-widget overrides, use [`ThemeFile::from_toml_str`] instead.
    ///
    /// # Errors
    ///
    /// Returns [`ThemeLoadError::Parse`] if the document is not valid TOML or
    /// does not match the expected shape.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use slt::Theme;
    ///
    /// let toml = r##"
    /// [theme]
    /// primary = "#ff6b6b"
    /// bg = "#1e1e2e"
    /// is_dark = true
    /// "##;
    /// let theme = Theme::from_toml_str(toml).unwrap();
    /// assert_eq!(theme.primary, slt::Color::Rgb(255, 107, 107));
    /// ```
    #[cfg(feature = "serde")]
    pub fn from_toml_str(src: &str) -> Result<Theme, ThemeLoadError> {
        ThemeFile::from_toml_str(src).map(|tf| tf.theme)
    }

    /// Load a [`Theme`] from a TOML file at `path`, ignoring `[widgets]`.
    ///
    /// Convenience over [`ThemeFile::load`] when you only need the base theme.
    ///
    /// # Errors
    ///
    /// Returns [`ThemeLoadError::Io`] if the file cannot be read, or
    /// [`ThemeLoadError::Parse`] if it is not valid TOML.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use slt::Theme;
    ///
    /// let theme = Theme::load("theme.toml").unwrap();
    /// println!("primary = {:?}", theme.primary);
    /// ```
    #[cfg(feature = "serde")]
    pub fn load(path: impl AsRef<std::path::Path>) -> Result<Theme, ThemeLoadError> {
        ThemeFile::load(path).map(|tf| tf.theme)
    }
}

/// Builder for creating custom themes with defaults from `Theme::dark()`.
pub struct ThemeBuilder {
    primary: Option<Color>,
    secondary: Option<Color>,
    accent: Option<Color>,
    text: Option<Color>,
    text_dim: Option<Color>,
    border: Option<Color>,
    bg: Option<Color>,
    success: Option<Color>,
    warning: Option<Color>,
    error: Option<Color>,
    selected_bg: Option<Color>,
    selected_fg: Option<Color>,
    surface: Option<Color>,
    surface_hover: Option<Color>,
    surface_text: Option<Color>,
    is_dark: Option<bool>,
    spacing: Option<Spacing>,
    syntax: Option<SyntaxPalette>,
}

impl ThemeBuilder {
    /// Set the primary color.
    pub const fn primary(mut self, color: Color) -> Self {
        self.primary = Some(color);
        self
    }

    /// Set the secondary color.
    pub const fn secondary(mut self, color: Color) -> Self {
        self.secondary = Some(color);
        self
    }

    /// Set the accent color.
    pub const fn accent(mut self, color: Color) -> Self {
        self.accent = Some(color);
        self
    }

    /// Set the main text color.
    pub const fn text(mut self, color: Color) -> Self {
        self.text = Some(color);
        self
    }

    /// Set the dimmed text color.
    pub const fn text_dim(mut self, color: Color) -> Self {
        self.text_dim = Some(color);
        self
    }

    /// Set the border color.
    pub const fn border(mut self, color: Color) -> Self {
        self.border = Some(color);
        self
    }

    /// Set the background color.
    pub const fn bg(mut self, color: Color) -> Self {
        self.bg = Some(color);
        self
    }

    /// Set the success indicator color.
    pub const fn success(mut self, color: Color) -> Self {
        self.success = Some(color);
        self
    }

    /// Set the warning indicator color.
    pub const fn warning(mut self, color: Color) -> Self {
        self.warning = Some(color);
        self
    }

    /// Set the error indicator color.
    pub const fn error(mut self, color: Color) -> Self {
        self.error = Some(color);
        self
    }

    /// Set the selected item background color.
    pub const fn selected_bg(mut self, color: Color) -> Self {
        self.selected_bg = Some(color);
        self
    }

    /// Set the selected item foreground color.
    pub const fn selected_fg(mut self, color: Color) -> Self {
        self.selected_fg = Some(color);
        self
    }

    /// Set the surface background color.
    pub const fn surface(mut self, color: Color) -> Self {
        self.surface = Some(color);
        self
    }

    /// Set the surface hover color.
    pub const fn surface_hover(mut self, color: Color) -> Self {
        self.surface_hover = Some(color);
        self
    }

    /// Set the surface text color.
    pub const fn surface_text(mut self, color: Color) -> Self {
        self.surface_text = Some(color);
        self
    }

    /// Set the dark mode flag.
    pub const fn is_dark(mut self, is_dark: bool) -> Self {
        self.is_dark = Some(is_dark);
        self
    }

    /// Set the spacing scale.
    pub const fn spacing(mut self, spacing: Spacing) -> Self {
        self.spacing = Some(spacing);
        self
    }

    /// Set the syntax-highlighting palette.
    pub const fn syntax(mut self, syntax: SyntaxPalette) -> Self {
        self.syntax = Some(syntax);
        self
    }

    /// Build the theme. Unfilled fields use [`Theme::dark()`] defaults.
    ///
    /// `match` is used in place of [`Option::unwrap_or`] so the entire
    /// builder chain compiles in `const` context (MSRV 1.81). All fields
    /// involved are `Copy`, so `Some(c) => c` is a plain bit-copy.
    pub const fn build(self) -> Theme {
        let d = Theme::dark();
        Theme {
            primary: match self.primary {
                Some(c) => c,
                None => d.primary,
            },
            secondary: match self.secondary {
                Some(c) => c,
                None => d.secondary,
            },
            accent: match self.accent {
                Some(c) => c,
                None => d.accent,
            },
            text: match self.text {
                Some(c) => c,
                None => d.text,
            },
            text_dim: match self.text_dim {
                Some(c) => c,
                None => d.text_dim,
            },
            border: match self.border {
                Some(c) => c,
                None => d.border,
            },
            bg: match self.bg {
                Some(c) => c,
                None => d.bg,
            },
            success: match self.success {
                Some(c) => c,
                None => d.success,
            },
            warning: match self.warning {
                Some(c) => c,
                None => d.warning,
            },
            error: match self.error {
                Some(c) => c,
                None => d.error,
            },
            selected_bg: match self.selected_bg {
                Some(c) => c,
                None => d.selected_bg,
            },
            selected_fg: match self.selected_fg {
                Some(c) => c,
                None => d.selected_fg,
            },
            surface: match self.surface {
                Some(c) => c,
                None => d.surface,
            },
            surface_hover: match self.surface_hover {
                Some(c) => c,
                None => d.surface_hover,
            },
            surface_text: match self.surface_text {
                Some(c) => c,
                None => d.surface_text,
            },
            is_dark: match self.is_dark {
                Some(b) => b,
                None => d.is_dark,
            },
            spacing: match self.spacing {
                Some(s) => s,
                None => d.spacing,
            },
            syntax: match self.syntax {
                Some(s) => s,
                None => d.syntax,
            },
        }
    }
}

impl Default for Theme {
    fn default() -> Self {
        Self::dark()
    }
}

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

    #[test]
    fn theme_dark_preset_builds() {
        let t = Theme::dark();
        assert_eq!(t.primary, Color::Cyan);
        assert!(t.is_dark);
    }

    #[test]
    fn dark_preset_uses_one_dark_syntax_palette() {
        assert_eq!(Theme::dark().syntax, SyntaxPalette::one_dark());
        assert_eq!(Theme::one_dark().syntax, SyntaxPalette::one_dark());
    }

    #[test]
    fn presets_carry_distinct_syntax_palettes() {
        // A non-One-Dark preset must override the keyword color.
        assert_ne!(
            Theme::nord().syntax.keyword,
            SyntaxPalette::one_dark().keyword
        );
        assert_ne!(
            Theme::nord().syntax.keyword,
            Theme::catppuccin().syntax.keyword
        );
    }

    #[test]
    fn builder_syntax_setter_overrides_palette() {
        let custom = SyntaxPalette {
            keyword: Color::Rgb(1, 2, 3),
            ..SyntaxPalette::one_dark()
        };
        let theme = Theme::builder().syntax(custom).build();
        assert_eq!(theme.syntax.keyword, Color::Rgb(1, 2, 3));
    }

    #[test]
    fn builder_from_threads_syntax_palette() {
        let theme = Theme::builder_from(Theme::nord())
            .primary(Color::Rgb(255, 0, 0))
            .build();
        // Syntax palette is preserved from the Nord base.
        assert_eq!(theme.syntax, Theme::nord().syntax);
    }

    #[test]
    fn syntax_palette_default_is_one_dark() {
        assert_eq!(SyntaxPalette::default(), SyntaxPalette::one_dark());
    }

    #[test]
    fn theme_light_preset_builds() {
        let t = Theme::light();
        assert_eq!(t.selected_fg, Color::White);
        assert!(!t.is_dark);
    }

    #[test]
    fn theme_dracula_preset_builds() {
        let t = Theme::dracula();
        assert_eq!(t.bg, Color::Rgb(40, 42, 54));
        assert!(t.is_dark);
    }

    #[test]
    fn theme_catppuccin_preset_builds() {
        let t = Theme::catppuccin();
        assert_eq!(t.bg, Color::Rgb(30, 30, 46));
        assert!(t.is_dark);
    }

    #[test]
    fn theme_nord_preset_builds() {
        let t = Theme::nord();
        assert_eq!(t.bg, Color::Rgb(46, 52, 64));
        assert!(t.is_dark);
    }

    #[test]
    fn theme_solarized_dark_preset_builds() {
        let t = Theme::solarized_dark();
        assert_eq!(t.bg, Color::Rgb(0, 43, 54));
        assert!(t.is_dark);
    }

    #[test]
    fn theme_tokyo_night_preset_builds() {
        let t = Theme::tokyo_night();
        assert_eq!(t.bg, Color::Rgb(26, 27, 38));
        assert!(t.is_dark);
    }

    #[test]
    fn theme_builder_sets_primary_and_accent() {
        let theme = Theme::builder()
            .primary(Color::Red)
            .accent(Color::Yellow)
            .build();

        assert_eq!(theme.primary, Color::Red);
        assert_eq!(theme.accent, Color::Yellow);
    }

    #[test]
    fn theme_builder_defaults_to_dark_for_unset_fields() {
        let defaults = Theme::dark();
        let theme = Theme::builder().primary(Color::Green).build();

        assert_eq!(theme.primary, Color::Green);
        assert_eq!(theme.secondary, defaults.secondary);
        assert_eq!(theme.text, defaults.text);
        assert_eq!(theme.text_dim, defaults.text_dim);
        assert_eq!(theme.border, defaults.border);
        assert_eq!(theme.surface_hover, defaults.surface_hover);
        assert_eq!(theme.is_dark, defaults.is_dark);
    }

    #[test]
    fn theme_builder_can_override_is_dark() {
        let theme = Theme::builder().is_dark(false).build();
        assert!(!theme.is_dark);
    }

    #[test]
    fn theme_default_matches_dark() {
        let default_theme = Theme::default();
        let dark = Theme::dark();
        assert_eq!(default_theme.primary, dark.primary);
        assert_eq!(default_theme.bg, dark.bg);
        assert_eq!(default_theme.is_dark, dark.is_dark);
    }

    #[test]
    fn theme_solarized_light_preset_builds() {
        let t = Theme::solarized_light();
        assert_eq!(t.bg, Color::Rgb(253, 246, 227));
        assert!(!t.is_dark);
    }

    #[test]
    fn theme_gruvbox_dark_preset_builds() {
        let t = Theme::gruvbox_dark();
        assert_eq!(t.bg, Color::Rgb(40, 40, 40));
        assert!(t.is_dark);
    }

    #[test]
    fn theme_one_dark_preset_builds() {
        let t = Theme::one_dark();
        assert_eq!(t.bg, Color::Rgb(40, 44, 52));
        assert!(t.is_dark);
    }

    // --- regression: issue #106 Nord/Solarized text_dim collides with border ---

    #[test]
    fn theme_text_dim_ne_border() {
        for theme in [
            Theme::nord(),
            Theme::solarized_dark(),
            Theme::solarized_light(),
        ] {
            assert_ne!(theme.text_dim, theme.border, "text_dim == border in theme");
        }
    }

    #[test]
    fn spacing_scale_values() {
        let sp = Spacing::new(1);
        assert_eq!(sp.none(), 0);
        assert_eq!(sp.xs(), 1);
        assert_eq!(sp.sm(), 2);
        assert_eq!(sp.md(), 3);
        assert_eq!(sp.lg(), 4);
        assert_eq!(sp.xl(), 6);
        assert_eq!(sp.xxl(), 8);
    }

    #[test]
    fn spacing_custom_base() {
        let sp = Spacing::new(2);
        assert_eq!(sp.xs(), 2);
        assert_eq!(sp.sm(), 4);
        assert_eq!(sp.md(), 6);
    }

    #[test]
    fn theme_color_resolve_maps_correctly() {
        let t = Theme::dark();
        assert_eq!(t.resolve(ThemeColor::Primary), t.primary);
        assert_eq!(t.resolve(ThemeColor::Secondary), t.secondary);
        assert_eq!(t.resolve(ThemeColor::Accent), t.accent);
        assert_eq!(t.resolve(ThemeColor::Text), t.text);
        assert_eq!(t.resolve(ThemeColor::TextDim), t.text_dim);
        assert_eq!(t.resolve(ThemeColor::Border), t.border);
        assert_eq!(t.resolve(ThemeColor::Bg), t.bg);
        assert_eq!(t.resolve(ThemeColor::Success), t.success);
        assert_eq!(t.resolve(ThemeColor::Warning), t.warning);
        assert_eq!(t.resolve(ThemeColor::Error), t.error);
        assert_eq!(t.resolve(ThemeColor::SelectedBg), t.selected_bg);
        assert_eq!(t.resolve(ThemeColor::SelectedFg), t.selected_fg);
        assert_eq!(t.resolve(ThemeColor::Surface), t.surface);
        assert_eq!(t.resolve(ThemeColor::SurfaceHover), t.surface_hover);
        assert_eq!(t.resolve(ThemeColor::SurfaceText), t.surface_text);
    }

    #[test]
    fn theme_color_aliases_resolve_to_primary() {
        let t = Theme::dark();
        assert_eq!(t.resolve(ThemeColor::Info), t.primary);
        assert_eq!(t.resolve(ThemeColor::Link), t.primary);
        assert_eq!(t.resolve(ThemeColor::FocusRing), t.primary);
    }

    #[test]
    fn theme_color_custom_passes_through() {
        let t = Theme::dark();
        let custom = Color::Rgb(42, 42, 42);
        assert_eq!(t.resolve(ThemeColor::Custom(custom)), custom);
    }

    #[test]
    fn theme_builder_spacing() {
        let sp = Spacing::new(3);
        let theme = Theme::builder().spacing(sp).build();
        assert_eq!(theme.spacing, sp);
    }

    #[test]
    fn theme_contrast_text_on_dark_bg() {
        let t = Theme::dark();
        let fg = t.contrast_text_on(Color::Rgb(0, 0, 0));
        assert_eq!(fg, Color::Rgb(255, 255, 255));
    }

    #[test]
    fn theme_contrast_text_on_light_bg() {
        let t = Theme::dark();
        let fg = t.contrast_text_on(Color::Rgb(255, 255, 255));
        assert_eq!(fg, Color::Rgb(0, 0, 0));
    }

    // --- regression: issue #109 ThemeBuilder methods are const fn ---

    /// If this `const` evaluation ever fails to compile, a setter on
    /// `ThemeBuilder` was accidentally demoted to a non-const fn.
    const _CONST_THEME: Theme = Theme::builder()
        .primary(Color::Rgb(255, 100, 100))
        .bg(Color::Rgb(20, 20, 20))
        .is_dark(true)
        .spacing(Spacing::new(2))
        .build();

    #[test]
    fn theme_builder_const_eval() {
        // Set fields take the override.
        assert_eq!(_CONST_THEME.primary, Color::Rgb(255, 100, 100));
        assert_eq!(_CONST_THEME.bg, Color::Rgb(20, 20, 20));
        assert_eq!(_CONST_THEME.spacing, Spacing::new(2));
        // Unset fields fall back to Theme::dark() — verifies the const
        // `match` arms in build() reproduce the unwrap_or semantics.
        let dark = Theme::dark();
        assert_eq!(_CONST_THEME.text, dark.text);
        assert_eq!(_CONST_THEME.border, dark.border);
        assert_eq!(_CONST_THEME.surface, dark.surface);
    }

    // --- regression: issue #110 builder_from / light_builder ---

    #[test]
    fn builder_from_preserves_base_fields() {
        // builder_from(base) must seed every field from `base`, so an
        // empty .build() yields a theme equal to `base`.
        let nord = Theme::nord();
        let t = Theme::builder_from(nord).build();
        assert_eq!(t.primary, nord.primary);
        assert_eq!(t.secondary, nord.secondary);
        assert_eq!(t.accent, nord.accent);
        assert_eq!(t.text, nord.text);
        assert_eq!(t.text_dim, nord.text_dim);
        assert_eq!(t.border, nord.border);
        assert_eq!(t.bg, nord.bg);
        assert_eq!(t.success, nord.success);
        assert_eq!(t.warning, nord.warning);
        assert_eq!(t.error, nord.error);
        assert_eq!(t.selected_bg, nord.selected_bg);
        assert_eq!(t.selected_fg, nord.selected_fg);
        assert_eq!(t.surface, nord.surface);
        assert_eq!(t.surface_hover, nord.surface_hover);
        assert_eq!(t.surface_text, nord.surface_text);
        assert_eq!(t.is_dark, nord.is_dark);
        assert_eq!(t.spacing, nord.spacing);
    }

    #[test]
    fn builder_from_overrides_only_specified_fields() {
        let t = Theme::builder_from(Theme::nord())
            .primary(Color::Rgb(255, 0, 0))
            .build();
        // Only primary changed; all other Nord fields preserved.
        assert_eq!(t.primary, Color::Rgb(255, 0, 0));
        assert_eq!(t.bg, Theme::nord().bg);
        assert_eq!(t.text, Theme::nord().text);
        assert_ne!(t.primary, Theme::nord().primary);
    }

    #[test]
    fn light_builder_starts_from_light_preset() {
        // Without builder_from, a plain Theme::builder() would inherit
        // dark() defaults — bg == Color::Reset — even when callers want
        // a light-base theme. light_builder() must keep light defaults.
        let t = Theme::light_builder()
            .primary(Color::Rgb(0, 100, 200))
            .build();
        let light = Theme::light();
        assert_eq!(t.primary, Color::Rgb(0, 100, 200));
        assert_eq!(t.bg, light.bg);
        assert_eq!(t.text, light.text);
        assert_eq!(t.surface, light.surface);
        assert!(!t.is_dark);
    }

    /// const-evaluation regression for builder_from + light_builder.
    const _CONST_LIGHT: Theme = Theme::light_builder().primary(Color::Rgb(1, 2, 3)).build();

    #[test]
    fn light_builder_is_const_evaluable() {
        assert_eq!(_CONST_LIGHT.primary, Color::Rgb(1, 2, 3));
        assert_eq!(_CONST_LIGHT.bg, Theme::light().bg);
        const { assert!(!_CONST_LIGHT.is_dark) };
    }
}