/* Accessible reader styles. All sizes are relative so browser zoom and the text-size setting both work. */

:root {
  --scale: 100%;
  --weight: 400;
  --line-height: 1.8;
  --para-space: 1.5em;
  --letter-spacing: normal;
  --word-spacing: normal;
  --measure: 60ch;
  --font-body: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  /* Light theme (default). All text pairs meet WCAG AA; body text meets AAA. */
  --bg: #fdfdfb;
  --fg: #1b1b1b;
  --muted: #474747;
  --link: #0a4a91;
  --link-visited: #5a2b86;
  --focus: #b33c00;
  --border: #8a8a8a;
  --panel: #f0f0ec;
  --mark-bg: #ffe066;
  --mark-fg: #000;
  --speaking-bg: #fff1a8;
  --speaking-fg: #000;
  --button-bg: #1b1b1b;
  --button-fg: #fff;
  --error: #a50e0e;

  color-scheme: light;
}

[data-theme="dark"] {
  --bg: #121212; --fg: #ececec; --muted: #c4c4c4; --link: #9cc3ff; --link-visited: #d1b3ff;
  --focus: #ffb366; --border: #8f8f8f; --panel: #1f1f1f; --mark-bg: #ffe066; --mark-fg: #000;
  --speaking-bg: #3d3600; --speaking-fg: #fff; --button-bg: #ececec; --button-fg: #121212; --error: #ff9b9b;
  color-scheme: dark;
}
[data-theme="sepia"] {
  --bg: #f4ecd8; --fg: #2b2117; --muted: #4f4030; --link: #7a3500; --link-visited: #5c2d6e;
  --focus: #0050a0; --border: #8a7a62; --panel: #ebe0c6; --mark-bg: #ffd24d; --mark-fg: #000;
  --speaking-bg: #ffe9a3; --speaking-fg: #000; --button-bg: #2b2117; --button-fg: #f4ecd8; --error: #9a0f0f;
  color-scheme: light;
}
[data-theme="hc-dark"] {
  --bg: #000; --fg: #fff; --muted: #fff; --link: #ffff00; --link-visited: #ffff00;
  --focus: #00ffff; --border: #fff; --panel: #000; --mark-bg: #ffff00; --mark-fg: #000;
  --speaking-bg: #ffff00; --speaking-fg: #000; --button-bg: #ffff00; --button-fg: #000; --error: #ff8080;
  color-scheme: dark;
}
[data-theme="hc-light"] {
  --bg: #fff; --fg: #000; --muted: #000; --link: #0000c8; --link-visited: #4b0082;
  --focus: #000; --border: #000; --panel: #fff; --mark-bg: #ffff00; --mark-fg: #000;
  --speaking-bg: #ffff00; --speaking-fg: #000; --button-bg: #000; --button-fg: #fff; --error: #a00000;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  [data-theme="system"] {
    --bg: #121212; --fg: #ececec; --muted: #c4c4c4; --link: #9cc3ff; --link-visited: #d1b3ff;
    --focus: #ffb366; --border: #8f8f8f; --panel: #1f1f1f; --speaking-bg: #3d3600; --speaking-fg: #fff;
    --button-bg: #ececec; --button-fg: #121212; --error: #ff9b9b;
    color-scheme: dark;
  }
}
@media (prefers-contrast: more) {
  [data-theme="system"] { --muted: var(--fg); --border: var(--fg); }
}

[data-font="serif"] { --font-body: Georgia, "Iowan Old Style", "Times New Roman", serif; }
[data-font="verdana"] { --font-body: Verdana, Geneva, "DejaVu Sans", sans-serif; }
[data-font="hyperlegible"] { --font-body: "Atkinson Hyperlegible Next", "Atkinson Hyperlegible", Verdana, sans-serif; }
[data-font="dyslexic"] { --font-body: "OpenDyslexic", "OpenDyslexic3", "Comic Sans MS", Verdana, sans-serif; }
[data-font="mono"] { --font-body: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }

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

html {
  font-size: var(--scale);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-body);
  font-weight: var(--weight);
  font-size: 1.125rem;
  line-height: var(--line-height);
  letter-spacing: var(--letter-spacing);
  word-spacing: var(--word-spacing);
  overflow-wrap: break-word;
}

main, .site-header nav, .site-footer, .settings-panel, .article-nav {
  max-width: min(100% - 2rem, 75rem);
  margin-inline: auto;
}

