File size: 108,103 Bytes
417c5c4
a84ab49
c20f696
 
 
 
 
 
 
 
5f366e7
417c5c4
5f366e7
 
 
417c5c4
 
4159427
 
417c5c4
c6d658a
655b3a1
417c5c4
ef12d63
0e33469
517db11
417c5c4
 
517db11
c6d658a
c20f696
 
 
 
 
 
 
 
417c5c4
 
 
a97ec38
c6d658a
 
 
 
 
 
 
850394e
417c5c4
2e2a4b0
bde5aa3
 
 
c20f696
ef12d63
 
 
417c5c4
2e2a4b0
dd35bcb
 
 
 
 
 
 
 
 
 
417c5c4
517db11
417c5c4
 
cc9b10b
 
 
417c5c4
 
cc9b10b
2e2a4b0
bc9985a
c48f9b9
 
daefea6
 
c48f9b9
 
432e3dc
c48f9b9
 
 
 
 
 
 
 
daefea6
2e2a4b0
cc9b10b
 
2e2a4b0
cc9b10b
2e2a4b0
 
cc9b10b
2e2a4b0
432e3dc
 
 
 
cc9b10b
2e2a4b0
cc9b10b
 
 
 
 
2e2a4b0
cc9b10b
bc9985a
 
 
feef8c6
bc9985a
 
 
c48f9b9
2e2a4b0
c48f9b9
2e2a4b0
feef8c6
c48f9b9
 
 
 
 
feef8c6
 
 
 
c48f9b9
feef8c6
 
daefea6
feef8c6
2e2a4b0
43339a7
 
c48f9b9
43339a7
 
 
 
 
517db11
417c5c4
cc9b10b
 
 
417c5c4
 
cc9b10b
517db11
417c5c4
cc9b10b
 
 
417c5c4
 
cc9b10b
517db11
cc9b10b
 
 
417c5c4
 
 
 
 
517db11
417c5c4
cc9b10b
 
517db11
417c5c4
cc9b10b
 
 
 
417c5c4
 
cc9b10b
517db11
417c5c4
cc9b10b
 
 
 
417c5c4
 
cc9b10b
517db11
417c5c4
cc9b10b
 
 
 
517db11
417c5c4
 
517db11
54a6f85
cc9b10b
 
 
 
417c5c4
 
cc9b10b
54a6f85
cc9b10b
 
 
 
 
 
 
54a6f85
 
 
 
 
417c5c4
517db11
417c5c4
 
cc9b10b
 
 
2e2a4b0
cc9b10b
 
417c5c4
 
cc9b10b
517db11
cc9b10b
 
417c5c4
 
cc9b10b
517db11
cc9b10b
 
417c5c4
 
cc9b10b
517db11
417c5c4
cc9b10b
 
 
2e2a4b0
cc9b10b
 
417c5c4
 
cc9b10b
517db11
cc9b10b
 
417c5c4
 
cc9b10b
517db11
cc9b10b
 
417c5c4
 
cc9b10b
517db11
417c5c4
 
 
 
 
 
 
 
850394e
 
 
 
 
cc9b10b
 
 
2e2a4b0
cc9b10b
 
 
 
850394e
 
 
 
 
 
 
 
 
cc9b10b
850394e
e8cda17
 
 
 
976746f
e8cda17
976746f
e8cda17
 
417c5c4
517db11
417c5c4
 
 
 
 
 
 
 
2380390
417c5c4
 
 
 
 
 
 
517db11
417c5c4
 
517db11
417c5c4
 
c6d658a
417c5c4
 
517db11
417c5c4
 
2380390
417c5c4
 
 
 
 
517db11
417c5c4
 
517db11
417c5c4
 
517db11
417c5c4
 
 
517db11
417c5c4
 
 
517db11
850394e
c6d658a
 
 
 
850394e
 
 
 
2380390
850394e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01327bf
417c5c4
 
c93381f
 
 
417c5c4
c93381f
417c5c4
 
 
 
 
c93381f
 
 
 
 
 
 
 
 
417c5c4
c93381f
417c5c4
 
517db11
417c5c4
 
 
 
517db11
bde5aa3
 
 
 
 
 
 
 
 
 
 
417c5c4
 
 
 
 
517db11
8dcacdf
417c5c4
 
517db11
417c5c4
 
 
 
 
 
 
8dcacdf
417c5c4
517db11
417c5c4
 
517db11
417c5c4
 
 
 
517db11
417c5c4
 
 
517db11
417c5c4
 
 
3058d07
c20f696
 
3058d07
 
 
 
 
 
2e2a4b0
3058d07
c20f696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
a84ab49
 
 
cc9b10b
 
2e2a4b0
d1c32da
 
2e2a4b0
c20f696
 
 
d1c32da
 
 
 
2e2a4b0
d1c32da
2e2a4b0
d1c32da
 
2e2a4b0
d1c32da
 
 
 
2e2a4b0
d1c32da
 
 
 
 
 
2e2a4b0
d1c32da
 
 
 
 
 
 
 
 
 
 
 
 
 
7fbfb89
d1c32da
 
7fbfb89
d1c32da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
d1c32da
 
 
c20f696
 
 
 
2e2a4b0
c20f696
 
 
 
3058d07
 
 
 
 
 
 
 
 
 
a84ab49
3058d07
 
 
a84ab49
3058d07
 
a84ab49
3058d07
a84ab49
 
 
3058d07
 
 
 
 
a84ab49
 
 
 
 
 
3058d07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c20f696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3058d07
c20f696
3058d07
 
 
 
 
c20f696
3058d07
 
 
c20f696
3058d07
 
 
c20f696
3058d07
 
 
 
 
c20f696
 
 
 
 
 
3058d07
c20f696
 
7fbfb89
 
 
 
c20f696
 
3058d07
 
 
 
c20f696
3058d07
 
c20f696
 
3058d07
 
 
 
 
c20f696
 
a84ab49
 
 
 
c20f696
 
 
 
 
3058d07
 
 
c20f696
 
 
 
 
 
 
 
 
0486a65
 
2e2a4b0
59db690
 
0486a65
2e2a4b0
59db690
 
2e2a4b0
0486a65
 
59db690
0486a65
 
 
59db690
0486a65
 
 
 
 
 
 
 
 
59db690
0486a65
8c95fa2
 
59db690
 
0486a65
 
2e2a4b0
0486a65
 
 
 
3058d07
c20f696
 
3058d07
0486a65
3058d07
 
 
c20f696
 
 
 
 
 
 
3058d07
 
c20f696
3058d07
 
883d505
c20f696
 
2e2a4b0
883d505
 
 
2e2a4b0
883d505
 
2380390
883d505
 
 
2e2a4b0
883d505
 
 
2e2a4b0
883d505
 
2e2a4b0
883d505
 
 
3058d07
883d505
 
 
 
2e2a4b0
883d505
 
 
2e2a4b0
883d505
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c20f696
3058d07
2e2a4b0
883d505
2e2a4b0
883d505
 
 
3058d07
c20f696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3058d07
 
 
 
 
 
 
 
 
 
 
 
c20f696
 
 
 
 
 
 
 
 
 
 
d3e0bda
c20f696
d3e0bda
4624a2d
d3e0bda
 
7fbfb89
d1c32da
 
 
 
 
7fbfb89
d1c32da
d3e0bda
 
d1c32da
d3e0bda
 
 
 
 
 
 
 
 
 
 
 
c20f696
 
 
d3e0bda
 
 
 
 
 
 
7fbfb89
d3e0bda
 
7fbfb89
d3e0bda
 
 
 
 
 
 
 
 
4624a2d
d3e0bda
 
 
 
 
 
 
7fbfb89
d3e0bda
 
 
 
 
 
 
c20f696
 
 
 
 
 
d3e0bda
c20f696
 
 
2e2a4b0
d3e0bda
 
2e2a4b0
c20f696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3058d07
c20f696
d1c32da
 
 
 
c20f696
 
 
 
 
d3e0bda
c20f696
 
 
d1c32da
 
7fbfb89
2e2a4b0
d3e0bda
2e2a4b0
 
 
7fbfb89
d1c32da
7fbfb89
2e2a4b0
 
 
 
7fbfb89
d1c32da
d3e0bda
2e2a4b0
2591af6
7fbfb89
2591af6
 
 
 
 
d3e0bda
c20f696
d3e0bda
c20f696
 
 
 
 
59db690
c20f696
 
 
 
 
 
 
 
 
 
59db690
7fbfb89
59db690
7fbfb89
 
 
 
 
59db690
 
 
 
2380390
59db690
 
 
c20f696
59db690
7fbfb89
2591af6
 
 
7fbfb89
2591af6
2e2a4b0
59db690
4624a2d
59db690
 
 
 
2e2a4b0
2591af6
59db690
2591af6
2e2a4b0
7fbfb89
 
2380390
 
 
 
 
7fbfb89
2380390
7fbfb89
2591af6
2380390
59db690
2591af6
59db690
2591af6
2380390
7fbfb89
 
2380390
59db690
2591af6
839c732
2380390
 
 
 
c20f696
 
 
 
 
 
2e2a4b0
d3e0bda
 
 
1042c44
 
7fbfb89
1042c44
 
d3e0bda
 
 
 
 
 
 
 
 
 
7fbfb89
d3e0bda
 
 
7fbfb89
 
d3e0bda
 
 
 
 
 
 
 
 
 
 
 
a97ec38
d3e0bda
a97ec38
 
d3e0bda
 
 
c0a9538
 
a97ec38
c20f696
540bab9
c20f696
 
6859b92
 
2e2a4b0
c20f696
a97ec38
 
84e8d07
a97ec38
 
7fbfb89
a97ec38
fd44f08
a97ec38
 
fd44f08
a97ec38
c20f696
3058d07
2e2a4b0
6859b92
 
 
2e2a4b0
c20f696
 
3058d07
 
 
c20f696
 
 
 
 
6859b92
f05b79e
 
 
 
 
 
6859b92
 
 
2e2a4b0
6859b92
 
fd44f08
6859b92
fd44f08
6859b92
 
 
2e2a4b0
6859b92
 
 
 
 
c20f696
 
 
 
c1a2741
 
2e2a4b0
c1a2741
 
 
 
 
2e2a4b0
540bab9
 
c1a2741
7fbfb89
c1a2741
 
 
 
7fbfb89
 
c1a2741
7fbfb89
c1a2741
7fbfb89
 
c1a2741
 
 
 
 
 
 
2e2a4b0
c1a2741
 
 
c20f696
 
 
 
2e2a4b0
0486a65
 
 
 
2e2a4b0
0486a65
 
 
 
 
 
 
 
 
c20f696
0486a65
 
c20f696
 
2e2a4b0
0486a65
 
 
2380390
0486a65
 
7fbfb89
2e2a4b0
0486a65
 
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
0486a65
 
7fbfb89
0486a65
 
 
7fbfb89
0486a65
 
2e2a4b0
0486a65
 
 
 
2e2a4b0
c20f696
 
 
 
beed762
 
 
 
c20f696
0486a65
 
 
 
2e2a4b0
0486a65
 
 
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
0486a65
 
7fbfb89
59db690
 
 
 
0486a65
7fbfb89
0486a65
7fbfb89
 
0486a65
 
 
2e2a4b0
0486a65
 
 
 
 
59db690
0486a65
2e2a4b0
0486a65
 
 
 
 
 
2e2a4b0
0486a65
 
c20f696
59db690
c20f696
0486a65
8c95fa2
 
59db690
 
0486a65
c20f696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fbfb89
c20f696
7fbfb89
c20f696
7fbfb89
c20f696
7fbfb89
c20f696
 
 
 
 
 
 
 
 
 
 
 
 
 
7fbfb89
a84ab49
cc9b10b
c20f696
2e2a4b0
d3e0bda
c20f696
 
7fbfb89
d3e0bda
c0a9538
d3e0bda
c20f696
2e2a4b0
d3e0bda
 
2e2a4b0
d3e0bda
 
 
 
2e2a4b0
 
c20f696
c0a9538
a97ec38
 
c20f696
 
 
 
 
 
 
c1a2741
 
c20f696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
c20f696
 
 
 
 
84e8d07
c20f696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
c20f696
 
 
 
2e2a4b0
c20f696
 
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
c20f696
 
 
0e33469
 
 
 
 
 
 
2e2a4b0
0e33469
 
e201c8f
 
0e33469
 
 
 
 
 
 
 
c20f696
 
 
0486a65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c20f696
 
 
 
 
 
 
 
 
 
 
 
 
 
2380390
 
c20f696
2e2a4b0
0486a65
 
 
 
 
c20f696
 
 
 
 
 
 
 
 
 
 
 
 
 
aa43da8
 
 
 
 
 
 
 
 
 
 
7fbfb89
2380390
 
 
 
 
7fbfb89
c20f696
 
