/* Shared reader controls */
.site-header { background: var(--paper); }
.site-header .bar { align-items: center; gap: 12px; flex-wrap: nowrap; }
.site-header .brand { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.site-header .brand small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.site-header .bar > nav { flex: 0 1 auto; min-width: 0; overflow: auto; flex-wrap: nowrap; justify-content: flex-start; }
.header-tools { display: flex; align-items: center; gap: 2px; border-left: 1px solid var(--hairline); padding-left: 8px; }
.header-menu { position: relative; }
.header-menu summary, .tool-button, .search-dialog button {
  font: 500 13px/1.4 var(--sans); color: var(--ink-soft); cursor: pointer;
  background: transparent; border: 0; border-radius: 4px;
}
.header-menu summary, .tool-button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 7px 9px; min-height: 36px; white-space: nowrap;
}
.header-menu summary { list-style: none; }
.header-menu summary::-webkit-details-marker { display: none; }
.header-menu:not(.theme-menu) summary::after {
  content: ''; width: 5px; height: 5px; border: solid currentColor; border-width: 0 1px 1px 0;
  transform: translateY(-2px) rotate(45deg);
}
.header-menu[open] summary, .header-menu summary:hover, .tool-button:hover { color: var(--accent); background: var(--accent-soft); }
.tool-button svg { flex: none; width: 18px; height: 18px; stroke: currentColor; stroke-width: 1.65; fill: none; }
.site-header .menu-panel {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 2;
  display: grid; margin: 0; padding: 6px; gap: 0;
  max-height: min(62vh, 440px); overflow: auto; overscroll-behavior: contain;
  background: var(--paper); border: 1px solid var(--hairline); border-radius: 6px;
  box-shadow: 0 8px 24px rgb(0 0 0 / .13); scrollbar-width: thin;
}
.site-header .menu-panel::-webkit-scrollbar { display: block; width: 5px; }
.site-header .chapter-menu .menu-panel { width: min(310px, 80vw); }
.site-header .menu-panel a, .menu-panel label {
  display: block; padding: 8px 10px; line-height: 1.55; white-space: normal;
  font: 13px/1.55 var(--sans); color: var(--ink); border: 0; border-radius: 3px;
}
.site-header .menu-panel a:hover, .site-header .menu-panel a[aria-current], .menu-panel label:hover {
  background: var(--accent-soft); color: var(--accent);
}
.menu-shortcuts { border-bottom: 1px solid var(--hairline-soft); padding-bottom: 4px; margin-bottom: 4px; }
.theme-menu .menu-panel { min-width: 8em; }
.theme-menu legend { position: absolute; clip-path: inset(50%); }
.theme-menu label { cursor: pointer; }
.theme-menu input { accent-color: var(--accent); margin: 0 8px 0 0; }
.mobile-menu { display: none; }
.bar :focus-visible, .search-dialog :focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

