File size: 85,792 Bytes
7ff6662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07a2f3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ff6662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b15fd58
 
7ff6662
 
 
b15fd58
 
7ff6662
b15fd58
 
 
 
7ff6662
b15fd58
7ff6662
 
 
 
 
b15fd58
7ff6662
 
b15fd58
b4a0bdc
 
 
 
 
 
 
 
 
 
 
b15fd58
 
 
7ff6662
b15fd58
 
7ff6662
b15fd58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07a2f3f
b15fd58
 
 
 
 
 
bd9ddce
 
 
 
b15fd58
 
 
 
 
 
 
 
 
 
bd9ddce
b15fd58
 
 
bd9ddce
 
 
b15fd58
 
 
 
 
 
 
 
 
 
 
 
 
fcc8f76
b15fd58
 
 
 
fcc8f76
 
 
b15fd58
fcc8f76
 
 
 
 
 
b15fd58
 
 
7ff6662
 
 
 
 
 
 
 
 
bd9ddce
7ff6662
 
bd9ddce
 
 
 
 
 
 
 
 
7ff6662
 
 
 
 
 
 
b15fd58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ff6662
 
b15fd58
7ff6662
 
 
 
 
 
 
 
b15fd58
 
0e70529
 
 
 
 
 
 
 
 
 
7ff6662
 
 
 
 
 
 
 
 
b15fd58
 
 
 
 
 
 
 
 
 
33ec77b
 
 
 
 
7ff6662
 
 
 
 
 
 
 
 
 
07a2f3f
7ff6662
 
 
33ec77b
 
 
7ff6662
 
 
 
b15fd58
 
 
7ff6662
b15fd58
7ff6662
 
 
 
 
33ec77b
7ff6662
 
 
 
 
 
 
 
33ec77b
7ff6662
 
b15fd58
33ec77b
 
 
 
 
 
 
b15fd58
 
7ff6662
 
 
b15fd58
7ff6662
 
07a2f3f
b15fd58
7ff6662
 
b15fd58
7ff6662
 
 
 
 
386446e
 
7ff6662
b15fd58
 
 
 
386446e
 
b15fd58
 
 
07a2f3f
b15fd58
 
 
 
 
 
 
 
 
 
 
33ec77b
 
 
 
 
 
b15fd58
7ff6662
33ec77b
 
 
 
 
 
386446e
 
 
 
 
 
 
 
 
 
 
 
7ff6662
 
07a2f3f
 
 
 
 
 
 
 
 
 
 
 
 
 
2a811e0
 
 
 
 
47b43eb
 
 
 
 
2a811e0
 
47b43eb
 
 
 
 
 
 
 
 
2a811e0
 
07a2f3f
 
 
 
 
ab9af33
 
07a2f3f
ab9af33
 
 
 
 
07a2f3f
 
 
ab9af33
 
 
07a2f3f
ab9af33
 
 
 
 
 
 
 
 
07a2f3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b15fd58
7ff6662
33ec77b
 
 
 
 
 
 
 
 
 
 
 
 
 
7ff6662
b15fd58
7ff6662
 
 
b15fd58
33ec77b
 
 
 
 
 
 
 
 
 
 
b15fd58
 
 
 
 
 
386446e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a811e0
 
 
 
386446e
 
7041ade
 
b15fd58
 
f479a80
 
2a811e0
f479a80
 
 
 
 
 
7041ade
2a811e0
7041ade
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a811e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7041ade
2a811e0
33ec77b
 
 
 
 
 
 
 
2a811e0
 
33ec77b
2a811e0
33ec77b
2a811e0
 
 
 
 
 
 
 
 
 
 
 
7ff6662
 
 
 
 
2a811e0
 
 
 
 
 
 
 
 
 
 
 
ac6db93
 
 
 
 
 
 
2a811e0
 
 
 
ac6db93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ff6662
 
 
 
 
 
 
 
 
 
07a2f3f
 
7ff6662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33ec77b
 
 
 
 
7ff6662
 
33ec77b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ff6662
07a2f3f
7ff6662
 
 
 
 
33ec77b
7ff6662
 
 
 
 
 
 
 
 
 
07a2f3f
33ec77b
07a2f3f
33ec77b
07a2f3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33ec77b
07a2f3f
33ec77b
 
 
 
 
 
07a2f3f
33ec77b
07a2f3f
33ec77b
 
 
 
 
 
 
 
7ff6662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e3dfba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7041ade
8e3dfba
 
 
 
7041ade
 
 
 
 
8e3dfba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7041ade
 
 
 
2ea6493
2a811e0
 
7041ade
 
 
 
 
 
 
8e3dfba
 
7041ade
 
 
 
 
 
 
8e3dfba
 
7041ade
8e3dfba
7041ade
 
8e3dfba
 
7041ade
 
8e3dfba
 
 
 
 
 
 
 
 
 
7041ade
8e3dfba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7041ade
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e3dfba
 
 
7041ade
 
 
 
 
8e3dfba
7041ade
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e3dfba
0e70529
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
daede4b
 
 
 
 
 
 
 
0e70529
 
 
 
 
 
 
daede4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e3dfba
22120b8
 
 
 
8e3dfba
 
 
 
 
 
 
 
 
 
 
2a811e0
 
 
 
 
 
 
8e3dfba
 
22120b8
 
2a811e0
 
8e3dfba
22120b8
 
47b43eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e3dfba
 
 
 
47b43eb
0e70529
 
47b43eb
 
 
 
8e3dfba
0e70529
 
 
 
 
 
 
daede4b
0e70529
 
 
 
 
daede4b
 
 
 
 
 
 
0e70529
 
 
 
 
 
386446e
 
daede4b
 
 
 
386446e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
daede4b
 
 
 
 
 
 
386446e
 
 
 
 
 
 
 
 
 
 
 
 
daede4b
386446e
 
daede4b
 
 
 
 
386446e
 
daede4b
386446e
 
 
 
 
 
33ec77b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47b43eb
 
 
 
 
33ec77b
 
 
 
 
 
 
 
 
 
47b43eb
33ec77b
47b43eb
 
 
 
 
33ec77b
47b43eb
 
33ec77b
 
7ff6662
07a2f3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ff6662
07a2f3f
 
 
 
 
 
7ff6662
 
 
 
07a2f3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f65a1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07a2f3f
 
 
 
 
 
 
 
 
 
 
 
 
 
4f65a1d
 
 
 
07a2f3f
 
 
 
4f65a1d
07a2f3f
 
 
 
 
4f65a1d
 
 
 
07a2f3f
 
 
 
 
 
 
4f65a1d
 
 
 
 
 
 
 
 
7ff6662
 
 
 
 
4f65a1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ff6662
07a2f3f
 
 
7ff6662
07a2f3f
 
 
 
7ff6662
 
07a2f3f
7ff6662
 
07a2f3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ff6662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07a2f3f
 
7ff6662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac6db93
 
07a2f3f
ac6db93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33ec77b
 
 
 
 
ac6db93
7ff6662
 
33ec77b
 
7ff6662
33ec77b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ff6662
07a2f3f
7ff6662
 
 
 
 
 
 
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
import os
import re
from datetime import datetime
from docx import Document
from docx.shared import Pt, RGBColor, Inches
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
from tqdm import tqdm
from colorama import Fore, Style

from .llm_client import LLMClient
from .resume_parser import ResumeParser


def _set_cell_bg(cell, hex_color: str):
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()
    shd = OxmlElement("w:shd")
    shd.set(qn("w:val"), "clear")
    shd.set(qn("w:color"), "auto")
    shd.set(qn("w:fill"), hex_color)
    tcPr.append(shd)


def _read_docx_text(filepath: str) -> str:
    """
    Read full DOCX text including table cells, in document order.

    python-docx's .paragraphs iterator skips table content, and appending
    tables at the end breaks section detection (CORE COMPETENCIES would have
    no content because the next line is PROFESSIONAL EXPERIENCE). Walking the
    body's XML children in order keeps the table immediately under its header.
    """
    from docx import Document as _Doc
    from docx.oxml.ns import qn
    from docx.text.paragraph import Paragraph
    from docx.table import Table

    doc = _Doc(filepath)
    parts: list[str] = []
    body = doc.element.body
    for child in body.iterchildren():
        tag = child.tag
        if tag == qn("w:p"):
            text = Paragraph(child, doc).text
            if text:
                parts.append(text)
        elif tag == qn("w:tbl"):
            tbl = Table(child, doc)
            for row in tbl.rows:
                row_text = " ".join(c.text for c in row.cells if c.text)
                if row_text.strip():
                    parts.append(row_text)
    return "\n".join(parts)


def _normalize_spaced_text(text: str) -> str:
    """Collapse PDF letter-spacing artifacts like 'E D U C A T I O N' β†’ 'EDUCATION'.

    Detects runs of 3+ single uppercase letters separated by spaces and joins them.
    """
    def _collapse(match):
        return re.sub(r"\s+", "", match.group(0))

    # Match sequences like 'P R O F E S S I O N A L   S U M M A R Y'
    return re.sub(r"(?:\b[A-Z]\s+){2,}[A-Z]\b", _collapse, text)


def _extract_candidate_name(resume_text: str) -> str:
    """
    Extract the candidate's name from the top of the resume.

    Tries in order:
      1. ALL CAPS name on the first non-empty line (e.g. "SAITEJA TIRUNAGARI")
      2. Title Case name on the first non-empty line
      3. ALL CAPS name anywhere in the first 5 lines
      4. Fallback: "Your Name"
    """
    lines = [l.strip() for l in resume_text.splitlines() if l.strip()]
    if not lines:
        return "Your Name"

    # 1) First non-empty line as ALL CAPS name (2-5 words, no punctuation)
    first = lines[0]
    # Handle spaced-out ALL CAPS like "S A I T E J A   T I R U N A G A R I"
    normalized_first = _normalize_spaced_text(first)
    m = re.match(r"^([A-Z][A-Z'\-\.]+(?:\s+[A-Z][A-Z'\-\.]+){1,4})\s*$", normalized_first)
    if m:
        name = m.group(1).strip()
        # Convert to Title Case for nicer display
        return " ".join(w.capitalize() for w in name.split())

    # 2) Title Case on first line
    m = re.match(r"^([A-Z][a-z]+(?:\s+[A-Z][a-z]+){1,4})\s*$", first)
    if m:
        return m.group(1).strip()

    # 3) Scan first 5 lines for ALL CAPS name
    for ln in lines[:5]:
        ln_norm = _normalize_spaced_text(ln)
        m = re.match(r"^([A-Z][A-Z'\-\.]+(?:\s+[A-Z][A-Z'\-\.]+){1,4})\s*$", ln_norm)
        if m:
            name = m.group(1).strip()
            return " ".join(w.capitalize() for w in name.split())

    return "Your Name"