7fbfb89
c20f696
7fbfb89
 
 
2380390
7fbfb89
c20f696
 
 
 
 
 
 
 
 
2380390
 
 
c20f696
 
 
aa43da8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c20f696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c95fa2
 
 
2e2a4b0
8c95fa2
2e2a4b0
 
8c95fa2
 
 
 
 
 
c20f696
 
 
 
 
 
 
 
 
 
6859b92
c20f696
 
 
 
 
 
 
 
 
 
 
 
3058d07
 
c20f696
 
 
 
 
 
 
 
7fbfb89
ba5b57f
7fbfb89
ba5b57f
fc4a3e4
ba5b57f
 
 
7fbfb89
c20f696
 
 
 
 
ba5b57f
 
c20f696
 
 
 
 
 
 
c93381f
eda18c9
 
c93381f
 
 
 
 
 
eda18c9
1c36432
 
 
 
eda18c9
c20f696
eda18c9
c20f696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba5b57f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fbfb89
 
 
 
 
 
 
 
 
 
ba5b57f
7fbfb89
ba5b57f
 
7fbfb89
 
ba5b57f
 
 
 
 
7fbfb89
ba5b57f
 
 
 
 
 
 
 
 
 
7fbfb89
ba5b57f
 
 
fc4a3e4
 
 
ba5b57f
 
 
 
7fbfb89
ba5b57f
 
 
 
 
 
7fbfb89
ba5b57f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fbfb89
ba5b57f
 
7fbfb89
ba5b57f
 
7fbfb89
ba5b57f
 
 
 
 
 
417c5c4
c20f696
 
2e2a4b0
c20f696
417c5c4
3058d07
 
 
 
 
417c5c4
 
 
 
 
 
 
2657f24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c20f696
 
5f366e7
 
 
 
 
 
 
 
 
 
 
 
e201c8f
 
 
 
5f366e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e201c8f
 
 
 
 
 
 
 
5f366e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e201c8f
5f366e7
2e2a4b0
e201c8f
 
2e2a4b0
e201c8f
 
2e2a4b0
5f366e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
e201c8f
 
 
2e2a4b0
5f366e7
 
 
 
 
 
 
e201c8f
 
 
 
 
 
 
2e2a4b0
e201c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
e201c8f
 
 
2e2a4b0
e201c8f
 
 
2e2a4b0
e201c8f
 
2e2a4b0
e201c8f
 
 
 
 
2e2a4b0
e201c8f
 
 
 
 
 
 
 
2e2a4b0
e201c8f
 
2e2a4b0
e201c8f
 
 
 
 
 
 
 
 
 
 
2e2a4b0
e201c8f
 
 
 
 
 
 
 
 
 
 
 
 
5f366e7
 
 
 
 
 
 
 
 
2e2a4b0
5f366e7
e201c8f
2e2a4b0
5f366e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
5f366e7
 
 
 
2e2a4b0
e201c8f
 
 
 
2e2a4b0
e201c8f
2e2a4b0
5f366e7
 
 
 
 
 
 
 
 
 
e201c8f
5f366e7
 
 
 
 
 
e201c8f
5f366e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e201c8f
2e2a4b0
5f366e7
 
 
 
 
e201c8f
 
 
 
 
5f366e7
e201c8f
2e2a4b0
e201c8f
 
 
2e2a4b0
e201c8f
 
 
2e2a4b0
e201c8f
 
 
 
 
 
 
 
 
5f366e7
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
5f366e7
e201c8f
 
2e2a4b0
e201c8f
5f366e7
 
e201c8f
 
 
 
5f366e7
 
 
 
e201c8f
5f366e7
 
 
e201c8f
 
5f366e7
 
 
 
 
 
e201c8f
 
5f366e7
 
 
e201c8f
 
 
 
 
 
 
 
 
2e2a4b0
e201c8f
 
 
 
 
 
2e2a4b0
e201c8f
 
 
5f366e7
e201c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
e201c8f
 
 
 
2e2a4b0
e201c8f
 
 
 
 
 
 
 
 
2e2a4b0
e201c8f
 
 
 
2e2a4b0
e201c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f366e7
 
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
5f366e7
2e2a4b0
5f366e7
 
2e2a4b0
5f366e7
 
 
 
2e2a4b0
5f366e7
 
2e2a4b0
5f366e7
 
2e2a4b0
5f366e7
 
 
 
 
 
 
 
 
 
 
2e2a4b0
5f366e7
 
 
 
e201c8f
5f366e7
 
 
e201c8f
5f366e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
5f366e7
 
 
 
 
2e2a4b0
5f366e7
 
2e2a4b0
5f366e7
 
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
5f366e7
 
 
 
 
2e2a4b0
5f366e7
 
2e2a4b0
5f366e7
 
 
 
 
 
 
 
 
 
 
 
 
 
bb7dc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84e8d07
bb7dc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c20f696
 
 
 
 
 
 
 
 
 
 
 
 
 
2e2a4b0
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
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
// Medical AI Assistant - Main Application JavaScript
// static/js/app.js

// TEMPORARILY DISABLED SUBMODULES
// import { attachUIHandlers } from './ui/handlers.js';
// import { attachDoctorUI } from './ui/doctor.js';
// import { attachPatientUI } from './ui/patient.js';
// import { attachSettingsUI } from './ui/settings.js';
// import { attachSessionsUI } from './chat/sessions.js';
// import { attachMessagingUI } from './chat/messaging.js';
// import { AudioRecordingUI } from './audio/recorder.js';

// ================================================================================
// MAIN APPLICATION CLASS
// ================================================================================
class MedicalChatbotApp {
    constructor() {
        this.currentUser = null; // doctor
        this.currentPatientId = null;
        this.currentSession = null;
        this.backendSessions = [];
        this.memory = new Map();  // In-memory storage for STM/demo
        this.isLoading = false;
        this.doctors = this.loadDoctors();
        this.audioRecorder = null; // Audio recording UI

        this.init();
    }

    async init() {
        // // TEMPORARILY DISABLED SUBMODULES ATTACHMENT
        // attachUIHandlers(this);
        // // Attach specialized UIs
        // attachDoctorUI(this);
        // attachPatientUI(this);
        // attachSettingsUI(this);
        // attachSessionsUI(this);
        // attachMessagingUI(this);
        this.setupEventListeners();
        this.loadUserPreferences();
        this.initializeUser();
        await this.loadSavedPatientId();

        // If a patient is selected, fetch sessions from backend first
        if (this.currentPatientId) {
            await this.fetchAndRenderPatientSessions();
        }

        // Ensure a session exists and is displayed immediately if nothing to show
        this.ensureStartupSession();
        this.loadChatSessions();

        // Bind patient handlers
        console.log('[DEBUG] Binding patient handlers');
        this.bindPatientHandlers();
        this.setupPatientModal();
        // Apply saved theme immediately
        const prefs = JSON.parse(localStorage.getItem('medicalChatbotPreferences') || '{}');
        this.setTheme(prefs.theme || 'auto');
        this.setupTheme();

        // Initialize audio recording (guarded if module not present)
        try {
            if (typeof AudioRecordingUI !== 'undefined') {
                this.initializeAudioRecording();
            } else {
                console.warn('[Audio] Recorder module not loaded; skipping initialization');
            }
        } catch (e) {
            console.warn('[Audio] Failed to initialize recorder', e);
        }
    }

    setupEventListeners() {
        // Sidebar toggle
        const sidebarToggle = document.getElementById('sidebarToggle');
        if (sidebarToggle) {
            sidebarToggle.addEventListener('click', () => {
            this.toggleSidebar();
        });
        }

        // Click outside sidebar to close (mobile/overlay behavior)
        const overlay = document.getElementById('appOverlay');
        console.log('[DEBUG] Overlay element found:', !!overlay);
        const updateOverlay = () => {
            const sidebar = document.getElementById('sidebar');
            const isOpen = sidebar && sidebar.classList.contains('show');
            console.log('[DEBUG] Updating overlay - sidebar open:', isOpen);
            if (overlay) {
                if (isOpen) {
                    overlay.classList.add('show');
                    console.log('[DEBUG] Overlay shown');
                } else {
                    overlay.classList.remove('show');
                    console.log('[DEBUG] Overlay hidden');
                }
            }
        };

        // Keep overlay synced when toggling
        const origToggle = this.toggleSidebar.bind(this);
        this.toggleSidebar = () => {
            console.log('[DEBUG] Wrapped toggleSidebar called');
            origToggle();
            updateOverlay();
        };

        // Initialize overlay state - ensure it's hidden on startup
        if (overlay) {
            overlay.classList.remove('show');
        }
        updateOverlay();

        // Handle window resize for responsive behavior
        window.addEventListener('resize', () => {
            console.log('[DEBUG] Window resized, updating overlay');
            updateOverlay();
        });

        // Click outside to close sidebar
        document.addEventListener('click', (e) => {
            const sidebar = document.getElementById('sidebar');
            const toggleBtn = document.getElementById('sidebarToggle');
            const main = document.querySelector('.main-content');
            if (!sidebar) return;
            const isOpen = sidebar.classList.contains('show');
            const clickInside = sidebar.contains(e.target) || (toggleBtn && toggleBtn.contains(e.target));
            const clickOnOverlay = overlay && overlay.contains(e.target);

            console.log('[DEBUG] Click event - sidebar open:', isOpen, 'click inside:', clickInside, 'click on overlay:', clickOnOverlay);

            if (isOpen && !clickInside) {
                if (clickOnOverlay) {
                    console.log('[DEBUG] Clicked on overlay, closing sidebar');
                } else {
                    console.log('[DEBUG] Clicked outside sidebar, closing sidebar');
                }
                sidebar.classList.remove('show');
            }
            // Also close if clicking the main-content while open
            if (isOpen && main && main.contains(e.target) && !sidebar.contains(e.target)) {
                console.log('[DEBUG] Clicked on main content, closing sidebar');
                sidebar.classList.remove('show');
            }
            updateOverlay();
        }, true);

        if (overlay) {
            overlay.addEventListener('click', () => {
                console.log('[DEBUG] Overlay clicked directly');
                const sidebar = document.getElementById('sidebar');
                if (sidebar) sidebar.classList.remove('show');
                updateOverlay();
            });
        }

        // New chat button
        const newChatBtn = document.getElementById('newChatBtn');
        if (newChatBtn) {
            newChatBtn.addEventListener('click', () => {
            this.startNewChat();
        });
        }

        // Send button and input
        const sendBtn = document.getElementById('sendBtn');
        if (sendBtn) {
            sendBtn.addEventListener('click', () => {
            this.sendMessage();
        });
        }

        const chatInput = document.getElementById('chatInput');
        if (chatInput) {
            chatInput.addEventListener('keydown', (e) => {
            if (e.key === 'Enter' && !e.shiftKey) {
                e.preventDefault();
                this.sendMessage();
            }
        });

        // Auto-resize textarea
            chatInput.addEventListener('input', (e) => this.autoResizeTextarea(e.target));
        }

        // User profile
        const userProfile = document.getElementById('userProfile');
        if (userProfile) {
            userProfile.addEventListener('click', () => {
                console.log('[DEBUG] User profile clicked');
            this.showUserModal();
        });
        }

        // Settings
        const settingsBtn = document.getElementById('settingsBtn');
        if (settingsBtn) {
            settingsBtn.addEventListener('click', () => {
                console.log('[DEBUG] Settings clicked');
            this.showSettingsModal();
        });
        }

        // Action buttons
        const exportBtn = document.getElementById('exportBtn');
        const clearBtn = document.getElementById('clearBtn');
        if (exportBtn) exportBtn.addEventListener('click', () => this.exportChat());
        if (clearBtn) clearBtn.addEventListener('click', () => this.clearChat());

        // Modal events
        this.setupModalEvents();

        // Theme toggle live
        const themeSelect = document.getElementById('themeSelect');
        if (themeSelect) {
            themeSelect.addEventListener('change', (e) => {
                console.log('[Theme] change ->', e.target.value);
            this.setTheme(e.target.value);
        });
        }
        // Font size live
        const fontSize = document.getElementById('fontSize');
        if (fontSize) {
            fontSize.addEventListener('change', (e) => {
                console.log('[Font] change ->', e.target.value);
                this.setFontSize(e.target.value);
            });
        }
        // Other preferences live
        const autoSaveEl = document.getElementById('autoSave');
        const notificationsEl = document.getElementById('notifications');
        if (autoSaveEl) autoSaveEl.addEventListener('change', () => this.savePreferences());
        if (notificationsEl) notificationsEl.addEventListener('change', () => this.savePreferences());
    }

