ddevmem 0.4.0-alpha.5

Safe, ergonomic access to physical memory via /dev/mem with volatile MMIO semantics, declarative register maps, and an optional web UI.
Documentation
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ddevmem — Register Map</title>
<script>
  // Apply saved theme as early as possible to avoid a flash.
  (function() {
    var t = localStorage.getItem('ddevmem-theme');
    if (!t) t = matchMedia('(prefers-color-scheme: light)').matches ? 'g10' : 'g100';
    document.documentElement.setAttribute('data-theme', t);
  })();
</script>
<style>
  /*
   * Carbon-inspired token system. Two themes:
   *   data-theme="g10"   → light (Gray 10 theme)
   *   data-theme="g100"  → dark  (Gray 100 theme)
   * The :root block holds the dark defaults so themes can be flipped via
   * the [data-theme] attribute on <html>.
   */
  :root, [data-theme="g100"] {
    --background: #161616;
    --layer-01: #262626;
    --layer-02: #393939;
    --layer-hover: #353535;
    --layer-active: #525252;
    --field-01: #262626;
    --field-02: #393939;
    --field-hover: #353535;
    --border-subtle: #393939;
    --border-strong: #6f6f6f;
    --text-primary: #f4f4f4;
    --text-secondary: #c6c6c6;
    --text-helper: #a8a8a8;
    --text-placeholder: #6f6f6f;
    --text-on-color: #ffffff;
    --link-primary: #78a9ff;
    --link-hover: #a6c8ff;
    --focus: #ffffff;
    /* Component tokens — same hue across themes for semantic consistency */
    --button-primary: #0f62fe;
    --button-primary-hover: #0353e9;
    --button-primary-active: #002d9c;
    --button-secondary: #6f6f6f;        /* lighter in dark theme for contrast on dark bg */
    --button-secondary-hover: #5e5e5e;
    --button-secondary-active: #393939;
    --button-tertiary: #ffffff;          /* on dark bg, tertiary uses white border/text */
    --button-tertiary-hover: #f4f4f4;
    --button-tertiary-active: #c6c6c6;
    --button-separator: #161616;         /* 1px line between buttons in a btn-set */
    /* Tag colours: dark theme uses step-20 text on step-70 background.
       rw → Blue, ro → Cool Gray, wo → Magenta. */
    --tag-bg-rw: #0043ce; --tag-fg-rw: #d0e2ff;
    --tag-bg-ro: #4d5358; --tag-fg-ro: #dde1e6;
    --tag-bg-wo: #9f1853; --tag-fg-wo: #ffd6e8;
    --support-success: #42be65;
  }

  [data-theme="g10"] {
    --background: #ffffff;
    --layer-01: #f4f4f4;
    --layer-02: #ffffff;
    --layer-hover: #e8e8e8;
    --layer-active: #c6c6c6;
    --field-01: #f4f4f4;
    --field-02: #ffffff;
    --field-hover: #e8e8e8;
    --border-subtle: #e0e0e0;
    --border-strong: #8d8d8d;
    --text-primary: #161616;
    --text-secondary: #525252;
    --text-helper: #6f6f6f;
    --text-placeholder: #a8a8a8;
    --text-on-color: #ffffff;
    --link-primary: #0f62fe;
    --link-hover: #0043ce;
    --focus: #0f62fe;
    --button-primary: #0f62fe;
    --button-primary-hover: #0353e9;
    --button-primary-active: #002d9c;
    --button-secondary: #393939;        /* Gray 80 per Carbon spec */
    --button-secondary-hover: #4c4c4c;
    --button-secondary-active: #6f6f6f;
    --button-tertiary: #0f62fe;          /* on light bg, tertiary uses Blue 60 */
    --button-tertiary-hover: #0353e9;
    --button-tertiary-active: #002d9c;
    --button-separator: #e0e0e0;         /* 1px line between buttons in a btn-set */
    /* Tag colours: light theme uses step-70 text on step-20 background. */
    --tag-bg-rw: #d0e2ff; --tag-fg-rw: #0043ce;
    --tag-bg-ro: #dde1e6; --tag-fg-ro: #4d5358;
    --tag-bg-wo: #ffd6e8; --tag-fg-wo: #9f1853;
    --support-success: #24a148;
  }

  /* Carbon spacing scale (subset). All paddings/margins below reference
     these tokens so the layout follows the official 8 px grid. */
  :root {
    --spacing-01: .125rem; /*  2 px */
    --spacing-02: .25rem;  /*  4 px */
    --spacing-03: .5rem;   /*  8 px */
    --spacing-04: .75rem;  /* 12 px */
    --spacing-05: 1rem;    /* 16 px */
    --spacing-06: 1.5rem;  /* 24 px */
    --spacing-07: 2rem;    /* 32 px */
    --spacing-08: 2.5rem;  /* 40 px */
    --spacing-09: 3rem;    /* 48 px */
    --header-h:  3rem;     /* UI-shell header */
    --sidenav-w: 16rem;    /* 256 px — Carbon side-nav width */
  }

  * { box-sizing: border-box; margin: 0; padding: 0; }
  html, body { background: var(--background); color: var(--text-primary); }
  body {
    font-family: 'IBM Plex Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 14px;
    line-height: 1.4;
    /* No body padding: the UI-Shell header is fixed on top, side-nav is
       fixed on the left, and `.main-content` carries its own gutters. */
    padding: 0;
    padding-top: var(--header-h);
    -webkit-font-smoothing: antialiased;
    transition: background-color 70ms, color 70ms;
  }
  code, kbd, .mono, input, select, .reg-offset, .hex, .bf-val {
    font-family: 'IBM Plex Mono', ui-monospace, 'JetBrains Mono', 'Fira Code', monospace;
  }

  h1 { font-size: 1.75rem; font-weight: 300; letter-spacing: -.01em; margin-bottom: var(--spacing-02); }
  h2 {
    font-size: 1.25rem; font-weight: 400; margin: var(--spacing-07) 0 var(--spacing-03);
    padding: var(--spacing-03) 0; border-bottom: 1px solid var(--border-subtle);
    display: flex; align-items: center; justify-content: space-between; gap: var(--spacing-05);
  }
  h2 .map-dump { font-size: .75rem; }
  .meta { color: var(--text-helper); font-size: .75rem; margin-bottom: var(--spacing-05); letter-spacing: .02em; }

  /* ---- UI-Shell header (Carbon `.cds--header`) -------------------------
     Fixed 48 px black/white bar across the top of the viewport. Holds
     the product name on the left and global actions (theme toggle) on
     the right. */
  .app-header {
    position: fixed; inset: 0 0 auto 0;
    height: var(--header-h);
    display: flex; align-items: center;
    background: var(--background);
    border-bottom: 1px solid var(--border-subtle);
    z-index: 10;
  }
  .app-header .brand {
    padding: 0 var(--spacing-05);
    font-size: .875rem;
    font-weight: 600;
    letter-spacing: .01em;
    color: var(--text-primary);
    display: inline-flex; align-items: center; gap: var(--spacing-03);
  }
  .app-header .brand .brand-sub {
    color: var(--text-helper);
    font-weight: 400;
  }
  .app-header .header-actions {
    margin-left: auto;
    display: inline-flex;
    height: 100%;
  }
  .app-header .header-actions > button {
    height: var(--header-h);
    width: var(--header-h);
    border-radius: 0;
    border-color: transparent;
    color: var(--text-primary);
  }
  .app-header .header-actions > button:hover {
    background: var(--layer-01);
    color: var(--text-primary);
    border-color: transparent;
  }
  .app-header .header-actions > button:focus {
    border-color: var(--focus);
    box-shadow: inset 0 0 0 1px var(--focus),
                inset 0 0 0 2px var(--background);
  }

  /* Tile (register card) */
  .reg-card {
    background: var(--layer-01);
    border: 1px solid transparent;
    padding: var(--spacing-05) var(--spacing-05);
    margin-bottom: 2px;
    transition: background-color 70ms, border-color 70ms;
  }
  .reg-header { display: flex; align-items: center; gap: var(--spacing-04); flex-wrap: wrap; margin-bottom: var(--spacing-02); }
  .reg-name { font-weight: 600; font-size: 1rem; color: var(--text-primary); }
  .reg-offset { color: var(--text-helper); font-size: .75rem; margin-left: auto; }
  .reg-doc { color: var(--text-secondary); font-size: .8125rem; margin: var(--spacing-02) 0 var(--spacing-04); }

  /*
   * Tag (Carbon spec, size sm = 18px, pill shape). rw/ro/wo carry no
   * "alarm" semantics, so we use Blue / Cool Gray / Magenta from the Carbon
   * tag palette (step-70 text on step-20 bg in light themes; inverted in
   * dark). Sentence case per Carbon content guidelines.
   */
  .badge {
    display: inline-flex;
    align-items: center;
    height: 18px;
    padding: 0 8px;
    border: 0;
    border-radius: 16px;
    font-size: .75rem;            /* 12px — $label-01 */
    line-height: 1.3334;
    font-weight: 400;
    letter-spacing: .32px;
    text-transform: uppercase;    /* short access labels read better caps */
  }
  .badge-rw { background: var(--tag-bg-rw); color: var(--tag-fg-rw); }
  .badge-ro { background: var(--tag-bg-ro); color: var(--tag-fg-ro); }
  .badge-wo { background: var(--tag-bg-wo); color: var(--tag-fg-wo); }

  /* Text input — Carbon style: square, bottom border, focus inset outline */
  input[type="text"], input:not([type]), select, .bf-input {
    background: var(--field-01);
    color: var(--text-primary);
    border: 0;
    border-bottom: 1px solid var(--border-strong);
    padding: 6px 10px;
    font-size: .8125rem;
    height: 2rem;
    outline: none;
    transition: background-color 70ms, border-color 70ms, outline-color 70ms;
    min-width: 0;
  }
  input::placeholder { color: var(--text-placeholder); }
  input:hover, select:hover, .bf-input:hover { background: var(--field-hover); }
  input:focus, select:focus, .bf-input:focus {
    outline: 2px solid var(--focus);
    outline-offset: -2px;
    border-bottom-color: transparent;
  }
  .reg-value input { width: 16ch; }
  .bf-input { height: 1.75rem; padding: 4px 8px; width: 9ch; }

  /* Set-cell layout: input/select stretches, the Set button always lands at
     the right edge of the column so buttons line up across rows. */
  .bf-set { display: flex; gap: .5rem; align-items: center; }
  .bf-set .bf-input { flex: 1 1 auto; width: auto; min-width: 0; }
  .bf-set > button { flex: 0 0 auto; }

  select, select.bf-input {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    background-repeat: no-repeat;
    background-position: right .75rem center;
    cursor: pointer;
    text-overflow: ellipsis;
    padding-right: 2rem;          /* 12 px chevron-edge + 16 px chevron + 4 px gap */
  }
  select::-ms-expand { display: none; }
  select.bf-input {
    width: auto;
    min-width: 11ch;
    height: 1.75rem;
    padding: 4px 2rem 4px 8px;
    background-position: right .5rem center;
  }

  .reg-value { display: flex; align-items: center; gap: .5rem; margin: .5rem 0; flex-wrap: wrap; }
  /* Carbon `.cds--btn-set` — adjacent buttons sit flush against each other,
     separated only by a 1px box-shadow line ($button-separator). The group
     gets its own `.btn-set` wrapper to override the .reg-value flex gap. */
  .btn-set { display: inline-flex; }
  .btn-set > button:not(:first-of-type):not(:focus) {
    box-shadow: -1px 0 0 0 var(--button-separator);
  }
  .btn-set > button:focus + button:not(:focus) { box-shadow: none; }
  .reg-value .hex { color: var(--text-secondary); font-size: .8125rem; min-width: 12ch; }

  /*
   * Buttons — match @carbon/styles/scss/components/button/_button.scss
   *
   *  default        → Primary    (Write, Set)
   *  .btn-secondary → Secondary  (Read)
   *  .btn-tertiary  → Tertiary   (Refresh All, Dump All, theme toggle)
   *
   * Carbon spec (size sm = 32 px):
   *   - inline-flex, justify-content: space-between, text-align: start
   *     → label is left-aligned, icon (if any) sits on the right
   *   - asymmetric padding-inline: 11px on the start side, 51px on the end
   *     side (= padding-inline * 3 + 16px − 1px); reserves icon space and
   *     gives the signature wide-button look even for label-only buttons.
   *   - body-compact-01: 14 px / 18 px line-height, weight 400, letter-spacing .16px
   *   - border: 1px solid (matches bg for primary/secondary, transparent for ghost)
   *   - focus: double inset box-shadow (focus colour + background), no outline
   */
  button {
    display: inline-flex;
    align-items: center;
    justify-content: space-between;
    text-align: start;
    background: var(--button-primary);
    color: var(--text-on-color);
    border: 1px solid var(--button-primary);
    /* size sm: padding-inline 11px / 51px, height 32px */
    padding: 0 3.1875rem 0 .6875rem;
    min-height: 2rem;
    min-width: max-content;
    max-width: 20rem;
    font-family: inherit;
    font-size: .875rem;          /* 14px — body-compact-01 */
    line-height: 1.28572;         /* 18 / 14 */
    font-weight: 400;
    letter-spacing: .16px;
    cursor: pointer;
    vertical-align: top;
    transition: background-color 70ms cubic-bezier(.2,0,.38,.9),
                box-shadow 70ms cubic-bezier(.2,0,.38,.9),
                border-color 70ms cubic-bezier(.2,0,.38,.9),
                color 70ms cubic-bezier(.2,0,.38,.9);
  }
  button:hover  { background: var(--button-primary-hover); border-color: var(--button-primary-hover); }
  button:active { background: var(--button-primary-active); border-color: var(--button-primary-active); }
  /*
   * Focus ring — verbatim from @carbon/styles button-theme mixin
   * (packages/styles/scss/components/button/_mixins.scss):
   *
   *   border-color: $button-focus-color;
   *   box-shadow:
   *     inset 0 0 0 $button-outline-width $button-focus-color,
   *     inset 0 0 0 $button-border-width  $background;
   *
   * with $button-outline-width: 1px and $button-border-width: 2px
   * (packages/styles/scss/components/button/_vars.scss).
   *
   * The first listed shadow paints on top of the second, so the
   * visible result is:
   *   0..1 px from edge — focus colour (outer line)
   *   1..2 px           — background colour (inner line, visible
   *                        only where the first shadow doesn't reach)
   * That's the characteristic two-line indicator: an outer focus
   * stripe + an inner gap of the page background.
   *
   * The selector list raises specificity to (0,2,1) so the focus
   * indicator wins over `.btn-secondary:hover`, `.btn-ghost:active`
   * etc. — otherwise hover/active rules would reset border-color
   * and erase the outer line while the user is pressing the button.
   */
  button:focus,
  button.btn-secondary:focus,
  button.btn-tertiary:focus,
  button.btn-ghost:focus,
  button.btn-icon-only:focus {
    outline: none;
    border-color: var(--focus);
    box-shadow: inset 0 0 0 1px var(--focus),
                inset 0 0 0 2px var(--background);
  }

  button.btn-secondary {
    background: var(--button-secondary);
    color: var(--text-on-color);
    border-color: var(--button-secondary);
  }
  button.btn-secondary:hover  { background: var(--button-secondary-hover); border-color: var(--button-secondary-hover); }
  button.btn-secondary:active { background: var(--button-secondary-active); border-color: var(--button-secondary-active); }

  button.btn-tertiary {
    background: transparent;
    color: var(--button-tertiary);
    border-color: var(--button-tertiary);
  }
  button.btn-tertiary:hover {
    background: var(--button-tertiary-hover);
    color: var(--background);
    border-color: var(--button-tertiary-hover);
  }
  button.btn-tertiary:active {
    background: var(--button-tertiary-active);
    color: var(--background);
    border-color: var(--button-tertiary-active);
  }

  /*
   * Ghost button. Carbon recommends ghost over tertiary for toolbar / page-
   * level supplementary actions when the page already has primary buttons
   * (see https://carbondesignsystem.com/components/button/usage,
   * “Ghost buttons for data table actions”).
   *   - transparent bg + border
   *   - label colour: $link-primary (Blue 60)
   *   - hover bg: $background-hover; active bg: $background-active
   *   - same 32 px height as other sm buttons but symmetric padding
   */
  button.btn-ghost {
    background: transparent;
    color: var(--link-primary);
    border-color: transparent;
    /* ghost: 15px on the start side, 15px on the end side (density 16 - 1) */
    padding: 0 .9375rem;
  }
  button.btn-ghost:hover  { background: var(--layer-hover);  color: var(--link-hover); border-color: transparent; }
  button.btn-ghost:active { background: var(--layer-active); color: var(--link-hover); border-color: transparent; }
  /* Ghost focus is handled by the shared `button:focus` selector list
     above so its specificity beats `.btn-ghost:hover/:active`. */

  /* Icon-only ghost button (Carbon `.cds--btn--icon-only`): square 2 rem,
     symmetric padding, centred 16 px glyph. */
  button.btn-icon-only {
    width: 2rem; height: 2rem;
    padding: 0;
    justify-content: center;
  }
  button.btn-icon-only svg { width: 16px; height: 16px; fill: currentColor; }

  /* Bitfield data table */
  .bitfields { margin-top: .75rem; }
  .bf-table { width: 100%; border-collapse: collapse; font-size: .8125rem; background: var(--layer-01); }
  .bf-table th, .bf-table td { padding: .5rem .75rem; text-align: left; vertical-align: middle; border: 0; }
  .bf-table thead th {
    background: var(--layer-02); color: var(--text-secondary);
    font-weight: 600; font-size: .75rem; letter-spacing: .02em;
    border-bottom: 1px solid var(--border-subtle);
  }
  .bf-table tbody tr { border-bottom: 1px solid var(--border-subtle); }
  .bf-table tbody tr:last-child { border-bottom: 0; }
  .bf-table tbody tr:hover { background: var(--layer-hover); }
  .bf-val { color: var(--text-primary); font-weight: 400; }
  .bf-doc { color: var(--text-helper); }

  /* Status / messages */
  .error { color: #da1e28; font-size: .75rem; }
  .status { font-size: .75rem; color: var(--text-helper); margin-left: .5rem; }

  /* Toolbar */
  .toolbar {
    display: flex; align-items: center; gap: var(--spacing-03);
    margin: var(--spacing-05) 0 var(--spacing-06);
    flex-wrap: wrap;
  }
  .toolbar .spacer { flex: 1; }
  .toolbar label { font-size: .8125rem; color: var(--text-secondary); cursor: pointer; user-select: none; display: inline-flex; align-items: center; gap: var(--spacing-03); }

  /* Checkbox */
  input[type="checkbox"] {
    appearance: none; -webkit-appearance: none;
    width: 1rem; height: 1rem;
    background: transparent;
    border: 1px solid var(--text-secondary);
    margin: 0; padding: 0;
    position: relative; cursor: pointer;
    transition: background-color 70ms, border-color 70ms;
  }
  input[type="checkbox"]:hover { border-color: var(--text-primary); }
  input[type="checkbox"]:checked { background: var(--text-primary); border-color: var(--text-primary); }
  input[type="checkbox"]:checked::after {
    content: ''; position: absolute;
    left: 3px; top: 0px;
    width: 5px; height: 9px;
    border: solid var(--background);
    border-width: 0 2px 2px 0;
    transform: rotate(45deg);
  }
  input[type="checkbox"]:focus { outline: 2px solid var(--focus); outline-offset: 1px; }

  /* Layout + side-nav (Carbon `.cds--side-nav`).
     The side-nav is fixed to the left edge under the UI-Shell header and
     spans the full remaining viewport height. Main content is offset by
     the sidebar width when the sidebar is visible. */
  .layout { display: block; }
  .sidebar {
    position: fixed;
    top: var(--header-h);
    left: 0;
    width: var(--sidenav-w);
    height: calc(100vh - var(--header-h));
    overflow-y: auto;
    background: var(--layer-01);
    border-right: 1px solid var(--border-subtle);
    padding: var(--spacing-03) 0;
    display: none;
    z-index: 5;
  }
  .sidebar.visible { display: block; }
  .sidebar a {
    display: flex; align-items: baseline; gap: var(--spacing-03);
    /* Carbon side-nav item: 32 px height, 16 px inline padding */
    padding: var(--spacing-02) var(--spacing-05);
    min-height: 2rem;
    color: var(--text-secondary); text-decoration: none;
    font-size: .8125rem;
    white-space: nowrap; overflow: hidden;
    border-left: 3px solid transparent;
    transition: background-color 70ms, color 70ms, border-color 70ms;
  }
  .sidebar a .nav-name { overflow: hidden; text-overflow: ellipsis; }
  .sidebar a .nav-addr {
    margin-left: auto;
    font-family: 'IBM Plex Mono', ui-monospace, monospace;
    font-size: .6875rem; color: var(--text-helper);
    flex-shrink: 0;
  }
  .sidebar a:hover .nav-addr,
  .sidebar a.active .nav-addr { color: var(--text-secondary); }
  .sidebar a:hover { background: var(--layer-hover); color: var(--text-primary); }
  /* Carbon side-nav focus: 2 px inset stroke matching the focus token. */
  .sidebar a:focus {
    outline: 2px solid var(--focus);
    outline-offset: -2px;
  }
  .sidebar a.active {
    background: var(--layer-02); color: var(--text-primary);
    border-left-color: var(--button-primary);
    font-weight: 600;
  }
  .sidebar .nav-heading {
    color: var(--text-helper);
    font-weight: 600; font-size: .6875rem; letter-spacing: .04em; text-transform: uppercase;
    padding: var(--spacing-04) var(--spacing-05) var(--spacing-01);
  }
  .sidebar .nav-base {
    font-family: 'IBM Plex Mono', ui-monospace, monospace;
    font-size: .6875rem; color: var(--text-helper);
    padding: 0 var(--spacing-05) var(--spacing-02);
    text-transform: none; letter-spacing: 0; font-weight: 400;
  }
  .sidebar::-webkit-scrollbar { width: 8px; }
  .sidebar::-webkit-scrollbar-thumb { background: var(--border-subtle); }
  .sidebar::-webkit-scrollbar-track { background: transparent; }

  .main-content {
    /* Carbon page gutters: 32 px on wide displays, 16 px below 672 px. */
    padding: var(--spacing-06) var(--spacing-07);
    min-width: 0;
    transition: margin-left 70ms;
  }
  /* When the side-nav is visible, push content to the right of it. */
  body:has(.sidebar.visible) .main-content { margin-left: var(--sidenav-w); }
  @media (max-width: 720px) {
    .main-content { padding: var(--spacing-05); }
    body:has(.sidebar.visible) .main-content { margin-left: 0; }
    .sidebar.visible { display: none; }
  }

  a { color: var(--link-primary); }
  a:hover { color: var(--link-hover); }

  /* Drop-down options. Native <option> only allows colour + bg. */
  option {
    background: var(--layer-hover);
    color: var(--text-primary);
  }

  ::selection { background: var(--button-primary); color: var(--text-on-color); }
</style>
</head>
<body>

<header class="app-header" role="banner">
  <span class="brand" id="brand">
    ddevmem
    <span class="brand-sub">· Register Map</span>
  </span>
  <div class="header-actions">
    <button class="btn-ghost btn-icon-only" id="theme-toggle" onclick="toggleTheme()" aria-label="Toggle theme" title="Toggle theme"></button>
  </div>
</header>

<div class="layout">
<nav class="sidebar" id="sidebar"></nav>
<div class="main-content">

<h1 id="title">ddevmem — Register Map</h1>
<div class="meta" id="meta"></div>

<div class="toolbar">
  <button class="btn-ghost" onclick="refreshAll()">Refresh all</button>
  <button class="btn-ghost" onclick="dumpAll()">Dump all</button>
  <label><input type="checkbox" id="auto-refresh"> Auto-refresh (1s)</label>
  <span class="status" id="global-status"></span>
</div>

<div id="maps"></div>
</div>
</div>

<script>
const API_BASE = window.location.pathname.replace(/\/?$/, '/') + 'api';
let allMaps = []; // { slug, name, info, apiPrefix }
let autoTimer = null;

const $  = id => document.getElementById(id);
const ce = tag => document.createElement(tag);

async function api(path, body) {
  const opts = body !== undefined
    ? { method: 'POST', headers: {'Content-Type':'application/json'}, body: JSON.stringify(body) }
    : {};
  const r = await fetch(API_BASE + path, opts);
  if (!r.ok) throw new Error(r.status + ' ' + r.statusText);
  if (r.headers.get('content-type')?.includes('json')) return r.json();
}

function hexStr(val, bits) {
  const digits = Math.ceil(bits / 4);
  return '0x' + BigInt(val).toString(16).toUpperCase().padStart(digits, '0');
}

function extractBits(val, lo, hi) {
  const v = BigInt(val);
  const width = BigInt(hi - lo + 1);
  const mask = (1n << width) - 1n;
  return Number((v >> BigInt(lo)) & mask);
}

function setBits(oldVal, lo, hi, newFieldVal) {
  let v = BigInt(oldVal);
  const width = BigInt(hi - lo + 1);
  const mask = (1n << width) - 1n;
  v = (v & ~(mask << BigInt(lo))) | ((BigInt(newFieldVal) & mask) << BigInt(lo));
  return Number(v & ((1n << 64n) - 1n));
}

function escHtml(s) {
  const d = document.createElement('div');
  d.textContent = s;
  return d.innerHTML;
}

// Replace the UI-Shell brand with a user-supplied title. The original
// "ddevmem · Register Map" markup is split into two spans so the
// theming CSS can dim the suffix; once a custom title is set we drop
// that decoration and show the user's string verbatim.
function setBrand(title) {
  const el = $('brand');
  if (!el) return;
  el.textContent = title;
}

async function init() {
  try {
    // Try multi-map mode first (/api/maps), fall back to single-map (/api/info)
    let multiMode = false;
    let customTitle = null;
    try {
      const resp = await api('/maps');
      // The endpoint returns either the legacy bare array `[{slug,name},…]`
      // or the new wrapper `{ title, maps: […] }`. Accept both.
      const maps = Array.isArray(resp) ? resp : resp.maps;
      if (!Array.isArray(resp) && typeof resp.title === 'string') {
        customTitle = resp.title;
      }
      const infos = await Promise.all(maps.map(m => api('/' + m.slug + '/info')));
      allMaps = maps.map((m, i) => ({
        slug: m.slug, name: m.name, info: infos[i],
        apiPrefix: '/' + m.slug
      }));
      multiMode = true;
    } catch (_) {
      const info = await api('/info');
      allMaps = [{ slug: '_', name: info.name, info: info, apiPrefix: '' }];
    }

    if (multiMode) {
      const heading = customTitle || 'ddevmem — Register Maps';
      $('title').textContent = heading;
      document.title = heading;
      if (customTitle) setBrand(customTitle);
      $('meta').textContent = allMaps.length + ' register map(s)';
    } else {
      const info = allMaps[0].info;
      $('title').textContent = info.name + ' — Register Map';
      document.title = info.name + ' — Register Map';
      $('meta').textContent = 'Base: ' + hexStr(info.base_address, 32)
        + '  |  Bus width: ' + (info.bus_width * 8) + ' bit'
        + '  |  ' + info.registers.length + ' register(s)';
    }

    renderAll(multiMode);
    refreshAll();
  } catch (e) {
    $('maps').innerHTML = '<div class="error">Failed to load: ' + escHtml(e.message) + '</div>';
  }
}

/*
 * Firefox and Safari do not move keyboard focus to <button> on click \u2014
 * `:focus` only fires on Tab navigation. Carbon explicitly relies on
 * pointerdown-driven focus to keep the focus indicator visible while
 * the user is pressing the button. We delegate from the document so
 * dynamically rendered buttons (re-created by renderAll) are covered.
 */
document.addEventListener('pointerdown', (e) => {
  const btn = e.target.closest('button');
  if (btn && !btn.disabled) btn.focus();
});

function renderAll(showHeaders) {
  const container = $('maps');
  container.innerHTML = '';
  for (const map of allMaps) {
    const info = map.info;
    const section = ce('div');
    section.id = 'map-' + map.slug;

    let html = '';
    if (showHeaders) {
      html += '<h2>' + escHtml(info.name)
            + '<button class="btn-ghost map-dump" onclick="dumpMap(\'' + map.slug + '\')">Dump</button>'
            + '</h2>';
      html += '<div class="meta">' + escHtml(map.slug) + ' · Base: ' + hexStr(info.base_address, 32)
            + ' · Bus: ' + (info.bus_width * 8) + '-bit · ' + info.registers.length + ' register(s)</div>';
    }

    for (const reg of info.registers) {
      const uid = map.slug + '-' + reg.offset;
      html += '<div class="reg-card" id="reg-' + uid + '">';
      html += '<div class="reg-header">'
            + '<span class="reg-name">' + escHtml(reg.name) + '</span>'
            + '<span class="badge badge-' + reg.access + '">' + reg.access + '</span>'
            + '<span class="reg-offset">' + hexStr(reg.offset, 16) + ' (' + reg.width + '-bit)</span>'
            + '</div>';
      if (reg.doc) html += '<div class="reg-doc">' + escHtml(reg.doc) + '</div>';

      html += '<div class="reg-value">';
      if (reg.access !== 'wo') {
        html += '<span>Value:</span><span class="hex" id="val-' + uid + '">—</span>';
      }
      if (reg.access !== 'ro') {
        html += '<input id="inp-' + uid + '" placeholder="hex value">';
      }
      // Write + Read form a Carbon btn-set (flush, 1px separator).
      const hasWrite = reg.access !== 'ro';
      const hasRead  = reg.access !== 'wo';
      if (hasWrite || hasRead) {
        html += '<div class="btn-set">';
        if (hasWrite) {
          html += '<button onclick="writeReg(\'' + map.slug + '\',' + reg.offset + ',' + reg.width + ')">Write</button>';
        }
        if (hasRead) {
          html += '<button class="btn-secondary" onclick="readReg(\'' + map.slug + '\',' + reg.offset + ',' + reg.width + ')">Read</button>';
        }
        html += '</div>';
      }
      html += '<span class="status" id="st-' + uid + '"></span></div>';

      if (reg.bitfields.length > 0) {
        html += '<div class="bitfields"><table class="bf-table"><thead><tr>'
              + '<th>Field</th><th>Bits</th><th>Value</th>';
        if (reg.access !== 'ro') html += '<th>Set</th>';
        html += '<th>Doc</th></tr></thead><tbody>';
        for (const bf of reg.bitfields) {
          const bits = bf.lo === bf.hi ? '' + bf.lo : bf.hi + ':' + bf.lo;
          const bfid = uid + '-' + bf.name;
          html += '<tr><td>' + escHtml(bf.name) + '</td><td>' + bits + '</td>'
                + '<td class="bf-val" id="bf-' + bfid + '">—</td>';
          if (reg.access !== 'ro') {
            html += '<td><div class="bf-set">';
            if (bf.variants && bf.variants.length > 0) {
              html += '<select class="bf-input" id="bfi-' + bfid + '">';
              for (const v of bf.variants) {
                html += '<option value="' + v.value + '">' + escHtml(v.name) + ' (' + v.value + ')</option>';
              }
              html += '</select>';
            } else {
              html += '<input class="bf-input" id="bfi-' + bfid + '" placeholder="val">';
            }
            html += '<button onclick="writeBitfield(\'' + map.slug + '\',' + reg.offset + ',' + bf.lo + ',' + bf.hi + ',\'' + bf.name + '\',' + reg.width + ')">Set</button></div></td>';
          }
          html += '<td class="bf-doc">' + escHtml(bf.doc) + '</td></tr>';
        }
        html += '</tbody></table></div>';
      }
      html += '</div>';
    }
    section.innerHTML = html;
    container.appendChild(section);
  }

  // Build sidebar navigation
  const sidebar = $('sidebar');
  let navHtml = '';
  for (const map of allMaps) {
    const info = map.info;
    if (allMaps.length > 1 || map.slug !== '_') {
      navHtml += '<div class="nav-heading">' + escHtml(info.name) + '</div>';
    }
    navHtml += '<div class="nav-base" title="Base address">Base ' + hexStr(info.base_address, 32) + '</div>';
    for (const reg of info.registers) {
      const uid = map.slug + '-' + reg.offset;
      navHtml += '<a href="#reg-' + uid + '" title="' + escHtml(reg.name) + ' ' + hexStr(reg.offset, 16) + '">'
               + '<span class="nav-name">' + escHtml(reg.name) + '</span>'
               + '<span class="nav-addr">' + hexStr(reg.offset, 16) + '</span>'
               + '</a>';
    }
  }
  sidebar.innerHTML = navHtml;
  if (allMaps.reduce((s, m) => s + m.info.registers.length, 0) > 1) {
    sidebar.classList.add('visible');
  }
  setupScrollSpy();
}

/*
 * Scroll spy: highlight the sidebar link that corresponds to the register
 * currently in view, and reflect its anchor in the URL hash. Uses
 * IntersectionObserver against the .reg-card elements.
 */
let scrollSpyObserver = null;
let visibleCards = new Set();
function setupScrollSpy() {
  if (scrollSpyObserver) scrollSpyObserver.disconnect();
  visibleCards = new Set();
  const cards = document.querySelectorAll('.reg-card');
  if (!cards.length) return;
  // rootMargin: trigger when card enters the top portion of the viewport.
  scrollSpyObserver = new IntersectionObserver((entries) => {
    for (const e of entries) {
      if (e.isIntersecting) visibleCards.add(e.target.id);
      else visibleCards.delete(e.target.id);
    }
    // When the page is scrolled to the very bottom the last card cannot
    // reach the top of the viewport — in that case force-pick the last
    // visible card so the sidebar indicator still tracks the user.
    const atBottom = window.innerHeight + window.scrollY >=
                     document.documentElement.scrollHeight - 2;
    if (atBottom && visibleCards.size > 0) {
      let bottomId = null, bottomY = -Infinity;
      for (const id of visibleCards) {
        const el = document.getElementById(id);
        if (!el) continue;
        const y = el.getBoundingClientRect().top;
        if (y > bottomY) { bottomY = y; bottomId = id; }
      }
      if (bottomId) { setActiveLink(bottomId); return; }
    }
    // Otherwise: pick the topmost visible card (smallest top offset).
    let topId = null, topY = Infinity;
    for (const id of visibleCards) {
      const el = document.getElementById(id);
      if (!el) continue;
      const y = el.getBoundingClientRect().top;
      if (y < topY) { topY = y; topId = id; }
    }
    if (topId) setActiveLink(topId);
  }, {
    rootMargin: '-10% 0px -70% 0px',
    threshold: 0,
  });
  cards.forEach(c => scrollSpyObserver.observe(c));

  // Direct click on a sidebar link: highlight immediately, regardless of
  // whether the target card can actually reach the IntersectionObserver's
  // trigger band (last register cannot, since there's nothing below it).
  document.querySelectorAll('.sidebar a').forEach(a => {
    a.addEventListener('click', () => {
      const href = a.getAttribute('href') || '';
      if (href.startsWith('#')) setActiveLink(href.slice(1));
    });
  });
}

function setActiveLink(cardId) {
  const links = document.querySelectorAll('.sidebar a');
  let activeLink = null;
  links.forEach(a => {
    const isActive = a.getAttribute('href') === '#' + cardId;
    a.classList.toggle('active', isActive);
    if (isActive) activeLink = a;
  });
  if (activeLink) {
    // Scroll the sidebar so the active link stays visible.
    const sb = $('sidebar');
    if (sb) {
      const lr = activeLink.getBoundingClientRect();
      const sr = sb.getBoundingClientRect();
      if (lr.top < sr.top || lr.bottom > sr.bottom) {
        activeLink.scrollIntoView({ block: 'nearest' });
      }
    }
    // Update the URL hash without triggering a jump.
    if (location.hash !== '#' + cardId) {
      history.replaceState(null, '', '#' + cardId);
    }
  }
}

function getMap(slug) {
  return allMaps.find(m => m.slug === slug);
}

async function readReg(slug, offset, width) {
  const map = getMap(slug);
  if (!map) return;
  const uid = slug + '-' + offset;
  const st = $('st-' + uid);
  try {
    const resp = await api(map.apiPrefix + '/read', { offset });
    const el = $('val-' + uid);
    if (el) el.textContent = hexStr(resp.value, width);
    updateBitfields(slug, offset, resp.value);
    if (st) st.textContent = '';
  } catch (e) {
    if (st) st.textContent = e.message;
  }
}

function updateBitfields(slug, offset, value) {
  const map = getMap(slug);
  if (!map) return;
  const reg = map.info.registers.find(r => r.offset === offset);
  if (!reg) return;
  for (const bf of reg.bitfields) {
    const el = $('bf-' + slug + '-' + offset + '-' + bf.name);
    if (!el) continue;
    const rawVal = extractBits(value, bf.lo, bf.hi);
    if (bf.variants && bf.variants.length > 0) {
      const v = bf.variants.find(v => v.value === rawVal);
      el.textContent = v ? v.name + ' (' + rawVal + ')' : String(rawVal);
      const sel = $('bfi-' + slug + '-' + offset + '-' + bf.name);
      if (sel && sel.tagName === 'SELECT') sel.value = String(rawVal);
    } else {
      el.textContent = rawVal;
    }
  }
}

async function writeReg(slug, offset, width) {
  const map = getMap(slug);
  if (!map) return;
  const uid = slug + '-' + offset;
  const inp = $('inp-' + uid);
  const st = $('st-' + uid);
  if (!inp) return;
  try {
    const value = Number(inp.value.trim());
    if (isNaN(value)) throw new Error('invalid number');
    await api(map.apiPrefix + '/write', { offset, value });
    if (st) st.textContent = 'written';
    await readReg(slug, offset, width);
  } catch (e) {
    if (st) st.textContent = e.message;
  }
}

async function writeBitfield(slug, offset, lo, hi, name, width) {
  const map = getMap(slug);
  if (!map) return;
  const uid = slug + '-' + offset;
  const inp = $('bfi-' + uid + '-' + name);
  const st = $('st-' + uid);
  if (!inp) return;
  try {
    const fieldVal = Number(inp.value.trim());
    if (isNaN(fieldVal)) throw new Error('invalid number');
    const resp = await api(map.apiPrefix + '/read', { offset });
    const newVal = setBits(resp.value, lo, hi, fieldVal);
    await api(map.apiPrefix + '/write', { offset, value: newVal });
    if (st) st.textContent = name + ' updated';
    await readReg(slug, offset, width);
  } catch (e) {
    if (st) st.textContent = e.message;
  }
}

async function refreshAll() {
  for (const map of allMaps) {
    for (const reg of map.info.registers) {
      if (reg.access !== 'wo') readReg(map.slug, reg.offset, reg.width);
    }
  }
}

async function dumpAll() { return dumpMaps(allMaps, 'global-status'); }
async function dumpMap(slug) {
  const map = getMap(slug);
  if (!map) return;
  return dumpMaps([map], 'global-status');
}

async function dumpMaps(maps, statusId) {
  if (!maps.length) return;
  const gs = statusId ? $(statusId) : null;
  if (gs) gs.textContent = 'Dumping...';
  try {
    const ts = new Date().toISOString().replace(/[:.]/g, '-');
    const multi = maps.length > 1 || maps[0].slug !== '_';
    let lines = [];
    lines.push('# ddevmem register dump');
    lines.push('# Date: ' + new Date().toISOString());
    if (multi) lines.push('# Maps: ' + maps.length);
    lines.push('');
    // For a single map the file is named after the map itself; for a
    // multi-map dump (e.g. "Dump all") we use a generic prefix instead of
    // accidentally taking the first map's name.
    let fileName = multi ? 'ddevmem-all' : null;
    for (const map of maps) {
      const info = map.info;
      if (multi) {
        lines.push('## ' + info.name + ' (' + map.slug + ')');
      } else {
        lines[0] = '# ' + info.name + ' register dump';
      }
      if (!fileName) {
        // Single-map dump: include the base address so dumps from
        // different instances of the same peripheral don't collide.
        fileName = info.name + '@' + hexStr(info.base_address, 32);
      }
      lines.push('# Base address: ' + hexStr(info.base_address, 32));
      lines.push('# Bus width: ' + (info.bus_width * 8) + ' bit');
      lines.push('');
      for (const reg of info.registers) {
        if (reg.access === 'wo') {
          lines.push(hexStr(reg.offset, 16) + '  ' + reg.name.padEnd(20) + '  [write-only]');
          continue;
        }
        try {
          const resp = await api(map.apiPrefix + '/read', { offset: reg.offset });
          const val = resp.value;
          lines.push(hexStr(reg.offset, 16) + '  ' + reg.name.padEnd(20) + '  ' + hexStr(val, reg.width) + '  (' + reg.access + ')');
          for (const bf of reg.bitfields) {
            const raw = extractBits(val, bf.lo, bf.hi);
            const bits = bf.lo === bf.hi ? 'bit ' + bf.lo : 'bits ' + bf.hi + ':' + bf.lo;
            let valStr = String(raw);
            if (bf.variants && bf.variants.length > 0) {
              const v = bf.variants.find(v => v.value === raw);
              if (v) valStr = v.name + ' (' + raw + ')';
            }
            lines.push('    ' + bf.name.padEnd(20) + '  [' + bits + ']  = ' + valStr);
          }
        } catch (e) {
          lines.push(hexStr(reg.offset, 16) + '  ' + reg.name.padEnd(20) + '  [read error: ' + e.message + ']');
        }
      }
      lines.push('');
    }
    const blob = new Blob([lines.join('\n') + '\n'], { type: 'text/plain' });
    const a = document.createElement('a');
    a.href = URL.createObjectURL(blob);
    a.download = (fileName || 'regdump') + '_' + ts + '.txt';
    a.click();
    URL.revokeObjectURL(a.href);
    if (gs) gs.textContent = 'Dump saved';
  } catch (e) {
    if (gs) gs.textContent = 'Dump failed: ' + e.message;
  }
}

$('auto-refresh').addEventListener('change', function() {
  if (this.checked) {
    autoTimer = setInterval(refreshAll, 1000);
  } else {
    clearInterval(autoTimer);
    autoTimer = null;
  }
});

function updateThemeButton() {
  const t = document.documentElement.getAttribute('data-theme');
  const btn = $('theme-toggle');
  if (!btn) return;
  // Carbon icons (16×16): "asleep" (moon) shown when in dark theme,
  // "light" (sun) shown when in light theme. The icon mirrors the *current*
  // theme, the title hints at the action ("Switch to …").
  const dark = t !== 'g10';
  const moon = '<svg viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">'
             + '<path d="M13.502 5.414a15.075 15.075 0 0 0 11.594 18.194 11.113 11.113 0 0 1-7.975 3.39c-.138 0-.278.005-.418 0a11.094 11.094 0 0 1-3.2-21.584M14.98 3a1 1 0 0 0-.175.016 13.096 13.096 0 0 0 1.825 25.981c.164.006.328 0 .49 0a13.072 13.072 0 0 0 10.703-5.555 1.01 1.01 0 0 0-.783-1.582 13.08 13.08 0 0 1-11.107-19.302A1.005 1.005 0 0 0 14.98 3z"/></svg>';
  const sun  = '<svg viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">'
             + '<path d="M16 12.005a4 4 0 1 1-4 4 4.005 4.005 0 0 1 4-4m0-2a6 6 0 1 0 6 6 6 6 0 0 0-6-6zM5.394 6.813 6.81 5.399 9.64 8.227 8.226 9.643zM2 15.005h4v2H2zM5.394 25.196 8.222 22.367 9.636 23.781 6.808 26.61zM15 26.005h2v4h-2zM22.362 23.79l1.414-1.414 2.828 2.827-1.414 1.415zM26 15.005h4v2h-4zM22.359 8.227 25.187 5.399 26.601 6.813 23.773 9.641zM15 2.005h2v4h-2z"/></svg>';
  btn.innerHTML = dark ? moon : sun;
  btn.title = dark ? 'Switch to light theme' : 'Switch to dark theme';
  btn.setAttribute('aria-label', btn.title);
}
function toggleTheme() {
  const cur = document.documentElement.getAttribute('data-theme');
  const next = cur === 'g10' ? 'g100' : 'g10';
  document.documentElement.setAttribute('data-theme', next);
  try { localStorage.setItem('ddevmem-theme', next); } catch (_) {}
  updateThemeButton();
}
updateThemeButton();

init();
</script>
</body>
</html>