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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd73c0d
8fc5f54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd73c0d
8fc5f54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af9cde9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
"use client";

import React, { useState, useRef, useEffect } from "react";
import { createPortal } from "react-dom";
import {
  Activity,
  X,
  Server,
  MessageSquare,
  Wrench,
  Layers,
  Clock,
  Zap,
  CheckCircle2,
  XCircle,
  Loader2,
  DollarSign,
  ChevronDown,
  ChevronRight,

} from "lucide-react";
import { useObservability } from "@/hooks/useObservability";
import { Message } from "@/hooks/useConversation";

interface ObservabilityPanelProps {
  isOpen?: boolean;
  onToggle?: () => void;
  messages: Message[];
  isSessionActive: boolean;
  isConnected: boolean;
}

export function ObservabilityPanel({
  isOpen,
  onToggle,
  messages,
  isSessionActive,
  isConnected,
}: ObservabilityPanelProps) {
  const data = useObservability({ messages, isSessionActive });

  if (!isOpen) {
    return (
      <button
        onClick={onToggle}
        className="flex flex-col items-center gap-1 p-2 text-gray-400 hover:text-white transition-all"
        title="Observability"
      >
        <Activity className="w-5 h-5" />
        <span className="text-[10px]">Monitor</span>
      </button>
    );
  }

  return (
    <>
      <button onClick={onToggle} className="btn btn-secondary p-2">
        <Activity className="w-5 h-5" />
      </button>

      {createPortal(
        <div
          className="modal-overlay"
          onClick={(e) => {
            if (e.target === e.currentTarget) onToggle?.();
          }}
        >
          <div className="modal-fullscreen p-0">
            {/* Header β€” merged with status strip */}
            <div
              style={{
                padding: "16px 24px",
                borderBottom: "1px solid rgba(255, 255, 255, 0.08)",
                background: "var(--color-surface-subtle)",
                display: "flex",
                alignItems: "center",
                justifyContent: "space-between",
                flexShrink: 0,
              }}
            >
              <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
                <div
                  style={{
                    width: 36,
                    height: 36,
                    borderRadius: 10,
                    background: "var(--color-accent-pink)",
                    display: "flex",
                    alignItems: "center",
                    justifyContent: "center",
                    boxShadow: "0 2px 8px rgba(255, 193, 204, 0.3)",
                  }}
                >
                  <Activity className="w-5 h-5" style={{ color: "#1a1a1a" }} />
                </div>
                <div>
                  <h3
                    style={{
                      fontSize: 20,
                      fontWeight: 700,
                      color: "var(--color-text-primary)",
                      margin: 0,
                      letterSpacing: "-0.01em",
                    }}
                  >
                    System Monitor
                  </h3>
                  <p
                    style={{
                      fontSize: 11,
                      color: "var(--color-text-muted)",
                      textTransform: "uppercase",
                      letterSpacing: "0.1em",
                      fontWeight: 500,
                      margin: 0,
                    }}
                  >
                    Real-Time Observability
                  </p>
                </div>
              </div>

              {/* Inline status indicators */}
              <div style={{ display: "flex", alignItems: "center", gap: 20 }}>
                {/* Connection */}
                <div style={{ display: "flex", alignItems: "center", gap: 6 }}>
                  <div
                    style={{
                      width: 8,
                      height: 8,
                      borderRadius: "50%",
                      background: isConnected ? "var(--color-success)" : "var(--color-error)",
                      boxShadow: isConnected
                        ? "0 0 8px var(--color-success)"
                        : "0 0 8px var(--color-error)",
                    }}
                  />
                  <span
                    style={{
                      fontSize: 14,
                      fontWeight: 500,
                      color: isConnected ? "var(--color-success)" : "var(--color-error)",
                    }}
                  >
                    {isConnected ? "Connected" : "Disconnected"}
                  </span>
                </div>

                <div style={{ width: 1, height: 16, background: "rgba(255,255,255,0.1)" }} />

                {/* Session uptime */}
                <div style={{ display: "flex", alignItems: "center", gap: 6 }}>
                  <Clock className="w-3.5 h-3.5" style={{ color: "var(--color-text-muted)" }} />
                  <span style={{ fontSize: 14, color: "var(--color-text-secondary)" }}>
                    {isSessionActive && data.sessionUptimeSeconds != null
                      ? formatDuration(data.sessionUptimeSeconds)
                      : "No session"}
                  </span>
                </div>

                <div style={{ width: 1, height: 16, background: "rgba(255,255,255,0.1)" }} />

                {/* Events/sec */}
                <div style={{ display: "flex", alignItems: "center", gap: 6 }}>
                  <Zap className="w-3.5 h-3.5" style={{ color: "var(--color-accent-pink)" }} />
                  <span
                    style={{
                      fontSize: 14,
                      fontFamily: "var(--font-mono)",
                      color: "var(--color-accent-pink)",
                    }}
                  >
                    {data.eventsPerSecond.toFixed(1)} evt/s
                  </span>
                </div>

                <div style={{ width: 1, height: 16, background: "rgba(255,255,255,0.1)" }} />

                <button
                  onClick={onToggle}
                  className="btn btn-ghost p-1.5 hover:bg-surface-elevated rounded-lg transition-colors"
                >
                  <X className="w-5 h-5 text-secondary" />
                </button>
              </div>
            </div>

            {/* Content β€” 3-column bento grid */}
            <div
              style={{
                flex: 1,
                overflow: "hidden",
                display: "grid",
                gridTemplateColumns: "1fr 1fr 1.5fr",
                gridTemplateRows: "1fr 1fr",
                gap: 0,
                minHeight: 0,
              }}
            >
              {/* ===== LEFT COLUMN: Messages + GenUI ===== */}
              <div
                style={{
                  padding: 20,
                  borderRight: "1px solid rgba(255, 255, 255, 0.06)",
                  display: "flex",
                  flexDirection: "column",
                  gap: 24,
                  overflow: "auto",
                  minHeight: 0,
                }}
              >
                {/* Messages */}
                <div>
                  <SectionLabel icon={<MessageSquare className="w-3.5 h-3.5" />} label="Messages" />
                  <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr 1fr", gap: 10, marginTop: 10 }}>
                    <StatCard label="User" value={data.userMessageCount} color="var(--color-cta)" />
                    <StatCard label="Assistant" value={data.assistantMessageCount} color="var(--color-accent-cyan)" />
                    <StatCard label="Tool" value={data.toolMessageCount} color="var(--color-accent-pink)" />
                  </div>
                </div>

                {/* GenUI Components */}
                {data.genuiComponentCount > 0 && (
                  <div>
                    <SectionLabel icon={<Layers className="w-3.5 h-3.5" />} label="GenUI Components" />
                    <div style={{ marginTop: 10, display: "flex", flexWrap: "wrap", gap: 8 }}>
                      <span className="pill pill-lavender text-xs font-bold">
                        {data.genuiComponentCount} rendered
                      </span>
                      {data.genuiComponentNames.map((name) => (
                        <span key={name} className="pill pill-cyan text-xs">
                          {name}
                        </span>
                      ))}
                    </div>
                  </div>
                )}
              </div>

              {/* ===== MIDDLE COLUMN: Token Usage + Server ===== */}
              <div
                style={{
                  padding: 20,
                  borderRight: "1px solid rgba(255, 255, 255, 0.06)",
                  display: "flex",
                  flexDirection: "column",
                  gap: 24,
                  overflow: "auto",
                  minHeight: 0,
                }}
              >
                {/* Token Usage */}
                <div>
                  <SectionLabel icon={<DollarSign className="w-3.5 h-3.5" />} label="Token Usage" />
                  <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr 1fr", gap: 10, marginTop: 10 }}>
                    <StatCard
                      label="Input"
                      value={
                        data.costMetrics
                          ? formatTokenCount(data.costMetrics.input_text + data.costMetrics.input_audio)
                          : "β€”"
                      }
                      color="var(--color-cta)"
                    />
                    <StatCard
                      label="Output"
                      value={
                        data.costMetrics
                          ? formatTokenCount(data.costMetrics.output_text + data.costMetrics.output_audio)
                          : "β€”"
                      }
                      color="var(--color-accent-cyan)"
                    />
                    <StatCard
                      label="Est. Cost"
                      value={
                        data.costMetrics
                          ? `$${data.costMetrics.total_cost.toFixed(4)}`
                          : "β€”"
                      }
                      color="var(--color-success)"
                    />
                  </div>
                  {data.costMetrics && data.costMetrics.response_count > 0 && (
                    <div style={{ marginTop: 10, display: "flex", flexWrap: "wrap", gap: 6 }}>
                      <span className="pill pill-cyan text-[9px]">
                        {data.costMetrics.response_count} responses
                      </span>
                      {data.costMetrics.input_audio > 0 && (
                        <span className="pill pill-lavender text-[9px]">
                          πŸŽ™ {formatTokenCount(data.costMetrics.input_audio)} audio in
                        </span>
                      )}
                      {data.costMetrics.output_audio > 0 && (
                        <span className="pill pill-pink text-[9px]">
                          πŸ”Š {formatTokenCount(data.costMetrics.output_audio)} audio out
                        </span>
                      )}
                      {data.costMetrics.cache_hit_rate > 0 && (
                        <span
                          className="pill text-[9px] font-bold"
                          style={{
                            background: "rgba(34, 197, 94, 0.15)",
                            border: "1px solid rgba(34, 197, 94, 0.3)",
                            color: "var(--color-success)",
                          }}
                        >
                          πŸ’Ύ {Math.round(data.costMetrics.cache_hit_rate * 100)}% cached
                        </span>
                      )}
                    </div>
                  )}
                </div>

                {/* Server Stats */}
                <div>
                  <SectionLabel icon={<Server className="w-3.5 h-3.5" />} label="Server" />
                  <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr 1fr", gap: 10, marginTop: 10 }}>
                    <StatCard
                      label="Clients"
                      value={data.serverMetrics?.connected_clients ?? "β€”"}
                      color="var(--color-accent-cyan)"
                    />
                    <StatCard
                      label="Uptime"
                      value={
                        data.serverMetrics?.uptime_seconds != null
                          ? formatDuration(data.serverMetrics.uptime_seconds)
                          : "β€”"
                      }
                      color="var(--color-success)"
                    />
                    <StatCard
                      label="Events"
                      value={data.serverMetrics?.total_events_broadcast ?? "β€”"}
                      color="var(--color-cta)"
                    />
                  </div>
                </div>
              </div>

              {/* ===== RIGHT COLUMN: Tool Calls ===== */}
              <div
                style={{
                  padding: 20,
                  display: "flex",
                  flexDirection: "column",
                  overflow: "hidden",
                  minHeight: 0,
                }}
              >
                <SectionLabel icon={<Wrench className="w-3.5 h-3.5" />} label="Tool Calls" />
                <div
                  style={{
                    marginTop: 10,
                    flex: 1,
                    overflowY: "auto",
                    display: "flex",
                    flexDirection: "column",
                    gap: 6,
                  }}
                >
                  {data.toolCalls.length === 0 ? (
                    <p
                      style={{
                        fontSize: 12,
                        color: "var(--color-text-muted)",
                        fontStyle: "italic",
                        textAlign: "center",
                        padding: "24px 0",
                      }}
                    >
                      No tool calls yet this session
                    </p>
                  ) : (
                    data.toolCalls.map((tool) => (
                      <ToolCallRow key={tool.id} tool={tool} />
                    ))
                  )}
                </div>
              </div>

              {/* ===== BOTTOM ROW: Conversation Log (full-width) ===== */}
              <div
                style={{
                  gridColumn: "1 / -1",
                  borderTop: "1px solid rgba(255, 255, 255, 0.06)",
                  overflow: "hidden",
                  minHeight: 0,
                }}
              >
                <ConversationLog messages={messages} />
              </div>
            </div>

            {/* Footer */}
            <div
              style={{
                padding: "12px 24px",
                background: "var(--color-surface-subtle)",
                borderTop: "1px solid rgba(255, 255, 255, 0.08)",
                display: "flex",
                alignItems: "center",
                justifyContent: "space-between",
                flexShrink: 0,
              }}
            >
              <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
                <div
                  style={{
                    padding: "2px 8px",
                    borderRadius: 4,
                    background: "rgba(255, 193, 204, 0.1)",
                    border: "1px solid rgba(255, 193, 204, 0.2)",
                    color: "var(--color-accent-pink)",
                    fontSize: 9,
                    fontWeight: 900,
                    textTransform: "uppercase",
                    letterSpacing: "0.15em",
                  }}
                >
                  Dev
                </div>
                <p style={{ fontSize: 11, color: "var(--color-text-muted)", fontWeight: 500, margin: 0 }}>
                  WS events: <strong>{data.totalWsEvents}</strong> this session
                </p>
              </div>
              <Zap className="w-4 h-4" style={{ color: "rgba(255, 193, 204, 0.3)" }} />
            </div>
          </div>
        </div>,
        document.body
      )}
    </>
  );
}

