fresh-editor 0.1.74

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

/// Convert a ratatui Color to RGB values.
/// Returns None for Reset or Indexed colors.
pub fn color_to_rgb(color: Color) -> Option<(u8, u8, u8)> {
    match color {
        Color::Rgb(r, g, b) => Some((r, g, b)),
        Color::White => Some((255, 255, 255)),
        Color::Black => Some((0, 0, 0)),
        Color::Red => Some((205, 0, 0)),
        Color::Green => Some((0, 205, 0)),
        Color::Blue => Some((0, 0, 238)),
        Color::Yellow => Some((205, 205, 0)),
        Color::Magenta => Some((205, 0, 205)),
        Color::Cyan => Some((0, 205, 205)),
        Color::Gray => Some((229, 229, 229)),
        Color::DarkGray => Some((127, 127, 127)),
        Color::LightRed => Some((255, 0, 0)),
        Color::LightGreen => Some((0, 255, 0)),
        Color::LightBlue => Some((92, 92, 255)),
        Color::LightYellow => Some((255, 255, 0)),
        Color::LightMagenta => Some((255, 0, 255)),
        Color::LightCyan => Some((0, 255, 255)),
        Color::Reset | Color::Indexed(_) => None,
    }
}

/// Brighten a color by adding an amount to each RGB component.
/// Clamps values to 255.
fn brighten_color(color: Color, amount: u8) -> Color {
    if let Some((r, g, b)) = color_to_rgb(color) {
        Color::Rgb(
            r.saturating_add(amount),
            g.saturating_add(amount),
            b.saturating_add(amount),
        )
    } else {
        color
    }
}

/// Serializable color representation
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum ColorDef {
    /// RGB color as [r, g, b]
    Rgb(u8, u8, u8),
    /// Named color
    Named(String),
}

impl From<ColorDef> for Color {
    fn from(def: ColorDef) -> Self {
        match def {
            ColorDef::Rgb(r, g, b) => Color::Rgb(r, g, b),
            ColorDef::Named(name) => match name.as_str() {
                "Black" => Color::Black,
                "Red" => Color::Red,
                "Green" => Color::Green,
                "Yellow" => Color::Yellow,
                "Blue" => Color::Blue,
                "Magenta" => Color::Magenta,
                "Cyan" => Color::Cyan,
                "Gray" => Color::Gray,
                "DarkGray" => Color::DarkGray,
                "LightRed" => Color::LightRed,
                "LightGreen" => Color::LightGreen,
                "LightYellow" => Color::LightYellow,
                "LightBlue" => Color::LightBlue,
                "LightMagenta" => Color::LightMagenta,
                "LightCyan" => Color::LightCyan,
                "White" => Color::White,
                // Default/Reset uses the terminal's default color (preserves transparency)
                "Default" | "Reset" => Color::Reset,
                _ => Color::White, // Default fallback
            },
        }
    }
}

/// Serializable theme definition (matches JSON structure)
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct ThemeFile {
    /// Theme name
    pub name: String,
    /// Editor area colors
    pub editor: EditorColors,
    /// UI element colors (tabs, menus, status bar, etc.)
    pub ui: UiColors,
    /// Search result highlighting colors
    pub search: SearchColors,
    /// LSP diagnostic colors (errors, warnings, etc.)
    pub diagnostic: DiagnosticColors,
    /// Syntax highlighting colors
    pub syntax: SyntaxColors,
}

/// Editor area colors
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct EditorColors {
    /// Editor background color
    #[serde(default = "default_editor_bg")]
    pub bg: ColorDef,
    /// Default text color
    #[serde(default = "default_editor_fg")]
    pub fg: ColorDef,
    /// Cursor color
    #[serde(default = "default_cursor")]
    pub cursor: ColorDef,
    /// Cursor color in unfocused splits
    #[serde(default = "default_inactive_cursor")]
    pub inactive_cursor: ColorDef,
    /// Selected text background
    #[serde(default = "default_selection_bg")]
    pub selection_bg: ColorDef,
    /// Background of the line containing cursor
    #[serde(default = "default_current_line_bg")]
    pub current_line_bg: ColorDef,
    /// Line number text color
    #[serde(default = "default_line_number_fg")]
    pub line_number_fg: ColorDef,
    /// Line number gutter background
    #[serde(default = "default_line_number_bg")]
    pub line_number_bg: ColorDef,
    /// Diff added line background
    #[serde(default = "default_diff_add_bg")]
    pub diff_add_bg: ColorDef,
    /// Diff removed line background
    #[serde(default = "default_diff_remove_bg")]
    pub diff_remove_bg: ColorDef,
    /// Diff modified line background
    #[serde(default = "default_diff_modify_bg")]
    pub diff_modify_bg: ColorDef,
}

// Default editor colors (for minimal themes)
fn default_editor_bg() -> ColorDef {
    ColorDef::Rgb(30, 30, 30)
}
fn default_editor_fg() -> ColorDef {
    ColorDef::Rgb(212, 212, 212)
}
fn default_cursor() -> ColorDef {
    ColorDef::Rgb(255, 255, 255)
}
fn default_inactive_cursor() -> ColorDef {
    ColorDef::Named("DarkGray".to_string())
}
fn default_selection_bg() -> ColorDef {
    ColorDef::Rgb(38, 79, 120)
}
fn default_current_line_bg() -> ColorDef {
    ColorDef::Rgb(40, 40, 40)
}
fn default_line_number_fg() -> ColorDef {
    ColorDef::Rgb(100, 100, 100)
}
fn default_line_number_bg() -> ColorDef {
    ColorDef::Rgb(30, 30, 30)
}
fn default_diff_add_bg() -> ColorDef {
    ColorDef::Rgb(35, 60, 35) // Dark green
}
fn default_diff_remove_bg() -> ColorDef {
    ColorDef::Rgb(70, 35, 35) // Dark red
}
fn default_diff_modify_bg() -> ColorDef {
    ColorDef::Rgb(40, 38, 30) // Very subtle yellow tint, close to dark bg
}

