/*
 * Components shared by every page. Depends on tokens.css and base.css.
 * Markup and JS for each one: docs/design/DESIGN.md and the live style guide at /design.
 *
 * Contents
 *   1 Icons            7 Inputs, fields       13 Pager
 *   2 Header           8 Popover, menu        14 Tabs
 *   3 Drawer           9 Settings panel       15 Tooltip, abbreviations
 *   4 Buttons         10 Dialog, sheet        16 Table of contents (sidebar)
 *   5 Pills           11 Toast                17 States: empty, loading, error
 *   6 Segmented       12 Progress bar         18 Site footer
 *   (search: 7b)
 */

/* ======================================================================================== */
/* 1 Icons                                                                                  */
/* ======================================================================================== */
.icon {
  width: var(--icon);
  height: var(--icon);
  flex: none;
  vertical-align: middle;
}
.icon-sm { width: var(--icon-sm); height: var(--icon-sm); }
.icon-xs { width: 12px; height: 12px; }

@keyframes vm-spin { to { transform: rotate(360deg); } }
.spin { animation: vm-spin 1s linear infinite; }

/* ======================================================================================== */
/* 2 Header                                                                                 */
/* ======================================================================================== */
.app-header {
  position: relative;
  z-index: var(--z-header);
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  min-width: 0;
  padding: 0 var(--sp-3) 0 var(--sp-3);
  background: var(--bg-header);
  border-bottom: 1px solid var(--border);
}

.header-start {
  display: flex;
  flex: 1 1 auto;
  align-items: center;
  min-width: 0;
}

/* The controls on the right keep their size; the nav gives way (see .site-nav). */
.header-end {
  display: flex;
  flex: none;
  align-items: center;
  gap: var(--sp-2);
  margin-left: auto;
}

/* Wordmark: serif caps in the accent with a quiet tag, like "MOTHER INDIA 1949–2024". */
.wordmark {
  flex: none;
  display: inline-flex;
  align-items: baseline;
  gap: 10px;                 /* 13px between the inks, as in the reference */
  margin-left: 7px;
  padding: 4px 2px;
  white-space: nowrap;
  border-radius: var(--radius-sm);
}
.wordmark-name {
  font-family: var(--font-display);
  font-size: var(--fs-wordmark);
  font-weight: 400;
  line-height: 1;
  letter-spacing: 0.015em;
  text-transform: uppercase;
  color: var(--accent);
}
/* The tag is the rest of the name ("Vande Mataram Library"). It is set in the kicker's caps, not
   in sentence case like the reference's "1949–2024": a plain "Library" right before the "Library"
   nav link would read as a second link. It shares the nav links' baseline, which sits above the
   wordmark's (as in the reference): baseline-aligned with the name, then lifted by the difference. */
.wordmark-tag {
  font-family: var(--font-ui);
  font-size: var(--fs-wordmark-tag);
  font-weight: var(--weight-ui);
  line-height: 1;
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--text-muted);
  transform: translateY(-5px);
}

/* Primary navigation: plain text links. Safety net for crowded headers (extra headerTools):
   links that do not fit wrap onto a second row, which the fixed height hides, so a link is
   never cut in half or drawn under the controls. The breakpoints below keep all six visible. */
.site-nav {
  display: flex;
  flex: 1 1 auto;
  flex-wrap: wrap;
  align-content: flex-start;
  align-items: center;
  gap: 8px 2px;
  min-width: 0;
  height: 40px;              /* one 32px row + room for the focus ring */
  padding-top: 4px;
  margin-left: 1px;          /* 18px from the tag to the first link's text, as in the reference */
  overflow: hidden;
}
.site-nav a {
  padding: 6px 11px;
  font-size: var(--fs-ui);
  line-height: 20px;
  color: var(--text-nav);
  border-radius: var(--radius-pill);
  transition: color var(--dur-fast) var(--ease);
}
.site-nav a:hover { color: var(--text-title); }
.site-nav a[aria-current] { color: var(--text-title); }

/* Header text link (Sign in). */
.header-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: var(--control-h);
  padding: 0 10px;
  font-size: var(--fs-ui);
  color: var(--text-nav);
  border-radius: var(--radius-pill);
  white-space: nowrap;
}
.header-link:hover { color: var(--text-title); }

.header-tools { display: flex; align-items: center; gap: var(--sp-2); }

/* Hamburger hides on desktop pages that have no sidebar (nothing to toggle). */
@media (min-width: 1200px) {
  body.app:not(.has-sidebar) .menu-toggle { display: none; }
  body.app:not(.has-sidebar) .wordmark { margin-left: 4px; }
}

/* Reader pages also carry the view-mode control: make room for it on narrower desktops. */
@media (min-width: 1200px) and (max-width: 1439.98px) {
  body.has-viewmode .wordmark-tag { display: none; }
  body.has-viewmode .search { width: 190px; }
}
@media (min-width: 1200px) and (max-width: 1319.98px) {
  body.has-viewmode .wordmark-name { font-size: var(--fs-wordmark-compact); }
  body.has-viewmode .site-nav { margin-left: 8px; }
  body.has-viewmode .site-nav a { padding-inline: 8px; }
}

