File size: 87,252 Bytes
e75654e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
7346308
e75654e
 
 
 
 
 
 
 
2703c4f
e75654e
2703c4f
 
e75654e
2703c4f
 
 
e75654e
 
2703c4f
 
 
 
 
 
 
 
e75654e
7346308
2703c4f
 
7346308
2703c4f
e75654e
 
2703c4f
 
e75654e
 
 
2703c4f
 
 
e75654e
 
 
2703c4f
e75654e
 
7346308
2703c4f
 
 
 
 
 
 
 
 
e75654e
 
2703c4f
 
 
 
 
 
 
 
 
 
 
 
e75654e
7346308
 
 
 
2703c4f
 
 
 
 
7346308
 
2703c4f
 
7346308
e75654e
7346308
 
 
 
 
 
 
 
 
2703c4f
7346308
 
 
 
 
 
 
 
 
 
 
 
 
 
2703c4f
 
e75654e
7346308
 
 
2703c4f
 
 
 
 
 
 
e75654e
 
 
2703c4f
 
 
 
 
 
 
 
7346308
 
2703c4f
e75654e
7346308
e75654e
 
2703c4f
7346308
2703c4f
 
 
e75654e
2703c4f
 
7346308
 
e75654e
 
7346308
2703c4f
 
 
7346308
 
 
 
 
2703c4f
 
e75654e
7346308
 
 
 
2703c4f
 
 
e75654e
7346308
 
 
e75654e
 
 
2703c4f
 
 
 
7346308
2703c4f
e75654e
7346308
 
2703c4f
7346308
 
 
 
2703c4f
 
e75654e
 
7346308
e75654e
 
2703c4f
 
e75654e
2703c4f
 
e75654e
2703c4f
 
 
 
 
 
 
 
7346308
 
2703c4f
 
7346308
 
2703c4f
7346308
 
e75654e
 
7346308
2703c4f
e75654e
 
 
2703c4f
 
 
 
 
7346308
2703c4f
 
7346308
2703c4f
 
7346308
e75654e
2703c4f
 
7346308
 
 
2703c4f
 
7346308
 
2703c4f
 
 
7346308
2703c4f
7346308
 
2703c4f
 
7346308
 
 
 
2703c4f
7346308
2703c4f
 
7346308
2703c4f
 
 
7346308
 
 
2703c4f
7346308
 
 
2703c4f
e75654e
 
 
 
 
 
2703c4f
7346308
 
 
2703c4f
 
 
 
e75654e
 
 
 
7346308
 
e75654e
7346308
e75654e
7346308
2703c4f
7346308
e75654e
 
7346308
e75654e
 
7346308
e75654e
 
7346308
e75654e
 
 
 
 
 
 
 
 
2703c4f
e75654e
7346308
e75654e
 
 
 
 
 
 
 
 
 
2703c4f
7346308
e75654e
 
 
 
 
2703c4f
 
 
e75654e
 
7346308
 
2703c4f
7346308
 
 
 
 
 
 
 
 
2703c4f
 
 
e75654e
2703c4f
 
e75654e
 
 
2703c4f
e75654e
 
 
 
7346308
e75654e
 
2703c4f
e75654e
 
7346308
 
2703c4f
e75654e
 
2703c4f
e75654e
 
 
2703c4f
 
 
e75654e
 
 
2703c4f
 
 
e75654e
 
 
2703c4f
7346308
e75654e
 
7346308
 
2703c4f
e75654e
 
7346308
 
e75654e
7346308
2703c4f
7346308
2703c4f
e75654e
 
 
2703c4f
7346308
 
2703c4f
7346308
 
2703c4f
7346308
 
 
e75654e
 
 
7346308
 
 
e75654e
7346308
 
 
 
 
 
 
e75654e
 
2703c4f
7346308
2703c4f
e75654e
7346308
 
 
e75654e
7346308
 
 
2703c4f
e75654e
7346308
2703c4f
e75654e
2703c4f
e75654e
7346308
 
 
2703c4f
7346308
 
 
2703c4f
7346308
 
 
2703c4f
 
7346308
e75654e
2703c4f
 
7346308
2703c4f
7346308
 
e75654e
 
 
 
 
 
2703c4f
e75654e
 
 
 
2703c4f
 
e75654e
7346308
2703c4f
e75654e
 
 
 
2703c4f
7346308
 
 
2703c4f
 
7346308
 
2703c4f
7346308
 
2703c4f
 
7346308
 
e75654e
2703c4f
e75654e
2703c4f
e75654e
 
 
 
 
7346308
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
 
 
 
7346308
e75654e
 
 
 
 
7346308
2703c4f
e75654e
 
2703c4f
 
 
 
e75654e
 
 
 
7346308
 
 
2703c4f
 
 
 
 
 
 
 
e75654e
 
 
 
 
7346308
2703c4f
e75654e
2703c4f
e75654e
2703c4f
 
 
 
e75654e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
7346308
e75654e
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
7346308
e75654e
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
7346308
e75654e
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
2703c4f
e75654e
 
 
 
 
 
2703c4f
 
 
e75654e
2703c4f
 
e75654e
 
2703c4f
e75654e
 
 
 
 
 
 
2703c4f
 
e75654e
7346308
2703c4f
 
 
 
 
 
e75654e
 
2703c4f
e75654e
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
2703c4f
 
e75654e
 
 
2703c4f
e75654e
 
 
 
 
 
7346308
 
2703c4f
e75654e
 
 
2703c4f
e75654e
 
 
 
 
 
2703c4f
7346308
2703c4f
e75654e
2703c4f
e75654e
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
2703c4f
e75654e
 
 
 
 
 
 
 
2703c4f
e75654e
7346308
 
e75654e
 
 
2703c4f
e75654e
 
7346308
e75654e
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
2703c4f
e75654e
2703c4f
e75654e
2703c4f
e75654e
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
7346308
e75654e
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
 
 
 
 
 
 
 
 
2703c4f
e75654e
 
7346308
 
2703c4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7346308
2703c4f
 
 
 
 
 
 
7346308
 
e75654e
2703c4f
 
 
e75654e
 
2703c4f
 
7346308
 
 
e75654e
 
 
7346308
e75654e
2703c4f
 
 
e75654e
 
2703c4f
 
7346308
 
 
e75654e
 
 
2703c4f
 
 
 
 
 
 
 
 
 
 
e75654e
 
2703c4f
 
 
 
7346308
2703c4f
e75654e
 
 
 
2703c4f
 
 
7346308
2703c4f
e75654e
 
 
 
2703c4f
 
 
 
7346308
2703c4f
e75654e
 
 
 
2703c4f
 
 
 
 
7346308
2703c4f
e75654e
 
 
 
2703c4f
7346308
 
2703c4f
 
 
 
 
 
 
 
 
 
 
 
 
 
7346308
 
 
 
 
 
 
2703c4f
 
7346308
 
 
 
 
 
2703c4f
7346308
2703c4f
7346308
2703c4f
7346308
 
e75654e
7346308
 
e75654e
2703c4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e75654e
 
