File size: 61,589 Bytes
3c189ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
// Firebase Configuration
const firebaseConfig = {
    apiKey: "AIzaSyB5NffQxssjZKEAUyp8Wyt2Y3rJmJOVfx4",
    authDomain: "holdspot-977d3.firebaseapp.com",
    projectId: "holdspot-977d3",
    storageBucket: "holdspot-977d3.firebasestorage.app",
    messagingSenderId: "969993737434",
    appId: "1:969993737434:web:17f43caab0bf46966701e3"
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();

// Icon Colors
const iconColors = {
    'fa-tag': '#6d6875',
    'fa-shopping-cart': '#1e3a8a',
    'fa-right-left': '#5a189a',
    'fa-gift': '#d00000',
    'fa-file-contract': '#007200',
    'fa-plane': '#023e8a',
    'fa-bed': '#6d4c41',
    'fa-map-marker-alt': '#e63946',
    'fa-users': '#4a1e6b',
    'fa-road': '#386641',
    'fa-briefcase': '#003049',
    'fa-user-tie': '#283618',
    'fa-person-digging': '#2d6a4f',
    'fa-heart': '#9d0208',
    'fa-lightbulb': '#ff8500',
    'fa-palette': '#9d4edd',
    'fa-film': '#560bad',
    'fa-hiking': '#004b23',
    'fa-glass-cheers': '#ff6d00',
    'fa-star': '#ffb700',
    'fa-exclamation-triangle': '#dc2f02',
    'fa-eye': '#480ca8',
    'fa-clipboard-list': '#495057',
    'fa-compass': '#774936',
    'fa-bolt': '#ffd000'
};

// Subcategory Names
const subcategoryNames = {
    'fa-tag': 'Sell',
    'fa-shopping-cart': 'Buy',
    'fa-right-left': 'Exchange',
    'fa-gift': 'Gifts',
    'fa-file-contract': 'Lease',
    'fa-plane': 'Travel',
    'fa-bed': 'Accommodation',
    'fa-map-marker-alt': 'Places',
    'fa-users': 'Communities',
    'fa-road': 'Trips',
    'fa-briefcase': 'Jobs',
    'fa-user-tie': 'Professional',
    'fa-concierge-bell': 'Services',
    'fa-heart': 'Dating',
    'fa-lightbulb': 'Ideas',
    'fa-palette': 'Arts',
    'fa-film': 'Entertainment',
    'fa-hiking': 'Recreational',
    'fa-glass-cheers': 'Gatherings',
    'fa-star': 'Experiences',
    'fa-exclamation-triangle': 'Alerts',
    'fa-eye': 'Monitor',
    'fa-clipboard-list': 'Reports',
    'fa-compass': 'Orientation',
    'fa-bolt': 'Actions'
};

// Icon Categories
const iconCategories = {
    'Transactions': [
        'fa-tag',
        'fa-shopping-cart',
        'fa-right-left',
        'fa-gift',
        'fa-file-contract'
    ],
    'Movement': [
        'fa-plane',
        'fa-bed',
        'fa-map-marker-alt',
        'fa-users',
        'fa-road'
    ],
    'Connections': [
        'fa-briefcase',
        'fa-user-tie',
        'fa-concierge-bell',
        'fa-heart',
        'fa-lightbulb'
    ],
    'Leisure': [
        'fa-palette',
        'fa-film',
        'fa-hiking',
        'fa-glass-cheers',
        'fa-star'
    ],
    'Oversight': [
        'fa-exclamation-triangle',
        'fa-eye',
        'fa-clipboard-list',
        'fa-compass',
        'fa-bolt'
    ]
};

// Archetype Mapping
const archetype = {
    'Transactions': 'MERCHANT',
    'Movement': 'EXPLORER',
    'Connections': 'DIPLOMAT',
    'Leisure': 'ARTISAN',
    'Oversight': 'SENTINEL'
};

// Abilities Data
const abilitiesData = [
    { id: 'foundation', name: 'First Foundation', icon: 'fa-home', requirement: '2 markers created', effect: '+1 random token when create markers', prerequisite: [], row: 1, col: 1 },
    { id: 'territoryClaim', name: 'Territory Claim', icon: 'fa-flag', requirement: 'Subtract/add 10 Visibility points in battles', effect: 'Winning/losing in battles subtract/add +1 Visibility', prerequisite: ['foundation'], row: 2, col: 1 },
    { id: 'branchingPaths', name: 'Branching Paths', icon: 'fa-code-branch', requirement: 'Create 5 markers in the same category', effect: '+1 Visibility for new markers in Archetype', prerequisite: ['foundation'], row: 2, col: 2 },
    { id: 'hybridization', name: 'Wild Hybridization', icon: 'fa-dna', requirement: 'Gain 25 points in any metric', effect: '+1 Visibility for new markers', prerequisite: ['foundation'], row: 2, col: 3 },
    { id: 'headhunter', name: 'Lethal Headhunter', icon: 'fa-crosshairs', requirement: 'Win 25 battles', effect: 'Battle cooldown reduced by 10%', prerequisite: ['territoryClaim', 'branchingPaths'], row: 3, col: 1 },
    { id: 'mediator', name: 'Lost Mediator', icon: 'fa-balance-scale', requirement: 'Lost 25 battles', effect: 'Win chances in battle increased by 5%', prerequisite: ['territoryClaim', 'branchingPaths'], row: 3, col: 2 },
    { id: 'grandmaster', name: 'Gory Grandmaster', icon: 'fa-crown', requirement: 'Battle 10 Archetype markers', effect: 'Gain +1 random token when battle against Archetype markers', prerequisite: ['branchingPaths', 'hybridization'], row: 3, col: 3 },
    { id: 'manipulator', name: 'Tricky Manipulator', icon: 'fa-chess', requirement: 'Create 10 Archetype markers', effect: 'Gain +1 random token when create Archetype markers', prerequisite: ['branchingPaths', 'hybridization'], row: 3, col: 4 },
    { id: 'networker', name: 'Logical Networker', icon: 'fa-network-wired', requirement: 'Gain 25 in Link/Mail metric', effect: 'Gain +1 random token when click first time on Link or Mail markers', prerequisite: ['hybridization'], row: 3, col: 5 },
    { id: 'matchmaker', name: 'Lovely Matchmaker', icon: 'fa-heart', requirement: 'Gain 25 in Likes/Fav metric', effect: 'Gain +1 random token when first time click on Like or Fav markers', prerequisite: ['hybridization'], row: 3, col: 6 },
    { id: 'tactician', name: 'Brutal Tactician', icon: 'fa-brain', requirement: 'Lost 50 battles', effect: 'Gain +5 random tokens on each battle lost', prerequisite: ['headhunter', 'mediator'], row: 4, col: 1 },
    { id: 'battleExpert', name: 'Battle Expert', icon: 'fa-shield-alt', requirement: 'Win 50 battles', effect: 'Win chances in battle increased by 10%', prerequisite: ['headhunter', 'mediator'], row: 4, col: 2 },
    { id: 'pathfinder', name: 'Smart Pathfinder', icon: 'fa-compass', requirement: 'Subtract/add +10 Visibility points in battles', effect: 'Winning/losing in battles subtract/add +5 more Visibility', prerequisite: ['mediator', 'grandmaster'], row: 4, col: 3 },
    { id: 'overlord', name: 'Absolute Overlord', icon: 'fa-skull', requirement: 'Gain 50 in Share metric', effect: 'Gain +5 random tokens when first time click on Share for Archetype markers', prerequisite: ['grandmaster', 'manipulator'], row: 4, col: 4 },
    { id: 'emissary', name: 'Gloomy Emissary', icon: 'fa-dove', requirement: 'Lose against 50 non Archetype markers', effect: 'Gain +10 random tokens on each battle lost against non Archetype markers', prerequisite: ['manipulator', 'networker'], row: 4, col: 5 },
    { id: 'kingpin', name: 'Rude Kingpin', icon: 'fa-user-tie', requirement: 'Create 100 non Archetype markers', effect: 'Gain +10 random token when create markers that are not Archetype', prerequisite: ['networker', 'matchmaker'], row: 4, col: 6 },
    { id: 'hybridWarrior', name: 'Hybrid Warrior', icon: 'fa-fist-raised', requirement: 'Win against 50 non Archetype markers', effect: 'Gain +10 random tokens on each battle victory against non Archetype markers', prerequisite: ['matchmaker'], row: 4, col: 7 },
    { id: 'virtuoso', name: 'Imperious Virtuoso', icon: 'fa-star', requirement: 'Own over 500 points in Visibility', effect: 'Markers gain +1 Visibility when first time click on Like/Fav/Link/Mail or Share', prerequisite: ['matchmaker'], row: 4, col: 8 },
    { id: 'arbitrator', name: 'Arcane Arbitrator', icon: 'fa-gavel', requirement: 'Spend 250 tokens on shop items', effect: 'Shop items that influence battles have 25% increased effect duration', prerequisite: ['tactician', 'battleExpert'], row: 5, col: 1 },
    { id: 'legend', name: 'Undying Legend', icon: 'fa-trophy', requirement: 'Win 100 battles', effect: 'Gain +10 random tokens on each battle victory', prerequisite: ['battleExpert', 'pathfinder'], row: 5, col: 2 },
    { id: 'quartermaster', name: 'Tyrannical Quartermaster', icon: 'fa-box', requirement: 'Lose 100 battles', effect: 'Gain +10 random tokens on each battle lost', prerequisite: ['pathfinder', 'overlord'], row: 5, col: 3 },
    { id: 'commander', name: 'Sovereign Commander', icon: 'fa-chess-king', requirement: 'Win/Lose battle ratio is above 50', effect: 'Win chances in battles increased by 25%', prerequisite: ['overlord', 'emissary'], row: 5, col: 4 },
    { id: 'titan', name: 'Primordial Titan', icon: 'fa-mountain', requirement: 'Gain 250 points in Interaction metric', effect: 'Gain +10 random token and add +5 Visibility when first time click on markers', prerequisite: ['emissary', 'kingpin'], row: 5, col: 5 },
    { id: 'dominator', name: 'Omniscient Dominator', icon: 'fa-crown', requirement: 'Subtract/add 100 Visibility points in battles', effect: 'Winning/losing in battles subtract/add +10 more Visibility', prerequisite: ['kingpin', 'hybridWarrior'], row: 5, col: 6 },
    { id: 'visionary', name: 'Remote Visionary', icon: 'fa-eye', requirement: 'Gain 500 points in any metric', effect: 'Gain +10 random token when first time click on Like/Fav/Link/Mail or Share', prerequisite: ['hybridWarrior', 'virtuoso'], row: 5, col: 7 },
    { id: 'expert', name: 'Trustfull Expert', icon: 'fa-award', requirement: 'Create 50 Archetype markers', effect: '+10 random token when create Archetype markers', prerequisite: ['virtuoso'], row: 5, col: 8 },
    { id: 'pioneer', name: 'Spiritual Pioneer', icon: 'fa-flag', requirement: 'Create 100 markers', effect: '+10 random token when create markers', prerequisite: ['virtuoso'], row: 5, col: 9 },
    { id: 'mastermind', name: 'Intimate Mastermind', icon: 'fa-brain', requirement: 'Buy 50 items in shop', effect: 'Shop items are now 25% cheaper', prerequisite: [], row: 5, col: 10 }
];

// Shop Items
const shopItems = {
    potions: [
        { id: 'potion1', name: 'Victory Elixir', value: 25, price: 250, time: 9000, type: 'liquidity', effect: '+25% win chance', description: 'Increases battle win chances temporarily' },
        { id: 'potion2', name: 'Archetype Brew', value: 25, price: 250, time: 9000, type: 'value', effect: '+25% win chance against Archetype markers', description: 'Increases chance to battle Archetype markers' },
        { id: 'potion3', name: 'Battle Serum', value: 5, price: 250, time: 9000, type: 'engagement', effect: 'x5 Visibility on battles', description: 'Improve battle gains' },
        { id: 'potion4', name: 'Holy Water', value: 50, price: 1500, time: 18000, type: 'value', effect: 'Each win in battles give additional 50 random tokens', description: 'Gain tokens in battles' }
    ],
    utilities: [
        { id: 'util1', name: 'Visibility Boost', price: 500, type: 'liquidity', effect: '+100 Visibility to owned marker', description: 'Increase marker visibility' },
        { id: 'util2', name: 'Marker Relocator', price: 500, type: 'value', effect: 'Move owned marker to new location', description: 'Relocate your markers' },
        { id: 'util3', name: 'Marker Eraser', price: 500, type: 'engagement', effect: 'Delete owned marker', description: 'Remove your markers' },
        { id: 'util4', name: 'Visibility Crusher', price: 1000, type: 'value', effect: '-100 Visibility to any marker', description: 'Decrease any marker visibility' },
        { id: 'util5', name: 'Exchange Master', price: 1000, type: 'value', effect: 'Reset exchange cooldown', description: 'Reset exchange cooldown' }
    ],
    boosters: [
        { id: 'boost1', name: 'Token Amplifier', value: 3, price: 1000, time: 6000, type: 'value', effect: 'Token gain tripled', description: 'Boost token gains' },
        { id: 'boost2', name: 'Abilities Surge', value: 3, price: 1000, time: 6000, type: 'engagement', effect: 'Abilities effects tripled', description: 'Boost abilities gains' },
        { id: 'boost3', name: 'Experience Multiplier', value: 2, price: 1000, time: 6000, type: 'liquidity', effect: 'Metric gain ', description: 'Double metric progression' },
        { id: 'boost4', name: 'Total Conquerer', value: 20, price: 2000, time: 6000, type: 'value', effect: 'Visibility from all sources increased', description: 'Visibility killer' }
    ]
};

// Global Variables
let map;
let markers = {};
let currentUser = null;
let userData = {
    markersCreated: 0,
    archetypeMarkersCreated: 0,
    likesGiven: 0,
    favoritesMade: 0,
    interactions: 0,
    shareClicked: 0,
    linksClicked: 0,
    emailsClicked: 0,
    archetype: '',
    liquidityTokensGained: 0,
    liquidityTokensSpent: 0,
    valueTokensGained: 0,
    valueTokensSpent: 0,
    engagementTokensGained: 0,
    engagementTokensSpent: 0,
    battlesWon: 0,
    battlesLost: 0,
    battleVisibilityChanged: 0,
    battleWinRatio: 0,
    engagement: 0,
    lastInteraction: Date.now(),
    abilities: [],
    activeItems: [],
    lastExchange: 0,
    lastBattle: 0
};
let selectedMarker = null;
let addingMarker = false;
let filteredMarkers = [];
let utilityMode = null;
let utilityTarget = null;

// Initialize App
document.addEventListener('DOMContentLoaded', async () => {
    initMap();
    await initUser();
    setupEventListeners();
    loadUserData();
    subscribeToMarkers();
    
    // Initial token display
    updateTokenDisplay();
});

// Initialize Map
function initMap() {
    map = L.map('map', {
        zoomControl: false,
        attributionControl: false,
        cursor: 'default',
        doubleClickZoom: false,
        minZoom: 2,
        maxZoom: 10,
        maxBounds: [[-60, -120], [60, 120]],
        maxBoundsViscosity: 1,
        center: [20, 0],
        zoom: 2
    });

    L.tileLayer('https://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}{r}.png', {
        attribution: '© OpenStreetMap, © CartoDB',
        subdomains: 'abcd',
        minZoom: 2,
        maxZoom: 10
    }).addTo(map);

    map.on('click', handleMapClick);
}

// Initialize User
async function initUser() {
    // Check if user exists in localStorage
    let userId = localStorage.getItem('userId');
    if (!userId) {
        // Generate new UUID
        userId = generateUUID();
        localStorage.setItem('userId', userId);
    }
    
    currentUser = {
        id: userId,
        liquidityTokens: 0,
        valueTokens: 0,
        engagementTokens: 0
    };
    
    // Check if user exists in Firestore
    const userDoc = await db.collection('users').doc(userId).get();
    if (!userDoc.exists) {
        // Create new user document
        await db.collection('users').doc(userId).set({
            userID: userId,
            liquidityTokens: 0,
            valueTokens: 0,
            engagementTokens: 0
        });
    } else {
        // Load user data
        const data = userDoc.data();
        currentUser.liquidityTokens = data.liquidityTokens || 0;
        currentUser.valueTokens = data.valueTokens || 0;
        currentUser.engagementTokens = data.engagementTokens || 0;
    }
}

// Generate UUID
function generateUUID() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        const r = Math.random() * 16 | 0;
        const v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

// Setup Event Listeners
function setupEventListeners() {
    // Theme Toggle
    document.getElementById('theme-toggle').addEventListener('click', toggleTheme);
    
    // Search Bar
    document.getElementById('search-bar').addEventListener('input', handleSearch);
    
    // Action Buttons
    document.getElementById('dashboard-btn').addEventListener('click', () => openModal('dashboard'));
    document.getElementById('battle-btn').addEventListener('click', () => openModal('battle'));
    document.getElementById('shop-btn').addEventListener('click', () => openModal('shop'));
    document.getElementById('add-marker-btn').addEventListener('click', startAddMarker);
    
    // Modal Overlay
    document.getElementById('modal-overlay').addEventListener('click', closeModal);
    
    // Add Marker Form
    document.getElementById('add-marker-form').addEventListener('submit', handleAddMarker);
    
    // Dashboard Tabs
    document.querySelectorAll('.tab-button').forEach(button => {
        button.addEventListener('click', () => switchTab('dashboard', button.dataset.tab));
    });
    
    // Analytics Tabs
    document.querySelectorAll('.analytics-tab-button').forEach(button => {
        button.addEventListener('click', () => switchAnalyticsTab(button.dataset.analyticsTab));
    });
    
    // Shop Tabs
    document.querySelectorAll('.shop-tab-button').forEach(button => {
        button.addEventListener('click', () => switchTab('shop', button.dataset.shopTab));
    });
    
    // Battle Button
    document.getElementById('start-battle-btn').addEventListener('click', startBattle);
    
    // Category Selection for Add Marker
    document.getElementById('marker-category').addEventListener('change', populateSubcategories);
    
    // Populate initial subcategories
    populateSubcategories();
}

// Toggle Theme
function toggleTheme() {
    document.documentElement.classList.toggle('dark');
    localStorage.setItem('theme', document.documentElement.classList.contains('dark') ? 'dark' : 'light');
}

// Handle Search
function handleSearch(e) {
    const query = e.target.value.toLowerCase();
    filteredMarkers = Object.values(markers).filter(marker => 
        marker.title.toLowerCase().includes(query) || 
        marker.body.toLowerCase().includes(query) ||
        marker.subcategory.toLowerCase().includes(query)
    );
    
    // Update map markers
    updateMapMarkers();
}

// Open Modal
function openModal(modalType) {
    document.body.classList.add('modal-active');
    document.getElementById('modal-overlay').classList.remove('hidden');
    document.getElementById(`${modalType}-modal`).classList.remove('hidden');
    
    // Load modal content
    switch(modalType) {
        case 'dashboard':
            loadDashboardContent();
            break;
        case 'shop':
            loadShopContent();
            break;
        case 'battle':
            loadBattleContent();
            break;
    }
}

// Close Modal
function closeModal() {
    document.body.classList.remove('modal-active');
    document.getElementById('modal-overlay').classList.add('hidden');
    document.querySelectorAll('[id$="-modal"]').forEach(modal => {
        modal.classList.add('hidden');
    });
    
    // Reset utility mode if active
    if (utilityMode) {
        utilityMode = null;
        utilityTarget = null;
        showNotification('Utility mode cancelled', 'info');
        updateMapMarkers();
    }
}

// Switch Tab
function switchTab(modalType, tabName) {
    // Hide all tabs
    document.querySelectorAll(`#${modalType}-modal .tab-content`).forEach(tab => {
        tab.classList.add('hidden');
    });
    
    // Remove active class from all buttons
    document.querySelectorAll(`#${modalType}-modal .tab-button`).forEach(button => {
        button.classList.remove('active');
    });
    
    // Show selected tab
    document.getElementById(`${tabName}-tab`).classList.remove('hidden');
    
    // Add active class to selected button
    document.querySelector(`#${modalType}-modal .tab-button[data-tab="${tabName}"]`).classList.add('active');
}

// Switch Analytics Tab
function switchAnalyticsTab(tabName) {
    // Hide all charts
    document.querySelectorAll('.analytics-chart-container').forEach(container => {
        container.classList.add('hidden');
    });
    
    // Remove active class from all buttons
    document.querySelectorAll('.analytics-tab-button').forEach(button => {
        button.classList.remove('active');
    });
    
    // Show selected chart
    document.getElementById(`${tabName}-chart-container`).classList.remove('hidden');
    
    // Add active class to selected button
    document.querySelector(`.analytics-tab-button[data-analytics-tab="${tabName}"]`).classList.add('active');
    
    // Load chart data
    loadChartData(tabName);
}

// Handle Map Click
function handleMapClick(e) {
    if (addingMarker) {
        // Place marker
        openModal('add-marker');
        addingMarker = false;
        document.getElementById('map').style.cursor = 'default';
    }
}

// Start Add Marker Process
function startAddMarker() {
    addingMarker = true;
    document.getElementById('map').style.cursor = 'crosshair';
    showNotification('Click on the map to place your marker', 'info');
}

// Populate Subcategories
function populateSubcategories() {
    const category = document.getElementById('marker-category').value;
    const subcategorySelect = document.getElementById('marker-subcategory');
    
    // Clear existing options
    subcategorySelect.innerHTML = '';
    
    if (category && iconCategories[category]) {
        iconCategories[category].forEach(icon => {
            const option = document.createElement('option');
            option.value = icon;
            option.textContent = subcategoryNames[icon];
            subcategorySelect.appendChild(option);
        });
    }
}

// Handle Add Marker Form Submission
async function handleAddMarker(e) {
    e.preventDefault();
    
    // Get form values
    const title = document.getElementById('marker-title-input').value;
    const body = document.getElementById('marker-body-input').value;
    const category = document.getElementById('marker-category').value;
    const subcategory = document.getElementById('marker-subcategory').value;
    const email = document.getElementById('marker-email').value;
    const link = document.getElementById('marker-link').value;
    
    // Validate inputs
    if (!title || !body || !category || !subcategory) {
        showNotification('Please fill in all required fields', 'error');
        return;
    }
    
    // Get map center for marker position
    const center = map.getCenter();
    
    // Calculate initial visibility based on abilities
    let visibility = 100;
    if (userData.abilities.includes('branchingPaths') && userData.archetype === archetype[category]) {
        visibility += 1;
    }
    if (userData.abilities.includes('hybridization')) {
        visibility += 1;
    }
    
    // Create marker object
    const markerData = {
        title,
        body,
        category,
        subcategory,
        email: email || null,
        link: link || null,
        lat: center.lat,
        lng: center.lng,
        likes: 0,
        favorites: 0,
        shares: 0,
        userID: currentUser.id,
        zDepth: visibility // Using zDepth as visibility
    };
    
    try {
        // Save to Firestore
        const docRef = await db.collection('markers').add(markerData);
        
        // Update user metrics
        userData.markersCreated++;
        if (userData.archetype === archetype[category]) {
            userData.archetypeMarkersCreated++;
        }
        
        // Award tokens based on abilities
        let tokensAwarded = false;
        if (userData.abilities.includes('foundation')) {
            awardRandomToken();
            tokensAwarded = true;
        }
        if (userData.abilities.includes('manipulator') && userData.archetype === archetype[category]) {
            awardRandomToken();
            tokensAwarded = true;
        }
        if (userData.abilities.includes('pioneer')) {
            awardRandomToken();
            tokensAwarded = true;
        }
        
        // Save user data
        saveUserData();
        
        // Update token display
        updateTokenDisplay();
        
        // Show success notification
        showNotification(
            `Marker created successfully! ${tokensAwarded ? 'Tokens awarded.' : ''}`, 
            'success'
        );
        
        // Close modal and reset form
        closeModal();
        document.getElementById('add-marker-form').reset();
        
    } catch (error) {
        console.error('Error creating marker:', error);
        showNotification('Failed to create marker. Please try again.', 'error');
    }
}

// Award Random Token
function awardRandomToken() {
    const tokenTypes = ['liquidity', 'value', 'engagement'];
    const tokenType = tokenTypes[Math.floor(Math.random() * tokenTypes.length)];
    
    switch(tokenType) {
        case 'liquidity':
            currentUser.liquidityTokens++;
            userData.liquidityTokensGained++;
            break;
        case 'value':
            currentUser.valueTokens++;
            userData.valueTokensGained++;
            break;
        case 'engagement':
            currentUser.engagementTokens++;
            userData.engagementTokensGained++;
            break;
    }
    
    // Update Firestore
    db.collection('users').doc(currentUser.id).update({
        [`userData.${tokenType}Tokens`]: currentUser[`${tokenType}Tokens`]
    });
}

// Load Dashboard Content
function loadDashboardContent() {
    // Metrics Tab
    const metricsTab = document.getElementById('metrics-tab');
    metricsTab.innerHTML = `
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
            <div class="bg-gray-50 dark:bg-gray-700 p-4 rounded-lg">
                <h3 class="font-bold mb-2">Basic Metrics</h3>
                <p>Markers Created: ${userData.markersCreated}</p>
                <p>Archetype Markers: ${userData.archetypeMarkersCreated}</p>
                <p>Likes Given: ${userData.likesGiven}</p>
                <p>Favorites Made: ${userData.favoritesMade}</p>
                <p>Interactions: ${userData.interactions}</p>
            </div>
            <div class="bg-gray-50 dark:bg-gray-700 p-4 rounded-lg">
                <h3 class="font-bold mb-2">Engagement</h3>
                <p>Share Clicked: ${userData.shareClicked}</p>
                <p>Links Clicked: ${userData.linksClicked}</p>
                <p>Emails Clicked: ${userData.emailsClicked}</p>
                <p>Archetype: ${userData.archetype || 'None'}</p>
                <p>Engagement: ${userData.engagement}</p>
            </div>
            <div class="bg-gray-50 dark:bg-gray-700 p-4 rounded-lg">
                <h3 class="font-bold mb-2">Tokens</h3>
                <p>Liquidity: ${userData.liquidityTokensGained}/${userData.liquidityTokensSpent}</p>
                <p>Value: ${userData.valueTokensGained}/${userData.valueTokensSpent}</p>
                <p>Engagement: ${userData.engagementTokensGained}/${userData.engagementTokensSpent}</p>
            </div>
            <div class="bg-gray-50 dark:bg-gray-700 p-4 rounded-lg">
                <h3 class="font-bold mb-2">Battles</h3>
                <p>Won: ${userData.battlesWon}</p>
                <p>Lost: ${userData.battlesLost}</p>
                <p>Visibility Changed: ${userData.battleVisibilityChanged}</p>
                <p>Win Ratio: ${userData.battleWinRatio.toFixed(2)}%</p>
            </div>
        </div>
    `;
    
    // Analytics Tab - will be loaded when tab is switched
    // Abilities Tab
    loadAbilitiesContent();
    // Exchange Tab
    loadExchangeContent();
}

// Load Abilities Content
function loadAbilitiesContent() {
    const abilitiesTab = document.getElementById('abilities-tab');
    
    // Count active abilities
    const activeCount = userData.abilities.length;
    const inactiveCount = abilitiesData.length - activeCount;
    
    // Create grid
    let gridHTML = `
        <div class="mb-4">
            <p>${activeCount} abilities active, ${inactiveCount} inactive</p>
        </div>
        <div class="abilities-grid">
    `;
    
    // Sort abilities by row and column
    const sortedAbilities = [...abilitiesData].sort((a, b) => {
        if (a.row !== b.row) return a.row - b.row;
        return a.col - b.col;
    });
    
    // Add abilities to grid
    sortedAbilities.forEach(ability => {
        const isActive = userData.abilities.includes(ability.id);
        const isAvailable = checkAbilityPrerequisites(ability.prerequisite);
        
        gridHTML += `
            <div class="ability-item ${isActive ? 'active' : isAvailable ? '' : 'inactive'}" 
                 title="${ability.name}: ${ability.effect}">
                <i class="fas ${ability.icon} ability-icon"></i>
                <span class="text-xs">${ability.name}</span>
            </div>
        `;
    });
    
    gridHTML += '</div>';
    abilitiesTab.innerHTML = gridHTML;
}

// Check Ability Prerequisites
function checkAbilityPrerequisites(prerequisites) {
    return prerequisites.every(req => userData.abilities.includes(req));
}

// Load Exchange Content
function loadExchangeContent() {
    const exchangeTab = document.getElementById('exchange-tab');
    
    // Calculate exchange rate (1:100/total markers)
    const totalMarkers = Object.keys(markers).length;
    const exchangeRate = totalMarkers > 0 ? (100 / totalMarkers).toFixed(2) : 0;
    
    // Check cooldown
    const now = Date.now();
    const cooldownRemaining = Math.max(0, 86400000 - (now - userData.lastExchange));
    const canExchange = cooldownRemaining <= 0;
    
    exchangeTab.innerHTML = `
        <div class="mb-4">
            <p>Current Exchange Rate: 1 token = ${exchangeRate} points</p>
            <p>Total Markers on Map: ${totalMarkers}</p>
            <p>Next exchange available in: ${formatTime(cooldownRemaining)}</p>
        </div>
        <div class="flex items-center space-x-4">
            <select id="from-token" class="flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700">
                <option value="liquidity">Liquidity Tokens</option>
                <option value="value">Value Tokens</option>
                <option value="engagement">Engagement Tokens</option>
            </select>
            <i class="fas fa-exchange-alt"></i>
            <select id="to-token" class="flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700">
                <option value="liquidity">Liquidity Tokens</option>
                <option value="value">Value Tokens</option>
                <option value="engagement">Engagement Tokens</option>
            </select>
            <input type="number" id="exchange-amount" min="1" placeholder="Amount" 
                   class="w-24 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700">
            <button id="exchange-btn" class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition ${canExchange ? '' : 'opacity-50 cursor-not-allowed'}"
                    ${canExchange ? '' : 'disabled'}>
                Exchange
            </button>
        </div>
    `;
    
    // Add event listener to exchange button
    document.getElementById('exchange-btn').addEventListener('click', handleExchange);
}

// Format Time
function formatTime(ms) {
    const seconds = Math.floor(ms / 1000);
    const minutes = Math.floor(seconds / 60);
    const hours = Math.floor(minutes / 60);
    
    if (hours > 0) {
        return `${hours}h ${minutes % 60}m`;
    }
    if (minutes > 0) {
        return `${minutes}m ${seconds % 60}s`;
    }
    return `${seconds}s`;
}

// Handle Exchange
function handleExchange() {
    const fromToken = document.getElementById('from-token').value;
    const toToken = document.getElementById('to-token').value;
    const amount = parseInt(document.getElementById('exchange-amount').value);
    
    // Validate inputs
    if (isNaN(amount) || amount <= 0) {
        showNotification('Please enter a valid amount', 'error');
        return;
    }
    
    // Check if user has enough tokens
    if (currentUser[`${fromToken}Tokens`] < amount) {
        showNotification('Insufficient tokens', 'error');
        return;
    }
    
    // Calculate exchange rate
    const totalMarkers = Object.keys(markers).length;
    const exchangeRate = totalMarkers > 0 ? (100 / totalMarkers) : 0;
    const receivedAmount = Math.floor(amount * exchangeRate);
    
    // Perform exchange
    currentUser[`${fromToken}Tokens`] -= amount;
    currentUser[`${toToken}Tokens`] += receivedAmount;
    
    // Update user data
    userData[`${fromToken}TokensSpent`] += amount;
    userData[`${toToken}TokensGained`] += receivedAmount;
    userData.lastExchange = Date.now();
    
    // Update Firestore
    db.collection('users').doc(currentUser.id).update({
        [`${fromToken}Tokens`]: currentUser[`${fromToken}Tokens`],
        [`${toToken}Tokens`]: currentUser[`${toToken}Tokens`]
    });
    
    // Save user data
    saveUserData();
    
    // Update token display
    updateTokenDisplay();
    
    // Show success notification
    showNotification(`Exchanged ${amount} ${fromToken} tokens for ${receivedAmount} ${toToken} tokens`, 'success');
    
    // Reload exchange content
    loadExchangeContent();
}

// Load Shop Content
function loadShopContent() {
    // Potions Tab
    loadShopTab('potions');
    // Utilities Tab
    loadShopTab('utilities');
    // Boosters Tab
    loadShopTab('boosters');
}

// Load Shop Tab
function loadShopTab(tabName) {
    const tabElement = document.getElementById(`${tabName}-tab`);
    const items = shopItems[tabName];
    
    let html = '';
    items.forEach(item => {
        html += `
            <div class="shop-item">
                <div class="shop-item-details">
                    <h3>${item.name}</h3>
                    <p class="shop-item-meta">${item.description}</p>
                    <p class="shop-item-meta">Effect: ${item.effect}</p>
                    ${item.value ? `<p class="shop-item-meta">Value: ${item.value}</p>` : ''}
                    ${item.time ? `<p class="shop-item-meta">Duration: ${Math.floor(item.time/1000)}s</p>` : ''}
                </div>
                <div class="shop-item-price">
                    <span class="font-bold">${item.price} ${item.type.charAt(0).toUpperCase() + item.type.slice(1)} Tokens</span>
                    <button class="mt-2 px-3 py-1 bg-blue-600 hover:bg-blue-700 text-white rounded text-sm transition buy-btn" 
                            data-item-id="${item.id}" data-item-type="${tabName}">
                        Buy
                    </button>
                </div>
            </div>
        `;
    });
    
    tabElement.innerHTML = html;
    
    // Add event listeners to buy buttons
    document.querySelectorAll('.buy-btn').forEach(button => {
        button.addEventListener('click', (e) => {
            const itemId = e.target.dataset.itemId;
            const itemType = e.target.dataset.itemType;
            buyItem(itemId, itemType);
        });
    });
}

// Buy Item
function buyItem(itemId, itemType) {
    const item = [...shopItems.potions, ...shopItems.utilities, ...shopItems.boosters]
        .find(i => i.id === itemId);
    
    // Check if user has enough tokens
    if (currentUser[`${item.type}Tokens`] < item.price) {
        showNotification('Insufficient tokens', 'error');
        return;
    }
    
    // Deduct tokens
    currentUser[`${item.type}Tokens`] -= item.price;
    userData[`${item.type}TokensSpent`] += item.price;
    
    // Update Firestore
    db.collection('users').doc(currentUser.id).update({
        [`${item.type}Tokens`]: currentUser[`${item.type}Tokens`]
    });
    
    // Save user data
    saveUserData();
    
    // Update token display
    updateTokenDisplay();
    
    // Handle item-specific actions
    switch(itemType) {
        case 'potions':
        case 'boosters':
            // Add to active items with expiration
            userData.activeItems.push({
                id: itemId,
                type: itemType,
                expiresAt: Date.now() + item.time
            });
            showNotification(`${item.name} activated!`, 'success');
            break;
        case 'utilities':
            // Enter utility mode
            utilityMode = itemId;
            utilityTarget = null;
            closeModal();
            showNotification(`Select a marker to apply ${item.name}`, 'info');
            updateMapMarkers();
            break;
    }
}

// Load Battle Content
function loadBattleContent() {
    const battleInfo = document.getElementById('battle-info');
    
    // Calculate battle stats
    const totalMarkers = Object.keys(markers).length;
    const archetypeMarkers = Object.values(markers).filter(m => 
        archetype[m.category] === userData.archetype
    ).length;
    
    // Calculate battle ratio
    const battleRatio = totalMarkers > 0 ? 
        ((userData.battlesWon + userData.battlesLost) / totalMarkers * 100).toFixed(1) : 0;
    
    // Calculate win chance (base 50% with modifiers)
    let winChance = 50;
    
    // Apply potion modifiers
    const victoryElixir = userData.activeItems.find(item => item.id === 'potion1');
    if (victoryElixir) {
        winChance += 25;
    }
    
    // Apply ability modifiers
    if (userData.abilities.includes('mediator')) {
        winChance += 5;
    }
    if (userData.abilities.includes('battleExpert')) {
        winChance += 10;
    }
    if (userData.abilities.includes('commander')) {
        winChance += 25;
    }
    
    // Clamp win chance between 5% and 95%
    winChance = Math.max(5, Math.min(95, winChance));
    
    // Check cooldown
    const now = Date.now();
    const cooldownRemaining = Math.max(0, 300000 - (now - userData.lastBattle));
    const canBattle = cooldownRemaining <= 0;
    
    battleInfo.innerHTML = `
        <div class="mb-4">
            <p>Total Markers: ${totalMarkers}</p>
            <p>Your Archetype Markers: ${archetypeMarkers}</p>
            <p>Battle Ratio: ${battleRatio}%</p>
            <p>Current Win Chance: ${winChance}%</p>
        </div>
        <div class="text-center">
            <button id="start-battle-btn-inner" class="px-6 py-3 bg-red-600 hover:bg-red-700 text-white rounded-lg transition ${canBattle ? '' : 'opacity-50 cursor-not-allowed'}"
                    ${canBattle ? '' : 'disabled'}>
                Start Battle
            </button>
            <p class="mt-2">Cooldown: <span id="battle-cooldown-inner">${formatTime(cooldownRemaining)}</span></p>
        </div>
    `;
    
    // Update cooldown timer
    if (!canBattle) {
        const cooldownInterval = setInterval(() => {
            const remaining = Math.max(0, 300000 - (Date.now() - userData.lastBattle));
            document.getElementById('battle-cooldown-inner').textContent = formatTime(remaining);
            
            if (remaining <= 0) {
                clearInterval(cooldownInterval);
                document.getElementById('start-battle-btn-inner').disabled = false;
                document.getElementById('start-battle-btn-inner').classList.remove('opacity-50', 'cursor-not-allowed');
            }
        }, 1000);
    }
}

// Start Battle
async function startBattle() {
    // Check cooldown
    const now = Date.now();
    if (now - userData.lastBattle < 300000) {
        showNotification('Battle cooldown not ready yet', 'error');
        return;
    }
    
    // Get all markers
    const markerList = Object.values(markers);
    if (markerList.length === 0) {
        showNotification('No markers available to battle', 'error');
        return;
    }
    
    // Select random marker
    const targetMarker = markerList[Math.floor(Math.random() * markerList.length)];
    
    // Determine win/loss (random with modifiers)
    let winChance = 50;
    
    // Apply potion modifiers
    const victoryElixir = userData.activeItems.find(item => item.id === 'potion1');
    if (victoryElixir) {
        winChance += 25;
    }
    
    // Apply ability modifiers
    if (userData.abilities.includes('mediator')) {
        winChance += 5;
    }
    if (userData.abilities.includes('battleExpert')) {
        winChance += 10;
    }
    if (userData.abilities.includes('commander')) {
        winChance += 25;
    }
    
    // Clamp win chance
    winChance = Math.max(5, Math.min(95, winChance));
    
    // Determine result
    const isWin = Math.random() * 100 < winChance;
    
    // Update battle stats
    if (isWin) {
        userData.battlesWon++;
    } else {
        userData.battlesLost++;
    }
    
    // Update visibility
    let visibilityChange = 0;
    if (isWin) {
        visibilityChange = -10;
        if (userData.abilities.includes('territoryClaim')) {
            visibilityChange -= 1;
        }
        if (userData.abilities.includes('pathfinder')) {
            visibilityChange -= 5;
        }
        if (userData.abilities.includes('dominator')) {
            visibilityChange -= 10;
        }
    } else {
        visibilityChange = 10;
        if (userData.abilities.includes('territoryClaim')) {
            visibilityChange += 1;
        }
        if (userData.abilities.includes('pathfinder')) {
            visibilityChange += 5;
        }
        if (userData.abilities.includes('dominator')) {
            visibilityChange += 10;
        }
    }
    
    userData.battleVisibilityChanged += Math.abs(visibilityChange);
    
    // Update marker visibility
    try {
        await db.collection('markers').doc(targetMarker.id).update({
            zDepth: Math.max(0, targetMarker.zDepth + visibilityChange)
        });
    } catch (error) {
        console.error('Error updating marker visibility:', error);
    }
    
    // Award tokens for wins/losses
    if (isWin) {
        // Check for Holy Water effect
        const holyWater = userData.activeItems.find(item => item.id === 'potion4');
        if (holyWater) {
            for (let i = 0; i < 50; i++) {
                awardRandomToken();
            }
        }
        
        // Check for abilities that give tokens on win
        if (userData.abilities.includes('legend')) {
            for (let i = 0; i < 10; i++) {
                awardRandomToken();
            }
        }
        
        // Check for Hybrid Warrior ability
        if (userData.abilities.includes('hybridWarrior') && 
            archetype[targetMarker.category] !== userData.archetype) {
            for (let i = 0; i < 10; i++) {
                awardRandomToken();
            }
        }
    } else {
        // Check for abilities that give tokens on loss
        if (userData.abilities.includes('tactician')) {
            for (let i = 0; i < 5; i++) {
                awardRandomToken();
            }
        }
        
        if (userData.abilities.includes('quartermaster')) {
            for (let i = 0; i < 10; i++) {
                awardRandomToken();
            }
        }
        
        // Check for Emissary ability
        if (userData.abilities.includes('emissary') && 
            archetype[targetMarker.category] !== userData.archetype) {
            for (let i = 0; i < 10; i++) {
                awardRandomToken();
            }
        }
    }
    
    // Update last battle time
    userData.lastBattle = now;
    
    // Save user data
    saveUserData();
    
    // Update token display
    updateTokenDisplay();
    
    // Show battle result
    const battleInfo = document.getElementById('battle-info');
    battleInfo.innerHTML = `
        <div class="text-center mb-4">
            <i class="fas fa-${isWin ? 'trophy text-yellow-500' : 'skull text-red-500'} text-4xl mb-2"></i>
            <h3 class="text-xl font-bold">${isWin ? 'Victory!' : 'Defeat!'}</h3>
            <p>You battled against:</p>
            <p class="font-bold">${targetMarker.title}</p>
            <p>Visibility changed by: ${visibilityChange > 0 ? '+' : ''}${visibilityChange}</p>
        </div>
        <button id="close-battle-btn" class="w-full py-2 bg-gray-600 hover:bg-gray-700 text-white rounded-lg transition">
            Close
        </button>
    `;
    
    // Add event listener to close button
    document.getElementById('close-battle-btn').addEventListener('click', closeModal);
    
    // Reload battle content after delay
    setTimeout(() => {
        if (document.getElementById('battle-modal').classList.contains('hidden') === false) {
            loadBattleContent();
        }
    }, 5000);
}

// Load Chart Data
function loadChartData(chartType) {
    // This would typically load data from localStorage or Firestore
    // For now, we'll create sample data
    
    switch(chartType) {
        case 'journey':
            loadJourneyChart();
            break;
        case 'performance':
            loadPerformanceChart();
            break;
        case 'battlegrounds':
            loadBattlegroundsChart();
            break;
    }
}

// Load Journey Chart
function loadJourneyChart() {
    const ctx = document.getElementById('journey-chart').getContext('2d');
    
    // Sample data - in a real app this would come from user history
    const labels = Array.from({length: 14}, (_, i) => `Day ${i+1}`);
    const archetypeData = Array.from({length: 14}, () => 
        Object.keys(archetype)[Math.floor(Math.random() * Object.keys(archetype).length)]
    );
    const battlesWon = Array.from({length: 14}, () => Math.floor(Math.random() * 10));
    const battlesLost = Array.from({length: 14}, () => Math.floor(Math.random() * 10));
    const valueTokens = Array.from({length: 14}, () => Math.floor(Math.random() * 50));
    
    new Chart(ctx, {
        type: 'bar',
        data: {
            labels: labels,
            datasets: [
                {
                    label: 'Battles Won',
                    data: battlesWon,
                    backgroundColor: 'rgba(75, 192, 192, 0.2)',
                    borderColor: 'rgba(75, 192, 192, 1)',
                    borderWidth: 1
                },
                {
                    label: 'Battles Lost',
                    data: battlesLost,
                    backgroundColor: 'rgba(255, 99, 132, 0.2)',
                    borderColor: 'rgba(255, 99, 132, 1)',
                    borderWidth: 1
                },
                {
                    label: 'Value Tokens Gained',
                    data: valueTokens,
                    type: 'line',
                    borderColor: 'rgba(153, 102, 255, 1)',
                    backgroundColor: 'rgba(153, 102, 255, 0.2)',
                    borderDash: [5, 5],
                    yAxisID: 'y1'
                }
            ]
        },
        options: {
            responsive: true,
            scales: {
                y: {
                    beginAtZero: true,
                    title: {
                        display: true,
                        text: 'Battles'
                    }
                },
                y1: {
                    position: 'right',
                    beginAtZero: true,
                    title: {
                        display: true,
                        text: 'Value Tokens'
                    },
                    grid: {
                        drawOnChartArea: false
                    }
                }
            }
        }
    });
}

// Load Performance Chart
function loadPerformanceChart() {
    const ctx = document.getElementById('performance-chart').getContext('2d');
    
    // Sample data
    const data = Array.from({length: 20}, () => ({
        x: Math.floor(Math.random() * 100),
        y: Math.floor(Math.random() * 100),
        r: Math.floor(Math.random() * 30) + 5,
        archetype: Object.keys(archetype)[Math.floor(Math.random() * Object.keys(archetype).length)]
    }));
    
    new Chart(ctx, {
        type: 'bubble',
        data: {
            datasets: [{
                label: 'Daily Performance',
                data: data,
                backgroundColor: 'rgba(54, 162, 235, 0.5)'
            }]
        },
        options: {
            responsive: true,
            scales: {
                x: {
                    title: {
                        display: true,
                        text: 'Engagement'
                    }
                },
                y: {
                    title: {
                        display: true,
                        text: 'Battle Visibility Changed'
                    }
                }
            }
        }
    });
}

// Load Battlegrounds Chart
function loadBattlegroundsChart() {
    const ctx = document.getElementById('battlegrounds-chart').getContext('2d');
    
    // Sample data
    const data = Array.from({length: 15}, () => ({
        x: Math.random() * 100,
        y: Math.random() * 2,
        archetype: Object.keys(archetype)[Math.floor(Math.random() * Object.keys(archetype).length)]
    }));
    
    new Chart(ctx, {
        type: 'scatter',
        data: {
            datasets: [{
                label: 'Specialization vs. Battle Success',
                data: data,
                backgroundColor: 'rgba(255, 99, 132, 0.5)'
            }]
        },
        options: {
            responsive: true,
            scales: {
                x: {
                    title: {
                        display: true,
                        text: 'Archetype Markers Created (%)'
                    }
                },
                y: {
                    title: {
                        display: true,
                        text: 'Battle Win/Loss Ratio'
                    }
                }
            }
        }
    });
}

// Subscribe to Markers
function subscribeToMarkers() {
    db.collection('markers').onSnapshot(snapshot => {
        snapshot.docChanges().forEach(change => {
            const doc = change.doc;
            const data = {...doc.data(), id: doc.id};
            
            if (change.type === 'added') {
                addMarkerToMap(data);
            } else if (change.type === 'modified') {
                updateMarkerOnMap(data);
            } else if (change.type === 'removed') {
                removeMarkerFromMap(doc.id);
            }
        });
        
        // Update filtered markers
        if (document.getElementById('search-bar').value) {
            handleSearch({target: {value: document.getElementById('search-bar').value}});
        } else {
            filteredMarkers = Object.values(markers);
        }
    });
}

// Add Marker to Map
function addMarkerToMap(markerData) {
    // Create icon
    const icon = L.divIcon({
        className: 'marker-icon',
        html: `<i class="fas ${markerData.subcategory}" style="color: ${iconColors[markerData.subcategory] || '#000'};"></i>`,
        iconSize: [48, 48],
        iconAnchor: [24, 24]
    });
    
    // Create marker
    const marker = L.marker([markerData.lat, markerData.lng], {icon: icon});
    
    // Add click event
    marker.on('click', () => openMarkerDetails(markerData));
    
    // Add to map
    marker.addTo(map);
    
    // Store reference
    markers[markerData.id] = {
        ...markerData,
        leafletMarker: marker
    };
}

// Update Marker on Map
function updateMarkerOnMap(markerData) {
    // Remove old marker if exists
    if (markers[markerData.id]) {
        map.removeLayer(markers[markerData.id].leafletMarker);
    }
    
    // Add updated marker
    addMarkerToMap(markerData);
    
    // If this was the selected marker, update details
    if (selectedMarker && selectedMarker.id === markerData.id) {
        selectedMarker = markerData;
        if (!document.getElementById('marker-modal').classList.contains('hidden')) {
            openMarkerDetails(markerData);
        }
    }
}

// Remove Marker from Map
function removeMarkerFromMap(markerId) {
    if (markers[markerId]) {
        map.removeLayer(markers[markerId].leafletMarker);
        delete markers[markerId];
    }
}

// Update Map Markers (for filtering)
function updateMapMarkers() {
    // Clear all markers
    Object.values(markers).forEach(marker => {
        map.removeLayer(marker.leafletMarker);
    });
    
    // Add filtered markers
    (filteredMarkers.length > 0 ? filteredMarkers : Object.values(markers)).forEach(marker => {
        // Check if in utility mode and owned by user
        if (utilityMode && marker.userID === currentUser.id) {
            // Highlight owned markers
            const icon = L.divIcon({
                className: 'marker-icon',
                html: `<i class="fas ${marker.subcategory}" style="color: ${iconColors[marker.subcategory] || '#000'}; filter: brightness(1.5);"></i>`,
                iconSize: [48, 48],
                iconAnchor: [24, 24]
            });
            marker.leafletMarker.setIcon(icon);
        } else if (utilityMode) {
            // Dim non-owned markers
            const icon = L.divIcon({
                className: 'marker-icon',
                html: `<i class="fas ${marker.subcategory}" style="color: ${iconColors[marker.subcategory] || '#000'}; opacity: 0.3;"></i>`,
                iconSize: [48, 48],
                iconAnchor: [24, 24]
            });
            marker.leafletMarker.setIcon(icon);
        } else {
            // Normal icon
            const icon = L.divIcon({
                className: 'marker-icon',
                html: `<i class="fas ${marker.subcategory}" style="color: ${iconColors[marker.subcategory] || '#000'};"></i>`,
                iconSize: [48, 48],
                iconAnchor: [24, 24]
            });
            marker.leafletMarker.setIcon(icon);
        }
        
        marker.leafletMarker.addTo(map);
        
        // Add click handler if not in utility mode
        if (!utilityMode) {
            marker.leafletMarker.off('click').on('click', () => openMarkerDetails(marker));
        } else if (marker.userID === currentUser.id) {
            // Add utility click handler
            marker.leafletMarker.off('click').on('click', () => handleUtilityMarkerClick(marker));
        }
    });
}

// Open Marker Details
function openMarkerDetails(markerData) {
    selectedMarker = markerData;
    
    // Update modal content
    document.getElementById('marker-title').textContent = markerData.title;
    
    // Create breadcrumbs
    const breadcrumbs = document.getElementById('marker-breadcrumbs');
    breadcrumbs.innerHTML = `
        <span>${markerData.category}</span> &gt; 
        <span>${subcategoryNames[markerData.subcategory]}</span> &gt; 
        <span>Visibility: ${markerData.zDepth}</span> &gt; 
        <span>Likes: ${markerData.likes}</span> &gt; 
        <span>Favorites: ${markerData.favorites}</span>
    `;
    
    document.getElementById('marker-body').textContent = markerData.body;
    
    // Create action buttons
    const actions = document.getElementById('marker-actions');
    actions.innerHTML = '';
    
    // Only show actions that exist
    if (markerData.link) {
        const linkBtn = document.createElement('button');
        linkBtn.className = 'p-2 rounded-full bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600';
        linkBtn.innerHTML = '<i class="fas fa-link"></i>';
        linkBtn.addEventListener('click', () => {
            window.open(markerData.link, '_blank');
            userData.linksClicked++;
            userData.interactions++;
            saveUserData();
            updateTokenDisplay();
        });
        actions.appendChild(linkBtn);
    }
    
    if (markerData.email) {
        const emailBtn = document.createElement('button');
        emailBtn.className = 'p-2 rounded-full bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600';
        emailBtn.innerHTML = '<i class="fas fa-envelope"></i>';
        emailBtn.addEventListener('click', () => {
            window.location.href = `mailto:${markerData.email}`;
            userData.emailsClicked++;
            userData.interactions++;
            saveUserData();
            updateTokenDisplay();
        });
        actions.appendChild(emailBtn);
    }
    
    const favoriteBtn = document.createElement('button');
    favoriteBtn.className = 'p-2 rounded-full bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600';
    favoriteBtn.innerHTML = '<i class="fas fa-star"></i>';
    favoriteBtn.addEventListener('click', async () => {
        try {
            await db.collection('markers').doc(markerData.id).update({
                favorites: markerData.favorites + 1
            });
            userData.favoritesMade++;
            userData.interactions++;
            saveUserData();
            updateTokenDisplay();
            showNotification('Added to favorites!', 'success');
        } catch (error) {
            console.error('Error updating favorites:', error);
            showNotification('Failed to add to favorites', 'error');
        }
    });
    actions.appendChild(favoriteBtn);
    
    const likeBtn = document.createElement('button');
    likeBtn.className = 'p-2 rounded-full bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600';
    likeBtn.innerHTML = '<i class="fas fa-heart"></i>';
    likeBtn.addEventListener('click', async () => {
        try {
            await db.collection('markers').doc(markerData.id).update({
                likes: markerData.likes + 1
            });
            userData.likesGiven++;
            userData.interactions++;
            saveUserData();
            updateTokenDisplay();
            showNotification('Liked!', 'success');
        } catch (error) {
            console.error('Error updating likes:', error);
            showNotification('Failed to like', 'error');
        }
    });
    actions.appendChild(likeBtn);
    
    const shareBtn = document.createElement('button');
    shareBtn.className = 'p-2 rounded-full bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600';
    shareBtn.innerHTML = '<i class="fas fa-share"></i>';
    shareBtn.addEventListener('click', () => {
        const url = `${window.location.origin}?marker=${markerData.id}`;
        navigator.clipboard.writeText(url);
        userData.shareClicked++;
        userData.interactions++;
        saveUserData();
        updateTokenDisplay();
        showNotification('Link copied to clipboard!', 'success');
    });
    actions.appendChild(shareBtn);
    
    // Open modal
    openModal('marker');
}

// Handle Utility Marker Click
async function handleUtilityMarkerClick(marker) {
    if (!utilityMode || marker.userID !== currentUser.id) return;
    
    try {
        switch(utilityMode) {
            case 'util1': // Visibility Boost
                await db.collection('markers').doc(marker.id).update({
                    zDepth: marker.zDepth + 100
                });
                showNotification('Visibility boosted by 100!', 'success');
                break;
            case 'util2': // Marker Relocator
                if (!utilityTarget) {
                    utilityTarget = marker;
                    showNotification('Now click where you want to move the marker', 'info');
                    return;
                } else {
                    // Move marker to new location
                    await db.collection('markers').doc(marker.id).update({
                        lat: utilityTarget.lat,
                        lng: utilityTarget.lng
                    });
                    showNotification('Marker relocated!', 'success');
                }
                break;
            case 'util3': // Marker Eraser
                await db.collection('markers').doc(marker.id).delete();
                showNotification('Marker deleted!', 'success');
                break;
            case 'util4': // Visibility Crusher
                // This utility affects any marker, not just owned ones
                // We handle this differently in the marker click handler
                break;
        }
    } catch (error) {
        console.error('Error applying utility:', error);
        showNotification('Failed to apply utility', 'error');
    }
    
    // Reset utility mode
    utilityMode = null;
    utilityTarget = null;
    updateMapMarkers();
}

// Show Notification
function showNotification(message, type) {
    const container = document.getElementById('notification-container');
    const notification = document.createElement('div');
    notification.className = `notification ${type}`;
    notification.textContent = message;
    
    container.appendChild(notification);
    
    // Remove after delay
    setTimeout(() => {
        notification.remove();
    }, type === 'error' ? 5000 : 3000);
}

// Update Token Display
function updateTokenDisplay() {
    document.getElementById('liquidity-tokens').textContent = currentUser.liquidityTokens;
    document.getElementById('value-tokens').textContent = currentUser.valueTokens;
    document.getElementById('engagement-tokens').textContent = currentUser.engagementTokens;
}

// Load User Data
function loadUserData() {
    const savedData = localStorage.getItem('userData');
    if (savedData) {
        userData = JSON.parse(savedData);
    }
    
    // Determine archetype
    determineArchetype();
}

// Save User Data
function saveUserData() {
    // Calculate derived metrics
    userData.battleWinRatio = userData.battlesWon + userData.battlesLost > 0 ? 
        (userData.battlesWon / (userData.battlesWon + userData.battlesLost)) * 100 : 0;
    
    // Calculate engagement (interactions per 24h)
    const now = Date.now();
    const oneDay = 24 * 60 * 60 * 1000;
    if (now - userData.lastInteraction > oneDay) {
        userData.engagement = userData.interactions;
        userData.lastInteraction = now;
        userData.interactions = 0;
    }
    
    localStorage.setItem('userData', JSON.stringify(userData));
}

// Determine Archetype
function determineArchetype() {
    // In a real implementation, this would analyze user's markers
    // For now, we'll randomly assign an archetype
    const archetypes = Object.values(archetype);
    userData.archetype = archetypes[Math.floor(Math.random() * archetypes.length)];
}