File size: 68,977 Bytes
79fd0ed
 
 
 
 
 
 
fe47075
79fd0ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe47075
79fd0ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe47075
79fd0ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe47075
79fd0ed
fe47075
79fd0ed
 
 
 
 
 
 
 
 
 
fe47075
79fd0ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe47075
 
79fd0ed
 
 
fe47075
 
79fd0ed
 
 
fe47075
 
79fd0ed
 
 
 
 
 
 
 
fe47075
79fd0ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe47075
 
79fd0ed
 
fe47075
79fd0ed
 
 
 
 
fe47075
79fd0ed
 
 
 
 
 
 
 
 
 
 
 
 
 
fe47075
1e15293
fe47075
 
 
79fd0ed
 
 
fe47075
1e15293
fe47075
 
79fd0ed
 
 
 
fe47075
79fd0ed
 
fe47075
 
 
 
79fd0ed
 
 
 
 
 
 
fe47075
79fd0ed
 
 
 
 
 
 
 
 
fe47075
 
79fd0ed
 
fe47075
79fd0ed
fe47075
79fd0ed
 
fe47075
79fd0ed
 
 
 
 
 
fe47075
79fd0ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe47075
 
 
 
79fd0ed
fe47075
79fd0ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe47075
79fd0ed
 
 
 
 
 
 
 
 
 
 
fe47075
 
 
79fd0ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe47075
 
79fd0ed
 
 
 
fe47075
79fd0ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe47075
 
79fd0ed
 
 
 
fe47075
79fd0ed
 
 
 
 
 
 
 
 
fe47075
79fd0ed
 
 
 
 
 
 
fe47075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79fd0ed
 
 
 
 
fe47075
79fd0ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe47075
 
 
 
79fd0ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe47075
3e289ed
fe47075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79fd0ed
fe47075
79fd0ed
fe47075
 
 
79fd0ed
 
 
fe47075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79fd0ed
 
 
fe47075
 
 
 
 
 
 
 
 
 
 
 
 
 
79fd0ed
 
fe47075
79fd0ed
fe47075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79fd0ed
 
 
 
 
 
fe47075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79fd0ed
fe47075
 
 
79fd0ed
 
fe47075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79fd0ed
 
fe47075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79fd0ed
 
 
 
fe47075
 
79fd0ed
fe47075
 
 
 
1e15293
fe47075
 
 
 
 
79fd0ed
fe47075
 
79fd0ed
fe47075
 
 
 
 
 
79fd0ed
 
fe47075
 
 
 
79fd0ed
fe47075
 
 
79fd0ed
fe47075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79fd0ed
 
 
fe47075
 
 
 
 
 
 
 
 
 
3e289ed
fe47075
1e15293
fe47075
 
1e15293
fe47075
 