2703c4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7346308
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gradio Choose Your Own Adventure</title>
    <script type="module" crossorigin src="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.css" />
    <style>
        body {
            font-family: 'Courier New', monospace;
            background-color: #f0f0f0;
            margin: 0;
            padding: 20px;
        }

        h1 {
            color: #c00;
            text-align: center;
            margin-bottom: 20px;
        }

        .container {
            max-width: 900px;
            margin: 0 auto;
            background-color: white;
            border: 5px solid #d4a017;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
        }

        .game-description {
            text-align: center;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #d4a017;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>CHOOSE YOUR OWN ADVENTURE</h1>
        <div class="game-description">
            <p>An interactive text adventure game powered by Gradio-lite</p>
            <p><em>Can you defeat the Evil Power Master and save the land?</em></p>
        </div>

        <gradio-lite theme="light">
<gradio-file name="app.py" entrypoint>
import gradio as gr
import random
import json
# Make sure to import everything needed from the other files
from game_data import game_data, illustrations, enemies_data, items_data
from game_engine import GameState, create_svg_illustration

def initialize_game():
    """Initialize a new game with default state"""
    game_state = GameState()
    current_page = 1
    page_data = game_data[str(current_page)]

    # Create the SVG illustration
    svg_content = create_svg_illustration(page_data.get("illustration", "default"))

    # Build page content
    content = f"<h2>{page_data.get('title', 'Untitled Page')}</h2>"
    content += page_data.get("content", "<p>No content for this page.</p>")

    # Build options
    options = []
    if "options" in page_data:
        for opt in page_data["options"]:
            options.append(opt["text"])
    if not options: # Fallback if no options defined
        options = ["Restart"]
        content += "<p><i>There are no actions defined here.</i></p>"


    # Update game statistics display
    stats = generate_stats_display(game_state) # Use the function here

    # Initialize inventory display
    inventory = generate_inventory_display(game_state) # Use the function here

    # Set story path
    story_path = "You are at the beginning of your adventure."

    return svg_content, content, gr.Dropdown(choices=options, label="What will you do?", value=None), game_state.to_json(), stats, inventory, story_path

def update_game(choice, game_state_json):
    """Update game based on player choice"""
    if not choice: # Handle empty choice if dropdown value is cleared
        return gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update()

    # Parse game state from JSON
    game_state_dict = json.loads(game_state_json)
    game_state = GameState.from_dict(game_state_dict)

    # Get current page data
    current_page = game_state.current_page
    # Ensure current_page is always a string for dictionary keys
    current_page_str = str(current_page)
    if current_page_str not in game_data:
         # Handle invalid page number - maybe reset?
         error_content = f"<p>Error: Reached invalid page number {current_page}. Resetting game.</p>"
         svg_error = create_svg_illustration("default")
         return svg_error, error_content, gr.Dropdown(choices=["Restart"], label="Error"), GameState().to_json(), generate_stats_display(GameState()), generate_inventory_display(GameState()), "Error - Resetting"

    page_data = game_data[current_page_str]

    # Find the selected option
    selected_option = None
    if "options" in page_data:
        for i, opt in enumerate(page_data["options"]):
            if opt["text"] == choice:
                selected_option = opt
                break

    # Also check alternative option if it was displayed and chosen
    alt_opt_data = page_data.get("alternativeOption")
    if not selected_option and alt_opt_data and alt_opt_data["text"] == choice:
        selected_option = alt_opt_data


    if selected_option is None:
        # Handle case where choice might be "Restart" or invalid
        if choice == "Restart":
             return initialize_game()
        else:
            # Stay on the same page if the choice wasn't found
            options = [opt["text"] for opt in page_data.get("options", [])]
            if not options: options = ["Restart"]
            svg_content = create_svg_illustration(page_data.get("illustration", "default"))
            content = f"<h2>{page_data.get('title', 'Error')}</h2>" + page_data.get("content", "") + "<p><i>Invalid choice selected. Please try again.</i></p>"
            stats_display = generate_stats_display(game_state)
            inventory_display = generate_inventory_display(game_state)
            story_path_display = f"You remain on page {current_page}: {page_data.get('title', 'Error')}"
            return svg_content, content, gr.Dropdown(choices=options, value=None), game_state.to_json(), stats_display, inventory_display, story_path_display

    # Check if this option requires an item
    item_required = selected_option.get("requireItem")
    if item_required and item_required not in game_state.inventory:
        content = f"<h2>{page_data['title']}</h2>" + page_data["content"] # Show original content
        content += f"<p style='color: red;'>You need the <strong>{item_required}</strong> for this option, but you don't have it.</p>"
        options = [opt["text"] for opt in page_data["options"]] # Keep current options
        svg_content = create_svg_illustration(page_data["illustration"])
        stats_display = generate_stats_display(game_state)
        inventory_display = generate_inventory_display(game_state)
        story_path_display = f"You remain on page {current_page}: {page_data['title']}"
        return svg_content, content, gr.Dropdown(choices=options, value=None), game_state.to_json(), stats_display, inventory_display, story_path_display

    # Check if option requires any item from a list
    any_item_required = selected_option.get("requireAnyItem")
    if any_item_required:
        has_required = any(item in game_state.inventory for item in any_item_required)
        if not has_required:
            item_list = ", ".join(f"'{item}'" for item in any_item_required)
            content = f"<h2>{page_data['title']}</h2>" + page_data["content"] # Show original content
            content += f"<p style='color: red;'>You need one of the following items for this option: <strong>{item_list}</strong>, but you don't have any.</p>"
            options = [opt["text"] for opt in page_data["options"]] # Keep current options
            svg_content = create_svg_illustration(page_data["illustration"])
            stats_display = generate_stats_display(game_state)
            inventory_display = generate_inventory_display(game_state)
            story_path_display = f"You remain on page {current_page}: {page_data['title']}"
            return svg_content, content, gr.Dropdown(choices=options, value=None), game_state.to_json(), stats_display, inventory_display, story_path_display

    # Process special items to collect
    item_to_add = selected_option.get("addItem")
    if item_to_add and item_to_add not in game_state.inventory:
        game_state.inventory.append(item_to_add)
        # Optionally remove item if it's consumable (needs item_data lookup)
        item_data = items_data.get(item_to_add, {})
        if item_data.get("type") == "consumable" and item_data.get("useOnAdd"): # e.g. a potion
             if "hpRestore" in item_data:
                  game_state.current_hp = min(game_state.max_hp, game_state.current_hp + item_data["hpRestore"])
             # Don't add consumable to inventory if used immediately? Or remove after use? Let's keep it simple and add it.

    # Move to the next page
    next_page = selected_option["next"]
    game_state.current_page = next_page
    next_page_str = str(next_page) # Use string for keys

    if next_page_str not in game_data:
         # Handle invalid destination page
         error_content = f"<p>Error: Option leads to invalid page number {next_page}. Resetting game.</p>"
         svg_error = create_svg_illustration("default")
         return svg_error, error_content, gr.Dropdown(choices=["Restart"], label="Error"), GameState().to_json(), generate_stats_display(GameState()), generate_inventory_display(GameState()), "Error - Resetting"

    if next_page not in game_state.visited_pages: # Avoid duplicates if revisiting
        game_state.visited_pages.append(next_page)

    # Update journey progress based on page transitions
    game_state.journey_progress += 5 # Increment progress with each decision
    if game_state.journey_progress > 100:
        game_state.journey_progress = 100

    battle_occurred = False
    battle_message = "" # Initialize battle message
    battle_result = True # Assume survival unless battle happens and player loses

    # Check for random battle (20% chance if page has randomBattle flag)
    # Check the *current* page data before moving
    if page_data.get("randomBattle", False) and random.random() < 0.2:
        battle_result, battle_log = simulate_battle(game_state)
        battle_occurred = True
        if not battle_result:
            # Player died in battle
            content = "<h2>Game Over</h2><p>You have been defeated in battle!</p>" + battle_log
            stats_display = generate_stats_display(game_state) # Show final stats
            inventory_display = generate_inventory_display(game_state) # Show final inventory
            return create_svg_illustration("game-over"), content, gr.Dropdown(choices=["Restart"], label="Game Over", value=None), game_state.to_json(), stats_display, inventory_display, "Game Over - You were defeated in battle."
        else:
             # Player survived battle, add battle log to the next page content
             battle_message = f"<div style='border: 1px solid green; padding: 10px; margin-top: 10px; background-color: #e8f5e9;'><strong>Battle Won!</strong>{battle_log}</div>"

    # Get new page data (for the destination page 'next_page')
    page_data = game_data[next_page_str]

    # Process page stat effects
    stat_increase = page_data.get("statIncrease")
    if stat_increase:
        stat = stat_increase["stat"]
        amount = stat_increase["amount"]
        if stat in game_state.stats:
             game_state.stats[stat] += amount

    # Process HP loss
    hp_loss = page_data.get("hpLoss")
    if hp_loss:
        game_state.current_hp -= hp_loss
        if game_state.current_hp <= 0:
            game_state.current_hp = 0
            content = "<h2>Game Over</h2><p>You have died from your wounds!</p>"
            stats_display = generate_stats_display(game_state) # Show final stats
            inventory_display = generate_inventory_display(game_state) # Show final inventory
            return create_svg_illustration("game-over"), content, gr.Dropdown(choices=["Restart"], label="Game Over", value=None), game_state.to_json(), stats_display, inventory_display, "Game Over - You died from your wounds."

    challenge_log = ""
    challenge_occurred = False
    # Check if this is a challenge page
    challenge = page_data.get("challenge")
    if challenge:
        challenge_occurred = True
        success, roll, total = perform_challenge(game_state, challenge)
        challenge_log = f"<div style='border: 1px solid #ccc; padding: 10px; margin-top: 10px; background-color: #eee;'>"
        challenge_log += f"<strong>Challenge: {challenge.get('title', 'Skill Check')}</strong><br>"
        challenge_log += f"Target Stat: {challenge['stat']}, Difficulty: {challenge['difficulty']}<br>"
        challenge_log += f"You rolled a {roll} + ({game_state.stats.get(challenge['stat'], 0)} {challenge['stat']}) = {total}<br>" # Corrected stat display

        # Update story based on challenge result
        if success:
            challenge_log += "<strong style='color: green;'>Success!</strong></div>"
            next_page = challenge["success"]
        else:
            # *** THIS IS THE CORRECTED LINE ***
            challenge_log += "<strong style='color: red;'>Failure!</strong></div>" 
            next_page = challenge["failure"]

        # Update game state to reflect the page change from the challenge outcome
        game_state.current_page = next_page
        next_page_str = str(next_page) # Use string for keys

        if next_page_str not in game_data:
            # Handle invalid page number from challenge outcome
            error_content = f"<p>Error: Challenge outcome leads to invalid page number {next_page}. Resetting game.</p>"
            svg_error = create_svg_illustration("default")
            return svg_error, error_content, gr.Dropdown(choices=["Restart"], label="Error"), GameState().to_json(), generate_stats_display(GameState()), generate_inventory_display(GameState()), "Error - Resetting"

        if next_page not in game_state.visited_pages:
            game_state.visited_pages.append(next_page)
        page_data = game_data[next_page_str] # Load data for the page determined by the challenge outcome

    # Create the SVG illustration for the final destination page
    svg_content = create_svg_illustration(page_data.get("illustration", "default"))

    # Handle game over on the destination page
    if page_data.get("gameOver", False):
        content = f"<h2>{page_data['title']}</h2>"
        content += page_data["content"]
        if battle_occurred and battle_result: content += battle_message # Add battle log if survived
        if challenge_occurred: content += challenge_log # Add challenge log
        if "ending" in page_data:
            content += f"<div style='text-align: center; margin-top: 20px; font-weight: bold; color: #c00;'>THE END</div>"
            content += f"<p style='font-style: italic;'>{page_data['ending']}</p>"

        stats_display = generate_stats_display(game_state) # Show final stats
        inventory_display = generate_inventory_display(game_state) # Show final inventory
        return svg_content, content, gr.Dropdown(choices=["Restart"], label="Game Over", value=None), game_state.to_json(), stats_display, inventory_display, "Game Over - " + page_data.get('title', 'Untitled')

    # Build page content for the final destination page
    content = f"<h2>{page_data.get('title', 'Untitled Page')}</h2>"
    content += page_data.get("content", "<p>No content.</p>")
    if battle_occurred and battle_result: content += battle_message # Add battle log if survived
    if challenge_occurred: content += challenge_log # Add challenge log

    # Build options for the final destination page
    options = []
    current_inventory = game_state.inventory # Cache inventory for checks
    if "options" in page_data: # Check if options exist
        for opt in page_data["options"]:
            option_available = True
            # Check if option requires an item
            req_item = opt.get("requireItem")
            if req_item and req_item not in current_inventory:
                 option_available = False
            # Check if option requires any item from a list
            req_any_item = opt.get("requireAnyItem")
            if option_available and req_any_item:
                if not any(item in current_inventory for item in req_any_item):
                     option_available = False

            if option_available:
                options.append(opt["text"])

    # Handle alternative option if necessary (only if no primary options are available/visible)
    alt_opt_data = page_data.get("alternativeOption")
    if alt_opt_data and not options:
        alt_show_if = alt_opt_data.get("showIf")
        # Show alternative if no condition or condition met
        if not alt_show_if or any(item in current_inventory for item in alt_show_if):
            options.append(alt_opt_data["text"])
            # Temporarily add the alternative option details to the current page's options
            # This ensures it can be selected on the next turn if it was the only one shown
            if "options" not in page_data: page_data["options"] = []
            # Avoid adding duplicate if somehow already there
            if alt_opt_data not in page_data["options"]:
                 page_data["options"].append(alt_opt_data)


    if not options: # If still no options (dead end maybe?)
        options = ["Restart"]
        content += "<p><i>There are no further actions you can take from here.</i></p>"

    # Update game progress display text
    story_path = f"You are on page {next_page}: {page_data.get('title', 'Untitled')}"
    if game_state.journey_progress >= 80:
        story_path += " (Nearing the conclusion)"
    elif game_state.journey_progress >= 50:
        story_path += " (Middle of your journey)"
    elif game_state.journey_progress >= 25:
        story_path += " (Adventure beginning)"

    # Generate final displays
    stats_display = generate_stats_display(game_state)
    inventory_display = generate_inventory_display(game_state)

    # Ensure dropdown value is reset so change event fires even if same choice is valid again
    return svg_content, content, gr.Dropdown(choices=options, label="What will you do?", value=None), game_state.to_json(), stats_display, inventory_display, story_path


def generate_stats_display(game_state):
    """Generate HTML for displaying player stats"""
    # Calculate HP percentage for the progress bar
    hp_percent = (game_state.current_hp / game_state.max_hp) * 100 if game_state.max_hp > 0 else 0
    hp_color = "#4CAF50" # Green
    if hp_percent < 30:
        hp_color = "#F44336" # Red
    elif hp_percent < 70:
        hp_color = "#FFC107" # Yellow

    stats_html = f"""
    <div style='display: flex; flex-wrap: wrap; gap: 10px; margin-top: 15px;'>
        <div style='background: #f0f0f0; padding: 5px 10px; border-radius: 5px;'>
            <strong>Courage:</strong> {game_state.stats.get('courage', 'N/A')}
        </div>
        <div style='background: #f0f0f0; padding: 5px 10px; border-radius: 5px;'>
            <strong>Wisdom:</strong> {game_state.stats.get('wisdom', 'N/A')}
        </div>
        <div style='background: #f0f0f0; padding: 5px 10px; border-radius: 5px;'>
            <strong>Strength:</strong> {game_state.stats.get('strength', 'N/A')}
        </div>
        <div style='background: #f0f0f0; padding: 5px 10px; border-radius: 5px; position: relative;'>
            <strong>HP:</strong> {game_state.current_hp}/{game_state.max_hp}
            <div style='width: 100px; height: 5px; background: #e0e0e0; margin-top: 3px; border-radius: 3px;'>
                <div style='width: {hp_percent}%; height: 100%; background: {hp_color}; border-radius: 3px;'></div>
            </div>
        </div>
    </div>
    """

    # Add journey progress
    stats_html += f"""
    <div style='margin-top: 10px;'>
        <div style='display: flex; justify-content: space-between; font-size: 12px;'>
            <span>Journey Progress:</span>
            <span>{game_state.journey_progress}%</span>
        </div>
        <div style='width: 100%; height: 5px; background: #e0e0e0; border-radius: 3px;'>
            <div style='width: {game_state.journey_progress}%; height: 100%; background: #2196F3; border-radius: 3px;'></div>
        </div>
    </div>
    """

    return stats_html

def generate_inventory_display(game_state):
    """Generate HTML for displaying player inventory"""
    if not game_state.inventory:
        return "<em>Your inventory is empty.</em>"

    inventory_html = "<div style='display: flex; flex-wrap: wrap; gap: 10px; margin-top: 10px;'>" # Added margin-top

    for item in game_state.inventory:
        item_data = items_data.get(item, {"type": "unknown", "description": "A mysterious item."})
        bg_color = "#e0e0e0" # Default grey
        item_type = item_data.get("type", "unknown")
        description = item_data.get('description', 'No description available.')

        if item_type == "weapon":
            bg_color = "#ffcdd2" # Light red
        elif item_type == "armor":
            bg_color = "#c8e6c9" # Light green
        elif item_type == "spell":
            bg_color = "#bbdefb" # Light blue
        elif item_type == "quest":
            bg_color = "#fff9c4" # Light yellow
        elif item_type == "consumable":
             bg_color = "#d1c4e9" # Light purple

        inventory_html += f"""
        <div style='background: {bg_color}; padding: 5px 10px; border-radius: 5px; position: relative; cursor: help;'
             title="{description}">
            {item}
        </div>
        """

    inventory_html += "</div>"
    return inventory_html

def perform_challenge(game_state, challenge):
    """Perform a skill challenge and determine success. Returns (success_bool, roll, total)"""
    stat = challenge["stat"]
    difficulty = challenge["difficulty"]

    # Roll dice (1-6) and add stat
    roll = random.randint(1, 6)
    player_stat_value = game_state.stats.get(stat, 0) # Default to 0 if stat doesn't exist
    total = roll + player_stat_value

    # Determine if successful
    success = total >= difficulty

    # Bonus for great success
    if success and total >= difficulty + 3:
        stat_increase = random.randint(1, 2)
        if stat in game_state.stats:
             game_state.stats[stat] = player_stat_value + stat_increase

    # Penalty for bad failure
    if not success and total <= difficulty - 3:
        stat_decrease = random.randint(1, 2)
        if stat in game_state.stats:
             game_state.stats[stat] = max(1, player_stat_value - stat_decrease) # Stat cannot go below 1

    # Record challenge outcome
    if success:
        game_state.challenges_won += 1

    return success, roll, total

def simulate_battle(game_state):
    """Simulate a battle with a random enemy. Returns (player_won_bool, battle_log_html)"""
    battle_log = "<div style='font-size: 0.9em; line-height: 1.4;'>"

    # Select a random enemy type
    enemy_types = list(enemies_data.keys())
    if not enemy_types:
         return True, "<p>No enemies defined for battle!</p>" # Avoid error if no enemies exist
    enemy_type = random.choice(enemy_types)
    enemy = enemies_data[enemy_type].copy() # Copy data to modify HP

    battle_log += f"<p>A wild <strong>{enemy_type}</strong> appears!</p>"

    # Simple battle simulation
    player_hp = game_state.current_hp
    enemy_hp = enemy["hp"]

    player_base_attack = game_state.stats.get("strength", 1)
    player_base_defense = 1 # Base defense

    player_attack = player_base_attack
    player_defense = player_base_defense

    # Add weapon/armor bonuses if the player has relevant items
    attack_bonuses = []
    defense_bonuses = []
    for item in game_state.inventory:
        item_data = items_data.get(item, {})
        if "attackBonus" in item_data:
            bonus = item_data["attackBonus"]
            player_attack += bonus
            attack_bonuses.append(f"{item} (+{bonus})")
        if "defenseBonus" in item_data:
            bonus = item_data["defenseBonus"]
            player_defense += bonus
            defense_bonuses.append(f"{item} (+{bonus})")

    if attack_bonuses: battle_log += f"<p>Your attack ({player_base_attack}) is boosted by: {', '.join(attack_bonuses)} = {player_attack} total.</p>"
    if defense_bonuses: battle_log += f"<p>Your defense ({player_base_defense}) is boosted by: {', '.join(defense_bonuses)} = {player_defense} total.</p>"

    enemy_attack = enemy["attack"]
    enemy_defense = enemy["defense"]

    battle_log += f"<p>Player HP: {player_hp}, Enemy HP: {enemy_hp}</p><hr>"

    # Simple turn-based combat
    turn = 1
    while player_hp > 0 and enemy_hp > 0 and turn < 20: # Add turn limit to prevent infinite loops
        battle_log += f"<p><strong>Turn {turn}:</strong></p>"
        # Player attacks
        damage_to_enemy = max(1, player_attack - enemy_defense)
        enemy_hp -= damage_to_enemy
        battle_log += f"&nbsp;&nbsp;You attack the {enemy_type} for {damage_to_enemy} damage. (Enemy HP: {max(0, enemy_hp)})<br>"

        if enemy_hp <= 0:
            battle_log += f"<p><strong>You defeated the {enemy_type}!</strong></p>"
            game_state.enemies_defeated += 1 # Increment defeated count
            break

        # Enemy attacks
        damage_to_player = max(1, enemy_attack - player_defense)
        player_hp -= damage_to_player
        battle_log += f"&nbsp;&nbsp;The {enemy_type} attacks you for {damage_to_player} damage. (Your HP: {max(0, player_hp)})"

        if player_hp <= 0:
            battle_log += f"<p><strong>The {enemy_type} defeated you!</strong></p>"
            break

        battle_log += "<hr>"
        turn += 1

    if turn >= 20 and player_hp > 0 and enemy_hp > 0: # Check if loop ended due to turn limit
         battle_log += "<p>The battle drags on too long and ends inconclusively. The enemy retreats!</p>"

    # Update player HP after battle
    game_state.current_hp = max(0, player_hp) # Ensure HP doesn't go below 0

    # Return True if player won (or survived), False if player lost
    player_won = game_state.current_hp > 0
    battle_log += "</div>"
    return player_won, battle_log


# Create Gradio interface
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# Choose Your Own Adventure")
    gr.Markdown("### Can you defeat the Evil Power Master?")

    with gr.Row():
        with gr.Column(scale=2):
            illustration = gr.HTML(label="Scene")
            content = gr.HTML(label="Story")
            choice = gr.Dropdown(label="What will you do?", allow_custom_value=False) # Prevent custom values

            # Hidden field to store game state as JSON
            game_state = gr.Textbox(label="Game State JSON", visible=False) # Changed label, still hidden

        with gr.Column(scale=1):
            story_path = gr.Markdown(label="Your Path")
            stats = gr.HTML(label="Character Stats")
            inventory = gr.HTML(label="Inventory")
            # map_btn = gr.Button("View Story Map") # Removed button for simplicity

    # Event listener for player choices
    choice.change(
        update_game,
        inputs=[choice, game_state],
        outputs=[illustration, content, choice, game_state, stats, inventory, story_path]
    )

    # Initialize game on load
    demo.load(
        initialize_game,
        inputs=[],
        outputs=[illustration, content, choice, game_state, stats, inventory, story_path]
    )

# Launch the Gradio app
demo.launch()

</gradio-file>

<gradio-file name="game_engine.py">
import json
import random
# Import illustrations from game_data for use in create_svg_illustration
from game_data import illustrations

class GameState:
    """Class to manage game state"""
    def __init__(self):
        self.current_page = 1
        self.visited_pages = [1]
        self.inventory = []
        self.max_hp = 30
        self.current_hp = 30
        self.stats = {
            "courage": 7,
            "wisdom": 5,
            "strength": 6,
            "luck": 4  # Luck isn't used currently, but kept it
        }
        self.status_effects = []
        self.enemies_defeated = 0
        self.challenges_won = 0
        self.current_path = "main"
        self.journey_progress = 0 # Initialize progress

    def to_json(self):
        """Convert game state to JSON string"""
        # Ensure stats is part of the dict being dumped
        state_dict = self.__dict__.copy()
        return json.dumps(state_dict)

    @classmethod
    def from_dict(cls, data):
        """Create a GameState object from a dictionary"""
        game_state = cls()
        # Make sure default values are preserved if not in data
        for key, value in game_state.__dict__.items():
             if key in data:
                 # Special handling for stats dictionary to avoid losing default keys
                 if key == 'stats' and isinstance(data[key], dict):
                      # Update default stats with loaded stats
                      default_stats = getattr(game_state, key)
                      default_stats.update(data[key])
                      setattr(game_state, key, default_stats)
                 else:
                      setattr(game_state, key, data[key])
        return game_state

def create_svg_illustration(illustration_key):
    """Return SVG illustration based on the illustration key"""
    # If illustration is not found, use a default illustration
    svg_code = illustrations.get(illustration_key, illustrations.get("default", "<svg viewBox='0 0 100 100'><rect width='100' height='100' fill='grey'/><text x='10' y='50' fill='white'>No Image</text></svg>"))

    # Wrap the SVG in a container div with appropriate styling
    # Ensure SVG scales correctly within the container
    return f"""
    <div style="width: 100%; height: 250px; border: 2px solid #999; border-radius: 5px; overflow: hidden; display: flex; justify-content: center; align-items: center; background-color: #eee;">
        <div style="width: 100%; height: 100%; display: flex; justify-content: center; align-items: center;">
             {svg_code}
        </div>
    </div>
    """
</gradio-file>

<gradio-file name="game_data.py">
# Game content data - contains all pages with branching storylines

game_data = {
    "1": {
        "title": "The Beginning",
        "content": """<p>The Evil Power Master has been terrorizing the land for years. With his dark magic, he has enslaved villages, summoned terrible monsters, and threatens to plunge the world into eternal darkness.</p>
        <p>You, a skilled adventurer, have been called upon to stop him. You stand at the entrance to the last free city, Silverhold, ready to begin your quest.</p>
        <p>The fate of the world rests on your shoulders. How will you prepare for your journey?</p>""",
        "options": [
            { "text": "Visit the local weaponsmith for equipment", "next": 2 },
            { "text": "Seek wisdom at the ancient temple", "next": 3 },
            { "text": "Meet with the resistance leader for information", "next": 4 }
        ],
        "illustration": "city-gates"
    },

    "2": {
        "title": "The Weaponsmith",
        "content": """<p>The city's renowned weaponsmith, Gorn, welcomes you to his forge. Weapons of all kinds line the walls, from simple daggers to exotic blades that pulse with magic.</p>
        <p>"I've been expecting you," Gorn says. "The Evil Power Master's fortress is treacherous. You'll need more than common steel to face what lies ahead."</p>
        <p>He presents you with three weapons, each crafted with special materials and enchantments.</p>""",
        "options": [
            { "text": "Take the Flaming Sword, powerful but dangerous", "next": 5, "addItem": "Flaming Sword" },
            { "text": "Choose the Whispering Bow, silent and precise", "next": 5, "addItem": "Whispering Bow" },
            { "text": "Select the Guardian Shield, defensive and protective", "next": 5, "addItem": "Guardian Shield" }
        ],
        "illustration": "weaponsmith"
    },

    "3": {
        "title": "The Ancient Temple",
        "content": """<p>The Temple of Eternal Light stands at the heart of Silverhold. Inside, the air is thick with incense, and soft chanting echoes through the chambers.</p>
        <p>High Priestess Alara greets you with concern in her eyes. "The Evil Power Master grows stronger each day. Before you face him, you should prepare your mind and spirit."</p>
        <p>She offers to teach you one of the temple's secret arts to aid your quest.</p>""",
        "options": [
            { "text": "Learn the Healing Light technique", "next": 5, "addItem": "Healing Light Spell" },
            { "text": "Master the Shield of Faith protection", "next": 5, "addItem": "Shield of Faith Spell" },
            { "text": "Study the ancient Binding Runes", "next": 5, "addItem": "Binding Runes Scroll" }
        ],
        "illustration": "temple"
    },

    "4": {
        "title": "The Resistance Leader",
        "content": """<p>In a secluded tavern, you meet Lyra, leader of the resistance against the Evil Power Master. Battle-scarred but determined, she unfurls a map across the table.</p>
        <p>"His fortress has three possible entry points," she explains, pointing to marked locations. "Each has its dangers and advantages. You must choose your path carefully."</p>
        <p>She also offers you a special item from the resistance's limited supplies.</p>""",
        "options": [
            { "text": "Take the map showing the secret tunnel entrance", "next": 5, "addItem": "Secret Tunnel Map" },
            { "text": "Accept the poison-tipped daggers for stealth", "next": 5, "addItem": "Poison Daggers" },
            { "text": "Choose the master key that unlocks ancient doors", "next": 5, "addItem": "Master Key" }
        ],
        "illustration": "resistance-meeting"
    },

    "5": {
        "title": "The Journey Begins",
        "content": """<p>Prepared as best you can be, you leave Silverhold behind. The path to the Evil Power Master's fortress takes you through the Shadowwood Forest, a place once beautiful but now corrupted by dark magic.</p>
        <p>Strange sounds echo among the twisted trees, and the feeling of being watched raises the hair on your neck. The main road is the fastest route, but there are alternatives.</p>""",
        "options": [
            { "text": "Take the main road through the forest", "next": 6 },
            { "text": "Follow the winding river path", "next": 7 },
            { "text": "Brave the ancient ruins shortcut", "next": 8 }
        ],
        "randomBattle": True,
        "illustration": "shadowwood-forest"
    },

    "6": {
        "title": "Ambush on the Road",
        "content": """<p>The main road through Shadowwood is overgrown but still visible. As you make your way forward, the trees suddenly rustle with movement.</p>
        <p>A group of the Evil Power Master's scouts emerges from hiding! They've been patrolling the road, looking for those who might oppose their master.</p>
        <p>"Surrender now," their leader snarls, "or face the consequences!"</p>""",
        "challenge": {
            "title": "Escape the Ambush",
            "description": "You need to either fight your way through or find an escape route.",
            "stat": "courage",
            "difficulty": 5, # Adjusted difficulty for demo
            "success": 9,
            "failure": 10
        },
        "illustration": "road-ambush"
    },

    "7": {
        "title": "The Mist-Shrouded River",
        "content": """<p>The river path is longer but less traveled. Thick mist clings to the water's surface, reducing visibility but potentially hiding you from enemies.</p>
        <p>As you follow the winding shore, you notice strange glowing lights beneath the water's surface. Suddenly, the mist parts to reveal a mysterious figure standing on the water itself.</p>
        <p>"Few choose this path," the figure says in a melodic voice. "To proceed, you must answer my riddle."</p>""",
        "challenge": {
            "title": "The River Spirit's Riddle",
            "description": "Answer correctly to gain passage and a blessing. Answer wrongly and face the river's peril.",
            "stat": "wisdom",
            "difficulty": 6, # Adjusted difficulty for demo
            "success": 11,
            "failure": 12
        },
        "illustration": "river-spirit"
    },

    "8": {
        "title": "The Forgotten Ruins",
        "content": """<p>The ancient ruins rise from the forest floor like the bones of a forgotten civilization. Moss-covered stones and crumbling arches create a labyrinth of passages and dead ends.</p>
        <p>This path is the shortest route to the fortress, but the ruins are said to be haunted by the spirits of those who built this place long ago.</p>
        <p>As you enter the heart of the ruins, the temperature drops dramatically, and whispers echo through the stone corridors.</p>""",
        "challenge": {
            "title": "Navigate the Haunted Ruins",
            "description": "Find the correct path through the ruins while avoiding the malevolent spirits.",
            "stat": "wisdom",
            "difficulty": 5, # Adjusted difficulty for demo
            "success": 13,
            "failure": 14
        },
        "illustration": "ancient-ruins"
    },

    "9": {
        "title": "Breaking Through",
        "content": """<p>With courage and quick thinking, you manage to fight your way through the ambush. Several scouts fall to your attacks, and the rest scatter into the forest.</p>
        <p>Though you've escaped, the commotion may have alerted other enemies in the area. You need to move quickly before reinforcements arrive.</p>
        <p>Ahead, the forest begins to thin, revealing the barren lands that surround the Evil Power Master's fortress.</p>""",
        "options": [
            { "text": "Rush across the open ground to the fortress", "next": 15 },
            { "text": "Wait for nightfall before crossing", "next": 16 },
            { "text": "Search for underground passages", "next": 17 }
        ],
        "randomBattle": True,
        "illustration": "forest-edge"
    },

    "10": {
        "title": "Captured!",
        "content": """<p>Despite your best efforts, the scouts overwhelm you. Disarmed and bound, you're dragged through the forest to a small outpost.</p>
        <p>"The Master will be pleased," the leader laughs. "He always enjoys interrogating those who think they can stand against him."</p>
        <p>As night falls, you're locked in a crude cell. All seems lost, but one guard looks at you with sympathy when the others aren't watching.</p>""",
        "options": [
            { "text": "Try to escape during the night", "next": 18 },
            { "text": "Appeal to the sympathetic guard", "next": 19 },
            { "text": "Wait for transport to the fortress", "next": 20 }
        ],
        "hpLoss": 5,
        "illustration": "prisoner-cell"
    },

    "11": {
        "title": "The Spirit's Blessing",
        "content": """<p>"Wisdom flows in you like the river itself," the spirit says, impressed by your answer. The mist swirls around you, and you feel a surge of energy.</p>
        <p>"Take my blessing. The waters will aid you against the darkness." The spirit touches your forehead, and a cool sensation spreads through your body.</p>
        <p>The mist parts, revealing a clear path forward and the distant silhouette of the Evil Power Master's fortress.</p>""",
        "options": [
            { "text": "Continue to the barren plains", "next": 15 },
            { "text": "Ask the spirit for more information", "next": 16 },
            { "text": "Follow the underwater passage the spirit reveals", "next": 17 }
        ],
        "addItem": "Water Spirit's Blessing",
        "statIncrease": { "stat": "wisdom", "amount": 2 },
        "illustration": "spirit-blessing"
    },

    "12": {
        "title": "The Spirit's Wrath",
        "content": """<p>"Incorrect," the spirit hisses, its melodic voice turning harsh. "The river does not suffer fools!"</p>
        <p>The mist thickens around you, and the calm water suddenly churns. A powerful current grabs your legs, pulling you into the depths.</p>
        <p>You struggle against the supernatural force, barely managing to break free and scramble onto the shore, soaked and battered.</p>""",
        "options": [
            { "text": "Continue along the riverbank", "next": 15 },
            { "text": "Find another path through the forest", "next": 16 },
            { "text": "Rest and recover before moving on", "next": 17 }
        ],
        "hpLoss": 8,
        "illustration": "river-danger"
    },

    "13": {
        "title": "Ancient Allies",
        "content": """<p>Your wisdom guides you through the ruins' labyrinth. As you navigate the crumbling corridors, the whispers change from threatening to curious.</p>
        <p>In the central chamber, transparent figures materialize - the ancient spirits of this place. They were not malevolent but protective, testing those who pass through.</p>
        <p>"You have shown respect and intelligence," their leader says. "We too oppose the darkness that has corrupted our land."</p>""",
        "options": [
            { "text": "Accept their offer of a magical artifact", "next": 15, "addItem": "Ancient Amulet" },
            { "text": "Ask for knowledge about the fortress", "next": 16 },
            { "text": "Request they guide you through secret paths", "next": 17 }
        ],
        "statIncrease": { "stat": "wisdom", "amount": 1 }, # Reward for passing challenge implicitly
        "illustration": "ancient-spirits"
    },

    "14": {
        "title": "Lost in Time",
        "content": """<p>The ruins become an impossible maze. Each turn leads to unfamiliar passages, and the whispers grow louder, disorienting you further.</p>
        <p>Hours pass as you wander, and malevolent spirits tug at your life force. By the time you finally stumble out of the ruins, you're exhausted and drained.</p>
        <p>Worse, you've emerged far from your intended path, and valuable time has been lost.</p>""",
        "options": [
            { "text": "Rest to recover your strength", "next": 15 },
            { "text": "Push on despite your exhaustion", "next": 16 },
            { "text": "Find a local guide in a nearby village", "next": 17 }
        ],
        "hpLoss": 10,
        "statIncrease": { "stat": "luck", "amount": -1 }, # Penalty for failing challenge implicitly
        "illustration": "lost-ruins"
    },

    "15": {
        "title": "The Looming Fortress",
        "content": """<p>The Evil Power Master's fortress dominates the landscape - a massive structure of black stone with towers that seem to pierce the clouds.</p>
        <p>Lightning crackles around its highest spires, and dark shapes patrol the battlements. The main gate is heavily guarded, but there must be other ways in.</p>
        <p>As you survey the imposing structure, you consider your options for infiltration.</p>""",
        "options": [
            { "text": "Approach the main gate (needs disguise)", "next": 21, "requireItem": "Guard Disguise" },
            { "text": "Scale the outer wall under cover of darkness", "next": 22 }, # Leads to Discovered! challenge
            { "text": "Look for the secret tunnel (requires Secret Tunnel Map)", "next": 24, "requireItem": "Secret Tunnel Map" }
        ],
        # Alternative option shows if neither disguise nor map is present
        "alternativeOption": { "text": "Search for another entrance (risky)", "next": 21, "showIf": ["Guard Disguise", "Secret Tunnel Map"] }, # Show if *missing* items
        "illustration": "evil-fortress"
    },

    "16": {
        "title": "Strategic Approach",
        "content": """<p>You take time to observe the fortress from a distance. Patrols move in predictable patterns, and supply wagons come and go at regular intervals.</p>
        <p>As night falls, different sections of the fortress light up, revealing potential weaknesses in security. Your careful observation provides valuable insights.</p>
        <p>You identify several possible entry points, each with its own risks and advantages.</p>""",
        "options": [
            { "text": "Infiltrate with an incoming supply wagon", "next": 21 },
            { "text": "Use magic to create a distraction (requires any spell)", "next": 22, "requireAnyItem": ["Healing Light Spell", "Shield of Faith Spell", "Binding Runes Scroll", "Water Spirit's Blessing"] }, # Leads to Discovered! challenge
            { "text": "Bribe a guard to let you in (requires special item)", "next": 23, "requireAnyItem": ["Ancient Amulet", "Master Key"] } # Leads to Secret Resistance
        ],
        # If no required items for specific options, provide a fallback
        "alternativeOption": {
            "text": "Attempt to sneak in using shadows",
            "next": 21,
            "showIf": ["Healing Light Spell", "Shield of Faith Spell", "Binding Runes Scroll", "Water Spirit's Blessing", "Ancient Amulet", "Master Key"] # Show if missing required items
        },
        "statIncrease": { "stat": "wisdom", "amount": 1 }, # Reward for observation
        "illustration": "fortress-observation"
    },

    "17": {
        "title": "The Hidden Way",
        "content": """<p>Your search reveals an unexpected approach to the fortress - an ancient maintenance tunnel that runs beneath the barren plains, likely forgotten by the Evil Power Master.</p>
        <p>The tunnel is old and could be unstable, but it would allow you to bypass most of the outer defenses entirely.</p>
        <p>As you consider this option, you hear patrol horns in the distance - they're sweeping the area for intruders.</p>""",
        "options": [
            { "text": "Enter the tunnel immediately", "next": 24 },
            { "text": "Hide and wait for the patrol to pass", "next": 21 },
            { "text": "Set a trap for the patrol", "next": 22 } # Leads to Discovered! challenge
        ],
        "illustration": "hidden-tunnel"
    },

    "18": {
        "title": "Night Escape",
        "content": """<p>You wait until the dead of night, when most guards are dozing at their posts. Using a loose stone you discovered in your cell, you work on the rusted lock.</p>
        <p>With a satisfying click, the cell door unlocks. Moving silently through the shadows, you retrieve your confiscated equipment from a nearby storeroom.</p>
        <p>Now you must leave the outpost without raising the alarm.</p>""",
        "challenge": {
            "title": "Stealthy Escape",
            "description": "Slip past the sleeping guards without alerting them.",
            "stat": "wisdom",
            "difficulty": 6,
            "success": 16, # Escaped successfully, back to observing
            "failure": 10 # Recaptured! Back to cell (loop)
        },
        "illustration": "night-escape"
    },

    "19": {
        "title": "Unexpected Ally",
        "content": """<p>When the guard passes your cell alone, you whisper, asking why they seemed sympathetic. The guard looks around nervously before responding.</p>
        <p>"My village was enslaved by the Evil Power Master. My family serves him under threat of death, but many of us secretly hope for his downfall."</p>
        <p>The guard offers to help you escape, but warns there will be a price for this betrayal if discovered.</p>""",
        "options": [
            { "text": "Accept the guard's help (gain disguise, proceed)", "next": 16, "addItem": "Guard Disguise"}, # Gain disguise, proceed to strategic approach
            { "text": "Decline - you don't want to put them at risk", "next": 18 }, # Try night escape instead
            { "text": "Convince them to join your cause", "next": 20 } # They arrange transport?
        ],
        "illustration": "guard-ally"
    },

    "20": {
        "title": "Journey to the Fortress",
        "content": """<p>At dawn, you're bound and loaded onto a prison wagon with other captured travelers. The journey to the fortress is uncomfortable, but you use the time to observe and plan.</p>
        <p>The other prisoners share what they know about the fortress. One mentions rumors of a resistance operating within the Evil Power Master's ranks.</p>
        <p>As the wagon approaches the massive gates, you begin to formulate a plan.</p>""",
        "options": [
            { "text": "Look for an opportunity to escape during transfer", "next": 22 }, # High risk, likely leads to discovered challenge
            { "text": "Remain captive to get inside, then escape", "next": 21 }, # Get inside, then try to move
            { "text": "Try to contact the internal resistance (needs clue/item)", "next": 23, "requireAnyItem": ["Guard Disguise", "Ancient Amulet"]} # Signal resistance
        ],
         "alternativeOption": { "text": "Remain captive with no plan (leads to capture)", "next": 28, "showIf": ["Guard Disguise", "Ancient Amulet"] }, # If no clue/item
        "illustration": "prison-wagon"
    },

    "21": {
        "title": "Infiltration",
        "content": """<p>Using your skills and preparation, you manage to infiltrate the outer perimeter of the fortress. The black stone walls loom overhead, even more intimidating up close.</p>
        <p>Guards patrol in pairs, and magical wards pulse with dark energy. You'll need to be extremely careful as you navigate deeper into enemy territory.</p>
        <p>The sound of a bell signals a shift change, providing a momentary distraction.</p>""",
        "options": [
            { "text": "Head towards the central tower", "next": 25 },
            { "text": "Search for the dungeons", "next": 26 },
            { "text": "Follow a group of mages", "next": 27 }
        ],
        "randomBattle": True,
        "illustration": "fortress-interior"
    },

    "22": {
        "title": "Discovered!",
        "content": """<p>As you make your way through the fortress, an alarm suddenly sounds! Your presence has been detected, and you hear the heavy footsteps of guards approaching.</p>
        <p>"Intruder in the south corridor!" a voice shouts. "Seal all exits!"</p>
        <p>You have moments to react before you're surrounded.</p>""",
        "challenge": {
            "title": "Escape Detection",
            "description": "You need to escape from the approaching guards before they trap you.",
            "stat": "courage",
            "difficulty": 7,
            "success": 25, # Managed to evade, head to tower
            "failure": 28 # Captured Again
        },
        "illustration": "fortress-alarm"
    },

    "23": {
        "title": "Secret Resistance",
        "content": """<p>Through luck or skill (or maybe a bribe or disguise), you make contact with members of the secret resistance operating within the fortress. They're skeptical of you at first, but your actions have convinced them of your intentions.</p>
        <p>"We've been working to undermine the Evil Power Master for years," their leader whispers. "Now with your help, we might finally have a chance to end his reign."</p>
        <p>They share crucial information about the fortress layout and the Evil Power Master's weaknesses.</p>""",
        "options": [
            { "text": "Join their planned uprising", "next": 29 },
            { "text": "Ask them to help you reach the Power Master directly", "next": 30 },
            { "text": "Share your knowledge with them", "next": 31 }
        ],
        "illustration": "secret-meeting"
    },

    "24": {
        "title": "Underground Passage",
        "content": """<p>The ancient tunnel is damp and narrow, forcing you to crouch as you make your way forward. Roots hang from the ceiling, and the air feels thick with age.</p>
        <p>After what seems like hours of careful navigation, you notice light ahead. The tunnel opens into what appears to be a forgotten storeroom beneath the fortress.</p>
        <p>Dust covers everything, but you can hear movement in the rooms above.</p>""",
        "options": [
            { "text": "Explore the storeroom for useful items", "next": 32 },
            { "text": "Find a way up to the main fortress", "next": 33 },
            { "text": "Listen carefully to determine what's happening above", "next": 34 }
        ],
        "illustration": "underground-passage"
    },

    "25": {
        "title": "The Central Tower",
        "content": """<p>The central tower rises from the heart of the fortress like a black spear. As you get closer, you can feel powerful magic emanating from its peak.</p>
        <p>Guards are more numerous here, and you spot several dark mages performing rituals in alcoves along the walls. Whatever the Evil Power Master is planning, it seems to be approaching its climax.</p>
        <p>A massive door blocks the entrance to the tower, engraved with strange symbols.</p>""",
        "options": [
            { "text": "Try to decipher the symbols (needs wisdom)", "next": 35 },
            { "text": "Look for another entrance", "next": 36 },
            { "text": "Use magic/key to open the door", "next": 37, "requireAnyItem": ["Healing Light Spell", "Shield of Faith Spell", "Binding Runes Scroll", "Water Spirit's Blessing", "Master Key"] }
        ],
        "alternativeOption": { "text": "Wait and observe the mages", "next": 36, "showIf": ["Healing Light Spell", "Shield of Faith Spell", "Binding Runes Scroll", "Water Spirit's Blessing", "Master Key"] },
        "illustration": "central-tower"
    },

    "26": {
        "title": "The Dungeons",
        "content": """<p>The dungeons are a maze of dark, damp corridors lined with cells. Moans and whispers echo off the stone walls, creating an eerie atmosphere of despair.</p>
        <p>Many of the cells contain prisoners - ordinary people from villages conquered by the Evil Power Master, resistance fighters, and even a few magical creatures.</p>
        <p>At the end of one corridor, you spot a heavily guarded cell that seems more important than the others.</p>""",
        "options": [
            { "text": "Try to free some prisoners to cause a distraction", "next": 38 },
            { "text": "Investigate the heavily guarded cell", "next": 39 },
            { "text": "Look for a passage leading upward", "next": 40 }
        ],
        "illustration": "dungeon-corridors"
    },

    "27": {
        "title": "The Ritual Chamber",
        "content": """<p>Following the group of mages leads you to a large circular chamber. Hidden in the shadows, you observe as they prepare for what appears to be an important ritual.</p>
        <p>In the center of the room hovers a dark crystal, pulsing with malevolent energy. The mages arrange themselves around it, beginning a chant in an ancient language.</p>
        <p>"Soon the master's power will be unstoppable," one of them mutters. "The alignment is nearly perfect."</p>""",
        "options": [
            { "text": "Disrupt the ritual", "next": 41 },
            { "text": "Quietly observe to learn more", "next": 42 },
            { "text": "Steal the crystal when they're distracted", "next": 43 }
        ],
        "illustration": "ritual-chamber"
    },

    "28": {
        "title": "Captured Again",
        "content": """<p>The guards overwhelm you, and this time, there's no easy escape. You're brought before a high-ranking officer, who smiles coldly.</p>
        <p>"The Master will be most pleased," he says. "He so rarely gets to meet those foolish enough to challenge him directly."</p>
        <p>You're escorted under heavy guard toward the central tower, where the Evil Power Master awaits.</p>""",
        "options": [
            { "text": "Look for another chance to escape (low odds)", "next": 44 },
            { "text": "Prepare yourself mentally to face the Power Master", "next": 45 },
            { "text": "Try to gather information from the guards", "next": 46 }
        ],
        "illustration": "prisoner-escort"
    },

    "29": {
        "title": "The Uprising",
        "content": """<p>The resistance has planned carefully. When the signal is given, chaos erupts throughout the fortress. Guards find themselves fighting servants, and magical wards fail as saboteurs destroy key components.</p>
        <p>In the confusion, you and a small team of resistance fighters make your way toward the central tower where the Evil Power Master resides.</p>
        <p>"This is our only chance," the resistance leader whispers. "We must reach him before he realizes what's happening."</p>""",
        "options": [
            { "text": "Lead the charge directly", "next": 47 },
            { "text": "Split up to create more distractions", "next": 48 }, # Send player to final battle
            { "text": "Sneak ahead while the others draw attention", "next": 49 }
        ],
        "illustration": "fortress-uprising"
    },

    "30": {
        "title": "The Final Approach",
        "content": """<p>With the resistance's help, you navigate through secret passages unknown to most of the fortress inhabitants. These narrow corridors, built during the fortress's construction, lead directly to the upper levels.</p>
        <p>"The Power Master's chambers are at the top of the central tower," your guide explains. "He rarely leaves them these days, too focused on some grand ritual. This may be our only chance to stop him."</p>
        <p>The passage ends at a hidden door, beyond which lies the base of the central tower.</p>""",
        "options": [
            { "text": "Enter the tower cautiously", "next": 50 },
            { "text": "Ask about the ritual the Power Master is performing", "next": 51 },
            { "text": "Request additional help for the final confrontation", "next": 52 }
        ],
        "illustration": "secret-passage"
    },

    "31": {
        "title": "Knowledge Exchange",
        "content": """<p>You share everything you've learned on your journey - about the river spirit, the ancient ruins, and any magical artifacts you've collected. In return, the resistance provides crucial information.</p>
        <p>"The Evil Power Master draws his power from an ancient crystal," their sage explains. "It amplifies his natural abilities tenfold, but it's also a weakness. Destroy the crystal, and his power will be greatly diminished."</p>
        <p>They also reveal that the Power Master is preparing for a cosmic alignment that will make him virtually unstoppable.</p>""",
        "options": [
            { "text": "Ask how to locate the crystal", "next": 53 },
            { "text": "Find out when the alignment will occur", "next": 54 },
            { "text": "Learn if there are any weapons that can harm the crystal", "next": 55 }
        ],
        "statIncrease": { "stat": "wisdom", "amount": 1 }, # Reward for sharing info
        "illustration": "knowledge-sharing"
    },

    # --- Placeholder Pages ---
    "32": { "title": "Storeroom Search", "content": "<p>You search the dusty storeroom. Mostly old crates and broken tools, but you find a forgotten Healing Potion tucked away!</p>", "options": [{ "text": "Drink Potion and find way up", "next": 33, "addItem": "Healing Potion" }], "illustration": "underground-passage" }, # Add Potion
    "33": { "title": "Ascending", "content": "<p>You find a loose grate leading to the fortress kitchens. Smells marginally better than the tunnel. You emerge cautiously.</p>", "options": [{ "text": "Sneak through the kitchens", "next": 21 }], "illustration": "fortress-interior" },
    "34": { "title": "Listening", "content": "<p>You hear the rhythmic marching of guards above, and the distant clang of metal from the armory. Sounds like standard patrols, nothing immediately alarming.</p>", "options": [{ "text": "Explore the storeroom", "next": 32 }, { "text": "Try to find a way up", "next": 33 }], "illustration": "underground-passage" },
    "35": { "title": "Deciphering Symbols", "content": "<p>The symbols pulse with dark energy. Your wisdom tells you they form a powerful ward, possibly tied to the Master's life force. It requires a specific magical resonance or immense force to bypass.</p>", "options": [{ "text": "Look for another entrance", "next": 36 }, { "text": "Use magic/key (if you have it)", "next": 37, "requireAnyItem": ["Healing Light Spell", "Shield of Faith Spell", "Binding Runes Scroll", "Water Spirit's Blessing", "Master Key", "Ancient Amulet"] }], "statIncrease": {"stat":"wisdom", "amount":1}, "illustration": "central-tower" },
    "36": { "title": "Searching for Entry", "content": "<p>You circle the base of the tower. High up, you spot a less guarded balcony. Scaling the sheer, dark stone might be possible for someone with exceptional strength or courage, but it's extremely dangerous.</p>", "options": [{ "text": "Attempt to climb (Strength/Courage Challenge)", "next": 22 }, {"text": "Return to the main door", "next": 25}], "illustration": "central-tower" }, # Leads to challenge
    "37": { "title": "Magical Entry", "content": "<p>You focus your magical item (or the Master Key). The symbols flare violently, then fade as the massive door groans open! You step into the oppressive darkness of the tower base.</p>", "options": [{ "text": "Ascend the tower", "next": 48 }], "illustration": "final-confrontation" }, # Straight to the top
    "38": { "title": "Causing Distraction", "content": "<p>Using your lockpicking skills (or the Master Key), you manage to unlock a few cells. The freed prisoners, though weak, immediately cause a commotion, shouting and banging, drawing several guards away from their posts.</p>", "options": [{ "text": "Investigate the guarded cell", "next": 39 }, {"text": "Look for an exit upwards", "next": 40}], "requireAnyItem": ["Master Key"], "illustration": "dungeon-corridors" }, # Added item requirement
    "39": { "title": "Guarded Cell", "content": "<p>Inside the heavily barred cell is a frail figure clad in mage robes – likely a captured resistance member or someone who defied the Master. They look up weakly as you approach the bars.</p>", "options": [{ "text": "Attempt to free the mage (needs Master Key/Runes)", "next": 30, "requireAnyItem": ["Master Key", "Binding Runes Scroll"] }, {"text": "Leave them for now", "next": 40}], "illustration": "dungeon-corridors" }, # Specified required items
    "40": { "title": "Finding Passage", "content": "<p>Behind a loose tapestry depicting the Master's grim visage, you find a hidden spiral staircase leading up, likely towards the main fortress levels above the dungeons.</p>", "options": [{ "text": "Take the stairs", "next": 21 }], "illustration": "fortress-interior" },
    "41": { "title": "Disrupting the Ritual", "content": "<p>You burst into the chamber! The mages are startled, their chant faltering. You launch an attack, disrupting their concentration entirely. The central crystal flares erratically, and raw magic arcs through the room.</p>", "options": [{ "text": "Fight the enraged mages!", "next": 22 }], "illustration": "ritual-chamber" }, # Likely leads to being discovered challenge
    "42": { "title": "Observing the Ritual", "content": "<p>You watch intently as the mages chant in unison. The crystal seems to be drawing power from ley lines deep within the fortress, focusing it towards the tower's peak for the Master's final empowerment during the alignment.</p>", "options": [{ "text": "Try to disrupt it now", "next": 41 }, {"text": "Try to steal the crystal (very risky)", "next": 43}, {"text": "Leave and head to the tower", "next": 25}], "statIncrease": {"stat": "wisdom", "amount": 1}, "illustration": "ritual-chamber" },
    "43": { "title": "Stealing the Crystal", "content": "<p>As the mages are deep in their chant, you attempt to swiftly snatch the pulsing crystal. It burns with raw, cold energy! They immediately sense the disturbance and react with fury, turning to attack you.</p>", "options": [{ "text": "Fight back!", "next": 22 }], "illustration": "ritual-chamber" }, # Leads to being discovered challenge
    "44": { "title": "Escape Attempt", "content": "<p>You scan your surroundings, looking for any weakness, any chance. But the guards are professionals, their grip like iron, their eyes vigilant. No escape seems possible right now.</p>", "options": [{ "text": "Focus and prepare mentally", "next": 45 }, {"text": "Try to glean info from guards", "next": 46}], "illustration": "prisoner-escort" },
    "45": { "title": "Mental Preparation", "content": "<p>You close your eyes briefly, focusing your mind. You recall your training, your purpose, the faces of those suffering under the Master's rule. You are ready. Let him come.</p>", "options": [{ "text": "Continue to the Master's chamber", "next": 48 }], "statIncrease": {"stat": "courage", "amount": 1}, "illustration": "final-confrontation" }, # Directly to final battle
    "46": { "title": "Gathering Information", "content": "<p>You try to subtly listen to the guards' chatter. They mutter about the Master being 'nearly ready' and the 'alignment is minutes away'. One nervously mentions the crystal is 'unstable' at peak power. Time is critically short.</p>", "options": [{ "text": "Focus and prepare mentally", "next": 45 }, {"text": "Look desperately for escape again", "next": 44}], "statIncrease": {"stat": "wisdom", "amount": 1}, "illustration": "prisoner-escort" },
    "47": { "title": "Leading the Charge", "content": "<p>With a battle cry, you lead the resistance fighters forward, cutting through the surprised ranks of the Master's personal guard. Steel clashes against steel as you push relentlessly towards the main tower entrance.</p>", "options": [{ "text": "Breach the tower door", "next": 25 }], "illustration": "fortress-uprising" },
    # Page 48 is the final confrontation
    "49": { "title": "Sneaking Ahead", "content": "<p>While the main uprising creates chaos as a diversion, you slip through the shadows, bypassing the main battle. You find a less-guarded servant's entrance to the central tower.</p>", "options": [{ "text": "Enter the tower stealthily", "next": 50 }], "illustration": "secret-passage" },
    "50": { "title": "Cautious Entry", "content": "<p>You ease open the hidden door and step into the base of the central tower. A grand staircase spirals upwards into oppressive darkness. The very air hums with palpable dark energy.</p>", "options": [{ "text": "Ascend the tower carefully", "next": 48 }], "illustration": "final-confrontation" }, # Head to final battle
    "51": { "title": "Ritual Information", "content": "<p>Your resistance guide explains the Master is performing a ritual tied to a rare cosmic alignment to achieve immense power, possibly immortality. The dark crystal in his chamber is the focal point.</p>", "options": [{ "text": "Enter the tower now", "next": 50 }, {"text": "Ask for additional backup", "next": 52}], "illustration": "secret-passage" },
    "52": { "title": "Requesting Help", "content": "<p>The resistance leader nods grimly. 'Take Kaelen,' she says, gesturing to a scarred warrior armed with a blessed axe. 'May their strength aid yours.' Kaelen gives a curt nod.</p>", "options": [{ "text": "Accept Kaelen's help and enter", "next": 48 }], "addItem": "Resistance Ally", "illustration": "secret-passage" }, # Add ally and go to fight
    "53": { "title": "Crystal Location", "content": "<p>The resistance sage confirms the crystal is kept in the Master's main audience chamber, at the very top of the central tower. It reputedly floats above his throne, radiating immense power.</p>", "options": [{ "text": "Prepare and head to the tower", "next": 48 }], "illustration": "knowledge-sharing" }, # Go to final battle
    "54": { "title": "Alignment Timing", "content": "<p>'The alignment is upon us!' the sage gasps, pointing to strange lights in the sky visible even through the fortress windows. 'It may already be too late! You must hurry!'</p>", "options": [{ "text": "Race to the tower immediately!", "next": 48 }], "illustration": "knowledge-sharing" }, # Go to final battle
    "55": { "title": "Crystal Weakness", "content": "<p>They mention ancient texts hinting that the crystal, forged in darkness, is vulnerable to sources of pure light or concentrated holy energy. Perhaps your Healing Light spell, if focused, or the Shield of Faith could disrupt it?</p>", "options": [{ "text": "Remember this and head to the tower", "next": 48 }], "illustration": "knowledge-sharing" }, # Go to final battle

    "56": { # Direct Confrontation Battle
        "title": "Direct Confrontation",
        "content": """<p>You charge at the Evil Power Master, drawing on all your courage and strength. He meets your attack with blasts of dark magic, the air between you crackling and distorting with raw energy.</p>
        <p>The battle is fierce, pushing you to your absolute limits. Each blow you land seems partially absorbed by the dark energy surrounding him, while his counter-attacks grow increasingly powerful and difficult to evade.</p>
        <p>As the fight rages, you realize that as long as the dark crystal pulses behind him, this might be an unwinnable war of attrition.</p>""",
        "challenge": {
            "title": "Battle with the Evil Power Master",
            "description": "You must overcome his dark magic through sheer determination, skill, and perhaps exploiting a weakness.",
            "stat": "strength", # Primary check is Strength
            "difficulty": 9, # High difficulty
            "success": 59, # Hero's Victory
            "failure": 60 # Darkness Prevails
        },
        "illustration": "power-battle"
    },
    "57": { # Crystal Destruction Attempt
        "title": "Crystal Destruction",
        "content": """<p>Recognizing the crystal as the true source of his overwhelming power, you feint an attack and then lunge towards the malevolently pulsing gem floating above the throne. The Evil Power Master shouts in alarm and fury, desperately trying to intercept you.</p>
        <p>"No! Stay away from that, you insignificant fool! You have no conception of the forces you're tampering with!"</p>
        <p>As you get closer, the crystal's energy intensifies dramatically, throwing up a shimmering barrier of pure dark magic that you must break through to reach it.</p>""",
        "challenge": {
            "title": "Breaking the Crystal Barrier",
            "description": "You must overcome the crystal's protective magic using wit, willpower, or perhaps a specific counter-energy.",
            "stat": "wisdom", # Primary check is Wisdom
            "difficulty": 8, # Slightly lower difficulty than direct fight
            "success": 61, # The Crystal Shatters
            "failure": 62 # A Desperate Gambit (leads to sacrifice ending)
        },
        "illustration": "crystal-barrier"
    },
    "58": {
         "title": "Attempt to Reason",
         "content": """<p>You hold up a hand, trying to speak to the Evil Power Master, appealing to any possible reason or shred of humanity that might remain within the darkness. He pauses, then throws back his head and laughs – a chilling, hollow sound.</p>
         <p>"Humanity? Reason? Such fleeting, petty concerns! Power is the only truth, the only reason! And soon, I shall have ultimate power!" he snarls, launching a devastating bolt of dark energy directly at you.</p>
         <p>Clearly, negotiation was never an option.</p>""",
         "options": [
             { "text": "Attack the Evil Power Master directly", "next": 56 },
             { "text": "Try to destroy the crystal", "next": 57 }
         ],
         "illustration": "final-confrontation"
    },
    "59": {
        "title": "A Hero's Victory",
        "content": """<p>Through sheer determination, skill, and perhaps exploiting a momentary weakness, you manage to overcome the Evil Power Master's formidable defenses. As he staggers backwards from your decisive blow, his concentration wavers, and his connection to the crystal weakens for a critical instant.</p>
        <p>Seizing the opportunity, you strike the dark crystal with all your remaining might (or perhaps channel pure energy into it). It cracks visibly, then shatters in a blinding flash of light and concussive energy that throws you back.</p>
        <p>The Evil Power Master screams a final, agonized cry as his borrowed power dissipates like smoke. "Impossible! I was... so close... to ultimate power!"</p>
        <p>As the blinding light fades, you stand victorious amidst the crumbling chamber. The dark magic recedes from the land, and the people are free once more.</p>""",
        "gameOver": True,
        "ending": "You have defeated the Evil Power Master and destroyed the source of his terrible power. Songs will be sung of your bravery and skill for generations to come. The land slowly begins to heal from his corruption, and you are hailed as the Hero of Silverhold throughout the recovering realm.",
        "illustration": "hero-victory"
    },
    "60": {
        "title": "Darkness Prevails",
        "content": """<p>Despite your bravery and best efforts, the Evil Power Master's command over dark magic, amplified by the crystal, is simply too great. His relentless attacks overwhelm your defenses, shattering your weapon or shield, and bringing you crashing to your knees.</p>
        <p>"Valiant, perhaps, but ultimately futile," he says, looking down at you with cold contempt. "You could have served me, you know. Gained power beyond your imagining. Now... you will simply serve as a warning." He raises a hand, dark energy coalescing.</p>
        <p>As your vision fades into darkness, you see the crystal's evil glow intensifying, the alignment reaching its peak. You have failed. Darkness will cover the land, and the Master's reign is absolute.</p>""",
        "gameOver": True,
        "ending": "The Evil Power Master proved too powerful, and your heroic quest ends in tragic defeat. Darkness spreads unchallenged across the land as he completes his ritual and ascends to even greater, terrifying power. Hope fades, but perhaps, someday, another hero will rise to challenge his eternal reign.",
        "illustration": "dark-victory"
    },
    "61": {
        "title": "The Crystal Shatters",
        "content": """<p>Drawing on all your wisdom and inner willpower (or perhaps using a specific magical counter), you manage to push through the crystal's defensive barrier. Reaching out, you touch its vibrating surface, which burns with an unnatural cold against your skin.</p>
        <p>The Evil Power Master shrieks in mingled fury and desperation, "Stop! Stop, you fool! You'll doom us all!"</p>
        <p>With a final, concentrated effort, you channel your own life force or focused energy into the dark gem. Cracks spiderweb across its obsidian surface, spreading rapidly until it detonates with a deafening implosion of light and sound, sucking the dark energy from the room.</p>
        <p>When your vision clears, the Evil Power Master lies broken and powerless on the floor, his power utterly broken along with the crystal. The oppressive atmosphere lifts, and peace can finally begin to return to the land.</p>""",
        "gameOver": True,
        "ending": "By cleverly targeting and destroying the source of the Evil Power Master's augmented strength, you have saved the realm from his tyranny. The fortress itself begins to shake and crumble as its dark magic foundations unravel. You escape just in time, leaving the ruin behind. Your name becomes a legend associated with wisdom and decisive action.",
        "illustration": "crystal-destroyed"
    },
    "62": {
        "title": "A Desperate Gambit",
        "content": """<p>The crystal's protective barrier proves too strong, violently repelling your attempts to break through. As you struggle against its crushing force, the Evil Power Master regains his composure and approaches, a cruel smile playing on his lips.</p>
        <p>"Did you truly think it would be that easy?" he mocks, preparing a final, devastating spell. "This crystal has endured for millennia. It cannot be destroyed by a pathetic mortal like you."</p>
        <p>Seeing no other option, in a final, desperate move, you gather all your remaining strength and hurl your most powerful weapon or focus your strongest spell directly at the unstable crystal.</p>
        <p>To your surprise and the Evil Power Master's horror, this doesn't break the barrier but instead creates a catastrophic resonance effect within the overloaded gem. The crystal begins to vibrate violently, its contained energy spiraling wildly out of control.</p>
        <p>"What have you done?!" the Master cries in terror as the crystal's energy erupts outwards, engulfing both of you, the chamber, and the entire tower top in blinding, destructive light.</p>""",
        "gameOver": True,
        "ending": "Your desperate, final attack destabilized the dark crystal, causing a catastrophic release of uncontrolled energy that utterly annihilated the Evil Power Master, his chamber, and tragically, yourself. Though you did not survive, your ultimate sacrifice shattered his power and saved the realm from imminent darkness. Bards will forever sing of your selfless heroism.",
        "illustration": "heroic-sacrifice"
    }
}


# --- Item Data ---
items_data = {
    "Flaming Sword": {"type": "weapon", "description": "A sword wreathed in magical fire. Looks dangerous to friend and foe. +3 Attack.", "attackBonus": 3},
    "Whispering Bow": {"type": "weapon", "description": "A finely crafted bow enchanted for silence. Favored by assassins. +2 Attack.", "attackBonus": 2},
    "Guardian Shield": {"type": "armor", "description": "A sturdy shield blessed with protective magic. Glows faintly. +2 Defense.", "defenseBonus": 2},
    "Healing Light Spell": {"type": "spell", "description": "A basic clerical spell that can mend minor wounds."},
    "Shield of Faith Spell": {"type": "spell", "description": "A protective spell that creates a temporary shimmering barrier."},
    "Binding Runes Scroll": {"type": "spell", "description": "A scroll containing complex runes to temporarily immobilize an enemy."},
    "Secret Tunnel Map": {"type": "quest", "description": "A crudely drawn map marking a hidden tunnel entrance near the fortress's north wall."},
    "Poison Daggers": {"type": "weapon", "description": "A pair of wicked-looking daggers coated with a fast-acting paralytic poison. +1 Attack.", "attackBonus": 1},
    "Master Key": {"type": "quest", "description": "A heavy iron key with intricate wards. Said to unlock any non-magical lock."},
    "Water Spirit's Blessing": {"type": "spell", "description": "A lingering blessing from the river spirit, enhancing clarity and insight."},
    "Ancient Amulet": {"type": "quest", "description": "A heavy stone amulet radiating ancient protective magic. Seems to resist dark energy. +1 Defense.", "defenseBonus": 1},
    "Guard Disguise": {"type": "quest", "description": "A slightly ill-fitting uniform worn by the fortress guards."},
    "Healing Potion": {"type": "consumable", "description": "A vial containing a sparkling red liquid. Restores 10 HP.", "hpRestore": 10, "useOnAdd": True}, # Added HP restore value and use flag
    "Resistance Ally": {"type": "quest", "description": "Kaelen, a grim resistance fighter armed with a blessed axe, fights alongside you."}, # Added name
}

# --- Enemy Data ---
enemies_data = {
    "Scout": {"hp": 15, "attack": 4, "defense": 1},
    "Dark Mage": {"hp": 20, "attack": 6, "defense": 2},
    "Fortress Guard": {"hp": 25, "attack": 5, "defense": 3},
    "Shadow Hound": {"hp": 18, "attack": 5, "defense": 2},
    "Animated Armor": {"hp": 30, "attack": 4, "defense": 4},
}


# --- SVG templates for illustrations ---
# Helper function for placeholder SVGs
def create_placeholder_svg(text="Placeholder", color="#cccccc"):
    # Basic SVG with a rectangle and centered text
    return f"""<svg viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
                 <rect width="200" height="100" fill="{color}" rx="5" ry="5"/>
                 <text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" font-family="monospace" font-size="14px" fill="#000">{text}</text>
                 Sorry, your browser does not support inline SVG.
               </svg>"""

illustrations = {
    "default": create_placeholder_svg("Default Scene", "#aaaaaa"),
    "game-over": create_placeholder_svg("Game Over", "#ffaaaa"),
    "city-gates": """
<svg viewBox="0 0 200 125" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
  <rect width="200" height="75" fill="#87CEEB" />
  <rect y="75" width="200" height="50" fill="#8B4513" />
  <rect x="25" y="25" width="150" height="50" fill="#A9A9A9" stroke="#696969" stroke-width="1"/>
  <rect x="25" y="15" width="20" height="60" fill="#A9A9A9" stroke="#696969" stroke-width="1"/>
  <rect x="155" y="15" width="20" height="60" fill="#A9A9A9" stroke="#696969" stroke-width="1"/>
  <path d="M 25 25 h 5 v -5 h 5 v 5 h 5 v -5 h 5 v 5 h 5 v -5 h 5 v 5 h 5 v -5 h 5 v 5 h 5 v -5 h 5 v 5 h 5 v -5 h 5 v 5 h 5 v -5 h 5 v 5 h 5 v -5 h 5 v 5 h 5 v -5 h 5 v 5 h 5 v -5 h 5 v 5 h 5 v -5 h 5 v 5 h 5 v -5 h 5 v 5 Z" fill="#A9A9A9" stroke="#696969" stroke-width="0.5"/>
  <path d="M 25 15 h 5 v -5 h 5 v 5 h 5 v -5 h 5 v 5 Z" fill="#A9A9A9" stroke="#696969" stroke-width="0.5"/>
  <path d="M 155 15 h 5 v -5 h 5 v 5 h 5 v -5 h 5 v 5 Z" fill="#A9A9A9" stroke="#696969" stroke-width="0.5"/>
  <rect x="75" y="40" width="50" height="60" fill="#654321" />
  <path d="M 75 40 Q 100 30 125 40 Z" fill="#654321"/>
  <rect x="98" y="40" width="4" height="60" fill="#402000" />
  <circle cx="90" cy="70" r="2" fill="#402000" />
  <circle cx="110" cy="70" r="2" fill="#402000" />
  <path d="M 75 125 Q 100 100 125 125 Z" fill="#A0522D" />
  <path d="M 75 100 L 125 100 L 100 75 Z" fill="#A0522D" opacity="0.5"/>
   <line x1="35" y1="0" x2="35" y2="15" stroke="black" stroke-width="1"/>
  <polygon points="35,0 45,5 35,10" fill="#DC143C"/>
  <line x1="165" y1="0" x2="165" y2="15" stroke="black" stroke-width="1"/>
  <polygon points="165,0 175,5 165,10" fill="#DC143C"/>
</svg>
    """,
    "weaponsmith": """
<svg viewBox="0 0 200 125" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
  <rect width="200" height="125" fill="#6B4226"/>
  <rect x="125" y="50" width="50" height="35" fill="#8B4513"/>
  <rect x="135" y="60" width="30" height="15" fill="#FF4500"/>
  <ellipse cx="150" cy="67.5" rx="12.5" ry="5" fill="#FFD700" opacity="0.8"/>
  <path d="M 90 80 L 110 80 L 115 75 L 105 70 L 95 70 L 85 75 Z" fill="#708090"/>
  <rect x="95" y="80" width="10" height="10" fill="#778899"/>
  <line x1="25" y1="30" x2="45" y2="35" stroke="#C0C0C0" stroke-width="2"/> <line x1="60" y1="25" x2="75" y2="35" stroke="#A0522D" stroke-width="1.5"/> <polygon points="75,35 78,33 80,35 78,37" fill="#C0C0C0"/> <path d="M 30 45 C 35 40, 45 40, 50 45 L 50 85 L 30 85 Z" fill="#808080"/> <circle cx="40" cy="65" r="3" fill="#696969"/> <line x1="70" y1="45" x2="70" y2="85" stroke="#A0522D" stroke-width="1.5"/>
   <path d="M 65 45 Q 70 40, 75 45 Q 80 55, 70 60 Z" fill="#C0C0C0"/>
  <rect x="15" y="90" width="60" height="5" fill="#8B4513"/>
  <rect x="20" y="95" width="5" height="10" fill="#8B4513"/>
  <rect x="65" y="95" width="5" height="10" fill="#8B4513"/>
  <circle cx="110" cy="60" r="12.5" fill="#DEB887"/> <rect x="100" y="72.5" width="20" height="17.5" fill="#A0522D"/> <rect x="95" y="70" width="30" height="10" fill="#D2691E"/> <circle cx="106.5" cy="57.5" r="1.5" fill="#000"/> <circle cx="113.5" cy="57.5" r="1.5" fill="#000"/> <path d="M 107.5 62.5 Q 110 65 112.5 62.5" fill="none" stroke="#000" stroke-width="0.5"/> <rect x="115" y="70" width="10" height="3" fill="#808090"/>
   <rect x="118" y="73" width="4" height="15" fill="#A0522D"/>
  <line x1="20" y1="90" x2="20" y2="75" stroke="#603813" stroke-width="2"/>
   <line x1="18" y1="75" x2="22" y2="75" stroke="#603813" stroke-width="1"/>
   <path d="M 20 75 L 19 70 L 20 65 L 21 70 Z" fill="#FF4500" opacity="0.7">
      <animate attributeName="opacity" values="0.7;1;0.7" dur="1s" repeatCount="indefinite" />
   </path>
   <path d="M 40 90 C 35 80, 35 70, 40 60" stroke="#556B2F" stroke-width="1.5" fill="none"/>
   <line x1="40" y1="90" x2="40" y2="60" stroke="#8FBC8F" stroke-width="0.5"/>
   <path d="M 55 90 C 58 85, 62 85, 65 90 L 65 80 L 55 80 Z" fill="#B0C4DE" stroke="#708090" stroke-width="1"/>
    <circle cx="60" cy="85" r="2" fill="#F0F8FF"/>
</svg>
    """,
    # --- Add other placeholders ---
    "temple": create_placeholder_svg("Ancient Temple", "#fffde7"),
    "resistance-meeting": create_placeholder_svg("Resistance Meeting", "#efebe9"),
    "shadowwood-forest": create_placeholder_svg("Shadowwood Forest", "#38502e"),
    "road-ambush": create_placeholder_svg("Road Ambush", "#795548"),
    "river-spirit": create_placeholder_svg("River Spirit", "#b3e5fc"),
    "ancient-ruins": create_placeholder_svg("Ancient Ruins", "#bdbdbd"),
    "forest-edge": create_placeholder_svg("Forest Edge", "#a5d6a7"),
    "prisoner-cell": create_placeholder_svg("Prisoner Cell", "#616161"),
    "spirit-blessing": create_placeholder_svg("Spirit Blessing", "#e1f5fe"),
    "river-danger": create_placeholder_svg("River Danger", "#42a5f5"),
    "ancient-spirits": create_placeholder_svg("Ancient Spirits", "#e0e0e0"),
    "lost-ruins": create_placeholder_svg("Lost in Ruins", "#9e9e9e"),
    "evil-fortress": create_placeholder_svg("Evil Fortress", "#212121"),
    "fortress-observation": create_placeholder_svg("Observing Fortress", "#424242"),
    "hidden-tunnel": create_placeholder_svg("Hidden Tunnel", "#757575"),
    "night-escape": create_placeholder_svg("Night Escape", "#37474f"),
    "guard-ally": create_placeholder_svg("Guard Ally", "#bcaaa4"),
    "prison-wagon": create_placeholder_svg("Prison Wagon", "#8d6e63"),
    "fortress-interior": create_placeholder_svg("Fortress Interior", "#616161"),
    "fortress-alarm": create_placeholder_svg("Fortress Alarm", "#f44336"),
    "secret-meeting": create_placeholder_svg("Secret Meeting", "#4e342e"),
    "underground-passage": create_placeholder_svg("Underground Passage", "#6d4c41"),
    "central-tower": create_placeholder_svg("Central Tower", "#3e2723"),
    "dungeon-corridors": create_placeholder_svg("Dungeon Corridors", "#5d4037"),
    "ritual-chamber": create_placeholder_svg("Ritual Chamber", "#4a148c"),
    "prisoner-escort": create_placeholder_svg("Prisoner Escort", "#757575"),
    "fortress-uprising": create_placeholder_svg("Fortress Uprising", "#ff7043"),
    "secret-passage": create_placeholder_svg("Secret Passage", "#a1887f"),
    "knowledge-sharing": create_placeholder_svg("Knowledge Sharing", "#fff176"),
    "final-confrontation": create_placeholder_svg("Final Confrontation", "#d32f2f"),
    "power-battle": create_placeholder_svg("Power Battle", "#ef5350"),
    "crystal-barrier": create_placeholder_svg("Crystal Barrier", "#ab47bc"),
    "hero-victory": create_placeholder_svg("Hero's Victory", "#ffee58"),
    "dark-victory": create_placeholder_svg("Darkness Prevails", "#263238"),
    "crystal-destroyed": create_placeholder_svg("Crystal Destroyed", "#81d4fa"),
    "heroic-sacrifice": create_placeholder_svg("Heroic Sacrifice", "#ffcc80"),

}
</gradio-file>

        </gradio-lite>
    </div>
</body>
</html>