File size: 87,813 Bytes
6d54fec
 
 
 
 
 
 
 
 
 
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a143487
 
 
 
 
 
 
 
 
6d54fec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
 
 
 
 
 
 
 
 
 
2f784f7
 
 
 
 
 
 
 
 
 
6d54fec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f784f7
 
6d54fec
 
 
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
 
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
 
 
 
 
 
 
 
 
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
 
 
 
 
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
 
 
 
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
 
 
 
 
 
 
 
2f784f7
6d54fec
 
 
 
2f784f7
 
6d54fec
 
 
 
 
2f784f7
 
 
 
 
 
 
 
 
 
 
 
6d54fec
 
 
2f784f7
 
 
 
6d54fec
 
 
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
 
 
 
 
 
 
 
 
2f784f7
6d54fec
 
 
 
 
 
 
 
 
 
 
 
 
 
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
2f784f7
 
 
6d54fec
2f784f7
 
 
6d54fec
2f784f7
 
 
6d54fec
 
 
 
 
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
 
 
 
2f784f7
 
 
 
 
6d54fec
 
 
 
 
 
2f784f7
 
 
 
 
6d54fec
 
 
 
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
2f784f7
 
6d54fec
 
 
 
 
 
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
2f784f7
 
6d54fec
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f784f7
6d54fec
 
 
2f784f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d54fec
 
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
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
// Map initialization
let map;
let units = [];
let selectedUnit = null;
let isPaused = false;
let dayCounter = 1;
let hourCounter = 0;
let battleIntervals = [];
let timer;

// Battle statistics
let statistics = {
    blueActive: 15,
    redActive: 15,
    blueCasualties: 0,
    redCasualties: 0,
    blueReinforcements: 0,
    redReinforcements: 0,
    activeBattles: 0,
    blueControl: 50,
    redControl: 50,
    nextUnitId: 30 // Starting after initial 30 units (15 blue + 15 red)
};

// Unit types with their properties - expanded with new types that unlock over time
const unitTypes = {
    // Base units available from start
    infantry: { speed: 0.001, attackPower: 10, range: 0.01, icon: 'πŸ‘€', unlockDay: 0 },
    armor: { speed: 0.002, attackPower: 25, range: 0.015, icon: 'πŸ›‘οΈ', unlockDay: 0 },
    artillery: { speed: 0.0005, attackPower: 30, range: 0.03, icon: 'πŸ’₯', unlockDay: 0 },
    recon: { speed: 0.003, attackPower: 5, range: 0.02, icon: 'πŸ”', unlockDay: 0 },
    airDefense: { speed: 0.0008, attackPower: 20, range: 0.025, icon: 'πŸš€', unlockDay: 0 },
    command: { speed: 0.0006, attackPower: 5, range: 0.015, icon: '⭐', unlockDay: 0 },
    medical: { speed: 0.001, attackPower: 0, range: 0.008, icon: '🩺', unlockDay: 0 },
    
    // Units that unlock over time
    airborne: { speed: 0.0025, attackPower: 15, range: 0.02, icon: 'πŸͺ‚', unlockDay: 2 },
    specialForces: { speed: 0.0035, attackPower: 18, range: 0.015, icon: 'πŸ₯·', unlockDay: 3 },
    mechanized: { speed: 0.0015, attackPower: 22, range: 0.018, icon: '🚜', unlockDay: 4 },
    engineers: { speed: 0.001, attackPower: 8, range: 0.01, icon: 'πŸ”§', unlockDay: 5, special: 'fortify' },
    naval: { speed: 0.0008, attackPower: 35, range: 0.04, icon: 'βš“', unlockDay: 6 },
    airForce: { speed: 0.004, attackPower: 28, range: 0.05, icon: '✈️', unlockDay: 7 },
    missile: { speed: 0.0005, attackPower: 40, range: 0.06, icon: 'πŸš€', unlockDay: 10 },
    elite: { speed: 0.002, attackPower: 35, range: 0.03, icon: 'πŸ‘‘', unlockDay: 15 }
};

// Initialize the map when the DOM is fully loaded
document.addEventListener('DOMContentLoaded', function() {
    initializeMap();
    generateUnits();
    placeUnitsOnMap();
    startSimulation();
    
    // Set up event listener for pause button
    document.getElementById('pause-button').addEventListener('click', togglePause);
});

function initializeMap() {
    // Create map centered on a fictional battle area
    map = L.map('map', {
        dragging: false,      // Disable map dragging
        zoomControl: false,   // Disable zoom control buttons
        doubleClickZoom: false, // Disable double click zoom
        scrollWheelZoom: false, // Disable scroll wheel zoom
        touchZoom: false,      // Disable touch zoom
        boxZoom: false,        // Disable box zoom
        keyboard: false        // Disable keyboard navigation
    }).setView([40.7, -74.0], 13);
    
    // Add the base map tiles (OpenStreetMap)
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
        maxZoom: 19
    }).addTo(map);
}

function generateUnits() {
    // Generate blue forces
    for (let i = 0; i < 15; i++) {
        const type = getRandomUnitType();
        units.push({
            id: `blue-${i}`,
            name: `Blue Force ${getUnitName(type)} ${i + 1}`,
            type: type,
            faction: 'blue',
            strength: Math.floor(Math.random() * 50) + 50, // 50-99
            health: 100,
            status: getRandomStatus(),
            lat: 40.7 + (Math.random() * 0.05 - 0.025), // Around the center
            lng: -74.0 + (Math.random() * 0.05 - 0.025),
            targetLat: null,
            targetLng: null,
            marker: null,
            icon: unitTypes[type].icon,
            speed: unitTypes[type].speed,
            attackPower: unitTypes[type].attackPower,
            range: unitTypes[type].range,
            inBattle: false,
            movingToTarget: false,
            kills: 0,
            lastMoveTimestamp: Date.now()
        });
    }
    
    // Generate red forces
    for (let i = 0; i < 15; i++) {
        const type = getRandomUnitType();
        units.push({
            id: `red-${i}`,
            name: `Red Force ${getUnitName(type)} ${i + 1}`,
            type: type,
            faction: 'red',
            strength: Math.floor(Math.random() * 50) + 50, // 50-99
            health: 100,
            status: getRandomStatus(),
            lat: 40.7 + (Math.random() * 0.05 - 0.025), // Around the center
            lng: -74.0 + (Math.random() * 0.05 - 0.025),
            targetLat: null,
            targetLng: null,
            marker: null,
            icon: unitTypes[type].icon,
            speed: unitTypes[type].speed,
            attackPower: unitTypes[type].attackPower,
            range: unitTypes[type].range,
            inBattle: false,
            movingToTarget: false,
            kills: 0,
            lastMoveTimestamp: Date.now()
        });
    }
}

function getRandomUnitType() {
    // Filter unit types based on the current day
    const availableTypes = Object.entries(unitTypes)
        .filter(([type, properties]) => properties.unlockDay <= dayCounter)
        .map(([type]) => type);
    
    // Give higher probability to newly unlocked units to make them more noticeable
    const newlyUnlockedTypes = availableTypes.filter(type =>
        unitTypes[type].unlockDay === dayCounter);
    
    // 30% chance to select a newly unlocked unit type if available
    if (newlyUnlockedTypes.length > 0 && Math.random() < 0.3) {
        const randomIndex = Math.floor(Math.random() * newlyUnlockedTypes.length);
        return newlyUnlockedTypes[randomIndex];
    }
    
    // Otherwise select randomly from all available types
    const randomIndex = Math.floor(Math.random() * availableTypes.length);
    return availableTypes[randomIndex];
}

function getUnitName(type) {
    const names = {
        infantry: 'Battalion',
        armor: 'Tank Division',
        artillery: 'Artillery Battery',
        recon: 'Reconnaissance Unit',
        airDefense: 'Air Defense Unit',
        command: 'Command Center',
        medical: 'Medical Corps',
        // New unit types
        airborne: 'Airborne Division',
        specialForces: 'Special Forces',
        mechanized: 'Mechanized Infantry',
        engineers: 'Combat Engineers',
        naval: 'Naval Support',
        airForce: 'Air Squadron',
        missile: 'Missile Battery',
        elite: 'Elite Force'
    };
    return names[type] || 'Unit';
}

function getRandomStatus() {
    const statuses = ['Operational', 'Engaged', 'Advancing', 'Defending', 'Flanking', 'Retreating'];
    return statuses[Math.floor(Math.random() * statuses.length)];
}

function placeUnitsOnMap() {
    units.forEach(unit => {
        const markerSize = unit.strength / 15 + 20; // Size based on strength
        
        // Create custom HTML element for the marker
        const markerHtml = `
            <div class="unit-marker ${unit.faction}-unit" style="width: ${markerSize}px; height: ${markerSize}px; line-height: ${markerSize}px; font-size: ${markerSize/2}px;">
                ${unit.icon}
            </div>
        `;
        
        // Create the icon
        const customIcon = L.divIcon({
            html: markerHtml,
            className: '',
            iconSize: [markerSize, markerSize],
            iconAnchor: [markerSize/2, markerSize/2]
        });
        
        // Create marker and add to map
        unit.marker = L.marker([unit.lat, unit.lng], { icon: customIcon })
            .addTo(map)
            .on('click', function() {
                selectUnit(unit);
            });
    });
}

function selectUnit(unit) {
    // Reset previous selection
    if (selectedUnit) {
        const prevMarker = selectedUnit.marker;
        const prevIcon = prevMarker.options.icon;
        const prevHtml = prevIcon.options.html.replace(' selected', '');
        
        const newIcon = L.divIcon({
            html: prevHtml,
            className: prevIcon.options.className,
            iconSize: prevIcon.options.iconSize,
            iconAnchor: prevIcon.options.iconAnchor
        });
        
        prevMarker.setIcon(newIcon);
    }
    
    // Set new selection
    selectedUnit = unit;
    
    // Update marker to show selection
    const marker = unit.marker;
    const icon = marker.options.icon;
    const newHtml = icon.options.html.replace('unit-marker', 'unit-marker selected');
    
    const newIcon = L.divIcon({
        html: newHtml,
        className: icon.options.className,
        iconSize: icon.options.iconSize,
        iconAnchor: icon.options.iconAnchor
    });
    
    marker.setIcon(newIcon);
    
    // Update info panel
    updateUnitInfo(unit);
}

