/* Glide
   A segmented control. The thumb slides to the selected option.

   <div class="glide" role="radiogroup" aria-label="View">
     <label class="glide__option"><input type="radio" name="view" value="day" checked /><span>Day</span></label>
     <label class="glide__option"><input type="radio" name="view" value="week" /><span>Week</span></label>
     <label class="glide__option"><input type="radio" name="view" value="month" /><span>Month</span></label>
   </div>

   Two to six options of equal width. CSS only: the thumb's position comes from
   which radio is checked. Configure it with the custom properties in the first
   rule, set on the element itself. */

.glide {
  /* Shape. The font size scales the control. */
  --glide-size: 13px; /* font size; height and padding scale with it */
  --glide-height: round(2.462em, 2px); /* height; 32px at the default size */
  --glide-pad: round(1.08em, 1px); /* horizontal padding inside each option; 14px */
  --glide-inset: 2px; /* gap between the thumb and the edge of the track */
  --glide-radius: 8px; /* corner radius of the track; the thumb's radius follows it */

  /* A lifted thumb */
  --glide-shadow: 1; /* strength of its contact shadow, 0 to 1; 0 is flat */
  --glide-border: 1; /* opacity of its hairline edge, 0 to 1; 0 is none */

  /* Colour */
  --glide-accent: #7a2fd6; /* accent: the tinted, solid and underline thumbs, and the focus ring */
  --glide-accent-end: var(--glide-accent); /* second colour of an outline thumb */
  --glide-hover: 0; /* opacity of the preview under an unselected hovered option, 0 to 1; 0 is none */
  --glide-track: light-dark(#1f17260f, #f1edf714); /* track background */
  --glide-text: light-dark(#5b5066, #b3aac0); /* text of unselected options */
  --glide-text-on: light-dark(#1f1726, #f4f0f9); /* text of the selected option */

  /* Motion */
  --glide-duration: 380ms; /* time the thumb takes to reach an option */

  /* Derived. On a dark scheme the accent is lightened where it is used as text. */
  --_accent: light-dark(var(--glide-accent), color-mix(in oklab, var(--glide-accent), #fff 42%));
  --_thumb: light-dark(#fff, #3a3344);
  --_thumb-shadow: 0 0 0 1px light-dark(#1f172614, #ffffff14), 0 1px 2px light-dark(#1f17261f, #00000066);
  --_on: var(--glide-text-on);
  --_ease: linear(0, 0.33 8%, 0.72 19%, 0.97 31%, 1.06 42%, 1.05 51%, 1.01 67%, 1 82%, 1);
  /* How many options there are, and which is selected. Set by the rules below. */
  --_n: 1;
  --_i: 0;
  --_hover-i: 0;

  position: relative;
  isolation: isolate;
  display: inline-grid;
  grid-auto-flow: column;
  /* Equal columns, each at least as wide as the longest label, so labels never run into each other. */
  grid-auto-columns: 1fr;
  box-sizing: border-box;
  height: var(--glide-height);
  padding: var(--glide-inset);
  border-radius: var(--glide-radius);
  background: var(--glide-track);
  font-size: var(--glide-size);
  font-weight: 600;
  line-height: 1;
  -webkit-tap-highlight-color: transparent;
}

/* The count and the selected index, read from the markup. */
.glide:has(> :nth-child(2)) { --_n: 2 }
.glide:has(> :nth-child(3)) { --_n: 3 }
.glide:has(> :nth-child(4)) { --_n: 4 }
.glide:has(> :nth-child(5)) { --_n: 5 }
.glide:has(> :nth-child(6)) { --_n: 6 }
.glide:has(> :nth-child(2) :checked) { --_i: 1 }
.glide:has(> :nth-child(3) :checked) { --_i: 2 }
.glide:has(> :nth-child(4) :checked) { --_i: 3 }
.glide:has(> :nth-child(5) :checked) { --_i: 4 }
.glide:has(> :nth-child(6) :checked) { --_i: 5 }

/* The thumb: one option wide, moved by its own width times the index. */
.glide::before,
.glide::after {
  content: '';
  position: absolute;
  z-index: -1;
  top: var(--glide-inset);
  bottom: var(--glide-inset);
  left: var(--glide-inset);
  width: calc((100% - 2 * var(--glide-inset)) / var(--_n));
  border-radius: max(0px, var(--glide-radius) - var(--glide-inset));
  background: var(--_thumb);
  box-shadow: var(--_thumb-shadow);
  translate: calc(var(--_i) * 100%) 0;
  transition:
    translate var(--glide-duration) var(--_ease),
    scale 240ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
.glide::after {
  z-index: -2;
  background: color-mix(in oklab, var(--_accent) calc(var(--glide-hover) * 100%), transparent);
  box-shadow: none;
  translate: calc(var(--_hover-i) * 100%) 0;
  opacity: 0;
  pointer-events: none;
  transition: translate var(--glide-duration) var(--_ease), opacity 160ms ease;
}
/* Preview uses the same columns and slide as the selection, but sits below it.
   A selected or disabled option never requests a preview. Touch selects directly. */
@media (hover: hover) {
  .glide:has(> .glide__option:hover)::after { opacity: 1 }
  .glide:has(> .glide__option:hover :is(:checked, :disabled))::after { opacity: 0 }
  .glide:has(> :nth-child(2):hover) { --_hover-i: 1 }
  .glide:has(> :nth-child(3):hover) { --_hover-i: 2 }
  .glide:has(> :nth-child(4):hover) { --_hover-i: 3 }
  .glide:has(> :nth-child(5):hover) { --_hover-i: 4 }
  .glide:has(> :nth-child(6):hover) { --_hover-i: 5 }
}
.glide:not(:has(:checked))::before { opacity: 0 }
/* Pressing anywhere on the control presses the thumb. */
.glide:active::before {
  scale: 0.96;
  transition-duration: var(--glide-duration), 90ms;
}
.glide[data-motion='smooth'] { --_ease: cubic-bezier(0.22, 1, 0.36, 1) }

.glide__option {
  position: relative;
  display: grid;
  place-items: center;
  padding: 0 var(--glide-pad);
  border-radius: max(0px, var(--glide-radius) - var(--glide-inset));
  color: var(--glide-text);
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  transition: color 160ms ease;
}
.glide__option:hover,
.glide__option:has(:checked) { color: var(--_on) }
.glide__option input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  appearance: none;
  cursor: pointer;
}
.glide__option:has(:focus-visible) {
  outline: 2px solid var(--_accent);
  outline-offset: 1px;
}
.glide__option:has(:disabled) {
  opacity: 0.45;
  pointer-events: none;
}

/* Thumb styles. The default is a raised thumb. */
.glide[data-thumb='tinted'] {
  --_thumb: color-mix(in oklab, var(--_accent) 16%, transparent);
  --_thumb-shadow: none;
  --_on: var(--_accent);
}
.glide[data-thumb='solid'] {
  --_thumb: var(--glide-accent);
  --_thumb-shadow: 0 1px 2px light-dark(#1f172633, #00000066);
  --_on: #fff;
}
/* Lifted: the raised chip of a sidebar's chosen row on its panel, so tabs and sidebars match. A near-white face,
   a hairline inside its edge and a soft contact shadow, on a neutral panel with a trace of the accent. */
.glide[data-thumb='lifted'] {
  --_thumb: light-dark(#ffffffdb, #ffffff1f);
  background: light-dark(color-mix(in oklab, var(--glide-accent) 5%, #f3f0f6), color-mix(in oklab, var(--glide-accent) 5%, #1a1720));
  --_thumb-shadow:
    inset 0 0 0 1px light-dark(rgb(42 24 62 / calc(0.08 * var(--glide-border))), rgb(255 255 255 / calc(0.07 * var(--glide-border)))),
    0 1px 2px light-dark(rgb(42 24 62 / calc(0.12 * var(--glide-shadow))), rgb(0 0 0 / calc(0.4 * var(--glide-shadow)))),
    0 3px 8px -3px light-dark(rgb(42 24 62 / calc(0.2 * var(--glide-shadow))), rgb(0 0 0 / calc(0.5 * var(--glide-shadow))));
  --_ease: cubic-bezier(0.2, 0, 0, 1);
}
/* Hovering an unselected solid option has no solid backing for white text. */
.glide[data-thumb='solid'] .glide__option:hover:not(:has(:checked)) { color: var(--_accent) }
/* A gradient border, with no fill inside the thumb. */
.glide[data-thumb='outline'] { --_on: var(--_accent) }
.glide[data-thumb='outline']::before {
  box-sizing: border-box;
  padding: 1.5px;
  background: linear-gradient(90deg in oklab, var(--_accent), light-dark(var(--glide-accent-end), color-mix(in oklab, var(--glide-accent-end), #fff 42%)));
  box-shadow: none;
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  mask-composite: exclude;
}
/* An underline instead of a filled thumb, for tabs. The track is not drawn. */
.glide[data-thumb='underline'] {
  --_thumb: var(--_accent);
  --_thumb-shadow: none;
  background: none;
  box-shadow: inset 0 -1px 0 light-dark(#1f17261f, #f1edf724);
  border-radius: 0;
}
.glide[data-thumb='underline']::before {
  top: auto;
  bottom: 0;
  left: calc(var(--glide-inset) + var(--glide-pad));
  width: calc((100% - 2 * var(--glide-inset)) / var(--_n) - 2 * var(--glide-pad));
  height: 2px;
  border-radius: 1px;
  translate: calc(var(--_i) * (100% + 2 * var(--glide-pad))) 0;
}
.glide[data-thumb='underline']:active::before { scale: 1 }

@media (prefers-reduced-motion: reduce) {
  .glide::before,
  .glide::after { transition: none }
}
@media (forced-colors: active) {
  .glide { background: Canvas; outline: 1px solid ButtonText; outline-offset: -1px }
  .glide::before { background: Highlight; box-shadow: none; forced-color-adjust: none }
  .glide::after { display: none }
  .glide[data-thumb='outline']::before { background: Highlight; mask: none; -webkit-mask: none }
  .glide__option:has(:checked) { color: HighlightText; forced-color-adjust: none }
  /* An underline has no filled background behind the label, so the label keeps the text colour. */
  .glide[data-thumb='underline'] { outline: 0 }
  .glide[data-thumb='underline'] .glide__option:has(:checked) { color: CanvasText }
}