// ---- Sub-components ----

function SectionLabel({ icon, label }: { icon: React.ReactNode; label: string }) {
  return (
    <div
      style={{
        display: "flex",
        alignItems: "center",
        gap: 8,
        fontSize: 12,
        fontWeight: 900,
        textTransform: "uppercase",
        letterSpacing: "0.15em",
        color: "var(--color-text-muted)",
      }}
    >
      {icon}
      {label}
    </div>
  );
}

function StatCard({
  label,
  value,
  color,
}: {
  label: string;
  value: string | number;
  color: string;
}) {
  return (
    <div
      style={{
        padding: "14px 12px",
        background: "rgba(36, 36, 36, 0.6)",
        border: "1px solid rgba(255, 255, 255, 0.08)",
        borderRadius: 12,
        textAlign: "center",
      }}
    >
      <div
        style={{
          fontSize: 28,
          fontWeight: 700,
          color,
          fontFamily: "var(--font-mono)",
          lineHeight: 1.2,
        }}
      >
        {value}
      </div>
      <div
        style={{
          fontSize: 12,
          fontWeight: 600,
          color: "var(--color-text-muted)",
          textTransform: "uppercase",
          letterSpacing: "0.1em",
          marginTop: 4,
        }}
      >
        {label}
      </div>
    </div>
  );
}

