File size: 28,883 Bytes
6f60a4c
 
 
7e4a932
 
5187fdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e4a932
 
 
 
 
 
 
 
 
 
 
 
 
6879a9a
952b165
6879a9a
 
 
 
952b165
7e4a932
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f60a4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e4a932
 
 
 
 
 
 
 
 
 
5187fdc
7e4a932
 
5187fdc
 
 
 
 
167d7da
5187fdc
454c289
5187fdc
454c289
 
a647683
7e4a932
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5187fdc
 
a647683
 
 
 
 
 
 
454c289
a647683
454c289
 
a647683
 
 
5187fdc
7e4a932
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5187fdc
7e4a932
 
 
 
 
 
 
 
 
5187fdc
 
 
7e4a932
5187fdc
7e4a932
 
 
 
5187fdc
 
 
 
 
 
 
 
 
 
 
7e4a932
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296b083
 
7e4a932
 
 
 
296b083
7e4a932
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* eslint-disable react-hooks/exhaustive-deps, react-hooks/set-state-in-effect, react-hooks/immutability, react-refresh/only-export-components */
import { useState, useEffect, useRef } from 'react';
import { NavLink, useNavigate, useLocation } from 'react-router-dom';
import { useAuth } from './AuthContext';

export const getInitials = (userData) => {
  if (!userData) return 'DP';
  const firstName = (userData.first_name || '').trim();
  const lastName = (userData.last_name || '').trim();
  if (firstName || lastName) {
    const f = firstName.charAt(0);
    const l = lastName.charAt(0);
    if (f && l) return `${f}${l}`.toUpperCase();
    if (f) return f.toUpperCase();
    if (l) return l.toUpperCase();
  }
  const name = (userData.name || userData.full_name || '').trim();
  if (name) {
    const parts = name.split(/\s+/);
    if (parts.length >= 2) {
      return `${parts[0].charAt(0)}${parts[parts.length - 1].charAt(0)}`.toUpperCase();
    }
    return name.substring(0, 2).toUpperCase();
  }
  const email = (userData.email || '').trim();
  if (email) {
    const username = email.split('@')[0];
    const parts = username.split(/[._-]/);
    if (parts.length >= 2 && parts[0] && parts[1]) {
      return `${parts[0].charAt(0)}${parts[1].charAt(0)}`.toUpperCase();
    }
    if (username.length >= 2) {
      return username.substring(0, 2).toUpperCase();
    }
    return username.charAt(0).toUpperCase();
  }
  return 'DP';
};