/// UI element colors (tabs, menus, status bar, etc.)
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct UiColors {
    /// Active tab text color
    #[serde(default = "default_tab_active_fg")]
    pub tab_active_fg: ColorDef,
    /// Active tab background color
    #[serde(default = "default_tab_active_bg")]
    pub tab_active_bg: ColorDef,
    /// Inactive tab text color
    #[serde(default = "default_tab_inactive_fg")]
    pub tab_inactive_fg: ColorDef,
    /// Inactive tab background color
    #[serde(default = "default_tab_inactive_bg")]
    pub tab_inactive_bg: ColorDef,
    /// Tab bar separator color
    #[serde(default = "default_tab_separator_bg")]
    pub tab_separator_bg: ColorDef,
    /// Tab close button hover color
    #[serde(default = "default_tab_close_hover_fg")]
    pub tab_close_hover_fg: ColorDef,
    /// Tab hover background color
    #[serde(default = "default_tab_hover_bg")]
    pub tab_hover_bg: ColorDef,
    /// Menu bar background
    #[serde(default = "default_menu_bg")]
    pub menu_bg: ColorDef,
    /// Menu bar text color
    #[serde(default = "default_menu_fg")]
    pub menu_fg: ColorDef,
    /// Active menu item background
    #[serde(default = "default_menu_active_bg")]
    pub menu_active_bg: ColorDef,
    /// Active menu item text color
    #[serde(default = "default_menu_active_fg")]
    pub menu_active_fg: ColorDef,
    /// Dropdown menu background
    #[serde(default = "default_menu_dropdown_bg")]
    pub menu_dropdown_bg: ColorDef,
    /// Dropdown menu text color
    #[serde(default = "default_menu_dropdown_fg")]
    pub menu_dropdown_fg: ColorDef,
    /// Highlighted menu item background
    #[serde(default = "default_menu_highlight_bg")]
    pub menu_highlight_bg: ColorDef,
    /// Highlighted menu item text color
    #[serde(default = "default_menu_highlight_fg")]
    pub menu_highlight_fg: ColorDef,
    /// Menu border color
    #[serde(default = "default_menu_border_fg")]
    pub menu_border_fg: ColorDef,
    /// Menu separator line color
    #[serde(default = "default_menu_separator_fg")]
    pub menu_separator_fg: ColorDef,
    /// Menu item hover background
    #[serde(default = "default_menu_hover_bg")]
    pub menu_hover_bg: ColorDef,
    /// Menu item hover text color
    #[serde(default = "default_menu_hover_fg")]
    pub menu_hover_fg: ColorDef,
    /// Disabled menu item text color
    #[serde(default = "default_menu_disabled_fg")]
    pub menu_disabled_fg: ColorDef,
    /// Disabled menu item background
    #[serde(default = "default_menu_disabled_bg")]
    pub menu_disabled_bg: ColorDef,
    /// Status bar text color
    #[serde(default = "default_status_bar_fg")]
    pub status_bar_fg: ColorDef,
    /// Status bar background color
    #[serde(default = "default_status_bar_bg")]
    pub status_bar_bg: ColorDef,
    /// Command prompt text color
    #[serde(default = "default_prompt_fg")]
    pub prompt_fg: ColorDef,
    /// Command prompt background
    #[serde(default = "default_prompt_bg")]
    pub prompt_bg: ColorDef,
    /// Prompt selected text color
    #[serde(default = "default_prompt_selection_fg")]
    pub prompt_selection_fg: ColorDef,
    /// Prompt selection background
    #[serde(default = "default_prompt_selection_bg")]
    pub prompt_selection_bg: ColorDef,
    /// Popup window border color
    #[serde(default = "default_popup_border_fg")]
    pub popup_border_fg: ColorDef,
    /// Popup window background
    #[serde(default = "default_popup_bg")]
    pub popup_bg: ColorDef,
    /// Popup selected item background
    #[serde(default = "default_popup_selection_bg")]
    pub popup_selection_bg: ColorDef,
    /// Popup window text color
    #[serde(default = "default_popup_text_fg")]
    pub popup_text_fg: ColorDef,
    /// Autocomplete suggestion background
    #[serde(default = "default_suggestion_bg")]
    pub suggestion_bg: ColorDef,
    /// Selected suggestion background
    #[serde(default = "default_suggestion_selected_bg")]
    pub suggestion_selected_bg: ColorDef,
    /// Help panel background
    #[serde(default = "default_help_bg")]
    pub help_bg: ColorDef,
    /// Help panel text color
    #[serde(default = "default_help_fg")]
    pub help_fg: ColorDef,
    /// Help keybinding text color
    #[serde(default = "default_help_key_fg")]
    pub help_key_fg: ColorDef,
    /// Help panel separator color
    #[serde(default = "default_help_separator_fg")]
    pub help_separator_fg: ColorDef,
    /// Help indicator text color
    #[serde(default = "default_help_indicator_fg")]
    pub help_indicator_fg: ColorDef,
    /// Help indicator background
    #[serde(default = "default_help_indicator_bg")]
    pub help_indicator_bg: ColorDef,
    /// Inline code block background
    #[serde(default = "default_inline_code_bg")]
    pub inline_code_bg: ColorDef,
    /// Split pane separator color
    #[serde(default = "default_split_separator_fg")]
    pub split_separator_fg: ColorDef,
    /// Split separator hover color
    #[serde(default = "default_split_separator_hover_fg")]
    pub split_separator_hover_fg: ColorDef,
    /// Scrollbar track color
    #[serde(default = "default_scrollbar_track_fg")]
    pub scrollbar_track_fg: ColorDef,
    /// Scrollbar thumb color
    #[serde(default = "default_scrollbar_thumb_fg")]
    pub scrollbar_thumb_fg: ColorDef,
    /// Scrollbar track hover color
    #[serde(default = "default_scrollbar_track_hover_fg")]
    pub scrollbar_track_hover_fg: ColorDef,
    /// Scrollbar thumb hover color
    #[serde(default = "default_scrollbar_thumb_hover_fg")]
    pub scrollbar_thumb_hover_fg: ColorDef,
    /// Compose mode margin background
    #[serde(default = "default_compose_margin_bg")]
    pub compose_margin_bg: ColorDef,
    /// Word under cursor highlight
    #[serde(default = "default_semantic_highlight_bg")]
    pub semantic_highlight_bg: ColorDef,
    /// Embedded terminal background (use Default for transparency)
    #[serde(default = "default_terminal_bg")]
    pub terminal_bg: ColorDef,
    /// Embedded terminal default text color
    #[serde(default = "default_terminal_fg")]
    pub terminal_fg: ColorDef,
    /// Warning indicator background in status bar
    #[serde(default = "default_status_warning_indicator_bg")]
    pub status_warning_indicator_bg: ColorDef,
    /// Warning indicator text color in status bar
    #[serde(default = "default_status_warning_indicator_fg")]
    pub status_warning_indicator_fg: ColorDef,
    /// Error indicator background in status bar
    #[serde(default = "default_status_error_indicator_bg")]
    pub status_error_indicator_bg: ColorDef,
    /// Error indicator text color in status bar
    #[serde(default = "default_status_error_indicator_fg")]
    pub status_error_indicator_fg: ColorDef,
    /// Warning indicator hover background
    #[serde(default = "default_status_warning_indicator_hover_bg")]
    pub status_warning_indicator_hover_bg: ColorDef,
    /// Warning indicator hover text color
    #[serde(default = "default_status_warning_indicator_hover_fg")]
    pub status_warning_indicator_hover_fg: ColorDef,
    /// Error indicator hover background
    #[serde(default = "default_status_error_indicator_hover_bg")]
    pub status_error_indicator_hover_bg: ColorDef,
    /// Error indicator hover text color
    #[serde(default = "default_status_error_indicator_hover_fg")]
    pub status_error_indicator_hover_fg: ColorDef,
    /// Tab drop zone background during drag
    #[serde(default = "default_tab_drop_zone_bg")]
    pub tab_drop_zone_bg: ColorDef,
    /// Tab drop zone border during drag
    #[serde(default = "default_tab_drop_zone_border")]
    pub tab_drop_zone_border: ColorDef,
}

// Default tab close hover color (for backward compatibility with existing themes)
// Default tab colors (for minimal themes)
fn default_tab_active_fg() -> ColorDef {
    ColorDef::Named("Yellow".to_string())
}
fn default_tab_active_bg() -> ColorDef {
    ColorDef::Named("Blue".to_string())
}
fn default_tab_inactive_fg() -> ColorDef {
    ColorDef::Named("White".to_string())
}
fn default_tab_inactive_bg() -> ColorDef {
    ColorDef::Named("DarkGray".to_string())
}
fn default_tab_separator_bg() -> ColorDef {
    ColorDef::Named("Black".to_string())
}
fn default_tab_close_hover_fg() -> ColorDef {
    ColorDef::Rgb(255, 100, 100) // Red-ish color for close button hover
}
fn default_tab_hover_bg() -> ColorDef {
    ColorDef::Rgb(70, 70, 75) // Slightly lighter than inactive tab bg for hover
}

// Default menu colors (for backward compatibility with existing themes)
fn default_menu_bg() -> ColorDef {
    ColorDef::Rgb(60, 60, 65)
}
fn default_menu_fg() -> ColorDef {
    ColorDef::Rgb(220, 220, 220)
}
fn default_menu_active_bg() -> ColorDef {
    ColorDef::Rgb(60, 60, 60)
}
fn default_menu_active_fg() -> ColorDef {
    ColorDef::Rgb(255, 255, 255)
}
fn default_menu_dropdown_bg() -> ColorDef {
    ColorDef::Rgb(50, 50, 50)
}
fn default_menu_dropdown_fg() -> ColorDef {
    ColorDef::Rgb(220, 220, 220)
}
fn default_menu_highlight_bg() -> ColorDef {
    ColorDef::Rgb(70, 130, 180)
}
fn default_menu_highlight_fg() -> ColorDef {
    ColorDef::Rgb(255, 255, 255)
}
fn default_menu_border_fg() -> ColorDef {
    ColorDef::Rgb(100, 100, 100)
}
fn default_menu_separator_fg() -> ColorDef {
    ColorDef::Rgb(80, 80, 80)
}
fn default_menu_hover_bg() -> ColorDef {
    ColorDef::Rgb(55, 55, 55)
}
fn default_menu_hover_fg() -> ColorDef {
    ColorDef::Rgb(255, 255, 255)
}
fn default_menu_disabled_fg() -> ColorDef {
    ColorDef::Rgb(100, 100, 100) // Gray for disabled items
}
fn default_menu_disabled_bg() -> ColorDef {
    ColorDef::Rgb(50, 50, 50) // Same as dropdown bg
}
// Default status bar colors
fn default_status_bar_fg() -> ColorDef {
    ColorDef::Named("White".to_string())
}
fn default_status_bar_bg() -> ColorDef {
    ColorDef::Named("DarkGray".to_string())
}

// Default prompt colors
fn default_prompt_fg() -> ColorDef {
    ColorDef::Named("White".to_string())
}
fn default_prompt_bg() -> ColorDef {
    ColorDef::Named("Black".to_string())
}
fn default_prompt_selection_fg() -> ColorDef {
    ColorDef::Named("White".to_string())
}
fn default_prompt_selection_bg() -> ColorDef {
    ColorDef::Rgb(58, 79, 120)
}

