File size: 38,830 Bytes
5d2f429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ac2ad7
 
5d2f429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ac2ad7
 
5d2f429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ac2ad7
5d2f429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Digital Archive Snake</title>
    <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body {
            font-family: monospace;
            background: #0a0a0f;
            min-height: 100vh;
            padding: 10px;
            color: #00ff41;
        }
        .container { max-width: 1000px; margin: 0 auto; }
        h1 { text-align: center; font-size: 1.6em; color: #00ff41; text-shadow: 0 0 10px #00ff41; margin: 0 0 5px 0; }
        .subtitle { text-align: center; color: #ffb000; font-size: 0.85em; margin-bottom: 15px; }
        .game-layout { display: flex; gap: 15px; flex-wrap: wrap; }
        .game-area { flex: 1; min-width: 320px; }
        .side-panel { width: 220px; }
        
        .game-board {
            background: #000;
            border: 2px solid #00cc33;
            border-radius: 5px;
            padding: 5px;
            position: relative;
            box-shadow: 0 0 15px rgba(0,255,65,0.2);
        }
        .grid {
            display: grid;
            grid-template-columns: repeat(25, 1fr);
            gap: 1px;
            background: #111;
            border: 1px solid rgba(255,68,68,0.3);
        }
        .cell {
            aspect-ratio: 1;
            background: #0a0a0a;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 14px;
            min-height: 20px;
        }
        .cell.snake { background: #00ff41; box-shadow: 0 0 5px #00ff41; }
        .cell.snake-head { background: #00ff41; box-shadow: 0 0 8px #00ff41; }
        .cell.snake-body { background: rgba(0,255,65,0.6); }
        
        .panel { background: #161b22; border: 1px solid #30363d; border-radius: 5px; padding: 10px; margin-bottom: 10px; }
        .panel-title { color: #ffb000; font-size: 0.9em; border-bottom: 1px solid #30363d; padding-bottom: 5px; margin-bottom: 8px; }
        .stat-row { display: flex; justify-content: space-between; font-size: 0.8em; margin-bottom: 5px; }
        .stat-label { color: #8b949e; }
        .stat-value { color: #00ff41; font-weight: bold; }
        .stat-value.warning { color: #ff4444; animation: pulse 0.5s infinite; }
        @keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.5; } }
        
        .progress-container { margin-bottom: 8px; }
        .progress-label { display: flex; justify-content: space-between; font-size: 0.7em; margin-bottom: 3px; }
        .progress-bar { height: 6px; background: #1a1a2e; border-radius: 3px; overflow: hidden; }
        .progress-fill { height: 100%; transition: width 0.3s; }
        .progress-fill.floppy { background: linear-gradient(90deg, #1a5fb4, #3584e4); }
        .progress-fill.cdrom { background: linear-gradient(90deg, #9141ac, #c061cb); }
        .progress-fill.vhs { background: linear-gradient(90deg, #c01c28, #e66100); }
        .progress-fill.win { background: linear-gradient(90deg, #00ff41, #ffd700); }
        
        .controls { display: flex; gap: 8px; justify-content: center; margin: 10px 0; flex-wrap: wrap; }
        button {
            font-family: monospace;
            font-size: 0.9em;
            padding: 8px 16px;
            border: 2px solid #00ff41;
            background: transparent;
            color: #00ff41;
            cursor: pointer;
            border-radius: 4px;
            transition: all 0.2s;
        }
        button:hover { background: #00ff41; color: #000; }
        button:disabled { opacity: 0.5; cursor: not-allowed; }
        button.danger { border-color: #ff4444; color: #ff4444; }
        button.danger:hover { background: #ff4444; color: #000; }
        
        .instructions { background: #161b22; border: 1px solid #30363d; border-radius: 5px; padding: 10px; font-size: 0.75em; color: #8b949e; margin-top: 10px; }
        .instructions strong { color: #ffb000; }
        .key { background: #0d1117; padding: 2px 6px; border-radius: 3px; color: #00ff41; }
        
        .attribution { background: #161b22; border: 1px solid #30363d; border-radius: 5px; padding: 12px; font-size: 0.7em; color: #8b949e; margin-top: 10px; line-height: 1.6; }
        .attribution a { color: #ffb000; text-decoration: none; }
        .attribution a:hover { text-decoration: underline; }
        
        /* Modal */
        .modal-overlay {
            display: none;
            position: fixed;
            top: 0; left: 0; right: 0; bottom: 0;
            background: rgba(0,0,0,0.85);
            z-index: 100;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        .modal-overlay.show { display: flex; }
        .modal {
            background: #161b22;
            border: 2px solid #ff4444;
            border-radius: 10px;
            padding: 20px;
            max-width: 400px;
            width: 100%;
            box-shadow: 0 0 30px rgba(255,68,68,0.3);
        }
        .modal.success { border-color: #00ff41; box-shadow: 0 0 30px rgba(0,255,65,0.3); }
        .modal.win { border-color: #ffd700; box-shadow: 0 0 40px rgba(255,215,0,0.4); }
        .modal h2 { text-align: center; margin-bottom: 10px; font-size: 1.3em; }
        .modal h2.error { color: #ff4444; }
        .modal h2.success { color: #00ff41; }
        .modal h2.win { color: #ffd700; text-shadow: 0 0 15px #ffd700; }
        .modal p { text-align: center; color: #8b949e; font-size: 0.85em; margin-bottom: 10px; }
        
        .term-box {
            background: #0d1117;
            border: 2px solid #ffb000;
            padding: 12px;
            border-radius: 6px;
            text-align: center;
            margin-bottom: 15px;
        }
        .term-box span { color: #ffb000; font-size: 1.5em; text-shadow: 0 0 10px #ffb000; }
        
        .answer-btn {
            width: 100%;
            text-align: left;
            padding: 10px;
            margin-bottom: 8px;
            background: #0d1117;
            border: 1px solid #30363d;
            color: #c9d1d9;
            font-size: 0.8em;
            line-height: 1.4;
        }
        .answer-btn:hover { border-color: #00ff41; background: rgba(0,255,65,0.1); }
        .answer-btn.correct { border-color: #00ff41; background: rgba(0,255,65,0.2); color: #00ff41; }
        .answer-btn.wrong { border-color: #ff4444; background: rgba(255,68,68,0.2); color: #ff4444; }
        
        .quiz-stakes { text-align: center; font-size: 0.85em; margin-top: 10px; }
        .quiz-stakes .gain { color: #00ff41; }
        .quiz-stakes .lose { color: #ff4444; }
        
        .final-stats { background: #0d1117; border-radius: 6px; padding: 10px; margin: 15px 0; }
        .final-stat { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid #30363d; font-size: 0.85em; }
        .final-stat:last-child { border-bottom: none; }
        .final-stat .label { color: #8b949e; }
        .final-stat .value { color: #00ff41; }
        
        .trophy { font-size: 3em; text-align: center; margin-bottom: 10px; animation: bounce 0.5s ease infinite alternate; }
        @keyframes bounce { from { transform: translateY(0); } to { transform: translateY(-8px); } }
        .win-score { font-size: 2em; color: #ffd700; text-align: center; text-shadow: 0 0 15px #ffd700; margin: 10px 0; }
        
        .legend { font-size: 0.75em; }
        .legend-item { display: flex; align-items: center; gap: 8px; margin-bottom: 5px; }
        
        .difficulty-slider { width: 100%; margin: 5px 0; }
        .slider-labels { display: flex; justify-content: space-between; font-size: 0.65em; color: #666; }
        
        @media (max-width: 600px) {
            .game-layout { flex-direction: column; }
            .side-panel { width: 100%; }
            .cell { font-size: 10px; min-height: 14px; }
        }
    </style>
</head>
<body>
    <div class="container">
        
        <div class="game-layout">
            <div class="game-area">
                <h1>🐍 DIGITAL ARCHIVE SNAKE</h1>
                <p class="subtitle">Rescue Data Before the Carriers Become Obsolete</p>
                <div class="game-board">
                    <div class="grid" id="grid"></div>
                </div>
                <div class="controls">
                    <button id="startBtn" onclick="startGame()">▶ Start Mission</button>
                    <button id="pauseBtn" onclick="togglePause()" disabled>⏸ Pause</button>
                    <button id="soundBtn" onclick="toggleSound()">🔊 Sound</button>
                    <button id="newGameBtn" class="danger" onclick="newGame()" style="display:none;">🔄 New Game</button>
                </div>
            </div>
            
            <div class="side-panel">
                <div class="panel">
                    <div class="panel-title">📊 Status</div>
                    <div class="stat-row"><span class="stat-label">Score</span><span class="stat-value" id="score">0</span></div>
                    <div class="stat-row"><span class="stat-label">Time</span><span class="stat-value" id="timer">1:00</span></div>
                    <div class="stat-row"><span class="stat-label">Quizzes</span><span class="stat-value" id="quizzes">0/0</span></div>
                    <div class="progress-container" style="margin-top:10px;">
                        <div class="progress-label"><span id="levelDisplay">📀 Level 1</span><span id="targetDisplay">Need 3 each</span></div>
                        <div class="progress-bar"><div class="progress-fill win" id="winProgress" style="width:0%"></div></div>
                    </div>
                </div>
                
                <div class="panel">
                    <div class="panel-title">📥 Carrier Ingest</div>
                    <div class="progress-container">
                        <div class="progress-label"><span>💾 Floppy</span><span id="floppyCount">0</span></div>
                        <div class="progress-bar"><div class="progress-fill floppy" id="floppyProgress" style="width:0%"></div></div>
                    </div>
                    <div class="progress-container">
                        <div class="progress-label"><span>💿 CD-ROM</span><span id="cdromCount">0</span></div>
                        <div class="progress-bar"><div class="progress-fill cdrom" id="cdromProgress" style="width:0%"></div></div>
                    </div>
                    <div class="progress-container">
                        <div class="progress-label"><span>📼 VHS</span><span id="vhsCount">0</span></div>
                        <div class="progress-bar"><div class="progress-fill vhs" id="vhsProgress" style="width:0%"></div></div>
                    </div>
                </div>
                
                <div class="panel">
                    <div class="panel-title">🎮 Starting Level</div>
                    <div class="stat-row"><span class="stat-label">Level</span><span class="stat-value" id="diffName">Level 1</span></div>
                    <input type="range" class="difficulty-slider" id="diffSlider" min="1" max="3" value="1">
                    <div class="slider-labels"><span>Level 1</span><span>Level 2</span><span>Level 3</span></div>
                    <div class="legend" style="margin-top:10px;">
                        <div class="legend-item"><span>☠️</span><span style="color:#8b949e">Bit Rot = Quiz!</span></div>
                        <div class="legend-item"><span>🧱</span><span style="color:#8b949e">Edge = Quiz!</span></div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="instructions">
            <strong>📋 Mission:</strong> Ingest data from 💾💿📼 obsolete carriers before they degrade! Collect the target amount of each type to advance.<br>
            <strong>Controls:</strong> <span class="key">↑↓←→</span> or <span class="key">WASD</span><br>
            <strong>Hazards:</strong> ☠️ Bit Rot and 🧱 Edges trigger quizzes. <span style="color:#00ff41">Correct = +time</span>, <span style="color:#ff4444">Wrong = -time</span><br>
            <span style="color:#8b949e;font-size:0.9em">The clock is ticking — carriers degrade and playback devices are disappearing. Preserve the data now!</span>
        </div>
        
        <div class="attribution">
            <strong>Source:</strong> Digital Preservation Handbook, 2nd Edition, <a href="https://www.dpconline.org/handbook" target="_blank">https://www.dpconline.org/handbook</a><br>
            Digital Preservation Coalition © 2015 licensed under the <a href="http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/" target="_blank">Open Government Licence v3.0</a>.<br>
            This interactive learning tool was created for educational purposes. For the most up-to-date glossary, please visit the <a href="https://www.dpconline.org/handbook/glossary" target="_blank">DPC Handbook Glossary</a>.
        </div>
    </div>
    
    <!-- Quiz Modal -->
    <div class="modal-overlay" id="quizModal">
        <div class="modal" id="quizModalInner">
            <h2 class="error" id="quizTitle">☠️ Bit Rot Detected!</h2>
            <p>Match this preservation term:</p>
            <div class="term-box"><span id="quizTerm">OAIS</span></div>
            <div id="answerOptions"></div>
            <div class="quiz-stakes"><span class="gain" id="quizGain">✓ +10s</span> | <span class="lose" id="quizLose">✗ -10s</span></div>
        </div>
    </div>
    
    <!-- Win Modal -->
    <div class="modal-overlay" id="winModal">
        <div class="modal win">
            <div class="trophy">🏆</div>
            <h2 class="win">🎉 ALL LEVELS COMPLETE! 🎉</h2>
            <p>Archive fully preserved! All data rescued from obsolete carriers!</p>
            <div class="win-score" id="winScore">500</div>
            <div class="final-stats">
                <div class="final-stat"><span class="label">💾 Floppies Ingested</span><span class="value" id="winFloppies">0</span></div>
                <div class="final-stat"><span class="label">💿 CD-ROMs Ingested</span><span class="value" id="winCDs">0</span></div>
                <div class="final-stat"><span class="label">📼 VHS Tapes Ingested</span><span class="value" id="winVHS">0</span></div>
                <div class="final-stat"><span class="label">Quiz Accuracy</span><span class="value" id="winAcc">0%</span></div>
            </div>
            <button onclick="newGame()" style="width:100%;border-color:#ffd700;color:#ffd700;">🎮 Play Again</button>
        </div>
    </div>
    
    <!-- Game Over Modal -->
    <div class="modal-overlay" id="overModal">
        <div class="modal">
            <h2 class="error" id="overTitle">⏰ Time Expired!</h2>
            <div class="final-stats">
                <div class="final-stat"><span class="label">Final Score</span><span class="value" id="finalScore">0</span></div>
                <div class="final-stat"><span class="label">💾 Floppies Ingested</span><span class="value" id="finalFloppies">0</span></div>
                <div class="final-stat"><span class="label">💿 CD-ROMs Ingested</span><span class="value" id="finalCDs">0</span></div>
                <div class="final-stat"><span class="label">📼 VHS Tapes Ingested</span><span class="value" id="finalVHS">0</span></div>
                <div class="final-stat"><span class="label">Quiz Accuracy</span><span class="value" id="finalAcc">0%</span></div>
            </div>
            <button onclick="newGame()" style="width:100%;">🔄 Try Again</button>
        </div>
    </div>
    
    <!-- Level Complete Modal -->
    <div class="modal-overlay" id="levelModal">
        <div class="modal success">
            <h2 class="success" id="levelCompleteTitle">🎉 Level 1 Complete!</h2>
            <p>All target carriers successfully ingested!</p>
            <div class="final-stats">
                <div class="final-stat"><span class="label">Score</span><span class="value" id="levelScore">0</span></div>
                <div class="final-stat"><span class="label">💾 Floppies</span><span class="value" id="levelFloppies">0</span></div>
                <div class="final-stat"><span class="label">💿 CD-ROMs</span><span class="value" id="levelCDs">0</span></div>
                <div class="final-stat"><span class="label">📼 VHS Tapes</span><span class="value" id="levelVHS">0</span></div>
            </div>
            <div style="background:#0d1117;padding:10px;border-radius:5px;margin:10px 0;text-align:center;">
                <div style="color:#ffb000;font-size:0.9em;">⚠️ Next: <span id="nextLevelName">Level 2</span></div>
                <div style="color:#8b949e;font-size:0.8em;margin-top:5px;">Target: <span id="nextLevelTarget">4</span> of each | ☠️ Bit Rots: <span id="nextLevelRots">5</span></div>
            </div>
            <button onclick="startNextLevel()" style="width:100%;border-color:#00ff41;color:#00ff41;">▶ Continue to Next Level</button>
        </div>
    </div>

    <script>
        // Audio system
        let audioCtx = null;
        let soundEnabled = true;
        function initAudio() {
            if (!audioCtx) {
                try { audioCtx = new (window.AudioContext || window.webkitAudioContext)(); } catch(e) {}
            }
        }
        function toggleSound() {
            soundEnabled = !soundEnabled;
            document.getElementById('soundBtn').textContent = soundEnabled ? '🔊 Sound' : '🔇 Muted';
        }
        function playSound(type) {
            if (!audioCtx || !soundEnabled) return;
            try {
                const osc = audioCtx.createOscillator();
                const gain = audioCtx.createGain();
                osc.connect(gain);
                gain.connect(audioCtx.destination);
                
                if (type === 'collect') {
                    osc.frequency.setValueAtTime(880, audioCtx.currentTime);
                    osc.frequency.exponentialRampToValueAtTime(1760, audioCtx.currentTime + 0.1);
                    gain.gain.setValueAtTime(0.08, audioCtx.currentTime);
                    gain.gain.exponentialRampToValueAtTime(0.01, audioCtx.currentTime + 0.1);
                    osc.start(); osc.stop(audioCtx.currentTime + 0.1);
                } else if (type === 'correct') {
                    osc.frequency.setValueAtTime(523, audioCtx.currentTime);
                    osc.frequency.setValueAtTime(659, audioCtx.currentTime + 0.1);
                    gain.gain.setValueAtTime(0.08, audioCtx.currentTime);
                    gain.gain.exponentialRampToValueAtTime(0.01, audioCtx.currentTime + 0.2);
                    osc.start(); osc.stop(audioCtx.currentTime + 0.2);
                } else if (type === 'wrong') {
                    osc.frequency.setValueAtTime(200, audioCtx.currentTime);
                    osc.frequency.exponentialRampToValueAtTime(100, audioCtx.currentTime + 0.25);
                    gain.gain.setValueAtTime(0.08, audioCtx.currentTime);
                    gain.gain.exponentialRampToValueAtTime(0.01, audioCtx.currentTime + 0.25);
                    osc.start(); osc.stop(audioCtx.currentTime + 0.25);
                } else if (type === 'bonus') {
                    [523, 659, 784].forEach((freq, i) => {
                        const o = audioCtx.createOscillator();
                        const g = audioCtx.createGain();
                        o.connect(g); g.connect(audioCtx.destination);
                        o.frequency.setValueAtTime(freq, audioCtx.currentTime + i * 0.1);
                        g.gain.setValueAtTime(0.08, audioCtx.currentTime + i * 0.1);
                        g.gain.exponentialRampToValueAtTime(0.01, audioCtx.currentTime + i * 0.1 + 0.15);
                        o.start(audioCtx.currentTime + i * 0.1);
                        o.stop(audioCtx.currentTime + i * 0.1 + 0.15);
                    });
                } else if (type === 'win') {
                    const notes = [523, 659, 784, 1047, 784, 1047];
                    const durs = [0.12, 0.12, 0.12, 0.2, 0.12, 0.3];
                    let t = audioCtx.currentTime;
                    notes.forEach((freq, i) => {
                        const o = audioCtx.createOscillator();
                        const g = audioCtx.createGain();
                        o.connect(g); g.connect(audioCtx.destination);
                        o.frequency.setValueAtTime(freq, t);
                        g.gain.setValueAtTime(0.12, t);
                        g.gain.exponentialRampToValueAtTime(0.01, t + durs[i]);
                        o.start(t); o.stop(t + durs[i]);
                        t += durs[i];
                    });
                } else if (type === 'hazard') {
                    osc.frequency.setValueAtTime(150, audioCtx.currentTime);
                    osc.frequency.setValueAtTime(100, audioCtx.currentTime + 0.1);
                    gain.gain.setValueAtTime(0.1, audioCtx.currentTime);
                    gain.gain.exponentialRampToValueAtTime(0.01, audioCtx.currentTime + 0.15);
                    osc.start(); osc.stop(audioCtx.currentTime + 0.15);
                } else if (type === 'gameover') {
                    osc.frequency.setValueAtTime(300, audioCtx.currentTime);
                    osc.frequency.exponentialRampToValueAtTime(100, audioCtx.currentTime + 0.5);
                    gain.gain.setValueAtTime(0.1, audioCtx.currentTime);
                    gain.gain.exponentialRampToValueAtTime(0.01, audioCtx.currentTime + 0.5);
                    osc.start(); osc.stop(audioCtx.currentTime + 0.5);
                }
            } catch(e) {}
        }

        const COLS = 25, ROWS = 15, TIME = 60, GOAL = 5;
        const DIFF = { 1: {name:'Level 1',rots:2,gain:12,loss:4,target:3}, 2: {name:'Level 2',rots:5,gain:10,loss:8,target:4}, 3: {name:'Level 3',rots:8,gain:8,loss:12,target:5} };
        const ARTS = [ {e:'💾',pts:10,k:'floppy',b:10}, {e:'💿',pts:20,k:'cdrom',b:15}, {e:'📼',pts:30,k:'vhs',b:20} ];
        const GLOSS = [
            {t:"OAIS",d:"Open Archival Information System - reference model for digital archives"},
            {t:"AIP",d:"Archival Information Package - preserved within an OAIS"},
            {t:"SIP",d:"Submission Information Package - delivered to construct AIPs"},
            {t:"DIP",d:"Dissemination Information Package - sent to users on request"},
            {t:"Checksum",d:"Unique numerical signature to verify file integrity"},
            {t:"Bit Preservation",d:"Basic preservation including backups and fixity-checking"},
            {t:"Born-Digital",d:"Digital materials with no analogue equivalent"},
            {t:"Emulation",d:"Imitating obsolete systems on future computers"},
            {t:"Migration",d:"Transferring resources between hardware/software generations"},
            {t:"Fixity Check",d:"Verifying a file has not been altered or corrupted"},
            {t:"Ingest",d:"Turning a SIP into an AIP in a digital archive"},
            {t:"PREMIS",d:"Preservation Metadata Implementation Strategies standard"},
            {t:"PRONOM",d:"Database of file formats for long-term access"},
            {t:"DROID",d:"File format identification tool based on PRONOM"},
            {t:"Dark Archive",d:"Archive not accessible until trigger events occur"},
            {t:"TDR",d:"Trusted Digital Repository for reliable long-term access"},
            {t:"WARC",d:"Web ARChive format for archived websites"},
            {t:"Metadata",d:"Information describing significant aspects of a resource"},
            {t:"PDF/A",d:"PDF versions intended for archival use"},
            {t:"Authenticity",d:"The digital material is what it purports to be"}
        ];

        let grid, cells, snake, dir, nextDir, arts, rots, score, time, diff, coll, bonus, qC, qT, running, paused, quiz, quizReason, pending, gameLoop, timerLoop, level;

        function init() {
            grid = document.getElementById('grid');
            grid.innerHTML = '';
            cells = [];
            for (let i = 0; i < ROWS * COLS; i++) {
                const cell = document.createElement('div');
                cell.className = 'cell';
                grid.appendChild(cell);
                cells.push(cell);
            }
            document.getElementById('diffSlider').addEventListener('input', e => {
                const startLvl = +e.target.value;
                document.getElementById('diffName').textContent = DIFF[startLvl].name;
                document.getElementById('levelDisplay').textContent = '📀 ' + DIFF[startLvl].name;
                document.getElementById('targetDisplay').textContent = 'Need ' + DIFF[startLvl].target + ' each';
            });
            document.addEventListener('keydown', handleKey);
            level = 1;
            diff = 1;
            render();
        }

        function idx(x, y) { return y * COLS + x; }

        function randPos() {
            let x, y, tries = 0;
            do {
                x = Math.floor(Math.random() * COLS);
                y = Math.floor(Math.random() * ROWS);
                tries++;
            } while (tries < 100 && (snake.some(s => s.x === x && s.y === y) || arts.some(a => a.x === x && a.y === y) || rots.some(r => r.x === x && r.y === y)));
            return {x, y};
        }

        function startGame(startLevel = null) {
            initAudio();
            if (startLevel === null) {
                startLevel = +document.getElementById('diffSlider').value;
            }
            level = startLevel;
            diff = level;
            snake = [{x:5,y:7},{x:4,y:7},{x:3,y:7}];
            dir = {x:1,y:0}; nextDir = {x:1,y:0};
            arts = []; rots = [];
            score = 0; time = TIME; qC = 0; qT = 0;
            coll = {floppy:0,cdrom:0,vhs:0};
            bonus = {floppy:0,cdrom:0,vhs:0};
            running = true; paused = false; quiz = null; pending = null;

            for (let i = 0; i < 5; i++) { const p = randPos(); arts.push({...p, ...ARTS[Math.floor(Math.random()*ARTS.length)]}); }
            for (let i = 0; i < DIFF[diff].rots; i++) rots.push(randPos());

            document.getElementById('startBtn').style.display = 'none';
            document.getElementById('newGameBtn').style.display = 'inline-block';
            document.getElementById('pauseBtn').disabled = false;
            document.getElementById('pauseBtn').textContent = '⏸ Pause';
            document.getElementById('diffSlider').disabled = true;
            document.getElementById('levelDisplay').textContent = '📀 ' + DIFF[level].name;
            document.getElementById('targetDisplay').textContent = 'Need ' + DIFF[level].target + ' each';
            document.getElementById('diffName').textContent = DIFF[level].name;

            clearInterval(gameLoop); clearInterval(timerLoop);
            gameLoop = setInterval(update, 150);
            timerLoop = setInterval(tick, 1000);
            updateUI(); render();
        }

        function newGame() {
            clearInterval(gameLoop); clearInterval(timerLoop);
            document.getElementById('quizModal').classList.remove('show');
            document.getElementById('winModal').classList.remove('show');
            document.getElementById('overModal').classList.remove('show');
            document.getElementById('levelModal').classList.remove('show');
            document.getElementById('diffSlider').disabled = false;
            startGame();
        }

        function togglePause() {
            if (!running) return;
            paused = !paused;
            document.getElementById('pauseBtn').textContent = paused ? '▶ Resume' : '⏸ Pause';
            if (paused) { clearInterval(gameLoop); clearInterval(timerLoop); }
            else { gameLoop = setInterval(update, 150); timerLoop = setInterval(tick, 1000); }
        }

        function update() {
            if (paused || quiz || !running) return;
            dir = {...nextDir};
            const head = {x: snake[0].x + dir.x, y: snake[0].y + dir.y};

            // Edge
            if (head.x < 0 || head.x >= COLS || head.y < 0 || head.y >= ROWS) {
                if (head.x < 0) head.x = COLS - 1;
                if (head.x >= COLS) head.x = 0;
                if (head.y < 0) head.y = ROWS - 1;
                if (head.y >= ROWS) head.y = 0;
                pending = head;
                triggerQuiz('edge');
                return;
            }

            // Self
            if (snake.some(s => s.x === head.x && s.y === head.y)) { gameOver(); return; }

            snake.unshift(head);

            // Artifact
            let ate = false;
            const artIdx = arts.findIndex(a => a.x === head.x && a.y === head.y);
            if (artIdx !== -1) {
                ate = true;
                const art = arts[artIdx];
                score += art.pts;
                coll[art.k]++;
                playSound('collect');
                checkBonus(art.k, art.b);
                arts.splice(artIdx, 1);
                const p = randPos();
                arts.push({...p, ...ARTS[Math.floor(Math.random()*ARTS.length)]});
                if (checkLevelComplete()) { levelComplete(); return; }
            }
            if (!ate) snake.pop();

            // Bit rot
            const rotIdx = rots.findIndex(r => r.x === head.x && r.y === head.y);
            if (rotIdx !== -1) {
                rots[rotIdx] = randPos();
                triggerQuiz('bitrot');
                return;
            }

            updateUI(); render();
        }

        function checkBonus(k, b) {
            const lvl = Math.floor(coll[k] / GOAL);
            if (lvl > bonus[k]) {
                time += b;
                score += 50;
                bonus[k] = lvl;
                playSound('bonus');
            }
        }

        function checkLevelComplete() {
            const target = DIFF[level].target;
            if (coll.floppy >= target && coll.cdrom >= target && coll.vhs >= target) {
                return true;
            }
            return false;
        }

        function tick() {
            if (paused || quiz || !running) return;
            time--;
            updateUI();
            if (time <= 0) gameOver();
        }

        function triggerQuiz(reason) {
            playSound('hazard');
            quiz = true; quizReason = reason;
            const i = Math.floor(Math.random() * GLOSS.length);
            const correct = GLOSS[i];
            let wrong = [];
            while (wrong.length < 3) {
                const j = Math.floor(Math.random() * GLOSS.length);
                if (j !== i && !wrong.some(w => w.t === GLOSS[j].t)) wrong.push(GLOSS[j]);
            }
            const answers = [correct, ...wrong].sort(() => Math.random() - 0.5);

            document.getElementById('quizTitle').textContent = reason === 'edge' ? '🧱 Edge Impact!' : '☠️ Bit Rot Detected!';
            document.getElementById('quizTerm').textContent = correct.t;
            document.getElementById('quizGain').textContent = '✓ +' + DIFF[diff].gain + 's';
            document.getElementById('quizLose').textContent = '✗ -' + DIFF[diff].loss + 's';

            const opts = document.getElementById('answerOptions');
            opts.innerHTML = '';
            answers.forEach(a => {
                const btn = document.createElement('button');
                btn.className = 'answer-btn';
                btn.textContent = a.d;
                btn.onclick = () => answerQuiz(a.t === correct.t, btn, correct.d);
                opts.appendChild(btn);
            });

            document.getElementById('quizModalInner').classList.remove('success');
            document.getElementById('quizModal').classList.add('show');
        }

        function answerQuiz(isCorrect, btn, correctDef) {
            qT++;
            document.querySelectorAll('.answer-btn').forEach(b => {
                b.onclick = null;
                if (b.textContent === correctDef) b.classList.add('correct');
            });
            const modal = document.getElementById('quizModalInner');
            const title = document.getElementById('quizTitle');

            if (isCorrect) {
                qC++;
                time += DIFF[diff].gain;
                score += 25;
                btn.classList.add('correct');
                modal.classList.add('success');
                title.textContent = '✓ Correct!';
                title.className = 'success';
                playSound('correct');
            } else {
                time = Math.max(0, time - DIFF[diff].loss);
                btn.classList.add('wrong');
                title.textContent = '✗ Wrong!';
                title.className = 'error';
                playSound('wrong');
            }
            updateUI();

            setTimeout(() => {
                document.getElementById('quizModal').classList.remove('show');
                quiz = null;
                if (pending) { snake.unshift(pending); snake.pop(); pending = null; }
                if (time <= 0) gameOver();
                else render();
            }, 1200);
        }

        function updateUI() {
            document.getElementById('score').textContent = score;
            const m = Math.floor(Math.max(0,time)/60), s = Math.max(0,time)%60;
            const timerEl = document.getElementById('timer');
            timerEl.textContent = m + ':' + s.toString().padStart(2,'0');
            timerEl.className = 'stat-value' + (time <= 15 ? ' warning' : '');
            document.getElementById('quizzes').textContent = qC + '/' + qT;
            
            // Calculate level progress (min of all 3 types progress toward target)
            const target = DIFF[level].target;
            const floppyPct = Math.min(100, (coll.floppy / target) * 100);
            const cdromPct = Math.min(100, (coll.cdrom / target) * 100);
            const vhsPct = Math.min(100, (coll.vhs / target) * 100);
            const avgPct = Math.round((floppyPct + cdromPct + vhsPct) / 3);
            document.getElementById('winProgress').style.width = avgPct + '%';

            ['floppy','cdrom','vhs'].forEach(k => {
                const count = coll[k];
                const pct = Math.min(100, (count / target) * 100);
                document.getElementById(k+'Count').textContent = count + '/' + target;
                document.getElementById(k+'Progress').style.width = pct + '%';
            });
        }

        function render() {
            cells.forEach(c => { c.className = 'cell'; c.textContent = ''; });
            rots.forEach(r => { cells[idx(r.x,r.y)].textContent = '☠️'; });
            arts.forEach(a => { cells[idx(a.x,a.y)].textContent = a.e; });
            snake.forEach((s,i) => {
                const c = cells[idx(s.x,s.y)];
                c.className = 'cell ' + (i === 0 ? 'snake-head' : 'snake-body');
            });
        }

        function levelComplete() {
            running = false;
            clearInterval(gameLoop); clearInterval(timerLoop);
            playSound('win');
            
            if (level >= 3) {
                // Beat all levels - final win!
                winGame();
            } else {
                // Show level complete modal, then advance
                document.getElementById('levelCompleteTitle').textContent = '🎉 ' + DIFF[level].name + ' Complete!';
                document.getElementById('levelScore').textContent = score;
                document.getElementById('levelFloppies').textContent = coll.floppy;
                document.getElementById('levelCDs').textContent = coll.cdrom;
                document.getElementById('levelVHS').textContent = coll.vhs;
                document.getElementById('nextLevelName').textContent = DIFF[level + 1].name;
                document.getElementById('nextLevelTarget').textContent = DIFF[level + 1].target;
                document.getElementById('nextLevelRots').textContent = DIFF[level + 1].rots;
                document.getElementById('levelModal').classList.add('show');
            }
        }

        function startNextLevel() {
            document.getElementById('levelModal').classList.remove('show');
            startGame(level + 1);
        }

        function winGame() {
            running = false;
            clearInterval(gameLoop); clearInterval(timerLoop);
            document.getElementById('pauseBtn').disabled = true;
            playSound('win');
            const acc = qT > 0 ? Math.round(qC/qT*100) : 0;
            document.getElementById('winScore').textContent = score;
            document.getElementById('winFloppies').textContent = coll.floppy;
            document.getElementById('winCDs').textContent = coll.cdrom;
            document.getElementById('winVHS').textContent = coll.vhs;
            document.getElementById('winAcc').textContent = acc + '%';
            document.getElementById('winModal').classList.add('show');
        }

        function gameOver() {
            running = false;
            clearInterval(gameLoop); clearInterval(timerLoop);
            document.getElementById('pauseBtn').disabled = true;
            playSound('gameover');
            const acc = qT > 0 ? Math.round(qC/qT*100) : 0;
            document.getElementById('overTitle').textContent = time <= 0 ? '⏰ Time Expired!' : '💀 Archive Corrupted!';
            document.getElementById('finalScore').textContent = score;
            document.getElementById('finalFloppies').textContent = coll.floppy;
            document.getElementById('finalCDs').textContent = coll.cdrom;
            document.getElementById('finalVHS').textContent = coll.vhs;
            document.getElementById('finalAcc').textContent = acc + '%';
            document.getElementById('overModal').classList.add('show');
        }

        function handleKey(e) {
            if (!running || paused || quiz) return;
            const k = e.key.toLowerCase();
            if ((k === 'arrowup' || k === 'w') && dir.y !== 1) nextDir = {x:0,y:-1};
            else if ((k === 'arrowdown' || k === 's') && dir.y !== -1) nextDir = {x:0,y:1};
            else if ((k === 'arrowleft' || k === 'a') && dir.x !== 1) nextDir = {x:-1,y:0};
            else if ((k === 'arrowright' || k === 'd') && dir.x !== -1) nextDir = {x:1,y:0};
            if (['arrowup','arrowdown','arrowleft','arrowright'].includes(k)) e.preventDefault();
        }

        // Initialize on load
        init();
    </script>
</body>
</html>