    setupModalEvents() {
        // User modal
        const userModalClose = document.getElementById('userModalClose');
        const userModalCancel = document.getElementById('userModalCancel');
        const userModalSave = document.getElementById('userModalSave');

        if (userModalClose) {
            userModalClose.addEventListener('click', () => {
            this.hideModal('userModal');
        });
        }

        if (userModalCancel) {
            userModalCancel.addEventListener('click', () => {
            this.hideModal('userModal');
        });
        }

        if (userModalSave) {
            userModalSave.addEventListener('click', () => {
            this.saveUserProfile();
        });
        }

        // Settings modal
        const settingsModalClose = document.getElementById('settingsModalClose');
        const settingsModalCancel = document.getElementById('settingsModalCancel');
        const settingsModalSave = document.getElementById('settingsModalSave');

        if (settingsModalClose) {
            settingsModalClose.addEventListener('click', () => {
            this.hideModal('settingsModal');
        });
        }

        if (settingsModalCancel) {
            settingsModalCancel.addEventListener('click', () => {
            this.hideModal('settingsModal');
        });
        }

        if (settingsModalSave) {
            settingsModalSave.addEventListener('click', () => {
            this.saveSettings();
        });
        }

        // Close modals when clicking outside
        document.querySelectorAll('.modal').forEach(modal => {
            modal.addEventListener('click', (e) => {
                if (e.target === modal) {
                    this.hideModal(modal.id);
                }
            });
        });

        // Edit title modal wiring
        const closeEdit = () => this.hideModal('editTitleModal');
        const editTitleModal = document.getElementById('editTitleModal');
        if (editTitleModal) {
            const editTitleModalClose = document.getElementById('editTitleModalClose');
            const editTitleModalCancel = document.getElementById('editTitleModalCancel');
            const editTitleModalSave = document.getElementById('editTitleModalSave');

            if (editTitleModalClose) editTitleModalClose.addEventListener('click', closeEdit);
            if (editTitleModalCancel) editTitleModalCancel.addEventListener('click', closeEdit);
            if (editTitleModalSave) {
                editTitleModalSave.addEventListener('click', () => {
                const input = document.getElementById('editSessionTitleInput');
                const newTitle = input.value.trim();
                if (!newTitle) return;
                if (!this._pendingEditSessionId) return;
                this.renameChatSession(this._pendingEditSessionId, newTitle);
                this._pendingEditSessionId = null;
                input.value = '';
                this.hideModal('editTitleModal');
            });
            }
        }

        // System access button handler
        const systemBtn = document.getElementById('systemAccessBtn');

        if (systemBtn) {
            systemBtn.addEventListener('click', () => {
                window.location.href = '/system-status';
            });
        }
    }

    initializeUser() {
        // Check if user exists in localStorage
        const savedUser = localStorage.getItem('medicalChatbotUser');
        if (savedUser) {
            this.currentUser = JSON.parse(savedUser);
        } else {
            // Create default user
            this.currentUser = {
                id: this.generateId(), // This is a temporary local ID
                name: 'Anonymous',
                role: 'Medical Professional',
                specialty: '',
                createdAt: new Date().toISOString()
            };
            this.saveUser();
        }

        this.updateUserDisplay();
    }

    startNewChat() {
        if (this.currentSession) {
            // Save current session (local only)
            this.saveCurrentSession();
        }

        // Create new session
        this.currentSession = {
            id: 'default',
            title: 'New Chat',
            messages: [],
            createdAt: new Date().toISOString(),
            lastActivity: new Date().toISOString()
        };

        // Clear chat messages
        this.clearChatMessages();

        // Add welcome message
        this.addMessage('assistant', this.getWelcomeMessage());

        // Update UI
        this.updateChatTitle();
        this.loadChatSessions();

        // Focus input
        document.getElementById('chatInput').focus();
    }

    ensureStartupSession() {
        // If we already have backend sessions for selected patient, do not create a local one
        if (this.backendSessions && this.backendSessions.length > 0) {
            return;
        }
        const sessions = this.getChatSessions();
        if (sessions.length === 0) {
            // Create a new session immediately so it shows in sidebar
            this.currentSession = {
                id: 'default',
                title: 'New Chat',
                messages: [],
                createdAt: new Date().toISOString(),
                lastActivity: new Date().toISOString()
            };
            this.saveCurrentSession();
            this.updateChatTitle();
        } else {
            // Load the most recent session into view
            this.currentSession = sessions[0];
            this.clearChatMessages();
            this.currentSession.messages.forEach(m => this.displayMessage(m));
            this.updateChatTitle();
        }
    }

    getWelcomeMessage() {
        return `👋 Welcome to Medical AI Assistant

# Who am I, and what can I do?

I'm here to help you with medical questions, diagnosis assistance, and healthcare information. I can:

🔍 Answer medical questions and provide information
📋 Help with symptom analysis and differential diagnosis
💊 Provide medication and treatment information
📚 Explain medical procedures and conditions
⚠️ Offer general health advice (not medical diagnosis)

# Key Features

**Medical Information:** I can provide evidence-based medical information and explanations.

**Symptom Analysis:** I can help analyze symptoms and suggest possible conditions.

**Treatment Guidance:** I can explain treatments, medications, and procedures.

**Important:** This is for informational purposes only. Always consult with qualified healthcare professionals for medical advice.

How can I assist you today?`;
    }

    clearChatMessages() {
        const chatMessages = document.getElementById('chatMessages');
        chatMessages.innerHTML = '';
    }

    showModal(modalId) {
        console.log('[DEBUG] showModal called with ID:', modalId);
        const modal = document.getElementById(modalId);
        if (modal) {
            modal.classList.add('show');
            console.log('[DEBUG] Modal shown:', modalId);
        } else {
            console.error('[DEBUG] Modal not found:', modalId);
        }
    }

    saveSettings() {
        const theme = document.getElementById('themeSelect').value;
        const fontSize = document.getElementById('fontSize').value;
        const autoSave = document.getElementById('autoSave').checked;
        const notifications = document.getElementById('notifications').checked;

        console.log('[Settings] save', { theme, fontSize, autoSave, notifications });
        this.setTheme(theme);
        this.setFontSize(fontSize);

        // Save additional preferences
        const preferences = {
            theme: theme,
            fontSize: fontSize,
            autoSave: autoSave,
            notifications: notifications
        };
        console.log('[Prefs] write', preferences);
        localStorage.setItem('medicalChatbotPreferences', JSON.stringify(preferences));

        this.hideModal('settingsModal');
    }

    updateUserDisplay() {
        document.getElementById('userName').textContent = this.currentUser.name;
        document.getElementById('userStatus').textContent = this.currentUser.role;
    }

    saveUser() {
        localStorage.setItem('medicalChatbotUser', JSON.stringify(this.currentUser));
    }

    generateId() {
        return Date.now().toString(36) + Math.random().toString(36).substr(2);
    }



// ----------------------------------------------------------
// Additional UI setup START
// -----------------------------
// Our submodules aren't lodaed on app.js, so we need to add them here
// Perhaps this is FastAPI limitation, remove this when proper deploy this
// On UI specific hosting site.
// ----------------------------------------------------------


    // ================================================================================
    // HANDLERS.JS FUNCTIONALITY
    // ================================================================================
    toggleSidebar() {
        const sidebar = document.getElementById('sidebar');
        console.log('[DEBUG] toggleSidebar called');
        if (sidebar) {
            const wasOpen = sidebar.classList.contains('show');
            sidebar.classList.toggle('show');
            const isNowOpen = sidebar.classList.contains('show');
            console.log('[DEBUG] Sidebar toggled - was open:', wasOpen, 'now open:', isNowOpen);
            } else {
            console.error('[DEBUG] Sidebar element not found');
        }
    }

    autoResizeTextarea(textarea) {
        if (!textarea) return;
        textarea.style.height = 'auto';
        textarea.style.height = Math.min(textarea.scrollHeight, 120) + 'px';
    }

    exportChat() {
        if (!this.currentSession || this.currentSession.messages.length === 0) {
            alert('No chat to export.');
            return;
        }
        const chatData = {
            user: this.currentUser?.name || 'Unknown',
            session: this.currentSession.title,
            date: new Date().toISOString(),
            messages: this.currentSession.messages
        };
        const blob = new Blob([JSON.stringify(chatData, null, 2)], { type: 'application/json' });
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = `medical-chat-${this.currentSession.title.replace(/[^a-z0-9]/gi, '-')}.json`;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
    }

    clearChat() {
        if (confirm('Are you sure you want to clear this chat? This action cannot be undone.')) {
            this.clearChatMessages();
            if (this.currentSession) {
                this.currentSession.messages = [];
                this.currentSession.title = 'New Chat';
                this.updateChatTitle();
            }
        }
    }

    hideModal(modalId) {
        console.log('[DEBUG] hideModal called with ID:', modalId);
        const modal = document.getElementById(modalId);
        if (modal) {
            modal.classList.remove('show');
            console.log('[DEBUG] Modal hidden:', modalId);
            } else {
            console.error('[DEBUG] Modal not found:', modalId);
        }
    }

    showUserModal() {
        this.populateDoctorSelect();
        const sel = document.getElementById('profileNameSelect');
        if (sel && sel.options.length === 0) {
            const createOpt = document.createElement('option');
            createOpt.value = '__create__';
            createOpt.textContent = 'Create doctor user...';
            sel.appendChild(createOpt);
        }
        if (sel && !sel.value) sel.value = this.currentUser?.name || '__create__';

        // Safely set role and specialty with null checks
        const roleEl = document.getElementById('profileRole');
        const specialtyEl = document.getElementById('profileSpecialty');
        if (roleEl) roleEl.value = (this.currentUser && this.currentUser.role) ? this.currentUser.role : 'Medical Professional';
        if (specialtyEl) specialtyEl.value = (this.currentUser && this.currentUser.specialty) ? this.currentUser.specialty : '';

        // Add event listener for doctor selection changes
        this.setupDoctorSelectionHandler();

        this.showModal('userModal');
    }

    setupDoctorSelectionHandler() {
        const sel = document.getElementById('profileNameSelect');
        const roleEl = document.getElementById('profileRole');
        const specialtyEl = document.getElementById('profileSpecialty');

        if (!sel || !roleEl || !specialtyEl) return;

        // Remove existing listeners to avoid duplicates
        sel.removeEventListener('change', this.handleDoctorSelection);

        // Add new listener
        this.handleDoctorSelection = async (event) => {
            const selectedName = event.target.value;
            console.log('[DEBUG] Doctor selected:', selectedName);

            if (selectedName === '__create__') {
                // Reset to default values for new doctor
                roleEl.value = 'Medical Professional';
                specialtyEl.value = '';
                return;
            }

            // Find the selected doctor in our doctors list
            const selectedDoctor = this.doctors.find(d => d.name === selectedName);
            if (selectedDoctor) {
                // Update role and specialty from the selected doctor
                if (selectedDoctor.role) {
                    roleEl.value = selectedDoctor.role;
                }
                if (selectedDoctor.specialty) {
                    specialtyEl.value = selectedDoctor.specialty;
                }
                console.log('[DEBUG] Updated role and specialty for doctor:', selectedName, selectedDoctor.role, selectedDoctor.specialty);
            } else {
                // If doctor not found in local list, try to fetch from backend
                try {
                    const resp = await fetch(`/account?q=${encodeURIComponent(selectedName)}&limit=1`);
                    if (resp.ok) {
                        const data = await resp.json();
                        const doctor = data && data[0];
                        if (doctor) {
                            if (doctor.role) {
                                roleEl.value = doctor.role;
                            }
                            if (doctor.specialty) {
                                specialtyEl.value = doctor.specialty;
                            }
                            console.log('[DEBUG] Fetched and updated role/specialty from backend:', doctor.role, doctor.specialty);
                        }
                    }
                } catch (e) {
                    console.warn('Failed to fetch doctor details from backend:', e);
                }
            }
        };

        sel.addEventListener('change', this.handleDoctorSelection);
    }

    showSettingsModal() {
        console.log('[DEBUG] showSettingsModal called');
        this.showModal('settingsModal');
    }


    // ================================================================================
    // SETTINGS.JS FUNCTIONALITY
    // ================================================================================
    loadUserPreferences() {
        const prefs = JSON.parse(localStorage.getItem('medicalChatbotPreferences') || '{}');
        if (prefs.theme) this.setTheme(prefs.theme);
        if (prefs.fontSize) this.setFontSize(prefs.fontSize);
        if (prefs.autoSave !== undefined) document.getElementById('autoSave').checked = prefs.autoSave;
        if (prefs.notifications !== undefined) document.getElementById('notifications').checked = prefs.notifications;
    }

    setTheme(theme) {
        const root = document.documentElement;
        console.log('[Theme] Setting theme to:', theme);
        if (theme === 'auto') {
            const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
            root.setAttribute('data-theme', prefersDark ? 'dark' : 'light');
            console.log('[Theme] Auto theme applied:', prefersDark ? 'dark' : 'light');
        } else {
            root.setAttribute('data-theme', theme);
            console.log('[Theme] Manual theme applied:', theme);
        }
        // Force a re-render by toggling a class
        root.classList.add('theme-updated');
        setTimeout(() => root.classList.remove('theme-updated'), 100);
    }

