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
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Arcane Dystopia</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        body {
            margin: 0;
            overflow: hidden;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        #game-container {
            position: relative;
            width: 100vw;
            height: 100vh;
        }
        #ui-overlay {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            pointer-events: none;
        }
        .ui-panel {
            pointer-events: auto;
            background-color: rgba(20, 20, 30, 0.8);
            border: 1px solid #4a5568;
            border-radius: 8px;
            padding: 10px;
            color: #e2e8f0;
        }
        .ability {
            transition: all 0.3s ease;
        }
        .ability:hover {
            transform: scale(1.05);
            box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
        }
        .cooldown {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            background-color: rgba(255, 0, 0, 0.5);
            transition: height 0.1s linear;
        }
        .quest-active {
            border-left: 3px solid #48bb78;
        }
        .inventory-item {
            transition: all 0.2s ease;
        }
        .inventory-item:hover {
            transform: scale(1.1);
            box-shadow: 0 0 5px rgba(255, 255, 255, 0.3);
        }
        #crosshair {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 20px;
            height: 20px;
            border: 2px solid rgba(255, 255, 255, 0.8);
            border-radius: 50%;
            pointer-events: none;
        }
        #crosshair::before, #crosshair::after {
            content: '';
            position: absolute;
            background-color: rgba(255, 255, 255, 0.8);
        }
        #crosshair::before {
            width: 2px;
            height: 8px;
            top: -10px;
            left: 50%;
            transform: translateX(-50%);
        }
        #crosshair::after {
            width: 8px;
            height: 2px;
            left: -10px;
            top: 50%;
            transform: translateY(-50%);
        }
    </style>