// Default popup colors
fn default_popup_border_fg() -> ColorDef {
    ColorDef::Named("Gray".to_string())
}
fn default_popup_bg() -> ColorDef {
    ColorDef::Rgb(30, 30, 30)
}
fn default_popup_selection_bg() -> ColorDef {
    ColorDef::Rgb(58, 79, 120)
}
fn default_popup_text_fg() -> ColorDef {
    ColorDef::Named("White".to_string())
}

// Default suggestion colors
fn default_suggestion_bg() -> ColorDef {
    ColorDef::Rgb(30, 30, 30)
}
fn default_suggestion_selected_bg() -> ColorDef {
    ColorDef::Rgb(58, 79, 120)
}

// Default help colors
fn default_help_bg() -> ColorDef {
    ColorDef::Named("Black".to_string())
}
fn default_help_fg() -> ColorDef {
    ColorDef::Named("White".to_string())
}
fn default_help_key_fg() -> ColorDef {
    ColorDef::Named("Cyan".to_string())
}
fn default_help_separator_fg() -> ColorDef {
    ColorDef::Named("DarkGray".to_string())
}
fn default_help_indicator_fg() -> ColorDef {
    ColorDef::Named("Red".to_string())
}
fn default_help_indicator_bg() -> ColorDef {
    ColorDef::Named("Black".to_string())
}

fn default_inline_code_bg() -> ColorDef {
    ColorDef::Named("DarkGray".to_string())
}

// Default split separator colors
fn default_split_separator_fg() -> ColorDef {
    ColorDef::Rgb(100, 100, 100)
}
fn default_split_separator_hover_fg() -> ColorDef {
    ColorDef::Rgb(100, 149, 237) // Cornflower blue for visibility
}
fn default_scrollbar_track_fg() -> ColorDef {
    ColorDef::Named("DarkGray".to_string())
}
fn default_scrollbar_thumb_fg() -> ColorDef {
    ColorDef::Named("Gray".to_string())
}
fn default_scrollbar_track_hover_fg() -> ColorDef {
    ColorDef::Named("Gray".to_string())
}
fn default_scrollbar_thumb_hover_fg() -> ColorDef {
    ColorDef::Named("White".to_string())
}
fn default_compose_margin_bg() -> ColorDef {
    ColorDef::Rgb(18, 18, 18) // Darker than editor_bg for "desk" effect
}
fn default_semantic_highlight_bg() -> ColorDef {
    ColorDef::Rgb(60, 60, 80) // Subtle dark highlight for word occurrences
}
fn default_terminal_bg() -> ColorDef {
    ColorDef::Named("Default".to_string()) // Use terminal's default background (preserves transparency)
}
fn default_terminal_fg() -> ColorDef {
    ColorDef::Named("Default".to_string()) // Use terminal's default foreground
}
fn default_status_warning_indicator_bg() -> ColorDef {
    ColorDef::Rgb(181, 137, 0) // Solarized yellow/amber - noticeable but not harsh
}
fn default_status_warning_indicator_fg() -> ColorDef {
    ColorDef::Rgb(0, 0, 0) // Black text on amber background
}
fn default_status_error_indicator_bg() -> ColorDef {
    ColorDef::Rgb(220, 50, 47) // Solarized red - clearly an error
}
fn default_status_error_indicator_fg() -> ColorDef {
    ColorDef::Rgb(255, 255, 255) // White text on red background
}
fn default_status_warning_indicator_hover_bg() -> ColorDef {
    ColorDef::Rgb(211, 167, 30) // Lighter amber for hover
}
fn default_status_warning_indicator_hover_fg() -> ColorDef {
    ColorDef::Rgb(0, 0, 0) // Black text on hover
}
fn default_status_error_indicator_hover_bg() -> ColorDef {
    ColorDef::Rgb(250, 80, 77) // Lighter red for hover
}
fn default_status_error_indicator_hover_fg() -> ColorDef {
    ColorDef::Rgb(255, 255, 255) // White text on hover
}
fn default_tab_drop_zone_bg() -> ColorDef {
    ColorDef::Rgb(70, 130, 180) // Steel blue with transparency effect
}
fn default_tab_drop_zone_border() -> ColorDef {
    ColorDef::Rgb(100, 149, 237) // Cornflower blue for border
}

/// Search result highlighting colors
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct SearchColors {
    /// Search match background color
    #[serde(default = "default_search_match_bg")]
    pub match_bg: ColorDef,
    /// Search match text color
    #[serde(default = "default_search_match_fg")]
    pub match_fg: ColorDef,
}

// Default search colors
fn default_search_match_bg() -> ColorDef {
    ColorDef::Rgb(100, 100, 20)
}
fn default_search_match_fg() -> ColorDef {
    ColorDef::Rgb(255, 255, 255)
}

/// LSP diagnostic colors (errors, warnings, etc.)
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct DiagnosticColors {
    /// Error message text color
    #[serde(default = "default_diagnostic_error_fg")]
    pub error_fg: ColorDef,
    /// Error highlight background
    #[serde(default = "default_diagnostic_error_bg")]
    pub error_bg: ColorDef,
    /// Warning message text color
    #[serde(default = "default_diagnostic_warning_fg")]
    pub warning_fg: ColorDef,
    /// Warning highlight background
    #[serde(default = "default_diagnostic_warning_bg")]
    pub warning_bg: ColorDef,
    /// Info message text color
    #[serde(default = "default_diagnostic_info_fg")]
    pub info_fg: ColorDef,
    /// Info highlight background
    #[serde(default = "default_diagnostic_info_bg")]
    pub info_bg: ColorDef,
    /// Hint message text color
    #[serde(default = "default_diagnostic_hint_fg")]
    pub hint_fg: ColorDef,
    /// Hint highlight background
    #[serde(default = "default_diagnostic_hint_bg")]
    pub hint_bg: ColorDef,
}

// Default diagnostic colors
fn default_diagnostic_error_fg() -> ColorDef {
    ColorDef::Named("Red".to_string())
}
fn default_diagnostic_error_bg() -> ColorDef {
    ColorDef::Rgb(60, 20, 20)
}
fn default_diagnostic_warning_fg() -> ColorDef {
    ColorDef::Named("Yellow".to_string())
}
fn default_diagnostic_warning_bg() -> ColorDef {
    ColorDef::Rgb(60, 50, 0)
}
fn default_diagnostic_info_fg() -> ColorDef {
    ColorDef::Named("Blue".to_string())
}
fn default_diagnostic_info_bg() -> ColorDef {
    ColorDef::Rgb(0, 30, 60)
}
fn default_diagnostic_hint_fg() -> ColorDef {
    ColorDef::Named("Gray".to_string())
}
fn default_diagnostic_hint_bg() -> ColorDef {
    ColorDef::Rgb(30, 30, 30)
}

/// Syntax highlighting colors
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct SyntaxColors {
    /// Language keywords (if, for, fn, etc.)
    #[serde(default = "default_syntax_keyword")]
    pub keyword: ColorDef,
    /// String literals
    #[serde(default = "default_syntax_string")]
    pub string: ColorDef,
    /// Code comments
    #[serde(default = "default_syntax_comment")]
    pub comment: ColorDef,
    /// Function names
    #[serde(default = "default_syntax_function")]
    pub function: ColorDef,
    /// Type names
    #[serde(rename = "type", default = "default_syntax_type")]
    pub type_: ColorDef,
    /// Variable names
    #[serde(default = "default_syntax_variable")]
    pub variable: ColorDef,
    /// Constants and literals
    #[serde(default = "default_syntax_constant")]
    pub constant: ColorDef,
    /// Operators (+, -, =, etc.)
    #[serde(default = "default_syntax_operator")]
    pub operator: ColorDef,
}

// Default syntax colors (VSCode Dark+ inspired)
fn default_syntax_keyword() -> ColorDef {
    ColorDef::Rgb(86, 156, 214)
}
fn default_syntax_string() -> ColorDef {
    ColorDef::Rgb(206, 145, 120)
}
fn default_syntax_comment() -> ColorDef {
    ColorDef::Rgb(106, 153, 85)
}
fn default_syntax_function() -> ColorDef {
    ColorDef::Rgb(220, 220, 170)
}
fn default_syntax_type() -> ColorDef {
    ColorDef::Rgb(78, 201, 176)
}
fn default_syntax_variable() -> ColorDef {
    ColorDef::Rgb(156, 220, 254)
}
fn default_syntax_constant() -> ColorDef {
    ColorDef::Rgb(79, 193, 255)
}
fn default_syntax_operator() -> ColorDef {
    ColorDef::Rgb(212, 212, 212)
}

/// Comprehensive theme structure with all UI colors
#[derive(Debug, Clone)]
pub struct Theme {
    /// Theme name (e.g., "dark", "light", "high-contrast")
    pub name: String,

