:root {
  --app-canvas: #fdfcf7;
  --app-surface: #fbf8f4;
  --app-raised: #fffdf8;
  --app-ink: #2d2924;
  --app-muted: #756d62;
  --app-border: rgb(45 41 36 / 18%);
  --app-hover: rgb(45 41 36 / 10%);
  --app-shadow: rgb(45 41 36 / 16%);
  --app-overlay: rgb(20 18 16 / 62%);
  --app-link: #4338ca;
  --app-danger: #b91c1c;
  --app-control-surface: #f3f2ef;
  --app-control-border: rgb(45 41 36 / 28%);
}

html[data-theme="dark"] {
  color-scheme: dark;
  --app-canvas: #000;
  --app-surface: #101010;
  --app-raised: #191919;
  --app-ink: #f1f1f1;
  --app-muted: #b9b9b9;
  --app-border: rgb(255 255 255 / 18%);
  --app-hover: rgb(255 255 255 / 11%);
  --app-shadow: rgb(0 0 0 / 48%);
  --app-overlay: rgb(0 0 0 / 72%);
  --app-link: #a5b4fc;
  --app-danger: #fca5a5;
  --app-control-surface: #242424;
  --app-control-border: rgb(255 255 255 / 28%);
}

:root,
body {
  background: var(--app-canvas);
  color: var(--app-ink);
}

/* Transitional utility bridge: background and text roles are mapped separately,
   never by redefining Tailwind's shared grey palette. */
