File size: 18,667 Bytes
be76c74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e188a74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be76c74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e188a74
 
 
 
 
 
be76c74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b82f276
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Legal Assistant | Judicial Deliberation System</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --bg-dark: #030617;
            --sidebar-bg: #050a24;
            --accent-gold: #fbbf24;
            --accent-navy: #1e3a8a;
            --text-primary: #f1f5f9;
            --text-dim: #94a3b8;
            --glass-bg: rgba(255, 255, 255, 0.03);
            --glass-border: rgba(255, 255, 255, 0.08);
        }

        * {
            margin: 0; padding: 0; box-sizing: border-box;
            font-family: 'Poppins', sans-serif;
        }

        body {
            background-color: var(--bg-dark);
            color: var(--text-primary);
            height: 100vh;
            display: flex;
            overflow: hidden;
        }

        /* SIDEBAR */
        .sidebar {
            width: 300px;
            background: var(--sidebar-bg);
            border-right: 1px solid var(--glass-border);
            display: flex; flex-direction: column;
            z-index: 100;
        }

        .sidebar-header { padding: 40px 25px; }

        .new-chat-btn {
            width: 100%; padding: 16px;
            background: linear-gradient(135deg, var(--accent-gold), #d97706);
            border: none; border-radius: 12px;
            color: #030617; font-weight: 700;
            display: flex; align-items: center; justify-content: center; gap: 10px;
            cursor: pointer; transition: 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
            box-shadow: 0 10px 20px rgba(251, 191, 36, 0.15);
        }

        .new-chat-btn:hover { transform: translateY(-3px); box-shadow: 0 15px 30px rgba(251, 191, 36, 0.25); }

        .history-label { padding: 0 25px 10px; font-size: 0.7rem; text-transform: uppercase; letter-spacing: 2.5px; color: var(--text-dim); font-weight: 700; opacity: 0.8; }
        .history-list { flex: 1; overflow-y: auto; padding: 15px; }
        .history-item {
            padding: 14px 18px; border-radius: 10px; margin-bottom: 8px;
            cursor: pointer; transition: 0.2s; font-size: 0.9rem; color: var(--text-dim);
            display: flex; align-items: center; gap: 12px; border: 1px solid transparent;
            white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
        }
        .history-item:hover { background: rgba(251, 191, 36, 0.05); color: var(--accent-gold); border-color: rgba(251, 191, 36, 0.2); }

        .sidebar-footer { padding: 25px; border-top: 1px solid var(--glass-border); }
        .user-badge { display: flex; align-items: center; gap: 15px; padding: 12px; background: rgba(251, 191, 36, 0.05); border-radius: 14px; border: 1px solid rgba(251, 191, 36, 0.1); }
        .user-avatar { width: 36px; height: 36px; background: var(--accent-gold); border-radius: 10px; display: flex; align-items: center; justify-content: center; font-weight: 800; color: #030617; font-size: 0.9rem; }

        /* MAIN AREA */
        .main-chat { flex: 1; display: flex; flex-direction: column; position: relative; background: radial-gradient(circle at top right, rgba(251, 191, 36, 0.02), transparent); }
        
        .top-bar {
            height: 80px; background: rgba(3, 6, 23, 0.85); backdrop-filter: blur(25px);
            border-bottom: 1px solid var(--glass-border);
            display: flex; align-items: center; justify-content: space-between; padding: 0 45px; z-index: 50;
        }

        .branding-title { font-weight: 700; color: white; letter-spacing: -0.5px; font-size: 1.1rem; }
        .status-pill { display: flex; align-items: center; gap: 10px; font-size: 0.75rem; background: rgba(251, 191, 36, 0.1); color: var(--accent-gold); padding: 5px 15px; border-radius: 20px; font-weight: 700; border: 1px solid rgba(251, 191, 36, 0.2); }
        .status-dot { width: 7px; height: 7px; background: var(--accent-gold); border-radius: 50%; animation: pulse-dot 2s infinite; }

        @keyframes pulse-dot { 0%, 100% { transform: scale(1); opacity: 1; } 50% { transform: scale(1.6); opacity: 0.3; } }

        .messages-container { flex: 1; overflow-y: auto; padding: 50px 12%; display: flex; flex-direction: column; gap: 40px; scroll-behavior: smooth; }
        .message { max-width: 900px; animation: message-slide 0.5s cubic-bezier(0.2, 0.8, 0.2, 1) forwards; }
        @keyframes message-slide { from { opacity: 0; transform: translateY(25px); } to { opacity: 1; transform: translateY(0); } }

        .user-msg { align-self: flex-end; background: linear-gradient(135deg, var(--accent-gold), #d97706); padding: 18px 26px; border-radius: 22px 22px 4px 22px; color: #030617; font-weight: 500; max-width: 70%; box-shadow: 0 10px 30px rgba(251, 191, 36, 0.2); }
        .ai-msg { align-self: flex-start; background: rgba(255, 255, 255, 0.02); border: 1px solid var(--glass-border); padding: 35px; border-radius: 24px 24px 24px 6px; max-width: 88%; line-height: 1.9; color: #e2e8f0; }
        .ai-msg h3 { color: var(--accent-gold); font-size: 1.4rem; margin-bottom: 25px; font-weight: 700; display: flex; align-items: center; gap: 15px; }
        .ai-msg ul { margin: 25px 0; list-style: none; }
        .ai-msg li { position: relative; padding-left: 32px; margin-bottom: 18px; }
        .ai-msg li::before { content: "βš–οΈ"; position: absolute; left: 0; font-size: 1rem; }
        .highlight { background: rgba(251, 191, 36, 0.05); border-left: 5px solid var(--accent-gold); padding: 25px; margin: 30px 0; border-radius: 0 18px 18px 0; font-weight: 600; color: #fff; }

        .typing {
            display: flex;
            flex-direction: column;
            gap: 12px;
            padding: 20px 30px;
            background: var(--glass-bg);
            border-radius: 24px;
            width: fit-content;
            margin: 25px 12%;
            border: 1px solid var(--glass-border);
            animation: pulse-border-gold 2s infinite ease-in-out;
        }

        .typing-text {
            font-size: 0.9rem;
            color: var(--accent-gold);
            font-weight: 700;
            letter-spacing: 1px;
            text-transform: uppercase;
        }

        .typing-dots {
            display: flex;
            gap: 8px;
        }

        .typing-dots span {
            width: 8px;
            height: 8px;
            background: var(--accent-gold);
            border-radius: 50%;
            animation: typing-blink 1.4s infinite ease-in-out;
        }

        .typing-dots span:nth-child(2) { animation-delay: 0.22s; }
        .typing-dots span:nth-child(3) { animation-delay: 0.44s; }

        @keyframes typing-blink {
            0%, 100% { opacity: 0.2; transform: scale(0.85); }
            50% { opacity: 1; transform: scale(1.15); }
        }

        @keyframes pulse-border-gold {
            0%, 100% { border-color: var(--glass-border); }
            50% { border-color: var(--accent-gold); box-shadow: 0 0 25px rgba(251, 191, 36, 0.15); }
        }

        .input-area { padding: 40px 12%; background: linear-gradient(to top, var(--bg-dark) 60%, transparent); border-top: 1px solid var(--glass-border); z-index: 60; }
        .input-box { background: rgba(255, 255, 255, 0.03); border: 1px solid var(--glass-border); border-radius: 20px; padding: 8px; display: flex; align-items: center; transition: 0.4s cubic-bezier(0.4, 0, 0.2, 1); }
        .input-box:focus-within { border-color: var(--accent-gold); box-shadow: 0 0 40px rgba(251, 191, 36, 0.1); background: rgba(255, 255, 255, 0.05); }
        .input-box input { flex: 1; background: transparent; border: none; padding: 18px 28px; color: white; font-size: 1.1rem; outline: none; }
        .send-btn { background: var(--accent-gold); color: #030617; border: none; width: 55px; height: 55px; border-radius: 16px; display: flex; align-items: center; justify-content: center; cursor: pointer; transition: 0.3s; margin-right: 8px; }
        .send-btn:hover { transform: scale(1.1) rotate(-5deg); filter: brightness(1.1); }

        .hidden { display: none !important; }
        @keyframes float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-20px); } }
    </style>
</head>
<body>
    <aside class="sidebar">
        <div class="sidebar-header">
            <button class="new-chat-btn" id="newChatBtn"><span>πŸ›οΈ</span> New Deliberation</button>
        </div>
        <div class="history-label">Case Files & Records</div>
        <div class="history-list" id="historyList"></div>
        <div class="sidebar-footer">
            <div class="user-badge">
                <div class="user-avatar" id="avatarName">JD</div>
                <div>
                    <div id="usernameLabel" style="font-size: 0.9rem; font-weight: 700;">Your Honor</div>
                    <div style="font-size: 0.75rem; color: var(--text-dim); font-weight: 500;">Judicial Authority</div>
                </div>
            </div>
            <div style="display: flex; flex-direction: column; gap: 10px; margin-top: 20px;">
                <a href="/judgedashboard.html" style="color: var(--accent-gold); text-decoration: none; font-size: 0.85rem; text-align: center; font-weight: 700; letter-spacing: 0.5px;">Return to Chambers</a>
                <a href="/role" style="color: var(--text-dim); text-decoration: none; font-size: 0.8rem; text-align: center;">← Change Role</a>
            </div>
        </div>
    </aside>

    <main class="main-chat">
        <header class="top-bar">
            <div class="system-branding">
                <span class="branding-title">πŸ›οΈ Judicial Intelligence System</span>
                <div class="status-pill"><div class="status-dot"></div>Secure Deliberation Active</div>
            </div>
            <div style="font-size: 0.85rem; color: var(--text-dim); font-weight: 600; letter-spacing: 1px;">JUDICIAL FRAMEWORK v5.0 PRO</div>
        </header>

        <div id="welcomeScreen" style="position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; padding: 40px; z-index: 1;">
            <div style="font-size: 5rem; margin-bottom: 30px; animation: float 8s ease-in-out infinite;">βš–οΈ</div>
            <h1 style="font-size: 3.2rem; font-weight: 800; margin-bottom: 15px; letter-spacing: -1.5px;">Protocol Initiated, Your Honor.</h1>
            <p style="color: var(--text-dim); max-width: 600px; line-height: 1.8; font-size: 1.2rem; font-weight: 300;">Enhanced judicial support is online.</p>
        </div>

        <div class="messages-container" id="chatContainer"></div>
        <div class="typing hidden" id="typingIndicator">
            <div class="typing-text">Law Bot is deliberating...</div>
            <div class="typing-dots">
                <span></span><span></span><span></span>
            </div>
        </div>

        <div class="input-area">
            <div class="input-box">
                <input type="text" id="messageInput" placeholder="Initiate legal inquiry..." autocomplete="off">
                <button class="send-btn" id="sendButton">
                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><line x1="22" y1="2" x2="11" y2="13"></line><polygon points="22 2 15 22 11 13 2 9 22 2"></polygon></svg>
                </button>
            </div>
            <p style="text-align: center; font-size: 0.8rem; color: var(--text-dim); margin-top: 25px; font-weight: 500; opacity: 0.7; letter-spacing: 0.5px;">πŸ”’ ENCRYPTED END-TO-END β€’ JUDICIAL PRIVACY PROTECTED</p>
        </div>
    </main>

    <script>
        let ws;
        const chatContainer = document.getElementById('chatContainer');
        const messageInput = document.getElementById('messageInput');
        const sendButton = document.getElementById('sendButton');
        const typingIndicator = document.getElementById('typingIndicator');
        const welcomeScreen = document.getElementById('welcomeScreen');
        const historyList = document.getElementById('historyList');

        let currentAiMessage = '';
        let currentAiMessageElement = null;
        let currentCaseId = crypto.randomUUID();
        const activeRole = 'Judge';

        function formatResponse(text) {
            let formatted = text;
            formatted = formatted.replace(/### (.*?)\s*(\n|$)/g, "<h3>πŸ›οΈ $1</h3>");
            formatted = formatted.replace(/^! (.*?)$/gm, '<div class="highlight">$1</div>');
            formatted = formatted.replace(/^\d+\.\s(.*?)$/gm, "<li>$1</li>");
            formatted = formatted.replace(/^\*\s(.*?)$/gm, "<li>$1</li>");
            formatted = formatted.replace(/- (.*?)$/gm, "<li>$1</li>");
            formatted = formatted.replace(/<li>(.*?)<\/li>(?!<li>)/g, "<ul><li>$1</li></ul>");
            formatted = formatted.replace(/\*\*(.*?)\*\*/g, "<strong>$1</strong>");
            formatted = formatted.replace(/\n/g, "<br>");
            return formatted;
        }

        function connectWS() {
            const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
            ws = new WebSocket(`${protocol}//${window.location.host}/conversational_chat?role=${activeRole}`);
            ws.onmessage = (event) => {
                typingIndicator.classList.add('hidden');
                welcomeScreen.classList.add('hidden');
                if (event.data === '[DONE]') {
                    saveInteraction(currentCaseId, messageInput.value, currentAiMessage);
                    return;
                }
                if (!currentAiMessageElement) {
                    currentAiMessage = event.data;
                    createNewMessage('ai', currentAiMessage);
                } else {
                    currentAiMessage += event.data;
                    currentAiMessageElement.innerHTML = formatResponse(currentAiMessage);
                }
                chatContainer.scrollTop = chatContainer.scrollHeight;
            };
            ws.onclose = () => setTimeout(connectWS, 2000);
        }

        function createNewMessage(type, content) {
            welcomeScreen.classList.add('hidden');
            const msgDiv = document.createElement('div');
            msgDiv.className = `message ${type}-msg`;
            if (type === 'ai') {
                msgDiv.innerHTML = formatResponse(content);
                currentAiMessageElement = msgDiv;
            } else {
                msgDiv.textContent = content;
            }
            chatContainer.appendChild(msgDiv);
            chatContainer.scrollTop = chatContainer.scrollHeight;
        }

        function sendMessage() {
            const text = messageInput.value.trim();
            if (!text || ws.readyState !== WebSocket.OPEN) return;
            createNewMessage('user', text);
            ws.send(text);
            messageInput.value = '';
            currentAiMessage = '';
            currentAiMessageElement = null;
            typingIndicator.classList.remove('hidden');
            chatContainer.scrollTop = chatContainer.scrollHeight;
        }

        async function saveInteraction(caseId, query, response) {
            const token = localStorage.getItem('token');
            if (!token) return;
            try {
                await fetch('/api/save-interaction', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` },
                    body: JSON.stringify({ caseId, query, response, role: activeRole })
                });
                loadHistory();
            } catch (err) { console.error(err); }
        }

        async function loadHistory() {
            const token = localStorage.getItem('token');
            if (!token) return;
            try {
                const res = await fetch(`/api/interactions?role=${activeRole}`, {
                    headers: { 'Authorization': `Bearer ${token}` }
                });
                const data = await res.json();
                historyList.innerHTML = '';
                data.forEach(item => {
                    const div = document.createElement('div');
                    div.className = 'history-item';
                    const preview = item.query.substring(0, 30) + "...";
                    div.innerHTML = `<span>πŸ“‚</span> ${preview}`;
                    div.onclick = () => loadThread(item.case_id);
                    historyList.appendChild(div);
                });
            } catch (err) { console.error(err); }
        }

        async function loadThread(caseId) {
            const token = localStorage.getItem('token');
            try {
                const res = await fetch(`/api/interactions/${caseId}?role=${activeRole}`, {
                    headers: { 'Authorization': `Bearer ${token}` }
                });
                const thread = await res.json();
                currentCaseId = caseId;
                chatContainer.innerHTML = '';
                welcomeScreen.classList.add('hidden');
                thread.forEach(msg => {
                    createNewMessage('user', msg.query);
                    createNewMessage('ai', msg.response);
                });
                currentAiMessageElement = null;
            } catch (err) { console.error(err); }
        }

        async function fetchUserStatus() {
            const token = localStorage.getItem('token');
            if (!token) return;
            try {
                const res = await fetch('/api/user-status', {
                    headers: { 'Authorization': `Bearer ${token}` }
                });
                const status = await res.json();
                document.getElementById('usernameLabel').innerText = status.username.charAt(0).toUpperCase() + status.username.slice(1);
                document.getElementById('avatarName').innerText = status.username.substring(0, 2).toUpperCase();
            } catch (err) { console.error(err); }
        }

        sendButton.onclick = sendMessage;
        messageInput.onkeypress = (e) => { if (e.key === 'Enter') sendMessage(); };
        document.getElementById('newChatBtn').onclick = () => {
            currentCaseId = crypto.randomUUID();
            chatContainer.innerHTML = '';
            welcomeScreen.classList.remove('hidden');
        };

        connectWS();
        loadHistory();
        fetchUserStatus();
    </script>
</body>
</html>