File size: 79,185 Bytes
5909b52
 
 
 
 
3247bda
5909b52
3247bda
 
 
 
5909b52
 
3247bda
5909b52
 
 
 
24482a2
 
3247bda
 
 
 
 
 
5909b52
3247bda
 
 
 
5909b52
3247bda
24482a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5909b52
 
 
 
3247bda
24482a2
 
 
 
5909b52
 
3247bda
e6e3a8d
5909b52
e6e3a8d
24482a2
e6e3a8d
 
 
 
 
 
 
 
 
24482a2
 
 
 
 
 
 
5909b52
e6e3a8d
24482a2
e6e3a8d
 
 
 
 
 
 
 
 
24482a2
 
 
 
 
 
 
5909b52
e6e3a8d
24482a2
e6e3a8d
 
 
 
 
 
 
 
 
24482a2
 
 
 
 
 
 
5909b52
 
3247bda
e6e3a8d
5909b52
24482a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3247bda
e6e3a8d
24482a2
 
 
 
e6e3a8d
3247bda
 
5909b52
3247bda
5909b52
71a91ad
 
55e4536
 
 
 
 
 
 
 
 
 
 
 
 
5909b52
24482a2
 
 
 
 
e6e3a8d
3247bda
 
5909b52
e6e3a8d
24482a2
 
 
5909b52
 
 
3247bda
e6e3a8d
 
24482a2
 
3247bda
 
 
 
e6e3a8d
 
5909b52
3247bda
 
 
24482a2
3247bda
24482a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3247bda
5909b52
3247bda
5909b52
 
63fe694
24482a2
 
 
 
 
 
 
 
 
3247bda
 
5909b52
3247bda
 
5909b52
 
 
3247bda
24482a2
cd183ea
 
3247bda
5909b52
3247bda
5909b52
24482a2
3247bda
 
5909b52
3247bda
5909b52
3247bda
5909b52
3247bda
24482a2
3247bda
5909b52
3247bda
 
5909b52
3247bda
5909b52
3247bda
 
 
 
 
5909b52
3247bda
 
5909b52
3247bda
 
63fe694
cd183ea
3247bda
 
 
 
 
 
 
 
24482a2
3247bda
 
 
 
 
 
 
5909b52
3247bda
cd183ea
 
24482a2
71a91ad
cd183ea
71a91ad
3247bda
71a91ad
cd183ea
71a91ad
 
 
 
24482a2
71a91ad
 
 
 
 
3247bda
24482a2
 
 
 
 
 
 
 
 
5909b52
3247bda
 
24482a2
 
 
 
 
 
 
 
a8cf587
3247bda
5909b52
 
3247bda
 
 
 
 
 
5909b52
 
3247bda
 
5909b52
e6e3a8d
3247bda
 
5909b52
 
 
3247bda
24482a2
 
 
 
 
 
 
 
 
 
71a91ad
 
 
 
24482a2
 
 
 
 
 
71a91ad
 
3247bda
 
5909b52
 
3247bda
 
24482a2
 
 
 
 
 
 
 
5909b52
 
 
 
 
 
71a91ad
24482a2
71a91ad
5909b52
24482a2
 
5909b52
3247bda
e6e3a8d
 
3247bda
 
 
 
e6e3a8d
 
3247bda
 
 
 
e6e3a8d
 
3247bda
5909b52
 
3247bda
5909b52
 
6c23e55
5909b52
 
3247bda
 
5909b52
3247bda
 
 
 
 
 
 
 
 
 
 
 
 
 
5909b52
 
 
 
3247bda
5909b52
 
 
 
3247bda
5909b52
 
3247bda
 
 
 
 
 
5909b52
 
 
3247bda
5909b52
3247bda
 
 
24482a2
 
5909b52
 
3247bda
5909b52
 
24482a2
3247bda
5909b52
3247bda
83e80b8
3247bda
 
 
 
 
5909b52
3247bda
5909b52
3247bda
5909b52
 
 
3247bda
 
 
5909b52
 
3247bda
71a91ad
24482a2
 
 
 
71a91ad
3247bda
 
 
 
 
 
 
 
 
 
 
5909b52
3247bda
 
 
 
 
71a91ad
 
 
 
3247bda
 
 
 
 
 
 
 
 
5909b52
 
3247bda
 
5909b52
3247bda
5909b52
3247bda
 
 
5909b52
3247bda
5909b52
 
3247bda
 
 
 
 
5909b52
 
3247bda
 
 
5909b52
71a91ad
24482a2
71a91ad
 
24482a2
 
 
 
 
3247bda
 
 
5909b52
3247bda
 
5909b52
 
3247bda
 
5909b52
3247bda
 
 
 
 
 
71a91ad
3247bda
71a91ad
 
3247bda
5909b52
 
3247bda
 
 
 
 
5909b52
3247bda
 
 
 
 
 
 
 
 
 
 
 
71a91ad
 
 
 
3247bda
 
 
 
 
 
 
24482a2
3247bda
 
5909b52
 
3247bda
 
71a91ad
3247bda
 
 
 
 
 
71a91ad
 
 
 
 
 
 
24482a2
71a91ad
3247bda
24482a2
3247bda
 
 
 
 
 
5909b52
3247bda
24482a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5909b52
3247bda
5909b52
3247bda
 
 
 
 
5909b52
3247bda
 
 
 
 
 
 
 
 
5909b52
3247bda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5909b52
 
3247bda
 
5909b52
3247bda
5909b52
3247bda
 
 
 
 
 
5909b52
3247bda
 
 
 
 
 
 
 
 
 
 
 
 
 
5909b52
3247bda
 
5909b52
3247bda
 
 
 
 
 
 
 
 
5909b52
3247bda
5909b52
 
 
3247bda
5909b52
 
3247bda
 
 
 
 
 
5909b52
3247bda
5909b52
24482a2
5909b52
 
3247bda
 
5909b52
 
 
 
3247bda
 
 
 
 
 
 
 
83e80b8
3247bda
 
 
5909b52
3247bda
 
 
 
5909b52
3247bda
 
5909b52
3247bda
 
5909b52
3247bda
5909b52
 
 
3247bda
5909b52
 
 
 
3247bda
 
5909b52
3247bda
 
5909b52
 
 
3247bda
5909b52
 
 
3247bda
5909b52
3247bda
5909b52
3247bda
5909b52
3247bda
5909b52
 
3247bda
 
5909b52
 
3247bda
5909b52
 
 
 
 
 
3247bda
5909b52
3247bda
5909b52
3247bda
5909b52
3247bda
 
5909b52
 
 
3247bda
5909b52
 
 
3247bda
 
5909b52
3247bda
 
 
 
 
 
5909b52
 
3247bda
5909b52
3247bda
5909b52
3247bda
83e80b8
5909b52
3247bda
5909b52
 
3247bda
 
5909b52
 
3247bda
5909b52
3247bda
 
5909b52
 
 
3247bda
5909b52
 
 
3247bda
 
5909b52
3247bda
 
 
 
 
 
71a91ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3247bda
71a91ad
 
 
 
 
 
 
 
 
 
 
 
 
24482a2
 
 
 
3247bda
24482a2
 
 
 
 
 
 
 
 
 
 
3247bda
24482a2
 
 
3247bda
5909b52
3247bda
5909b52
3247bda
5909b52
 
3247bda
 
 
 
 
 
 
 
5909b52
 
 
 
3247bda
 
5909b52
 
3247bda
24482a2
 
 
 
5909b52
24482a2
5909b52
 
 
24482a2
5909b52
 
 
 
 
 
 
3247bda
 
24482a2
 
 
 
 
 
 
 
 
3247bda
 
 
5909b52
 
 
24482a2
 
5909b52
 
3247bda
cd183ea
 
3247bda
 
 
 
 
 
cd183ea
24482a2
 
 
 
 
 
 
 
 
 
 
 
3247bda
 
 
5909b52
3247bda
 
 
 
 
 
 
 
 
 
 
 
 
 
24482a2
 
 
 
 
 
 
 
 
 
 
 
 
3247bda
 
 
24482a2
 
 
3247bda
 
24482a2
 
 
 
e6e3a8d
3247bda
24482a2
 
 
 
 
 
3247bda
 
 
24482a2
3247bda
24482a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3247bda
 
 
 
e6e3a8d
 
 
 
3247bda
 
 
 
 
 
 
 
 
 
 
 
 
24482a2
 
 
 
 
 
 
 
3247bda
3a33624
 
 
24482a2
3a33624
 
 
 
 
24482a2
 
3a33624
 
e6e3a8d
24482a2
3a33624
 
24482a2
e6e3a8d
3a33624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24482a2
 
3a33624
 
 
 
24482a2
3a33624
 
 
 
 
 
24482a2
 
 
 
3a33624
 
 
 
 
 
 
 
 
 
24482a2
 
 
 
3a33624
24482a2
3a33624
24482a2
3a33624
24482a2
3a33624
 
24482a2
 
 
 
 
 
 
 
 
 
 
 
3a33624
 
 
 
 
 
 
24482a2
3a33624
24482a2
 
 
 
 
 
3a33624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6e3a8d
3a33624
 
 
 
 
 
e6e3a8d
0ccf0a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a33624
 
0ccf0a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a33624
 
0ccf0a7
 
 
 
 
e6e3a8d
 
0ccf0a7
 
 
 
 
3a33624
 
 
 
e6e3a8d
 
 
0ccf0a7
3a33624
 
0ccf0a7
 
 
 
 
3a33624
0ccf0a7
 
 
 
 
 
 
3a33624
 
0ccf0a7
e6e3a8d
 
0ccf0a7
 
 
 
e6e3a8d
 
 
3a33624
0ccf0a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a33624
 
 
e6e3a8d
 
 
 
3a33624
 
0ccf0a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8cf587
 
 
0ccf0a7
24482a2
 
 
 
 
0ccf0a7
 
24482a2
 
 
 
 
 
 
0ccf0a7
 
24482a2
 
a8cf587
 
 
0ccf0a7
 
a8cf587
 
 
 
0ccf0a7
 
a8cf587
3a33624
0ccf0a7
3a33624
24482a2
 