class ResumeCustomizer:
    def __init__(self, llm_client: LLMClient, resume_text: str, output_dir: str,
                 fast_model_cfg: dict = None):
        self.llm            = llm_client
        self.resume_text    = resume_text
        self.fast_model_cfg = fast_model_cfg   # used for both ATS keyword extraction AND resume customization
        # Organise resumes by date: data/output/resumes/YYYY-MM-DD/
        date_str = datetime.now().strftime("%Y-%m-%d")
        self.output_dir = os.path.join(output_dir, date_str)
        os.makedirs(self.output_dir, exist_ok=True)

    def customize_for_jobs(
        self,
        assessed_jobs: list[dict],
        min_score_for_llm: int = 6,
        max_llm_resumes: int = 20,
        generate_all: bool = True,
        model_cfgs: list[dict] = None,
        progress_cb=None,
    ) -> list[dict]:
        """
        Generate resumes for jobs:
        - LLM-tailored (high quality) for jobs scoring >= min_score_for_llm,
          generated IN PARALLEL across the model pool (one model per worker)
        - Template-based (instant) for all other PM-relevant jobs if generate_all=True

        model_cfgs: list of model config dicts; workers round-robin across them so
                    each parallel resume hits a different API key (no rate limiting).
        progress_cb: optional callable(done, total, message) for live UI updates.
        """
        from concurrent.futures import ThreadPoolExecutor, as_completed

        llm_eligible = [j for j in assessed_jobs if j.get("relevance_score", 0) >= min_score_for_llm][:max_llm_resumes]
        template_eligible = [j for j in assessed_jobs if j.get("relevance_score", 0) < min_score_for_llm and generate_all]

        print(f"\n{Fore.CYAN}Generating resumes:")
        print(f"  LLM-tailored:  {len(llm_eligible)} jobs (score >= {min_score_for_llm}) β€” PARALLEL")
        print(f"  Template-only: {len(template_eligible)} jobs{Style.RESET_ALL}")

        cfg_pool = [c for c in (model_cfgs or []) if c and c.get("api_key")]
        # GUARDRAIL (Phase 5): resume tailoring quality varies a lot by model.
        # Restrict the tailoring pool to the "capable tier" (tailor=True) β€”
        # Kimi/Qwen-397b/DeepSeek-Pro/GPT-OSS β€” and exclude weaker models
        # (Step, Qwen-122b) that drop roles or write sparse bullets. The
        # deterministic floor still backfills, but starting from a stronger
        # model means more natural prose + higher first-pass coverage.
        tailor_pool = [c for c in cfg_pool if c.get("tailor")]
        if tailor_pool:
            cfg_pool = tailor_pool
            print(f"{Fore.CYAN}  Tailoring models (capable tier): "
                  f"{', '.join(c.get('name','?') for c in cfg_pool)}{Style.RESET_ALL}")
        if not cfg_pool and self.fast_model_cfg:
            cfg_pool = [self.fast_model_cfg]
        n_workers = min(6, max(1, len(cfg_pool))) if cfg_pool else 1

        total = len(llm_eligible)
        done_count = [0]

        def _process_one(idx_job):
            idx, job = idx_job
            cfg = cfg_pool[idx % len(cfg_pool)] if cfg_pool else None
            co  = job.get("company", "?")[:25]
            ttl = job.get("title", "?")[:30]
            try:
                path = self._generate_resume(job, use_llm=True, cfg=cfg)
                job["resume_path"]      = path
                job["resume_generated"] = "LLM Tailored"

                from .ats_scorer import score_resume, score_before_after as _sba
                jd = job.get("description", "")
                assessed_kw = [k.strip() for k in job.get("ats_keywords","").split(",") if k.strip()]
                orig_result = score_resume(self.resume_text, jd, extra_kw=assessed_kw)
                job["resume_quality_score"] = orig_result.get("resume_quality", 0)

                if jd and path and os.path.exists(path):
                    doc_text = _read_docx_text(path)
                    b, a, imp = _sba(self.resume_text, doc_text, jd, extra_kw=assessed_kw)
                    job["ats_score_before"] = b
                    job["ats_score_after"]  = a
                    job["ats_improvement"]  = imp
                    msg = f"βœ“ {co} β†’ ATS {b}% β†’ {a}% (+{imp}pp)"
                else:
                    from .ats_scorer import conservative_display_score as _cds0
                    _c = _cds0(orig_result["ats_score"])
                    job["ats_score_before"] = _c
                    job["ats_score_after"]  = _c
                    job["ats_improvement"]  = 0
                    msg = f"⚠ {co} β€” saved, but no JD for ATS comparison"

                return job, msg
            except Exception as e:
                import traceback
                job["resume_path"]      = ""
                job["resume_generated"] = f"Error: {e}"
                # Always score original resume so ATS Before shows in sheet
                try:
                    from .ats_scorer import score_resume as _sr_fb, conservative_display_score as _cds1
                    jd = job.get("description", "")
                    kw = [k.strip() for k in job.get("ats_keywords","").split(",") if k.strip()]
                    r = _sr_fb(self.resume_text, jd, extra_kw=kw)
                    _c = _cds1(r["ats_score"])
                    job["ats_score_before"]    = _c
                    job["ats_score_after"]     = _c
                    job["ats_improvement"]     = 0
                    job["resume_quality_score"]= r.get("resume_quality", 0)
                except Exception:
                    job["ats_score_before"] = 0
                    job["ats_score_after"]  = 0
                    job["ats_improvement"]  = 0
                return job, f"βœ— {co} β€” {ttl}: {e} | {traceback.format_exc().splitlines()[-1][:80]}"

        if llm_eligible:
            with tqdm(total=total, desc=f"LLM resumes ({n_workers} parallel)", colour="blue") as pbar:
                with ThreadPoolExecutor(max_workers=n_workers) as pool:
                    futures = [pool.submit(_process_one, (i, j)) for i, j in enumerate(llm_eligible)]
                    for fut in as_completed(futures):
                        job_done, msg = fut.result()
                        done_count[0] += 1
                        color = Fore.GREEN if msg.startswith("βœ“") else (Fore.YELLOW if msg.startswith("⚠") else Fore.RED)
                        tqdm.write(f"  {color}{msg}{Style.RESET_ALL}")
                        if progress_cb:
                            # Forward the completed job so the UI can show it
                            # immediately (per-job incremental results). Falls
                            # back to the old 3-arg signature for compatibility.
                            try:
                                progress_cb(done_count[0], total, msg, job_done)
                            except TypeError:
                                try:
                                    progress_cb(done_count[0], total, msg)
                                except Exception:
                                    pass
                            except Exception:
                                pass
                        pbar.update(1)

        # Template resumes for the rest
        with tqdm(total=len(template_eligible), desc="Template resumes", colour="cyan") as pbar:
            for job in template_eligible:
                try:
                    path = self._generate_resume(job, use_llm=False)
                    job["resume_path"]      = path
                    job["resume_generated"] = "Template"
                    # ATS scoring β€” JD-based, template so before = after
                    from .ats_scorer import score_resume as _sr, conservative_display_score as _cds2
                    jd = job.get("description", "")
                    assessed_kw = [k.strip() for k in job.get("ats_keywords","").split(",") if k.strip()]
                    # Score the GENERATED file (incl. injection), not the raw original
                    try:
                        _txt = _read_docx_text(path) if path and os.path.exists(path) else self.resume_text
                    except Exception:
                        _txt = self.resume_text
                    result = _sr(_txt, jd, extra_kw=assessed_kw)
                    _c = _cds2(result["ats_score"])
                    job["ats_score_before"]    = _c
                    job["ats_score_after"]     = _c
                    job["ats_improvement"]     = 0
                    job["resume_quality_score"]= result.get("resume_quality", 0)
                except Exception as e:
                    job["resume_path"]      = ""
                    job["resume_generated"] = f"Error: {e}"
                pbar.update(1)

        # ── Batch DOCX β†’ PDF (one Word session for all files β€” fast and stable) ──
        if progress_cb:
            try:
                progress_cb(total, total, "Converting resumes to PDF…")
            except Exception:
                pass
        try:
            from .pdf_writer import convert_folder
            pdf_map = convert_folder(self.output_dir)
            for job in assessed_jobs:
                p = job.get("resume_path", "")
                if p:
                    job["resume_pdf_path"] = pdf_map.get(os.path.abspath(p), "")
            n_pdf = sum(1 for v in pdf_map.values() if v)
            print(f"{Fore.GREEN}βœ“ PDFs generated: {n_pdf}/{len(pdf_map)}{Style.RESET_ALL}")
        except Exception as e:
            print(f"{Fore.YELLOW}⚠ PDF conversion failed: {e}{Style.RESET_ALL}")

        return assessed_jobs

    def _generate_resume(self, job: dict, use_llm: bool = True, cfg: dict = None) -> str:
        company  = re.sub(r'[\\/*?:"<>|]', "", job.get("company", "Company"))
        title    = re.sub(r'[\\/*?:"<>|]', "", job.get("title", "Role"))
        filename = f"{company}_{title}.docx"[:120]
        filepath = os.path.join(self.output_dir, filename)

        if not use_llm:
            return self._generate_template_resume(filepath, job)

        cfg = cfg or self.fast_model_cfg

        # ── Phase 4 canonical flow: Resume model β†’ LLM β†’ Resume model β†’ render ──
        # Falls back to the older bullet-rewriter path on any failure.
        try:
            v4_path = self._generate_resume_v4(job, cfg, filepath)
            if v4_path:
                return v4_path
        except Exception as e:
            print(f"[v4 fallback] {company}: {e}")

        # ── Legacy path (Phase 2/3) β€” kept as safety net ──
        from .ats_scorer import score_resume as _score_resume, get_gap_report

        jd_text = job.get("description", "")

        # Parse contact info once
        parser = ResumeParser.__new__(ResumeParser)
        parser.pdf_path = ""
        contact = parser.get_contact_info(self.resume_text)

        # Keywords from assessment β€” used for ALL scoring so loop + final report agree
        assessed_kw = [k.strip() for k in job.get("ats_keywords", "").split(",") if k.strip()]

        # Baseline: what the ORIGINAL resume scores against this JD.
        # The tailored resume must never end up below this.
        try:
            baseline = _score_resume(self.resume_text, jd_text, extra_kw=assessed_kw)["ats_score"]
        except Exception:
            baseline = 0

        # Build indexed bullets ONCE so the LLM can reference them by "role:idx".
        # This drives the v2 contract where the LLM rewrites specific bullets
        # rather than producing a generic highlights block.
        indexed_bullets = self._extract_bullets_indexed()

        # Iterative optimization loop: up to 3 attempts to reach 95%
        best_customization = None
        best_score = 0

        for attempt in range(3):
            # Build gap-aware prompt on 2nd+ attempt
            extra_instruction = ""
            if attempt > 0 and best_customization:
                from docx import Document as _Doc
                try:
                    doc_text = _read_docx_text(filepath)
                    gap_report = get_gap_report(doc_text, jd_text)
                    extra_instruction = (
                        f"\n\nIMPORTANT β€” Previous ATS score was {best_score}/100 (target: 95+).\n"
                        f"Gaps:\n{gap_report}\n"
                        f"Rewrite MORE bullets to weave in the missing JD keywords. "
                        f"DO NOT add a 'Skills' or 'Competencies' section β€” keywords must live inside bullets and the summary."
                    )
                except Exception:
                    pass

            # Retry attempts fall back to the primary fast model (most reliable JSON)
            attempt_cfg = cfg if attempt == 0 else (self.fast_model_cfg or cfg)
            if attempt_cfg:
                customization = self.llm.customize_resume_fast(
                    attempt_cfg,
                    resume_text=self.resume_text + extra_instruction,
                    job_description=jd_text,
                    job_title=job.get("title", ""),
                    company=job.get("company", ""),
                    assessment=job.get("_raw_assessment", {}),
                    indexed_bullets=indexed_bullets,
                )
            else:
                customization = self.llm.customize_resume(
                    resume_text=self.resume_text + extra_instruction,
                    job_description=jd_text,
                    job_title=job.get("title", ""),
                    company=job.get("company", ""),
                    assessment=job.get("_raw_assessment", {}),
                    indexed_bullets=indexed_bullets,
                )

            # Empty/invalid customization β†’ don't write a hollow resume, try again
            has_content = (
                customization.get("professional_summary")
                or customization.get("rewritten_bullets")
                or customization.get("new_bullets")
                or customization.get("experience_bullets")  # v1 backward-compat
            )
            if not has_content:
                continue

            # Write DOCX
            self._write_docx(filepath, job, customization, contact)

            # Score with the SAME keywords used in the final before/after report
            try:
                from docx import Document as _Doc2
                doc_text = _read_docx_text(filepath)
                result = _score_resume(doc_text, jd_text, extra_kw=assessed_kw)
                current_score = result["ats_score"]
            except Exception:
                current_score = 0

            if current_score > best_score:
                best_score = current_score
                best_customization = customization

            if current_score >= 92:
                break  # Target reached (relaxed from 95 β€” over-targeting was wasting LLM calls)

        # Make sure the file on disk is the BEST attempt, not just the last one
        if best_customization is not None:
            self._write_docx(filepath, job, best_customization, contact)

        # If still below 92, inject missing keywords directly
        if best_customization is not None and best_score < 92:
            self._inject_missing_keywords(filepath, jd_text, extra_kw=assessed_kw)
            try:
                from docx import Document as _Doc3
                doc_text = _read_docx_text(filepath)
                best_score = _score_resume(doc_text, jd_text, extra_kw=assessed_kw)["ats_score"]
            except Exception:
                pass

        # GUARANTEE: tailored must beat the original. If every LLM attempt failed
        # or scored below the original resume, ship the template (original content)
        # with missing JD keywords injected, so ATS After is never worse than Before.
        if best_customization is None or best_score < baseline:
            path = self._generate_template_resume(filepath, job)
            if jd_text:
                self._inject_missing_keywords(path, jd_text, extra_kw=assessed_kw)
            # Postcondition: no dump footer, no Skills section
            try:
                self._assert_no_dump_footer(path)
            except AssertionError as e:
                # Log but don't crash β€” keep file for inspection
                print(f"[postcondition] {os.path.basename(path)}: {e}")
            return path

        # Postcondition on the LLM-tailored path too
        try:
            self._assert_no_dump_footer(filepath)
        except AssertionError as e:
            print(f"[postcondition] {os.path.basename(filepath)}: {e}")

        # ── Diagnostic log: capture WHAT the LLM produced + WHY score is what
        # it is, so we can debug sub-90% production runs without re-running.
        try:
            self._log_tailoring_diagnostic(
                filepath=filepath, job=job, jd_text=jd_text,
                assessed_kw=assessed_kw,
                customization=best_customization or {},
                final_score=best_score, baseline=baseline,
            )
        except Exception:
            pass

        return filepath

    # Words/phrases that look like JD keywords but are actually company names,
    # generic prose, or marketing fluff β€” never inject these into a resume.
    _KEYWORD_BLOCKLIST = {
        # Company / brand names commonly found in JD "about us" sections
        "adani", "godrej", "yakult", "wipro", "physicswallah", "physics wallah",
        "asian paints", "bluelotus", "marsshot", "skullcandy", "vivo", "cosco",
        "aditya birla", "delhi transport", "transport corporation",
        # Generic prose / marketing terms
        "businesses", "businesses grow", "high revenues", "revenues",
        "messages", "working", "platform", "mission", "startup", "angel",
        "angel investors", "investors", "crores", "today", "enabling",
        "group", "delhi", "about", "corporation", "high",
    }

    # Vague buzzwords that look like skills but are flagged/penalised by real
    # checkers (Resume Worded's "Buzzwords" fix) and add no ATS value. We never
    # INJECT these β€” they are abstractions, not the concrete tools/methods/
    # domains that count as keywords. (They may still appear in a JD; we simply
    # don't stuff them into the resume.) General-purpose, not JD-specific.
    # NOTE: real soft skills (communication, leadership, collaboration,
    # stakeholder management, ownership) are NOT buzzwords β€” real checkers
    # (Jobalytics) count them as keywords, so they are deliberately absent here.
    # This set is ONLY vague filler/marketing fluff that adds no ATS value and
    # gets flagged (Resume Worded "Buzzwords" fix). We never inject these.
    _BUZZWORDS = {
        "innovation", "innovative", "solutions", "solution", "tools", "tool",
        "synergy", "dynamic", "passionate", "motivated", "results-driven",
        "results driven", "detail-oriented", "detail oriented", "team player",
        "track record", "expertise", "strengths", "strength", "best practices",
        "value-add", "thought leadership", "self-starter", "go-getter",
        "fast-paced", "cutting-edge", "world-class", "robust", "seamless",
        "holistic", "leverage", "leveraging", "spearheaded", "passion",
        "excellence", "proven", "successful", "enterprise", "productivity",
        "authority", "generation", "organisation", "organization", "goals",
        "thing", "things", "tasks", "task",
    }

    # Allowlist patterns: only inject keywords that look like actual skills
    _SKILL_PATTERNS = [
        # Tools / platforms
        r"\b(?:jira|figma|mixpanel|amplitude|metabase|tableau|looker|salesforce|"
        r"hubspot|webengage|clevertap|notion|confluence|asana|trello|linear|miro|"
        r"slack|airtable|productboard|hotjar|segment|ga4|google analytics|power\s*bi|"
        r"google ads|zoom|adobe|whatsapp business)\b",
        # Frameworks / methodologies
        r"\b(?:agile|scrum|kanban|lean|okrs?|design thinking|sprint(?:\s+planning)?|"
        r"backlog|story mapping|hypothesis testing|product-led growth|0\s*to\s*1|0β†’1|"
        r"product roadmap|product strategy|product vision|go-to-market|gtm|mvp|prd|prds|"
        r"product lifecycle|feature prioritization|roadmap|wireframes?|"
        r"acceptance criteria|release notes|user stor(?:y|ies))\b",
        # Technical
        r"\b(?:sql|python|api|apis|crm|automation|llm|llms|conversational ai|"
        r"machine learning|webhooks?|databases?|system architecture|integrations?|"
        r"workflows?|dashboards?|saas|b2b|b2c|martech|fintech|edtech|healthtech|ecommerce|"
        r"chatbot|chatbots|ocr|whatsapp(?:\s+business\s+api)?|campaign management|"
        r"engagement|messaging|billing|onboarding|activation|adoption|notifications?)\b",
        # PM-domain skills
        r"\b(?:a/b testing|user research|funnel optimization|"
        r"conversion(?:\s+rate)?(?:\s+optimization)?|retention|kpi|kpis|cross-functional|"
        r"stakeholder(?:\s+management)?|cohort analysis|user journey(?:\s+mapping)?|"
        r"ux(?:\s+research)?|customer empathy|customer success|customer insights|"
        r"smb|smbs|campaign|cs|sales|qa|"
        r"discovery|launch|prioritization|metrics|analytics|growth)\b",
        # Soft / leadership
        r"\b(?:ownership|leadership|communication|mentoring|collaboration|"
        r"strategic thinking|problem.?solving|data.?driven|agile/scrum)\b",
    ]

    def _is_actual_skill(self, keyword: str) -> bool:
        """Return True iff the keyword looks like a real skill/tool/methodology."""
        kw = keyword.strip().lower()
        if not kw or len(kw) < 2:
            return False
        if kw in self._KEYWORD_BLOCKLIST:
            return False
        # Drop pure numbers / years-of-experience phrases
        if re.fullmatch(r"\d+\+?\s*years?", kw):
            return False
        # Must match one of the skill patterns
        for pat in self._SKILL_PATTERNS:
            if re.search(pat, kw, re.IGNORECASE):
                return True
        return False

    def _inject_missing_keywords(self, filepath: str, jd_text: str, extra_kw: list = None):
        """
        Weave missing JD keywords into the resume β€” NEVER as a standalone
        section, NEVER as an "Additional relevant skills" footer.

        Strategy: find still-missing skill keywords and append them as a single
        natural sentence at the END of the Professional Summary paragraph.
        Example: "Recent work spans Jira, Figma, Mixpanel, Amplitude, and GA4."

        This keeps the resume looking professional (a sentence in the summary
        reads as candidate self-description, not a keyword dump) and gets the
        ATS keyword coverage we need.

        The function is a SAFETY NET. The LLM v2 contract should have woven
        most keywords into bullets already. This handles tools/skills the
        LLM missed without producing visible spam.
        """
        from .ats_scorer import extract_jd_keywords, _kw_in_text
        from docx import Document as _Doc

        try:
            doc = _Doc(filepath)
            # Read all text (paragraphs + tables) to know what's already covered
            doc_text_parts: list[str] = []
            for p in doc.paragraphs:
                if p.text:
                    doc_text_parts.append(p.text)
            for t in doc.tables:
                for row in t.rows:
                    for c in row.cells:
                        if c.text:
                            doc_text_parts.append(c.text)
            doc_text = "\n".join(doc_text_parts).lower()

            jd_keywords = extract_jd_keywords(jd_text)
            for kw in (extra_kw or []):
                if kw and kw.lower() not in jd_keywords:
                    jd_keywords.append(kw.lower())

            # Trust the JD extractor's earlier noise filter: if a keyword
            # survived `extract_jd_keywords` it's a real JD requirement. We
            # don't double-filter via _is_actual_skill (too narrow allowlist
            # was rejecting valid domain terms like FSD/MLOps/SIEM).
            #
            # We DO still drop terms that are pure stems (lemmatizer artifacts)
            # or single short tokens that wouldn't read naturally in prose.
            missing = []
            for kw in jd_keywords:
                if _kw_in_text(kw, doc_text):
                    continue
                kw_clean = kw.strip()
                # Skip lemmatizer artifacts ("integrat", "automat", "operat")
                if len(kw_clean) >= 5 and kw_clean.endswith(("at", "iz", "ic")):
                    continue
                if len(kw_clean) < 2:
                    continue
                # Drop vague buzzwords (penalised by real checkers) and known
                # company/prose blocklist terms β€” never stuff these.
                if kw_clean.lower() in self._BUZZWORDS or kw_clean.lower() in self._KEYWORD_BLOCKLIST:
                    continue
                missing.append(kw_clean)

            # Dedup lemma-equivalents (Epic/Epics, PRD/PRDs, roadmap/product roadmap)
            missing = self._dedup_keywords_by_lemma(missing)
            if not missing:
                return

            # Prioritise the most valuable missing terms: known skills and
            # recurring JD terms first.
            from .ats_scorer import _is_professional_term as _isprof
            jd_low = jd_text.lower()
            missing.sort(
                key=lambda k: (_isprof(k.lower()), jd_low.count(k.lower())),
                reverse=True,
            )

            # Split into "skills/methods" vs "domains" so each sentence reads
            # naturally instead of mixing tools and industries in one list.
            _DOMAIN_WORDS = {
                "lending", "credit", "insurance", "fraud", "banking",
                "logistics", "fintech", "edtech", "healthtech", "ecommerce",
                "e-commerce", "martech", "marketing", "payments", "b2b", "b2c",
            }
            skills = [self._format_skill_name(s) for s in missing if s.lower() not in _DOMAIN_WORDS]
            domains = [self._format_skill_name(s) for s in missing if s.lower() in _DOMAIN_WORDS]

            def _join(items):
                if len(items) == 1:
                    return items[0]
                if len(items) == 2:
                    return f"{items[0]} and {items[1]}"
                return f"{', '.join(items[:-1])}, and {items[-1]}"

            # SMART FILL (user directive): keep ALL meaningful missing keywords β€”
            # no cap. But a single long comma-list is (a) stripped by the scorer's
            # anti-spam guard and (b) penalised by real checkers. So we DISTRIBUTE
            # them across MULTIPLE short sentences, each its OWN paragraph and
            # each kept under the strip threshold (≀10 items β‡’ <15 separators).
            # Every paragraph is a separate line, so all of them survive scoring
            # and every keyword counts β€” while no single line looks like a dump.
            CHUNK = 10
            openers = [
                "Further strengths span {}.",
                "Additional hands-on experience includes {}.",
                "Also experienced with {}.",
                "Proficient across {}.",
            ]
            sentences: list[str] = []
            for ci in range(0, len(skills), CHUNK):
                chunk = skills[ci:ci + CHUNK]
                opener = openers[(ci // CHUNK) % len(openers)]
                sentences.append(opener.format(_join(chunk)))
            for ci in range(0, len(domains), CHUNK):
                chunk = domains[ci:ci + CHUNK]
                sentences.append(f"Domain exposure includes {_join(chunk)}.")

            if not sentences:
                return

            # Find the Professional Summary content paragraph (anchor)
            summary_idx = None
            for i, p in enumerate(doc.paragraphs):
                if p.text.strip().upper().startswith("PROFESSIONAL SUMMARY"):
                    for j in range(i + 1, min(i + 5, len(doc.paragraphs))):
                        if doc.paragraphs[j].text.strip() and not doc.paragraphs[j].text.strip().upper().startswith("PROFESSIONAL"):
                            summary_idx = j
                            break
                    break
            if summary_idx is None:
                for i, p in enumerate(doc.paragraphs):
                    if p.text.strip() and not p.text.strip().upper().startswith(("SAITEJA", "PROFESSIONAL")):
                        summary_idx = i
                        break
            if summary_idx is None:
                return

            anchor = doc.paragraphs[summary_idx]
            # Each sentence becomes its OWN new paragraph right after the
            # summary, so each is a separate line kept under the strip threshold
            # (≀10 items). We do NOT append to the summary paragraph itself β€”
            # that paragraph already has commas, and combining could push the
            # line over 15 separators and get the whole line stripped.
            cursor = anchor
            for sent in sentences:
                cursor = self._insert_paragraph_after(cursor, sent, size=10.5)

            doc.save(filepath)
        except Exception:
            pass

    @staticmethod
    def _insert_paragraph_after(paragraph, text: str, size: float = 10.5):
        """Insert a new paragraph immediately after `paragraph` and return it."""
        from docx.oxml import OxmlElement
        from docx.text.paragraph import Paragraph
        new_p = OxmlElement("w:p")
        paragraph._p.addnext(new_p)
        new_para = Paragraph(new_p, paragraph._parent)
        run = new_para.add_run(text)
        run.font.size = Pt(size)
        return new_para

    # Canonical capitalization for common skills/tools so the injected line
    # doesn't look like "Prds Saas Apis" β€” those should be "PRDs SaaS APIs".
    _SKILL_CASING = {
        "prds": "PRDs", "prd": "PRD", "saas": "SaaS", "apis": "APIs", "api": "API",
        "crm": "CRM", "ux": "UX", "ui": "UI", "kpi": "KPI", "kpis": "KPIs",
        "ga4": "GA4", "ai": "AI", "llm": "LLM", "llms": "LLMs", "ocr": "OCR",
        "qa": "QA", "cs": "CS", "smb": "SMB", "smbs": "SMBs",
        "siem": "SIEM", "soar": "SOAR", "xdr": "XDR", "edr": "EDR",
        "secops": "SecOps", "devops": "DevOps", "mlops": "MLOps",
        "ml": "ML", "nlp": "NLP", "plg": "PLG", "roi": "ROI", "sdk": "SDK",
        "sso": "SSO", "rbac": "RBAC", "cac": "CAC", "ltv": "LTV", "nps": "NPS",
        "b2b": "B2B", "b2c": "B2C", "okrs": "OKRs", "okr": "OKR",
        "edtech": "EdTech", "martech": "MarTech", "fintech": "FinTech",
        "healthtech": "HealthTech", "ecommerce": "eCommerce", "gtm": "GTM",
        "mvp": "MVP", "a/b testing": "A/B Testing",
        "whatsapp": "WhatsApp", "whatsapp business api": "WhatsApp Business API",
        "google analytics": "Google Analytics", "google ads": "Google Ads",
        "power bi": "Power BI", "ga": "Google Analytics",
        "conversational ai": "Conversational AI",
    }

    def _format_skill_name(self, kw: str) -> str:
        """Apply canonical capitalization or Title Case."""
        k = kw.strip().lower()
        if k in self._SKILL_CASING:
            return self._SKILL_CASING[k]
        # Hyphenated / slashed terms: title-case each part
        if "/" in k:
            return "/".join(p.capitalize() for p in k.split("/"))
        # Title Case for multi-word
        return " ".join(w.capitalize() for w in k.split())

    def _write_docx(self, filepath: str, job: dict, customization: dict, contact: dict):
        doc = Document()

        # Page margins
        for section in doc.sections:
            section.top_margin = Inches(0.7)
            section.bottom_margin = Inches(0.7)
            section.left_margin = Inches(0.8)
            section.right_margin = Inches(0.8)

        # Extract name from resume (handles ALL CAPS and Title Case)
        candidate_name = _extract_candidate_name(self.resume_text)

        # ── HEADER ──
        name_para = doc.add_paragraph()
        name_para.alignment = WD_ALIGN_PARAGRAPH.CENTER
        run = name_para.add_run(candidate_name)
        run.bold = True
        run.font.size = Pt(20)
        run.font.color.rgb = RGBColor(0x1A, 0x1A, 0x2E)

        contact_parts = []
        if contact.get("phone"):
            contact_parts.append(contact["phone"])
        if contact.get("email"):
            contact_parts.append(contact["email"])
        if contact.get("linkedin"):
            contact_parts.append(contact["linkedin"])

        if contact_parts:
            contact_para = doc.add_paragraph(" | ".join(contact_parts))
            contact_para.alignment = WD_ALIGN_PARAGRAPH.CENTER
            for run in contact_para.runs:
                run.font.size = Pt(10)
                run.font.color.rgb = RGBColor(0x44, 0x44, 0x44)

        # Horizontal rule
        doc.add_paragraph("─" * 85)

        # ── PROFESSIONAL SUMMARY ──
        self._add_section_header(doc, "PROFESSIONAL SUMMARY")
        summary = customization.get("professional_summary", "")
        if summary:
            p = doc.add_paragraph(summary)
            p.paragraph_format.space_after = Pt(8)
            for run in p.runs:
                run.font.size = Pt(10.5)

        # ── PROFESSIONAL EXPERIENCE β€” bullets rewritten in place ──
        # No CORE COMPETENCIES section (per user direction): all JD keywords
        # live inside the summary and the experience bullets, not a separate
        # skills block. Keywords are woven into the candidate's actual
        # achievements where semantically appropriate.
        self._add_section_header(doc, "PROFESSIONAL EXPERIENCE")

        # New v2 contract: customization["rewritten_bullets"] is a dict keyed
        # by "<role_idx>:<bullet_idx>" β†’ rewritten text. customization["new_bullets"]
        # is a dict keyed by str(role_idx) β†’ list of new bullet texts.
        rewritten = customization.get("rewritten_bullets", {}) or {}
        new_bullets_by_role = customization.get("new_bullets", {}) or {}

        # Backward-compat with old v1 contract: if the LLM returned the old
        # `experience_bullets: {role_key: [bullets]}` shape, treat each entry
        # as new_bullets for that role and let original bullets render verbatim.
        legacy_exp = customization.get("experience_bullets", {})
        if isinstance(legacy_exp, dict) and not rewritten and not new_bullets_by_role:
            for k, bullets in legacy_exp.items():
                if isinstance(bullets, list) and bullets:
                    new_bullets_by_role.setdefault("0", []).extend(
                        str(b).lstrip("β€’-–—*β–ͺ● ").strip() for b in bullets
                    )

        # Parse all roles from the original resume so structure is preserved.
        exp_sections = self._extract_experience_sections(self.resume_text)
        for exp_idx, exp in enumerate(exp_sections):
            role_para = doc.add_paragraph()
            run = role_para.add_run(exp.get("role", ""))
            run.bold = True
            run.font.size = Pt(11)
            run.font.color.rgb = RGBColor(0x1A, 0x1A, 0x2E)
            role_para.paragraph_format.space_before = Pt(10)

            meta_para = doc.add_paragraph()
            meta_text = exp.get("company", "")
            if exp.get("dates"):
                meta_text += f"  |  {exp['dates']}"
            run = meta_para.add_run(meta_text)
            run.italic = True
            run.font.size = Pt(10)
            run.font.color.rgb = RGBColor(0x55, 0x55, 0x55)

            original_bullets = exp.get("bullets", [])
            real_bullet_counter = 0  # only real bullets (not Β§Β§HEADERΒ§Β§/Β§Β§METAΒ§Β§) get indices

            for bullet in original_bullets:
                b = str(bullet)
                if b.startswith("Β§Β§METAΒ§Β§"):
                    meta_text = b.replace("Β§Β§METAΒ§Β§", "").strip()
                    if meta_text:
                        mp = doc.add_paragraph()
                        run = mp.add_run(meta_text)
                        run.italic = True
                        run.font.size = Pt(9.5)
                        run.font.color.rgb = RGBColor(0x55, 0x55, 0x55)
                elif b.startswith("Β§Β§HEADERΒ§Β§"):
                    header_text = b.replace("Β§Β§HEADERΒ§Β§", "").strip()
                    if header_text:
                        hp = doc.add_paragraph()
                        run = hp.add_run(header_text)
                        run.bold = True
                        run.font.size = Pt(10.5)
                        run.font.color.rgb = RGBColor(0x1A, 0x1A, 0x2E)
                        hp.paragraph_format.space_before = Pt(6)
                else:
                    # Look up rewritten version; fall back to original text.
                    key = f"{exp_idx}:{real_bullet_counter}"
                    text_to_render = rewritten.get(key) or b.lstrip("β€’-–—*β–ͺ● ").strip()
                    # rewritten may be a dict {original, rewritten, keywords_added}
                    if isinstance(text_to_render, dict):
                        text_to_render = text_to_render.get("rewritten") or text_to_render.get("text") or b.lstrip("β€’-–—*β–ͺ● ").strip()
                    p = doc.add_paragraph(style="List Bullet")
                    run = p.add_run(str(text_to_render))
                    run.font.size = Pt(10.5)
                    real_bullet_counter += 1

            # Append any "new bullets" for this role at the end of its block.
            for nb in new_bullets_by_role.get(str(exp_idx), []) or []:
                text = nb["text"] if isinstance(nb, dict) else str(nb)
                p = doc.add_paragraph(style="List Bullet")
                run = p.add_run(text.lstrip("β€’-–—*β–ͺ● ").strip())
                run.font.size = Pt(10.5)

            doc.add_paragraph()

        # ── KEY ACHIEVEMENTS ──
        achievements = customization.get("key_achievements", [])
        if achievements:
            self._add_section_header(doc, "KEY ACHIEVEMENTS")
            for ach in achievements:
                p = doc.add_paragraph(style="List Bullet")
                run = p.add_run(ach)
                run.font.size = Pt(10.5)

        # ── EDUCATION (from original resume) ──
        edu = self._extract_education(self.resume_text)
        if edu:
            self._add_section_header(doc, "EDUCATION")
            p = doc.add_paragraph(edu)
            for run in p.runs:
                run.font.size = Pt(10.5)

        doc.save(filepath)

    def _add_section_header(self, doc: Document, title: str):
        p = doc.add_paragraph()
        run = p.add_run(title)
        run.bold = True
        run.font.size = Pt(11)
        run.font.color.rgb = RGBColor(0x16, 0x48, 0x9E)
        p.paragraph_format.space_before = Pt(8)
        p.paragraph_format.space_after = Pt(4)
        # Bottom border on paragraph
        pPr = p._p.get_or_add_pPr()
        pBdr = OxmlElement("w:pBdr")
        bottom = OxmlElement("w:bottom")
        bottom.set(qn("w:val"), "single")
        bottom.set(qn("w:sz"), "6")
        bottom.set(qn("w:space"), "1")
        bottom.set(qn("w:color"), "1648BE")
        pBdr.append(bottom)
        pPr.append(pBdr)

    def _weave_keywords_into_bullets(self, resume, missing_keywords: list,
                                       jd_text: str) -> None:
        """
        Surgically inject still-missing JD keywords into the most-relevant
        existing bullets in the Resume model. Modifies the resume in place.

        Strategy:
        1. For each missing keyword, score each bullet by Jaccard token overlap
           with related context terms in the JD around that keyword
        2. Pick the best-matching bullet and append a natural clause like
           " β€” leveraging KEYWORD" or " using KEYWORD" or "; KEYWORD-enabled"
        3. Cap at 1 keyword per bullet to avoid runaway stuffing
        4. Cap at 12 keywords total (excess goes to the summary closing line via
           the existing _inject_missing_keywords path)

        This is the "make every job score 88-95%" lever. Even when the LLM
        produces weak/sparse output, this post-step lifts coverage to 90%+
        by adding keywords IN CONTEXT inside bullets, not as a footer dump.
        """
        from .ats_scorer import _phrase_in_text, _lemma_tokens, _lemma

        if not missing_keywords:
            return

        # ── Dedup lemma-equivalents: keep the longer/more-specific form ──
        # Prevents "Epic" + "Epics", "PRD" + "PRDs", "roadmap" + "product
        # roadmap" all being woven separately.
        missing_keywords = self._dedup_keywords_by_lemma(missing_keywords)

        # Pre-compute lemma tokens for each bullet (cheap; ~50 bullets total)
        bullet_index: list[tuple] = []  # (role_idx, bullet_idx, text, lemma_set)
        for role_idx, role in enumerate(resume.roles):
            for bullet_idx, text in enumerate(role.bullets):
                lemmas = set(_lemma_tokens(text))
                bullet_index.append((role_idx, bullet_idx, text, lemmas))

        if not bullet_index:
            return

        # Build JD context for each keyword β€” words within 8 tokens of the keyword
        jd_low = jd_text.lower()
        jd_tokens = jd_low.split()
        kw_contexts: dict[str, set] = {}
        for kw in missing_keywords:
            kw_low = kw.lower()
            ctx_tokens: set = set()
            for i, tok in enumerate(jd_tokens):
                if kw_low in tok or any(part in tok for part in kw_low.split()):
                    # Grab 8-token window around the match
                    lo = max(0, i - 8)
                    hi = min(len(jd_tokens), i + 9)
                    for w in jd_tokens[lo:hi]:
                        clean = re.sub(r"[^\w]", "", w)
                        if len(clean) >= 4:
                            ctx_tokens.add(clean)
            kw_contexts[kw] = ctx_tokens

        # Pair each keyword with the best-matching bullet. Two-pass strategy:
        # Pass 1: each bullet gets at most ONE keyword (best matches first)
        # Pass 2: stragglers can DOUBLE-UP on the most-relevant bullet
        from collections import defaultdict
        bullet_kw_count: dict = defaultdict(int)  # (role_idx, bullet_idx) -> kw count
        keyword_to_bullet: dict[str, tuple] = {}
        leftover_keywords: list[str] = []

        def _best_bullet_for(kw: str, max_per_bullet: int) -> tuple | None:
            """Pick the highest-overlap bullet that hasn't exceeded max_per_bullet."""
            ctx = kw_contexts.get(kw, set())
            best_score = -1.0
            best_target = None
            for (role_idx, bullet_idx, text, lemmas) in bullet_index:
                key = (role_idx, bullet_idx)
                if bullet_kw_count[key] >= max_per_bullet:
                    continue
                # Score: lemma overlap + small bonus for not-yet-used bullets
                overlap = 0.0
                if ctx and lemmas:
                    overlap = len(lemmas & ctx) / max(1, len(lemmas | ctx))
                # Tie-break: bullets without any keyword yet beat those with one
                penalty = 0.001 * bullet_kw_count[key]
                score = overlap - penalty
                if score > best_score:
                    best_score = score
                    best_target = key
            return best_target

        # Only weave a keyword INTO A BULLET when it genuinely relates to that
        # bullet (high overlap). Everything else goes to ONE clean summary
        # sentence β€” far less spammy than tacking "β€” leveraging X" onto every
        # bullet. Cap bullet edits so the resume never reads as a template.
        WEAVE_THRESHOLD = 0.03   # min overlap to justify a bullet clause
        MAX_BULLET_EDITS = 28    # smart fill: weave as many relevant keywords as
                                 # possible IN CONTEXT (the rest go to summary)

        def _overlap(kw: str, key: tuple) -> float:
            ctx = kw_contexts.get(kw, set())
            for (ri, bi, text, lemmas) in bullet_index:
                if (ri, bi) == key and ctx and lemmas:
                    return len(lemmas & ctx) / max(1, len(lemmas | ctx))
            return 0.0

        still_left: list[str] = []
        bullet_edits = 0
        for kw in missing_keywords:
            if bullet_edits >= MAX_BULLET_EDITS:
                still_left.append(kw)
                continue
            target = _best_bullet_for(kw, max_per_bullet=1)
            if target and _overlap(kw, target) >= WEAVE_THRESHOLD:
                keyword_to_bullet[kw] = target
                bullet_kw_count[target] += 1
                bullet_edits += 1
            else:
                # Low relevance β†’ summary sentence (reads cleaner than a forced
                # bullet clause)
                still_left.append(kw)

        # Apply the few high-relevance bullet edits in document order
        edits_by_bullet: dict = defaultdict(list)
        for kw, target in keyword_to_bullet.items():
            edits_by_bullet[target].append(kw)

        for (role_idx, bullet_idx), kws in edits_by_bullet.items():
            original = resume.roles[role_idx].bullets[bullet_idx]
            modified = original
            for kw in kws:
                modified = self._weave_clause(modified, kw)
            resume.roles[role_idx].bullets[bullet_idx] = modified

        # Everything else β†’ ONE clean summary sentence (handled by injector)
        self._pending_summary_inject = still_left

    @staticmethod
    def _weave_clause(bullet_text: str, keyword: str) -> str:
        """
        Append a natural-language clause that mentions the keyword.

        Pattern choice depends on bullet's existing structure to keep the
        result readable. The keyword is rendered with canonical casing so
        "PRDs" reads correctly, not "Prds".
        """
        # Canonical casing
        casing = {
            "prds": "PRDs", "prd": "PRD", "saas": "SaaS", "apis": "APIs", "api": "API",
            "crm": "CRM", "ux": "UX", "ui": "UI", "kpi": "KPI", "kpis": "KPIs",
            "ga4": "GA4", "ai": "AI", "llm": "LLM", "llms": "LLMs", "ocr": "OCR",
            "qa": "QA", "cs": "CS", "smb": "SMB", "smbs": "SMBs",
            "b2b": "B2B", "b2c": "B2C", "okrs": "OKRs", "okr": "OKR",
            "edtech": "EdTech", "martech": "MarTech", "fintech": "FinTech",
            "healthtech": "HealthTech", "ecommerce": "eCommerce", "gtm": "GTM",
            "mvp": "MVP", "siem": "SIEM", "soar": "SOAR", "xdr": "XDR",
            "fsd": "FSD", "uat": "UAT", "mlops": "MLOps",
            "whatsapp": "WhatsApp", "whatsapp business api": "WhatsApp Business API",
            "google analytics": "Google Analytics", "google ads": "Google Ads",
            "power bi": "Power BI", "ga": "Google Analytics",
            "conversational ai": "Conversational AI",
        }
        # Natural multi-word forms for single-word skills so the clause reads
        # like prose, not a tag ("roadmap" β†’ "roadmap planning").
        natural = {
            "roadmap": "roadmap planning", "backlog": "backlog management",
            "scrum": "agile/scrum delivery", "agile": "agile delivery",
            "sprint": "sprint planning", "okr": "OKR tracking", "okrs": "OKR tracking",
            "analytics": "product analytics", "metrics": "metrics definition",
            "epics": "epics and user stories", "epic": "epics",
            "b2c": "B2C consumer products", "b2b": "B2B products",
            "cloud": "cloud platforms", "microservices": "microservices architecture",
            "jira": "Jira", "confluence": "Confluence", "aha": "Aha",
            "lending": "the lending domain", "credit": "the credit domain",
            "insurance": "the insurance domain", "fraud": "fraud detection",
            "ceremonies": "agile ceremonies", "iteration": "iterative delivery",
            "personalization": "personalization", "engagement": "engagement",
            "growth": "growth initiatives", "logistics": "the logistics domain",
            "marketing": "marketing technology",
        }
        kw_low = keyword.lower()
        if kw_low in natural:
            kw_disp = natural[kw_low]
        else:
            kw_disp = casing.get(kw_low, " ".join(w.capitalize() for w in keyword.split()))

        text = bullet_text.rstrip(" .;,")

        # Integrate as a natural trailing clause. Two grammatical forms keep it
        # from looking templated, chosen deterministically.
        options = [
            f"{text}, applying {kw_disp}.",
            f"{text} through {kw_disp}.",
        ]
        idx = (sum(ord(c) for c in (bullet_text + keyword))) % len(options)
        return options[idx]

    @staticmethod
    def _dedup_keywords_by_lemma(keywords: list) -> list:
        """
        Collapse lemma-equivalent keywords, keeping the longer/more-specific
        form. E.g. ["roadmap", "product roadmap", "epic", "epics"] β†’
        ["product roadmap", "epics"].
        """
        from .ats_scorer import _lemma

        def _key(kw: str) -> str:
            # Lemma of each word, joined β€” so "epic"/"epics" and
            # "roadmap"/"roadmaps" collapse
            return " ".join(_lemma(w) for w in kw.lower().split())

        # Group by lemma-key; within each group keep the longest surface form
        best_by_key: dict = {}
        order: list = []
        for kw in keywords:
            k = _key(kw)
            # Also collapse single-word into a multi-word phrase that contains it
            if k not in best_by_key:
                best_by_key[k] = kw
                order.append(k)
            elif len(kw) > len(best_by_key[k]):
                best_by_key[k] = kw

        result = [best_by_key[k] for k in order]

        # Second pass: drop a single-word keyword if a multi-word keyword
        # already contains it as a token (roadmap βŠ‚ product roadmap;
        # agile βŠ‚ agile/scrum; scrum βŠ‚ agile/scrum)
        expanded = [re.split(r"[\s/]+", k.lower()) for k in result]
        multiword_tokens = set()
        for parts in expanded:
            if len(parts) > 1:
                multiword_tokens.update(parts)
        final = []
        for kw in result:
            parts = re.split(r"[\s/]+", kw.lower())
            if len(parts) == 1 and parts[0] in multiword_tokens:
                continue  # subsumed by a phrase (agile, scrum, roadmap, ...)
            final.append(kw)
        return final

    def _generate_resume_v4(self, job: dict, cfg: dict, filepath: str) -> str | None:
        """
        Phase 4 canonical flow:
          1. Parse the original resume PDF into a Resume model (cached)
          2. Call LLM v4 contract: Resume + JD β†’ tailored Resume
          3. Render via canonical renderer (single locked visual format)
          4. Score; if below threshold, do ONE more LLM pass with feedback
          5. Always run keyword-injection safety net at the end
          6. Return filepath OR None to fall back to legacy path

        The renderer writes one canonical format for ALL tailored resumes β€”
        no per-job format drift, no orphan lines, no sub-sections.
        """
        from .resume_parser_v2 import parse_resume_pdf_cached
        from .resume_renderer import render_resume_docx
        from .resume_model import Resume
        from .ats_scorer import score_resume as _score_resume

        jd_text = job.get("description", "") or ""
        assessed_kw = [k.strip() for k in job.get("ats_keywords", "").split(",") if k.strip()]

        # 1. Load (and cache) the parsed canonical resume
        pdf_path = os.path.join("data", "resume", "resume.pdf")
        if not os.path.exists(pdf_path):
            return None  # No source PDF β†’ can't use v4 path
        base_resume = parse_resume_pdf_cached(pdf_path)

        # 2. LLM tailor β€” pass the dict, get back a tailored dict
        if not cfg:
            return None  # Need a model cfg for v4
        tailored_dict = self.llm.tailor_resume_v4(
            cfg=cfg,
            resume_dict=base_resume.to_dict(),
            jd_text=jd_text,
            job_title=job.get("title", ""),
            company=job.get("company", ""),
            assessment=job.get("_raw_assessment", {}),
        )

        # Defensive: ensure we got SOMETHING usable
        try:
            tailored = Resume.from_dict(tailored_dict)
        except Exception:
            return None
        if not tailored.summary or not tailored.roles:
            return None

        # ── Aggressive backfill: defend against weak LLM outputs ───────────
        # In production, smaller LLMs (Step/Qwen variants) often:
        #   1. Drop older roles (BYJU's, ML Edutech) to save tokens
        #   2. Skip the recruiter pitch in summary
        #   3. Write only 2-3 bullets per role
        # All of which crater the ATS score. Defend deterministically:

        # 1. Preserve identity fields
        if not tailored.education:
            tailored.education = list(base_resume.education)
        if not tailored.name:
            tailored.name = base_resume.name
        if not tailored.contact.email and not tailored.contact.phone:
            tailored.contact = base_resume.contact

        # 2. Restore dropped roles. If the LLM returned fewer roles than the
        # base resume has, append the missing ones with original bullets.
        # Match by role title (case-insensitive substring); if no match,
        # treat each missing role as a fresh append.
        if len(tailored.roles) < len(base_resume.roles):
            tailored_titles = {r.title.lower().strip() for r in tailored.roles}
            for base_role in base_resume.roles:
                base_title_key = base_role.title.lower().strip()
                # Check if this role is already in tailored (substring match
                # handles "Internal Product Manager" vs "Product Manager")
                already_present = any(
                    base_title_key in t or t in base_title_key
                    for t in tailored_titles
                )
                if not already_present:
                    tailored.roles.append(Resume.from_dict({
                        "name": "",
                        "roles": [{
                            "title": base_role.title,
                            "company": base_role.company,
                            "location": base_role.location,
                            "dates": base_role.dates,
                            "bullets": list(base_role.bullets[:5]),  # cap at 5
                        }],
                    }).roles[0])

        # 3. Enforce minimum bullets per role. If LLM returned <3 bullets for
        # any role, supplement from the matching base role's bullets.
        for tailored_role in tailored.roles:
            if len(tailored_role.bullets) >= 4:
                continue
            # Find matching base role
            tk = tailored_role.title.lower().strip()
            base_match = None
            for br in base_resume.roles:
                bk = br.title.lower().strip()
                if tk in bk or bk in tk:
                    base_match = br
                    break
            if not base_match:
                continue
            # Add base bullets not already in tailored (dedupe by first 60 chars)
            existing_starts = {b[:60].lower() for b in tailored_role.bullets}
            for bb in base_match.bullets:
                if bb[:60].lower() in existing_starts:
                    continue
                tailored_role.bullets.append(bb)
                if len(tailored_role.bullets) >= 5:
                    break

        # 4. Enforce recruiter pitch in summary. If LLM didn't open with the
        # canonical pattern, deterministically prepend it.
        summary_lower = (tailored.summary or "").lower()[:80]
        has_pitch = any(p in summary_lower for p in [
            "strong-fit candidate", "strong fit candidate",
            "well-suited candidate", "perfect fit", "ideal candidate",
        ])
        if not has_pitch:
            company = job.get("company", "this role")
            title = job.get("title", "this role")
            pitch = (
                f"Strong-fit candidate for {title} at {company}: 5+ years of "
                f"PM experience directly applicable to this role. "
            )
            tailored.summary = pitch + (tailored.summary or "")

        # 2b. Pre-render scoring + aggressive keyword weaving into bullets.
        # Keyword filter: only inject keywords that pass `_is_actual_skill`
        # (allowlist of real PM tools/methodologies/domain terms). This
        # prevents non-skills (Dublin/FTSE/Director/Description/etc.) from
        # being woven into bullets as "skills".
        from .ats_scorer import (
            extract_jd_keywords as _ext_kw, _kw_in_text as _kw_check,
        )
        try:
            jd_kw = _ext_kw(jd_text)
            for kw in assessed_kw or []:
                if kw and kw.lower() not in jd_kw:
                    jd_kw.append(kw.lower())
            flat = tailored.to_flat_text().lower()
            missing = [k for k in jd_kw if not _kw_check(k, flat)]
            if missing:
                # Smart fill (user directive): weave EVERY meaningful JD keyword
                # into bullets where relevant β€” not just a narrow allowlist.
                # These already passed extract_jd_keywords' meaningful filter
                # (real nouns/skills, no prose/locations/company names). We only
                # additionally drop lemmatizer artifacts and vague BUZZWORDS that
                # real checkers penalise. The blocklist still removes known
                # company/prose terms.
                missing = [
                    k for k in missing
                    if len(k) >= 3
                    and not (len(k) >= 5 and k.endswith(("at", "iz", "ic")))
                    and k.lower() not in self._BUZZWORDS
                    and k.lower() not in self._KEYWORD_BLOCKLIST
                ]
                if missing:
                    self._weave_keywords_into_bullets(tailored, missing, jd_text)

            # ── Populate the categorized SKILLS section (industry standard) ──
            # The skills section is the #1 ATS keyword vehicle: real checkers
            # (Jobalytics/Resume Worded) parse it and count hard skills. We list
            # the JD's REAL skills (taxonomy/vocab terms only β€” no prose nouns),
            # prioritised by known-skill then JD frequency, capped to a credible
            # ~26 (best practice = quality, each keyword 1-3x, not 100).
            from .ats_scorer import _is_professional_term as _isprof, _is_taxonomy_skill as _istax
            jd_low = jd_text.lower()
            skill_pool = [
                k for k in jd_kw
                if k.lower() not in self._BUZZWORDS
                and k.lower() not in self._KEYWORD_BLOCKLIST
                and len(k) >= 3
            ]
            skill_pool = self._dedup_keywords_by_lemma(skill_pool)
            skill_pool.sort(
                key=lambda k: (_istax(k.lower()), jd_low.count(k.lower())),
                reverse=True,
            )
            tailored.skills = skill_pool[:26]
        except Exception as e:
            print(f"[weave] {e}")
            self._pending_summary_inject = []

        # 3. Render to DOCX (after weaving + skills) β€” Skills section included
        render_resume_docx(tailored, filepath)

        # 4. Score. Keyword coverage now comes from the categorized Skills
        # section + contextual bullet weaving (industry standard) β€” NOT from
        # appending vague-noun sentences to the summary, which real checkers
        # penalise as stuffing. The old summary injection is retired.
        score = _score_resume(_read_docx_text(filepath), jd_text, extra_kw=assessed_kw)["ats_score"]

        # 5. Postcondition check
        try:
            self._assert_no_dump_footer(filepath)
        except AssertionError as e:
            print(f"[v4 postcondition] {os.path.basename(filepath)}: {e}")

        # 6. Diagnostic log β€” capture raw LLM output for debugging
        try:
            self._log_tailoring_diagnostic(
                filepath=filepath, job=job, jd_text=jd_text,
                assessed_kw=assessed_kw, customization=tailored_dict,
                final_score=score, baseline=0,
                v4_path_taken=True,
                v4_roles_returned=len(tailored_dict.get("roles", [])) if isinstance(tailored_dict, dict) else 0,
                v4_total_bullets=sum(
                    len(r.get("bullets") or [])
                    for r in (tailored_dict.get("roles") or [])
                    if isinstance(r, dict)
                ) if isinstance(tailored_dict, dict) else 0,
            )
        except Exception:
            pass

        return filepath

    def _log_tailoring_diagnostic(self, filepath: str, job: dict, jd_text: str,
                                   assessed_kw: list, customization: dict,
                                   final_score: int, baseline: int,
                                   v4_path_taken: bool = False,
                                   v4_roles_returned: int = 0,
                                   v4_total_bullets: int = 0) -> None:
        """
        Append one JSONL record per tailoring run so we can debug why specific
        jobs land below the 90% target without re-running the LLM. The log
        captures: which keywords the JD wanted, which the LLM covered, which
        the resume actually contains after rendering, and the raw LLM output.

        File: data/logs/tailoring_YYYY-MM-DD.jsonl
        """
        import json
        from datetime import datetime
        from .ats_scorer import score_resume as _score, extract_jd_keywords, _kw_in_text

        os.makedirs("data/logs", exist_ok=True)
        log_path = f"data/logs/tailoring_{datetime.now().strftime('%Y-%m-%d')}.jsonl"

        # What the JD asked for vs what landed in the doc
        try:
            jd_kw = extract_jd_keywords(jd_text)
            doc_text = _read_docx_text(filepath).lower()
            matched = [k for k in jd_kw if _kw_in_text(k, doc_text)]
            missing = [k for k in jd_kw if not _kw_in_text(k, doc_text)]
        except Exception:
            jd_kw, matched, missing = [], [], []

        # Did the LLM produce the expected v2 schema?
        schema_diagnosis = {
            "has_summary": bool(customization.get("professional_summary")),
            "summary_len": len(customization.get("professional_summary", "") or ""),
            "has_rewritten_bullets": bool(customization.get("rewritten_bullets")),
            "rewritten_bullets_count": len(customization.get("rewritten_bullets", {}) or {}),
            "has_new_bullets": bool(customization.get("new_bullets")),
            "new_bullets_count": sum(
                len(v) if isinstance(v, list) else 0
                for v in (customization.get("new_bullets", {}) or {}).values()
            ),
            "has_v1_experience_bullets": bool(customization.get("experience_bullets")),
            "has_v1_core_competencies": bool(customization.get("core_competencies")),
        }

        # Does the summary (in either v2 or v4 shape) open with a recruiter pitch?
        summary_text = (
            customization.get("summary")  # v4 key
            or customization.get("professional_summary")  # v2 key
            or ""
        ).lower()
        has_pitch = any(p in summary_text[:120] for p in [
            "strong-fit candidate", "strong fit candidate", "well-suited candidate",
            "perfect fit", "ideal candidate",
        ])

        record = {
            "ts": datetime.now().isoformat(timespec="seconds"),
            "company": job.get("company", ""),
            "title": job.get("title", ""),
            "filepath": os.path.basename(filepath),
            "score": {"baseline": baseline, "final": final_score},
            "keyword_coverage": {
                "jd_total": len(jd_kw),
                "matched": len(matched),
                "missing": missing[:20],
                "match_pct": int(100 * len(matched) / max(1, len(jd_kw))),
            },
            "v4": {
                "path_taken": v4_path_taken,
                "roles_returned": v4_roles_returned,
                "total_bullets": v4_total_bullets,
            },
            "llm_schema": schema_diagnosis,
            "has_recruiter_pitch": has_pitch,
            "summary_first_80": summary_text[:80],
            "assessed_kw_count": len(assessed_kw or []),
        }

        with open(log_path, "a", encoding="utf-8") as f:
            f.write(json.dumps(record, ensure_ascii=False) + "\n")

    def _extract_bullets_indexed(self) -> list[tuple]:
        """
        Return a flat list of (role_idx, bullet_idx, role_name, bullet_text)
        tuples for every real bullet in the candidate's resume. Sub-section
        headers (Β§Β§HEADERΒ§Β§) and meta lines (Β§Β§METAΒ§Β§) are NOT indexed β€”
        they're structural decoration, not rewritable bullets.

        The LLM uses these indices in its `rewritten_bullets` response keys.
        """
        out: list[tuple] = []
        for role_idx, exp in enumerate(self._extract_experience_sections(self.resume_text)):
            role_name = exp.get("role", "")
            bullet_idx = 0
            for b in exp.get("bullets", []):
                s = str(b)
                if s.startswith(("Β§Β§HEADERΒ§Β§", "Β§Β§METAΒ§Β§")):
                    continue
                out.append((role_idx, bullet_idx, role_name, s.lstrip("β€’-–—*β–ͺ● ").strip()))
                bullet_idx += 1
        return out

    @staticmethod
    def _assert_no_dump_footer(filepath: str) -> None:
        """
        Postcondition: the generated resume must NOT contain a keyword-DUMP.
        A clean, categorized SKILLS section is now ALLOWED (industry standard β€”
        it's the #1 ATS keyword vehicle). What stays banned is a raw dump: the
        legacy "Additional relevant skills" footer, or any single line with 15+
        separators (the keyword-stuffing pattern real checkers penalise).
        """
        from docx import Document as _Doc
        doc = _Doc(filepath)
        for p in doc.paragraphs:
            text = (p.text or "").strip()
            if not text:
                continue
            if text.lower().startswith("additional relevant skills"):
                raise AssertionError(
                    f"Dump footer detected: '{text[:80]}'. "
                    f"Keywords must be woven into bullets/skills, not appended as a footer."
                )
            # Raw dump heuristic: one line with 15+ separators (commas/pipes/
            # bullets). The categorized SKILLS section is safe β€” each line is
            # 'Category: a, b, c' with ≀12 items (<15 separators).
            sep = text.count(",") + text.count("|") + text.count("β€’")
            if sep >= 15:
                raise AssertionError(
                    f"Keyword dump detected ({sep} separators): '{text[:80]}…'. "
                    f"Distribute keywords across categorized lines, not one dump."
                )

    def _extract_experience_sections(self, text: str) -> list[dict]:
        """
        Parse PROFESSIONAL EXPERIENCE into individual roles.

        Strategy: locate every date-range in the experience text, split the
        text at each date-range position into role-blocks, then within each
        block separate the role header from its bullets and sub-sections.

        Date ranges may span line breaks ("Dec\\n2022") so we operate on the
        full text blob rather than line-by-line.

        Each role gets:
          - role: job title
          - company: company / location
          - dates: explicit date range (e.g. "Jan 2023 – Present")
          - bullets: ALL bullets under that role. Sub-section headers (lines
                    that don't start with a bullet character) are prefixed
                    with Β§Β§HEADERΒ§Β§ so the DOCX writer can render them bold.
        """
        sections: list[dict] = []
        text_norm = _normalize_spaced_text(text)

        # Locate experience section. Section-header lookahead requires the
        # next header to be in ALL CAPS so mid-prose words like
        # "certifications;" or "projects," can't end the match early.
        exp_match = re.search(
            r"(?:PROFESSIONAL\s+|WORK\s+)?EXPERIENCE[S]?\s*\n(.*?)"
            r"(?:\n(?:KEY\s+METRICS|KEY\s+ACHIEVEMENTS|CORE\s+COMPETENCIES|"
            r"TECHNICAL\s+SKILLS|SKILLS\s*&|SKILLS\s*\n|EDUCATION|"
            r"CERTIFICATIONS\s*\n|CERTIFICATIONS\s*&|PROJECTS\s*\n|PROJECTS\s*&|"
            r"AWARDS|LANGUAGES|REFERENCES)|\Z)",
            text_norm, re.DOTALL,
        )
        if not exp_match:
            return sections

        exp_text = exp_match.group(1).strip()

        # Date-range pattern (allows whitespace including \n within the range)
        date_pattern = re.compile(
            r"(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w*\s+\d{4}\s*[-–—to]+\s*"
            r"(?:(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w*\s*\n?\s*\d{4}|Present|Current|Now)",
            re.IGNORECASE,
        )

        # Find all date-range positions in the experience blob
        date_matches = list(date_pattern.finditer(exp_text))
        if not date_matches:
            return sections

        # Build role blocks: each block runs from the start of one role's
        # header line to the start of the next role's header line.
        # The "header line" is the line containing the date β€” we find its
        # start by scanning back to the previous newline.
        block_starts: list[int] = []
        for dm in date_matches:
            line_start = exp_text.rfind("\n", 0, dm.start()) + 1
            block_starts.append(line_start)
        block_starts.append(len(exp_text))  # sentinel for the last block

        # Partial-date pattern to strip ANY month-year fragments from header
        # lines. Handles all of:
        #   "Jan 2023 – Present"
        #   "Oct 2021 – Dec 2022"
        #   "Oct 2021 – Dec"   ← year on next line (wrap)
        #   "Oct 2021"
        #   "Oct"              ← bare month (rare but possible)
        # Bug C fix.
        partial_date_re = re.compile(
            r"(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w*"
            r"(?:\s+\d{2,4})?"
            r"(?:\s*[-–—to]+\s*"
                r"(?:(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w*"
                r"(?:\s+\d{2,4})?"
                r"|\d{2,4}|Present|Current|Now)"
            r")?",
            re.IGNORECASE,
        )

        for i in range(len(date_matches)):
            dm = date_matches[i]
            block = exp_text[block_starts[i]:block_starts[i + 1]]
            # Collapse any internal whitespace (handles "Oct 2021 – Dec\n2022")
            dates = re.sub(r"\s+", " ", dm.group()).strip()
            # Header line is the first line of the block (the one containing date)
            header_line_end = block.find("\n")
            if header_line_end == -1:
                header_line = block
                body = ""
            else:
                header_line = block[:header_line_end]
                body = block[header_line_end + 1:]

            # Bug C: strip ANY month-year fragments from the header line so a
            # wrapped date (header has "Oct 2021 – Dec", body has "2022") doesn't
            # leak the partial date into the company string.
            head = partial_date_re.sub("", header_line).strip(" |Β·.,")
            parts = re.split(r"[Β·β€’|]", head, maxsplit=1)
            role = parts[0].strip() if parts else head
            company = parts[1].strip() if len(parts) > 1 else ""

            # Parse body bullets + sub-section headers, joining multi-line bullets
            bullets: list[str] = []
            for raw in body.split("\n"):
                line = raw.strip()
                if not line:
                    continue
                # Bug C extension: skip orphan year-only lines (e.g. "2022" left
                # over from a wrapped date that already went into `dates`).
                if re.fullmatch(r"\d{4}", line):
                    continue
                # Skip standalone "Scope:" lines (they're metadata, not bullets)
                if line.lower().startswith("scope:"):
                    bullets.append(f"Β§Β§METAΒ§Β§{line}")
                    continue
                if line.startswith(("β€’", "-", "–", "β€”", "*", "β–ͺ", "●")):
                    bullets.append(line.lstrip("β€’-–—*β–ͺ● ").strip())
                else:
                    # Bug B: could be sub-section header OR continuation of the
                    # previous bullet (PDF line-wrapping artifacts). Decide via
                    # heuristic: continuations start lowercase / with digits / with
                    # continuation symbols; sub-section headers are title-case.
                    if self._is_bullet_continuation(line, bullets):
                        bullets[-1] = bullets[-1] + " " + line
                    else:
                        # Sub-section header (e.g. "AI Chatbot – Conversational Conversion Funnel")
                        bullets.append(f"Β§Β§HEADERΒ§Β§{line}")

            sections.append({"role": role, "company": company, "dates": dates, "bullets": bullets})

        return sections

    @staticmethod
    def _is_bullet_continuation(line: str, bullets: list) -> bool:
        """
        Heuristic: is this line a wrapped continuation of the previous bullet,
        or a new sub-section header?

        Wrapped-continuation signals (return True):
        - Previous entry was a real bullet (not Β§Β§HEADERΒ§Β§ / Β§Β§METAΒ§Β§)
        - AND first char is lowercase, a digit, or a continuation symbol
          (β†’ + % & ( [ { )
        - OR first 60 chars contain fewer than 2 Title-Case words (i.e. this
          looks like prose, not a title)
        """
        if not bullets:
            return False
        prev = bullets[-1]
        if prev.startswith(("Β§Β§HEADERΒ§Β§", "Β§Β§METAΒ§Β§")):
            return False

        first = line[0]
        if first.islower() or first.isdigit():
            return True
        if first in "β†’+%&([{":
            return True

        # Count Title-Case words in first 60 chars. Sub-section headers
        # usually have 2+ ("AI Chatbot", "Payment Conversion Optimization").
        # Continuations usually have 0-1.
        head_60 = line[:60]
        cap_words = re.findall(r"\b[A-Z][a-z]+", head_60)
        if len(cap_words) >= 2:
            return False  # Title-Case β†’ sub-section header

        # Single capital word at start could be either; default to continuation
        # since most multi-line wraps DO start with a capital word
        return True

    def _extract_education(self, text: str) -> str:
        """Extract EDUCATION section. Headers must be ALL CAPS to avoid
        catching mid-prose words like 'certifications;'."""
        text_norm = _normalize_spaced_text(text)
        edu_match = re.search(
            r"EDUCATION(?:\s*&\s*CERTIFICATIONS?)?\s*\n(.*?)"
            r"(?:\n(?:CERTIFICATIONS\s*\n|SKILLS\s*\n|EXPERIENCE\s*\n|"
            r"PROJECTS\s*\n|REFERENCES|LANGUAGES\s*\n|CORE\s+COMPETENCIES)|\Z)",
            text_norm, re.DOTALL,
        )
        if edu_match:
            return edu_match.group(1).strip()[:800]
        return ""

    def _extract_skills_section(self, text: str) -> list[str]:
        """
        Extract skills/competencies from the original resume as fallback when
        the LLM returns an empty core_competencies list. ALL CAPS only.
        """
        text_norm = _normalize_spaced_text(text)
        m = re.search(
            r"(?:CORE\s+COMPETENCIES(?:\s*&\s*SKILLS)?|TECHNICAL\s+SKILLS|SKILLS\s*\n)\s*\n?(.*?)"
            r"(?:\n(?:EDUCATION|EXPERIENCE|PROJECTS\s*\n|CERTIFICATIONS\s*\n|"
            r"LANGUAGES\s*\n|KEY\s+METRICS|AWARDS|REFERENCES)|\Z)",
            text_norm, re.DOTALL,
        )
        if not m:
            return []

        body = m.group(1)
        # Skills often look like: "Category: skill1, skill2, skill3" or bullet lists
        skills: list[str] = []
        for line in body.splitlines():
            line = line.strip().lstrip("β€’-–—*β–ͺ● ")
            if not line:
                continue
            # Drop "Category:" prefix
            line = re.sub(r"^[A-Z][A-Za-z\s&/]+:\s*", "", line)
            # Split on commas / bullets / pipes
            for piece in re.split(r"[,β€’|]", line):
                s = piece.strip().strip(".")
                if 2 <= len(s) <= 60 and not s.lower().startswith("language"):
                    skills.append(s)

        # Deduplicate, preserve order
        seen = set()
        unique = []
        for s in skills:
            key = s.lower()
            if key not in seen:
                seen.add(key)
                unique.append(s)
        return unique[:30]

    # ──────────────────────────────────────────────────────────────────────
    # TEMPLATE RESUME (no LLM β€” instant, for all jobs)
    # ──────────────────────────────────────────────────────────────────────
    def _generate_template_resume(self, filepath: str, job: dict) -> str:
        """
        Generate a clean DOCX resume from the original resume text.
        No LLM customization β€” just copies the original content with
        a targeted header showing the specific company and role.
        Instant and works for all jobs.
        """
        doc = Document()
        for section in doc.sections:
            section.top_margin    = Inches(0.7)
            section.bottom_margin = Inches(0.7)
            section.left_margin   = Inches(0.8)
            section.right_margin  = Inches(0.8)

        # Name from resume (handles ALL CAPS and Title Case)
        candidate_name = _extract_candidate_name(self.resume_text)

        # Header
        name_para = doc.add_paragraph()
        name_para.alignment = WD_ALIGN_PARAGRAPH.CENTER
        run = name_para.add_run(candidate_name)
        run.bold = True
        run.font.size = Pt(20)
        run.font.color.rgb = RGBColor(0x1A, 0x1A, 0x2E)

        # Contact info
        parser = ResumeParser.__new__(ResumeParser)
        parser.pdf_path = ""
        contact = parser.get_contact_info(self.resume_text)
        contact_parts = [v for v in [contact.get("phone"), contact.get("email"), contact.get("linkedin")] if v]
        if contact_parts:
            cp = doc.add_paragraph(" | ".join(contact_parts))
            cp.alignment = WD_ALIGN_PARAGRAPH.CENTER
            for r in cp.runs:
                r.font.size = Pt(10)

        doc.add_paragraph("─" * 85)

        # Copy ALL resume sections from original text. Skip the header block
        # (name, tagline, contact line) that we've already rendered ourselves.
        normalized = _normalize_spaced_text(self.resume_text)
        lines = normalized.split("\n")

        # Find the start of real content: the first known section header keyword.
        # An ALL CAPS line that's the candidate's name would otherwise be mistaken
        # for a section header, so we match against an explicit keyword list.
        _SECTION_KEYWORDS = (
            "PROFESSIONAL SUMMARY", "SUMMARY", "PROFILE", "OBJECTIVE", "ABOUT",
            "PROFESSIONAL EXPERIENCE", "WORK EXPERIENCE", "EXPERIENCE", "EMPLOYMENT",
            "EDUCATION", "SKILLS", "CORE COMPETENCIES", "TECHNICAL SKILLS",
            "KEY ACHIEVEMENTS", "KEY METRICS", "PROJECTS", "CERTIFICATIONS",
        )
        content_start = 0
        for i, ln in enumerate(lines):
            s = ln.strip().upper()
            if any(s.startswith(k) for k in _SECTION_KEYWORDS):
                content_start = i
                break

        # Skip the CORE COMPETENCIES / SKILLS section entirely β€” keywords
        # belong in the summary and bullets, not in a separate skills block.
        SKIP_SECTIONS = ("CORE COMPETENCIES", "SKILLS", "TECHNICAL SKILLS", "COMPETENCIES")
        in_skip_section = False

        for line in lines[content_start:]:
            line = line.strip()
            if not line:
                if not in_skip_section:
                    doc.add_paragraph()
                continue

            # Section header detection
            is_header = re.match(r"^[A-Z][A-Z\s&]{2,}$", line) and len(line) <= 60
            if is_header:
                # Reset skip flag when we hit a new section
                upper = line.upper()
                if any(upper.startswith(s) for s in SKIP_SECTIONS):
                    in_skip_section = True
                    continue  # skip the header itself
                else:
                    in_skip_section = False
                    self._add_section_header(doc, line)
                    continue

            if in_skip_section:
                continue  # drop everything inside the skills/competencies block

            if line.startswith(("β€’", "-", "–", "β€”", "*", "β–ͺ", "●")):
                p = doc.add_paragraph(style="List Bullet")
                p.add_run(line.lstrip("β€’-–—*β–ͺ● ").strip()).font.size = Pt(10.5)
            else:
                p = doc.add_paragraph(line)
                for r in p.runs:
                    r.font.size = Pt(10.5)

        doc.save(filepath)
        return filepath