/* Tablet: nav lives in the drawer. */
@media (max-width: 1199.98px) {
  .site-nav { display: none; }
}
@media (min-width: 768px) and (max-width: 1023.98px) {
  body.has-viewmode .wordmark-tag { display: none; }
}

/* Below 1024px the search field becomes an icon that opens it over the header (7b). */
.icon-btn.search-toggle { display: none; }
@media (max-width: 1023.98px) {
  .icon-btn.search-toggle { display: inline-flex; }
}

/* Phone: wordmark + search icon + menu. */
@media (max-width: 767.98px) {
  .app-header { padding: 0 var(--sp-2); gap: var(--sp-1); }
  .wordmark { margin-left: 2px; gap: 9px; }
  /* At this size EB Garamond's caps sit high in the em box: a 1px optical nudge centres them. */
  .wordmark-name { font-size: var(--fs-wordmark-phone); transform: translateY(1px); }
  .wordmark-tag { font-size: var(--fs-2xs); letter-spacing: .18em; transform: translateY(-4px); }
  .header-end .settings-toggle,
  .header-end .account,
  .header-end .header-tools,
  .header-end [data-viewmode] { display: none; }
}
@media (max-width: 359.98px) {
  .wordmark-tag { display: none; }
}

/* ======================================================================================== */
/* 3 Drawer (the sidebar below 1200px)                                                      */
/* ======================================================================================== */
.drawer-head {
  position: sticky;
  top: 0;
  z-index: 1;
  align-items: center;
  justify-content: space-between;
  height: var(--header-h);
  padding: 0 var(--sp-2) 0 var(--sp-3);
  background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border);
}
.drawer-head .wordmark { margin-left: 0; }
.drawer-head .wordmark-name { font-size: var(--fs-wordmark-phone); transform: translateY(1px); }

.drawer-nav {
  padding: var(--sp-3) var(--sp-3) var(--sp-2);
  border-bottom: 1px solid var(--border);
}
.drawer-nav a {
  display: flex;
  align-items: center;
  min-height: 40px;
  padding: 0 var(--sp-3);
  font-size: var(--fs-md);
  color: var(--text-sidebar);
  border-radius: var(--radius-sm);
}
.drawer-nav a:hover { background: var(--hover-bg); color: var(--text-title); }
.drawer-nav a[aria-current] { color: var(--text-title); background: var(--current-bg); }

.drawer-footer {
  padding: var(--sp-3) var(--sp-3) calc(var(--sp-4) + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
}
.drawer-footer .menu-item { font-size: var(--fs-md); min-height: 40px; }

/* ======================================================================================== */
/* 4 Buttons                                                                                */
/* ======================================================================================== */
.btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 36px;
  padding: 0 16px;
  font-size: var(--fs-ui);
  font-weight: var(--weight-ui-strong);
  line-height: 1;
  white-space: nowrap;
  color: var(--text);
  background: transparent;
  border: 1px solid var(--control-border);
  border-radius: var(--radius-pill);
  transition: background-color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.btn:hover { background: var(--hover-bg); }
/* Touch screens: like .pill, a small button takes taps on an invisible --tap-min box around it. */
.btn::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  height: max(100%, var(--tap-min));
  transform: translateY(-50%);
}
.btn:disabled, .btn[aria-disabled="true"] { opacity: .5; pointer-events: none; }
.btn .icon { width: var(--icon-sm); height: var(--icon-sm); }

.btn-primary {
  color: var(--segment-on-fg);
  background: var(--segment-on-bg);
  border-color: var(--segment-on-bg);
}
.btn-primary:hover { background: var(--segment-on-bg); opacity: .9; }

.btn-accent {
  color: var(--accent-fg);
  background: var(--accent);
  border-color: var(--accent);
}
.btn-accent:hover { background: var(--accent); opacity: .92; }

.btn-ghost { border-color: transparent; }
.btn-danger { color: var(--danger); }
.btn-sm { height: 30px; padding: 0 12px; font-size: var(--fs-sm); }

/* Square icon button (header, toolbars). Always give it an aria-label. */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: var(--icon-btn);
  height: var(--icon-btn);
  color: var(--text-nav);
  border-radius: var(--radius-pill);
  transition: background-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.icon-btn:hover { color: var(--text-title); background: var(--hover-bg); }
.icon-btn[aria-expanded="true"]:not(.menu-toggle),
.icon-btn[aria-pressed="true"] { color: var(--text-title); background: var(--current-bg); }
/* Header icons (menu, settings, search on phones) in their own tone: in the dark theme a step brighter
   than the nav links, so the crisp 1px lines weigh what the reference's do. */
.app-header .icon-btn { color: var(--icon-header); }
.app-header .icon-btn:hover,
.app-header .icon-btn[aria-expanded="true"]:not(.menu-toggle),
.app-header .icon-btn[aria-pressed="true"] { color: var(--text-title); }
.icon-btn-sm { width: 30px; height: 30px; }
.icon-btn-sm .icon { width: var(--icon-sm); height: var(--icon-sm); }

/* Avatar (signed-in account button). */
.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  font-size: var(--fs-sm);
  font-weight: 600;
  line-height: 1;
  letter-spacing: 0;
  color: var(--accent);
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 1px var(--control-border);
  text-transform: uppercase;
}