function ToolCallRow({ tool }: { tool: { id: string; name: string; status: string; duration: number | null } }) {
  const statusIcon =
    tool.status === "completed" ? (
      <CheckCircle2 className="w-3.5 h-3.5" style={{ color: "var(--color-success)" }} />
    ) : tool.status === "error" ? (
      <XCircle className="w-3.5 h-3.5" style={{ color: "var(--color-error)" }} />
    ) : (
      <Loader2 className="w-3.5 h-3.5 animate-spin" style={{ color: "var(--color-accent-cyan)" }} />
    );

  return (
    <div
      style={{
        display: "flex",
        alignItems: "center",
        justifyContent: "space-between",
        padding: "8px 12px",
        background: "rgba(36, 36, 36, 0.5)",
        border: "1px solid rgba(255, 255, 255, 0.06)",
        borderRadius: 8,
      }}
    >
      <div className="flex items-center gap-2">
        {statusIcon}
        <span style={{ fontSize: 14, fontWeight: 500, color: "var(--color-text-primary)" }}>
          {tool.name}
        </span>
      </div>
      <div className="flex items-center gap-2">
        {tool.duration != null && (
          <span
            style={{
              fontSize: 10,
              fontFamily: "var(--font-mono)",
              color: "var(--color-text-muted)",
            }}
          >
            {tool.duration}ms
          </span>
        )}
        <span
          className={`pill text-[9px] font-bold ${
            tool.status === "completed"
              ? "pill-cyan"
              : tool.status === "error"
              ? "pill-pink"
              : "pill-lavender"
          }`}
        >
          {tool.status}
        </span>
      </div>
    </div>
  );
}

