/* Screen-only palette: source artwork and printed lessons retain their colours. */
@media screen {
  html[data-theme="dark"] {
    color-scheme: dark;
    --paper: #15171c;
    --ink: #f2ede2;
    --white: #1f232b;
    --muted: #b7b5ad;
    --line: #444953;
    --blue: #91a8ff;
    --red: #ff8372;
    --theme-brand-blue: #244bcf;
    --theme-brand-red: #c93426;
    --theme-cream: #f2ede2;
    --theme-surface: #1f232b;
    --theme-surface-hover: #2a303d;
  }

  html[data-theme="dark"] ::selection {
    background: #91a8ff;
    color: #15171c;
  }

  /* Surfaces with a hard-coded light background in the original theme. */
  html[data-theme="dark"] :is(
    .patterns-section, .derivation-steps, .feedback-section,
    .home-review-grid > p, .returning-student, .discovery-playground,
    .photo-crop, .learning-empty, .topic-search-input,
    .lesson-lab-plots figure, .trig-lab-board
  ) {
    background: var(--theme-surface);
    color: var(--ink);
  }

  html[data-theme="dark"] :is(.learning-collection-message, .integration-equation) {
    background: #222b43;
    color: var(--ink);
  }

  html[data-theme="dark"] :is(.learning-alert, .latex-source .review-warning) {
    background: #382421;
    color: var(--ink);
  }

  html[data-theme="dark"] .home-page .home-topic-card {
    background: var(--theme-surface);
  }

  html[data-theme="dark"] :is(
    .home-page .home-topic-card:hover,
    .case-tab:hover:not(.active),
    .learning-filters button:hover:not([aria-pressed="true"])
  ) {
    background: var(--theme-surface-hover);
  }

  /* Keep the signature blue panels blue, with consistently light lettering. */
  html[data-theme="dark"] :is(
    .lesson-nav, .latex-source .lesson-nav, .blue-solution,
    thead, .series-table-wrap th, .latex-source .source-table th,
    .challenge-section, .resource-download, .binomial-question,
    .binomial-term-cards article:nth-child(2), .series-callout,
    .latex-source .source-callout, .about-cta, .home-personal,
    .lab-board, .learning-resume, .learning-continue,
    #surprise-topic, .topic-surprise,
    .pascal-row:nth-child(5) > div > span
  ) {
    background: var(--theme-brand-blue);
    color: var(--theme-cream);
  }

  html[data-theme="dark"] .blue-solution {
    border-color: var(--theme-brand-blue);
  }

  html[data-theme="dark"] .lesson-nav .nav-inner :is(a, a.active, a > span) {
    color: var(--theme-cream);
  }

  html[data-theme="dark"] .lesson-nav .nav-inner a.active::after {
    background: var(--theme-cream);
  }

  html[data-theme="dark"] :is(
    .dark-callout, .worked-board, .applications-section,
    .recursive-worked, .integration-worked, .mode-demo,
    .binomial-term-cards article, .home-final, .latex-source .draft-bar
  ) {
    background: #20242c;
    color: var(--theme-cream);
  }

  html[data-theme="dark"] .binomial-term-cards article:nth-child(2) {
    background: var(--theme-brand-blue);
  }

  html[data-theme="dark"] :is(
    .step-next, .challenge-next, .home-final .primary-link,
    .ratio-badge, .method-steps li::before
  ) {
    background: var(--theme-brand-red);
    color: var(--theme-cream);
  }

  html[data-theme="dark"] :is(
    .recursive-worked .step-next, .integration-worked .step-next,
    .method-steps li:nth-child(even)::before,
    .mode-switch button[aria-pressed="true"],
    .topic-area-filters button[aria-pressed="true"],
    .lesson-progress-links a.is-complete,
    .section-progress-tool button[aria-pressed="true"],
    .is-completed .learning-topic-status,
    .learning-section-links .is-complete .learning-section-marker,
    .trig-lab-controls button[aria-pressed="true"],
    .lesson-lab-presets button:hover, .profile-dropdown a:hover,
    .binomial-roadmap a:hover, .sequence-diagram > span:last-child
  ) {
    background: var(--theme-brand-blue);
    border-color: var(--theme-brand-blue);
    color: var(--theme-cream);
  }

  html[data-theme="dark"] :is(
    .primary-link:hover, .resource-download:hover, .challenge-next:hover,
    .step-next:hover, .recursive-worked .step-next:hover,
    .integration-worked .step-next:hover, .playground-footer button:hover
  ) {
    background: #364c99;
    color: var(--theme-cream);
  }

  html[data-theme="dark"] .primary-link:hover > span,
  html[data-theme="dark"] .home-final .primary-link > span {
    color: inherit;
  }

  html[data-theme="dark"] .step-next:disabled {
    background: #424750;
    color: var(--theme-cream);
  }

  html[data-theme="dark"] :is(
    .about-cta, .home-personal, .learning-resume, .learning-continue
  ) .primary-link {
    background: var(--theme-cream);
    color: #191919;
    border-color: var(--theme-cream);
  }

  html[data-theme="dark"] :is(
    .about-cta, .home-personal, .learning-resume, .learning-continue
  ) .primary-link > span {
    color: inherit;
  }

  html[data-theme="dark"] :is(
    .about-cta, .home-personal, .learning-resume, .learning-continue
  ) .primary-link:hover {
    background: #15171c;
    color: var(--theme-cream);
    border-color: var(--theme-cream);
  }

  /* Blue and dark lesson panels contain their own contrasting controls. */
  html[data-theme="dark"] :is(.ratio-controls button, .pattern-key, .mode-switch button) {
    color: var(--theme-cream);
  }

  html[data-theme="dark"] :is(
    .recursive-worked .reset-steps,
    .integration-worked .reset-steps, .integration-worked .show-all
  ) {
    color: var(--theme-cream);
  }

  html[data-theme="dark"] .sequence-diagram > span:first-child {
    background: var(--theme-brand-red);
    border-color: var(--theme-brand-red);
    color: var(--theme-cream);
  }

  html[data-theme="dark"] .mode-switch button {
    border-color: #bbb9b2;
  }

  html[data-theme="dark"] .ratio-controls button[aria-pressed="true"] {
    background: var(--theme-cream);
    color: var(--theme-brand-blue);
    border-color: var(--theme-cream);
  }

  html[data-theme="dark"] .pattern-column i:not(.negative),
  html[data-theme="dark"] .learning-resume .learning-progress-track > span {
    background: var(--theme-cream);
  }

  html[data-theme="dark"] .question-number {
    background: var(--theme-cream);
    color: var(--theme-brand-blue);
  }

  html[data-theme="dark"] .challenge-art i:nth-child(2) {
    border-color: var(--theme-cream);
  }

  html[data-theme="dark"] .challenge-art i:last-child {
    background: var(--theme-cream);
  }

  html[data-theme="dark"] .integration-challenge .recursive-panel {
    border-color: var(--theme-cream);
  }

  html[data-theme="dark"] .integration-challenge .section-number {
    color: var(--theme-cream);
  }

  html[data-theme="dark"] .integration-challenge .integration-equation {
    background: transparent;
    color: inherit;
  }

  html[data-theme="dark"] .integration-challenge .solution {
    background: var(--theme-surface);
    color: var(--ink);
  }

  /* Formula text uses the current palette; original generated artwork does not. */
  html[data-theme="dark"] .latex-source .source-math.display {
    background: var(--theme-surface);
    color: var(--ink);
  }

  html[data-theme="dark"] :is(
    .latex-source figure img, .series-chart img, .series-equation,
    .trig-diagram img, .recursive-chart img, .source-chart
  ) {
    background: #f2ede2;
    color: #191919;
  }

  html[data-theme="dark"] .series-equation {
    padding-inline: 12px;
    margin-block: 8px;
  }

  html[data-theme="dark"] :is(.poster-thumb, .dialog-image) {
    background: #ded9ce;
  }

  html[data-theme="dark"] :is(.resource-qr, .companion-share img) {
    background: #fff;
  }

  /* Dynamic plots remain theme-aware without changing their mathematical data. */
  html[data-theme="dark"] :is(.lab-axis, .trig-axis) {
    stroke: var(--muted);
  }

  html[data-theme="dark"] .trig-grid-line {
    stroke: var(--line);
  }

  html[data-theme="dark"] .trig-legend .trig-dash-key::before {
    border-color: var(--muted);
  }

  html[data-theme="dark"] .lab-dot {
    stroke: var(--theme-surface);
  }

  html[data-theme="dark"] .trig-marker {
    fill: var(--theme-surface);
  }

  /* The home teaser retains the source graph on a light artboard under a veil. */
  html[data-theme="dark"] .latest-feature {
    background: var(--theme-surface);
  }

  html[data-theme="dark"] .latest-graph {
    opacity: .45;
  }

  html[data-theme="dark"] .latest-graph img {
    background: #f2ede2;
    mix-blend-mode: normal;
  }

  html[data-theme="dark"] .latest-feature::after {
    background: linear-gradient(90deg, #1f232b 0%, #1f232bf5 37%, #1f232b88 76%, #1f232b33 100%);
  }

  /* Account and feedback fields, browser controls and secondary copy. */
  html[data-theme="dark"] :is(
    .feedback-form input, .feedback-form select, .feedback-form textarea,
    .account-panel input, .account-panel select,
    .review-entry input, .review-entry select, .review-entry textarea
  ) {
    background: var(--paper);
    border-color: #737986;
    color: var(--ink);
  }

  html[data-theme="dark"] :is(input, textarea)::placeholder {
    color: var(--muted);
    opacity: 1;
  }

  html[data-theme="dark"] :is(select, option) {
    background-color: var(--paper);
    color: var(--ink);
  }

  html[data-theme="dark"] :is(input[type="checkbox"], input[type="radio"], input[type="range"]) {
    accent-color: var(--blue);
  }

  html[data-theme="dark"] :is(
    .feedback-form label > span, .feedback-message-note,
    .feedback-actions > span, .feedback-card-heading time,
    .feedback-card-meta, .inbox-empty p,
    .topic-search-input input::placeholder
  ) {
    color: var(--muted);
  }

  html[data-theme="dark"] .feedback-status.is-error {
    color: var(--red);
  }

  html[data-theme="dark"] :is(.account-panel, .account-bar, .email-pending) {
    border-color: var(--line);
  }

  html[data-theme="dark"] .profile-dropdown {
    background: var(--theme-surface);
    border-color: #737986;
    box-shadow: 5px 5px 0 #080a0f;
  }

  html[data-theme="dark"] .account-shapes i:nth-child(3) {
    background: var(--theme-cream);
  }

  html[data-theme="dark"] .lesson-progress > progress,
  html[data-theme="dark"] .lesson-progress > progress::-webkit-progress-bar {
    background: var(--line);
  }

  html[data-theme="dark"] :is(
    .worked-board button, .blue-solution summary, .challenge-section a,
    .lab-board button, .learning-resume a, .lesson-nav .nav-inner a
  ):focus-visible {
    outline-color: var(--theme-cream);
  }

  @media (max-width: 650px) {
    html[data-theme="dark"] .latest-feature::after {
      background: linear-gradient(90deg, #1f232bf5, #1f232bcc);
    }
  }
}
