:root {
  color-scheme: light dark;

  --bg-light: #faf8f4;
  --surface-light: #f1eee7;
  --border-light: #dcd6ca;
  --text-light: #1c1b18;
  --muted-light: #5c574d;
  --accent-light: #b45309;
  --accent-text-light: #ffffff;
  --sent-bg-light: #fef3c7;
  --sent-text-light: #1c1b18;
  --word-bg-light: #f59e0b;
  --word-text-light: #111111;

  --bg-dark: #141413;
  --surface-dark: #1f1e1c;
  --border-dark: #33312d;
  --text-dark: #eae7e0;
  --muted-dark: #a6a095;
  --accent-dark: #f59e0b;
  --accent-text-dark: #141413;
  --sent-bg-dark: #3b2f13;
  --sent-text-dark: #faf8f4;
  --word-bg-dark: #d97706;
  --word-text-dark: #ffffff;

  --bg: var(--bg-light);
  --surface: var(--surface-light);
  --border: var(--border-light);
  --text: var(--text-light);
  --muted: var(--muted-light);
  --accent: var(--accent-light);
  --accent-text: var(--accent-text-light);
  --sent-bg: var(--sent-bg-light);
  --sent-text: var(--sent-text-light);
  --word-bg: var(--word-bg-light);
  --word-text: var(--word-text-light);

  --reader-font-size: 1.125rem;

  @media (prefers-color-scheme: dark) {
    --bg: var(--bg-dark);
    --surface: var(--surface-dark);
    --border: var(--border-dark);
    --text: var(--text-dark);
    --muted: var(--muted-dark);
    --accent: var(--accent-dark);
    --accent-text: var(--accent-text-dark);
    --sent-bg: var(--sent-bg-dark);
    --sent-text: var(--sent-text-dark);
    --word-bg: var(--word-bg-dark);
    --word-text: var(--word-text-dark);
  }

  @supports (color: light-dark(white, black)) {
    --bg: light-dark(var(--bg-light), var(--bg-dark));
    --surface: light-dark(var(--surface-light), var(--surface-dark));
    --border: light-dark(var(--border-light), var(--border-dark));
    --text: light-dark(var(--text-light), var(--text-dark));
    --muted: light-dark(var(--muted-light), var(--muted-dark));
    --accent: light-dark(var(--accent-light), var(--accent-dark));
    --accent-text: light-dark(var(--accent-text-light), var(--accent-text-dark));
    --sent-bg: light-dark(var(--sent-bg-light), var(--sent-bg-dark));
    --sent-text: light-dark(var(--sent-text-light), var(--sent-text-dark));
    --word-bg: light-dark(var(--word-bg-light), var(--word-bg-dark));
    --word-text: light-dark(var(--word-text-light), var(--word-text-dark));
  }

  &:has(> head > meta[name="color-scheme"][content="dark"]) {
    color-scheme: dark;
  }

  &:has(> head > meta[name="color-scheme"][content="light"]) {
    color-scheme: light;
  }
}

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

html {
  accent-color: var(--accent);
  scrollbar-color: var(--muted) var(--surface);
  scroll-behavior: smooth;
}

body {
  margin: 0;
  background-color: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
  min-height: 100dvh;
}

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

/* Theme toggle visibility synchronized strictly to meta[name="color-scheme"] */
.theme-toggle .when-dark {
  display: none;
}

:root:has(> head > meta[name="color-scheme"][content="dark"]) .theme-toggle .when-light {
  display: none;
}
:root:has(> head > meta[name="color-scheme"][content="dark"]) .theme-toggle .when-dark {
  display: inline;
}

@media (prefers-color-scheme: dark) {
  :root:has(> head > meta[name="color-scheme"][content="light dark"]) .theme-toggle .when-light {
    display: none;
  }
  :root:has(> head > meta[name="color-scheme"][content="light dark"]) .theme-toggle .when-dark {
    display: inline;
  }
}

.top-bar {
  position: sticky;
  top: 0;
  z-index: 20;
  background-color: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.5rem 1rem;
}

.top-bar-inner,
.player-inner {
  max-width: 72rem;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.nav-group,
.action-group,
.font-controls,
.transport-group {
  display: flex;
  align-items: center;
  gap: 0.45rem;
}

.nav-group {
  flex: 1 1 20rem;
  min-width: 0;
}

.control-select,
.icon-btn,
.pill-btn,
.play-btn,
.resume-follow-btn {
  font: inherit;
  font-size: 0.875rem;
  border-radius: 0.5rem;
  border: 1px solid var(--border);
  background-color: var(--bg);
  color: var(--text);
  padding: 0.4rem 0.65rem;
  min-height: 2.25rem;
  cursor: pointer;
}

.control-select {
  max-width: 100%;
  min-width: 0;
  text-overflow: ellipsis;
}

#chapter-select {
  max-width: 15rem;
}

#section-select {
  flex: 1 1 12rem;
  max-width: 24rem;
}

.pill-btn[aria-pressed="true"],
.play-btn,
.resume-follow-btn {
  background-color: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
  font-weight: 600;
}

