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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>3D Flappy Bird</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            overflow: hidden;
            background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
            min-height: 100vh;
        }

        #game-container {
            position: relative;
            width: 100vw;
            height: 100vh;
            overflow: hidden;
        }

        #canvas-container {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }

        /* Header */
        .header {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 20px 30px;
            z-index: 100;
            background: linear-gradient(180deg, rgba(0,0,0,0.4) 0%, transparent 100%);
        }

        .logo {
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .logo-icon {
            width: 45px;
            height: 45px;
            background: linear-gradient(135deg, #ffd700, #ffaa00);
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 4px 15px rgba(255, 215, 0, 0.4);
            animation: pulse 2s infinite;
        }

        @keyframes pulse {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.05); }
        }

        .logo-text {
            color: #fff;
            font-size: 24px;
            font-weight: 700;
            text-shadow: 0 2px 10px rgba(0,0,0,0.3);
        }

        .anycoder-link {
            color: rgba(255,255,255,0.7);
            text-decoration: none;
            font-size: 14px;
            padding: 8px 16px;
            border: 1px solid rgba(255,255,255,0.2);
            border-radius: 20px;
            transition: all 0.3s ease;
            backdrop-filter: blur(10px);
        }

        .anycoder-link:hover {
            background: rgba(255,255,255,0.1);
            color: #fff;
            border-color: rgba(255,255,255,0.4);
        }

        /* Score Display */
        .score-container {
            position: absolute;
            top: 100px;
            left: 50%;
            transform: translateX(-50%);
            z-index: 100;
            text-align: center;
        }

        .current-score {
            font-size: 72px;
            font-weight: 800;
            color: #fff;
            text-shadow: 0 4px 20px rgba(0,0,0,0.3), 0 0 40px rgba(255,215,0,0.2);
            line-height: 1;
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .current-score.visible {
            opacity: 1;
        }

        .best-score {
            font-size: 16px;
            color: rgba(255,255,255,0.6);
            margin-top: 8px;
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .best-score.visible {
            opacity: 1;
        }

        /* Start Screen */
        .start-screen {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            z-index: 200;
            background: rgba(0,0,0,0.6);
            backdrop-filter: blur(10px);
            transition: opacity 0.5s ease, visibility 0.5s ease;
        }

        .start-screen.hidden {
            opacity: 0;
            visibility: hidden;
        }

        .game-title {
            font-size: 64px;
            font-weight: 800;
            color: #fff;
            text-shadow: 0 4px 30px rgba(255,215,0,0.5);
            margin-bottom: 10px;
            animation: float 3s ease-in-out infinite;
        }

        .game-subtitle {
            font-size: 18px;
            color: rgba(255,255,255,0.6);
            margin-bottom: 50px;
        }

        @keyframes float {
            0%, 100% { transform: translateY(0); }
            50% { transform: translateY(-10px); }
        }

        .start-btn {
            padding: 18px 60px;
            font-size: 22px;
            font-weight: 700;
            color: #1a1a2e;
            background: linear-gradient(135deg, #ffd700 0%, #ffaa00 100%);
            border: none;
            border-radius: 50px;
            cursor: pointer;
            box-shadow: 0 8px 30px rgba(255,215,0,0.4);
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }

        .start-btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.4), transparent);
            transition: left 0.5s ease;
        }

        .start-btn:hover::before {
            left: 100%;
        }

        .start-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 12px 40px rgba(255,215,0,0.5);
        }

        .start-btn:active {
            transform: translateY(0);
        }

        .instructions {
            margin-top: 40px;
            display: flex;
            gap: 30px;
            color: rgba(255,255,255,0.5);
            font-size: 14px;
        }

        .instruction-item {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .key {
            padding: 6px 12px;
            background: rgba(255,255,255,0.1);
            border-radius: 6px;
            font-weight: 600;
        }

        /* Game Over Screen */
        .game-over-screen {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            z-index: 200;
            background: rgba(0,0,0,0.7);
            backdrop-filter: blur(10px);
            opacity: 0;
            visibility: hidden;
            transition: opacity 0.5s ease, visibility 0.5s ease;
        }

        .game-over-screen.visible {
            opacity: 1;
            visibility: visible;
        }

        .game-over-title {
            font-size: 56px;
            font-weight: 800;
            color: #ff6b6b;
            text-shadow: 0 4px 30px rgba(255,107,107,0.5);
            margin-bottom: 30px;
            animation: shake 0.5s ease;
        }

        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            25% { transform: translateX(-10px); }
            75% { transform: translateX(10px); }
        }

        .final-score-container {
            background: rgba(255,255,255,0.1);
            padding: 30px 50px;
            border-radius: 20px;
            margin-bottom: 30px;
            text-align: center;
        }

        .final-score-label {
            font-size: 16px;
            color: rgba(255,255,255,0.6);
            margin-bottom: 8px;
        }

        .final-score {
            font-size: 72px;
            font-weight: 800;
            color: #ffd700;
            text-shadow: 0 4px 20px rgba(255,215,0,0.5);
            line-height: 1;
        }

        .new-record {
            color: #4ecdc4;
            font-size: 18px;
            margin-top: 10px;
            opacity: 0;
            transform: scale(0.5);
            transition: all 0.3s ease;
        }

        .new-record.visible {
            opacity: 1;
            transform: scale(1);
        }

        .restart-btn {
            padding: 16px 50px;
            font-size: 20px;
            font-weight: 700;
            color: #1a1a2e;
            background: linear-gradient(135deg, #4ecdc4 0%, #44a08d 100%);
            border: none;
            border-radius: 50px;
            cursor: pointer;
            box-shadow: 0 8px 30px rgba(78,205,196,0.4);
            transition: all 0.3s ease;
        }

        .restart-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 12px 40px rgba(78,205,196,0.5);
        }

        /* Mobile Controls */
        .mobile-controls {
            position: absolute;
            bottom: 30px;
            left: 50%;
            transform: translateX(-50%);
            z-index: 100;
            display: none;
        }

        .tap-area {
            width: 120px;
            height: 120px;
            background: rgba(255,255,255,0.1);
            border: 2px solid rgba(255,255,255,0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.3s ease;
            backdrop-filter: blur(10px);
        }

        .tap-area:hover, .tap-area:active {
            background: rgba(255,255,255,0.2);
            transform: scale(0.95);
        }

        .tap-icon {
            font-size: 40px;
        }

        /* Responsive Design */
        @media (max-width: 768px) {
            .header {
                padding: 15px 20px;
            }

            .logo-text {
                font-size: 18px;
            }

            .logo-icon {
                width: 35px;
                height: 35px;
            }

            .anycoder-link {
                font-size: 12px;
                padding: 6px 12px;
            }

            .game-title {
                font-size: 42px;
            }

            .game-subtitle {
                font-size: 14px;
            }

            .start-btn {
                padding: 14px 40px;
                font-size: 18px;
            }

            .current-score {
                font-size: 56px;
            }

            .instructions {
                flex-direction: column;
                gap: 15px;
            }

            .mobile-controls {
                display: block;
            }

            .game-over-title {
                font-size: 42px;
            }

            .final-score {
                font-size: 56px;
            }
        }

        @media (max-width: 480px) {
            .game-title {
                font-size: 36px;
            }

            .current-score {
                font-size: 48px;
            }

            .tap-area {
                width: 100px;
                height: 100px;
            }
        }

        /* Loading Animation */
        .loading {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 300;
        }

        .loading-spinner {
            width: 50px;
            height: 50px;
            border: 4px solid rgba(255,255,255,0.1);
            border-top-color: #ffd700;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        /* Particle effects container */
        #particles {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            pointer-events: none;
            z-index: 50;
        }
    </style>
</head>
<body>
    <div id="game-container">
        <div id="canvas-container"></div>
        
        <!-- Header -->
        <header class="header">
            <div class="logo">
                <div class="logo-icon">🐦</div>
                <span class="logo-text">Flappy 3D</span>
            </div>
            <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" class="anycoder-link">Built with anycoder</a>
        </header>

        <!-- Score -->
        <div class="score-container">
            <div class="current-score" id="current-score">0</div>
            <div class="best-score" id="best-score">Best: 0</div>
        </div>

        <!-- Start Screen -->
        <div class="start-screen" id="start-screen">
            <h1 class="game-title">Flappy 3D</h1>
            <p class="game-subtitle">Experience the classic in stunning 3D</p>
            <button class="start-btn" id="start-btn">Start Game</button>
            <div class="instructions">
                <div class="instruction-item">
                    <span class="key">SPACE</span>
                    <span>or</span>
                </div>
                <div class="instruction-item">
                    <span class="key">↑</span>
                    <span>to jump</span>
                </div>
            </div>
        </div>

        <!-- Game Over Screen -->
        <div class="game-over-screen" id="game-over-screen">
            <h1 class="game-over-title">Game Over</h1>
            <div class="final-score-container">
                <div class="final-score-label">Score</div>
                <div class="final-score" id="final-score">0</div>
                <div class="new-record" id="new-record">🏆 New Record!</div>
            </div>
            <button class="restart-btn" id="restart-btn">Play Again</button>
        </div>

        <!-- Mobile Controls -->
        <div class="mobile-controls">
            <div class="tap-area" id="tap-area">
                <span class="tap-icon">👆</span>
            </div>
        </div>

        <!-- Loading -->
        <div class="loading" id="loading">
            <div class="loading-spinner"></div>
        </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
    <script>
        // Game State
        const GameState = {
            MENU: 'menu',
            PLAYING: 'playing',
            GAMEOVER: 'gameover'
        };

        // Game Configuration
        const CONFIG = {
            gravity: 0.015,
            jumpStrength: 0.35,
            pipeSpeed: 0.12,
            pipeSpawnDistance: 15,
            pipeGap: 4,
            pipeMinHeight: 2,
            pipeMaxHeight: 6,
            birdSize: 0.8,
            rotationSpeed: 0.1
        };

        // Game Variables
        let scene, camera, renderer;
        let bird, birdGroup;
        let pipes = [];
        let particles = [];
        let gameState = GameState.MENU;
        let score = 0;
        let bestScore = localStorage.getItem('flappy3d_best') || 0;
        let birdVelocity = 0;
        let distance = 0;
        let lastPipeX = 0;
        let animationId;

        // DOM Elements
        const canvasContainer = document.getElementById('canvas-container');
        const startScreen = document.getElementById('start-screen');
        const gameOverScreen = document.getElementById('game-over-screen');
        const startBtn = document.getElementById('start-btn');
        const restartBtn = document.getElementById('restart-btn');
        const currentScoreEl = document.getElementById('current-score');
        const bestScoreEl = document.getElementById('best-score');
        const finalScoreEl = document.getElementById('final-score');
        const newRecordEl = document.getElementById('new-record');
        const tapArea = document.getElementById('tap-area');
        const loadingEl = document.getElementById('loading');

        // Initialize Three.js
        function init() {
            // Scene
            scene = new THREE.Scene();
            scene.fog = new THREE.Fog(0x1a1a2e, 20, 60);

            // Camera
            camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 100);
            camera.position.set(0, 2, 10);
            camera.lookAt(0, 2, 0);

            // Renderer
            renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
            renderer.setSize(window.innerWidth, window.innerHeight);
            renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
            renderer.shadowMap.enabled = true;
            renderer.shadowMap.type = THREE.PCFSoftShadowMap;
            canvasContainer.appendChild(renderer.domElement);

            // Lighting
            const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
            scene.add(ambientLight);

            const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
            directionalLight.position.set(10, 20, 10);
            directionalLight.castShadow = true;
            directionalLight.shadow.mapSize.width = 2048;
            directionalLight.shadow.mapSize.height = 2048;
            directionalLight.shadow.camera.near = 0.5;
            directionalLight.shadow.camera.far = 50;
            directionalLight.shadow.camera.left = -20;
            directionalLight.shadow.camera.right = 20;
            directionalLight.shadow.camera.top = 20;
            directionalLight.shadow.camera.bottom = -20;
            scene.add(directionalLight);

            // Add point light for bird glow
            const pointLight = new THREE.PointLight(0xffd700, 0.5, 10);
            pointLight.position.set(0, 2, 5);
            scene.add(pointLight);

            // Create scene elements
            createBackground();
            createGround();
            createBird();
            createClouds();

            // Event listeners
            window.addEventListener('resize', onWindowResize);
            document.addEventListener('keydown', onKeyDown);
            tapArea.addEventListener('touchstart', (e) => {
                e.preventDefault();
                jump();
            });
            tapArea.addEventListener('click', jump);

            // Update best score display
            bestScoreEl.textContent = `Best: ${bestScore}`;
            bestScoreEl.classList.add('visible');

            // Hide loading
            setTimeout(() => {
                loadingEl.style.display = 'none';
            }, 500);

            // Start render loop
            animate();
        }

        function createBackground() {
            // Create gradient background with particles
            const particleCount = 500;
            const geometry = new THREE.BufferGeometry();
            const positions = new Float32Array(particleCount * 3);
            const colors = new Float32Array(particleCount * 3);

            for (let i = 0; i < particleCount; i++) {
                positions[i * 3] = (Math.random() - 0.5) * 100;
                positions[i * 3 + 1] = Math.random() * 50;
                positions[i * 3 + 2] = (Math.random() - 0.5) * 50 - 10;

                colors[i * 3] = 1;
                colors[i * 3 + 1] = 0.84;
                colors[i * 3 + 2] = 0;
            }

            geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
            geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3));

            const material = new THREE.PointsMaterial({
                size: 0.1,
                vertexColors: true,
                transparent: true,
                opacity: 0.6,
                blending: THREE.AdditiveBlending
            });

            const stars = new THREE.Points(geometry, material);
            scene.add(stars);
        }

        function createGround() {
            // Main ground
            const groundGeometry = new THREE.PlaneGeometry(200, 30, 100, 15);
            const groundMaterial = new THREE.MeshStandardMaterial({
                color: 0x2d5a27,
                roughness: 0.8,
                metalness: 0.1
            });

            // Add some height variation
            const vertices = groundGeometry.attributes.position.array;
            for (let i = 0; i < vertices.length; i += 3) {
                vertices[i + 2] = Math.random() * 0.3;
            }
            groundGeometry.computeVertexNormals();

            const ground = new THREE.Mesh(groundGeometry, groundMaterial);
            ground.rotation.x = -Math.PI / 2;
            ground.position.y = -5;
            ground.receiveShadow = true;
            scene.add(ground);

            // Add grass blades
            const grassGeometry = new THREE.ConeGeometry(0.1, 0.5, 4);
            const grassMaterial = new THREE.MeshStandardMaterial({
                color: 0x3d7a37,
                roughness: 0.9
            });

            for (let i = 0; i < 200; i++) {
                const grass = new THREE.Mesh(grassGeometry, grassMaterial);
                grass.position.set(
                    (Math.random() - 0.5) * 100,
                    -4.75,
                    (Math.random() - 0.5) * 15
                );
                grass.scale.setScalar(0.5 + Math.random() * 0.5);
                grass.rotation.z = (Math.random() - 0.5) * 0.3;
                scene.add(grass);
            }
        }

        function createBird() {
            birdGroup = new THREE.Group();

            // Body
            const bodyGeometry = new THREE.SphereGeometry(CONFIG.birdSize / 2, 32, 32);
            const bodyMaterial = new THREE.MeshStandardMaterial({
                color: 0xffd700,
                roughness: 0.3,
                metalness: 0.5,
                emissive: 0xffd700,
                emissiveIntensity: 0.1
            });
            const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
            body.castShadow = true;
            birdGroup.add(body);

            // Wings
            const wingGeometry = new THREE.BoxGeometry(0.8, 0.1, 0.4);
            const wingMaterial = new THREE.MeshStandardMaterial({
                color: 0xffa500,
                roughness: 0.4
            });

            const leftWing = new THREE.Mesh(wingGeometry, wingMaterial);
            leftWing.position.set(-0.3, 0, 0);
            leftWing.castShadow = true;
            birdGroup.add(leftWing);

            const rightWing = new THREE.Mesh(wingGeometry, wingMaterial);
            rightWing.position.set(0.3, 0, 0);
            rightWing.castShadow = true;
            birdGroup.add(rightWing);

            // Eyes
            const eyeGeometry = new THREE.SphereGeometry(0.1, 16, 16);
            const eyeWhiteMaterial = new THREE.MeshStandardMaterial({ color: 0xffffff });
            const eyePupilMaterial = new THREE.MeshStandardMaterial({ color: 0x000000 });

            const leftEyeWhite = new THREE.Mesh(eyeGeometry, eyeWhiteMaterial);
            leftEyeWhite.position.set(0.15, 0.15, 0.25);
            birdGroup.add(leftEyeWhite);

            const leftEyePupil = new THREE.Mesh(
                new THREE.SphereGeometry(0.05, 8, 8),
                eyePupilMaterial
            );
            leftEyePupil.position.set(0.18, 0.15, 0.3);
            birdGroup.add(leftEyePupil);

            const rightEyeWhite = new THREE.Mesh(eyeGeometry, eyeWhiteMaterial);
            rightEyeWhite.position.set(0.15, 0.15, -0.25);
            birdGroup.add(rightEyeWhite);

            const rightEyePupil = new THREE.Mesh(
                new THREE.SphereGeometry(0.05, 8, 8),
                eyePupilMaterial
            );
            rightEyePupil.position.set(0.18, 0.15, -0.3);
            birdGroup.add(rightEyePupil);

            // Beak
            const beakGeometry = new THREE.ConeGeometry(0.15, 0.4, 4);
            const beakMaterial = new THREE.MeshStandardMaterial({ color: 0xff6600 });
            const beak = new THREE.Mesh(beakGeometry, beakMaterial);
            beak.rotation.z = -Math.PI / 2;
            beak.position.set(0.4, 0, 0);
            birdGroup.add(beak);

            // Crest
            const crestGeometry = new THREE.BoxGeometry(0.1, 0.3, 0.3);
            const crestMaterial = new THREE.MeshStandardMaterial({ color: 0xff0000 });
            const crest = new THREE.Mesh(crestGeometry, crestMaterial);
            crest.position.set(-0.2, 0.3, 0);
            crest.rotation.z = 0.3;
            birdGroup.add(crest);

            birdGroup.position.set(0, 2, 0);
            scene.add(birdGroup);
            bird = birdGroup;
        }

        function createClouds() {
            const cloudGeometry = new THREE.SphereGeometry(1, 16, 16);
            const cloudMaterial = new THREE.MeshStandardMaterial({
                color: 0xffffff,
                transparent: true,
                opacity: 0.8
            });

            for (let i = 0; i < 20; i++) {
                const cloud = new THREE.Group();
                const numSpheres = 3 + Math.floor(Math.random() * 3);

                for (let j = 0; j < numSpheres; j++) {
                    const sphere = new THREE.Mesh(cloudGeometry, cloudMaterial);
                    sphere.position.set(
                        j * 1.2 - numSpheres * 0.6,
                        Math.random() * 0.5,
                        (Math.random() - 0.5) * 0.5
                    );
                    sphere.scale.setScalar(0.8 + Math.random() * 0.5);
                    cloud.add(sphere);
                }

                cloud.position.set(
                    (Math.random() - 0.5) * 150,
                    10 + Math.random() * 15,
                    -20 - Math.random() * 20
                );
                cloud.scale.setScalar(1 + Math.random() * 2);
                scene.add(cloud);
            }
        }

        function createPipe(x) {
            const pipeGroup = new THREE.Group();

            // Pipe material with gradient-like appearance
            const pipeMaterial = new THREE.MeshStandardMaterial({
                color: 0x2ecc71,
                roughness: 0.4,
                metalness: 0.2
            });

            const pipeTopMaterial = new THREE.MeshStandardMaterial({
                color: 0x27ae60,
                roughness: 0.3,
                metalness: 0.3
            });

            // Pipe dimensions
            const pipeRadius = 0.8;
            const pipeLength = 2;

            // Top pipe
            const topHeight = CONFIG.pipeMinHeight + Math.random() * (CONFIG.pipeMaxHeight - CONFIG.pipeMinHeight);
            const topPipeGeometry = new THREE.CylinderGeometry(pipeRadius, pipeRadius, topHeight, 16);
            const topPipe = new THREE.Mesh(topPipeGeometry, pipeMaterial);
            topPipe.position.y = 5 + topHeight / 2;
            topPipe.castShadow = true;
            topPipe.receiveShadow = true;
            pipeGroup.add(topPipe);

            // Top cap
            const capGeometry = new THREE.CylinderGeometry(pipeRadius * 1.2, pipeRadius * 1.2, 0.4, 16);
            const topCap = new THREE.Mesh(capGeometry, pipeTopMaterial);
            topCap.position.y = 5 + topHeight;
            topCap.castShadow = true;
            pipeGroup.add(topCap);

            // Bottom pipe
            const bottomHeight = 12 - topHeight - CONFIG.pipeGap;
            const bottomPipeGeometry = new THREE.CylinderGeometry(pipeRadius, pipeRadius, bottomHeight, 16);
            const bottomPipe = new THREE.Mesh(bottomPipeGeometry, pipeMaterial);
            bottomPipe.position.y = -5 - bottomHeight / 2;
            bottomPipe.castShadow = true;
            bottomPipe.receiveShadow = true;
            pipeGroup.add(bottomPipe);

            // Bottom cap
            const bottomCap = new THREE.Mesh(capGeometry, pipeTopMaterial);
            bottomCap.position.y = -5 - bottomHeight;
            bottomCap.castShadow = true;
            pipeGroup.add(bottomCap);

            // Add stripes to pipe
            const stripeGeometry = new THREE.RingGeometry(pipeRadius * 0.9, pipeRadius * 1.1, 16);
            const stripeMaterial = new THREE.MeshStandardMaterial({
                color: 0x1a8c4a,
                side: THREE.DoubleSide
            });

            for (let i = 0; i < 3; i++) {
                const stripe = new THREE.Mesh(stripeGeometry, stripeMaterial);
                stripe.rotation.x = Math.PI / 2;
                stripe.position.y = 5 + topHeight * (0.2 + i * 0.3);
                pipeGroup.add(stripe);

                const stripe2 = new THREE.Mesh(stripeGeometry, stripeMaterial);
                stripe2.rotation.x = Math.PI / 2;
                stripe2.position.y = -5 - bottomHeight * (0.2 + i * 0.3);
                pipeGroup.add(stripe2);
            }

            pipeGroup.position.x = x;
            pipeGroup.userData = {
                passed: false,
                topHeight: topHeight,
                bottomHeight: bottomHeight
            };

            scene.add(pipeGroup);
            pipes.push(pipeGroup);

            return pipeGroup;
        }

        function jump() {
            if (gameState === GameState.PLAYING) {
                birdVelocity = CONFIG.jumpStrength;
                
                // Create jump particles
                createJumpParticles();
            }
        }

        function createJumpParticles() {
            const particleCount = 10;
            const geometry = new THREE.SphereGeometry(0.05, 8, 8);
            const material = new THREE.MeshBasicMaterial({
                color: 0xffd700,
                transparent: true,
                opacity: 1
            });

            for (let i = 0; i < particleCount; i++) {
                const particle = new THREE.Mesh(geometry, material.clone());
                particle.position.copy(bird.position);
                particle.position.x -= 0.3;
                particle.userData = {
                    velocity: new THREE.Vector3(
                        -0.1 - Math.random() * 0.1,
                        -0.1 + Math.random() * 0.2,
                        (Math.random() - 0.5) * 0.1
                    ),
                    life: 1
                };
                scene.add(particle);
                particles.push(particle);
            }
        }

        function createScoreParticles() {
            const particleCount = 20;
            const geometry = new THREE.SphereGeometry(0.08, 8, 8);
            const material = new THREE.MeshBasicMaterial({
                color: 0x4ecdc4,
                transparent: true,
                opacity: 1
            });

            for (let i = 0; i < particleCount; i++) {
                const particle = new THREE.Mesh(geometry, material.clone());
                particle.position.set(lastPipeX, 0, 0);
                particle.userData = {
                    velocity: new THREE.Vector3(
                        (Math.random() - 0.5) * 0.2,
                        Math.random() * 0.3,
                        (Math.random() - 0.5) * 0.2
                    ),
                    life: 1
                };
                scene.add(particle);
                particles.push(particle);
            }
        }

        function updateParticles() {
            for (let i = particles.length - 1; i >= 0; i--) {
                const particle = particles[i];
                particle.position.add(particle.userData.velocity);
                particle.userData.life -= 0.02;
                particle.material.opacity = particle.userData.life;
                particle.scale.setScalar(particle.userData.life);

                if (particle.userData.life <= 0) {
                    scene.remove(particle);
                    particles.splice(i, 1);
                }
            }
        }

        function checkCollision() {
            const birdBox = new THREE.Box3().setFromObject(bird);
            // Shrink hitbox slightly for better gameplay
            birdBox.min.addScalar(0.2);
            birdBox.max.subScalar(0.2);

            // Check ground collision
            if (bird.position.y < -4) {
                return true;
            }

            // Check pipe collision
            for (const pipe of pipes) {
                const pipeBox = new THREE.Box3().setFromObject(pipe);
                if (birdBox.intersectsBox(pipeBox)) {
                    return true;
                }
            }

            return false;
        }

        function startGame() {
            gameState = GameState.PLAYING;
            score = 0;
            distance = 0;
            birdVelocity = 0;
            lastPipeX = 8;

            // Reset bird position
            bird.position.set(0, 2, 0);
            bird.rotation.set(0, 0, 0);

            // Clear existing pipes
            for (const pipe of pipes) {
                scene.remove(pipe);
            }
            pipes = [];

            // Clear particles
            for (const particle of particles) {
                scene.remove(particle);
            }
            particles = [];

            // Create initial pipes
            for (let i = 0; i < 5; i++) {
                lastPipeX += CONFIG.pipeSpawnDistance;
                createPipe(lastPipeX);
            }

            // Update UI
            currentScoreEl.textContent = score;
            currentScoreEl.classList.add('visible');
            bestScoreEl.classList.remove('visible');
            startScreen.classList.add('hidden');
            gameOverScreen.classList.remove('visible');
        }

        function gameOver() {
            gameState = GameState.GAMEOVER;

            // Check for new record
            if (score > bestScore) {
                bestScore = score;
                localStorage.setItem('flappy3d_best', bestScore);
                newRecordEl.classList.add('visible');
            } else {
                newRecordEl.classList.remove('visible');
            }

            // Update UI
            finalScoreEl.textContent = score;
            currentScoreEl.classList.remove('visible');
            bestScoreEl.textContent = `Best: ${bestScore}`;
            bestScoreEl.classList.add('visible');
            gameOverScreen.classList.add('visible');
        }

        function update() {
            if (gameState !== GameState.PLAYING) return;

            // Apply gravity
            birdVelocity -= CONFIG.gravity;
            bird.position.y += birdVelocity;

            // Rotate bird based on velocity
            bird.rotation.z = Math.min(Math.max(birdVelocity * 3, -0.5), 0.8);

            // Move pipes
            for (let i = pipes.length - 1; i >= 0; i--) {
                const pipe = pipes[i];
                pipe.position.x -= CONFIG.pipeSpeed;

                // Check if bird passed pipe
                if (!pipe.userData.passed && pipe.position.x < bird.position.x) {
                    pipe.userData.passed = true;
                    score++;
                    currentScoreEl.textContent = score;
                    createScoreParticles();
                }

                // Remove off-screen pipes
                if (pipe.position.x < -15) {
                    scene.remove(pipe);
                    pipes.splice(i, 1);
                }
            }

            // Spawn new pipes
            const lastPipe = pipes[pipes.length - 1];
            if (lastPipe && lastPipe.position.x < 30) {
                lastPipeX = lastPipe.position.x + CONFIG.pipeSpawnDistance;
                createPipe(lastPipeX);
            }

            // Update particles
            updateParticles();

            // Check collision
            if (checkCollision()) {
                gameOver();
            }
        }

        function animate() {
            animationId = requestAnimationFrame(animate);

            // Idle animation when in menu
            if (gameState === GameState.MENU && bird) {
                bird.position.y = 2 + Math.sin(Date.now() * 0.003) * 0.3;
                bird.rotation.z = Math.sin(Date.now() * 0.002) * 0.1;
            }

            update();
            renderer.render(scene, camera);
        }

        function onWindowResize() {
            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();
            renderer.setSize(window.innerWidth, window.innerHeight);
        }

        function onKeyDown(event) {
            if (event.code === 'Space' || event.code === 'ArrowUp') {
                event.preventDefault();
                if (gameState === GameState.MENU) {
                    startGame();
                } else if (gameState === GameState.PLAYING) {
                    jump();
                } else if (gameState === GameState.GAMEOVER) {
                    startGame();
                }
            }
        }

        // Event Listeners
        startBtn.addEventListener('click', startGame);
        restartBtn.addEventListener('click', startGame);

        // Initialize game
        init();
    </script>
</body>
</html>