/* Load after the existing game/workshop styles. Only the explicit display
   preference enables these colours; pitch geometry and game data are untouched. */
.display-theme-toggle {
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
  flex-shrink: 0; min-height: 44px; max-width: 100%; padding: .5rem .65rem;
  border: 1px solid var(--line, #728675); border-radius: 8px;
  background: transparent; color: var(--muted, #bccbc0); font: inherit; font-size: .8rem; cursor: pointer;
}
.display-theme-toggle[aria-pressed="true"] { border-width: 2px; font-weight: 750; }
.display-theme-toggle:hover { border-color: var(--gold-bright, #f6d98e); }
.display-theme-toggle:focus-visible { outline: 3px solid var(--gold-bright, #f6d98e); outline-offset: 3px; }
.topbar.has-display-theme { flex-wrap: wrap; }
.has-display-theme .topbar-actions { flex-wrap: wrap; min-width: 0; max-width: 100%; }
.site-header.has-display-theme { gap: .65rem; flex-wrap: wrap; }
.site-header.has-display-theme .brand { margin-inline-end: auto; }

/* Forced-colour users keep their system palette and existing keyboard rules. */
@media not (forced-colors: active) {
  html[data-klang-theme="light"] {
    color-scheme: light;
    --theme-page: #edf3ef;
    --theme-paper: #fffef9;
    --theme-soft: #e5eee8;
    --theme-ink: #172d25;
    --theme-muted: #3e584b;
    --theme-border: #7b9182;
    --theme-focus: #684600;
    --theme-selected: #f2dda5;
    --theme-success: #d9eddf;
    --theme-error: #f9e2dc;
    --ink: var(--theme-ink); --muted: var(--theme-muted);
    --deep: var(--theme-page); --panel: var(--theme-paper); --panel-soft: var(--theme-paper);
    --line: var(--theme-border); --gold: #76520a; --gold-bright: #634500;
    --success: #246440; --danger: #a12d25; --shadow: 0 12px 32px #183b251a;
    --genre-text: var(--theme-ink); --genre-muted: var(--theme-muted);
    --genre-line: var(--theme-border); --genre-gold: #76520a; --genre-surface: var(--theme-paper);
    background: var(--theme-page); color: var(--theme-ink);
  }
  html[data-klang-theme="light"] body,
  html[data-klang-theme="light"] .game-frame { background: var(--theme-page) !important; color: var(--theme-ink); }
  html[data-klang-theme="light"] :is(.topbar,.statusbar,.site-header,.site-footer) {
    background: var(--theme-paper) !important; color: var(--theme-ink); border-color: var(--theme-border); box-shadow: none;
  }
  /* Text is explicitly dark even in older modules with pale hardcoded labels.
     Both real keyboard families, including their labels, retain their colours. */
  html[data-klang-theme="light"] :where(.app-shell,.service-dialog,.recording-drawer,.rhythm-dialog-backdrop,.site-header,#main,.site-footer)
    :is(h1,h2,h3,h4,h5,p,li,dt,dd,label,legend,summary,small,span,strong,b,em,output,a,th,td,kbd):not(:where(.piano-key,.piano-key *,.g7-create-key,.g7-create-key *)) {
    color: var(--theme-ink) !important; text-shadow: none !important;
  }
  html[data-klang-theme="light"] :where(.app-shell,.service-dialog,.recording-drawer,#main) a {
    color: #164d72 !important; text-decoration-color: currentColor; text-underline-offset: 3px;
  }

  /* Component palettes are local in the original workshop styles. */
  html[data-klang-theme="light"] :is(.rhythm-workshop,.listening-workshop,.genre-workshop,.echo-workshop,.archive-workshop,.melody-workshop,.form-workshop) {
    --rhythm-bg: var(--theme-page); --rhythm-surface: var(--theme-paper); --rhythm-surface-raised: var(--theme-soft); --rhythm-surface-soft: #dbe8df;
    --rhythm-line: var(--theme-border); --rhythm-line-strong: #526d5b; --rhythm-text: var(--theme-ink); --rhythm-muted: var(--theme-muted);
    --rhythm-gold: #76520a; --rhythm-gold-bright: #634500; --rhythm-purple: #61458b; --rhythm-purple-bright: #61458b; --rhythm-purple-deep: #e5ddf3;
    --rhythm-emerald: #286244; --rhythm-danger: #a12d25; --rhythm-warning: #76520a; --rhythm-success: #246440; --rhythm-focus: var(--theme-focus); --rhythm-shadow: var(--shadow);
    --listen-bg: var(--theme-page); --listen-surface: var(--theme-paper); --listen-surface-raised: var(--theme-soft); --listen-line: var(--theme-border); --listen-line-strong: #526d5b;
    --listen-text: var(--theme-ink); --listen-muted: var(--theme-muted); --listen-gold: #76520a; --listen-gold-bright: #634500; --listen-blue: #24607a; --listen-blue-bright: #24607a;
    --listen-violet: #61458b; --listen-green: #246440; --listen-red: #a12d25; --listen-warning: #76520a; --listen-focus: var(--theme-focus); --listen-shadow: var(--shadow);
    --genre-text: var(--theme-ink); --genre-muted: var(--theme-muted); --genre-line: var(--theme-border); --genre-gold: #76520a; --genre-surface: var(--theme-paper);
    --echo-text: var(--theme-ink); --echo-muted: var(--theme-muted); --echo-line: var(--theme-border); --echo-gold: #76520a; --echo-surface: var(--theme-paper);
    --archive-text: var(--theme-ink); --archive-muted: var(--theme-muted); --archive-line: var(--theme-border); --archive-gold: #76520a; --archive-surface: var(--theme-paper);
    --melody-text: var(--theme-ink); --melody-muted: var(--theme-muted); --melody-line: var(--theme-border); --melody-gold: #76520a; --melody-mint: #246440;
    --form-text: var(--theme-ink); --form-muted: var(--theme-muted); --form-line: var(--theme-border); --form-gold: #76520a; --form-mint: #246440;
    background: var(--theme-page) !important; color: var(--theme-ink);
  }

  /* Main cards, year overviews, teacher workspaces, public entry and dialogs. */
  html[data-klang-theme="light"] :is(
    .start-card,.dialogue-card,.challenge-card,.result-card,.song-preview-card,.quest-panel,.world-atlas,.grade6-card,.g7-card,
    .teacher-login-card,.teacher-workspace,.teacher-tabs,.song-library,.lesson-share,.lesson-card,.lesson-code-row,
    .student-score,.performance-panel,.virtual-keyboard-card,.instrument-connect-panel,.equipment-test-panel,.song-play-guide,
    .rhythm-card,.rhythm-dialog,.rhythm-topbar,.rhythm-transport,.rhythm-loop-card,.rhythm-tempo-card,
    .listening-card,.listening-topbar,.listening-transport,.listening-listen-panel,.listening-answer-feedback,
    .genre-card,.genre-topbar,.genre-transport,.genre-answer-feedback,.genre-review-card,.genre-listen-panel,.genre-tradition-card,
    .echo-card,.echo-topbar,.echo-transport,.echo-answer-feedback,.echo-review-card,.echo-listen-panel,.echo-scene,
    .archive-card,.archive-topbar,.archive-transport,.archive-answer-feedback,.archive-review-card,
    .melody-card,.melody-phrase,.melody-transport,.form-card,.form-transport,.form-section,.form-motif,.form-loop,
    .g7-play-orientation,.g8-play-chart,.g9-play-chart,.g7-create-analysis,.g8-example,.g7-first-listen-guide,.g7-create-bottom-nav,
    .g7-editing-context,.g7-draft-origin,.g8-review-mix p,.g8-section-mix fieldset,
    .recording-dialog,.recording-dialog-header,.klang-local-audio,.kla-capture,.kla-capture-controls,.kla-chord-chart,.kla-backing,.kla-monitor,.kla-input-level,.kla-level-check,
    .service-dialog,.klang-support-panel,.klang-support-header,.klang-support-question,.klang-support-report,.klang-support-start,.school-license-status,
    .exam-mode-card,.exam-student-hold,.music-word-help,.music-word-entry,.world-explorer,.world-toolbar,.world-encounter,.world-dialogue,
    .entry-card,.teacher-section,.new-code,.notice,.progression-preview
  ) { background: var(--theme-paper) !important; color: var(--theme-ink); border-color: var(--theme-border) !important; box-shadow: 0 5px 18px #183b2512; }

  /* Dense nested data and help surfaces. */
  html[data-klang-theme="light"] :is(
    .quick-exam-card,.world-atlas-entry,.classroom-pass,.anonymous-code-card,.student-code-option,.session-match,
    .mechanic-note,.shared-lesson-notice,.preview-progression,.preview-mode-selector,.preview-badge,.song-countdown,
    .hand-expectation span,.song-meta span,.keyboard-score>span,.attached-file,.input-with-unit,.pulse-countdown,
    .lesson-stats>div,.results-summary>div,.equipment-test-readouts>div,
    .rhythm-submission,.rhythm-bar,.rhythm-result-grid>div,.listening-result-grid>div,.genre-result-grid>div,.echo-result-grid>div,.archive-result-grid>div,
    .listening-choice,.genre-choice,.echo-choice,.archive-choice,.listening-role-card,.listening-result-mark,.archive-listen-panel,.archive-method-grid article,.echo-method-grid article,
    .melody-identity,.melody-example,.form-identity,.form-boss-seals li,.form-boss-trophy,
    .g7-completion-checks>li,.society-next-step,.society-confidence-help,.g7-show-reason,
    .kla-notice,.kla-device-status,.kla-level-status,.kla-private,.recording-dialog [data-export-status],
    .exam-safety-note,.exam-notice,.klang-support-report-text
  ) { background: var(--theme-soft) !important; color: var(--theme-ink); border-color: var(--theme-border); }
  html[data-klang-theme="light"] :is(.results-table,.g7 table,.exam-mode table) { background: var(--theme-paper); color: var(--theme-ink); }
  html[data-klang-theme="light"] :is(.results-table,.g7 table,.exam-mode table) :is(th,td) { border-color: var(--theme-border) !important; }
  html[data-klang-theme="light"] :is(.results-table th,.g7 th,.exam-mode th) { background: var(--theme-soft) !important; }
  html[data-klang-theme="light"] :is(.results-table,.g7 table) tbody tr:nth-child(even) { background: #f0f5f1; }
  html[data-klang-theme="light"] :is(.klang-drum-staff,.g7-live-tap-staff) { color: var(--theme-ink) !important; }

  /* Native inputs remain native. Checkbox/range geometry and audio meters stay intact. */
  html[data-klang-theme="light"] :is(input,select,textarea) { accent-color: #32694c; }
  html[data-klang-theme="light"] :is(input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),select,textarea) {
    background: #fff !important; color: var(--theme-ink) !important; border-color: #718778 !important; -webkit-text-fill-color: var(--theme-ink);
  }
  html[data-klang-theme="light"] :is(input,textarea)::placeholder { color: #52685b !important; opacity: 1; }
  html[data-klang-theme="light"] :is(input,select,textarea)[readonly] { background: #edf2ee !important; }
  html[data-klang-theme="light"] :is(input,select,textarea)[aria-invalid="true"] { border-color: #a12d25 !important; outline-color: #a12d25; }
  html[data-klang-theme="light"] :is(input,select,textarea):disabled { background: #e4e9e5 !important; color: #4d5e53 !important; opacity: .85; }
  html[data-klang-theme="light"] input::file-selector-button { background: var(--theme-soft); color: var(--theme-ink); border-color: var(--theme-border); }

  /* Keyboard keys and image-backed map launchers have dedicated rules instead. */
  html[data-klang-theme="light"] button:not(:where(.piano-key,.g7-create-key,.world-landmark,.world-guide,.journey-portal,.destination-card,.avatar-card)) {
    background: var(--theme-soft) !important; color: var(--theme-ink) !important; border-color: #687f6e !important; filter: none;
  }
  html[data-klang-theme="light"] button:not(:where(.piano-key,.g7-create-key,.world-landmark,.world-guide,.journey-portal,.destination-card,.avatar-card)):where(:hover:not(:disabled)) {
    background: #d3e4d8 !important; border-color: #345c43 !important;
  }
  html[data-klang-theme="light"] button:not(:where(.piano-key,.g7-create-key)):disabled { opacity: .78; filter: none; cursor: not-allowed; }
  html[data-klang-theme="light"] .button-secondary { background: var(--theme-soft) !important; color: var(--theme-ink) !important; border-color: #687f6e !important; }
  html[data-klang-theme="light"] :is(.primary-button,.button-primary,.rhythm-primary,.listening-primary,.genre-primary,.echo-primary,.archive-primary,.melody-primary,.form-primary,.kla-primary,.klang-support-primary) {
    background: var(--theme-selected) !important; color: var(--theme-ink) !important; border-color: #8b681f !important;
  }
  html[data-klang-theme="light"] :is(.danger-button,.kla-delete) { background: var(--theme-error) !important; color: #7d241e !important; border-color: #a24d40 !important; }
  html[data-klang-theme="light"] :is(button,a,input,select,textarea,summary,[tabindex]):focus-visible {
    outline: 3px solid var(--theme-focus) !important; outline-offset: 3px;
  }
  html[data-klang-theme="light"] :is(.piano-key,.g7-create-key):focus-visible { outline-offset: -4px; }
  html[data-klang-theme="light"] kbd:not(:where(.piano-key *,.g7-create-key *)) { background: #f4f5ee !important; border-color: #7b9182; }

  /* Selection, recorded notes, current position and completion remain distinct.
     These scoped rules also outrank styles injected by the Year 7–9 editors. */
  html[data-klang-theme="light"] :is(.g7-step,.rhythm-step,.form-drum,.form-note,.melody-step,.song-bar,.g7-play-bar,.g7-create-analysis-bar,.kla-chart-bar,.klang-drum-grid span,.g7-live-pattern span,.g8-play-bars>span,.g9-play-bars>span,.beat-strip>span,.pattern-strip>span) {
    background: #f2f5ee !important; color: var(--theme-ink) !important; border-color: #8a9c8b !important;
  }
  html[data-klang-theme="light"] :is(.g7-step.is-active,.rhythm-step.is-active,.form-drum.is-active,.form-note.has-note,.melody-step.has-note,.klang-drum-grid .is-hit,.g7-live-pattern .is-hit,.pattern-strip>.filled) {
    background: #c9e2d1 !important; border-color: #35704a !important;
  }
  html[data-klang-theme="light"] :is(.g7-step.is-selected,.rhythm-step.is-selected,.form-note.is-selected,.melody-step.is-selected,.g7-create .is-selected):not(:where(.piano-key,.g7-create-key)) {
    outline: 3px solid #8a5b00 !important; outline-offset: 1px; background: #f4e2b3 !important;
  }
  html[data-klang-theme="light"] :is(.song-bar.is-current,.g7-play-bar.is-active,.g7-create-analysis-bar.is-playing,.kla-chart-bar.is-playing,.form-section.is-current,.form-note.is-current,.form-drum.is-current,.melody-step.is-current,.rhythm-step.is-current,.g8-play-bars>.is-active,.g9-play-bars>.is-active,.g7-live-pattern .is-current) {
    background: #f0ce76 !important; color: #172d25 !important; outline: 3px solid #76520a !important; outline-offset: -3px;
  }
  html[data-klang-theme="light"] .preview-chord.is-current { background: #c9e2d1 !important; color: #172d25 !important; outline-color: #32694c !important; text-decoration: underline; text-decoration-thickness: 2px; }
  html[data-klang-theme="light"] :is(button[aria-pressed="true"],.teacher-tab[aria-selected="true"],.song-list-item.is-selected,.atlas-grade-tabs .is-selected):not(:where(.piano-key,.g7-create-key,.g7-step,.rhythm-step,.form-note,.form-drum,.melody-step,.world-landmark,.journey-portal)) {
    background: var(--theme-selected) !important; color: var(--theme-ink) !important; border-color: #8b681f !important; box-shadow: inset 0 0 0 2px #8b681f;
  }
  html[data-klang-theme="light"] :is(.genre-choice,.echo-choice,.archive-choice,.listening-choice):has(input:checked),
  html[data-klang-theme="light"] :is(.g7s,.g8s,.g9s) fieldset label:has(input:checked) { background: #f4e2b3 !important; border-color: #8b681f !important; }
  html[data-klang-theme="light"] .g7-editing-context[data-editing-part="B"] { background: #e0eafa !important; border-color: #4c6e9b !important; }
  html[data-klang-theme="light"] .g7-editing-context[data-editing-part="C"] { background: #efe2f4 !important; border-color: #855894 !important; }
  html[data-klang-theme="light"] .g7-editing-context[data-editing-part="full"] { background: #dceeea !important; border-color: #39796c !important; }
  html[data-klang-theme="light"] .rhythm-bar.is-a { background: #e5ddf3 !important; border-color: #705294 !important; }
  html[data-klang-theme="light"] .rhythm-bar.is-b { background: #d9eddf !important; border-color: #35704a !important; }
  html[data-klang-theme="light"] .rhythm-bar.is-current { background: #f0ce76 !important; border-color: #76520a !important; outline: 3px solid #76520a; outline-offset: -3px; }
  html[data-klang-theme="light"] :is(.feedback.is-success,.builder-feedback.is-success,.keyboard-feedback.is-success,.g7-feedback,.kla-notice,.school-license-status,.journey-node-complete,.result-level-mastered,.g7-completion-checks>li.is-complete) { background: var(--theme-success) !important; border-color: #35704a !important; }
  html[data-klang-theme="light"] :is(.feedback.is-error,.builder-feedback.is-error,.keyboard-feedback.is-error,.kla-error,.result-level-practice,.session-match.is-error,.session-match.is-closed,[role="alert"]) { background: var(--theme-error) !important; color: #7d241e !important; border-color: #a24d40 !important; }
  html[data-klang-theme="light"] :is(.feedback.is-warning,.builder-feedback.is-warning,.keyboard-feedback.is-warning,.result-level-progress,.kla-level-status[data-level="silent"],.kla-level-status[data-level="low"]) { background: #f5e8bd !important; color: #604410 !important; border-color: #8b681f !important; }
  html[data-klang-theme="light"] .kla-level-status[data-level="clipping"] { background: var(--theme-error) !important; color: #7d241e !important; border-color: #a12d25 !important; }
  html[data-klang-theme="light"] :is(.grade6-field-error,.rhythm-export-feedback.is-error,.listening-storage-feedback.is-error,.g7-check-detail) { color: #7d241e !important; }
  html[data-klang-theme="light"] :is(.g7-version-status.is-complete,.kla-capture[data-state="recording"] .kla-status) { color: #246440 !important; font-weight: 800; }

  /* World images/avatars retain their art. Their information and controls use
     pale opaque labels, so legibility does not depend on the image behind them. */
  html[data-klang-theme="light"] :is(.world-explorer,.journey-path) { --world-ink: var(--theme-ink); --world-muted: var(--theme-muted); --world-accent: #76520a; --world-accent-bright: #634500; --world-line: #7b9182; --journey-accent: #39704a; }
  html[data-klang-theme="light"] :is(.world-landmark-title,.world-landmark-state,.world-map-badge,.world-map-caption,.world-map-status,.world-avatar-name,.world-guide-name,.journey-world-progress,.journey-node-marker) {
    color: var(--theme-ink) !important; background: var(--theme-paper) !important; border-color: #687f6e !important; text-shadow: none !important;
  }
  html[data-klang-theme="light"] .world-landmark-icon { background: #fffef9 !important; color: #294c38; border-color: #687f6e !important; }
  html[data-klang-theme="light"] .world-landmark.is-selected :is(.world-landmark-icon,.world-landmark-title) { background: var(--theme-selected) !important; border-color: #76520a !important; outline-color: #76520a !important; }
  html[data-klang-theme="light"] :is(.journey-world,.journey-node,.destination-card,.avatar-card) { background: var(--theme-paper) !important; border-color: #7b9182 !important; }
  html[data-klang-theme="light"] .destination-card::before { opacity: .18; }
  html[data-klang-theme="light"] .journey-portal { background-image: linear-gradient(0deg,#fffef9f5 10%,#fffef9c4 100%),url("./assets/ekomarken-stig.png") !important; border-color: #687f6e; }
  html[data-klang-theme="light"] .journey-portal-create { background-image: linear-gradient(0deg,#fffef9f5 10%,#fffef9c4 100%),url("./assets/ackordsmedjan-plats.png") !important; }
  html[data-klang-theme="light"] .journey-portal-society { background-image: linear-gradient(0deg,#fffef9f5 10%,#fffef9c4 100%),url("./assets/ekomarken.png") !important; }
  html[data-klang-theme="light"] .journey-portal[aria-pressed="true"],
  html[data-klang-theme="light"] :is(.destination-card,.avatar-card).is-selected { outline: 3px solid #76520a; outline-offset: 2px; border-color: #76520a !important; }
  html[data-klang-theme="light"] :is(.journey-node-complete,.journey-world-complete .journey-world-progress) { background: var(--theme-success) !important; border-color: #35704a !important; }
  html[data-klang-theme="light"] .journey-node-unavailable button { background: #e4e9e5 !important; color: #4d5e53 !important; }
  html[data-klang-theme="light"] .energy-segments i,
  html[data-klang-theme="light"] .progress-knot { background: #d8e3da; border-color: #7b9182; }
  html[data-klang-theme="light"] .energy-segments i.is-full,
  html[data-klang-theme="light"] :is(.progress-knot.is-current,.progress-knot.is-done) { background: #a77515; border-color: #76520a; }
  html[data-klang-theme="light"] .calm-view-toggle[aria-pressed="true"] { background: #d9eddf !important; }
}

@media (max-width: 600px) {
  .display-theme-toggle { padding: .45rem .55rem; }
  .site-header.has-display-theme .brand { flex: 1 1 100%; }
}
@media print { .display-theme-toggle { display: none; } }