function updateUnitInfo(unit) {
    const unitInfoDiv = document.getElementById('unit-info');
    
    let healthClass = unit.health > 70 ? '' : (unit.health > 30 ? 'health-warning' : 'health-critical');
    
    unitInfoDiv.innerHTML = `
        <div class="unit-name">${unit.name}</div>
        <div class="unit-type">Type: ${getUnitName(unit.type)}</div>
        <div class="unit-strength">Strength: ${unit.strength} personnel</div>
        <div class="unit-status">Status: ${unit.status}</div>
        <div class="unit-health">
            Health: ${unit.health}%
            <div class="health-bar ${healthClass}">
                <div class="health-fill" style="width: ${unit.health}%"></div>
            </div>
        </div>
        <div class="unit-stats">
            <div>Attack Power: ${unit.attackPower}</div>
            <div>Range: ${(unit.range * 1000).toFixed(1)} meters</div>
            <div>Speed: ${(unit.speed * 1000).toFixed(1)}</div>
            <div>Kills: ${unit.kills}</div>
        </div>
        <div class="unit-position">
            Position: ${unit.lat.toFixed(4)}, ${unit.lng.toFixed(4)}
        </div>
    `;
    
    // If the unit is in battle, show that information
    if (unit.inBattle) {
        unitInfoDiv.innerHTML += `
            <div class="unit-battle-status">
                <span style="color: red; font-weight: bold;">βš”οΈ ENGAGED IN BATTLE βš”οΈ</span>
            </div>
        `;
    }
}

function startSimulation() {
    // Update time every second
    timer = setInterval(() => {
        if (!isPaused) {
            updateTime();
            
            // More frequent target assignments on busier days
            if (hourCounter % (3 - Math.min(2, Math.floor(dayCounter / 5))) === 0) {
                assignRandomTargets();
            }
            
            // Dynamic reinforcement spawning
            // Base chance increases with time and if there are fewer active battles
            const baseSpawnChance = 0.15 + (dayCounter / 50);
            const battleAdjustment = Math.max(0, 0.1 - (statistics.activeBattles / 30));
            
            if (Math.random() < baseSpawnChance + battleAdjustment) {
                spawnReinforcements();
            }
            
            // Create sudden "hot zones" where units are redirected
            if (Math.random() < 0.03) {
                createHotZone();
            }
            
            moveUnits();
            checkForBattles();
            updateBattleStatistics();
            
            // Increase frequency of random events for more action
            if (Math.random() < 0.05) {  // 5x more frequent events
                triggerRandomEvent();
            }
            
            // Ensure battle never ends by checking unit counts and adding reinforcements if needed
            const blueSurvivors = units.filter(u => u.faction === 'blue' && u.health > 0).length;
            const redSurvivors = units.filter(u => u.faction === 'red' && u.health > 0).length;
            
            // If either faction is below minimum threshold, add emergency reinforcements
            const MIN_UNITS_PER_FACTION = 8;  // Ensure at least 8 units per side
            if (blueSurvivors < MIN_UNITS_PER_FACTION) {
                for (let i = 0; i < (MIN_UNITS_PER_FACTION - blueSurvivors); i++) {
                    spawnNewUnit('blue');
                }
            }
            if (redSurvivors < MIN_UNITS_PER_FACTION) {
                for (let i = 0; i < (MIN_UNITS_PER_FACTION - redSurvivors); i++) {
                    spawnNewUnit('red');
                }
            }
            
            // Update the selected unit's info if one is selected
            if (selectedUnit) {
                updateUnitInfo(selectedUnit);
            }
        }
    }, 1000);
}

// Helper function to get a random unit type based on day counter
function getRandomUnitType() {
    // Get all unit types that have been unlocked by the current day
    const availableTypes = Object.entries(unitTypes)
        .filter(([type, properties]) => properties.unlockDay <= dayCounter)
        .map(([type]) => type);
    
    // Select a random type from available ones
    return availableTypes[Math.floor(Math.random() * availableTypes.length)];
}

// Helper function to get a formatted name for a unit type
function getUnitName(type) {
    // Format the type name with proper capitalization and spacing
    const formattedName = type
        .replace(/([A-Z])/g, ' $1') // Add space before capital letters
        .replace(/^./, str => str.toUpperCase()); // Capitalize first letter
    
    return formattedName;
}

function createHotZone() {
    // Create a random focal point on the map with faction control bias
    // This makes hotspots appear more often in controlled territory
    let hotspotLat, hotspotLng;
    
    if (Math.random() < 0.5) {
        // Neutral hotspot
        hotspotLat = 40.7 + (Math.random() * 0.1 - 0.05);
        hotspotLng = -74.0 + (Math.random() * 0.1 - 0.05);
    } else {
        // Faction-biased hotspot
        const dominantFaction = statistics.blueControl > statistics.redControl ? 'blue' : 'red';
        
        if (dominantFaction === 'blue') {
            // Blue territory tends to be west/south
            hotspotLat = 40.68 + (Math.random() * 0.06);
            hotspotLng = -74.03 - (Math.random() * 0.04);
        } else {
            // Red territory tends to be east/north
            hotspotLat = 40.72 + (Math.random() * 0.06);
            hotspotLng = -73.97 - (Math.random() * 0.04);
        }
    }
    
    // Divert some portion of units to this hotspot
    units.forEach(unit => {
        if (unit.health > 0 && Math.random() < 0.3) {
            unit.targetLat = hotspotLat;
            unit.targetLng = hotspotLng;
            unit.movingToTarget = true;
            unit.status = 'Redeploying';
        }
    });
    
    // Determine the type of hotspot (battle, supply drop, strategic location)
    const hotspotTypes = [
        { name: 'battle', probability: 0.5, color: 'rgba(255, 0, 0, 0.2)', shadowColor: 'rgba(255, 0, 0, 0.5)' },
        { name: 'supply', probability: 0.3, color: 'rgba(0, 255, 0, 0.2)', shadowColor: 'rgba(0, 255, 0, 0.5)' },
        { name: 'strategic', probability: 0.2, color: 'rgba(255, 255, 0, 0.2)', shadowColor: 'rgba(255, 255, 0, 0.5)' }
    ];
    
    // Select a hotspot type based on probability
    let hotspotType;
    const random = Math.random();
    let cumulativeProbability = 0;
    
    for (const type of hotspotTypes) {
        cumulativeProbability += type.probability;
        if (random < cumulativeProbability) {
            hotspotType = type;
            break;
        }
    }
    
    // Determine if this is a major event (less common, but more dramatic)
    const isMajorEvent = Math.random() < 0.3;
    const size = isMajorEvent ? 60 : 40;
    const duration = isMajorEvent ? 8000 : 4000;
    
    // Create visual effect for the hotspot
    const point = map.latLngToLayerPoint([hotspotLat, hotspotLng]);
    
    // Create main hotspot element
    const hotspotEl = document.createElement('div');
    hotspotEl.style.position = 'absolute';
    hotspotEl.style.left = (point.x - size/2) + 'px';
    hotspotEl.style.top = (point.y - size/2) + 'px';
    hotspotEl.style.width = size + 'px';
    hotspotEl.style.height = size + 'px';
    hotspotEl.style.backgroundColor = hotspotType.color;
    hotspotEl.style.borderRadius = '50%';
    hotspotEl.style.zIndex = '600';
    hotspotEl.style.boxShadow = `0 0 ${size/2}px ${hotspotType.shadowColor}`;
    hotspotEl.style.animation = `battle-expand ${duration/1000}s forwards`;
    
    // Add pulsing effect to make it more dynamic
    const pulseAnimation = document.createElement('style');
    pulseAnimation.textContent = `
        @keyframes pulse-${Date.now()} {
            0% { transform: scale(1); opacity: 1; }
            50% { transform: scale(1.2); opacity: 0.8; }
            100% { transform: scale(1); opacity: 1; }
        }
    `;
    document.head.appendChild(pulseAnimation);
    hotspotEl.style.animation = `pulse-${Date.now()} 1.5s infinite`;
    
    document.querySelector('#map').appendChild(hotspotEl);
    
    // Add secondary ring effect for major events
    if (isMajorEvent) {
        const outerRing = document.createElement('div');
        outerRing.style.position = 'absolute';
        outerRing.style.left = (point.x - size) + 'px';
        outerRing.style.top = (point.y - size) + 'px';
        outerRing.style.width = (size * 2) + 'px';
        outerRing.style.height = (size * 2) + 'px';
        outerRing.style.border = `2px solid ${hotspotType.color}`;
        outerRing.style.borderRadius = '50%';
        outerRing.style.zIndex = '599';
        outerRing.style.opacity = '0.7';
        
        // Outward expansion animation
        const expandAnimation = document.createElement('style');
        expandAnimation.textContent = `
            @keyframes expand-${Date.now()} {
                0% { transform: scale(0.5); opacity: 0.7; }
                100% { transform: scale(2); opacity: 0; }
            }
        `;
        document.head.appendChild(expandAnimation);
        outerRing.style.animation = `expand-${Date.now()} 4s infinite`;
        
        document.querySelector('#map').appendChild(outerRing);
        
        // Add notification for major events
        let eventTitle = '';
        let eventDesc = '';
        
        if (hotspotType.name === 'battle') {
            eventTitle = 'Major Battle Erupting';
            eventDesc = 'Heavy fighting reported in sector';
        } else if (hotspotType.name === 'supply') {
            eventTitle = 'Supply Drop Zone';
            eventDesc = 'Critical supplies have been airdropped';
        } else {
            eventTitle = 'Strategic Location';
            eventDesc = 'Forces moving to secure key position';
        }
        
        displayEventNotification(eventTitle, eventDesc);
        
        // Remove the outer ring after the duration
        setTimeout(() => {
            if (outerRing.parentNode) {
                outerRing.parentNode.removeChild(outerRing);
            }
        }, duration);
    }
    
    // Remove the hotspot element after the duration
    setTimeout(() => {
        if (hotspotEl.parentNode) {
            hotspotEl.parentNode.removeChild(hotspotEl);
        }
    }, duration);
}