/* ======================================================================================== */
/* 5 Pills (outlined, rounded): "Read beside the scans", "Cite", "1950 >"                   */
/* ======================================================================================== */
.pill {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: var(--pill-h);
  padding: 0 12px;
  font-family: var(--font-ui);
  font-size: var(--fs-sm);
  font-weight: 400;
  line-height: 1;
  letter-spacing: 0.01em;
  white-space: nowrap;
  color: var(--text-nav);
  background: transparent;
  border: 1px solid var(--control-border);
  border-radius: var(--radius-pill);
  transition: color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease), background-color var(--dur-fast) var(--ease);
}
.pill:hover { color: var(--text-title); border-color: var(--rule); background: var(--hover-bg); }
/* Touch screens: the pill keeps its size; an invisible box around it takes the tap (--tap-min). */
.pill::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  height: max(100%, var(--tap-min));
  transform: translateY(-50%);
}
.pill .icon { width: 13px; height: 13px; }
.pill[aria-pressed="true"],
.pill.is-active { color: var(--accent); border-color: currentColor; background: var(--accent-soft); }

.pill-sm {
  height: var(--pill-h-sm);
  padding: 0 10px;
  gap: 5px;
  font-size: var(--fs-xs);
}
.pill-sm .icon { width: 11px; height: 11px; }
.pill .icon-fill { fill: currentColor; }

.pill-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.pill-row-start { justify-content: flex-start; }

/* Filter chips (search page): a pill with a count. */
.pill .count {
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
}
.pill[aria-pressed="true"] .count { color: inherit; opacity: .8; }

/* ======================================================================================== */
/* 6 Segmented control: [data-viewmode] in the header, radios in settings                   */
/* ======================================================================================== */
.segmented {
  display: inline-flex;
  align-items: stretch;
  gap: 0;
  height: var(--control-h);
  padding: 2px;
  margin: 0;
  background: var(--control-bg);
  border: 1px solid var(--control-border);
  border-radius: var(--radius-pill);
  min-width: 0;
}
.segmented > button,
.segmented > label > span {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 100%;
  padding: 0 12px;
  font-size: var(--fs-segment);
  line-height: 1;
  white-space: nowrap;
  color: var(--text-nav);
  border-radius: var(--radius-pill);
  transition: background-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
  cursor: pointer;
  user-select: none;
}
.segmented > label { display: flex; flex: 1; position: relative; }
.segmented > label > span { flex: 1; }
.segmented > label > input { position: absolute; opacity: 0; inset: 0; margin: 0; pointer-events: none; }
.segmented > button:hover,
.segmented > label:hover > span { color: var(--text-title); }
.segmented > button[aria-pressed="true"],
.segmented > button[aria-checked="true"],
.segmented > label > input:checked + span {
  color: var(--segment-on-fg);
  background: var(--segment-on-bg);
}
.segmented > label > input:focus-visible + span {
  outline: 2px solid var(--focus-ring);
  outline-offset: 1px;
}
.segmented .icon { width: 15px; height: 15px; }

/* View-mode control: the pressed segment follows <html data-view>, which the head script sets
   before first paint (from ?view= or the remembered choice), so the control matches the panes
   even before app.js runs or if it never does. reader.js keeps aria-pressed in step for AT.
   All three rules have the same specificity; order decides (hover is :where()d down). */
html[data-view] [data-viewmode] > button[data-mode] { color: var(--text-nav); background: transparent; }
html[data-view] [data-viewmode] > button[data-mode]:where(:hover) { color: var(--text-title); }
html[data-view="text"] [data-viewmode] > button[data-mode="text"],
html[data-view="split"] [data-viewmode] > button[data-mode="split"],
html[data-view="scan"] [data-viewmode] > button[data-mode="scan"],
html[data-view="split"] .viewbar [data-viewmode] > button[data-mode="text"] {   /* the phone bar has no "split" */
  color: var(--segment-on-fg);
  background: var(--segment-on-bg);
}

/* Full-width variant (settings panel). */
.segmented-block { display: flex; width: 100%; height: max(34px, var(--tap-min)); }
.segmented-block > button { flex: 1; }

/* Touch screens: the segments themselves are --tap-min tall (the 2px padding and the 1px border
   of the track would otherwise take 6px of it, and taps there hit nothing), as in the phone bar. */
@media (pointer: coarse) {
  .segmented,
  .segmented-block { height: calc(var(--tap-min) + 6px); }
}


