/* Shared color system and toggle controls. Loaded after each page's own styles. */
:root:not([data-theme="dark"]) {
  color-scheme: light;
  --void: #f0ece2;
  --void-2: #fbf8f1;
  --bg: #f0ece2;
  --panel: #fffcf6;
  --panel-2: #f3eee4;
  --page-bg: #f0ece2;
  --theme-toggle-bg: rgba(255, 252, 246, .96);
  --theme-toggle-fg: #2d332e;
  --theme-toggle-border: rgba(45, 51, 46, .2);
  --theme-toggle-shadow: 0 5px 18px rgba(45, 51, 46, .13);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --void: #181b18;
  --void-2: #222721;
  --sand: #ece9e0;
  --sand-dim: #b4b8ac;
  --clay: #df8964;
  --clay-deep: #a95736;
  --verdant: #88b99c;
  --crimson: #df7777;
  --gold: #e0bd7b;
  --hair: rgba(236, 233, 224, .17);

  --bg: #181b18;
  --panel: #222721;
  --panel-2: #2b302a;
  --ink: #ece9e0;
  --muted: #b4b8ac;
  --line: #41483f;
  --shadow: 0 18px 45px rgba(0, 0, 0, .34);

  --page-bg: #181b18;
  --card-bg: rgba(34, 39, 33, .97);
  --accent-gold: #e0bd7b;
  --accent-blue: #88b99c;
  --text-main: #ece9e0;
  --text-muted: #b4b8ac;
  --border-color: #41483f;

  --theme-toggle-bg: rgba(34, 39, 33, .96);
  --theme-toggle-fg: #ece9e0;
  --theme-toggle-border: rgba(236, 233, 224, .24);
  --theme-toggle-shadow: 0 6px 20px rgba(0, 0, 0, .34);
}

.theme-toggle {
  position: fixed;
  z-index: 10000;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  padding: 8px 13px;
  border: 1px solid var(--theme-toggle-border);
  border-radius: 999px;
  background: var(--theme-toggle-bg);
  color: var(--theme-toggle-fg);
  box-shadow: var(--theme-toggle-shadow);
  font: 600 .78rem/1.15 system-ui, sans-serif;
  white-space: nowrap;
  cursor: pointer;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  transition: transform .18s ease, background-color .18s ease, border-color .18s ease;
}

.theme-toggle:hover {
  border-color: var(--gold, var(--accent-gold, #875d20));
  transform: translateY(-1px);
}

.theme-toggle:focus-visible {
  outline: 3px solid var(--gold, var(--accent-gold, #875d20));
  outline-offset: 3px;
}

.theme-toggle-icon {
  font-size: 1rem;
  line-height: 1;
}

.theme-toggle--header {
  position: static;
  z-index: auto;
  top: auto;
  right: auto;
  margin-left: 8px;
  flex: 0 0 auto;
  box-shadow: none;
}

:root[data-theme="dark"] ::selection {
  background: #a95736;
  color: #fff;
}

/* Main story surfaces. The light background is intentionally a shade deeper
   than the paper-white cards, so the page feels softer without looking dark. */
:root[data-theme="dark"] .hero-art {
  border-color: var(--hair);
  box-shadow: 0 26px 60px rgba(0, 0, 0, .34);
}

:root[data-theme="dark"] .hero-art figcaption {
  border-color: var(--hair);
  background: rgba(34, 39, 33, .9);
  color: var(--sand-dim);
}

:root[data-theme="dark"] .cards a {
  background: rgba(34, 39, 33, .82);
}

:root[data-theme="dark"] body:not([data-page]) .btn {
  background: #875d20;
  color: #fffdf8;
}

:root[data-theme="dark"] body:not([data-page]) .btn:hover {
  background: #a16f27;
}

:root[data-theme="dark"] #lat-merc {
  mix-blend-mode: screen;
  opacity: .62;
}

/* Connected experience surfaces, controls, and data visualizations. */
:root[data-theme="dark"] input[type="date"],
:root[data-theme="dark"] input[type="time"],
:root[data-theme="dark"] select,
:root[data-theme="dark"] #copytext {
  border-color: var(--line);
  background-color: var(--panel);
  color: var(--ink);
  color-scheme: dark;
}

:root[data-theme="dark"] .control,
:root[data-theme="dark"] .visual,
:root[data-theme="dark"] .humanity-strip,
:root[data-theme="dark"] .share-panel,
:root[data-theme="dark"] .cards a {
  background: linear-gradient(180deg, rgba(255, 255, 255, .035), rgba(255, 255, 255, 0)), var(--panel);
  border-color: var(--line);
  box-shadow: var(--shadow);
}

:root[data-theme="dark"] .insight,
:root[data-theme="dark"] .visual-card,
:root[data-theme="dark"] .time-card {
  border-color: rgba(224, 189, 123, .22);
  background: rgba(255, 255, 255, .045);
}

:root[data-theme="dark"] .stat-pill,
:root[data-theme="dark"] .explanation-badge {
  border-color: rgba(224, 189, 123, .28);
  background: rgba(255, 255, 255, .055);
  color: var(--ink);
}

:root[data-theme="dark"] .branch-track,
:root[data-theme="dark"] .scale-track,
:root[data-theme="dark"] .time-beam {
  background-color: rgba(236, 233, 224, .14);
}

:root[data-theme="dark"] .share-link {
  border-color: rgba(224, 189, 123, .32);
  background: rgba(224, 189, 123, .1);
  color: var(--ink);
}

:root[data-theme="dark"] .share-link:hover {
  background: rgba(224, 189, 123, .18);
  color: var(--ink);
}

:root[data-theme="dark"] .planet-africa {
  background: rgba(223, 137, 100, .92);
}

:root[data-theme="dark"] .share-btn:hover {
  color: #181b18;
}

:root[data-theme="dark"] #map-svg path {
  stroke: rgba(236, 233, 224, .42);
}

:root[data-theme="dark"] .piece-label {
  fill: #fffdf8;
  stroke: var(--void);
}

:root[data-theme="dark"] body[data-page]:not([data-page="ancestor"]) button:not(.secondary):not(.theme-toggle):not(.celebrate-button) {
  border-color: #875d20;
  background: #875d20;
  color: #fffdf8;
}

:root[data-theme="dark"] body[data-page]:not([data-page="ancestor"]) button:not(.secondary):not(.theme-toggle):not(.celebrate-button):hover {
  border-color: #a16f27;
  background: #a16f27;
}

:root[data-theme="dark"] button.celebrate-button {
  border-color: rgba(223, 137, 100, .62);
  background: rgba(223, 137, 100, .13);
  color: #f0a98a;
}

:root[data-theme="dark"] button.celebrate-button:hover {
  border-color: var(--clay);
  background: rgba(223, 137, 100, .2);
  color: #ffd0b8;
}

/* Alternate ancestor story. */
:root[data-theme="dark"] .main-container {
  box-shadow: 0 24px 70px rgba(0, 0, 0, .38);
}

:root[data-theme="dark"] .modal-overlay {
  background: rgba(0, 0, 0, .72);
}

:root[data-theme="dark"] select.region-select {
  background: var(--panel);
  color: var(--text-main);
  color-scheme: dark;
}

:root[data-theme="dark"] .ancestor-result-card {
  background: #2b302a;
}

:root[data-theme="dark"] .aphorism-card {
  background: rgba(255, 255, 255, .045);
}

@media (max-width: 520px) {
  .theme-toggle {
    min-height: 38px;
    padding: 7px 11px;
    font-size: .73rem;
  }
}

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