// Function to spawn a new unit for a given faction
function spawnNewUnit(faction) {
    // Determine entry point based on faction
    let entryLat, entryLng;
    
    if (faction === 'blue') {
        // Blue forces come from west or south
        if (Math.random() < 0.5) {
            entryLat = 40.7 + (Math.random() * 0.05 - 0.025);
            entryLng = -74.05 - (Math.random() * 0.02); // West
        } else {
            entryLat = 40.65 - (Math.random() * 0.02); // South
            entryLng = -74.0 + (Math.random() * 0.05 - 0.025);
        }
    } else {
        // Red forces come from east or north
        if (Math.random() < 0.5) {
            entryLat = 40.7 + (Math.random() * 0.05 - 0.025);
            entryLng = -73.95 + (Math.random() * 0.02); // East
        } else {
            entryLat = 40.75 + (Math.random() * 0.02); // North
            entryLng = -74.0 + (Math.random() * 0.05 - 0.025);
        }
    }
    
    // Create a new unit
    const type = getRandomUnitType();
    const unitId = `${faction}-${statistics.nextUnitId++}`;
    
    const newUnit = {
        id: unitId,
        name: `${faction.charAt(0).toUpperCase() + faction.slice(1)} Force ${getUnitName(type)} ${statistics.nextUnitId}`,
        type: type,
        faction: faction,
        strength: 50 + Math.floor(Math.random() * 50), // 50-99
        health: 100,
        status: 'Reinforcing',
        lat: entryLat,
        lng: entryLng,
        targetLat: null,
        targetLng: null,
        marker: null,
        icon: unitTypes[type].icon,
        speed: unitTypes[type].speed,
        attackPower: unitTypes[type].attackPower,
        range: unitTypes[type].range,
        inBattle: false,
        movingToTarget: false,
        kills: 0,
        lastMoveTimestamp: Date.now()
    };
    
    units.push(newUnit);
    
    // Create marker for the new unit
    const markerSize = newUnit.strength / 15 + 20;
    
    const markerHtml = `
        <div class="unit-marker ${newUnit.faction}-unit new-unit" style="width: ${markerSize}px; height: ${markerSize}px; line-height: ${markerSize}px; font-size: ${markerSize/2}px;">
            ${newUnit.icon}
        </div>
    `;
    
    const customIcon = L.divIcon({
        html: markerHtml,
        className: '',
        iconSize: [markerSize, markerSize],
        iconAnchor: [markerSize/2, markerSize/2]
    });
    
    newUnit.marker = L.marker([newUnit.lat, newUnit.lng], { icon: customIcon })
        .addTo(map)
        .on('click', function() {
            selectUnit(newUnit);
        });
        
    // Add an entrance effect
    const point = map.latLngToLayerPoint([newUnit.lat, newUnit.lng]);
    
    const entryEffect = document.createElement('div');
    entryEffect.style.position = 'absolute';
    entryEffect.style.left = point.x + 'px';
    entryEffect.style.top = point.y + 'px';
    entryEffect.style.width = '30px';
    entryEffect.style.height = '30px';
    entryEffect.style.backgroundColor = faction === 'blue' ? 'rgba(0, 100, 255, 0.5)' : 'rgba(255, 0, 0, 0.5)';
    entryEffect.style.borderRadius = '50%';
    entryEffect.style.zIndex = '700';
    entryEffect.style.boxShadow = `0 0 15px ${faction === 'blue' ? 'blue' : 'red'}`;
    entryEffect.style.animation = 'unit-entry 1.5s forwards';
    
    // Add custom animation if not already defined
    if (!document.querySelector('#unit-entry-animation')) {
        const style = document.createElement('style');
        style.id = 'unit-entry-animation';
        style.textContent = `
            @keyframes unit-entry {
                0% { transform: scale(0); opacity: 1; }
                70% { transform: scale(1.5); opacity: 0.7; }
                100% { transform: scale(2); opacity: 0; }
            }
            
            .new-unit {
                animation: highlight-new 2s;
            }
            
            @keyframes highlight-new {
                0% { box-shadow: 0 0 15px gold; transform: scale(1.2); }
                100% { box-shadow: none; transform: scale(1); }
            }
        `;
        document.head.appendChild(style);
    }
    
    document.querySelector('#map').appendChild(entryEffect);
    
    // Remove effect after animation completes
    setTimeout(() => {
        if (entryEffect.parentNode) entryEffect.parentNode.removeChild(entryEffect);
    }, 1500);
    
    // Assign target to the new unit
    assignTargetToUnit(newUnit);
    
    // Update faction statistics
    if (faction === 'blue') {
        statistics.blueActive++;
        statistics.blueReinforcements++;
    } else {
        statistics.redActive++;
        statistics.redReinforcements++;
    }
    
    return newUnit;
}

function triggerRandomEvent() {
    // Increment hour counter - every 12 hours is a new day
    hourCounter++;
    if (hourCounter >= 12) {
        hourCounter = 0;
        dayCounter++;
        
        // Update the day counter in UI if it exists
        const dayCounterElement = document.getElementById('day-counter');
        if (dayCounterElement) {
            dayCounterElement.textContent = `Day ${dayCounter}`;
        } else {
            // Create day counter element if it doesn't exist
            const statsContainer = document.querySelector('.statistics-container');
            if (statsContainer) {
                const dayElement = document.createElement('div');
                dayElement.id = 'day-counter';
                dayElement.style.fontSize = '18px';
                dayElement.style.fontWeight = 'bold';
                dayElement.style.marginBottom = '10px';
                dayElement.textContent = `Day ${dayCounter}`;
                statsContainer.prepend(dayElement);
            }
        }
        
        // Check if new unit types have been unlocked
        const newlyUnlocked = Object.entries(unitTypes)
            .filter(([type, properties]) => properties.unlockDay === dayCounter)
            .map(([type]) => type);
        
        if (newlyUnlocked.length > 0) {
            // Announce new technology
            const techAnnouncement = document.createElement('div');
            techAnnouncement.className = 'tech-announcement';
            techAnnouncement.style.position = 'absolute';
            techAnnouncement.style.top = '50%';
            techAnnouncement.style.left = '50%';
            techAnnouncement.style.transform = 'translate(-50%, -50%)';
            techAnnouncement.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
            techAnnouncement.style.color = 'white';
            techAnnouncement.style.padding = '20px';
            techAnnouncement.style.borderRadius = '10px';
            techAnnouncement.style.zIndex = '2000';
            techAnnouncement.style.textAlign = 'center';
            techAnnouncement.style.boxShadow = '0 0 20px gold';
            
            techAnnouncement.innerHTML = `
                <h3>New Technology Unlocked!</h3>
                <div>${newlyUnlocked.map(type => `${unitTypes[type].icon} ${getUnitName(type)}`).join('<br>')}</div>
            `;
            
            document.body.appendChild(techAnnouncement);
            
            // Remove after 5 seconds
            setTimeout(() => {
                techAnnouncement.style.opacity = '0';
                techAnnouncement.style.transition = 'opacity 1s';
                setTimeout(() => {
                    if (techAnnouncement.parentNode) {
                        techAnnouncement.parentNode.removeChild(techAnnouncement);
                    }
                }, 1000);
            }, 5000);
        }
    }
    
    // List of possible random events with weighted probabilities
    const events = [
        { name: 'Reinforcement Surge', weight: 0.3, handler: () => {
            // Spawn extra units for a random faction
            const faction = Math.random() < 0.5 ? 'blue' : 'red';
            for (let i = 0; i < 3 + Math.floor(Math.random() * 3); i++) {
                spawnNewUnit(faction);
            }
            
            // Display notification
            displayEventNotification(
                'Reinforcement Surge',
                `${faction.charAt(0).toUpperCase() + faction.slice(1)} forces receiving reinforcements`
            );
        }},
        { name: 'Supply Line Disruption', weight: 0.15, handler: () => {
            // Randomly weaken some units of one faction
            const faction = Math.random() < 0.5 ? 'blue' : 'red';
            const affectedUnits = units.filter(u => u.faction === faction && u.health > 30);
            
            if (affectedUnits.length > 0) {
                affectedUnits.forEach(unit => {
                    unit.health = Math.max(30, unit.health - 20);
                    updateUnitMarkerAppearance(unit);
                });
                
                // Display notification
                displayEventNotification(
                    'Supply Line Disruption',
                    `${faction.charAt(0).toUpperCase() + faction.slice(1)} forces weakened by supply issues`
                );
            }
        }},
        { name: 'Strategic Redeployment', weight: 0.2, handler: () => {
            // Randomly reassign targets to create new battle lines
            assignRandomTargets(0.7); // Higher chance of reassignment
            
            // Display notification
            displayEventNotification(
                'Strategic Redeployment',
                'Forces repositioning for tactical advantage'
            );
        }},
        { name: 'Environmental Event', weight: 0.2, handler: () => {
            // Trigger a weather or environment event
            triggerEnvironmentalEvent();
        }},
        { name: 'Hot Zone', weight: 0.15, handler: () => {
            // Create a hot zone where forces converge
            createHotZone();
            
            // Display notification
            displayEventNotification(
                'Strategic Hotspot',
                'A critical location has been identified for control'
            );
        }}
    ];
    
    // Weighted random selection
    const totalWeight = events.reduce((sum, event) => sum + event.weight, 0);
    let random = Math.random() * totalWeight;
    let selectedEvent = null;
    
    for (const event of events) {
        random -= event.weight;
        if (random <= 0) {
            selectedEvent = event;
            break;
        }
    }
    
    // Execute the selected event
    selectedEvent.handler();
    
    // Auto-balance forces if one side is getting too dominant
    const blueSurvivors = units.filter(u => u.faction === 'blue' && u.health > 0).length;
    const redSurvivors = units.filter(u => u.faction === 'red' && u.health > 0).length;
    
    if (blueSurvivors < 5 || redSurvivors < 5) {
        // If either side is getting too low, reinforce them
        const weakerFaction = blueSurvivors < redSurvivors ? 'blue' : 'red';
        const reinforcementCount = 3 + Math.floor(Math.random() * 3);
        
        for (let i = 0; i < reinforcementCount; i++) {
            spawnNewUnit(weakerFaction);
        }
        
        displayEventNotification(
            'Emergency Reinforcements',
            `${weakerFaction.charAt(0).toUpperCase() + weakerFaction.slice(1)} forces receiving critical support`
        );
    }
}

