File size: 25,292 Bytes
8a76eec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Glass Chat | iOS Style</title>
    
    <!-- Vazirmatn Font for Persian/Arabic Support -->
    <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=Vazirmatn:wght@100;300;400;500;700&display=swap" rel="stylesheet">

    <style>
        /* --- 1. BASE & RESET --- */
        :root {
            --glass-bg: rgba(255, 255, 255, 0.07);
            --glass-border: rgba(255, 255, 255, 0.15);
            --glass-highlight: rgba(255, 255, 255, 0.25);
            --primary-gradient: linear-gradient(135deg, #6366f1 0%, #a855f7 100%);
            --message-me: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
            --message-other: rgba(255, 255, 255, 0.15);
            --text-main: #ffffff;
            --text-muted: rgba(255, 255, 255, 0.7);
            --shadow-soft: 0 8px 32px 0 rgba(0, 0, 0, 0.3);
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            -webkit-tap-highlight-color: transparent;
            font-family: 'Vazirmatn', sans-serif;
        }

        body {
            height: 100vh;
            width: 100vw;
            overflow: hidden;
            background: radial-gradient(circle at 10% 20%, rgb(69, 42, 122) 0%, rgb(26, 26, 46) 90%);
            color: var(--text-main);
            display: flex;
            align-items: center;
            justify-content: center;
        }

        /* --- 2. GLASS UTILITIES --- */
        .glass-panel {
            background: var(--glass-bg);
            backdrop-filter: blur(16px);
            -webkit-backdrop-filter: blur(16px);
            border: 1px solid var(--glass-border);
            box-shadow: var(--shadow-soft);
        }

        .glass-button {
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            color: white;
            cursor: pointer;
            transition: all 0.2s ease;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .glass-button:hover {
            background: rgba(255, 255, 255, 0.2);
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(0,0,0,0.2);
        }

        .glass-button:active {
            transform: scale(0.95);
        }

        .glass-input {
            background: rgba(0, 0, 0, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.1);
            color: white;
            padding: 12px 16px;
            border-radius: 24px;
            outline: none;
            transition: all 0.3s;
            width: 100%;
        }

        .glass-input:focus {
            background: rgba(0, 0, 0, 0.4);
            border-color: rgba(255, 255, 255, 0.3);
            box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.2);
        }

        /* --- 3. LAYOUT --- */
        #app-container {
            width: 100%;
            height: 100%;
            max-width: 1400px;
            display: flex;
            position: relative;
        }

        /* Sidebar */
        #sidebar {
            width: 320px;
            height: 100%;
            display: flex;
            flex-direction: column;
            border-left: 1px solid var(--glass-border);
            z-index: 20;
            transition: transform 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
        }

        .sidebar-header {
            padding: 20px;
            border-bottom: 1px solid var(--glass-border);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .room-list {
            flex: 1;
            overflow-y: auto;
            padding: 10px;
        }

        .room-item {
            padding: 15px;
            margin-bottom: 8px;
            border-radius: 16px;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 12px;
            transition: all 0.2s;
            border: 1px solid transparent;
        }

        .room-item:hover {
            background: rgba(255, 255, 255, 0.05);
        }

        .room-item.active {
            background: rgba(255, 255, 255, 0.1);
            border-color: var(--glass-highlight);
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }

        .room-avatar {
            width: 48px;
            height: 48px;
            border-radius: 50%;
            background: var(--primary-gradient);
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            font-size: 1.2rem;
            box-shadow: 0 4px 10px rgba(0,0,0,0.2);
        }

        /* Main Chat Area */
        #main-chat {
            flex: 1;
            display: flex;
            flex-direction: column;
            position: relative;
            background: rgba(0,0,0,0.1); /* Slightly darker for depth */
        }

        .chat-header {
            height: 70px;
            padding: 0 24px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid var(--glass-border);
            background: rgba(255, 255, 255, 0.03);
        }

        .chat-messages {
            flex: 1;
            overflow-y: auto;
            padding: 24px;
            display: flex;
            flex-direction: column;
            gap: 16px;
            scroll-behavior: smooth;
        }

        /* Message Bubbles */
        .message-wrapper {
            display: flex;
            width: 100%;
            animation: messagePop 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
        }

        .message-wrapper.me {
            justify-content: flex-start; /* RTL: Me is on right visually, but flex-start in RTL context */
            flex-direction: row-reverse;
        }

        .message-wrapper.other {
            justify-content: flex-start;
        }

        .message-bubble {
            max-width: 70%;
            padding: 12px 18px;
            line-height: 1.5;
            position: relative;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            font-size: 0.95rem;
        }

        .message-bubble.me {
            background: var(--message-me);
            border-radius: 20px 20px 4px 20px;
            color: white;
        }

        .message-bubble.other {
            background: var(--message-other);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255,255,255,0.1);
            border-radius: 20px 20px 20px 4px;
            color: var(--text-main);
        }

        .message-time {
            font-size: 0.7rem;
            opacity: 0.7;
            margin-top: 6px;
            display: block;
            text-align: left;
        }

        /* Input Area */
        .chat-input-area {
            padding: 20px 24px;
            border-top: 1px solid var(--glass-border);
            display: flex;
            gap: 12px;
            align-items: center;
            background: rgba(0,0,0,0.2);
        }

        .icon-btn {
            width: 44px;
            height: 44px;
            border-radius: 50%;
            font-size: 1.2rem;
        }

        .send-btn {
            background: var(--primary-gradient);
            border: none;
            width: 48px;
            height: 48px;
            border-radius: 50%;
            color: white;
            font-size: 1.2rem;
            cursor: pointer;
            transition: transform 0.2s;
            box-shadow: 0 4px 15px rgba(168, 85, 247, 0.4);
        }

        .send-btn:hover {
            transform: scale(1.05);
        }

        .send-btn:active {
            transform: scale(0.95);
        }

        /* --- 4. MODALS & OVERLAYS --- */
        .modal-overlay {
            position: fixed;
            inset: 0;
            background: rgba(0,0,0,0.6);
            backdrop-filter: blur(5px);
            z-index: 100;
            display: flex;
            align-items: center;
            justify-content: center;
            opacity: 0;
            pointer-events: none;
            transition: opacity 0.3s;
        }

        .modal-overlay.active {
            opacity: 1;
            pointer-events: auto;
        }

        .modal-content {
            width: 90%;
            max-width: 400px;
            padding: 24px;
            border-radius: 24px;
            transform: scale(0.9);
            transition: transform 0.3s;
        }

        .modal-overlay.active .modal-content {
            transform: scale(1);
        }

        /* Sticker Picker */
        .sticker-panel {
            position: absolute;
            bottom: 90px;
            left: 24px;
            right: 24px;
            background: rgba(20, 20, 35, 0.95);
            backdrop-filter: blur(20px);
            border: 1px solid var(--glass-border);
            border-radius: 20px;
            padding: 15px;
            display: grid;
            grid-template-columns: repeat(6, 1fr);
            gap: 10px;
            max-height: 250px;
            overflow-y: auto;
            box-shadow: 0 -10px 40px rgba(0,0,0,0.5);
            opacity: 0;
            transform: translateY(20px);
            pointer-events: none;
            transition: all 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
            z-index: 50;
        }

        .sticker-panel.open {
            opacity: 1;
            transform: translateY(0);
            pointer-events: auto;
        }

        .sticker-item {
            font-size: 1.8rem;
            text-align: center;
            cursor: pointer;
            padding: 5px;
            border-radius: 8px;
            transition: background 0.2s;
        }
        .sticker-item:hover { background: rgba(255,255,255,0.1); }

        /* --- 5. ANIMATIONS --- */
        @keyframes messagePop {
            0% { opacity: 0; transform: scale(0.8) translateY(20px); }
            100% { opacity: 1; transform: scale(1) translateY(0); }
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }

        /* --- 6. RESPONSIVE --- */
        @media (max-width: 768px) {
            #sidebar {
                position: absolute;
                right: 0;
                top: 0;
                bottom: 0;
                width: 80%;
                transform: translateX(100%);
                background: rgba(20, 20, 40, 0.95); /* Solid-ish on mobile for readability */
            }
            #sidebar.open {
                transform: translateX(0);
            }
            .mobile-overlay {
                position: fixed;
                inset: 0;
                background: rgba(0,0,0,0.5);
                z-index: 15;
                opacity: 0;
                pointer-events: none;
                transition: opacity 0.3s;
            }
            .mobile-overlay.active {
                opacity: 1;
                pointer-events: auto;
            }
            .chat-header {
                padding: 0 16px;
            }
            .chat-messages {
                padding: 16px;
            }
        }

        /* Scrollbar */
        ::-webkit-scrollbar { width: 6px; }
        ::-webkit-scrollbar-track { background: transparent; }
        ::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.2); border-radius: 10px; }
        ::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.3); }

    </style>
