aboutsummaryrefslogtreecommitdiffstats
path: root/claude_logbook/template.html
blob: c018320ad96a219396702f637b7881ced219e366 (plain)
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
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light dark">
<meta name="robots" content="noindex, nofollow">
<title>Claude Code sessions</title>

<script>
  // Runs before the first paint so the chosen theme does not flicker:
  // the saved preference first, otherwise the system one.
  (function () {
    var saved = null;
    try { saved = localStorage.getItem('cl-theme'); } catch (e) {}
    var dark = saved ? saved === 'dark'
                     : matchMedia('(prefers-color-scheme: dark)').matches;
    document.documentElement.dataset.theme = dark ? 'dark' : 'light';
  })();
</script>

<style>
  /* One palette per theme and nothing else: data-theme is set by the script
     above before painting, so there is no need to repeat the values inside an
     @media. The toolbar button toggles that same attribute. */

  :root, :root[data-theme="light"] {
    --ground:   #EDEEF2;
    --surface:  #FFFFFF;
    --raised:   #F6F7F9;
    --ink:      #14161C;
    --ink-soft: #3D4250;
    --muted:    #6A6F7E;
    --faint:    #9AA0AE;
    --line:     #DCDEE5;
    --line-soft:#E7E9EE;
    --amber:    #A85A16;
    --amber-lo: #C98A47;
    --amber-bg: rgba(168, 90, 22, 0.09);
    --scrim:    rgba(20, 22, 28, .38);
    --shadow:   0 1px 2px rgba(20, 22, 28, .06), 0 8px 24px -12px rgba(20, 22, 28, .18);
    --shadow-lg:0 24px 70px -20px rgba(20, 22, 28, .45);
    --focus:    #2C6FD1;

    --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, "JetBrains Mono", "SFMono-Regular", "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  }

  :root[data-theme="dark"] {
    --ground:   #0E1015;
    --surface:  #171A21;
    --raised:   #1D212A;
    --ink:      #DFE2EA;
    --ink-soft: #B4BAC7;
    --muted:    #878DA0;
    --faint:    #666C7D;
    --line:     #262A34;
    --line-soft:#1F232C;
    --amber:    #E29A4A;
    --amber-lo: #8A6435;
    --amber-bg: rgba(226, 154, 74, 0.11);
    --scrim:    rgba(0, 0, 0, .62);
    --shadow:   0 1px 2px rgba(0,0,0,.4), 0 8px 24px -12px rgba(0,0,0,.7);
    --shadow-lg:0 24px 70px -20px rgba(0,0,0,.85);
    --focus:    #6BA5F0;
  }

  * { box-sizing: border-box; }

  body {
    margin: 0;
    background: var(--ground);
    color: var(--ink);
    font-family: var(--sans);
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
  }
  body.locked { overflow: hidden; }

  .wrap { max-width: 1180px; margin: 0 auto; padding: 40px 24px 96px; }

  /* ---------- masthead ---------- */

  .masthead { display: flex; flex-direction: column; gap: 8px; margin-bottom: 28px; }

  .eyebrow {
    font-family: var(--mono);
    font-size: 11px;
    letter-spacing: .14em;
    text-transform: uppercase;
    color: var(--amber);
  }

  h1 {
    margin: 0;
    font-size: clamp(28px, 4.5vw, 40px);
    line-height: 1.1;
    letter-spacing: -.022em;
    font-weight: 620;
    text-wrap: balance;
  }

  .lede { margin: 0; max-width: 64ch; color: var(--muted); font-size: 15px; }
  .lede code, .foot code {
    font-family: var(--mono);
    font-size: .88em;
    background: var(--amber-bg);
    color: var(--amber);
    padding: 1px 5px;
    border-radius: 4px;
  }

  /* ---------- stats ---------- */

  .stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(132px, 1fr));
    gap: 1px;
    background: var(--line);
    border: 1px solid var(--line);
    border-radius: 10px;
    overflow: hidden;
    margin-bottom: 24px;
  }
  .stat { background: var(--surface); padding: 14px 16px 16px; }
  .stat-k {
    display: block;
    font-family: var(--mono);
    font-size: 10px;
    letter-spacing: .13em;
    text-transform: uppercase;
    color: var(--faint);
    margin-bottom: 5px;
  }
  .stat-v {
    font-family: var(--mono);
    font-size: 22px;
    font-variant-numeric: tabular-nums;
    letter-spacing: -.02em;
    color: var(--ink);
  }
  .stat-v small { font-size: 12px; color: var(--muted); letter-spacing: 0; }

  /* ---------- toolbar ---------- */

  .toolbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
    padding: 12px 0;
    background: linear-gradient(var(--ground) 76%, transparent);
  }

  .field { position: relative; flex: 1 1 240px; min-width: 180px; }
  .field svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--faint); pointer-events: none; }

  input[type="search"], select {
    width: 100%;
    font: inherit;
    font-size: 14px;
    color: var(--ink);
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 9px 12px;
    appearance: none;
  }
  input[type="search"] { padding-left: 34px; font-family: var(--mono); font-size: 13px; }
  input[type="search"]::-webkit-search-cancel-button { filter: grayscale(1) opacity(.5); }

  select {
    flex: 0 1 auto;
    width: auto;
    max-width: 300px;
    padding-right: 30px;
    font-family: var(--mono);
    font-size: 12.5px;
    background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
    background-position: calc(100% - 16px) calc(50% + 1px), calc(100% - 11px) calc(50% + 1px);
    background-size: 5px 5px, 5px 5px;
    background-repeat: no-repeat;
  }

  .toggle {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    font-family: var(--mono);
    font-size: 12px;
    color: var(--muted);
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 9px 12px;
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
  }
  .toggle input { accent-color: var(--amber); margin: 0; }
  .toggle:hover { color: var(--ink); }
  .toggle:has(input:checked) { color: var(--amber); border-color: var(--amber-lo); background: var(--amber-bg); }

  .tbtn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 9px 11px;
    cursor: pointer;
    flex-shrink: 0;
  }
  .tbtn:hover { color: var(--amber); border-color: var(--amber-lo); }
  .tbtn .sun { display: none; }
  :root[data-theme="dark"] .tbtn .sun { display: block; }
  :root[data-theme="dark"] .tbtn .moon { display: none; }

  .noscript {
    margin: 0 0 24px;
    padding: 14px 16px;
    border: 1px solid var(--amber-lo);
    background: var(--amber-bg);
    border-radius: 10px;
    color: var(--ink);
    font-size: 14px;
  }

  :is(input, select, button, .toggle, .row[tabindex]):focus-visible {
    outline: 2px solid var(--focus);
    outline-offset: 1px;
  }

  /* ---------- table ---------- */

  .grid { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--surface); box-shadow: var(--shadow); }

  .row {
    display: grid;
    grid-template-columns: 3px 1fr 108px 58px 66px 128px;
    align-items: center;
    gap: 0 14px;
    border-bottom: 1px solid var(--line-soft);
  }
  .row:last-child { border-bottom: 0; }
  .row.clickable { cursor: pointer; }
  .row.clickable:hover { background: var(--raised); }

  .head {
    position: sticky;
    top: 62px;
    z-index: 10;
    background: var(--raised);
    border-bottom: 1px solid var(--line);
  }
  .head span, .head button {
    font-family: var(--mono);
    font-size: 10px;
    letter-spacing: .12em;
    text-transform: uppercase;
    color: var(--faint);
    padding: 9px 0;
    background: none;
    border: 0;
    text-align: left;
    cursor: default;
  }
  .head button { cursor: pointer; display: inline-flex; align-items: center; gap: 4px; }
  .head button:hover { color: var(--ink); }
  .head button[aria-sort] { color: var(--amber); }
  .head button .car { opacity: 0; }
  .head button[aria-sort] .car { opacity: 1; }
  .head .num { justify-content: flex-end; text-align: right; }
  .head .pad-l { padding-left: 14px; }
  .head .pad-r { padding-right: 16px; }

  .r-body { padding: 11px 0 12px; min-width: 0; }

  .stripe { align-self: stretch; background: var(--stripe, transparent); }

  .title {
    margin: 0 0 3px;
    font-size: 14px;
    line-height: 1.42;
    color: var(--ink);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  mark { background: var(--amber-bg); color: var(--amber); border-radius: 3px; padding: 0 1px; }

  .meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 8px;
    font-family: var(--mono);
    font-size: 11.5px;
    color: var(--muted);
    min-width: 0;
  }
  .path { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }

  .chip {
    font-size: 10px;
    letter-spacing: .06em;
    text-transform: uppercase;
    padding: 1px 5px;
    border-radius: 3px;
    border: 1px solid var(--line);
    color: var(--faint);
    white-space: nowrap;
  }
  .chip-branch { color: var(--muted); }
  .chip-empty, .chip-auto { color: var(--amber); border-color: var(--amber-lo); background: var(--amber-bg); }

  .snippet {
    margin: 5px 0 0;
    font-family: var(--mono);
    font-size: 11.5px;
    line-height: 1.5;
    color: var(--muted);
    border-left: 2px solid var(--amber-lo);
    padding-left: 9px;
  }

  .cell-n {
    font-family: var(--mono);
    font-size: 12px;
    font-variant-numeric: tabular-nums;
    color: var(--ink-soft);
    text-align: right;
    padding: 11px 0 12px;
  }
  .cell-date { font-family: var(--mono); font-size: 12px; font-variant-numeric: tabular-nums; color: var(--ink-soft); padding: 11px 0 12px; }
  .cell-date small { display: block; color: var(--faint); font-size: 10.5px; letter-spacing: .04em; }
  .dim { color: var(--faint); }

  .cell-act { padding: 9px 16px 9px 0; display: flex; justify-content: flex-end; }

  .copy {
    font-family: var(--mono);
    font-size: 11px;
    color: var(--muted);
    background: var(--raised);
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 6px 9px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
    transition: color .12s, border-color .12s, background .12s;
  }
  .copy:hover, .copy.done { color: var(--amber); border-color: var(--amber-lo); background: var(--amber-bg); }

  .empty-row .title { color: var(--muted); font-style: italic; }

  .none { padding: 48px 20px; text-align: center; color: var(--muted); font-family: var(--mono); font-size: 13px; }
  .count { font-family: var(--mono); font-size: 11.5px; color: var(--faint); padding: 10px 2px 0; letter-spacing: .04em; }

  /* ---------- reader ---------- */

  .scrim {
    position: fixed;
    inset: 0;
    background: var(--scrim);
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s;
    z-index: 40;
  }
  .scrim.open { opacity: 1; pointer-events: auto; }

  .reader {
    position: fixed;
    top: 0;
    right: 0;
    height: 100%;
    width: min(920px, 100%);
    display: flex;
    flex-direction: column;
    background: var(--ground);
    border-left: 1px solid var(--line);
    box-shadow: var(--shadow-lg);
    transform: translateX(100%);
    transition: transform .24s cubic-bezier(.4, 0, .2, 1);
    z-index: 41;
    visibility: hidden;
  }
  .reader.open { transform: none; visibility: visible; }

  .reader-head {
    display: flex;
    align-items: flex-start;
    gap: 14px;
    padding: 18px 20px 15px;
    border-bottom: 1px solid var(--line);
    background: var(--surface);
  }
  .reader-head h2 {
    margin: 0 0 6px;
    font-size: 17px;
    line-height: 1.3;
    letter-spacing: -.012em;
    font-weight: 600;
    text-wrap: balance;
  }
  .rh-text { flex: 1; min-width: 0; }
  .rh-acts { display: flex; gap: 7px; flex-shrink: 0; }

  .xbtn {
    background: var(--raised);
    border: 1px solid var(--line);
    border-radius: 6px;
    color: var(--muted);
    cursor: pointer;
    padding: 7px 9px;
    display: inline-flex;
    align-items: center;
    transition: color .12s, border-color .12s, background .12s;
  }
  .xbtn:hover { color: var(--ink); }
  .xbtn.done { color: var(--amber); border-color: var(--amber-lo); background: var(--amber-bg); }

  .transcript {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 20px 20px 64px;
    display: flex;
    flex-direction: column;
    gap: 14px;
  }

  .turn { display: flex; flex-direction: column; gap: 5px; }
  .who {
    font-family: var(--mono);
    font-size: 10px;
    letter-spacing: .13em;
    text-transform: uppercase;
    color: var(--faint);
  }
  .turn-u .who { color: var(--amber); }

  .bubble {
    border-radius: 8px;
    padding: 11px 14px;
    font-size: 14.5px;
    line-height: 1.62;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    border: 1px solid var(--line-soft);
    background: var(--surface);
    color: var(--ink-soft);
  }
  .turn-u .bubble { background: var(--amber-bg); border-color: var(--amber-lo); color: var(--ink); }

  .bubble pre {
    margin: 9px 0;
    padding: 10px 12px;
    background: var(--ground);
    border: 1px solid var(--line);
    border-radius: 6px;
    overflow-x: auto;
    font-family: var(--mono);
    font-size: 12.5px;
    line-height: 1.55;
    white-space: pre;
    color: var(--ink);
  }
  .bubble code {
    font-family: var(--mono);
    font-size: .87em;
    background: var(--raised);
    border: 1px solid var(--line-soft);
    border-radius: 4px;
    padding: 1px 4px;
  }
  .bubble strong { color: var(--ink); font-weight: 600; }
  .bubble .h {
    display: block;
    margin: 4px 0 -2px;
    font-size: .95em;
    font-weight: 620;
    letter-spacing: -.008em;
    color: var(--ink);
  }
  .bubble pre code { background: none; border: 0; padding: 0; font-size: inherit; }

  .tool {
    font-family: var(--mono);
    font-size: 11.5px;
    color: var(--muted);
    display: flex;
    align-items: baseline;
    gap: 8px;
    padding: 3px 0 3px 12px;
    border-left: 2px solid var(--line);
    overflow-wrap: anywhere;
  }
  .tool b { color: var(--amber); font-weight: 500; flex-shrink: 0; }

  .reader-empty { color: var(--muted); font-family: var(--mono); font-size: 13px; text-align: center; padding: 60px 20px; }

  /* ---------- toast ---------- */

  .toast {
    position: fixed;
    left: 50%;
    bottom: 26px;
    transform: translate(-50%, 14px);
    background: var(--ink);
    color: var(--ground);
    font-family: var(--mono);
    font-size: 12px;
    padding: 9px 14px;
    border-radius: 7px;
    box-shadow: var(--shadow);
    opacity: 0;
    pointer-events: none;
    transition: opacity .16s, transform .16s;
    z-index: 60;
    max-width: min(90vw, 560px);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .toast.show { opacity: 1; transform: translate(-50%, 0); }

  .legend {
    display: flex;
    flex-wrap: wrap;
    gap: 6px 16px;
    align-items: center;
    margin-top: 18px;
    font-family: var(--mono);
    font-size: 11px;
    color: var(--faint);
  }
  .legend i { display: inline-block; width: 16px; height: 3px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }

  .foot {
    margin-top: 26px;
    padding-top: 18px;
    border-top: 1px solid var(--line);
    color: var(--muted);
    font-size: 13px;
    max-width: 68ch;
  }
  .foot p { margin: 0 0 8px; }

  @media (max-width: 860px) {
    .wrap { padding: 28px 14px 72px; }
    .row { grid-template-columns: 3px 1fr auto; gap: 0 12px; }
    .head { display: none; }
    .r-body { padding: 12px 0 4px; grid-column: 2 / -1; }
    .cell-date { grid-column: 2; padding: 0 0 10px; }
    .cell-n { display: none; }
    .cell-act { grid-column: 3; grid-row: 2; padding: 0 12px 10px 0; }
    select { max-width: 100%; flex: 1 1 100%; }
    .reader { width: 100%; border-left: 0; }
  }

  /* ---------- tabs and memories ---------- */

  .tabs {
    display: flex;
    gap: 2px;
    margin-bottom: 18px;
    border-bottom: 1px solid var(--line);
  }
  .tab {
    font: inherit;
    font-size: 13.5px;
    color: var(--muted);
    background: none;
    border: 0;
    border-bottom: 2px solid transparent;
    padding: 8px 14px;
    margin-bottom: -1px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 7px;
  }
  .tab:hover { color: var(--ink); }
  .tab[aria-selected="true"] { color: var(--amber); border-bottom-color: var(--amber); }
  .tab .n {
    font-family: var(--mono);
    font-size: 11px;
    font-variant-numeric: tabular-nums;
    color: var(--faint);
    background: var(--raised);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 0 6px;
  }
  .tab[aria-selected="true"] .n { color: var(--amber); border-color: var(--amber-lo); background: var(--amber-bg); }

  body[data-mode="s"] .only-m,
  body[data-mode="m"] .only-s { display: none; }

  .row.mem { grid-template-columns: 3px 1fr 104px 210px 108px; }

  .mem-name { display: flex; align-items: baseline; gap: 7px; flex-wrap: wrap; }
  .mem-name .nm { font-size: 14px; color: var(--ink); font-family: var(--mono); }
  .mem-desc {
    margin: 3px 0 0;
    font-size: 12.5px;
    line-height: 1.45;
    color: var(--muted);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  .ty {
    font-family: var(--mono);
    font-size: 10.5px;
    letter-spacing: .05em;
    padding: 1px 6px;
    border-radius: 3px;
    border: 1px solid var(--line);
    color: var(--muted);
    white-space: nowrap;
  }
  .ty-project   { color: var(--amber); border-color: var(--amber-lo); background: var(--amber-bg); }
  .ty-user      { color: var(--focus); border-color: var(--focus); background: transparent; }
  .ty-feedback  { color: var(--copper, var(--amber)); border-color: var(--amber-lo); }
  .ty-reference { color: var(--muted); }

  /* A memory missing from MEMORY.md is still on disk but is not loaded into context. */
  .chip-unlisted { color: var(--amber); border-color: var(--amber-lo); background: var(--amber-bg); }

  .mem-body {
    font-size: 14.5px;
    line-height: 1.68;
    color: var(--ink-soft);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
  }
  .mem-body pre {
    margin: 10px 0;
    padding: 10px 12px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 6px;
    overflow-x: auto;
    font-family: var(--mono);
    font-size: 12.5px;
    white-space: pre;
    color: var(--ink);
  }
  .mem-body code {
    font-family: var(--mono);
    font-size: .87em;
    background: var(--surface);
    border: 1px solid var(--line-soft);
    border-radius: 4px;
    padding: 1px 4px;
  }
  .mem-body pre code { background: none; border: 0; padding: 0; font-size: inherit; }
  .mem-body strong { color: var(--ink); font-weight: 600; }
  .mem-body .h { display: block; margin: 10px 0 -2px; font-weight: 620; color: var(--ink); }

  .wl {
    font-family: var(--mono);
    font-size: .9em;
    color: var(--amber);
    background: var(--amber-bg);
    border: 1px solid var(--amber-lo);
    border-radius: 4px;
    padding: 0 4px;
    cursor: pointer;
  }
  .wl.dead { color: var(--faint); border-color: var(--line); background: none; cursor: default; }

  .mem-foot {
    margin-top: 22px;
    padding-top: 14px;
    border-top: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    gap: 8px;
    font-family: var(--mono);
    font-size: 11.5px;
    color: var(--muted);
  }
  .mem-foot .lbl { color: var(--faint); margin-right: 6px; }

  .jump {
    align-self: flex-start;
    font-family: var(--mono);
    font-size: 11.5px;
    color: var(--muted);
    background: var(--raised);
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 6px 10px;
    cursor: pointer;
  }
  .jump:hover { color: var(--amber); border-color: var(--amber-lo); background: var(--amber-bg); }

  @media (max-width: 860px) {
    .row.mem { grid-template-columns: 3px 1fr auto; }
    .row.mem .cell-ty { grid-column: 2; padding-bottom: 10px; }
    .row.mem .cell-proj { display: none; }
  }

  @media (prefers-reduced-motion: reduce) {
    * { transition: none !important; }
  }
</style>
</head>

<body>
<div class="wrap">
  <header class="masthead">
    <span class="eyebrow">~/.claude/projects</span>
    <h1>Your Claude Code sessions</h1>
    <p class="lede">Every conversation is kept in a local <code>.jsonl</code>. Click any row to read it, or copy the command to pick it up again — <code>--resume</code> only finds the session from its original directory, so the button copies the <code>cd</code> too.</p>
  </header>

  <noscript>
    <p class="noscript">This page builds the list with JavaScript. With JavaScript
    disabled nothing shows up — the data is still inside the file, in the
    <code>&lt;script id="payload"&gt;</code>.</p>
  </noscript>

  <div class="tabs" role="tablist" aria-label="What to browse">
    <button type="button" class="tab" role="tab" data-mode="s" aria-selected="true">
      Sessions <span class="n" id="nS"></span>
    </button>
    <button type="button" class="tab" role="tab" data-mode="m" aria-selected="false">
      Memories <span class="n" id="nM"></span>
    </button>
  </div>

  <div class="stats" id="stats"></div>

  <div class="toolbar">
    <div class="field">
      <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6"><circle cx="7" cy="7" r="4.5"/><path d="M10.5 10.5 14 14"/></svg>
      <input type="search" id="q" placeholder="search title, path or uuid…  (/)" autocomplete="off" spellcheck="false" aria-label="Search sessions">
    </div>
    <select id="proj" aria-label="Filter by project"></select>
    <label class="toggle only-s"><input type="checkbox" id="deep"> search inside chats</label>
    <label class="toggle only-s"><input type="checkbox" id="hideEmpty"> hide empty</label>
    <select id="mtype" class="only-m" aria-label="Filter by memory type"></select>
    <label class="toggle only-m"><input type="checkbox" id="unlisted"> only unindexed</label>
    <button type="button" class="tbtn" id="theme" title="Switch between light and dark" aria-label="Toggle theme">
      <svg class="moon" width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.4"><path d="M13.5 9.6A5.8 5.8 0 0 1 6.4 2.5a5.8 5.8 0 1 0 7.1 7.1Z"/></svg>
      <svg class="sun" width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.4"><circle cx="8" cy="8" r="3.1"/><path d="M8 1v1.6M8 13.4V15M1 8h1.6M13.4 8H15M3.1 3.1l1.1 1.1M11.8 11.8l1.1 1.1M12.9 3.1l-1.1 1.1M4.2 11.8l-1.1 1.1"/></svg>
    </button>
  </div>

  <div class="grid" role="table" aria-label="Claude Code sessions">
    <div class="row head only-s" role="row">
      <span></span>
      <button type="button" data-sort="t" class="pad-l">Session <span class="car">▾</span></button>
      <button type="button" data-sort="l">Date <span class="car">▾</span></button>
      <button type="button" data-sort="u" class="num">Msgs <span class="car">▾</span></button>
      <button type="button" data-sort="d" class="num">Dur. <span class="car">▾</span></button>
      <span class="pad-r" style="text-align:right">Resume</span>
    </div>
    <div class="row head mem only-m" role="row">
      <span></span>
      <button type="button" data-msort="name" class="pad-l">Memory <span class="car">▾</span></button>
      <button type="button" data-msort="ty">Type <span class="car">▾</span></button>
      <button type="button" data-msort="p">Project <span class="car">▾</span></button>
      <button type="button" data-msort="l">Date <span class="car">▾</span></button>
    </div>
    <div id="rows"></div>
  </div>

  <p class="count" id="count"></p>

  <div class="legend only-s">
    <span>Left stripe = age:</span>
    <span><i style="background:var(--amber)"></i>today / yesterday</span>
    <span><i style="background:var(--amber);opacity:.55"></i>last week</span>
    <span><i style="background:var(--amber);opacity:.26"></i>two weeks</span>
    <span><i style="background:var(--line)"></i>older</span>
  </div>

  <div class="legend only-m">
    <span><span class="chip chip-unlisted">unindexed</span> = on disk but not listed in MEMORY.md, so it is not loaded into context</span>
  </div>

  <div class="foot only-m">
    <p>Each project keeps its memories in <code>&lt;project&gt;/memory/</code>: one <code>.md</code> per memory and a <code>MEMORY.md</code> that indexes them. The index is the only thing read when a session starts.</p>
    <p>Claude picks the type when it writes them: <b>project</b> is ongoing work, <b>user</b> who you are and how you work, <b>feedback</b> your corrections, <b>reference</b> pointers to external resources.</p>
  </div>

  <div class="foot only-s">
    <p>Titles are generated by Claude during the session. When there is none, it falls back to your first message — which shows, because it starts in lowercase or reads like a stray question.</p>
    <p><b>Empty</b> is a session that was opened but never got a message: a cancelled <code>/resume</code>, a <code>/login</code>. <b>Non-interactive</b> is a <code>claude -p</code> with something piped through stdin — typically a <code>git diff</code> to draft a commit message, not a chat.</p>
    <p>The transcript has the conversation text and one line per tool used, but not what those tools returned: that is 95% of the size on disk.</p>
    <p id="snap"></p>
  </div>
</div>

<div class="scrim" id="scrim"></div>

<aside class="reader" id="reader" role="dialog" aria-modal="true" aria-labelledby="rTitle" tabindex="-1">
  <div class="reader-head">
    <div class="rh-text">
      <h2 id="rTitle"></h2>
      <div class="meta" id="rMeta"></div>
    </div>
    <div class="rh-acts">
      <button type="button" class="xbtn" id="rCopy" title="Copy resume command" aria-label="Copy resume command">
        <svg width="13" height="13" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><rect x="5.5" y="5.5" width="8" height="9" rx="1.5"/><path d="M10.5 3.5H3a.5.5 0 0 0-.5.5v8"/></svg>
      </button>
      <button type="button" class="xbtn" id="rClose" title="Close (Esc)" aria-label="Close">
        <svg width="13" height="13" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6"><path d="m4 4 8 8M12 4l-8 8"/></svg>
      </button>
    </div>
  </div>
  <div class="transcript" id="rBody"></div>
</aside>

<div class="toast" id="toast" role="status" aria-live="polite"></div>

<script id="payload" type="application/json">__DATA__</script>
<script>
const PAYLOAD = JSON.parse(document.getElementById('payload').textContent);
const DATA = PAYLOAD.s || [];
const MEM = PAYLOAD.m || [];
// Snapshot = the most recent activity in the data, so that "today" stays true
// relative to when the logs were read and not to the viewer's clock.
const NOW = new Date(Math.max(
  ...DATA.map(d => +new Date(d.l)), ...MEM.map(m => +new Date(m.l))));
const BY_ID = new Map(DATA.map(d => [d.id, d]));
const BY_NAME = new Map(MEM.map(m => [m.name, m]));
// [[...]] links may point to the declared name or to the file name.
MEM.forEach(m => { const f = m.file.replace(/\.md$/, ''); if (!BY_NAME.has(f)) BY_NAME.set(f, m); });

const $ = s => document.querySelector(s);
const rowsEl = $('#rows'), qEl = $('#q'), projEl = $('#proj'), deepEl = $('#deep'),
      emptyEl = $('#hideEmpty'), countEl = $('#count'), toastEl = $('#toast'),
      readerEl = $('#reader'), scrimEl = $('#scrim'), bodyEl = $('#rBody'),
      typeEl = $('#mtype'), unlistedEl = $('#unlisted');

let sortKey = 'l', sortDir = -1, lastFocus = null;
let mSortKey = 'l', mSortDir = -1;
// What is open in the reader: {kind: 's'|'m', key} or null.
let open_ = null;
// Without memories the tab adds nothing, so we always start on sessions.
let mode = 's';

/* ---------- formatting ---------- */

const DAY = 864e5;
const MONTHS = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
const pad = n => String(n).padStart(2, '0');

const daysAgo = iso => (NOW - new Date(iso)) / DAY;
const fmtDate = iso => { const d = new Date(iso); return `${pad(d.getDate())} ${MONTHS[d.getMonth()]}`; };
const fmtTime = iso => { const d = new Date(iso); return `${pad(d.getHours())}:${pad(d.getMinutes())}`; };

function fmtRel(iso) {
  const n = daysAgo(iso);
  if (n < 1) return 'today';
  if (n < 2) return 'yesterday';
  if (n < 7) return `${Math.floor(n)} d ago`;
  if (n < 30) return `${Math.floor(n / 7)} wk ago`;
  return `${Math.floor(n / 30)} mo ago`;
}
function fmtDur(m) {
  if (m == null) return '—';
  if (m < 1) return '<1m';
  if (m < 60) return m + 'm';
  const h = Math.floor(m / 60), r = m % 60;
  return r ? `${h}h${pad(r)}` : `${h}h`;
}
function stripeFor(iso) {
  const n = daysAgo(iso);
  if (n < 2)  return 'var(--amber)';
  if (n < 7)  return 'color-mix(in srgb, var(--amber) 55%, transparent)';
  if (n < 14) return 'color-mix(in srgb, var(--amber) 26%, transparent)';
  return 'var(--line)';
}

const esc = s => String(s).replace(/[&<>"']/g, c => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));

// Highlights the first match. The needle is escaped like the haystack: otherwise
// searching for "&" or "<" would never find anything, because in `s` they are entities.
function mark(text, q) {
  const s = esc(text);
  if (!q) return s;
  const needle = esc(q);
  const i = s.toLowerCase().indexOf(needle.toLowerCase());
  if (i < 0) return s;
  return s.slice(0, i) + '<mark>' + s.slice(i, i + needle.length) + '</mark>'
       + s.slice(i + needle.length);
}

// Minimal markdown: code, bold and headings. Line breaks are handled by pre-wrap.
// Code is set aside before touching bold, so that a ** inside a code block
// stays as is.
function renderBody(text, wikilinks) {
  const stash = [];
  // After esc() no raw "<" is left in the text: they are all &lt;. So a
  // placeholder with "<" cannot collide with the message content.
  const keep = html => `<K${stash.push(html) - 1}>`;

  let out = esc(text);
  out = out.replace(/```[a-zA-Z0-9_+-]*\n?([\s\S]*?)```/g,
    (_, code) => keep(`<pre>${code.replace(/\n+$/, '')}</pre>`));
  out = out.replace(/`([^`\n]+)`/g, (_, code) => keep(`<code>${code}</code>`));
  out = out.replace(/^#{1,6}\s+(.+)$/gm, '<b class="h">$1</b>');
  out = out.replace(/\*\*([^*\n]+)\*\*/g, '<strong>$1</strong>');
  if (wikilinks) {
    // This goes in here, with code already set aside: a [[...]] inside a
    // code block must stay as text.
    out = out.replace(/\[\[([^\]\n]+)\]\]/g, (_, name) => {
      const hit = BY_NAME.get(name);
      return hit
        ? `<button type="button" class="wl" data-wl="${esc(hit.name)}">${esc(name)}</button>`
        : `<span class="wl dead" title="no memory with that name exists yet">${esc(name)}</span>`;
    });
  }

  return out.replace(/<K(\d+)>/g, (_, i) => stash[i]);
}

/* ---------- header ---------- */

(function stats() {
  const msgs = DATA.reduce((a, d) => a + d.u, 0);
  const mb = DATA.reduce((a, d) => a + d.k, 0) / 1024;
  const ds = DATA.map(d => new Date(d.l)).sort((a, b) => a - b);
  const f = ds[0], l = ds[ds.length - 1];
  $('#stats').innerHTML = [
    ['sessions', DATA.length, ''],
    ['projects', new Set(DATA.map(d => d.p)).size, ''],
    ['your messages', msgs, ''],
    ['on disk', mb.toFixed(1), ' MB'],
    ['period', `${pad(f.getDate())} ${MONTHS[f.getMonth()]} – ${pad(l.getDate())} ${MONTHS[l.getMonth()]}`, ''],
    ['memories', MEM.length, ''],
  ].map(([k, v, s]) => `<div class="stat"><span class="stat-k">${k}</span><span class="stat-v">${v}${s ? `<small>${s}</small>` : ''}</span></div>`).join('');
})();

function fillProjects() {
  const src = mode === 'm' ? MEM : DATA;
  const counts = {};
  src.forEach(d => counts[d.p] = (counts[d.p] || 0) + 1);
  const label = mode === 'm' ? 'memories' : 'sessions';
  projEl.innerHTML = `<option value="">all projects (${src.length} ${label})</option>` +
    Object.entries(counts).sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
      .map(([p, n]) => `<option value="${esc(p)}">${esc(p)} · ${n}</option>`).join('');
}

(function types() {
  const counts = {};
  MEM.forEach(m => counts[m.ty] = (counts[m.ty] || 0) + 1);
  typeEl.innerHTML = `<option value="">all types (${MEM.length})</option>` +
    Object.entries(counts).sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
      .map(([t, n]) => `<option value="${esc(t)}">${esc(t)} · ${n}</option>`).join('');
})();

$('#nS').textContent = DATA.length;
$('#nM').textContent = MEM.length;

/* ---------- list ---------- */

function convoText(d) {
  if (d._flat === undefined) d._flat = d.c.map(m => m.x).join('\n').toLowerCase();
  return d._flat;
}

function snippetFor(d, q) {
  for (const m of d.c) {
    const i = m.x.toLowerCase().indexOf(q);
    if (i < 0) continue;
    const from = Math.max(0, i - 55);
    const raw = m.x.slice(from, i + q.length + 75).replace(/\s+/g, ' ');
    return (from ? '…' : '') + raw + '…';
  }
  return null;
}

function current() {
  const q = qEl.value.trim().toLowerCase();
  const proj = projEl.value;
  const deep = deepEl.checked;

  const list = DATA.filter(d => {
    if (proj && d.p !== proj) return false;
    if (emptyEl.checked && d.e) return false;
    if (!q) return true;
    if ((d.t || '').toLowerCase().includes(q) || d.p.toLowerCase().includes(q)
        || d.id.toLowerCase().includes(q) || (d.b || '').toLowerCase().includes(q)) return true;
    return deep && convoText(d).includes(q);
  });

  const k = sortKey;
  list.sort((a, b) => {
    if (k === 't') {
      const av = (a.t || '~').toLowerCase(), bv = (b.t || '~').toLowerCase();
      return av < bv ? -sortDir : av > bv ? sortDir : 0;
    }
    const av = k === 'l' ? new Date(a.l) : (a[k] ?? -1);
    const bv = k === 'l' ? new Date(b.l) : (b[k] ?? -1);
    return (av - bv) * sortDir;
  });
  return list;
}

function renderSessions() {
  const list = current();
  const q = qEl.value.trim();
  const ql = q.toLowerCase();

  if (!list.length) {
    rowsEl.innerHTML = `<div class="none">No session matches that filter.</div>`;
    countEl.textContent = '';
    return;
  }

  rowsEl.innerHTML = list.map(d => {
    const inMeta = q && ((d.t || '').toLowerCase().includes(ql) || d.p.toLowerCase().includes(ql));
    const snip = q && deepEl.checked && !inMeta ? snippetFor(d, ql) : null;
    const chips = [
      d.e ? '<span class="chip chip-empty">empty</span>' : '',
      d.n ? '<span class="chip chip-auto">non-interactive</span>' : '',
      d.b && d.b !== 'HEAD' ? `<span class="chip chip-branch">${esc(d.b)}</span>` : '',
      d.i ? '<span class="chip" title="Path deduced from other sessions of the same project">inferred path</span>' : '',
    ].join('');

    return `
    <div class="row${d.e ? ' empty-row' : ' clickable'}" role="row" data-id="${d.id}"${d.e ? '' : ' tabindex="0"'}>
      <div class="stripe" style="--stripe:${stripeFor(d.l)}"></div>
      <div class="r-body">
        <p class="title">${d.t ? mark(d.t, q) : 'session opened with no messages'}</p>
        <div class="meta"><span class="path" title="${esc(d.p)}">${mark(d.p, q)}</span>${chips}</div>
        ${snip ? `<p class="snippet">${mark(snip, q)}</p>` : ''}
      </div>
      <div class="cell-date">${fmtDate(d.l)}<small>${fmtRel(d.l)} · ${fmtTime(d.l)}</small></div>
      <div class="cell-n${d.u ? '' : ' dim'}">${d.u || '—'}</div>
      <div class="cell-n${d.d ? '' : ' dim'}">${fmtDur(d.d)}</div>
      <div class="cell-act">
        <button type="button" class="copy" data-copy="${d.id}" title="cd ${esc(d.p)} &amp;&amp; claude --resume ${d.id}">
          <svg width="11" height="11" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><rect x="5.5" y="5.5" width="8" height="9" rx="1.5"/><path d="M10.5 3.5H3a.5.5 0 0 0-.5.5v8"/></svg>
          <span>${d.id.slice(0, 8)}</span>
        </button>
      </div>
    </div>`;
  }).join('');

  countEl.textContent = list.length === DATA.length
    ? `${DATA.length} sessions · click a row to read it`
    : `${list.length} of ${DATA.length} sessions`;
}

/* ---------- memories ---------- */

function currentMem() {
  const q = qEl.value.trim().toLowerCase();
  const proj = projEl.value, ty = typeEl.value;

  const list = MEM.filter(m => {
    if (proj && m.p !== proj) return false;
    if (ty && m.ty !== ty) return false;
    if (unlistedEl.checked && m.ix) return false;
    if (!q) return true;
    return m.name.toLowerCase().includes(q)
        || m.desc.toLowerCase().includes(q)
        || m.p.toLowerCase().includes(q)
        || m.body.toLowerCase().includes(q);
  });

  list.sort((a, b) => {
    const k = mSortKey;
    if (k === 'l') return (new Date(a.l) - new Date(b.l)) * mSortDir;
    const av = String(a[k]).toLowerCase(), bv = String(b[k]).toLowerCase();
    return av < bv ? -mSortDir : av > bv ? mSortDir : 0;
  });
  return list;
}

function renderMem() {
  const list = currentMem();
  const q = qEl.value.trim();

  if (!list.length) {
    rowsEl.innerHTML = `<div class="none">No memory matches that filter.</div>`;
    countEl.textContent = '';
    return;
  }

  rowsEl.innerHTML = list.map(m => `
    <div class="row mem clickable" role="row" data-mem="${esc(m.name)}" tabindex="0">
      <div class="stripe" style="--stripe:${stripeFor(m.l)}"></div>
      <div class="r-body">
        <div class="mem-name">
          <span class="nm">${mark(m.name, q)}</span>
          ${m.ix ? '' : '<span class="chip chip-unlisted">unindexed</span>'}
        </div>
        ${m.desc ? `<p class="mem-desc">${mark(m.desc, q)}</p>` : ''}
      </div>
      <div class="cell-ty"><span class="ty ty-${esc(m.ty)}">${esc(m.ty)}</span></div>
      <div class="cell-proj meta"><span class="path" title="${esc(m.p)}">${mark(m.p, q)}</span></div>
      <div class="cell-date">${fmtDate(m.l)}<small>${fmtRel(m.l)}</small></div>
    </div>`).join('');

  countEl.textContent = list.length === MEM.length
    ? `${MEM.length} memories · click one to read it`
    : `${list.length} of ${MEM.length} memories`;
}

function render() {
  return mode === 'm' ? renderMem() : renderSessions();
}

function setMode(next) {
  if (next === mode) return;
  mode = next;
  document.body.dataset.mode = mode;
  document.querySelectorAll('.tab').forEach(t =>
    t.setAttribute('aria-selected', String(t.dataset.mode === mode)));
  qEl.placeholder = mode === 'm'
    ? 'search name, description or content…  (/)'
    : 'search title, path or uuid…  (/)';
  // Filters belong to another set: carrying them across tabs is confusing.
  projEl.value = '';
  fillProjects();
  render();
}

/* ---------- reader ---------- */

const WHO = { u: 'you', a: 'claude' };

function showReader() {
  bodyEl.scrollTop = 0;
  scrimEl.classList.add('open');
  readerEl.classList.add('open');
  document.body.classList.add('locked');
  readerEl.focus();
}

function openChat(id) {
  const d = BY_ID.get(id);
  if (!d || d.e) return;
  open_ = { kind: 's', key: id };
  lastFocus = document.activeElement;
  $('#rCopy').hidden = false;

  $('#rTitle').textContent = d.t || 'Untitled session';
  $('#rMeta').innerHTML = [
    `<span class="path">${esc(d.p)}</span>`,
    `<span>${fmtDate(d.l)} · ${fmtTime(d.l)}</span>`,
    `<span>${d.u} yours / ${d.a} from Claude</span>`,
    `<span>${fmtDur(d.d)}</span>`,
    `<span class="chip">${d.id.slice(0, 8)}</span>`,
  ].join('');

  bodyEl.innerHTML = d.c.length
    ? d.c.map(m => m.r === 't'
        ? `<div class="tool"><b>⚒</b><span>${esc(m.x)}</span></div>`
        : `<div class="turn turn-${m.r}"><span class="who">${WHO[m.r]}</span><div class="bubble">${renderBody(m.x)}</div></div>`
      ).join('')
    : `<p class="reader-empty">This session has no messages.</p>`;

  if (location.hash.slice(1) !== id) history.replaceState(null, '', '#' + id);
  showReader();
}

function openMemory(name) {
  const m = BY_NAME.get(name);
  if (!m) return;
  open_ = { kind: 'm', key: m.name };
  lastFocus = document.activeElement;
  // The copy button resumes a session: it does not apply to a memory.
  $('#rCopy').hidden = true;

  $('#rTitle').textContent = m.name;
  $('#rMeta').innerHTML = [
    `<span class="ty ty-${esc(m.ty)}">${esc(m.ty)}</span>`,
    `<span class="path">${esc(m.p)}</span>`,
    `<span>${fmtDate(m.l)} · ${fmtTime(m.l)}</span>`,
    `<span>${m.k} KB</span>`,
    m.ix ? '' : '<span class="chip chip-unlisted">unindexed</span>',
  ].filter(Boolean).join('');

  const source = BY_ID.get(m.src);
  const foot = [];
  if (m.desc) foot.push(`<div><span class="lbl">description</span>${esc(m.desc)}</div>`);
  foot.push(`<div><span class="lbl">file</span>${esc(m.file)}</div>`);
  if (!m.ix) {
    foot.push(`<div>${m.hix
      ? 'Not listed in MEMORY.md: still on disk, but not loaded into context.'
      : 'This project has no MEMORY.md, so none of its memories are loaded.'}</div>`);
  }
  if (m.src) {
    foot.push(source
      ? `<button type="button" class="jump" data-chat="${esc(m.src)}">↩ written by this session: ${esc(source.t || m.src.slice(0, 8))}</button>`
      : `<div><span class="lbl">written by</span>session ${esc(m.src.slice(0, 8))}, no longer on disk</div>`);
  }

  bodyEl.innerHTML = `<div class="mem-body">${renderBody(m.body, true)}</div>`
    + `<div class="mem-foot">${foot.join('')}</div>`;

  const hash = 'm:' + m.name;
  if (decodeURIComponent(location.hash.slice(1)) !== hash) {
    history.replaceState(null, '', '#' + encodeURIComponent(hash));
  }
  showReader();
}

function closeReader() {
  if (!open_) return;
  open_ = null;
  if (location.hash) history.replaceState(null, '', location.pathname + location.search);
  scrimEl.classList.remove('open');
  readerEl.classList.remove('open');
  document.body.classList.remove('locked');
  if (lastFocus && document.contains(lastFocus)) lastFocus.focus();
}

function openFromHash() {
  const ref = decodeURIComponent(location.hash.slice(1));
  if (!ref) return;
  if (ref.startsWith('m:')) {
    const name = ref.slice(2);
    if (BY_NAME.has(name)) { setMode('m'); openMemory(name); }
  } else if (BY_ID.has(ref)) {
    openChat(ref);
  }
}

// The dialog is aria-modal: focus must stay inside while it is open.
readerEl.addEventListener('keydown', e => {
  if (e.key !== 'Tab') return;
  const f = [...readerEl.querySelectorAll('button, [tabindex]:not([tabindex="-1"])')]
    .filter(el => el.offsetParent !== null);
  if (!f.length) return;
  const first = f[0], last = f[f.length - 1];
  if (e.shiftKey && (document.activeElement === first || document.activeElement === readerEl)) {
    e.preventDefault(); last.focus();
  } else if (!e.shiftKey && document.activeElement === last) {
    e.preventDefault(); first.focus();
  }
});

/* ---------- copy ---------- */

const cmdFor = d => `cd ${d.p} && claude --resume ${d.id}`;

function copyText(text) {
  if (navigator.clipboard && window.isSecureContext) return navigator.clipboard.writeText(text);
  return new Promise((res, rej) => {
    const ta = document.createElement('textarea');
    ta.value = text;
    ta.style.cssText = 'position:fixed;opacity:0;top:0;left:0';
    document.body.appendChild(ta);
    ta.select();
    try { document.execCommand('copy') ? res() : rej(); }
    catch (e) { rej(e); }
    finally { ta.remove(); }
  });
}

let toastTimer;
function toast(msg) {
  toastEl.textContent = msg;
  toastEl.classList.add('show');
  clearTimeout(toastTimer);
  toastTimer = setTimeout(() => toastEl.classList.remove('show'), 2600);
}

function copyFor(d, btn) {
  if (!d) return;
  copyText(cmdFor(d)).then(() => {
    if (btn) { btn.classList.add('done'); setTimeout(() => btn.classList.remove('done'), 1400); }
    toast('Copied: ' + cmdFor(d));
  }).catch(() => toast('Could not copy — the command is in the tooltip'));
}

/* ---------- events ---------- */

rowsEl.addEventListener('click', e => {
  const copyBtn = e.target.closest('[data-copy]');
  if (copyBtn) {
    e.stopPropagation();
    copyFor(BY_ID.get(copyBtn.dataset.copy), copyBtn);
    return;
  }
  const row = e.target.closest('.row.clickable');
  if (!row) return;
  if (row.dataset.mem) openMemory(row.dataset.mem);
  else openChat(row.dataset.id);
});

rowsEl.addEventListener('keydown', e => {
  if (e.key !== 'Enter' && e.key !== ' ') return;
  const row = e.target.closest('.row.clickable');
  if (!row || e.target.closest('[data-copy]')) return;
  e.preventDefault();
  if (row.dataset.mem) openMemory(row.dataset.mem);
  else openChat(row.dataset.id);
});

// Inside the reader: [[...]] links between memories and jump to the source session.
bodyEl.addEventListener('click', e => {
  const wl = e.target.closest('[data-wl]');
  if (wl) { openMemory(wl.dataset.wl); return; }
  const jump = e.target.closest('[data-chat]');
  if (jump) { setMode('s'); openChat(jump.dataset.chat); }
});

$('#rClose').addEventListener('click', closeReader);
scrimEl.addEventListener('click', closeReader);
$('#rCopy').addEventListener('click', e => {
  if (open_ && open_.kind === 's') copyFor(BY_ID.get(open_.key), e.currentTarget);
});

document.addEventListener('keydown', e => {
  if (e.key === 'Escape') {
    if (open_) closeReader();
    else if (document.activeElement === qEl && qEl.value) { qEl.value = ''; render(); }
    return;
  }
  if (open_) return;
  const typing = /^(INPUT|SELECT|TEXTAREA)$/.test(document.activeElement.tagName);
  if ((e.key === '/' && !typing) || (e.key.toLowerCase() === 'k' && (e.metaKey || e.ctrlKey))) {
    e.preventDefault();
    qEl.focus();
    qEl.select();
  }
});

$('#theme').addEventListener('click', () => {
  const next = document.documentElement.dataset.theme === 'dark' ? 'light' : 'dark';
  document.documentElement.dataset.theme = next;
  try { localStorage.setItem('cl-theme', next); } catch (e) {}
});

document.querySelectorAll('[data-sort]').forEach(btn => {
  btn.addEventListener('click', () => {
    const k = btn.dataset.sort;
    if (sortKey === k) sortDir = -sortDir;
    else { sortKey = k; sortDir = k === 't' ? 1 : -1; }
    document.querySelectorAll('[data-sort]').forEach(b => b.removeAttribute('aria-sort'));
    btn.setAttribute('aria-sort', sortDir === 1 ? 'ascending' : 'descending');
    btn.querySelector('.car').textContent = sortDir === 1 ? '▴' : '▾';
    render();
  });
});
document.querySelector('[data-sort="l"]').setAttribute('aria-sort', 'descending');

document.querySelectorAll('[data-msort]').forEach(btn => {
  btn.addEventListener('click', () => {
    const k = btn.dataset.msort;
    if (mSortKey === k) mSortDir = -mSortDir;
    else { mSortKey = k; mSortDir = k === 'l' ? -1 : 1; }
    document.querySelectorAll('[data-msort]').forEach(b => b.removeAttribute('aria-sort'));
    btn.setAttribute('aria-sort', mSortDir === 1 ? 'ascending' : 'descending');
    btn.querySelector('.car').textContent = mSortDir === 1 ? '▴' : '▾';
    render();
  });
});
document.querySelector('[data-msort="l"]').setAttribute('aria-sort', 'descending');

document.querySelectorAll('.tab').forEach(t =>
  t.addEventListener('click', () => setMode(t.dataset.mode)));

// Without memories the tab has nothing to show.
if (!MEM.length) document.querySelector('.tab[data-mode="m"]').hidden = true;

$('#snap').textContent = `Snapshot read on ${pad(NOW.getDate())} ${MONTHS[NOW.getMonth()]} ${NOW.getFullYear()} at ${fmtTime(NOW.toISOString())}. Relative dates are measured from that moment; run claude-logbook --html again to refresh it.`;

[qEl, projEl, deepEl, emptyEl, typeEl, unlistedEl].forEach(
  el => el.addEventListener(el === qEl ? 'input' : 'change', render));

document.body.dataset.mode = mode;
fillProjects();
render();
openFromHash();
</script>
</body>
</html>