// Function to display event notifications
function displayEventNotification(title, message) {
    const notification = document.createElement('div');
    notification.className = 'event-notification';
    notification.style.position = 'absolute';
    notification.style.top = '20px';
    notification.style.right = '20px';
    notification.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
    notification.style.color = 'white';
    notification.style.padding = '10px 15px';
    notification.style.borderRadius = '5px';
    notification.style.zIndex = '2000';
    notification.style.maxWidth = '250px';
    notification.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.5)';
    notification.style.opacity = '0';
    notification.style.transition = 'opacity 0.5s';
    
    notification.innerHTML = `
        <div style="font-weight: bold; font-size: 14px; margin-bottom: 5px;">${title}</div>
        <div style="font-size: 12px;">${message}</div>
    `;
    
    document.body.appendChild(notification);
    
    // Fade in
    setTimeout(() => {
        notification.style.opacity = '1';
    }, 100);
    
    // Remove after 5 seconds
    setTimeout(() => {
        notification.style.opacity = '0';
        setTimeout(() => {
            if (notification.parentNode) {
                notification.parentNode.removeChild(notification);
            }
        }, 500);
    }, 5000);
}

// Environmental events function
function triggerEnvironmentalEvent() {
    // Random selection of environmental events
    const events = [
        {
            name: 'Fog of War',
            probability: 0.3,
            handler: () => {
                // Reduce all units' range temporarily
                units.forEach(unit => {
                    if (!unit.originalRange) unit.originalRange = unit.range;
                    unit.range *= 0.5; // 50% reduction in range
                });
                
                // Add a fog overlay to the map
                const fogOverlay = document.createElement('div');
                fogOverlay.id = 'fog-overlay';
                fogOverlay.style.position = 'absolute';
                fogOverlay.style.top = '0';
                fogOverlay.style.left = '0';
                fogOverlay.style.width = '100%';
                fogOverlay.style.height = '100%';
                fogOverlay.style.backgroundColor = 'rgba(255, 255, 255, 0.5)';
                fogOverlay.style.pointerEvents = 'none';
                fogOverlay.style.zIndex = '500';
                fogOverlay.style.transition = 'opacity 2s';
                
                document.querySelector('#map').appendChild(fogOverlay);
                
                // Show event notification
                displayEventNotification('Fog of War', 'Visibility reduced for all units');
                
                // Restore normal visibility after duration
                setTimeout(() => {
                    // Fade out fog
                    fogOverlay.style.opacity = '0';
                    
                    // Remove fog and restore ranges
                    setTimeout(() => {
                        if (fogOverlay.parentNode) fogOverlay.parentNode.removeChild(fogOverlay);
                        units.forEach(unit => {
                            if (unit.originalRange) {
                                unit.range = unit.originalRange;
                                delete unit.originalRange;
                            }
                        });
                    }, 2000);
                }, 15000); // 15 seconds for simulation
                
                return true;
            }
        },
        {
            name: 'Artillery Barrage',
            probability: 0.3,
            handler: () => {
                // Random faction launches barrage
                const launchingFaction = Math.random() < 0.5 ? 'blue' : 'red';
                const targetFaction = launchingFaction === 'blue' ? 'red' : 'blue';
                
                // Find target units
                const targetUnits = units.filter(u => u.faction === targetFaction && u.health > 0);
                
                if (targetUnits.length > 0) {
                    // Calculate target area (center of enemy forces)
                    let centerLat = 0, centerLng = 0;
                    targetUnits.forEach(unit => {
                        centerLat += unit.lat;
                        centerLng += unit.lng;
                    });
                    centerLat /= targetUnits.length;
                    centerLng /= targetUnits.length;
                    
                    displayEventNotification(
                        'Artillery Barrage',
                        `${launchingFaction.charAt(0).toUpperCase() + launchingFaction.slice(1)} forces launching artillery strike`
                    );
                    
                    // Visual effects - multiple explosions
                    for (let i = 0; i < 5; i++) {
                        setTimeout(() => {
                            const offsetLat = centerLat + (Math.random() * 0.01 - 0.005);
                            const offsetLng = centerLng + (Math.random() * 0.01 - 0.005);
                            
                            // Create explosion animation
                            const point = map.latLngToLayerPoint([offsetLat, offsetLng]);
                            
                            const explosionEl = document.createElement('div');
                            explosionEl.style.position = 'absolute';
                            explosionEl.style.left = point.x + 'px';
                            explosionEl.style.top = point.y + 'px';
                            explosionEl.style.width = '40px';
                            explosionEl.style.height = '40px';
                            explosionEl.style.backgroundColor = '#ff4500';
                            explosionEl.style.borderRadius = '50%';
                            explosionEl.style.zIndex = '700';
                            explosionEl.style.boxShadow = '0 0 30px red';
                            explosionEl.style.animation = 'explosion 1.5s forwards';
                            
                            // Add custom animation if not already defined
                            if (!document.querySelector('#explosion-animation')) {
                                const style = document.createElement('style');
                                style.id = 'explosion-animation';
                                style.textContent = `
                                    @keyframes explosion {
                                        0% { transform: scale(0.2); opacity: 0.8; }
                                        50% { transform: scale(1.5); opacity: 0.9; }
                                        100% { transform: scale(2); opacity: 0; }
                                    }
                                `;
                                document.head.appendChild(style);
                            }
                            
                            document.querySelector('#map').appendChild(explosionEl);
                            
                            // Apply damage to units in radius
                            targetUnits.forEach(unit => {
                                const dLat = unit.lat - offsetLat;
                                const dLng = unit.lng - offsetLng;
                                const distance = Math.sqrt(dLat * dLat + dLng * dLng);
                                
                                // Units within blast radius take damage
                                if (distance < 0.005) {
                                    const damage = Math.round(20 * (1 - distance / 0.005));
                                    unit.health = Math.max(0, unit.health - damage);
                                    
                                    // Update unit appearance
                                    updateUnitMarkerAppearance(unit);
                                    
                                    // Check for death
                                    if (unit.health <= 0) {
                                        checkUnitDeath(unit);
                                    }
                                }
                            });
                            
                            // Remove explosion after animation
                            setTimeout(() => {
                                if (explosionEl.parentNode) {
                                    explosionEl.parentNode.removeChild(explosionEl);
                                }
                            }, 1500);
                        }, i * 800); // Staggered explosions
                    }
                    
                    return true;
                }
                
                return false;
            }
        },
        {
            name: 'Supply Drop',
            probability: 0.4,
            handler: () => {
                // Random faction receives supplies
                const targetFaction = Math.random() < 0.5 ? 'blue' : 'red';
                
                // Find units that would benefit
                const targetUnits = units.filter(u => u.faction === targetFaction && u.health < 80 && u.health > 0);
                
                if (targetUnits.length > 0) {
                    // Calculate drop zone
                    let dropLat = 0, dropLng = 0;
                    const selectedUnits = targetUnits.slice(0, Math.min(5, targetUnits.length));
                    
                    selectedUnits.forEach(unit => {
                        dropLat += unit.lat;
                        dropLng += unit.lng;
                    });
                    dropLat /= selectedUnits.length;
                    dropLng /= selectedUnits.length;
                    
                    displayEventNotification(
                        'Supply Drop',
                        `${targetFaction.charAt(0).toUpperCase() + targetFaction.slice(1)} forces receiving supplies`
                    );
                    
                    // Visual effect for supply drop
                    const point = map.latLngToLayerPoint([dropLat, dropLng]);
                    
                    const supplyEl = document.createElement('div');
                    supplyEl.style.position = 'absolute';
                    supplyEl.style.left = point.x + 'px';
                    supplyEl.style.top = point.y + 'px';
                    supplyEl.style.fontSize = '30px';
                    supplyEl.style.zIndex = '700';
                    supplyEl.textContent = 'πŸ“¦';
                    supplyEl.style.animation = 'supply-drop 2s forwards';
                    
                    // Add custom animation if not already defined
                    if (!document.querySelector('#supply-drop-animation')) {
                        const style = document.createElement('style');
                        style.id = 'supply-drop-animation';
                        style.textContent = `
                            @keyframes supply-drop {
                                0% { transform: translateY(-50px); opacity: 0; }
                                80% { transform: translateY(0); opacity: 1; }
                                100% { transform: translateY(0); opacity: 0; }
                            }
                        `;
                        document.head.appendChild(style);
                    }
                    
                    document.querySelector('#map').appendChild(supplyEl);
                    
                    // Apply healing to nearby units
                    selectedUnits.forEach(unit => {
                        const dLat = unit.lat - dropLat;
                        const dLng = unit.lng - dropLng;
                        const distance = Math.sqrt(dLat * dLat + dLng * dLng);
                        
                        // Units near the drop get healed
                        if (distance < 0.008) {
                            const healAmount = 15 + Math.floor(Math.random() * 20);
                            unit.health = Math.min(100, unit.health + healAmount);
                            
                            // Update unit appearance
                            updateUnitMarkerAppearance(unit);
                            
                            // Show healing effect
                            if (unit.marker) {
                                const unitPoint = map.latLngToLayerPoint([unit.lat, unit.lng]);
                                
                                const healEl = document.createElement('div');
                                healEl.style.position = 'absolute';
                                healEl.style.left = unitPoint.x + 'px';
                                healEl.style.top = unitPoint.y + 'px';
                                healEl.style.fontSize = '14px';
                                healEl.style.fontWeight = 'bold';
                                healEl.style.color = '#2ecc71';
                                healEl.style.textShadow = '0 0 3px white';
                                healEl.style.zIndex = '1000';
                                healEl.style.transition = 'transform 1.5s, opacity 1.5s';
                                healEl.style.opacity = '1';
                                healEl.textContent = `+${healAmount}`;
                                
                                document.querySelector('#map').appendChild(healEl);
                                
                                setTimeout(() => {
                                    healEl.style.transform = 'translateY(-20px)';
                                    healEl.style.opacity = '0';
                                }, 100);
                                
                                setTimeout(() => {
                                    if (healEl.parentNode) {
                                        healEl.parentNode.removeChild(healEl);
                                    }
                                }, 1600);
                            }
                        }
                    });
                    
                    // Remove supply after animation
                    setTimeout(() => {
                        if (supplyEl.parentNode) {
                            supplyEl.parentNode.removeChild(supplyEl);
                        }
                    }, 2000);
                    
                    return true;
                }
                
                return false;
            }
        }
    ];
    
    // Randomly select an event based on probability
    const randomValue = Math.random();
    let cumulativeProbability = 0;
    
    for (const event of events) {
        cumulativeProbability += event.probability;
        if (randomValue < cumulativeProbability) {
            // Execute the selected event and check if it was successful
            if (event.handler()) {
                return event.name;
            }
        }
    }
    
    // If no event was triggered or all failed, default to a supply drop for a random faction
    const faction = Math.random() < 0.5 ? 'blue' : 'red';
    for (let i = 0; i < 2; i++) {
        spawnNewUnit(faction);
    }
    
    displayEventNotification(
        'Tactical Reinforcements',
        `${faction.charAt(0).toUpperCase() + faction.slice(1)} forces receiving light support`
    );
    
    return 'Tactical Reinforcements';
}