main { padding-block: 1.5rem 3rem; }
main:focus { outline: none; }

h1, h2, h3 { line-height: 1.25; font-weight: 700; letter-spacing: var(--letter-spacing); }
h1 { font-size: 2rem; margin: 0 0 0.75rem; }
h2 { font-size: 1.4rem; margin: 2rem 0 0.75rem; }

a { color: var(--link); text-decoration-thickness: 0.08em; text-underline-offset: 0.18em; }
a:visited { color: var(--link-visited); }
a:hover { text-decoration-thickness: 0.15em; }

:focus-visible {
  outline: 0.2rem solid var(--focus);
  outline-offset: 0.2rem;
  border-radius: 0.1rem;
}

mark { background: var(--mark-bg); color: var(--mark-fg); padding: 0 0.1em; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%);
  white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute;
  left: 1rem; top: -100rem;
  background: var(--button-bg); color: var(--button-fg);
  padding: 0.75rem 1rem; z-index: 10; font-weight: 700;
}
.skip-link:focus { top: 1rem; }

/* ── Header / nav ─────────────────────────────────────────────────── */
.site-header { border-bottom: 1px solid var(--border); }
.site-header ul {
  list-style: none; margin: 0; padding: 0.5rem 0;
  display: flex; flex-wrap: wrap; gap: 0.25rem 1.5rem; align-items: center;
}
.site-header a, .settings-toggle {
  display: inline-flex; align-items: center; min-height: 2.75rem; padding: 0 0.25rem;
  font-size: 1rem; font-weight: 600;
}
.site-header a[aria-current="page"] { text-decoration-thickness: 0.2em; }
.site-footer { border-top: 1px solid var(--border); padding-block: 1rem 2rem; color: var(--muted); font-size: 0.95rem; }

/* Sign-out: a real form button styled like the other nav links. */
.inline-form { display: inline; margin: 0; }
button.link-button {
  background: transparent; color: var(--link); border-color: transparent;
  text-decoration: underline; text-decoration-thickness: 0.08em; text-underline-offset: 0.18em;
  font-size: 1rem; font-weight: 600; padding: 0 0.25rem;
}
.login-form { max-width: 28rem; }

/* ── Controls ─────────────────────────────────────────────────────── */
button, input, select, textarea { font: inherit; letter-spacing: inherit; word-spacing: inherit; }

button {
  min-height: 2.75rem; min-width: 2.75rem;
  padding: 0.4rem 1.1rem;
  background: var(--button-bg); color: var(--button-fg);
  border: 0.125rem solid var(--button-bg); border-radius: 0.375rem;
  font-weight: 700; cursor: pointer; line-height: 1.3;
}
button:disabled { opacity: 0.55; cursor: not-allowed; }
button.settings-toggle {
  background: transparent; color: var(--link); border-color: transparent;
  text-decoration: underline; text-decoration-thickness: 0.08em; text-underline-offset: 0.18em;
}
button.settings-toggle[aria-expanded="true"] { text-decoration-thickness: 0.2em; }

input[type="text"], input[type="url"], input[type="search"], select, textarea {
  width: 100%;
  min-height: 2.75rem;
  padding: 0.4rem 0.6rem;
  color: var(--fg); background: var(--bg);
  border: 0.125rem solid var(--border); border-radius: 0.25rem;
  line-height: 1.5;
}
textarea { line-height: var(--line-height); }
input[aria-invalid="true"], textarea[aria-invalid="true"] { border-color: var(--error); border-width: 0.2rem; }

.field { margin-bottom: 1.25rem; }
.field label { display: block; font-weight: 700; margin-bottom: 0.25rem; }
.hint { color: var(--muted); font-size: 0.95rem; margin: 0 0 0.35rem; }
.req { font-weight: 400; color: var(--muted); }
.field.checkbox { display: grid; grid-template-columns: auto 1fr; gap: 0 0.75rem; align-items: start; }
.field.checkbox input { width: 1.5rem; height: 1.5rem; margin-top: 0.25rem; accent-color: var(--fg); }
.field.checkbox .hint { grid-column: 2; }
.error-text { color: var(--error); font-weight: 700; margin: 0 0 0.35rem; }

.error-summary {
  border: 0.25rem solid var(--error); padding: 1rem 1.25rem; margin-bottom: 1.5rem;
}
.error-summary h2 { margin-top: 0; color: var(--error); }

