/*
 * Base: reset, document typography and the app grid. Depends on tokens.css.
 *
 *  body.app
 *  ├─ header.app-header                      fixed height --header-h, full width
 *  ├─ div.app-body                            grid: sidebar | text | scan, each pane scrolls itself
 *  │   ├─ aside.pane-sidebar#sidebar          TOC; a drawer below 1200px (also holds the nav there)
 *  │   ├─ main.pane-text#main                 reading column / page content
 *  │   └─ section.pane-scan#scan              scan viewer (dark in both themes)
 *  └─ section.viewbar                         phones only: Text / Scan bar at the bottom
 *
 * Panes are keyed on <html> attributes, set before first paint:
 *   data-view="text|split|scan"   which panes show (only matters when body.has-scan)
 *   data-sidebar="collapsed"      desktop sidebar hidden (remembered in localStorage vm.sidebar)
 *   class="drawer-open"           drawer open (tablet/phone)
 * Breakpoints: phone < 768px, tablet 768-1199px, desktop >= 1200px.
 */

/* ---------------------------------------------------------------------------------------- */
/* Reset                                                                                     */
/* ---------------------------------------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body,
h1, h2, h3, h4, h5, h6,
p, figure, blockquote, dl, dd, ol, ul, pre {
  margin: 0;
}

/* Zero specificity, so a component's own padding (.sv-thumb-grid, .find-results) always wins. */
:where(ol[class], ul[class]) { padding: 0; list-style: none; }

img, svg, video, canvas, iframe { display: block; max-width: 100%; }
img, video { height: auto; }

button, input, select, textarea {
  font: inherit;
  color: inherit;
  letter-spacing: inherit;
}

button { cursor: pointer; background: none; border: 0; padding: 0; }
button:disabled { cursor: default; }

a { color: inherit; text-decoration: none; }

table { border-collapse: collapse; }

[hidden] { display: none !important; }

/* ---------------------------------------------------------------------------------------- */
/* Document                                                                                  */
/* ---------------------------------------------------------------------------------------- */
body {
  font-family: var(--font-ui);
  font-size: var(--fs-ui);
  line-height: 1.5;
  color: var(--text);
  background: var(--bg-reading);
  font-kerning: normal;
  font-optical-sizing: auto;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

::selection { background: var(--accent-soft); color: inherit; }
:root[data-theme="dark"] ::selection { background: rgba(224, 133, 107, .3); }

/* Focus: one visible ring everywhere, keyboard only. */
:focus { outline: none; }
:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}
.pane-text:focus-visible, .pane-sidebar:focus-visible { outline-offset: -2px; border-radius: 0; }

/* Thin, quiet scrollbars: rounded thumbs, no arrow buttons. Chromium and Safari use the
   ::-webkit-scrollbar rules; the standard properties would override them there (Chrome 121+
   then draws its native bar with arrows), so only browsers without ::-webkit-scrollbar get them. */
@supports not selector(::-webkit-scrollbar) {
  * { scrollbar-width: thin; scrollbar-color: var(--scrollbar) transparent; }
}
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-button { display: none; }
::-webkit-scrollbar-corner { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--scrollbar);
  border: 3px solid transparent;
  background-clip: padding-box;
  border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover { background-color: var(--scrollbar-hover); }

/* Accessibility helpers. */
.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

.skip-link {
  position: fixed;
  top: 8px; left: 8px;
  z-index: calc(var(--z-progress) + 1);
  padding: 8px 14px;
  border-radius: var(--radius-pill);
  background: var(--segment-on-bg);
  color: var(--segment-on-fg);
  font-size: var(--fs-ui);
  transform: translateY(-150%);
  transition: transform var(--dur) var(--ease);
}
.skip-link:focus-visible { transform: none; }

/* Reduced motion: nothing animates. transition: none, not a near-zero duration: with
   transition-property all, a .01ms transition on every element still keeps the old value of
   any changed property for one frame (the panes' grid columns, so a pane coming back would lay out
   at 0px wide). No script waits for transitionend. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
}

/* ---------------------------------------------------------------------------------------- */
/* Type scale (utility classes; components use the tokens directly)                          */
/* ---------------------------------------------------------------------------------------- */
.serif { font-family: var(--font-display); }
.sans { font-family: var(--font-ui); }

.t-display { font-family: var(--font-display); font-size: var(--fs-display); font-weight: 400; line-height: 1.1; color: var(--text-title); letter-spacing: -0.005em; font-variant-numeric: lining-nums; }
.t-title   { font-family: var(--font-display); font-size: var(--fs-article-title); font-weight: 400; line-height: 1.15; color: var(--text-title); }
.t-h2      { font-family: var(--font-display); font-size: var(--fs-h2); font-weight: 400; line-height: 1.2; color: var(--text-title); }
.t-h3      { font-family: var(--font-display); font-size: var(--fs-h3); font-weight: 400; line-height: 1.25; color: var(--text-title); }
.t-body    { font-family: var(--font-reading); font-size: var(--reading-size); line-height: var(--reading-leading); }
.t-ui      { font-size: var(--fs-ui); }
.t-small   { font-size: var(--fs-sm); }
.t-muted   { color: var(--text-muted); }

.t-caps {
  font-size: var(--fs-xs);
  font-weight: var(--weight-ui-strong);
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
}

.kicker {
  font-family: var(--font-ui);
  font-size: var(--fs-kicker);
  font-weight: var(--weight-ui);
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--accent);
  text-wrap: balance;
}