// Function to update unit type counts display
function updateUnitTypeCounts() {
    // Container for unit type statistics
    let unitCountsContainer = document.getElementById('unit-counts-container');
    
    if (!unitCountsContainer) {
        // Create container if it doesn't exist
        const statsContainer = document.querySelector('.statistics-container');
        if (statsContainer) {
            unitCountsContainer = document.createElement('div');
            unitCountsContainer.id = 'unit-counts-container';
            unitCountsContainer.style.marginTop = '15px';
            unitCountsContainer.style.padding = '10px';
            unitCountsContainer.style.borderTop = '1px solid #444';
            unitCountsContainer.style.display = 'flex';
            unitCountsContainer.style.flexWrap = 'wrap';
            unitCountsContainer.style.justifyContent = 'space-between';
            
            const title = document.createElement('div');
            title.style.width = '100%';
            title.style.fontWeight = 'bold';
            title.style.marginBottom = '5px';
            title.textContent = 'Active Unit Types:';
            unitCountsContainer.appendChild(title);
            
            statsContainer.appendChild(unitCountsContainer);
        }
    }
    
    if (unitCountsContainer) {
        // Clear previous counts (except the title)
        while (unitCountsContainer.childNodes.length > 1) {
            unitCountsContainer.removeChild(unitCountsContainer.lastChild);
        }
        
        // Get counts by unit type for each faction
        const blueUnitCounts = {};
        const redUnitCounts = {};
        
        units.forEach(unit => {
            if (unit.health <= 0) return; // Skip dead units
            
            if (unit.faction === 'blue') {
                blueUnitCounts[unit.type] = (blueUnitCounts[unit.type] || 0) + 1;
            } else {
                redUnitCounts[unit.type] = (redUnitCounts[unit.type] || 0) + 1;
            }
        });
        
        // Get all unit types currently in use
        const activeTypes = Object.keys({...blueUnitCounts, ...redUnitCounts})
            .filter(type => unitTypes[type].unlockDay <= dayCounter)
            .sort();
        
        // Create unit type count display
        activeTypes.forEach(type => {
            const blueCount = blueUnitCounts[type] || 0;
            const redCount = redUnitCounts[type] || 0;
            
            const unitTypeEl = document.createElement('div');
            unitTypeEl.style.display = 'flex';
            unitTypeEl.style.alignItems = 'center';
            unitTypeEl.style.margin = '3px 0';
            unitTypeEl.style.width = '48%';
            
            unitTypeEl.innerHTML = `
                <span style="font-size: 16px; margin-right: 5px;">${unitTypes[type].icon}</span>
                <span style="flex-grow: 1;">${getUnitName(type)}</span>
                <span style="color: #3498db; margin-right: 5px;">${blueCount}</span>
                <span style="color: #e74c3c;">${redCount}</span>
            `;
            
            unitCountsContainer.appendChild(unitTypeEl);
        });
    }
}

function updateBattleStatistics() {
    // Update UI statistics
    document.getElementById('blue-active').textContent = statistics.blueActive;
    document.getElementById('red-active').textContent = statistics.redActive;
    document.getElementById('blue-casualties').textContent = statistics.blueCasualties;
    document.getElementById('red-casualties').textContent = statistics.redCasualties;
    document.getElementById('blue-reinforcements').textContent = statistics.blueReinforcements;
    document.getElementById('red-reinforcements').textContent = statistics.redReinforcements;
    document.getElementById('active-battles').textContent = statistics.activeBattles;
    
    // Update territory control visualization
    document.getElementById('blue-control').style.width = `${statistics.blueControl}%`;
    document.getElementById('red-control').style.width = `${statistics.redControl}%`;
    
    // Update day counter or create it if it doesn't exist
    if (!document.getElementById('day-counter')) {
        const statsContainer = document.querySelector('.statistics-container');
        if (statsContainer) {
            const dayElement = document.createElement('div');
            dayElement.id = 'day-counter';
            dayElement.style.fontSize = '18px';
            dayElement.style.fontWeight = 'bold';
            dayElement.style.marginBottom = '10px';
            dayElement.style.textAlign = 'center';
            dayElement.style.color = '#FFD700'; // Gold color for day counter
            dayElement.textContent = `Day ${dayCounter}`;
            statsContainer.prepend(dayElement);
            
            // Add hour indicator
            const hourElement = document.createElement('span');
            hourElement.id = 'hour-indicator';
            hourElement.style.fontSize = '14px';
            hourElement.style.marginLeft = '10px';
            hourElement.style.color = '#AAAAAA';
            hourElement.textContent = `(Hour ${hourCounter})`;
            dayElement.appendChild(hourElement);
        }
    } else {
        document.getElementById('day-counter').textContent = `Day ${dayCounter}`;
        
        // Update hour indicator if it exists, or create it
        if (!document.getElementById('hour-indicator')) {
            const dayCounterElement = document.getElementById('day-counter');
            const hourElement = document.createElement('span');
            hourElement.id = 'hour-indicator';
            hourElement.style.fontSize = '14px';
            hourElement.style.marginLeft = '10px';
            hourElement.style.color = '#AAAAAA';
            hourElement.textContent = `(Hour ${hourCounter})`;
            dayCounterElement.appendChild(hourElement);
        } else {
            document.getElementById('hour-indicator').textContent = `(Hour ${hourCounter})`;
        }
    }
    
    // Update unit type counts
    updateUnitTypeCounts();
    
    // Randomize territory control slightly for visual effect
    let blueVariation = Math.random() * 5 - 2.5; // -2.5 to +2.5
    let newBlueControl = Math.min(Math.max(statistics.blueControl + blueVariation, 10), 90); // Keep between 10% and 90%
    
    statistics.blueControl = newBlueControl;
    statistics.redControl = 100 - newBlueControl;
}

function spawnReinforcements() {
    // Ensure minimum active forces for each side
    const minimumForceSize = 10 + Math.floor(dayCounter / 2); // Increases with time
    
    // Calculate the total active units
    const totalActive = statistics.blueActive + statistics.redActive;
    const maxTotalUnits = 60 + (dayCounter * 5) + Math.floor(Math.random() * 20); // More variance in unit cap
    
    // If we're under the maximum total, proceed with reinforcements
    if (totalActive < maxTotalUnits) {
        // Track reinforcements for notification
        let blueSpawned = 0;
        let redSpawned = 0;
        
        // Always spawn at least one unit for each faction that's below minimum strength
        if (statistics.blueActive < minimumForceSize) {
            spawnNewUnit('blue');
            blueSpawned++;
            statistics.blueReinforcements++;
        }
        
        if (statistics.redActive < minimumForceSize) {
            spawnNewUnit('red');
            redSpawned++;
            statistics.redReinforcements++;
        }
        
        // Additional random reinforcements (favor the losing side)
        let spawnBlueSide = Math.random() < (statistics.redActive / (statistics.blueActive + statistics.redActive));
        let faction = spawnBlueSide ? 'blue' : 'red';
        
        // Determine how many extra units to spawn (1-4, increasing with time)
        // Increased maximum from 3+1 to 5+1 for more variance
        const extraUnitCount = Math.floor(Math.random() * 5) + 1 + Math.floor(dayCounter / 10);
        
        // Generate units for the chosen faction
        for (let i = 0; i < extraUnitCount; i++) {
            // 50% chance to switch to the other faction for balance
            if (i > 0 && Math.random() < 0.5) {
                faction = faction === 'blue' ? 'red' : 'blue';
            }
            
            // 20% chance to use a specific unit type for variety
            if (Math.random() < 0.2) {
                // Get all available unit types
                const availableTypes = Object.entries(unitTypes)
                    .filter(([type, props]) => props.unlockDay <= dayCounter)
                    .map(([type]) => type);
                
                // Pick a random type
                const randomType = availableTypes[Math.floor(Math.random() * availableTypes.length)];
                spawnNewUnit(faction, randomType);
            } else {
                spawnNewUnit(faction);
            }
            
            // Count reinforcements
            if (faction === 'blue') {
                blueSpawned++;
                statistics.blueReinforcements++;
            } else {
                redSpawned++;
                statistics.redReinforcements++;
            }
        }
        
        // Display notification if significant reinforcements arrive
        if (blueSpawned >= 3) {
            displayEventNotification(
                'Blue Reinforcements',
                `${blueSpawned} Blue units have arrived as reinforcements`
            );
        }
        
        if (redSpawned >= 3) {
            displayEventNotification(
                'Red Reinforcements',
                `${redSpawned} Red units have arrived as reinforcements`
            );
        }
    }
    
    // Randomly vary some statistics to make the display more dynamic even if not much is happening
    // This creates the impression of ongoing battle even if units are repositioning
    
    // Randomize territory control slightly for visual effect
    const controlVariation = Math.random() * 4 - 2; // -2 to +2 percent
    statistics.blueControl = Math.min(Math.max(statistics.blueControl + controlVariation, 15), 85);
    statistics.redControl = 100 - statistics.blueControl;
    
    // Vary active battles count slightly
    const battleDelta = Math.random() < 0.3 ? (Math.random() < 0.5 ? 1 : -1) : 0;
    statistics.activeBattles = Math.max(1, statistics.activeBattles + battleDelta);
}

