/* Colors: every text/background pair meets WCAG AA (4.5:1) or better in both themes. */
:root {
  color-scheme: light;
  --bg: #fbfbf9;
  --surface: #ffffff;
  --surface-2: #f0f1ee;
  --text: #1b1c1f;
  --muted: #4a4d55;
  --border: #767983;
  --link: #0a55a8;
  --link-visited: #6b2fa0;
  --accent: #0a55a8;
  --accent-text: #ffffff;
  --focus: #b3440b;
  --good: #1d6b35;
  --warn: #8a4b00;
  --error: #b3261e;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #131417;
    --surface: #1c1e22;
    --surface-2: #26292e;
    --text: #eceef2;
    --muted: #b9bdc7;
    --border: #8a8f9b;
    --link: #8cc4ff;
    --link-visited: #d3a8ff;
    --accent: #8cc4ff;
    --accent-text: #0b1a2b;
    --focus: #ffb86b;
    --good: #7fd69a;
    --warn: #ffc56b;
    --error: #ff9b91;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #131417;
  --surface: #1c1e22;
  --surface-2: #26292e;
  --text: #eceef2;
  --muted: #b9bdc7;
  --border: #8a8f9b;
  --link: #8cc4ff;
  --link-visited: #d3a8ff;
  --accent: #8cc4ff;
  --accent-text: #0b1a2b;
  --focus: #ffb86b;
  --good: #7fd69a;
  --warn: #ffc56b;
  --error: #ff9b91;
}

*, *::before, *::after { box-sizing: border-box; }

html { font-size: 100%; -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 1.0625rem;
  line-height: 1.55;
}

.wrap { max-width: 72rem; margin-inline: auto; padding-inline: 1rem; }

a { color: var(--link); text-underline-offset: 0.15em; }
a:visited { color: var(--link-visited); }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}
h1:focus, main:focus { outline: none; }
h1:focus-visible { outline: 3px solid var(--focus); }

.skip-link {
  position: absolute;
  left: 1rem;
  top: -4rem;
  padding: 0.75rem 1rem;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  z-index: 10;
}
.skip-link:focus { top: 0.5rem; }

.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

/* Header */
.site-header { background: var(--surface); border-bottom: 1px solid var(--border); }
.header-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.5rem 1rem; padding-block: 0.75rem; }
.site-name { margin: 0; font-size: 1.25rem; font-weight: 700; }
.header-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem 1rem; }
.theme-control { display: flex; align-items: center; gap: 0.5rem; }
.narrow { max-width: 40rem; }
button[aria-disabled="true"] { opacity: 0.6; cursor: progress; }
.nav-list { list-style: none; display: flex; flex-wrap: wrap; gap: 0.25rem; margin: 0; padding: 0 0 0.5rem; }
.nav-list a {
  display: inline-block;
  min-height: 44px;
  padding: 0.6rem 1rem;
  border-radius: 0.4rem;
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
}
.nav-list a:hover { background: var(--surface-2); text-decoration: underline; }
.nav-list a[aria-current="page"] {
  background: var(--accent);
  color: var(--accent-text);
}

main { padding-block: 1.5rem 3rem; }
h1 { font-size: 1.75rem; line-height: 1.25; margin: 0 0 0.75rem; }
h2 { font-size: 1.35rem; margin: 1.75rem 0 0.5rem; }
h3 { font-size: 1.15rem; margin: 0 0 0.35rem; }

.notice { padding: 0.75rem 1rem; border: 1px solid var(--border); border-radius: 0.4rem; background: var(--surface); }
.notice.error, .error { color: var(--error); }

/* Forms */
.filters {
  display: flex; flex-wrap: wrap; align-items: flex-end; gap: 0.75rem 1rem;
  padding: 1rem; margin-block: 1rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: 0.5rem;
}
.field { display: flex; flex-direction: column; gap: 0.25rem; min-width: 10rem; }
.field.grow { flex: 1 1 16rem; }
.field.actions { min-width: auto; }
.field.icon-actions { flex-direction: row; align-items: flex-end; gap: 0.5rem; }
.field.check { flex-direction: row; align-items: center; gap: 0.5rem; min-width: auto; min-height: 44px; }
.field.check input { width: 1.4rem; height: 1.4rem; min-height: 0; margin: 0; accent-color: var(--accent); }
label { font-weight: 600; }