</head>
<body>

    <div id="app-container">
        <!-- Sidebar -->
        <aside id="sidebar" class="glass-panel">
            <div class="sidebar-header">
                <h2 style="font-weight: 500; display:flex; align-items:center; gap:10px;">
                    <span style="font-size:1.5rem;">💬</span> گفتگوها
                </h2>
                <button class="glass-button icon-btn" onclick="openModal()" title="گفتگوی جدید">
                    <span>+</span>
                </button>
            </div>
            
            <div class="room-list" id="roomList">
                <!-- Rooms injected by JS -->
            </div>

            <div style="padding: 20px; border-top: 1px solid var(--glass-border);">
                <div style="display:flex; align-items:center; gap: 12px; opacity: 0.8;">
                    <div style="width: 36px; height: 36px; border-radius: 50%; background: #333; display:flex; align-items:center; justify-content:center; border: 2px solid rgba(255,255,255,0.2);">
                        👤
                    </div>
                    <div style="font-size: 0.9rem;">
                        <div style="font-weight: bold;">کاربر مهمان</div>
                        <div style="font-size: 0.75rem; opacity: 0.7;">آنلاین</div>
                    </div>
                </div>
            </div>
        </aside>

        <!-- Mobile Overlay -->
        <div id="mobileOverlay" class="mobile-overlay" onclick="toggleSidebar()"></div>

        <!-- Main Chat -->
        <main id="main-chat">
            <header class="chat-header glass-panel" style="border-top:none; border-right:none; border-left:none;">
                <div style="display:flex; align-items:center; gap: 15px;">
                    <button class="glass-button icon-btn" style="display:none;" id="menuBtn" onclick="toggleSidebar()"></button>
                    <div>
                        <h3 id="currentRoomTitle" style="font-weight: 500; margin-bottom: 2px;">عمومی</h3>
                        <span id="currentRoomStatus" style="font-size: 0.75rem; opacity: 0.7;">۱۲ عضو آنلاین</span>
                    </div>
                </div>
                <div style="display:flex; gap: 10px;">
                    <button class="glass-button icon-btn"></button>
                </div>
            </header>

            <div class="chat-messages" id="chatMessages">
                <!-- Messages injected by JS -->
            </div>

            <!-- Sticker Panel -->
            <div id="stickerPanel" class="sticker-panel">
                <div class="sticker-item" onclick="sendSticker('😂')">😂</div>
                <div class="sticker-item" onclick="sendSticker('❤️')">❤️</div>
                <div class="sticker-item" onclick="sendSticker('🔥')">🔥</div>
                <div class="sticker-item" onclick="sendSticker('👍')">👍</div>
                <div class="sticker-item" onclick="sendSticker('🎉')">🎉</div>
                <div class="sticker-item" onclick="sendSticker('🤔')">🤔</div>
                <div class="sticker-item" onclick="sendSticker('😭')">😭</div>
                <div class="sticker-item" onclick="sendSticker('😎')">😎</div>
                <div class="sticker-item" onclick="sendSticker('🥳')">🥳</div>
                <div class="sticker-item" onclick="sendSticker('🤯')">🤯</div>
                <div class="sticker-item" onclick="sendSticker('👀')">👀</div>
                <div class="sticker-item" onclick="sendSticker('🌈')">🌈</div>
            </div>

            <div class="chat-input-area">
                <button class="glass-button icon-btn" onclick="toggleStickers()" id="stickerBtn">😊</button>
                <input type="text" id="messageInput" class="glass-input" placeholder="پیام خود را بنویسید..." onkeypress="handleKeyPress(event)">
                <button class="send-btn" onclick="sendMessage()"></button>
            </div>
        </main>
    </div>

    <!-- New Room Modal -->
    <div id="newRoomModal" class="modal-overlay">
        <div class="modal-content glass-panel">
            <h3 style="margin-bottom: 20px; text-align: center;">گفتگوی جدید</h3>
            <input type="text" id="newRoomName" class="glass-input" placeholder="نام گفتگو..." style="margin-bottom: 15px;">
            <div style="display:flex; gap: 10px;">
                <button class="glass-button" style="flex:1; padding: 12px; border-radius: 12px;" onclick="closeModal()">انصراف</button>
                <button class="send-btn" style="flex:1; border-radius: 12px; width: auto;" onclick="createRoom()">ایجاد</button>
            </div>
        </div>
    </div>

    <script>
        // --- STATE MANAGEMENT ---
        const state = {
            rooms: [
                { id: 1, name: 'عمومی', avatar: '🌐', messages: [
                    { id: 1, text: 'سلام به همه! 👋', sender: 'other', time: '10:00' },
                    { id: 2, text: 'سلام، حالتون چطوره؟', sender: 'me', time: '10:05' }
                ]},
                { id: 2, name: 'طراحی UI', avatar: '🎨', messages: [
                    { id: 1, text: 'این رنگ جدید عالیه!', sender: 'other', time: '09:30' }
                ]},
                { id: 3, name: 'پروژه مخفی', avatar: '🚀', messages: []}
            ],
            activeRoomId: 1,
            showStickers: false
        };

        // --- DOM ELEMENTS ---
        const roomListEl = document.getElementById('roomList');
        const chatMessagesEl = document.getElementById('chatMessages');
        const messageInput = document.getElementById('messageInput');
        const currentRoomTitle = document.getElementById('currentRoomTitle');
        const stickerPanel = document.getElementById('stickerPanel');
        const modal = document.getElementById('newRoomModal');
        const sidebar = document.getElementById('sidebar');
        const mobileOverlay = document.getElementById('mobileOverlay');
        const menuBtn = document.getElementById('menuBtn');

        // --- INITIALIZATION ---
        function init() {
            renderRooms();
            loadRoom(state.activeRoomId);
            
            // Check mobile
            if(window.innerWidth <= 768) {
                menuBtn.style.display = 'flex';
            }
            window.addEventListener('resize', () => {
                menuBtn.style.display = window.innerWidth <= 768 ? 'flex' : 'none';
                if(window.innerWidth > 768) {
                    sidebar.classList.remove('open');
                    mobileOverlay.classList.remove('active');
                }
            });
        }

        // --- RENDER FUNCTIONS ---
        function renderRooms() {
            roomListEl.innerHTML = '';
            state.rooms.forEach(room => {
                const isActive = room.id === state.activeRoomId;
                const div = document.createElement('div');
                div.className = `room-item glass-panel ${isActive ? 'active' : ''}`;
                div.onclick = () => loadRoom(room.id);
                
                // Get last message preview
                const lastMsg = room.messages.length > 0 ? room.messages[room.messages.length-1].text : 'بدون پیام...';
                
                div.innerHTML = `
                    <div class="room-avatar">${room.avatar}</div>
                    <div style="flex: 1; overflow: hidden;">
                        <div style="display:flex; justify-content:space-between; margin-bottom: 4px;">
                            <span style="font-weight: 600;">${room.name}</span>
                            <span style="font-size: 0.7rem; opacity: 0.6;">${room.messages.length > 0 ? room.messages[room.messages.length-1].time : ''}</span>
                        </div>
                        <div style="font-size: 0.8rem; opacity: 0.7; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
                            ${lastMsg}
                        </div>
                    </div>
                `;
                roomListEl.appendChild(div);
            });
        }

        function loadRoom(id) {
            state.activeRoomId = id;
            const room = state.rooms.find(r => r.id === id);
            
            // Update Header
            currentRoomTitle.innerText = room.name;
            
            // Render Messages
            chatMessagesEl.innerHTML = '';
            room.messages.forEach(msg => appendMessageToDOM(msg));
            
            // Scroll to bottom
            scrollToBottom();
            
            // Update Sidebar UI
            renderRooms();
            
            // Close mobile sidebar if open
            if(window.innerWidth <= 768) {
                toggleSidebar();
            }
        }

        function appendMessageToDOM(msg) {
            const wrapper = document.createElement('div');
            wrapper.className = `message-wrapper ${msg.sender}`;
            
            const bubble = document.createElement('div');
            bubble.className = `message-bubble ${msg.sender}`;
            
            // Check if text is emoji only (simple check)
            const isEmoji = /^(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])+$/.test(msg.text);
            
            if(isEmoji) {
                bubble.style.fontSize = '2.5rem';
                bubble.style.background = 'transparent';
                bubble.style.boxShadow = 'none';
                bubble.style.padding = '0';
            }

            bubble.innerHTML = `
                ${msg.text}
                <span class="message-time">${msg.time}</span>
            `;
            
            wrapper.appendChild(bubble);
            chatMessagesEl.appendChild(wrapper);
            scrollToBottom();
        }

        // --- ACTIONS ---
        function sendMessage() {
            const text = messageInput.value.trim();
            if(!text) return;

            const newMsg = {
                id: Date.now(),
                text: text,
                sender: 'me',
                time: new Date().toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'})
            };

            // Add to state
            const room = state.rooms.find(r => r.id === state.activeRoomId);
            room.messages.push(newMsg);

            // Update UI
            appendMessageToDOM(newMsg);
            messageInput.value = '';
            renderRooms(); // Update preview in sidebar

            // Simulate Reply
            setTimeout(() => {
                const replyMsg = {
                    id: Date.now() + 1,
                    text: 'پیام شما دریافت شد ✅',
                    sender: 'other',
                    time: new Date().toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'})
                };
                room.messages.push(replyMsg);
                appendMessageToDOM(replyMsg);
                renderRooms();
            }, 1500);
        }

        function sendSticker(emoji) {
            messageInput.value = emoji;
            toggleStickers();
            sendMessage();
        }

        function handleKeyPress(e) {
            if(e.key === 'Enter') sendMessage();
        }

        function scrollToBottom() {
            chatMessagesEl.scrollTop = chatMessagesEl.scrollHeight;
        }

        function toggleStickers() {
            state.showStickers = !state.showStickers;
            if(state.showStickers) {
                stickerPanel.classList.add('open');
                document.getElementById('stickerBtn').style.background = 'rgba(255,255,255,0.3)';
            } else {
                stickerPanel.classList.remove('open');
                document.getElementById('stickerBtn').style.background = '';
            }
        }

        // --- MODAL LOGIC ---
        function openModal() {
            modal.classList.add('active');
            document.getElementById('newRoomName').focus();
        }

        function closeModal() {
            modal.classList.remove('active');
            document.getElementById('newRoomName').value = '';
        }

        function createRoom() {
            const name = document.getElementById('newRoomName').value.trim();
            if(name) {
                const newRoom = {
                    id: Date.now(),
                    name: name,
                    avatar: '💬',
                    messages: []
                };
                state.rooms.unshift(newRoom); // Add to top
                renderRooms();
                loadRoom(newRoom.id);
                closeModal();
            }
        }

        // --- SIDEBAR LOGIC ---
        function toggleSidebar() {
            sidebar.classList.toggle('open');
            mobileOverlay.classList.toggle('active');
        }

        // Close sticker panel when clicking outside
        document.addEventListener('click', (e) => {
            if(!e.target.closest('.chat-input-area') && !e.target.closest('.sticker-panel')) {
                stickerPanel.classList.remove('open');
                state.showStickers = false;
                document.getElementById('stickerBtn').style.background = '';
            }
        });

        // Run
        init();

    </script>
</body>
</html>