    setFontSize(size) {
        const root = document.documentElement;
        const sizes = { small: '14px', medium: '16px', large: '18px' };
        const fontSize = sizes[size] || '16px';
        console.log('[Font] Setting font size to:', fontSize);
        root.style.fontSize = fontSize;
        // Force a re-render
        root.classList.add('font-updated');
        setTimeout(() => root.classList.remove('font-updated'), 100);
    }

    setupTheme() {
        const themeSelect = document.getElementById('themeSelect');
        if (themeSelect) {
            const prefs = JSON.parse(localStorage.getItem('medicalChatbotPreferences') || '{}');
            themeSelect.value = prefs.theme || 'auto';
        }
    }

    savePreferences() {
        const preferences = {
            theme: document.getElementById('themeSelect').value,
            fontSize: document.getElementById('fontSize').value,
            autoSave: document.getElementById('autoSave').checked,
            notifications: document.getElementById('notifications').checked
        };
        localStorage.setItem('medicalChatbotPreferences', JSON.stringify(preferences));
    }

    showLoading(show) {
        const overlay = document.getElementById('loadingOverlay');
        if (overlay) {
            if (show) {
                overlay.classList.add('show');
            } else {
                overlay.classList.remove('show');
            }
        }
        this.isLoading = show;
    }


    // ================================================================================
    // SESSIONS.JS FUNCTIONALITY
    // ================================================================================
    getChatSessions() {
        const sessions = localStorage.getItem(`chatSessions_${this.currentUser.id}`);
        return sessions ? JSON.parse(sessions) : [];
    }

    saveCurrentSession() {
        if (!this.currentSession) return;
        if (this.currentSession.source === 'backend') return; // do not persist backend sessions locally here
        const sessions = this.getChatSessions();
        const existingIndex = sessions.findIndex(s => s.id === this.currentSession.id);
        if (existingIndex >= 0) {
            sessions[existingIndex] = { ...this.currentSession };
        } else {
            sessions.unshift(this.currentSession);
        }
        localStorage.setItem(`chatSessions_${this.currentUser.id}`, JSON.stringify(sessions));
    }

    loadChatSessions() {
        const sessionsContainer = document.getElementById('chatSessions');
        sessionsContainer.innerHTML = '';
        const sessions = (this.backendSessions && this.backendSessions.length > 0) ? this.backendSessions : this.getChatSessions();
        if (sessions.length === 0) {
            sessionsContainer.innerHTML = '<div class="no-sessions">No chat sessions yet</div>';
            return;
        }
        sessions.forEach(session => {
            const sessionElement = document.createElement('div');
            sessionElement.className = `chat-session ${session.id === this.currentSession?.id ? 'active' : ''}`;
            sessionElement.addEventListener('click', async () => {
                // Avoid re-loading if the session is already active
                if (session.id === this.currentSession?.id) return;

                await this.switchToSession(session);
            });
            const time = this.formatTime(session.lastActivity);
            sessionElement.innerHTML = `
                <div class="chat-session-row">
                    <div class="chat-session-meta">
                        <div class="chat-session-title">${session.title}</div>
                        <div class="chat-session-time">${time}</div>
                    </div>
                    <div class="chat-session-actions">
                        <button class="chat-session-menu" title="Options" aria-label="Options" data-session-id="${session.id}">
                            <i class="fas fa-ellipsis-vertical"></i>
                        </button>
                    </div>
                </div>
            `;
            sessionsContainer.appendChild(sessionElement);
            const menuBtn = sessionElement.querySelector('.chat-session-menu');
            if (session.source !== 'backend') {
            menuBtn.addEventListener('click', (e) => {
                e.stopPropagation();
                this.showSessionMenu(e.currentTarget, session.id);
            });
            } else {
                menuBtn.disabled = true;
                menuBtn.style.opacity = 0.5;
                menuBtn.title = 'Options available for local sessions only';
            }
        });
    }

    updateChatTitle() {
        const titleElement = document.getElementById('chatTitle');
        if (this.currentSession) {
            titleElement.textContent = this.currentSession.title;
        } else {
            titleElement.textContent = 'Medical AI Assistant';
        }
    }

    async switchToSession(session) {
        console.log('[DEBUG] Switching to session:', session.id, session.source);

        // Clear current session and messages first
        this.currentSession = null;
        this.clearChatMessages();

        // Set new session
        this.currentSession = { ...session };

        if (session.source === 'backend') {
            // For backend sessions, always fetch fresh messages
            console.log('[DEBUG] Fetching messages for backend session:', session.id);
            await this.hydrateMessagesForSession(session.id);
        } else {
            // For local sessions, load from localStorage
            console.log('[DEBUG] Loading messages for local session:', session.id);
            const localSessions = this.getChatSessions();
            const localSession = localSessions.find(s => s.id === session.id);
            if (localSession && localSession.messages) {
                // Sort messages by timestamp
                const sortedMessages = localSession.messages.sort((a, b) => {
                    const timeA = new Date(a.timestamp || 0).getTime();
                    const timeB = new Date(b.timestamp || 0).getTime();
                    return timeA - timeB; // Ascending order for display
                });
                console.log('[DEBUG] Displaying', sortedMessages.length, 'messages for local session');
                sortedMessages.forEach(message => this.displayMessage(message));
                // Check if session needs title generation
                this.checkAndGenerateSessionTitle();
            } else {
                console.log('[DEBUG] No messages found for local session:', session.id);
            }
        }

        this.updateChatTitle();
        this.loadChatSessions(); // Re-render to update active state
    }

    loadChatSession(sessionId) {
        const sessions = this.getChatSessions();
        const session = sessions.find(s => s.id === sessionId);
        if (!session) return;
        this.switchToSession(session);
    }

    renameChatSession(sessionId, newTitle) {
        const sessions = this.getChatSessions();
        const idx = sessions.findIndex(s => s.id === sessionId);
        if (idx === -1) return;
        sessions[idx] = { ...sessions[idx], title: newTitle };
        localStorage.setItem(`chatSessions_${this.currentUser.id}`, JSON.stringify(sessions));
        if (this.currentSession && this.currentSession.id === sessionId) {
            this.currentSession.title = newTitle;
            this.updateChatTitle();
        }
        this.loadChatSessions();
    }

    async deleteChatSession(sessionId) {
        const confirmDelete = confirm('Delete this chat session? This cannot be undone.');
        if (!confirmDelete) return;

        try {
            // Check if it's a backend session
            const isBackendSession = this.backendSessions && this.backendSessions.some(s => s.id === sessionId);

            if (isBackendSession) {
                // Delete from backend (MongoDB + memory system)
                const resp = await fetch(`/session/${sessionId}`, {
                    method: 'DELETE',
                    headers: { 'Content-Type': 'application/json' }
                });

                if (!resp.ok) {
                    throw new Error(`HTTP ${resp.status}`);
                }

                // Remove from backend sessions
                this.backendSessions = this.backendSessions.filter(s => s.id !== sessionId);

                // Invalidate caches
                this.invalidateSessionCache(this.currentPatientId);
                this.invalidateMessageCache(this.currentPatientId, sessionId);
            } else {
                // Delete from localStorage only
                const sessions = this.getChatSessions();
                const index = sessions.findIndex(s => s.id === sessionId);
                if (index === -1) return;

                sessions.splice(index, 1);
                localStorage.setItem(`chatSessions_${this.currentUser.id}`, JSON.stringify(sessions));
            }

            // Handle current session cleanup
            if (this.currentSession && this.currentSession.id === sessionId) {
                if (isBackendSession) {
                    // For backend sessions, switch to another session or clear
                    if (this.backendSessions.length > 0) {
                        this.currentSession = this.backendSessions[0];
                        await this.hydrateMessagesForSession(this.currentSession.id);
                    } else {
                        this.currentSession = null;
                        this.clearChatMessages();
                    }
                } else {
                    // For local sessions, switch to another session or clear
                    const remainingSessions = this.getChatSessions();
                    if (remainingSessions.length > 0) {
                        this.currentSession = remainingSessions[0];
                        this.clearChatMessages();
                        this.currentSession.messages.forEach(m => this.displayMessage(m));
                    } else {
                        this.currentSession = null;
                        this.clearChatMessages();
                    }
                }
                this.updateChatTitle();
            }

            this.loadChatSessions();

        } catch (error) {
            console.error('Error deleting session:', error);
            alert('Failed to delete session. Please try again.');
        }
    }

    showSessionMenu(anchorEl, sessionId) {
        // Remove existing popover
        document.querySelectorAll('.chat-session-menu-popover').forEach(p => p.remove());
        const rect = anchorEl.getBoundingClientRect();
        const pop = document.createElement('div');
        pop.className = 'chat-session-menu-popover show';
        pop.innerHTML = `
            <div class="chat-session-menu-item" data-action="edit" data-session-id="${sessionId}"><i class="fas fa-pen"></i> Edit Name</div>
            <div class="chat-session-menu-item" data-action="delete" data-session-id="${sessionId}"><i class="fas fa-trash"></i> Delete</div>
        `;
        document.body.appendChild(pop);
        pop.style.top = `${rect.bottom + window.scrollY + 6}px`;
        pop.style.left = `${rect.right + window.scrollX - pop.offsetWidth}px`;
        const onDocClick = (ev) => {
            if (!pop.contains(ev.target) && ev.target !== anchorEl) {
                pop.remove();
                document.removeEventListener('click', onDocClick);
            }
        };
        setTimeout(() => document.addEventListener('click', onDocClick), 0);
        pop.querySelectorAll('.chat-session-menu-item').forEach(item => {
            item.addEventListener('click', (e) => {
                const action = item.getAttribute('data-action');
                const id = item.getAttribute('data-session-id');
                if (action === 'delete') this.deleteChatSession(id);
                else if (action === 'edit') {
                    this._pendingEditSessionId = id;
                    const sessions = this.getChatSessions();
                    const s = sessions.find(x => x.id === id);
                    const input = document.getElementById('editSessionTitleInput');
                    if (input) input.value = s ? s.title : '';
                    this.showModal('editTitleModal');
                }
                pop.remove();
            });
        });
    }

    updateBackendSession(sessionId, updates) {
        // This would call the backend API to update session metadata
        console.log('Updating backend session:', sessionId, updates);
    }

    deleteBackendSession(sessionId) {
        // This would call the backend API to delete the session
        console.log('Deleting backend session:', sessionId);
    }

    updateCurrentSession() {
        if (this.currentSession) {
            this.currentSession.lastActivity = new Date().toISOString();
            this.saveCurrentSession();
        }
    }


    // ================================================================================
    // DOCTOR.JS FUNCTIONALITY
    // ================================================================================
    async loadDoctors() {
        try {
            // Fetch doctors from MongoDB
            const resp = await fetch('/account');
            if (resp.ok) {
                const data = await resp.json();
                this.doctors = data || [];
                // Ensure each doctor has role and specialty information
                this.doctors = this.doctors.map(doctor => ({
                    name: doctor.name,
                    role: doctor.role || 'Medical Professional',
                    specialty: doctor.specialty || '',
                    id: doctor.id
                }));
                // Also save to localStorage for offline access
                localStorage.setItem('medicalChatbotDoctors', JSON.stringify(this.doctors));
                console.log('[DEBUG] Loaded doctors with role/specialty:', this.doctors);
                return this.doctors;
            } else {
                // Fallback to localStorage if API fails
                const raw = localStorage.getItem('medicalChatbotDoctors');
                const arr = raw ? JSON.parse(raw) : [];
                const seen = new Set();
                this.doctors = arr.filter(x => x && x.name && !seen.has(x.name) && seen.add(x.name));
                return this.doctors;
            }
        } catch (e) {
            console.warn('Failed to load doctors from API, using localStorage fallback:', e);
            // Fallback to localStorage
            const raw = localStorage.getItem('medicalChatbotDoctors');
            const arr = raw ? JSON.parse(raw) : [];
            const seen = new Set();
            this.doctors = arr.filter(x => x && x.name && !seen.has(x.name) && seen.add(x.name));
            return this.doctors;
        }
    }

    async searchDoctors(query) {
        try {
            const resp = await fetch(`/account?q=${encodeURIComponent(query)}&limit=10`);
            if (resp.ok) {
                const data = await resp.json();
                return data || [];
            }
        } catch (e) {
            console.warn('Doctor search failed:', e);
        }
        return [];
    }