input, select, button {
  font: inherit;
  min-height: 44px;
  border-radius: 0.4rem;
}
input, select {
  padding: 0.45rem 0.6rem;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
}
button {
  padding: 0.5rem 1.1rem;
  border: 2px solid var(--accent);
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  cursor: pointer;
}
button.secondary { background: transparent; color: var(--link); }
button:hover { text-decoration: underline; }
button.more { margin-top: 1rem; }

.status { min-height: 1.5em; font-weight: 600; }

/* Books table */
.table-scroll { overflow-x: auto; border: 1px solid var(--border); border-radius: 0.5rem; background: var(--surface); }
table { width: 100%; border-collapse: collapse; }
caption { text-align: left; padding: 0.75rem 1rem; font-weight: 700; }
th, td { text-align: left; vertical-align: top; padding: 0.6rem 0.75rem; border-top: 1px solid var(--border); }
thead th { background: var(--surface-2); border-top: 0; }
tbody th { font-weight: 600; }
td ul, .plain { list-style: none; margin: 0; padding: 0; }
td li + li { margin-top: 0.25rem; }
.muted { color: var(--muted); }
.tag { display: inline; }

/* Series and search cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  padding: 1rem;
  margin-block: 0.75rem;
}
.card p { margin: 0.25rem 0; }
.card ol, .card ul { margin: 0.4rem 0 0; padding-left: 1.4rem; }
.card li + li { margin-top: 0.2rem; }
.card h4 { font-size: 1rem; margin: 0.75rem 0 0.2rem; }
.good { color: var(--good); font-weight: 600; }
.warn { color: var(--warn); font-weight: 600; }

.site-footer { padding-block: 1rem 2rem; color: var(--muted); border-top: 1px solid var(--border); }

/* Small screens: full-width fields. The table keeps real table layout (so screen readers
   still announce rows and columns) and scrolls sideways inside its own box. */
@media (max-width: 40rem) {
  .field, .field.grow { min-width: 100%; }
  table { min-width: 44rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; scroll-behavior: auto !important; }
}

@media (forced-colors: active) {
  .nav-list a[aria-current="page"] { border: 2px solid CanvasText; }
  button { border: 2px solid ButtonText; }
}

.browse-source { margin-top: 2rem; padding-top: 0.5rem; border-top: 2px solid var(--border); }
.book-about { color: var(--muted); margin: 0.35rem 0 0; }
button.small { min-height: 44px; padding: 0.3rem 0.8rem; font-size: 0.95rem; }

.results > li.result { padding: 0.75rem 0; border-top: 1px solid var(--border); }
.results > li.result:first-child { border-top: 0; }
.results > li.result { padding: 0.25rem 0; }
.result-more { padding-bottom: 0.4rem; }
.results ul { margin: 0; padding-left: 1.4rem; }
.show-field { max-width: 22rem; margin: 0.5rem 0 0.75rem; }
.search-controls { margin: 0.25rem 0 0.75rem; }

.app-actions { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 0.4rem 0.75rem; }
.app-link { display: inline-flex; align-items: center; min-height: 44px; font-weight: 600; }
details.help { margin: 0.75rem 0; padding: 0.5rem 1rem; border: 1px solid var(--border); border-radius: 0.5rem; background: var(--surface); }
details.help summary { min-height: 44px; display: flex; align-items: center; font-weight: 600; cursor: pointer; }

.series-genre { margin-top: 1.5rem; }
.series-genre > h2 { border-bottom: 2px solid var(--border); padding-bottom: 0.25rem; }
.series-books { list-style: none; padding-left: 0; }
.series-book { padding: 0 0 0.25rem; border-top: 1px solid var(--border); }
.series-book:first-child { border-top: 0; }
.series-row { display: flex; align-items: center; gap: 0.5rem; }
button.series-open {
  flex: 0 1 auto; text-align: left; padding: 0.6rem 0; min-height: 44px;
  background: transparent; border: 0; color: var(--text); font-weight: 400;
}
/* The arrow is for sighted readers only; '' after the slash keeps VoiceOver from reading it. */
button.series-open::before { content: '▸' / ''; display: inline-block; width: 1.2em; }
button.series-open[aria-expanded="true"]::before { content: '▾' / ''; }
.series-more { padding-bottom: 0.25rem; }
.series-book .series-status { font-weight: 600; }
.series-book.read .series-status { color: var(--good); }
.series-book ul { margin: 0.25rem 0 0; padding-left: 1.4rem; }