    // Editor colors
    pub editor_bg: Color,
    pub editor_fg: Color,
    pub cursor: Color,
    pub inactive_cursor: Color,
    pub selection_bg: Color,
    pub current_line_bg: Color,
    pub line_number_fg: Color,
    pub line_number_bg: Color,

    // Diff highlighting colors
    pub diff_add_bg: Color,
    pub diff_remove_bg: Color,
    pub diff_modify_bg: Color,
    /// Brighter background for inline diff highlighting on added content
    pub diff_add_highlight_bg: Color,
    /// Brighter background for inline diff highlighting on removed content
    pub diff_remove_highlight_bg: Color,

    // UI element colors
    pub tab_active_fg: Color,
    pub tab_active_bg: Color,
    pub tab_inactive_fg: Color,
    pub tab_inactive_bg: Color,
    pub tab_separator_bg: Color,
    pub tab_close_hover_fg: Color,
    pub tab_hover_bg: Color,

    // Menu bar colors
    pub menu_bg: Color,
    pub menu_fg: Color,
    pub menu_active_bg: Color,
    pub menu_active_fg: Color,
    pub menu_dropdown_bg: Color,
    pub menu_dropdown_fg: Color,
    pub menu_highlight_bg: Color,
    pub menu_highlight_fg: Color,
    pub menu_border_fg: Color,
    pub menu_separator_fg: Color,
    pub menu_hover_bg: Color,
    pub menu_hover_fg: Color,
    pub menu_disabled_fg: Color,
    pub menu_disabled_bg: Color,

    pub status_bar_fg: Color,
    pub status_bar_bg: Color,
    pub prompt_fg: Color,
    pub prompt_bg: Color,
    pub prompt_selection_fg: Color,
    pub prompt_selection_bg: Color,

    pub popup_border_fg: Color,
    pub popup_bg: Color,
    pub popup_selection_bg: Color,
    pub popup_text_fg: Color,

    pub suggestion_bg: Color,
    pub suggestion_selected_bg: Color,

    pub help_bg: Color,
    pub help_fg: Color,
    pub help_key_fg: Color,
    pub help_separator_fg: Color,

    pub help_indicator_fg: Color,
    pub help_indicator_bg: Color,

    /// Background color for inline code in help popups
    pub inline_code_bg: Color,

    pub split_separator_fg: Color,
    pub split_separator_hover_fg: Color,

    // Scrollbar colors
    pub scrollbar_track_fg: Color,
    pub scrollbar_thumb_fg: Color,
    pub scrollbar_track_hover_fg: Color,
    pub scrollbar_thumb_hover_fg: Color,

    // Compose mode colors
    pub compose_margin_bg: Color,

    // Semantic highlighting (word under cursor)
    pub semantic_highlight_bg: Color,

    // Terminal colors (for embedded terminal buffers)
    pub terminal_bg: Color,
    pub terminal_fg: Color,

    // Status bar warning/error indicator colors
    pub status_warning_indicator_bg: Color,
    pub status_warning_indicator_fg: Color,
    pub status_error_indicator_bg: Color,
    pub status_error_indicator_fg: Color,
    pub status_warning_indicator_hover_bg: Color,
    pub status_warning_indicator_hover_fg: Color,
    pub status_error_indicator_hover_bg: Color,
    pub status_error_indicator_hover_fg: Color,

    // Tab drag-and-drop colors
    pub tab_drop_zone_bg: Color,
    pub tab_drop_zone_border: Color,

    // Search colors
    pub search_match_bg: Color,
    pub search_match_fg: Color,

    // Diagnostic colors
    pub diagnostic_error_fg: Color,
    pub diagnostic_error_bg: Color,
    pub diagnostic_warning_fg: Color,
    pub diagnostic_warning_bg: Color,
    pub diagnostic_info_fg: Color,
    pub diagnostic_info_bg: Color,
    pub diagnostic_hint_fg: Color,
    pub diagnostic_hint_bg: Color,

    // Syntax highlighting colors
    pub syntax_keyword: Color,
    pub syntax_string: Color,
    pub syntax_comment: Color,
    pub syntax_function: Color,
    pub syntax_type: Color,
    pub syntax_variable: Color,
    pub syntax_constant: Color,
    pub syntax_operator: Color,
}

impl From<ThemeFile> for Theme {
    fn from(file: ThemeFile) -> Self {
        Self {
            name: file.name,
            editor_bg: file.editor.bg.into(),
            editor_fg: file.editor.fg.into(),
            cursor: file.editor.cursor.into(),
            inactive_cursor: file.editor.inactive_cursor.into(),
            selection_bg: file.editor.selection_bg.into(),
            current_line_bg: file.editor.current_line_bg.into(),
            line_number_fg: file.editor.line_number_fg.into(),
            line_number_bg: file.editor.line_number_bg.into(),
            diff_add_bg: file.editor.diff_add_bg.clone().into(),
            diff_remove_bg: file.editor.diff_remove_bg.clone().into(),
            diff_modify_bg: file.editor.diff_modify_bg.into(),
            // Compute brighter highlight colors from base diff colors
            diff_add_highlight_bg: brighten_color(file.editor.diff_add_bg.into(), 40),
            diff_remove_highlight_bg: brighten_color(file.editor.diff_remove_bg.into(), 40),
            tab_active_fg: file.ui.tab_active_fg.into(),
            tab_active_bg: file.ui.tab_active_bg.into(),
            tab_inactive_fg: file.ui.tab_inactive_fg.into(),
            tab_inactive_bg: file.ui.tab_inactive_bg.into(),
            tab_separator_bg: file.ui.tab_separator_bg.into(),
            tab_close_hover_fg: file.ui.tab_close_hover_fg.into(),
            tab_hover_bg: file.ui.tab_hover_bg.into(),
            menu_bg: file.ui.menu_bg.into(),
            menu_fg: file.ui.menu_fg.into(),
            menu_active_bg: file.ui.menu_active_bg.into(),
            menu_active_fg: file.ui.menu_active_fg.into(),
            menu_dropdown_bg: file.ui.menu_dropdown_bg.into(),
            menu_dropdown_fg: file.ui.menu_dropdown_fg.into(),
            menu_highlight_bg: file.ui.menu_highlight_bg.into(),
            menu_highlight_fg: file.ui.menu_highlight_fg.into(),
            menu_border_fg: file.ui.menu_border_fg.into(),
            menu_separator_fg: file.ui.menu_separator_fg.into(),
            menu_hover_bg: file.ui.menu_hover_bg.into(),
            menu_hover_fg: file.ui.menu_hover_fg.into(),
            menu_disabled_fg: file.ui.menu_disabled_fg.into(),
            menu_disabled_bg: file.ui.menu_disabled_bg.into(),
            status_bar_fg: file.ui.status_bar_fg.into(),
            status_bar_bg: file.ui.status_bar_bg.into(),
            prompt_fg: file.ui.prompt_fg.into(),
            prompt_bg: file.ui.prompt_bg.into(),
            prompt_selection_fg: file.ui.prompt_selection_fg.into(),
            prompt_selection_bg: file.ui.prompt_selection_bg.into(),
            popup_border_fg: file.ui.popup_border_fg.into(),
            popup_bg: file.ui.popup_bg.into(),
            popup_selection_bg: file.ui.popup_selection_bg.into(),
            popup_text_fg: file.ui.popup_text_fg.into(),
            suggestion_bg: file.ui.suggestion_bg.into(),
            suggestion_selected_bg: file.ui.suggestion_selected_bg.into(),
            help_bg: file.ui.help_bg.into(),
            help_fg: file.ui.help_fg.into(),
            help_key_fg: file.ui.help_key_fg.into(),
            help_separator_fg: file.ui.help_separator_fg.into(),
            help_indicator_fg: file.ui.help_indicator_fg.into(),
            help_indicator_bg: file.ui.help_indicator_bg.into(),
            inline_code_bg: file.ui.inline_code_bg.into(),
            split_separator_fg: file.ui.split_separator_fg.into(),
            split_separator_hover_fg: file.ui.split_separator_hover_fg.into(),
            scrollbar_track_fg: file.ui.scrollbar_track_fg.into(),
            scrollbar_thumb_fg: file.ui.scrollbar_thumb_fg.into(),
            scrollbar_track_hover_fg: file.ui.scrollbar_track_hover_fg.into(),
            scrollbar_thumb_hover_fg: file.ui.scrollbar_thumb_hover_fg.into(),
            compose_margin_bg: file.ui.compose_margin_bg.into(),
            semantic_highlight_bg: file.ui.semantic_highlight_bg.into(),
            terminal_bg: file.ui.terminal_bg.into(),
            terminal_fg: file.ui.terminal_fg.into(),
            status_warning_indicator_bg: file.ui.status_warning_indicator_bg.into(),
            status_warning_indicator_fg: file.ui.status_warning_indicator_fg.into(),
            status_error_indicator_bg: file.ui.status_error_indicator_bg.into(),
            status_error_indicator_fg: file.ui.status_error_indicator_fg.into(),
            status_warning_indicator_hover_bg: file.ui.status_warning_indicator_hover_bg.into(),
            status_warning_indicator_hover_fg: file.ui.status_warning_indicator_hover_fg.into(),
            status_error_indicator_hover_bg: file.ui.status_error_indicator_hover_bg.into(),
            status_error_indicator_hover_fg: file.ui.status_error_indicator_hover_fg.into(),
            tab_drop_zone_bg: file.ui.tab_drop_zone_bg.into(),
            tab_drop_zone_border: file.ui.tab_drop_zone_border.into(),
            search_match_bg: file.search.match_bg.into(),
            search_match_fg: file.search.match_fg.into(),
            diagnostic_error_fg: file.diagnostic.error_fg.into(),
            diagnostic_error_bg: file.diagnostic.error_bg.into(),
            diagnostic_warning_fg: file.diagnostic.warning_fg.into(),
            diagnostic_warning_bg: file.diagnostic.warning_bg.into(),
            diagnostic_info_fg: file.diagnostic.info_fg.into(),
            diagnostic_info_bg: file.diagnostic.info_bg.into(),
            diagnostic_hint_fg: file.diagnostic.hint_fg.into(),
            diagnostic_hint_bg: file.diagnostic.hint_bg.into(),
            syntax_keyword: file.syntax.keyword.into(),
            syntax_string: file.syntax.string.into(),
            syntax_comment: file.syntax.comment.into(),
            syntax_function: file.syntax.function.into(),
            syntax_type: file.syntax.type_.into(),
            syntax_variable: file.syntax.variable.into(),
            syntax_constant: file.syntax.constant.into(),
            syntax_operator: file.syntax.operator.into(),
        }
    }
}