// ---- Conversation Log (Chat-style) ----

function relativeTime(timestamp: string): string {
  try {
    const now = Date.now();
    const then = new Date(timestamp).getTime();
    const diff = Math.max(0, Math.floor((now - then) / 1000));
    if (diff < 5) return "just now";
    if (diff < 60) return `${diff}s ago`;
    const mins = Math.floor(diff / 60);
    if (mins < 60) return `${mins}m ago`;
    const hrs = Math.floor(mins / 60);
    return `${hrs}h ${mins % 60}m ago`;
  } catch {
    return "";
  }
}

function fullTime(timestamp: string): string {
  try {
    return new Date(timestamp).toLocaleTimeString("en-GB", {
      hour: "2-digit",
      minute: "2-digit",
      second: "2-digit",
    });
  } catch {
    return "";
  }
}

/** Friendly tool name: "log_entry" β†’ "Log Entry" */
function friendlyToolName(name: string): string {
  return name
    .replace(/_/g, " ")
    .replace(/\b\w/g, (c) => c.toUpperCase());
}

function ToolDetailBlock({ data, label }: { data: Record<string, unknown>; label: string }) {
  const [expanded, setExpanded] = useState(false);

  const keys = Object.keys(data);
  const summary =
    keys.length <= 3
      ? keys
          .map((k) => {
            const v = data[k];
            const display =
              typeof v === "string"
                ? v.length > 40
                  ? v.slice(0, 40) + "…"
                  : v
                : JSON.stringify(v);
            return `${k}: ${display}`;
          })
          .join(", ")
      : `${keys.length} fields`;

  return (
    <div style={{ marginTop: 4 }}>
      <button
        onClick={() => setExpanded((p) => !p)}
        style={{
          background: "none",
          border: "none",
          cursor: "pointer",
          padding: 0,
          display: "inline-flex",
          alignItems: "center",
          gap: 4,
          fontSize: 11,
          color: "var(--color-text-muted)",
        }}
      >
        <span style={{ fontSize: 9, opacity: 0.7 }}>{expanded ? "β–Ύ" : "β–Έ"}</span>
        <span
          style={{
            padding: "1px 5px",
            borderRadius: 3,
            background: "rgba(255, 255, 255, 0.05)",
            border: "1px solid rgba(255, 255, 255, 0.08)",
            fontSize: 9,
            fontWeight: 700,
            textTransform: "uppercase",
            letterSpacing: "0.05em",
          }}
        >
          {label}
        </span>
        {!expanded && (
          <span style={{ opacity: 0.5, fontSize: 11, fontFamily: "var(--font-mono)" }}>
            {summary}
          </span>
        )}
      </button>
      {expanded && (
        <pre
          style={{
            marginTop: 4,
            padding: "8px 10px",
            background: "rgba(20, 20, 20, 0.8)",
            border: "1px solid rgba(255, 255, 255, 0.06)",
            borderRadius: 6,
            fontSize: 11,
            lineHeight: 1.5,
            fontFamily: "var(--font-mono)",
            color: "var(--color-text-secondary)",
            overflowX: "auto",
            maxHeight: 200,
            whiteSpace: "pre-wrap",
            wordBreak: "break-word",
          }}
        >
          {JSON.stringify(data, null, 2)}
        </pre>
      )}
    </div>
  );
}

