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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener("DOMContentLoaded", () => {
  // --- Element Selectors ---
  const promptInput = document.getElementById("prompt-input");
  const generateBtn = document.getElementById("generate-btn");
  const modificationInput = document.getElementById("modification-input");
  const modifyBtn = document.getElementById("modify-btn");
  const downloadBtn = document.getElementById("download-btn");
  const openTabBtn = document.getElementById("open-tab-btn");
  const copyCodeBtn = document.getElementById("copy-code-btn");

  const togglePreviewBtn = document.getElementById("toggle-preview-btn");
  const toggleCodeBtn = document.getElementById("toggle-code-btn");
  const refreshPreviewBtn = document.getElementById("refresh-preview-btn");

  const codeContainer = document.querySelector(".code-container");
  const previewContainer = document.querySelector(".preview-container");
  const monacoContainer = document.getElementById("monaco-editor");
  const gameIframe = document.getElementById("game-iframe");

  const initialGenerationPanel = document.getElementById("initial-generation");
  const modificationPanel = document.getElementById("modification-panel");
  const spinner = document.querySelector(".spinner-container");

  const analysisContainer = document.getElementById("analysis-container");
  const aiAnalysis = document.getElementById("ai-analysis");
  const changesContainer = document.getElementById("changes-container");
  const summaryOfChanges = document.getElementById("summary-of-changes");
  const instructionsContainer = document.getElementById(
    "instructions-container"
  );
  const gameInstructions = document.getElementById("game-instructions");

  const consoleContainer = document.getElementById("console-container");
  const consoleOutput = document.getElementById("console-output");

  const followUpInput = document.getElementById("follow-up-input");
  const followUpBtn = document.getElementById("follow-up-btn");
  const followUpSpinner = document.getElementById("follow-up-spinner");
  const followUpOutputContainer = document.getElementById(
    "follow-up-output-container"
  );
  const followUpOutput = document.getElementById("follow-up-output");

  const versionHistoryControls = document.getElementById(
    "version-history-controls"
  );
  const versionHistorySelect = document.getElementById(
    "version-history-select"
  );

  const loadSessionBtn = document.getElementById("load-session-btn");
  const saveSessionBtn = document.getElementById("save-session-btn");
  const loadSessionPopup = document.getElementById("load-session-popup");
  const saveSessionPopup = document.getElementById("save-session-popup");
  const loadSessionPopupClose = document.getElementById(
    "load-session-popup-close"
  );
  const saveSessionPopupClose = document.getElementById(
    "save-session-popup-close"
  );

  const followupFloat = document.getElementById("followup-float");
  const followupBtn = document.getElementById("followup-btn");
  const followupPopup = document.getElementById("followup-popup");
  const followupPopupClose = document.getElementById("followup-popup-close");

  const sessionSelect = document.getElementById("session-select");
  const sessionNameInput = document.getElementById("session-name-input");
  const loadSessionActionBtn = document.getElementById(
    "load-session-action-btn"
  );
  const saveSessionActionBtn = document.getElementById(
    "save-session-action-btn"
  );
  const deleteSessionBtn = document.getElementById("delete-session-btn");
  const newSessionBtn = document.getElementById("new-session-btn");

  const hamburgerBtn = document.getElementById("hamburger-btn");
  const sidebar = document.getElementById("sidebar");
  const sidebarOverlay = document.getElementById("sidebar-overlay");
  const themeToggle = document.getElementById("theme-toggle");

  const generateFileInput = document.getElementById("generate-file-input");
  const generateFileList = document.getElementById("generate-file-list");
  const modifyFileInput = document.getElementById("modify-file-input");
  const modifyFileList = document.getElementById("modify-file-list");

  // --- State Variables ---
  let promptHistory = [];
  let consoleLogs = [];
  let abortController = null;
  let versionHistory = [];
  let currentSessionId = null;
  const clientSideAssets = new Map();
  const DB_NAME = "CodeCanvasDB";
  const DB_VERSION = 1;
  const STORE_NAME = "sessions";
  let marked = null;
  let db = null;
  let monacoEditor = null;
  let monaco = null;
  let lastScrollPosition = { lineNumber: 1, column: 1 };
  let hasGeneratedContent = false;
  const mainContainer = document.querySelector(".main-container");

  let autoScrollEnabled = true;

  // --- IndexedDB Functions ---
  function initDB() {
    return new Promise((resolve, reject) => {
      const request = indexedDB.open(DB_NAME, DB_VERSION);
      request.onerror = () => reject(request.error);
      request.onsuccess = () => {
        db = request.result;
        resolve(db);
      };
      request.onupgradeneeded = (event) => {
        db = event.target.result;
        if (!db.objectStoreNames.contains(STORE_NAME)) {
          const store = db.createObjectStore(STORE_NAME, { keyPath: "id" });
          store.createIndex("name", "name", { unique: false });
          store.createIndex("savedAt", "savedAt", { unique: false });
        }
      };
    });
  }

  async function saveSessionToDB(sessionData) {
    if (!db) await initDB();
    return new Promise((resolve, reject) => {
      const transaction = db.transaction([STORE_NAME], "readwrite");
      const store = transaction.objectStore(STORE_NAME);
      const request = store.put(sessionData);
      request.onsuccess = () => resolve(request.result);
      request.onerror = () => reject(request.error);
    });
  }

  async function getSessionsFromDB() {
    if (!db) await initDB();
    return new Promise((resolve, reject) => {
      const transaction = db.transaction([STORE_NAME], "readonly");
      const store = transaction.objectStore(STORE_NAME);
      const request = store.getAll();
      request.onsuccess = () => resolve(request.result || []);
      request.onerror = () => reject(request.error);
    });
  }

  async function deleteSessionFromDB(sessionId) {
    if (!db) await initDB();
    return new Promise((resolve, reject) => {
      const transaction = db.transaction([STORE_NAME], "readwrite");
      const store = transaction.objectStore(STORE_NAME);
      const request = store.delete(sessionId);
      request.onsuccess = () => resolve();
      request.onerror = () => reject(request.error);
    });
  }

  // --- Core Application Logic ---

  class StreamParser {
    constructor() {
      this.reset();
    }

    reset() {
      this.analysis = "";
      this.changes = "";
      this.instructions = "";
      this.html = "";
      this.currentSection = null;
      this.buffer = "";
    }

    processChunk(chunk) {
      this.buffer += chunk;
      const lines = this.buffer.split("\n");
      this.buffer = lines.pop();

      for (const line of lines) {
        const trimmedLine = line.trim();

        if (trimmedLine === "[ANALYSIS]") {
          this.currentSection = "ANALYSIS";
          continue;
        }
        if (trimmedLine === "[END_ANALYSIS]") {
          this.currentSection = null;
          continue;
        }
        if (trimmedLine === "[CHANGES]") {
          this.currentSection = "CHANGES";
          continue;
        }
        if (trimmedLine === "[END_CHANGES]") {
          this.currentSection = null;
          continue;
        }
        if (trimmedLine === "[INSTRUCTIONS]") {
          this.currentSection = "INSTRUCTIONS";
          continue;
        }
        if (trimmedLine === "[END_INSTRUCTIONS]") {
          this.currentSection = "HTML";
          continue;
        }

        switch (this.currentSection) {
          case "ANALYSIS":
            this.analysis += line + "\n";
            break;
          case "CHANGES":
            this.changes += line + "\n";
            break;
          case "INSTRUCTIONS":
            this.instructions += line + "\n";
            break;
          case "HTML":
            this.html += line + "\n";
            break;
          default:
            if (
              trimmedLine.startsWith("<!DOCTYPE html>") ||
              this.currentSection === "HTML"
            ) {
              this.currentSection = "HTML";
              this.html += line + "\n";
            }
            break;
        }
      }
    }

    /**
     * BUGFIX: Returns the current state NON-DESTRUCTIVELY for live UI updates.
     * It does NOT process the buffer, preventing duplication errors.
     */
    getCurrentState() {
      return {
        analysis: this.analysis.trim(),
        changes: this.changes.trim(),
        instructions: this.instructions.trim(),
        html: this.cleanHtml(this.html),
      };
    }

    /**
     * BUGFIX: Finalizes the stream by processing the remaining buffer.
     * This should ONLY be called once at the very end.
     */
    finalize() {
      if (this.buffer) {
        // Assume any remaining buffer content belongs to the last active section, defaulting to HTML
        const targetSection = this.currentSection || "HTML";
        switch (targetSection) {
          case "ANALYSIS":
            this.analysis += this.buffer;
            break;
          case "CHANGES":
            this.changes += this.buffer;
            break;
          case "INSTRUCTIONS":
            this.instructions += this.buffer;
            break;
          case "HTML":
          default:
            this.html += this.buffer;
            break;
        }
        this.buffer = ""; // Clear the buffer after finalizing
      }
      return this.getCurrentState();
    }

    cleanHtml(htmlString) {
      return htmlString.replace(/^```html\s*|\s*```$/g, "").trim();
    }
  }

  async function streamResponse(url, body, targetElement) {
    setLoading(true, url);
    const parser = new StreamParser();

    if (!targetElement) {
      setView("code");
      if (monacoEditor) monacoEditor.setValue("");
      clearInfoPanels();
    }

    if (abortController) abortController.abort();
    abortController = new AbortController();

    try {
      const isFormData = body instanceof FormData;
      const response = await fetch(url, {
        method: "POST",
        body: isFormData ? body : JSON.stringify(body),
        headers: isFormData ? {} : { "Content-Type": "application/json" },
        signal: abortController.signal,
      });

      if (!response.ok)
        throw new Error(`HTTP error! status: ${response.status}`);
      const reader = response.body.getReader();
      const decoder = new TextDecoder();

      while (true) {
        const { done, value } = await reader.read();
        if (done) break;
        let chunk = decoder.decode(value, { stream: true });

        if (chunk.includes("[STREAM_RESTART]")) {
          console.warn("Stream restart signal received.");
          showNotification(
            "Primary model failed; switching to fallback.",
            "warning"
          );
          parser.reset();
          clearInfoPanels();
          if (monacoEditor) monacoEditor.setValue("");
          chunk = chunk.replace(/\[STREAM_RESTART\]\s*\n?/, "");
        }

        if (targetElement) {
          targetElement.textContent += chunk;
          targetElement.scrollTop = targetElement.scrollHeight;
        } else {
          parser.processChunk(chunk);
          // BUGFIX: Use the non-destructive getter for live updates
          const currentData = parser.getCurrentState();
          updateUIFromStream(currentData);
        }
      }

      if (!targetElement) {
        const prompt = isFormData ? body.get("prompt") : body.prompt;
        // BUGFIX: Call finalize() only once at the end of the stream
        const finalData = parser.finalize();

        // Final UI update with the fully parsed data
        updateUIFromStream(finalData);
        if (monacoEditor) {
          const totalLines = monacoEditor.getModel().getLineCount();
          monacoEditor.revealLineNearTop(
            totalLines,
            monaco.editor.ScrollType.Smooth
          );
        }

        // Update iframe and save version with the final, clean data
        updateIframe(finalData.html);
        saveVersion({ ...finalData, prompt });
        showModificationPanel();
        setTimeout(minimizeInfoPanels, 2000);
      }
    } catch (error) {
      if (error.name !== "AbortError") {
        console.error("Streaming failed:", error);
        const errorMessage = `Error: Failed to get response. Details: ${error.message}`;
        if (targetElement) {
          targetElement.textContent = "Error: Could not get response.";
        } else if (monacoEditor) {
          monacoEditor.setValue(errorMessage);
          setView("code");
        }
      }
    } finally {
      setLoading(false, url);
      abortController = null;
    }
  }

  function updateUIFromStream({ analysis, changes, instructions, html }) {
    if (analysis) {
      analysisContainer.classList.remove("hidden");
      aiAnalysis.innerHTML = marked ? marked.parse(analysis) : analysis;
      aiAnalysis.scrollTop = aiAnalysis.scrollHeight;
    }

    if (changes) {
      changesContainer.classList.remove("hidden");
      summaryOfChanges.innerHTML = marked ? marked.parse(changes) : changes;
      summaryOfChanges.scrollTop = summaryOfChanges.scrollHeight;
    }

    if (instructions) {
      instructionsContainer.classList.remove("hidden");
      gameInstructions.innerHTML = marked
        ? marked.parse(instructions)
        : instructions;
      gameInstructions.scrollTop = gameInstructions.scrollHeight;
    }

    if (html && monacoEditor) {
      const currentContent = monacoEditor.getValue();
      if (html !== currentContent) {
        const model = monacoEditor.getModel();

        monacoEditor.executeEdits(null, [
          { range: model.getFullModelRange(), text: html },
        ]);

        // --- Force scroll to bottom every time ---
        const scrollHeight = monacoEditor.getScrollHeight();
        monacoEditor.setScrollTop(scrollHeight);
      }
    }
  }

  // --- Client-Side Asset Handling ---
  function handleFileSelection(event, fileListElement) {
    for (const file of event.target.files) {
      if (!clientSideAssets.has(file.name)) {
        clientSideAssets.set(file.name, {
          file: file,
          blobUrl: URL.createObjectURL(file),
        });
      }
    }
    renderSelectedFiles(fileListElement);
    event.target.value = "";
  }

  function renderSelectedFiles(fileListElement) {
    fileListElement.innerHTML = "";
    fileListElement.classList.toggle("hidden", clientSideAssets.size === 0);
    clientSideAssets.forEach((asset, fileName) => {
      const fileItem = document.createElement("div");
      fileItem.className = "file-item";
      const nameSpan = document.createElement("span");
      nameSpan.textContent = fileName;
      const removeBtn = document.createElement("button");
      removeBtn.innerHTML = "&times;";
      removeBtn.className = "file-remove-btn";
      removeBtn.onclick = () => {
        URL.revokeObjectURL(asset.blobUrl);
        clientSideAssets.delete(fileName);
        renderSelectedFiles(generateFileList);
        renderSelectedFiles(modifyFileList);
      };
      fileItem.appendChild(nameSpan);
      fileItem.appendChild(removeBtn);
      fileListElement.appendChild(fileItem);
    });
  }

  async function deserializeAssets(assets) {
    clientSideAssets.clear();
    for (const assetData of assets) {
      try {
        const blob = new Blob([assetData.data], { type: assetData.type });
        const file = new File([blob], assetData.fileName, {
          type: assetData.type,
        });
        clientSideAssets.set(assetData.fileName, {
          file: file,
          blobUrl: URL.createObjectURL(blob),
        });
      } catch (error) {
        console.error(`Failed to restore asset ${assetData.fileName}:`, error);
      }
    }
    renderSelectedFiles(generateFileList);
    renderSelectedFiles(modifyFileList);
  }

  // --- Event Handlers ---
  loadSessionBtn.addEventListener("click", () => {
    populateSessionDropdown();
    openPopup(loadSessionPopup);
  });

  saveSessionBtn.addEventListener("click", () => {
    openPopup(saveSessionPopup);
  });

  generateFileInput.addEventListener("change", (e) =>
    handleFileSelection(e, generateFileList)
  );
  modifyFileInput.addEventListener("change", (e) =>
    handleFileSelection(e, modifyFileList)
  );

  generateBtn.addEventListener("click", async () => {
    const prompt = promptInput.value.trim();
    if (!prompt) {
      showNotification("Please enter a prompt", "error");
      return;
    }
    hasGeneratedContent = true;
    updateLayoutState();
    versionHistory = [];
    promptHistory = [];
    currentSessionId = null;
    sessionNameInput.value = prompt.substring(0, 50);
    const formData = new FormData();
    formData.append("prompt", prompt);
    clientSideAssets.forEach((asset) =>
      formData.append("files", asset.file, asset.file.name)
    );
    streamResponse("/generate", formData);
    if (window.innerWidth <= 768) closeSidebar();
  });

  modifyBtn.addEventListener("click", async () => {
    const modification = modificationInput.value.trim();
    if (!modification) {
      showNotification("Please enter a modification request", "error");
      return;
    }
    hasGeneratedContent = true;
    updateLayoutState();
    const currentHtml = monacoEditor ? monacoEditor.getValue() : "";
    if (!currentHtml) return alert("There is no code to modify!");
    const formData = new FormData();
    formData.append("prompt", modification);
    formData.append("current_code", currentHtml);
    formData.append("console_logs", consoleLogs.join("\n"));
    promptHistory.forEach((p) => formData.append("prompt_history", p));
    clientSideAssets.forEach((asset) =>
      formData.append("files", asset.file, asset.file.name)
    );
    streamResponse("/modify", formData);
    if (window.innerWidth <= 768) closeSidebar();
  });

async function streamResponseMarkdown(url, body, targetElement) {
  setLoading(true, url);
  if (abortController) abortController.abort();
  abortController = new AbortController();
  
  let accumulatedText = "";
  
  try {
    const isFormData = body instanceof FormData;
    const response = await fetch(url, {
      method: "POST",
      body: isFormData ? body : JSON.stringify(body),
      headers: isFormData ? {} : { "Content-Type": "application/json" },
      signal: abortController.signal,
    });
    if (!response.ok)
      throw new Error(`HTTP error! status: ${response.status}`);
    const reader = response.body.getReader();
    const decoder = new TextDecoder();
    
    // Show loading indicator while streaming
    targetElement.innerHTML = '<div class="streaming-indicator">Receiving response...</div>';
    
    while (true) {
      const { done, value } = await reader.read();
      if (done) break;
      let chunk = decoder.decode(value, { stream: true });
      accumulatedText += chunk;
    }
    
    // Render markdown only once after all text is received
    const md = window.markdownit();
    targetElement.innerHTML = md.render(accumulatedText);
    targetElement.scrollTop = targetElement.scrollHeight;
    
  } catch (error) {
    if (error.name !== "AbortError") {
      console.error("Streaming failed:", error);
      targetElement.textContent = "Error: Could not get response.";
    }
  } finally {
    setLoading(false, url);
    abortController = null;
  }
}

  followUpBtn.addEventListener("click", () => {
    const question = followUpInput.value.trim();
    if (!question) return alert("Please ask a question!");
    const currentHtml = monacoEditor ? monacoEditor.getValue() : "";
    if (!currentHtml) return alert("There is no code to ask about!");
    followUpOutputContainer.classList.remove("hidden");
    followUpOutput.innerHTML = "";
    streamResponseMarkdown(
      "/explain",
      { question, current_code: currentHtml },
      followUpOutput
    );
  });

  downloadBtn.addEventListener("click", async () => {
    const html = monacoEditor ? monacoEditor.getValue() : "";
    if (!html) return alert("No code to download!");
    if (clientSideAssets.size === 0) {
      const blob = new Blob([html], { type: "text/html" });
      const url = URL.createObjectURL(blob);
      const a = document.createElement("a");
      a.href = url;
      a.download = "creation.html";
      a.click();
      URL.revokeObjectURL(url);
      return;
    }
    const formData = new FormData();
    formData.append("html_content", html);
    clientSideAssets.forEach((asset) =>
      formData.append("files", asset.file, asset.file.name)
    );
    try {
      const response = await fetch("/download_zip", {
        method: "POST",
        body: formData,
      });
      if (!response.ok)
        throw new Error(`Failed to create zip file: ${response.statusText}`);
      const blob = await response.blob();
      const url = URL.createObjectURL(blob);
      const a = document.createElement("a");
      a.href = url;
      a.download = "promptlab-project.zip";
      a.click();
      URL.revokeObjectURL(url);
    } catch (error) {
      console.error("Download failed:", error);
      alert("Failed to download project as a zip. Check the console.");
    }
  });

  openTabBtn.addEventListener("click", () => {
    const html = monacoEditor ? monacoEditor.getValue() : "";
    if (!html) return alert("No code to open!");
    const processedHtml = replaceAssetPathsWithBlobs(html);
    const blob = new Blob([processedHtml], { type: "text/html" });
    const url = URL.createObjectURL(blob);
    window.open(url, "_blank");
  });

  copyCodeBtn.addEventListener("click", async () => {
    const code = monacoEditor ? monacoEditor.getValue() : "";
    if (!code) return;
    try {
      await navigator.clipboard.writeText(code);
      const originalContent = copyCodeBtn.innerHTML;
      copyCodeBtn.innerHTML =
        '<i class="fas fa-check"></i> <span>Copied!</span>';
      setTimeout(() => {
        copyCodeBtn.innerHTML = originalContent;
      }, 2000);
    } catch (err) {
      console.error("Failed to copy text: ", err);
    }
  });

  refreshPreviewBtn.addEventListener("click", () => {
    const code = monacoEditor ? monacoEditor.getValue() : "";
    updateIframe(code);
    setView("preview");
  });

  togglePreviewBtn.addEventListener("click", () => setView("preview"));
  toggleCodeBtn.addEventListener("click", () => setView("code"));

  // --- UI and State Management Functions ---
  function replaceAssetPathsWithBlobs(htmlContent) {
    if (clientSideAssets.size === 0) return htmlContent;
    let processedHtml = htmlContent;
    clientSideAssets.forEach((asset, fileName) => {
      const safeFileName = fileName.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
      const regex = new RegExp(`(['"])assets/${safeFileName}\\1`, "g");
      processedHtml = processedHtml.replace(regex, `$1${asset.blobUrl}$1`);
    });
    return processedHtml;
  }

  function updateIframe(htmlContent) {
    const consoleLoggerScript = `<script>
        const originalConsole = { ...window.console };
        const postLog = (type, args) => { try { const message = args.map(arg => (typeof arg === 'object' && arg !== null) ? JSON.stringify(arg) : String(arg)).join(' '); window.parent.postMessage({ type: 'console', level: type, message }, '*'); } catch (e) {} };
        window.console.log = (...args) => { postLog('log', args); originalConsole.log(...args); };
        window.console.error = (...args) => { postLog('error', args); originalConsole.error(...args); };
        window.console.warn = (...args) => { postLog('warn', args); originalConsole.warn(...args); };
        window.addEventListener('error', e => postLog('error', [e.message, 'at', e.filename + ':' + e.lineno]));
    </script>`;
    consoleLogs = [];
    updateConsoleDisplay();
    const processedHtml = replaceAssetPathsWithBlobs(htmlContent);
    const enhancedHtml = `<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>html, body { margin: 0; padding: 0; overflow: auto; min-height: 100vh; box-sizing: border-box; } * { box-sizing: border-box; }</style>${consoleLoggerScript}</head><body>${processedHtml}</body></html>`;
    gameIframe.srcdoc = enhancedHtml;
  }

  window.addEventListener("message", (event) => {
    if (event.data && event.data.type === "console") {
      const { level, message } = event.data;
      consoleLogs.push(`[${level.toUpperCase()}] ${message}`);
      updateConsoleDisplay();
    }
  });

  function updateConsoleDisplay() {
    consoleContainer.classList.toggle("hidden", consoleLogs.length === 0);
    if (consoleLogs.length > 0) {
      consoleOutput.value = consoleLogs.join("\n");
      consoleOutput.scrollTop = consoleOutput.scrollHeight;
    }
  }

  function setView(view) {
    if (view === "code" && monacoEditor) {
      lastScrollPosition = monacoEditor.getPosition() || {
        lineNumber: 1,
        column: 1,
      };
    }
    previewContainer.classList.toggle("hidden", view !== "preview");
    codeContainer.classList.toggle("hidden", view === "preview");
    togglePreviewBtn.classList.toggle("active", view === "preview");
    toggleCodeBtn.classList.toggle("active", view !== "preview");
    refreshPreviewBtn.classList.toggle("hidden", view === "preview");

    if (view === "code" && monacoEditor) {
      setTimeout(() => {
        monacoEditor.layout();
        monacoEditor.setPosition(lastScrollPosition);
        monacoEditor.revealPosition(lastScrollPosition);
      }, 100);
    }
  }

  function setLoading(isLoading, url) {
    if (url === "/explain") {
      followUpSpinner.classList.toggle("hidden", !isLoading);
      followUpBtn.disabled = isLoading;
    } else {
      spinner.classList.toggle("hidden", !isLoading);
      generateBtn.disabled = isLoading;
      modifyBtn.disabled = isLoading;
    }
  }

  function showModificationPanel() {
    initialGenerationPanel.classList.add("hidden");
    modificationPanel.classList.remove("hidden");
    followupFloat.classList.add("show");
    renderSelectedFiles(generateFileList);
    renderSelectedFiles(modifyFileList);
  }

  function clearInfoPanels() {
    analysisContainer.classList.add("hidden");
    changesContainer.classList.add("hidden");
    instructionsContainer.classList.add("hidden");
    aiAnalysis.innerHTML = "";
    summaryOfChanges.innerHTML = "";
    gameInstructions.innerHTML = "";
  }

  function minimizeInfoPanels() {
    [analysisContainer, changesContainer, instructionsContainer].forEach(
      (panel) => {
        if (!panel.classList.contains("hidden")) {
          panel.open = false;
        }
      }
    );
  }

  function updateLayoutState() {
    mainContainer.classList.toggle("centered", !hasGeneratedContent);
  }

  // --- Session, Version, and UI Initialization ---
  function saveVersion(versionData) {
    versionHistory.push(versionData);
    promptHistory = versionHistory.map((v) => v.prompt);
    updateVersionHistoryUI();
  }

  function updateVersionHistoryUI() {
    versionHistoryControls.classList.toggle(
      "hidden",
      versionHistory.length === 0
    );
    versionHistorySelect.innerHTML = versionHistory
      .map(
        (version, index) =>
          `<option value="${index}">V${index + 1}: ${version.prompt.substring(
            0,
            50
          )}...</option>`
      )
      .reverse()
      .join("");
    versionHistorySelect.value = versionHistory.length - 1;
  }

  function loadVersion(index) {
    const version = versionHistory[index];
    if (!version) return;
    if (monacoEditor) {
      monacoEditor.setValue(version.html || "");
    }
    updateIframe(version.html || "");
    clearInfoPanels();
    if (version.analysis) {
      analysisContainer.classList.remove("hidden");
      aiAnalysis.innerHTML = marked.parse(version.analysis);
    }
    if (version.changes) {
      changesContainer.classList.remove("hidden");
      summaryOfChanges.innerHTML = marked.parse(version.changes);
    }
    if (version.instructions) {
      instructionsContainer.classList.remove("hidden");
      gameInstructions.innerHTML = marked.parse(version.instructions);
    }
    promptHistory = versionHistory.slice(0, index + 1).map((v) => v.prompt);
  }

  versionHistorySelect.addEventListener("change", (e) =>
    loadVersion(Number(e.target.value))
  );

  async function populateSessionDropdown() {
    try {
      const sessions = await getSessionsFromDB();
      if (sessions.length === 0) {
        sessionSelect.innerHTML = '<option value="">No sessions found</option>';
        return;
      }
      sessionSelect.innerHTML = sessions
        .sort((a, b) => new Date(b.savedAt) - new Date(a.savedAt))
        .map(
          (s) =>
            `<option value="${s.id}">${s.name} - ${new Date(
              s.savedAt
            ).toLocaleDateString()}</option>`
        )
        .join("");
    } catch (error) {
      console.error("Error loading sessions:", error);
      sessionSelect.innerHTML =
        '<option value="">Error loading sessions</option>';
    }
  }

  async function saveCurrentSession() {
    const sessionName = sessionNameInput.value.trim();
    if (!sessionName) return alert("Please enter a session name.");
    if (versionHistory.length === 0) return alert("Nothing to save.");

    try {
      saveSessionActionBtn.disabled = true;
      saveSessionActionBtn.innerHTML =
        '<i class="fas fa-spinner fa-spin"></i> Saving...';

      const assets = [];
      for (const [fileName, asset] of clientSideAssets.entries()) {
        const arrayBuffer = await asset.file.arrayBuffer();
        assets.push({ fileName, data: arrayBuffer, type: asset.file.type });
      }

      const sessionData = {
        id: currentSessionId || Date.now(),
        name: sessionName,
        history: versionHistory,
        assets,
        savedAt: new Date().toISOString(),
      };

      await saveSessionToDB(sessionData);
      currentSessionId = sessionData.id;

      saveSessionActionBtn.innerHTML = '<i class="fas fa-check"></i> Saved!';
      setTimeout(() => {
        closePopup(saveSessionPopup);
        saveSessionActionBtn.disabled = false;
        saveSessionActionBtn.innerHTML = '<i class="fas fa-save"></i> Save';
      }, 1500);
    } catch (error) {
      console.error("Error saving session:", error);
      alert(`Failed to save session: ${error.message}`);
      saveSessionActionBtn.disabled = false;
      saveSessionActionBtn.innerHTML = '<i class="fas fa-save"></i> Save';
    }
  }

  async function loadSelectedSession() {
    const sessionId = sessionSelect.value;
    if (!sessionId) return alert("Please select a session to load.");
    try {
      const sessions = await getSessionsFromDB();
      const session = sessions.find((s) => s.id === Number(sessionId));
      if (!session) return alert("Session not found.");

      versionHistory = session.history || [];
      currentSessionId = session.id;
      sessionNameInput.value = session.name;

      clientSideAssets.forEach((asset) => URL.revokeObjectURL(asset.blobUrl));
      clientSideAssets.clear();
      if (session.assets) {
        await deserializeAssets(session.assets);
      }

      updateVersionHistoryUI();
      if (versionHistory.length > 0) {
        loadVersion(versionHistory.length - 1);
      }
      showModificationPanel();
      hasGeneratedContent = true;
      updateLayoutState();
      closePopup(loadSessionPopup);
    } catch (error) {
      console.error("Failed to load session:", error);
      alert(`Failed to load session: ${error.message}`);
    }
  }

  async function deleteSelectedSession() {
    const sessionId = sessionSelect.value;
    if (
      !sessionId ||
      !confirm(
        "Are you sure you want to delete this session? This cannot be undone."
      )
    )
      return;
    try {
      await deleteSessionFromDB(Number(sessionId));
      if (currentSessionId === Number(sessionId)) {
        startNewSession();
      }
      await populateSessionDropdown();
    } catch (error) {
      console.error("Error deleting session:", error);
      alert(`Failed to delete session: ${error.message}`);
    }
  }

  function startNewSession() {
    versionHistory = [];
    promptHistory = [];
    currentSessionId = null;
    sessionNameInput.value = "";
    promptInput.value = "";
    modificationInput.value = "";
    clientSideAssets.forEach((asset) => URL.revokeObjectURL(asset.blobUrl));
    clientSideAssets.clear();
    renderSelectedFiles(generateFileList);
    renderSelectedFiles(modifyFileList);
    if (monacoEditor) monacoEditor.setValue("");
    updateIframe("");
    clearInfoPanels();
    initialGenerationPanel.classList.remove("hidden");
    modificationPanel.classList.add("hidden");
    followupFloat.classList.remove("show");
    versionHistoryControls.classList.add("hidden");
    hasGeneratedContent = false;
    updateLayoutState();
    closePopup(loadSessionPopup);
  }

  saveSessionActionBtn.addEventListener("click", saveCurrentSession);
  loadSessionActionBtn.addEventListener("click", loadSelectedSession);
  deleteSessionBtn.addEventListener("click", deleteSelectedSession);
  newSessionBtn.addEventListener("click", startNewSession);

  // --- Popups and Mobile Menu ---
  function openPopup(popup) {
    popup.classList.remove("hidden");
  }
  function closePopup(popup) {
    popup.classList.add("hidden");
  }

  loadSessionPopupClose.addEventListener("click", () =>
    closePopup(loadSessionPopup)
  );
  saveSessionPopupClose.addEventListener("click", () =>
    closePopup(saveSessionPopup)
  );
  followupPopupClose.addEventListener("click", () => closePopup(followupPopup));

  [loadSessionPopup, saveSessionPopup, followupPopup].forEach((popup) => {
    popup.addEventListener("click", (e) => {
      if (e.target.classList.contains("popup-overlay")) closePopup(popup);
    });
  });

  document.addEventListener("keydown", (e) => {
    if (e.key === "Escape") {
      [loadSessionPopup, saveSessionPopup, followupPopup].forEach(closePopup);
    }
  });

  hamburgerBtn.addEventListener("click", () => {
    sidebar.classList.toggle("open");
    sidebarOverlay.classList.toggle("show");
  });
  sidebarOverlay.addEventListener("click", () => {
    sidebar.classList.remove("open");
    sidebarOverlay.classList.remove("show");
  });

  // --- Theme ---
  function initTheme() {
    const theme = localStorage.getItem("theme") || "dark";
    document.documentElement.setAttribute("data-theme", theme);
    updateThemeIcon(theme);
    updateMonacoTheme(theme);
  }

  function toggleTheme() {
    const current = document.documentElement.getAttribute("data-theme");
    const next = current === "dark" ? "light" : "dark";
    document.documentElement.setAttribute("data-theme", next);
    localStorage.setItem("theme", next);
    updateThemeIcon(next);
    updateMonacoTheme(next);
  }

  function updateThemeIcon(theme) {
    themeToggle.querySelector("i").className =
      theme === "dark" ? "fas fa-sun" : "fas fa-moon";
  }

  function updateMonacoTheme(theme) {
    if (monaco && monaco.editor) {
      monaco.editor.setTheme(theme === "dark" ? "vs-dark" : "vs");
    }
  }

  themeToggle.addEventListener("click", toggleTheme);

  // --- Initialization ---
  function initializeMonacoEditor() {
    if (typeof require === "undefined") {
      console.error("Monaco Editor loader not available.");
      return;
    }
    require.config({
      paths: { vs: "https://cdn.jsdelivr.net/npm/monaco-editor@0.44.0/min/vs" },
    });
    require(["vs/editor/editor.main"], () => {
      monaco = window.monaco;
      const theme = localStorage.getItem("theme") || "dark";
      monacoEditor = monaco.editor.create(monacoContainer, {
        value:
          "// AI code will appear here. Describe your idea to get started!",
        language: "html",
        theme: theme === "dark" ? "vs-dark" : "vs",
        automaticLayout: true,
        minimap: { enabled: false },
        scrollBeyondLastLine: false,
        wordWrap: "on",
      });

      monacoEditor.onDidScrollChange((e) => {
        const { scrollTop, scrollHeight, height } = e;
        autoScrollEnabled = scrollTop + height >= scrollHeight - 50;
      });
    });
  }

  async function initializeApp() {
    if (window.marked) {
      marked = window.marked;
    } else {
      console.warn("Marked.js not loaded. AI thoughts will be unformatted.");
    }
    await initDB();
    initTheme();
    initializeMonacoEditor();
    updateLayoutState();
    populateSessionDropdown();
  }

  followupBtn.addEventListener("click", () => openPopup(followupPopup));

  function showNotification(message, type) {
    // A simple alert, can be replaced with a more sophisticated notification system
    alert(`[${type.toUpperCase()}] ${message}`);
  }

  initializeApp();
});