.download-link {
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.download-link[hidden] {
  display: none;
}

.play-btn {
  min-width: 4.5rem;
}

a:focus-visible,
button:focus-visible,
select:focus-visible,
input[type="range"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.reader-article {
  max-width: 66ch;
  margin: 0 auto;
  padding: 1.75rem 1.25rem 2rem;
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", Palatino, serif;
  font-size: var(--reader-font-size);
  line-height: 1.75;
}

.chapter-section {
  margin-bottom: 1.5rem;
}

.deferred-section {
  content-visibility: auto;
  contain-intrinsic-size: auto none auto 900px;
}

.reader-block {
  cursor: pointer;
  border-radius: 0.25rem;
  transition: background-color 0.12s ease;
}

.reader-block:hover {
  background-color: color-mix(in srgb, var(--surface) 70%, transparent);
}

.reader-block.block-active {
  background-color: var(--sent-bg);
}

.reader-article h1 {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 1.75em;
  line-height: 1.25;
  margin: 0 0 1.25rem;
}

.reader-article h2 {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 1.38em;
  line-height: 1.3;
  margin: 2.25rem 0 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}

.reader-article h3 {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 1.15em;
  line-height: 1.35;
  margin: 1.5rem 0 0.5rem;
}

.reader-article h4 {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 1.02em;
  line-height: 1.4;
  margin: 1.1rem 0 0.4rem;
}

.reader-article h6 {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 0.88em;
  font-weight: 500;
  font-style: italic;
  color: var(--muted);
  line-height: 1.45;
  margin: 0.65rem 0 0.85rem;
}

.reader-article p {
  margin: 0 0 0.95rem;
}

.reader-article p.is-bullet,
.reader-article p.is-cw-item {
  margin-bottom: 0.25rem;
  padding-left: 0.5rem;
}

.reader-article p.is-cw-item {
  font-size: 0.95em;
  line-height: 1.5;
}

.reader-article p.is-bullet + p:not(.is-bullet),
.reader-article p.is-cw-item + p:not(.is-cw-item) {
  margin-top: 0.85rem;
}

/* CSS Custom Highlight API styling */
::highlight(active-sentence) {
  background-color: var(--sent-bg);
  color: var(--sent-text);
}

::highlight(active-word) {
  background-color: var(--word-bg);
  color: var(--word-text);
  text-decoration: underline;
  text-decoration-thickness: 2px;
  text-underline-offset: 2px;
}

mark.fallback-active {
  background-color: var(--sent-bg);
  color: var(--sent-text);
  text-decoration: underline;
  text-decoration-thickness: 2px;
  border-radius: 0.15rem;
}

.resume-follow-btn {
  position: fixed;
  right: 1.25rem;
  bottom: 5.5rem;
  z-index: 25;
  box-shadow: 0 4px 14px rgb(0 0 0 / 0.22);
}

.player-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  background-color: var(--surface);
  border-top: 1px solid var(--border);
  padding: 0.55rem 1rem calc(0.55rem + env(safe-area-inset-bottom, 0px));
}

.seek-group {
  flex: 1 1 16rem;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-width: 12rem;
}

.time-label {
  font-variant-numeric: tabular-nums;
  font-size: 0.8rem;
  color: var(--muted);
  min-width: 3.4rem;
  text-align: center;
}

/* Native accessible range slider styled per modern-web-guidance */
input[type="range"] {
  --slider-track-size: 0.45rem;
  --slider-thumb-size: 1.1rem;
  --slider-track-radius: 999px;
  --slider-thumb-radius: 50%;
  --slider-track-color: var(--border);
  --slider-fill-color: var(--accent);
  --slider-thumb-color: var(--accent);
  --slider-direction: to right;

  --attr-min: attr(min type(<number>), 0);
  --attr-max: attr(max type(<number>), 100);
  --control-value: attr(value type(<number>), 0);
  --slider-progress: calc(
    progress(var(--control-value), var(--attr-min), var(--attr-max)) * 100%
  );

  appearance: none;
  flex: 1 1 auto;
  inline-size: 100%;
  block-size: 2.25rem;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  cursor: pointer;
}

@supports not (opacity: progress(1, 0, 100)) {
  input[type="range"] {
    --slider-progress: calc(
      (var(--control-value) - var(--attr-min)) /
      max(0.001, (var(--attr-max) - var(--attr-min))) * 100%
    );
  }
}

input[type="range"]::-webkit-slider-runnable-track {
  block-size: var(--slider-track-size);
  border: 0;
  border-radius: var(--slider-track-radius);
  background: linear-gradient(
    var(--slider-direction),
    var(--slider-fill-color) var(--slider-progress),
    var(--slider-track-color) var(--slider-progress)
  );
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  inline-size: var(--slider-thumb-size);
  block-size: var(--slider-thumb-size);
  margin-block-start: calc((var(--slider-track-size) - var(--slider-thumb-size)) / 2);
  border: 0;
  border-radius: var(--slider-thumb-radius);
  background: var(--slider-thumb-color);
}

input[type="range"]::-moz-range-track {
  block-size: var(--slider-track-size);
  border: 0;
  border-radius: var(--slider-track-radius);
  background: var(--slider-track-color);
}

input[type="range"]::-moz-range-progress {
  block-size: var(--slider-track-size);
  border-radius: var(--slider-track-radius);
  background: var(--slider-fill-color);
}

input[type="range"]::-moz-range-thumb {
  inline-size: var(--slider-thumb-size);
  block-size: var(--slider-thumb-size);
  border: 0;
  border-radius: var(--slider-thumb-radius);
  background: var(--slider-thumb-color);
}

.loading-state {
  color: var(--muted);
  font-family: system-ui, sans-serif;
}

.site-footer {
  max-width: 66ch;
  margin: 0 auto;
  padding: 1.25rem 1.25rem 7.5rem;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.55;
}

.site-footer p {
  margin: 0 0 0.45rem;
}

.site-footer p:last-child {
  margin-bottom: 0;
}

.site-footer a {
  color: var(--accent);
}

@media (max-width: 640px) {
  .reader-article {
    padding: 1.25rem 0.9rem 1.75rem;
  }

  .site-footer {
    padding: 1.25rem 0.9rem 9rem;
  }

  .resume-follow-btn {
    bottom: 7.25rem;
  }

  .player-inner {
    gap: 0.35rem 0.5rem;
  }

  .seek-group {
    order: -1;
    flex-basis: 100%;
  }
}
