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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Stack Overflow - Where Developers Learn, Share, & Build</title>
    <style>
        /* --- RESET & BASIC VARS --- */
        :root {
            --orange: #f48024;
            --black: #232629;
            --black-700: #525960;
            --blue: #0a95ff;
            --blue-dark: #0074cc;
            --bg-body: #f8f9f9;
            --white: #ffffff;
            --border: #d6d9dc;
            --shadow: 0 1px 2px rgba(0,0,0,0.05), 0 1px 4px rgba(0,0,0,0.05), 0 2px 8px rgba(0,0,0,0.05);
        }

        * { box-sizing: border-box; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
            background-color: var(--bg-body);
            color: var(--black);
            margin: 0;
            display: flex;
            flex-direction: column;
            min-height: 100vh;
        }

        /* --- HEADER --- */
        header {
            background-color: var(--bg-body);
            border-top: 3px solid var(--orange);
            box-shadow: 0 1px 2px rgba(0,0,0,0.1);
            position: fixed;
            width: 100%;
            top: 0;
            z-index: 100;
            height: 50px;
            display: flex;
            align-items: center;
            padding: 0 20px;
        }

        .logo {
            font-size: 1.2rem;
            font-weight: bold;
            margin-right: 20px;
            display: flex;
            align-items: center;
            cursor: pointer;
        }
        .logo span:first-child { margin-right: 5px; } 
        /* Stack Icon Mock */
        .icon-stack { width: 25px; height: 25px; background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 37"><path d="M26 33v-9h4v13H0V24h4v9h22Z" fill="%23BCBBBB"/><path d="M21.5 0l-2.7 2 9.9 13.3 2.7-2L21.5 0ZM26 18.4L13.3 7.8l2.1-2.5 12.7 10.6-2.1 2.5ZM9.1 15.2l15 7 1.4-3-15-7-1.4 3Zm14 10.79l.68-2.95-16.1-3.35L7 23l16.1 2.99ZM23 30H7v-3h16v3Z" fill="%23F48024"/></svg>') no-repeat center; background-size: contain; }

        .search-bar {
            flex-grow: 1;
            max-width: 700px;
            position: relative;
        }
        .search-bar input {
            width: 100%;
            padding: 8px 10px 8px 35px;
            border: 1px solid #babfc4;
            border-radius: 3px;
            font-size: 13px;
        }
        .search-icon {
            position: absolute;
            left: 10px;
            top: 50%;
            transform: translateY(-50%);
            color: #888;
        }

        .nav-links {
            margin-left: auto;
            display: flex;
            gap: 15px;
            font-size: 13px;
            color: var(--black-700);
        }
        .nav-btn {
            padding: 8px 12px;
            border-radius: 3px;
            cursor: pointer;
        }
        .nav-btn.primary { background-color: #e1ecf4; color: #39739d; }
        .nav-btn.blue { background-color: var(--blue); color: var(--white); }

        /* --- LAYOUT --- */
        .container {
            display: flex;
            max-width: 1264px;
            width: 100%;
            margin: 50px auto 0;
            flex: 1;
        }

        /* --- SIDEBAR --- */
        .sidebar {
            width: 164px;
            flex-shrink: 0;
            padding: 20px 0;
            border-right: 1px solid var(--border);
            font-size: 13px;
        }
        .sidebar ul { list-style: none; padding: 0; margin: 0; }
        .sidebar li { margin-bottom: 5px; }
        .sidebar a {
            text-decoration: none;
            color: var(--black-700);
            display: block;
            padding: 8px 10px;
        }
        .sidebar a.active {
            font-weight: bold;
            color: var(--black);
            background-color: #f1f2f3;
            border-right: 3px solid var(--orange);
        }

        /* --- CONTENT --- */
        .content {
            flex-grow: 1;
            padding: 24px;
        }
        .content-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 24px;
        }
        .content-header h1 { font-size: 27px; margin: 0; font-weight: 400; }
        .ask-btn {
            background-color: var(--blue);
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 3px;
            font-size: 13px;
            cursor: pointer;
            box-shadow: inset 0 1px 0 0 rgba(255,255,255,0.4);
        }

        .filter-bar {
            display: flex;
            justify-content: flex-end;
            margin-bottom: 15px;
            border-bottom: 1px solid var(--border);
            padding-bottom: 15px;
        }
        .btn-group { border: 1px solid var(--black-700); border-radius: 3px; display: flex; }
        .btn-group button {
            background: white;
            border: none;
            border-right: 1px solid var(--black-700);
            padding: 8px 10px;
            font-size: 12px;
            cursor: pointer;
            color: var(--black-700);
        }
        .btn-group button:last-child { border-right: none; }
        .btn-group button.active { background-color: #e3e6e8; color: var(--black); }

        /* --- QUESTIONS LIST --- */
        .question-item {
            display: flex;
            padding: 16px;
            border-bottom: 1px solid var(--border);
        }
        .stats {
            display: flex;
            flex-direction: column;
            align-items: flex-end;
            margin-right: 16px;
            font-size: 13px;
            gap: 6px;
            min-width: 60px;
            color: var(--black-700);
        }
        .stats .votes { color: var(--black); }
        .stats .answers { border: 1px solid #48a868; color: #48a868; padding: 2px 4px; border-radius: 3px; }
        .stats .answers.has-answers { background-color: #48a868; color: white; }

        .q-summary { flex-grow: 1; }
        .q-title {
            color: var(--blue-dark);
            font-size: 17px;
            margin: 0 0 5px 0;
            text-decoration: none;
            cursor: pointer;
        }
        .q-excerpt {
            font-size: 13px;
            color: #3b4045;
            margin: 0 0 8px 0;
            line-height: 1.4;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }
        .tags { display: flex; gap: 5px; margin-bottom: 10px; }
        .tag {
            background-color: #e1ecf4;
            color: #39739d;
            font-size: 12px;
            padding: 4px 6px;
            border-radius: 3px;
            text-decoration: none;
        }
        .user-card {
            display: flex;
            justify-content: flex-end;
            font-size: 12px;
            color: var(--black-700);
        }

        /* --- CHATBOT WIDGET --- */
        .chat-toggle {
            position: fixed;
            bottom: 20px;
            right: 20px;
            width: 60px;
            height: 60px;
            background-color: var(--orange);
            border-radius: 50%;
            box-shadow: 0 4px 12px rgba(0,0,0,0.2);
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
            z-index: 1000;
            transition: transform 0.2s;
        }
        .chat-toggle:hover { transform: scale(1.05); }
        .chat-toggle svg { width: 30px; height: 30px; fill: white; }

        .chat-window {
            position: fixed;
            bottom: 90px;
            right: 20px;
            width: 350px;
            height: 500px;
            background-color: white;
            border: 1px solid var(--border);
            border-radius: 8px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.15);
            display: flex;
            flex-direction: column;
            z-index: 999;
            transform-origin: bottom right;
            transform: scale(0);
            transition: transform 0.3s ease;
            overflow: hidden;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
        }
        .chat-window.open { transform: scale(1); }

        .chat-header {
            background-color: var(--orange);
            color: white;
            padding: 15px;
            font-weight: bold;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .chat-close { cursor: pointer; font-size: 1.2rem; }

        .chat-messages {
            flex-grow: 1;
            padding: 15px;
            overflow-y: auto;
            background-color: #f1f2f3;
            display: flex;
            flex-direction: column;
            gap: 10px;
        }

        .message {
            max-width: 85%;
            padding: 10px;
            border-radius: 8px;
            font-size: 14px;
            line-height: 1.4;
            word-wrap: break-word;
        }
        .message.bot {
            background-color: white;
            align-self: flex-start;
            border: 1px solid #e0e0e0;
        }
        .message.user {
            background-color: #dbeafe; /* Light blue */
            align-self: flex-end;
            color: #1e40af;
        }
        .message strong { font-weight: bold; }
        .message code {
            background-color: #f6f6f6;
            padding: 2px 4px;
            border-radius: 3px;
            font-family: monospace;
            font-size: 90%;
        }
        .message pre {
            background-color: #f6f6f6;
            padding: 10px;
            border-radius: 5px;
            overflow-x: auto;
            margin: 5px 0;
        }

        .chat-input-area {
            padding: 10px;
            border-top: 1px solid var(--border);
            display: flex;
            gap: 5px;
            background: white;
        }
        .chat-input-area input {
            flex-grow: 1;
            padding: 10px;
            border: 1px solid var(--border);
            border-radius: 4px;
            outline: none;
        }
        .chat-input-area input:focus { border-color: var(--blue); }
        .chat-send {
            background-color: var(--blue);
            color: white;
            border: none;
            padding: 0 15px;
            border-radius: 4px;
            cursor: pointer;
            font-weight: bold;
        }
        .chat-send:disabled { background-color: #ccc; cursor: not-allowed; }

        /* Loading Dots */
        .typing-indicator { font-style: italic; color: #888; font-size: 12px; margin-left: 5px; display: none; }
    </style>
</head>
<body>

    <header>
        <div class="logo">
            <div class="icon-stack"></div>
            <span>stack</span><b>overflow</b>
        </div>
        <div class="search-bar">
            <span class="search-icon">🔍</span>
            <input type="text" placeholder="Search...">
        </div>
        <div class="nav-links">
            <div class="nav-btn primary">Log in</div>
            <div class="nav-btn blue">Sign up</div>
        </div>
    </header>

    <div class="container">
        <nav class="sidebar">
            <ul>
                <li><a href="#" class="active">Home</a></li>
                <li><span style="padding: 8px 10px; color: #888; text-transform: uppercase; font-size: 11px;">Public</span></li>
                <li><a href="#">Questions</a></li>
                <li><a href="#">Tags</a></li>
                <li><a href="#">Users</a></li>
                <li><a href="#">Companies</a></li>
            </ul>
        </nav>

        <main class="content">
            <div class="content-header">
                <h1>Top Questions</h1>
                <button class="ask-btn">Ask Question</button>
            </div>

            <div class="filter-bar">
                <div class="btn-group">
                    <button class="active">Interesting</button>
                    <button>Hot</button>
                    <button>Week</button>
                    <button>Month</button>
                </div>
            </div>

            <div id="questions-list">
                <div class="question-item">
                    <div class="stats">
                        <div class="votes"><strong>12</strong> votes</div>
                        <div class="answers has-answers"><strong>4</strong> answers</div>
                        <div class="views">123 views</div>
                    </div>
                    <div class="q-summary">
                        <div class="q-title">How do I merge two dictionaries in a single expression in Python?</div>
                        <div class="q-excerpt">I want to merge two dictionaries into a new one. I have x = {'a': 1, 'b': 2} and y = {'b': 3, 'c': 4}. I want z = {'a': 1, 'b': 3, 'c': 4}. How do I do this effectively?</div>
                        <div class="tags">
                            <a href="#" class="tag">python</a>
                            <a href="#" class="tag">dictionary</a>
                            <a href="#" class="tag">merge</a>
                        </div>
                        <div class="user-card">asked 2 min ago by <strong>py_dev_99</strong></div>
                    </div>
                </div>

                <div class="question-item">
                    <div class="stats">
                        <div class="votes"><strong>5</strong> votes</div>
                        <div class="answers"><strong>0</strong> answers</div>
                        <div class="views">45 views</div>
                    </div>
                    <div class="q-summary">
                        <div class="q-title">Java Stream API - filtering and collecting a list of objects</div>
                        <div class="q-excerpt">I have a list of Person objects. I want to filter out anyone under 18 and collect their names into a List&lt;String&gt;. What is the most concise way to do this using Streams?</div>
                        <div class="tags">
                            <a href="#" class="tag">java</a>
                            <a href="#" class="tag">stream-api</a>
                            <a href="#" class="tag">lambda</a>
                        </div>
                        <div class="user-card">asked 15 min ago by <strong>java_guru</strong></div>
                    </div>
                </div>

                 <div class="question-item">
                    <div class="stats">
                        <div class="votes"><strong>0</strong> votes</div>
                        <div class="answers"><strong>0</strong> answers</div>
                        <div class="views">8 views</div>
                    </div>
                    <div class="q-summary">
                        <div class="q-title">Understanding Python's GIL (Global Interpreter Lock)</div>
                        <div class="q-excerpt">Can someone explain why Python has a GIL and how it affects multi-threaded applications? Is it possible to bypass it for CPU-bound tasks?</div>
                        <div class="tags">
                            <a href="#" class="tag">python</a>
                            <a href="#" class="tag">multithreading</a>
                            <a href="#" class="tag">gil</a>
                        </div>
                        <div class="user-card">asked 1 hour ago by <strong>concurrency_fan</strong></div>
                    </div>
                </div>
            </div>
        </main>
    </div>

    <div class="chat-toggle" id="chat-toggle" title="Ask AI Helper">
        <svg viewBox="0 0 24 24">
            <path d="M20 2H4c-1.1 0-2 .9-2 2v18l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm0 14H6l-2 2V4h16v12z"/>
        </svg>
    </div>

    <div class="chat-window" id="chat-window">
        <div class="chat-header">
            <span>Stack Overflow AI Helper</span>
            <span class="chat-close" id="chat-close">×</span>
        </div>
        <div class="chat-messages" id="chat-messages">
            <div class="message bot">
                Hello! I am an AI assistant powered by Gemini Flash 1.5. I can help you with <strong>Python</strong> and <strong>Java</strong> queries.<br><br>To get started, please paste your Gemini API Key below. (It will not be stored permanently).
            </div>
        </div>
        <div class="typing-indicator" id="typing-indicator">AI is typing...</div>
        <div class="chat-input-area">
            <input type="text" id="chat-input" placeholder="Type a message..." autocomplete="off">
            <button class="chat-send" id="chat-send">Send</button>
        </div>
    </div>

    <script>
        // --- CHAT UI LOGIC ---
        const toggleBtn = document.getElementById('chat-toggle');
        const chatWindow = document.getElementById('chat-window');
        const closeBtn = document.getElementById('chat-close');
        const messagesContainer = document.getElementById('chat-messages');
        const inputField = document.getElementById('chat-input');
        const sendBtn = document.getElementById('chat-send');
        const typingIndicator = document.getElementById('typing-indicator');

        let apiKey = null;

        toggleBtn.addEventListener('click', () => {
            chatWindow.classList.toggle('open');
            if(chatWindow.classList.contains('open')) inputField.focus();
        });

        closeBtn.addEventListener('click', () => {
            chatWindow.classList.remove('open');
        });

        // Add message to chat
        function addMessage(text, sender) {
            const div = document.createElement('div');
            div.classList.add('message', sender);
            
            // Basic formatting for bot messages
            if (sender === 'bot') {
                // Convert simple markdown code blocks (```...```) to <pre>
                let formatted = text.replace(/```([\s\S]*?)```/g, '<pre>$1</pre>');
                // Convert inline code (`...`) to <code>
                formatted = formatted.replace(/`([^`]+)`/g, '<code>$1</code>');
                // Convert bold (**...**)
                formatted = formatted.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
                div.innerHTML = formatted;
            } else {
                div.textContent = text;
            }
            
            messagesContainer.appendChild(div);
            messagesContainer.scrollTop = messagesContainer.scrollHeight;
        }

        // --- GEMINI API LOGIC ---
        async function callGemini(prompt) {
            if (!apiKey) return;

            typingIndicator.style.display = 'block';
            
            const systemInstruction = `
                You are a helpful assistant on a Stack Overflow-style website. 
                You are an expert in Python and Java.
                1. If the user asks a question about Python or Java, answer it clearly with code examples where necessary.
                2. If the user asks about other programming languages or general life topics, politely refuse and remind them you only handle Python and Java.
                3. Keep answers concise and helpful.
            `;

            const fullPrompt = systemInstruction + "\n\nUser Question: " + prompt;

            const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-latest:generateContent?key=${apiKey}`;
            
            try {
                const response = await fetch(url, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        contents: [{
                            parts: [{ text: fullPrompt }]
                        }]
                    })
                });

                const data = await response.json();
                
                if (data.error) {
                    addMessage("Error: " + data.error.message, 'bot');
                } else {
                    const text = data.candidates[0].content.parts[0].text;
                    addMessage(text, 'bot');
                }

            } catch (err) {
                addMessage("Network Error: Unable to reach Gemini API.", 'bot');
                console.error(err);
            } finally {
                typingIndicator.style.display = 'none';
            }
        }

        async function handleSend() {
            const text = inputField.value.trim();
            if (!text) return;

            // Clear input
            inputField.value = '';
            
            // If API Key is not set yet, treat this input as the key
            if (!apiKey) {
                // Basic validation (Google API keys usually start with AIza)
                if (text.startsWith('AIza') && text.length > 20) {
                    apiKey = text;
                    addMessage('API Key accepted! How can I help you with Python or Java today?', 'bot');
                } else {
                    addMessage("That doesn't look like a valid Google API Key. It should start with 'AIza'. Please try again.", 'bot');
                }
                return;
            }

            // Normal chat flow
            addMessage(text, 'user');
            await callGemini(text);
        }

        sendBtn.addEventListener('click', handleSend);
        inputField.addEventListener('keypress', (e) => {
            if (e.key === 'Enter') handleSend();
        });

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