File size: 30,998 Bytes
518343a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
import { useDemoMode } from '../../lib/operations/demo-mode';
import { EnvironmentLabel } from '@szl-holdings/shared-ui/alloy-decision-card';
import { SectionErrorBoundary } from '@szl-holdings/shared-ui/error-boundary';
import { useAuth } from '@szl-holdings/replit-auth-web';
import {
  GettingStartedChecklist,
  type OnboardingConfig,
  OnboardingWizard,
  useOnboardingState,
} from '@szl-holdings/shared-ui/onboarding';
import { RealtimeStatusIndicator } from '@szl-holdings/shared-ui/realtime-status-indicator';
import { useSandboxMode } from '@szl-holdings/shared-ui/sandbox-mode';
import { ServiceStatusRail } from '@szl-holdings/shared-ui/service-status-rail';
import { useRealtimeChannel } from '@szl-holdings/shared-ui/use-realtime-channel';
import { cn } from '@szl-holdings/shared-ui/utils';
import {
  Activity,
  AlertTriangle,
  BarChart3,
  Bell,
  BellOff,
  BookOpen,
  Bot,
  Boxes,
  Brain,
  Building,
  Calculator,
  Calendar,
  CheckSquare,
  ChevronDown,
  Clapperboard,
  Code,
  Cpu,
  Database,
  DollarSign,
  Download,
  FileText,
  Flag,
  Gauge,
  GitBranch,
  Globe,
  Headphones,
  Heart,
  History,
  Inbox,
  Layers,
  LayoutDashboard,
  Menu,
  Monitor,
  Network,
  Phone,
  Play,
  Power,
  Radio,
  RotateCcw,
  Scale,
  Search,
  Send,
  Settings,
  Shield,
  Sliders,
  Target,
  TrendingUp,
  Users,
  Workflow,
  X,
  Zap,
} from 'lucide-react';
import { type ReactNode, useState } from 'react';
import { Link, useLocation } from 'wouter';

const COMMAND_ONBOARDING_CONFIG: OnboardingConfig = {
  appId: 'command',
  appName: 'KORA',
  accentColor: '#d4a054',
  steps: [
    {
      id: 'welcome',
      title: 'Welcome to Command',
      description:
        'Command is your business observability command center — detecting operational risk, surfacing ownership gaps, and routing action before business damage occurs.',
      placement: 'center',
      icon: Zap,
    },
    {
      id: 'prism',
      title: 'The PRISM Framework',
      description:
        "PRISM is Command's intelligence spine: Pulse (health), Risk (exposure), Intelligence (analysis), Signals (events), and Motion (action). Each dimension gives you a different lens on your business.",
      placement: 'center',
      icon: Activity,
    },
    {
      id: 'signals',
      title: 'Signals Feed',
      description:
        'Signals are cross-system events that matter — approval delays, ownership gaps, compliance exposure, SLA risks. Each signal carries evidence, confidence scores, and recommended next actions.',
      targetSelector: "a[href='/operations/prism/signals']",
      placement: 'right',
      icon: Radio,
    },
    {
      id: 'inbox',
      title: 'Command Inbox',
      description:
        'Your inbox surfaces the highest-priority items requiring your attention — ranked by business impact and urgency, not alert timestamp.',
      targetSelector: "a[href='/operations/inbox']",
      placement: 'right',
      icon: Inbox,
    },
    {
      id: 'ownership',
      title: 'Ownership Intelligence',
      description:
        "See who owns what, what's unassigned, where handoffs break, and who's overloaded — the gaps that cause issues to fall through the cracks.",
      targetSelector: "a[href='/operations/ownership']",
      placement: 'right',
      icon: Users,
    },
  ],
  checklist: [
    {
      id: 'explore-prism',
      label: 'Explore the PRISM framework',
      description: 'Check all five intelligence dimensions',
    },
    {
      id: 'check-signals',
      label: 'Review your signals feed',
      description: 'See cross-system events by urgency',
    },
    {
      id: 'check-inbox',
      label: 'Clear your command inbox',
      description: 'Review high-priority action items',
    },
    {
      id: 'check-ownership',
      label: 'Check ownership gaps',
      description: 'Review unassigned ownership areas',
    },
    {
      id: 'configure-alerts',
      label: 'Configure alert thresholds',
      description: 'Set risk tolerance and routing rules',
    },
    {
      id: 'explore-topology',
      label: 'Explore service topology',
      description: 'View system dependency relationships',
    },
  ],
};