function ConversationLog({ messages }: { messages: Message[] }) {
  const [isOpen, setIsOpen] = useState(true);
  const scrollRef = useRef<HTMLDivElement>(null);
  // Re-render every 30s so relative timestamps update
  const [, setTick] = useState(0);
  useEffect(() => {
    const id = setInterval(() => setTick((t) => t + 1), 30_000);
    return () => clearInterval(id);
  }, []);

  // No auto-scroll needed β€” newest messages are at the top

  // Newest first
  const logMessages = messages.filter((m) => !m.isPartial).slice().reverse();

  return (
    <div style={{ display: "flex", flexDirection: "column", height: "100%" }}>
      {/* Header toggle */}
      <button
        onClick={() => setIsOpen((prev) => !prev)}
        style={{
          display: "flex",
          alignItems: "center",
          gap: 6,
          width: "100%",
          background: "none",
          border: "none",
          padding: "12px 20px",
          cursor: "pointer",
        }}
      >
        <SectionLabel
          icon={<MessageSquare className="w-3.5 h-3.5" />}
          label="Conversation"
        />
        <div style={{ marginLeft: "auto", display: "flex", alignItems: "center", gap: 4 }}>
          {logMessages.length > 0 && (
            <span className="pill pill-cyan text-[9px] font-bold">{logMessages.length}</span>
          )}
          {isOpen ? (
            <ChevronDown className="w-3.5 h-3.5 text-muted" />
          ) : (
            <ChevronRight className="w-3.5 h-3.5 text-muted" />
          )}
        </div>
      </button>

      {isOpen && (
        <div
          ref={scrollRef}
          style={{
            flex: 1,
            overflowY: "auto",
            background: "rgba(18, 18, 18, 0.95)",
            borderTop: "1px solid rgba(255, 255, 255, 0.06)",
            padding: "16px 20px",
            display: "flex",
            flexDirection: "column",
            gap: 6,
          }}
        >
          {logMessages.length === 0 ? (
            <div
              style={{
                display: "flex",
                flexDirection: "column",
                alignItems: "center",
                justifyContent: "center",
                gap: 8,
                padding: "32px 0",
                color: "var(--color-text-muted)",
              }}
            >
              <MessageSquare className="w-6 h-6" style={{ opacity: 0.3 }} />
              <p style={{ fontSize: 13, fontStyle: "italic", margin: 0 }}>
                Start talking to see the conversation here
              </p>
            </div>
          ) : (
            logMessages.map((msg) => {
              const isUser = msg.role === "user";

              const isTool = msg.role === "tool" && msg.tool;
              const isSystem = msg.role === "system";
              const isGenUI = msg.role === "assistant" && !!msg.component;

              // ── System event chip (centered) ──
              if (isSystem) {
                return (
                  <div
                    key={msg.id}
                    style={{
                      display: "flex",
                      justifyContent: "center",
                      padding: "4px 0",
                    }}
                  >
                    <span
                      title={fullTime(msg.timestamp)}
                      style={{
                        display: "inline-flex",
                        alignItems: "center",
                        gap: 6,
                        padding: "4px 14px",
                        borderRadius: 20,
                        background: "rgba(255, 255, 255, 0.04)",
                        border: "1px solid rgba(255, 255, 255, 0.08)",
                        fontSize: 11,
                        color: "var(--color-text-muted)",
                        fontWeight: 500,
                      }}
                    >
                      {msg.content}
                    </span>
                  </div>
                );
              }

              // ── Tool call badge (compact inline) ──
              if (isTool) {
                const toolStatus = msg.tool!.status;
                const statusEmoji =
                  toolStatus === "completed" ? "βœ“" : toolStatus === "error" ? "βœ—" : "β‹―";
                const statusColor =
                  toolStatus === "completed"
                    ? "var(--color-success)"
                    : toolStatus === "error"
                    ? "var(--color-error)"
                    : "var(--color-accent-cyan)";

                return (
                  <div key={msg.id} style={{ padding: "2px 0" }}>
                    <div
                      style={{
                        display: "inline-flex",
                        alignItems: "center",
                        gap: 6,
                        padding: "5px 12px",
                        borderRadius: 8,
                        background: "rgba(255, 193, 204, 0.06)",
                        border: "1px solid rgba(255, 193, 204, 0.12)",
                        fontSize: 12,
                        color: "var(--color-text-secondary)",
                      }}
                    >
                      <span style={{ fontSize: 13 }}>πŸ› </span>
                      <span style={{ fontWeight: 600, color: "var(--color-accent-pink)" }}>
                        {friendlyToolName(msg.tool!.name)}
                      </span>
                      <span
                        style={{
                          fontWeight: 700,
                          fontSize: 11,
                          color: statusColor,
                        }}
                      >
                        {statusEmoji}
                      </span>
                      <span
                        title={fullTime(msg.timestamp)}
                        style={{ fontSize: 10, color: "var(--color-text-muted)", marginLeft: 4 }}
                      >
                        {relativeTime(msg.timestamp)}
                      </span>
                    </div>
                    {/* Expandable args/result (hidden by default) */}
                    {msg.tool!.args && Object.keys(msg.tool!.args).length > 0 && (
                      <div style={{ marginLeft: 16, marginTop: 2 }}>
                        <ToolDetailBlock data={msg.tool!.args} label="Input" />
                      </div>
                    )}
                    {msg.tool!.result && Object.keys(msg.tool!.result).length > 0 && (
                      <div style={{ marginLeft: 16, marginTop: 2 }}>
                        <ToolDetailBlock data={msg.tool!.result} label="Output" />
                      </div>
                    )}
                  </div>
                );
              }

              // ── GenUI badge (compact) ──
              if (isGenUI) {
                return (
                  <div key={msg.id} style={{ padding: "2px 0" }}>
                    <div
                      style={{
                        display: "inline-flex",
                        alignItems: "center",
                        gap: 6,
                        padding: "5px 12px",
                        borderRadius: 8,
                        background: "rgba(168, 218, 220, 0.06)",
                        border: "1px solid rgba(168, 218, 220, 0.12)",
                        fontSize: 12,
                      }}
                    >
                      <span style={{ fontSize: 13 }}>πŸ–₯</span>
                      <span style={{ fontWeight: 600, color: "var(--color-accent-cyan)" }}>
                        {msg.component!.name}
                      </span>
                      <span style={{ fontSize: 10, color: "var(--color-text-muted)" }}>shown</span>
                      <span
                        title={fullTime(msg.timestamp)}
                        style={{ fontSize: 10, color: "var(--color-text-muted)", marginLeft: 4 }}
                      >
                        {relativeTime(msg.timestamp)}
                      </span>
                    </div>
                    {Object.keys(msg.component!.props).length > 0 && (
                      <div style={{ marginLeft: 16, marginTop: 2 }}>
                        <ToolDetailBlock data={msg.component!.props} label="Props" />
                      </div>
                    )}
                  </div>
                );
              }

              // ── Chat bubble (user = left, assistant = right) ──
              return (
                <div
                  key={msg.id}
                  style={{
                    display: "flex",
                    flexDirection: "column",
                    alignItems: isUser ? "flex-start" : "flex-end",
                    maxWidth: "85%",
                    alignSelf: isUser ? "flex-start" : "flex-end",
                  }}
                >
                  {/* Sender label */}
                  <span
                    style={{
                      fontSize: 10,
                      fontWeight: 700,
                      textTransform: "uppercase",
                      letterSpacing: "0.08em",
                      color: isUser ? "var(--color-cta)" : "var(--color-accent-cyan)",
                      marginBottom: 3,
                      paddingLeft: isUser ? 2 : 0,
                      paddingRight: isUser ? 0 : 2,
                    }}
                  >
                    {isUser ? "You" : "Reachy"}
                  </span>
                  {/* Bubble */}
                  <div
                    style={{
                      padding: "10px 14px",
                      borderRadius: isUser ? "2px 14px 14px 14px" : "14px 2px 14px 14px",
                      background: isUser
                        ? "rgba(255, 255, 255, 0.06)"
                        : "rgba(168, 218, 220, 0.10)",
                      border: isUser
                        ? "1px solid rgba(255, 255, 255, 0.10)"
                        : "1px solid rgba(168, 218, 220, 0.18)",
                      fontSize: 14,
                      lineHeight: 1.6,
                      color: "var(--color-text-primary)",
                      wordBreak: "break-word",
                    }}
                  >
                    {msg.content || "(no text)"}
                  </div>
                  {/* Timestamp */}
                  <span
                    title={fullTime(msg.timestamp)}
                    style={{
                      fontSize: 10,
                      color: "var(--color-text-muted)",
                      marginTop: 3,
                      paddingLeft: isUser ? 2 : 0,
                      paddingRight: isUser ? 0 : 2,
                      opacity: 0.7,
                    }}
                  >
                    {relativeTime(msg.timestamp)}
                  </span>
                </div>
              );
            })
          )}
        </div>
      )}
    </div>
  );
}

// ---- Helpers ----

function formatDuration(seconds: number): string {
  const mins = Math.floor(seconds / 60);
  const secs = Math.floor(seconds % 60);
  if (mins > 0) {
    return `${mins}m ${secs.toString().padStart(2, "0")}s`;
  }
  return `${secs}s`;
}

function formatTokenCount(count: number): string {
  if (count >= 1_000_000) {
    return `${(count / 1_000_000).toFixed(1)}M`;
  }
  if (count >= 10_000) {
    return `${(count / 1_000).toFixed(1)}k`;
  }
  return new Intl.NumberFormat().format(count);
}