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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
<script>
  import { onMount, onDestroy } from 'svelte';
  import { page } from '$app/stores';
  import { goto } from '$app/navigation';
  import { authStore, isAdmin, isFacultyOrAdmin, showToast } from '$lib/stores.js';
  import { t } from '$lib/i18n.js';

  const MIN_W = 52;
  const MAX_W = 360;
  const DEFAULT_W = 220;
  const COMPACT_THRESHOLD = 72;

  let sidebarWidth = DEFAULT_W;
  let compact = false;
  let resizerEl;
  let dragging = false;
  let dragStartX = 0;
  let dragStartW = 0;

  $: path = $page.url.pathname;
  $: compact = sidebarWidth < COMPACT_THRESHOLD;

  const navItems = [
    { href: '/chat',          key: 'nav.chat',          label: 'Chat' },
    { href: '/dashboard',     key: 'nav.dashboard',     label: 'Dashboard' },
    { href: '/notebooks',     key: 'nav.notebooks',     label: 'Notebooks' },
    { href: '/rag',           key: 'nav.rag',           label: 'Knowledge' },
    { href: '/doubts',        key: 'nav.doubts',        label: 'Doubts' },
    { href: '/attendance',    key: 'nav.attendance',    label: 'Attendance' },
    { href: '/files',         key: 'nav.files',         label: 'Files' },
    { href: '/notifications', key: 'nav.notifications', label: 'Notifications' },
    { href: '/keys',          key: 'nav.keys',          label: 'API Keys' },
    { href: '/settings',      key: 'nav.settings',      label: 'Settings' },
  ];

  const adminItems = [
    { href: '/copy-check', key: 'nav.copycheck', label: 'Copy Check', faculty: true },
    { href: '/admin',      key: 'nav.admin',     label: 'Admin' },
    { href: '/cluster',    key: 'nav.cluster',   label: 'Cluster' },
  ];

  // ── SVG icons ────────────────────────────────────────────────────────────────
  const icons = {
    chat: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>`,
    dashboard: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/><rect x="14" y="14" width="7" height="7" rx="1"/><rect x="3" y="14" width="7" height="7" rx="1"/></svg>`,
    notebooks: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M4 19.5V5a2 2 0 0 1 2-2h11a3 3 0 0 1 3 3v13.5"/><path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"/><path d="M8 7h7"/><path d="M8 11h5"/></svg>`,
    rag: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"/><path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"/></svg>`,
    doubts: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M12 18h.01"/><path d="M9.1 9a3 3 0 1 1 5.2 2c-.9.8-1.8 1.3-2.1 2.7"/><path d="M21 12a9 9 0 1 1-4.2-7.6"/></svg>`,
    attendance: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M16 21v-2a4 4 0 0 0-8 0v2"/><circle cx="12" cy="7" r="4"/><path d="m17 11 2 2 4-4"/></svg>`,
    files: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><path d="M14 2v6h6"/></svg>`,
    notifications: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.73 21a2 2 0 0 1-3.46 0"/></svg>`,
    keys: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="7.5" cy="15.5" r="5.5"/><path d="M21 2l-9.6 9.6"/><path d="M15.5 7.5l3 3L22 7l-3-3"/></svg>`,
    settings: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>`,
    copycheck: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M9 11 12 14 22 4"/><path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11"/></svg>`,
    admin: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M12 2L3 7l1.5 10L12 22l7.5-5L21 7z"/><circle cx="12" cy="12" r="3"/></svg>`,
    cluster: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="2" y="2" width="20" height="8" rx="2"/><rect x="2" y="14" width="20" height="8" rx="2"/><line x1="6" y1="6" x2="6.01" y2="6"/><line x1="6" y1="18" x2="6.01" y2="18"/></svg>`,
    logout: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/></svg>`,
    logo: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/></svg>`,
  };

  const iconMap = {
    '/chat':          'chat',
    '/dashboard':     'dashboard',
    '/notebooks':     'notebooks',
    '/rag':           'rag',
    '/doubts':        'doubts',
    '/attendance':    'attendance',
    '/files':         'files',
    '/notifications': 'notifications',
    '/keys':          'keys',
    '/settings':      'settings',
    '/copy-check':    'copycheck',
    '/admin':         'admin',
    '/cluster':       'cluster',
  };

  // ── Resize logic ─────────────────────────────────────────────────────────────
  function onMouseDown(e) {
    dragging = true;
    dragStartX = e.clientX;
    dragStartW = sidebarWidth;
    e.preventDefault();
  }

  function onMouseMove(e) {
    if (!dragging) return;
    const delta = e.clientX - dragStartX;
    sidebarWidth = Math.min(MAX_W, Math.max(MIN_W, dragStartW + delta));
  }

  function onMouseUp() {
    if (dragging) {
      dragging = false;
      try { localStorage.setItem('mac_sidebar_w', String(sidebarWidth)); } catch {}
    }
  }

  function onDblClick() {
    sidebarWidth = compact ? DEFAULT_W : MIN_W;
    try { localStorage.setItem('mac_sidebar_w', String(sidebarWidth)); } catch {}
  }

  // ── Touch resize ─────────────────────────────────────────────────────────────
  function onTouchStart(e) {
    dragging = true;
    dragStartX = e.touches[0].clientX;
    dragStartW = sidebarWidth;
  }

  function onTouchMove(e) {
    if (!dragging) return;
    const delta = e.touches[0].clientX - dragStartX;
    sidebarWidth = Math.min(MAX_W, Math.max(MIN_W, dragStartW + delta));
    e.preventDefault();
  }

  function onTouchEnd() {
    if (dragging) {
      dragging = false;
      try { localStorage.setItem('mac_sidebar_w', String(sidebarWidth)); } catch {}
    }
  }

  async function logout() {
    await authStore.logout();
    goto('/login');
  }

  onMount(() => {
    try {
      const saved = localStorage.getItem('mac_sidebar_w');
      if (saved) sidebarWidth = Math.min(MAX_W, Math.max(MIN_W, Number(saved)));
    } catch {}

    window.addEventListener('mousemove', onMouseMove);
    window.addEventListener('mouseup', onMouseUp);
  });

  onDestroy(() => {
    window.removeEventListener('mousemove', onMouseMove);
    window.removeEventListener('mouseup', onMouseUp);
  });
</script>

<aside
  class="sidebar"
  class:compact
  class:dragging
  style="width: {sidebarWidth}px; min-width: {sidebarWidth}px;"
>
  <!-- Logo -->
  <div class="logo-area">
    <span class="glitch sidebar-mac" data-text="MAC">MAC</span>
    {#if !compact}
      <span class="logo-sub">MBM AI Cloud</span>
    {/if}
  </div>

  <!-- Nav -->
  <nav class="nav-section">
    {#each navItems as item}
      <a
        href={item.href}
        class="nav-link"
        class:active={path.startsWith(item.href)}
        title={compact ? ($t(item.key) || item.label) : ''}
      >
        <span class="nav-icon">{@html icons[iconMap[item.href]]}</span>
        {#if !compact}
          <span class="nav-label">{$t(item.key) || item.label}</span>
        {/if}
      </a>
    {/each}

    {#if $isFacultyOrAdmin}
      {#if !compact}
        <div class="section-header">Controls</div>
      {:else}
        <div class="section-divider"></div>
      {/if}
      {#each adminItems as item}
        {#if item.faculty || $isAdmin}
          <a
            href={item.href}
            class="nav-link"
            class:active={path.startsWith(item.href)}
            title={compact ? ($t(item.key) || item.label) : ''}
          >
            <span class="nav-icon">{@html icons[iconMap[item.href]]}</span>
            {#if !compact}
              <span class="nav-label">{$t(item.key) || item.label}</span>
            {/if}
          </a>
        {/if}
      {/each}
    {/if}
  </nav>

  <!-- User + Logout -->
  <div class="user-area">
    <div class="user-info" class:compact-user={compact}>
      <div class="avatar">
        {$authStore.user?.name?.charAt(0)?.toUpperCase() ?? '?'}
      </div>
      {#if !compact}
        <div class="user-text">
          <span class="user-name">{$authStore.user?.name ?? ''}</span>
          <span class="user-role">{$authStore.user?.role ?? ''}</span>
        </div>
      {/if}
    </div>
    <button
      class="nav-link logout-btn"
      on:click={logout}
      title={compact ? 'Logout' : ''}
    >
      <span class="nav-icon">{@html icons.logout}</span>
      {#if !compact}
        <span class="nav-label">Logout</span>
      {/if}
    </button>
  </div>

  <!-- Resize handle -->
  <div
    class="resizer"
    bind:this={resizerEl}
    on:mousedown={onMouseDown}
    on:dblclick={onDblClick}
    on:touchstart|passive={onTouchStart}
    on:touchmove|preventDefault={onTouchMove}
    on:touchend={onTouchEnd}
    role="separator"
    aria-label="Resize sidebar"
  >
    <div class="resizer-line"></div>
  </div>
</aside>

<style>
  .sidebar {
    position: relative;
    z-index: 2;
    display: flex;
    flex-direction: column;
    height: 100vh;
    background: var(--surface);
    border-right: 1px solid var(--border);
    flex-shrink: 0;
    transition: width 0.15s ease;
    overflow: hidden;
    user-select: none;
  }

  .sidebar.dragging {
    transition: none;
  }

  /* ── Logo ─────────────────────────────────────── */
  .logo-area {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 14px 14px 14px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    overflow: hidden;
  }

  /* Sidebar MAC text β€” uses global .glitch class, just size it here */
  .sidebar-mac {
    font-size: 16px;
    flex-shrink: 0;
  }

  .logo-sub {
    font-size: 10px;
    color: var(--text3);
    white-space: nowrap;
    margin-top: 1px;
  }

  /* ── Nav ──────────────────────────────────────── */
  .nav-section {
    flex: 1;
    padding: 8px 6px;
    overflow-y: auto;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
    gap: 2px;
  }

  .nav-section::-webkit-scrollbar { width: 4px; }
  .nav-section::-webkit-scrollbar-track { background: transparent; }
  .nav-section::-webkit-scrollbar-thumb { background: var(--surface3); border-radius: 2px; }

  .nav-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 8px;
    color: var(--text2);
    font-size: 13px;
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
    background: none;
    border: none;
    width: 100%;
    transition: background 0.15s, color 0.15s;
    white-space: nowrap;
    overflow: hidden;
  }

  .nav-link:hover {
    background: var(--surface2);
    color: var(--text);
  }

  .nav-link.active {
    background: rgba(217,116,73,0.10);
    color: var(--accent);
  }

  .nav-link.active .nav-icon :global(svg) {
    stroke: var(--accent);
  }

  .nav-icon {
    width: 18px;
    height: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }

  .nav-icon :global(svg) {
    width: 18px;
    height: 18px;
    stroke: currentColor;
    flex-shrink: 0;
  }

  .nav-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
  }

  .section-header {
    padding: 10px 12px 4px;
    font-size: 10px;
    color: var(--text3);
    text-transform: uppercase;
    letter-spacing: 0.12em;
    font-weight: 600;
  }

  .section-divider {
    height: 1px;
    background: var(--border);
    margin: 6px 8px;
  }

  /* ── Compact mode ─────────────────────────────── */
  .compact .nav-link {
    justify-content: center;
    padding: 10px;
    gap: 0;
  }

  .compact .logo-area {
    justify-content: center;
    padding: 14px 10px;
  }

  /* ── User area ────────────────────────────────── */
  .user-area {
    border-top: 1px solid var(--border);
    padding: 8px 6px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
  }

  .user-info {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 10px 4px;
    overflow: hidden;
  }

  .user-info.compact-user {
    justify-content: center;
    padding: 6px 10px;
  }

  .avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: rgba(217,116,73,0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-weight: 700;
    color: var(--accent);
    flex-shrink: 0;
  }

  .user-text {
    display: flex;
    flex-direction: column;
    min-width: 0;
    overflow: hidden;
  }

  .user-name {
    font-size: 12px;
    font-weight: 600;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .user-role {
    font-size: 10px;
    color: var(--text3);
    text-transform: capitalize;
    white-space: nowrap;
  }

  .logout-btn {
    color: var(--text3);
  }

  .logout-btn:hover {
    background: var(--error-bg);
    color: var(--error);
  }

  /* ── Resize handle ────────────────────────────── */
  .resizer {
    position: absolute;
    top: 0;
    right: 0;
    width: 8px;
    height: 100%;
    cursor: col-resize;
    z-index: 10;
    display: flex;
    align-items: stretch;
    justify-content: flex-end;
  }

  .resizer-line {
    width: 2px;
    background: transparent;
    transition: background 0.2s;
    border-radius: 1px;
    margin-right: 1px;
  }

  .resizer:hover .resizer-line,
  .dragging .resizer .resizer-line {
    background: var(--accent);
  }
</style>