/* The knitted select: the size of a merino jumper, on a white page. The field is the ribbed collar in loden green,
   with the size's woven label sewn on and a ball of the same yarn at its end; the list under it is knitted in the
   same yarn. The knit, its shadow and the yarn are drawn by the script on two canvases. */
:root {
  --page: #ffffff;          /* the page */
  --yarn: #4a5b3f;          /* the merino: the collar, the knitted list and the ball */
  --satin: #f2ebda;         /* the woven labels */
  --thread: #1c2440;        /* the size woven into the labels */
  --chalk: #f4ecd9;         /* the words printed on the knit */
  --amber: #f5c784;         /* a size with few left */
  --ink: #1c2440;           /* the field's name and the ring of the focus */
}

* {
  box-sizing: border-box;
}

html {
  touch-action: manipulation;
}

html,
body {
  margin: 0;
  overflow-x: clip;
}

body {
  min-height: 100svh;
  background: var(--page);
  color: var(--ink);
  font-family: 'Commissioner Variable', system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.stage {
  min-height: 100svh;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  place-items: center;
  padding: 40px 16px;
}

/* ---------------------------------------------------------------- the select */
.size {
  width: min(360px, 100%);
}

.size__label {
  display: block;
  margin: 0 0 10px 2px;
  font-size: 15px;
  font-weight: 640;
  line-height: 18px;
  letter-spacing: -0.005em;
  color: var(--ink);
  cursor: default;
}

/* the field, and under it the room of the open list (the script measures it), so the page never moves */
.size__box {
  position: relative;
  height: 400px;
}

/* ---------------------------------------------------------------- the field: the ribbed collar */
.field {
  position: relative;
  z-index: 1;
  height: 60px;
  overflow: hidden;
  border-radius: 14px;
  background: var(--yarn);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.14),
    inset 0 -1px 0 rgba(0, 0, 0, 0.3),
    0 1px 1px rgba(20, 26, 14, 0.25),
    0 8px 18px -8px rgba(20, 26, 14, 0.5);
  cursor: pointer;
  outline: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  transition: border-radius 0.16s ease, box-shadow 0.2s ease;
}

/* open, the list is knitted on to the collar: its lower corners square off */
.size.is-shown .field {
  border-bottom-left-radius: 3px;
  border-bottom-right-radius: 3px;
}

.field:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 3px;
}

.field__rib {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  transition: filter 0.25s ease;
}

@media (hover: hover) and (pointer: fine) {
  .field:hover .field__rib {
    filter: brightness(1.07);
  }
}

.field:active .field__rib {
  filter: brightness(0.94);
}

.field__tag {
  position: absolute;
  left: 12px;
  top: 16px;
}

/* the chest measurement, printed on the collar, in a slot one line high where the words roll over */
.field__text {
  position: absolute;
  left: 76px;
  right: 60px;
  top: 50%;
  height: 24px;
  margin-top: -12px;
  overflow: hidden;
  color: var(--chalk);
  font-size: 15px;
  font-weight: 560;
  line-height: 24px;
  white-space: nowrap;
  text-shadow: 0 1px 2px rgba(10, 14, 6, 0.55);
}

.field__words {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
}

.field__words.is-old {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
}

/* ---------------------------------------------------------------- the woven labels */
.tag {
  --tilt: 0deg;
  --stitch: #b8ab90;
  position: relative;
  display: grid;
  place-items: center;
  width: 48px;
  height: 28px;
  border-radius: 1.5px;
  background:
    /* the two ends, folded under */
    linear-gradient(90deg,
      rgba(70, 52, 22, 0.2) 0 2.5px,
      rgba(255, 255, 255, 0.55) 2.5px 3.5px,
      transparent 3.5px calc(100% - 3.5px),
      rgba(255, 255, 255, 0.55) calc(100% - 3.5px) calc(100% - 2.5px),
      rgba(70, 52, 22, 0.2) calc(100% - 2.5px)),
    /* the weave */
    repeating-linear-gradient(0deg, rgba(95, 75, 40, 0.075) 0 1px, transparent 1px 2px),
    /* the satin's sheen */
    linear-gradient(162deg, rgba(255, 255, 255, 0.62), rgba(255, 255, 255, 0) 52%, rgba(60, 45, 20, 0.06)),
    var(--satin);
  box-shadow:
    inset 0 0.5px 0 rgba(255, 255, 255, 0.6),
    0 1px 1px rgba(10, 14, 6, 0.38),
    0 3px 6px -1px rgba(10, 14, 6, 0.34);
  color: var(--thread);
  transform: rotate(var(--tilt));
  transition: transform 0.42s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.3s ease;
}