/* ======================================================================================== */
/* 7 Inputs, fields                                                                         */
/* ======================================================================================== */
.input {
  display: block;
  width: 100%;
  height: var(--control-h);
  padding: 0 14px;
  font-size: var(--fs-ui);
  color: var(--text);
  background: var(--control-bg);
  border: 1px solid var(--control-border);
  border-radius: var(--radius-pill);
  transition: border-color var(--dur-fast) var(--ease);
  appearance: none;
}
.input::placeholder { color: var(--text-muted); opacity: 1; }
.input:hover { border-color: var(--rule); }
.input:focus-visible { outline: 2px solid var(--focus-ring); outline-offset: 1px; border-radius: var(--radius-pill); }
.input-sm { height: 30px; padding: 0 12px; font-size: var(--fs-sm); }
textarea.input { height: auto; min-height: 96px; padding: 10px 14px; line-height: 1.5; border-radius: var(--radius); resize: vertical; }
textarea.input:focus-visible { border-radius: var(--radius); }
select.input {
  padding-right: 34px;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-muted) 50%), linear-gradient(135deg, var(--text-muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%;
  background-size: 5px 5px;
  background-repeat: no-repeat;
}
.input[aria-invalid="true"] { border-color: var(--danger); }

input[type="checkbox"], input[type="radio"] { accent-color: var(--accent); width: 16px; height: 16px; margin: 0; }

.field { display: grid; gap: 6px; }
.field-label { font-size: var(--fs-sm); font-weight: var(--weight-ui-strong); color: var(--text); }
.field-help { font-size: var(--fs-sm); color: var(--text-muted); }
.field-error { font-size: var(--fs-sm); color: var(--danger); }

.check {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-size: var(--fs-ui);
  cursor: pointer;
}

/* Input with a leading icon (search, filter). */
.input-wrap { position: relative; display: block; }
.input-wrap > .icon {
  position: absolute;
  left: 13px;
  top: 50%;
  width: 15px;
  height: 15px;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}
.input-wrap > .input { padding-left: 36px; }
.input-wrap > .input-sm { padding-left: 32px; }
.input-wrap > .icon:has(+ .input-sm) { left: 11px; width: 14px; height: 14px; }

/* ---------------------------------------------------------------------------------------- */
/* 7b Header search: pill input + instant results (js/components/search-box.js)             */
/* ---------------------------------------------------------------------------------------- */
.search {
  position: relative;
  display: flex;
  align-items: center;
  width: 216px;
  height: var(--control-h);
  margin: 0;
}
.search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  width: 15px;
  height: 15px;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}
.search-input {
  width: 100%;
  height: 100%;
  padding: 0 14px 0 37px;
  font-size: var(--fs-search);
  color: var(--text);
  background: var(--control-bg);
  border: 1px solid var(--control-border);
  border-radius: var(--radius-pill);
  appearance: none;
  transition: border-color var(--dur-fast) var(--ease);
}
.search-input::placeholder { color: var(--text-muted); opacity: 1; }
.search-input::-webkit-search-cancel-button { display: none; }
.search-input:hover { border-color: var(--rule); }
.search-input:focus-visible { outline: 2px solid var(--focus-ring); outline-offset: 1px; border-radius: var(--radius-pill); }
.search-close { display: none; }

.search-results {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: var(--z-popover);
  width: min(460px, calc(100vw - 24px));
  max-height: min(70vh, 560px);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 6px;
  background: var(--bg-elevated);
  border: 1px solid var(--control-border);
  border-radius: var(--radius-lg);
}
.sr-group + .sr-group { margin-top: 4px; padding-top: 4px; border-top: 1px solid var(--border); }
.sr-group-label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 8px 10px 4px;
  font-size: var(--fs-2xs);
  font-weight: var(--weight-caps);
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--text-caps);
}
.sr-group-label .count { font-weight: 400; letter-spacing: 0; text-transform: none; font-variant-numeric: tabular-nums; }
.sr-item {
  display: block;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  color: var(--text);
  cursor: pointer;
}
.sr-item:hover { background: var(--hover-bg); }
/* The keyboard's option (what Enter opens): tint plus an accent bar, so it reads at a glance. */
.sr-item[aria-selected="true"] { background: var(--current-bg); box-shadow: inset 2px 0 var(--accent); }
.sr-title {
  display: block;
  font-size: var(--fs-ui-lg);
  line-height: 1.35;
  color: var(--text-title);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sr-snippet {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  margin-top: 2px;
  font-family: var(--font-reading);
  font-size: var(--fs-snippet);
  line-height: 1.4;
  color: var(--text-sidebar);
}
.sr-meta { display: block; margin-top: 3px; font-size: var(--fs-xs); color: var(--text-muted); }
.sr-hint {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 10px;
  font-size: var(--fs-ui);
  color: var(--text-muted);
}
.sr-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 4px;
  padding: 10px;
  border-top: 1px solid var(--border);
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
  font-size: var(--fs-ui);
  color: var(--accent);
  cursor: pointer;
}
.sr-footer[aria-selected="true"], .sr-footer:hover { background: var(--current-bg); }
.sr-footer[aria-selected="true"] { box-shadow: inset 2px 0 var(--accent); }
.sr-footer.is-fallback { margin-top: 0; border-top: 0; color: var(--text-muted); }
.sr-footer.is-fallback:hover, .sr-footer.is-fallback[aria-selected="true"] { color: var(--text-title); }
.sr-footer kbd { color: var(--text-muted); }

/* Tablet: narrower field. */
@media (max-width: 1199.98px) {
  .search { width: 190px; }
}