    async createDoctor(doctorData) {
        try {
            const resp = await fetch('/account', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(doctorData)
            });
            if (resp.ok) {
                const data = await resp.json();
                // Add to local doctors list
                this.doctors.push({ name: data.name, id: data.id });
                this.saveDoctors();
                return data;
            }
        } catch (e) {
            console.error('Failed to create doctor:', e);
        }
        return null;
    }

    saveDoctors() {
        localStorage.setItem('medicalChatbotDoctors', JSON.stringify(this.doctors));
    }

    async populateDoctorSelect() {
        const sel = document.getElementById('profileNameSelect');
        const newSec = document.getElementById('newDoctorSection');
        if (!sel) return;

        // Load doctors from MongoDB
        await this.loadDoctors();

        sel.innerHTML = '';
        const createOpt = document.createElement('option');
        createOpt.value = '__create__';
        createOpt.textContent = 'Create doctor user...';
        sel.appendChild(createOpt);
        // Ensure no duplicates, include current doctor
        const names = new Set(this.doctors.map(d => d.name));
        if (this.currentUser?.name && !names.has(this.currentUser.name)) {
            this.doctors.unshift({ name: this.currentUser.name });
            names.add(this.currentUser.name);
            this.saveDoctors();
        }
        this.doctors.forEach(d => {
            const opt = document.createElement('option');
            opt.value = d.name;
            opt.textContent = d.name;
            if (this.currentUser?.name === d.name) opt.selected = true;
            sel.appendChild(opt);
        });
        sel.addEventListener('change', () => {
            if (sel.value === '__create__') {
                newSec.style.display = '';
                const input = document.getElementById('newDoctorName');
                if (input) input.value = '';
            } else {
                newSec.style.display = 'none';
                // Trigger doctor selection handler to update role/specialty
                if (this.handleDoctorSelection) {
                    this.handleDoctorSelection({ target: sel });
                }
            }
        });
        const cancelBtn = document.getElementById('cancelNewDoctor');
        const confirmBtn = document.getElementById('confirmNewDoctor');
        if (cancelBtn) cancelBtn.onclick = () => { newSec.style.display = 'none'; sel.value = this.currentUser?.name || ''; };
        if (confirmBtn) confirmBtn.onclick = async () => {
            const name = (document.getElementById('newDoctorName').value || '').trim();
            if (!name) return;
            if (!this.doctors.find(d => d.name === name)) {
                // Get current role and specialty from the form
                const role = document.getElementById('profileRole').value || 'Medical Professional';
                const specialty = document.getElementById('profileSpecialty').value.trim() || null;

                // Create doctor in MongoDB
                const result = await this.createDoctor({
                    name,
                    role,
                    specialty
                });
                if (result && result.id) {
                    this.doctors.unshift({
                        name,
                        role,
                        specialty,
                        id: result.id
                    });
                    this.saveDoctors();

                    // Update current user profile
                    this.currentUser.id = result.id;
                    this.currentUser.name = name;
                    this.currentUser.role = role;
                    this.currentUser.specialty = specialty;
                    this.saveUser();
                    this.updateUserDisplay();
                }
            }
            await this.populateDoctorSelect();
            sel.value = name;
            newSec.style.display = 'none';
        };
    }

    async saveUserProfile() {
        const nameSel = document.getElementById('profileNameSelect');
        const name = nameSel ? nameSel.value : '';
        const role = document.getElementById('profileRole').value;
        const specialty = document.getElementById('profileSpecialty').value.trim();

        if (!name || name === '__create__') {
            alert('Please select or create a doctor name.');
            return;
        }

        // Check if doctor exists in MongoDB first
        let existingDoctor = null;
        try {
            const resp = await fetch(`/account?q=${encodeURIComponent(name)}`);
            if(resp.ok) {
                const accounts = await resp.json();
                existingDoctor = accounts.find(acc => acc.name === name);
            }
        } catch (e) {
            console.warn('Failed to check doctor existence:', e);
        }

        // Update current user profile info (ID will be updated upon creation)
        this.currentUser.name = name;
        this.currentUser.role = role;
        this.currentUser.specialty = specialty;

        // Only create new doctor in MongoDB if it doesn't exist
        if (!existingDoctor) {
            const doctorPayload = {
                name: name,
                role: role,
                specialty: specialty || null
            };

            try {
                const resp = await fetch('/account', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(doctorPayload)
                });

                if (!resp.ok) throw new Error('Failed to create doctor in backend');
                const data = await resp.json();
                console.log('[Doctor] Created new doctor in backend:', data);

                if (data.id) {
                    this.currentUser.id = data.id;
                }

                // Update local doctor list with the ID from backend
                const existingDoctorIndex = this.doctors.findIndex(d => d.name === name);
                if (existingDoctorIndex === -1) {
                    this.doctors.unshift({ name, role, specialty, id: data.id });
                } else {
                    this.doctors[existingDoctorIndex].id = data.id;
                }

            } catch (err) {
                console.warn('[Doctor] failed to create doctor in backend:', err);
            }
        } else {
             // If doctor exists, find their ID and update currentUser
            if (existingDoctor && existingDoctor.id) {
                this.currentUser.id = existingDoctor.id;
            }
            console.log('[Doctor] Doctor already exists in backend, no creation needed');
        }

        // Save the updated user profile (with potentially new ID)
        this.saveUser();
        this.updateUserDisplay();
        this.saveDoctors();
        this.hideModal('userModal');
    }

    // ================================================================================
    // PATIENT.JS FUNCTIONALITY
    // ================================================================================

    async getLocalStorageSuggestions(query) {
        try {
            const storedPatients = JSON.parse(localStorage.getItem('medicalChatbotPatients') || '[]');
            return storedPatients.filter(p => {
                const nameMatch = p.name.toLowerCase().includes(query.toLowerCase());
                const idMatch = p.id && p.id.includes(query);
                return nameMatch || idMatch;
            });
        } catch (e) {
            console.warn('Failed to get localStorage suggestions:', e);
            return [];
        }
    }

    combinePatientResults(mongoResults, localResults) {
        const resultMap = new Map();
        // Add MongoDB results first (they take priority)
        mongoResults.forEach(patient => {
            if(patient.id) resultMap.set(patient.id, patient);
        });
        // Add localStorage results only if not already present
        localResults.forEach(patient => {
            if (patient.id && !resultMap.has(patient.id)) {
                resultMap.set(patient.id, patient);
            }
        });
        return Array.from(resultMap.values());
    }

    async tryFallbackSearch(query, renderSuggestions) {
        // Use localStorage for fallback suggestions
        try {
            const localResults = await this.getLocalStorageSuggestions(query);
            if (localResults.length > 0) {
                console.log('[DEBUG] Fallback search found matches from localStorage:', localResults);
                renderSuggestions(localResults);
            } else {
                console.log('[DEBUG] No fallback matches found');
                renderSuggestions([]);
            }
        } catch (e) {
            console.warn('Fallback search failed:', e);
            renderSuggestions([]);
        }
    }
    async loadSavedPatientId() {
        const pid = localStorage.getItem('medicalChatbotPatientId');
        if (pid) {
            this.currentPatientId = pid;
            const status = document.getElementById('patientStatus');
            const actions = document.getElementById('patientActions');
            const emrLink = document.getElementById('emrLink');

            if (status) {
                // Try to fetch patient name
                try {
                    const resp = await fetch(`/patient/${pid}`);
                    if (resp.ok) {
                        const patient = await resp.json();
                        status.textContent = `Patient: ${patient.name || 'Unknown'} (${patient.id})`;
                    } else {
                        status.textContent = `Patient: ${pid}`;
                    }
                } catch (e) {
                    status.textContent = `Patient: ${pid}`;
                }
                status.style.color = 'var(--text-secondary)';
            }

            // Show EMR link
            if (actions) actions.style.display = 'block';
            if (emrLink) emrLink.href = `/static/emr.html?patient_id=${pid}`;

            const input = document.getElementById('patientIdInput');
            if (input) input.value = pid;
        }
    }

    savePatientId() {
        if (this.currentPatientId) localStorage.setItem('medicalChatbotPatientId', this.currentPatientId);
        else localStorage.removeItem('medicalChatbotPatientId');
    }

    updatePatientDisplay(patientId, patientName = null) {
        // Safety check: don't update display if patientId is undefined or null
        if (!patientId || patientId === 'undefined' || patientId === 'null') {
            console.warn('updatePatientDisplay called with invalid patientId:', patientId);
            return;
        }

        const status = document.getElementById('patientStatus');
        const actions = document.getElementById('patientActions');
        const emrLink = document.getElementById('emrLink');

        if (status) {
            if (patientName) {
                status.textContent = `Patient: ${patientName} (${patientId})`;
        } else {
                status.textContent = `Patient: ${patientId}`;
            }
            status.style.color = 'var(--text-secondary)';
        }

        // Show EMR link
        if (actions) actions.style.display = 'block';
        if (emrLink) emrLink.href = `/static/emr.html?patient_id=${patientId}`;
    }

    loadPatient = async function () {
        console.log('[DEBUG] loadPatient called');
        const input = document.getElementById('patientIdInput');
        const status = document.getElementById('patientStatus');
        const value = (input?.value || '').trim();
        console.log('[DEBUG] Patient input value:', value);

        if (!value) {
            console.log('[DEBUG] No input provided');
            if (status) { status.textContent = 'Please enter patient ID or name.'; status.style.color = 'var(--warning-color)'; }
            return;
        }

        // Search for patient by name or ID
        console.log('[DEBUG] Searching for patient');
        try {
            const resp = await fetch(`/patient?q=${encodeURIComponent(value)}&limit=1`);
            console.log('[DEBUG] Search response status:', resp.status);
            if (resp.ok) {
                const data = await resp.json();
                console.log('[DEBUG] Search results:', data);
                const first = (data || [])[0];
                if (first && first.id) {
                    console.log('[DEBUG] Found patient, setting as current:', first);
                    this.currentPatientId = first.id;
                    this.savePatientId();
                    input.value = first.id;
                    this.updatePatientDisplay(first.id, first.name || 'Unknown');
                    await this.fetchAndRenderPatientSessions();
                    return;
                }
            }
        } catch (e) {
            console.error('[DEBUG] Search error:', e);
        }

        // No patient found
        console.log('[DEBUG] No patient found');
        if (status) { status.textContent = 'No patient found. Try a different search.'; status.style.color = 'var(--warning-color)'; }
    }

    fetchAndRenderPatientSessions = async function () {
        if (!this.currentPatientId) return;

        // Check localStorage cache first
        const cacheKey = `sessions_${this.currentPatientId}`;
        const cached = localStorage.getItem(cacheKey);
        let sessions = [];

        if (cached) {
            try {
                const cachedData = JSON.parse(cached);
                // Check if cache is recent (less than 2 minutes old)
                const cacheTime = new Date(cachedData.timestamp || 0).getTime();
                const now = new Date().getTime();
                if (now - cacheTime < 2 * 60 * 1000) { // 2 minutes
                    sessions = cachedData.sessions || [];
                    console.log('[DEBUG] Using cached sessions for patient:', this.currentPatientId);
                }
            } catch (e) {
                console.warn('Failed to parse cached sessions:', e);
            }
        }

        // If no cache or cache is stale, fetch from backend
        if (sessions.length === 0) {
            try {
                const resp = await fetch(`/patient/${this.currentPatientId}/session`);
                if (resp.ok) {
                    const data = await resp.json();
                    sessions = Array.isArray(data) ? data : [];

                    // Cache the sessions
                    localStorage.setItem(cacheKey, JSON.stringify({
                        sessions: sessions,
                        timestamp: new Date().toISOString()
                    }));
                    console.log('[DEBUG] Cached sessions for patient:', this.currentPatientId);
                } else {
                    console.warn('Failed to fetch patient sessions', resp.status);
                }
            } catch (e) {
                console.error('Failed to load patient sessions', e);
            }
        }

        // Process sessions
        this.backendSessions = sessions.map(s => ({
            id: s.id,
            title: s.title || 'New Chat',
            messages: [],
            createdAt: s.created_at || new Date().toISOString(),
            lastActivity: s.updated_at || new Date().toISOString(),
            source: 'backend'
        }));

        if (this.backendSessions.length > 0) {
            this.currentSession = this.backendSessions[0];
            await this.hydrateMessagesForSession(this.currentSession.id);
        }

        this.loadChatSessions();
    }

    hydrateMessagesForSession = async function (sessionId) {
        if (!sessionId || sessionId === 'undefined') {
            console.error('[DEBUG] hydrateMessagesForSession was called with an invalid session ID:', sessionId);
            return;
        }
        try {
            // Check localStorage cache first
            const cacheKey = `messages_${this.currentPatientId}_${sessionId}`;
            const cached = localStorage.getItem(cacheKey);
            let messages = [];

            if (cached) {
                try {
                    const cachedData = JSON.parse(cached);
                    // Check if cache is recent (less than 5 minutes old)
                    const cacheTime = new Date(cachedData.timestamp || 0).getTime();
                    const now = new Date().getTime();
                    if (now - cacheTime < 5 * 60 * 1000) { // 5 minutes
                        messages = cachedData.messages || [];
                        console.log('[DEBUG] Using cached messages for session:', sessionId);
                    }
                } catch (e) {
                    console.warn('Failed to parse cached messages:', e);
                }
            }

            // If no cache or cache is stale, fetch from backend
            if (messages.length === 0) {
                const resp = await fetch(`/session/${sessionId}/messages`);
                if (!resp.ok) {
                    console.warn(`Failed to fetch messages for session ${sessionId}:`, resp.status);
                    return;
                }
                const data = await resp.json();
                const msgs = Array.isArray(data) ? data : [];
                messages = msgs.map(m => ({
                    id: m.id || this.generateId(),
                    role: m.sent_by_user ? 'user' : 'assistant',
                    content: m.content,
                    timestamp: m.timestamp
                }));

                // Cache the messages
                localStorage.setItem(cacheKey, JSON.stringify({
                    messages: messages,
                    timestamp: new Date().toISOString()
                }));
                console.log('[DEBUG] Cached messages for session:', sessionId, 'count:', messages.length);
            }

            // Sort messages by timestamp (ascending order for display)
            const sortedMessages = messages.sort((a, b) => {
                const timeA = new Date(a.timestamp || 0).getTime();
                const timeB = new Date(b.timestamp || 0).getTime();
                return timeA - timeB;
            });

            if (this.currentSession && this.currentSession.id === sessionId) {
                this.currentSession.messages = sortedMessages;
                this.clearChatMessages();
                console.log('[DEBUG] Displaying', sortedMessages.length, 'messages for session:', sessionId);
                this.currentSession.messages.forEach(m => this.displayMessage(m));
                this.updateChatTitle();
                // Check if session needs title generation
                this.checkAndGenerateSessionTitle();
            } else {
                console.warn('[DEBUG] Session mismatch - current session:', this.currentSession?.id, 'requested session:', sessionId);
            }
        } catch (e) {
            console.error('Failed to hydrate session messages', e);
        }
    }

    bindPatientHandlers() {
        console.log('[DEBUG] bindPatientHandlers called');
        const loadBtn = document.getElementById('loadPatientBtn');
        console.log('[DEBUG] Load button found:', !!loadBtn);
        if (loadBtn) loadBtn.addEventListener('click', () => this.loadPatient());
        const patientInput = document.getElementById('patientIdInput');
        const suggestionsEl = document.getElementById('patientSuggestions');
        console.log('[DEBUG] Patient input found:', !!patientInput);
        console.log('[DEBUG] Suggestions element found:', !!suggestionsEl);
        if (!patientInput) return;
        let debounceTimer;
        const hideSuggestions = () => { if (suggestionsEl) suggestionsEl.style.display = 'none'; };
        const renderSuggestions = (items) => {
            if (!suggestionsEl) return;
            if (!items || items.length === 0) { hideSuggestions(); return; }
            suggestionsEl.innerHTML = '';
            items.forEach(p => {
                const div = document.createElement('div');
                div.className = 'patient-suggestion';
                div.textContent = `${p.name || 'Unknown'} (${p.id})`;
                div.addEventListener('click', async () => {
                    this.currentPatientId = p.id;
                    this.savePatientId();
                    patientInput.value = p.id;
                    hideSuggestions();
                    this.updatePatientDisplay(p.id, p.name || 'Unknown');
                    await this.fetchAndRenderPatientSessions();
                });
                suggestionsEl.appendChild(div);
            });
            suggestionsEl.style.display = 'block';
        };
        patientInput.addEventListener('input', () => {
            const q = patientInput.value.trim();
            console.log('[DEBUG] Patient input changed:', q);
            clearTimeout(debounceTimer);
            if (!q) { hideSuggestions(); return; }
            debounceTimer = setTimeout(async () => {
                try {
                    console.log('[DEBUG] Searching patients with query:', q);
                    const url = `/patient?q=${encodeURIComponent(q)}&limit=8`;
                    console.log('[DEBUG] Search URL:', url);
                    const resp = await fetch(url);
                    console.log('[DEBUG] Search response status:', resp.status);

                    let mongoResults = [];
                    if (resp.ok) {
                        const data = await resp.json();
                        mongoResults = data || [];
                        console.log('[DEBUG] MongoDB search results:', mongoResults);
                    } else {
                        console.warn('MongoDB search request failed', resp.status);
                    }

                    // Get localStorage suggestions as fallback/additional results
                    const localResults = await this.getLocalStorageSuggestions(q);

                    // Combine and deduplicate results (MongoDB results take priority)
                    const combinedResults = this.combinePatientResults(mongoResults, localResults);
                    console.log('[DEBUG] Combined search results:', combinedResults);
                    renderSuggestions(combinedResults);

                } catch (e) {
                    console.error('[DEBUG] Search error:', e);
                    // Fallback for network errors
                    console.log('[DEBUG] Trying fallback search after error');
                    await this.tryFallbackSearch(q, renderSuggestions);
                }
            }, 200);
        });
        patientInput.addEventListener('keydown', async (e) => {
            if (e.key === 'Enter') {
                const value = patientInput.value.trim();
                console.log('[DEBUG] Patient input Enter pressed with value:', value);
                hideSuggestions();
                await this.loadPatient();
            }
        });
        document.addEventListener('click', (ev) => {
            if (!suggestionsEl) return;
            if (!suggestionsEl.contains(ev.target) && ev.target !== patientInput) hideSuggestions();
        });
    }

    // Patient modal functionality
    setupPatientModal() {
        const profileBtn = document.getElementById('patientMenuBtn');
        const modal = document.getElementById('patientModal');
        const closeBtn = document.getElementById('patientModalClose');
        const logoutBtn = document.getElementById('patientLogoutBtn');
        const createBtn = document.getElementById('patientCreateBtn');

        if (profileBtn && modal) {
            profileBtn.addEventListener('click', async () => {
                const pid = this?.currentPatientId;
                if (pid) {
                    try {
                        const resp = await fetch(`/patient/${pid}`);
                        if (resp.ok) {
                            const p = await resp.json();
                            const name = p.name || 'Unknown';
                            const age = typeof p.age === 'number' ? p.age : '-';
                            const sex = p.sex || '-';
                            const meds = Array.isArray(p.medications) && p.medications.length > 0 ? p.medications.join(', ') : '-';
                            document.getElementById('patientSummary').textContent = `${name}${sex}, ${age}`;
                            document.getElementById('patientMedications').textContent = meds;
                            document.getElementById('patientAssessment').textContent = p.past_assessment_summary || '-';
                        }
                    } catch (e) {
                        console.error('Failed to load patient profile', e);
                    }
                }
                modal.classList.add('show');
            });
        }

        if (closeBtn && modal) {
            closeBtn.addEventListener('click', () => modal.classList.remove('show'));
            modal.addEventListener('click', (e) => { if (e.target === modal) modal.classList.remove('show'); });
        }

        if (logoutBtn) {
            logoutBtn.addEventListener('click', () => {
                if (confirm('Log out current patient?')) {
                    this.currentPatientId = null;
                    localStorage.removeItem('medicalChatbotPatientId');
                    const status = document.getElementById('patientStatus');
                    if (status) { status.textContent = 'No patient selected'; status.style.color = 'var(--text-secondary)'; }
                    const input = document.getElementById('patientIdInput');
                    if (input) input.value = '';
                    modal.classList.remove('show');
                }
            });
        }

        if (createBtn) createBtn.addEventListener('click', () => modal.classList.remove('show'));
    }

    // ================================================================================
    // AUDIO RECORDING FUNCTIONALITY
    // ================================================================================
    async initializeAudioRecording() {
        try {
            this.audioRecorder = new AudioRecordingUI(this);
            const success = await this.audioRecorder.initialize();

            if (success) {
                console.log('[Audio] Audio recording initialized successfully');
                // Make globally accessible for voice detection callback
                window.audioRecordingUI = this.audioRecorder;
            } else {
                console.warn('[Audio] Audio recording initialization failed');
            }
        } catch (error) {
            console.error('[Audio] Failed to initialize audio recording:', error);
        }
    }

    // ================================================================================
    // MESSAGING.JS FUNCTIONALITY
    // ================================================================================
    // Cache invalidation methods
    invalidateSessionCache(patientId) {
        const cacheKey = `sessions_${patientId}`;
        localStorage.removeItem(cacheKey);
        console.log('[DEBUG] Invalidated session cache for patient:', patientId);
    }

    invalidateMessageCache(patientId, sessionId) {
        const cacheKey = `messages_${patientId}_${sessionId}`;
        localStorage.removeItem(cacheKey);
        console.log('[DEBUG] Invalidated message cache for session:', sessionId);
    }

    invalidateAllCaches(patientId) {
        this.invalidateSessionCache(patientId);
        // Invalidate all message caches for this patient
        const keys = Object.keys(localStorage);
        keys.forEach(key => {
            if (key.startsWith(`messages_${patientId}_`)) {
                localStorage.removeItem(key);
            }
        });
        console.log('[DEBUG] Invalidated all caches for patient:', patientId);
    }

    sendMessage = async function () {
        const input = document.getElementById('chatInput');
        const message = input.value.trim();
        if (!message || this.isLoading) return;
        if (!this.currentPatientId) {
            const status = document.getElementById('patientStatus');
            if (status) { status.textContent = 'Select a patient before chatting.'; status.style.color = 'var(--warning-color)'; }
            return;
        }
        input.value = '';
        this.autoResizeTextarea(input);
        this.addMessage('user', message);
        this.showLoading(true);
        try {
            const responseData = await this.callMedicalAPI(message);
            this.addMessage('assistant', responseData.response || 'I apologize, but I received an empty response. Please try again.');
            this.updateCurrentSession();

            // Invalidate caches after successful message exchange
            if (this.currentSession && this.currentSession.id) {
                this.invalidateMessageCache(this.currentPatientId, this.currentSession.id);
                this.invalidateSessionCache(this.currentPatientId);
            }
        } catch (error) {
            console.error('Error sending message:', error);
            let errorMessage = 'I apologize, but I encountered an error processing your request.';
            if (error.message.includes('500')) errorMessage = 'The server encountered an internal error. Please try again in a moment.';
            else if (error.message.includes('404')) errorMessage = 'The requested service was not found. Please check your connection.';
            else if (error.message.includes('fetch')) errorMessage = 'Unable to connect to the server. Please check your internet connection.';
            this.addMessage('assistant', errorMessage);
        } finally {
            this.showLoading(false);
        }
    }

    callMedicalAPI = async function (message) {
        try {
            let sessionId = this.currentSession?.id;
            
            // If no session or default session, create a new one first
            if (!sessionId || sessionId === 'default') {
                console.log('[DEBUG] Creating new session before sending message');
                sessionId = await this.createNewSession();
                if (!sessionId) {
                    throw new Error('Failed to create new session');
                }
            }

            const messagePayload = {
                account_id: this.currentUser.id,
                patient_id: this.currentPatientId,
                message: message
            };

            const messageResponse = await fetch(`/session/${sessionId}/messages`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(messagePayload)
            });

            if (!messageResponse.ok) throw new Error(`HTTP error! status: ${messageResponse.status}`);
            const data = await messageResponse.json();
            return data;

        } catch (error) {
            console.error('API call failed:', error);
            console.error('Error details:', {
                message: error.message,
                stack: error.stack,
                user: this.currentUser,
                session: this.currentSession,
                patientId: this.currentPatientId
            });
            if (error.name === 'TypeError' && error.message.includes('fetch')) {
                return { response: this.generateMockResponse(message) };
            }
            throw error;
        }
    }

    createNewSession = async function () {
        try {
            const payload = {
                account_id: this.currentUser.id,
                patient_id: this.currentPatientId,
                title: "New Chat"
            };

            const response = await fetch('/session', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(payload)
            });
            
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            
            const session = await response.json();
            console.log('[DEBUG] Created new session:', session);
            
            // Update current session with the new session data
            this.currentSession = {
                id: session.id,
                title: session.title,
                messages: [],
                createdAt: session.created_at,
                lastActivity: new Date().toISOString(),
                source: 'backend'
            };
            
            return session.id;
        } catch (error) {
            console.error('Failed to create new session:', error);
            throw error;
        }
    }

    generateMockResponse(message) {
        const responses = [
            "Based on your question about medical topics, I can provide general information. However, please remember that this is for educational purposes only and should not replace professional medical advice.",
            "That's an interesting medical question. While I can offer some general insights, it's important to consult with healthcare professionals for personalized medical advice.",
            "I understand your medical inquiry. For accurate diagnosis and treatment recommendations, please consult with qualified healthcare providers who can assess your specific situation.",
            "Thank you for your medical question. I can provide educational information, but medical decisions should always be made in consultation with healthcare professionals.",
            "I appreciate your interest in medical topics. Remember that medical information found online should be discussed with healthcare providers for proper evaluation."
        ];
        return responses[Math.floor(Math.random() * responses.length)];
    }

    addMessage(role, content) {
        if (!this.currentSession) this.startNewChat();
        const message = { id: this.generateId(), role, content, timestamp: new Date().toISOString() };
        this.currentSession.messages.push(message);
        this.displayMessage(message);
        if (role === 'user' && this.currentSession.messages.length === 2) this.summariseAndSetTitle(content);
    }

    // Check if session needs title generation after messages are loaded
    checkAndGenerateSessionTitle() {
        if (!this.currentSession || !this.currentSession.messages) return;

        // Check if this is a new session that needs a title (exactly 2 messages: user + assistant)
        if (this.currentSession.messages.length === 2 &&
            this.currentSession.title === 'New Chat' &&
            this.currentSession.messages[0].role === 'user') {
            const firstMessage = this.currentSession.messages[0].content;
            this.summariseAndSetTitle(firstMessage);
        }
    }

    summariseAndSetTitle = async function (text) {
        try {
            const resp = await fetch('/summarise', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ text, max_words: 5 }) });
            if (resp.ok) {
                const data = await resp.json();
                const title = (data.title || 'New Chat').trim();
                this.currentSession.title = title;
                this.updateCurrentSession();
                this.updateChatTitle();
                this.loadChatSessions();
        } else {
                const fallback = text.length > 50 ? text.substring(0, 50) + '...' : text;
                this.currentSession.title = fallback;
                this.updateCurrentSession();
                this.updateChatTitle();
                this.loadChatSessions();
            }
        } catch (e) {
            const fallback = text.length > 50 ? text.substring(0, 50) + '...' : text;
            this.currentSession.title = fallback;
            this.updateCurrentSession();
            this.updateChatTitle();
            this.loadChatSessions();
        }
    }

    displayMessage(message) {
        const chatMessages = document.getElementById('chatMessages');
        const messageElement = document.createElement('div');
        messageElement.className = `message ${message.role}-message fade-in`;
        messageElement.id = `message-${message.id}`;
        const avatar = message.role === 'user' ? '<i class="fas fa-user"></i>' : '<i class="fas fa-robot"></i>';
        const time = this.formatTime(message.timestamp);

        // Add EMR icon for assistant messages (system-generated)
        const emrIcon = message.role === 'assistant' ?
            `<div class="message-actions">
                <button class="emr-extract-btn" onclick="app.extractEMR('${message.id}')" title="Extract to EMR" data-message-id="${message.id}">
                    <i class="fas fa-file-medical"></i>
                </button>
            </div>` : '';

        messageElement.innerHTML = `
            <div class="message-avatar">${avatar}</div>
            <div class="message-content">
                <div class="message-text">${this.formatMessageContent(message.content)}</div>
                <div class="message-time">${time}</div>
            </div>
            ${emrIcon}`;
        chatMessages.appendChild(messageElement);
        chatMessages.scrollTop = chatMessages.scrollHeight;
        if (this.currentSession) this.currentSession.lastActivity = new Date().toISOString();
    }

    formatMessageContent(content) {
        return content
            // Handle headers (1-6 # symbols) - create tagged titles
            .replace(/^#{1,6}\s+(.+)$/gm, (match, text, offset, string) => {
                const level = match.match(/^#+/)[0].length;
                return `<div class="tagged-title">
                    <div class="tagged-title-bar"></div>
                    <div class="tagged-title-content">
                        <h${level}>${text}</h${level}>
                    </div>
                </div>`;
            })
            // Handle bold text - create blue bubbles (improved regex to handle edge cases)
            .replace(/\*\*([^*]+)\*\*/g, '<span class="blue-bubble">$1</span>')
            // Handle italic text (only if not already processed as bold)
            .replace(/(?<!\*)\*([^*]+)\*(?!\*)/g, '<em>$1</em>')
            // Handle line breaks
            .replace(/\n/g, '<br>')
            // Handle emojis with colors
            .replace(/🔍/g, '<span style="color: var(--primary-color);">🔍</span>')
            .replace(/📋/g, '<span style="color: var(--secondary-color);">📋</span>')
            .replace(/💊/g, '<span style="color: var(--accent-color);">💊</span>')
            .replace(/📚/g, '<span style="color: var(--success-color);">📚</span>')
            .replace(/⚠️/g, '<span style="color: var(--warning-color);">⚠️</span>');
    }

    formatTime(timestamp) {
        const date = new Date(timestamp);
        const now = new Date();
        const diff = now - date;
        if (diff < 60000) return 'Just now';
        if (diff < 3600000) { const minutes = Math.floor(diff / 60000); return `${minutes} minute${minutes > 1 ? 's' : ''} ago`; }
        if (diff < 86400000) { const hours = Math.floor(diff / 3600000); return `${hours} hour${hours > 1 ? 's' : ''} ago`; }
        return date.toLocaleDateString();
    }

    async extractEMR(messageId) {
        try {
            // Check if patient is selected
            if (!this.currentPatientId) {
                alert('Please select a patient before extracting EMR data.');
                return;
            }

            // Check if doctor is logged in
            if (!this.currentUser) {
                alert('Please log in as a doctor before extracting EMR data.');
                return;
            }

            // Find the message
            const message = this.currentSession?.messages?.find(m => m.id === messageId);
            if (!message) {
                console.error('Message not found:', messageId);
                return;
            }

            // Show loading state
            const button = document.querySelector(`[onclick="app.extractEMR('${messageId}')"]`);
            if (button) {
                button.disabled = true;
                button.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
            }

            // Build URL with query parameters
            const params = new URLSearchParams({
                patient_id: this.currentPatientId,
                doctor_id: this.currentUser.id || 'default-doctor',
                message_id: messageId,
                session_id: this.currentSession?.id || 'default-session',
                message: message.content
            });
            const url = `/emr/extract?${params.toString()}`;

            // Call EMR extraction API
            const response = await fetch(url, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json', // Header might still be needed by middleware
                }
            });

            if (response.ok) {
                const result = await response.json();
                console.log('EMR extraction successful:', result);

                // Show success message
                if (button) {
                    button.innerHTML = '<i class="fas fa-check"></i>';
                    button.style.color = 'var(--success-color)';
                    setTimeout(() => {
                        button.innerHTML = '<i class="fas fa-file-medical"></i>';
                        button.style.color = '';
                        button.disabled = false;
                    }, 2000);
                }

                // Show notification
                this.showNotification('EMR data extracted successfully!', 'success');
            } else {
                const errorData = await response.json().catch(() => ({ detail: 'Unknown error' }));
                const errorMessage = errorData.detail || `HTTP ${response.status}: ${response.statusText}`;
                throw new Error(errorMessage);
            }

        } catch (error) {
            console.error('Error extracting EMR:', error);

            // Reset button state
            const button = document.querySelector(`[onclick="app.extractEMR('${messageId}')"]`);
            if (button) {
                button.innerHTML = '<i class="fas fa-file-medical"></i>';
                button.disabled = false;
            }

            // Show error message
            this.showNotification('Failed to extract EMR data. Please try again.', 'error');
        }
    }

    showNotification(message, type = 'info') {
        // Create notification element
        const notification = document.createElement('div');
        notification.className = `notification notification-${type}`;
        notification.innerHTML = `
            <div class="notification-content">
                <i class="fas fa-${type === 'success' ? 'check-circle' : type === 'error' ? 'exclamation-circle' : 'info-circle'}"></i>
                <span>${message}</span>
            </div>
        `;

        // Add to page
        document.body.appendChild(notification);

        // Show notification
        setTimeout(() => notification.classList.add('show'), 100);

        // Remove after 3 seconds
        setTimeout(() => {
            notification.classList.remove('show');
            setTimeout(() => notification.remove(), 300);
        }, 3000);
    }
}
// ----------------------------------------------------------
// Additional UI setup END
// ----------------------------------------------------------