const BG = { sidebar: '#060a12', header: 'rgba(6,10,18,0.85)', main: 'var(--gi-bg-base)' };
const BORDER = { subtle: 'rgba(255,255,255,0.04)', muted: 'rgba(255,255,255,0.06)' };
const TEXT = {
  primary: 'rgba(255,255,255,0.88)',
  secondary: 'rgba(255,255,255,0.5)',
  tertiary: 'rgba(255,255,255,0.28)',
  muted: 'rgba(255,255,255,0.14)',
};

const PRISM_ITEMS = [
  { key: 'P', label: 'Pulse', color: '#c9b787', icon: Heart, href: '/operations/prism/pulse' },
  {
    key: 'R',
    label: 'Risk',
    color: '#c45a4a',
    icon: AlertTriangle,
    href: '/operations/prism/risk',
  },
  {
    key: 'I',
    label: 'Intelligence',
    color: '#8b7ac8',
    icon: Brain,
    href: '/operations/prism/intelligence',
  },
  { key: 'S', label: 'Signals', color: '#c8953c', icon: Radio, href: '/operations/prism/signals' },
  { key: 'M', label: 'Motion', color: '#4a90b8', icon: Workflow, href: '/operations/prism/motion' },
];

const NAV_GROUPS = [
  {
    label: null,
    items: [
      { href: '/operations', label: 'Exec Command', icon: LayoutDashboard },
      { href: '/operations/demo-live', label: 'Live Demo', icon: Clapperboard },
    ],
  },
  {
    label: 'Core',
    items: [
      { href: '/operations/board-mode', label: 'Board Mode', icon: LayoutDashboard },
      { href: '/operations/blocker-board', label: 'Blocker Board', icon: AlertTriangle },
      { href: '/operations/bottleneck-heatmap', label: 'Bottleneck Heatmap', icon: BarChart3 },
      { href: '/operations/governed-decision-loop', label: 'Decision Loop', icon: Scale },
      { href: '/operations/decision-receipts', label: 'Decision Receipts', icon: FileText },
      { href: '/operations/digest', label: 'Digest Center', icon: FileText },
      { href: '/operations/approvals', label: 'Approvals', icon: CheckSquare },
      { href: '/operations/trust-audit', label: 'Trust & Audit', icon: Shield },
    ],
  },
  {
    label: 'Intelligence',
    items: [
      { href: '/operations/prism/pulse', label: 'Pulse', icon: Heart },
      { href: '/operations/prism/risk', label: 'Risk', icon: AlertTriangle },
      { href: '/operations/prism/intelligence', label: 'Intelligence', icon: Brain },
      { href: '/operations/prism/signals', label: 'Signals Feed', icon: Radio },
      { href: '/operations/prism/motion', label: 'Motion', icon: Workflow },
      { href: '/operations/living-topology', label: 'Living Topology', icon: Network },
      { href: '/operations/ai-ops', label: 'AI Quality Dashboard', icon: Brain },
      { href: '/operations/gpu-observatory', label: 'GPU & AI Observatory', icon: Cpu },
      { href: '/operations/business-signals', label: 'Business Signals', icon: DollarSign },
      { href: '/operations/predictive-intelligence', label: 'Predictive Intel', icon: TrendingUp },
      { href: '/operations/cognitive-runtime', label: 'Cognitive Runtime', icon: Brain },
      { href: '/operations/revenue-impact', label: 'Revenue Impact', icon: DollarSign },
      { href: '/operations/self-healing', label: 'Self-Healing Ops', icon: RotateCcw },
      { href: '/operations/failure-timeline', label: 'Failure Timeline', icon: TrendingUp },
      { href: '/operations/client-value', label: 'Client Value', icon: Users },
      { href: '/operations/ops-savings', label: 'Ops Savings', icon: Calculator },
      { href: '/operations/escalation-intelligence', label: 'Escalation Intel', icon: Brain },
      { href: '/operations/outcome-loop', label: 'Outcome Loop', icon: Activity },
      { href: '/operations/defer-lane', label: 'Defer Lane', icon: Gauge },
      { href: '/operations/shadow-mode', label: 'Shadow Mode', icon: Brain },
    ],
  },
  {
    label: 'Observability',
    items: [
      { href: '/operations/slo', label: 'SLO / SLI Management', icon: Target },
      { href: '/operations/finops', label: 'FinOps & Cloud Cost', icon: DollarSign },
      { href: '/operations/tracing', label: 'Distributed Tracing', icon: GitBranch },
      { href: '/operations/logs', label: 'Log Analytics', icon: Database },
      { href: '/operations/on-call', label: 'On-Call Management', icon: Phone },
      { href: '/operations/change-management', label: 'Change Intelligence', icon: Calendar },
      { href: '/operations/synthetic', label: 'Synthetic Monitoring', icon: Globe },
      { href: '/operations/capacity-planning', label: 'Capacity Planning', icon: Layers },
    ],
  },
  {
    label: 'Autonomous NOC',
    items: [
      { href: '/operations/autonomous-noc', label: 'Autonomous NOC', icon: Bot },
      { href: '/operations/noise-reduction', label: 'Noise Reduction', icon: BellOff },
      { href: '/operations/knowledge-graph', label: 'Knowledge Graph', icon: Network },
      { href: '/operations/runbook-studio', label: 'Runbook Studio', icon: BookOpen },
      { href: '/operations/dex', label: 'DEX Scoring', icon: Monitor },
      { href: '/operations/msp-command', label: 'MSP Command', icon: Building },
      { href: '/operations/dev-feedback', label: 'Dev Feedback', icon: Code },
    ],
  },
  {
    label: 'Operations',
    items: [
      { href: '/operations/inbox', label: 'Inbox', icon: Inbox },
      { href: '/operations/explorer', label: 'Explorer', icon: Search },
      { href: '/operations/topology', label: 'Topology', icon: Network },
      { href: '/operations/logs', label: 'Log Explorer', icon: Database },
      { href: '/operations/alert-management', label: 'Alert Management', icon: Bell },
      { href: '/operations/executive-summary', label: 'Executive Summary', icon: BarChart3 },
      { href: '/operations/ownership', label: 'Ownership', icon: Users },
      { href: '/operations/workflows', label: 'Workflows', icon: Workflow },
      { href: '/operations/readiness', label: 'Readiness', icon: Shield },
      { href: '/operations/continuum/actions', label: 'Action Queue', icon: Activity },
      { href: '/operations/continuum/templates', label: 'Templates', icon: Workflow },
      { href: '/operations/continuum/gates', label: 'Write-Back Gates', icon: CheckSquare },
      { href: '/operations/continuum/intelligence', label: 'Intelligence Fabric', icon: Zap },
      { href: '/operations/continuum/canvas', label: 'Workflow Canvas', icon: Brain },
      { href: '/operations/continuum/agents', label: 'Agent Monitor', icon: Radio },
      { href: '/operations/continuum/traces', label: 'Execution Traces', icon: Activity },
      { href: '/operations/continuum/governance', label: 'Governance', icon: Shield },
      { href: '/operations/continuum/integrations', label: 'Integration Health', icon: Network },
      { href: '/operations/continuum/compiler', label: 'Graph Compiler', icon: GitBranch },
      { href: '/operations/continuum/replay', label: 'Replay Timeline', icon: Play },
      { href: '/operations/continuum/simulate', label: 'Policy Simulator', icon: Shield },
      { href: '/operations/continuum/handoffs', label: 'Agent Handoffs', icon: Send },
      { href: '/operations/continuum/receipts', label: 'Trust Receipts', icon: FileText },
    ],
  },
];