/* Below 1024px the field opens over the header from the search icon (html.search-open). */
@media (max-width: 1023.98px) {
  .search {
    display: none;
    position: absolute;
    inset: 0;
    z-index: 2;
    width: auto;
    height: auto;
    padding: 0 var(--sp-2) 0 var(--sp-3);
    gap: var(--sp-1);
    background: var(--bg-header);
  }
  html.search-open .search { display: flex; }
  .search-icon { left: 26px; }
  .search-input { height: max(38px, var(--tap-min)); font-size: var(--fs-input-touch); }
  .search-close { display: inline-flex; }
  .search-results {
    position: fixed;
    top: var(--header-h);
    left: 0;
    right: 0;
    width: auto;
    max-height: calc(100dvh - var(--header-h));
    border-radius: 0;
    border-width: 1px 0 0;
  }
}

/* ======================================================================================== */
/* 8 Popover, menu                                                                          */
/* ======================================================================================== */
.popover {
  position: fixed;
  inset: auto;
  z-index: var(--z-popover);
  margin: 0;
  min-width: 200px;
  max-width: calc(100vw - 16px);
  max-height: calc(100dvh - var(--header-h) - 16px);
  overflow-y: auto;
  padding: 6px;
  color: var(--text);
  background: var(--bg-elevated);
  border: 1px solid var(--control-border);
  border-radius: var(--radius-lg);
}
.popover::backdrop { background: transparent; }
/* Phones: popovers opened with sheetOnPhone sit at the bottom like a sheet. */
.popover.is-sheet {
  top: auto;
  right: 0;
  bottom: 0;
  left: 0;
  width: auto;
  max-width: none;
  max-height: 85dvh;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  border-width: 1px 0 0;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
}
.popover.is-sheet::backdrop { background: var(--scrim); }
.popover.is-sheet::before {                 /* grab handle: reads as a sheet; tap outside closes */
  content: "";
  display: block;
  width: 36px;
  height: 4px;
  margin: 2px auto 4px;
  border-radius: 2px;
  background: var(--control-border);
}
.popover-title {
  padding: 8px 10px 6px;
  font-size: var(--fs-2xs);
  font-weight: var(--weight-caps);
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--text-caps);
}

.menu { display: grid; gap: 1px; }
.menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 34px;
  padding: 0 10px;
  font-size: var(--fs-ui);
  text-align: left;
  color: var(--text);
  border-radius: var(--radius-sm);
}
.menu-item .icon { width: var(--icon-sm); height: var(--icon-sm); color: var(--text-muted); }
.menu-item:hover,
.menu-item:focus-visible { background: var(--hover-bg); outline: none; }
.menu-item:focus-visible { box-shadow: inset 0 0 0 2px var(--focus-ring); }
.menu-item[aria-checked="true"] .icon-check { visibility: visible; }
.menu-item .icon-check { margin-left: auto; visibility: hidden; color: var(--accent); }
.menu-sep { height: 1px; margin: 5px 4px; background: var(--border); }
.menu-head { padding: 8px 10px; }
.menu-head strong { display: block; font-size: var(--fs-ui); font-weight: var(--weight-ui-strong); color: var(--text-title); }
.menu-head span { display: block; font-size: var(--fs-sm); color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; }

/* ======================================================================================== */
/* 9 Settings panel (sliders icon)                                                          */
/* ======================================================================================== */
.settings { width: 300px; padding: 4px 14px 8px; }
.settings-group { display: block; padding: 12px 0; border: 0; margin: 0; min-width: 0; }
.settings-group + .settings-group { border-top: 1px solid var(--border); }
.settings-label {
  display: block;
  float: left;            /* keeps a <legend> in normal flow */
  width: 100%;
  margin: 0 0 9px;
  padding: 0;
  font-size: var(--fs-2xs);
  font-weight: var(--weight-caps);
  line-height: 14px;
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--text-caps);
}
.settings-label + * { clear: both; }
/* EB Garamond's x-height is lower than Inter's: a larger size makes "Serif" look as big as "Sans".
   (Specific enough to beat the segmented label rule below.) */
.segmented > label > span.settings-serif { font-family: var(--font-display); font-size: var(--fs-md); }
.segmented > label > span.settings-deva { font-family: var(--font-deva); font-size: var(--fs-ui-lg); }
.stepper-glyph { font-family: var(--font-display); font-size: var(--fs-stepper); line-height: 1; }
.stepper-glyph-sm { font-size: var(--fs-ui-lg); }
.stepper {
  display: flex;
  align-items: center;
  gap: 10px;
}
.stepper output {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.stepper .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  /* Off dots: a ring in the muted text colour, so the scale reads (3:1 against the panel). */
  background: transparent;
  box-shadow: inset 0 0 0 1px var(--text-muted);
}
.stepper .dot.on { background: var(--accent); box-shadow: none; }
.stepper .icon-btn { border: 1px solid var(--control-border); width: max(34px, var(--tap-min)); height: max(34px, var(--tap-min)); }
/* aria-disabled (not disabled) at the ends of the scale, so the focused button keeps focus. */
.stepper .icon-btn[aria-disabled="true"] { opacity: .4; cursor: default; }
.stepper .icon-btn[aria-disabled="true"]:hover { color: var(--text-nav); background: transparent; }
.settings .segmented-block > label > span { padding: 0 8px; }

@media (max-width: 767.98px) {
  .settings { width: auto; }
}