// Initialize the app when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
    window.medicalChatbot = new MedicalChatbotApp();
});

// Handle system theme changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
    const themeSelect = document.getElementById('themeSelect');
    if (themeSelect && themeSelect.value === 'auto') {
        window.medicalChatbot.setTheme('auto');
    }
});

// Add patient registration link under patient ID form if not present
(function ensurePatientCreateLink() {
    document.addEventListener('DOMContentLoaded', () => {
        const patientSection = document.querySelector('.patient-section');
        const inputGroup = document.querySelector('.patient-input-group');
        if (patientSection && inputGroup) {
            let link = document.getElementById('createPatientLink');
            if (!link) {
                link = document.createElement('a');
                link.id = 'createPatientLink';
                link.href = '/static/patient.html';
                link.className = 'patient-create-link';
                link.title = 'Create new patient';
                link.innerHTML = '<i class="fas fa-user-plus"></i>';
                inputGroup.appendChild(link);
            }
        }
    });
})();


// ================================================================================
// RECORDER.JS FUNCTIONALITY
// ================================================================================

class AudioRecorder {
    constructor() {
        this.mediaRecorder = null;
        this.audioChunks = [];
        this.isRecording = false;
        this.audioContext = null;
        this.audioStream = null;
        this.analyser = null;
        this.silenceTimer = null;
        this.recordingStartTime = null;
        this.timerInterval = null;
    }