const ADMIN_NAV = [
  { href: '/operations/admin/ops', label: 'Ops Console', icon: Network },
  { href: '/operations/admin/overview', label: 'System', icon: Settings },
  { href: '/operations/admin/settings', label: 'Platform Settings', icon: Settings },
  { href: '/operations/admin/tenant-health', label: 'Tenant Health', icon: Building },
  { href: '/operations/admin/users', label: 'Users', icon: Users },
  { href: '/operations/admin/flags', label: 'Flags', icon: Flag },
  { href: '/operations/admin/runtime-config', label: 'Runtime Config', icon: Sliders },
  { href: '/operations/admin/runtime-config/history', label: 'Runtime Config History', icon: History },
  { href: '/operations/admin/apps', label: 'Apps', icon: Boxes },
  { href: '/operations/admin/runs', label: 'Runs', icon: Play },
  { href: '/operations/admin/approvals', label: 'Queue', icon: CheckSquare },
  { href: '/operations/admin/audit', label: 'Audit', icon: FileText },
  { href: '/operations/admin/exports', label: 'Exports', icon: Download },
  { href: '/operations/admin/seeder', label: 'Seeder', icon: Database },
  { href: '/operations/admin/jobs', label: 'Jobs', icon: Activity },
  { href: '/operations/admin/kb', label: 'Knowledge Base', icon: BookOpen },
  { href: '/operations/admin/support', label: 'Support Ops', icon: Headphones },
];

