File size: 33,819 Bytes
e163dc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9971a1
e163dc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ntfy Messenger</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/qrcode.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js"></script>
    <style>
        .message-bubble {
            max-width: 70%;
            word-wrap: break-word;
        }
        .file-preview {
            transition: all 0.3s ease;
        }
        .file-preview:hover {
            transform: scale(1.05);
        }
        .gradient-bg {
            background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
        }
        .typing-indicator span {
            display: inline-block;
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background-color: #8b5cf6;
            margin: 0 2px;
            animation: bounce 1.4s infinite ease-in-out;
        }
        .typing-indicator span:nth-child(2) {
            animation-delay: 0.2s;
        }
        .typing-indicator span:nth-child(3) {
            animation-delay: 0.4s;
        }
        @keyframes bounce {
            0%, 80%, 100% { transform: translateY(0); }
            40% { transform: translateY(-10px); }
        }
        .emoji-picker {
            position: absolute;
            bottom: 60px;
            right: 20px;
            z-index: 10;
        }
    </style>
</head>
<body class="bg-gray-100 font-sans">
    <div class="flex h-screen">
        <!-- Sidebar -->
        <div class="w-80 bg-white border-r border-gray-200 flex flex-col">
            <div class="p-4 gradient-bg text-white">
                <h1 class="text-xl font-bold">SharePulse</h1>
                <p class="text-sm opacity-80">Secure peer-to-peer messaging</p>
            </div>
            
            <div class="p-4 border-b border-gray-200">
                <div class="flex items-center space-x-2">
                    <div class="w-10 h-10 rounded-full bg-indigo-100 flex items-center justify-center">
                        <span class="text-indigo-600 font-bold" id="userInitial">U</span>
                    </div>
                    <div>
                        <p class="font-medium" id="username">User</p>
                        <p class="text-xs text-gray-500">Online</p>
                    </div>
                </div>
            </div>
            
            <div class="p-4 border-b border-gray-200">
                <h2 class="text-sm font-semibold text-gray-500 mb-2">CHANNEL CODE</h2>
                <div class="flex items-center justify-between bg-gray-50 p-2 rounded">
                    <code class="text-sm font-mono" id="channelCode">Not connected</code>
                    <button id="copyCodeBtn" class="text-indigo-600 hover:text-indigo-800">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                            <path d="M8 3a1 1 0 011-1h2a1 1 0 110 2H9a1 1 0 01-1-1z" />
                            <path d="M6 3a2 2 0 00-2 2v11a2 2 0 002 2h8a2 2 0 002-2V5a2 2 0 00-2-2 3 3 0 01-3 3H9a3 3 0 01-3-3z" />
                        </svg>
                    </button>
                </div>
                <div class="mt-3 flex space-x-2">
                    <button id="showQRBtn" class="flex-1 bg-indigo-100 text-indigo-700 py-2 px-3 rounded text-sm font-medium hover:bg-indigo-200 transition">
                        Show QR
                    </button>
                    <button id="shareBtn" class="flex-1 bg-indigo-100 text-indigo-700 py-2 px-3 rounded text-sm font-medium hover:bg-indigo-200 transition">
                        Share
                    </button>
                </div>
            </div>
            
            <div class="p-4 border-b border-gray-200">
                <h2 class="text-sm font-semibold text-gray-500 mb-2">CONNECT TO CHANNEL</h2>
                <div class="flex">
                    <input type="text" id="joinCodeInput" placeholder="Enter code" class="flex-1 border border-gray-300 rounded-l px-3 py-2 text-sm focus:outline-none focus:ring-1 focus:ring-indigo-500">
                    <button id="joinBtn" class="bg-indigo-600 text-white px-3 py-2 rounded-r text-sm font-medium hover:bg-indigo-700 transition">
                        Join
                    </button>
                </div>
            </div>
            
            <div class="p-4 border-b border-gray-200">
                <h2 class="text-sm font-semibold text-gray-500 mb-2">FILES</h2>
                <div class="space-y-2" id="fileList">
                    <p class="text-sm text-gray-500">No files shared yet</p>
                </div>
            </div>
            
            <div class="p-4 mt-auto">
                <button id="newChannelBtn" class="w-full bg-indigo-600 text-white py-2 px-3 rounded text-sm font-medium hover:bg-indigo-700 transition">
                    Create New Channel
                </button>
            </div>
        </div>
        
        <!-- Main Chat Area -->
        <div class="flex-1 flex flex-col">
            <!-- Chat Header -->
            <div class="bg-white border-b border-gray-200 p-4 flex items-center justify-between">
                <div class="flex items-center space-x-3">
                    <div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center">
                        <span class="text-purple-600 font-bold">F</span>
                    </div>
                    <div>
                        <p class="font-medium">Friend</p>
                        <div class="flex items-center">
                            <div class="typing-indicator hidden" id="typingIndicator">
                                <span></span>
                                <span></span>
                                <span></span>
                            </div>
                            <p class="text-xs text-gray-500" id="statusText">Offline</p>
                        </div>
                    </div>
                </div>
                <div class="flex items-center space-x-2">
                    <button class="p-2 text-gray-500 hover:text-gray-700 rounded-full hover:bg-gray-100">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                            <path d="M2 5a2 2 0 012-2h7a2 2 0 012 2v4a2 2 0 01-2 2H9l-3 3v-3H4a2 2 0 01-2-2V5z" />
                            <path d="M15 7v2a4 4 0 01-4 4H9.828l-1.766 1.767c.28.149.599.233.938.233h2l3 3v-3h2a2 2 0 002-2V9a2 2 0 00-2-2h-1z" />
                        </svg>
                    </button>
                    <button class="p-2 text-gray-500 hover:text-gray-700 rounded-full hover:bg-gray-100">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                            <path fill-rule="evenodd" d="M11.49 3.17c.38-1.56 2.6-1.56 2.98 0 .36 1.56 1.52 2.82 3.08 3.08 1.56.36 1.56 2.6 0 2.98-1.56.36-2.82 1.52-3.08 3.08-.38 1.56-2.6 1.56-2.98 0-.36-1.56-1.52-2.82-3.08-3.08-1.56-.36-1.56-2.6 0-2.98 1.56-.36 2.82-1.52 3.08-3.08zM7 9a1 1 0 011-1h1a1 1 0 110 2H8a1 1 0 01-1-1zm-2 0a3 3 0 013-3h1a3 3 0 013 3v1a3 3 0 01-3 3H8a3 3 0 01-3-3V9zm6.5 4.5a1.5 1.5 0 100-3 1.5 1.5 0 000 3z" clip-rule="evenodd" />
                        </svg>
                    </button>
                </div>
            </div>
            
            <!-- Messages -->
            <div class="flex-1 overflow-y-auto p-4 bg-gray-50" id="messagesContainer">
                <div class="text-center py-10 text-gray-500" id="emptyState">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-12 w-12 mx-auto text-gray-300" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z" />
                    </svg>
                    <p class="mt-2">No messages yet</p>
                    <p class="text-sm">Join or create a channel to start messaging</p>
                </div>
            </div>
            
            <!-- Message Input -->
            <div class="bg-white border-t border-gray-200 p-4">
                <div class="relative">
                    <div id="filePreview" class="mb-2 flex space-x-2 overflow-x-auto"></div>
                    <div class="flex items-end">
                        <button id="emojiBtn" class="p-2 text-gray-500 hover:text-gray-700 rounded-full hover:bg-gray-100 mr-1">
                            <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M14.828 14.828a4 4 0 01-5.656 0M9 10h.01M15 10h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                            </svg>
                        </button>
                        <button id="fileBtn" class="p-2 text-gray-500 hover:text-gray-700 rounded-full hover:bg-gray-100 mr-1">
                            <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13" />
                            </svg>
                        </button>
                        <input type="file" id="fileInput" class="hidden" multiple>
                        <div class="flex-1 relative">
                            <textarea id="messageInput" rows="1" placeholder="Type a message..." class="w-full border border-gray-300 rounded-full px-4 py-2 pr-10 focus:outline-none focus:ring-1 focus:ring-indigo-500 resize-none" style="min-height: 44px;"></textarea>
                            <button id="sendBtn" class="absolute right-2 bottom-2 p-1 text-indigo-600 hover:text-indigo-800 rounded-full">
                                <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8" />
                                </svg>
                            </button>
                        </div>
                    </div>
                    <emoji-picker class="emoji-picker"></emoji-picker>
                </div>
            </div>
        </div>
    </div>
    
    <!-- QR Code Modal -->
    <div id="qrModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden">
        <div class="bg-white rounded-lg p-6 w-80">
            <div class="flex justify-between items-center mb-4">
                <h3 class="text-lg font-medium">Share Channel</h3>
                <button id="closeQRModal" class="text-gray-500 hover:text-gray-700">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                    </svg>
                </button>
            </div>
            <div class="flex flex-col items-center">
                <div id="qrCode" class="p-4 bg-white rounded border border-gray-200 mb-4"></div>
                <div class="w-full mb-4">
                    <h4 class="text-sm font-medium text-gray-700 mb-2">Or share these words:</h4>
                    <div class="bg-gray-50 p-3 rounded text-center">
                        <p class="font-mono text-sm" id="seedWords">apple banana cherry dolphin elephant</p>
                    </div>
                </div>
                <button id="copySeedBtn" class="w-full bg-indigo-600 text-white py-2 px-3 rounded text-sm font-medium hover:bg-indigo-700 transition">
                    Copy Words
                </button>
            </div>
        </div>
    </div>
    
    <script>
        // App state
        const state = {
            channelCode: null,
            messages: [],
            files: [],
            isTyping: false,
            username: 'User' + Math.floor(Math.random() * 1000),
            friendName: 'Friend' + Math.floor(Math.random() * 1000),
            eventSource: null
        };
        
        // DOM elements
        const elements = {
            messageInput: document.getElementById('messageInput'),
            sendBtn: document.getElementById('sendBtn'),
            messagesContainer: document.getElementById('messagesContainer'),
            emptyState: document.getElementById('emptyState'),
            channelCode: document.getElementById('channelCode'),
            copyCodeBtn: document.getElementById('copyCodeBtn'),
            showQRBtn: document.getElementById('showQRBtn'),
            shareBtn: document.getElementById('shareBtn'),
            joinCodeInput: document.getElementById('joinCodeInput'),
            joinBtn: document.getElementById('joinBtn'),
            newChannelBtn: document.getElementById('newChannelBtn'),
            qrModal: document.getElementById('qrModal'),
            closeQRModal: document.getElementById('closeQRModal'),
            qrCode: document.getElementById('qrCode'),
            seedWords: document.getElementById('seedWords'),
            copySeedBtn: document.getElementById('copySeedBtn'),
            typingIndicator: document.getElementById('typingIndicator'),
            statusText: document.getElementById('statusText'),
            fileInput: document.getElementById('fileInput'),
            fileBtn: document.getElementById('fileBtn'),
            filePreview: document.getElementById('filePreview'),
            fileList: document.getElementById('fileList'),
            emojiBtn: document.getElementById('emojiBtn'),
            userInitial: document.getElementById('userInitial'),
            username: document.getElementById('username')
        };
        
        // Initialize
        function init() {
            // Set username
            elements.username.textContent = state.username;
            elements.userInitial.textContent = state.username.charAt(0).toUpperCase();
            
            // Event listeners
            elements.sendBtn.addEventListener('click', sendMessage);
            elements.messageInput.addEventListener('keypress', (e) => {
                if (e.key === 'Enter' && !e.shiftKey) {
                    e.preventDefault();
                    sendMessage();
                }
            });
            
            elements.messageInput.addEventListener('input', () => {
                if (elements.messageInput.value.trim() && !state.isTyping) {
                    state.isTyping = true;
                    simulateTyping();
                } else if (!elements.messageInput.value.trim() && state.isTyping) {
                    state.isTyping = false;
                }
            });
            
            elements.copyCodeBtn.addEventListener('click', copyChannelCode);
            elements.showQRBtn.addEventListener('click', showQRModal);
            elements.shareBtn.addEventListener('click', shareChannel);
            elements.joinBtn.addEventListener('click', joinChannel);
            elements.newChannelBtn.addEventListener('click', createNewChannel);
            elements.closeQRModal.addEventListener('click', () => elements.qrModal.classList.add('hidden'));
            elements.copySeedBtn.addEventListener('click', copySeedWords);
            elements.fileBtn.addEventListener('click', () => elements.fileInput.click());
            elements.fileInput.addEventListener('change', handleFileSelect);
            
            // Emoji picker
            const picker = document.querySelector('emoji-picker');
            picker.addEventListener('emoji-click', (event) => {
                elements.messageInput.value += event.detail.unicode;
                elements.messageInput.focus();
            });
            
            elements.emojiBtn.addEventListener('click', () => {
                picker.classList.toggle('hidden');
            });
            
            // Generate seed words
            generateSeedWords();
            
            // Create initial channel
            createNewChannel();
        }
        
        // Generate random seed words
        function generateSeedWords() {
            const words = [
                'apple', 'banana', 'cherry', 'dolphin', 'elephant',
                'flamingo', 'giraffe', 'honey', 'igloo', 'jungle',
                'koala', 'lemon', 'mango', 'narwhal', 'octopus',
                'penguin', 'quokka', 'raccoon', 'sunflower', 'tiger',
                'unicorn', 'violet', 'watermelon', 'xylophone', 'yellow', 'zebra'
            ];
            
            const selected = [];
            for (let i = 0; i < 5; i++) {
                selected.push(words[Math.floor(Math.random() * words.length)]);
            }
            
            state.seedWords = selected.join(' ');
            elements.seedWords.textContent = state.seedWords;
        }
        
        // Create new channel
        function createNewChannel() {
            // Generate random channel code
            state.channelCode = 'ntfy-' + Math.random().toString(36).substring(2, 10);
            elements.channelCode.textContent = state.channelCode;
            
            // Clear messages
            state.messages = [];
            renderMessages();
            
            // Clear files
            state.files = [];
            renderFiles();
            
            // Update status
            elements.statusText.textContent = 'Waiting for connection...';
            
            // Close any existing connection
            if (state.eventSource) {
                state.eventSource.close();
            }
            
            // Connect to ntfy
            connectToNtfy();
            
            // Generate new seed words
            generateSeedWords();
        }
        
        // Connect to ntfy
        function connectToNtfy() {
            const url = `https://ntfy.sh/${state.channelCode}/json`;
            
            state.eventSource = new EventSource(url);
            
            state.eventSource.onmessage = (e) => {
                const data = JSON.parse(e.data);
                
                if (data.event === 'message') {
                    // Check if message is from friend (not from ourselves)
                    if (data.message !== `${state.username}: ${elements.messageInput.value.trim()}`) {
                        const message = {
                            text: data.message,
                            sender: 'friend',
                            timestamp: new Date(data.time * 1000)
                        };
                        
                        state.messages.push(message);
                        renderMessages();
                        
                        // Check if message contains a file
                        if (data.message.includes('FILE:')) {
                            const fileName = data.message.split('FILE:')[1].trim();
                            if (!state.files.includes(fileName)) {
                                state.files.push(fileName);
                                renderFiles();
                            }
                        }
                    }
                }
                
                // Update status
                elements.statusText.textContent = 'Online';
                elements.typingIndicator.classList.add('hidden');
            };
            
            state.eventSource.onerror = () => {
                elements.statusText.textContent = 'Connection error';
            };
        }
        
        // Join existing channel
        function joinChannel() {
            const code = elements.joinCodeInput.value.trim();
            if (!code) return;
            
            state.channelCode = code;
            elements.channelCode.textContent = state.channelCode;
            elements.joinCodeInput.value = '';
            
            // Clear messages
            state.messages = [];
            renderMessages();
            
            // Clear files
            state.files = [];
            renderFiles();
            
            // Update status
            elements.statusText.textContent = 'Connecting...';
            
            // Close any existing connection
            if (state.eventSource) {
                state.eventSource.close();
            }
            
            // Connect to ntfy
            connectToNtfy();
        }
        
        // Send message
        function sendMessage() {
            const text = elements.messageInput.value.trim();
            const files = Array.from(elements.fileInput.files);
            
            if (!text && files.length === 0) return;
            
            // Clear files preview
            elements.filePreview.innerHTML = '';
            elements.fileInput.value = '';
            
            // Send text message if exists
            if (text) {
                const message = {
                    text: text,
                    sender: 'me',
                    timestamp: new Date()
                };
                
                state.messages.push(message);
                renderMessages();
                
                // Send to ntfy
                sendToNtfy(`${state.username}: ${text}`);
                
                elements.messageInput.value = '';
            }
            
            // Send files if exists
            files.forEach(file => {
                const fileName = `FILE: ${file.name}`;
                const message = {
                    text: fileName,
                    sender: 'me',
                    timestamp: new Date(),
                    isFile: true,
                    fileName: file.name
                };
                
                state.messages.push(message);
                renderMessages();
                
                // Send to ntfy
                sendToNtfy(`${state.username}: ${fileName}`);
                
                // Add to files list
                if (!state.files.includes(fileName)) {
                    state.files.push(fileName);
                    renderFiles();
                }
            });
            
            // Reset typing state
            state.isTyping = false;
        }
        
        // Send data to ntfy
        function sendToNtfy(message) {
            fetch(`https://ntfy.sh/${state.channelCode}`, {
                method: 'POST',
                body: message
            });
        }
        
        // Render messages
        function renderMessages() {
            if (state.messages.length === 0) {
                elements.emptyState.classList.remove('hidden');
                elements.messagesContainer.innerHTML = '';
                return;
            }
            
            elements.emptyState.classList.add('hidden');
            
            let html = '';
            state.messages.forEach((msg, index) => {
                const time = msg.timestamp.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
                
                if (msg.sender === 'me') {
                    html += `
                        <div class="flex justify-end mb-4" data-index="${index}">
                            <div class="message-bubble bg-indigo-100 text-gray-800 rounded-l-xl rounded-tr-xl px-4 py-2">
                                ${msg.isFile ? 
                                    `<div class="mb-1">
                                        <div class="flex items-center text-indigo-600">
                                            <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                                            </svg>
                                            <span class="text-xs">${msg.fileName}</span>
                                        </div>
                                    </div>` : 
                                    `<p>${msg.text.replace(`${state.username}: `, '')}</p>`
                                }
                                <p class="text-xs text-gray-500 text-right mt-1">${time}</p>
                            </div>
                        </div>
                    `;
                } else {
                    html += `
                        <div class="flex justify-start mb-4" data-index="${index}">
                            <div class="message-bubble bg-white text-gray-800 rounded-r-xl rounded-tl-xl px-4 py-2 border border-gray-200">
                                ${msg.text.includes('FILE:') ? 
                                    `<div class="mb-1">
                                        <div class="flex items-center text-purple-600">
                                            <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                                            </svg>
                                            <span class="text-xs">${msg.text.split('FILE:')[1].trim()}</span>
                                        </div>
                                    </div>` : 
                                    `<p>${msg.text.replace(`${state.friendName}: `, '')}</p>`
                                }
                                <p class="text-xs text-gray-500 mt-1">${time}</p>
                            </div>
                        </div>
                    `;
                }
            });
            
            elements.messagesContainer.innerHTML = html;
            elements.messagesContainer.scrollTop = elements.messagesContainer.scrollHeight;
        }
        
        // Render files list
        function renderFiles() {
            if (state.files.length === 0) {
                elements.fileList.innerHTML = '<p class="text-sm text-gray-500">No files shared yet</p>';
                return;
            }
            
            let html = '';
            state.files.forEach(file => {
                const fileName = file.includes('FILE:') ? file.split('FILE:')[1].trim() : file;
                html += `
                    <div class="flex items-center p-2 bg-gray-50 rounded hover:bg-gray-100">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-gray-400 mr-2" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                        </svg>
                        <span class="text-sm truncate flex-1">${fileName}</span>
                    </div>
                `;
            });
            
            elements.fileList.innerHTML = html;
        }
        
        // Handle file selection
        function handleFileSelect() {
            const files = Array.from(elements.fileInput.files);
            if (files.length === 0) return;
            
            elements.filePreview.innerHTML = '';
            
            files.forEach(file => {
                const preview = document.createElement('div');
                preview.className = 'file-preview bg-white p-2 rounded border border-gray-200 flex-shrink-0 w-24';
                
                if (file.type.startsWith('image/')) {
                    const reader = new FileReader();
                    reader.onload = (e) => {
                        preview.innerHTML = `
                            <img src="${e.target.result}" class="w-full h-16 object-cover rounded mb-1">
                            <p class="text-xs truncate">${file.name}</p>
                        `;
                    };
                    reader.readAsDataURL(file);
                } else {
                    preview.innerHTML = `
                        <div class="w-full h-16 bg-gray-100 rounded flex items-center justify-center mb-1">
                            <svg xmlns="http://www.w3.org/2000/svg" class="h-8 w-8 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                            </svg>
                        </div>
                        <p class="text-xs truncate">${file.name}</p>
                    `;
                }
                
                elements.filePreview.appendChild(preview);
            });
        }
        
        // Copy channel code
        function copyChannelCode() {
            if (!state.channelCode) return;
            navigator.clipboard.writeText(state.channelCode);
            
            // Show feedback
            const originalText = elements.copyCodeBtn.innerHTML;
            elements.copyCodeBtn.innerHTML = `
                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-green-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                </svg>
            `;
            
            setTimeout(() => {
                elements.copyCodeBtn.innerHTML = originalText;
            }, 2000);
        }
        
        // Show QR modal
        function showQRModal() {
            if (!state.channelCode) return;
            
            // Generate QR code
            QRCode.toCanvas(state.channelCode, { 
                width: 200,
                margin: 1,
                color: {
                    dark: '#000000',
                    light: '#ffffff'
                }
            }, (err, canvas) => {
                if (err) {
                    console.error(err);
                    return;
                }
                
                elements.qrCode.innerHTML = '';
                elements.qrCode.appendChild(canvas);
            });
            
            elements.qrModal.classList.remove('hidden');
        }
        
        // Share channel
        function shareChannel() {
            if (!state.channelCode) return;
            
            if (navigator.share) {
                navigator.share({
                    title: 'Join my Ntfy Messenger channel',
                    text: `Use this code to join my channel: ${state.channelCode}`,
                    url: window.location.href
                }).catch(err => {
                    console.log('Error sharing:', err);
                });
            } else {
                // Fallback for browsers that don't support Web Share API
                copyChannelCode();
                alert('Channel code copied to clipboard. Share it with your friend!');
            }
        }
        
        // Copy seed words
        function copySeedWords() {
            navigator.clipboard.writeText(state.seedWords);
            
            // Show feedback
            const originalText = elements.copySeedBtn.textContent;
            elements.copySeedBtn.textContent = 'Copied!';
            
            setTimeout(() => {
                elements.copySeedBtn.textContent = originalText;
            }, 2000);
        }
        
        // Simulate friend typing
        function simulateTyping() {
            if (!state.isTyping) return;
            
            elements.typingIndicator.classList.remove('hidden');
            elements.statusText.textContent = '';
            
            setTimeout(() => {
                if (state.isTyping) {
                    elements.typingIndicator.classList.add('hidden');
                    elements.statusText.textContent = 'Online';
                    state.isTyping = false;
                }
            }, 2000);
        }
        
        // Initialize the app
        init();
    </script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - <a href="https://enzostvs-deepsite.hf.space?remix=vs4vijay/sharepulse" style="color: #fff;text-decoration: underline;" target="_blank" >🧬 Remix</a></p></body>
</html>