function spawnNewUnit(faction) {
    const type = getRandomUnitType();
    const unitId = statistics.nextUnitId++;
    
    // Calculate spawn location - at the edges of the map with some randomness
    let lat, lng;
    
    if (faction === 'blue') {
        // Blue forces spawn from the west
        lat = 40.7 + (Math.random() * 0.1 - 0.05);
        lng = -74.05 - (Math.random() * 0.02); // West of center
        statistics.blueActive++;
        statistics.blueReinforcements++;
    } else {
        // Red forces spawn from the east
        lat = 40.7 + (Math.random() * 0.1 - 0.05);
        lng = -73.95 + (Math.random() * 0.02); // East of center
        statistics.redActive++;
        statistics.redReinforcements++;
    }
    
    // Create the new unit
    const newUnit = {
        id: `${faction}-${unitId}`,
        name: `${faction.charAt(0).toUpperCase() + faction.slice(1)} Force ${getUnitName(type)} ${unitId}`,
        type: type,
        faction: faction,
        strength: Math.floor(Math.random() * 50) + 50, // 50-99
        health: 100,
        status: 'Reinforcement',
        lat: lat,
        lng: lng,
        targetLat: null,
        targetLng: null,
        marker: null,
        icon: unitTypes[type].icon,
        speed: unitTypes[type].speed,
        attackPower: unitTypes[type].attackPower,
        range: unitTypes[type].range,
        inBattle: false,
        movingToTarget: false,
        kills: 0,
        lastMoveTimestamp: Date.now()
    };
    
    units.push(newUnit);
    
    // Create marker for the new unit
    const markerSize = newUnit.strength / 15 + 20;
    
    const markerHtml = `
        <div class="unit-marker ${newUnit.faction}-unit" style="width: ${markerSize}px; height: ${markerSize}px; line-height: ${markerSize}px; font-size: ${markerSize/2}px;">
            ${newUnit.icon}
        </div>
    `;
    
    const customIcon = L.divIcon({
        html: markerHtml,
        className: '',
        iconSize: [markerSize, markerSize],
        iconAnchor: [markerSize/2, markerSize/2]
    });
    
    newUnit.marker = L.marker([newUnit.lat, newUnit.lng], { icon: customIcon })
        .addTo(map)
        .on('click', function() {
            selectUnit(newUnit);
        });
    
    // Immediately assign target to move toward
    assignTargetToUnit(newUnit);
}

function assignTargetToUnit(unit) {
    // Find a strategic position to move to
    const enemyFaction = unit.faction === 'blue' ? 'red' : 'blue';
    const enemies = units.filter(u => u.faction === enemyFaction && u.health > 0);
    
    if (enemies.length > 0) {
        // Choose random enemy as target
        const target = enemies[Math.floor(Math.random() * enemies.length)];
        unit.targetLat = target.lat;
        unit.targetLng = target.lng;
        unit.movingToTarget = true;
        unit.status = 'Advancing';
    } else {
        // Move toward center of map if no enemies
        unit.targetLat = 40.7;
        unit.targetLng = -74.0;
        unit.movingToTarget = true;
        unit.status = 'Advancing';
    }
}

function updateTime() {
    hourCounter++;
    if (hourCounter >= 24) {
        hourCounter = 0;
        dayCounter++;
        
        // Check for newly unlocked units on day change
        const newlyUnlockedTypes = Object.entries(unitTypes)
            .filter(([type, props]) => props.unlockDay === dayCounter)
            .map(([type]) => type);
            
        if (newlyUnlockedTypes.length > 0) {
            displayTechUnlockNotification(newlyUnlockedTypes);
        }
        
        // Add additional random reinforcements on new day
        const blueSurvivors = units.filter(u => u.faction === 'blue' && u.health > 0).length;
        const redSurvivors = units.filter(u => u.faction === 'red' && u.health > 0).length;
        
        // Add 1-3 reinforcements for each faction at day change
        const blueReinforcements = 1 + Math.floor(Math.random() * 3);
        const redReinforcements = 1 + Math.floor(Math.random() * 3);
        
        for (let i = 0; i < blueReinforcements; i++) {
            spawnNewUnit('blue');
        }
        
        for (let i = 0; i < redReinforcements; i++) {
            spawnNewUnit('red');
        }
        
        // Display day change notification
        displayEventNotification(
            `Day ${dayCounter} Begins`,
            'Forces are repositioning and receiving reinforcements'
        );
    }
    
    // Format hours with leading zero
    const formattedHours = hourCounter.toString().padStart(2, '0');
    
    // Make sure day-counter element exists before updating
    const dayCounter_el = document.getElementById('day-counter');
    if (dayCounter_el) {
        dayCounter_el.textContent = `Day ${dayCounter} - ${formattedHours}:00 hours`;
    }
}

// Display tech unlock notification
function displayTechUnlockNotification(unlockTypes) {
    // Create notification element
    const notification = document.createElement('div');
    notification.className = 'tech-unlock-notification';
    notification.style.position = 'absolute';
    notification.style.top = '50%';
    notification.style.left = '50%';
    notification.style.transform = 'translate(-50%, -50%)';
    notification.style.backgroundColor = 'rgba(0, 0, 0, 0.85)';
    notification.style.color = 'white';
    notification.style.padding = '20px';
    notification.style.borderRadius = '10px';
    notification.style.zIndex = '2000';
    notification.style.minWidth = '300px';
    notification.style.textAlign = 'center';
    notification.style.boxShadow = '0 0 30px gold';
    notification.style.border = '2px solid gold';
    
    // Create content
    let innerHTML = `
        <h2 style="margin: 0 0 15px 0; color: gold;">New Technology Unlocked!</h2>
        <div style="display: flex; flex-direction: column; gap: 10px;">
    `;
    
    unlockTypes.forEach(type => {
        innerHTML += `
            <div style="display: flex; align-items: center; justify-content: center; gap: 10px;">
                <span style="font-size: 24px;">${unitTypes[type].icon}</span>
                <span style="font-size: 18px;">${getUnitName(type)}</span>
            </div>
        `;
    });
    
    innerHTML += '</div>';
    notification.innerHTML = innerHTML;
    
    document.body.appendChild(notification);
    
    // Add animation CSS if not already defined
    if (!document.getElementById('notification-animations')) {
        const styleEl = document.createElement('style');
        styleEl.id = 'notification-animations';
        styleEl.textContent = `
            @keyframes fadeIn {
                from { opacity: 0; transform: translate(-50%, -60%); }
                to { opacity: 1; transform: translate(-50%, -50%); }
            }
            
            @keyframes fadeOut {
                from { opacity: 1; transform: translate(-50%, -50%); }
                to { opacity: 0; transform: translate(-50%, -40%); }
            }
            
            .tech-unlock-notification {
                animation: fadeIn 0.8s forwards;
            }
            
            .tech-unlock-notification.fadeout {
                animation: fadeOut 0.8s forwards;
            }
        `;
        document.head.appendChild(styleEl);
    }
    
    // Remove after delay
    setTimeout(() => {
        notification.classList.add('fadeout');
        
        setTimeout(() => {
            if (notification.parentNode) {
                notification.parentNode.removeChild(notification);
            }
        }, 800);
    }, 5000);
}

function togglePause() {
    isPaused = !isPaused;
    const pauseButton = document.getElementById('pause-button');
    pauseButton.textContent = isPaused ? 'Resume' : 'Pause';
}

function assignRandomTargets(chance = 0.3) {
    const activeFactions = ['blue', 'red'];
    
    // Randomly select units to assign new targets
    units.forEach(unit => {
        // Only assign new targets to units that aren't in battle and at random (default 30% chance)
        if (!unit.inBattle && Math.random() < chance) {
            // Find potential enemy targets
            const enemyFaction = unit.faction === 'blue' ? 'red' : 'blue';
            const enemies = units.filter(u => u.faction === enemyFaction && u.health > 0);
            
            if (enemies.length > 0) {
                // Choose random enemy as target - prefer weaker enemies occasionally
                let target;
                if (Math.random() < 0.2) {
                    // Sort by health ascending and pick from the weakest third
                    const sortedEnemies = [...enemies].sort((a, b) => a.health - b.health);
                    const weakEnemyIndex = Math.floor(Math.random() * Math.ceil(sortedEnemies.length / 3));
                    target = sortedEnemies[weakEnemyIndex];
                } else {
                    // Random target
                    target = enemies[Math.floor(Math.random() * enemies.length)];
                }
                
                unit.targetLat = target.lat;
                unit.targetLng = target.lng;
                unit.movingToTarget = true;
                
                // Occasionally give units different movement statuses for variety
                const statuses = ['Advancing', 'Flanking', 'Pursuing', 'Engaging'];
                unit.status = statuses[Math.floor(Math.random() * statuses.length)];
            }
        }
    });
    
    // Occasionally upgrade some units
    if (Math.random() < 0.1) {
        upgradeRandomUnit();
    }
}