0ccf0a7
a8cf587
0ccf0a7
 
 
a8cf587
0ccf0a7
 
 
24482a2
a8cf587
 
 
0ccf0a7
 
a8cf587
 
0ccf0a7
 
a8cf587
0ccf0a7
 
24482a2
 
 
0ccf0a7
 
 
 
24482a2
 
 
0ccf0a7
 
 
 
 
3a33624
a8cf587
0ccf0a7
3a33624
a8cf587
 
 
0ccf0a7
 
a8cf587
3a33624
a8cf587
e6e3a8d
a8cf587
e6e3a8d
0ccf0a7
 
a8cf587
3a33624
a8cf587
e6e3a8d
a8cf587
 
 
 
 
 
0ccf0a7
 
 
 
 
 
 
 
 
a8cf587
 
 
24482a2
a8cf587
 
0ccf0a7
a8cf587
 
 
 
 
 
0ccf0a7
 
a8cf587
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a33624
24482a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a33624
 
 
 
 
24482a2
 
 
3a33624
 
24482a2
 
 
 
e6e3a8d
3a33624
 
 
 
 
 
24482a2
3a33624
 
 
 
 
 
 
 
0ccf0a7
2c0dc15
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
import gradio as gr
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.patches import Rectangle, FancyBboxPatch, Circle, Polygon, Wedge, Path, PathPatch
from matplotlib.collections import PatchCollection
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import json
from datetime import datetime
import io
import base64
import tempfile