.tag__text {
  padding-left: 0.08em;
  font-family: 'Brygada 1918 Variable', Georgia, serif;
  font-size: 13.5px;
  font-weight: 700;
  line-height: 1;
  letter-spacing: 0.08em;
}

/* sewn on: two stitches at each end */
.tag::before,
.tag::after {
  content: '';
  position: absolute;
  top: 7px;
  width: 1.6px;
  height: 4.5px;
  border-radius: 1px;
  background: var(--stitch);
  box-shadow: 0 9.5px 0 var(--stitch);
  transform-origin: 50% 0;
}

.tag::before {
  left: 0.8px;
}

.tag::after {
  right: 0.8px;
}

/* the label flying up into the collar, and the old one leaving it: placed and moved by the script */
.tag.is-flying,
.tag.is-leaving {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 6;
  transition: none;
  pointer-events: none;
}

.tag.is-flying::before,
.tag.is-flying::after,
.tag.is-leaving::before,
.tag.is-leaving::after {
  opacity: 0;
}

/* a stitch of the old label, giving way */
.snip {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 6;
  width: 1.6px;
  height: 6px;
  border-radius: 1px;
  background: #cfc3a8;
  pointer-events: none;
}

/* landed in the collar: it settles and is sewn on */
.field__tag.is-sewn {
  animation: tag-land 0.5s cubic-bezier(0.3, 1.4, 0.5, 1);
}

.field__tag.is-sewn::before,
.field__tag.is-sewn::after {
  animation: tag-sew 0.36s ease-out 0.08s backwards;
}

@keyframes tag-land {
  0% {
    scale: 1.08 0.9;
  }

  45% {
    scale: 0.97 1.04;
  }

  100% {
    scale: 1 1;
  }
}

@keyframes tag-sew {
  from {
    scale: 1 0;
  }

  to {
    scale: 1 1;
  }
}

/* a sold-out size refuses: its label shakes on its stitches */
.tag.is-nope {
  animation: tag-nope 0.46s ease-out;
}

@keyframes tag-nope {
  0%,
  100% {
    rotate: 0deg;
  }

  20% {
    rotate: -7deg;
  }

  42% {
    rotate: 5deg;
  }

  64% {
    rotate: -3deg;
  }

  84% {
    rotate: 1deg;
  }
}

/* ---------------------------------------------------------------- the knitting and the ball */
.knit {
  position: absolute;
  z-index: 2;
  left: -30px;
  top: 0;
  display: block;
  pointer-events: none;
}

.ball {
  position: absolute;
  z-index: 4;
  top: 12px;
  right: 13px;
  width: 36px;
  height: 36px;
  pointer-events: none;
  filter: drop-shadow(0 2px 1.5px rgba(10, 14, 6, 0.45));
  transition: transform 0.15s ease;
}

.size__box:has(.field:active) .ball {
  transform: scale(0.94);
}

