File size: 30,121 Bytes
20811d7
 
 
 
 
 
 
 
 
a4bac17
20811d7
 
 
 
 
 
 
 
 
a4bac17
20811d7
 
 
a4bac17
 
 
 
 
 
20811d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4bac17
 
 
20811d7
 
 
 
 
 
 
 
 
 
 
 
a4bac17
20811d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4bac17
20811d7
 
 
 
 
 
 
a4bac17
 
 
20811d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4bac17
20811d7
 
 
 
a4bac17
20811d7
 
 
 
 
 
 
a4bac17
20811d7
 
 
 
a4bac17
20811d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4bac17
 
 
 
 
20811d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4bac17
20811d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ChessMaster Pro</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.2/chess.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chessboard-js/1.0.0/chessboard-1.0.0.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/chessboard-js/1.0.0/chessboard-1.0.0.min.js"></script>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@400;600;700&display=swap');
        
        body {
            font-family: 'Montserrat', sans-serif;
            background-color: #f5f5f5;
        }
        
        .chessboard-container {
            width: 100%;
            max-width: 500px;
            margin: 0 auto;
            position: relative;
        }
        
        #puzzleBoard {
            width: 100%;
            aspect-ratio: 1/1;
        }
        
        .tab-content {
            display: none;
        }
        
        .tab-content.active {
            display: block;
        }
        
        .piece-option {
            width: 40px;
            height: 40px;
            cursor: pointer;
            margin: 5px;
            border: 2px solid transparent;
        }
        
        .piece-option.selected {
            border-color: #3b82f6;
        }
        
        .move-list {
            max-height: 300px;
            overflow-y: auto;
        }
        
        .move-item:hover {
            background-color: #e5e7eb;
        }
        
        .move-item.current {
            background-color: #bfdbfe;
        }
        
        .analysis-bar {
            height: 20px;
            background: linear-gradient(to right, #ef4444, #f59e0b, #10b981);
        }
        
        .analysis-marker {
            position: absolute;
            width: 2px;
            height: 20px;
            background-color: black;
        }
        
        .puzzle-theme-btn {
            transition: all 0.2s;
            padding: 0.5rem 1rem;
            border-radius: 0.375rem;
            background-color: #e5e7eb;
        }
        
        .puzzle-theme-btn.active {
            background-color: #3b82f6;
            color: white;
        }
        
        @media (max-width: 640px) {
            .mobile-flex-col {
                flex-direction: column;
            }
            
            .chessboard-container {
                max-width: 100%;
            }
        }
    </style>
</head>
<body class="min-h-screen bg-gray-100">
    <div class="container mx-auto px-4 py-8">
        <!-- Header -->
        <header class="bg-white shadow-md rounded-lg p-6 mb-8">
            <div class="flex justify-between items-center">
                <div class="flex items-center">
                    <i class="fas fa-chess-king text-3xl text-blue-600 mr-3"></i>
                    <h1 class="text-2xl font-bold text-gray-800">ChessMaster Pro</h1>
                </div>
                <div class="flex space-x-4">
                    <button id="userBtn" class="bg-gray-200 hover:bg-gray-300 text-gray-800 px-4 py-2 rounded-lg">
                        <i class="fas fa-user mr-2"></i>Guest
                    </button>
                    <button id="settingsBtn" class="bg-gray-200 hover:bg-gray-300 text-gray-800 px-4 py-2 rounded-lg">
                        <i class="fas fa-cog"></i>
                    </button>
                </div>
            </div>
        </header>

        <!-- Main Content -->
        <div class="bg-white shadow-md rounded-lg overflow-hidden">
            <!-- Tabs -->
            <div class="flex border-b">
                <button class="tab-btn py-4 px-6 font-medium text-gray-600 hover:text-blue-600 border-b-2 border-transparent hover:border-blue-600" data-tab="play">
                    <i class="fas fa-chess-board mr-2"></i>Play
                </button>
                <button class="tab-btn py-4 px-6 font-medium text-gray-600 hover:text-blue-600 border-b-2 border-transparent hover:border-blue-600 active border-blue-600 text-blue-600" data-tab="puzzles">
                    <i class="fas fa-puzzle-piece mr-2"></i>Puzzles
                </button>
                <button class="tab-btn py-4 px-6 font-medium text-gray-600 hover:text-blue-600 border-b-2 border-transparent hover:border-blue-600" data-tab="analysis">
                    <i class="fas fa-chart-line mr-2"></i>Analysis
                </button>
                <button class="tab-btn py-4 px-6 font-medium text-gray-600 hover:text-blue-600 border-b-2 border-transparent hover:border-blue-600" data-tab="learn">
                    <i class="fas fa-graduation-cap mr-2"></i>Learn
                </button>
            </div>

            <!-- Tab Contents -->
            <div class="p-6">
                <!-- Play Tab -->
                <div id="play" class="tab-content">
                    <!-- Play tab content would go here -->
                </div>

                <!-- Puzzles Tab -->
                <div id="puzzles" class="tab-content active">
                    <div class="flex mobile-flex-col gap-6">
                        <!-- Puzzle Board -->
                        <div class="flex-1">
                            <div class="chessboard-container">
                                <div id="puzzleBoard"></div>
                            </div>
                            <div class="mt-4 flex justify-center space-x-4">
                                <button id="newPuzzleBtn" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg">
                                    <i class="fas fa-random mr-2"></i>Random Puzzle
                                </button>
                                <button id="importPuzzleBtn" class="bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-lg">
                                    <i class="fas fa-file-import mr-2"></i>Import Puzzle
                                </button>
                            </div>
                        </div>
                        
                        <!-- Puzzle Controls -->
                        <div class="flex-1">
                            <div class="bg-gray-100 p-4 rounded-lg">
                                <h3 class="font-bold text-lg mb-4">Puzzle Controls</h3>
                                
                                <div class="mb-6">
                                    <h4 class="font-medium mb-2">Puzzle Info</h4>
                                    <div id="puzzleInfo" class="bg-white p-3 rounded-lg">
                                        <p><strong>Theme:</strong> <span id="puzzleTheme">-</span></p>
                                        <p><strong>Rating:</strong> <span id="puzzleRating">-</span></p>
                                        <p><strong>Moves:</strong> <span id="puzzleMoves">-</span></p>
                                        <p><strong>Source:</strong> <span id="puzzleSource">-</span></p>
                                    </div>
                                </div>
                                
                                <div class="mb-6">
                                    <h4 class="font-medium mb-2">Puzzle Status</h4>
                                    <div id="puzzleStatus" class="bg-white p-3 rounded-lg">
                                        Find the best move!
                                    </div>
                                </div>
                                
                                <div class="mb-6">
                                    <h4 class="font-medium mb-2">Puzzle Themes</h4>
                                    <div class="flex flex-wrap gap-2 mb-3">
                                        <button class="puzzle-theme-btn active" data-theme="all">All</button>
                                        <button class="puzzle-theme-btn" data-theme="opening">Opening</button>
                                        <button class="puzzle-theme-btn" data-theme="middlegame">Middlegame</button>
                                        <button class="puzzle-theme-btn" data-theme="endgame">Endgame</button>
                                        <button class="puzzle-theme-btn" data-theme="mate">Mate</button>
                                        <button class="puzzle-theme-btn" data-theme="tactics">Tactics</button>
                                    </div>
                                    <div class="flex flex-wrap gap-2">
                                        <button class="puzzle-theme-btn" data-theme="fork">Fork</button>
                                        <button class="puzzle-theme-btn" data-theme="pin">Pin</button>
                                        <button class="puzzle-theme-btn" data-theme="skewer">Skewer</button>
                                        <button class="puzzle-theme-btn" data-theme="discovery">Discovery</button>
                                        <button class="puzzle-theme-btn" data-theme="sacrifice">Sacrifice</button>
                                    </div>
                                </div>
                                
                                <div class="mb-6">
                                    <h4 class="font-medium mb-2">Import Puzzle</h4>
                                    <div id="importPuzzleForm" class="hidden bg-white p-3 rounded-lg">
                                        <div class="mb-3">
                                            <label class="block mb-1">Format:</label>
                                            <select id="importFormat" class="border rounded px-2 py-1 w-full">
                                                <option value="fen">FEN</option>
                                                <option value="pgn">PGN</option>
                                            </select>
                                        </div>
                                        <div class="mb-3">
                                            <label class="block mb-1">Puzzle Data:</label>
                                            <textarea id="puzzleData" class="border rounded px-2 py-1 w-full" rows="4"></textarea>
                                        </div>
                                        <button id="submitPuzzleBtn" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg w-full">
                                            Import Puzzle
                                        </button>
                                    </div>
                                </div>
                                
                                <div class="flex space-x-2">
                                    <button id="hintBtn" class="flex-1 bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-lg">
                                        <i class="fas fa-lightbulb mr-2"></i>Hint
                                    </button>
                                    <button id="solutionBtn" class="flex-1 bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg">
                                        <i class="fas fa-eye mr-2"></i>Show Solution
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Analysis Tab -->
                <div id="analysis" class="tab-content">
                    <!-- Analysis tab content would go here -->
                </div>

                <!-- Learn Tab -->
                <div id="learn" class="tab-content">
                    <!-- Learn tab content would go here -->
                </div>
            </div>
        </div>
    </div>

    <!-- Settings Modal -->
    <div id="settingsModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden">
        <!-- Settings modal content would go here -->
    </div>

    <!-- User Modal -->
    <div id="userModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden">
        <!-- User modal content would go here -->
    </div>

    <!-- Stockfish Worker -->
    <script>
        // Initialize Stockfish Web Worker
        const stockfish = new Worker('https://cdnjs.cloudflare.com/ajax/libs/stockfish.js/10.0.2/stockfish.js');
        
        // Game state variables
        let game = new Chess();
        let puzzleGame = new Chess();
        let analysisGame = new Chess();
        let board, puzzleBoard, analysisBoard;
        let playerColor = 'white';
        let gameMode = 'human';
        let computerDifficulty = 5;
        let currentPuzzle = null;
        let puzzleSolution = [];
        let currentPuzzleMove = 0;
        let analysisPosition = 0;
        let currentPuzzleTheme = 'all';
        
        // Comprehensive puzzle database (simplified versions of real puzzles from Lichess and Chess.com)
        const puzzleDatabase = [
            // Opening puzzles
            {
                fen: 'r1bqkbnr/pppp1ppp/2n5/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3',
                solution: ['f3g5', 'e5e4', 'g5f7'],
                theme: 'Opening',
                subtheme: 'Fork',
                rating: 1200,
                moves: 3,
                source: 'Lichess'
            },
            {
                fen: 'r1bqkb1r/pppp1ppp/2n2n2/4p2Q/2B1P3/8/PPPP1PPP/RNB1K1NR w KQkq - 4 4',
                solution: ['h5f7'],
                theme: 'Opening',
                subtheme: 'Mate',
                rating: 1500,
                moves: 1,
                source: 'Chess.com'
            },
            {
                fen: 'r1bqkb1r/pppp1ppp/2n2n2/4p3/2B1P3/2P2N2/PP1P1PPP/RNBQK2R b KQkq - 0 4',
                solution: ['f6e4', 'd1d8', 'e8d8', 'c3e4'],
                theme: 'Opening',
                subtheme: 'Pin',
                rating: 1000,
                moves: 4,
                source: 'Lichess'
            },
            
            // Middlegame puzzles
            {
                fen: 'r1b2rk1/pp2bppp/2n1pn2/2qp4/2P5/2N1PN2/PPQ1BPPP/R1B2RK1 w - - 0 11',
                solution: ['c4d5', 'e6d5', 'e3c5'],
                theme: 'Middlegame',
                subtheme: 'Skewer',
                rating: 1800,
                moves: 3,
                source: 'Chess.com'
            },
            {
                fen: 'r3k2r/ppp2ppp/2n1bn2/3q4/3P4/P1P1PN2/4BPPP/R2QK2R b KQkq - 0 12',
                solution: ['d5a2', 'e1c1', 'a2b2'],
                theme: 'Middlegame',
                subtheme: 'Discovery',
                rating: 2000,
                moves: 3,
                source: 'Lichess'
            },
            {
                fen: 'r1bq1rk1/ppp2ppp/2n5/3p4/2PPn3/P1Q5/1P2NPPP/R1B2RK1 b - - 0 12',
                solution: ['e4f2', 'f1f2', 'd8d4'],
                theme: 'Middlegame',
                subtheme: 'Sacrifice',
                rating: 2200,
                moves: 3,
                source: 'Chess.com'
            },
            
            // Endgame puzzles
            {
                fen: '8/8/8/5k2/8/4K3/8/5R2 w - - 5 45',
                solution: ['f1f5', 'f5f5', 'e3f4'],
                theme: 'Endgame',
                subtheme: 'Tactics',
                rating: 1400,
                moves: 3,
                source: 'Lichess'
            },
            {
                fen: '8/8/8/4k3/8/4K3/8/5Q2 w - - 5 45',
                solution: ['f1f5', 'e5e6', 'f5f6', 'e6e7', 'f6f7', 'e7e8', 'f7e7'],
                theme: 'Endgame',
                subtheme: 'Mate',
                rating: 1600,
                moves: 7,
                source: 'Chess.com'
            },
            {
                fen: '8/8/8/3k4/8/3K4/8/3Q4 w - - 5 45',
                solution: ['d1a1', 'd5c5', 'a1a5', 'c5b4', 'a5b6', 'b4c5', 'b6c7', 'c5d5', 'c7d7'],
                theme: 'Endgame',
                subtheme: 'Tactics',
                rating: 1800,
                moves: 9,
                source: 'Lichess'
            },
            
            // Mate in X puzzles
            {
                fen: 'r1bqk2r/pppp1ppp/2n2n2/2b5/2B5/2N2N2/PPPP1PPP/R1BQK2R w KQkq - 6 7',
                solution: ['d1d8', 'f6d8', 'c3d5', 'c5d6', 'd5f6'],
                theme: 'Middlegame',
                subtheme: 'Mate',
                rating: 1700,
                moves: 5,
                source: 'Chess.com'
            },
            {
                fen: 'r1bq1rk1/ppp2ppp/2np1n2/2b1p3/2B1P3/2NP1N2/PPP2PPP/R1BQ1RK1 w - - 0 8',
                solution: ['c4f7', 'f8f7', 'd1d8'],
                theme: 'Middlegame',
                subtheme: 'Mate',
                rating: 1900,
                moves: 3,
                source: 'Lichess'
            },
            
            // Tactical puzzles
            {
                fen: 'r1bq1rk1/ppp2ppp/2np1n2/2b1p3/2B1P3/2NP1N2/PPP2PPP/R1BQ1RK1 w - - 0 8',
                solution: ['c4f7', 'f8f7', 'd1d8'],
                theme: 'Tactics',
                subtheme: 'Fork',
                rating: 1500,
                moves: 3,
                source: 'Chess.com'
            },
            {
                fen: 'r1bqkbnr/ppp2ppp/2np4/4p3/2B1P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 0 5',
                solution: ['c4f7', 'e8f7', 'd1d8'],
                theme: 'Tactics',
                subtheme: 'Pin',
                rating: 1300,
                moves: 3,
                source: 'Lichess'
            },
            {
                fen: 'r1bqk2r/pppp1ppp/2n2n2/2b5/2B5/2N2N2/PPPP1PPP/R1BQK2R w KQkq - 6 7',
                solution: ['d1d8', 'f6d8', 'c3d5', 'c5d6', 'd5f6'],
                theme: 'Tactics',
                subtheme: 'Skewer',
                rating: 1700,
                moves: 5,
                source: 'Chess.com'
            },
            
            // Advanced puzzles
            {
                fen: 'r3k2r/ppp2ppp/2n1bn2/3q4/3P4/P1P1PN2/4BPPP/R2QK2R b KQkq - 0 12',
                solution: ['d5a2', 'e1c1', 'a2b2'],
                theme: 'Middlegame',
                subtheme: 'Sacrifice',
                rating: 2200,
                moves: 3,
                source: 'Lichess'
            },
            {
                fen: 'r1bq1rk1/ppp2ppp/2n5/3p4/2PPn3/P1Q5/1P2NPPP/R1B2RK1 b - - 0 12',
                solution: ['e4f2', 'f1f2', 'd8d4'],
                theme: 'Middlegame',
                subtheme: 'Discovery',
                rating: 2300,
                moves: 3,
                source: 'Chess.com'
            }
        ];

        // Initialize chessboards when DOM is loaded
        document.addEventListener('DOMContentLoaded', function() {
            // Puzzle board
            puzzleBoard = Chessboard('puzzleBoard', {
                position: 'start',
                draggable: true,
                dropOffBoard: 'trash',
                sparePieces: false,
                onDragStart: onPuzzleDragStart,
                onDrop: onPuzzleDrop,
                onSnapEnd: onPuzzleSnapEnd
            });
            
            // Set up event listeners
            setupEventListeners();
            
            // Load a random puzzle
            loadRandomPuzzle();
        });
        
        // Set up all event listeners
        function setupEventListeners() {
            // Tab switching
            document.querySelectorAll('.tab-btn').forEach(btn => {
                btn.addEventListener('click', function() {
                    const tabId = this.getAttribute('data-tab');
                    document.querySelectorAll('.tab-content').forEach(content => {
                        content.classList.remove('active');
                    });
                    document.getElementById(tabId).classList.add('active');
                    
                    // Update active tab styling
                    document.querySelectorAll('.tab-btn').forEach(tabBtn => {
                        tabBtn.classList.remove('border-blue-600', 'text-blue-600');
                        tabBtn.classList.add('border-transparent', 'text-gray-600');
                    });
                    this.classList.add('border-blue-600', 'text-blue-600');
                    this.classList.remove('border-transparent', 'text-gray-600');
                    
                    // Resize board when tab changes
                    setTimeout(() => {
                        if (puzzleBoard) puzzleBoard.resize();
                    }, 10);
                });
            });
            
            // Puzzle theme buttons
            document.querySelectorAll('.puzzle-theme-btn').forEach(btn => {
                btn.addEventListener('click', function() {
                    document.querySelectorAll('.puzzle-theme-btn').forEach(b => {
                        b.classList.remove('active', 'bg-blue-600', 'text-white');
                        b.classList.add('bg-gray-200', 'hover:bg-gray-300');
                    });
                    
                    this.classList.add('active', 'bg-blue-600', 'text-white');
                    this.classList.remove('bg-gray-200', 'hover:bg-gray-300');
                    
                    currentPuzzleTheme = this.getAttribute('data-theme');
                    loadRandomPuzzle();
                });
            });
            
            // Puzzle controls
            document.getElementById('newPuzzleBtn').addEventListener('click', loadRandomPuzzle);
            document.getElementById('importPuzzleBtn').addEventListener('click', toggleImportPuzzleForm);
            document.getElementById('submitPuzzleBtn').addEventListener('click', importPuzzle);
            document.getElementById('hintBtn').addEventListener('click', giveHint);
            document.getElementById('solutionBtn').addEventListener('click', showSolution);
        }
        
        // Load a random puzzle from the database
        function loadRandomPuzzle() {
            let filteredPuzzles = puzzleDatabase;
            
            // Filter by theme if not 'all'
            if (currentPuzzleTheme !== 'all') {
                filteredPuzzles = puzzleDatabase.filter(puzzle => {
                    return puzzle.theme.toLowerCase() === currentPuzzleTheme.toLowerCase() || 
                           puzzle.subtheme.toLowerCase() === currentPuzzleTheme.toLowerCase();
                });
            }
            
            // If no puzzles match the filter, use all puzzles
            if (filteredPuzzles.length === 0) {
                filteredPuzzles = puzzleDatabase;
            }
            
            // Select a random puzzle from the filtered list
            const puzzle = filteredPuzzles[Math.floor(Math.random() * filteredPuzzles.length)];
            currentPuzzle = puzzle;
            
            // Load the puzzle position
            puzzleGame = new Chess(puzzle.fen);
            puzzleBoard.position(puzzle.fen, false); // false to prevent animation
            
            // Convert solution to move objects
            puzzleSolution = puzzle.solution.map(moveStr => {
                return {
                    from: moveStr.substr(0, 2),
                    to: moveStr.substr(2, 2),
                    promotion: moveStr.length > 4 ? moveStr.substr(4, 1) : undefined
                };
            });
            
            currentPuzzleMove = 0;
            
            // Update puzzle info
            document.getElementById('puzzleTheme').textContent = `${puzzle.theme} (${puzzle.subtheme})`;
            document.getElementById('puzzleRating').textContent = puzzle.rating;
            document.getElementById('puzzleMoves').textContent = puzzle.moves;
            document.getElementById('puzzleSource').textContent = puzzle.source;
            document.getElementById('puzzleStatus').textContent = 'Find the best move!';
            
            // Hide import form if visible
            document.getElementById('importPuzzleForm').classList.add('hidden');
        }
        
        // Puzzle functions
        function onPuzzleDragStart(source, piece, position, orientation) {
            // Only allow dragging if it's the player's turn
            if (puzzleGame.turn() !== 'w') {
                return false;
            }
            
            // Only allow dragging of white pieces
            if (piece.search(/^b/) !== -1) {
                return false;
            }
            
            return true;
        }
        
        function onPuzzleDrop(source, target) {
            // See if the move is legal
            const move = puzzleGame.move({
                from: source,
                to: target,
                promotion: 'q' // Always promote to queen for simplicity
            });
            
            // Illegal move
            if (move === null) return 'snapback';
            
            // Check if the move matches the puzzle solution
            if (currentPuzzleMove < puzzleSolution.length) {
                const solutionMove = puzzleSolution[currentPuzzleMove];
                
                if (move.from === solutionMove.from && move.to === solutionMove.to) {
                    // Correct move
                    currentPuzzleMove++;
                    document.getElementById('puzzleStatus').innerHTML = '<span class="text-green-600">Correct!</span>';
                    
                    // If puzzle is complete
                    if (currentPuzzleMove >= puzzleSolution.length) {
                        document.getElementById('puzzleStatus').innerHTML = '<span class="text-green-600">Puzzle solved!</span>';
                    } else {
                        // Make the computer's response move
                        setTimeout(() => {
                            const nextMove = puzzleSolution[currentPuzzleMove];
                            puzzleGame.move(nextMove);
                            puzzleBoard.position(puzzleGame.fen());
                            currentPuzzleMove++;
                            document.getElementById('puzzleStatus').textContent = 'Find the best move!';
                        }, 500);
                    }
                } else {
                    // Incorrect move
                    document.getElementById('puzzleStatus').innerHTML = '<span class="text-red-600">Incorrect, try again!</span>';
                    return 'snapback';
                }
            }
            
            puzzleBoard.position(puzzleGame.fen());
        }
        
        function onPuzzleSnapEnd() {
            puzzleBoard.position(puzzleGame.fen());
        }
        
        function toggleImportPuzzleForm() {
            const form = document.getElementById('importPuzzleForm');
            form.classList.toggle('hidden');
        }
        
        function importPuzzle() {
            const format = document.getElementById('importFormat').value;
            const data = document.getElementById('puzzleData').value.trim();
            
            try {
                if (format === 'fen') {
                    puzzleGame = new Chess(data);
                    puzzleBoard.position(data);
                } else if (format === 'pgn') {
                    puzzleGame = new Chess();
                    puzzleGame.loadPgn(data);
                    puzzleBoard.position(puzzleGame.fen());
                }
                
                // For this demo, we'll just set a simple solution
                puzzleSolution = [];
                currentPuzzleMove = 0;
                
                document.getElementById('puzzleStatus').textContent = 'Puzzle loaded! Add solution moves.';
                document.getElementById('importPuzzleForm').classList.add('hidden');
                
                // Update puzzle info
                document.getElementById('puzzleTheme').textContent = 'Custom';
                document.getElementById('puzzleRating').textContent = 'N/A';
                document.getElementById('puzzleMoves').textContent = '0';
                document.getElementById('puzzleSource').textContent = 'Custom';
            } catch (e) {
                alert('Error loading puzzle: ' + e.message);
            }
        }
        
        function giveHint() {
            if (currentPuzzle && currentPuzzleMove < puzzleSolution.length) {
                const hintMove = puzzleSolution[currentPuzzleMove];
                alert(`Hint: Try moving from ${hintMove.from} to ${hintMove.to}`);
            } else {
                alert('No active puzzle or puzzle already solved!');
            }
        }
        
        function showSolution() {
            if (currentPuzzle) {
                let solutionText = 'Solution:\n';
                puzzleSolution.forEach((move, i) => {
                    if (i % 2 === 0) {
                        solutionText += `${Math.floor(i/2) + 1}. `;
                    }
                    solutionText += `${move.from}${move.to} `;
                });
                alert(solutionText);
            } else {
                alert('No active puzzle!');
            }
        }
    </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=HuggingeFaceeee/spacee" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>