File size: 26,485 Bytes
056d297
 
 
 
 
ec5ee9a
056d297
 
7a87633
056d297
 
7a87633
 
 
 
 
 
 
 
 
 
 
 
 
 
 
056d297
 
7a87633
 
056d297
7a87633
056d297
 
7a87633
 
056d297
 
 
7a87633
 
 
056d297
7a87633
 
056d297
 
 
 
7a87633
056d297
7a87633
 
056d297
7a87633
 
 
056d297
 
 
 
 
 
 
 
 
7a87633
056d297
 
7a87633
 
 
 
056d297
7a87633
056d297
 
7a87633
056d297
7a87633
 
 
 
 
 
 
056d297
7a87633
 
 
 
 
 
 
 
056d297
ca87d12
7a87633
 
 
 
 
 
 
 
 
 
ca87d12
056d297
 
 
7a87633
 
 
 
 
 
 
056d297
 
7a87633
 
056d297
 
7a87633
 
 
 
 
f937534
 
 
 
 
 
056d297
7a87633
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
056d297
7a87633
 
056d297
 
7a87633
 
 
 
 
 
 
 
 
056d297
 
7a87633
 
 
 
 
 
056d297
 
 
7a87633
 
 
056d297
 
7a87633
 
056d297
 
 
 
 
 
7a87633
056d297
7a87633
 
056d297
 
7a87633
 
056d297
7a87633
056d297
 
 
 
 
7a87633
056d297
7a87633
 
 
056d297
7a87633
 
056d297
7a87633
056d297
 
 
7a87633
056d297
 
 
7a87633
056d297
 
7a87633
056d297
 
7a87633
056d297
 
 
 
 
 
 
7a87633
 
056d297
7a87633
 
 
056d297
7a87633
 
056d297
 
7a87633
 
 
 
056d297
 
7a87633
 
 
 
 
 
056d297
 
7a87633
 
 
 
 
 
 
 
 
 
 
 
 
f937534
7a87633
 
 
 
 
 
 
 
056d297
7a87633
056d297
7a87633
 
 
 
 
056d297
 
 
7a87633
056d297
7a87633
 
056d297
 
7a87633
056d297
 
7a87633
056d297
 
7a87633
 
 
 
 
056d297
7a87633
056d297
7a87633
 
056d297
 
7a87633
 
056d297
 
 
7a87633
056d297
 
 
 
 
7a87633
 
 
ec5ee9a
7a87633
 
 
 
056d297
7a87633
 
 
 
ca87d12
7a87633
ca87d12
 
 
 
 
 
 
056d297
 
7a87633
 
 
 
 
 
 
056d297
7a87633
 
 
 
 
 
 
 
056d297
 
7a87633
 
 
 
 
 
056d297
7a87633
 
 
 
 
 
 
056d297
7a87633
 
 
 
 
 
 
056d297
 
 
 
 
 
 
 
 
 
 
7a87633
 
056d297
 
 
 
 
 
 
 
 
7a87633
056d297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a87633
056d297
 
 
 
7a87633
 
056d297
7a87633
056d297
7a87633
056d297
 
 
 
7a87633
056d297
 
 
 
 
 
 
 
 
7a87633
056d297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a87633
 
 
 
056d297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a87633
056d297
 
7a87633
056d297
 
 
7a87633
056d297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a87633
056d297
7a87633
056d297
 
 
 
 
 
 
 
 
 
7a87633
 
 
 
 
056d297
 
 
 
7a87633
056d297
 
7a87633
056d297
 
