Spaces:
Running

File size: 24,214 Bytes
25c1c72
 
 
43a6614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25c1c72
43a6614
25c1c72
 
43a6614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25c1c72
43a6614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25c1c72
43a6614
 
25c1c72
43a6614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25c1c72
43a6614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25c1c72
 
 
 
43a6614
 
 
 
 
 
25c1c72
43a6614
 
25c1c72
43a6614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25c1c72
43a6614
 
 
 
 
 
25c1c72
43a6614
 
 
 
25c1c72
43a6614
25c1c72
43a6614
 
 
 
25c1c72
43a6614
 
 
 
 
 
 
 
25c1c72
43a6614
 
25c1c72
43a6614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25c1c72
43a6614
 
 
25c1c72
43a6614
25c1c72
43a6614
25c1c72
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
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>PURE_ENGINE // BI-DIRECTIONAL_NEXUS_V.6</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@300;500;700&family=Rajdhani:wght@400;600;700&display=swap" rel="stylesheet">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css">
  <style>
    :root {
      --bg-color: #050508;
      --panel-bg: #0a0a10;
      --primary-color: #00ff9d;
      --secondary-color: #ff0055;
      --accent-color: #00ccff;
      --text-dim: #8892b0;
      --code-font: 'Fira Code', monospace;
      --ui-font: 'Rajdhani', sans-serif;
      --border-glow: 0 0 10px rgba(0, 255, 157, 0.3);
      --panel-glow: 0 0 20px rgba(0, 0, 0, 0.5);
    }

    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    body {
      background-color: var(--bg-color);
      color: var(--primary-color);
      font-family: var(--ui-font);
      height: 100vh;
      overflow: hidden;
      display: flex;
      flex-direction: column;
      position: relative;
    }

    /* CRT Overlay */
    body::before {
      content: " ";
      display: block;
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      background: linear-gradient(rgba(18, 16, 16, 0) 50%, rgba(0, 0, 0, 0.1) 50%), linear-gradient(90deg, rgba(255, 0, 0, 0.03), rgba(0, 255, 0, 0.01), rgba(0, 0, 255, 0.03));
      z-index: 999;
      background-size: 100% 2px, 3px 100%;
      pointer-events: none;
    }

    /* Header */
    header {
      height: 70px;
      border-bottom: 1px solid var(--primary-color);
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 2rem;
      background: rgba(5, 5, 8, 0.95);
      backdrop-filter: blur(10px);
      z-index: 100;
      box-shadow: var(--border-glow);
    }

    .brand {
      display: flex;
      align-items: center;
      gap: 15px;
    }

    .brand h1 {
      font-size: 1.8rem;
      letter-spacing: 3px;
      text-transform: uppercase;
      font-weight: 700;
      text-shadow: var(--border-glow);
    }

    .brand span {
      color: var(--secondary-color);
      font-family: var(--code-font);
      font-size: 0.9rem;
    }

    .system-status {
      display: flex;
      gap: 20px;
      font-size: 1rem;
      font-weight: 600;
    }

    .status-dot {
      width: 10px;
      height: 10px;
      border-radius: 50%;
      background-color: var(--primary-color);
      box-shadow: var(--border-glow);
      animation: pulse 2s infinite;
    }

    /* Main Workspace */
    main {
      flex: 1;
      display: flex;
      padding: 20px;
      gap: 20px;
      overflow: hidden;
      z-index: 10;
    }

    /* Panels */
    .workspace {
      flex: 2;
      display: flex;
      flex-direction: column;
      gap: 15px;
    }

    .preview-panel {
      flex: 1;
      display: flex;
      flex-direction: column;
      gap: 15px;
    }

    .panel-container {
      flex: 1;
      background: var(--panel-bg);
      border: 1px solid var(--primary-color);
      border-radius: 4px;
      position: relative;
      box-shadow: var(--panel-glow);
      display: flex;
      flex-direction: column;
      overflow: hidden;
    }

    .panel-header {
      background: rgba(0, 255, 157, 0.1);
      padding: 10px 15px;
      border-bottom: 1px solid rgba(0, 255, 157, 0.3);
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-family: var(--code-font);
      font-size: 0.9rem;
    }

    .panel-title {
      display: flex;
      align-items: center;
      gap: 10px;
    }

    .panel-title::before {
      content: '';
      display: block;
      width: 8px;
      height: 8px;
      background: var(--primary-color);
      border-radius: 50%;
    }

    .editor-wrapper {
      flex: 1;
      position: relative;
    }

    textarea {
      width: 100%;
      height: 100%;
      background: rgba(0,0,0,0.8);
      color: var(--text-dim);
      border: none;
      padding: 20px;
      font-family: var(--code-font);
      font-size: 14px;
      line-height: 1.6;
      resize: none;
      outline: none;
      transition: background 0.3s;
    }

    textarea:focus {
      background: rgba(0,0,0,1);
    }

    textarea.python-editor {
      color: #d4d4d4;
    }

    /* Readable Preview Styling (Simulated UI) */
    .readable-preview {
      padding: 20px;
      overflow-y: auto;
      background: #0d0d12;
      font-family: var(--code-font);
      font-size: 14px;
    }

    .data-record {
      border-left: 3px solid var(--accent-color);
      background: rgba(0, 204, 255, 0.05);
      padding: 15px;
      margin-bottom: 15px;
      transition: all 0.3s ease;
    }

    .data-record:hover {
      background: rgba(0, 204, 255, 0.1);
      transform: translateX(5px);
    }

    .data-record.credit-card { border-color: var(--primary-color); }
    .data-record.email { border-color: var(--secondary-color); }
    .data-record.number { border-color: #ffcc00; }

    .record-header {
      display: flex;
      justify-content: space-between;
      margin-bottom: 10px;
      font-size: 0.8rem;
      color: var(--text-dim);
      text-transform: uppercase;
      border-bottom: 1px solid rgba(255,255,255,0.1);
      padding-bottom: 5px;
    }

    .record-content {
      color: #e0e0e0;
      white-space: pre-wrap;
      line-height: 1.5;
    }

    .highlight-val {
      color: var(--primary-color);
      font-weight: bold;
    }

    /* Controls */
    .controls {
      display: flex;
      justify-content: center;
      align-items: center;
      gap: 20px;
      padding: 20px;
      background: rgba(0,0,0,0.8);
      border-top: 1px solid #333;
    }

    .btn {
      padding: 12px 30px;
      background: transparent;
      border: 1px solid var(--primary-color);
      color: var(--primary-color);
      font-family: var(--ui-font);
      font-size: 1.2rem;
      font-weight: 700;
      text-transform: uppercase;
      cursor: pointer;
      transition: all 0.3s;
      clip-path: polygon(10% 0, 100% 0, 100% 70%, 90% 100%, 0 100%, 0 30%);
      position: relative;
      overflow: hidden;
    }

    .btn:hover {
      background: var(--primary-color);
      color: #000;
      box-shadow: var(--border-glow);
    }

    .btn-secondary {
      border-color: var(--accent-color);
      color: var(--accent-color);
    }

    .btn-secondary:hover {
      background: var(--accent-color);
      box-shadow: 0 0 15px rgba(0, 204, 255, 0.4);
    }

    .btn-danger {
      border-color: var(--secondary-color);
      color: var(--secondary-color);
    }

    .btn-danger:hover {
      background: var(--secondary-color);
      box-shadow: 0 0 15px rgba(255, 0, 85, 0.4);
    }

    /* Terminal */
    .terminal {
      height: 150px;
      background: #000;
      border-top: 1px solid var(--text-dim);
      padding: 15px;
      font-family: var(--code-font);
      font-size: 0.85rem;
      color: var(--text-dim);
      overflow-y: auto;
      display: flex;
      flex-direction: column-reverse;
    }

    .log-entry {
      margin-bottom: 5px;
    }

    .log-entry::before {
      content: "> ";
      color: var(--primary-color);
    }

    .log-error { color: var(--secondary-color); }
    .log-info { color: var(--accent-color); }
    .log-success { color: var(--primary-color); font-weight: bold; }

    /* Responsive */
    @media (max-width: 1024px) {
      main {
        flex-direction: column;
        overflow-y: auto;
      }
      .workspace, .preview-panel {
        flex: none;
        height: 500px;
      }
    }

    /* Animations */
    @keyframes pulse {
      0% { box-shadow: 0 0 0 0 rgba(0, 255, 157, 0.7); }
      70% { box-shadow: 0 0 0 10px rgba(0, 255, 157, 0); }
      100% { box-shadow: 0 0 0 0 rgba(0, 255, 157, 0); }
    }

    @keyframes glitch {
      0% { transform: translate(0); }
      20% { transform: translate(-2px, 2px); }
      40% { transform: translate(-2px, -2px); }
      60% { transform: translate(2px, 2px); }
      80% { transform: translate(2px, -2px); }
      100% { transform: translate(0); }
    }

    .glitch-effect {
      animation: glitch 0.2s cubic-bezier(.25, .46, .45, .94) both infinite;
      color: var(--secondary-color) !important;
      border-color: var(--secondary-color) !important;
    }
  </style>
</head>

<body>

  <header>
    <div class="brand">
      <h1>PURE_ENGINE</h1>
      <span>// BI-DIRECTIONAL_NEXUS_V.6</span>
    </div>
    <div class="system-status">
      <div class="status-item">SYSTEM: <span style="color:var(--primary-color)">ONLINE</span></div>
      <div class="status-item">MODE: <span style="color:var(--accent-color)">SYNCHRONIZED</span></div>
      <div class="status-item">
        <div class="status-dot"></div> 
        <span style="margin-left: 5px">NEURAL_LINK</span>
      </div>
    </div>
  </header>

  <main>
    <!-- Left Side: Python Code Generation -->
    <section class="workspace">
      <div class="panel-container">
        <div class="panel-header">
          <div class="panel-title">PYTHON_SCRIPT_GENERATOR</div>
          <div style="color: var(--text-dim);">READ/WRITE_PERMISSION: GRANTED</div>
        </div>
        <div class="editor-wrapper">
          <textarea id="pythonCode" spellcheck="false" placeholder="# Paste raw data here to generate Python structure...
# Or edit this code to update the readable output.
# Example:
# data_list = ['item1', 'item2']
# parsed_output = { 'id': 1, 'value': 'data' }"></textarea>
        </div>
      </div>
      
      <div class="controls">
        <button class="btn btn-danger" id="clearBtn">PURGE_DATA</button>
        <button class="btn" id="generateBtn">INITIATE_CONVERSION</button>
        <button class="btn btn-secondary" id="syncBtn">FORCE_SYNC</button>
      </div>
    </section>

    <!-- Right Side: Readable Dashboard -->
    <section class="preview-panel">
      <div class="panel-container" style="border-color: var(--accent-color);">
        <div class="panel-header">
          <div class="panel-title">HUMAN_READABLE_DASHBOARD</div>
          <div style="color: var(--text-dim);">LIVE_UPDATE: ACTIVE</div>
        </div>
        <div id="readableContent" class="readable-preview">
          <div style="text-align: center; padding: 50px; color: var(--text-dim);">
            <p>AWAITING DATA STREAM...</p>
            <p style="font-size: 0.8rem; margin-top: 10px;">
              [ SYSTEM READY. INPUT RAW TEXT OR PYTHON CODE TO BEGIN DECONSTRUCTION. ]
            </p>
          </div>
        </div>
      </div>

      <div class="terminal" id="terminalLog">
        <div class="log-entry">> SYSTEM_INIT: PURE_ENGINE_SINGULARITY...</div>
        <div class="log-entry">> MODULES_LOADED: [PYTHON_PARSER, PATTERN_MATCHER, UI_RENDER]</div>
        <div class="log-entry log-success">> STATUS: SYNCHRONIZATION ESTABLISHED</div>
      </div>
    </section>
  </main>

  <script>
    // --- CORE ENGINE LOGIC ---

    const terminal = document.getElementById('terminalLog');
    const readableContent = document.getElementById('readableContent');
    const pythonCodeArea = document.getElementById('pythonCode');
    const generateBtn = document.getElementById('generateBtn');
    const clearBtn = document.getElementById('clearBtn');
    const syncBtn = document.getElementById('syncBtn');

    // Helper: Log to Terminal
    function log(msg, type = 'info') {
        const div = document.createElement('div');
        div.className = 'log-entry ' + (type === 'error' ? 'log-error' : type === 'success' ? 'log-success' : 'log-info');
        
        const timestamp = new Date().toLocaleTimeString('en-US', { hour12: false });
        div.innerText = `[${timestamp}] ${msg}`;
        
        // Insert before the last child (which is effectively the top due to flex-reverse logic if we appended, 
        // but here we want prepend for "newest at bottom" visually in flex-direction: column-reverse)
        // Actually, with flex-direction: column-reverse, the last child is at the bottom visually.
        // So we want to append to the DOM to put it at the bottom visually? 
        // Let's just prepend for "newest at top" standard terminal style or append for newest at bottom.
        // Let's use prepend for "Newest at Top" (like a chat).
        terminal.prepend(div);
    }

    // Helper: Create HTML for Readable Card
    function createReadableCard(type, title, contentHTML) {
        const card = document.createElement('div');
        card.className = `data-record ${type}`;
        
        let typeLabel = "UNKNOWN_DATA";
        if(type === 'credit-card') typeLabel = "FINANCIAL_TRANSACTION";
        if(type === 'email') typeLabel = "USER_COMMUNICATION";
        if(type === 'number') typeLabel = "STRUCTURED_IDENTIFIER";
        if(type === 'text') typeLabel = "DECODED_TEXTURE";

        card.innerHTML = `
            <div class="record-header">
                <span>${typeLabel}</span>
                <span style="font-family:var(--code-font)">ID:${Math.floor(Math.random() * 9000) + 1000}</span>
            </div>
            <div class="record-content">
                ${contentHTML}
            </div>
        `;
        return card;
    }

    // --- PATTERN RECOGNITION & PYTHON GENERATION ---

    function generatePythonCodeFromText(text) {
        if (!text || text.trim().length === 0) return "";

        let pythonCode = "# Generated Data Structure from PURE_ENGINE\n";
        pythonCode += "raw_input_text = '''" + text.replace(/'/g, "\\'") + "'''\n\n";
        pythonCode += "parsed_data = {\n    'source': 'user_input_stream',\n    'records': [\n";

        let recordsAdded = 0;

        // 1. Credit Card Detection
        const ccRegex = /(?:\d[ -]*?){13,19}/g;
        const ccMatches = text.match(ccRegex);
        
        if (ccMatches) {
            log(`DETECTED_CREDIT_CARDS: ${ccMatches.length} FOUND`, 'info');
            ccMatches.forEach((match, index) => {
                const clean = match.replace(/\D/g, '');
                const groups = clean.match(/.{1,4}/g);
                const formatted = groups ? groups.join(' ') : clean;
                
                pythonCode += `        {\n`;
                pythonCode += `            'type': 'credit_card',\n`;
                pythonCode += `            'masked_number': '${"**** **** **** " + clean.slice(-4)}',\n`;
                pythonCode += `            'raw_value': '${formatted}',\n`;
                pythonCode += `            'luhn_valid': True,\n`;
                pythonCode += `            'bin': '${clean.slice(0,6)}'\n`;
                pythonCode += `        },\n`;
                recordsAdded++;
            });
        }

        // 2. Email Detection
        const emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g;
        const emailMatches = text.match(emailRegex);

        if (emailMatches) {
            log(`DETECTED_EMAILS: ${emailMatches.length} FOUND`, 'info');
            emailMatches.forEach((email, index) => {
                const parts = email.split('@');
                pythonCode += `        {\n`;
                pythonCode += `            'type': 'email',\n`;
                pythonCode += `            'address': '${email}',\n`;
                pythonCode += `            'user': '${parts[0]}',\n`;
                pythonCode += `            'domain': '${parts[1]}',\n`;
                pythonCode += `            'provider': '${parts[1].split('.')[0]}'\n`;
                pythonCode += `        },\n`;
                recordsAdded++;
            });
        }

        // 3. Numbers / IDs
        const numberRegex = /\b\d{5,}\b/g;
        const numberMatches = text.match(numberRegex);
        
        // Filter out numbers that are part of CC numbers (simple check: if they are in a long sequence)
        // For simplicity in this demo, we show standalone long numbers
        if (numberMatches) {
             // Filter to avoid duplicates of CC numbers
             const uniqueNumbers = [...new Set(numberMatches.filter(n => !ccMatches || !ccMatches.some(c => c.includes(n))))];
             
             if (uniqueNumbers.length > 0) {
                log(`DETECTED_IDS/STREAMS: ${uniqueNumbers.length} FOUND`, 'info');
                uniqueNumbers.forEach((num, index) => {
                    let type = "INTEGER";
                    if (num.length === 9) type = "SSN/LINK_ID";
                    else if (num.length > 10) type = "UNIVERSAL_ID";
                    
                    pythonCode += `        {\n`;
                    pythonCode += `            'type': 'identifier',\n`;
                    pythonCode += `            'value': ${num},\n`;
                    pythonCode += `            'classification': '${type}'\n`;
                    pythonCode += `        },\n`;
                    recordsAdded++;
                });
             }
        }

        // 4. General Text Extraction
        const textBlocks = text.match(/[a-zA-Z0-9\s,.'"]{50,}/g);
        if (textBlocks && textBlocks.length > 0) {
            log("DETECTED_TEXTURE_BLOCKS...", 'info');
            // Just take the first block for the demo to avoid clutter
            const block = textBlocks[0].trim();
            // Escape for Python
            const safeBlock = block.replace(/'/g, "\\'");
            
            pythonCode += `        {\n`;
            pythonCode += `            'type': 'text_fragment',\n`;
            pythonCode += `            'content': '${safeBlock}',\n`;
            pythonCode += `            'length': ${block.length}\n`;
            pythonCode += `        },\n`;
            recordsAdded++;
        }

        pythonCode += "    ]\n";
        pythonCode += "}\n\n";
        pythonCode += "print(f'Processed {recordsAdded} records successfully.')";

        return pythonCode;
    }

    // Function to Parse Python Code and Render UI
    function renderUIFromPython(code) {
        readableContent.innerHTML = ''; // Clear current

        // Simple heuristic parsing for the UI (Not a full Python parser, but works for the generated format)
        // We look for patterns like 'type': '...' and 'value': ...
        
        // 1. Find all 'type' declarations
        const typeRegex = /'type':\s*'([^']+)'/g;
        const types = [...code.matchAll(typeRegex)];
        
        // 2. Extract values based on type
        types.forEach((match, index) => {
            const type = match[1];
            const start = match.index;
            
            // Extract content until the next record or end of records array
            const nextRecordStart = code.indexOf("},", start);
            const recordBlock = code.substring(start, nextRecordStart !== -1 ? nextRecordStart + 2 : code.length);
            
            let contentHTML = "";
            
            if (type === 'credit_card') {
                const numMatch = recordBlock.match(/'masked_number':\s*'([^']+)'/);
                const rawMatch = recordBlock.match(/'raw_value':\s*'([^']+)'/);
                contentHTML = `<div>STATUS: <span class="highlight-val">VALIDATED</span></div>`;
                contentHTML += `<div>CARD: ${numMatch ? numMatch[1] : 'N/A'}</div>`;
                contentHTML += `<div>FULL: ${rawMatch ? rawMatch[1] : 'N/A'}</div>`;
                readableContent.appendChild(createReadableCard('credit-card', "Credit Card Node", contentHTML));
            } 
            else if (type === 'email') {
                const addrMatch = recordBlock.match(/'address':\s*'([^']+)'/);
                const domMatch = recordBlock.match(/'domain':\s*'([^']+)'/);
                contentHTML = `<div>TO: <span class="highlight-val">${addrMatch ? addrMatch[1] : 'N/A'}</span></div>`;
                contentHTML += `<div>DOMAIN: ${domMatch ? domMatch[1] : 'N/A'}</div>`;
                contentHTML += `<div>SECURITY: ENCRYPTED</div>`;
                readableContent.appendChild(createReadableCard('email', "Email Node", contentHTML));
            }
            else if (type === 'identifier') {
                const valMatch = recordBlock.match(/'value':\s*(\d+)/);
                const classMatch = recordBlock.match(/'classification':\s*'([^']+)'/);
                contentHTML = `<div>ID_CLASS: ${classMatch ? classMatch[1] : 'UNKNOWN'}</div>`;
                contentHTML += `<div>VALUE: <span class="highlight-val">${valMatch ? valMatch[1] : 'N/A'}</span></div>`;
                readableContent.appendChild(createReadableCard('number', "Data Identifier", contentHTML));
            }
            else if (type === 'text_fragment') {
                const contentMatch = recordBlock.match(/'content':\s*'([^']+)'/);
                contentHTML = `<div style="white-space: pre-wrap; color: #ccc;">"${contentMatch ? contentMatch[1] : ''}"</div>`;
                readableContent.appendChild(createReadableCard('text', "Text Fragment", contentHTML));
            }
        });

        if (readableContent.children.length === 0) {
            readableContent.innerHTML = '<div style="text-align: center; padding: 50px; color: var(--text-dim);"><p>NO STRUCTURED DATA DETECTED IN CODE</p><p>EDIT PYTHON CODE TO ADD RECORDS</p></div>';
        }
    }

    // Main Processor
    function processStream() {
        const input = pythonCodeArea.value;
        
        if (input.trim().length === 0) {
            log("ERROR: EMPTY_INPUT_STREAM", "error");
            return;
        }

        // Check if input looks like Python code or raw text
        const looksLikePython = input.includes("def ") || input.includes("import ") || input.includes("'type'");

        if (looksLikePython) {
            log("DETECTING_PYTHON_STRUCTURE...", "info");
            renderUIFromPython(input);
            log("UI_RENDER_COMPLETE: SYNCHRONIZED", "success");
        } else {
            log("CONVERTING_RAW_TEXT_TO_PYTHON...", "info");
            const generatedCode = generatePythonCodeFromText(input);
            pythonCodeArea.value = generatedCode;
            
            // Trigger UI render after a slight delay to allow DOM update
            setTimeout(() => {
                renderUIFromPython(generatedCode);
                log("PYTHON_GENERATION_COMPLETE", "success");
            }, 100);
        }
    }

    // --- EVENT LISTENERS ---

    generateBtn.addEventListener('click', () => {
        // Visual Glitch Effect
        generateBtn.classList.add('glitch-effect');
        setTimeout(() => generateBtn.classList.remove('glitch-effect'), 300);

        processStream();
    });

    clearBtn.addEventListener('click', () => {
        pythonCodeArea.value = '';
        readableContent.innerHTML = '<div style="text-align: center; padding: 50px; color: var(--text-dim);"><p>AWAITING DATA STREAM...</p><p style="font-size: 0.8rem; margin-top: 10px;">[ SYSTEM PURGED. READY. ]</p></div>';
        terminal.innerHTML = '';
        log("SYSTEM PURGED. READY FOR NEW_INPUT.", "info");
    });

    syncBtn.addEventListener('click', () => {
        // If user manually edited the Python code, force re-render UI
        log("MANUAL_SYNC_INITIATED...", "info");
        renderUIFromPython(pythonCodeArea.value);
        log("UI_REFRESHED.", "success");
    });

    // Auto-update UI if user types in the Python editor
    let autoSaveTimer;
    pythonCodeArea.addEventListener('input', () => {
        clearTimeout(autoSaveTimer);
        autoSaveTimer = setTimeout(() => {
            // Only auto-render if it looks like python to avoid errors on typing
            if (pythonCodeArea.value.includes("'type'")) {
                renderUIFromPython(pythonCodeArea.value);
            }
        }, 1000);
    });

    // Initialize
    hljs.highlightAll();
    log("CONNECTION_ESTABLISHED. AWAITING_COMMAND.");

  </script>
</body>

</html>