    async initialize() {
        try {
            // Request microphone access
            this.audioStream = await navigator.mediaDevices.getUserMedia({
                audio: {
                    sampleRate: 16000, // 16kHz for better speech recognition
                    channelCount: 1,   // Mono
                    echoCancellation: true,
                    noiseSuppression: true,
                    autoGainControl: true
                }
            });

            // Create audio context for voice detection
            this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
            const source = this.audioContext.createMediaStreamSource(this.audioStream);
            this.analyser = this.audioContext.createAnalyser();
            this.analyser.fftSize = 256;
            this.analyser.smoothingTimeConstant = 0.8;
            source.connect(this.analyser);

            // Create MediaRecorder
            this.mediaRecorder = new MediaRecorder(this.audioStream, {
                mimeType: 'audio/webm;codecs=opus'
            });

            // Set up event handlers
            this.mediaRecorder.ondataavailable = (event) => {
                if (event.data.size > 0) {
                    this.audioChunks.push(event.data);
                }
            };

            this.mediaRecorder.onstop = () => {
                this.processRecording();
            };

            return true;
        } catch (error) {
            console.error('Failed to initialize audio recorder:', error);
            throw new Error('Microphone access denied or not available');
        }
    }

    startRecording() {
        if (!this.mediaRecorder || this.isRecording) {
            return false;
        }

        try {
            this.audioChunks = [];
            this.mediaRecorder.start();
            this.isRecording = true;
            this.recordingStartTime = Date.now();
            console.log('Audio recording started');

            // Start timer
            this.startTimer();

            // Start voice detection
            this.startVoiceDetection();

            return true;
        } catch (error) {
            console.error('Failed to start recording:', error);
            return false;
        }
    }

    stopRecording() {
        if (!this.mediaRecorder || !this.isRecording) {
            return false;
        }

        try {
            this.mediaRecorder.stop();
            this.isRecording = false;
            console.log('Audio recording stopped');

            // Stop timer and voice detection
            this.stopTimer();
            this.stopVoiceDetection();

            return true;
        } catch (error) {
            console.error('Failed to stop recording:', error);
            return false;
        }
    }

    startTimer() {
        this.timerInterval = setInterval(() => {
            if (this.recordingStartTime) {
                const elapsed = Math.floor((Date.now() - this.recordingStartTime) / 1000);
                const minutes = Math.floor(elapsed / 60);
                const seconds = elapsed % 60;
                const timeString = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;

                const timerElement = document.getElementById('recordingTimer');
                if (timerElement) {
                    timerElement.textContent = timeString;
                }
            }
        }, 1000);
    }

    stopTimer() {
        if (this.timerInterval) {
            clearInterval(this.timerInterval);
            this.timerInterval = null;
        }
    }

    startVoiceDetection() {
        const checkVoice = () => {
            if (!this.isRecording || !this.analyser) return;

            const bufferLength = this.analyser.frequencyBinCount;
            const dataArray = new Uint8Array(bufferLength);
            this.analyser.getByteFrequencyData(dataArray);

            // Calculate average volume
            const average = dataArray.reduce((sum, value) => sum + value, 0) / bufferLength;
            const threshold = 20; // Adjust this value to change sensitivity

            const container = document.querySelector('.recording-container');
            const statusElement = document.getElementById('recordingStatus');

            if (average > threshold) {
                // Voice detected
                container.classList.remove('silent');
                container.classList.add('listening');
                if (statusElement) statusElement.textContent = 'Listening...';

                // Reset silence timer
                this.resetSilenceTimer();
            } else {
                // Silence detected
                container.classList.remove('listening');
                container.classList.add('silent');
                if (statusElement) statusElement.textContent = 'Silence detected...';
            }

            requestAnimationFrame(checkVoice);
        };

        checkVoice();
    }

    stopVoiceDetection() {
        // Voice detection stops when recording stops
    }