3e289ed
fe47075
 
 
1e15293
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe47075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e15293
fe47075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e289ed
79fd0ed
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>TreyOS</title>
<style>
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

  * { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

  :root {
    --bg: #eef0f8;
    --surface: rgba(255,255,255,0.72);
    --surface-hover: rgba(255,255,255,0.9);
    --border: rgba(255,255,255,0.6);
    --text: #1a1d2e;
    --text-secondary: #6b7280;
    --accent: #6366f1;
    --accent2: #818cf8;
    --accent3: #a5b4fc;
    --danger: #ef4444;
    --success: #22c55e;
    --shadow: 0 4px 24px rgba(99,102,241,0.10);
    --shadow-lg: 0 8px 40px rgba(99,102,241,0.16);
    --radius: 20px;
    --radius-sm: 12px;
    --blur: blur(20px);
    --font: 'Inter', system-ui, sans-serif;
    --dock-h: 90px;
  }

  html, body {
    height: 100%; width: 100%;
    font-family: var(--font);
    background: var(--bg);
    overflow: hidden;
    color: var(--text);
  }

  /* ─── SPLASH ─── */
  #splash {
    position: fixed; inset: 0; z-index: 9999;
    background: linear-gradient(145deg, #dde3f8 0%, #eef0fb 40%, #d8dcf5 100%);
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 28px;
    transition: opacity 0.6s ease, transform 0.6s ease;
  }
  #splash.hidden { opacity: 0; transform: scale(1.04); pointer-events: none; }
  .splash-logo {
    width: 120px; height: 120px;
    background: rgba(255,255,255,0.6);
    backdrop-filter: var(--blur);
    border-radius: 50%;
    border: 2px solid rgba(99,102,241,0.3);
    display: flex; align-items: center; justify-content: center;
    box-shadow: 0 0 60px rgba(99,102,241,0.25), 0 0 120px rgba(99,102,241,0.1);
    animation: pulse 2s ease-in-out infinite;
  }
  @keyframes pulse {
    0%,100% { box-shadow: 0 0 60px rgba(99,102,241,0.25), 0 0 120px rgba(99,102,241,0.1); }
    50% { box-shadow: 0 0 80px rgba(99,102,241,0.4), 0 0 160px rgba(99,102,241,0.15); }
  }
  .splash-t { font-size: 14px; color: #6366f1; font-weight: 500; }
  .splash-t span { color: var(--text); font-weight: 700; }
  .splash-bar { width: 200px; height: 4px; background: rgba(99,102,241,0.15); border-radius: 99px; overflow: hidden; }
  .splash-fill { height: 100%; background: linear-gradient(90deg, #6366f1, #818cf8); border-radius: 99px; animation: load 1.8s ease forwards; }
  @keyframes load { from { width: 0 } to { width: 100% } }
  .splash-sub { font-size: 12px; color: var(--text-secondary); }

  /* ─── WALLPAPER ─── */
  #os {
    position: fixed; inset: 0;
    background: linear-gradient(160deg, #c7d2fe 0%, #e0e7ff 30%, #f0f4ff 60%, #dde8ff 100%);
    display: flex; flex-direction: column;
    overflow: hidden;
  }

  /* ─── STATUS BAR ─── */
  .statusbar {
    display: flex; align-items: center; justify-content: space-between;
    padding: 10px 20px 4px;
    font-size: 12px; font-weight: 600;
    color: #1a1d2e;
    flex-shrink: 0;
  }
  .statusbar-right { display: flex; gap: 6px; align-items: center; }
  .sb-icon { font-size: 11px; }

  /* ─── SCREENS ─── */
  .screen { display: none; flex: 1; overflow: hidden; flex-direction: column; }
  .screen.active { display: flex; }

  /* HOME SCREEN */
  #screen-home {
    padding: 8px 0 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .home-widgets {
    display: grid; grid-template-columns: 1fr 1fr;
    gap: 12px; padding: 8px 16px 4px;
  }
  .widget {
    background: var(--surface);
    backdrop-filter: var(--blur);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
  }
  .widget-welcome { grid-column: span 1; }
  .widget-welcome .greeting { font-size: 11px; color: var(--accent); font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; }
  .widget-welcome h2 { font-size: 20px; font-weight: 700; line-height: 1.2; margin: 4px 0 8px; }
  .widget-welcome h2 span { color: var(--accent); }
  .widget-ai {
    display: flex; align-items: center; gap: 8px;
    background: rgba(99,102,241,0.08); border-radius: 10px;
    padding: 8px 10px; font-size: 11px; color: var(--text-secondary);
  }
  .ai-orb {
    width: 22px; height: 22px; border-radius: 50%;
    background: conic-gradient(from 0deg, #6366f1, #818cf8, #a5b4fc, #6366f1);
    flex-shrink: 0; animation: spin 4s linear infinite;
  }
  @keyframes spin { to { transform: rotate(360deg); } }

  .widget-time { text-align: center; display: flex; flex-direction: column; justify-content: center; align-items: center; }
  .widget-time .clock { font-size: 32px; font-weight: 300; letter-spacing: -1px; }
  .widget-time .date-str { font-size: 11px; color: var(--text-secondary); margin-top: 2px; }

  .widget-sys { grid-column: span 2; }
  .sys-stats { display: grid; grid-template-columns: repeat(3,1fr); gap: 10px; }
  .stat-item label { font-size: 10px; color: var(--text-secondary); font-weight: 500; display: block; margin-bottom: 4px; }
  .stat-bar { height: 5px; background: rgba(99,102,241,0.12); border-radius: 99px; overflow: hidden; }
  .stat-fill { height: 100%; border-radius: 99px; background: linear-gradient(90deg, var(--accent), var(--accent2)); }
  .stat-val { font-size: 12px; font-weight: 600; margin-top: 3px; }

  .home-search {
    margin: 12px 16px 4px;
    display: flex; align-items: center; gap: 10px;
    background: var(--surface); backdrop-filter: var(--blur);
    border: 1px solid var(--border); border-radius: 50px;
    padding: 12px 18px; box-shadow: var(--shadow);
  }
  .home-search input {
    border: none; background: transparent; font-size: 14px;
    font-family: var(--font); color: var(--text); width: 100%;
    outline: none;
  }
  .home-search input::placeholder { color: var(--text-secondary); }
  .search-ai { width: 22px; height: 22px; background: linear-gradient(135deg, var(--accent), var(--accent2)); border-radius: 50%; display: flex; align-items: center; justify-content: center; flex-shrink: 0; cursor: pointer; }

  .app-section { padding: 12px 16px; }
  .section-label { font-size: 11px; font-weight: 600; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 12px; }
  .app-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
  .app-icon-wrap { display: flex; flex-direction: column; align-items: center; gap: 6px; cursor: pointer; }
  .app-icon {
    width: 60px; height: 60px; border-radius: 16px;
    display: flex; align-items: center; justify-content: center;
    font-size: 28px; box-shadow: var(--shadow);
    transition: transform 0.15s ease, box-shadow 0.15s ease;
    position: relative; overflow: hidden;
  }
  .app-icon:active { transform: scale(0.9); }
  .app-icon::before {
    content: ''; position: absolute; inset: 0;
    background: linear-gradient(145deg, rgba(255,255,255,0.35) 0%, transparent 60%);
    border-radius: inherit;
  }
  .app-label { font-size: 11px; font-weight: 500; color: var(--text); text-align: center; }

  .ai-files    { background: linear-gradient(145deg, #3b82f6, #60a5fa); }
  .ai-browser  { background: linear-gradient(145deg, #10b981, #34d399); }
  .ai-editor   { background: linear-gradient(145deg, #1a1d2e, #374151); }
  .ai-settings { background: linear-gradient(145deg, #6b7280, #9ca3af); }
  .ai-video    { background: linear-gradient(145deg, #8b5cf6, #a78bfa); }
  .ai-gallery  { background: linear-gradient(145deg, #f59e0b, #fbbf24); }
  .ai-terminal { background: linear-gradient(145deg, #111827, #1f2937); }
  .ai-docker   { background: linear-gradient(145deg, #0ea5e9, #38bdf8); }

  /* ─── FILE MANAGER ─── */
  #screen-files { background: #f8f9ff; }
  .app-header {
    display: flex; align-items: center; gap: 12px;
    padding: 12px 16px 8px;
    background: rgba(248,249,255,0.95);
    backdrop-filter: var(--blur);
    border-bottom: 1px solid rgba(0,0,0,0.06);
    flex-shrink: 0;
  }
  .app-header-icon { width: 36px; height: 36px; border-radius: 10px; display: flex; align-items: center; justify-content: center; font-size: 20px; }
  .app-header h1 { font-size: 18px; font-weight: 700; flex: 1; }
  .header-btn { width: 34px; height: 34px; border-radius: 10px; background: rgba(0,0,0,0.06); border: none; font-size: 16px; cursor: pointer; display: flex; align-items: center; justify-content: center; }

  .files-layout { display: flex; flex: 1; overflow: hidden; }
  .files-sidebar {
    width: 140px; flex-shrink: 0;
    padding: 10px 8px;
    overflow-y: auto;
    border-right: 1px solid rgba(0,0,0,0.06);
  }
  .sidebar-section { margin-bottom: 16px; }
  .sidebar-label { font-size: 10px; font-weight: 600; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 0.06em; padding: 0 4px 6px; display: block; }
  .sidebar-item {
    display: flex; align-items: center; gap: 7px;
    padding: 8px 8px; border-radius: 10px; cursor: pointer;
    font-size: 12px; font-weight: 500; color: var(--text);
    transition: background 0.15s;
  }
  .sidebar-item:hover, .sidebar-item.active { background: rgba(99,102,241,0.1); color: var(--accent); }
  .sidebar-item .si-icon { font-size: 14px; }

  .storage-stat { margin: 8px; padding: 10px; background: rgba(99,102,241,0.08); border-radius: 12px; }
  .storage-stat .st-label { font-size: 10px; color: var(--text-secondary); }
  .storage-stat .st-bar { height: 4px; background: rgba(0,0,0,0.1); border-radius: 99px; margin: 5px 0; overflow: hidden; }
  .storage-stat .st-fill { height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent2)); width: 50%; border-radius: 99px; }
  .storage-stat .st-val { font-size: 11px; font-weight: 600; }

  .files-main { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 10px 12px; }
  .quick-access { margin-bottom: 16px; }
  .qa-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .qa-item {
    background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
    padding: 12px; cursor: pointer; transition: transform 0.15s;
    display: flex; flex-direction: column; align-items: flex-start; gap: 6px;
    backdrop-filter: var(--blur);
  }
  .qa-item:active { transform: scale(0.97); }
  .qa-icon { font-size: 22px; }
  .qa-name { font-size: 12px; font-weight: 600; }
  .qa-count { font-size: 10px; color: var(--text-secondary); }

  .file-list { display: flex; flex-direction: column; gap: 2px; }
  .file-row {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 10px; border-radius: 12px; cursor: pointer;
    transition: background 0.12s;
  }
  .file-row:hover, .file-row:active { background: rgba(99,102,241,0.07); }
  .file-icon { font-size: 22px; width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border-radius: 10px; background: rgba(0,0,0,0.04); flex-shrink: 0; }
  .file-info { flex: 1; min-width: 0; }
  .file-name { font-size: 13px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .file-meta { font-size: 10px; color: var(--text-secondary); margin-top: 1px; }
  .file-menu { font-size: 16px; color: var(--text-secondary); padding: 4px; border-radius: 6px; cursor: pointer; }

  /* ─── EDITOR ─── */
  #screen-editor { background: #0d1117; color: #e6edf3; }
  #screen-editor .app-header { background: #161b22; border-bottom-color: #30363d; }
  #screen-editor .app-header h1 { color: #e6edf3; }
  #screen-editor .header-btn { background: rgba(255,255,255,0.08); color: #e6edf3; }
  .editor-layout { display: flex; flex: 1; overflow: hidden; flex-direction: column; }
  .editor-tabs { display: flex; background: #161b22; border-bottom: 1px solid #30363d; overflow-x: auto; flex-shrink: 0; }
  .editor-tab { padding: 9px 14px; font-size: 12px; color: #8b949e; cursor: pointer; white-space: nowrap; border-right: 1px solid #30363d; display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
  .editor-tab.active { color: #e6edf3; background: #0d1117; border-bottom: 2px solid var(--accent); }
  .editor-body { display: flex; flex: 1; overflow: hidden; }

  .code-area { flex: 1; overflow: auto; padding: 16px; font-family: 'Courier New', monospace; font-size: 13px; line-height: 1.7; color: #e6edf3; position: relative; }
  .code-line { display: flex; gap: 16px; }
  .ln { color: #3d444d; min-width: 24px; text-align: right; user-select: none; }
  .kw { color: #ff7b72; }
  .fn { color: #d2a8ff; }
  .str { color: #a5d6ff; }
  .cm { color: #8b949e; font-style: italic; }
  .num { color: #79c0ff; }
  .editor-terminal { height: 130px; background: #010409; border-top: 1px solid #30363d; padding: 10px 14px; font-family: 'Courier New', monospace; font-size: 12px; color: #7ee787; overflow-y: auto; flex-shrink: 0; }
  .term-line { margin-bottom: 2px; }
  .term-prompt { color: #56d364; }
  .term-input { display: flex; align-items: center; gap: 6px; margin-top: 4px; }
  .term-input input { background: transparent; border: none; color: #e6edf3; font-family: inherit; font-size: 12px; flex: 1; outline: none; }

  /* ─── SETTINGS ─── */
  #screen-settings { background: #f8f9ff; }
  .settings-list { overflow-y: auto; flex: 1; padding: 10px 16px; }
  .settings-section { margin-bottom: 20px; }
  .settings-section-title { font-size: 11px; font-weight: 600; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 8px; padding: 0 4px; }
  .settings-card { background: var(--surface); backdrop-filter: var(--blur); border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; box-shadow: var(--shadow); }
  .settings-row {
    display: flex; align-items: center; gap: 12px;
    padding: 13px 14px; border-bottom: 1px solid rgba(0,0,0,0.05); cursor: pointer;
    transition: background 0.12s;
  }
  .settings-row:last-child { border-bottom: none; }
  .settings-row:hover { background: rgba(99,102,241,0.04); }
  .sr-icon { width: 32px; height: 32px; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 16px; flex-shrink: 0; }
  .sr-label { flex: 1; font-size: 13px; font-weight: 500; }
  .sr-value { font-size: 12px; color: var(--text-secondary); }
  .sr-arrow { color: var(--text-secondary); font-size: 12px; }
  .toggle { width: 40px; height: 22px; background: #d1d5db; border-radius: 11px; position: relative; cursor: pointer; transition: background 0.2s; flex-shrink: 0; }
  .toggle.on { background: var(--accent); }
  .toggle::after { content: ''; position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; background: white; border-radius: 50%; transition: left 0.2s; box-shadow: 0 1px 4px rgba(0,0,0,0.2); }
  .toggle.on::after { left: 20px; }

  .theme-row { display: flex; gap: 8px; padding: 12px 14px; flex-wrap: wrap; }
  .theme-dot { width: 28px; height: 28px; border-radius: 50%; cursor: pointer; border: 3px solid transparent; transition: border 0.15s; }
  .theme-dot.active { border-color: #1a1d2e; }

  .server-info { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 14px; box-shadow: var(--shadow); }
  .info-row { display: flex; justify-content: space-between; padding: 5px 0; font-size: 12px; border-bottom: 1px solid rgba(0,0,0,0.04); }
  .info-row:last-child { border-bottom: none; }
  .info-key { color: var(--text-secondary); }
  .info-val { font-weight: 600; font-family: 'Courier New', monospace; }

  /* ─── VIDEO ─── */
  #screen-video { background: #000; color: white; }
  #screen-video .app-header { background: rgba(0,0,0,0.8); border-bottom-color: rgba(255,255,255,0.1); }
  #screen-video .app-header h1 { color: white; }
  #screen-video .header-btn { background: rgba(255,255,255,0.12); color: white; }
  .video-player-area { width: 100%; background: #111; position: relative; overflow: hidden; flex-shrink: 0; aspect-ratio: 16/9; }
  .video-thumb { width: 100%; height: 100%; object-fit: cover; opacity: 0; }
  .video-overlay {
    position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
    background: linear-gradient(to bottom, rgba(0,0,0,0.2) 0%, rgba(0,0,0,0.6) 100%);
  }
  .video-play-btn {
    width: 64px; height: 64px; background: rgba(255,255,255,0.2); backdrop-filter: blur(8px);
    border-radius: 50%; border: 2px solid rgba(255,255,255,0.4); display: flex; align-items: center; justify-content: center;
    cursor: pointer; font-size: 24px; transition: transform 0.15s;
  }
  .video-play-btn:hover { transform: scale(1.08); }
  .video-controls { padding: 14px 16px; background: #111; }
  .video-progress { height: 4px; background: rgba(255,255,255,0.2); border-radius: 99px; margin-bottom: 12px; overflow: hidden; }
  .video-fill { height: 100%; background: var(--accent2); width: 35%; border-radius: 99px; }
  .video-ctrl-row { display: flex; align-items: center; gap: 16px; }
  .vc-btn { background: none; border: none; color: white; font-size: 18px; cursor: pointer; padding: 4px; }
  .vc-time { font-size: 12px; color: rgba(255,255,255,0.7); flex: 1; }
  .video-list { flex: 1; overflow-y: auto; padding: 8px 12px; }
  .video-item {
    display: flex; align-items: center; gap: 10px; padding: 10px 8px;
    border-radius: 12px; cursor: pointer; border-bottom: 1px solid rgba(255,255,255,0.06);
    transition: background 0.12s;
  }
  .video-item:hover { background: rgba(255,255,255,0.05); }
  .video-thumb-mini { width: 72px; height: 44px; background: #222; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 20px; flex-shrink: 0; }
  .vi-name { font-size: 13px; font-weight: 500; margin-bottom: 3px; }
  .vi-meta { font-size: 11px; color: rgba(255,255,255,0.4); }

  /* ─── GALLERY ─── */
  #screen-gallery { background: #f8f9ff; }
  .gallery-toolbar { display: flex; gap: 8px; padding: 8px 16px; border-bottom: 1px solid rgba(0,0,0,0.06); overflow-x: auto; flex-shrink: 0; }
  .gallery-tab { padding: 6px 14px; border-radius: 50px; font-size: 12px; font-weight: 600; cursor: pointer; white-space: nowrap; background: transparent; border: 1.5px solid rgba(0,0,0,0.1); color: var(--text-secondary); transition: all 0.15s; }
  .gallery-tab.active { background: var(--accent); color: white; border-color: var(--accent); }
  .gallery-grid { flex: 1; overflow-y: auto; padding: 10px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 3px; }
  .gallery-cell {
    aspect-ratio: 1; border-radius: 6px; overflow: hidden;
    background: linear-gradient(135deg, #e0e7ff, #c7d2fe);
    display: flex; align-items: center; justify-content: center;
    font-size: 28px; cursor: pointer;
    transition: opacity 0.15s;
    position: relative;
  }
  .gallery-cell:active { opacity: 0.8; }
  .gallery-cell .cell-overlay { position: absolute; inset: 0; background: linear-gradient(to bottom, transparent 60%, rgba(0,0,0,0.3)); }

  /* ─── BROWSER ─── */
  #screen-browser { background: #f1f3f9; }
  .browser-bar {
    display: flex; align-items: center; gap: 8px; padding: 8px 12px;
    background: rgba(241,243,249,0.95); border-bottom: 1px solid rgba(0,0,0,0.08); flex-shrink: 0;
  }
  .url-bar {
    flex: 1; background: white; border: 1.5px solid rgba(0,0,0,0.1);
    border-radius: 50px; padding: 8px 14px; font-size: 13px;
    font-family: var(--font); outline: none; color: var(--text);
  }
  .url-bar:focus { border-color: var(--accent); }
  .browser-btn { width: 32px; height: 32px; border-radius: 8px; background: rgba(0,0,0,0.06); border: none; font-size: 14px; cursor: pointer; }
  .browser-tabs { display: flex; gap: 2px; padding: 6px 12px 0; overflow-x: auto; flex-shrink: 0; }
  .btab { padding: 6px 12px 7px; background: rgba(0,0,0,0.06); border-radius: 10px 10px 0 0; font-size: 12px; cursor: pointer; white-space: nowrap; }
  .btab.active { background: white; }
  .browser-content { flex: 1; background: white; overflow: hidden; position: relative; }
  .browser-mock { padding: 20px; }
  .bm-hero { width: 100%; height: 120px; background: linear-gradient(135deg, #6366f1, #818cf8); border-radius: 12px; margin-bottom: 14px; display: flex; align-items: center; justify-content: center; color: white; font-size: 18px; font-weight: 700; }
  .bm-card { background: #f8f9ff; border: 1px solid rgba(0,0,0,0.08); border-radius: 10px; padding: 12px; margin-bottom: 10px; }
  .bm-card h3 { font-size: 13px; font-weight: 600; margin-bottom: 5px; }
  .bm-card p { font-size: 11px; color: var(--text-secondary); line-height: 1.5; }

  /* ─── DOCK ─── */
  .dock-spacer { height: var(--dock-h); flex-shrink: 0; }
  .dock {
    position: fixed; bottom: 0; left: 0; right: 0;
    height: var(--dock-h);
    padding: 8px 12px 18px;
    display: flex; align-items: flex-end; justify-content: space-around;
    z-index: 100;
  }
  .dock-bg {
    position: absolute; inset: 0;
    background: rgba(255,255,255,0.55);
    backdrop-filter: blur(30px);
    border-top: 1px solid rgba(255,255,255,0.6);
  }
  .dock-item {
    position: relative; z-index: 1;
    display: flex; flex-direction: column; align-items: center; gap: 4px;
    cursor: pointer;
  }
  .dock-icon {
    width: 52px; height: 52px; border-radius: 14px;
    display: flex; align-items: center; justify-content: center;
    font-size: 26px; box-shadow: 0 4px 16px rgba(0,0,0,0.12);
    transition: transform 0.15s ease;
    position: relative; overflow: hidden;
  }
  .dock-icon::before {
    content: ''; position: absolute; inset: 0;
    background: linear-gradient(145deg, rgba(255,255,255,0.3) 0%, transparent 60%);
  }
  .dock-icon:active { transform: scale(0.88) translateY(-4px); }
  .dock-dot { width: 4px; height: 4px; border-radius: 50%; background: var(--text); opacity: 0.4; }
  .dock-dot.active { opacity: 1; background: var(--accent); }

  .notch {
    width: 36px; height: 4px; background: rgba(0,0,0,0.2); border-radius: 99px;
    margin: 0 auto 8px; display: none;
  }

  /* Animations */
  @keyframes slideUp {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
  }
  .screen.active { animation: slideUp 0.25s ease; }

  /* Scrollbar */
  ::-webkit-scrollbar { width: 3px; height: 3px; }
  ::-webkit-scrollbar-track { background: transparent; }
  ::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.15); border-radius: 99px; }

  .hidden { display: none !important; }
</style>
</head>
<body>

<!-- SPLASH -->
<div id="splash">
  <div class="splash-logo">
    <svg width="60" height="60" viewBox="0 0 60 60" fill="none">
      <path d="M12 30 Q20 10 30 20 Q40 30 48 18" stroke="url(#sg)" stroke-width="5" stroke-linecap="round" fill="none"/>
      <path d="M12 42 Q24 22 36 32 Q44 40 50 30" stroke="url(#sg2)" stroke-width="5" stroke-linecap="round" fill="none"/>
      <defs>
        <linearGradient id="sg" x1="12" y1="20" x2="48" y2="20" gradientUnits="userSpaceOnUse">
          <stop stop-color="#6366f1"/><stop offset="1" stop-color="#a5b4fc"/>
        </linearGradient>
        <linearGradient id="sg2" x1="12" y1="36" x2="50" y2="36" gradientUnits="userSpaceOnUse">
          <stop stop-color="#818cf8"/><stop offset="1" stop-color="#c7d2fe"/>
        </linearGradient>
      </defs>
    </svg>
  </div>
  <div>
    <div class="splash-t"><span>TreyOS</span></div>
  </div>
  <div class="splash-bar"><div class="splash-fill"></div></div>
  <div class="splash-sub">Loading your experience...</div>
</div>

<!-- OS SHELL -->
<div id="os">
  <!-- Status Bar -->
  <div class="statusbar">
    <span id="sb-time">10:30</span>
    <div class="statusbar-right">
      <span class="sb-icon">▲▲▲▲</span>
      <span class="sb-icon"></span>
      <span class="sb-icon">🔋</span>
      <span>100%</span>
    </div>
  </div>

  <!-- HOME SCREEN -->
  <div class="screen active" id="screen-home">
    <div class="home-widgets">
      <div class="widget widget-welcome">
        <div class="greeting">Good morning</div>
        <h2>Welcome to<br><span>TreyOS</span></h2>
        <div class="widget-ai">
          <div class="ai-orb"></div>
          <span>How can I help you?</span>
        </div>
      </div>
      <div class="widget widget-time">
        <div class="clock" id="clock">10:30</div>
        <div class="date-str" id="datestr">Sun, Jun 28</div>
      </div>
      <div class="widget widget-sys">
        <div class="section-label" style="margin-bottom:10px">System</div>
        <div class="sys-stats">
          <div class="stat-item">
            <label>CPU</label>
            <div class="stat-bar"><div class="stat-fill" id="cpu-bar" style="width:0%"></div></div>
            <div class="stat-val" id="cpu-val"></div>
          </div>
          <div class="stat-item">
            <label>RAM</label>
            <div class="stat-bar"><div class="stat-fill" id="ram-bar" style="width:0%"></div></div>
            <div class="stat-val" id="ram-val"></div>
          </div>
          <div class="stat-item">
            <label>Disk</label>
            <div class="stat-bar"><div class="stat-fill" id="disk-bar" style="width:0%"></div></div>
            <div class="stat-val" id="disk-val"></div>
          </div>
        </div>
      </div>
    </div>

    <div class="home-search">
      <span>🔍</span>
      <input type="text" placeholder="Search apps, files, and more...">

    </div>

    <div class="app-section">
      <div class="section-label">Core Apps</div>
      <div class="app-grid">
        <div class="app-icon-wrap" onclick="openApp('files')">
          <div class="app-icon ai-files">📁</div>
          <div class="app-label">Files</div>
        </div>
        <div class="app-icon-wrap" onclick="openApp('browser')">
          <div class="app-icon ai-browser">🌐</div>
          <div class="app-label">Browser</div>
        </div>
        <div class="app-icon-wrap" onclick="openApp('editor')">
          <div class="app-icon ai-editor">💻</div>
          <div class="app-label">Editor</div>
        </div>
        <div class="app-icon-wrap" onclick="openApp('settings')">
          <div class="app-icon ai-settings">⚙️</div>
          <div class="app-label">Settings</div>
        </div>
        <div class="app-icon-wrap" onclick="openApp('video')">
          <div class="app-icon ai-video">▶️</div>
          <div class="app-label">Videos</div>
        </div>
        <div class="app-icon-wrap" onclick="openApp('gallery')">
          <div class="app-icon ai-gallery">🖼️</div>
          <div class="app-label">Gallery</div>
        </div>
        <div class="app-icon-wrap" onclick="openApp('terminal')">
          <div class="app-icon ai-terminal" style="font-family:monospace;font-size:20px;color:#56d364">$_</div>
          <div class="app-label">Terminal</div>
        </div>

      </div>
    </div>
    <div class="dock-spacer"></div>
  </div>

  <!-- FILE MANAGER -->
  <div class="screen" id="screen-files">
    <div class="app-header">
      <div class="app-icon-wrap" onclick="goHome()" style="cursor:pointer">
        <div style="font-size:20px"></div>
      </div>
      <div class="app-header-icon ai-files" style="font-size:18px">📁</div>
      <h1>Files</h1>
      <button class="header-btn">🔍</button>
      <button class="header-btn"></button>
    </div>
    <div class="files-layout">
      <div class="files-sidebar">
        <div class="sidebar-section">
          <span class="sidebar-label">Browse</span>
          <div class="sidebar-item active" onclick="loadFiles('/')"><span class="si-icon">🏠</span> Root</div>
          <div class="sidebar-item" onclick="loadFiles('/app')"><span class="si-icon">⚙️</span> App</div>
          <div class="sidebar-item" onclick="loadFiles('/trey/backend')"><span class="si-icon">🐍</span> Backend</div>
          <div class="sidebar-item" onclick="loadFiles('/trey/frontend')"><span class="si-icon">🌐</span> Frontend</div>
          <div class="sidebar-item" onclick="loadFiles('/tmp')"><span class="si-icon">🗂️</span> Temp</div>
        </div>
        <div class="sidebar-section">
          <span class="sidebar-label">Locations</span>
          <div class="sidebar-item" onclick="loadFiles('/')"><span class="si-icon">💾</span> /</div>
          <div class="sidebar-item" onclick="loadFiles('/app')"><span class="si-icon">📦</span> /app</div>
          <div class="sidebar-item" onclick="loadFiles('/tmp')"><span class="si-icon">🗂️</span> /tmp</div>
          <div class="sidebar-item" onclick="loadFiles('/home')"><span class="si-icon">🏠</span> /home</div>
        </div>
        <div class="storage-stat">
          <div class="st-label">Storage</div>
          <div class="st-bar"><div class="st-fill"></div></div>
          <div class="st-val" id="storage-val">— GB</div>
        </div>
      </div>
      <div class="files-main" id="files-main">
        <div id="files-breadcrumb" style="font-size:11px;color:var(--text-secondary);padding:2px 2px 10px;font-family:monospace">/</div>
        <div id="files-content">
          <div style="text-align:center;padding:40px 0;color:var(--text-secondary);font-size:13px">Loading filesystem...</div>
        </div>
        <div style="height:80px"></div>
      </div>
    </div>
    <div class="dock-spacer"></div>
  </div>

  <!-- CODE EDITOR -->
  <div class="screen" id="screen-editor">
    <div class="app-header">
      <div onclick="goHome()" style="cursor:pointer;font-size:20px;color:#e6edf3;padding:4px"></div>
      <div class="app-header-icon ai-terminal" style="font-size:16px;font-family:monospace;font-weight:700;color:#56d364">$_</div>
      <h1>Editor</h1>
      <button class="header-btn"></button>
      <button class="header-btn"></button>
    </div>
    <div class="editor-layout">
      <div class="editor-tabs" id="editor-tabs">
        <div class="editor-tab active">Terminal</div>
      </div>
      <div class="editor-body">
        <div class="code-area" id="code-area" style="display:none"></div>
      </div>
      <div class="editor-terminal" id="editor-terminal">
        <div class="term-input">
          <span class="term-prompt">$</span>
          <input type="text" id="term-input" placeholder="Connecting to terminal...">
        </div>
      </div>
    </div>
    <div class="dock-spacer"></div>
  </div>

  <!-- SETTINGS -->
  <div class="screen" id="screen-settings">
    <div class="app-header">
      <div onclick="goHome()" style="cursor:pointer;font-size:20px;padding:4px"></div>
      <div class="app-header-icon ai-settings" style="font-size:18px">⚙️</div>
      <h1>Settings</h1>
    </div>
    <div class="settings-list">
      <div class="settings-section">
        <div class="settings-section-title">Appearance</div>
        <div class="settings-card">
          <div class="settings-row">
            <div class="sr-icon" style="background:#f0f4ff">🎨</div>
            <div class="sr-label">Accent Color</div>
            <div class="theme-row" style="padding:0;gap:6px">
              <div class="theme-dot active" style="background:#6366f1" onclick="setAccent('#6366f1',this)"></div>
              <div class="theme-dot" style="background:#ec4899" onclick="setAccent('#ec4899',this)"></div>
              <div class="theme-dot" style="background:#10b981" onclick="setAccent('#10b981',this)"></div>
              <div class="theme-dot" style="background:#f59e0b" onclick="setAccent('#f59e0b',this)"></div>
            </div>
          </div>
          <div class="settings-row">
            <div class="sr-icon" style="background:#f0f4ff">🌙</div>
            <div class="sr-label">Dark Mode</div>
            <div class="toggle" id="dark-toggle" onclick="this.classList.toggle('on')"></div>
          </div>
          <div class="settings-row">
            <div class="sr-icon" style="background:#f0f4ff">🖼️</div>
            <div class="sr-label">Wallpaper</div>
            <div class="sr-value">Periwinkle</div>
            <div class="sr-arrow"></div>
          </div>
        </div>
      </div>

      <div class="settings-section">
        <div class="settings-section-title">System</div>
        <div class="settings-card">
          <div class="settings-row">
            <div class="sr-icon" style="background:#fff0f0">🔒</div>
            <div class="sr-label">Security</div>
            <div class="sr-arrow"></div>
          </div>
          <div class="settings-row">
            <div class="sr-icon" style="background:#f0fff4">👥</div>
            <div class="sr-label">Users</div>
            <div class="sr-value">1 active</div>
            <div class="sr-arrow"></div>
          </div>
          <div class="settings-row">
            <div class="sr-icon" style="background:#f0f0ff">🐳</div>
            <div class="sr-label">Docker</div>
            <div class="toggle on" onclick="this.classList.toggle('on')"></div>
          </div>
          <div class="settings-row">
            <div class="sr-icon" style="background:#fffbf0">💾</div>
            <div class="sr-label">Storage</div>
            <div class="sr-value">128 / 256 GB</div>
            <div class="sr-arrow"></div>
          </div>
        </div>
      </div>

      <div class="settings-section">
        <div class="settings-section-title">Server Info</div>
        <div class="server-info">
          <div class="info-row"><span class="info-key">Hostname</span><span class="info-val" id="info-hostname"></span></div>
          <div class="info-row"><span class="info-key">OS</span><span class="info-val" id="info-os"></span></div>
          <div class="info-row"><span class="info-key">Kernel</span><span class="info-val" id="info-kernel"></span></div>
          <div class="info-row"><span class="info-key">Uptime</span><span class="info-val" id="info-uptime"></span></div>
          <div class="info-row"><span class="info-key">TreyOS</span><span class="info-val">v1.0.0</span></div>
          <div class="info-row"><span class="info-key">IP</span><span class="info-val" id="info-ip"></span></div>
        </div>
      </div>

      <div class="settings-section">
        <div class="settings-section-title">About</div>
        <div class="settings-card">
          <div class="settings-row">
            <div class="sr-icon" style="background:#f0f4ff">ℹ️</div>
            <div class="sr-label">TreyOS Version</div>
            <div class="sr-value">1.0.0</div>
          </div>
          <div class="settings-row">
            <div class="sr-icon" style="background:#f0f4ff">📦</div>
            <div class="sr-label">App Store</div>
            <div class="sr-arrow"></div>
          </div>
        </div>
      </div>
      <div style="height:80px"></div>
    </div>
    <div class="dock-spacer"></div>
  </div>

  <!-- VIDEO PLAYER -->
  <div class="screen" id="screen-video">
    <div class="app-header">
      <div onclick="goHome()" style="cursor:pointer;font-size:20px;color:white;padding:4px"></div>
      <div class="app-header-icon ai-video" style="font-size:18px">▶️</div>
      <h1>Videos</h1>
      <button class="header-btn"></button>
    </div>
    <div class="video-player-area">
      <div class="video-overlay">
        <div class="video-play-btn"></div>
      </div>
      <div id="video-now-playing" style="position:absolute;bottom:0;left:0;right:0;padding:10px 14px;background:linear-gradient(transparent,rgba(0,0,0,0.7));display:none">
        <div id="video-title" style="font-size:13px;font-weight:600;color:white"></div>
        <div id="video-info" style="font-size:10px;color:rgba(255,255,255,0.6)"></div>
      </div>
    </div>
    <div class="video-controls">
      <div class="video-progress"><div class="video-fill"></div></div>
      <div class="video-ctrl-row">
        <button class="vc-btn"></button>
        <button class="vc-btn"></button>
        <button class="vc-btn" style="font-size:24px"></button>
        <button class="vc-btn"></button>
        <button class="vc-btn"></button>
        <span class="vc-time">3:29 / 9:56</span>
        <button class="vc-btn">📋</button>
        <button class="vc-btn"></button>
      </div>
    </div>
    <div class="video-list" id="video-list">
      <div style="text-align:center;padding:40px 0;color:rgba(255,255,255,0.3);font-size:13px">Loading videos...</div>
    </div>
    <div class="dock-spacer"></div>
  </div>

  <!-- GALLERY -->
  <div class="screen" id="screen-gallery">
    <div class="app-header">
      <div onclick="goHome()" style="cursor:pointer;font-size:20px;padding:4px"></div>
      <div class="app-header-icon ai-gallery" style="font-size:18px">🖼️</div>
      <h1>Gallery</h1>
      <button class="header-btn">🔍</button>
      <button class="header-btn"></button>
    </div>
    <div class="gallery-toolbar">
      <div class="gallery-tab active">All</div>
      <div class="gallery-tab">Photos</div>
      <div class="gallery-tab">Videos</div>
      <div class="gallery-tab">Screenshots</div>
      <div class="gallery-tab">Downloads</div>
    </div>
    <div class="gallery-grid" id="gallery-grid">
      <div style="grid-column:span 3;text-align:center;padding:40px 0;color:var(--text-secondary);font-size:13px">Loading images...</div>
    </div>
    <div class="dock-spacer"></div>
  </div>

  <!-- BROWSER -->
  <div class="screen" id="screen-browser">
    <div class="app-header">
      <div onclick="goHome()" style="cursor:pointer;font-size:20px;padding:4px"></div>
      <div class="app-header-icon ai-browser" style="font-size:18px">🌐</div>
      <h1>Browser</h1>
      <button class="header-btn"></button>
    </div>
    <div class="browser-tabs">
      <div class="btab active">🌐 New Tab</div>
<div class="btab"></div>
    </div>
    <div class="browser-bar">
      <button class="browser-btn"></button>
      <button class="browser-btn"></button>
      <input class="url-bar" type="text" value="https://localhost:8000" placeholder="Search or enter URL">
      <button class="browser-btn"></button>
    </div>
    <div class="browser-content" style="display:flex;align-items:center;justify-content:center;flex:1;background:#1a1a2e;">
      <div style="text-align:center;color:rgba(255,255,255,0.3)">
        <div style="font-size:32px;margin-bottom:12px">🌐</div>
        <div style="font-size:13px">Enter a URL above and press Enter</div>
        <div style="font-size:11px;margin-top:6px;opacity:0.6">Browser requires Playwright backend</div>
      </div>
    </div>
    <div class="dock-spacer"></div>
  </div>


  <!-- TERMINAL -->
  <div class="screen" id="screen-terminal">
    <div class="app-header" style="background:#161b22;border-bottom-color:#30363d;">
      <div onclick="goHome()" style="cursor:pointer;font-size:20px;color:#e6edf3;padding:4px"></div>
      <div class="app-header-icon ai-terminal" style="font-family:monospace;font-size:14px;font-weight:700;color:#56d364">$_</div>
      <h1 style="color:#e6edf3">Terminal</h1>
    </div>
    <div style="flex:1;display:flex;flex-direction:column;background:#010409;overflow:hidden;">
      <div id="terminal-output" style="flex:1;overflow-y:auto;padding:12px 14px;font-family:'Courier New',monospace;font-size:13px;color:#7ee787;"></div>
      <div style="display:flex;align-items:center;gap:8px;padding:10px 14px;border-top:1px solid #30363d;background:#0d1117;">
        <span style="color:#56d364;font-family:monospace;font-size:13px">$</span>
        <input id="terminal-input" type="text" placeholder="Connecting..."
          style="flex:1;background:transparent;border:none;color:#e6edf3;font-family:'Courier New',monospace;font-size:13px;outline:none;">
      </div>
    </div>
    <div class="dock-spacer"></div>
  </div>

  <!-- DOCK -->
  <div class="dock">
    <div class="dock-bg"></div>
    <div class="dock-item" onclick="openApp('home')">
      <div class="dock-icon ai-files">🏠</div>
      <div class="dock-dot" id="dot-home"></div>
    </div>
    <div class="dock-item" onclick="openApp('files')">
      <div class="dock-icon ai-files">📁</div>
      <div class="dock-dot" id="dot-files"></div>
    </div>
    <div class="dock-item" onclick="openApp('editor')">
      <div class="dock-icon ai-terminal" style="font-family:monospace;font-size:16px;color:#56d364">$_</div>
      <div class="dock-dot" id="dot-editor"></div>
    </div>
    <div class="dock-item" onclick="openApp('browser')">
      <div class="dock-icon ai-browser">🌐</div>
      <div class="dock-dot" id="dot-browser"></div>
    </div>
    <div class="dock-item" onclick="openApp('terminal')">
      <div class="dock-icon ai-terminal" style="font-family:monospace;font-size:18px;color:#56d364">$_</div>
      <div class="dock-dot" id="dot-terminal"></div>
    </div>
    <div class="dock-item" onclick="openApp('settings')">
      <div class="dock-icon ai-settings">⚙️</div>
      <div class="dock-dot" id="dot-settings"></div>
    </div>
  </div>
</div>

<script>
  // Clock
  function updateClock() {
    const now = new Date();
    const t = now.toLocaleTimeString([], {hour:'2-digit',minute:'2-digit'});
    const d = now.toLocaleDateString([], {weekday:'short',month:'short',day:'numeric'});
    document.getElementById('clock').textContent = t;
    document.getElementById('datestr').textContent = d;
    document.getElementById('sb-time').textContent = t;
  }
  updateClock();
  setInterval(updateClock, 10000);

  // Splash
  setTimeout(() => {
    document.getElementById('splash').classList.add('hidden');
  }, 2000);

  // Navigation
  let current = 'home';
  function openApp(id) {
    document.querySelectorAll('.screen').forEach(s => s.classList.remove('active'));
    document.querySelectorAll('.dock-dot').forEach(d => d.classList.remove('active'));
    const target = id === 'home' ? 'screen-home' : `screen-${id}`;
    const el = document.getElementById(target);
    if (el) el.classList.add('active');
    const dot = document.getElementById(`dot-${id}`);
    if (dot) dot.classList.add('active');
    const homeDot = document.getElementById('dot-home');
    if (id === 'home' && homeDot) homeDot.classList.add('active');
    current = id;
  }
  function goHome() { openApp('home'); }

  // Gallery tabs
  document.querySelectorAll('.gallery-tab').forEach(tab => {
    tab.addEventListener('click', () => {
      document.querySelectorAll('.gallery-tab').forEach(t => t.classList.remove('active'));
      tab.classList.add('active');
    });
  });

  // Accent color
  function setAccent(color, el) {
    document.documentElement.style.setProperty('--accent', color);
    document.querySelectorAll('.theme-dot').forEach(d => d.classList.remove('active'));
    el.classList.add('active');
  }

  // ── BACKEND ───────────────────────────────────────────────────────────────
  const API = window.location.origin;
  const WS  = API.replace(/^http/, 'ws');

  // ── UTILS ─────────────────────────────────────────────────────────────────
  function formatBytes(b) {
    if (!b) return '0 B';
    if (b > 1e9) return (b/1e9).toFixed(1)+' GB';
    if (b > 1e6) return (b/1e6).toFixed(1)+' MB';
    if (b > 1e3) return (b/1e3).toFixed(1)+' KB';
    return b+' B';
  }

  function fileIcon(ext) {
    const m = {'.pdf':'📕','.doc':'📘','.docx':'📘','.xls':'📗','.xlsx':'📗',
      '.jpg':'🖼️','.jpeg':'🖼️','.png':'🖼️','.gif':'🖼️','.webp':'🖼️',
      '.mp4':'🎬','.mkv':'🎬','.mov':'🎬','.webm':'🎬','.avi':'🎬',
      '.mp3':'🎵','.wav':'🎵','.flac':'🎵','.ogg':'🎵',
      '.py':'🐍','.js':'📜','.ts':'📜','.json':'📋','.sh':'⚡',
      '.zip':'📦','.tar':'📦','.gz':'📦','.txt':'📝','.md':'📝',
      '.html':'🌐','.css':'🎨','.env':'🔐','.yml':'⚙️','.yaml':'⚙️'};
    return m[ext] || '📄';
  }

  function isImage(ext) {
    return ['.jpg','.jpeg','.png','.gif','.webp','.bmp','.svg'].includes(ext);
  }
  function isVideo(ext) {
    return ['.mp4','.mkv','.webm','.mov','.avi','.m4v'].includes(ext);
  }

  // ── SYSTEM STATS ──────────────────────────────────────────────────────────
  function connectSystemStats() {
    const ws = new WebSocket(`${WS}/ws/system?interval=3`);
    ws.onmessage = e => {
      try {
        const msg = JSON.parse(e.data);
        if (msg.type !== 'stats') return;
        const { cpu, memory, disk } = msg.data;

        // Home screen bars
        const cb = document.getElementById('cpu-bar');   if(cb) cb.style.width = cpu.percent+'%';
        const cv = document.getElementById('cpu-val');   if(cv) cv.textContent = cpu.percent+'%';
        const rb = document.getElementById('ram-bar');   if(rb) rb.style.width = memory.percent+'%';
        const rv = document.getElementById('ram-val');   if(rv) rv.textContent = memory.used_gb+' GB';
        const db = document.getElementById('disk-bar');  if(db) db.style.width = disk.percent+'%';
        const dv = document.getElementById('disk-val');  if(dv) dv.textContent = disk.used_gb+' GB';

        // Settings server info
        const d = msg.data;
        const set = (id, v) => { const el = document.getElementById(id); if(el) el.textContent = v; };
        set('info-hostname', d.hostname);
        set('info-os', d.os);
        set('info-kernel', d.os);
        set('info-uptime', d.uptime);

        // Files sidebar storage
        const sf = document.getElementById('storage-fill');
        const sv = document.getElementById('storage-val');
        if(sf) sf.style.width = disk.percent+'%';
        if(sv) sv.textContent = `${disk.used_gb} / ${disk.total_gb} GB`;
      } catch(_) {}
    };
    ws.onclose = () => setTimeout(connectSystemStats, 5000);
    ws.onerror = () => ws.close();
  }
  connectSystemStats();

  // ── FILE MANAGER ──────────────────────────────────────────────────────────
  let currentPath = '/';

  async function loadFiles(path) {
    path = path || '/';
    currentPath = path;

    const breadcrumb = document.getElementById('files-breadcrumb');
    const content    = document.getElementById('files-content');
    if (!content) return;

    if (breadcrumb) breadcrumb.textContent = path;
    content.innerHTML = '<div style="text-align:center;padding:30px 0;color:var(--text-secondary);font-size:12px">Loading...</div>';

    try {
      const res = await fetch(`${API}/api/files?path=${encodeURIComponent(path)}`);
      if (!res.ok) throw new Error(await res.text());
      const data = await res.json();

      if (!data.entries.length) {
        content.innerHTML = '<div style="text-align:center;padding:30px 0;color:var(--text-secondary);font-size:12px">Empty folder</div>';
        return;
      }

      // Back button if not root
      let html = '';
      if (data.parent && path !== '/') {
        html += `<div class="file-row" onclick="loadFiles('${data.parent}')">
          <div class="file-icon">⬆️</div>
          <div class="file-info"><div class="file-name">..</div><div class="file-meta">Parent directory</div></div>
        </div>`;
      }

      html += data.entries.map(e => {
        const click = e.type === 'dir'
          ? `loadFiles('${e.path.replace(/'/g,"\\'")}')` 
          : `openFile('${e.path.replace(/'/g,"\\'")}','${e.extension||''}')`;
        return `<div class="file-row" onclick="${click}">
          <div class="file-icon">${e.type === 'dir' ? '📁' : fileIcon(e.extension)}</div>
          <div class="file-info">
            <div class="file-name">${e.name}</div>
            <div class="file-meta">${e.type === 'dir' ? 'Folder' : formatBytes(e.size)} • ${e.modified ? e.modified.split('T')[0] : ''}</div>
          </div>
          <div class="file-menu"></div>
        </div>`;
      }).join('');

      content.innerHTML = '<div class="file-list">'+html+'</div>';
    } catch(err) {
      content.innerHTML = `<div style="text-align:center;padding:30px;color:#ef4444;font-size:12px">Error: ${err.message}</div>`;
    }
  }

  function openFile(path, ext) {
    if (isImage(ext)) {
      window.open(`${API}/api/files/download?path=${encodeURIComponent(path)}`, '_blank');
    } else if (isVideo(ext)) {
      openApp('video');
      playVideo(path);
    } else {
      // Open in editor
      openApp('editor');
      loadFileInEditor(path);
    }
  }

  // ── GALLERY ───────────────────────────────────────────────────────────────
  async function loadGallery(filterExt = null) {
    const grid = document.getElementById('gallery-grid');
    if (!grid) return;
    grid.innerHTML = '<div style="grid-column:span 3;text-align:center;padding:30px 0;color:var(--text-secondary);font-size:12px">Scanning for images...</div>';

    try {
      // Search entire filesystem for images
      const exts = filterExt
        ? [filterExt]
        : ['.jpg','.jpeg','.png','.gif','.webp','.bmp'];

      const results = [];
      for (const ext of exts) {
        const res = await fetch(`${API}/api/files/search?path=/&query=${ext}&limit=30`);
        if (res.ok) {
          const data = await res.json();
          results.push(...data.results.filter(r => r.type === 'file' && isImage(r.extension)));
        }
      }

      if (!results.length) {
        grid.innerHTML = '<div style="grid-column:span 3;text-align:center;padding:40px 0;color:var(--text-secondary);font-size:13px">No images found on this system</div>';
        return;
      }

      grid.innerHTML = results.map(img => `
        <div class="gallery-cell" onclick="window.open('${API}/api/files/download?path=${encodeURIComponent(img.path)}','_blank')"
          style="background:#1a1a2e;position:relative;">
          <img src="${API}/api/files/download?path=${encodeURIComponent(img.path)}"
            style="width:100%;height:100%;object-fit:cover;position:absolute;inset:0;"
            onerror="this.style.display='none'"
            loading="lazy">
          <div class="cell-overlay"></div>
        </div>`).join('');
    } catch(err) {
      grid.innerHTML = `<div style="grid-column:span 3;text-align:center;padding:30px;color:#ef4444;font-size:12px">Error: ${err.message}</div>`;
    }
  }

  // Gallery tab filtering
  document.querySelectorAll('.gallery-tab').forEach((tab, i) => {
    tab.addEventListener('click', () => {
      document.querySelectorAll('.gallery-tab').forEach(t => t.classList.remove('active'));
      tab.classList.add('active');
    });
  });

  // ── VIDEO PLAYER ──────────────────────────────────────────────────────────
  async function loadVideoLibrary() {
    const list = document.getElementById('video-list');
    if (!list) return;
    list.innerHTML = '<div style="text-align:center;padding:20px;color:rgba(255,255,255,0.3);font-size:12px">Scanning for videos...</div>';

    try {
      const res = await fetch(`${API}/api/files/search?path=/&query=.mp4&limit=20`);
      const data = await res.json();
      const videos = data.results.filter(r => isVideo(r.extension));

      // Also search for mkv and webm
      const res2 = await fetch(`${API}/api/files/search?path=/&query=.mkv&limit=10`);
      const data2 = await res2.json();
      videos.push(...data2.results.filter(r => isVideo(r.extension)));

      if (!videos.length) {
        list.innerHTML = '<div style="text-align:center;padding:40px;color:rgba(255,255,255,0.3);font-size:13px">No videos found on this system</div>';
        return;
      }

      list.innerHTML = '<div style="font-size:11px;font-weight:600;color:rgba(255,255,255,0.4);text-transform:uppercase;letter-spacing:0.06em;padding:8px 8px 4px">Library</div>'
        + videos.map(v => `
        <div class="video-item" onclick="playVideo('${v.path.replace(/'/g,"\\'")}','${v.name}')">
          <div class="video-thumb-mini">🎬</div>
          <div>
            <div class="vi-name">${v.name}</div>
            <div class="vi-meta">${formatBytes(v.size)} • ${v.modified ? v.modified.split('T')[0] : ''}</div>
          </div>
        </div>`).join('');
    } catch(err) {
      list.innerHTML = `<div style="text-align:center;padding:20px;color:#ef4444;font-size:12px">Error: ${err.message}</div>`;
    }
  }

  function playVideo(path, name) {
    const url = `${API}/api/files/download?path=${encodeURIComponent(path)}`;
    const overlay = document.querySelector('.video-overlay');
    const area = document.querySelector('.video-player-area');
    // Remove old video element
    const old = area.querySelector('video');
    if (old) old.remove();
    // Create real video element
    const vid = document.createElement('video');
    vid.src = url;
    vid.controls = false;
    vid.style = 'position:absolute;inset:0;width:100%;height:100%;object-fit:contain;background:#000';
    area.appendChild(vid);
    if (overlay) overlay.style.display = 'none';
    // Update now playing
    document.getElementById('video-title').textContent = name || path.split('/').pop();
    document.getElementById('video-now-playing').style.display = 'block';
    vid.play();
    // Wire play button
    document.querySelector('.video-play-btn').onclick = () => vid.paused ? vid.play() : vid.pause();
  }

  // ── EDITOR / TERMINAL ─────────────────────────────────────────────────────
  async function loadFileInEditor(path) {
    const area = document.getElementById('code-area');
    if (!area) return;
    area.style.display = 'block';
    area.textContent = 'Loading...';
    try {
      const res = await fetch(`${API}/api/files/read?path=${encodeURIComponent(path)}`);
      const data = await res.json();
      area.textContent = data.content;
    } catch(e) {
      area.textContent = 'Could not read file.';
    }
  }

  // Terminal WebSocket
  let termWs = null;
  let termReady = false;

  function connectTerminal() {
    const terminal = document.getElementById('editor-terminal');
    const input    = document.getElementById('term-input');
    if (!input) return;

    termWs = new WebSocket(`${WS}/ws/terminal?cwd=/`);

    termWs.onopen = () => {
      termReady = true;
      input.placeholder = 'Enter command...';
    };

    termWs.onmessage = e => {
      try {
        const msg = JSON.parse(e.data);
        const text = msg.data || msg.message || '';
        if (!text && msg.type === 'prompt') {
          input.placeholder = `${msg.cwd} $`;
          return;
        }
        if (text) {
          const line = document.createElement('div');
          line.className = 'term-line';
          line.style.whiteSpace = 'pre-wrap';
          line.textContent = text;
          terminal.insertBefore(line, input.parentElement);
          terminal.scrollTop = terminal.scrollHeight;
        }
        if (msg.type === 'prompt') input.placeholder = `${msg.cwd || '/'} $`;
      } catch(_) {}
    };

    termWs.onclose = () => {
      termReady = false;
      input.placeholder = 'Disconnected — reconnecting...';
      setTimeout(connectTerminal, 3000);
    };

    input.addEventListener('keydown', ev => {
      if (ev.key !== 'Enter') return;
      const cmd = input.value.trim();
      if (!cmd) return;
      const echo = document.createElement('div');
      echo.className = 'term-line';
      echo.style.color = '#56d364';
      echo.textContent = '$ ' + cmd;
      terminal.insertBefore(echo, input.parentElement);
      if (termReady) termWs.send(JSON.stringify({ type: 'command', data: cmd }));
      input.value = '';
    });
  }
  connectTerminal();

  // ── NAVIGATION ────────────────────────────────────────────────────────────
  function openApp(id) {
    document.querySelectorAll('.screen').forEach(s => s.classList.remove('active'));
    document.querySelectorAll('.dock-dot').forEach(d => d.classList.remove('active'));
    const target = id === 'home' ? 'screen-home' : `screen-${id}`;
    const el = document.getElementById(target);
    if (el) el.classList.add('active');
    const dot = document.getElementById(`dot-${id}`);
    if (dot) dot.classList.add('active');
    if (id === 'home') { const h = document.getElementById('dot-home'); if(h) h.classList.add('active'); }

    // Lazy-load each app on first open
    if (id === 'files')   loadFiles('/');
    if (id === 'terminal') initStandaloneTerminal();
    if (id === 'gallery') loadGallery();
    if (id === 'browser' && !browserWsReady) browserConnect();
    if (id === 'video')   loadVideoLibrary();
  }

  function goHome() { openApp('home'); }



  // ── EDITOR ────────────────────────────────────────────────────────────────
  const editorTabs = [];
  let editorActive = null;

  function editorNewFile() {
    const name = 'untitled-' + (editorTabs.length + 1) + '.txt';
    const tab = { name, path: null, content: '', dirty: false };
    editorTabs.push(tab);
    editorActivate(tab);
  }

  function editorActivate(tab) {
    editorActive = tab;
    renderEditorTabs();
    const code = document.getElementById('editor-code');
    const empty = document.getElementById('editor-empty');
    const pathBar = document.getElementById('editor-path-bar');
    const saveBtn = document.getElementById('editor-save-btn');
    if (code && empty) {
      code.style.display = 'block';
      empty.style.display = 'none';
      code.value = tab.content;
      code.oninput = () => {
        tab.content = code.value;
        if (!tab.dirty) { tab.dirty = true; renderEditorTabs(); }
      };
    }
    if (pathBar) {
      pathBar.textContent = tab.path || 'New File — unsaved';
      pathBar.style.display = 'block';
    }
    if (saveBtn) saveBtn.style.display = 'flex';
  }

  function renderEditorTabs() {
    const bar = document.getElementById('editor-tabs');
    if (!bar) return;
    bar.innerHTML = editorTabs.map((t, i) => `
      <div onclick="editorActivate(editorTabs[${i}])"
        style="padding:8px 14px;font-size:12px;cursor:pointer;white-space:nowrap;flex-shrink:0;
               border-right:1px solid #30363d;display:flex;align-items:center;gap:6px;
               background:${t===editorActive?'#0d1117':'transparent'};
               color:${t===editorActive?'#e6edf3':'#8b949e'};
               border-bottom:${t===editorActive?'2px solid #6366f1':'2px solid transparent'}">
        <span>${t.dirty?'●':''} ${t.name}</span>
        <span onclick="event.stopPropagation();editorCloseTab(${i})"
          style="color:#6e7681;font-size:14px;line-height:1;">×</span>
      </div>`).join('');
  }

  function editorCloseTab(i) {
    editorTabs.splice(i, 1);
    if (editorActive === editorTabs[i]) editorActive = editorTabs[i-1] || editorTabs[0] || null;
    if (!editorTabs.length) {
      editorActive = null;
      const code = document.getElementById('editor-code');
      const empty = document.getElementById('editor-empty');
      const pathBar = document.getElementById('editor-path-bar');
      const saveBtn = document.getElementById('editor-save-btn');
      if (code) code.style.display = 'none';
      if (empty) empty.style.display = 'flex';
      if (pathBar) pathBar.style.display = 'none';
      if (saveBtn) saveBtn.style.display = 'none';
    } else {
      editorActivate(editorActive || editorTabs[0]);
    }
    renderEditorTabs();
  }

  async function editorSave() {
    if (!editorActive) return;
    const code = document.getElementById('editor-code');
    if (code) editorActive.content = code.value;

    if (!editorActive.path) {
      const p = prompt('Save as (full path):', '/app/' + editorActive.name);
      if (!p) return;
      editorActive.path = p;
      editorActive.name = p.split('/').pop();
    }
    try {
      const res = await fetch(`${API}/api/files/write?path=${encodeURIComponent(editorActive.path)}&content=${encodeURIComponent(editorActive.content)}`, {method:'POST'});
      if (!res.ok) throw new Error(await res.text());
      editorActive.dirty = false;
      renderEditorTabs();
      document.getElementById('editor-path-bar').textContent = editorActive.path;
    } catch(e) {
      alert('Save failed: ' + e.message);
    }
  }

  async function editorOpenFile(path) {
    // Check if already open
    const existing = editorTabs.find(t => t.path === path);
    if (existing) { editorActivate(existing); return; }
    try {
      const res = await fetch(`${API}/api/files/read?path=${encodeURIComponent(path)}`);
      if (!res.ok) throw new Error(await res.text());
      const data = await res.json();
      const name = path.split('/').pop();
      const tab = { name, path, content: data.content, dirty: false };
      editorTabs.push(tab);
      editorActivate(tab);
    } catch(e) {
      alert('Cannot open file: ' + e.message);
    }
  }

  async function editorOpenPicker() {
    const picker = document.getElementById('editor-picker');
    if (!picker) return;
    picker.style.display = 'flex';
    picker.style.flexDirection = 'column';
    await editorPickerLoad('/');
    document.getElementById('editor-picker-path').addEventListener('keydown', async ev => {
      if (ev.key === 'Enter') await editorPickerLoad(ev.target.value);
    });
  }

  async function editorPickerLoad(path) {
    document.getElementById('editor-picker-path').value = path;
    const list = document.getElementById('editor-picker-list');
    list.innerHTML = '<div style="color:#8b949e;padding:8px;font-size:12px">Loading...</div>';
    try {
      const res = await fetch(`${API}/api/files?path=${encodeURIComponent(path)}`);
      const data = await res.json();
      const parent = data.parent;
      let html = parent ? `<div onclick="editorPickerLoad('${parent}')"
        style="padding:10px;color:#8b949e;font-family:monospace;font-size:12px;cursor:pointer;border-bottom:1px solid #21262d;">⬆ ..</div>` : '';
      html += data.entries.map(e => `
        <div onclick="${e.type==='dir' ? `editorPickerLoad('${e.path.replace(/'/g,"\'")}')` : `editorPickOpenFile('${e.path.replace(/'/g,"\'")}')` }"
          style="padding:10px 12px;font-size:13px;cursor:pointer;border-bottom:1px solid #21262d;
                 color:${e.type==='dir'?'#a5b4fc':'#e6edf3'};font-family:monospace;
                 display:flex;align-items:center;gap:8px;">
          ${e.type==='dir'?'📁':'📄'} ${e.name}
        </div>`).join('');
      list.innerHTML = html || '<div style="color:#6e7681;padding:12px;font-size:12px">Empty</div>';
    } catch(e) {
      list.innerHTML = `<div style="color:#ef4444;padding:12px;font-size:12px">${e.message}</div>`;
    }
  }

  function editorPickOpenFile(path) {
    document.getElementById('editor-picker').style.display = 'none';
    editorOpenFile(path);
  }
  window.editorPickOpenFile = editorPickOpenFile;
  window.editorPickerLoad = editorPickerLoad;

  // ── BROWSER (Playwright WebSocket) ────────────────────────────────────────
  let browserWs = null, browserWsReady = false;

  function browserConnect() {
    browserWs = new WebSocket(`${WS}/ws/browser`);
    browserWs.onopen = () => { browserWsReady = true; };
    browserWs.onmessage = e => {
      try {
        const msg = JSON.parse(e.data);
        if (msg.type === 'screenshot') {
          const shot = document.getElementById('browser-shot');
          const empty = document.getElementById('browser-empty');
          const loading = document.getElementById('browser-loading');
          const errEl = document.getElementById('browser-error');
          if (shot) { shot.src = 'data:image/jpeg;base64,' + msg.data; shot.style.display='block'; }
          if (empty) empty.style.display = 'none';
          if (loading) loading.style.display = 'none';
          if (errEl) errEl.style.display = 'none';
          const urlInput = document.getElementById('browser-url');
          if (urlInput && msg.url) urlInput.value = msg.url;
        } else if (msg.type === 'loading') {
          document.getElementById('browser-loading').style.display = 'block';
          document.getElementById('browser-shot').style.display = 'none';
          document.getElementById('browser-error').style.display = 'none';
        } else if (msg.type === 'error') {
          const errEl = document.getElementById('browser-error');
          if (errEl) { errEl.textContent = msg.message; errEl.style.display = 'block'; }
          document.getElementById('browser-loading').style.display = 'none';
        }
      } catch(_) {}
    };
    browserWs.onclose = () => { browserWsReady = false; setTimeout(browserConnect, 3000); };
    browserWs.onerror = () => browserWs.close();
  }

  function browserSend(msg) {
    if (browserWs && browserWsReady) browserWs.send(JSON.stringify(msg));
  }

  function browserNav(url) {
    if (!url) return;
    if (!url.startsWith('http')) url = 'https://' + url;
    document.getElementById('browser-url').value = url;
    browserSend({ type: 'navigate', url });
  }

  function browserNew() {
    document.getElementById('browser-url').value = '';
    document.getElementById('browser-shot').style.display = 'none';
    document.getElementById('browser-empty').style.display = 'flex';
    document.getElementById('browser-empty').style.flexDirection = 'column';
    document.getElementById('browser-empty').style.alignItems = 'center';
    document.getElementById('browser-empty').style.justifyContent = 'center';
  }

  function browserClick(ev) {
    const img = document.getElementById('browser-shot');
    if (!img) return;
    const rect = img.getBoundingClientRect();
    // Scale click coords to 390x700 viewport
    const x = Math.round((ev.clientX - rect.left) / rect.width  * 390);
    const y = Math.round((ev.clientY - rect.top)  / rect.height * 700);
    browserSend({ type: 'click', x, y });
  }

  document.getElementById('browser-url')?.addEventListener('keydown', ev => {
    if (ev.key === 'Enter') browserNav(ev.target.value.trim());
  });

  // Connect browser WebSocket when browser app is opened
  window.browserConnect = browserConnect;

  // ── STANDALONE TERMINAL ───────────────────────────────────────────────────
  let stermWs = null, stermReady = false, stermInit = false;

  function initStandaloneTerminal() {
    if (stermInit) return;
    stermInit = true;
    const out   = document.getElementById('terminal-output');
    const input = document.getElementById('terminal-input');

    function addLine(text, color) {
      const line = document.createElement('div');
      line.style.whiteSpace = 'pre-wrap';
      line.style.marginBottom = '2px';
      if (color) line.style.color = color;
      line.textContent = text;
      out.appendChild(line);
      out.scrollTop = out.scrollHeight;
    }

    function connect() {
      stermWs = new WebSocket(`${WS}/ws/terminal?cwd=/`);
      stermWs.onopen = () => { stermReady = true; input.placeholder = 'Enter command...'; };
      stermWs.onmessage = e => {
        try {
          const msg = JSON.parse(e.data);
          if (msg.type === 'welcome') { addLine(msg.message, '#a5b4fc'); return; }
          if (msg.type === 'prompt')  { input.placeholder = (msg.cwd || '/') + ' $'; return; }
          if (msg.type === 'output' && msg.data) addLine(msg.data);
          if (msg.type === 'error'  && msg.data) addLine(msg.data, '#ef4444');
        } catch(_) {}
      };
      stermWs.onclose = () => {
        stermReady = false;
        input.placeholder = 'Reconnecting...';
        setTimeout(connect, 3000);
      };
    }
    connect();

    input.addEventListener('keydown', ev => {
      if (ev.key !== 'Enter') return;
      const cmd = input.value.trim();
      if (!cmd) return;
      addLine('$ ' + cmd, '#56d364');
      if (stermReady) stermWs.send(JSON.stringify({ type: 'command', data: cmd }));
      input.value = '';
    });
  }

  // Init
  openApp('home');

</script>
</body>
</html>