impl Theme {
    /// Load theme from a JSON file
    fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, String> {
        let content = std::fs::read_to_string(path)
            .map_err(|e| format!("Failed to read theme file: {}", e))?;
        let theme_file: ThemeFile = serde_json::from_str(&content)
            .map_err(|e| format!("Failed to parse theme file: {}", e))?;
        Ok(theme_file.into())
    }

    /// Load builtin theme from the themes directory
    fn load_builtin_theme(name: &str) -> Option<Self> {
        // Build list of paths to search
        let mut theme_paths = vec![
            format!("themes/{}.json", name),
            format!("../themes/{}.json", name),
            format!("../../themes/{}.json", name),
        ];

        // Also check user config themes directory
        if let Some(config_dir) = dirs::config_dir() {
            let user_theme_path = config_dir
                .join("fresh")
                .join("themes")
                .join(format!("{}.json", name));
            theme_paths.insert(0, user_theme_path.to_string_lossy().to_string());
        }

        for path in &theme_paths {
            if let Ok(theme) = Self::from_file(path) {
                return Some(theme);
            }
        }

        None
    }

    /// Default dark theme (VSCode Dark+ inspired)
    /// Fallback if JSON file cannot be loaded
    pub fn dark() -> Self {
        Self {
            name: "dark".to_string(),

            // Editor colors
            editor_bg: Color::Rgb(30, 30, 30),
            editor_fg: Color::Rgb(212, 212, 212),
            cursor: Color::Rgb(255, 255, 255),
            inactive_cursor: Color::Rgb(100, 100, 100),
            selection_bg: Color::Rgb(38, 79, 120),
            current_line_bg: Color::Rgb(40, 40, 40),
            line_number_fg: Color::Rgb(100, 100, 100),
            line_number_bg: Color::Rgb(30, 30, 30),

            // Diff highlighting colors
            diff_add_bg: Color::Rgb(35, 60, 35),    // Dark green
            diff_remove_bg: Color::Rgb(70, 35, 35), // Dark red
            diff_modify_bg: Color::Rgb(40, 38, 30), // Subtle yellow tint, close to editor_bg
            diff_add_highlight_bg: Color::Rgb(60, 110, 60), // Brighter green for inline
            diff_remove_highlight_bg: Color::Rgb(120, 50, 50), // Brighter red for inline

            // UI element colors
            tab_active_fg: Color::Yellow,
            tab_active_bg: Color::Blue,
            tab_inactive_fg: Color::White,
            tab_inactive_bg: Color::DarkGray,
            tab_separator_bg: Color::Rgb(45, 45, 48),
            tab_close_hover_fg: Color::Rgb(255, 100, 100),
            tab_hover_bg: Color::Rgb(70, 70, 75),

            // Menu bar colors
            menu_bg: Color::Rgb(60, 60, 65),
            menu_fg: Color::Rgb(220, 220, 220),
            menu_active_bg: Color::Rgb(60, 60, 60),
            menu_active_fg: Color::Rgb(255, 255, 255),
            menu_dropdown_bg: Color::Rgb(50, 50, 50),
            menu_dropdown_fg: Color::Rgb(220, 220, 220),
            menu_highlight_bg: Color::Rgb(70, 130, 180),
            menu_highlight_fg: Color::Rgb(255, 255, 255),
            menu_border_fg: Color::Rgb(100, 100, 100),
            menu_separator_fg: Color::Rgb(80, 80, 80),
            menu_hover_bg: Color::Rgb(55, 55, 55),
            menu_hover_fg: Color::Rgb(255, 255, 255),
            menu_disabled_fg: Color::Rgb(100, 100, 100), // Gray for disabled items
            menu_disabled_bg: Color::Rgb(50, 50, 50),

            status_bar_fg: Color::White,
            status_bar_bg: Color::Rgb(30, 30, 30), // Darker than DarkGray
            prompt_fg: Color::White,
            prompt_bg: Color::Rgb(20, 20, 20), // Very dark
            prompt_selection_fg: Color::White,
            prompt_selection_bg: Color::Rgb(58, 79, 120), // Blue selection

            popup_border_fg: Color::Gray,
            popup_bg: Color::Rgb(30, 30, 30),
            popup_selection_bg: Color::Rgb(58, 79, 120),
            popup_text_fg: Color::White,

            suggestion_bg: Color::Rgb(30, 30, 30),
            suggestion_selected_bg: Color::Rgb(58, 79, 120),

            help_bg: Color::Black,
            help_fg: Color::White,
            help_key_fg: Color::Cyan,
            help_separator_fg: Color::DarkGray,

            help_indicator_fg: Color::Red,
            help_indicator_bg: Color::Black,

            inline_code_bg: Color::Rgb(60, 60, 60), // Dark gray for code blocks

            split_separator_fg: Color::Rgb(100, 100, 100),
            split_separator_hover_fg: Color::Rgb(100, 149, 237), // Cornflower blue

            // Scrollbar colors
            scrollbar_track_fg: Color::DarkGray,
            scrollbar_thumb_fg: Color::Gray,
            scrollbar_track_hover_fg: Color::Gray,
            scrollbar_thumb_hover_fg: Color::White,

            // Compose mode colors
            compose_margin_bg: Color::Rgb(18, 18, 18), // Darker than editor_bg for "desk" effect

            // Semantic highlighting (word under cursor)
            semantic_highlight_bg: Color::Rgb(60, 60, 80), // Subtle dark highlight

            // Terminal colors (use terminal's default colors to preserve transparency)
            terminal_bg: Color::Reset,
            terminal_fg: Color::Reset,

            // Status bar warning/error indicator colors
            status_warning_indicator_bg: Color::Rgb(181, 137, 0), // Solarized amber
            status_warning_indicator_fg: Color::Rgb(0, 0, 0),     // Black text
            status_error_indicator_bg: Color::Rgb(220, 50, 47),   // Solarized red
            status_error_indicator_fg: Color::Rgb(255, 255, 255), // White text
            status_warning_indicator_hover_bg: Color::Rgb(211, 167, 30), // Lighter amber
            status_warning_indicator_hover_fg: Color::Rgb(0, 0, 0),
            status_error_indicator_hover_bg: Color::Rgb(250, 80, 77), // Lighter red
            status_error_indicator_hover_fg: Color::Rgb(255, 255, 255),

            // Tab drag-and-drop colors
            tab_drop_zone_bg: Color::Rgb(70, 130, 180), // Steel blue
            tab_drop_zone_border: Color::Rgb(100, 149, 237), // Cornflower blue

            // Search colors
            search_match_bg: Color::Rgb(100, 100, 20), // Yellow-brown highlight
            search_match_fg: Color::Rgb(255, 255, 255),

            // Diagnostic colors
            diagnostic_error_fg: Color::Red,
            diagnostic_error_bg: Color::Rgb(60, 20, 20),
            diagnostic_warning_fg: Color::Yellow,
            diagnostic_warning_bg: Color::Rgb(60, 50, 0),
            diagnostic_info_fg: Color::Blue,
            diagnostic_info_bg: Color::Rgb(0, 30, 60),
            diagnostic_hint_fg: Color::Gray,
            diagnostic_hint_bg: Color::Rgb(30, 30, 30),

            // Syntax highlighting colors (VSCode Dark+ palette)
            syntax_keyword: Color::Rgb(86, 156, 214),
            syntax_string: Color::Rgb(206, 145, 120),
            syntax_comment: Color::Rgb(106, 153, 85),
            syntax_function: Color::Rgb(220, 220, 170),
            syntax_type: Color::Rgb(78, 201, 176),
            syntax_variable: Color::Rgb(156, 220, 254),
            syntax_constant: Color::Rgb(79, 193, 255),
            syntax_operator: Color::Rgb(212, 212, 212),
        }
    }

