/*
 * 01 Scroll-Snap Date Picker
 *
 * Five scroll-snap wheels and a readout that counts along with them, with no
 * JavaScript. Two Chromium-only features do the work:
 *
 *   1. Named scroll timelines (Chrome 115+). Each wheel publishes its own
 *      scroll progress under a name; the frame puts those names in scope, and
 *      the readout reads them back.
 *   2. scroll-state(snapped) container queries (Chrome 133+), so an item can
 *      tell that it is the one parked on the red line.
 *
 * Nothing here polls scroll position, and nothing measures an offset. The
 * numbers in the readout are counters whose values are animated integers.
 */

@layer tokens {
  :root {
    /*
     * The picker is a light device screen sitting on the lab's dark page, so
     * it carries its own palette rather than bending the shared dark tokens.
     */
    --paper: oklch(96.8% 0.002 106);
    --paper-sunk: oklch(94% 0.002 106);
    --paper-edge: oklch(87% 0.004 106);
    --ink: oklch(19% 0 0);
    --ink-mute: oklch(63% 0 0);
    --ink-ghost: oklch(80% 0 0);
    /* Two reds: the darker one is the only one that ever carries text. */
    --mark-line: oklch(58% 0.21 27);
    --mark-text: oklch(50% 0.2 27);

    --screen-radius: 1rem;
    --pill-radius: var(--radius-full);
    --hair: 1px;

    --type-screen: "Helvetica Neue", Arial, system-ui, sans-serif;
    --text-micro: .625rem;
    --text-label: .6875rem;
    --text-item: .9375rem;
    --track-label: .16em;

    /* Wheel geometry. Every column centres its snapped item on the same line,
       so columns are free to disagree about item height. */
    --wheel-block: clamp(15rem, 40cqi, 22rem);
    --item-block: 2.75rem;
    --item-block-year: clamp(5rem, 15cqi, 9.5rem);

    /* The time wheel's item count. Only this wheel needs its length as a
       number, because only its stepped counters need the range correction
       written against it. The others carry their length in the from/to of
       their keyframes, where a linear count needs nothing else. */
    --time-steps: 96;

    --z-selection: 1;

    --duration-ink: 160ms;
  }

  /*
   * The readout is four counters, not four copies of the wheels. A registered
   * <integer> can be interpolated, so a scroll timeline can drive it, and
   * counter() prints whatever it currently holds.
   */
  @property --year-n {
    syntax: "<integer>";
    inherits: false;
    initial-value: 20;
  }

  @property --day-n {
    syntax: "<integer>";
    inherits: false;
    initial-value: 1;
  }

  @property --month-n {
    syntax: "<integer>";
    inherits: false;
    initial-value: 1;
  }

  @property --hour-n {
    syntax: "<integer>";
    inherits: false;
    initial-value: 0;
  }

  @property --minute-n {
    syntax: "<integer>";
    inherits: false;
    initial-value: 0;
  }
}

@layer layout {
  .demo-stage {
    inline-size: min(92vw, var(--measure-xl));
    margin-block: var(--space-3xl) var(--space-4xl);
    margin-inline: auto;
    padding-inline: var(--space-l);
  }

  .support-note {
    max-inline-size: 60ch;
    margin-block: 0 var(--space-l);
    padding: var(--space-m) var(--space-l);
    border: 1px dashed var(--stage-border);
    border-radius: var(--radius-s);
    color: var(--color-muted);
    font-size: .875rem;
  }
}