.ball svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.ball__core {
  fill: color-mix(in srgb, var(--yarn) 70%, #000);
}

.ball__rim {
  fill: none;
  stroke: rgba(10, 14, 6, 0.35);
  stroke-width: 0.8;
}

.strand {
  fill: none;
  stroke-linecap: round;
}

.strand--under {
  stroke: color-mix(in srgb, var(--yarn) 55%, #000);
  stroke-width: 2.3;
}

.strand--over {
  stroke: color-mix(in srgb, var(--yarn) 86%, #fff);
  stroke-width: 1.2;
}

/* the loose end, tucked: while the list is out, the yarn runs from the ball into the knitting instead */
.ball__tail {
  fill: none;
  stroke: color-mix(in srgb, var(--yarn) 85%, #fff);
  stroke-width: 2.4;
  stroke-linecap: round;
  transition: opacity 0.18s ease;
}

.size.is-shown .ball__tail {
  opacity: 0;
}

/* ---------------------------------------------------------------- the list, knitted */
.list {
  position: absolute;
  left: 0;
  top: 60px;
  z-index: 3;
  width: 100%;
  margin: 0;
  padding: 10px 0 14px;
  list-style: none;
  outline: none;
  visibility: hidden;
}

.size.is-shown .list {
  visibility: visible;
}

.opt {
  position: relative;
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr) 16px;
  align-items: center;
  column-gap: 16px;
  height: 50px;
  padding: 0 50px 0 12px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

.opt[aria-disabled='true'] {
  --satin: #cbc4b4;
  --thread: #6f6858;
  cursor: not-allowed;
}

/* a row's words and label show once the knitting has gone past it, and leave before it unravels */
.opt > * {
  opacity: 0;
  translate: 0 -5px;
  transition: opacity 0.16s ease, translate 0.38s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.opt > .tag {
  transition:
    opacity 0.16s ease,
    translate 0.38s cubic-bezier(0.34, 1.56, 0.64, 1),
    transform 0.42s cubic-bezier(0.34, 1.56, 0.64, 1),
    box-shadow 0.3s ease;
}

.opt.is-knit > * {
  opacity: 1;
  translate: 0 0;
}

/* a size chosen: the other labels are unpicked at once (the chosen one flies up their column), the words wait for
   the unravelling to reach them */
.list.is-leaving .opt > .tag,
.list.is-leaving .opt > .opt__check {
  opacity: 0;
  transition-duration: 0.12s;
}

/* the chosen label has flown up into the collar */
.opt.is-gone .tag {
  visibility: hidden;
}

.opt__text {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.opt__chest {
  overflow: hidden;
  color: var(--chalk);
  font-size: 14.5px;
  font-weight: 540;
  line-height: 1.2;
  white-space: nowrap;
  text-overflow: ellipsis;
  text-shadow: 0 1px 2px rgba(10, 14, 6, 0.55);
  transition: color 0.2s ease;
}

.opt__note {
  overflow: hidden;
  color: color-mix(in srgb, var(--chalk) 66%, transparent);
  font-size: 12px;
  font-weight: 500;
  line-height: 1.2;
  white-space: nowrap;
  text-overflow: ellipsis;
  text-shadow: 0 1px 2px rgba(10, 14, 6, 0.5);
}

.opt__note--low {
  color: var(--amber);
}

.opt[aria-disabled='true'] .opt__chest {
  color: color-mix(in srgb, var(--chalk) 52%, transparent);
}

.opt__check {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: var(--chalk);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  visibility: hidden;
  filter: drop-shadow(0 1px 1px rgba(10, 14, 6, 0.5));
}

.opt[aria-selected='true'] .opt__check {
  visibility: visible;
}

/* the row under the pointer or the keys: its label lifts off the knit (the light on the knit is drawn) */
.opt.is-active .tag {
  transform: rotate(calc(var(--tilt) - 2.5deg)) translateY(-2px) scale(1.05);
  box-shadow:
    inset 0 0.5px 0 rgba(255, 255, 255, 0.6),
    0 2px 2px rgba(10, 14, 6, 0.3),
    0 8px 12px -3px rgba(10, 14, 6, 0.42);
}

.opt.is-active .opt__chest {
  color: #ffffff;
}

.opt[aria-disabled='true'].is-active .opt__chest {
  color: color-mix(in srgb, var(--chalk) 52%, transparent);
}

/* ---------------------------------------------------------------- reduced motion */
@media (prefers-reduced-motion: reduce) {
  .tag,
  .opt > *,
  .opt > .tag {
    transition: opacity 0.2s ease;
  }

  .opt > * {
    translate: none;
  }

  .opt.is-active .tag {
    transform: rotate(var(--tilt));
  }

  .field__tag.is-sewn,
  .field__tag.is-sewn::before,
  .field__tag.is-sewn::after {
    animation: none;
  }

  /* a sold-out size still answers, without moving: its label dims for a moment */
  .tag.is-nope {
    animation: tag-dim 0.5s ease;
  }

  @keyframes tag-dim {
    0%,
    100% {
      opacity: 1;
    }

    35% {
      opacity: 0.45;
    }
  }

  .knit {
    transition: opacity 0.22s ease;
  }
}