/* Text field with a round clear (x) button inside its right end, shown while there's text.
   The button is a 40px tap target around a smaller circle. The browser's own small clear
   button is hidden so there's only one. */
.with-clear { position: relative; }
.with-clear input { width: 100%; padding-right: 3rem; }
.with-clear .clear-text {
  position: absolute; top: 50%; right: 0.2rem; transform: translateY(-50%);
  width: 2.5rem; height: 2.5rem; min-height: 0; padding: 0;
  display: flex; align-items: center; justify-content: center;
  background: transparent; border: 0; border-radius: 50%;
}
.with-clear .clear-text[hidden] { display: none; }
.with-clear .clear-x {
  display: flex; align-items: center; justify-content: center;
  width: 1.6rem; height: 1.6rem; border-radius: 50%;
  background: var(--muted); color: var(--bg);
  font-size: 1.2rem; font-weight: 700; line-height: 1;
}
.with-clear .clear-text:hover { text-decoration: none; }
.with-clear .clear-text:hover .clear-x { background: var(--text); }
@media (forced-colors: active) {
  .with-clear .clear-text { border: 0; }
  .with-clear .clear-x { border: 2px solid ButtonText; background: ButtonFace; color: ButtonText; }
}
input[type="search"]::-webkit-search-cancel-button { -webkit-appearance: none; appearance: none; display: none; }

/* Recent searches, shown under the Search box while you're in it. */
.recent { margin-top: 0.5rem; }
.recent p { margin: 0 0 0.25rem; }
.recent ul { list-style: none; margin: 0 0 0.5rem; padding: 0; display: flex; flex-direction: column; gap: 0.25rem; }
.recent ul button { text-align: left; width: 100%; }

/* "Sign in another browser" one-time code, in the footer. */
.device-code { margin-bottom: 1.5rem; }
.device-code h2 { font-size: 1.15rem; margin: 0; }
.device-code code { font-size: 1.5rem; letter-spacing: 0.15em; font-weight: 700; }

/* Round icon buttons (Description, Read aloud, Mark as read, copy, open app, theme, sign out):
   a 40px circle with a small one- or two-word caption under it. The whole thing is the button. */
.icon-group { display: inline-flex; gap: 0.35rem; vertical-align: middle; align-items: flex-start; }
button.icon-button, a.icon-button {
  min-height: 0; padding: 0; min-width: 2.75rem;
  display: inline-flex; flex-direction: column; align-items: center; gap: 0.1rem; vertical-align: middle;
  border: 0; background: transparent; color: var(--link); font-weight: 400; text-decoration: none;
}
.icon-circle {
  width: 2.5rem; height: 2.5rem; display: inline-flex; align-items: center; justify-content: center;
  border-radius: 50%; border: 1px solid var(--border);
}
.icon-caption { font-size: 0.7rem; line-height: 1.1; color: var(--muted); white-space: nowrap; }
button.icon-button:hover, a.icon-button:hover { text-decoration: none; }
button.icon-button:hover .icon-circle, a.icon-button:hover .icon-circle { background: var(--surface-2); }
button.icon-button[aria-expanded="true"] .icon-circle { background: var(--surface-2); border-color: var(--link); }
button.icon-button.mark.on { color: var(--good); }
button.icon-button.mark.on .icon-circle { border-color: var(--good); }
button.icon-button .icon-cut { stroke: var(--surface); }
.read-toggle { display: inline-flex; vertical-align: middle; }
@media (forced-colors: active) {
  button.icon-button, a.icon-button { border: 0; color: ButtonText; }
  .icon-circle { border: 2px solid ButtonText; }
  .icon-caption { color: ButtonText; }
  button.icon-button .icon-cut { stroke: ButtonFace; }
}