    resetSilenceTimer() {
        if (this.silenceTimer) {
            clearTimeout(this.silenceTimer);
        }

        // Auto-stop after 3 seconds of silence
        this.silenceTimer = setTimeout(() => {
            if (this.isRecording) {
                console.log('Auto-stopping recording due to silence');
                this.stopRecording();
                // Trigger the modal close and processing
                if (window.audioRecordingUI) {
                    window.audioRecordingUI.handleRecordingComplete();
                }
            }
        }, 3000);
    }

    async processRecording() {
        if (this.audioChunks.length === 0) {
            console.warn('No audio data recorded');
            return null;
        }

        try {
            // Create audio blob
            const audioBlob = new Blob(this.audioChunks, { type: 'audio/webm' });

            // Transcribe audio
            const transcribedText = await this.transcribeAudio(audioBlob);

            return transcribedText;
        } catch (error) {
            console.error('Failed to process recording:', error);
            throw error;
        }
    }

    async transcribeAudio(audioBlob) {
        try {
            const formData = new FormData();
            formData.append('file', audioBlob, 'recording.webm');
            formData.append('language_code', 'en');

            const response = await fetch('/audio/transcribe', {
                method: 'POST',
                body: formData
            });

            if (!response.ok) {
                const errorData = await response.json();
                throw new Error(errorData.detail || 'Transcription failed');
            }

            const result = await response.json();
            return result.transcribed_text;
        } catch (error) {
            console.error('Transcription failed:', error);
            throw error;
        }
    }

    cleanup() {
        if (this.audioStream) {
            this.audioStream.getTracks().forEach(track => track.stop());
            this.audioStream = null;
        }

        if (this.audioContext) {
            this.audioContext.close();
            this.audioContext = null;
        }

        if (this.silenceTimer) {
            clearTimeout(this.silenceTimer);
            this.silenceTimer = null;
        }

        this.stopTimer();

        this.mediaRecorder = null;
        this.audioChunks = [];
        this.isRecording = false;
    }

    isAvailable() {
        return !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia);
    }
}

// Audio recording modal UI controller
class AudioRecordingUI {
    constructor(app) {
        this.app = app;
        this.recorder = new AudioRecorder();
        this.microphoneBtn = null;
        this.isInitialized = false;
        this.modal = null;
    }

    async initialize() {
        if (!this.recorder.isAvailable()) {
            console.warn('Audio recording not supported in this browser');
            return false;
        }

        try {
            await this.recorder.initialize();
            this.setupUI();
            this.isInitialized = true;
            return true;
        } catch (error) {
            console.error('Failed to initialize audio recording UI:', error);
            this.showError('Microphone access denied. Please allow microphone access to use voice input.');
            return false;
        }
    }

    setupUI() {
        this.microphoneBtn = document.getElementById('microphoneBtn');
        this.modal = document.getElementById('audioRecordingModal');

        if (!this.microphoneBtn) {
            console.error('Microphone button not found');
            return;
        }

        if (!this.modal) {
            console.error('Audio recording modal not found');
            return;
        }

        // Set up event listeners
        this.microphoneBtn.addEventListener('click', (e) => this.startRecording(e));

        // Modal close handlers
        const closeBtn = document.getElementById('audioRecordingModalClose');
        const stopBtn = document.getElementById('stopRecordingBtn');

        if (closeBtn) {
            closeBtn.addEventListener('click', () => this.closeModal());
        }

        if (stopBtn) {
            stopBtn.addEventListener('click', () => this.stopRecording());
        }

        // Close modal when clicking outside
        this.modal.addEventListener('click', (e) => {
            if (e.target === this.modal) {
                this.closeModal();
            }
        });

        // Update button appearance
        this.updateButtonState('ready');
    }

    async startRecording(event) {
        if (!this.isInitialized || this.recorder.isRecording) {
            return;
        }

        event.preventDefault();

        try {
            // Show modal
            this.showModal();

            // Start recording
            const success = this.recorder.startRecording();
            if (success) {
                this.updateModalState('listening');
            } else {
                this.showError('Failed to start recording. Please try again.');
                this.closeModal();
            }
        } catch (error) {
            console.error('Failed to start recording:', error);
            this.showError('Failed to start recording. Please try again.');
            this.closeModal();
        }
    }

    async stopRecording() {
        if (!this.recorder.isRecording) {
            return;
        }

        try {
            const success = this.recorder.stopRecording();
            if (success) {
                this.updateModalState('processing');
                this.handleRecordingComplete();
            }
        } catch (error) {
            console.error('Failed to stop recording:', error);
            this.showError('Failed to stop recording. Please try again.');
            this.closeModal();
        }
    }

    async handleRecordingComplete() {
        try {
            // Process the recording
            const transcribedText = await this.recorder.processRecording();

            if (transcribedText) {
                this.insertTranscribedText(transcribedText);
                this.showSuccess('Audio transcribed successfully!');
            } else {
                this.showError('No speech detected. Please try again.');
            }

            this.closeModal();
        } catch (error) {
            console.error('Failed to process recording:', error);
            this.showError('Transcription failed. Please try again.');
            this.closeModal();
        }
    }

    showModal() {
        if (this.modal) {
            this.modal.classList.add('show');
            document.body.style.overflow = 'hidden'; // Prevent background scrolling
        }
    }

    closeModal() {
        if (this.modal) {
            this.modal.classList.remove('show');
            document.body.style.overflow = ''; // Restore scrolling

            // Stop recording if still active
            if (this.recorder.isRecording) {
                this.recorder.stopRecording();
            }

            // Reset modal state
            this.updateModalState('ready');
        }
    }

    updateModalState(state) {
        const container = document.querySelector('.recording-container');
        const statusElement = document.getElementById('recordingStatus');
        const stopBtn = document.getElementById('stopRecordingBtn');

        if (!container) return;

        // Remove all state classes
        container.classList.remove('listening', 'silent', 'processing');

        switch (state) {
            case 'ready':
                container.classList.add('listening');
                if (statusElement) statusElement.textContent = 'Ready to record...';
                if (stopBtn) stopBtn.style.display = 'none';
                break;
            case 'listening':
                container.classList.add('listening');
                if (statusElement) statusElement.textContent = 'Listening...';
                if (stopBtn) stopBtn.style.display = 'block';
                break;
            case 'processing':
                container.classList.add('processing');
                if (statusElement) statusElement.textContent = 'Processing audio...';
                if (stopBtn) stopBtn.style.display = 'none';
                break;
        }
    }

    insertTranscribedText(text) {
        const chatInput = document.getElementById('chatInput');
        if (!chatInput) {
            console.error('Chat input not found');
            return;
        }

        // Append transcribed text to existing content
        const currentText = chatInput.value.trim();
        const newText = currentText ? `${currentText} ${text}` : text;

        chatInput.value = newText;

        // Add visual feedback for transcribed text
        chatInput.classList.add('transcribed');

        // Remove the highlighting after a few seconds
        setTimeout(() => {
            chatInput.classList.remove('transcribed');
        }, 3000);

        // Trigger input event to update UI
        chatInput.dispatchEvent(new Event('input', { bubbles: true }));

        // Focus the input
        chatInput.focus();

        // Auto-resize if needed
        if (this.app && this.app.autoResizeTextarea) {
            this.app.autoResizeTextarea(chatInput);
        }
    }

    updateButtonState(state) {
        if (!this.microphoneBtn) return;

        // Remove all state classes
        this.microphoneBtn.classList.remove('recording-ready', 'recording-active', 'recording-processing');

        // Add appropriate state class
        switch (state) {
            case 'ready':
                this.microphoneBtn.classList.add('recording-ready');
                this.microphoneBtn.title = 'Click to record voice input';
                break;
            case 'recording':
                this.microphoneBtn.classList.add('recording-active');
                this.microphoneBtn.title = 'Recording... Click to stop';
                break;
            case 'processing':
                this.microphoneBtn.classList.add('recording-processing');
                this.microphoneBtn.title = 'Processing audio...';
                break;
        }
    }

    showError(message) {
        // Create or update error message
        let errorMsg = document.getElementById('audioError');
        if (!errorMsg) {
            errorMsg = document.createElement('div');
            errorMsg.id = 'audioError';
            errorMsg.className = 'audio-error-message';

            const chatInputContainer = document.querySelector('.chat-input-container');
            if (chatInputContainer) {
                chatInputContainer.appendChild(errorMsg);
            }
        }

        errorMsg.textContent = message;
        errorMsg.style.display = 'block';

        // Hide after 5 seconds
        setTimeout(() => {
            errorMsg.style.display = 'none';
        }, 5000);
    }

    showSuccess(message) {
        // Create or update success message
        let successMsg = document.getElementById('audioSuccess');
        if (!successMsg) {
            successMsg = document.createElement('div');
            successMsg.id = 'audioSuccess';
            successMsg.className = 'audio-success-message';

            const chatInputContainer = document.querySelector('.chat-input-container');
            if (chatInputContainer) {
                chatInputContainer.appendChild(successMsg);
            }
        }

        successMsg.textContent = message;
        successMsg.style.display = 'block';

        // Hide after 3 seconds
        setTimeout(() => {
            successMsg.style.display = 'none';
        }, 3000);
    }

    cleanup() {
        if (this.recorder) {
            this.recorder.cleanup();
        }
    }
}


// Settings modal open/close wiring (from settings.js)
document.addEventListener('DOMContentLoaded', () => {
    const settingsBtn = document.getElementById('settingsBtn');
    const modal = document.getElementById('settingsModal');
    const closeBtn = document.getElementById('settingsModalClose');
    const cancelBtn = document.getElementById('settingsModalCancel');
    if (settingsBtn && modal) settingsBtn.addEventListener('click', () => modal.classList.add('show'));
    if (closeBtn) closeBtn.addEventListener('click', () => modal.classList.remove('show'));
    if (cancelBtn) cancelBtn.addEventListener('click', () => modal.classList.remove('show'));
    if (modal) modal.addEventListener('click', (e) => { if (e.target === modal) modal.classList.remove('show'); });
});

// Patient modal open/close wiring (from patient.js)
document.addEventListener('DOMContentLoaded', () => {
    const profileBtn = document.getElementById('patientMenuBtn');
    const modal = document.getElementById('patientModal');
    const closeBtn = document.getElementById('patientModalClose');
    const logoutBtn = document.getElementById('patientLogoutBtn');
    const createBtn = document.getElementById('patientCreateBtn');
    if (profileBtn && modal) {
        profileBtn.addEventListener('click', async () => {
            const pid = window.medicalChatbot?.currentPatientId;
            if (pid) {
                try {
                    const resp = await fetch(`/patient/${pid}`);
                    if (resp.ok) {
                        const p = await resp.json();
                        const name = p.name || 'Unknown';
                        const age = typeof p.age === 'number' ? p.age : '-';
                        const sex = p.sex || '-';
                        const meds = Array.isArray(p.medications) && p.medications.length > 0 ? p.medications.join(', ') : '-';
                        document.getElementById('patientSummary').textContent = `${name}${sex}, ${age}`;
                        document.getElementById('patientMedications').textContent = meds;
                        document.getElementById('patientAssessment').textContent = p.past_assessment_summary || '-';
                    }
                } catch (e) {
                    console.error('Failed to load patient profile', e);
                }
            }
            modal.classList.add('show');
        });
    }
    if (closeBtn && modal) {
        closeBtn.addEventListener('click', () => modal.classList.remove('show'));
        modal.addEventListener('click', (e) => { if (e.target === modal) modal.classList.remove('show'); });
    }
    if (logoutBtn) {
        logoutBtn.addEventListener('click', () => {
            if (confirm('Log out current patient?')) {
                window.medicalChatbot.currentPatientId = null;
                localStorage.removeItem('medicalChatbotPatientId');
                const status = document.getElementById('patientStatus');
                if (status) { status.textContent = 'No patient selected'; status.style.color = 'var(--text-secondary)'; }
                const input = document.getElementById('patientIdInput');
                if (input) input.value = '';
                modal.classList.remove('show');
            }
        });
    }
    if (createBtn) createBtn.addEventListener('click', () => modal.classList.remove('show'));
});

// Doctor modal open/close wiring (from doctor.js)
document.addEventListener('DOMContentLoaded', () => {
    const doctorCard = document.getElementById('userProfile');
    const userModal = document.getElementById('userModal');
    const closeBtn = document.getElementById('userModalClose');
    const cancelBtn = document.getElementById('userModalCancel');
    if (doctorCard && userModal) {
        doctorCard.addEventListener('click', () => userModal.classList.add('show'));
    }
    if (closeBtn) closeBtn.addEventListener('click', () => userModal.classList.remove('show'));
    if (cancelBtn) cancelBtn.addEventListener('click', () => userModal.classList.remove('show'));
    if (userModal) {
        userModal.addEventListener('click', (e) => { if (e.target === userModal) userModal.classList.remove('show'); });
    }
});