/* Cite popover (js/components/cite.js). */
.cite { display: grid; gap: 2px; width: min(440px, calc(100vw - 32px)); }
.cite-popover { padding: 6px 6px 8px; }
.cite-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 8px 4px 8px 10px;
  border-radius: var(--radius-sm);
}
.cite-row + .cite-row { border-top: 1px solid var(--border); border-radius: 0; }
.cite-text { flex: 1; min-width: 0; }
.cite-name {
  display: block;
  margin-bottom: 3px;
  font-size: var(--fs-2xs);
  font-weight: var(--weight-caps);
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--text-caps);
}
.cite-text p {
  font-family: var(--font-reading);
  font-size: var(--fs-md);
  line-height: 1.45;
  color: var(--text);
  overflow-wrap: anywhere;
  user-select: text;
}
.sheet .cite { width: auto; }

/* ======================================================================================== */
/* 10 Dialog and bottom sheet (native <dialog>, js/components/dialog.js)                    */
/* ======================================================================================== */
.dialog {
  width: min(520px, calc(100vw - 32px));
  max-height: min(80dvh, 720px);
  padding: 0;
  color: var(--text);
  background: var(--bg-elevated);
  border: 1px solid var(--control-border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.dialog[open] { display: flex; flex-direction: column; }
.dialog::backdrop { background: var(--scrim); }
.dialog-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 12px 10px 20px;
}
.dialog-title {
  flex: 1;
  font-family: var(--font-display);
  font-size: var(--fs-dialog-title);
  font-weight: 400;
  line-height: 1.25;
  color: var(--text-title);
}
.dialog-body {
  padding: 4px 20px 16px;
  overflow-y: auto;
  font-size: var(--fs-md);
  line-height: 1.55;
}
.dialog-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px 16px;
  border-top: 1px solid var(--border);
}

/* Bottom sheet: a dialog that slides up from the bottom (phones; also used for settings there). */
.sheet {
  width: 100%;
  max-width: 640px;
  margin: auto auto 0;
  max-height: 85dvh;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  border-bottom: 0;
  padding-bottom: env(safe-area-inset-bottom);
}
.sheet::before {
  content: "";
  display: block;
  width: 36px;
  height: 4px;
  margin: 8px auto 0;
  border-radius: 2px;
  background: var(--control-border);
}
@keyframes vm-sheet-in { from { transform: translateY(100%); } }
.sheet[open] { animation: vm-sheet-in var(--dur) var(--ease); }

/* ======================================================================================== */
/* 11 Toast (js/components/toast.js)                                                         */
/* ======================================================================================== */
.toast-region {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: var(--z-toast);
  display: grid;
  gap: 8px;
  justify-items: center;
  width: max-content;
  max-width: calc(100vw - 32px);
  transform: translateX(-50%);
  pointer-events: none;
}
body.has-viewmode .toast-region { bottom: calc(var(--bottombar-h) + 16px); }
.toast {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 40px;
  padding: 8px 8px 8px 16px;
  font-size: var(--fs-ui);
  color: var(--segment-on-fg);
  background: var(--segment-on-bg);
  border-radius: var(--radius-pill);
  pointer-events: auto;
  animation: vm-toast-in var(--dur) var(--ease);
}
.toast:not(:has(.toast-action, .toast-close)) { padding-right: 16px; }
.toast-action {
  font-weight: var(--weight-ui-strong);
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 3px;
  padding: 4px 6px;
  border-radius: var(--radius-sm);
}
.toast .icon-btn { width: 28px; height: 28px; color: inherit; opacity: .7; }
.toast .icon-btn:hover { background: transparent; opacity: 1; }
.toast.is-leaving { opacity: 0; transform: translateY(6px); transition: opacity var(--dur) var(--ease), transform var(--dur) var(--ease); }
@keyframes vm-toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ======================================================================================== */
/* 12 Progress bar (js/core/http.js)                                                         */
/* ======================================================================================== */
.progress-bar {
  position: fixed;
  top: 0;
  left: 0;
  z-index: var(--z-progress);
  width: 100%;
  height: 2px;
  background: var(--accent);
  transform-origin: 0 50%;
  transform: scaleX(0);
  opacity: 0;
  pointer-events: none;
  transition: transform 300ms var(--ease), opacity 300ms var(--ease) 150ms;
}
.progress-bar.is-active { opacity: 1; transition: transform 600ms var(--ease), opacity 0s; }

/* Inline, determinate (uploads etc.). */
.meter { height: 4px; border-radius: 2px; background: var(--control-border); overflow: hidden; }
.meter > span { display: block; height: 100%; background: var(--accent); border-radius: inherit; transition: width var(--dur) var(--ease); }

/* ======================================================================================== */
/* 13 Pager (js/components/pager.js)                                                         */
/* ======================================================================================== */
.pager {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: var(--fs-ui);
  font-variant-numeric: tabular-nums;
}
.pager a,
.pager button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  height: 34px;
  padding: 0 10px;
  color: var(--text-nav);
  border-radius: var(--radius-pill);
}
.pager a:hover, .pager button:hover { color: var(--text-title); background: var(--hover-bg); }
.pager [aria-current="page"] { color: var(--segment-on-fg); background: var(--segment-on-bg); }
.pager [aria-current="page"]:hover { color: var(--segment-on-fg); background: var(--segment-on-bg); }
.pager [aria-disabled="true"] { opacity: .4; pointer-events: none; }
.pager .pager-gap { min-width: 20px; text-align: center; color: var(--text-muted); }
.pager .pager-step { gap: 4px; }
.pager-summary { margin-top: 8px; text-align: center; font-size: var(--fs-sm); color: var(--text-muted); }