    /// Light theme (VSCode Light+ inspired)
    pub fn light() -> Self {
        Self {
            name: "light".to_string(),

            // Editor colors
            editor_bg: Color::Rgb(255, 255, 255),
            editor_fg: Color::Rgb(0, 0, 0),
            cursor: Color::Rgb(0, 0, 0),
            inactive_cursor: Color::Rgb(180, 180, 180),
            selection_bg: Color::Rgb(173, 214, 255),
            current_line_bg: Color::Rgb(245, 245, 245),
            line_number_fg: Color::Rgb(140, 140, 140),
            line_number_bg: Color::Rgb(255, 255, 255),

            // Diff highlighting colors
            diff_add_bg: Color::Rgb(200, 255, 200), // Light green
            diff_remove_bg: Color::Rgb(255, 200, 200), // Light red
            diff_modify_bg: Color::Rgb(255, 252, 240), // Subtle cream, close to white
            diff_add_highlight_bg: Color::Rgb(140, 220, 140), // Darker green for inline (contrast on light bg)
            diff_remove_highlight_bg: Color::Rgb(220, 140, 140), // Darker red for inline

            // UI element colors
            tab_active_fg: Color::Rgb(40, 40, 40),
            tab_active_bg: Color::Rgb(255, 255, 255),
            tab_inactive_fg: Color::Rgb(100, 100, 100),
            tab_inactive_bg: Color::Rgb(230, 230, 230),
            tab_separator_bg: Color::Rgb(200, 200, 200),
            tab_close_hover_fg: Color::Rgb(220, 50, 50),
            tab_hover_bg: Color::Rgb(210, 210, 210),

            // Menu bar colors - dark text on light backgrounds
            menu_bg: Color::Rgb(245, 245, 245),
            menu_fg: Color::Rgb(30, 30, 30),
            menu_active_bg: Color::Rgb(225, 225, 225),
            menu_active_fg: Color::Rgb(0, 0, 0),
            menu_dropdown_bg: Color::Rgb(248, 248, 248),
            menu_dropdown_fg: Color::Rgb(30, 30, 30),
            menu_highlight_bg: Color::Rgb(209, 226, 243), // Light blue highlight
            menu_highlight_fg: Color::Rgb(0, 0, 0),       // Dark text on light highlight
            menu_border_fg: Color::Rgb(180, 180, 180),
            menu_separator_fg: Color::Rgb(210, 210, 210),
            menu_hover_bg: Color::Rgb(230, 235, 240),
            menu_hover_fg: Color::Rgb(0, 0, 0),
            menu_disabled_fg: Color::Rgb(160, 160, 160), // Gray for disabled items
            menu_disabled_bg: Color::Rgb(248, 248, 248),

            status_bar_fg: Color::Black,
            status_bar_bg: Color::Rgb(220, 220, 220), // Light grey
            prompt_fg: Color::Black,
            prompt_bg: Color::Rgb(230, 240, 250), // Very light blue
            prompt_selection_fg: Color::Black,
            prompt_selection_bg: Color::Rgb(173, 214, 255), // Light blue selection

            popup_border_fg: Color::Rgb(180, 180, 180),
            popup_bg: Color::Rgb(232, 238, 245), // Light blue-gray
            popup_selection_bg: Color::Rgb(209, 226, 243),
            popup_text_fg: Color::Rgb(30, 30, 30),

            suggestion_bg: Color::Rgb(232, 238, 245), // Light blue-gray
            suggestion_selected_bg: Color::Rgb(209, 226, 243),

            help_bg: Color::White,
            help_fg: Color::Black,
            help_key_fg: Color::Blue,
            help_separator_fg: Color::Gray,

            help_indicator_fg: Color::Red,
            help_indicator_bg: Color::White,

            inline_code_bg: Color::Rgb(230, 230, 235), // Light gray for code blocks

            split_separator_fg: Color::Rgb(140, 140, 140),
            split_separator_hover_fg: Color::Rgb(70, 130, 180), // Steel blue

            // Scrollbar colors
            scrollbar_track_fg: Color::Rgb(220, 220, 220),
            scrollbar_thumb_fg: Color::Rgb(180, 180, 180),
            scrollbar_track_hover_fg: Color::Rgb(200, 200, 200),
            scrollbar_thumb_hover_fg: Color::Rgb(140, 140, 140),

            // Compose mode colors
            compose_margin_bg: Color::Rgb(220, 220, 225), // Slightly darker than white for "desk" effect

            // Semantic highlighting (word under cursor)
            semantic_highlight_bg: Color::Rgb(220, 230, 240), // Subtle light blue highlight

            // Terminal colors (use terminal's default colors to preserve transparency)
            terminal_bg: Color::Reset,
            terminal_fg: Color::Reset,

            // Status bar warning/error indicator colors (darker for light theme)
            status_warning_indicator_bg: Color::Rgb(202, 145, 0), // Darker amber for light bg
            status_warning_indicator_fg: Color::Rgb(0, 0, 0),     // Black text
            status_error_indicator_bg: Color::Rgb(200, 40, 40),   // Darker red for light bg
            status_error_indicator_fg: Color::Rgb(255, 255, 255), // White text
            status_warning_indicator_hover_bg: Color::Rgb(232, 175, 30), // Lighter amber
            status_warning_indicator_hover_fg: Color::Rgb(0, 0, 0),
            status_error_indicator_hover_bg: Color::Rgb(230, 70, 70), // Lighter red
            status_error_indicator_hover_fg: Color::Rgb(255, 255, 255),

            // Tab drag-and-drop colors (lighter for light theme)
            tab_drop_zone_bg: Color::Rgb(173, 214, 255), // Light blue
            tab_drop_zone_border: Color::Rgb(70, 130, 180), // Steel blue

            // Search colors
            search_match_bg: Color::Rgb(255, 255, 150), // Light yellow highlight
            search_match_fg: Color::Rgb(0, 0, 0),

            // Diagnostic colors
            diagnostic_error_fg: Color::Red,
            diagnostic_error_bg: Color::Rgb(255, 220, 220),
            diagnostic_warning_fg: Color::Rgb(128, 128, 0),
            diagnostic_warning_bg: Color::Rgb(255, 255, 200),
            diagnostic_info_fg: Color::Blue,
            diagnostic_info_bg: Color::Rgb(220, 240, 255),
            diagnostic_hint_fg: Color::DarkGray,
            diagnostic_hint_bg: Color::Rgb(240, 240, 240),

            // Syntax highlighting colors (improved light theme palette)
            syntax_keyword: Color::Rgb(175, 0, 219), // Purple keywords
            syntax_string: Color::Rgb(163, 21, 21),  // Dark red strings
            syntax_comment: Color::Rgb(0, 128, 0),   // Green comments
            syntax_function: Color::Rgb(121, 94, 38), // Brown functions
            syntax_type: Color::Rgb(0, 128, 128),    // Teal types
            syntax_variable: Color::Rgb(0, 16, 128), // Dark blue variables
            syntax_constant: Color::Rgb(0, 112, 193), // Blue constants
            syntax_operator: Color::Rgb(0, 0, 0),    // Black operators
        }
    }