class AdvancedGridOptimizer:
    def __init__(self):
        # Conventional lot widths and their typical depths
        self.conventional_lot_specifications = {
            8.5: {"depths": [21, 25, 28], "type": "SLHC", "squares": "11-16"},
            10.5: {"depths": [21, 25, 28, 32, 35], "type": "SLHC", "squares": "13-21.5"},
            12.5: {"depths": [21, 25, 28, 30, 32], "type": "Standard", "squares": "16-24"},
            14.0: {"depths": [21, 25, 28, 30, 32, 34], "type": "Standard", "squares": "17-28"},
            16.0: {"depths": [28, 30, 32, 34, 36, 40], "type": "Premium", "squares": "24-38"},
            18.0: {"depths": [32, 34, 36], "type": "Premium", "squares": "32-39"},
            # Traditional corner lots
            11.0: {"depths": [21, 25], "type": "Corner-SLHC", "squares": "13-17"},
            13.3: {"depths": [25, 28], "type": "Corner-Standard", "squares": "18-22"},
            14.8: {"depths": [28, 30], "type": "Corner-Standard", "squares": "22-26"},
            16.8: {"depths": [30, 32], "type": "Corner-Premium", "squares": "26-32"}
        }
        
        # Medium Density lot specifications
        self.md_rear_loaded_specifications = {
            4.5: {"depths": [19, 25, 28], "type": "MD-Rear Load", "squares": "85.5-126", "build": "2/2/1"},
            6.0: {"depths": [19, 25, 28], "type": "MD-Rear Load", "squares": "114-168", "build": "3/2/2"},
            7.5: {"depths": [25, 28], "type": "MD-Rear Load", "squares": "187.5-210", "build": "3-4/2/2"}
        }
        
        self.md_front_loaded_specifications = {
            7.0: {"depths": [21], "type": "MD-Front Load", "squares": "147", "build": "3/2/1"},
            8.0: {"depths": [21], "type": "MD-Front Load", "squares": "168", "build": "3-4/2/2"},
            8.5: {"depths": [16], "type": "MD-Front Load", "squares": "136", "build": "3/2/1"},
            10.5: {"depths": [16], "type": "MD-Front Load", "squares": "168", "build": "3-4/2/2"}
        }
        
        # Set initial lot specifications to conventional
        self.lot_specifications = self.conventional_lot_specifications
        
        self.slhc_widths = [8.5, 10.5]
        self.standard_widths = [12.5, 14.0]
        self.premium_widths = [16.0, 18.0]
        self.corner_specific = [11.0, 13.3, 14.8, 16.8]
        
        # Medium density categories
        self.md_rear_widths = [4.5, 6.0, 7.5]
        self.md_front_widths = [7.0, 8.0, 8.5, 10.5]
        
        # Define corner_widths as all widths suitable for corners
        self.corner_widths = self.corner_specific + [14.0, 16.0, 18.0]
        
        # Enhanced color palette with RPM brand colors
        self.color_schemes = {
            'rpm_primary': {
                # Conventional colors
                8.5: '#802B2B',    # Burgundy for SLHC
                10.5: '#AB3838',   # Burgundy 75%
                12.5: '#216767',   # Teal
                14.0: '#2E3E2F',   # RPM Green (hero color)
                16.0: '#415B6E',   # Blue
                18.0: '#FF8E3C',   # Yellow
                11.0: '#4F8585',   # Teal 75%
                13.3: '#545D51',   # RPM Green 75%
                14.8: '#697687',   # Blue 75%
                16.8: '#FFCF6D',   # Yellow 75%
                # Medium Density colors
                4.5: '#6B4C8A',    # Purple for MD
                6.0: '#8A6BB3',    # Purple 75%
                7.5: '#9F85C7',    # Purple 50%
                7.0: '#4A7C7E',    # Teal-Blue for MD Front
                8.0: '#5A9A9C'     # Teal-Blue 75%
            },
            'rpm_contrast': {
                # Conventional colors
                8.5: '#D69C9C',    # Burgundy 50%
                10.5: '#E2C1B7',   # Burgundy 25%
                12.5: '#95B5B5',   # Teal 50%
                14.0: '#80857B',   # RPM Green 50%
                16.0: '#99AFC9',   # Blue 50%
                18.0: '#FFDF9D',   # Yellow 50%
                11.0: '#D6E3E3',   # Teal 25%
                13.3: '#B6B8B2',   # RPM Green 25%
                14.8: '#CCD7E4',   # Blue 25%
                16.8: '#FFEFCE',   # Yellow 25%
                # Medium Density colors
                4.5: '#B5A6C5',    # Purple 50%
                6.0: '#C7BDD6',    # Purple 25%
                7.5: '#DDD6E8',    # Purple 15%
                7.0: '#8FB8BA',    # Teal-Blue 50%
                8.0: '#B3D0D2'     # Teal-Blue 25%
            },
            'rpm_monochrome': {
                # All widths use grayscale
                8.5: '#2E3E2F',    # RPM Green 100%
                10.5: '#545D51',   # RPM Green 75%
                12.5: '#80857B',   # RPM Green 50%
                14.0: '#B6B8B2',   # RPM Green 25%
                16.0: '#636466',   # Black 75%
                18.0: '#939598',   # Black 50%
                11.0: '#D1D3D4',   # Black 25%
                13.3: '#216767',   # Teal (accent)
                14.8: '#415B6E',   # Blue (accent)
                16.8: '#FF8E3C',   # Yellow (accent)
                # Medium Density
                4.5: '#4A4B4D',    # Dark gray
                6.0: '#6B6C6E',    # Medium gray
                7.5: '#8C8D8F',    # Light gray
                7.0: '#5C5D5F',    # Gray
                8.0: '#7D7E80'     # Light gray
            }
        }
        
        self.current_scheme = 'rpm_primary'
        self.current_solution = None  # Store current AI solution
        self.development_mode = 'conventional'  # conventional or medium_density
        self.md_load_type = 'front'  # front or rear
    
    def set_development_mode(self, mode, load_type=None):
        """Set the development mode and update lot specifications"""
        self.development_mode = mode
        if mode == 'medium_density':
            if load_type == 'rear':
                self.lot_specifications = self.md_rear_loaded_specifications
                self.md_load_type = 'rear'
            else:
                self.lot_specifications = self.md_front_loaded_specifications
                self.md_load_type = 'front'
        else:
            self.lot_specifications = self.conventional_lot_specifications
            self.md_load_type = None
    
    def create_enhanced_visualization(self, solution, stage_width, stage_depth=32, title="Premium Grid Layout", show_variance=None):
        """Create a clean 2D visualization with corner splays and optional laneway"""
        # Adjust figure size for laneway if needed
        fig_height = 14 if self.md_load_type == 'rear' else 12
        fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(18, fig_height), gridspec_kw={'height_ratios': [3, 1]}, 
                                      facecolor='#2E3E2F')
        
        # Main visualization
        colors = self.color_schemes[self.current_scheme]
        
        x_pos = 0
        for i, (width, lot_type) in enumerate(solution):
            # Get base color
            if width in colors:
                base_color = colors[width]
            else:
                # Use MD colors if in MD mode
                if self.development_mode == 'medium_density':
                    if width <= 6.0:
                        base_color = colors.get(4.5, '#6B4C8A')
                    else:
                        base_color = colors.get(7.0, '#4A7C7E')
                else:
                    closest_width = min(colors.keys(), key=lambda x: abs(x - width))
                    base_color = colors[closest_width]
            # Get base color
        ax1.set_xlim(-5, stage_width + 5)
        # Adjust y-limits for rear laneway
        if self.md_load_type == 'rear':
            ax1.set_ylim(-10, 60)  # Extended for laneway
        else:
            ax1.set_ylim(-10, 50)
        ax1.set_facecolor('#2E3E2F')
        
        # Add title with variance if provided
        if show_variance is not None:
            variance_color = '#216767' if abs(show_variance) < 0.001 else '#802B2B'
            mode_text = "MD " if self.development_mode == 'medium_density' else ""
            load_text = f"({self.md_load_type.title()} Loaded) " if self.md_load_type else ""
            title_text = f"{mode_text}{load_text}{title}\nGrid Variance: {show_variance:+.1f}m"
            ax1.set_title(title_text, fontsize=28, fontweight='bold', pad=25, color='white')
        else:
            ax1.set_title(title, fontsize=28, fontweight='bold', pad=25, color='white')
        
        # Add subtle gradient background
        gradient = np.linspace(0.3, 0.1, 100).reshape(1, -1)
        y_max = 60 if self.md_load_type == 'rear' else 50
        ax1.imshow(gradient, extent=[-5, stage_width + 5, -10, y_max], aspect='auto', 
                   cmap='Greys', alpha=0.3, zorder=0)
        
        # Add street with label
        street = Rectangle((-5, -8), stage_width + 10, 12, 
                          facecolor='#000000', alpha=0.8, zorder=1,
                          edgecolor='#636466', linewidth=2)
        ax1.add_patch(street)
        ax1.text(stage_width/2, -2, 'STREET', ha='center', va='center', 
                fontsize=20, color='white', fontweight='bold')
        
        # Draw lots with corner splays
        splay_size = 3  # 3m corner splay
        
        # Get appropriate depth for current mode
        if self.development_mode == 'medium_density':
            # Use first available depth for MD lots
            lot_height = 28  # Default
            for width, _ in solution:
                if width in self.lot_specifications:
                    lot_height = self.lot_specifications[width]['depths'][0]
                    break
        else:
            lot_height = 28  # Standard height for conventional
        
        # Add rear laneway if rear loaded MD
        if self.md_load_type == 'rear':
            laneway_y = 8 + lot_height
            laneway = Rectangle((-5, laneway_y), stage_width + 10, 7, 
                              facecolor='#3A3A3A', alpha=0.9, zorder=1,
                              edgecolor='#FFCF6D', linewidth=2, linestyle='--')
            ax1.add_patch(laneway)
            ax1.text(stage_width/2, laneway_y + 3.5, 'REAR LANEWAY (7m)', 
                    ha='center', va='center', fontsize=16, color='#FFCF6D', 
                    fontweight='bold', alpha=0.9)
        
        for i, (width, lot_type) in enumerate(solution):
            # Get base color
            if width in colors:
                base_color = colors[width]
            else:
                # Use MD colors if in MD mode
                if self.development_mode == 'medium_density':
                    if width <= 6.0:
                        base_color = colors.get(4.5, '#6B4C8A')
                    else:
                        base_color = colors.get(7.0, '#4A7C7E')
                else:
                    closest_width = min(colors.keys(), key=lambda x: abs(x - width))
                    base_color = colors[closest_width]
            
            # Check position
            is_corner = (i == 0 or i == len(solution) - 1)
            
            # Consistent styling for visual alignment
            face_color = base_color
            edge_color = 'white'
            linewidth = 4.0 if is_corner else 3.0
            
            # Create lot shape with appropriate height
            if is_corner:
                # Corner lot with splay for both conventional and MD
                if i == 0:  # First corner
                    vertices = [
                        (x_pos + splay_size, 8),  # Start after splay
                        (x_pos + width, 8),
                        (x_pos + width, 8 + lot_height),
                        (x_pos, 8 + lot_height),  # Straight rear
                        (x_pos, 8 + splay_size)   # Splay corner
                    ]
                else:  # Last corner
                    vertices = [
                        (x_pos, 8),
                        (x_pos + width - splay_size, 8),
                        (x_pos + width, 8 + splay_size),  # Splay corner
                        (x_pos + width, 8 + lot_height),
                        (x_pos, 8 + lot_height)
                    ]
                
                # Create polygon path
                codes = [Path.MOVETO] + [Path.LINETO] * (len(vertices) - 1) + [Path.CLOSEPOLY]
                vertices.append(vertices[0])  # Close the path
                path = Path(vertices, codes)
                lot = PathPatch(path, facecolor=face_color, edgecolor=edge_color, 
                               linewidth=linewidth, zorder=3)
                ax1.add_patch(lot)
                
                # Add splay line
                if i == 0:
                    ax1.plot([x_pos, x_pos + splay_size], [8 + splay_size, 8], 
                            'white', linewidth=2, alpha=0.8)
                else:
                    ax1.plot([x_pos + width - splay_size, x_pos + width], 
                            [8, 8 + splay_size], 'white', linewidth=2, alpha=0.8)
            else:
                # Regular lot
                lot = FancyBboxPatch((x_pos, 8), width, lot_height,
                                    boxstyle="round,pad=0.1",
                                    facecolor=face_color, 
                                    edgecolor=edge_color,
                                    linewidth=linewidth,
                                    zorder=3)
                ax1.add_patch(lot)
            
            # Add subtle glow
            glow = FancyBboxPatch((x_pos - 0.2, 7.8), width + 0.4, lot_height + 0.4,
                                 boxstyle="round,pad=0.15",
                                 facecolor='none',
                                 edgecolor=face_color,
                                 linewidth=1,
                                 alpha=0.5,
                                 zorder=2)
            ax1.add_patch(glow)
            
            # Add lot information (positioned inside the lot)
            lot_center_y = 8 + lot_height / 2  # Center of the lot
            
            # Just show width in center (like conventional)
            ax1.text(x_pos + width/2, lot_center_y, f'{width:.1f}m', 
                    ha='center', va='center', fontsize=16, fontweight='bold', color='white')
            
            # Only show CORNER label for corner lots - positioned lower
            if is_corner:
                ax1.text(x_pos + width/2, 8 + lot_height/4, "CORNER",
                        ha='center', va='center', fontsize=12, 
                        bbox=dict(boxstyle="round,pad=0.3", facecolor='#545D51', 
                                 edgecolor='white', alpha=0.9, linewidth=1.5), color='white')
            
            # Dimension lines - make more visible
            dim_y = 8 + lot_height + 2  # Just above the lot
            ax1.plot([x_pos, x_pos + width], [dim_y, dim_y], 'w-', linewidth=1.5, alpha=0.6)
            ax1.plot([x_pos, x_pos], [dim_y - 1, dim_y + 1], 'w-', linewidth=1.5, alpha=0.6)
            ax1.plot([x_pos + width, x_pos + width], [dim_y - 1, dim_y + 1], 'w-', linewidth=1.5, alpha=0.6)
            
            # Add garage indicators for rear loaded
            if self.md_load_type == 'rear':
                # Small garage icon at rear
                garage_y = 8 + lot_height - 6
                garage = Rectangle((x_pos + width/2 - 1.5, garage_y), 3, 5,
                                 facecolor='#636466', edgecolor='white', 
                                 linewidth=1, alpha=0.8, zorder=4)
                ax1.add_patch(garage)
            
            x_pos += width
        
        # Add rear alignment line across all lots
        rear_y = 8 + lot_height
        if self.md_load_type != 'rear':  # Don't show if laneway present
            ax1.plot([0, stage_width], [rear_y, rear_y], 
                    '#216767', linewidth=2, alpha=0.8, linestyle='-')
            ax1.text(stage_width/2, rear_y + 1, 'REAR ALIGNMENT LINE', 
                    ha='center', va='bottom', fontsize=12, color='#216767', alpha=0.8,
                    bbox=dict(boxstyle="round,pad=0.3", facecolor='#2E3E2F', 
                             edgecolor='#216767', alpha=0.8))
        
        # Add stage dimensions
        arrow_props = dict(arrowstyle='<->', color='white', lw=3)
        ax1.annotate('', xy=(0, -6), xytext=(stage_width, -6), arrowprops=arrow_props)
        ax1.text(stage_width/2, -7, f'{stage_width}m Γ— {stage_depth}m', 
                ha='center', va='top', fontsize=16, fontweight='bold', color='white')
        
        # Style axes
        ax1.set_xticks([])
        ax1.set_yticks([])
        for spine in ax1.spines.values():
            spine.set_visible(False)
        
        # Metrics panel
        ax2.axis('off')
        ax2.set_facecolor('#2E3E2F')
        
        # Calculate metrics with diversity score
        total_lots = len(solution)
        unique_widths = len(set(w for w, _ in solution))
        diversity_score = unique_widths / len(set(self.lot_specifications.keys()))
        
        if self.development_mode == 'conventional':
            slhc_count = sum(1 for w, _ in solution if w <= 10.5)
            standard_count = sum(1 for w, _ in solution if 10.5 < w <= 14)
            premium_count = sum(1 for w, _ in solution if w > 14)
            
            # SLHC pairs
            slhc_pairs = 0
            for i in range(len(solution) - 1):
                if solution[i][0] <= 10.5 and solution[i+1][0] <= 10.5:
                    slhc_pairs += 1
            
            # Set MD-specific variables to avoid reference errors
            narrow_count = slhc_count
            wide_count = premium_count
        else:
            # MD metrics
            narrow_count = sum(1 for w, _ in solution if w <= 6.0)
            standard_count = sum(1 for w, _ in solution if 6.0 < w <= 8.0)
            wide_count = sum(1 for w, _ in solution if w > 8.0)
            slhc_pairs = 0  # Not applicable for MD
            slhc_count = narrow_count
            premium_count = wide_count
        
        # Calculate actual total width and variance
        total_width = sum(w for w, _ in solution)
        variance = total_width - stage_width
        efficiency = "100%" if abs(variance) < 0.001 else f"{(total_width/stage_width)*100:.1f}%"
        
        # Calculate yield
        if self.development_mode == 'medium_density':
            # Assume potential for duplex on lots β‰₯ 7m
            potential_dwellings = sum(2 if w >= 7.0 else 1 for w, _ in solution)
            yield_text = f"🏘️ Potential Dwellings: {potential_dwellings}"
        else:
            yield_text = f"πŸ’° Revenue: ${total_lots * 0.5:.1f}M - ${total_lots * 1.2:.1f}M"
        
        metrics_lines = [
            f"πŸ“Š TOTAL LOTS: {total_lots}",
            f"πŸ“ LAND EFFICIENCY: {efficiency}",
            f"🎯 DIVERSITY: {diversity_score:.0%} ({unique_widths} types)",
            f"πŸ“ GRID VARIANCE: {variance:+.2f}m",
            "",
            f"{'Narrow (≀6m)' if self.development_mode == 'medium_density' else 'SLHC (≀10.5m)'}: {narrow_count} lots",
            f"{'Standard (6-8m)' if self.development_mode == 'medium_density' else 'Standard (11-14m)'}: {standard_count} lots",
            f"{'Wide (>8m)' if self.development_mode == 'medium_density' else 'Premium (>14m)'}: {wide_count} lots",
            "",
            f"{'πŸš— Access: ' + ('Rear Laneway' if self.md_load_type == 'rear' else 'Front Loaded') if self.development_mode == 'medium_density' else f'πŸš— SLHC Pairs: {slhc_pairs}'}",
            yield_text
        ]
        
        col1_text = '\n'.join(metrics_lines[:5])
        col2_text = '\n'.join(metrics_lines[5:])
        
        ax2.text(0.05, 0.5, col1_text, transform=ax2.transAxes, 
                fontsize=14, verticalalignment='center', fontweight='bold',
                color='white',
                bbox=dict(boxstyle="round,pad=0.5", facecolor='#545D51', 
                         edgecolor='#216767', alpha=0.8))
        
        ax2.text(0.55, 0.5, col2_text, transform=ax2.transAxes, 
                fontsize=14, verticalalignment='center', fontweight='bold',
                color='white',
                bbox=dict(boxstyle="round,pad=0.5", facecolor='#545D51', 
                         edgecolor='#216767', alpha=0.8))
        
        plt.tight_layout()
        return fig
    
    def parse_manual_adjustments(self, adjustment_text):
        """Parse manual adjustment input into a list of widths"""
        try:
            if not adjustment_text:
                return []
            
            # Remove any whitespace and split by commas or spaces
            adjustment_text = adjustment_text.strip()
            
            # Try parsing as comma-separated values
            if ',' in adjustment_text:
                widths = [float(w.strip()) for w in adjustment_text.split(',') if w.strip()]
            # Try parsing as space-separated values
            elif ' ' in adjustment_text:
                widths = [float(w.strip()) for w in adjustment_text.split() if w.strip()]
            # Try parsing as newline-separated values
            elif '\n' in adjustment_text:
                widths = [float(w.strip()) for w in adjustment_text.split('\n') if w.strip()]
            else:
                # Single value
                widths = [float(adjustment_text)]
            
            return widths
        except Exception as e:
            print(f"Error parsing manual adjustments: {e}")
            return []
    
    def validate_manual_solution(self, widths, stage_width):
        """Validate and provide feedback on manual solution"""
        if not widths:
            return None, "No widths provided"
        
        total_width = sum(widths)
        variance = total_width - stage_width
        
        # Create solution format
        solution = [(w, 'corner' if i in [0, len(widths)-1] else 'standard') 
                    for i, w in enumerate(widths)]
        
        # Provide feedback
        if abs(variance) < 0.001:
            feedback = "βœ… Perfect fit! Grid is exactly aligned."
        elif variance > 0:
            feedback = f"⚠️ Grid is {variance:.2f}m too wide. Remove {variance:.2f}m total width."
        else:
            feedback = f"⚠️ Grid is {-variance:.2f}m too narrow. Add {-variance:.2f}m total width."
        
        # Add suggestions if not perfect
        min_width = 4.5 if self.development_mode == 'medium_density' else 8.5
        
        if abs(variance) > 0.001:
            if variance > 0:
                # Suggest which lots could be reduced
                suggestions = []
                for i, w in enumerate(widths):
                    if w - variance >= min_width:  # Minimum viable width
                        suggestions.append(f"L{i+1}: reduce from {w:.1f}m to {w-variance:.1f}m")
                if suggestions:
                    feedback += f"\n\nSuggestions:\n" + "\n".join(suggestions[:3])
            else:
                # Suggest which lots could be increased
                suggestions = []
                add_per_lot = -variance / len(widths)
                feedback += f"\n\nSuggestion: Add {add_per_lot:.2f}m to each lot"
        
        return solution, feedback
    
    def solution_to_string(self, solution):
        """Convert solution to string format for manual editing"""
        if not solution:
            return ""
        return ", ".join([f"{w:.1f}" for w, _ in solution])
    
    def find_optimal_custom_corners(self, stage_width, internal_widths, base_corner_width, tolerance=0.5):
        """Find optimal corner widths that can vary slightly from base width"""
        best_solution = None
        best_fitness = -float('inf')
        
        # Ensure corners are at least as wide as smallest internal lot
        if self.development_mode == 'medium_density':
            min_internal = min(internal_widths) if internal_widths else 4.5
        else:
            min_internal = min(internal_widths) if internal_widths else 8.5
        min_corner_width = max(base_corner_width - tolerance, min_internal)
        
        # Try variations of corner widths within tolerance
        variations = np.arange(min_corner_width, 
                              base_corner_width + tolerance + 0.1, 
                              0.1)
        
        for corner1 in variations:
            for corner2 in variations:
                # Calculate internal space
                internal_width = stage_width - corner1 - corner2
                if internal_width <= 0:
                    continue
                
                # Try to fill internal space exactly
                internal_solution = self.find_exact_solution_with_diversity(internal_width, internal_widths)
                
                if internal_solution:
                    # Verify no internal lot is wider than corners
                    max_internal = max(internal_solution) if internal_solution else 0
                    if max_internal > min(corner1, corner2):
                        continue
                    
                    # Build complete solution
                    solution = [(round(corner1, 1), 'corner')]
                    solution.extend([(w, 'standard') for w in internal_solution])
                    solution.append((round(corner2, 1), 'corner'))
                    
                    # Evaluate (prefer balanced corners and diversity)
                    fitness = self.evaluate_solution_with_diversity(solution, stage_width)
                    
                    if fitness > best_fitness:
                        best_fitness = fitness
                        best_solution = solution
        
        return best_solution
    
    def optimize_with_flexible_corners(self, stage_width, enabled_widths, allow_custom_corners=True):
        """Enhanced optimization allowing flexible corner sizes with diversity"""
        
        # Separate widths by type
        standard_internal = [w for w in enabled_widths if w not in self.corner_specific]
        
        best_solution = None
        best_fitness = -float('inf')
        
        # Strategy 1: Try exact widths first with diversity
        solution = self.optimize_with_corners_diverse(stage_width, enabled_widths, None)
        if solution:
            fitness = self.evaluate_solution_with_diversity(solution, stage_width)
            if fitness > best_fitness:
                best_fitness = fitness
                best_solution = solution
        
        # Strategy 2: Try flexible corners if enabled
        if allow_custom_corners and standard_internal:
            # Use appropriate corner bases for each mode
            if self.development_mode == 'medium_density':
                # For MD, use the largest available widths as corner bases
                corner_bases = sorted(enabled_widths, reverse=True)[:4]
            else:
                # For conventional, use traditional corner widths
                corner_bases = [11.0, 13.3, 14.8, 16.8, 14.0, 16.0]
            
            for base_width in corner_bases:
                if any(abs(w - base_width) < 2 for w in enabled_widths):
                    custom_solution = self.find_optimal_custom_corners(
                        stage_width, standard_internal, base_width, tolerance=0.5
                    )
                    if custom_solution:
                        fitness = self.evaluate_solution_with_diversity(custom_solution, stage_width)
                        if fitness > best_fitness:
                            best_fitness = fitness
                            best_solution = custom_solution
        
        return best_solution
    
    def optimize_with_corners_diverse(self, stage_width, enabled_widths, manual_allocation=None):
        """Find lot arrangement with emphasis on diversity and proper corner sizing"""
        
        # Separate widths by size
        all_widths = sorted(enabled_widths)
        min_internal_width = min(all_widths) if all_widths else 4.5
        
        # Corner lots must be at least as wide as smallest internal lot
        corner_options = [w for w in enabled_widths if w >= max(11.0 if self.development_mode == 'conventional' else min_internal_width, min_internal_width)]
        
        best_solution = None
        best_fitness = -float('inf')
        
        # Try different corner combinations
        for corner1 in corner_options:
            for corner2 in corner_options:
                if abs(corner1 - corner2) > 3.0:  # Skip very unbalanced
                    continue
                
                # Calculate internal space
                internal_width = stage_width - corner1 - corner2
                if internal_width <= 0:
                    continue
                
                # Find diverse internal solutions
                internal_solutions = self.find_diverse_combinations(
                    internal_width, all_widths, max_solutions=20
                )
                
                for internal_widths in internal_solutions:
                    # Verify no internal lot is wider than corners
                    max_internal = max(internal_widths) if internal_widths else 0
                    if max_internal > min(corner1, corner2):
                        continue  # Skip if internal lots are wider than corners
                    
                    # Build complete solution
                    solution = [(corner1, 'corner')]
                    solution.extend([(w, 'standard') for w in internal_widths])
                    solution.append((corner2, 'corner'))
                    
                    # Optimize arrangement
                    optimized = self.optimize_lot_grouping(solution)
                    fitness = self.evaluate_solution_with_diversity(optimized, stage_width)
                    
                    if fitness > best_fitness:
                        best_fitness = fitness
                        best_solution = optimized
        
        # If no good solution, try without strict corner rules but maintain size hierarchy
        if not best_solution:
            all_solutions = []
            self.find_all_combinations_recursive(stage_width, sorted(enabled_widths), 
                                               [], all_solutions, 20)
            
            for widths in all_solutions[:50]:
                # Ensure corners are among the largest lots
                sorted_widths = sorted(widths)
                if len(sorted_widths) >= 2:
                    # Put two largest widths at corners
                    solution = [(sorted_widths[-1], 'corner')]  # Largest
                    solution.extend([(w, 'standard') for w in sorted_widths[:-2]])
                    solution.append((sorted_widths[-2], 'corner'))  # Second largest
                else:
                    solution = [(w, 'standard') for w in widths]
                
                optimized = self.optimize_lot_grouping(solution)
                fitness = self.evaluate_solution_with_diversity(optimized, stage_width)
                
                if fitness > best_fitness:
                    best_fitness = fitness
                    best_solution = optimized
        
        return best_solution
    
    def optimize_lot_grouping(self, lots):
        """Optimize lot arrangement based on development mode"""
        if self.development_mode == 'medium_density':
            return self.optimize_md_grouping(lots)
        else:
            return self.optimize_slhc_grouping(lots)
    
    def optimize_md_grouping(self, lots):
        """Optimize lot arrangement for medium density"""
        if not lots or len(lots) <= 1:
            return lots
        
        # Separate lots by width
        narrow_lots = []  # 4.5-6m
        medium_lots = []  # 7-8m
        wide_lots = []    # >8m
        
        for width, lot_type in lots:
            if width <= 6.0:
                narrow_lots.append((width, lot_type))
            elif width <= 8.0:
                medium_lots.append((width, lot_type))
            else:
                wide_lots.append((width, lot_type))
        
        # Build optimized layout
        optimized = []
        
        # For rear loaded, group similar widths for efficient laneway access
        if self.md_load_type == 'rear':
            # Group narrow lots together
            optimized.extend(narrow_lots)
            optimized.extend(medium_lots)
            optimized.extend(wide_lots)
        else:
            # For front loaded, alternate sizes for variety
            while narrow_lots or medium_lots or wide_lots:
                if wide_lots:
                    optimized.append(wide_lots.pop(0))
                if narrow_lots:
                    optimized.append(narrow_lots.pop(0))
                if medium_lots:
                    optimized.append(medium_lots.pop(0))
        
        return optimized
    
    def find_diverse_combinations(self, target_width, available_widths, max_solutions=20):
        """Find combinations that maximize diversity"""
        all_solutions = []
        self.find_all_combinations_recursive(target_width, available_widths, 
                                           [], all_solutions, 20)
        
        # Sort by diversity (number of unique widths)
        diverse_solutions = []
        for sol in all_solutions:
            unique_count = len(set(sol))
            diverse_solutions.append((unique_count, sol))
        
        # Sort by diversity, then by total lots
        diverse_solutions.sort(key=lambda x: (x[0], len(x[1])), reverse=True)
        
        # Return the most diverse solutions
        return [sol[1] for sol in diverse_solutions[:max_solutions]]
    
    def find_exact_solution_with_diversity(self, target_width, enabled_widths, max_depth=20):
        """Find exact solution prioritizing diversity"""
        
        # Try to use multiple different widths
        solutions = []
        
        # Dynamic programming with diversity tracking
        dp = {}
        dp[0] = ([], set())  # (solution, unique_widths)
        
        for current_target in range(1, int(target_width) + 1):
            best_diversity = -1
            best_solution = None
            
            for width in enabled_widths:
                if width <= current_target and (current_target - width) in dp:
                    prev_solution, prev_unique = dp[current_target - width]
                    if len(prev_solution) < max_depth:
                        new_solution = prev_solution + [width]
                        new_unique = prev_unique.copy()
                        new_unique.add(width)
                        
                        diversity = len(new_unique)
                        if diversity > best_diversity:
                            best_diversity = diversity
                            best_solution = (new_solution, new_unique)
            
            if best_solution:
                dp[current_target] = best_solution
        
        if target_width in dp:
            return dp[target_width][0]
        
        # Fallback to regular solution
        return self.find_exact_solution(target_width, enabled_widths, max_depth)
    
    def find_exact_solution(self, target_width, enabled_widths, max_depth=20):
        """Find exact combination that sums to target_width"""
        
        # Quick check for simple solutions
        for width in enabled_widths:
            if abs(target_width % width) < 0.001:
                count = int(target_width / width)
                if count <= max_depth:
                    return [width] * count
        
        # Dynamic programming solution
        dp = {}
        dp[0] = []
        
        for current_target in range(1, int(target_width) + 1):
            for width in enabled_widths:
                if width <= current_target and (current_target - width) in dp:
                    prev_solution = dp[current_target - width]
                    if len(prev_solution) < max_depth:
                        dp[current_target] = prev_solution + [width]
        
        if target_width in dp:
            return dp[target_width]
        
        # Try exhaustive search
        all_solutions = []
        self.find_all_combinations_recursive(target_width, sorted(enabled_widths), 
                                           [], all_solutions, max_depth)
        
        if all_solutions:
            # Return shortest solution
            return min(all_solutions, key=len)
        
        return None
    
    def find_all_combinations_recursive(self, remaining, widths, current, all_solutions, max_depth):
        """Recursively find all exact combinations"""
        if abs(remaining) < 0.001:
            all_solutions.append(current[:])
            return
        
        if remaining < 0 or len(current) >= max_depth or len(all_solutions) >= 100:
            return
        
        for i, width in enumerate(widths):
            if width <= remaining + 0.001:
                current.append(width)
                self.find_all_combinations_recursive(remaining - width, widths[i:], 
                                                   current, all_solutions, max_depth)
                current.pop()
    
    def optimize_slhc_grouping(self, lots):
        """Optimize lot arrangement with sophisticated rules for conventional"""
        if not lots or len(lots) <= 1:
            return lots
        
        # Separate lots by type
        corner_specific = []
        slhc_lots = []
        standard_lots = []
        custom_lots = []
        
        for width, lot_type in lots:
            if width in self.corner_specific:
                corner_specific.append((width, lot_type))
            elif width <= 10.5:
                slhc_lots.append((width, lot_type))
            elif width in self.standard_widths + self.premium_widths:
                standard_lots.append((width, lot_type))
            else:
                # Custom width
                if width > 10.8 and width < 17:
                    custom_lots.append((width, lot_type))
                else:
                    standard_lots.append((width, lot_type))
        
        # Further separate SLHC by width
        slhc_8_5 = [(w, t) for w, t in slhc_lots if abs(w - 8.5) < 0.1]
        slhc_10_5 = [(w, t) for w, t in slhc_lots if abs(w - 10.5) < 0.1]
        
        # Determine corner placement
        corner_solution = self._determine_best_corners(corner_specific + custom_lots, standard_lots)
        
        # Build optimized layout
        optimized = []
        
        # Place first corner
        if corner_solution and corner_solution[0]:
            optimized.append((corner_solution[0][0], 'corner'))
            # Remove from appropriate list
            for lst in [corner_specific, custom_lots, standard_lots]:
                if corner_solution[0] in lst:
                    lst.remove(corner_solution[0])
                    break
        
        # Add SLHC groups optimally
        optimized.extend(self._arrange_slhc_optimally(slhc_8_5, slhc_10_5))
        
        # Add remaining lots
        optimized.extend(standard_lots)
        optimized.extend(custom_lots)
        optimized.extend(corner_specific)
        
        # Place second corner
        if corner_solution and len(corner_solution) > 1 and corner_solution[1]:
            optimized.append((corner_solution[1][0], 'corner'))
        
        return optimized
    
    def _determine_best_corners(self, corner_suitable, standard_lots):
        """Determine the best corner placement strategy"""
        all_suitable = corner_suitable + [(w, t) for w, t in standard_lots if w >= 12.5]
        
        if len(all_suitable) < 2:
            return None
        
        # Find best matching pair
        best_pair = None
        min_diff = float('inf')
        
        for i in range(len(all_suitable)):
            for j in range(i + 1, len(all_suitable)):
                diff = abs(all_suitable[i][0] - all_suitable[j][0])
                if diff < min_diff:
                    min_diff = diff
                    best_pair = (all_suitable[i], all_suitable[j])
        
        return best_pair
    
    def _arrange_slhc_optimally(self, slhc_8_5, slhc_10_5):
        """Arrange SLHC lots for optimal garage adjacency"""
        arranged = []
        
        # Pair matching widths first
        while len(slhc_8_5) >= 2:
            arranged.extend(slhc_8_5[:2])
            slhc_8_5 = slhc_8_5[2:]
        
        while len(slhc_10_5) >= 2:
            arranged.extend(slhc_10_5[:2])
            slhc_10_5 = slhc_10_5[2:]
        
        # Mixed pairing
        while slhc_8_5 and slhc_10_5:
            arranged.append(slhc_8_5[0])
            arranged.append(slhc_10_5[0])
            slhc_8_5 = slhc_8_5[1:]
            slhc_10_5 = slhc_10_5[1:]
        
        # Add remaining
        arranged.extend(slhc_8_5)
        arranged.extend(slhc_10_5)
        
        return arranged
    
    def evaluate_solution_with_diversity(self, solution, stage_width):
        """Evaluate fitness with strong emphasis on diversity"""
        if not solution:
            return -float('inf')
        
        total_width = sum(w for w, _ in solution)
        waste = stage_width - total_width
        
        # Must have 100% usage
        if abs(waste) > 0.001:
            return -float('inf')
        
        lot_count = len(solution)
        
        # Calculate diversity metrics
        width_counts = {}
        for w, _ in solution:
            width_counts[w] = width_counts.get(w, 0) + 1
        
        unique_widths = len(width_counts)
        max_repetition = max(width_counts.values())
        diversity_ratio = unique_widths / lot_count if lot_count > 0 else 0
        
        # Base fitness
        fitness = lot_count * 1000
        
        # STRONG diversity bonus
        fitness += unique_widths * 2000  # Big bonus for each unique width
        fitness -= max_repetition * 500   # Penalty for too many of same width
        fitness += diversity_ratio * 3000 # Bonus for good diversity ratio
        
        # Corner evaluation - apply to both conventional and MD
        if len(solution) >= 2:
            first_width = solution[0][0]
            last_width = solution[-1][0]
            
            # Get max internal width
            internal_widths = [w for w, t in solution[1:-1]]
            max_internal = max(internal_widths) if internal_widths else 0
            
            # Penalty if corners are not wider than internals
            if first_width <= max_internal:
                fitness -= 2000
            if last_width <= max_internal:
                fitness -= 2000
            
            # Bonus for good corners (wider than internals)
            if first_width > max_internal:
                fitness += 1000
            if last_width > max_internal:
                fitness += 1000
            
            # Balance bonus
            corner_diff = abs(first_width - last_width)
            if corner_diff < 0.1:
                fitness += 1500  # Perfect match
            elif corner_diff <= 1.0:
                fitness += 1000  # Very good
            elif corner_diff <= 2.0:
                fitness += 500   # Good
            else:
                fitness -= 500   # Poor balance
        
        # Mode-specific bonuses
        if self.development_mode == 'conventional':
            # SLHC grouping bonus
            for i in range(len(solution) - 1):
                if solution[i][0] <= 10.5 and solution[i+1][0] <= 10.5:
                    fitness += 300  # Adjacent SLHC bonus
            
            # Penalize corner-specific widths used internally
            for i in range(1, len(solution) - 1):
                if solution[i][0] in self.corner_specific:
                    fitness -= 200
        else:
            # MD-specific bonuses
            if self.md_load_type == 'rear':
                # Bonus for grouping similar widths (efficient laneway access)
                for i in range(len(solution) - 1):
                    if abs(solution[i][0] - solution[i+1][0]) < 1.5:
                        fitness += 200
            
            # Bonus for potential duplex lots (β‰₯7m)
            duplex_count = sum(1 for w, _ in solution if w >= 7.0)
            fitness += duplex_count * 500
        
        return fitness
    
    def generate_report(self, solution, stage_width, stage_depth, manual_allocation=None):
        """Generate a professional report"""
        if not solution:
            return None
        
        # Check for custom widths
        custom_widths = []
        for width, _ in solution:
            if width not in self.lot_specifications:
                custom_widths.append(f"{width:.1f}m")
        
        # Calculate diversity
        unique_widths = len(set(w for w, _ in solution))
        width_counts = {}
        for w, _ in solution:
            width_counts[w] = width_counts.get(w, 0) + 1
        
        # Calculate variance
        total_width = sum(w for w, _ in solution)
        variance = total_width - stage_width
        
        # Mode-specific title
        mode_text = "MEDIUM DENSITY " if self.development_mode == 'medium_density' else ""
        load_text = f"({self.md_load_type.upper()} LOADED) " if self.md_load_type else ""
        
        report = f"""
# {mode_text}{load_text}SUBDIVISION OPTIMIZATION REPORT
## Project Analysis for {stage_width}m Γ— {stage_depth}m Stage

### EXECUTIVE SUMMARY
- **Development Type**: {self.development_mode.replace('_', ' ').title()}
- **Total Lots**: {len(solution)}
- **Unique Lot Types**: {unique_widths}
- **Land Efficiency**: {"100%" if abs(variance) < 0.001 else f"{(total_width/stage_width)*100:.1f}%"}
- **Grid Variance**: {variance:+.2f}m
- **Stage Dimensions**: {stage_width}m Γ— {stage_depth}m
- **Total Area**: {stage_width * stage_depth}mΒ²
{f"- **Custom Widths Used**: {', '.join(custom_widths)}" if custom_widths else ""}
"""
        
        # Add MD-specific info
        if self.development_mode == 'medium_density':
            potential_dwellings = sum(2 if w >= 7.0 else 1 for w, _ in solution)
            density = potential_dwellings / (stage_width * stage_depth / 10000)  # per hectare
            report += f"- **Potential Dwellings**: {potential_dwellings} ({density:.0f} dwellings/ha)\n"
            report += f"- **Access Type**: {'Rear Laneway (7m)' if self.md_load_type == 'rear' else 'Front Loaded'}\n"
        
        report += f"\n### LOT DIVERSITY ANALYSIS\n"
        
        # Sort by count to show distribution
        sorted_widths = sorted(width_counts.items(), key=lambda x: x[1], reverse=True)
        for width, count in sorted_widths:
            percentage = (count / len(solution)) * 100
            if width in self.lot_specifications:
                spec = self.lot_specifications[width]
                build_info = f" [{spec.get('build', 'N/A')}]" if 'build' in spec else ""
                report += f"- **{width:.1f}m** Γ— {count} ({percentage:.1f}%): {spec['type']}{build_info}\n"
            else:
                report += f"- **{width:.1f}m** Γ— {count} ({percentage:.1f}%): Custom Width\n"
        
        # Corner analysis
        if len(solution) >= 2:
            report += f"\n### CORNER ANALYSIS\n"
            report += f"- **Front Corner**: {solution[0][0]:.1f}m with 3m Γ— 3m splay\n"
            report += f"- **Rear Corner**: {solution[-1][0]:.1f}m with 3m Γ— 3m splay\n"
            report += f"- **Balance**: {abs(solution[0][0] - solution[-1][0]):.1f}m difference\n"
        
        report += f"\n### DESIGN FEATURES\n"
        report += f"- Corner splays provide safe sight lines at intersections\n"
        if self.development_mode == 'medium_density':
            if self.md_load_type == 'rear':
                report += f"- 7m rear laneway provides vehicle access and services\n"
                report += f"- Garages positioned at rear for better street presentation\n"
            else:
                report += f"- Front loaded design with integrated garages\n"
            report += f"- Compact lots maximize dwelling yield\n"
            report += f"- Potential for duplex/triplex on wider lots (β‰₯7m)\n"
        else:
            report += f"- All lots have identical rear alignment for visual consistency\n"
            report += f"- Diverse lot mix ensures varied streetscape\n"
            report += f"- SLHC lots grouped for efficient garbage collection\n"
        
        report += f"\n---\n*Report generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*"
        
        return report
    
    def darken_color(self, hex_color, factor=0.8):
        """Darken a hex color by a factor"""
        try:
            hex_color = hex_color.lstrip('#')
            rgb = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
            darker_rgb = tuple(int(c * factor) for c in rgb)
            return '#' + ''.join(f'{c:02x}' for c in darker_rgb)
        except:
            return hex_color