const ADMIN_ROLES = ['admin', 'super_admin', 'ops'];

const PRISM_COLORS: Record<string, string> = {
  Pulse: '#d4a054',
  Risk: '#c45a4a',
  Intelligence: '#8b7ac8',
  Signals: '#c8953c',
  Motion: '#4a90b8',
};

function NavItem({
  href,
  icon: Icon,
  label,
  isActive,
  accent,
  onClick,
}: {
  href: string;
  icon: any;
  label: string;
  isActive: boolean;
  accent?: string;
  onClick?: () => void;
}) {
  return (
    <Link
      href={href}
      onClick={onClick}
      className={cn(
        'flex items-center gap-2 px-2.5 py-[5px] text-[10px] font-medium transition-all relative group',
        isActive ? '' : 'hover:bg-white/[0.03]',
      )}
      style={{ color: isActive ? (accent ?? '#d4a054') : TEXT.secondary }}
    >
      {isActive && (
        <div
          className="absolute left-0 top-1 bottom-1 w-[2px] rounded-r"
          style={{ background: accent ?? '#d4a054' }}
        />
      )}
      <Icon
        className="w-3 h-3 shrink-0"
        style={{
          color: isActive ? (accent ?? '#d4a054') : TEXT.tertiary,
          opacity: isActive ? 1 : 0.7,
        }}
      />
      <span>{label}</span>
    </Link>
  );
}