/* ── Settings panel ───────────────────────────────────────────────── */
.settings-panel {
  background: var(--panel);
  border: 0.125rem solid var(--border);
  border-radius: 0.5rem;
  padding: 1rem 1.25rem 0.25rem;
  margin-block: 1rem;
}
.settings-panel h2 { margin-top: 0; }
.settings-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 16rem), 1fr)); gap: 0 1.5rem; }
.field-actions { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: end; }

/* ── Library & search ─────────────────────────────────────────────── */
.search-form { margin-block: 1rem 1.5rem; max-width: 45rem; }
.search-form label { display: block; font-weight: 700; }
.search-row { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.search-row input { flex: 1 1 14rem; width: auto; }
.count { color: var(--muted); }

.article-list { list-style: none; padding: 0; margin: 0; max-width: 50rem; }
.article-item { border-top: 1px solid var(--border); padding-block: 1rem; }
.item-title { font-size: 1.3rem; margin: 0 0 0.35rem; }
.item-dek { margin: 0 0 0.35rem; }
.item-meta, .item-snippet { margin: 0; color: var(--muted); font-size: 1rem; }
.item-snippet { margin-top: 0.4rem; color: var(--fg); }
.badge {
  display: inline-block; margin-left: 0.5rem; padding: 0 0.45rem;
  border: 0.1rem solid currentColor; border-radius: 0.25rem;
  font-size: 0.8rem; font-weight: 700; vertical-align: middle; color: var(--fg);
}
.action-links { padding-left: 1.25rem; }
.action-links li { margin-block: 0.5rem; }

/* ── Reader ───────────────────────────────────────────────────────── */
.reader { max-width: var(--measure); margin-inline: auto; }
.reader .article-header { margin-bottom: 1.5rem; }
.reader .section { text-transform: uppercase; letter-spacing: 0.08em; font-size: 0.85rem; font-weight: 700; color: var(--muted); margin: 0 0 0.5rem; }
.reader h1 { font-size: 2.1rem; }
.reader .dek { font-size: 1.3rem; line-height: 1.5; margin: 0 0 1rem; color: var(--muted); }
.reader .byline { font-weight: 700; margin: 0; }
.reader .meta { color: var(--muted); margin: 0.25rem 0 0; font-size: 1rem; }

.article-body { font-size: 1.25rem; }
.article-body p, .article-body ul { margin: 0 0 var(--para-space); }
.article-body h2 { font-size: 1.45rem; margin-top: calc(var(--para-space) * 1.5); }
.article-body li { margin-bottom: calc(var(--para-space) / 2); }

[data-speak].speaking {
  background: var(--speaking-bg); color: var(--speaking-fg);
  box-shadow: 0 0 0 0.4rem var(--speaking-bg);
  border-radius: 0.15rem;
}

.listen {
  border: 0.125rem solid var(--border); border-radius: 0.5rem;
  padding: 0.75rem 1rem; margin-bottom: 2rem; background: var(--panel);
}
.listen-controls { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-bottom: 0.75rem; }
.listen-options { display: flex; flex-wrap: wrap; gap: 0 1rem; }
.listen-options .field { flex: 1 1 12rem; margin-bottom: 0.5rem; }
.tts-progress { margin: 0; color: var(--muted); font-size: 1rem; min-height: 1.5em; }

.article-footer { border-top: 1px solid var(--border); margin-top: 2.5rem; padding-top: 0.5rem; font-size: 1rem; color: var(--muted); }
.footer-heading { font-size: 1.1rem; }
.plain-list { padding-left: 1.25rem; }
.article-nav { max-width: var(--measure); padding-bottom: 2rem; }

/* ── Preferences of the operating system ──────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
}

@media (forced-colors: active) {
  button { border-color: ButtonText; }
  [data-speak].speaking { forced-color-adjust: none; background: Highlight; color: HighlightText; box-shadow: none; outline: 0.2rem solid Highlight; }
  :focus-visible { outline-color: Highlight; }
  mark { forced-color-adjust: none; background: Mark; color: MarkText; }
}

@media print {
  .site-header, .site-footer, .settings-panel, .listen, .article-nav, .skip-link { display: none !important; }
  body { background: #fff; color: #000; }
}

@media (max-width: 40rem) {
  h1, .reader h1 { font-size: 1.7rem; }
  .article-body { font-size: 1.15rem; }
}