def create_advanced_app():
    optimizer = AdvancedGridOptimizer()
    
    def update_available_widths(development_mode, md_load_type):
        """Update the available width options based on development mode"""
        if development_mode == "Medium Density":
            if md_load_type == "Rear Loaded":
                # Rear loaded MD widths
                return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
            else:
                # Front loaded MD widths
                return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
        else:
            # Conventional widths
            return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
    
    def optimize_grid(
        stage_width,
        stage_depth,
        development_mode,
        md_load_type,
        # Conventional widths
        enable_8_5, enable_10_5, enable_12_5, enable_14, enable_16, enable_18,
        enable_corners, enable_11, enable_13_3, enable_14_8, enable_16_8,
        # MD rear widths
        enable_4_5, enable_6_0, enable_7_5,
        # MD front widths
        enable_7_0, enable_8_0, enable_md_8_5, enable_md_10_5,
        allow_custom_corners, color_scheme
    ):
        # Update optimizer mode
        if development_mode == "Medium Density":
            optimizer.set_development_mode('medium_density', 'rear' if md_load_type == "Rear Loaded" else 'front')
        else:
            optimizer.set_development_mode('conventional')
        
        # Update color scheme
        optimizer.current_scheme = color_scheme
        
        # Collect enabled widths based on mode
        enabled_widths = []
        
        if development_mode == "Conventional Land":
            if enable_8_5: enabled_widths.append(8.5)
            if enable_10_5: enabled_widths.append(10.5)
            if enable_12_5: enabled_widths.append(12.5)
            if enable_14: enabled_widths.append(14.0)
            if enable_16: enabled_widths.append(16.0)
            if enable_18: enabled_widths.append(18.0)
            
            if enable_corners:
                if enable_11: enabled_widths.append(11.0)
                if enable_13_3: enabled_widths.append(13.3)
                if enable_14_8: enabled_widths.append(14.8)
                if enable_16_8: enabled_widths.append(16.8)
        else:
            if md_load_type == "Rear Loaded":
                if enable_4_5: enabled_widths.append(4.5)
                if enable_6_0: enabled_widths.append(6.0)
                if enable_7_5: enabled_widths.append(7.5)
            else:
                if enable_7_0: enabled_widths.append(7.0)
                if enable_8_0: enabled_widths.append(8.0)
                if enable_md_8_5: enabled_widths.append(8.5)
                if enable_md_10_5: enabled_widths.append(10.5)
        
        if not enabled_widths:
            return None, None, pd.DataFrame(), "Please select at least one lot width!", "", ""
        
        # Run optimization with diversity focus
        optimized_solution = optimizer.optimize_with_flexible_corners(
            stage_width, enabled_widths, allow_custom_corners
        )
        
        # Store current solution for manual adjustment
        optimizer.current_solution = optimized_solution
        
        # Calculate variance for display
        if optimized_solution:
            total_width = sum(w for w, _ in optimized_solution)
            variance = total_width - stage_width
        else:
            variance = None
        
        # Verify solution
        if not optimized_solution or abs(sum(w for w, _ in optimized_solution) - stage_width) > 0.001:
            # Provide mode-specific suggestions
            if development_mode == "Medium Density":
                width_suggestions = "4.5m, 6m, 7.5m" if md_load_type == "Rear Loaded" else "7m, 8m, 8.5m, 10.5m"
                stage_suggestions = "54m, 72m, 90m"
            else:
                width_suggestions = "8.5m-18m plus corner widths"
                stage_suggestions = "84m, 105m, 126m"
            
            return None, pd.DataFrame(), f"""
### ❌ Cannot achieve 100% usage with selected widths

**Stage Width**: {stage_width}m  
**Mode**: {development_mode} {f'({md_load_type})' if development_mode == 'Medium Density' else ''}
**Available Widths**: {', '.join([f"{w}m" for w in sorted(enabled_widths)])}

**Try:**
1. Enable more lot types for flexibility
2. Enable "Custom Corners" option
3. Try common stage widths: {stage_suggestions}
4. Available widths: {width_suggestions}
""", "", ""
        
        # Create visualizations with variance indicator
        title = f"{'MD ' if development_mode == 'Medium Density' else ''}Grid Cut Optimization"
        fig_2d = optimizer.create_enhanced_visualization(
            optimized_solution, stage_width, stage_depth,
            title,
            show_variance=variance
        )
        
        # Create results table
        width_counts = {}
        for width, lot_type in optimized_solution:
            key = f"{width:.1f}m"
            if key in width_counts:
                width_counts[key]['count'] += 1
            else:
                # Handle both standard and custom widths
                if width in optimizer.lot_specifications:
                    spec = optimizer.lot_specifications[width]
                elif int(width) in optimizer.lot_specifications:
                    spec = optimizer.lot_specifications[int(width)]
                else:
                    # Custom width - find closest
                    closest = min(optimizer.lot_specifications.keys(), 
                                key=lambda x: abs(x - width))
                    spec = optimizer.lot_specifications[closest]
                    spec = {**spec, 'type': 'Custom', 'squares': 'Custom'}
                
                width_counts[key] = {
                    'count': 1,
                    'type': spec.get('type', 'Custom'),
                    'squares': spec.get('squares', 'N/A'),
                    'area': width * stage_depth,
                    'build': spec.get('build', 'N/A')
                }
        
        results_data = []
        for width, info in sorted(width_counts.items()):
            row_data = {
                'Lot Width': width,
                'Count': info['count'],
                'Type': info['type'],
                'Area Each': f"{info['area']:.0f}mΒ²",
                'Total Width': f"{float(width[:-1]) * info['count']:.1f}m",
                'Total Area': f"{info['area'] * info['count']:.0f}mΒ²"
            }
            if development_mode == "Medium Density":
                row_data['Build Type'] = info['build']
            results_data.append(row_data)
        
        results_df = pd.DataFrame(results_data)
        
        # Generate report
        report = optimizer.generate_report(optimized_solution, stage_width, stage_depth, None)
        
        # Create summary
        total_lots = len(optimized_solution)
        unique_widths = len(set(w for w, _ in optimized_solution))
        
        if development_mode == "Medium Density":
            # MD specific metrics
            potential_dwellings = sum(2 if w >= 7.0 else 1 for w, _ in optimized_solution)
            density = potential_dwellings / (stage_width * stage_depth / 10000)
            
            summary = f"""
**Stage**: {stage_width}m Γ— {stage_depth}m = {stage_width * stage_depth}mΒ²
**Development**: {development_mode} ({md_load_type})
**Total Lots**: {total_lots}
**Potential Dwellings**: {potential_dwellings} ({density:.0f}/ha)
**Unique Lot Types**: {unique_widths}
**Grid Variance**: {variance:+.2f}m {"βœ…" if abs(variance) < 0.001 else "⚠️"}
"""
        else:
            # Conventional metrics
            slhc_pairs = sum(1 for i in range(len(optimized_solution) - 1) 
                            if optimized_solution[i][0] <= 10.5 and optimized_solution[i+1][0] <= 10.5)
            
            summary = f"""
**Stage**: {stage_width}m Γ— {stage_depth}m = {stage_width * stage_depth}mΒ²
**Total Lots**: {total_lots}
**Unique Lot Types**: {unique_widths}
**SLHC Pairs**: {slhc_pairs}
**Grid Variance**: {variance:+.2f}m {"βœ…" if abs(variance) < 0.001 else "⚠️"}
"""
        
        # Convert solution to string for manual editing
        manual_edit_string = optimizer.solution_to_string(optimized_solution)
        
        return fig_2d, results_df, summary, report, manual_edit_string
    
    def update_manual_adjustment(manual_widths_text, stage_width, stage_depth, development_mode, md_load_type, color_scheme):
        """Update visualization based on manual adjustment"""
        # Set mode
        if development_mode == "Medium Density":
            optimizer.set_development_mode('medium_density', 'rear' if md_load_type == "Rear Loaded" else 'front')
        else:
            optimizer.set_development_mode('conventional')
        
        optimizer.current_scheme = color_scheme
        
        # Parse manual widths
        widths = optimizer.parse_manual_adjustments(manual_widths_text)
        
        if not widths:
            return None, "Please enter lot widths (e.g., '14.0, 8.5, 10.5, 8.5, 14.0')"
        
        # Validate and get feedback
        solution, feedback = optimizer.validate_manual_solution(widths, stage_width)
        
        if not solution:
            return None, feedback
        
        # Calculate variance
        total_width = sum(widths)
        variance = total_width - stage_width
        
        # Create visualization with variance
        fig = optimizer.create_enhanced_visualization(
            solution, stage_width, stage_depth,
            "Manually Adjusted Layout",
            show_variance=variance
        )
        
        return fig, feedback
    
    # Create Gradio interface
    with gr.Blocks(
        title="RPM Grid Cut Optimizer", 
        theme=gr.themes.Base(
            primary_hue="teal",
            secondary_hue="green",
            neutral_hue="gray",
            font=["Arial", "sans-serif"]
        ).set(
            body_background_fill="#2E3E2F",
            body_background_fill_dark="#2E3E2F",
            block_background_fill="#2E3E2F",
            block_background_fill_dark="#2E3E2F",
            panel_background_fill="#545D51",
            panel_background_fill_dark="#545D51",
            input_background_fill="#545D51",
            input_background_fill_dark="#545D51",
            button_primary_background_fill="#216767",
            button_primary_background_fill_dark="#216767",
            block_label_text_color="white",
            block_title_text_color="white",
            body_text_color="white"
        ),
        css="""
        .gradio-container {
            font-family: 'Arial', sans-serif !important;
            background: #2E3E2F !important;
            background-color: #2E3E2F !important;
            color: white !important;
        }
        .dark {
            --body-background-fill: #2E3E2F !important;
            --background-fill-primary: #2E3E2F !important;
            --background-fill-secondary: #545D51 !important;
            --panel-background-fill: #545D51 !important;
            --input-background-fill: #545D51 !important;
            --block-background-fill: #2E3E2F !important;
            --body-text-color: white !important;
            --block-label-text-color: white !important;
            --block-title-text-color: white !important;
            --text-color: white !important;
        }
        body {
            background-color: #2E3E2F !important;
        }
        .main {
            background-color: #2E3E2F !important;
        }
        .contain {
            background-color: #2E3E2F !important;
        }
        .app {
            background-color: #2E3E2F !important;
        }
        .gr-button-primary {
            background: #216767 !important;
            background-color: #216767 !important;
            border: none !important;
            box-shadow: 0 3px 5px 2px rgba(33, 103, 103, .3) !important;
            color: white !important;
        }
        .gr-button-primary:hover {
            background: #4F8585 !important;
            background-color: #4F8585 !important;
        }
        h1, h2, h3, h4, h5, h6 {
            color: white !important;
        }
        h1 {
            text-align: center;
            font-size: 2.5em;
            margin-bottom: 0.5em;
        }
        h3 {
            color: #FFCF6D !important;
        }
        .gr-form {
            background: rgba(84, 93, 81, 0.9) !important;
            background-color: rgba(84, 93, 81, 0.9) !important;
            border-radius: 10px !important;
            padding: 20px !important;
            border: 1px solid #216767 !important;
        }
        .gr-input, input[type="number"], input[type="text"], textarea {
            background-color: #545D51 !important;
            color: white !important;
            border: 1px solid #216767 !important;
        }
        .gr-input-label, .gr-radio-label {
            color: white !important;
        }
        .gr-check-radio {
            background-color: #545D51 !important;
        }
        .gr-checkbox {
            background-color: #545D51 !important;
        }
        .gr-checkbox input[type="checkbox"] + label {
            color: white !important;
        }
        label {
            color: white !important;
        }
        .gr-panel {
            background-color: #545D51 !important;
            border: 1px solid #216767 !important;
        }
        .gr-box {
            background-color: rgba(84, 93, 81, 0.5) !important;
            border-color: #216767 !important;
        }
        .gr-padded {
            background-color: transparent !important;
        }
        .gr-compact {
            background-color: rgba(84, 93, 81, 0.5) !important;
        }
        .gr-accordion {
            background-color: #545D51 !important;
            border-color: #216767 !important;
        }
        .output-class {
            background-color: #2E3E2F !important;
        }
        .input-container {
            background-color: #545D51 !important;
        }
        .wrap {
            background-color: transparent !important;
        }
        .wrap > div {
            background-color: transparent !important;
        }
        .gr-input-label {
            background-color: transparent !important;
        }
        .gr-group {
            background-color: rgba(84, 93, 81, 0.5) !important;
            border: 1px solid #216767 !important;
        }
        .markdown-text {
            color: white !important;
        }
        .markdown-text p {
            color: white !important;
        }
        .markdown-text h1, .markdown-text h2, .markdown-text h3 {
            color: white !important;
        }
        p {
            color: white !important;
        }
        /* Dark mode for radio buttons */
        .dark-radio {
            background-color: #545D51 !important;
            color: white !important;
        }
        .dark-radio label {
            color: white !important;
        }
        .dark-radio input[type="radio"] + label {
            color: white !important;
        }
        /* Dark mode for number inputs */
        .dark-input input {
            background-color: #545D51 !important;
            color: white !important;
            border: 1px solid #216767 !important;
        }
        .dark-input label {
            color: white !important;
        }
        /* Radio button container */
        .gr-radio {
            background-color: #545D51 !important;
        }
        /* Info text */
        .gr-info {
            color: #B6B8B2 !important;
        }
        /* Dark mode for checkboxes */
        .dark-checkbox {
            background-color: transparent !important;
        }
        .dark-checkbox label {
            color: white !important;
        }
        .dark-checkbox input[type="checkbox"] {
            background-color: #545D51 !important;
            border-color: #216767 !important;
        }
        .dark-checkbox input[type="checkbox"]:checked {
            background-color: #216767 !important;
        }
        /* Dataframe styling */
        .gr-dataframe {
            background-color: #545D51 !important;
            color: white !important;
        }
        .gr-dataframe th {
            background-color: #216767 !important;
            color: white !important;
        }
        .gr-dataframe td {
            background-color: #545D51 !important;
            color: white !important;
            border-color: #216767 !important;
        }
        /* Textbox styling */
        .dark-input textarea {
            background-color: #545D51 !important;
            color: white !important;
            border: 1px solid #216767 !important;
        }
        /* Secondary button styling */
        .gr-button-secondary {
            background: #545D51 !important;
            background-color: #545D51 !important;
            color: white !important;
            border: 1px solid #216767 !important;
        }
        .gr-button-secondary:hover {
            background: #697687 !important;
            background-color: #697687 !important;
        }
        /* Output containers */
        .gr-markdown {
            color: white !important;
        }
        .gr-markdown * {
            color: white !important;
        }
        /* Plot container */
        .gr-plot {
            background-color: #2E3E2F !important;
        }
        /* Fix for light theme bleeding through */
        :root {
            --body-background-fill: #2E3E2F !important;
            --background-fill-primary: #2E3E2F !important;
            --background-fill-secondary: #545D51 !important;
            --panel-background-fill: #545D51 !important;
            --input-background-fill: #545D51 !important;
            --block-background-fill: #2E3E2F !important;
            --body-text-color: white !important;
            --block-label-text-color: white !important;
            --block-title-text-color: white !important;
        }
        """
    ) as demo:
        gr.Markdown("""
        <div style='text-align: center; margin-bottom: 2em;'>
            <h1 style='color: white; margin-bottom: 0;'>RPM Grid Cut Optimizer</h1>
            <p style='color: #216767; font-size: 1.2em;'>AI-Powered Subdivision Planning</p>
        </div>
        """)
        
        # Force dark mode
        demo.load(
            lambda: None,
            None,
            None,
            js="""
            () => {
                document.body.classList.add('dark');
                document.documentElement.style.setProperty('--body-background-fill', '#2E3E2F');
                document.documentElement.style.setProperty('--background-fill-primary', '#2E3E2F');
                document.documentElement.style.setProperty('--background-fill-secondary', '#545D51');
                document.documentElement.style.setProperty('--panel-background-fill', '#545D51');
                document.documentElement.style.setProperty('--input-background-fill', '#545D51');
                document.documentElement.style.setProperty('--block-background-fill', '#2E3E2F');
                document.documentElement.style.setProperty('--body-text-color', 'white');
                document.documentElement.style.setProperty('--block-label-text-color', 'white');
                document.documentElement.style.setProperty('--block-title-text-color', 'white');
            }
            """
        )
        
        with gr.Row():
            with gr.Column(scale=1):
                with gr.Group():
                    gr.Markdown("<h3 style='color: #FFCF6D'>πŸ“ Stage Configuration</h3>")
                    
                    development_mode = gr.Radio(
                        ["Conventional Land", "Medium Density"],
                        label="🏘️ Development Mode",
                        value="Conventional Land",
                        info="Select the type of development",
                        elem_classes=["dark-radio"]
                    )
                    
                    md_load_type = gr.Radio(
                        ["Front Loaded", "Rear Loaded"],
                        label="πŸš— MD Access Type",
                        value="Front Loaded",
                        visible=False,
                        info="Rear loaded includes 7m laneway",
                        elem_classes=["dark-radio"]
                    )
                    
                    stage_width = gr.Number(
                        label="Stage Width (m)", 
                        value=105.0,
                        info="Width along the street",
                        elem_classes=["dark-input"]
                    )
                    stage_depth = gr.Number(
                        label="Stage Depth (m)", 
                        value=32.0,
                        info="Depth of lots (perpendicular to street)",
                        elem_classes=["dark-input"]
                    )
                
                gr.Markdown("<h3 style='color: #FFCF6D'>πŸ“ Lot Width Options</h3>")
                
                # Conventional widths group
                with gr.Group(visible=True) as conventional_group:
                    gr.Markdown("<p style='color: white; font-weight: bold'>Standard Widths</p>")
                    with gr.Row():
                        enable_8_5 = gr.Checkbox(label="8.5m SLHC", value=True, elem_classes=["dark-checkbox"])
                        enable_10_5 = gr.Checkbox(label="10.5m SLHC", value=True, elem_classes=["dark-checkbox"])
                        enable_12_5 = gr.Checkbox(label="12.5m", value=True, elem_classes=["dark-checkbox"])
                    with gr.Row():
                        enable_14 = gr.Checkbox(label="14.0m", value=True, elem_classes=["dark-checkbox"])
                        enable_16 = gr.Checkbox(label="16.0m", value=True, elem_classes=["dark-checkbox"])
                        enable_18 = gr.Checkbox(label="18.0m", value=False, elem_classes=["dark-checkbox"])
                    
                    enable_corners = gr.Checkbox(
                        label="Enable Corner-Specific Widths", 
                        value=True,
                        info="Adds variety and helps achieve 100%",
                        elem_classes=["dark-checkbox"]
                    )
                    with gr.Row():
                        enable_11 = gr.Checkbox(label="11.0m", value=True, elem_classes=["dark-checkbox"])
                        enable_13_3 = gr.Checkbox(label="13.3m", value=True, elem_classes=["dark-checkbox"])
                    with gr.Row():
                        enable_14_8 = gr.Checkbox(label="14.8m", value=True, elem_classes=["dark-checkbox"])
                        enable_16_8 = gr.Checkbox(label="16.8m", value=True, elem_classes=["dark-checkbox"])
                
                # MD Rear Loaded widths
                with gr.Group(visible=False) as md_rear_group:
                    gr.Markdown("<p style='color: white; font-weight: bold'>MD Rear Loaded Widths</p>")
                    enable_4_5 = gr.Checkbox(label="4.5m (2/2/1)", value=True, elem_classes=["dark-checkbox"])
                    enable_6_0 = gr.Checkbox(label="6.0m (3/2/2)", value=True, elem_classes=["dark-checkbox"])
                    enable_7_5 = gr.Checkbox(label="7.5m (3-4/2/2)", value=True, elem_classes=["dark-checkbox"])
                
                # MD Front Loaded widths
                with gr.Group(visible=False) as md_front_group:
                    gr.Markdown("<p style='color: white; font-weight: bold'>MD Front Loaded Widths</p>")
                    enable_7_0 = gr.Checkbox(label="7.0m (3/2/1)", value=True, elem_classes=["dark-checkbox"])
                    enable_8_0 = gr.Checkbox(label="8.0m (3-4/2/2)", value=True, elem_classes=["dark-checkbox"])
                    enable_md_8_5 = gr.Checkbox(label="8.5m (3/2/1)", value=True, elem_classes=["dark-checkbox"])
                    enable_md_10_5 = gr.Checkbox(label="10.5m (3-4/2/2)", value=True, elem_classes=["dark-checkbox"])
            
            with gr.Column(scale=1):
                gr.Markdown("<h3 style='color: #FFCF6D'>βš™οΈ Settings</h3>")
                
                allow_custom_corners = gr.Checkbox(
                    label="🎯 Allow Flexible Corner Widths",
                    value=True,
                    info="Enables 13.8m, 13.9m etc. for perfect fits",
                    elem_classes=["dark-checkbox"]
                )
                
                color_scheme = gr.Radio(
                    ["rpm_primary", "rpm_contrast", "rpm_monochrome"],
                    label="🎨 Color Scheme",
                    value="rpm_primary",
                    info="RPM brand color palettes",
                    elem_classes=["dark-radio"]
                )
                
                optimize_btn = gr.Button(
                    "πŸš€ Optimize Grid Cut", 
                    variant="primary", 
                    size="lg",
                    elem_id="optimize-button"
                )
                
                gr.Markdown("""
                <div style='background-color: rgba(84, 93, 81, 0.5); padding: 15px; border-radius: 8px; border: 1px solid #216767;'>
                <h3 style='color: #FFCF6D; margin-top: 0;'>πŸ’‘ Quick Tips:</h3>
                <ul style='color: white; margin-bottom: 0;'>
                <li><strong style='color: #FFCF6D'>Conventional</strong>: Traditional lots with corner splays</li>
                <li><strong style='color: #FFCF6D'>Medium Density</strong>: Compact lots for higher yield</li>
                <li><strong style='color: #FFCF6D'>Rear Loaded</strong>: Includes 7m laneway visualization</li>
                <li><strong style='color: #FFCF6D'>Grid Variance</strong>: Shows if layout is perfect (0.0m)</li>
                </ul>
                </div>
                """)
        
        with gr.Row():
            plot_2d = gr.Plot(label="2D Layout Visualization")
        
        # Manual adjustment section
        gr.Markdown("<h3 style='color: #FFCF6D'>✏️ Fine-Tune Result</h3>")
        with gr.Row():
            with gr.Column(scale=2):
                manual_widths = gr.Textbox(
                    label="Manually Adjust Lot Widths",
                    placeholder="Widths will appear here after optimization",
                    info="Edit the widths (comma-separated) and click 'Update Layout'",
                    lines=2,
                    elem_classes=["dark-input"]
                )
            with gr.Column(scale=1):
                update_btn = gr.Button("πŸ”„ Update Layout", variant="secondary")
                adjustment_feedback = gr.Markdown(
                    value="",
                    label="Adjustment Feedback"
                )
        
        with gr.Row():
            results_table = gr.DataFrame(label="Lot Distribution Analysis")
        
        with gr.Row():
            with gr.Column():
                summary_output = gr.Markdown(label="Optimization Summary")
            with gr.Column():
                report_output = gr.Markdown(label="Professional Report")
        
        # Wire up development mode changes
        def handle_mode_change(mode):
            if mode == "Medium Density":
                return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
            else:
                return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
        
        def handle_md_type_change(md_type):
            if md_type == "Rear Loaded":
                return gr.update(visible=True), gr.update(visible=False)
            else:
                return gr.update(visible=False), gr.update(visible=True)
        
        development_mode.change(
            handle_mode_change,
            inputs=[development_mode],
            outputs=[md_load_type, conventional_group, md_rear_group, md_front_group]
        )
        
        md_load_type.change(
            handle_md_type_change,
            inputs=[md_load_type],
            outputs=[md_rear_group, md_front_group]
        )
        
        # Wire up the optimize button
        optimize_btn.click(
            optimize_grid,
            inputs=[
                stage_width,
                stage_depth,
                development_mode,
                md_load_type,
                # Conventional
                enable_8_5, enable_10_5, enable_12_5, enable_14, enable_16, enable_18,
                enable_corners, enable_11, enable_13_3, enable_14_8, enable_16_8,
                # MD Rear
                enable_4_5, enable_6_0, enable_7_5,
                # MD Front
                enable_7_0, enable_8_0, enable_md_8_5, enable_md_10_5,
                allow_custom_corners, color_scheme
            ],
            outputs=[plot_2d, results_table, summary_output, report_output, manual_widths]
        )
        
        update_btn.click(
            update_manual_adjustment,
            inputs=[manual_widths, stage_width, stage_depth, development_mode, md_load_type, color_scheme],
            outputs=[plot_2d, adjustment_feedback]
        )
        
    return demo

# Create and launch
if __name__ == "__main__":
    app = create_advanced_app()
    app.queue()
    app.launch(share=False, inbrowser=True)