function DemoModeToggle() {
  const { state, activate, deactivate } = useDemoMode();
  const isActive = state.active;
  return (
    <div
      className="mx-1.5 mb-1"
      style={{ borderTop: `1px solid rgba(255,255,255,0.04)`, paddingTop: 6 }}
    >
      <button
        onClick={() => (isActive ? deactivate() : activate(state.currentScenario))}
        className="flex items-center justify-between w-full px-2.5 py-1.5 rounded transition-all text-[9px] font-medium"
        style={{
          background: isActive ? 'rgba(212,160,84,0.08)' : 'rgba(255,255,255,0.02)',
          border: `1px solid ${isActive ? 'rgba(212,160,84,0.2)' : 'rgba(255,255,255,0.05)'}`,
          color: isActive ? '#d4a054' : TEXT.tertiary,
        }}
        aria-label={isActive ? 'Deactivate demo mode' : 'Activate demo mode'}
        aria-pressed={isActive}
      >
        <div className="flex items-center gap-1.5">
          <Power className="w-2.5 h-2.5" />
          <span className="font-mono uppercase tracking-wider">Demo Mode</span>
        </div>
        <div className="flex items-center gap-1">
          {isActive && (
            <span className="relative flex h-1.5 w-1.5">
              <span
                className="animate-ping absolute inline-flex h-full w-full rounded-full opacity-60"
                style={{ background: '#d4a054' }}
              />
              <span
                className="relative inline-flex rounded-full h-1.5 w-1.5"
                style={{ background: '#d4a054' }}
              />
            </span>
          )}
          <span
            className="text-[8px] font-mono"
            style={{ color: isActive ? '#d4a054' : TEXT.muted }}
          >
            {isActive ? 'ON' : 'OFF'}
          </span>
        </div>
      </button>
    </div>
  );
}

function AdminSection({ location }: { location: string }) {
  const [open, setOpen] = useState(false);
  const { user } = useAuth();
  const userRoles: string[] = (user as { roles?: string[] })?.roles ?? [];
  const isAdmin = userRoles.some((r) => ADMIN_ROLES.includes(r));
  if (!isAdmin) return null;
  const isInAdmin = location.startsWith('/operations/admin');
  if (!open && !isInAdmin) {
    return (
      <div className="mt-1 pt-1" style={{ borderTop: `1px solid ${BORDER.subtle}` }}>
        <button
          onClick={() => setOpen(true)}
          className="flex items-center gap-2 px-2.5 py-[5px] text-[10px] font-medium w-full transition-all hover:bg-white/[0.03]"
          style={{ color: TEXT.tertiary }}
        >
          <Settings className="w-3 h-3 shrink-0" />
          <span>Settings</span>
          <ChevronDown className="w-2.5 h-2.5 ml-auto" />
        </button>
      </div>
    );
  }
  return (
    <div className="mt-1 pt-1" style={{ borderTop: `1px solid ${BORDER.subtle}` }}>
      <button onClick={() => setOpen(false)} className="flex items-center gap-2 px-2.5 pb-1 w-full">
        <span
          className="text-[8px] uppercase tracking-widest font-medium"
          style={{ color: TEXT.muted }}
        >
          Settings
        </span>
        <ChevronDown className="w-2.5 h-2.5 ml-auto rotate-180" style={{ color: TEXT.muted }} />
      </button>
      {ADMIN_NAV.map((item) => {
        const isActive = location === item.href || location.startsWith(item.href + '/') || (item.href !== '/operations/admin' && location.startsWith(item.href));
        return (
          <NavItem
            key={item.href}
            href={item.href}
            icon={item.icon}
            label={item.label}
            isActive={isActive}
          />
        );
      })}
    </div>
  );
}

