/* =============================================================================
   TerraSight public website: theme plumbing that is always on.

   Loaded by templates/base.html after style.css on every page EXCEPT the
   editor (request.endpoint == 'editor_page'), which has its own light/dark
   theming in frontend/src/styles.css.

   The dark theme ("espresso") itself lives in style-dark-theme.css. Its
   <link id="ts-dark-theme"> gets a media attribute from the inline script in
   base.html, so the theme follows the visitor's choice from the switch under
   the background picker (site-background.js):
     - no choice stored  -> media="not all": light, even on a dark device.
                            Only "light" is declared, so a browser that forces
                            dark (Safari's Adaptive Dark Mode) may darken it.
     - "dark" chosen     -> media="all"
   <html> carries data-site-theme="light|dark" (what is shown) and
   data-site-theme-pref="auto|dark" (what was chosen).

   Structure:
     1. Scheme declaration + light defaults of the shared tokens
     2. Theme switch (light look; its dark look is in style-dark-theme.css)
     3. @media (inverted-colors: inverted): re-invert media for iOS Smart Invert
   ========================================================================== */

/* — 1. Scheme declaration ————————————————————————————————————————————————— */

/* Light by default, mirroring <meta name="color-scheme" content="light"> in
   base.html: a dark device alone does not turn the site dark, and native
   controls stay light. A browser that forces dark may still darken it. */
:root {
  color-scheme: light;

  /* Site background canvas (static/site-background.js reads these with
     getComputedStyle; INK/ORANGE are "r, g, b" triplets for rgba()). */
  --bg-canvas-paper: #f7f3ec;
  --bg-canvas-ground-shadow: #e8e0d1;
  --bg-canvas-roof: #fbf9f4;
  --bg-canvas-roof-shadow: #eee8dc;
  --bg-canvas-ink: 17, 17, 17;
  --bg-canvas-orange: 191, 81, 43;

  /* Tokens introduced with the dark theme, given their light equivalents so
     a rule may reference them unconditionally. */
  --color-text-muted: #7d766c;
  --color-surface-elevated: #ffffff;
  --color-on-accent: #ffffff;
  --color-header-bg: #fdfbf8;
  --color-link: #bf512b;
  --color-focus: #bf512b;
  --color-field-bg: #ffffff;
  --color-field-border: var(--hairline-strong);
  --color-placeholder: #8f8676;
  --color-success: #2d5a27;
  --color-warning: #8a5a00;
  --color-danger: #b42318;
  --color-backdrop: rgba(17, 17, 17, 0.45);
  --shadow-elevated: 0 0 0 1px var(--hairline), 0 10px 30px rgba(58, 44, 24, 0.14);
}

/* Dark canvas palette. Keyed on the attribute the inline script sets, not on
   style-dark-theme.css: a <link media> change applies a frame late, and
   site-background.js reads these right when the theme changes. */
:root[data-site-theme="dark"] {
  --bg-canvas-paper: #15130f;
  --bg-canvas-ground-shadow: #0f0d0a;
  --bg-canvas-roof: #1e1b16;
  --bg-canvas-roof-shadow: #191611;
  --bg-canvas-ink: 246, 236, 220;
  --bg-canvas-orange: 226, 122, 82;
}

/* Dark chosen: native controls and scrollbars go dark too, and browsers that
   force dark see the page has its own dark design and leave it alone. */
:root[data-site-theme-pref="dark"] {
  color-scheme: dark;
}

/* — 2. Theme switch ———————————————————————————————————————————————————————
   Sits under the background picker in #site-bg-switch, as its own control. */

/* No text label: the sun / moon switch speaks for itself (aria-label and
   title still name it). */
#site-bg-switch .site-theme-switch {
  display: flex;
  align-items: center;
  margin-top: 6px;
  padding: 7px;
  background: #fff;
  border: 1px solid var(--hairline-strong);
  border-radius: 8px;
  box-shadow: var(--shadow-soft);
}

#site-bg-switch .site-theme-switch:focus-visible {
  outline: 2px solid var(--color-focus);
  outline-offset: 2px;
}

.site-theme-switch__track {
  position: relative;
  flex: none;
  width: 34px;
  height: 20px;
  border-radius: 999px;
  background: var(--color-surface-soft);
  box-shadow: inset 0 0 0 1px var(--hairline-strong);
  transition: background-color 0.2s ease;
}

.site-theme-switch__knob {
  position: absolute;
  top: 2px;
  left: 2px;
  display: grid;
  place-items: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  color: var(--color-accent);
  box-shadow: 0 1px 2px rgba(58, 44, 24, 0.25);
  transition: transform 0.2s ease;
}

.site-theme-switch__knob svg {
  width: 11px;
  height: 11px;
}

.site-theme-switch__moon {
  display: none;
}

.site-theme-switch[aria-checked="true"] .site-theme-switch__track {
  background: var(--color-accent);
  box-shadow: none;
}

.site-theme-switch[aria-checked="true"] .site-theme-switch__knob {
  transform: translateX(14px);
}

.site-theme-switch[aria-checked="true"] .site-theme-switch__sun {
  display: none;
}

.site-theme-switch[aria-checked="true"] .site-theme-switch__moon {
  display: block;
}

@media (prefers-reduced-motion: reduce) {
  .site-theme-switch__track,
  .site-theme-switch__knob {
    transition: none;
  }
}

/* Phones: a square sun / moon button as small as the picker's icon button,
   so it stays clear of the page content beside it. */
@media (max-width: 768px) {
  #site-bg-switch .site-theme-switch {
    margin-top: 4px;
    padding: 7px;
  }

  /* #site-bg-switch: beats the dark knob colours in style-dark-theme.css. */
  #site-bg-switch .site-theme-switch__track,
  #site-bg-switch .site-theme-switch[aria-checked="true"] .site-theme-switch__track {
    width: 18px;
    height: 18px;
    background: none;
    box-shadow: none;
  }

  #site-bg-switch .site-theme-switch__knob,
  #site-bg-switch .site-theme-switch[aria-checked="true"] .site-theme-switch__knob {
    position: static;
    width: 18px;
    height: 18px;
    background: none;
    box-shadow: none;
    transform: none;
    color: var(--color-text-strong);
  }

  .site-theme-switch__knob svg {
    width: 16px;
    height: 16px;
  }
}

/* — 3. iOS Smart Invert ————————————————————————————————————————————————————
   Smart Invert flips the whole page. Flip photos, video, maps and canvases
   back so they look natural. Only leaf media are flipped (never a container,
   or its children would flip twice): MapLibre draws tiles into a <canvas>
   (.maplibregl-canvas) and map snapshots/results are <img>, so both are
   covered while the map controls stay inverted with the rest of the UI.
   The logo SVGs and the decorative site background canvas are UI and stay
   inverted with the page (their dark-mode recolour still applies). */
@media (inverted-colors: inverted) {
  img:not(.logo-img):not(.logo-text img),
  video,
  iframe,
  canvas:not(#site-bg-canvas) {
    filter: invert(1) hue-rotate(180deg);
  }
}
