/* Shared semantic palette. Artwork, video and the deliberate black fallback stay intact. */
:root[data-theme] {
  --accent:#AA7ED3; --accent-hover:#c09be0;
  --bg-primary:#050507; --bg-secondary:#0c0a10; --surface:#111116;
  --surface-elevated:#201829; --surface-raised:var(--surface-elevated); --text-primary:#f7f4fa; --text-secondary:#c5bdcd;
  --accent-text:#AA7ED3; --border:#796486; --positive:#79efb0;
  --negative:#ff9ca9; --focus:#c9a3ed; --selected:#342344;
  --score-bg:#0b2118; --score-text:#96ffbf; --score-secondary:#c7e5d5;
  --shadow-color:#0004; color-scheme:dark;
}
:root[data-theme="light"] {
  --bg-primary:#C9C9C9; --bg-secondary:#BDBDBD; --surface:#DADADA;
  --surface-elevated:#DEDEDE; --surface-raised:var(--bg-secondary); --text-primary:#111111; --text-secondary:#343434;
  --accent-text:#633381; --accent-hover:#9460bd; --border:#666666;
  --positive:#17613c; --negative:#a92c42; --focus:#633381; --selected:#e6d4f4;
  --score-bg:#BDBDBD; --score-text:#111111; --score-secondary:#343434;
  --shadow-color:#38204714; color-scheme:light;
}
/* Map legacy tokens to the same palette, including inherited Shadow DOM tokens. */
:root[data-theme], :host {
  --black:var(--bg-primary)!important; --bg:var(--bg-primary)!important;
  --panel:var(--surface)!important; --panel-soft:var(--surface-raised)!important;
  --white:var(--text-primary)!important; --muted:var(--text-secondary)!important;
  --ink:var(--text-primary)!important; --line:var(--border)!important;
  --purple:var(--accent-text)!important; --green:var(--positive)!important;
}
html[data-theme], html[data-theme] body, :host {
  background:var(--bg-primary)!important; color:var(--text-primary)!important;
}
.site-header {background:var(--bg-primary)!important;border-color:var(--border)!important}
.site-header .brand {color:var(--accent-text)}
html[data-theme] .nav {background:var(--bg-primary)}
/* Fixed control geometry prevents initialisation and toggle layout shift. */
.theme-toggle {
  display:inline-flex;align-items:center;justify-content:center;gap:6px;
  flex:0 0 76px;width:76px;height:44px;min-height:44px;padding:0 8px;margin:0;
  border:1px solid var(--border);border-radius:4px;background:var(--surface);
  color:var(--text-primary);font:700 12px/1 Arial,sans-serif;cursor:pointer;
}
.theme-toggle .to-light {display:none}
[data-theme="dark"] .theme-toggle .to-light {display:inline}
[data-theme="dark"] .theme-toggle .to-dark {display:none}
.header-inner>.theme-toggle {order:3}
body>header:not(.site-header) {position:relative}
body.social-page>header {display:flex;align-items:center;gap:12px;flex-wrap:wrap}
body.social-page>header .theme-toggle {margin-left:auto}
html[data-theme] :focus-visible, :host :focus-visible {outline:3px solid var(--focus)!important;outline-offset:3px}
button:disabled {opacity:1!important;color:var(--text-secondary)!important;cursor:not-allowed;background:var(--bg-secondary)!important}
.theme-toggle:hover, .rail-controls button:hover, .tag-filter:hover, #copy:hover {background:var(--surface-raised);border-color:var(--accent)}
button, input, textarea, select {accent-color:var(--accent);color-scheme:inherit}
input:not([type=checkbox]):not([type=radio]), textarea, select {color:var(--text-primary)!important;background:var(--surface)!important;border-color:var(--border)!important}
.tag-filter[aria-pressed=true] {background:var(--selected)!important;color:var(--text-primary)!important;border-color:var(--accent-text)!important;box-shadow:inset 0 -3px var(--accent);font-weight:700}
.availability,.badges span {border-color:var(--border)}
.board-intro,.landing-slogan .brand-secondary,.team-live-heading span,#team-live-status,
.home-affiliate p,.fx-disclosure,.verified,.mission-photo-credit {color:var(--text-secondary)!important}
.team-live-heading strong {color:var(--accent-text)!important}
#team-live {color:var(--text-primary)}
.home-how-to-play {background:var(--bg-primary)!important}
.home-how-to-play h2 {color:var(--text-primary)!important}
.join,#join.compact-join {background:linear-gradient(110deg,var(--surface-raised),var(--surface))!important;border-color:var(--accent)}
.card.issuer-card,.media .card.issuer-card {border-color:var(--border)}
.issuer-card .money,.list-row .money,.social-money {color:var(--positive)!important}
.issuer-card .source {color:var(--positive)!important}
.organization-identity:not([data-visual="fallback"]) :is(.organization-role,.secondary-role) {color:var(--text-secondary)!important}
.issuer-card .mission-photo-credit {color:var(--text-secondary)!important}
.organization-identity:not([data-visual="fallback"]) {background:var(--surface-raised);border-color:var(--border)}
.mission-imagery-card .mission-identity>.organization-identity {background:none}
.mission-intelligence,.mission-visual {border-color:var(--border)!important}
.scoreboard {background:var(--score-bg)!important;border-color:var(--positive)!important}
.score-value {color:var(--score-text)!important;text-shadow:none}
.score-count,.score-updated,.score-label,.score-note {color:var(--score-secondary)!important}
.fx-disclosure a,.social-page a,.social-login a {color:var(--accent-text);text-decoration:underline;text-underline-offset:3px}
/* Both dynamically opened dialogs and standalone mission detail pages. */
.social-controls,.social-page,.social-login,.social-drawer {background:var(--bg-primary)!important;color:var(--text-primary)!important}
.social-controls {background:transparent!important}
.social-controls button,.social-controls a,.social-page button,.social-drawer .drawer-close {background:var(--surface-raised)!important;color:var(--text-primary)!important;border-color:var(--border)!important}
.social-controls [aria-pressed=true] {background:var(--selected)!important;border-color:var(--accent)!important;text-decoration:underline}
.social-status,.social-comment small {color:var(--text-secondary)!important}
.social-comment,.social-replies {border-color:var(--border)}
.social-login::backdrop,.social-drawer::backdrop {background:#140b2066}
/* Editorial text surfaces; image overlays and sponsor logo surfaces keep their approved colors. */
html[data-theme="light"] .book-card,
html[data-theme="light"] .card:not(.issuer-card),html[data-theme="light"] .about-panel {background:var(--surface);color:var(--text-primary)}
html[data-theme="light"] .rafi-memorandum {color:var(--text-primary)}
html[data-theme="light"] .rafi-memorandum__card {background:var(--surface);box-shadow:0 10px 32px var(--shadow-color)}
html[data-theme="light"] .rafi-memorandum__carousel,html[data-theme="light"] .rafi-memorandum__episode-nav {background:var(--surface)}
html[data-theme="light"] .rafi-memorandum__episode-position {color:var(--text-secondary)}
html[data-theme="light"] :is(.rafi-memorandum__dek,.rafi-memorandum__updated,.rafi-memorandum__status) {color:var(--text-secondary)}
html[data-theme="light"] .rafi-memorandum__eyebrow {color:var(--accent-text)}
html[data-theme="light"] #rafi-command-center {--text:var(--text-primary);--muted:var(--text-secondary);--panel:var(--surface);--panel2:var(--surface-raised);--purple:var(--accent-text)}
html[data-theme] #rafi-command-center .rf-news-tag {background:var(--accent);color:#21182b}
html[data-theme] #about {background:var(--bg-secondary);color:var(--text-primary)}
html[data-theme] #about h2 {color:var(--text-primary)}
html[data-theme] #about p,html[data-theme] #about .lead {color:var(--text-primary)}
html[data-theme] .disclaimer {background:var(--surface);color:var(--text-secondary)}
html[data-theme] .site-footer {background:var(--bg-secondary);color:var(--text-secondary)}
/* Structural page canvas must not inherit an elevated component surface. */
html[data-theme="light"] :is(.section,#about,.site-footer) {background:var(--bg-primary)}
html[data-theme] :is(.affiliate-disclosure,.affiliate-disclosure strong,.affiliate-placement-disclosure,.affiliate-placement-disclosure strong) {color:var(--text-secondary)!important}
html[data-theme] :is(.privacy-policy-copy a,.footer-legal-link) {color:var(--accent-text)}
html[data-theme="light"] .stars {color:#865500}
html[data-theme] .rafi-banner-space:not(.has-media) {background:var(--surface);color:var(--text-secondary)}
html[data-theme="light"] .rbt-banner-slot {background:var(--bg-secondary)}
html[data-theme="light"] .affiliate-placement-disclosure {background:var(--bg-primary)}
html[data-theme] .recon-strip {background:var(--surface-raised);color:var(--text-primary);border-color:var(--border)}
.recon-strip a,.change-state {color:var(--positive)!important}
.discovery-disclaimer {color:var(--text-secondary)!important}
html[data-theme] :is(.dek,.end-marker) {color:var(--text-secondary)}
html[data-theme] .section-label {color:var(--accent-text)}
html[data-theme="light"] .brand {color:var(--accent-text)}
/* RA-FI black fallback is a deliberate local dark surface in either theme. */
.issuer-card:has([data-visual="fallback"]),.list-row:has([data-visual="fallback"]) {
  --bg-primary:#000;--bg-secondary:#111;--surface:#000;--surface-raised:#211329;
  --text-primary:#fff;--text-secondary:#d6ccdf;--positive:#79efb0;
  --accent-text:#AA7ED3;--focus:#AA7ED3;--border:#9873ba;--selected:#342344;
  color-scheme:dark;background:#000!important;color:#fff!important;
}
@media(max-width:800px){
  .header-inner {gap:10px!important}
  .header-inner>.brand {margin-right:auto;letter-spacing:.07em}
  .header-inner>.theme-toggle {order:0}
  .header-inner>.mobile-menu-toggle {order:1;flex-shrink:0}
}
@media(max-width:760px){
  body:not(.social-page)>header:not(.site-header)>.theme-toggle {position:absolute;right:20px;top:12px}
  body:not(.social-page)>header:not(.site-header)>.brand {padding-right:82px}
  .theme-toggle {width:70px;flex-basis:70px}
}
