/* ─────────────────────────────────────────────────────────────────────────
   Day / night theme switch.

   Rebuilt from the reference recording: a pill track holding two full-bleed
   scenes that cross-fade, and a knob that slides between them.

     Day    — blue sky gradient, white cumulus stacked bottom-right,
              yellow sun as the knob on the left.
     Night  — near-black sky banded lighter towards the right, small
              four-point stars, grey cratered moon as the knob on the right.

   Both scenes are always rendered and cross-faded rather than swapped, which
   is what the recording shows mid-transition: sun and moon are briefly both
   visible. Only transform and opacity animate, so the whole thing stays on
   the compositor.

   Sizing is driven by --ts-h; everything else derives from it, so the control
   can be dropped in at another size without re-tuning.
   ───────────────────────────────────────────────────────────────────────── */

.theme-switch {
  --ts-h: 32px;
  --ts-w: calc(var(--ts-h) * 2.05);
  --ts-pad: calc(var(--ts-h) * 0.09);
  --ts-knob: calc(var(--ts-h) - var(--ts-pad) * 2);
  --ts-travel: calc(var(--ts-w) - var(--ts-knob) - var(--ts-pad) * 2);

  position: relative;
  flex: none;
  width: var(--ts-w);
  height: var(--ts-h);
  padding: 0;
  border: 0;
  background: none;
  border-radius: 999px;
  cursor: pointer;
  overflow: hidden;
  isolation: isolate;
  -webkit-tap-highlight-color: transparent;

  /* The recording shows the track pressed into the surface — a dark inner rim
     at the top with light spill at the bottom, not a flat fill. */
  box-shadow:
    inset 0 2px 4px rgba(0, 0, 0, 0.45),
    inset 0 -1px 2px rgba(255, 255, 255, 0.22),
    0 1px 2px rgba(0, 0, 0, 0.25);

  transition: transform 120ms cubic-bezier(0.23, 1, 0.32, 1);
}

.theme-switch:active { transform: scale(0.96); }

/* On a light header the near-black rim reads as a hole punched in the page.
   The reference sits the control on a light surface, where the depth comes
   from a soft outer bevel rather than a hard dark inset. */
:root.light-mode .theme-switch {
  box-shadow:
    inset 0 2px 4px rgba(16, 16, 18, 0.16),
    inset 0 -1px 2px rgba(255, 255, 255, 0.5),
    0 1px 2px rgba(16, 16, 18, 0.1),
    0 0 0 1px rgba(16, 16, 18, 0.08);
}

:root.light-mode .theme-switch:focus-visible {
  box-shadow:
    inset 0 2px 4px rgba(16, 16, 18, 0.16),
    0 0 0 3px rgba(8, 127, 145, 0.55);
}

.theme-switch:focus-visible {
  outline: none;
  box-shadow:
    inset 0 2px 4px rgba(0, 0, 0, 0.45),
    0 0 0 3px rgba(0, 212, 255, 0.55);
}

/* ─── Scenes ──────────────────────────────────────────────────────────── */

.ts-scene {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  transition: opacity 260ms cubic-bezier(0.23, 1, 0.32, 1);
}

/* Day: deeper blue top-left falling to a paler horizon, as in the reference. */
.ts-day {
  background:
    linear-gradient(160deg, #4b93d8 0%, #64a8e4 38%, #8fc6ef 78%, #b7ddf6 100%);
  opacity: 0;
}

/* Night: banded rather than a smooth ramp — the reference steps from near
   black on the left through progressively lighter greys towards the knob. */
.ts-night {
  background:
    linear-gradient(90deg,
      #1a1b24 0%, #1a1b24 26%,
      #2a2c36 26%, #2a2c36 46%,
      #383a46 46%, #383a46 64%,
      #474a58 64%, #474a58 82%,
      #565a6a 82%, #565a6a 100%);
  opacity: 1;
}

.theme-switch[aria-checked="true"] .ts-day { opacity: 1; }
.theme-switch[aria-checked="true"] .ts-night { opacity: 0; }

/* The SVG scene art fills its layer. */
.ts-scene svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

/* ─── Knob ────────────────────────────────────────────────────────────── */

.ts-knob {
  position: absolute;
  top: var(--ts-pad);
  left: var(--ts-pad);
  width: var(--ts-knob);
  height: var(--ts-knob);
  border-radius: 50%;
  z-index: 2;

  /* Sun sits left in day, moon right in night — so the resting position is
     the right-hand end while dark mode is active. */
  transform: translateX(var(--ts-travel));

  /* A drawer-style curve makes the throw read as physical rather than
     mechanical: quick to leave, settling softly. */
  transition: transform 300ms cubic-bezier(0.32, 0.72, 0, 1);
}

.theme-switch[aria-checked="true"] .ts-knob { transform: translateX(0); }

.ts-face {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  transition: opacity 220ms cubic-bezier(0.23, 1, 0.32, 1);
}

/* Sun — golden disc with a soft outer glow, per the reference. */
.ts-sun {
  background: radial-gradient(circle at 38% 34%, #ffe27a 0%, #fdd045 42%, #f5bd23 100%);
  box-shadow:
    0 0 6px rgba(255, 208, 70, 0.75),
    inset 0 -1px 2px rgba(180, 120, 0, 0.35);
  opacity: 0;
}

/* Moon — cool grey disc; craters are drawn by the SVG child. */
.ts-moon {
  background: radial-gradient(circle at 36% 32%, #eef0f4 0%, #d3d7df 55%, #b9bec9 100%);
  box-shadow:
    0 1px 3px rgba(0, 0, 0, 0.45),
    inset 0 -1px 2px rgba(90, 95, 110, 0.4);
  opacity: 1;
  overflow: hidden;
}

.theme-switch[aria-checked="true"] .ts-sun { opacity: 1; }
.theme-switch[aria-checked="true"] .ts-moon { opacity: 0; }

.ts-moon svg { position: absolute; inset: 0; width: 100%; height: 100%; }

/* ─── Hover ───────────────────────────────────────────────────────────── */
/* The reference has a distinct hover frame where the knob nudges toward the
   direction it would travel. Gated on pointer capability so touch, which
   fires a synthetic hover on tap, never triggers it. */

@media (hover: hover) and (pointer: fine) {
  .theme-switch:hover .ts-knob {
    transform: translateX(calc(var(--ts-travel) - var(--ts-h) * 0.09));
  }
  .theme-switch[aria-checked="true"]:hover .ts-knob {
    transform: translateX(calc(var(--ts-h) * 0.09));
  }
}

/* ─── Reduced motion ──────────────────────────────────────────────────── */
/* Position carries the state, so the knob still moves — it just arrives
   without a travelled feel. The scene cross-fade is opacity only and carries
   no vestibular risk, so it stays. */

@media (prefers-reduced-motion: reduce) {
  .ts-knob { transition-duration: 1ms !important; }
  .ts-scene,
  .ts-face { transition-duration: 160ms !important; }
  .theme-switch:active { transform: none; }
  .theme-switch:hover .ts-knob { transform: translateX(var(--ts-travel)); }
  .theme-switch[aria-checked="true"]:hover .ts-knob { transform: translateX(0); }
}