function upgradeRandomUnit() {
    // Find a unit that's active and has been in battles
    const eligibleUnits = units.filter(u => u.health > 50 && u.kills > 0);
    
    if (eligibleUnits.length > 0) {
        const unit = eligibleUnits[Math.floor(Math.random() * eligibleUnits.length)];
        
        // Boost its stats
        unit.attackPower = Math.min(100, unit.attackPower + 5);
        unit.range = Math.min(0.05, unit.range + 0.002);
        unit.speed = Math.min(0.005, unit.speed * 1.1);
        
        // Visual upgrade - make it slightly larger and add a star if it's reached elite status
        if (unit.marker) {
            const markerSize = unit.strength / 15 + 20 + (unit.kills * 0.5);
            
            // Add a visual indicator for veteran/elite units
            let eliteIndicator = '';
            if (unit.kills >= 10) {
                eliteIndicator = '⭐';
            } else if (unit.kills >= 5) {
                eliteIndicator = 'β˜…';
            }
            
            const markerHtml = `
                <div class="unit-marker ${unit.faction}-unit" style="width: ${markerSize}px; height: ${markerSize}px; line-height: ${markerSize}px; font-size: ${markerSize/2}px;">
                    ${unit.icon}${eliteIndicator}
                </div>
            `;
            
            const customIcon = L.divIcon({
                html: markerHtml,
                className: '',
                iconSize: [markerSize, markerSize],
                iconAnchor: [markerSize/2, markerSize/2]
            });
            
            unit.marker.setIcon(customIcon);
        }
    }
}

function moveUnits() {
    const now = Date.now();
    
    units.forEach(unit => {
        // Only move units that are alive and have a target
        if (unit.health > 0 && unit.movingToTarget && unit.targetLat && unit.targetLng) {
            const timeDelta = (now - unit.lastMoveTimestamp) / 1000; // Time in seconds since last move
            unit.lastMoveTimestamp = now;
            
            // Calculate direction vector
            const dLat = unit.targetLat - unit.lat;
            const dLng = unit.targetLng - unit.lng;
            
            // Calculate distance to target
            const distance = Math.sqrt(dLat * dLat + dLng * dLng);
            
            // If we've reached the target (or close enough)
            if (distance < 0.0005) {
                unit.movingToTarget = false;
                unit.status = 'Operational';
                return;
            }
            
            // Normalize direction vector
            const magnitude = Math.sqrt(dLat * dLat + dLng * dLng);
            const dirLat = dLat / magnitude;
            const dirLng = dLng / magnitude;
            
            // Move unit towards target with speed influenced by unit type
            const moveDistance = unit.speed * timeDelta;
            unit.lat += dirLat * moveDistance;
            unit.lng += dirLng * moveDistance;
            
            // Update marker position
            if (unit.marker) {
                unit.marker.setLatLng([unit.lat, unit.lng]);
            }
        }
    });
}

function checkForBattles() {
    // Clear any existing battle animations that are outdated
    document.querySelectorAll('.battle-indicator').forEach(el => {
        // Only remove indicators that have been around for more than 3 seconds
        if (el.dataset.timestamp && (Date.now() - parseInt(el.dataset.timestamp)) > 3000) {
            el.remove();
        }
    });
    
    // Reset active battles counter
    statistics.activeBattles = 0;
    
    // Create a set to track unique battle pairs
    const battlePairs = new Set();
    
    // Check each unit against potential enemies
    units.forEach(unit => {
        if (unit.health <= 0) return; // Skip dead units
        
        const enemyFaction = unit.faction === 'blue' ? 'red' : 'blue';
        const enemies = units.filter(u => u.faction === enemyFaction && u.health > 0);
        
        // Reset battle state
        const wasInBattle = unit.inBattle;
        unit.inBattle = false;
        
        enemies.forEach(enemy => {
            // Calculate distance between units
            const dLat = unit.lat - enemy.lat;
            const dLng = unit.lng - enemy.lng;
            const distance = Math.sqrt(dLat * dLat + dLng * dLng);
            
            // If within range, start battle
            if (distance < unit.range) {
                // Mark both units as in battle
                unit.inBattle = true;
                enemy.inBattle = true;
                
                // Create a unique identifier for this battle pair
                const battleId = [unit.id, enemy.id].sort().join('-');
                
                // Only count unique battles
                if (!battlePairs.has(battleId)) {
                    battlePairs.add(battleId);
                    statistics.activeBattles++;
                    
                    // Create battle animation at midpoint with slight randomness
                    const battleLat = (unit.lat + enemy.lat) / 2 + (Math.random() * 0.002 - 0.001);
                    const battleLng = (unit.lng + enemy.lng) / 2 + (Math.random() * 0.002 - 0.001);
                    
                    // More frequent battle animations for increased visual impact
                    // but avoid too many by only creating on every other check or for elite units
                    if (Math.random() < 0.7 || unit.kills >= 3 || enemy.kills >= 3) {
                        createBattleAnimation(battleLat, battleLng);
                    }
                }
                
                // More varied battle statuses
                const battleStatuses = ['Engaged', 'In Combat', 'Fighting', 'Attacking', 'Defending'];
                unit.status = battleStatuses[Math.floor(Math.random() * battleStatuses.length)];
                enemy.status = battleStatuses[Math.floor(Math.random() * battleStatuses.length)];
                
                // More dynamic damage calculation
                // Base damage
                let unitBaseDamage = unit.attackPower * 0.15 * (0.5 + Math.random());
                let enemyBaseDamage = enemy.attackPower * 0.15 * (0.5 + Math.random());
                
                // Modify damage based on unit kills/experience (veteran bonus)
                unitBaseDamage *= (1 + (unit.kills * 0.05));
                enemyBaseDamage *= (1 + (enemy.kills * 0.05));
                
                // Health-based modifications (wounded units deal less damage)
                unitBaseDamage *= (0.5 + (unit.health / 200));
                enemyBaseDamage *= (0.5 + (enemy.health / 200));
                
                // Final randomization and rounding
                const unitDamage = Math.round(unitBaseDamage * (0.8 + Math.random() * 0.4));
                const enemyDamage = Math.round(enemyBaseDamage * (0.8 + Math.random() * 0.4));
                
                // Apply damage
                enemy.health = Math.max(0, enemy.health - unitDamage);
                unit.health = Math.max(0, unit.health - enemyDamage);
                
                // Update marker appearance based on health
                updateUnitMarkerAppearance(unit);
                updateUnitMarkerAppearance(enemy);
                
                // Critical hits for more drama (rare but powerful)
                if (Math.random() < 0.05) {
                    const criticalTarget = Math.random() < 0.5 ? unit : enemy;
                    const criticalDamage = Math.round(criticalTarget.health * 0.3);
                    criticalTarget.health = Math.max(0, criticalTarget.health - criticalDamage);
                    
                    // Visual indication of critical hit
                    if (criticalTarget.marker) {
                        const point = map.latLngToLayerPoint([criticalTarget.lat, criticalTarget.lng]);
                        
                        const criticalEl = document.createElement('div');
                        criticalEl.style.position = 'absolute';
                        criticalEl.style.left = point.x + 'px';
                        criticalEl.style.top = point.y + 'px';
                        criticalEl.style.fontSize = '16px';
                        criticalEl.style.fontWeight = 'bold';
                        criticalEl.style.color = '#ff0000';
                        criticalEl.style.textShadow = '0 0 3px #ffffff';
                        criticalEl.style.zIndex = '1000';
                        criticalEl.style.transition = 'transform 1s, opacity 1s';
                        criticalEl.style.opacity = '1';
                        criticalEl.textContent = "CRITICAL!";
                        
                        document.querySelector('#map').appendChild(criticalEl);
                        
                        setTimeout(() => {
                            criticalEl.style.transform = 'translateY(-20px) scale(1.5)';
                            criticalEl.style.opacity = '0';
                        }, 100);
                        
                        setTimeout(() => {
                            if (criticalEl.parentNode) {
                                criticalEl.parentNode.removeChild(criticalEl);
                            }
                        }, 1100);
                    }
                }
                
                // Check if unit defeated enemy
                if (enemy.health <= 0 && unitDamage > 0) {
                    unit.kills++;
                    checkUnitDeath(enemy);
                    // Victory effect
                    if (unit.marker) {
                        const point = map.latLngToLayerPoint([unit.lat, unit.lng]);
                        showVictoryEffect(point.x, point.y, unit.faction);
                    }
                }
                
                // Check if enemy defeated unit
                if (unit.health <= 0 && enemyDamage > 0) {
                    enemy.kills++;
                    checkUnitDeath(unit);
                    // Victory effect
                    if (enemy.marker) {
                        const point = map.latLngToLayerPoint([enemy.lat, enemy.lng]);
                        showVictoryEffect(point.x, point.y, enemy.faction);
                    }
                    return; // Stop checking more enemies if this unit is dead
                }
            }
        });
        
        // If unit was in battle but no longer is, potentially assign new target
        if (wasInBattle && !unit.inBattle) {
            // Higher chance to pursue new targets for experienced units
            const reassignChance = 0.3 + (unit.kills * 0.05);
            if (Math.random() < reassignChance) {
                assignTargetToUnit(unit);
            }
        }
    });
}

