/* The theme switch (lib/theme.php sg_theme_switch, user/assets/theme.js):
   Light / Auto / Dark on one day-and-night track, after the Uiverse.io switch by
   Galahhad. It is drawn from <html>'s data-theme (the sky) and
   data-theme-choice (where the sun is), so it is right from the first paint;
   theme_switch.js gives it its hand feel.

   One file for the dashboard and the extension (its tools/sync-kit.mjs copies this one). On its own
   (the welcome tour) it is 210px wide; at the foot of a sidebar it takes the sidebar's width. */

/* One track, three stops: Light | Auto | Dark, each an icon and a word. The
   track is the sky: a blue day with clouds along the bottom, a navy night with
   stars. The sun, in its halo, sits on the chosen stop's icon; at night the
   moon slides over it. Under Auto it sits on the middle stop and the sky shows
   what the device gave. 30px high.

   Under Auto the sky tells the time of day: dawn, morning, noon, golden hour,
   dusk, night, by the clock (theme.js's sky()).

   Everything is a function of a few numbers on the switch: --tt-pos, where the
   sun is in stops (0 Light, 1 Auto, 2 Dark); --tt-night, how much night the
   sky shows (0 to 1); --tt-glow, the warm light of dawn and dusk; --tt-rise,
   how high the sun or moon stands; --tt-warm, how golden the sun is. The
   stylesheet sets --tt-pos from <html data-theme-choice>, with a spring when
   it changes (a keyboard pick, another tab), and the sky from <html data-theme>
   as a fallback; theme_switch.js sets the sky each minute from the clock, takes
   everything over while the sun is pressed or dragged (.is-live), driving it
   each frame, and hands back once it has settled. Changes fade over half a
   second. Decorations are sized in em of the switch's 12px. */