7a87633
056d297
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cv3inx - Web Terminal</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@5.3.0/css/xterm.css">
    <style>
        :root {
            --primary: #10b981;
            --primary-dark: #059669;
            --bg: #0c0c0c;
            --bg-card: #18181b;
            --bg-header: #23232a;
            --border: #27272a;
            --border-light: #3f3f46;
            --text: #e4e4e7;
            --text-muted: #a1a1aa;
            --danger: #ef4444;
            --shadow: 0 4px 24px rgba(0,0,0,0.25);
        }
        * { box-sizing: border-box; margin: 0; padding: 0; }
        html, body { height: 100%; }
        body {
            font-family: 'JetBrains Mono', monospace;
            background: var(--bg);
            color: var(--text);
            min-height: 100vh;
            overflow-x: hidden;
        }
        .header {
            background: var(--bg-header);
            border-bottom: 1px solid var(--border-light);
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 20px;
            height: 56px;
            position: sticky;
            top: 0;
            z-index: 10;
            box-shadow: 0 2px 8px rgba(0,0,0,0.08);
        }
        .logo {
            display: flex;
            align-items: center;
            gap: 10px;
            font-weight: 600;
            font-size: 18px;
            letter-spacing: 1px;
        }
        .logo-dot {
            width: 10px; height: 10px;
            background: var(--primary);
            border-radius: 50%;
            animation: pulse 2s infinite;
        }
        @keyframes pulse {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.3; }
        }
        .header-actions {
            display: flex;
            gap: 10px;
        }
        .btn {
            background: rgba(255,255,255,0.05);
            border: 1px solid var(--border-light);
            color: var(--text);
            padding: 6px 14px;
            border-radius: 6px;
            font-size: 13px;
            cursor: pointer;
            font-family: inherit;
            transition: all 0.2s;
        }
        .btn:hover { background: rgba(16,185,129,0.08); border-color: var(--primary); }
        .btn.danger:hover { background: rgba(239,68,68,0.15); border-color: var(--danger); }
        .main {
            display: flex;
            flex-direction: row;
            height: calc(100vh - 56px);
            background: var(--border);
        }
        .terminal-panel {
            flex: 2;
            display: flex;
            flex-direction: column;
            justify-content: stretch;
            background: var(--bg-card);
            border-right: 1px solid var(--border-light);
            min-width: 0;
        }
        .terminal-card {
            background: var(--bg-card);
            border-radius: 18px;
            box-shadow: var(--shadow);
            border: 1px solid var(--border);
            margin: 24px;
            margin-bottom: 0;
            display: flex;
            flex-direction: column;
            flex: 1;
            min-width: 0;
        }
        .terminal-header {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 14px 20px;
            border-bottom: 1px solid var(--border-light);
            font-size: 15px;
            font-weight: 600;
            background: rgba(39,39,42,0.92);
            border-radius: 18px 18px 0 0;
        }
        .terminal-icon {
            color: var(--primary);
            font-size: 18px;
        }
        #terminal {
            flex: 1;
            padding: 16px;
            background: var(--bg);
            border-radius: 0 0 18px 18px;
            min-width: 0;
            min-height: 180px;
            max-height: 60vh;
            height: 40vh;
            transition: height 0.2s;
            box-sizing: border-box;
            overflow: auto;
        }
        .connection-status {
            position: absolute;
            top: 18px;
            right: 36px;
            font-size: 12px;
            color: var(--text-muted);
            display: flex;
            align-items: center;
            gap: 6px;
        }
        .connection-dot {
            width: 8px; height: 8px;
            border-radius: 50%;
            background: var(--danger);
            transition: background-color 0.3s;
        }
        .connection-dot.connected { background: var(--primary); }
        /* Sidebar */
        .sidebar {
            flex: 1;
            background: var(--bg-card);
            display: flex;
            flex-direction: column;
            border-left: 1px solid var(--border-light);
            min-width: 0;
            max-width: 370px;
            transition: transform 0.3s;
        }
        .sidebar-inner {
            padding: 24px 18px 18px 18px;
            overflow-y: auto;
            height: 100%;
        }
        .sidebar-section {
            background: rgba(24,24,27,0.7);
            border-radius: 14px;
            border: 1px solid var(--border-light);
            margin-bottom: 18px;
            padding: 18px 16px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.08);
        }
        .section-title {
            font-size: 13px;
            font-weight: 700;
            color: var(--primary);
            margin-bottom: 10px;
            letter-spacing: 0.5px;
            display: flex;
            align-items: center;
            gap: 7px;
        }
        .info-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 12px;
            margin-bottom: 7px;
        }
        .info-label { color: var(--text-muted); }
        .info-value { color: var(--text); font-weight: 500; }
        .metric-bar {
            width: 100%;
            height: 5px;
            background: rgba(255,255,255,0.07);
            border-radius: 2px;
            margin-bottom: 10px;
            overflow: hidden;
        }
        .metric-fill {
            height: 100%;
            border-radius: 2px;
            transition: width 0.3s;
        }
        .cpu-fill { background: linear-gradient(90deg, #10b981, #059669); }
        .memory-fill { background: linear-gradient(90deg, #3b82f6, #2563eb); }
        .disk-fill { background: linear-gradient(90deg, #f59e0b, #d97706); }
        .mini-chart {
            height: 48px;
            background: rgba(255,255,255,0.03);
            border-radius: 8px;
            margin-top: 10px;
            display: flex;
            align-items: end;
            gap: 1px;
            padding: 3px;
        }
        .chart-bar {
            flex: 1;
            background: rgba(16,185,129,0.3);
            border-radius: 1px 1px 0 0;
            min-height: 2px;
            transition: height 0.3s;
        }
        .processes {
            max-height: 120px;
            overflow-y: auto;
        }
        .process-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 11px;
            padding: 4px 0;
            border-bottom: 1px solid rgba(255,255,255,0.05);
        }
        .process-item:last-child { border-bottom: none; }
        .process-name { color: var(--text); flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
        .process-cpu { color: var(--primary); font-weight: 500; margin-left: 8px; }
        /* Mobile Styles */
        @media (max-width: 900px) {
            .main { flex-direction: column; }
            .sidebar {
                position: fixed;
                left: 0; right: 0; bottom: 0;
                top: unset;
                max-width: 100vw;
                height: 70vh;
                z-index: 1001;
                transform: translateY(100%);
                box-shadow: 0 -10px 40px rgba(0,0,0,0.6);
                border-radius: 18px 18px 0 0;
                border-left: none;
                border-top: 2px solid var(--border-light);
                background: var(--bg-card);
                transition: transform 0.3s;
                display: flex;
            }
            .sidebar.show { transform: translateY(0); }
            .sidebar-inner { padding: 18px 8px 8px 8px; }
            .terminal-panel { border-right: none; }
            .terminal-card { margin: 12px 6px 0 6px; border-radius: 14px; }
            .terminal-header { border-radius: 14px 14px 0 0; padding: 10px 14px; }
            #terminal { border-radius: 0 0 14px 14px; padding: 10px; }
            .connection-status { right: 16px; top: 10px; font-size: 11px; }
        }
        @media (max-width: 600px) {
            .header { height: 48px; padding: 0 10px; font-size: 14px; }
            .logo { font-size: 14px; }
            .terminal-card { margin: 8px 2px 0 2px; border-radius: 10px; }
            .terminal-header { border-radius: 10px 10px 0 0; padding: 8px 8px; }
            #terminal { border-radius: 0 0 10px 10px; padding: 6px; min-height: 90px; height: 28vh; max-height: 35vh; }
            .sidebar { border-radius: 10px 10px 0 0; }
            .sidebar-inner { padding: 8px 2px 2px 2px; }
        }
        /* Mobile toggle button */
        .mobile-toggle {
            display: none;
        }
        @media (max-width: 900px) {
            .mobile-toggle {
                display: flex;
                position: fixed;
                bottom: 18px;
                right: 18px;
                width: 54px;
                height: 54px;
                background: linear-gradient(135deg, var(--primary), var(--primary-dark));
                border: none;
                border-radius: 50%;
                color: white;
                font-size: 26px;
                cursor: pointer;
                z-index: 1100;
                box-shadow: 0 8px 25px rgba(16,185,129,0.4);
                align-items: center;
                justify-content: center;
                transition: all 0.3s;
            }
            .mobile-toggle.active {
                background: linear-gradient(135deg, var(--danger), #dc2626);
                transform: rotate(45deg);
            }
        }
        .mobile-overlay {
            display: none;
        }
        @media (max-width: 900px) {
            .mobile-overlay {
                display: block;
                position: fixed;
                top: 0; left: 0; right: 0; bottom: 0;
                background: rgba(0,0,0,0.5);
                z-index: 1000;
                opacity: 0;
                pointer-events: none;
                transition: opacity 0.3s;
            }
            .mobile-overlay.show {
                opacity: 1;
                pointer-events: all;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="logo">
            <div class="logo-dot"></div>
            Cv3inx - Terminal
        </div>
        <div class="header-actions">
            <button class="btn" id="clear-btn">Clear</button>
            <a href="/logout" class="btn danger">Logout</a>
        </div>
    </div>
    <div class="main">
        <div class="terminal-panel">
            <div class="terminal-card" style="position:relative;">
                <div class="terminal-header">
                    <span class="terminal-icon">πŸ–₯️</span>
                    Shell Session
                </div>
                <div id="terminal"></div>
                <div class="connection-status">
                    <div class="connection-dot" id="connection-dot"></div>
                    <span id="connection-text">Connecting...</span>
                </div>
            </div>
        </div>
        <div class="sidebar" id="sidebar">
            <div class="sidebar-inner">
                <div class="sidebar-section">
                    <div class="section-title">System Overview</div>
                    <div class="info-item"><span class="info-label">πŸ–₯️ Host</span><span class="info-value" id="hostname">-</span></div>
                    <div class="info-item"><span class="info-label">⏱️ Uptime</span><span class="info-value" id="uptime">-</span></div>
                    <div class="info-item"><span class="info-label">πŸ‘₯ Users</span><span class="info-value" id="users">-</span></div>
                </div>
                <div class="sidebar-section">
                    <div class="section-title">Performance</div>
                    <div class="info-item"><span class="info-label">🧠 CPU</span><span class="info-value" id="cpu-usage">0%</span></div>
                    <div class="metric-bar"><div class="metric-fill cpu-fill" id="cpu-bar" style="width:0%"></div></div>
                    <div class="info-item"><span class="info-label">πŸ’Ύ Memory</span><span class="info-value" id="memory-usage">0%</span></div>
                    <div class="metric-bar"><div class="metric-fill memory-fill" id="memory-bar" style="width:0%"></div></div>
                    <div class="info-item"><span class="info-label">πŸ’Ώ Disk</span><span class="info-value" id="disk-usage">0%</span></div>
                    <div class="metric-bar"><div class="metric-fill disk-fill" id="disk-bar" style="width:0%"></div></div>
                    <div class="mini-chart" id="cpu-chart"></div>
                </div>
                <div class="sidebar-section">
                    <div class="section-title">Network & Storage</div>
                    <div class="info-item"><span class="info-label">🌐 Network RX</span><span class="info-value" id="network-rx">0 KB/s</span></div>
                    <div class="info-item"><span class="info-label">πŸ“‘ Network TX</span><span class="info-value" id="network-tx">0 KB/s</span></div>
                    <div class="info-item"><span class="info-label">πŸ’Ύ Memory Used</span><span class="info-value" id="memory-details">0 MB / 0 MB</span></div>
                    <div class="info-item"><span class="info-label">πŸ’Ώ Disk Space</span><span class="info-value" id="disk-details">0 GB / 0 GB</span></div>
                </div>
                <div class="sidebar-section">
                    <div class="section-title">System Details</div>
                    <div class="info-item"><span class="info-label">πŸ—οΈ Platform</span><span class="info-value" id="platform">-</span></div>
                    <div class="info-item"><span class="info-label">πŸ”§ Architecture</span><span class="info-value" id="arch">-</span></div>
                    <div class="info-item"><span class="info-label">🧠 CPU Model</span><span class="info-value" id="cpu-model">-</span></div>
                    <div class="info-item"><span class="info-label">⚑ CPU Cores</span><span class="info-value" id="cpu-cores">-</span></div>
                    <div class="info-item"><span class="info-label">πŸ“Š Load Avg</span><span class="info-value" id="load-avg">-</span></div>
                </div>
                <div class="sidebar-section">
                    <div class="section-title">Top Processes</div>
                    <div class="processes" id="processes">
                        <div class="process-item">
                            <div class="process-name">Loading...</div>
                            <div class="process-cpu">-</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- Mobile elements -->
    <div class="mobile-overlay" id="mobile-overlay"></div>
    <button class="mobile-toggle" id="mobile-toggle">πŸ“Š</button>
    <script src="/socket.io/socket.io.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/xterm@5.3.0/lib/xterm.js"></script>
    <script>
        // Terminal & socket logic (sama seperti sebelumnya)
        let terminal, socket, isConnected = false, cpuHistory = [];
        function initializeTerminal() {
            terminal = new Terminal({
                cursorBlink: true,
                fontSize: 14,
                fontFamily: 'JetBrains Mono, monospace',
                theme: {
                    background: '#0c0c0c',
                    foreground: '#e4e4e7',
                    cursor: '#10b981',
                    selection: 'rgba(16,185,129,0.3)',
                    black: '#27272a',
                    red: '#ef4444',
                    green: '#10b981',
                    yellow: '#f59e0b',
                    blue: '#3b82f6',
                    magenta: '#a855f7',
                    cyan: '#06b6d4',
                    white: '#f4f4f5'
                }
            });
            terminal.open(document.getElementById('terminal'));
            terminal.focus();
            connectToServer();
            document.getElementById('clear-btn').addEventListener('click', clearTerminal);
            initializeMobileToggle();
        }
        function initializeMobileToggle() {
            const mobileToggle = document.getElementById('mobile-toggle');
            const sidebar = document.getElementById('sidebar');
            const overlay = document.getElementById('mobile-overlay');
            if (mobileToggle && sidebar && overlay) {
                mobileToggle.addEventListener('click', () => {
                    const isVisible = sidebar.classList.contains('show');
                    if (isVisible) hideMobileSidebar();
                    else showMobileSidebar();
                });
                overlay.addEventListener('click', hideMobileSidebar);
                document.addEventListener('keydown', (e) => {
                    if (e.key === 'Escape' && sidebar.classList.contains('show')) hideMobileSidebar();
                });
            }
        }
        function showMobileSidebar() {
            const sidebar = document.getElementById('sidebar');
            const overlay = document.getElementById('mobile-overlay');
            const toggle = document.getElementById('mobile-toggle');
            sidebar.classList.add('show');
            overlay.classList.add('show');
            toggle.classList.add('active');
            toggle.innerHTML = 'βœ•';
            document.body.style.overflow = 'hidden';
        }
        function hideMobileSidebar() {
            const sidebar = document.getElementById('sidebar');
            const overlay = document.getElementById('mobile-overlay');
            const toggle = document.getElementById('mobile-toggle');
            sidebar.classList.remove('show');
            overlay.classList.remove('show');
            toggle.classList.remove('active');
            toggle.innerHTML = 'πŸ“Š';
            document.body.style.overflow = '';
        }
        function connectToServer() {
            socket = io();
            socket.on('connect', () => {
                isConnected = true;
                updateConnectionStatus(true);
            });
            socket.on('disconnect', () => {
                isConnected = false;
                updateConnectionStatus(false);
                terminal.writeln('\r\n\x1b[31mβœ— Connection lost. Reconnecting...\x1b[0m');
            });
            socket.on('connect_error', (error) => {
                updateConnectionStatus(false);
                terminal.writeln('\r\n\x1b[31mβœ— Connection failed\x1b[0m');
            });
            socket.on('terminal_output', (data) => { terminal.write(data); });
            socket.on('system_info', (data) => { updateSystemInfo(data); });
            terminal.onData(data => { if (isConnected && socket) socket.emit('terminal_input', data); });
            terminal.onResize(({ cols, rows }) => { if (isConnected && socket) socket.emit('terminal_resize', { cols, rows }); });
        }
        function updateConnectionStatus(connected) {
            const dot = document.getElementById('connection-dot');
            const text = document.getElementById('connection-text');
            if (connected) {
                dot.classList.add('connected');
                text.textContent = 'Connected';
            } else {
                dot.classList.remove('connected');
                text.textContent = 'Disconnected';
            }
        }
        function updateSystemInfo(data) {
            if (!data) return;
            document.getElementById('hostname').textContent = data.hostname || '-';
            document.getElementById('uptime').textContent = data.uptime || '-';
            document.getElementById('users').textContent = data.user_info ? '1' : '-';
            document.getElementById('cpu-usage').textContent = `${Math.round(data.cpu_usage || 0)}%`;
            document.getElementById('cpu-bar').style.width = `${data.cpu_usage || 0}%`;
            document.getElementById('memory-usage').textContent = `${Math.round(data.mem_usage || 0)}%`;
            document.getElementById('memory-bar').style.width = `${data.mem_usage || 0}%`;
            document.getElementById('disk-usage').textContent = `${Math.round(data.disk_usage || 0)}%`;
            document.getElementById('disk-bar').style.width = `${data.disk_usage || 0}%`;
            document.getElementById('memory-details').textContent = `${data.used_mem || 0} MB / ${data.total_mem || 0} MB`;
            const diskUsed = typeof data.disk_used === 'number' ? `${data.disk_used} GB` : data.disk_used || '0';
            const diskTotal = typeof data.disk_total === 'number' ? `${data.disk_total} GB` : data.disk_total || '0';
            document.getElementById('disk-details').textContent = `${diskUsed} / ${diskTotal}`;
            document.getElementById('network-rx').textContent = formatBytes(data.network_rx || 0) + '/s';
            document.getElementById('network-tx').textContent = formatBytes(data.network_tx || 0) + '/s';
            document.getElementById('platform').textContent = data.platform || '-';
            document.getElementById('arch').textContent = data.arch || '-';
            document.getElementById('cpu-model').textContent = truncateText(data.cpu_model || '-', 20);
            document.getElementById('cpu-cores').textContent = data.cpu_cores || '-';
            if (data.loadavg && Array.isArray(data.loadavg)) {
                document.getElementById('load-avg').textContent = data.loadavg.map(l => l.toFixed(2)).join(', ');
            } else {
                document.getElementById('load-avg').textContent = '-';
            }
            if (data.cpu_usage !== undefined) {
                cpuHistory.push(data.cpu_usage);
                if (cpuHistory.length > 20) cpuHistory.shift();
                updateCpuChart();
            }
            if (data.processes && Array.isArray(data.processes)) updateProcessList(data.processes);
        }
        function formatBytes(bytes) {
            if (bytes === 0) return '0 B';
            const k = 1024, sizes = ['B', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(Math.abs(bytes)) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i];
        }
        function truncateText(text, maxLength) {
            if (text.length <= maxLength) return text;
            return text.substring(0, maxLength - 3) + '...';
        }
        function updateCpuChart() {
            const chartContainer = document.getElementById('cpu-chart');
            chartContainer.innerHTML = '';
            cpuHistory.forEach(value => {
                const bar = document.createElement('div');
                bar.className = 'chart-bar';
                bar.style.height = `${(value / 100) * 100}%`;
                chartContainer.appendChild(bar);
            });
        }
        function updateProcessList(processes) {
            const container = document.getElementById('processes');
            if (!processes || !Array.isArray(processes) || processes.length === 0) {
                container.innerHTML = `
                    <div class="process-item">
                        <div class="process-name">No data</div>
                        <div class="process-cpu">0%</div>
                    </div>
                `;
                return;
            }
            container.innerHTML = processes.map(proc => `
                <div class="process-item">
                    <div class="process-name">${proc.name || 'unknown'}</div>
                    <div class="process-cpu">${Math.round(proc.cpu || 0)}%</div>
                </div>
            `).join('');
        }
        function clearTerminal() { if (terminal) terminal.clear(); }
        document.addEventListener('DOMContentLoaded', initializeTerminal);
        // Responsive terminal resize
        let resizeTimeout;
        window.addEventListener('resize', () => {
            clearTimeout(resizeTimeout);
            resizeTimeout = setTimeout(() => {
                if (terminal && terminal.element && terminal.element.offsetParent !== null) {
                    try {
                        const container = document.getElementById('terminal');
                        if (container) {
                            const containerRect = container.getBoundingClientRect();
                            if (containerRect.width > 0 && containerRect.height > 0) {
                                const cols = Math.floor(containerRect.width / 9);
                                const rows = Math.floor(containerRect.height / 17);
                                if (cols > 10 && rows > 5) {
                                    terminal.resize(cols, rows);
                                    if (isConnected && socket) socket.emit('terminal_resize', { cols, rows });
                                }
                            }
                        }
                    } catch (error) {
                        try { terminal.resize(80, 24); } catch {}
                    }
                }
            }, 150);
        });
        window.addEventListener('orientationchange', () => {
            setTimeout(() => { window.dispatchEvent(new Event('resize')); }, 500);
        });
    </script>
</body>
</html>