File size: 35,693 Bytes
8af739b
 
 
 
 
 
 
 
062c414
31eb5de
8af739b
 
 
7f20600
8af739b
 
7f20600
8af739b
 
 
66097e1
734142c
467789d
95df94d
163eb99
16a4dbd
31eb5de
bc139ec
 
 
 
 
 
 
 
 
 
2a1df12
 
 
062c414
163eb99
 
bc139ec
8af739b
 
7767652
8af739b
 
062c414
7f20600
8af739b
7f20600
8af739b
 
 
66097e1
8af739b
31eb5de
8af739b
 
 
 
75d362b
8af739b
66097e1
734142c
 
467789d
ad31128
467789d
95df94d
163eb99
 
ad31128
 
 
 
 
062c414
7f20600
ad31128
7f20600
66097e1
ad31128
467789d
ad31128
467789d
95df94d
163eb99
ad31128
31eb5de
3c1a712
467789d
3c1a712
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
062c414
3c1a712
 
467789d
3c1a712
467789d
95df94d
163eb99
3c1a712
 
 
 
 
062c414
3c1a712
 
467789d
3c1a712
467789d
95df94d
163eb99
3c1a712
 
 
 
 
8af739b
 
7767652
8af739b
 
ad31128
ef3fe15
8af739b
 
 
 
7767652
8af739b
 
 
7767652
8af739b
ad31128
ef3fe15
8af739b
 
 
 
7767652
8af739b
 
 
 
ad31128
ef3fe15
8af739b
 
 
 
7767652
8af739b
 
062c414
 
 
ef3fe15
062c414
8af739b
062c414
 
 
 
8af739b
 
7767652
8af739b
ad31128
ef3fe15
8af739b
 
 
 
7767652
8af739b
 
 
 
 
 
734142c
 
 
ad31128
ef3fe15
734142c
 
 
 
7767652
734142c
 
 
ad31128
 
 
4f10612
 
f77e773
 
ef3fe15
ad31128
 
 
 
7767652
ad31128
 
467789d
 
 
ef3fe15
467789d
 
 
 
 
 
 
163eb99
95df94d
 
 
 
 
 
 
 
 
 
 
 
163eb99
 
 
 
 
 
 
 
 
 
 
8af739b
734142c
8af739b
 
 
 
 
 
 
 
 
062c414
 
 
8af739b
 
 
7f20600
66097e1
467789d
ad31128
 
 
467789d
 
 
163eb99
 
 
8af739b
 
 
 
734142c
8af739b
 
 
c6c4e58
 
8af739b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31eb5de
 
 
 
 
86fdeeb
16a4dbd
8af739b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
734142c
8af739b
 
 
 
 
 
 
734142c
8af739b
 
 
 
 
 
 
 
 
 
bc139ec
 
 
 
 
 
 
 
 
 
 
 
ef3fe15
 
 
 
bc139ec
 
 
 
 
 
 
 
 
 
 
 
ef3fe15
 
 
 
bc139ec
 
 
 
 
 
 
 
 
 
 
 
ef3fe15
 
 
 
bc139ec
 
 
062c414
 
 
 
 
 
 
 
 
 
ef3fe15
 
 
 
062c414
 
bc139ec
 
 
 
 
 
 
 
 
 
ef3fe15
 
 
 
bc139ec
 
 
7f20600
bc139ec
66097e1
bc139ec
 
 
 
 
 
2a1df12
 
 
 
 
 
bc139ec
 
ef3fe15
 
 
 
bc139ec
 
 
467789d
bc139ec
 
 
 
 
 
2a1df12
 
 
 
 
 
bc139ec
 
ef3fe15
 
 
 
bc139ec
 
 
467789d
 
 
 
 
 
 
 
 
 
ef3fe15
 
 
 
467789d
 
 
95df94d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163eb99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7767652
 
 
 
 
 
062c414
7767652
 
 
 
062c414
7767652
 
062c414
7767652
8af739b
5ab4f0a
8af739b
734142c
8af739b
 
 
 
 
 
 
 
 
 
 
734142c
8af739b
 
734142c
8af739b
 
 
 
 
 
31eb5de
 
 
 
 
 
8af739b
ad31128
 
 
 
062c414
ad31128
3c1a712
ad31128
 
 
 
062c414
ad31128
 
bc139ec
ad31128
8af739b
7767652
b15ad2e
 
 
734142c
 
 
 
 
 
 
8af739b
734142c
7f20600
b15ad2e
062c414
 
 
 
 
 
 
 
 
 
b15ad2e
734142c
 
 
 
 
 
 
 
 
b15ad2e
734142c
 
 
 
 
 
 
 
 
3c1a712
b15ad2e
734142c
 
 
 
 
 
 
 
 
7f20600
b15ad2e
734142c
 
 
 
 
 
 
 
 
3c1a712
b15ad2e
ad31128
 
 
2a1df12
ad31128
 
 
 
 
3c1a712
b15ad2e
467789d
 
 
 
 
 
 
 
 
163eb99
b15ad2e
163eb99
 
 
 
 
 
 
 
 
734142c
8af739b
b15ad2e
 
3c1a712
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
467789d
 
 
 
3c1a712
 
 
 
95df94d
3c1a712
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
062c414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c1a712
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
467789d
 
 
 
 
 