</head>
<body class="bg-gray-900 text-white">
    <div id="game-container">
        <div id="ui-overlay" class="flex flex-col justify-between p-4">
            <!-- Top Bar -->
            <div class="flex justify-between items-start">
                <!-- Player Stats -->
                <div class="ui-panel flex space-x-6">
                    <div class="flex items-center space-x-2">
                        <i class="fas fa-heart text-red-500"></i>
                        <span id="health">100/100</span>
                    </div>
                    <div class="flex items-center space-x-2">
                        <i class="fas fa-bolt text-yellow-400"></i>
                        <span id="mana">150/150</span>
                    </div>
                    <div class="flex items-center space-x-2">
                        <i class="fas fa-coins text-yellow-300"></i>
                        <span id="gold">250</span>
                    </div>
                </div>
                
                <!-- Quest Info -->
                <div id="quest-info" class="ui-panel max-w-xs hidden">
                    <div class="flex justify-between items-center mb-2">
                        <h3 class="font-bold" id="quest-title">Current Quest</h3>
                        <button id="close-quest" class="text-gray-400 hover:text-white">
                            <i class="fas fa-times"></i>
                        </button>
                    </div>
                    <p id="quest-description">No active quest</p>
                    <div class="mt-2">
                        <div class="w-full bg-gray-700 rounded-full h-2">
                            <div id="quest-progress" class="bg-green-500 h-2 rounded-full" style="width: 0%"></div>
                        </div>
                        <p class="text-xs mt-1 text-right" id="quest-progress-text">0%</p>
                    </div>
                </div>
            </div>
            
            <!-- Center UI -->
            <div class="flex justify-center">
                <div id="crosshair"></div>
            </div>
            
            <!-- Bottom Bar -->
            <div class="flex justify-between items-end">
                <!-- Abilities -->
                <div class="ui-panel flex space-x-4">
                    <div class="ability relative w-16 h-16 bg-gray-800 rounded-lg flex items-center justify-center cursor-pointer" data-ability="fireball">
                        <div class="cooldown" style="height: 0%"></div>
                        <i class="fas fa-fire text-2xl text-orange-500"></i>
                        <div class="absolute bottom-0 right-0 bg-gray-900 text-xs px-1 rounded-tl">Q</div>
                    </div>
                    <div class="ability relative w-16 h-16 bg-gray-800 rounded-lg flex items-center justify-center cursor-pointer" data-ability="frost-nova">
                        <div class="cooldown" style="height: 0%"></div>
                        <i class="fas fa-snowflake text-2xl text-blue-300"></i>
                        <div class="absolute bottom-0 right-0 bg-gray-900 text-xs px-1 rounded-tl">E</div>
                    </div>
                    <div class="ability relative w-16 h-16 bg-gray-800 rounded-lg flex items-center justify-center cursor-pointer" data-ability="lightning">
                        <div class="cooldown" style="height: 0%"></div>
                        <i class="fas fa-bolt text-2xl text-yellow-400"></i>
                        <div class="absolute bottom-0 right-0 bg-gray-900 text-xs px-1 rounded-tl">R</div>
                    </div>
                    <div class="ability relative w-16 h-16 bg-gray-800 rounded-lg flex items-center justify-center cursor-pointer" data-ability="teleport">
                        <div class="cooldown" style="height: 0%"></div>
                        <i class="fas fa-location-arrow text-2xl text-purple-400"></i>
                        <div class="absolute bottom-0 right-0 bg-gray-900 text-xs px-1 rounded-tl">F</div>
                    </div>
                </div>
                
                <!-- Inventory Button -->
                <button id="inventory-btn" class="ui-panel p-3 rounded-full hover:bg-gray-700 transition">
                    <i class="fas fa-backpack text-xl"></i>
                </button>
            </div>
            
            <!-- Inventory Modal -->
            <div id="inventory-modal" class="hidden fixed inset-0 bg-black bg-opacity-70 flex items-center justify-center z-50">
                <div class="ui-panel w-full max-w-2xl">
                    <div class="flex justify-between items-center mb-4">
                        <h2 class="text-xl font-bold">Inventory</h2>
                        <button id="close-inventory" class="text-gray-400 hover:text-white">
                            <i class="fas fa-times"></i>
                        </button>
                    </div>
                    <div class="grid grid-cols-4 gap-4" id="inventory-grid">
                        <!-- Inventory items will be added here -->
                    </div>
                </div>
            </div>
            
            <!-- Quests Modal -->
            <div id="quests-modal" class="hidden fixed inset-0 bg-black bg-opacity-70 flex items-center justify-center z-50">
                <div class="ui-panel w-full max-w-2xl">
                    <div class="flex justify-between items-center mb-4">
                        <h2 class="text-xl font-bold">Quests</h2>
                        <button id="close-quests" class="text-gray-400 hover:text-white">
                            <i class="fas fa-times"></i>
                        </button>
                    </div>
                    <div class="space-y-4" id="quests-list">
                        <!-- Quests will be added here -->
                    </div>
                </div>
            </div>
            
            <!-- Controls Help -->
            <div id="controls-help" class="ui-panel absolute bottom-4 right-4 text-xs opacity-70 hover:opacity-100 transition">
                <p>WASD: Move | SPACE: Jump | Q/E/R/F: Abilities | I: Inventory | J: Quests</p>
            </div>
        </div>
    </div>

    <script>
        // Game State
        const gameState = {
            player: {
                health: 100,
                maxHealth: 100,
                mana: 150,
                maxMana: 150,
                gold: 250,
                position: { x: 0, y: 0, z: 0 },
                rotation: { x: 0, y: 0 },
                velocity: { x: 0, y: 0, z: 0 },
                isGrounded: false,
                abilities: {
                    fireball: { cooldown: 0, maxCooldown: 3, cost: 20 },
                    frostNova: { cooldown: 0, maxCooldown: 8, cost: 35 },
                    lightning: { cooldown: 0, maxCooldown: 15, cost: 50 },
                    teleport: { cooldown: 0, maxCooldown: 10, cost: 40 }
                }
            },
            inventory: [
                { id: 'health-potion', name: 'Health Potion', icon: 'fa-flask', quantity: 3 },
                { id: 'mana-potion', name: 'Mana Potion', icon: 'fa-flask', quantity: 2 },
                { id: 'ancient-scroll', name: 'Ancient Scroll', icon: 'fa-scroll', quantity: 1 },
                { id: 'magic-orb', name: 'Magic Orb', icon: 'fa-globe', quantity: 1 }
            ],
            quests: [
                {
                    id: 'initiation',
                    title: 'Arcane Initiation',
                    description: 'Prove your worth by casting 5 spells in the training grounds.',
                    progress: 0,
                    goal: 5,
                    reward: { gold: 100, item: 'enchanted-ring' },
                    completed: false
                },
                {
                    id: 'rebellion',
                    title: 'Spark of Rebellion',
                    description: 'Find and destroy 3 surveillance drones in the district.',
                    progress: 0,
                    goal: 3,
                    reward: { gold: 200, item: 'cloak-of-shadows' },
                    completed: false
                },
                {
                    id: 'artifact',
                    title: 'Lost Artifact',
                    description: 'Recover the ancient artifact from the ruins beneath the city.',
                    progress: 0,
                    goal: 1,
                    reward: { gold: 500, item: 'staff-of-power' },
                    completed: false
                }
            ],
            activeQuest: 'initiation',
            keys: {
                w: false,
                a: false,
                s: false,
                d: false,
                space: false
            },
            scene: null,
            camera: null,
            renderer: null,
            controls: null,
            clock: new THREE.Clock(),
            lastTime: 0
        };

        // Initialize Three.js scene
        function initScene() {
            const container = document.getElementById('game-container');
            
            // Scene
            gameState.scene = new THREE.Scene();
            gameState.scene.background = new THREE.Color(0x111122);
            gameState.scene.fog = new THREE.FogExp2(0x111122, 0.002);
            
            // Camera
            gameState.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
            gameState.camera.position.set(0, 2, 5);
            
            // Renderer
            gameState.renderer = new THREE.WebGLRenderer({ antialias: true });
            gameState.renderer.setSize(window.innerWidth, window.innerHeight);
            gameState.renderer.shadowMap.enabled = true;
            container.appendChild(gameState.renderer.domElement);
            
            // Controls
            gameState.controls = new THREE.OrbitControls(gameState.camera, gameState.renderer.domElement);
            gameState.controls.enableDamping = true;
            gameState.controls.dampingFactor = 0.25;
            gameState.controls.enableZoom = false;
            gameState.controls.enablePan = false;
            
            // Lights
            const ambientLight = new THREE.AmbientLight(0x404040);
            gameState.scene.add(ambientLight);
            
            const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
            directionalLight.position.set(1, 1, 1);
            directionalLight.castShadow = true;
            gameState.scene.add(directionalLight);
            
            // Ground
            const groundGeometry = new THREE.PlaneGeometry(100, 100);
            const groundMaterial = new THREE.MeshStandardMaterial({ 
                color: 0x333344,
                roughness: 0.8,
                metalness: 0.2
            });
            const ground = new THREE.Mesh(groundGeometry, groundMaterial);
            ground.rotation.x = -Math.PI / 2;
            ground.receiveShadow = true;
            gameState.scene.add(ground);
            
            // Buildings
            const buildingGeometry = new THREE.BoxGeometry(10, 15, 10);
            const buildingMaterial = new THREE.MeshStandardMaterial({ 
                color: 0x222233,
                roughness: 0.7,
                metalness: 0.3
            });
            
            for (let i = 0; i < 20; i++) {
                const building = new THREE.Mesh(buildingGeometry, buildingMaterial);
                building.position.x = (Math.random() - 0.5) * 80;
                building.position.z = (Math.random() - 0.5) * 80;
                building.position.y = 7.5;
                building.castShadow = true;
                building.receiveShadow = true;
                gameState.scene.add(building);
            }
            
            // Player
            const playerGeometry = new THREE.CapsuleGeometry(0.5, 1, 4, 8);
            const playerMaterial = new THREE.MeshStandardMaterial({ 
                color: 0x4a5568,
                roughness: 0.5,
                metalness: 0.5
            });
            gameState.player.mesh = new THREE.Mesh(playerGeometry, playerMaterial);
            gameState.player.mesh.position.set(0, 1, 0);
            gameState.player.mesh.castShadow = true;
            gameState.scene.add(gameState.player.mesh);
            
            // Event listeners
            window.addEventListener('resize', onWindowResize);
            document.addEventListener('keydown', onKeyDown);
            document.addEventListener('keyup', onKeyUp);
            
            // UI Event Listeners
            document.querySelectorAll('.ability').forEach(ability => {
                ability.addEventListener('click', () => castSpell(ability.dataset.ability));
            });
            
            document.getElementById('inventory-btn').addEventListener('click', openInventory);
            document.getElementById('close-inventory').addEventListener('click', closeInventory);
            document.getElementById('close-quest').addEventListener('click', () => {
                document.getElementById('quest-info').classList.add('hidden');
            });
            
            // Initialize UI
            updateUI();
            loadInventory();
            updateQuestUI();
            
            // Start game loop
            animate();
        }
        
        // Game loop
        function animate() {
            requestAnimationFrame(animate);
            
            const deltaTime = gameState.clock.getDelta();
            const time = gameState.clock.getElapsedTime();
            
            // Update player movement
            updatePlayer(deltaTime);
            
            // Update cooldowns
            updateCooldowns(deltaTime);
            
            // Update controls
            gameState.controls.update();
            
            // Render scene
            gameState.renderer.render(gameState.scene, gameState.camera);
            
            // Update UI every 10 frames
            if (Math.floor(time * 10) % 10 === 0) {
                updateUI();
            }
            
            gameState.lastTime = time;
        }
        
        // Update player movement
        function updatePlayer(deltaTime) {
            const moveSpeed = 5;
            const jumpForce = 8;
            const gravity = -20;
            
            // Movement direction
            const direction = new THREE.Vector3();
            
            if (gameState.keys.w) direction.z -= 1;
            if (gameState.keys.s) direction.z += 1;
            if (gameState.keys.a) direction.x -= 1;
            if (gameState.keys.d) direction.x += 1;
            
            // Normalize direction
            if (direction.length() > 0) {
                direction.normalize();
            }
            
            // Apply movement
            const moveDistance = moveSpeed * deltaTime;
            direction.multiplyScalar(moveDistance);
            
            // Apply rotation to movement direction
            direction.applyEuler(new THREE.Euler(0, gameState.camera.rotation.y, 0));
            
            gameState.player.velocity.x = direction.x;
            gameState.player.velocity.z = direction.z;
            
            // Jumping
            if (gameState.keys.space && gameState.player.isGrounded) {
                gameState.player.velocity.y = jumpForce;
                gameState.player.isGrounded = false;
            }
            
            // Apply gravity
            gameState.player.velocity.y += gravity * deltaTime;
            
            // Update position
            gameState.player.mesh.position.x += gameState.player.velocity.x;
            gameState.player.mesh.position.y += gameState.player.velocity.y;
            gameState.player.mesh.position.z += gameState.player.velocity.z;
            
            // Ground collision
            if (gameState.player.mesh.position.y < 1) {
                gameState.player.mesh.position.y = 1;
                gameState.player.velocity.y = 0;
                gameState.player.isGrounded = true;
            }
            
            // Update camera position to follow player
            gameState.camera.position.x = gameState.player.mesh.position.x;
            gameState.camera.position.z = gameState.player.mesh.position.z + 5;
            gameState.camera.position.y = gameState.player.mesh.position.y + 2;
            gameState.camera.lookAt(
                gameState.player.mesh.position.x,
                gameState.player.mesh.position.y + 1,
                gameState.player.mesh.position.z
            );
        }
        
        // Update ability cooldowns
        function updateCooldowns(deltaTime) {
            for (const ability in gameState.player.abilities) {
                if (gameState.player.abilities[ability].cooldown > 0) {
                    gameState.player.abilities[ability].cooldown -= deltaTime;
                    if (gameState.player.abilities[ability].cooldown < 0) {
                        gameState.player.abilities[ability].cooldown = 0;
                    }
                    
                    // Update UI
                    const abilityElement = document.querySelector(`.ability[data-ability="${ability}"]`);
                    if (abilityElement) {
                        const cooldownElement = abilityElement.querySelector('.cooldown');
                        const cooldownPercent = (gameState.player.abilities[ability].cooldown / 
                                                gameState.player.abilities[ability].maxCooldown) * 100;
                        cooldownElement.style.height = `${cooldownPercent}%`;
                    }
                }
            }
        }
        
        // Cast a spell
        function castSpell(spellName) {
            const ability = gameState.player.abilities[spellName];
            
            // Check if ability is on cooldown or not enough mana
            if (ability.cooldown > 0 || gameState.player.mana < ability.cost) {
                // Play error sound or show feedback
                return;
            }
            
            // Deduct mana
            gameState.player.mana -= ability.cost;
            ability.cooldown = ability.maxCooldown;
            
            // Spell effects
            switch (spellName) {
                case 'fireball':
                    createFireball();
                    break;
                case 'frost-nova':
                    createFrostNova();
                    break;
                case 'lightning':
                    createLightning();
                    break;
                case 'teleport':
                    teleportPlayer();
                    break;
            }
            
            // Update quest progress if it's the initiation quest
            if (gameState.activeQuest === 'initiation' && !gameState.quests[0].completed) {
                gameState.quests[0].progress++;
                if (gameState.quests[0].progress >= gameState.quests[0].goal) {
                    completeQuest('initiation');
                }
                updateQuestUI();
            }
            
            updateUI();
        }
        
        // Create a fireball projectile
        function createFireball() {
            const fireballGeometry = new THREE.SphereGeometry(0.3, 16, 16);
            const fireballMaterial = new THREE.MeshStandardMaterial({ 
                color: 0xff6600,
                emissive: 0xff3300,
                emissiveIntensity: 1,
                roughness: 0.1,
                metalness: 0.3
            });
            const fireball = new THREE.Mesh(fireballGeometry, fireballMaterial);
            
            // Position fireball in front of player
            fireball.position.copy(gameState.player.mesh.position);
            fireball.position.y += 1;
            
            // Add light
            const fireballLight = new THREE.PointLight(0xff6600, 2, 10);
            fireball.add(fireballLight);
            
            gameState.scene.add(fireball);
            
            // Fireball direction
            const direction = new THREE.Vector3(0, 0, -1);
            direction.applyQuaternion(gameState.camera.quaternion);
            direction.normalize();
            
            // Fireball speed
            const speed = 20;
            
            // Animate fireball
            const fireballInterval = setInterval(() => {
                fireball.position.x += direction.x * speed * 0.016;
                fireball.position.y += direction.y * speed * 0.016;
                fireball.position.z += direction.z * speed * 0.016;
                
                // Check if fireball is out of bounds
                if (fireball.position.length() > 50) {
                    clearInterval(fireballInterval);
                    gameState.scene.remove(fireball);
                }
                
                // Check for collisions (simplified)
                // In a real game, you'd use a physics engine for this
            }, 16);
            
            // Remove fireball after 3 seconds
            setTimeout(() => {
                clearInterval(fireballInterval);
                gameState.scene.remove(fireball);
            }, 3000);
        }
        
        // Create a frost nova effect
        function createFrostNova() {
            const frostGeometry = new THREE.SphereGeometry(5, 32, 32);
            const frostMaterial = new THREE.MeshStandardMaterial({ 
                color: 0x00aaff,
                transparent: true,
                opacity: 0.5,
                roughness: 0.2,
                metalness: 0.7
            });
            const frostNova = new THREE.Mesh(frostGeometry, frostMaterial);
            frostNova.position.copy(gameState.player.mesh.position);
            frostNova.position.y += 1;
            
            // Add light
            const frostLight = new THREE.PointLight(0x00aaff, 3, 10);
            frostNova.add(frostLight);
            
            gameState.scene.add(frostNova);
            
            // Animate frost nova
            let scale = 0.1;
            const frostInterval = setInterval(() => {
                scale += 0.1;
                frostNova.scale.set(scale, scale, scale);
                
                if (scale >= 1) {
                    clearInterval(frostInterval);
                    
                    // Fade out
                    let opacity = 0.5;
                    const fadeInterval = setInterval(() => {
                        opacity -= 0.05;
                        frostMaterial.opacity = opacity;
                        
                        if (opacity <= 0) {
                            clearInterval(fadeInterval);
                            gameState.scene.remove(frostNova);
                        }
                    }, 50);
                }
            }, 50);
        }
        
        // Create a lightning strike
        function createLightning() {
            const lightningGroup = new THREE.Group();
            lightningGroup.position.copy(gameState.player.mesh.position);
            lightningGroup.position.y += 10;
            
            // Create multiple lightning bolts
            for (let i = 0; i < 5; i++) {
                const points = [];
                const height = 10 + Math.random() * 5;
                const segments = 10;
                
                for (let j = 0; j <= segments; j++) {
                    const offset = j === 0 || j === segments ? 0 : Math.random() * 0.5 - 0.25;
                    points.push(new THREE.Vector3(
                        offset,
                        -height * (j / segments),
                        offset
                    ));
                }
                
                const lightningGeometry = new THREE.BufferGeometry().setFromPoints(points);
                const lightningMaterial = new THREE.LineBasicMaterial({ 
                    color: 0x88ccff,
                    transparent: true,
                    opacity: 0.8
                });
                
                const lightning = new THREE.Line(lightningGeometry, lightningMaterial);
                lightningGroup.add(lightning);
            }
            
            gameState.scene.add(lightningGroup);
            
            // Add light
            const lightningLight = new THREE.PointLight(0x88ccff, 5, 20);
            lightningGroup.add(lightningLight);
            
            // Fade out
            let opacity = 0.8;
            const fadeInterval = setInterval(() => {
                opacity -= 0.05;
                lightningGroup.children.forEach(child => {
                    if (child.material) child.material.opacity = opacity;
                });
                
                if (opacity <= 0) {
                    clearInterval(fadeInterval);
                    gameState.scene.remove(lightningGroup);
                }
            }, 50);
        }
        
        // Teleport player forward
        function teleportPlayer() {
            const distance = 8;
            const direction = new THREE.Vector3(0, 0, -1);
            direction.applyQuaternion(gameState.camera.quaternion);
            direction.normalize();
            
            gameState.player.mesh.position.x += direction.x * distance;
            gameState.player.mesh.position.z += direction.z * distance;
            
            // Create teleport effect
            const teleportGeometry = new THREE.SphereGeometry(1, 32, 32);
            const teleportMaterial = new THREE.MeshStandardMaterial({ 
                color: 0xaa00ff,
                transparent: true,
                opacity: 0.7,
                roughness: 0.1,
                metalness: 0.8
            });
            
            const teleportStart = new THREE.Mesh(teleportGeometry, teleportMaterial);
            teleportStart.position.copy(gameState.player.mesh.position);
            teleportStart.position.y += 1;
            
            const teleportEnd = new THREE.Mesh(teleportGeometry, teleportMaterial);
            teleportEnd.position.copy(gameState.player.mesh.position);
            teleportEnd.position.x -= direction.x * distance;
            teleportEnd.position.z -= direction.z * distance;
            teleportEnd.position.y += 1;
            
            gameState.scene.add(teleportStart);
            gameState.scene.add(teleportEnd);
            
            // Add lights
            const teleportLightStart = new THREE.PointLight(0xaa00ff, 3, 5);
            teleportStart.add(teleportLightStart);
            
            const teleportLightEnd = new THREE.PointLight(0xaa00ff, 3, 5);
            teleportEnd.add(teleportLightEnd);
            
            // Fade out
            let scale = 1;
            const teleportInterval = setInterval(() => {
                scale += 0.1;
                teleportStart.scale.set(scale, scale, scale);
                teleportEnd.scale.set(scale, scale, scale);
                
                teleportMaterial.opacity -= 0.05;
                
                if (teleportMaterial.opacity <= 0) {
                    clearInterval(teleportInterval);
                    gameState.scene.remove(teleportStart);
                    gameState.scene.remove(teleportEnd);
                }
            }, 50);
        }
        
        // Complete a quest
        function completeQuest(questId) {
            const questIndex = gameState.quests.findIndex(q => q.id === questId);
            if (questIndex === -1) return;
            
            gameState.quests[questIndex].completed = true;
            
            // Give rewards
            const reward = gameState.quests[questIndex].reward;
            gameState.gold += reward.gold;
            
            if (reward.item) {
                // Add item to inventory
                const existingItem = gameState.inventory.find(item => item.id === reward.item);
                if (existingItem) {
                    existingItem.quantity++;
                } else {
                    // Define new items here
                    const newItems = {
                        'enchanted-ring': { id: 'enchanted-ring', name: 'Enchanted Ring', icon: 'fa-ring', quantity: 1 },
                        'cloak-of-shadows': { id: 'cloak-of-shadows', name: 'Cloak of Shadows', icon: 'fa-vest', quantity: 1 },
                        'staff-of-power': { id: 'staff-of-power', name: 'Staff of Power', icon: 'fa-magic', quantity: 1 }
                    };
                    
                    if (newItems[reward.item]) {
                        gameState.inventory.push(newItems[reward.item]);
                    }
                }
                
                loadInventory();
            }
            
            // Update UI
            updateUI();
            updateQuestUI();
            
            // Move to next quest if available
            if (questId === 'initiation') {
                gameState.activeQuest = 'rebellion';
            } else if (questId === 'rebellion') {
                gameState.activeQuest = 'artifact';
            }
        }
        
        // Open inventory
        function openInventory() {
            document.getElementById('inventory-modal').classList.remove('hidden');
        }
        
        // Close inventory
        function closeInventory() {
            document.getElementById('inventory-modal').classList.add('hidden');
        }
        
        // Load inventory items into UI
        function loadInventory() {
            const inventoryGrid = document.getElementById('inventory-grid');
            inventoryGrid.innerHTML = '';
            
            gameState.inventory.forEach(item => {
                const itemElement = document.createElement('div');
                itemElement.className = 'inventory-item ui-panel flex flex-col items-center p-2 cursor-pointer';
                itemElement.innerHTML = `
                    <i class="fas ${item.icon} text-2xl mb-1"></i>
                    <span class="text-sm text-center">${item.name}</span>
                    ${item.quantity > 1 ? `<span class="absolute top-0 right-0 bg-gray-700 text-xs px-1 rounded-bl">${item.quantity}</span>` : ''}
                `;
                
                // Add click handler for potions
                if (item.id.includes('potion')) {
                    itemElement.addEventListener('click', () => useItem(item.id));
                }
                
                inventoryGrid.appendChild(itemElement);
            });
        }
        
        // Use an item
        function useItem(itemId) {
            const itemIndex = gameState.inventory.findIndex(item => item.id === itemId);
            if (itemIndex === -1) return;
            
            const item = gameState.inventory[itemIndex];
            
            switch (itemId) {
                case 'health-potion':
                    gameState.player.health = Math.min(gameState.player.health + 30, gameState.player.maxHealth);
                    break;
                case 'mana-potion':
                    gameState.player.mana = Math.min(gameState.player.mana + 40, gameState.player.maxMana);
                    break;
            }
            
            // Remove item or decrement quantity
            if (item.quantity > 1) {
                item.quantity--;
            } else {
                gameState.inventory.splice(itemIndex, 1);
            }
            
            // Update UI
            updateUI();
            loadInventory();
        }
        
        // Update quest UI
        function updateQuestUI() {
            const activeQuest = gameState.quests.find(q => q.id === gameState.activeQuest);
            if (!activeQuest) return;
            
            document.getElementById('quest-title').textContent = activeQuest.title;
            document.getElementById('quest-description').textContent = activeQuest.description;
            
            const progressPercent = (activeQuest.progress / activeQuest.goal) * 100;
            document.getElementById('quest-progress').style.width = `${progressPercent}%`;
            document.getElementById('quest-progress-text').textContent = `${Math.round(progressPercent)}%`;
            
            // Show quest info if not already shown
            if (document.getElementById('quest-info').classList.contains('hidden')) {
                document.getElementById('quest-info').classList.remove('hidden');
            }
        }
        
        // Update UI elements
        function updateUI() {
            document.getElementById('health').textContent = 
                `${Math.floor(gameState.player.health)}/${gameState.player.maxHealth}`;
            document.getElementById('mana').textContent = 
                `${Math.floor(gameState.player.mana)}/${gameState.player.maxMana}`;
            document.getElementById('gold').textContent = gameState.player.gold;
        }
        
        // Window resize handler
        function onWindowResize() {
            gameState.camera.aspect = window.innerWidth / window.innerHeight;
            gameState.camera.updateProjectionMatrix();
            gameState.renderer.setSize(window.innerWidth, window.innerHeight);
        }
        
        // Key down handler
        function onKeyDown(event) {
            switch (event.key.toLowerCase()) {
                case 'w': gameState.keys.w = true; break;
                case 'a': gameState.keys.a = true; break;
                case 's': gameState.keys.s = true; break;
                case 'd': gameState.keys.d = true; break;
                case ' ': gameState.keys.space = true; break;
                case 'q': castSpell('fireball'); break;
                case 'e': castSpell('frost-nova'); break;
                case 'r': castSpell('lightning'); break;
                case 'f': castSpell('teleport'); break;
                case 'i': openInventory(); break;
                case 'j': 
                    document.getElementById('quest-info').classList.toggle('hidden');
                    break;
            }
        }
        
        // Key up handler
        function onKeyUp(event) {
            switch (event.key.toLowerCase()) {
                case 'w': gameState.keys.w = false; break;
                case 'a': gameState.keys.a = false; break;
                case 's': gameState.keys.s = false; break;
                case 'd': gameState.keys.d = false; break;
                case ' ': gameState.keys.space = false; break;
            }
        }
        
        // Initialize the game when the page loads
        window.onload = initScene;
    </script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=toastiselite/test" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>