/* ======================================================================================== */
/* 14 Tabs (js/components/tabs.js)                                                           */
/* ======================================================================================== */
.tabs {
  display: flex;
  gap: 20px;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs [role="tab"] {
  position: relative;
  padding: 10px 2px 11px;
  font-size: var(--fs-ui);
  color: var(--text-nav);
  white-space: nowrap;
}
.tabs [role="tab"]:hover { color: var(--text-title); }
.tabs [role="tab"][aria-selected="true"] { color: var(--text-title); }
.tabs [role="tab"][aria-selected="true"]::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: -1px;
  height: 2px;
  background: var(--accent);
  border-radius: 1px;
}
.tabs [role="tab"]:focus-visible { outline-offset: -2px; }
[role="tabpanel"] { padding-top: var(--sp-5); }
[role="tabpanel"]:focus-visible { outline-offset: 4px; }

/* ======================================================================================== */
/* 15 Tooltip and abbreviations (js/components/tooltip.js)                                  */
/* ======================================================================================== */
.tooltip {
  position: fixed;
  inset: auto;               /* shown as popover="manual": undo the UA's centred [popover] box */
  z-index: var(--z-popover);
  margin: 0;
  border: 0;
  overflow: visible;
  max-width: min(320px, calc(100vw - 16px));
  padding: 7px 10px;
  font-family: var(--font-ui);
  font-size: var(--fs-sm);
  font-style: normal;
  font-weight: 400;
  line-height: 1.45;
  letter-spacing: 0;
  text-transform: none;
  color: var(--segment-on-fg);
  background: var(--segment-on-bg);
  border-radius: var(--radius-sm);
  pointer-events: none;
  opacity: 0;
  transition: opacity var(--dur-fast) var(--ease);
}
.tooltip.is-open { opacity: 1; }
@starting-style { .tooltip.is-open { opacity: 0; } }   /* fade in also when shown as a popover */
.tooltip strong { font-weight: 600; }

/* Abbreviations in text (NCC): dotted underline, focusable, full form on hover/focus. */
abbr[title], .abbr {
  text-decoration: underline dotted;
  text-decoration-color: var(--text-muted);
  text-underline-offset: .18em;
  text-decoration-thickness: 1px;
  cursor: help;
}
.abbr:focus-visible { border-radius: 2px; outline-offset: 1px; }

/* ======================================================================================== */
/* 16 Table of contents (sidebar)                                                            */
/* ======================================================================================== */
.toc-head { padding: 26px 18px 6px; }
.toc-title {
  font-family: var(--font-display);
  font-size: var(--fs-sidebar-title);
  font-weight: 400;
  line-height: 1.2;
  color: var(--text-title);
  font-variant-numeric: lining-nums;
  text-wrap: balance;        /* "India: What Can / It Teach Us?", never a one-word last line */
}
.toc-meta {
  margin-top: 5px;
  font-size: var(--fs-sm);
  letter-spacing: 0.02em;
  line-height: 1.4;
  color: var(--text-muted);
  text-wrap: balance;        /* wraps between its parts (.meta-part), in even lines */
}
.toc-nav { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.toc-nav .pill { height: 30px; color: var(--text-sidebar); background: var(--control-bg); }
.toc-nav .pill:hover { color: var(--text-title); }

.toc-filter { padding: 10px 16px 4px; }

.toc { padding: 11px 8px 28px; }
.toc-list, .toc-list ol, .toc-list ul { margin: 0; padding: 0; list-style: none; }

.toc-heading,
.toc-item,
.toc-group {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  width: 100%;
  padding: 4.5px 10px 4.5px 19px;
  text-align: left;
  border-radius: var(--radius-sm);
}
.toc-item, .toc-group {
  font-size: var(--fs-ui);
  line-height: 19px;
  color: var(--text-sidebar);
}
.toc-group { color: var(--text-nav); }   /* neutral, like the date rows in the reference */
.toc-heading {
  margin-top: 15px;
  padding-top: 6px;
  padding-bottom: 4px;
  font-size: var(--fs-2xs);
  font-weight: var(--weight-caps);
  line-height: 15px;
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--text-caps);
}
.toc-list > li:first-child > .toc-heading { margin-top: 0; }
.toc-group + .toc-heading,
li:has(> .toc-group) + li > .toc-heading { margin-top: 12px; }
.toc-label { flex: 1; min-width: 0; overflow-wrap: anywhere; }
/* A Devanagari folio in a Latin label ("Page १"): the Devanagari face, a size that matches the digits. */
.toc-folio:lang(sa), .toc-folio:lang(hi), .toc-folio:lang(mr), .toc-folio:lang(ne) {
  font-family: var(--font-deva);
  font-size: 1.12em;
  line-height: 0;
  font-synthesis: none;
}
/* Touch screens: rows tall enough to tap (the list gets longer, not denser). */
@media (pointer: coarse) {
  .toc-item, .toc-group, a.toc-heading { min-height: var(--tap-min); align-items: center; }
}