ef3fe15
 
467789d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f20600
467789d
f77e773
 
 
ef3fe15
 
f77e773
467789d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef3fe15
467789d
 
 
 
95df94d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163eb99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
467789d
3c1a712
8af739b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31eb5de
 
 
 
 
 
 
8af739b
 
 
 
 
 
 
 
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
'use client'

import React, { useState, useEffect } from 'react'
import { Dock } from './Dock'
import { TopBar } from './TopBar'
import { FileManager } from './FileManager'
import { Calendar } from './Calendar'
import { DraggableDesktopIcon } from './DraggableDesktopIcon'
import { Messages } from './Messages'

import { HelpModal } from './HelpModal'
import { DesktopContextMenu } from './DesktopContextMenu'
import { BackgroundSelector } from './BackgroundSelector'

import { GeminiChat } from './GeminiChat'
import { Clock } from './Clock'

import { SpotlightSearch } from './SpotlightSearch'
import { ContextMenu } from './ContextMenu'
import { AboutModal } from './AboutModal'

import { FlutterRunner } from './FlutterRunner'
import { QuizApp } from './QuizApp'
import { TextEditor } from './TextEditor'
import { VoiceApp } from './VoiceApp'
import { motion, AnimatePresence } from 'framer-motion'
import { SystemPowerOverlay } from './SystemPowerOverlay'
import {
  Folder,
  Calendar as CalendarIcon,
  Clock as ClockIcon,
  Globe,
  Sparkle,
  Terminal as TerminalIcon,
  Key,
  Brain,
  Code,
  FileText,
  DeviceMobile,
  Lightning,
  Function,
  ChatCircleDots,
  MusicNote
} from '@phosphor-icons/react'