/* Small help box (Search > Help). */
dialog.help-box {
  max-width: min(28rem, calc(100vw - 2rem)); padding: 1rem 1.25rem;
  color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 0.6rem;
}
dialog.help-box::backdrop { background: rgb(0 0 0 / 0.5); }
dialog.help-box h2 { margin: 0 0 0.5rem; font-size: 1.15rem; }
dialog.help-box p { margin: 0 0 1rem; }
.stats { margin: 0 0 1rem; }
.stats-result { margin: 0.5rem 0 0; font-weight: 600; }
.stats-result:empty, #books-summary:empty { display: none; }
dialog.help-box .field { margin-bottom: 0.75rem; }
dialog.help-box .status:empty { display: none; }
.dialog-buttons { display: flex; gap: 0.75rem; }
.select-row { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
/* A page title with a small link on the right of the same line (Search > Help). */
.heading-row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.help-link { font-size: 0.9rem; font-weight: 600; white-space: nowrap; display: inline-flex; align-items: center; min-height: 44px; }

/* Header: color theme and sign out as round icons, on the right of the title. */
.header-controls { flex-wrap: nowrap; gap: 0.5rem; }
.theme-menu-wrap { position: relative; }
.theme-menu {
  position: absolute; right: 0; top: calc(100% + 0.4rem); z-index: 20; min-width: 13rem;
  padding: 0.5rem; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 0.6rem; box-shadow: 0 6px 20px rgb(0 0 0 / 0.35);
}
.theme-menu-title { margin: 0.25rem 0.5rem 0.4rem; font-weight: 700; }
.theme-menu ul { list-style: none; margin: 0; padding: 0; }
.theme-menu button {
  width: 100%; text-align: left; background: transparent; color: var(--text);
  border: 0; border-radius: 0.4rem; font-weight: 400; padding: 0.5rem 0.75rem;
}
.theme-menu button:hover { background: var(--surface-2); text-decoration: none; }
/* The check mark is for sighted readers; VoiceOver hears "selected" from aria-pressed. */
.theme-menu button[aria-pressed="true"]::after { content: '✓' / ''; float: right; font-weight: 700; color: var(--good); }
/* The sign-out message gets its own line, so the icons stay at the right edge. */
.header-row > .sign-out-status { flex: 1 0 100%; min-height: 0; margin: 0; }
/* Search > Show: the label and its list on one line. */
.field.show-field { flex-direction: row; align-items: center; gap: 0.75rem; }
.field.show-field select { flex: 1 1 auto; max-width: 20rem; }
/* Anything marked hidden stays hidden, even when a layout rule sets how it is displayed. */
[hidden] { display: none !important; }

/* Browse: each source opens from its heading. */
h2 button.section-open {
  font: inherit; color: var(--text); background: transparent; border: 0; padding: 0.4rem 0;
  text-align: left; min-height: 44px; width: 100%;
}
h2 button.section-open::before { content: '▸' / ''; display: inline-block; width: 1.1em; }
h2 button.section-open[aria-expanded="true"]::before { content: '▾' / ''; }
h2 button.section-open:hover { text-decoration: underline; }
/* A label and its list on one line. */
.field.inline { flex-direction: row; align-items: center; gap: 0.75rem; }
.field.inline label { white-space: nowrap; }
.field.inline select { flex: 1 1 auto; min-width: 0; }
/* A check box with its short label underneath. */
.field.check.stacked { flex-direction: column; align-items: center; gap: 0.15rem; min-width: 3.5rem; }
.field.check.stacked label { font-size: 0.75rem; font-weight: 400; color: var(--muted); text-align: center; }

/* Two short label-and-list pairs side by side on one line (Browse: Show and Sort by). */
.field-pair { display: flex; gap: 0.75rem; align-items: center; flex: 1 1 100%; min-width: 0; }
.field-pair .field { flex: 1 1 0; min-width: 0; }
/* Browse notes only appear when there is an error to show. */
#bard-browse-note:empty, #hoopla-browse-note:empty { display: none; }

/* Series: Clear, Show all and Per page on one line. */
.field.icon-actions .field.inline { min-width: 0; align-self: center; margin-left: 0.5rem; }
.field.icon-actions { flex-wrap: wrap; max-width: 100%; }
.field.icon-actions .field.inline { flex: 1 1 9rem; }
.field.icon-actions .field.inline select { min-width: 0; width: 100%; }