a.toc-item:hover,
a.toc-group:hover,
a.toc-heading:hover { color: var(--text-title); background: var(--hover-bg); }

.toc-item[aria-current],
.toc-group[aria-current],
.toc-heading[aria-current],
.toc .is-current {
  color: var(--text-title);
  background: var(--current-bg);
}
.toc-item[aria-current]::before,
.toc .toc-item.is-current::before { content: none; }

/* Right-aligned marker icon (listenable, has scan, etc.). */
.toc-icon {
  flex: none;
  width: 13px;
  height: 13px;
  margin-top: 3px;
  margin-right: -2px;
  color: var(--text-caps);
}
.toc-heading .toc-icon { margin-top: 0; }

/* Nested levels: the first level of items aligns with its heading (as in the reference);
   deeper levels indent. */
.toc-list .toc-list .toc-list .toc-item { padding-left: 31px; }
.toc-list .toc-list .toc-list .toc-list .toc-item { padding-left: 43px; }

.toc-empty { padding: 10px 10px; font-size: var(--fs-ui); color: var(--text-muted); }
.toc [data-filtered-out] { display: none; }

/* ======================================================================================== */
/* 17 States: empty, loading, error                                                          */
/* ======================================================================================== */
.state {
  display: grid;
  justify-items: center;
  gap: 10px;
  max-width: 440px;
  margin: 0 auto;
  padding: 56px 24px;
  text-align: center;
}
.state-icon {
  width: 28px;
  height: 28px;
  color: var(--text-caps);
  margin-bottom: 4px;
}
.state-title {
  font-family: var(--font-display);
  font-size: var(--fs-h3);
  font-weight: 400;
  line-height: 1.25;
  color: var(--text-title);
}
.state-body {
  font-size: var(--fs-md);
  line-height: 1.55;
  color: var(--text-muted);
}
.state-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 8px; }
.state-error .state-icon { color: var(--danger); }
.state-compact { padding: 24px 16px; }
.state-compact .state-title { font-size: var(--fs-stepper); }

/* Skeleton lines for loading text (no layout shift: reserve the final height). */
.skeleton {
  display: block;
  height: .9em;
  margin: .45em 0;
  border-radius: 4px;
  background: var(--hover-bg);
  background-image: linear-gradient(90deg, transparent, var(--hover-bg), transparent);
  background-size: 200% 100%;
  animation: vm-shimmer 1.4s ease-in-out infinite;
}
@keyframes vm-shimmer { from { background-position: 150% 0; } to { background-position: -50% 0; } }

/* ======================================================================================== */
/* 18 Site footer (non-reader pages)                                                         */
/* ======================================================================================== */
.site-footer {
  max-width: calc(880px + 2 * var(--pane-pad-x));   /* the width of .page */
  margin: 0 auto;
  padding: 48px var(--pane-pad-x) 40px;
  font-size: var(--fs-sm);
  color: var(--text-muted);
}
.site-footer-inner {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 12px 24px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}
.site-footer nav { display: flex; flex-wrap: wrap; gap: 4px 18px; }
.site-footer a { color: var(--text-nav); }
@media (pointer: coarse) {
  .site-footer nav { gap: 0 18px; }
  .site-footer a { display: inline-flex; align-items: center; min-height: var(--tap-min); }
}
.site-footer a:hover { color: var(--text-title); }

/* ======================================================================================== */
/* 19 Forced colours (Windows High Contrast): selection and "current" need their own cues   */
/* ======================================================================================== */
@media (forced-colors: active) {
  .segmented > button[aria-pressed="true"],
  .segmented > button[aria-checked="true"],
  .segmented > label > input:checked + span,
  html[data-view="text"] [data-viewmode] > button[data-mode="text"],
  html[data-view="split"] [data-viewmode] > button[data-mode="split"],
  html[data-view="scan"] [data-viewmode] > button[data-mode="scan"],
  html[data-view="split"] .viewbar [data-viewmode] > button[data-mode="text"],
  .pager [aria-current="page"] {
    forced-color-adjust: none;
    color: HighlightText;
    background: Highlight;
  }
  .stepper .dot { forced-color-adjust: none; background: Canvas; box-shadow: inset 0 0 0 1px CanvasText; }
  .stepper .dot.on { background: Highlight; box-shadow: none; }
  .toc [aria-current], .toc .is-current { outline: 2px solid Highlight; outline-offset: -2px; }
  .site-nav a[aria-current], .drawer-nav a[aria-current] { text-decoration: underline 2px; text-underline-offset: 4px; }
  .tabs [role="tab"][aria-selected="true"]::after { background: Highlight; }
  .sr-item[aria-selected="true"], .sr-footer[aria-selected="true"] { outline: 2px solid Highlight; outline-offset: -2px; }
  .pill[aria-pressed="true"], .icon-btn[aria-pressed="true"] { outline: 2px solid Highlight; outline-offset: 1px; }
}