.meta-caps {
  font-family: var(--font-ui);
  font-size: var(--fs-sm);
  letter-spacing: var(--tracking-meta);
  text-transform: uppercase;
  color: var(--text-muted);
  text-wrap: balance;
}
/* One part of a meta line (Html.MetaLine): "1893–1896" or "528 pages" never breaks inside, so a long
   line wraps between parts, after their middle dot. */
.meta-part { white-space: nowrap; }

kbd {
  font-family: var(--font-ui);
  font-size: var(--fs-xs);
  line-height: 1;
  padding: 3px 6px;
  border: 1px solid var(--control-border);
  border-radius: 5px;
  color: var(--text-muted);
  background: transparent;
}

/* ---------------------------------------------------------------------------------------- */
/* App grid                                                                                  */
/* ---------------------------------------------------------------------------------------- */
body.app {
  --col-sidebar: 0px;
  --col-text: 1fr;
  --col-scan: 0px;
  display: grid;
  grid-template-rows: var(--header-h) minmax(0, 1fr);
  grid-template-columns: minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.app-header { grid-row: 1; }

.app-body {
  grid-row: 2;
  display: grid;
  grid-template-columns: var(--col-sidebar) minmax(0, var(--col-text)) minmax(0, var(--col-scan));
  grid-template-areas: "sidebar text scan";
  min-height: 0;
  min-width: 0;
}

.pane-sidebar,
.pane-text,
.pane-scan {
  min-height: 0;
  min-width: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.pane-sidebar {
  grid-area: sidebar;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  color: var(--text-sidebar);
}

.pane-text {
  grid-area: text;
  background: var(--bg-reading);
  scroll-padding-top: var(--sp-6);
}

.pane-scan {
  grid-area: scan;
  display: none;
  background: var(--scan-bg);
  color: var(--scan-fg);
  overflow: hidden;            /* the viewer manages its own scrolling/panning */
  color-scheme: dark;
}

/* View modes (only pages with a scan pane switch panes). */
html[data-view="split"] body.has-scan { --col-scan: 1fr; }
html[data-view="split"] body.has-scan .pane-scan { display: flex; flex-direction: column; border-left: 1px solid var(--border); }
html[data-view="scan"] body.has-scan { --col-text: 0px; --col-scan: 1fr; }
html[data-view="scan"] body.has-scan .pane-scan { display: flex; flex-direction: column; }
html[data-view="scan"] body.has-scan .pane-text { display: none; }
/* The page's heading while the text pane (with its h1) is hidden. */
.scan-heading { display: none; }
html[data-view="scan"] body.has-scan .scan-heading { display: block; }

/* Drawer-only blocks inside the sidebar (head + nav on tablet/phone, settings + account on phone). */
.drawer-head,
.drawer-nav,
.drawer-footer { display: none; }

.scrim {
  position: fixed;
  inset: 0;
  z-index: calc(var(--z-drawer) - 1);
  background: var(--scrim);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--dur) var(--ease), visibility 0s linear var(--dur);
}
html.drawer-open .scrim {
  opacity: 1;
  visibility: visible;
  transition: opacity var(--dur) var(--ease), visibility 0s;
}

/* Phone bottom bar for view modes. */
.viewbar { display: none; }

/* ---------------------------------------------------------------------------------------- */
/* Desktop >= 1200px: sidebar is a column                                                    */
/* ---------------------------------------------------------------------------------------- */
@media (min-width: 1200px) {
  body.app.has-sidebar { --col-sidebar: var(--sidebar-w); }
  html[data-sidebar="collapsed"] body.app.has-sidebar { --col-sidebar: 0px; }
  html[data-sidebar="collapsed"] .pane-sidebar { display: none; }
  body.app:not(.has-sidebar) .pane-sidebar { display: none; }
  .scrim { display: none; }
}

/* ---------------------------------------------------------------------------------------- */
/* Tablet and phone: the sidebar is an overlay drawer                                        */
/* ---------------------------------------------------------------------------------------- */
@media (max-width: 1199.98px) {
  .pane-sidebar {
    position: fixed;
    z-index: var(--z-drawer);
    top: 0;
    bottom: 0;
    left: 0;
    width: min(var(--sidebar-w) + 32px, 86vw);
    border-right: 1px solid var(--border);
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform var(--dur) var(--ease), visibility 0s linear var(--dur);
  }
  html.drawer-open .pane-sidebar {
    transform: none;
    visibility: visible;
    transition: transform var(--dur) var(--ease), visibility 0s;
  }
  .drawer-head { display: flex; }
  .drawer-nav { display: block; }
}

/* ---------------------------------------------------------------------------------------- */
/* Phone < 768px: one column; view mode moves to a bottom bar (Text / Scan)                  */
/* ---------------------------------------------------------------------------------------- */
@media (max-width: 767.98px) {
  body.app.has-viewbar { grid-template-rows: var(--header-h) minmax(0, 1fr) var(--bottombar-h); }

  /* "Side by side" does not fit: show the text, the bar offers Text / Scan. */
  html[data-view="split"] body.has-scan { --col-scan: 0px; }
  html[data-view="split"] body.has-scan .pane-scan { display: none; }

  .viewbar {
    grid-row: 3;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 var(--sp-4) env(safe-area-inset-bottom);
    background: var(--bg-header);
    border-top: 1px solid var(--border);
  }
  .viewbar .segmented { width: min(100%, 320px); height: max(var(--control-h), calc(var(--tap-min) + 6px)); }
  .viewbar .segmented button { flex: 1; }

  .drawer-footer { display: block; }
}