@layer components {
  .timer {
    /* The container is the wrapper, not the frame. A container query never
       matches the element that establishes it, and the frame's own column
       track list is the first thing that has to change. */
    container-type: inline-size;
    container-name: frame;
    inline-size: min(100%, 64rem);
    margin-inline: auto;
    font-family: var(--type-screen);
    color: var(--ink);
  }

  .timer-chrome {
    display: flex;
    gap: var(--space-2xl);
    padding-block-end: var(--space-s);
    color: var(--color-muted);
    font-size: var(--text-micro);
    letter-spacing: var(--track-label);
    text-transform: uppercase;
  }

  .timer-frame {
    /*
     * One grid owns the column rhythm; the head and the wheels borrow it with
     * subgrid so a label can never drift off the column it names.
     */
    --cols: 2.1fr 0.62fr 1.05fr 1.25fr 1fr;

    display: grid;
    grid-template-columns: var(--cols);
    grid-template-rows: auto minmax(0, 1fr) auto;
    overflow: clip;
    border: var(--hair) solid var(--paper-edge);
    border-radius: var(--screen-radius);
    background: var(--paper);
    /* Publishes the four wheel timelines to everything inside the frame. */
    timeline-scope: --year-scroll, --day-scroll, --month-scroll, --time-scroll;
  }

  .picker-head {
    display: grid;
    grid-column: 1 / -1;
    grid-template-columns: subgrid;
    align-items: center;
    padding-block: var(--space-l);
    border-block-end: var(--hair) solid var(--paper-edge);
  }

  .field-label {
    grid-row: 1;
    margin: 0;
    padding-inline-start: var(--space-xl);
    color: var(--ink-mute);
    font-size: var(--text-label);
    letter-spacing: var(--track-label);
    text-transform: uppercase;

    /* Placed by hand for the same reason the wheels are: the Confirm button
       is explicitly placed, and one explicit item is enough to send an
       auto-placed sibling into a second row. */
    &[data-field="year"] {
      grid-column: 1;
    }

    /* The wheel the demo opens on is the one named in red. */
    &[data-field="day"] {
      grid-column: 2;
      color: var(--mark-text);
    }

    &[data-field="month"] {
      grid-column: 3;
    }

    &[data-field="time"] {
      grid-column: 4;
    }
  }

  .confirm {
    grid-row: 1;
    grid-column: 5;
    justify-self: end;
    margin-inline-end: var(--space-xl);
    padding: var(--space-2xs) var(--space-l);
    border: var(--hair) solid var(--ink);
    border-radius: var(--pill-radius);
    background: transparent;
    color: var(--ink);
    font: inherit;
    font-size: var(--text-label);
    letter-spacing: .04em;
    cursor: pointer;
    transition:
      background var(--duration-ink) ease,
      color var(--duration-ink) ease;

    &:hover {
      background: var(--ink);
      color: var(--paper);
    }

    &:focus-visible {
      outline: 2px solid var(--mark-line);
      outline-offset: 2px;
    }
  }

  .wheels {
    display: grid;
    grid-column: 1 / -1;
    grid-template-columns: subgrid;
    grid-template-rows: minmax(0, 1fr);
  }

  /*
   * Every column is placed by hand. The red line is a sixth grid item sitting
   * across the same row, and one explicitly placed item is enough to push
   * auto-placed siblings into a second implicit row, which silently stacked
   * the wheels below the line rather than behind it.
   */
  .col {
    grid-row: 1;
    min-inline-size: 0;

    &[data-field="year"] {
      grid-column: 1;
    }

    &[data-field="day"] {
      grid-column: 2;
    }

    &[data-field="month"] {
      grid-column: 3;
    }

    &[data-field="ruler"] {
      grid-column: 4;
    }

    &[data-field="time"] {
      grid-column: 5;
    }

    & + & {
      border-inline-start: var(--hair) solid var(--paper-edge);
    }
  }

  .wheel {
    block-size: var(--wheel-block);
    overflow-y: auto;
    /* Half a wheel of padding at each end, so the first and last items can
       both reach the centre line. It also makes the scrollable range exactly
       (item count - 1) items tall, which is what lets the counters line up. */
    padding-block: calc((var(--wheel-block) - var(--item-block)) / 2);
    scroll-snap-type: y mandatory;
    scrollbar-width: none;

    &:focus-visible {
      outline: 2px solid var(--mark-line);
      outline-offset: -2px;
    }

    &[data-field="year"] {
      padding-block: calc((var(--wheel-block) - var(--item-block-year)) / 2);
      scroll-timeline-name: --year-scroll;
    }

    &[data-field="day"] {
      scroll-timeline-name: --day-scroll;
    }

    &[data-field="month"] {
      scroll-timeline-name: --month-scroll;
    }

    &[data-field="time"] {
      scroll-timeline-name: --time-scroll;
    }
  }

  .wheel-list {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .wheel-item {
    display: grid;
    align-content: center;
    block-size: var(--item-block);
    padding-inline: var(--space-xl);
    scroll-snap-align: center;
    /*
     * Forbids a scrolling operation from passing over a snap position, so a
     * touch flick or trackpad fling advances one item instead of sailing
     * through ten.
     *
     * It does nothing for a mouse wheel in Chrome 149: measured against a
     * bare snap list, a 100px notch lands two items along with this property
     * and two items along without it. A notch is a fixed platform distance
     * and no CSS property scales it, so on a mouse the wheel columns step in
     * twos and threes. Arrow keys are exact.
     */
    scroll-snap-stop: always;
  }

  .wheel-value {
    font-size: var(--text-item);
    font-variant-numeric: tabular-nums;
    transition: color var(--duration-ink) ease;
  }

  /* Year: the reference treats the two-digit year as the headline of the
     screen, cropped by the wheel rather than fitted inside it. */
  .wheel[data-field="year"] {
    .wheel-item {
      block-size: var(--item-block-year);
    }

    .wheel-value {
      font-size: clamp(4rem, 12.5cqi, 8rem);
      font-weight: 500;
      line-height: .9;
      letter-spacing: -.04em;
    }
  }

  .wheel[data-field="day"] {
    .wheel-item {
      position: relative;
      padding-inline: var(--space-l);
    }

    .wheel-value {
      color: var(--mark-text);
      font-size: var(--text-label);
    }

    /* The tick rail on the day column: one tick per day, so the ticks scroll
       with the numbers instead of sitting behind them. */
    .wheel-item::after {
      content: "";
      position: absolute;
      inset-inline-end: 0;
      inset-block-start: 50%;
      inline-size: var(--space-sm);
      border-block-start: var(--hair) solid var(--mark-line);
      opacity: .55;
    }
  }

  .wheel[data-field="time"] .wheel-value {
    font-weight: 500;
  }

  .wheel[data-field="time"] .wheel-item {
    justify-items: end;
  }

  /*
   * The ruler column is not a wheel. It is the time wheel drawn as an
   * instrument: a dotted red axis, and a painted scale that the time wheel's
   * marker rail replaces with real, clickable ticks where ::scroll-marker is
   * supported.
   */
  .ruler {
    display: grid;
    justify-items: center;
    block-size: var(--wheel-block);
    background-image: repeating-linear-gradient(
      to bottom,
      var(--paper-edge) 0 var(--hair),
      transparent var(--hair) var(--space-m)
    );
    background-position: center;
    background-repeat: no-repeat;
    background-size: var(--space-2xl) 100%;
  }

  .ruler-line {
    grid-area: 1 / 1;
    inline-size: var(--hair);
    block-size: 100%;
    background-image: repeating-linear-gradient(
      to bottom,
      var(--mark-line) 0 var(--space-3xs),
      transparent var(--space-3xs) var(--space-2xs)
    );
  }

  /* The one line every column agrees on. It is drawn over the wheels, so it
     has to be invisible to the pointer: a wheel event landing on it would
     otherwise chain past the wheel it covers and scroll the page instead. */
  .selection-line {
    pointer-events: none;
    grid-row: 1;
    grid-column: 1 / -1;
    align-self: center;
    z-index: var(--z-selection);
    block-size: var(--hair);
    background: var(--mark-line);
  }

  .readout {
    display: grid;
    grid-column: 1 / -1;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end;
    gap: var(--space-l);
    padding: var(--space-l) var(--space-xl) var(--space-2xl);
    border-block-start: var(--hair) solid var(--paper-edge);
    background: var(--paper-sunk);
  }

  .readout-label {
    grid-column: 1 / -1;
    margin: 0;
    color: var(--ink-mute);
    font-size: var(--text-micro);
    letter-spacing: var(--track-label);
    text-transform: uppercase;
  }

  .readout-title {
    grid-column: 1 / -1;
    margin-block: var(--space-l) var(--space-2xs);
    font-size: clamp(1rem, 2.6cqi, 1.5rem);
    font-weight: 400;
  }

  .readout-value {
    margin: 0;
    font-size: clamp(2rem, 8.5cqi, 5rem);
    font-weight: 500;
    line-height: 1;
    letter-spacing: -.03em;
    font-variant-numeric: tabular-nums;
    text-wrap: nowrap;
  }

  .readout-at {
    font-weight: 400;
  }

  .readout-time {
    color: var(--ink-mute);
  }

  /*
   * The readout prints registered integers through counter(). Static here,
   * which is the honest unenhanced state: a real date, just not one that
   * follows the wheels.
   */
  .count[data-part="year"] {
    counter-reset: year var(--year-n);

    &::after {
      content: counter(year, decimal-leading-zero);
    }
  }

  .count[data-part="day"] {
    counter-reset: day var(--day-n);

    &::after {
      content: counter(day, decimal-leading-zero);
    }
  }

  .count[data-part="month"] {
    counter-reset: month var(--month-n);

    &::after {
      content: counter(month, decimal-leading-zero);
    }
  }

  .count[data-part="hour"] {
    counter-reset: hour var(--hour-n);

    &::after {
      content: counter(hour, decimal-leading-zero);
    }
  }

  .count[data-part="minute"] {
    counter-reset: minute var(--minute-n);

    &::after {
      content: counter(minute, decimal-leading-zero);
    }
  }

  .knob {
    position: relative;
    align-self: end;
    inline-size: clamp(3rem, 9cqi, 5.5rem);
    aspect-ratio: 1;
    border-radius: 50%;
    background: var(--ink);

    &::after {
      content: "";
      position: absolute;
      inset-block-start: 18%;
      inset-inline-end: 18%;
      inline-size: 14%;
      aspect-ratio: 1;
      border-radius: 50%;
      background: var(--paper);
    }
  }

  .confirm-pop {
    /* No margin reset here: the UA's own `margin: auto` with `inset: 0` is
       what centres a popover in the viewport, and the demo has nothing better
       to offer than the centre of the screen. */
    padding: var(--space-l) var(--space-xl);
    border: var(--hair) solid var(--ink);
    border-radius: var(--radius-s);
    background: var(--paper);
    color: var(--ink);
    font-family: var(--type-screen);
    font-size: var(--text-label);
    letter-spacing: var(--track-label);
    text-transform: uppercase;
  }
}

@layer components {
  /*
   * Enhancement. Without scroll timelines the wheels still scroll and snap;
   * the readout just holds the values the counters start on, and the note
   * above the frame stays visible to say so.
   */
  @supports (animation-timeline: scroll()) {
    .support-note {
      display: none;
    }

    /*
     * A wheel's scroll range covers (steps - 1) gaps, and a linear count from
     * the first value to the last covers the same number of gaps, so every
     * snap position lands on a whole number with no rounding to spare.
     *
     * These are not decorative animations and are deliberately not disabled
     * under prefers-reduced-motion: the only thing moving is the number the
     * user is currently choosing, driven by their own scroll.
     */
    .count {
      animation-timing-function: linear;
      animation-fill-mode: both;
    }

    .count[data-part="year"] {
      animation-name: count-year;
      animation-timeline: --year-scroll;
    }

    .count[data-part="day"] {
      animation-name: count-day;
      animation-timeline: --day-scroll;
    }

    .count[data-part="month"] {
      animation-name: count-month;
      animation-timeline: --month-scroll;
    }

    /*
     * Time is the odd one out. One wheel carries two numbers, so instead of
     * counting linearly each part steps: the hour holds for four items, the
     * minute cycles 00/15/30/45 inside every one of those hours (24 timeline
     * iterations, which a scroll timeline divides across its range).
     *
     * The stepped version needs the range stretched by one item. Linear
     * counting measures gaps between items, steps() measures the items
     * themselves, so the timeline has to run to 96 items rather than the 95
     * gaps the scroll range is worth, or every hour would change a quarter of
     * an item early.
     */
    .count[data-part="hour"] {
      animation-name: count-hour;
      animation-timing-function: steps(24, jump-none);
      animation-timeline: --time-scroll;
      animation-range-end: calc(100% * var(--time-steps) / (var(--time-steps) - 1));
    }

    .count[data-part="minute"] {
      animation-name: count-minute;
      animation-timing-function: steps(4, jump-none);
      animation-iteration-count: 24;
      animation-timeline: --time-scroll;
      animation-range-end: calc(100% * var(--time-steps) / (var(--time-steps) - 1));
    }
  }

  /* The knob is the one piece of pure decoration here, so it is the one piece
     that stops when motion is unwelcome. */
  @media (prefers-reduced-motion: no-preference) {
    @supports (animation-timeline: scroll()) {
      .knob {
        animation-name: turn-knob;
        animation-timing-function: linear;
        animation-fill-mode: both;
        animation-timeline: --time-scroll;
      }
    }
  }

  /*
   * Which item is chosen is a question the scroller can answer itself. Without
   * this every item stays full ink, which reads as a plain list rather than a
   * broken one.
   */
  @supports (container-type: scroll-state) {
    .wheel-item {
      container-type: scroll-state;
    }

    .wheel-value {
      color: var(--ink-ghost);
    }

    .wheel[data-field="day"] .wheel-value {
      color: color-mix(in oklch, var(--mark-text) 40%, var(--paper));
    }

    @container scroll-state(snapped: y) {
      .wheel-value {
        color: var(--ink);
      }

      .wheel[data-field="day"] .wheel-value {
        color: var(--mark-text);
      }
    }
  }

  /*
   * The tick rails are real controls, not decoration.
   *
   * A mouse wheel notch is a fixed platform distance (about 100px here) and no
   * CSS property scales it, so the wheels themselves step two or three items
   * per notch. ::scroll-marker gives the precision back: one marker per snap
   * item, each one scrolling its own item to the centre line exactly, and
   * :target-current painting whichever marker the scroller is currently
   * parked on. Measured: clicking marker n lands on item n, to the pixel.
   *
   * A marker group is laid out as a sibling of its scroller rather than
   * inside it, which is why each group is placed into the grid by hand (and
   * why no wrapper element is needed to hold it). The time wheel's group goes
   * into the ruler column, because a dense rail of 96 ticks with the current
   * one marked is what that column was drawing a picture of.
   */
  @supports selector(::scroll-marker) {
    .wheel {
      scroll-marker-group: after;
    }

    /*
     * The group covers its whole column and the markers are aligned inside
     * it, rather than the group being aligned within the column: justify-self
     * on a marker group puts its start edge on the column's end edge, which
     * pushed the rail into the neighbouring column and left a 2px strip of it
     * clickable. Stretching it and aligning the markers is stable.
     *
     * A group that covers the column would also eat the wheel gestures aimed
     * at the numbers underneath, so the group itself is transparent to the
     * pointer and each marker takes its own hits back.
     */
    .wheel::scroll-marker-group {
      grid-row: 1;
      display: grid;
      grid-auto-rows: 1fr;
      justify-items: end;
      align-self: stretch;
      block-size: var(--wheel-block);
      pointer-events: none;
    }

    .wheel[data-field="year"]::scroll-marker-group {
      grid-column: 1;
    }

    .wheel[data-field="day"]::scroll-marker-group {
      grid-column: 2;
    }

    .wheel[data-field="month"]::scroll-marker-group {
      grid-column: 3;
    }

    .wheel[data-field="time"]::scroll-marker-group {
      grid-column: 4;
      justify-items: center;
    }

    .wheel-item::scroll-marker {
      content: "";
      inline-size: var(--space-m);
      border: 0;
      border-block-start: var(--hair) solid var(--paper-edge);
      background: transparent;
      cursor: pointer;
      pointer-events: auto;
    }

    .wheel-item::scroll-marker:hover {
      border-block-start-color: var(--ink-mute);
    }

    /* The selected tick is the one the scroller is parked on, which the
       scroller knows and nothing else has to be told. */
    .wheel-item::scroll-marker:target-current {
      border-block-start-color: var(--mark-line);
      border-block-start-width: 2px;
    }

    .wheel-item::scroll-marker:focus-visible {
      outline: 2px solid var(--mark-line);
      outline-offset: 1px;
    }

    .wheel[data-field="year"] .wheel-item::scroll-marker,
    .wheel[data-field="month"] .wheel-item::scroll-marker {
      inline-size: var(--space-l);
    }

    .wheel[data-field="time"] .wheel-item::scroll-marker {
      inline-size: var(--space-2xl);
    }

    /* 96 ticks at 3.7px apart read as a dense instrument scale, and the
       current one is the cursor the reference draws as a red square. It is
       taller than its own 3.7px row on purpose, so it reads at a glance. */
    .wheel[data-field="time"] .wheel-item::scroll-marker:target-current {
      inline-size: var(--space-xs);
      block-size: var(--space-xs);
      border: 0;
      background: var(--mark-line);
    }

    /* The day ticks and the ruler's painted scale were standing in for these,
       so they step aside rather than doubling up. */
    .wheel[data-field="day"] .wheel-item::after {
      content: none;
    }

    .ruler {
      background-image: none;
    }
  }

  /* Not Baseline (Safari has not shipped it), and the demo is Chromium-only
     anyway: without it, scrolling past the end of a wheel hands the rest of
     the gesture to the page and the picker slides away mid-choice. */
  @supports (overscroll-behavior: contain) {
    .wheel {
      overscroll-behavior-y: contain;
    }
  }
}

@layer components {
  @keyframes count-year {
    from {
      --year-n: 20;
    }

    to {
      --year-n: 25;
    }
  }

  @keyframes count-day {
    from {
      --day-n: 1;
    }

    to {
      --day-n: 31;
    }
  }

  @keyframes count-month {
    from {
      --month-n: 1;
    }

    to {
      --month-n: 12;
    }
  }

  @keyframes count-hour {
    from {
      --hour-n: 0;
    }

    to {
      --hour-n: 23;
    }
  }

  @keyframes count-minute {
    from {
      --minute-n: 0;
    }

    to {
      --minute-n: 45;
    }
  }

  @keyframes turn-knob {
    to {
      rotate: 1turn;
    }
  }
}

@layer overrides {
  /* Narrow screens drop the ruler first: it is an instrument face for the time
     wheel, and the time wheel is still there. */
  @container frame (inline-size < 44rem) {
    .timer-frame {
      --cols: 1.5fr 0.7fr 1.1fr 1fr;
    }

    .ruler {
      display: none;
    }

    .col[data-field="time"] {
      grid-column: 4;
    }

    .wheel[data-field="time"]::scroll-marker-group {
      grid-column: 4;
      justify-items: end;
    }

    .confirm {
      grid-row: 2;
      grid-column: 1 / -1;
      margin-block-start: var(--space-s);
    }
  }

  @container frame (inline-size < 30rem) {
    .timer-frame {
      --cols: 1.3fr 0.8fr 1.2fr;
    }

    .wheel[data-field="month"],
    .field-label[data-field="month"] {
      display: none;
    }

    .col[data-field="time"],
    .field-label[data-field="time"] {
      grid-column: 3;
    }

    .wheel[data-field="time"]::scroll-marker-group {
      grid-column: 3;
    }
  }

  @media (prefers-contrast: more) {
    :root {
      --paper-edge: oklch(60% 0.004 106);
      --ink-ghost: oklch(55% 0 0);
    }
  }

  @media (forced-colors: active) {
    .selection-line,
    .ruler-line {
      background: Highlight;
      forced-color-adjust: none;
    }

    .timer-frame,
    .picker-head,
    .readout {
      border-color: CanvasText;
    }
  }
}