export function Desktop() {
  const [fileManagerOpen, setFileManagerOpen] = useState(false)
  const [calendarOpen, setCalendarOpen] = useState(false)
  const [clockOpen, setClockOpen] = useState(false)
  const [messagesOpen, setMessagesOpen] = useState(false)

  const [geminiChatOpen, setGeminiChatOpen] = useState(false)

  const [spotlightOpen, setSpotlightOpen] = useState(false)
  const [contextMenuVisible, setContextMenuVisible] = useState(false)
  const [contextMenuPosition, setContextMenuPosition] = useState({ x: 0, y: 0 })

  const [currentPath, setCurrentPath] = useState('')

  const [helpModalOpen, setHelpModalOpen] = useState(false)
  const [contextMenuOpen, setContextMenuOpen] = useState(false)
  const [contextMenuPos, setContextMenuPos] = useState({ x: 0, y: 0 })
  const [backgroundSelectorOpen, setBackgroundSelectorOpen] = useState(false)
  const [currentBackground, setCurrentBackground] = useState('https://images.unsplash.com/photo-1545159639-3f3534aa074e')
  const [aboutModalOpen, setAboutModalOpen] = useState(false)

  const [flutterRunnerOpen, setFlutterRunnerOpen] = useState(false)
  const [activeFlutterApp, setActiveFlutterApp] = useState<any>(null)

  const [flutterCodeEditorOpen, setFlutterCodeEditorOpen] = useState(false)
  const [quizAppOpen, setQuizAppOpen] = useState(false)
  const [textEditorOpen, setTextEditorOpen] = useState(false)
  const [activeTextFile, setActiveTextFile] = useState<{ content: string, fileName: string, filePath: string, passkey: string } | null>(null)
  const [voiceAppOpen, setVoiceAppOpen] = useState(false)

  // Minimized states
  const [fileManagerMinimized, setFileManagerMinimized] = useState(false)
  const [calendarMinimized, setCalendarMinimized] = useState(false)
  const [clockMinimized, setClockMinimized] = useState(false)
  const [messagesMinimized, setMessagesMinimized] = useState(false)

  const [geminiChatMinimized, setGeminiChatMinimized] = useState(false)


  const [flutterRunnerMinimized, setFlutterRunnerMinimized] = useState(false)

  const [flutterCodeEditorMinimized, setFlutterCodeEditorMinimized] = useState(false)
  const [quizAppMinimized, setQuizAppMinimized] = useState(false)
  const [textEditorMinimized, setTextEditorMinimized] = useState(false)
  const [voiceAppMinimized, setVoiceAppMinimized] = useState(false)

  const [powerState, setPowerState] = useState<'active' | 'sleep' | 'restart' | 'shutdown'>('active')
  const [globalZIndex, setGlobalZIndex] = useState(1000)
  const [windowZIndices, setWindowZIndices] = useState<{ [key: string]: number }>({})

  const getNextZIndex = () => {
    const nextZ = globalZIndex + 1
    setGlobalZIndex(nextZ)
    return nextZ
  }

  const bringWindowToFront = (windowId: string) => {
    setWindowZIndices(prev => ({ ...prev, [windowId]: getNextZIndex() }))
  }

  const closeAllApps = () => {
    // Close all apps
    setFileManagerOpen(false)
    setCalendarOpen(false)
    setClockOpen(false)
    setMessagesOpen(false)
    setGeminiChatOpen(false)
    setFlutterRunnerOpen(false)

    setFlutterCodeEditorOpen(false)
    setQuizAppOpen(false)
    setTextEditorOpen(false)
    setVoiceAppOpen(false)

    // Reset all minimized states
    setFileManagerMinimized(false)
    setCalendarMinimized(false)
    setClockMinimized(false)
    setMessagesMinimized(false)
    setGeminiChatMinimized(false)
    setFlutterRunnerMinimized(false)

    setFlutterCodeEditorMinimized(false)
    setQuizAppMinimized(false)
    setTextEditorMinimized(false)
    setVoiceAppMinimized(false)

    // Reset window z-indices
    setWindowZIndices({})
    setGlobalZIndex(1000)
  }

  const openFileManager = (path: string) => {
    console.log('Opening File Manager with path:', path)
    setCurrentPath(path)
    setFileManagerOpen(true)
    setFileManagerMinimized(false)
    bringWindowToFront('fileManager')
  }

  const closeFileManager = () => {
    setFileManagerOpen(false)
    setFileManagerMinimized(false)
  }

  const openCalendar = () => {
    console.log('Opening Calendar')
    setCalendarOpen(true)
    setCalendarMinimized(false)
    bringWindowToFront('calendar')
  }

  const closeCalendar = () => {
    setCalendarOpen(false)
    setCalendarMinimized(false)
  }

  const openClock = () => {
    setClockOpen(true)
    setClockMinimized(false)
    bringWindowToFront('clock')
  }

  const closeClock = () => {
    setClockOpen(false)
    setClockMinimized(false)
  }

  const openMessages = () => {
    setMessagesOpen(true)
    setMessagesMinimized(false)
    bringWindowToFront('messages')
  }

  const closeMessages = () => {
    setMessagesOpen(false)
    setMessagesMinimized(false)
  }

  const openGeminiChat = () => {
    console.log('Opening Gemini Chat')
    setGeminiChatOpen(true)
    setGeminiChatMinimized(false)
    bringWindowToFront('gemini')
  }

  const closeGeminiChat = () => {
    setGeminiChatOpen(false)
    setGeminiChatMinimized(false)
  }





  const openFlutterRunner = (appFile: any) => {
    setActiveFlutterApp(appFile)
    setFlutterRunnerOpen(true)
    setFlutterRunnerMinimized(false)
    bringWindowToFront('flutterRunner')
  }

  const closeFlutterRunner = () => {
    setFlutterRunnerOpen(false)
    setFlutterRunnerMinimized(false)
    setActiveFlutterApp(null)
  }



  const openFlutterCodeEditor = () => {
    // Don't read/remove sessionStorage here - let FlutterRunner handle it
    // This allows the polling mechanism in FlutterRunner to detect new files
    setFlutterCodeEditorOpen(true)
    setFlutterCodeEditorMinimized(false)
    bringWindowToFront('flutterCodeEditor')
  }

  const closeFlutterCodeEditor = () => {
    setFlutterCodeEditorOpen(false)
    setFlutterCodeEditorMinimized(false)
  }

  const openQuizApp = () => {
    setQuizAppOpen(true)
    setQuizAppMinimized(false)
    bringWindowToFront('quizApp')
  }

  const closeQuizApp = () => {
    setQuizAppOpen(false)
    setQuizAppMinimized(false)
  }

  const openTextEditor = (fileData: { content: string, fileName: string, filePath: string, passkey: string }) => {
    setActiveTextFile(fileData)
    setTextEditorOpen(true)
    setTextEditorMinimized(false)
    bringWindowToFront('textEditor')
  }

  const closeTextEditor = () => {
    setTextEditorOpen(false)
    setTextEditorMinimized(false)
    setActiveTextFile(null)
  }

  const openVoiceApp = () => {
    setVoiceAppOpen(true)
    setVoiceAppMinimized(false)
    bringWindowToFront('voiceApp')
  }

  const closeVoiceApp = () => {
    setVoiceAppOpen(false)
    setVoiceAppMinimized(false)
  }

  const handleOpenApp = (appId: string) => {
    switch (appId) {
      case 'files':
        openFileManager('')
        break
      case 'calendar':
        openCalendar()
        break
      case 'clock':
        openClock()
        break
      case 'messages':
        openMessages()
        break
      case 'gemini':
        openGeminiChat()
        break



      case 'flutter-editor':
        openFlutterCodeEditor()
        break
      case 'quiz':
        openQuizApp()
        break
      case 'voice-app':
        openVoiceApp()
        break
    }
  }

  const handleContextMenuAction = (action: string) => {
    switch (action) {
      case 'change-wallpaper':
        setBackgroundSelectorOpen(true)
        break
      case 'get-info':
        setAboutModalOpen(true)
        break
      case 'refresh':
        window.location.reload()
        break
    }
  }



  const openHelpModal = () => {
    setHelpModalOpen(true)
  }

  const closeHelpModal = () => {
    setHelpModalOpen(false)
  }

  const handleDesktopRightClick = (e: React.MouseEvent) => {
    e.preventDefault()
    setContextMenuPosition({ x: e.clientX, y: e.clientY })
    setContextMenuVisible(true)
  }

  const handleChangeBackground = (type: 'upload' | 'preset') => {
    setContextMenuOpen(false)
    setBackgroundSelectorOpen(true)
  }

  const handleSelectBackground = (background: string | File) => {
    if (typeof background === 'string') {
      // Handle preset backgrounds
      if (background.startsWith('gradient-')) {
        setCurrentBackground(background)
      } else {
        setCurrentBackground(background)
      }
    } else {
      // Handle uploaded file
      const url = URL.createObjectURL(background)
      setCurrentBackground(url)
    }
  }

  const handleSleep = () => setPowerState('sleep')
  const handleRestart = () => setPowerState('restart')
  const handleShutdown = () => setPowerState('shutdown')
  const handleWake = () => setPowerState('active')



  // Keyboard shortcuts
  useEffect(() => {
    const handleKeyDown = (e: KeyboardEvent) => {
      // Cmd/Ctrl + Space for Spotlight Search
      if ((e.metaKey || e.ctrlKey) && e.key === ' ') {
        e.preventDefault()
        setSpotlightOpen(true)
      }
      // Cmd/Ctrl + K for Spotlight Search (alternative)
      if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
        e.preventDefault()
        setSpotlightOpen(true)
      }
    }

    window.addEventListener('keydown', handleKeyDown)
    return () => window.removeEventListener('keydown', handleKeyDown)
  }, [])

  const getBackgroundStyle = () => {
    if (currentBackground.startsWith('gradient-')) {
      const gradients: Record<string, string> = {
        'gradient-sonoma': 'linear-gradient(135deg, #e0c3fc 0%, #8ec5fc 100%)',
        'gradient-purple': 'linear-gradient(135deg, #77216F 0%, #5E2750 50%, #2C001E 100%)',
        'gradient-blue': 'linear-gradient(135deg, #1e3c72 0%, #2a5298 50%, #7e8ba3 100%)',
        'gradient-green': 'linear-gradient(135deg, #134e5e 0%, #71b280 50%, #a8e063 100%)',
        'gradient-orange': 'linear-gradient(135deg, #ff512f 0%, #dd2476 50%, #f09819 100%)',
        'gradient-dark': 'linear-gradient(135deg, #0f0f0f 0%, #1a1a1a 50%, #2d2d2d 100%)',
        'gradient-cosmic': 'linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%)'
      }
      return { background: gradients[currentBackground] || gradients['gradient-sonoma'] }
    } else {
      return {
        backgroundImage: `url('${currentBackground}')`,
        backgroundSize: 'cover',
        backgroundPosition: 'center',
        backgroundRepeat: 'no-repeat'
      }
    }
  }

  // Build minimized apps list for dock
  const minimizedApps = []

  if (fileManagerMinimized && fileManagerOpen) {
    minimizedApps.push({
      id: 'files',
      label: 'Files',
      icon: (
        <div className="bg-gradient-to-br from-blue-400 to-cyan-200 w-full h-full rounded-xl flex items-center justify-center border border-white/30">
          <Folder size={20} weight="regular" className="text-blue-900" />
        </div>
      ),
      onRestore: () => {
        setFileManagerMinimized(false)
        bringWindowToFront('fileManager')
      }
    })
  }

  if (calendarMinimized && calendarOpen) {
    minimizedApps.push({
      id: 'calendar',
      label: 'Calendar',
      icon: (
        <div className="bg-gradient-to-br from-purple-500 to-purple-600 w-full h-full rounded-xl flex items-center justify-center shadow-inner">
          <CalendarIcon size={20} weight="regular" className="text-white" />
        </div>
      ),
      onRestore: () => {
        setCalendarMinimized(false)
        bringWindowToFront('calendar')
      }
    })
  }

  if (clockMinimized && clockOpen) {
    minimizedApps.push({
      id: 'clock',
      label: 'Clock',
      icon: (
        <div className="bg-black w-full h-full rounded-full flex items-center justify-center border border-gray-600">
          <ClockIcon size={20} weight="regular" className="text-white" />
        </div>
      ),
      onRestore: () => {
        setClockMinimized(false)
        bringWindowToFront('clock')
      }
    })
  }

  if (messagesMinimized && messagesOpen) {
    minimizedApps.push({
      id: 'messages',
      label: 'Messages',
      icon: (
        <div className="bg-gradient-to-b from-[#4CD964] to-[#2E8B57] w-full h-full rounded-[22%] flex items-center justify-center shadow-lg border-[0.5px] border-white/20 relative overflow-hidden">
          <div className="absolute inset-0 bg-gradient-to-b from-white/20 to-transparent opacity-50" />
          <ChatCircleDots size={20} weight="fill" className="text-white relative z-10 drop-shadow-md" />
        </div>
      ),
      onRestore: () => {
        setMessagesMinimized(false)
        bringWindowToFront('messages')
      }
    })
  }

  if (geminiChatMinimized && geminiChatOpen) {
    minimizedApps.push({
      id: 'gemini',
      label: 'Gemini',
      icon: (
        <div className="bg-white w-full h-full rounded-xl flex items-center justify-center border border-gray-200">
          <Sparkle size={20} weight="fill" className="text-blue-500" />
        </div>
      ),
      onRestore: () => {
        setGeminiChatMinimized(false)
        bringWindowToFront('gemini')
      }
    })
  }





  if (flutterRunnerMinimized && flutterRunnerOpen) {
    minimizedApps.push({
      id: 'flutter-runner',
      label: 'Flutter Runner',
      icon: (
        <div className="bg-gradient-to-b from-[#54C5F8] to-[#29B6F6] w-full h-full rounded-[22%] flex items-center justify-center shadow-lg border-[0.5px] border-white/20 relative overflow-hidden">
          <div className="absolute inset-0 bg-gradient-to-b from-white/20 to-transparent opacity-50" />
          <DeviceMobile size={20} weight="fill" className="text-white relative z-10 drop-shadow-md" />
          <div className="absolute top-1 right-1">
            <Lightning size={8} weight="fill" className="text-yellow-300 drop-shadow-md" />
          </div>
        </div>
      ),
      onRestore: () => {
        setFlutterRunnerMinimized(false)
        bringWindowToFront('flutterRunner')
      }
    })
  }



  if (flutterCodeEditorMinimized && flutterCodeEditorOpen) {
    minimizedApps.push({
      id: 'flutter-editor',
      label: 'Flutter IDE',
      icon: (
        <div className="bg-gradient-to-b from-[#54C5F8] to-[#29B6F6] w-full h-full rounded-[22%] flex items-center justify-center shadow-lg border-[0.5px] border-white/20 relative overflow-hidden">
          <div className="absolute inset-0 bg-gradient-to-b from-white/20 to-transparent opacity-50" />
          <DeviceMobile size={20} weight="fill" className="text-white relative z-10 drop-shadow-md" />
          <div className="absolute top-1 right-1">
            <Lightning size={8} weight="fill" className="text-yellow-300 drop-shadow-md" />
          </div>
        </div>
      ),
      onRestore: () => {
        setFlutterCodeEditorMinimized(false)
        bringWindowToFront('flutterCodeEditor')
      }
    })
  }

  if (quizAppMinimized && quizAppOpen) {
    minimizedApps.push({
      id: 'quiz',
      label: 'Quiz Master',
      icon: (
        <div className="bg-gradient-to-br from-teal-400 to-emerald-500 w-full h-full rounded-[22%] flex items-center justify-center shadow-lg border-[0.5px] border-white/20 relative overflow-hidden">
          <div className="absolute inset-0 bg-gradient-to-b from-white/20 to-transparent opacity-50" />
          <Brain size={20} weight="fill" className="text-white relative z-10 drop-shadow-md" />
        </div>
      ),
      onRestore: () => {
        setQuizAppMinimized(false)
        bringWindowToFront('quizApp')
      }
    })
  }

  if (textEditorMinimized && textEditorOpen) {
    minimizedApps.push({
      id: 'text-editor',
      label: activeTextFile?.fileName || 'Text Editor',
      icon: (
        <div className="bg-gradient-to-br from-gray-700 to-gray-900 w-full h-full rounded-[22%] flex items-center justify-center shadow-lg border-[0.5px] border-white/20 relative overflow-hidden">
          <div className="absolute inset-0 bg-gradient-to-b from-white/10 to-transparent opacity-30" />
          <FileText size={20} weight="fill" className="text-blue-400 relative z-10 drop-shadow-md" />
        </div>
      ),
      onRestore: () => {
        setTextEditorMinimized(false)
        bringWindowToFront('textEditor')
      }
    })
  }

  if (voiceAppMinimized && voiceAppOpen) {
    minimizedApps.push({
      id: 'voice-app',
      label: 'Voice Studio',
      icon: (
        <div className="bg-gradient-to-br from-purple-500 to-pink-500 w-full h-full rounded-[22%] flex items-center justify-center shadow-lg border-[0.5px] border-white/20 relative overflow-hidden">
          <div className="absolute inset-0 bg-gradient-to-b from-white/20 to-transparent opacity-50" />
          <MusicNote size={20} weight="fill" className="text-white relative z-10 drop-shadow-md" />
        </div>
      ),
      onRestore: () => {
        setVoiceAppMinimized(false)
        bringWindowToFront('voiceApp')
      }
    })
  }

  // Debug info
  useEffect(() => {
    console.log('App States:', {
      fileManagerOpen,
      calendarOpen,
      clockOpen,
      messagesOpen,
      geminiChatOpen,
      fileManagerMinimized,
      calendarMinimized,
      clockMinimized,
      messagesMinimized,
      geminiChatMinimized
    })
  }, [fileManagerOpen, calendarOpen, clockOpen, messagesOpen, geminiChatOpen, fileManagerMinimized, calendarMinimized, clockMinimized, messagesMinimized, geminiChatMinimized])

  return (
    <div className="relative h-screen w-screen overflow-hidden flex touch-auto" onContextMenu={handleDesktopRightClick}>
      <div
        className="absolute inset-0 transition-all duration-500 ease-in-out"
        style={getBackgroundStyle()}
      >
        {/* Overlay for better text visibility */}
        {!currentBackground.startsWith('gradient-') && (
          <div
            className="absolute inset-0 bg-black/20"
            style={{
              mixBlendMode: 'multiply'
            }}
          />
        )}
        <svg className="absolute inset-0 w-full h-full opacity-10 pointer-events-none" xmlns="http://www.w3.org/2000/svg">
          <defs>
            <pattern id="ubuntu-pattern" x="0" y="0" width="100" height="100" patternUnits="userSpaceOnUse">
              <circle cx="50" cy="50" r="2" fill="white" opacity="0.3" />
            </pattern>
          </defs>
          <rect width="100%" height="100%" fill="url(#ubuntu-pattern)" />
        </svg>
      </div>

      <TopBar
        onAboutClick={() => setAboutModalOpen(true)}
        onSleep={handleSleep}
        onRestart={handleRestart}
        onShutdown={handleShutdown}
      />

      <Dock
        onOpenFileManager={openFileManager}
        onOpenCalendar={openCalendar}
        onOpenClock={openClock}
        onOpenMessages={openMessages}
        onOpenGeminiChat={openGeminiChat}
        onCloseAllApps={closeAllApps}
        openApps={{
          files: fileManagerOpen,
          calendar: calendarOpen,
          clock: clockOpen,
          messages: messagesOpen,
          gemini: geminiChatOpen
        }}
        minimizedApps={minimizedApps}
      />

      <div className="flex-1 z-10 relative pointer-events-auto">
        {/* Desktop Icons - Responsive grid layout, visible on all screen sizes */}
        <div className="absolute top-16 left-2 right-2 sm:left-4 sm:right-4 bottom-24 grid grid-cols-3 xs:grid-cols-4 sm:grid-cols-4 md:grid-cols-5 lg:grid-flow-col lg:grid-cols-none lg:grid-rows-[repeat(auto-fill,100px)] gap-2 sm:gap-4 pointer-events-none overflow-y-auto lg:overflow-visible z-0">
          <div className="pointer-events-auto w-20 h-20 sm:w-24 sm:h-24">
            <DraggableDesktopIcon
              id="files"
              label="Files"
              iconType="files"
              initialPosition={{ x: 0, y: 0 }}
              onClick={() => { }}
              onDoubleClick={() => openFileManager('')}
            />
          </div>

          <div className="pointer-events-auto w-20 h-20 sm:w-24 sm:h-24">
            <DraggableDesktopIcon
              id="messages"
              label="Messages"
              iconType="messages"
              initialPosition={{ x: 0, y: 0 }}
              onClick={() => { }}
              onDoubleClick={openMessages}
            />
          </div>

          <div className="pointer-events-auto w-20 h-20 sm:w-24 sm:h-24">
            <DraggableDesktopIcon
              id="gemini"
              label="Gemini"
              iconType="gemini"
              initialPosition={{ x: 0, y: 0 }}
              onClick={() => { }}
              onDoubleClick={openGeminiChat}
            />
          </div>
          <div className="pointer-events-auto w-20 h-20 sm:w-24 sm:h-24">
            <DraggableDesktopIcon
              id="clock"
              label="Clock"
              iconType="clock"
              initialPosition={{ x: 0, y: 0 }}
              onClick={() => { }}
              onDoubleClick={openClock}
            />
          </div>

          <div className="pointer-events-auto w-20 h-20 sm:w-24 sm:h-24">
            <DraggableDesktopIcon
              id="calendar"
              label="Calendar"
              iconType="calendar"
              initialPosition={{ x: 0, y: 0 }}
              onClick={() => { }}
              onDoubleClick={openCalendar}
            />
          </div>

          <div className="pointer-events-auto w-20 h-20 sm:w-24 sm:h-24">
            <DraggableDesktopIcon
              id="harddrive"
              label="System"
              iconType="harddrive"
              initialPosition={{ x: 0, y: 0 }}
              onClick={() => { }}
              onDoubleClick={() => openFileManager('')}
            />
          </div>

          <div className="pointer-events-auto w-20 h-20 sm:w-24 sm:h-24">
            <DraggableDesktopIcon
              id="flutter-editor"
              label="Flutter IDE"
              iconType="flutter"
              initialPosition={{ x: 0, y: 0 }}
              onClick={() => { }}
              onDoubleClick={openFlutterCodeEditor}
            />
          </div>

          <div className="pointer-events-auto w-20 h-20 sm:w-24 sm:h-24">
            <DraggableDesktopIcon
              id="quiz"
              label="Quiz Master"
              iconType="quiz"
              initialPosition={{ x: 0, y: 0 }}
              onClick={() => { }}
              onDoubleClick={openQuizApp}
            />
          </div>

          <div className="pointer-events-auto w-20 h-20 sm:w-24 sm:h-24">
            <DraggableDesktopIcon
              id="voice-app"
              label="Voice Studio"
              iconType="voice-app"
              initialPosition={{ x: 0, y: 0 }}
              onClick={() => { }}
              onDoubleClick={openVoiceApp}
            />
          </div>
        </div>

        {/* Windows Container - adjusted for mobile topbar height */}
        <div className="fixed inset-0 pointer-events-none" style={{ position: 'fixed', top: '40px', left: 0, right: 0, bottom: 0 }}>
          <AnimatePresence>
            {fileManagerOpen && (
              <motion.div
                key="file-manager"
                initial={{ opacity: 0, scale: 0.95 }}
                animate={{
                  opacity: fileManagerMinimized ? 0 : 1,
                  scale: fileManagerMinimized ? 0.9 : 1,
                  y: fileManagerMinimized ? 100 : 0,
                }}
                exit={{ opacity: 0, scale: 0.95 }}
                transition={{ duration: 0.2 }}
                style={{
                  pointerEvents: fileManagerMinimized ? 'none' : 'auto',
                  display: fileManagerMinimized ? 'none' : 'block'
                }}
              >
                <FileManager
                  currentPath={currentPath}
                  onNavigate={setCurrentPath}
                  onClose={closeFileManager}
                  onOpenFlutterApp={openFlutterRunner}
                  onMinimize={() => setFileManagerMinimized(true)}
                  onFocus={() => bringWindowToFront('fileManager')}
                  zIndex={windowZIndices.fileManager || 1000}
                  onOpenApp={handleOpenApp}
                  onOpenTextFile={openTextEditor}
                />
              </motion.div>
            )}

            {calendarOpen && (
              <motion.div
                key="calendar"
                initial={{ opacity: 0, scale: 0.95 }}
                animate={{
                  opacity: calendarMinimized ? 0 : 1,
                  scale: calendarMinimized ? 0.9 : 1,
                  y: calendarMinimized ? 100 : 0,
                }}
                exit={{ opacity: 0, scale: 0.95 }}
                transition={{ duration: 0.2 }}
                style={{
                  pointerEvents: calendarMinimized ? 'none' : 'auto',
                  display: calendarMinimized ? 'none' : 'block'
                }}
              >
                <Calendar
                  onClose={closeCalendar}
                  onMinimize={() => setCalendarMinimized(true)}
                  onFocus={() => bringWindowToFront('calendar')}
                  zIndex={windowZIndices.calendar || 1000}
                />
              </motion.div>
            )}

            {clockOpen && (
              <motion.div
                key="clock"
                initial={{ opacity: 0, scale: 0.95 }}
                animate={{
                  opacity: clockMinimized ? 0 : 1,
                  scale: clockMinimized ? 0.9 : 1,
                  y: clockMinimized ? 100 : 0,
                }}
                exit={{ opacity: 0, scale: 0.95 }}
                transition={{ duration: 0.2 }}
                style={{
                  pointerEvents: clockMinimized ? 'none' : 'auto',
                  display: clockMinimized ? 'none' : 'block'
                }}
              >
                <Clock
                  onClose={closeClock}
                  onMinimize={() => setClockMinimized(true)}
                  onFocus={() => bringWindowToFront('clock')}
                  zIndex={windowZIndices.clock || 1000}
                />
              </motion.div>
            )}

            {messagesOpen && (
              <motion.div
                key="messages"
                initial={{ opacity: 0, scale: 0.95 }}
                animate={{
                  opacity: messagesMinimized ? 0 : 1,
                  scale: messagesMinimized ? 0.9 : 1,
                  y: messagesMinimized ? 100 : 0,
                }}
                exit={{ opacity: 0, scale: 0.95 }}
                transition={{ duration: 0.2 }}
                style={{
                  pointerEvents: messagesMinimized ? 'none' : 'auto',
                  display: messagesMinimized ? 'none' : 'block'
                }}
              >
                <Messages
                  onClose={closeMessages}
                  onMinimize={() => setMessagesMinimized(true)}
                  onFocus={() => bringWindowToFront('messages')}
                  zIndex={windowZIndices.messages || 1000}
                />
              </motion.div>
            )}

            {geminiChatOpen && (
              <motion.div
                key="gemini"
                initial={{ opacity: 0, scale: 0.95 }}
                animate={{
                  opacity: geminiChatMinimized ? 0 : 1,
                  scale: geminiChatMinimized ? 0.9 : 1,
                  y: geminiChatMinimized ? 100 : 0,
                }}
                exit={{ opacity: 0, scale: 0.95 }}
                transition={{ duration: 0.2 }}
                style={{
                  pointerEvents: geminiChatMinimized ? 'none' : 'auto',
                  display: geminiChatMinimized ? 'none' : 'block'
                }}
              >
                <GeminiChat
                  onClose={closeGeminiChat}
                  onMinimize={() => setGeminiChatMinimized(true)}
                  onFocus={() => bringWindowToFront('gemini')}
                  zIndex={windowZIndices.gemini || 1000}
                />
              </motion.div>
            )}

            {flutterRunnerOpen && activeFlutterApp && (
              <motion.div
                key="flutter-runner"
                initial={{ opacity: 0, scale: 0.95 }}
                animate={{
                  opacity: flutterRunnerMinimized ? 0 : 1,
                  scale: flutterRunnerMinimized ? 0.9 : 1,
                  y: flutterRunnerMinimized ? 100 : 0,
                }}
                exit={{ opacity: 0, scale: 0.95 }}
                transition={{ duration: 0.2 }}
                style={{
                  pointerEvents: flutterRunnerMinimized ? 'none' : 'auto',
                  display: flutterRunnerMinimized ? 'none' : 'block'
                }}
              >
                <FlutterRunner
                  onClose={() => {
                    setFlutterRunnerOpen(false)
                    setActiveFlutterApp(null)
                  }}
                  onMinimize={() => setFlutterRunnerMinimized(true)}
                  onFocus={() => bringWindowToFront('flutterRunner')}
                  zIndex={windowZIndices.flutterRunner || 1000}
                />
              </motion.div>
            )}

            {flutterCodeEditorOpen && (
              <motion.div
                key="flutter-code-editor"
                initial={{ opacity: 0, scale: 0.95 }}
                animate={{
                  opacity: flutterCodeEditorMinimized ? 0 : 1,
                  scale: flutterCodeEditorMinimized ? 0.9 : 1,
                  y: flutterCodeEditorMinimized ? 100 : 0,
                }}
                exit={{ opacity: 0, scale: 0.95 }}
                transition={{ duration: 0.2 }}
                style={{
                  pointerEvents: flutterCodeEditorMinimized ? 'none' : 'auto',
                  display: flutterCodeEditorMinimized ? 'none' : 'block'
                }}
              >
                <FlutterRunner
                  onClose={closeFlutterCodeEditor}
                  onMinimize={() => setFlutterCodeEditorMinimized(true)}
                  onFocus={() => bringWindowToFront('flutterCodeEditor')}
                  zIndex={windowZIndices.flutterCodeEditor || 1000}
                />
              </motion.div>
            )}

            {quizAppOpen && (
              <motion.div
                key="quiz-app"
                initial={{ opacity: 0, scale: 0.95 }}
                animate={{
                  opacity: quizAppMinimized ? 0 : 1,
                  scale: quizAppMinimized ? 0.9 : 1,
                  y: quizAppMinimized ? 100 : 0,
                }}
                exit={{ opacity: 0, scale: 0.95 }}
                transition={{ duration: 0.2 }}
                style={{
                  pointerEvents: quizAppMinimized ? 'none' : 'auto',
                  display: quizAppMinimized ? 'none' : 'block'
                }}
              >
                <QuizApp
                  onClose={closeQuizApp}
                  onMinimize={() => setQuizAppMinimized(true)}
                  onFocus={() => bringWindowToFront('quizApp')}
                  zIndex={windowZIndices.quizApp || 1000}
                />
              </motion.div>
            )}

            {textEditorOpen && activeTextFile && (
              <motion.div
                key="text-editor"
                initial={{ opacity: 0, scale: 0.95 }}
                animate={{
                  opacity: textEditorMinimized ? 0 : 1,
                  scale: textEditorMinimized ? 0.9 : 1,
                  y: textEditorMinimized ? 100 : 0,
                }}
                exit={{ opacity: 0, scale: 0.95 }}
                transition={{ duration: 0.2 }}
                style={{
                  pointerEvents: textEditorMinimized ? 'none' : 'auto',
                  display: textEditorMinimized ? 'none' : 'block'
                }}
              >
                <TextEditor
                  initialContent={activeTextFile.content}
                  fileName={activeTextFile.fileName}
                  filePath={activeTextFile.filePath}
                  passkey={activeTextFile.passkey}
                  onClose={closeTextEditor}
                  onMinimize={() => setTextEditorMinimized(true)}
                  onFocus={() => bringWindowToFront('textEditor')}
                  zIndex={windowZIndices.textEditor || 1000}
                />
              </motion.div>
            )}

            {voiceAppOpen && (
              <motion.div
                key="voice-app"
                initial={{ opacity: 0, scale: 0.95 }}
                animate={{
                  opacity: voiceAppMinimized ? 0 : 1,
                  scale: voiceAppMinimized ? 0.9 : 1,
                  y: voiceAppMinimized ? 100 : 0,
                }}
                exit={{ opacity: 0, scale: 0.95 }}
                transition={{ duration: 0.2 }}
                style={{
                  pointerEvents: voiceAppMinimized ? 'none' : 'auto',
                  display: voiceAppMinimized ? 'none' : 'block'
                }}
              >
                <VoiceApp
                  onClose={closeVoiceApp}
                  onMinimize={() => setVoiceAppMinimized(true)}
                  onFocus={() => bringWindowToFront('voiceApp')}
                  zIndex={windowZIndices.voiceApp || 1000}
                />
              </motion.div>
            )}
          </AnimatePresence>
        </div>
      </div>

      {/* Spotlight Search */}
      <SpotlightSearch
        isOpen={spotlightOpen}
        onClose={() => setSpotlightOpen(false)}
        onOpenApp={handleOpenApp}
      />

      {/* Context Menu */}
      <ContextMenu
        isOpen={contextMenuVisible}
        position={contextMenuPosition}
        onClose={() => setContextMenuVisible(false)}
        onAction={handleContextMenuAction}
      />

      {/* Help Modal */}
      <HelpModal isOpen={helpModalOpen} onClose={closeHelpModal} />

      {/* Desktop Context Menu */}
      <DesktopContextMenu
        isOpen={contextMenuOpen}
        position={contextMenuPos}
        onClose={() => setContextMenuOpen(false)}
        onChangeBackground={handleChangeBackground}
      />

      {/* Background Selector Modal */}
      <BackgroundSelector
        isOpen={backgroundSelectorOpen}
        onClose={() => setBackgroundSelectorOpen(false)}
        onSelectBackground={handleSelectBackground}
        currentBackground={currentBackground}
      />

      {/* System Power Overlay */}
      <SystemPowerOverlay
        state={powerState}
        onWake={handleWake}
        onRestartComplete={handleWake}
      />

      {/* About Modal */}
      <AboutModal
        isOpen={aboutModalOpen}
        onClose={() => setAboutModalOpen(false)}
      />
    </div>
  )
}