.search-dialog {
  width: min(620px, calc(100% - 24px)); max-height: 78vh; max-height: 78dvh;
  padding: 20px; color: var(--ink); background: var(--paper); border: 1px solid var(--hairline);
  border-top: 3px solid var(--accent); border-radius: 6px; overscroll-behavior: contain;
  font: 14px/1.6 var(--sans);
}
.search-dialog::backdrop { background: rgb(19 23 28 / .5); }
.search-top, .search-fields { display: flex; align-items: center; gap: 8px; }
.search-top { justify-content: space-between; margin-bottom: 12px; }
.search-top h2 { margin: 0; font: bold 22px/1.4 var(--serif); }
.search-dialog button { padding: 7px 10px; }
.search-dialog button:hover { color: var(--accent); background: var(--accent-soft); }
.search-fields input, .search-fields select {
  min-width: 0; font: inherit; color: var(--ink); background: var(--paper-warm);
  border: 1px solid var(--hairline); border-radius: 4px; height: 42px; padding: 0 9px;
}
.search-fields input { flex: 1; }
.search-fields select { max-width: 37%; }
.search-status { color: var(--ink-mute); font-size: 13px; margin: 12px 0 3px; }
.search-results { margin: 0; padding: 0; list-style: none; }
.search-results li { border-top: 1px solid var(--hairline-soft); }
.search-results a { display: grid; gap: 3px; padding: 10px 4px; color: var(--ink); overflow-wrap: anywhere; }
.search-results a:hover { text-decoration: none; background: var(--accent-soft); }
.search-results small { color: var(--accent); }
.search-results strong { font: 600 16px/1.55 var(--serif); }
.search-results span { color: var(--ink-soft); font-size: 13px; }
.search-more { width: 100%; }
@media (max-width: 800px) {
  .site-header .bar { padding: 2px 10px 2px 14px; gap: 4px; min-height: 46px; }
  .site-header .brand { flex: 1; }
  .site-header .brand small, .site-header .bar > nav, .search-trigger span { display: none; }
  .mobile-menu { display: block; }
  .header-tools { padding-left: 3px; }
  .site-header .tool-button { width: 42px; height: 42px; }
  .site-header .header-menu > summary { min-height: 42px; }
  .site-header .header-menu:not(.theme-menu) { position: static; }
  .site-header .header-menu:not(.theme-menu) .menu-panel { right: 10px; top: 100%; width: min(320px, calc(100vw - 20px)); }
}
@media (max-width: 440px) {
  .search-fields { align-items: stretch; flex-direction: column; }
  .search-fields input { flex: none; }
  .search-fields select { align-self: flex-start; max-width: 100%; height: 34px; }
}
@media screen {
  html[data-theme="warm"] {
    color-scheme: light;
    --paper: #f6efdf; --paper-warm: #efe5d1; --ink: #342d23; --ink-soft: #594e40;
    --ink-mute: #685c4c; --hairline: #cabda7; --hairline-soft: #daceba;
  }
  html[data-theme="dark"] {
    color-scheme: dark;
    --paper: #191d21; --paper-warm: #22282d; --ink: #e8e5df; --ink-soft: #c7c5bf;
    --ink-mute: #a8b0b6; --hairline: #51595e; --hairline-soft: #384046;
    --ch1: #88c6ca; --ch2: #a5bde8; --ch3: #cbb0e2; --ch4: #a0caab;
    --ch5: #e2b08b; --ch6: #e7a6bc; --ch7: #91c8e0; --ch8: #dfc382; --ch9: #adbedf;
  }
  /* A few older books declare chapter ink and pale callouts directly on <body>. */
  html[data-theme="dark"] body {
    --accent: var(--night-accent, var(--ch1)) !important;
    --accent-soft: color-mix(in srgb, var(--accent) 11%, var(--paper)) !important;
    --accent-line: color-mix(in srgb, var(--accent) 48%, var(--paper)) !important;
  }
  .ch2 { --night-accent: var(--ch2); } .ch3 { --night-accent: var(--ch3); }
  .ch4 { --night-accent: var(--ch4); } .ch5 { --night-accent: var(--ch5); }
  .ch6 { --night-accent: var(--ch6); } .ch7 { --night-accent: var(--ch7); }
  .ch8 { --night-accent: var(--ch8); } .ch9 { --night-accent: var(--ch9); }
  html[data-theme="dark"] .bcard-cat, html[data-theme="dark"] .bcard:hover .bcard-go {
    color: color-mix(in srgb, var(--bc), white 55%);
  }
  html[data-theme="dark"] .sbox-warn .box-label { color: #eaa79f; }
  html[data-theme="dark"] .sections li a { background: var(--paper-warm); }
  html[data-theme="dark"] .sections li a:hover { background: var(--accent-soft); }
  /* Existing scientific artwork mixes fixed inks with theme variables. Keep each
     plate on its original light stock; labels outside the plate follow the reader. */
  html[data-theme="dark"] :is(.figure svg, .fig-inner canvas, .gcn-viz .gv-panel) {
    color-scheme: light; color: #1d232b; background-color: #fbfaf7;
    --ink: #1d232b; --ink-soft: #4a5462; --ink-mute: #606973; --paper: #fff; --paper-warm: #fbfaf7;
    --accent: #2e5f66; --accent-soft: #edf2f1; --hairline: #d9d6cd; --hairline-soft: #e8e5dc;
    --ch1:#2e5f66; --ch2:#35507e; --ch3:#6b4d8f; --ch4:#3d6b4a; --ch5:#9c5230;
    --ch6:#8f3d5c; --ch7:#2f6f8f; --ch8:#96702e; --ch9:#33415e;
  }
  html[data-theme="dark"] .gcn-tour { background: var(--paper-warm); }
  html[data-theme="dark"] :is(.demo-btn,.btn-top,.nmr-primer,details.nmr-depth,.nmr-lab > .fig-inner,
    .nmr-lab select,.nmr-lab button:not([data-lab-play]),.nmr-lab-plot,.nmr-mode-buttons button:not([aria-pressed="true"])) {
    background: var(--paper-warm);
  }
  html[data-theme="dark"] :is(.nmr-lab button[data-lab-play], .nmr-mode-buttons button[aria-pressed="true"]) { color: var(--paper); }
}
@media print { .search-dialog, .header-tools { display: none; } }