html[data-theme="dark"] :is(.bg-white, .bg-gray-50, .bg-slate-50, .bg-stone-50) { background-color: var(--app-surface); }
html[data-theme="dark"] :is(.bg-gray-100, .bg-slate-100, .bg-gray-200, .bg-gray-300) { background-color: var(--app-raised); }
html[data-theme="dark"] :is(
  .bg-indigo-50, .bg-indigo-100,
  .bg-violet-50, .bg-violet-100,
  .bg-blue-50, .bg-blue-100,
  .bg-sky-50, .bg-sky-100,
  .bg-emerald-50, .bg-emerald-100,
  .bg-green-50, .bg-green-100, .bg-green-200,
  .bg-amber-50, .bg-amber-100,
  .bg-red-50, .bg-red-100,
  .bg-rose-50, .bg-rose-100
) { background-color: var(--app-raised); }
html[data-theme="dark"] :is(
  .text-gray-950, .text-gray-900, .text-gray-800,
  .text-stone-950, .text-stone-900, .text-stone-800,
  .text-slate-950, .text-slate-900, .text-slate-800,
  .text-zinc-950, .text-zinc-900, .text-zinc-800,
  .text-neutral-950, .text-neutral-900, .text-neutral-800
) { color: var(--app-ink); }
html[data-theme="dark"] :is(
  .text-gray-700, .text-gray-600, .text-gray-500, .text-gray-400,
  .text-stone-700, .text-stone-600, .text-stone-500, .text-stone-400,
  .text-slate-700, .text-slate-600, .text-slate-500, .text-slate-400,
  .text-zinc-700, .text-zinc-600, .text-zinc-500, .text-zinc-400,
  .text-neutral-700, .text-neutral-600, .text-neutral-500, .text-neutral-400
) { color: var(--app-muted); }
html[data-theme="dark"] :is(.text-indigo-500, .text-indigo-600, .text-indigo-700, .text-indigo-800, .text-indigo-900, .text-indigo-950, .text-violet-700, .text-violet-900, .text-violet-950) { color: #a5b4fc; }
html[data-theme="dark"] :is(.text-blue-800, .text-sky-800, .text-sky-900, .text-sky-950) { color: #93c5fd; }
html[data-theme="dark"] :is(.text-emerald-700, .text-emerald-800, .text-emerald-900, .text-emerald-950, .text-green-600, .text-green-700, .text-green-800, .text-green-900, .text-green-950) { color: #86efac; }
html[data-theme="dark"] .foundation-candidate-quote {
  background-color: #166534;
  color: #f0fdf4;
  text-decoration-color: #86efac;
}
html[data-theme="dark"] .foundation-candidate-quote:hover { background-color: #15803d; }
html[data-theme="dark"] .foundation-candidate-quote:focus-visible {
  outline: 2px solid #86efac;
  outline-offset: 2px;
}
html[data-theme="dark"] :is(.text-amber-500, .text-amber-700, .text-amber-800, .text-amber-900, .text-amber-950) { color: #fcd34d; }
html[data-theme="dark"] :is(.text-red-600, .text-red-700, .text-red-800, .text-red-900, .text-red-950, .text-rose-700, .text-rose-800, .text-rose-900, .text-rose-950) { color: #fca5a5; }
html[data-theme="dark"] :is(
  .border-gray-100, .border-gray-200, .border-gray-300, .border-gray-400,
  .border-stone-100, .border-stone-200, .border-stone-300, .border-stone-400,
  .border-slate-100, .border-slate-200, .border-slate-300, .border-slate-400,
  .border-zinc-100, .border-zinc-200, .border-zinc-300,
  .border-neutral-100, .border-neutral-200, .border-neutral-300,
  .ring-gray-200, .ring-gray-300, .ring-gray-400,
  .ring-slate-200, .ring-slate-300
) { border-color: var(--app-border); --tw-ring-color: var(--app-border); }
html[data-theme="dark"] .hover\:bg-stone-100:hover,
html[data-theme="dark"] .hover\:bg-stone-50:hover { background-color: var(--app-hover); }
html[data-theme="dark"] .hover\:bg-gray-50:hover,
html[data-theme="dark"] .hover\:bg-gray-100:hover { background-color: var(--app-hover); }

html[data-theme="dark"] :is(.site-page-surface, #main-panel, #primary-sidebar, #primary-sidebar .primary-sidebar-menu, .secondary-panel-popover, #sidebar .bg-white, el-menu[popover], el-dialog-panel) {
  background: var(--app-surface);
  color: var(--app-ink);
  border-color: var(--app-border);
}
/* Keep the secondary navigation distinct from the page while allowing a
   restrained amount of the blurred reader surface to show through. */
html[data-theme="dark"] .secondary-panel-popover {
  background: rgb(16 16 16 / 82%);
}
html[data-theme="dark"] :is(#main-panel, .site-page-surface) { background-color: var(--app-canvas); }
html[data-theme="dark"] #primary-sidebar :is(a, button) { color: var(--app-ink); }
html[data-theme="dark"] #primary-sidebar :is(a, button):hover,
html[data-theme="dark"] #primary-sidebar :is(a, button):focus-visible,
html[data-theme="dark"] #primary-sidebar :is(.bg-gray-100, .bg-indigo-100) { background-color: var(--app-hover); }

html[data-theme="dark"] body:has(.chapter-detail-paper),
html[data-theme="dark"]:has(.chapter-detail-paper),
html[data-theme="dark"] body:has(.element-detail-magazine),
html[data-theme="dark"]:has(.element-detail-magazine),
html[data-theme="dark"] #main-panel:has(.chapter-detail-paper),
html[data-theme="dark"] #main-panel:has(.element-detail-magazine) { background: var(--app-canvas); }

/* Editorial detail summaries inherit a translucent white panel in light mode.
   On the black reader canvas, keep the prose on the same uninterrupted surface. */
html[data-theme="dark"] .element-detail-summary { background: var(--app-canvas); }

/* Keep multiline text boxes visible before they receive focus. Tailwind's
   reset makes controls transparent, so give textareas a quiet surface and a
   dependable boundary in both themes. */
html textarea {
  border: 1px solid var(--app-control-border);
  background-color: var(--app-control-surface);
  color: var(--app-ink);
}

html textarea:focus {
  border-color: var(--app-ink);
}

html[data-theme="dark"] .element-detail-hero-heading [aria-label="Breadcrumb"] a {
  color: var(--app-muted);
}

html[data-theme="dark"] .element-detail-hero-heading [aria-label="Breadcrumb"] a:hover,
html[data-theme="dark"] .element-detail-hero-heading [aria-label="Breadcrumb"] a:focus-visible {
  color: var(--app-ink);
}

html[data-theme="dark"] .post-action { color: var(--app-muted); }
html[data-theme="dark"] .post-action:hover,
html[data-theme="dark"] .post-action:focus-visible {
  background: var(--app-hover);
  color: var(--app-ink);
}
html[data-theme="dark"] .post-action.is-active { color: #fb7185; }

html[data-theme="dark"] .element-detail-planner {
  background: var(--app-canvas);
  border-color: var(--app-border);
}

/* Shared legacy panels that pre-date semantic theme variables. */
html[data-theme="dark"] :is(
  .analytics-consent,
  .library-access-dialog,
  .pwa-install-dialog,
  .post-share-popover,
  .narrative-element-preview-card,
  .definitional-explainer-card,
  .edit-inspector-confirm-card
) {
  background: var(--app-surface);
  color: var(--app-ink);
  border-color: var(--app-border);
}

html[data-theme="dark"] :is(
  .analytics-consent button[data-analytics-consent="denied"],
  .library-access-dialog-button--cancel,
  .pwa-install-dialog .pwa-install-dismiss,
  .post-share-popover input,
  .edit-inspector-close
) {
  background: var(--app-raised);
  color: var(--app-ink);
  border-color: var(--app-border);
}

/* Overview links and eyebrow labels use indigo in the light theme. On the
   dark reader canvas, keep that supporting text neutral and legible. */
html[data-theme="dark"] .text-overview .text-indigo-700 { color: var(--app-muted); }
html[data-theme="dark"] .text-overview .hover\:text-indigo-800:hover,
html[data-theme="dark"] .text-overview .hover\:text-indigo-800:focus-visible {
  color: var(--app-ink);
}

/* Key-event cards are pale green in the light chapter timeline. Keep their
   structure and green timeline markers, but use the dark canvas for readable prose. */
html[data-theme="dark"] .chapter-detail-event {
  background: var(--app-canvas);
  color: var(--app-ink);
}

/* The mobile reader uses its own key-event timeline. It has the same pale
   green light-theme cards, so explicitly place its prose on the dark canvas
   too instead of inheriting light card colours under dark text. */
html[data-theme="dark"] .mobile-event-timeline-item {
  background: var(--app-canvas);
  color: var(--app-ink);
}

html[data-theme="dark"] :is(
  .text-overview,
  .text-overview-image-placeholder,
  .element-index-card,
  .generated-image,
  .mobile-search,
  .mobile-book-card,
  .mobile-more-list,
  .mobile-more-dialog,
  .mobile-library-account-menu,
  .mobile-copy-section,
  .mobile-editorial-card,
  .mobile-deeper-analysis-gate,
  .mobile-request-text-dialog,
  .mobile-book-purchase-dialog,
  .reader-comment,
  .reader-comment-loading,
  .post-share-popover,
  .quote-analysis-panel
) {
  background-color: var(--app-surface);
  color: var(--app-ink);
  border-color: var(--app-border);
}

html[data-theme="dark"] :is(
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]):not([type="hidden"]),
  textarea,
  select
) {
  border-color: var(--app-border);
  background-color: var(--app-raised);
  color: var(--app-ink);
}

.theme-mode-form { margin: 0; }
.theme-mode-toggle {
  display: block;
  width: 100%;
  border: 0;
  padding: .5rem .75rem;
  background: transparent;
  color: inherit;
  cursor: pointer;
  font: inherit;
  font-size: .875rem;
  text-align: left;
}
.theme-mode-toggle:hover,
.theme-mode-toggle:focus-visible { background: var(--app-hover); }
.theme-mode-toggle:focus-visible { outline: 2px solid currentColor; outline-offset: -2px; }
