:root {
  --theme-dark-bg: #08111f;
  --theme-dark-surface: #0f1d2f;
  --theme-dark-elevated: #15263c;
  --theme-dark-soft: #1a304b;
  --theme-dark-line: #2a4565;
  --theme-dark-text: #edf6ff;
  --theme-dark-muted: #aec2d7;
  --theme-dark-accent: #78baff;
  --theme-dark-accent-strong: #9bd0ff;
}

/* Match the document canvas to the saved theme before the body is painted. */
html[data-theme="light"] { background-color: #f7faff; }
html[data-theme="dark"] { background-color: #08111f; }

.theme-toggle {
  min-height: 44px;
  padding: 8px 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid var(--line, #d5e2ef);
  border-radius: 8px;
  background: transparent;
  color: var(--text, #162d49);
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
}
.theme-toggle:hover { border-color: var(--blue, #2466b3); color: var(--blue, #2466b3); }
.theme-toggle-icon { width: 18px; font-size: 19px; line-height: 1; text-align: center; }
.theme-toggle-floating { position: fixed; right: 18px; bottom: 18px; z-index: 100; background: white; box-shadow: 0 8px 28px #10284826; }

html[data-theme="dark"] {
  color-scheme: dark;
  --navy: #78baff !important;
  --blue: #78baff !important;
  --ink: #edf6ff !important;
  --paper: #edf6ff !important;
  --text: #edf6ff !important;
  --bg: #08111f !important;
  --night: #08111f !important;
  --night-2: #0f1d2f !important;
  --panel: #0f1d2f !important;
  --panel-2: #15263c !important;
  --panel-strong: #15263c !important;
  --light: #1a304b !important;
  --muted: #aec2d7 !important;
  --mist: #aec2d7 !important;
  --line: #2a4565 !important;
  --line-strong: #4f7195 !important;
  --aqua: #78baff !important;
  --aqua-2: #9bd0ff !important;
  --cyan: #78baff !important;
  --gold: #f2c877 !important;
  --shadow: 0 12px 32px #0007 !important;
}

html[data-theme="dark"] body,
html[data-theme="dark"] body.interior-page,
html[data-theme="dark"] body.stats-page,
html[data-theme="dark"] body.economy-page,
html[data-theme="dark"] .panel-shell {
  background: var(--theme-dark-bg) !important;
  color: var(--theme-dark-text) !important;
}
html[data-theme="dark"] body::before,
html[data-theme="dark"] body::after { opacity: .22; }
html[data-theme="dark"] a { color: inherit; }
html[data-theme="dark"] .header,
html[data-theme="dark"] .wn-header,
html[data-theme="dark"] .topbar {
  background: rgb(8 17 31 / 96%) !important;
  border-color: var(--theme-dark-line) !important;
}
html[data-theme="dark"] .alpha-bar,
html[data-theme="dark"] .wn-alpha-bar { background: #050b14 !important; color: #d8ebff !important; }
html[data-theme="dark"] footer,
html[data-theme="dark"] .wn-footer,
html[data-theme="dark"] .site-footer { background: #091423 !important; border-color: var(--theme-dark-line) !important; }
html[data-theme="dark"] .brand,
html[data-theme="dark"] .wn-brand,
html[data-theme="dark"] .panel-shell label { color: var(--theme-dark-text) !important; }
html[data-theme="dark"] .brand small,
html[data-theme="dark"] .wn-brand small { color: var(--theme-dark-muted) !important; }
html[data-theme="dark"] .dropdown > div,
html[data-theme="dark"] .wn-dropdown > div { background: var(--theme-dark-elevated) !important; border-color: var(--theme-dark-line) !important; box-shadow: var(--shadow) !important; }
html[data-theme="dark"] .dropdown a:hover,
html[data-theme="dark"] .wn-dropdown a:hover { background: var(--theme-dark-soft) !important; }

html[data-theme="dark"] :where(
  .join-box,.quick-section,.realm-card,.faq-box,.guide-card,.vote-grid a,
  .profile-card,.leaderboard-card,.stats-live-card,.find-player-card,.join-mini-card,
  .catalog-table-wrap,.catalog-search,.metric-navigation,.realm-tabs,.player-hero,
  .profile-overview article,.economy-page .realm-card,.map-card,.login-card,.stats,
  .panel-card,.policy-shell .hero,.success-shell .hero,.connection-box span,
  .realm-jump,.address-copy,.address-row code,.secondary-action,.live-card,.live-meta span,
  .hero-realm-card,.join-card,.edition-mark,.tool-card,.tool-card > span,
  .alpha-card,.coming-game-card,.faq-list details,.step-row article,
  .realm-sidebar details,.battle-highlight,.game-list article
) {
  background: var(--theme-dark-surface) !important;
  border-color: var(--theme-dark-line) !important;
  color: var(--theme-dark-text) !important;
  box-shadow: var(--shadow) !important;
}
html[data-theme="dark"] :where(
  .page-hero,.stats-hero,.economy-hero,.first-steps,.community-section,.compact-cta,
  .join-strip,.realms-section,.atlas-section,.tools-section,.values-section,
  .faq-section,.alpha-section,.final-cta,.realm-section,.vote-section,.guide-section
) { background: var(--theme-dark-bg) !important; border-color: var(--theme-dark-line) !important; }
html[data-theme="dark"] :where(.hero-picture,.realm-art,.realm-visual,.map-intro,.item-icon-frame,.record-sigil) {
  background-color: var(--theme-dark-soft) !important;
  border-color: var(--theme-dark-line) !important;
}
html[data-theme="dark"] .realm-record,
html[data-theme="dark"] .realm-record::before { background: var(--theme-dark-surface) !important; }
html[data-theme="dark"] .realm-record h2,
html[data-theme="dark"] .coming-game-card h3,
html[data-theme="dark"] .realm-sidebar summary,
html[data-theme="dark"] .realm-sidebar nav strong,
html[data-theme="dark"] .battle-highlight h3,
html[data-theme="dark"] .game-list h3 { color: var(--theme-dark-text) !important; }
html[data-theme="dark"] .realm-sidebar nav a:hover,
html[data-theme="dark"] .realm-sidebar nav a:focus-visible { background: var(--theme-dark-soft) !important; color: var(--theme-dark-accent-strong) !important; }
html[data-theme="dark"] .realm-sidebar nav small { color: var(--theme-dark-muted) !important; }
html[data-theme="dark"] .battle-highlight-icon { background: #4f9ee8 !important; }
html[data-theme="dark"] :where(.realm-record p:not(.card-label),.realm-body p,.faq-box p,.history-note,.board-meta,.catalog-meta,.sub,.muted) {
  color: var(--theme-dark-muted) !important;
}
html[data-theme="dark"] :where(.category,.realm-record li,.realm-copy li,.catalog-filters button,.time-filters button.active,.realm-tabs a.active,.metric-navigation button.active) {
  background: var(--theme-dark-soft) !important;
  color: var(--theme-dark-accent-strong) !important;
  border-color: var(--theme-dark-line) !important;
}
html[data-theme="dark"] :where(input,select,textarea,.find-player-card input,.panel-shell input,.panel-shell select,.panel-shell textarea) {
  background: #0a1727 !important;
  border-color: var(--theme-dark-line) !important;
  color: var(--theme-dark-text) !important;
}
html[data-theme="dark"] :where(input,textarea)::placeholder { color: #8199b1 !important; opacity: 1; }
html[data-theme="dark"] :where(table,thead,tbody,tr,th,td) { border-color: var(--theme-dark-line) !important; }
html[data-theme="dark"] :where(.leaderboard-table thead th,.profile-rankings thead th,.catalog-table thead th) {
  background: var(--theme-dark-elevated) !important;
  color: var(--theme-dark-muted) !important;
}
html[data-theme="dark"] :where(.leaderboard-table tbody tr:hover,.profile-rankings tbody tr:hover,.catalog-table tbody tr:hover,.player-results a:hover) {
  background: var(--theme-dark-soft) !important;
}
html[data-theme="dark"] :where(.primary-action,.wn-button,.button,.navigation-menu > .button,.panel-shell button:not(.ghost)) {
  background: #4f9ee8 !important;
  border-color: #4f9ee8 !important;
  color: #06101d !important;
}
html[data-theme="dark"] :where(.primary-action:hover,.wn-button:hover,.button:hover,.navigation-menu > .button:hover) { background: #78baff !important; }
html[data-theme="dark"] :where(
  .filters button,
  .catalog-filters button,
  .time-filters button,
  .metric-navigation button,
  .realm-tabs a,
  .catalog-pager button
) {
  background: var(--theme-dark-elevated) !important;
  border-color: var(--theme-dark-line) !important;
  color: var(--theme-dark-muted) !important;
  box-shadow: none !important;
}
html[data-theme="dark"] :where(
  .filters button:hover,
  .catalog-filters button:hover:not(:disabled),
  .time-filters button:hover,
  .metric-navigation button:hover,
  .realm-tabs a:hover,
  .catalog-pager button:hover:not(:disabled)
) {
  background: var(--theme-dark-soft) !important;
  border-color: var(--theme-dark-accent) !important;
  color: var(--theme-dark-accent-strong) !important;
}
html[data-theme="dark"] :where(
  .filters button[aria-pressed="true"],
  .catalog-filters button.active,
  .time-filters button.active,
  .metric-navigation button.active,
  .realm-tabs a.active
) {
  background: #4f9ee8 !important;
  border-color: #78baff !important;
  color: #06101d !important;
  box-shadow: 0 4px 14px rgb(79 158 232 / 18%) !important;
}
html[data-theme="dark"] .catalog-filters button.active,
html[data-theme="dark"] .time-filters button.active,
html[data-theme="dark"] .metric-navigation button.active,
html[data-theme="dark"] .realm-tabs a.active {
  background: #4f9ee8 !important;
  border-color: #78baff !important;
  color: #06101d !important;
}
html[data-theme="dark"] .catalog-pager button:disabled {
  background: #101d2d !important;
  border-color: #203a57 !important;
  color: #6f879e !important;
}
html[data-theme="dark"] .catalog-filters {
  background: var(--theme-dark-surface) !important;
  border-color: var(--theme-dark-line) !important;
}
html[data-theme="dark"] .economy-page .realm-card {
  background: var(--theme-dark-surface) !important;
  border-color: var(--theme-dark-line) !important;
  color: var(--theme-dark-text) !important;
}
html[data-theme="dark"] .panel-shell button.ghost,
html[data-theme="dark"] .menu-button,
html[data-theme="dark"] .wn-menu-button { background: var(--theme-dark-elevated) !important; color: var(--theme-dark-text) !important; border-color: var(--theme-dark-line) !important; }
html[data-theme="dark"] .panel-shell .terminal { background: #050b14 !important; color: #e8f4ff !important; }
html[data-theme="dark"] :where(.state,.pill,.brand-mark) { background: var(--theme-dark-elevated) !important; border-color: var(--theme-dark-line) !important; }
html[data-theme="dark"] .theme-toggle { color: var(--theme-dark-text); border-color: var(--theme-dark-line); background: var(--theme-dark-elevated); }
html[data-theme="dark"] .theme-toggle:hover { border-color: var(--theme-dark-accent); color: var(--theme-dark-accent-strong); }
html[data-theme="dark"] .theme-toggle-floating { background: var(--theme-dark-elevated); }
html[data-theme="dark"] .map-card iframe { background: var(--theme-dark-soft) !important; }
html[data-theme="dark"] .toast { background: #4f9ee8 !important; color: #06101d !important; }
html[data-theme="dark"] .skip,
html[data-theme="dark"] .skip-link { background: var(--theme-dark-elevated) !important; color: var(--theme-dark-text) !important; }

@media (max-width: 760px) {
  .navigation-menu .theme-toggle,
  .wn-navigation-menu .theme-toggle { width: 100%; justify-content: flex-start; margin-top: 4px; padding: 12px; }
  html[data-theme="dark"] .dropdown > div,
  html[data-theme="dark"] .wn-dropdown > div { background: #0b1727 !important; box-shadow: none !important; }
}

@media (prefers-reduced-motion: reduce) {
  .theme-toggle { transition: none; }
}