@property --tt-pos { syntax: "<number>"; inherits: true; initial-value: 0; }
@property --tt-night { syntax: "<number>"; inherits: true; initial-value: 0; }
@property --tt-lean { syntax: "<length>"; inherits: true; initial-value: 0px; }
@property --tt-glow { syntax: "<number>"; inherits: true; initial-value: 0; }
@property --tt-rise { syntax: "<number>"; inherits: true; initial-value: 1; }
@property --tt-warm { syntax: "<number>"; inherits: true; initial-value: 0; }
.theme-switch {
  --tt-pos: 0; --tt-night: 0; --tt-glow: 0; --tt-rise: 1; --tt-warm: 0; --tt-lean: 0px;
  /* The clouds blush at dawn and dusk. */
  --tt-cloud: color-mix(in oklab, #f3fdff, #ffc3a0 calc(var(--tt-glow) * 70%));
  --tt-cloud-back: color-mix(in oklab, #aacadf, #e59a86 calc(var(--tt-glow) * 70%));
  --tt-knob-x: 0px;                      /* the sun's offset within its stop */
  --tt-spring: linear(0, 0.031, 0.114, 0.231, 0.367, 0.51, 0.649, 0.778, 0.89, 0.983, 1.055, 1.108, 1.142, 1.159, 1.163, 1.156, 1.141, 1.122, 1.099, 1.076, 1.053, 1.033, 1.015, 1, 0.989, 0.981, 0.976, 0.974, 0.974, 0.975, 0.977, 0.981, 0.985, 0.988, 0.992, 0.995, 0.998, 1, 1.002, 1.003, 1);
  position: relative; display: flex; flex: none; box-sizing: border-box; width: 210px; padding: 3px; overflow: hidden; isolation: isolate;
  border-radius: 999px; background-color: #3d7eae; font-size: 12px;
  box-shadow: 0 -.06em .06em rgba(0, 0, 0, .25), 0 .06em .12em rgba(255, 255, 255, .6);
  touch-action: pan-y; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  transition: --tt-pos .7s var(--tt-spring), --tt-night .5s cubic-bezier(.3, .7, .2, 1), --tt-glow .5s cubic-bezier(.3, .7, .2, 1), --tt-rise .6s cubic-bezier(.3, .7, .2, 1), --tt-warm .5s cubic-bezier(.3, .7, .2, 1), --tt-lean .35s cubic-bezier(.3, 1.4, .5, 1), transform .18s cubic-bezier(.3, 1.2, .5, 1);
}
.theme-switch.is-live { transition: none; }
/* Pressed, the whole track gives a little. */
.theme-switch.is-pressed { transform: scale(.985); }
.theme-switch.is-pressed, .theme-switch.is-pressed * { cursor: grabbing !important; }
/* The track's inner shadow, over the sky and under the sun. */
.theme-switch::before { position: absolute; z-index: 1; inset: 0; border-radius: inherit; box-shadow: inset 0 .05em .19em rgba(0, 0, 0, .25), inset 0 .05em .19em rgba(0, 0, 0, .25); content: ""; pointer-events: none; }
.theme-switch > span, .theme-switch .tt-sky > span, .theme-switch .tt-knob, .theme-switch .tt-moon { position: absolute; display: block; box-sizing: border-box; pointer-events: none; }
.theme-switch .tt-sky { inset: 0; z-index: 0; }
/* Night, laid over the day as far as --tt-night says, and over that the warm
   light of dawn and dusk, low in the sky. */
.theme-switch .tt-night { inset: 0; background-color: #1d1f2c; opacity: var(--tt-night); }
.theme-switch .tt-glow { inset: 0; background: linear-gradient(to top, #ff8c52 0%, rgba(255, 150, 105, .55) 42%, rgba(255, 150, 105, 0) 85%); opacity: var(--tt-glow); }

/* Clouds along the bottom, a bank at each end: front puffs in white, back
   puffs in a paler blue, kept low so the words sit above them. They sink out
   of the track as night comes. */
.theme-switch .tt-clouds {
  bottom: calc(-.85em - 3.3em * var(--tt-night)); left: .31em; width: 1.25em; height: 1.25em; border-radius: 50%; background-color: var(--tt-cloud);
  box-shadow: .94em .31em var(--tt-cloud), -.31em -.31em var(--tt-cloud-back), 1.44em .37em var(--tt-cloud), .5em -.12em var(--tt-cloud-back), 2.19em 0 var(--tt-cloud), 1.25em -.06em var(--tt-cloud-back),
    2.94em .31em var(--tt-cloud), 2em -.31em var(--tt-cloud-back), 3.62em -.06em var(--tt-cloud), 2.62em 0 var(--tt-cloud-back), 4.5em -.31em var(--tt-cloud), 3.37em -.44em var(--tt-cloud-back),
    5.25em -.12em var(--tt-cloud), 4em -.62em var(--tt-cloud-back), 4.75em -.75em var(--tt-cloud-back);
}
.theme-switch .tt-clouds-r { left: auto; right: .31em; transform: scaleX(-1); }
/* Stars, a field at each end, coming down into the night. */
.theme-switch .tt-stars {
  top: 50%; left: .31em; width: 2.75em; height: 1.05em; opacity: var(--tt-night); transform: translateY(calc(-50% - 220% * (1 - var(--tt-night))));
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 144 55'%3E%3Cpath fill='white' d='M135.831 3.00688C135.055 3.85027 134.111 4.29946 133 4.35447C134.111 4.40947 135.055 4.85867 135.831 5.71123C136.607 6.55462 136.996 7.56303 136.996 8.72727C136.996 7.95722 137.172 7.25134 137.525 6.59129C137.886 5.93124 138.372 5.39954 138.98 5.00535C139.598 4.60199 140.268 4.39114 141 4.35447C139.88 4.2903 138.936 3.85027 138.16 3.00688C137.384 2.16348 136.996 1.16425 136.996 0C136.996 1.16425 136.607 2.16348 135.831 3.00688ZM31 23.3545C32.1114 23.2995 33.0551 22.8503 33.8313 22.0069C34.6075 21.1635 34.9956 20.1642 34.9956 19C34.9956 20.1642 35.3837 21.1635 36.1599 22.0069C36.9361 22.8503 37.8798 23.2903 39 23.3545C38.2679 23.3911 37.5976 23.602 36.9802 24.0053C36.3716 24.3995 35.8864 24.9312 35.5248 25.5913C35.172 26.2513 34.9956 26.9572 34.9956 27.7273C34.9956 26.563 34.6075 25.5546 33.8313 24.7112C33.0551 23.8587 32.1114 23.4095 31 23.3545ZM0 36.3545C1.11136 36.2995 2.05513 35.8503 2.83131 35.0069C3.6075 34.1635 3.99559 33.1642 3.99559 32C3.99559 33.1642 4.38368 34.1635 5.15987 35.0069C5.93605 35.8503 6.87982 36.2903 8 36.3545C7.26792 36.3911 6.59757 36.602 5.98015 37.0053C5.37155 37.3995 4.88644 37.9312 4.52481 38.5913C4.172 39.2513 3.99559 39.9572 3.99559 40.7273C3.99559 39.563 3.6075 38.5546 2.83131 37.7112C2.05513 36.8587 1.11136 36.4095 0 36.3545ZM56.8313 24.0069C56.0551 24.8503 55.1114 25.2995 54 25.3545C55.1114 25.4095 56.0551 25.8587 56.8313 26.7112C57.6075 27.5546 57.9956 28.563 57.9956 29.7273C57.9956 28.9572 58.172 28.2513 58.5248 27.5913C58.8864 26.9312 59.3716 26.3995 59.9802 26.0053C60.5976 25.602 61.2679 25.3911 62 25.3545C60.8798 25.2903 59.9361 24.8503 59.1599 24.0069C58.3837 23.1635 57.9956 22.1642 57.9956 21C57.9956 22.1642 57.6075 23.1635 56.8313 24.0069ZM81 25.3545C82.1114 25.2995 83.0551 24.8503 83.8313 24.0069C84.6075 23.1635 84.9956 22.1642 84.9956 21C84.9956 22.1642 85.3837 23.1635 86.1599 24.0069C86.9361 24.8503 87.8798 25.2903 89 25.3545C88.2679 25.3911 87.5976 25.602 86.9802 26.0053C86.3716 26.3995 85.8864 26.9312 85.5248 27.5913C85.172 28.2513 84.9956 28.9572 84.9956 29.7273C84.9956 28.563 84.6075 27.5546 83.8313 26.7112C83.0551 25.8587 82.1114 25.4095 81 25.3545ZM136 36.3545C137.111 36.2995 138.055 35.8503 138.831 35.0069C139.607 34.1635 139.996 33.1642 139.996 32C139.996 33.1642 140.384 34.1635 141.16 35.0069C141.936 35.8503 142.88 36.2903 144 36.3545C143.268 36.3911 142.598 36.602 141.98 37.0053C141.372 37.3995 140.886 37.9312 140.525 38.5913C140.172 39.2513 139.996 39.9572 139.996 40.7273C139.996 39.563 139.607 38.5546 138.831 37.7112C138.055 36.8587 137.111 36.4095 136 36.3545ZM101.831 49.0069C101.055 49.8503 100.111 50.2995 99 50.3545C100.111 50.4095 101.055 50.8587 101.831 51.7112C102.607 52.5546 102.996 53.563 102.996 54.7273C102.996 53.9572 103.172 53.2513 103.525 52.5913C103.886 51.9312 104.372 51.3995 104.98 51.0053C105.598 50.602 106.268 50.3911 107 50.3545C105.88 50.2903 104.936 49.8503 104.16 49.0069C103.384 48.1635 102.996 47.1642 102.996 46C102.996 47.1642 102.607 48.1635 101.831 49.0069Z'/%3E%3C/svg%3E") center / contain no-repeat;
}
.theme-switch .tt-stars-r { left: auto; right: .31em; transform: translateY(calc(-50% - 220% * (1 - var(--tt-night)))) scaleX(-1); }

/* The sun, in its halo, and the moon that slides over it. .tt-slot is a third
   of the track, moved to --tt-pos (plus a lean towards a hovered stop); the sun
   sits at its left, on the stop's icon, and above the words it passes over,
   lower in the sky at dawn and dusk (--tt-rise). The sun's own transform is its
   squash and stretch, theme_switch.js's while it moves; it turns golden at
   dawn and dusk (--tt-warm). */
.theme-switch .tt-slot { top: 3px; left: 3px; z-index: 4; width: calc((100% - 6px) / 3); height: 24px; transform: translateX(calc(var(--tt-pos) * 100% + var(--tt-lean))) translateY(calc((1 - var(--tt-rise)) * 4px)); }
.theme-switch .tt-knob {
  top: 0; left: var(--tt-knob-x); width: 24px; height: 24px; overflow: hidden; border-radius: 50%; background-color: color-mix(in oklab, #ecca2f, #f2873a calc(var(--tt-warm) * 100%)); pointer-events: auto; cursor: grab;
  box-shadow: inset .06em .06em .06em 0 rgba(254, 255, 239, .61), inset 0 -.06em .06em 0 #a1872a, 0 0 0 5px rgba(255, 255, 255, .1), 0 0 0 10px rgba(255, 255, 255, .1), 0 0 0 16px rgba(255, 255, 255, .1);
  filter: drop-shadow(.06em .12em .12em rgba(0, 0, 0, .25)) drop-shadow(0 .06em .12em rgba(0, 0, 0, .25));
  transition: box-shadow .25s ease;
}
/* Pressed, the halo brightens around the finger. */
.theme-switch.is-pressed .tt-knob { box-shadow: inset .06em .06em .06em 0 rgba(254, 255, 239, .61), inset 0 -.06em .06em 0 #a1872a, 0 0 0 5px rgba(255, 255, 255, .16), 0 0 0 10px rgba(255, 255, 255, .13), 0 0 0 16px rgba(255, 255, 255, .1); }
.theme-switch .tt-moon {
  inset: 0; border-radius: inherit; background-color: #c7c7ce; transform: translateX(calc(100% * (1 - var(--tt-night))));
  box-shadow: inset .06em .06em .06em 0 rgba(254, 255, 239, .61), inset 0 -.06em .06em 0 #969696;
}
.theme-switch .tt-moon > i { position: absolute; top: .75em; left: .31em; width: .75em; height: .75em; border-radius: 50%; background-color: #9d9da9; box-shadow: inset 0 .03em .06em rgba(0, 0, 0, .25); }
.theme-switch .tt-moon > i:nth-child(2) { top: .94em; left: 1.37em; width: .37em; height: .37em; }
.theme-switch .tt-moon > i:nth-child(3) { top: .31em; left: .81em; width: .25em; height: .25em; }
/* A shine on the sun or moon, which leans into the motion (--tt-tilt, in %). */
.theme-switch .tt-knob::after { position: absolute; inset: 0; border-radius: inherit; background: radial-gradient(circle at calc(38% + var(--tt-tilt, 0) * 1%) 30%, rgba(255, 255, 255, .55), rgba(255, 255, 255, 0) 58%); content: ""; }
:root[data-theme-choice="auto"] .theme-switch { --tt-pos: 1; }
:root[data-theme-choice="dark"] .theme-switch { --tt-pos: 2; }
:root[data-theme="dark"] .theme-switch { --tt-night: 1; --tt-rise: .85; }
/* On Auto the halo breathes: this one is alive, it moves with the clock. */
@keyframes tt-breathe {
  50% { box-shadow: inset .06em .06em .06em 0 rgba(254, 255, 239, .61), inset 0 -.06em .06em 0 #a1872a, 0 0 0 5px rgba(255, 255, 255, .17), 0 0 0 10px rgba(255, 255, 255, .13), 0 0 0 16px rgba(255, 255, 255, .11); }
}
:root[data-theme-choice="auto"] .theme-switch:not(.is-live) .tt-knob { animation: tt-breathe 4.5s ease-in-out infinite; }

/* The stops: an icon slot the size of the sun, then the word, in white on the
   sky. A stop's icon fades as the sun comes over it, and its word brightens. */
.theme-switch button[data-theme-set] {
  all: unset; position: relative; z-index: 3; display: inline-flex; flex: 1; align-items: center; gap: 4px; box-sizing: border-box;
  height: 24px; padding: 0 4px 0 0; border-radius: 999px; color: #fff; font-size: 12px; font-weight: 600; text-shadow: 0 1px 2px rgba(0, 0, 0, .3); white-space: nowrap; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.theme-switch button[data-theme-set="light"] { --tt-i: 0; }
.theme-switch button[data-theme-set="auto"] { --tt-i: 1; }
.theme-switch button[data-theme-set="dark"] { --tt-i: 2; }
/* How far the sun is from this stop, 0 to 1. */
.theme-switch button[data-theme-set] { --tt-far: clamp(0, max(var(--tt-pos) - var(--tt-i), var(--tt-i) - var(--tt-pos)), 1); }
.theme-switch button[data-theme-set]:focus-visible { box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .9); }
/* The pages' own button rules (a tint, a lift, a shadow under the pointer or the press) stop at the track's edge. */
.theme-switch button[data-theme-set], .theme-switch button[data-theme-set]:hover, .theme-switch button[data-theme-set]:active { background: transparent !important; border: 0 !important; transform: none !important; }
.theme-switch button[data-theme-set]:hover, .theme-switch button[data-theme-set]:active { box-shadow: none !important; color: #fff !important; }
.theme-switch .tt-icon { display: grid; flex: none; place-items: center; width: 24px; height: 24px; }
.theme-switch .tt-icon svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; opacity: clamp(0, calc(var(--tt-far) * 1.8 - .2), .85); }
.theme-switch button[data-theme-set] > span:not(.tt-icon) { opacity: calc(1 - .22 * var(--tt-far)); transition: opacity .15s; }
.theme-switch button[data-theme-set]:hover > span:not(.tt-icon) { opacity: 1; }

/* Night: the track's edge. */
:root[data-theme="dark"] .theme-switch { box-shadow: 0 -.06em .06em rgba(0, 0, 0, .5), 0 .06em .12em rgba(255, 255, 255, .14); }

/* Icons only: the home page's header, and the portal's top bar on a phone.
   Each stop is as wide as the sun's halo, with the sun centred in it. */
.theme-switch.compact { --tt-knob-x: 3px; width: 96px; }
.theme-switch.compact button[data-theme-set] { justify-content: center; padding: 0; }
.theme-switch.compact button[data-theme-set] > span:not(.tt-icon) { display: none; }

@media (prefers-reduced-motion: reduce) {
  .theme-switch, .theme-switch * { transition: none !important; animation: none !important; }
}

/* At the foot of a sidebar: as wide as the sidebar, a step under the menu. (A settings sidebar turned
   into a top bar on a narrow window sizes it itself: the kit's narrow rules.) */
.sidebar > .theme-switch { width: auto; margin: 8px 2px 0; }
@media (min-width: 861px) { .settings-sidebar > .theme-switch { width: auto; margin: 8px 2px 0; } }