function showVictoryEffect(x, y, faction) {
    const victoryEl = document.createElement('div');
    victoryEl.style.position = 'absolute';
    victoryEl.style.left = x + 'px';
    victoryEl.style.top = y + 'px';
    victoryEl.style.fontSize = '14px';
    victoryEl.style.fontWeight = 'bold';
    victoryEl.style.color = faction === 'blue' ? '#3498db' : '#e74c3c';
    victoryEl.style.textShadow = '0 0 3px white';
    victoryEl.style.zIndex = '1000';
    victoryEl.style.transition = 'transform 1.5s, opacity 1.5s';
    victoryEl.style.opacity = '1';
    victoryEl.textContent = "Victory!";
    
    document.querySelector('#map').appendChild(victoryEl);
    
    setTimeout(() => {
        victoryEl.style.transform = 'translateY(-25px) scale(1.2)';
        victoryEl.style.opacity = '0';
    }, 100);
    
    setTimeout(() => {
        if (victoryEl.parentNode) {
            victoryEl.parentNode.removeChild(victoryEl);
        }
    }, 1600);
}

function updateUnitMarkerAppearance(unit) {
    if (!unit.marker || unit.health <= 0) return;
    
    const markerSize = unit.strength / 15 + 20 + (unit.kills * 0.5);
    
    // Visual indicator for health status
    let healthIndicator = '';
    let glowColor = '';
    
    if (unit.health < 30) {
        healthIndicator = '🩸'; // Injured
        glowColor = 'rgba(255, 0, 0, 0.5)';
    } else if (unit.inBattle) {
        healthIndicator = 'βš”οΈ'; // In battle
        glowColor = 'rgba(255, 165, 0, 0.5)';
    }
    
    // Add a visual indicator for veteran/elite units
    let eliteIndicator = '';
    if (unit.kills >= 10) {
        eliteIndicator = '⭐';
    } else if (unit.kills >= 5) {
        eliteIndicator = 'β˜…';
    }
    
    const markerHtml = `
        <div class="unit-marker ${unit.faction}-unit"
             style="width: ${markerSize}px; height: ${markerSize}px;
                   line-height: ${markerSize}px;
                   font-size: ${markerSize/2}px;
                   box-shadow: 0 0 8px ${glowColor};">
            ${unit.icon}${healthIndicator}${eliteIndicator}
        </div>
    `;
    
    const customIcon = L.divIcon({
        html: markerHtml,
        className: '',
        iconSize: [markerSize, markerSize],
        iconAnchor: [markerSize/2, markerSize/2]
    });
    
    unit.marker.setIcon(customIcon);
}

function createBattleAnimation(lat, lng) {
    // Convert lat/lng to pixel coordinates
    const point = map.latLngToLayerPoint([lat, lng]);
    
    // Create multiple battle indicators for more intense visuals
    const indicatorCount = 1 + Math.floor(Math.random() * 3); // 1-3 indicators per battle
    
    for (let i = 0; i < indicatorCount; i++) {
        // Add slight position variance for multiple indicators
        const offsetX = Math.random() * 30 - 15;
        const offsetY = Math.random() * 30 - 15;
        
        // Choose a random battle animation type to add variety
        const animationTypes = ['flash', 'expand', 'pulse', 'burst'];
        const battleType = animationTypes[Math.floor(Math.random() * animationTypes.length)];
        
        // Randomly vary the size and color of battle indicators
        const size = 15 + Math.floor(Math.random() * 20); // 15-35px
        
        // More varied colors including faction colors
        const baseColors = ['#ffcc00', '#ff9900', '#ff3300', '#cc0000', '#3498db', '#e74c3c'];
        const color = baseColors[Math.floor(Math.random() * baseColors.length)];
        
        // Create battle indicator element with staggered timing
        setTimeout(() => {
            const battleEl = document.createElement('div');
            battleEl.className = `battle-indicator battle-${battleType}`;
            battleEl.style.left = (point.x + offsetX) + 'px';
            battleEl.style.top = (point.y + offsetY) + 'px';
            battleEl.style.width = size + 'px';
            battleEl.style.height = size + 'px';
            battleEl.style.backgroundColor = color;
            
            // Add to map container
            document.querySelector('#map').appendChild(battleEl);
            
            // Remove element after animation completes
            setTimeout(() => {
                if (battleEl.parentNode) {
                    battleEl.parentNode.removeChild(battleEl);
                }
            }, 2000 + Math.random() * 1000);
        }, i * 150); // Staggered start times
    }
    
    // Add combat symbols with more variety and quantity
    const symbolCount = Math.floor(Math.random() * 4) + 1; // 1-4 symbols
    
    for (let i = 0; i < symbolCount; i++) {
        setTimeout(() => {
            // Expanded symbol set with more variety
            const symbols = ['πŸ’₯', 'πŸ”₯', 'βš”οΈ', 'πŸ’£', '🧨', 'πŸ’€', '⚑', 'πŸ›‘οΈ', '🏹', 'πŸ—‘οΈ', 'πŸ”«'];
            const symbol = symbols[Math.floor(Math.random() * symbols.length)];
            
            const symbolEl = document.createElement('div');
            symbolEl.style.position = 'absolute';
            symbolEl.style.left = (point.x + Math.random() * 40 - 20) + 'px';
            symbolEl.style.top = (point.y + Math.random() * 40 - 20) + 'px';
            symbolEl.style.fontSize = (12 + Math.random() * 16) + 'px';
            symbolEl.style.zIndex = '1000';
            symbolEl.style.transition = 'transform 1.5s, opacity 1.5s';
            symbolEl.style.transform = 'translateY(0) rotate(0deg)';
            symbolEl.style.opacity = '1';
            symbolEl.textContent = symbol;
            
            document.querySelector('#map').appendChild(symbolEl);
            
            // Random movement direction
            const dirX = Math.random() * 30 - 15;
            const dirY = -10 - Math.random() * 20; // Always move up somewhat
            const rotation = Math.random() * 360;
            
            setTimeout(() => {
                symbolEl.style.transform = `translate(${dirX}px, ${dirY}px) rotate(${rotation}deg)`;
                symbolEl.style.opacity = '0';
            }, 100);
            
            setTimeout(() => {
                if (symbolEl.parentNode) {
                    symbolEl.parentNode.removeChild(symbolEl);
                }
            }, 1600);
        }, i * 200); // Staggered symbols
    }
    
    // Occasionally add damage numbers for more battle feedback
    if (Math.random() < 0.4) {
        setTimeout(() => {
            const damageEl = document.createElement('div');
            damageEl.style.position = 'absolute';
            damageEl.style.left = (point.x + Math.random() * 20 - 10) + 'px';
            damageEl.style.top = (point.y + Math.random() * 20 - 10) + 'px';
            damageEl.style.fontSize = '14px';
            damageEl.style.fontWeight = 'bold';
            damageEl.style.color = 'red';
            damageEl.style.textShadow = '0 0 3px white';
            damageEl.style.zIndex = '1000';
            damageEl.style.transition = 'transform 1.5s, opacity 1.5s';
            damageEl.style.opacity = '1';
            
            // Generate a random damage number
            const damage = Math.floor(Math.random() * 20) + 5;
            damageEl.textContent = `-${damage}`;
            
            document.querySelector('#map').appendChild(damageEl);
            
            setTimeout(() => {
                damageEl.style.transform = 'translateY(-20px)';
                damageEl.style.opacity = '0';
            }, 100);
            
            setTimeout(() => {
                if (damageEl.parentNode) {
                    damageEl.parentNode.removeChild(damageEl);
                }
            }, 1600);
        }, Math.random() * 500);
    }
}

function checkUnitDeath(unit) {
    if (unit.health <= 0) {
        // Update the unit's appearance to show it's destroyed
        if (unit.marker) {
            // Change the marker to show a destroyed unit (smaller and gray)
            const markerSize = 15; // Smaller size for destroyed units
            
            const markerHtml = `
                <div class="unit-marker ${unit.faction}-unit" style="width: ${markerSize}px; height: ${markerSize}px; line-height: ${markerSize}px; font-size: ${markerSize/2}px; opacity: 0.5; background-color: #888;">
                    ☠️
                </div>
            `;
            
            const destroyedIcon = L.divIcon({
                html: markerHtml,
                className: '',
                iconSize: [markerSize, markerSize],
                iconAnchor: [markerSize/2, markerSize/2]
            });
            
            unit.marker.setIcon(destroyedIcon);
        }
        
        // Update unit status
        unit.status = 'Destroyed';
        unit.movingToTarget = false;
        unit.inBattle = false;
        
        // Update statistics
        if (unit.faction === 'blue') {
            statistics.blueActive--;
            statistics.blueCasualties++;
            
            // Adjust territory control slightly toward red
            statistics.redControl = Math.min(statistics.redControl + 2, 90);
            statistics.blueControl = 100 - statistics.redControl;
        } else {
            statistics.redActive--;
            statistics.redCasualties++;
            
            // Adjust territory control slightly toward blue
            statistics.blueControl = Math.min(statistics.blueControl + 2, 90);
            statistics.redControl = 100 - statistics.blueControl;
        }
        
        // After a delay, remove the unit marker from the map
        setTimeout(() => {
            if (unit.marker) {
                // Fade out effect (optional)
                const icon = unit.marker.options.icon;
                const html = icon.options.html.replace('opacity: 0.5', 'opacity: 0.2');
                
                const fadedIcon = L.divIcon({
                    html: html,
                    className: icon.options.className,
                    iconSize: icon.options.iconSize,
                    iconAnchor: icon.options.iconAnchor
                });
                
                unit.marker.setIcon(fadedIcon);
                
                // After another longer delay, units will disappear completely
                // This creates a nice visual effect of "cleaning up" the battlefield
                // Left commented out to keep destroyed units visible on the map
                /*
                setTimeout(() => {
                    map.removeLayer(unit.marker);
                    unit.marker = null;
                }, 30000);
                */
            }
        }, 5000);
    }
}