export const Layout = ({ children }) => {
  const { user, logout } = useAuth();
  const navigate = useNavigate();
  const location = useLocation();

  const searchParams = new URLSearchParams(location.search);
  const urlQuery = searchParams.get('q') || '';
  const [globalSearchQuery, setGlobalSearchQuery] = useState(urlQuery);

  useEffect(() => {
    setGlobalSearchQuery(urlQuery);
  }, [urlQuery]);

  // Always scroll to top of page on route change
  useEffect(() => {
    window.scrollTo(0, 0);
    const mainElement = document.querySelector('main');
    if (mainElement) mainElement.scrollTop = 0;
  }, [location.pathname]);

  const [isDemoMode, setIsDemoMode] = useState(false);
  const [isProfileOpen, setIsProfileOpen] = useState(false);
  const [isNotificationsOpen, setIsNotificationsOpen] = useState(false);
  const [notifications, setNotifications] = useState([]);
  const [loadingNotifications, setLoadingNotifications] = useState(false);
  const [hasUnreadNotifications, setHasUnreadNotifications] = useState(false);

  const notificationsRef = useRef(null);
  const profileRef = useRef(null);

  useEffect(() => {
    const handleClickOutside = (event) => {
      if (notificationsRef.current && !notificationsRef.current.contains(event.target)) {
        setIsNotificationsOpen(false);
      }
      if (profileRef.current && !profileRef.current.contains(event.target)) {
        setIsProfileOpen(false);
      }
    };
    document.addEventListener("mousedown", handleClickOutside);
    return () => {
      document.removeEventListener("mousedown", handleClickOutside);
    };
  }, []);
  
  const fetchNotifications = async () => {
    if (!user) return;
    setLoadingNotifications(true);
    try {
      const token = localStorage.getItem('wss_token') || sessionStorage.getItem('wss_token');
      const res = await fetch('/api/auth/notifications', {
        headers: { 'Authorization': `Bearer ${token}` }
      });
      if (res.ok) {
        const data = await res.json();
        const fetched = data.notifications || [];
        
        const lastSeenId = localStorage.getItem('last_seen_notification_id');
        
        if (fetched.length > 0 && String(fetched[0].id) !== lastSeenId) {
          setHasUnreadNotifications(true);
        }
        
        setNotifications(fetched);
      }
    } catch (err) {
      console.error("Failed to fetch notifications:", err);
    } finally {
      setLoadingNotifications(false);
    }
  };

  useEffect(() => {
    if (user) {
      fetchNotifications();
      const interval = setInterval(fetchNotifications, 60000); // refresh every minute
      return () => clearInterval(interval);
    }
  }, [user]);
  useEffect(() => {
    setIsDemoMode(!!window.WSS_DEMO_MODE);
    const checkInterval = setInterval(() => {
      setIsDemoMode(!!window.WSS_DEMO_MODE);
    }, 1000);
    return () => clearInterval(checkInterval);
  }, []);

  // Force removal of dark theme and clear localStorage keys
  useEffect(() => {
    document.documentElement.classList.remove('dark');
    localStorage.removeItem('color-theme');
  }, []);

  const [impersonationToken, setImpersonationToken] = useState(null);
  const [organizations, setOrganizations] = useState([]);

  useEffect(() => {
    setImpersonationToken(localStorage.getItem('original_admin_token'));
  }, [location.pathname]);

  useEffect(() => {
    const fetchOrgs = async () => {
      const adminToken = localStorage.getItem('original_admin_token') || localStorage.getItem('wss_token') || sessionStorage.getItem('wss_token');
      // Only attempt if they might be an admin
      if (!adminToken) return;
      
      try {
        const res = await fetch('/api/auth/organizations', {
          headers: { 'Authorization': `Bearer ${adminToken}` }
        });
        if (res.ok) {
          const data = await res.json();
          setOrganizations(data.organizations || []);
        }
      } catch (err) {
        console.error("Failed to fetch organizations for dropdown", err);
      }
    };
    
    const isSuperAdmin = user?.role === 'super_admin' || user?.role === 'admin' || localStorage.getItem('original_admin_token');
    if (isSuperAdmin) {
      fetchOrgs();
    }
  }, [user]);

  const handleReturnToAdmin = () => {
    const orig = localStorage.getItem('original_admin_token');
    if (orig) {
      localStorage.removeItem('original_admin_token');
      localStorage.setItem('wss_token', orig);
      window.location.href = location.pathname;
    }
  };

  const handleLogout = () => {
    const isSuperAdmin = user?.role === 'super_admin' || sessionStorage.getItem('superAdminAuth') === 'true';
    logout();
    sessionStorage.removeItem('superAdminAuth');
    localStorage.removeItem('original_admin_token');
    navigate(isSuperAdmin ? '/' : '/login');
  };

  const navItems = [
    ...(user?.role === 'executive_user'
      ? [
          { to: '/scans/history', label: 'Organization Reports', icon: 'analytics' },
          { to: '/settings', label: 'Settings', icon: 'settings' },
        ]
      : [
          { to: '/dashboard', label: 'Dashboard', icon: 'dashboard' },
          { to: '/scans/new', label: 'New Scan', icon: 'security' },
          { to: '/scans/history', label: 'Reports', icon: 'analytics' },
          { to: '/scans/results', label: 'Vulnerabilities', icon: 'bug_report' },
          { to: '/settings', label: 'Settings', icon: 'settings' },
        ]),
    ...(user?.role === 'super_admin'
      ? [{ to: '/super-admin', label: 'Global Management', icon: 'admin_panel_settings' }]
      : user?.role === 'admin'
      ? [{ to: '/admin', label: 'Global Management', icon: 'admin_panel_settings' }]
      : user?.role === 'support_engineer'
      ? [{ to: '/support', label: 'Global Management', icon: 'support_agent' }]
      : []),
  ];

  return (
    <div className="bg-background text-on-background font-body-md text-body-md antialiased min-h-screen flex flex-col md:flex-row w-full transition-colors duration-300">
      
      {/* SideNavBar (Stitch Layout) */}
      <nav className="bg-white dark:bg-inverse-surface h-screen w-64 flex-col fixed left-0 top-0 border-r border-outline-variant dark:border-outline z-40 hidden md:flex py-lg px-md gap-sm text-left">
        
        {/* Header Branding */}
        <div className="mb-xl flex flex-col gap-sm">
          <div className="flex items-center gap-sm">
            <div className="h-12 flex items-center justify-center shrink-0">
              <img src="/logo.png" alt="LarShield Logo" className="h-full object-contain" />
            </div>
            <div className="flex flex-col justify-center">
              <h1 className="font-extrabold tracking-tight m-0 text-[18px] leading-none flex items-center">
                <span className="text-[#0b132a] dark:text-white">Lar</span>
                <span className="text-[#5856d6]">Shield</span>
              </h1>
              <p className="text-[12px] text-[#64748b] dark:text-slate-400 uppercase tracking-[0.14em] m-0 font-medium leading-tight mt-1">
                {user?.role === 'super_admin' ? 'SUPER ADMIN' : 
                 user?.role === 'admin' ? 'ADMIN' : 
                 user?.role === 'support_engineer' ? 'SUPPORT ENGINEER' : 
                 user?.role === 'executive_user' ? 'EXECUTIVE USER' :
                 user?.role === 'soc_analyst' ? 'SOC ANALYST' :
                 'ORG ADMIN'}
              </p>
            </div>
          </div>
          
          {user?.role !== 'executive_user' && (
            <NavLink 
              to="/scans/new" 
              className="mt-md w-full bg-primary text-on-primary rounded-lg py-sm px-md font-label-md text-label-md flex items-center justify-center gap-xs hover:opacity-90 transition-opacity border-0 cursor-pointer"
              style={{ textDecoration: 'none' }}
            >
              <span className="material-symbols-outlined" style={{ fontSize: '18px' }}>add</span>
              Quick Scan
            </NavLink>
          )}
        </div>

        {/* Navigation Links */}
        <div className="flex flex-col gap-base flex-1">
          {navItems.map((item) => (
            <NavLink
              key={item.to}
              to={item.to}
              className={({ isActive }) => 
                `flex items-center gap-sm px-sm py-sm rounded-lg transition-colors cursor-pointer transition-all duration-200 border-0 ${
                  isActive 
                    ? 'text-primary dark:text-primary-fixed-dim font-bold bg-surface-container-high dark:bg-on-secondary-fixed-variant' 
                    : 'text-on-surface-variant dark:text-surface-variant hover:bg-surface-container-high dark:hover:bg-on-secondary-fixed-variant'
                }`
              }
              style={{ textDecoration: 'none' }}
            >
              <span className="material-symbols-outlined">{item.icon}</span>
              <span className="font-label-md text-label-md">{item.label}</span>
            </NavLink>
          ))}

          {/* Help Page Link */}
          <NavLink 
            to="/help"
            className={({ isActive }) => 
              `flex items-center gap-sm px-sm py-sm rounded-lg transition-colors cursor-pointer transition-all duration-200 mt-auto border-0 ${
                isActive 
                  ? 'text-primary dark:text-primary-fixed-dim font-bold bg-surface-container-high dark:bg-on-secondary-fixed-variant' 
                  : 'text-on-surface-variant dark:text-surface-variant hover:bg-surface-container-high dark:hover:bg-on-secondary-fixed-variant'
              }`
            }
            style={{ textDecoration: 'none' }}
          >
            <span className="material-symbols-outlined">help</span>
            <span className="font-label-md text-label-md">Help</span>
          </NavLink>
        </div>


        {/* Sidebar Status / Log Out */}
        <div className="mt-md pt-md border-t border-outline-variant">
          <button 
            onClick={handleLogout}
            className="w-full flex items-center gap-sm px-sm py-sm rounded-lg text-on-surface-variant dark:text-surface-variant hover:bg-surface-container-high dark:hover:bg-on-secondary-fixed-variant transition-colors cursor-pointer transition-all duration-200 border-0 bg-transparent text-left"
          >
            <span className="material-symbols-outlined">logout</span>
            <span className="font-label-md text-label-md">Log Out</span>
          </button>
        </div>
      </nav>

      {/* TopNavBar (Stitch Layout) */}
      <header className="bg-surface/80 dark:bg-inverse-surface/80 backdrop-blur-md fixed top-0 w-full z-50 border-b border-outline-variant dark:border-outline shadow-sm flex justify-between items-center h-16 px-gutter max-w-container-max mx-auto md:w-[calc(100%-16rem)] md:left-64 md:px-lg">
        
        {/* Mobile Hamburger menu */}
        <div className="md:hidden flex items-center gap-sm">
          <span className="material-symbols-outlined text-primary cursor-pointer">menu</span>
          <div className="flex items-center gap-xs">
            <img src="/logo.png" alt="LarShield Logo" className="h-7 w-7 object-contain" />
            <div className="flex flex-col justify-center">
              <span className="font-extrabold tracking-tight text-[15px] leading-none flex items-center">
                <span className="text-[#0b132a] dark:text-white">Lar</span>
                <span className="text-[#5856d6]">Shield</span>
              </span>
              <span className="text-[10px] text-[#64748b] dark:text-slate-400 uppercase tracking-[0.12em] font-medium leading-none mt-0.5">
                {user?.role === 'super_admin' ? 'SUPER ADMIN' : 
                 user?.role === 'admin' ? 'ADMIN' : 
                 user?.role === 'support_engineer' ? 'SUPPORT ENGINEER' : 
                 user?.role === 'executive_user' ? 'EXECUTIVE USER' :
                 user?.role === 'soc_analyst' ? 'SOC ANALYST' :
                 'ORG ADMIN'}
              </span>
            </div>
          </div>
        </div>

        {/* Search Bar Utility */}
        <div className="hidden md:flex flex-1 max-w-md ml-xl relative">
          <span className="material-symbols-outlined absolute left-sm top-1/2 -translate-y-1/2 text-outline" style={{ fontSize: '20px' }}>search</span>
          <input 
            className="w-full bg-surface-container-lowest border border-outline-variant rounded-lg pl-xl pr-sm py-xs font-body-sm text-body-sm text-on-surface focus:outline-none focus:border-primary focus:ring-1 focus:ring-primary transition-all placeholder:text-outline" 
            placeholder="Search vulnerabilities, reports, assets..." 
            type="text"
            value={globalSearchQuery}
            onChange={(e) => setGlobalSearchQuery(e.target.value)}
            onKeyDown={(e) => {
              if (e.key === 'Enter') {
                if (globalSearchQuery.trim()) {
                  navigate(`/scans/history?q=${encodeURIComponent(globalSearchQuery.trim())}`);
                } else {
                  navigate(`/scans/history`);
                }
              }
            }}
          />
        </div>

        {/* Right Nav Icons / Mode Selector */}
        <div className="flex items-center gap-gutter ml-auto">

          {/* Organization Display / Admin Dropdown */}
          {(user?.role === 'admin' || user?.role === 'super_admin' || impersonationToken) && organizations.length > 0 ? (
            <div className="flex items-center">
              <select
                className="bg-surface-container border border-outline-variant rounded-md px-sm py-xs font-label-sm text-on-surface focus:outline-none focus:ring-1 focus:ring-primary focus:border-primary max-w-[200px] truncate"
                value={impersonationToken ? (user?.org_id || '') : ''}
                onChange={async (e) => {
                  const targetOrgId = e.target.value;
                  if (!targetOrgId) {
                    // Return to Admin
                    handleReturnToAdmin();
                    return;
                  }
                  
                  try {
                    const activeToken = localStorage.getItem('original_admin_token') || localStorage.getItem('wss_token');
                    const res = await fetch(`/api/auth/impersonate/${targetOrgId}`, {
                      method: 'POST',
                      headers: { 'Authorization': `Bearer ${activeToken}` }
                    });
                    
                    if (res.ok) {
                      const data = await res.json();
                      localStorage.setItem('original_admin_token', activeToken);
                      localStorage.setItem('wss_token', data.access_token);
                      window.location.href = location.pathname; // Reload current page with new token
                    }
                  } catch (err) {
                    console.error("Failed to impersonate from dropdown:", err);
                  }
                }}
              >
                <option value="">-- Return to Admin --</option>
                {organizations.map(org => (
                  <option key={org.id} value={org.id}>{org.name}</option>
                ))}
              </select>
            </div>
          ) : user?.org_name ? (
            <div className="hidden md:flex items-center gap-xs font-label-md text-label-md text-on-surface bg-surface-container-low px-sm py-xs rounded-md border border-outline-variant">
              <span className="material-symbols-outlined text-[18px] text-primary">domain</span>
              <span className="font-bold">{user.org_name}</span>
            </div>
          ) : null}
          
          {/* Active Status Badge */}
          {isDemoMode ? (
            <div className="hidden md:inline-flex items-center gap-xs bg-yellow-500/10 border border-yellow-500/30 rounded-full px-sm py-[2px] font-label-sm text-label-sm text-yellow-600 dark:text-yellow-500 font-bold uppercase tracking-wider">
              <span className="w-2 h-2 rounded-full bg-yellow-500 animate-pulse"></span> Sandbox Mode
            </div>
          ) : (
            <div className="hidden md:inline-flex items-center gap-xs bg-green-500/10 border border-green-500/30 rounded-full px-sm py-[2px] font-label-sm text-label-sm text-green-600 dark:text-green-500 font-bold uppercase tracking-wider">
              <span className="w-2 h-2 rounded-full bg-green-500 animate-pulse"></span> Connected
            </div>
          )}



          {/* Profile & Controls */}
          <div className="flex items-center gap-sm">
            <div className="relative" ref={notificationsRef}>
              <button 
                onClick={() => {
                  const willOpen = !isNotificationsOpen;
                  setIsNotificationsOpen(willOpen);
                  setIsProfileOpen(false);
                  if (willOpen) {
                    setHasUnreadNotifications(false);
                    if (notifications.length > 0) {
                      localStorage.setItem('last_seen_notification_id', String(notifications[0].id));
                    }
                  }
                }}
                className="text-on-surface-variant dark:text-surface-variant hover:text-primary transition-colors duration-200 active:opacity-80 transition-all flex items-center justify-center relative border-0 bg-transparent cursor-pointer"
              >
                <span className="material-symbols-outlined text-[26px]">notifications</span>
                {hasUnreadNotifications && (
                  <span className="absolute top-0 right-0 flex h-2.5 w-2.5">
                    <span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-error opacity-75"></span>
                    <span className="relative inline-flex rounded-full h-2.5 w-2.5 bg-error border-[1.5px] border-white dark:border-inverse-surface"></span>
                  </span>
                )}
              </button>
              
              {isNotificationsOpen && (
                <div className="absolute right-0 mt-sm w-80 bg-surface border border-outline-variant rounded-xl shadow-xl z-50 flex flex-col overflow-hidden">
                  <div className="px-md py-md border-b border-outline-variant bg-surface/95 backdrop-blur-sm flex justify-between items-center z-10 shrink-0">
                    <h3 className="font-label-lg text-label-lg font-bold text-on-surface m-0 flex items-center gap-xs">
                      <span className="material-symbols-outlined text-primary text-xl">notifications</span>
                      Notifications
                    </h3>
                    <button 
                      onClick={fetchNotifications} 
                      className="border border-outline-variant/50 bg-surface-container-lowest hover:bg-surface-container rounded-full w-8 h-8 flex items-center justify-center cursor-pointer transition-all text-on-surface-variant hover:text-primary shadow-sm"
                      title="Refresh Notifications"
                    >
                      <span className={"material-symbols-outlined text-base" + (loadingNotifications ? " animate-spin" : "")}>sync</span>
                    </button>
                  </div>
                  
                  <div className="flex flex-col bg-surface-container-lowest overflow-hidden">
                    {loadingNotifications && notifications.length === 0 ? (
                      <div className="p-xl flex-1 flex flex-col justify-center items-center text-on-surface-variant font-body-sm gap-sm">
                        <span className="material-symbols-outlined animate-spin text-3xl text-primary/60">sync</span>
                        <span>Loading notifications...</span>
                      </div>
                    ) : notifications.length === 0 ? (
                      <div className="p-xl flex-1 flex flex-col justify-center items-center text-on-surface-variant font-body-sm gap-xs text-center">
                        <div className="w-12 h-12 rounded-full bg-surface-container flex items-center justify-center mb-sm shadow-inner border border-outline-variant/30">
                          <span className="material-symbols-outlined text-3xl text-on-surface-variant/60">notifications_off</span>
                        </div>
                        <span className="font-bold text-on-surface font-label-md">All Caught Up!</span>
                        <span className="text-xs opacity-80 mt-1">You have no new notifications right now.</span>
                      </div>
                    ) : (
                      notifications.slice(0, 3).map(n => (
                        <div key={n.id} className="flex gap-md p-md border-b border-outline-variant/40 hover:bg-surface-container-low transition-colors cursor-default text-left group">
                          <div className={`w-10 h-10 rounded-full flex items-center justify-center shrink-0 shadow-sm ${n.bg || 'bg-primary/10'}`}>
                            <span className={`material-symbols-outlined text-[20px] ${n.color || 'text-primary'}`}>{n.icon}</span>
                          </div>
                          <div className="flex flex-col flex-1 justify-center">
                            <span className="font-label-md text-label-md font-bold text-on-surface group-hover:text-primary transition-colors">{n.title}</span>
                            <span className="font-body-sm text-[13px] text-on-surface-variant leading-snug mt-[2px]">{n.message}</span>
                            <span className="font-label-sm text-[11px] text-on-surface-variant/60 mt-xs uppercase tracking-wider">{new Date(n.timestamp).toLocaleString(undefined, { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' })}</span>
                          </div>
                        </div>
                      ))
                    )}
                  </div>
                </div>
              )}
            </div>
            
            {!user && (
              <button className="flex items-center gap-xs text-primary font-label-md text-label-md hover:bg-primary/5 px-sm py-xs rounded-lg transition-colors border-0 bg-transparent cursor-pointer">
                <span className="font-bold">Book Demo</span>
                <span className="material-symbols-outlined" style={{ fontSize: '18px' }}>calendar_today</span>
              </button>
            )}

            {user && (
              <div className="relative ml-sm" ref={profileRef}>
                <button 
                  onClick={() => {
                    setIsProfileOpen(!isProfileOpen);
                    setIsNotificationsOpen(false);
                  }}
                  className="flex items-center gap-xs border-0 bg-transparent cursor-pointer hover:opacity-80 transition-opacity"
                >
                  <div className="w-8 h-8 rounded-full bg-primary/10 text-primary border border-primary/20 flex items-center justify-center font-bold text-xs uppercase shadow-sm">
                    {getInitials(user)}
                  </div>
                  <span className="hidden lg:inline text-xs font-semibold text-on-surface-variant dark:text-surface-variant">
                    {user.first_name || user.last_name ? `${user.first_name || ''} ${user.last_name || ''}`.trim() : user.email.split('@')[0]}
                  </span>
                </button>
                
                {isProfileOpen && (
                  <div className="absolute right-0 mt-xs w-52 bg-surface border border-outline-variant rounded-lg shadow-lg overflow-hidden z-50">
                    <div className="px-md py-sm border-b border-outline-variant bg-surface-container-low flex items-center gap-sm">
                      <div className="w-9 h-9 rounded-full bg-primary/10 text-primary border border-primary/20 flex items-center justify-center font-bold text-sm uppercase shrink-0">
                        {getInitials(user)}
                      </div>
                      <div className="flex flex-col min-w-0">
                        <p className="font-label-md text-label-md font-bold text-on-surface truncate m-0">
                          {user.first_name || user.last_name ? `${user.first_name || ''} ${user.last_name || ''}`.trim() : user.email}
                        </p>
                        <p className="font-body-sm text-[11px] text-on-surface-variant capitalize m-0 truncate">{(user.role || 'User').replace(/_/g, ' ')}</p>
                      </div>
                    </div>
                    <div className="p-xs">
                      <button 
                        onClick={() => {
                          setIsProfileOpen(false);
                          navigate('/settings');
                        }}
                        className="w-full text-left px-sm py-xs font-label-md text-label-md text-on-surface hover:bg-surface-container rounded transition-colors flex items-center gap-xs border-0 bg-transparent cursor-pointer"
                      >
                        <span className="material-symbols-outlined text-[18px]">person</span>
                        My Profile
                      </button>

                      <button 
                        onClick={handleLogout}
                        className="w-full text-left px-sm py-xs font-label-md text-label-md text-error hover:bg-error/10 rounded transition-colors flex items-center gap-xs border-0 bg-transparent cursor-pointer mt-xs"
                      >
                        <span className="material-symbols-outlined text-[18px]">logout</span>
                        Logout
                      </button>
                    </div>
                  </div>
                )}
              </div>
            )}
          </div>
        </div>
      </header>

      {/* Main Content Render Area */}
      <main className="flex-grow pt-[64px] md:pl-64 min-h-screen bg-background pb-xl w-full text-left">
        {impersonationToken && (
          <div className="bg-primary text-white px-md py-sm flex justify-between items-center shadow-md mx-md md:mx-xl mt-md md:mt-0 mb-sm rounded-lg animate-fade-in">
            <div className="flex items-center gap-sm">
              <span className="material-symbols-outlined text-[20px]">vpn_key</span>
              <span className="font-bold text-[14px]">Impersonation Mode Active</span>
              <span className="text-[13px] opacity-90 border-l border-white/30 pl-sm ml-sm">
                You are currently viewing data for <strong>{user?.org_name || organizations.find(o => String(o.id) === String(user?.org_id))?.name || 'this organization'}</strong>.
              </span>
            </div>
            <button 
              onClick={handleReturnToAdmin} 
              className="bg-white text-primary hover:bg-surface-container transition-colors px-3 py-1.5 rounded-md font-bold text-[12px] border-0 cursor-pointer shadow-sm flex items-center gap-xs shrink-0"
            >
              <span className="material-symbols-outlined text-[16px]">exit_to_app</span>
              Return
            </button>
          </div>
        )}
        <div className="px-md py-sm md:px-xl md:py-md max-w-container-max mx-auto flex flex-col gap-gutter">
          {children}
        </div>
      </main>

    </div>
  );
};