File size: 77,479 Bytes
d8635c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<script lang="ts">
  import { onMount } from 'svelte'
  import { questions, rfpData, nextStep, prevStep, isLoading, referenceDocuments } from '../stores/appStore'
  import { generateAnswer as generateAIAnswer } from '../utils/aiGenerator'
  import { backendService } from '../utils/backendService'
  import { suggestionEngine } from '../utils/suggestionEngine'
  import { vectorStorage } from '../utils/vectorStorage'
  import type { RFPAnswer } from '../stores/appStore'
  import RFPAnalyzer from './RFPAnalyzer.svelte'
  
  let selectedQuestions: Set<number> = new Set()
  let generatedAnswers: RFPAnswer[] = []
  let selectedCategory: string = 'all' // Default to all categories
  let currentQuestionIndex: number = 0
  let showAnalyzer: boolean = false // Toggle for RFP Analyzer view
  
  // Answer type selection
  let selectedAnswerType: string = 'ai' // 'ai' for AI answer, 'previous' for previous answer
  
  $: questionsToProcess = $questions
  $: allSelected = selectedQuestions.size === questionsToProcess.length
  $: currentQuestion = questionsToProcess[currentQuestionIndex]
  $: currentAnswerId = `answer-${currentQuestionIndex}`
  $: currentAnswer = $rfpData.answers?.find(a => a.id === currentAnswerId)
  
  // Track formatted answers
  let formattedAnswers = new Map()
  let formattingInProgress = new Set()
  
  // Additional information for AI processing
  let additionalInfo = ""
  
  // Auto-generate answers when current question changes
  $: if (currentQuestion && !currentAnswer && !$isLoading && $rfpData.extractedData?.length > 0) {
    console.log(`πŸ€– Auto-generating answer for question ${currentQuestionIndex + 1}: "${currentQuestion.text.substring(0, 50)}..."`)
    generateAnswer()
  }
  
  // Auto-select all questions when questions change
  $: if (questionsToProcess.length > 0 && selectedQuestions.size === 0) {
    selectedQuestions = new Set(questionsToProcess.map((_, index) => index))
  }
  
  // Get available categories from reference documents
  $: availableCategories = [...new Set($referenceDocuments.map(doc => doc.category))].filter(Boolean)
  
  function toggleQuestion(index: number) {
    if (selectedQuestions.has(index)) {
      selectedQuestions.delete(index)
    } else {
      selectedQuestions.add(index)
    }
    selectedQuestions = new Set(selectedQuestions)
  }
  
  function toggleAllQuestions() {
    if (allSelected) {
      selectedQuestions.clear()
    } else {
      selectedQuestions = new Set(questionsToProcess.map((_, index) => index))
    }
    selectedQuestions = new Set(selectedQuestions)
  }
  
  function nextQuestion() {
    if (currentQuestionIndex < questionsToProcess.length - 1) {
      currentQuestionIndex++
    }
  }
  
  function prevQuestion() {
    if (currentQuestionIndex > 0) {
      currentQuestionIndex--
    }
  }
  
  function goToQuestion(index: number) {
    currentQuestionIndex = index
  }
  
  // Function to reprocess answer with additional information
  async function reprocessAnswerWithAdditionalInfo(answer) {
    if (!answer || !additionalInfo.trim()) return
    
    isLoading.set(true)
    console.log('πŸ”„ Reprocessing answer with additional information:', additionalInfo.trim())
    
    try {
      // Combine original answer with additional information
      const enhancedContext = `${answer.aiGeneratedAnswer || answer.finalAnswer}

      

Additional Requirements/Context:

${additionalInfo.trim()}`
      
      // Format the enhanced answer using the AI API
      const formatResult = await backendService.formatAnswer(
        answer.question,
        enhancedContext,
        `Enhanced with user input: ${additionalInfo.trim().substring(0, 100)}...`
      )
      
      if (formatResult.success) {
        // Update the answer with the enhanced version
        rfpData.update(data => {
          const answers = data.answers || []
          const answerIndex = answers.findIndex(a => a.id === answer.id)
          
          if (answerIndex >= 0) {
            answers[answerIndex] = {
              ...answers[answerIndex],
              finalAnswer: formatResult.formattedAnswer,
              isFormatted: true,
              hasAdditionalInfo: true,
              additionalInfo: additionalInfo.trim()
            }
          }
          
          return { ...data, answers }
        })
        
        // Clear the additional info input
        additionalInfo = ""
        
        console.log('βœ… Answer reprocessed successfully with additional information')
      } else {
        console.warn('Failed to reprocess answer:', formatResult)
      }
    } catch (error) {
      console.error('Error reprocessing answer with additional info:', error)
    } finally {
      isLoading.set(false)
    }
  }
  
  // Function to format a previous answer using AI
  async function formatPreviousAnswer(answer, questionText) {
    const answerId = `${answer.docName}-${answer.question.substring(0, 50)}`
    
    if (formattingInProgress.has(answerId)) {
      console.log('⏳ Already formatting this answer:', answerId)
      return // Already formatting this answer
    }
    
    if (formattedAnswers.has(answerId)) {
      console.log('βœ… Answer already formatted:', answerId)
      return formattedAnswers.get(answerId) // Already formatted
    }
    
    console.log('πŸš€ Starting format for answer:', answerId)
    formattingInProgress.add(answerId)
    formattingInProgress = formattingInProgress // Trigger reactivity
    
    try {
      console.log('πŸ“ Formatting previous answer:', answer.question.substring(0, 60) + '...')
      console.log('πŸ“„ Answer text length:', answer.answer?.length || 0)
      console.log('πŸ”§ Question text:', questionText || answer.question)
      
      const result = await backendService.formatAnswer(
        questionText || answer.question,
        answer.answer,
        `Document: ${answer.docName}, Page: ${answer.pageNumber}`
      )
      
      console.log('πŸ“₯ Format result received:', result)
      
      if (result.success) {
        const formatted = {
          ...answer,
          formattedAnswer: result.formattedAnswer,
          originalAnswer: result.originalAnswer
        }
        formattedAnswers.set(answerId, formatted)
        formattedAnswers = formattedAnswers // Trigger reactivity
        console.log('βœ… Successfully formatted answer for:', answerId)
        return formatted
      } else {
        console.warn('⚠️ Failed to format answer:', result)
        return answer
      }
    } catch (error) {
      console.error('❌ Error formatting previous answer:', error)
      console.error('❌ Error details:', {
        message: error.message,
        stack: error.stack,
        answerId,
        questionText: questionText || answer.question
      })
      return answer
    } finally {
      console.log('🏁 Cleaning up format process for:', answerId)
      formattingInProgress.delete(answerId)
      formattingInProgress = formattingInProgress // Trigger reactivity
    }
  }

  
  // Function to extract relevant previous answers from reference documents
  function getPreviousAnswers(questionText: string) {
    console.log('πŸ” HYBRID SEARCH: Regex + Vector for question:', questionText)
    
    if (!questionText || questionText.length < 10) {
      console.log('❌ Question too short for search')
      return []
    }
    
    const allMatches = []
    
    // ============================================
    // STAGE 1: REGEX PATTERN MATCHING (Fast & Precise)
    // ============================================
    console.log('πŸ“‹ STAGE 1: Trying regex pattern matching...')
    
    if (referenceDocsWithContent && referenceDocsWithContent.length > 0) {
      for (const doc of referenceDocsWithContent) {
        if (selectedCategory !== 'all' && doc.category !== selectedCategory) {
          continue
        }
        
        if (!doc.content || doc.content.length < 50) continue
        
        // Try to find exact question match using regex
        const questionAnswerPairs = extractQuestionAnswerPairs(doc.content, doc.name, questionText)
        
        for (const pair of questionAnswerPairs) {
          // Check for exact or very high similarity match
          const similarity = calculateSimpleTextSimilarity(questionText.toLowerCase(), pair.question.toLowerCase())
          const percentage = Math.round(similarity * 100)
          
          if (percentage >= 85) { // High confidence regex match
            console.log(`  βœ… REGEX MATCH: ${doc.name} - ${percentage}% - "${pair.question.substring(0, 60)}..."`)
            
            allMatches.push({
              docName: doc.name,
              question: pair.question,
              answer: pair.answer,
              content: `Q: ${pair.question}\n\nA: ${pair.answer}`,
              matchScore: similarity,
              matchPercentage: percentage,
              category: doc.category || 'General',
              pageNumber: pair.pageNumber || 1,
              sectionType: percentage >= 95 ? 'exact-match' : 'high-confidence-match',
              source: 'regex-pattern',
              isExactMatch: percentage >= 95,
              extractionStrategy: pair.extractionStrategy || 'regex'
            })
            
            // If exact match (95%+), return immediately - no need for vector search
            if (percentage >= 95) {
              console.log('🎯 EXACT REGEX MATCH - Returning immediately')
              return [allMatches[allMatches.length - 1]]
            }
          }
        }
      }
    }
    
    // ============================================
    // STAGE 2: VECTOR SEARCH (Semantic & Fuzzy)
    // ============================================
    console.log('🧠 STAGE 2: Trying vector semantic search...')
    
    try {
      const searchOptions = {
        topK: 10, // Increased from 5 to get more results
        categoryFilter: selectedCategory === 'all' ? undefined : selectedCategory,
        expandQuery: true
      }
      
      const searchResults = vectorStorage.semanticSearch(questionText, searchOptions)
      console.log(`  βœ… Found ${searchResults.length} vector matches`)
      
      // Add vector results
      searchResults.forEach(result => {
        const score = Math.round(result.score * 100)
        
        console.log(`    πŸ“„ ${result.chunk.metadata.source}: ${score}% match`)
        console.log(`    πŸ”‘ Keywords: ${result.matchedKeywords.join(', ')}`)
        
        allMatches.push({
          docName: result.chunk.metadata.source,
          question: 'Relevant Content', // Vector finds content, not Q&A
          answer: result.chunk.content,
          content: result.chunk.content,
          matchScore: result.score,
          matchPercentage: score,
          category: result.chunk.metadata.category || 'General',
          matchedKeywords: result.matchedKeywords,
          pageNumber: result.chunk.metadata.pageNumber || 1,
          chunkIndex: result.chunk.metadata.chunkIndex,
          sectionType: 'vector-match',
          source: 'vector-search',
          isExactMatch: false
        })
      })
    } catch (error) {
      console.error('❌ Vector search failed:', error)
    }
    
    // ============================================
    // COMBINE & RANK RESULTS
    // ============================================
    console.log(`πŸ“Š Total matches: ${allMatches.length} (regex + vector)`)
    
    // Log all match scores for debugging
    if (allMatches.length > 0) {
      console.log('πŸ“ˆ All match scores:')
      allMatches.forEach(m => {
        console.log(`  ${m.docName}: ${m.matchPercentage}% (${m.source})`)
      })
    }
    
    // Filter weak matches - lowered to 10% to catch semantic matches
    const strongMatches = allMatches.filter(match => match.matchPercentage >= 10)
    
    if (strongMatches.length === 0) {
      console.log('⚠️ No strong matches found (minimum 10% required)')
      return []
    }
    
    console.log(`βœ… ${strongMatches.length} matches above 10% threshold`)
    
    // Prioritize regex matches, then vector matches
    const sortedMatches = strongMatches.sort((a, b) => {
      // Exact matches first
      if (a.isExactMatch && !b.isExactMatch) return -1
      if (!a.isExactMatch && b.isExactMatch) return 1
      
      // Regex matches before vector matches
      if (a.source === 'regex-pattern' && b.source === 'vector-search') return -1
      if (a.source === 'vector-search' && b.source === 'regex-pattern') return 1
      
      // Then by score
      return b.matchPercentage - a.matchPercentage
    })
    
    const topMatches = sortedMatches.slice(0, 3)
    console.log('πŸ† Top 3 matches:')
    topMatches.forEach(m => {
      console.log(`  ${m.source === 'regex-pattern' ? 'πŸ“‹' : '🧠'} ${m.docName}: ${m.matchPercentage}% (${m.source})`)
    })
    
    return topMatches
  }
  
  // Simple text similarity for regex results
  function calculateSimpleTextSimilarity(text1: string, text2: string): number {
    const words1 = text1.split(/\s+/).filter(w => w.length > 2)
    const words2 = text2.split(/\s+/).filter(w => w.length > 2)
    
    const set1 = new Set(words1)
    const set2 = new Set(words2)
    
    let matchCount = 0
    set1.forEach(word => {
      if (set2.has(word)) matchCount++
    })
    
    return matchCount / Math.max(set1.size, set2.size)
  }
  
  // Function to deduplicate similar answers
  function deduplicateAnswers(matches: any[]) {
    const uniqueMatches = []
    const seenAnswers = new Set()
    
    for (const match of matches) {
      // Create a normalized version of the answer for comparison
      const normalizedAnswer = match.answer?.toLowerCase()
        .replace(/\s+/g, ' ')
        .replace(/[^\w\s]/g, '')
        .trim() || ''
      
      // Skip if we've seen a very similar answer
      let isDuplicate = false
      for (const seenAnswer of seenAnswers) {
        const similarity = calculateTextSimilarity(normalizedAnswer, seenAnswer)
        if (similarity > 0.8) { // 80% similarity threshold
          isDuplicate = true
          break
        }
      }
      
      if (!isDuplicate && normalizedAnswer.length > 50) {
        uniqueMatches.push(match)
        seenAnswers.add(normalizedAnswer)
      }
    }
    
    return uniqueMatches
  }
  
  // Simple text similarity calculation
  function calculateTextSimilarity(text1: string, text2: string): number {
    const words1 = new Set(text1.split(' ').filter(w => w.length > 3))
    const words2 = new Set(text2.split(' ').filter(w => w.length > 3))
    
    const intersection = new Set([...words1].filter(x => words2.has(x)))
    const union = new Set([...words1, ...words2])
    
    return union.size > 0 ? intersection.size / union.size : 0
  }
  
  // Enhanced question-answer pair extraction
  function extractQuestionAnswerPairs(content: string, docName: string, searchQuestion?: string) {
    const pairs = []
    
    console.log(`πŸ“„ Extracting Q&A pairs from ${docName}...`)
    
    // FIX: Normalize PDF text that has spaces between every character
    // Common OCR/PDF extraction issue: "E d g e M a r k e t" -> "EdgeMarket"
    // Check if content has this pattern
    const sampleText = content.substring(0, 500)
    const spacedChars = (sampleText.match(/\b[A-Za-z]\s(?=[A-Za-z]\s)/g) || []).length
    const totalChars = sampleText.replace(/\s/g, '').length
    
    // If more than 20% of characters are spaced individually, fix it
    if (spacedChars > totalChars * 0.2) {
      console.log(`   πŸ”§ Detected spaced character formatting (${Math.round(spacedChars/totalChars*100)}% spaced), fixing...`)
      // Remove spaces between individual letters while preserving word boundaries
      // "S h e l b y  C o u n t y" -> "Shelby County"
      content = content.replace(/([A-Za-z])\s+(?=[A-Za-z](?:\s[A-Za-z]|\b))/g, '$1')
      // Clean up excessive whitespace
      content = content.replace(/\s{3,}/g, '  ').replace(/\s{2}/g, ' ')
      console.log(`   βœ… Text normalized, sample: "${content.substring(0, 100)}"`)
    }
    
    // Strategy 1: Split by numbered questions (1., 2., 3., etc.)
    const numberedSections = content.split(/(?=^\s*\d+\.)/m)
    
    for (let i = 0; i < numberedSections.length; i++) {
      const section = numberedSections[i].trim()
      if (section.length < 20) continue
      
      // Extract question from numbered format
      const questionMatch = section.match(/^\s*(\d+)\.\s*(.+?)(?:\n\n|\n[A-Z]|\n\d+\.|\s{3,}|$)/s)
      if (questionMatch) {
        const questionNumber = questionMatch[1]
        const questionText = questionMatch[2].trim()
        
        // Extract answer (everything after the question)
        let answerText = section.replace(questionMatch[0], '').trim()
        
        // Clean up answer text
        answerText = answerText.replace(/^\n+/, '').replace(/\n+$/, '')
        
        if (questionText.length > 10 && answerText.length > 20) {
          pairs.push({
            questionNumber: parseInt(questionNumber),
            question: questionText,
            answer: answerText,
            pageNumber: Math.max(1, Math.ceil(i / 300)) + 1, // More realistic page estimate
            sectionIndex: i
          })
          
          console.log(`   βœ… Q${questionNumber}: "${questionText.substring(0, 50)}..." -> ${answerText.length} chars`)
        }
      }
    }
    
    // Strategy 2: Look for Q: A: patterns
    const qaMatches = content.match(/Q[:\s]+(.*?)(?:\n|\r\n)A[:\s]+(.*?)(?=Q[:\s]+|$)/gs)
    if (qaMatches) {
      qaMatches.forEach((match, index) => {
        const parts = match.match(/Q[:\s]+(.*?)(?:\n|\r\n)A[:\s]+(.*)/s)
        if (parts) {
          pairs.push({
            questionNumber: pairs.length + 1,
            question: parts[1].trim(),
            answer: parts[2].trim(),
            pageNumber: Math.max(1, Math.ceil(index / 300)) + 1, // More realistic page estimate
            sectionIndex: index
          })
        }
      })
    }
    
    // Strategy 3: Look for section headers with content (for RFP documents)
    // Split content into paragraphs and look for header-like text followed by content
    const paragraphs = content.split(/\n\s*\n/).filter(p => p.trim().length > 0)
    
    for (let i = 0; i < paragraphs.length - 1; i++) {
      const currentPara = paragraphs[i].trim()
      const nextPara = paragraphs[i + 1]?.trim()
      
      // Check if current paragraph looks like a question/header
      const isQuestionLike = (
        currentPara.length > 10 && currentPara.length < 200 && // Reasonable length for question
        (currentPara.includes('?') || // Contains question mark
         currentPara.match(/^[A-Z][^.!?]*[A-Za-z]$/) || // Capitalized statement without ending punctuation
         currentPara.match(/Company|Background|Experience|Profile|Founded|Describe|Provide|Explain/i)) && // Common RFP terms
        !currentPara.match(/^\d+\./) && // Not already a numbered question
        nextPara && nextPara.length > 50 // Has substantial content following
      )
      
      if (isQuestionLike && nextPara) {
        // Collect answer content (current and potentially next few paragraphs)
        let answerContent = nextPara
        let j = i + 2
        while (j < paragraphs.length && j < i + 5) { // Look ahead max 3 more paragraphs
          const para = paragraphs[j]?.trim()
          if (para && para.length > 20 && !para.match(/^[A-Z][^.!?]*[A-Za-z]$/)) {
            answerContent += '\n\n' + para
            j++
          } else {
            break
          }
        }
        
        if (answerContent.length > 50) {
          pairs.push({
            questionNumber: pairs.length + 1,
            question: currentPara,
            answer: answerContent,
            pageNumber: Math.max(1, Math.ceil(i / 250)) + 1, // More realistic page estimate
            sectionIndex: i,
            extractionStrategy: 'section-header'
          })
          
          console.log(`   βœ… Header Q: "${currentPara.substring(0, 50)}..." -> ${answerContent.length} chars`)
        }
      }
    }
    
    // Strategy 4: Direct text matching - Look for the exact question in content
    const currentQuestionText = searchQuestion?.trim()
    if (currentQuestionText && currentQuestionText.length > 10) {
      const searchText = currentQuestionText.toLowerCase()
      let contentLower = content.toLowerCase()
      
      // SKIP PDF FRONT MATTER - Find where real content starts
      const executiveSummaryIndex = contentLower.indexOf('executive summar')
      const tabCIndex = contentLower.indexOf('tab c:')
      const contentStartIndex = Math.max(0, executiveSummaryIndex, tabCIndex)
      
      if (contentStartIndex > 0) {
        console.log(`πŸ“„ Skipping ${contentStartIndex} chars of front matter (cover page, TOC)`)
        content = content.substring(contentStartIndex)
        contentLower = content.toLowerCase()
      }
      
      const exactMatchIndex = contentLower.indexOf(searchText)
      if (exactMatchIndex !== -1) {
        // Find the end of the question text in the document
        const questionEndIndex = exactMatchIndex + searchText.length
        
        // Extract answer content starting after the question
        let answerStartIndex = questionEndIndex
        
        // Skip any immediate whitespace or formatting characters after the question
        while (answerStartIndex < content.length && 
               (content[answerStartIndex] === ' ' || content[answerStartIndex] === '\n' || 
                content[answerStartIndex] === '\r' || content[answerStartIndex] === '\t')) {
          answerStartIndex++
        }
        
        // Extract ONLY the immediate answer content below the question
        let answerEndIndex = answerStartIndex
        let sentenceCount = 0
        let paragraphCount = 0
        let foundContent = false
        
        // Look for a focused answer - stop at logical boundaries
        while (answerEndIndex < content.length) {
          const currentChar = content[answerEndIndex]
          const nextChars = content.substring(answerEndIndex, answerEndIndex + 100).toLowerCase()
          const currentSection = content.substring(answerStartIndex, answerEndIndex)
          
          // Count sentences and paragraphs to limit content
          if (currentChar === '.' || currentChar === '!' || currentChar === '?') {
            sentenceCount++
          }
          if (currentChar === '\n' && content[answerEndIndex + 1] === '\n') {
            paragraphCount++
          }
          
          // CONSERVATIVE stopping criteria - extract ONLY direct answer
          if (foundContent && (
            // Stop at new questions or major sections
            nextChars.match(/^\s*\d+\.\s*[A-Z]/) || // New numbered question/section
            nextChars.match(/^[A-Z][A-Z\s&]{8,}:/) || // Section headers like "COMPANY DETAILS:"
            nextChars.includes('question:') ||
            nextChars.includes('response:') ||
            nextChars.includes('answer:') ||
            nextChars.match(/^\s*q\d+/i) || // Q1, Q2, etc.
            nextChars.match(/^\s*section\s+[a-z\d]/i) || // Section markers
            
            // Stop at metadata sections and tables
            nextChars.includes('company details') ||
            nextChars.includes('year founded') ||
            nextChars.includes('total employees') ||
            nextChars.includes('contact information') ||
            nextChars.includes('business address') ||
            nextChars.includes('incorporation') ||
            nextChars.match(/entity\s+duration/i) || // Contract tables
            nextChars.match(/here is a sample/i) || // Sample sections
            nextChars.includes('entity') && nextChars.includes('duration') && nextChars.includes('value') ||
            
            // MUCH MORE CONSERVATIVE - Stop after short focused answer
            sentenceCount >= 3 || // Max 3 sentences only
            paragraphCount >= 2 || // Max 2 paragraphs only  
            (answerEndIndex - answerStartIndex > 500) || // Max 500 chars (very focused!)
            
            // Stop at table-like data or lists of specs
            nextChars.match(/^\s*\|\s*\w+\s*\|/) || // Table rows
            nextChars.match(/^\s*[β€’Β·-]\s*\w+:/) || // Spec lists
            
            // Stop at new page indicators or RFP metadata
            nextChars.includes('page ') ||
            nextChars.includes('continued on') ||
            nextChars.includes('tab ') ||
            nextChars.match(/rfp #\d/i) ||
            nextChars.includes('proposal due date')
          )) {
            break
          }
          
          if (currentChar.trim() !== '' && !currentChar.match(/[\r\n\t]/)) {
            foundContent = true
          }
          
          answerEndIndex++
        }
        
        // Extract and format the answer content
        let answerContent = content.substring(answerStartIndex, answerEndIndex)
        
        // Basic cleanup first
        answerContent = answerContent
          .replace(/\s+/g, ' ') // Normalize whitespace
          .replace(/\s([.,!?;:])/g, '$1') // Fix punctuation spacing
          .replace(/([.!?])\s*([A-Z])/g, '$1\n\n$2') // Add paragraph breaks after sentences
          .replace(/(\d+\.)\s*([A-Z])/g, '\n\n$1 $2') // Format numbered lists
          .replace(/([Β·β€’])\s*/g, '\nβ€’ ') // Format bullet points
          .replace(/\n\s*\n\s*\n+/g, '\n\n') // Clean up multiple line breaks
          .trim()

        // Enhanced AI formatting using Ollama (async - will be processed later)
        const processAnswerWithAI = async (rawAnswer: string, question: string) => {
          try {
            console.log(`Formatting answer with AI for: "${question.substring(0, 50)}..."`);
            const formatResult = await backendService.formatAnswer(question, rawAnswer, docName);
            
            if (formatResult.success && formatResult.formattedAnswer !== rawAnswer) {
              console.log(`AI formatting improved answer quality`);
              // Update the stored answer with formatted version
              const pairIndex = pairs.findIndex(p => p.question === question);
              if (pairIndex !== -1) {
                pairs[pairIndex].answer = formatResult.formattedAnswer;
                pairs[pairIndex].aiFormatted = true;
                pairs[pairIndex].originalAnswer = rawAnswer;
              }
            }
          } catch (error) {
            console.warn('AI formatting failed, using basic cleanup:', error);
          }
        };
        
        // Estimate page number more accurately (assuming ~3000 chars per page)
        const estimatedPage = Math.floor(exactMatchIndex / 3000) + 1
        
        const answerPair = {
          questionNumber: pairs.length + 1,
          question: currentQuestionText,
          answer: answerContent,
          pageNumber: estimatedPage,
          sectionIndex: pairs.length,
          extractionStrategy: 'exact-match',
          matchScore: 100,
          documentSource: docName,
          exactMatch: true,
          aiFormatted: false,
          originalAnswer: answerContent
        };
        
        pairs.push(answerPair);
        
        // Process with AI formatting in the background
        processAnswerWithAI(answerContent, currentQuestionText);
        
        console.log(`   βœ… EXACT MATCH: "${currentQuestionText.substring(0, 50)}..." -> 100% match found on page ${estimatedPage}`)
        console.log(`   πŸ“„ Source: ${docName}`)
        console.log(`   πŸ“ Answer length: ${answerContent.length} characters`)
        console.log(`   πŸ“ Answer preview: "${answerContent.substring(0, 200)}..."`)
        console.log(`   πŸ€– AI formatting queued for processing`)
        
        // Return early if we found an exact match - no need to look for other strategies
        return pairs
      }
    }
    
    console.log(`   πŸ“Š Total extracted: ${pairs.length} Q&A pairs`)
    return pairs
  }
  
  // Advanced similarity calculation
  function calculateAdvancedSimilarity(q1: string, q2: string, pair?: any): number {
    const clean1 = q1.toLowerCase().replace(/[^\w\s]/g, ' ').replace(/\s+/g, ' ').trim()
    const clean2 = q2.toLowerCase().replace(/[^\w\s]/g, ' ').replace(/\s+/g, ' ').trim()
    
    // If this pair was extracted with exact match strategy, give it 100%
    if (pair?.extractionStrategy === 'exact-match' && pair?.matchScore === 100) {
      return 1.0
    }
    
    // Exact match
    if (clean1 === clean2) return 1.0
    
    // Check if one contains the other (high similarity for partial matches)
    if (clean1.includes(clean2) || clean2.includes(clean1)) {
      const longer = clean1.length > clean2.length ? clean1 : clean2
      const shorter = clean1.length > clean2.length ? clean2 : clean1
      return Math.max(0.85, shorter.length / longer.length)
    }
    
    // Word-based similarity
    const words1 = clean1.split(' ').filter(w => w.length > 2)
    const words2 = clean2.split(' ').filter(w => w.length > 2)
    
    if (words1.length === 0 || words2.length === 0) return 0
    
    const commonWords = words1.filter(w => words2.includes(w))
    const wordSimilarity = commonWords.length / Math.max(words1.length, words2.length)
    
    // Substring similarity
    const longer = clean1.length > clean2.length ? clean1 : clean2
    const shorter = clean1.length > clean2.length ? clean2 : clean1
    let substringScore = 0
    
    if (longer.includes(shorter)) substringScore = 0.3
    
    // Key term matching (boost for important business terms)
    const keyTerms = ['company', 'background', 'experience', 'profile', 'corporate', 'founded', 'healthcare', 'education', 'staffing', 'management', 'services', 'approach', 'methodology', 'process']
    const keyTermMatches = keyTerms.filter(term => 
      clean1.includes(term) && clean2.includes(term)
    )
    const keyTermScore = keyTermMatches.length * 0.05
    
    // Length similarity bonus
    const lengthRatio = Math.min(clean1.length, clean2.length) / Math.max(clean1.length, clean2.length)
    const lengthScore = lengthRatio * 0.1
    
    const totalSimilarity = wordSimilarity + substringScore + keyTermScore + lengthScore
    return Math.min(totalSimilarity, 1.0)
  }
  
  // Find matched words between questions
  function findMatchedWords(q1: string, q2: string): string[] {
    const words1 = q1.toLowerCase().split(/\s+/).filter(w => w.length > 2)
    const words2 = q2.toLowerCase().split(/\s+/).filter(w => w.length > 2)
    return words1.filter(w => words2.includes(w))
  }
  
  // Content-based matching fallback
  function findContentMatches(doc: any, questionText: string) {
    const matches = []
    const questionLower = questionText.toLowerCase()
    const keyWords = questionLower.split(/\s+/).filter(w => w.length > 3)
    
    const sections = doc.content.split(/\n\s*\n+/)
    
    for (let i = 0; i < sections.length; i++) {
      const section = sections[i].trim()
      if (section.length < 50) continue
      
      const sectionLower = section.toLowerCase()
      let matchCount = 0
      const matchedWords = []
      
      for (const word of keyWords) {
        if (sectionLower.includes(word)) {
          matchCount++
          matchedWords.push(word)
        }
      }
      
      // STRICTER THRESHOLD: Need at least 40% keyword match OR 5+ keywords
      const matchPercentage = (matchCount / keyWords.length) * 100
      if (matchCount >= 5 || matchPercentage >= 40) {
        const percentage = Math.min(Math.round(matchPercentage), 95)
        
        matches.push({
          docName: doc.name,
          question: 'Content Match',
          answer: section,
          content: section.substring(0, 600) + (section.length > 600 ? '...' : ''),
          matchScore: matchCount / keyWords.length,
          matchPercentage: percentage,
          category: doc.category || 'General',
          matchedWords: matchedWords,
          pageNumber: Math.max(1, Math.ceil(i / 200)) + 1, // More realistic page estimate
          sectionType: 'content-match',
          source: 'content-fallback',
          isExactMatch: false
        })
      }
    }
    
    return matches
  }
  
  // Get previous answers for current question
  $: {
    console.log('πŸ”„ Reactive update triggered for previousAnswers')
    console.log('Current question:', currentQuestion?.text)
    console.log('Reference docs with content available:', referenceDocsWithContent?.length || 0)
    if (referenceDocsWithContent && referenceDocsWithContent.length > 0) {
      console.log('Content preview:', referenceDocsWithContent[0].content?.substring(0, 100))
    }
  }
  
  $: previousAnswers = (currentQuestion && referenceDocsWithContent?.length > 0) ? getPreviousAnswers(currentQuestion.text) : []
  
  // Load reference documents with content on component mount
  let referenceDocsWithContent: Array<{
    id: string
    name: string
    category: string
    content: string
    fileType: string
  }> = []
  
  async function loadReferenceDocuments() {
    try {
      console.log('πŸ“š Loading reference documents with content...')
      const docs = await backendService.getPreviousRFPs()
      console.log('Found documents:', docs.map(d => d.name))
      
      // Clear existing vector storage
      vectorStorage.clear()
      console.log('πŸ—‘οΈ Cleared vector storage')
      
      const docsWithContent = []
      for (const doc of docs) {
        try {
          const docWithContent = await backendService.getDocumentContent(doc.id)
          console.log(`Loaded content for ${docWithContent.name}: ${docWithContent.content?.length || 0} characters`)
          docsWithContent.push(docWithContent)
          
          // Add document to vector storage
          if (docWithContent.content && docWithContent.content.length > 100) {
            await vectorStorage.addDocument(docWithContent.content, {
              source: docWithContent.name,
              category: docWithContent.category || 'General'
            })
            console.log(`βœ… Added ${docWithContent.name} to vector storage`)
          }
        } catch (error) {
          console.error(`Failed to load content for ${doc.name}:`, error)
        }
      }
      
      referenceDocsWithContent = docsWithContent
      
      // Show vector storage stats
      const stats = vectorStorage.getStats()
      console.log('πŸ“Š Vector Storage Stats:', stats)
      
      // Trigger reactive update after documents are loaded
      console.log('πŸ“‘ Triggering reactive update after document loading')
      if (currentQuestion) {
        previousAnswers = getPreviousAnswers(currentQuestion.text)
      }
    } catch (error) {
      console.error('Error loading reference documents:', error)
    }
  }
  
  // Load documents on component mount
  onMount(() => {
    loadReferenceDocuments()
  })
  
  async function generateAnswer() {
    if (!currentQuestion) return
    
    isLoading.set(true)
    const categoryFilter = selectedCategory === 'all' ? undefined : selectedCategory
    console.log(`Generating answer for question ${currentQuestionIndex + 1} using ${categoryFilter ? `${categoryFilter} category only` : 'all categories'}`)
    
    try {
      // Start with basic answer generation
      const aiResponse = await generateAIAnswer(currentQuestion.text, $rfpData.extractedData, undefined, categoryFilter)
      
      const answer: RFPAnswer = {
        id: `answer-${currentQuestionIndex}`,
        question: currentQuestion.text,
        aiGeneratedAnswer: aiResponse.generatedAnswer,
        finalAnswer: aiResponse.generatedAnswer,
        isFinalized: false,
        citations: aiResponse.citations.map(c => `${c.source} (Page ${c.pageNumber})`),
        source: aiResponse.citations[0]?.source,
        pageNumber: aiResponse.citations[0]?.pageNumber
      }
      
      // Update the store with the initial answer
      rfpData.update(data => {
        const existingAnswers = data.answers || []
        const existingIndex = existingAnswers.findIndex(a => a.id === answer.id)
        
        if (existingIndex >= 0) {
          existingAnswers[existingIndex] = answer
        } else {
          existingAnswers.push(answer)
        }
        
        return { ...data, answers: existingAnswers }
      })
      
      // Now format the answer in real-time using AI
      console.log('Formatting generated answer in real-time...')
      try {
        const formatResult = await backendService.formatAnswer(
          currentQuestion.text,
          aiResponse.generatedAnswer,
          `Reference from: ${aiResponse.citations.map(c => `${c.source} (Page ${c.pageNumber})`).join(', ')}`
        )
        
        if (formatResult.success) {
          // Update the answer with formatted version
          rfpData.update(data => {
            const answers = data.answers || []
            const answerIndex = answers.findIndex(a => a.id === answer.id)
            
            if (answerIndex >= 0) {
              answers[answerIndex] = {
                ...answers[answerIndex],
                finalAnswer: formatResult.formattedAnswer,
                aiGeneratedAnswer: formatResult.originalAnswer,
                isFormatted: true
              }
            }
            
            return { ...data, answers }
          })
          
          console.log('βœ… Answer formatted successfully in real-time')
        } else {
          console.warn('Failed to format answer:', formatResult)
        }
      } catch (formatError) {
        console.error('Error formatting answer:', formatError)
        // Continue with unformatted answer
      }
      
      // Move to next question or complete
      if (currentQuestionIndex < questionsToProcess.length - 1) {
        nextQuestion()
      } else {
        // All questions processed, go to next step
        nextStep()
      }
    } catch (error) {
      console.error('Error generating answer:', error)
    } finally {
      isLoading.set(false)
    }
  }

  // Function to use a previous answer directly
  async function usePreviousAnswer(previousAnswer: any) {
    if (!currentQuestion || !previousAnswer) return
    
    isLoading.set(true)
    
    try {
      const answer: RFPAnswer = {
        id: `answer-${currentQuestionIndex}`,
        question: currentQuestion.text,
        aiGeneratedAnswer: previousAnswer.answer,
        finalAnswer: previousAnswer.answer,
        isFinalized: false,
        citations: [`${previousAnswer.docName} (Page ${previousAnswer.pageNumber})`],
        source: previousAnswer.docName,
        pageNumber: previousAnswer.pageNumber
      }
      
      // Update the store with the previous answer
      rfpData.update(data => {
        const existingAnswers = data.answers || []
        const existingIndex = existingAnswers.findIndex(a => a.id === answer.id)
        
        if (existingIndex >= 0) {
          existingAnswers[existingIndex] = answer
        } else {
          existingAnswers.push(answer)
        }
        
        return { ...data, answers: existingAnswers }
      })
      
      console.log('βœ… Previous answer used successfully')
      
      // Move to next question or complete
      if (currentQuestionIndex < questionsToProcess.length - 1) {
        nextQuestion()
      } else {
        // All questions processed, go to next step
        nextStep()
      }
    } catch (error) {
      console.error('Error using previous answer:', error)
    } finally {
      isLoading.set(false)
    }
  }
</script>

<div class="max-w-6xl mx-auto">
  <div class="bg-gray-800 rounded-lg shadow-lg p-8">
    
    <!-- View Toggle Buttons -->
    <div class="mb-6 flex gap-3">
      <button
        on:click={() => showAnalyzer = false}
        class="px-4 py-2 rounded-lg font-medium transition-colors {!showAnalyzer ? 'bg-blue-600 text-white' : 'bg-gray-700 text-gray-300 hover:bg-gray-600'}"
      >
        πŸ“ Standard Answer Generation
      </button>
      <button
        on:click={() => showAnalyzer = true}
        class="px-4 py-2 rounded-lg font-medium transition-colors {showAnalyzer ? 'bg-purple-600 text-white' : 'bg-gray-700 text-gray-300 hover:bg-gray-600'}"
      >
        πŸ” Advanced RFP Analysis
      </button>
    </div>
    
    {#if showAnalyzer}
      <!-- RFP Analyzer View -->
      <div class="bg-gray-700 rounded-lg p-6 border border-gray-600">
        <h3 class="text-xl font-bold text-white mb-4">πŸ” Advanced RFP Question Analysis</h3>
        <p class="text-gray-300 mb-6">
          Get comprehensive analysis with structured previous answers, AI-generated responses considering stress points, gap analysis, and actionable suggestions.
        </p>
        
        <RFPAnalyzer 
          question={currentQuestion?.text || ''} 
          currentRFPContext={$rfpData.extractedData?.map(d => d.text).join('\n') || ''}
        />
      </div>
    {:else}
      <!-- Standard View (existing content) -->
    
    <!-- Configuration -->
    <div class="mb-8">
      <!-- Reference Category Selection -->
      {#if availableCategories.length > 0}
        <div class="mb-6 p-4 bg-gray-700 rounded-lg border border-gray-600">
          <h4 class="text-sm font-semibold text-white mb-3">
            Reference Document Category
          </h4>
          <div class="flex flex-wrap gap-2">
            <label class="flex items-center">
              <input 
                type="radio" 
                name="category" 
                value="all"
                bind:group={selectedCategory}
                class="mr-2 text-blue-600"
              />
              <span class="text-sm text-gray-200 px-2 py-1 bg-gray-800 rounded border border-gray-600">
                All Categories ({$referenceDocuments.length} docs)
              </span>
            </label>
            {#each availableCategories as category}
              {@const categoryDocs = $referenceDocuments.filter(doc => doc.category === category)}
              <label class="flex items-center">
                <input 
                  type="radio" 
                  name="category" 
                  value={category}
                  bind:group={selectedCategory}
                  class="mr-2 text-blue-600"
                />
                <span class="text-sm text-gray-200 px-2 py-1 bg-gray-800 rounded border border-gray-600">
                  {category === 'it-staffing' ? 'IT Staffing' : category === 'ai-rfps' ? 'AI RFPs' : category} ({categoryDocs.length} docs)
                </span>
              </label>
            {/each}
          </div>
          <p class="text-xs text-gray-300 mt-2">
            AI will only reference documents from the selected category when generating answers.
          </p>
        </div>
      {/if}
      
      <!-- Current Question Display -->
      <div class="mb-6">
        <div class="flex items-center justify-between mb-4">
          <h4 class="text-lg font-semibold text-white">
            Question {currentQuestionIndex + 1} of {questionsToProcess.length}
          </h4>
          <div class="flex items-center space-x-2">
            <button
              on:click={prevQuestion}
              disabled={currentQuestionIndex === 0}
              class="px-3 py-1 bg-gray-600 text-white rounded text-sm hover:bg-gray-500 disabled:opacity-50 disabled:cursor-not-allowed"
            >
              ← Previous
            </button>
            <button
              on:click={nextQuestion}
              disabled={currentQuestionIndex >= questionsToProcess.length - 1}
              class="px-3 py-1 bg-gray-600 text-white rounded text-sm hover:bg-gray-500 disabled:opacity-50 disabled:cursor-not-allowed"
            >
              Next β†’
            </button>
          </div>
        </div>
        
        {#if currentQuestion}
          <div class="p-4 border border-gray-600 rounded-lg bg-gray-700">
            <div class="text-gray-200 mb-4">
              {currentQuestion.text}
            </div>
            
            <!-- Answer Type Selection -->
            <div class="mb-6 p-4 bg-gray-800 rounded-lg border border-gray-600">
              <h4 class="text-sm font-semibold text-white mb-3">
                Choose Answer Source
              </h4>
              <div class="flex gap-4">
                <label class="flex items-center">
                  <input 
                    type="radio" 
                    name="answerType" 
                    value="ai"
                    bind:group={selectedAnswerType}
                    class="mr-2 text-blue-600"
                  />
                  <span class="text-sm text-gray-200 px-3 py-2 bg-gray-700 rounded border border-gray-600">
                    Generate AI Answer
                  </span>
                </label>
                <label class="flex items-center">
                  <input 
                    type="radio" 
                    name="answerType" 
                    value="previous"
                    bind:group={selectedAnswerType}
                    class="mr-2 text-blue-600"
                  />
                  <span class="text-sm text-gray-200 px-3 py-2 bg-gray-700 rounded border border-gray-600">
                    Use Previous Answer
                  </span>
                </label>
              </div>
            </div>
            
            <!-- Three-part answer display -->
            <div class="space-y-4">
              <!-- 1. Previous RFP Answers -->
              <div class="bg-gray-800 rounded-lg p-4 border border-gray-600">
                <h5 class="text-sm font-semibold text-gray-300 mb-3 flex items-center">
                  <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                  </svg>
                  Previous RFP Answers
                </h5>
                <div class="text-gray-400 text-sm">
                  {#if previousAnswers.length > 0}
                    <div class="space-y-3">
                      {#each previousAnswers as answer, index}
                        <div class="bg-gray-700 p-4 rounded border border-gray-600 
                          {answer.isExactMatch ? 'border-green-500 bg-green-900/10' : ''}
                          {answer.matchPercentage >= 80 ? 'border-blue-500 bg-blue-900/10' : ''}
                          {answer.matchPercentage >= 60 ? 'border-yellow-500 bg-yellow-900/10' : ''}">
                          
                          <!-- Header with match percentage -->
                          <div class="flex items-center justify-between mb-3">
                            <div class="flex items-center space-x-2">
                              <div class="font-medium text-gray-200 text-sm">
                                πŸ“„ {answer.documentSource || answer.docName}
                              </div>
                              <div class="flex items-center space-x-1">
                                <div class="text-lg font-bold 
                                  {answer.matchPercentage >= 95 ? 'text-green-400' : ''}
                                  {answer.matchPercentage >= 80 && answer.matchPercentage < 95 ? 'text-blue-400' : ''}
                                  {answer.matchPercentage >= 60 && answer.matchPercentage < 80 ? 'text-yellow-400' : ''}
                                  {answer.matchPercentage < 60 ? 'text-orange-400' : ''}">
                                  {answer.matchPercentage}%
                                </div>
                                <div class="text-xs text-gray-400">match</div>
                              </div>
                            </div>
                            <div class="flex items-center space-x-2">
                              <span class="text-xs 
                                {answer.isExactMatch ? 'text-green-400 bg-green-900/20' : 'text-blue-400 bg-blue-900/20'} 
                                px-2 py-1 rounded">
                                {answer.category || 'General'}
                              </span>
                              <span class="text-xs text-green-400 px-2 py-1 bg-green-900/20 rounded font-bold">
                                πŸ“ Page {answer.pageNumber || 'N/A'}
                              </span>
                              {#if answer.source === 'regex-pattern'}
                                <span class="text-xs text-orange-400 px-2 py-1 bg-orange-900/20 rounded font-bold">
                                  πŸ“‹ Regex Match
                                </span>
                              {:else if answer.source === 'vector-search'}
                                <span class="text-xs text-purple-400 px-2 py-1 bg-purple-900/20 rounded font-bold">
                                  🧠 Vector Match
                                </span>
                              {/if}
                              {#if answer.isExactMatch}
                                <span class="text-xs text-green-400 px-2 py-1 bg-green-900/20 rounded font-bold">
                                  βœ… EXACT MATCH
                                </span>
                              {/if}
                              {#if answer.extractionStrategy}
                                <span class="text-xs text-purple-400 px-2 py-1 bg-purple-900/20 rounded">
                                  {answer.extractionStrategy}
                                </span>
                              {/if}
                            </div>
                          </div>
                          
                          <!-- Question and Answer Content -->
                          {#if answer.question && answer.answer}
                            <div class="space-y-2">
                              <!-- Show matched keywords for vector search -->
                              {#if answer.source === 'vector-search' && answer.matchedKeywords && answer.matchedKeywords.length > 0}
                                <div class="flex items-center gap-2 flex-wrap">
                                  <span class="text-xs text-gray-400">Matched keywords:</span>
                                  {#each answer.matchedKeywords as keyword}
                                    <span class="text-xs px-2 py-0.5 bg-purple-900/30 text-purple-300 rounded">
                                      {keyword}
                                    </span>
                                  {/each}
                                </div>
                              {/if}
                              
                              {#if answer.question !== 'Relevant Content'}
                                <div class="text-xs text-gray-400 font-medium">Original Question:</div>
                                <div class="text-gray-300 text-sm italic bg-gray-800 p-2 rounded border-l-4 border-blue-500">
                                  "{answer.question}"
                                </div>
                              {/if}
                              
                              <!-- Format Answer Button -->
                              <div class="flex items-center justify-between">
                                <div class="text-xs text-gray-400 font-medium mt-3">Complete Answer:</div>
                                <div class="flex gap-2">
                                  {#if selectedAnswerType === 'previous'}
                                    <button 
                                      class="text-xs px-3 py-1 bg-green-600 hover:bg-green-700 text-white rounded-md transition-colors duration-200 flex items-center space-x-1"
                                      on:click={() => usePreviousAnswer(answer)}
                                      disabled={$isLoading}
                                    >
                                      <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                                      </svg>
                                      <span>Use This Answer</span>
                                    </button>
                                  {/if}
                                  <button 
                                    class="text-xs px-3 py-1 bg-blue-600 hover:bg-blue-700 text-white rounded-md transition-colors duration-200 flex items-center space-x-1"
                                    on:click={() => formatPreviousAnswer(answer, currentQuestion?.text)}
                                    disabled={formattingInProgress.has(`${answer.docName}-${answer.question.substring(0, 50)}`)}
                                  >
                                    {#if formattingInProgress.has(`${answer.docName}-${answer.question.substring(0, 50)}`)}
                                      <svg class="w-3 h-3 animate-spin" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
                                      </svg>
                                      <span>Formatting...</span>
                                    {:else}
                                      <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
                                      </svg>
                                      <span>Format with AI</span>
                                    {/if}
                                  </button>
                                </div>
                              </div>
                              
                              <!-- Show formatted answer if available -->
                              {#if formattedAnswers.has(`${answer.docName}-${answer.question.substring(0, 50)}`)}
                                {@const formattedAnswer = formattedAnswers.get(`${answer.docName}-${answer.question.substring(0, 50)}`)}
                                
                                <!-- Formatted Answer -->
                                <div class="text-gray-200 text-sm leading-relaxed bg-gradient-to-r from-green-900/20 to-blue-900/20 p-4 rounded border-l-4 border-green-400">
                                  <div class="flex items-center mb-2">
                                    <svg class="w-4 h-4 text-green-400 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z" />
                                    </svg>
                                    <span class="text-xs font-semibold text-green-400">AI-Formatted Answer</span>
                                  </div>
                                  <div class="prose prose-sm max-w-none text-gray-200">
                                    {#each (formattedAnswer.formattedAnswer || formattedAnswer.answer).split('\n\n') as paragraph}
                                      {#if paragraph.trim()}
                                        <p class="mb-3 last:mb-0">{@html paragraph.trim().replace(/\n/g, '<br>')}</p>
                                      {/if}
                                    {/each}
                                  </div>
                                </div>
                                
                                <!-- Original Answer (collapsed by default) -->
                                <details class="text-gray-200 text-sm leading-relaxed">
                                  <summary class="text-xs text-gray-400 cursor-pointer hover:text-gray-300 mb-2">View Original Answer</summary>
                                  <div class="bg-gray-800 p-4 rounded border-l-4 border-gray-500">
                                    <div class="prose prose-sm max-w-none text-gray-300">
                                      {#each answer.answer.split('\n\n') as paragraph}
                                        {#if paragraph.trim()}
                                          <p class="mb-3 last:mb-0">{@html paragraph.trim().replace(/\n/g, '<br>')}</p>
                                        {/if}
                                      {/each}
                                    </div>
                                  </div>
                                </details>
                              {:else}
                                <!-- Original Answer (when not formatted) -->
                                <div class="text-gray-200 text-sm leading-relaxed bg-gray-800 p-4 rounded border-l-4 border-green-500">
                                  <div class="prose prose-sm max-w-none text-gray-200">
                                    {#each answer.answer.split('\n\n') as paragraph}
                                      {#if paragraph.trim()}
                                        <p class="mb-3 last:mb-0">{@html paragraph.trim().replace(/\n/g, '<br>')}</p>
                                      {/if}
                                    {/each}
                                  </div>
                                </div>
                              {/if}
                            </div>
                          {:else}
                            <div class="text-gray-200 text-sm leading-relaxed">
                              {answer.content}
                            </div>
                          {/if}
                          
                          <!-- Match Details -->
                          <div class="mt-3 pt-3 border-t border-gray-600 flex items-center justify-between">
                            <div class="text-xs text-gray-400">
                              <span class="font-medium">Matched words:</span>
                              {#if answer.matchedWords && answer.matchedWords.length > 0}
                                <span class="text-blue-300">{answer.matchedWords.slice(0, 5).join(', ')}</span>
                                {#if answer.matchedWords.length > 5}
                                  <span class="text-gray-500"> +{answer.matchedWords.length - 5} more</span>
                                {/if}
                              {:else}
                                <span class="text-gray-500">Content-based match</span>
                              {/if}
                            </div>
                            <div class="text-xs text-gray-500">
                              Type: {answer.sectionType || 'content'} | Strategy: {answer.extractionStrategy || answer.source || 'extraction'}
                              {#if answer.documentSource}
                                | Source: {answer.documentSource}
                              {/if}
                            </div>
                          </div>
                        </div>
                      {/each}
                      
                      <!-- Summary -->
                      <div class="mt-4 p-3 bg-gray-800 rounded border border-gray-600">
                        <div class="text-xs text-gray-400">
                          <strong>Search Summary:</strong> Found {previousAnswers.length} relevant matches
                          {#if previousAnswers.some(a => a.isExactMatch)}
                            <span class="text-green-400 font-bold"> β€’ Contains exact question match!</span>
                          {/if}
                          {#if previousAnswers.filter(a => a.matchPercentage >= 80).length > 0}
                            <span class="text-blue-400"> β€’ {previousAnswers.filter(a => a.matchPercentage >= 80).length} high-confidence matches</span>
                          {/if}
                        </div>
                      </div>

                    </div>
                  {:else}
                    <div class="text-gray-500 italic">
                      {#if referenceDocsWithContent.length === 0}
                        <div class="space-y-2">
                          <div>No reference documents loaded yet...</div>
                          <div class="text-xs">Documents available: {$referenceDocuments.length}</div>
                          {#if $referenceDocuments.length > 0}
                            <div class="text-xs">
                              {#each $referenceDocuments as doc}
                                <div>β€’ {doc.name} ({doc.fileType || 'unknown type'})</div>
                              {/each}
                            </div>
                          {/if}
                        </div>
                      {:else}
                        <div class="space-y-2">
                          <div>No relevant previous answers found for this question</div>
                          <div class="text-xs">Searched {referenceDocsWithContent.length} documents with content:</div>
                          <div class="text-xs">
                            {#each referenceDocsWithContent as doc}
                              <div>β€’ {doc.name}: {doc.content?.length || 0} characters</div>
                            {/each}
                          </div>
                          <div class="text-xs text-blue-400 mt-2">Check browser console for detailed search logs</div>
                        </div>
                      {/if}
                    </div>
                  {/if}
                </div>
              </div>
              
              <!-- 2. AI Generated Answer -->
              <div class="bg-gray-800 rounded-lg p-4 border border-gray-600">
                <h5 class="text-sm font-semibold text-gray-300 mb-3 flex items-center">
                  <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z" />
                  </svg>
                  AI Generated Answer
                  {#if currentAnswer && currentAnswer.isFormatted}
                    <span class="ml-2 text-xs bg-green-600 text-white px-2 py-1 rounded-full">AI-Formatted</span>
                  {/if}
                  {#if currentAnswer && currentAnswer.hasAdditionalInfo}
                    <span class="ml-2 text-xs bg-purple-600 text-white px-2 py-1 rounded-full">Enhanced</span>
                  {/if}
                </h5>
                <div class="text-gray-400 text-sm">
                  {#if currentAnswer && (currentAnswer.finalAnswer || currentAnswer.aiGeneratedAnswer)}
                    <div class="bg-gray-700 p-3 rounded border border-gray-600">
                      {#if currentAnswer.isFormatted && currentAnswer.finalAnswer}
                        <!-- Show formatted answer with enhanced styling -->
                        <div class="bg-gradient-to-r from-green-900/20 to-blue-900/20 p-4 rounded border-l-4 border-green-400 mb-3">
                          <div class="flex items-center mb-2">
                            <svg class="w-4 h-4 text-green-400 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z" />
                            </svg>
                            <span class="text-xs font-semibold text-green-400">AI-Enhanced Answer</span>
                          </div>
                          <div class="text-gray-200 leading-relaxed whitespace-pre-wrap">{currentAnswer.finalAnswer}</div>
                        </div>
                        
                        <!-- Show original answer in collapsible section -->
                        <details class="mt-3">
                          <summary class="text-xs text-gray-400 cursor-pointer hover:text-gray-300 mb-2">View Original Generated Answer</summary>
                          <div class="bg-gray-800 p-3 rounded border border-gray-500">
                            <div class="text-gray-300 leading-relaxed whitespace-pre-wrap">{currentAnswer.aiGeneratedAnswer}</div>
                          </div>
                        </details>
                      {:else}
                        <!-- Show regular generated answer -->
                        <div class="text-gray-200 leading-relaxed whitespace-pre-wrap">{currentAnswer.finalAnswer || currentAnswer.aiGeneratedAnswer}</div>
                      {/if}
                      
                      {#if currentAnswer.citations && currentAnswer.citations.length > 0}
                        <div class="mt-3 pt-3 border-t border-gray-600">
                          <div class="text-xs text-blue-400 font-medium mb-1">Citations:</div>
                          <div class="space-y-1">
                            {#each currentAnswer.citations as citation}
                              <div class="text-xs text-blue-300">β€’ {citation}</div>
                            {/each}
                          </div>
                        </div>
                      {/if}
                      
                      <!-- Additional Information Input -->
                      <div class="mt-4 pt-3 border-t border-gray-600">
                        <div class="text-xs text-gray-400 font-medium mb-2">Add Additional Information for AI Processing:</div>
                        <div class="space-y-2">
                          <textarea
                            bind:value={additionalInfo}
                            placeholder="Enter any additional context, requirements, or specific details you want the AI to include when processing this answer..."
                            class="w-full px-3 py-2 bg-gray-800 border border-gray-600 rounded-md text-gray-200 text-sm placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent resize-none"
                            rows="3"
                          ></textarea>
                          <button
                            class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white text-sm rounded-md transition-colors duration-200 flex items-center space-x-2"
                            on:click={() => reprocessAnswerWithAdditionalInfo(currentAnswer)}
                            disabled={$isLoading || !additionalInfo.trim()}
                          >
                            {#if $isLoading}
                              <svg class="w-4 h-4 animate-spin" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
                              </svg>
                              <span>Processing...</span>
                            {:else}
                              <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
                              </svg>
                              <span>Reprocess with Additional Info</span>
                            {/if}
                          </button>
                        </div>
                      </div>
                    </div>
                  {:else if $isLoading}
                    <div class="bg-gray-700 p-3 rounded border border-gray-600">
                      <div class="flex items-center space-x-2">
                        <div class="animate-spin rounded-full h-4 w-4 border-b-2 border-blue-500"></div>
                        <div class="text-gray-300">Generating and formatting AI answer in real-time...</div>
                      </div>
                    </div>
                  {:else}
                    <div class="bg-gray-700 p-3 rounded border border-gray-600">
                      <div class="text-gray-400">AI-generated answer will appear here after clicking "Generate Answer"...</div>
                    </div>
                  {/if}
                </div>
              </div>
              
              <!-- 3. Suggestions -->
              <div class="bg-gray-800 rounded-lg p-4 border border-gray-600">
                <h5 class="text-sm font-semibold text-gray-300 mb-3 flex items-center">
                  <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z" />
                  </svg>
                  Suggestions
                </h5>
                <div class="text-gray-400 text-sm">
                  <div class="space-y-2">
                    <div class="bg-gray-700 p-3 rounded border border-gray-600">
                      <div class="text-gray-300 text-xs">β€’ Include specific metrics and KPIs</div>
                    </div>
                    <div class="bg-gray-700 p-3 rounded border border-gray-600">
                      <div class="text-gray-300 text-xs">β€’ Add relevant case studies or examples</div>
                    </div>
                    <div class="bg-gray-700 p-3 rounded border border-gray-600">
                      <div class="text-gray-300 text-xs">β€’ Mention compliance and security standards</div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        {/if}
        
        <!-- Question Navigation -->
        <div class="mt-4">
          <div class="flex flex-wrap gap-2">
            {#each questionsToProcess as question, index}
              <button
                on:click={() => goToQuestion(index)}
                class="px-3 py-1 text-sm rounded {index === currentQuestionIndex ? 'bg-blue-600 text-white' : 'bg-gray-600 text-gray-300 hover:bg-gray-500'}"
              >
                Q{index + 1}
              </button>
            {/each}
          </div>
        </div>
      </div>
    </div>
    
    <!-- AI Generation Info -->
    <div class="mb-8 p-6 bg-gray-800 rounded-lg border border-gray-600">
      <h4 class="text-lg font-semibold text-white mb-3">
         AI Answer Generation
      </h4>
      <div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-sm text-gray-200">
        <div class="flex items-center">
          <svg class="w-4 h-4 mr-2 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
          </svg>
          Searches previous RFP responses for relevant content
        </div>
        <div class="flex items-center">
          <svg class="w-4 h-4 mr-2 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
          </svg>
          Provides citations with source and page numbers
        </div>
        <div class="flex items-center">
          <svg class="w-4 h-4 mr-2 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
          </svg>
          Suggests improvements based on new RFP context
        </div>
        <div class="flex items-center">
          <svg class="w-4 h-4 mr-2 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
          </svg>
          Allows full editing before finalization
        </div>
      </div>
    </div>
    
    <!-- Loading State -->
    {#if $isLoading}
      <div class="flex flex-col items-center justify-center p-8 space-y-4">
        <div class="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600"></div>
        <div class="text-center">
          <div class="text-lg font-medium text-white">Generating AI Answers</div>
          <div class="text-gray-300">This may take a few moments...</div>
        </div>
        <div class="w-full max-w-md bg-gray-700 rounded-full h-2">
          <div class="bg-blue-600 h-2 rounded-full animate-pulse" style="width: 60%"></div>
        </div>
      </div>
    {/if}
    
    <!-- Navigation Buttons -->
    <div class="flex justify-between">
      <button 
        class="px-6 py-3 bg-gray-700 text-gray-300 rounded-lg font-medium hover:bg-gray-600 transition-colors"
        on:click={prevStep}
        disabled={$isLoading}
      >
        <svg class="w-5 h-5 mr-2 inline" fill="none" stroke="currentColor" viewBox="0 0 24 24">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
        </svg>
        Back to Upload
      </button>
      
      <button 
        class="px-6 py-3 bg-blue-600 text-white rounded-lg font-medium hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
        disabled={!currentQuestion || $isLoading || (selectedAnswerType === 'previous' && previousAnswers.length === 0)}
        on:click={generateAnswer}
      >
        {#if selectedAnswerType === 'ai'}
          Generate AI Answer for Q{currentQuestionIndex + 1}
          {#if selectedCategory !== 'all'}
            <span class="text-xs">({selectedCategory === 'it-staffing' ? 'IT Staffing' : selectedCategory === 'ai-rfps' ? 'AI RFPs' : selectedCategory})</span>
          {/if}
          <svg class="w-5 h-5 ml-2 inline" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z" />
          </svg>
        {:else}
          {#if previousAnswers.length > 0}
            Select Previous Answer Above ↑
            <svg class="w-5 h-5 ml-2 inline" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
            </svg>
          {:else}
            No Previous Answers Available
            <svg class="w-5 h-5 ml-2 inline" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L4.082 15.5c-.77.833.192 2.5 1.732 2.5z" />
            </svg>
          {/if}
        {/if}
      </button>
    </div>
    {/if}
    <!-- End of Standard View -->
  </div>
</div>