export function CommandLayout({ children }: { children: ReactNode }) {
  const [location] = useLocation();
  const [sidebarOpen, setSidebarOpen] = useState(false);
  const { status: wsStatus } = useRealtimeChannel('command-metrics');
  const { replay: replayOnboarding } = useOnboardingState('command');
  const { sandboxActive } = useSandboxMode();
  const isDemoMode =
    sandboxActive || new URLSearchParams(window.location.search).get('demo') === 'true';
  const { state: demoState } = useDemoMode();
  const isLiveDemoActive = demoState.active;

  return (
    <div className="flex h-full overflow-hidden">
      <a href="#main-content" className="skip-to-content">
        Skip to main content
      </a>
      {sidebarOpen && (
        <div
          className="fixed inset-0 bg-black/60 z-10 md:hidden"
          onClick={() => setSidebarOpen(false)}
          aria-hidden="true"
        />
      )}

      <aside
        className={cn(
          'flex flex-col shrink-0 relative z-20 transition-transform duration-200',
          'fixed md:relative inset-y-0 left-0 w-48',
          sidebarOpen ? 'translate-x-0' : '-translate-x-full md:translate-x-0',
        )}
        style={{ background: BG.sidebar, borderRight: `1px solid ${BORDER.subtle}` }}
        role="navigation"
        aria-label="Sidebar navigation"
      >
        <div
          className="h-12 flex items-center px-3"
          style={{ borderBottom: `1px solid ${BORDER.subtle}` }}
        >
          <div className="flex items-center gap-2">
            <div
              className="w-6 h-6 rounded flex items-center justify-center"
              style={{
                background: 'rgba(212,160,84,0.08)',
                border: `1px solid rgba(212,160,84,0.12)`,
              }}
            >
              <Zap className="w-3.5 h-3.5" style={{ color: '#d4a054' }} />
            </div>
            <div>
              <div
                className="text-[11px] font-bold tracking-wide leading-none"
                style={{ color: TEXT.primary }}
              >
                COMMAND
              </div>
              <div
                className="text-[7px] uppercase tracking-[0.15em] mt-px"
                style={{ color: 'rgba(212,160,84,0.5)' }}
              >
                Business Observability
              </div>
            </div>
          </div>
        </div>

        <div className="px-2 py-2" style={{ borderBottom: `1px solid ${BORDER.subtle}` }}>
          <div className="grid grid-cols-5 gap-0.5">
            {PRISM_ITEMS.map((p) => {
              const isActive = location.startsWith(p.href);
              return (
                <Link
                  key={p.key}
                  href={p.href}
                  className="flex flex-col items-center py-1 rounded transition-all"
                  style={{
                    background: isActive ? `${p.color}12` : 'transparent',
                    border: `1px solid ${isActive ? `${p.color}30` : 'transparent'}`,
                  }}
                >
                  <span className="text-[9px] font-black" style={{ color: p.color }}>
                    {p.key}
                  </span>
                  <span
                    className="text-[6px] uppercase tracking-wider"
                    style={{ color: `${p.color}70` }}
                  >
                    {p.label}
                  </span>
                </Link>
              );
            })}
          </div>
        </div>

        <div className="flex-1 min-h-0 flex flex-col">
          <nav className="flex-1 min-h-0 px-1.5 py-2 flex flex-col gap-px overflow-y-auto">
            {NAV_GROUPS.map((group, gi) => (
              <div
                key={group.label ?? gi}
                className={gi > 0 ? 'mt-1 pt-1' : ''}
                style={gi > 0 ? { borderTop: `1px solid ${BORDER.subtle}` } : {}}
              >
                {group.label && (
                  <div
                    className="px-2.5 pb-1 text-[8px] font-medium uppercase tracking-widest"
                    style={{ color: TEXT.muted }}
                  >
                    {group.label}
                  </div>
                )}
                {group.items.map((item) => {
                  const isPrism = item.href.startsWith('/operations/prism/');
                  const prismColor = isPrism ? PRISM_COLORS[item.label] : undefined;
                  const isActive =
                    item.href === '/operations'
                      ? location === '/operations'
                      : location.startsWith(item.href);
                  return (
                    <NavItem
                      key={item.href}
                      href={item.href}
                      icon={item.icon}
                      label={item.label}
                      isActive={isActive}
                      accent={prismColor}
                      onClick={() => setSidebarOpen(false)}
                    />
                  );
                })}
              </div>
            ))}
            <AdminSection location={location} />
          </nav>

          <DemoModeToggle />

          {COMMAND_ONBOARDING_CONFIG.checklist && (
            <div className="mx-2 mb-2">
              <GettingStartedChecklist
                appId={COMMAND_ONBOARDING_CONFIG.appId}
                appName={COMMAND_ONBOARDING_CONFIG.appName}
                items={COMMAND_ONBOARDING_CONFIG.checklist}
                accentColor={COMMAND_ONBOARDING_CONFIG.accentColor}
                onReplayTour={replayOnboarding}
                collapsed
              />
            </div>
          )}
          <div
            className="shrink-0 mx-2 mb-2 px-2.5 py-2 rounded"
            style={{
              background: 'rgba(212,160,84,0.02)',
              border: `1px solid rgba(212,160,84,0.05)`,
            }}
          >
            <div
              className="text-[7px] uppercase tracking-widest font-mono mb-1.5"
              style={{ color: TEXT.muted }}
            >
              System Pulse
            </div>
            <div className="space-y-1">
              {[
                { k: 'Urgent', v: '5', c: '#c45a4a' },
                { k: 'Gaps', v: '8', c: '#c8953c' },
                { k: 'Latency', v: '34h', c: TEXT.tertiary },
              ].map((r) => (
                <div key={r.k} className="flex justify-between text-[8px]">
                  <span style={{ color: TEXT.tertiary }}>{r.k}</span>
                  <span className="font-mono" style={{ color: r.c }}>
                    {r.v}
                  </span>
                </div>
              ))}
            </div>
            <div
              className="mt-1.5 h-px overflow-hidden rounded-full"
              style={{ background: 'rgba(255,255,255,0.04)' }}
            >
              <div
                className="h-full"
                style={{ width: '38%', background: 'linear-gradient(90deg, #c45a4a, #c8953c)' }}
              />
            </div>
            <div className="flex justify-between mt-px">
              <span className="text-[7px]" style={{ color: TEXT.muted }}>
                Resolution
              </span>
              <span className="text-[7px] font-mono" style={{ color: TEXT.tertiary }}>
                38%
              </span>
            </div>
          </div>
        </div>

        <div className="px-3 py-2" style={{ borderTop: `1px solid ${BORDER.subtle}` }}>
          <div
            className="flex items-center gap-1.5 text-[8px] mb-1.5"
            style={{ color: TEXT.muted }}
          >
            <Zap className="w-2.5 h-2.5" />
            <span className="font-mono tracking-wide">SZL Business OS</span>
          </div>
          <div className="flex gap-1">
            {[
              { label: 'DOMAINE', href: '/terra/', color: '#a07848' },
              { label: 'Counsel', href: '/continuum', color: '#4B8BDB' },
              { label: 'SEXTANT', href: '/vessels/', color: '#38bdf8' },
            ].map((p) => (
              <a
                key={p.label}
                href={p.href}
                className="text-[7px] px-1 py-px rounded font-mono hover:opacity-80"
                style={{
                  color: p.color,
                  background: `${p.color}10`,
                  border: `1px solid ${p.color}18`,
                }}
              >
                {p.label}
              </a>
            ))}
          </div>
        </div>
      </aside>

      <div className="flex-1 flex flex-col min-w-0">
        {isLiveDemoActive && (
          <div
            className="flex items-center justify-center gap-2.5 py-1 text-[9px] font-mono uppercase tracking-widest shrink-0"
            style={{
              background: 'rgba(212,160,84,0.12)',
              borderBottom: '1px solid rgba(212,160,84,0.25)',
              color: '#d4a054',
            }}
            role="status"
            aria-live="polite"
          >
            <span className="relative flex h-1.5 w-1.5">
              <span
                className="animate-ping absolute inline-flex h-full w-full rounded-full opacity-60"
                style={{ background: '#d4a054' }}
              />
              <span
                className="relative inline-flex rounded-full h-1.5 w-1.5"
                style={{ background: '#d4a054' }}
              />
            </span>
            <span>Demo Mode — Synthetic data only · No live systems connected</span>
            <span
              className="px-1.5 py-px rounded font-bold"
              style={{
                background: 'rgba(212,160,84,0.15)',
                border: '1px solid rgba(212,160,84,0.3)',
              }}
            >
              SEEDED
            </span>
          </div>
        )}
        <header
          className="h-10 flex items-center justify-between px-3 md:px-4 shrink-0 z-10"
          style={{
            borderBottom: `1px solid ${isLiveDemoActive ? 'rgba(212,160,84,0.15)' : BORDER.subtle}`,
            background: isLiveDemoActive ? 'rgba(212,160,84,0.03)' : BG.header,
            backdropFilter: 'blur(8px)',
          }}
        >
          <div className="flex items-center gap-2.5 text-[10px] font-mono">
            <button
              onClick={() => setSidebarOpen(!sidebarOpen)}
              className="md:hidden p-1 rounded hover:bg-white/5 mr-1"
              style={{ color: TEXT.secondary }}
              aria-label={sidebarOpen ? 'Close sidebar' : 'Open sidebar'}
              aria-expanded={sidebarOpen}
            >
              {sidebarOpen ? <X className="w-4 h-4" /> : <Menu className="w-4 h-4" />}
            </button>
            <span
              className="w-1 h-1 rounded-full animate-pulse hidden sm:block"
              style={{ background: '#c45a4a' }}
              aria-hidden="true"
            />
            <span className="hidden sm:block" style={{ color: '#c45a4a' }}>
              5 Urgent
            </span>
            <span className="hidden sm:block" style={{ color: TEXT.muted }}>
              ·
            </span>
            <span style={{ color: '#c8953c' }}>8 Gaps</span>
            <span className="hidden sm:block" style={{ color: TEXT.muted }}>
              ·
            </span>
            <span className="hidden sm:block" style={{ color: '#d4a054' }}>
              $5.03M at risk
            </span>
            {isDemoMode && !isLiveDemoActive && (
              <span className="hidden sm:flex items-center ml-1 gap-1">
                <span style={{ color: TEXT.muted }}>·</span>
                <EnvironmentLabel environment="demo" />
                <EnvironmentLabel environment="seeded" />
              </span>
            )}
          </div>
          <div className="flex items-center gap-2">
            <RealtimeStatusIndicator status={wsStatus} compact />
            <button
              className="relative p-1 rounded hover:bg-white/5 transition-colors"
              style={{ color: TEXT.secondary }}
              aria-label="Notifications"
            >
              <Bell className="w-3.5 h-3.5" />
              <span
                className="absolute top-0.5 right-0.5 w-1 h-1 rounded-full animate-pulse"
                style={{ background: '#d4a054' }}
                aria-hidden="true"
              />
            </button>
            <div className="h-4 w-px" style={{ background: BORDER.subtle }} />
            <div className="flex items-center gap-2">
              <div className="text-right hidden sm:block">
                <div className="text-[10px] font-medium" style={{ color: TEXT.primary }}>
                  Stephen Lutar
                </div>
                <div className="text-[8px] font-mono" style={{ color: 'rgba(212,160,84,0.45)' }}>
                  SZL Holdings
                </div>
              </div>
              <div
                className="w-6 h-6 rounded flex items-center justify-center text-[9px] font-bold"
                style={{
                  background: 'rgba(212,160,84,0.1)',
                  border: `1px solid rgba(212,160,84,0.15)`,
                  color: '#d4a054',
                }}
              >
                SL
              </div>
            </div>
          </div>
        </header>
        <main
          id="main-content"
          className="flex-1 overflow-auto"
          style={{ background: BG.main }}
        >
          <SectionErrorBoundary sectionName="KORA">{children}</SectionErrorBoundary>
        </main>
        <ServiceStatusRail />
      </div>
      <OnboardingWizard config={COMMAND_ONBOARDING_CONFIG} />
    </div>
  );
}