    /// High contrast theme for accessibility
    pub fn high_contrast() -> Self {
        Self {
            name: "high-contrast".to_string(),

            // Editor colors
            editor_bg: Color::Black,
            editor_fg: Color::White,
            cursor: Color::White,
            inactive_cursor: Color::DarkGray,
            selection_bg: Color::Rgb(0, 100, 200),
            current_line_bg: Color::Rgb(20, 20, 20),
            line_number_fg: Color::Rgb(140, 140, 140),
            line_number_bg: Color::Black,

            // Diff highlighting colors
            diff_add_bg: Color::Rgb(0, 80, 0),     // Dark green
            diff_remove_bg: Color::Rgb(100, 0, 0), // Dark red
            diff_modify_bg: Color::Rgb(25, 22, 0), // Subtle yellow, close to black
            diff_add_highlight_bg: Color::Rgb(0, 140, 0), // Brighter green
            diff_remove_highlight_bg: Color::Rgb(180, 0, 0), // Brighter red

            // UI element colors
            tab_active_fg: Color::Black,
            tab_active_bg: Color::Yellow,
            tab_inactive_fg: Color::White,
            tab_inactive_bg: Color::Black,
            tab_separator_bg: Color::Rgb(30, 30, 35),
            tab_close_hover_fg: Color::Rgb(249, 38, 114), // Monokai pink for hover
            tab_hover_bg: Color::Rgb(50, 50, 55),

            // Menu bar colors
            menu_bg: Color::Rgb(50, 50, 55),
            menu_fg: Color::White,
            menu_active_bg: Color::Yellow,
            menu_active_fg: Color::Black,
            menu_dropdown_bg: Color::Rgb(20, 20, 20),
            menu_dropdown_fg: Color::White,
            menu_highlight_bg: Color::Rgb(0, 100, 200),
            menu_highlight_fg: Color::White,
            menu_border_fg: Color::Yellow,
            menu_separator_fg: Color::White,
            menu_hover_bg: Color::Rgb(50, 50, 50),
            menu_hover_fg: Color::Yellow,
            menu_disabled_fg: Color::DarkGray, // Low contrast gray for disabled
            menu_disabled_bg: Color::Rgb(20, 20, 20),

            status_bar_fg: Color::White,
            status_bar_bg: Color::Rgb(20, 20, 20), // Darker for high contrast
            prompt_fg: Color::White,
            prompt_bg: Color::Rgb(10, 10, 10), // Very dark
            prompt_selection_fg: Color::White,
            prompt_selection_bg: Color::Rgb(0, 100, 200), // Blue selection

            popup_border_fg: Color::LightCyan,
            popup_bg: Color::Black,
            popup_selection_bg: Color::Rgb(0, 100, 200),
            popup_text_fg: Color::White,

            suggestion_bg: Color::Black,
            suggestion_selected_bg: Color::Rgb(0, 100, 200),

            help_bg: Color::Black,
            help_fg: Color::White,
            help_key_fg: Color::LightCyan,
            help_separator_fg: Color::White,

            help_indicator_fg: Color::Red,
            help_indicator_bg: Color::Black,

            inline_code_bg: Color::Rgb(40, 40, 40), // Dark gray for code blocks

            split_separator_fg: Color::Rgb(140, 140, 140),
            split_separator_hover_fg: Color::Yellow,

            // Scrollbar colors
            scrollbar_track_fg: Color::White,
            scrollbar_thumb_fg: Color::Yellow,
            scrollbar_track_hover_fg: Color::Yellow,
            scrollbar_thumb_hover_fg: Color::Cyan,

            // Compose mode colors
            compose_margin_bg: Color::Rgb(10, 10, 10), // Very dark for high contrast "desk" effect

            // Semantic highlighting (word under cursor)
            semantic_highlight_bg: Color::Rgb(0, 60, 100), // Bright blue highlight for visibility

            // Terminal colors (use terminal's default colors to preserve transparency)
            terminal_bg: Color::Reset,
            terminal_fg: Color::Reset,

            // Status bar warning/error indicator colors (high visibility)
            status_warning_indicator_bg: Color::Yellow, // Bright yellow
            status_warning_indicator_fg: Color::Black,  // Black text
            status_error_indicator_bg: Color::Red,      // Bright red
            status_error_indicator_fg: Color::White,    // White text
            status_warning_indicator_hover_bg: Color::LightYellow, // Lighter yellow
            status_warning_indicator_hover_fg: Color::Black,
            status_error_indicator_hover_bg: Color::LightRed, // Lighter red
            status_error_indicator_hover_fg: Color::White,

            // Tab drag-and-drop colors (high visibility)
            tab_drop_zone_bg: Color::Rgb(0, 100, 200), // Bright blue
            tab_drop_zone_border: Color::Yellow,       // Yellow border for visibility

            // Search colors
            search_match_bg: Color::Yellow,
            search_match_fg: Color::Black,

            // Diagnostic colors
            diagnostic_error_fg: Color::Red,
            diagnostic_error_bg: Color::Rgb(100, 0, 0),
            diagnostic_warning_fg: Color::Yellow,
            diagnostic_warning_bg: Color::Rgb(100, 100, 0),
            diagnostic_info_fg: Color::Cyan,
            diagnostic_info_bg: Color::Rgb(0, 50, 100),
            diagnostic_hint_fg: Color::White,
            diagnostic_hint_bg: Color::Rgb(50, 50, 50),

            // Syntax highlighting colors (high contrast)
            syntax_keyword: Color::Cyan,
            syntax_string: Color::Green,
            syntax_comment: Color::Gray,
            syntax_function: Color::Yellow,
            syntax_type: Color::Magenta,
            syntax_variable: Color::White,
            syntax_constant: Color::LightBlue,
            syntax_operator: Color::White,
        }
    }

    /// Get a theme by name, defaults to dark if not found
    /// Tries to load from JSON file first, falls back to hardcoded themes
    pub fn from_name(name: &str) -> Self {
        let normalized_name = name.to_lowercase().replace('_', "-");

        // Try to load from JSON file first
        if let Some(theme) = Self::load_builtin_theme(&normalized_name) {
            return theme;
        }

        // Fall back to hardcoded themes
        match normalized_name.as_str() {
            "light" => Self::light(),
            "high-contrast" => Self::high_contrast(),
            "nostalgia" => Self::nostalgia(),
            _ => Self::dark(),
        }
    }

    /// Get all available theme names (builtin + user themes)
    pub fn available_themes() -> Vec<String> {
        let mut themes: Vec<String> = vec![
            "dark".to_string(),
            "light".to_string(),
            "high-contrast".to_string(),
            "nostalgia".to_string(),
        ];

        // Scan built-in themes directory (themes/*.json in the project)
        if let Ok(entries) = std::fs::read_dir("themes") {
            for entry in entries.flatten() {
                let path = entry.path();
                if path.extension().is_some_and(|ext| ext == "json") {
                    if let Some(stem) = path.file_stem() {
                        let name = stem.to_string_lossy().to_string();
                        // Avoid duplicates
                        if !themes.iter().any(|t| t == &name) {
                            themes.push(name);
                        }
                    }
                }
            }
        }

        // Scan user themes directory (user themes can override built-ins)
        if let Some(config_dir) = dirs::config_dir() {
            let user_themes_dir = config_dir.join("fresh").join("themes");
            if let Ok(entries) = std::fs::read_dir(&user_themes_dir) {
                for entry in entries.flatten() {
                    let path = entry.path();
                    if path.extension().is_some_and(|ext| ext == "json") {
                        if let Some(stem) = path.file_stem() {
                            let name = stem.to_string_lossy().to_string();
                            // Avoid duplicates (user theme overriding builtin)
                            if !themes.iter().any(|t| t == &name) {
                                themes.push(name);
                            }
                        }
                    }
                }
            }
        }

        themes
    }

    /// Nostalgia theme (Turbo Pascal 5 / WordPerfect 5 inspired)
    pub fn nostalgia() -> Self {
        Self {
            name: "nostalgia".to_string(),

            // Editor colors - classic blue background with yellow/white text
            editor_bg: Color::Rgb(0, 0, 170),    // Classic DOS blue
            editor_fg: Color::Rgb(255, 255, 85), // Bright yellow
            cursor: Color::Rgb(255, 255, 255),   // White block cursor
            inactive_cursor: Color::Rgb(170, 170, 170),
            selection_bg: Color::Rgb(170, 170, 170), // Gray selection
            current_line_bg: Color::Rgb(0, 0, 128),  // Slightly darker blue
            line_number_fg: Color::Rgb(85, 255, 255), // Cyan
            line_number_bg: Color::Rgb(0, 0, 170),

            // Diff highlighting colors
            diff_add_bg: Color::Rgb(0, 100, 0),      // DOS green
            diff_remove_bg: Color::Rgb(170, 0, 0),   // DOS red
            diff_modify_bg: Color::Rgb(20, 20, 140), // Subtle purple-blue, close to DOS blue bg
            diff_add_highlight_bg: Color::Rgb(0, 170, 0), // Brighter DOS green
            diff_remove_highlight_bg: Color::Rgb(255, 0, 0), // Brighter DOS red

            // UI element colors
            tab_active_fg: Color::Rgb(0, 0, 0),
            tab_active_bg: Color::Rgb(170, 170, 170),
            tab_inactive_fg: Color::Rgb(255, 255, 85),
            tab_inactive_bg: Color::Rgb(0, 0, 128),
            tab_separator_bg: Color::Rgb(0, 0, 170),
            tab_close_hover_fg: Color::Rgb(255, 85, 85), // Bright red for close hover
            tab_hover_bg: Color::Rgb(0, 0, 200),         // Slightly brighter blue for hover

            // Menu bar colors - classic DOS menu style
            menu_bg: Color::Rgb(170, 170, 170),
            menu_fg: Color::Rgb(0, 0, 0),
            menu_active_bg: Color::Rgb(0, 170, 0),
            menu_active_fg: Color::Rgb(255, 255, 255),
            menu_dropdown_bg: Color::Rgb(170, 170, 170),
            menu_dropdown_fg: Color::Rgb(0, 0, 0),
            menu_highlight_bg: Color::Rgb(0, 170, 0), // Green highlight
            menu_highlight_fg: Color::Rgb(255, 255, 255),
            menu_border_fg: Color::Rgb(0, 0, 0),
            menu_separator_fg: Color::Rgb(85, 85, 85),
            menu_hover_bg: Color::Rgb(0, 170, 0),
            menu_hover_fg: Color::Rgb(255, 255, 255),
            menu_disabled_fg: Color::Rgb(85, 85, 85), // Dark gray for disabled
            menu_disabled_bg: Color::Rgb(170, 170, 170),

            status_bar_fg: Color::Rgb(0, 0, 0),
            status_bar_bg: Color::Rgb(0, 170, 170), // Cyan status bar
            prompt_fg: Color::Rgb(255, 255, 85),    // Yellow text
            prompt_bg: Color::Rgb(0, 0, 170),       // Blue background
            prompt_selection_fg: Color::Rgb(0, 0, 0),
            prompt_selection_bg: Color::Rgb(170, 170, 170),

            popup_border_fg: Color::Rgb(255, 255, 255),
            popup_bg: Color::Rgb(0, 0, 170),
            popup_selection_bg: Color::Rgb(0, 170, 0),
            popup_text_fg: Color::Rgb(255, 255, 85),

            suggestion_bg: Color::Rgb(0, 0, 170),
            suggestion_selected_bg: Color::Rgb(0, 170, 0),

            help_bg: Color::Rgb(0, 0, 170),
            help_fg: Color::Rgb(255, 255, 85),
            help_key_fg: Color::Rgb(85, 255, 255),
            help_separator_fg: Color::Rgb(170, 170, 170),

            help_indicator_fg: Color::Rgb(255, 85, 85),
            help_indicator_bg: Color::Rgb(0, 0, 170),

            inline_code_bg: Color::Rgb(0, 0, 85), // Darker blue for code blocks

            split_separator_fg: Color::Rgb(85, 255, 255),
            split_separator_hover_fg: Color::Rgb(255, 255, 255),

            // Scrollbar colors
            scrollbar_track_fg: Color::Rgb(0, 0, 128),
            scrollbar_thumb_fg: Color::Rgb(170, 170, 170),
            scrollbar_track_hover_fg: Color::Rgb(0, 0, 128),
            scrollbar_thumb_hover_fg: Color::Rgb(255, 255, 255),

            // Compose mode colors
            compose_margin_bg: Color::Rgb(0, 0, 128), // Darker blue for margins

            // Semantic highlighting (word under cursor)
            semantic_highlight_bg: Color::Rgb(0, 85, 170), // Lighter blue highlight

            // Terminal colors (Turbo Pascal style - blue background, yellow text)
            terminal_bg: Color::Rgb(0, 0, 170), // Classic DOS blue
            terminal_fg: Color::Rgb(255, 255, 85), // Bright yellow

            // Status bar warning/error indicator colors (DOS style)
            status_warning_indicator_bg: Color::Rgb(170, 85, 0), // Brown/orange (DOS warning)
            status_warning_indicator_fg: Color::Rgb(255, 255, 255), // White text
            status_error_indicator_bg: Color::Rgb(170, 0, 0),    // DOS red
            status_error_indicator_fg: Color::Rgb(255, 255, 255), // White text
            status_warning_indicator_hover_bg: Color::Rgb(200, 115, 30), // Lighter brown
            status_warning_indicator_hover_fg: Color::Rgb(255, 255, 255),
            status_error_indicator_hover_bg: Color::Rgb(200, 30, 30), // Lighter red
            status_error_indicator_hover_fg: Color::Rgb(255, 255, 255),

            // Tab drag-and-drop colors (DOS style)
            tab_drop_zone_bg: Color::Rgb(0, 170, 170), // Cyan (DOS style)
            tab_drop_zone_border: Color::Rgb(255, 255, 255), // White border

            // Search colors
            search_match_bg: Color::Rgb(170, 85, 0), // Orange/brown
            search_match_fg: Color::Rgb(255, 255, 255),

            // Diagnostic colors
            diagnostic_error_fg: Color::Rgb(255, 85, 85),
            diagnostic_error_bg: Color::Rgb(128, 0, 0),
            diagnostic_warning_fg: Color::Rgb(255, 255, 85),
            diagnostic_warning_bg: Color::Rgb(128, 128, 0),
            diagnostic_info_fg: Color::Rgb(85, 255, 255),
            diagnostic_info_bg: Color::Rgb(0, 85, 128),
            diagnostic_hint_fg: Color::Rgb(170, 170, 170),
            diagnostic_hint_bg: Color::Rgb(0, 0, 128),

            // Syntax highlighting colors (Turbo Pascal / Borland style)
            syntax_keyword: Color::Rgb(255, 255, 255), // Bright white keywords
            syntax_string: Color::Rgb(0, 255, 255),    // Bright cyan strings
            syntax_comment: Color::Rgb(128, 128, 128), // Dark gray comments
            syntax_function: Color::Rgb(255, 255, 0),  // Bright yellow functions
            syntax_type: Color::Rgb(0, 255, 0),        // Bright green types
            syntax_variable: Color::Rgb(255, 255, 85), // Yellow variables
            syntax_constant: Color::Rgb(255, 0, 255),  // Bright magenta constants
            syntax_operator: Color::Rgb(170, 170, 170), // Light gray operators
        }
    }

    /// Set the terminal cursor color using OSC 12 escape sequence.
    /// This makes the hardware cursor visible on any background.
    pub fn set_terminal_cursor_color(&self) {
        use std::io::Write;
        if let Some((r, g, b)) = color_to_rgb(self.cursor) {
            // OSC 12 sets cursor color: \x1b]12;#RRGGBB\x07
            let _ = write!(
                std::io::stdout(),
                "\x1b]12;#{:02x}{:02x}{:02x}\x07",
                r,
                g,
                b
            );
            let _ = std::io::stdout().flush();
        }
    }

    /// Reset the terminal cursor color to default.
    pub fn reset_terminal_cursor_color() {
        use std::io::Write;
        // OSC 112 resets cursor color to default
        let _ = write!(std::io::stdout(), "\x1b]112\x07");
        let _ = std::io::stdout().flush();
    }
}

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

// =============================================================================
// Theme Schema Generation for Plugin API
// =============================================================================

/// Returns the raw JSON Schema for ThemeFile, generated by schemars.
/// The schema uses standard JSON Schema format with $ref for type references.
/// Plugins are responsible for parsing and resolving $ref references.
pub fn get_theme_schema() -> serde_json::Value {
    use schemars::schema_for;
    let schema = schema_for!(ThemeFile);
    serde_json::to_value(&schema).unwrap_or_default()
}

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

    #[test]
    fn test_theme_creation() {
        let dark = Theme::dark();
        assert_eq!(dark.name, "dark");

        let light = Theme::light();
        assert_eq!(light.name, "light");

        let high_contrast = Theme::high_contrast();
        assert_eq!(high_contrast.name, "high-contrast");
    }

    #[test]
    fn test_theme_from_name() {
        let theme = Theme::from_name("light");
        assert_eq!(theme.name, "light");

        let theme = Theme::from_name("high-contrast");
        assert_eq!(theme.name, "high-contrast");

        let theme = Theme::from_name("unknown");
        assert_eq!(theme.name, "dark");
    }

    #[test]
    fn test_available_themes() {
        let themes = Theme::available_themes();
        // At minimum, should have the 4 builtin themes
        assert!(themes.len() >= 4);
        assert!(themes.contains(&"dark".to_string()));
        assert!(themes.contains(&"light".to_string()));
        assert!(themes.contains(&"high-contrast".to_string()));
        assert!(themes.contains(&"nostalgia".to_string()));
    }

    #[test]
    fn test_default_theme() {
        let theme = Theme::default();
        assert_eq!(theme.name, "high-contrast");
    }

    #[test]
    fn test_default_reset_color() {
        // Test that "Default" maps to Color::Reset
        let color: Color = ColorDef::Named("Default".to_string()).into();
        assert_eq!(color, Color::Reset);

        // Test that "Reset" also maps to Color::Reset
        let color: Color = ColorDef::Named("Reset".to_string()).into();
        assert_eq!(color, Color::Reset);
    }
}