@layer components {
  /* A date field: a 234×32 box that reads the day chosen, or what to do while there is none, with the calendar at its
     end. The whole box opens the month; the date input it sends sits under it, unseen. */
  .date-field {
    display: inline-flex;
    inline-size: 234px;
    max-inline-size: 100%;
    position: relative;
    vertical-align: middle;
  }

  .date-field__value {
    block-size: 100%;
    inline-size: 100%;
    inset: 0;
    opacity: 0;
    pointer-events: none;
    position: absolute;
  }

  .date-field__toggle {
    --icon-size: 16px;
    --input-background: var(--color-canvas);
    --input-border-color: var(--tint-ink-15);
    --input-padding: 2px 6px 2px 12px;

    align-items: center;
    block-size: var(--control-size);
    cursor: pointer;
    display: inline-flex;
    gap: 0.4em;
    justify-content: space-between;
    text-align: start;

    .icon {
      color: var(--tint-ink-50);
    }

    &:hover .icon,
    &[aria-expanded="true"] .icon {
      color: var(--color-ink);
    }
  }

  .date-field__text {
    white-space: nowrap;

    &:empty::before {
      color: var(--color-text-placeholder);
      content: attr(data-placeholder);
    }
  }

  /* The month hangs 8px under its field, 260px wide on a soft shadow with a hair of a border and round corners. */
  .date-picker:is(.popup) {
    --date-picker-day: 30px;
    --panel-border-color: var(--tint-ink-10);
    --panel-border-radius: 16px;
    --panel-border-size: 1px;
    --panel-padding: 16px 16px 20px;

    box-shadow: 0 4px 10px var(--tint-ink-10);
    flex-direction: column;
    inline-size: 260px;
    inset-block-start: calc(100% + 8px);
    max-block-size: calc(100dvh - 1rem);
    max-inline-size: calc(100vw - 1rem);
  }

  .date-picker__sheet-head {
    display: none;
  }

  /* The month and the year, each a name with a caret that opens its list, and the steps to the months either side. */
  .date-picker__head {
    align-items: center;
    display: flex;
    margin-block-end: 10px;
  }

  .date-picker__select {
    display: inline-flex;
    margin-block-start: 4px;
    position: relative;

    select {
      block-size: 100%;
      cursor: pointer;
      inline-size: 100%;
      inset: 0;
      opacity: 0;
      position: absolute;
    }
  }

  .date-picker__select-label {
    --icon-size: 10px;

    align-items: center;
    border-radius: 0.5em;
    display: flex;
    font-size: 14px;
    font-weight: 700;
    gap: 4px;
    line-height: 1.25;
    padding: 2px 4px 4px 8px;
    pointer-events: none;

    select:focus + & {
      box-shadow: 0 0 0 1px var(--tint-ink-10);
    }
  }

  .date-picker__step {
    --icon-size: 21px;

    align-items: center;
    background: var(--color-canvas-light);
    block-size: 30px;
    border: 0;
    border-radius: 50%;
    color: var(--color-ink);
    cursor: pointer;
    display: inline-flex;
    inline-size: 30px;
    justify-content: center;
    margin-inline-start: 8px;
    padding: 0;

    &:first-of-type {
      margin-inline-start: auto;
    }

    &:focus-visible {
      outline: var(--focus-ring);
    }
  }

  /* The days at 12px in 30px rounds under their weekdays' small bold initials; today on a pale disc, the day chosen in ink. */
  .date-picker__month {
    border-collapse: collapse;
    font-size: 10px;
    inline-size: 100%;
    line-height: 1.25;
    text-align: center;

    th {
      font-size: 0.85em;
      font-weight: 700;
      letter-spacing: 1px;
      padding: 0 0 8px;
      text-transform: uppercase;
    }

    td {
      padding: 0;
    }
  }

  .date-picker__day {
    aspect-ratio: 1;
    background: transparent;
    border: 0;
    border-radius: 50%;
    color: var(--color-ink);
    cursor: pointer;
    display: grid;
    font-size: 12px;
    font-variant-numeric: tabular-nums;
    inline-size: var(--date-picker-day);
    margin-inline: auto;
    padding: 0;
    place-items: center;

    @media (any-hover: hover) {
      &:hover {
        background-color: var(--tint-ink-20);
      }
    }

    &[aria-current="date"] {
      background-color: var(--tint-ink-20);
    }

    &.date-picker__day--outside {
      color: var(--color-text-placeholder);
    }

    [aria-selected="true"] > & {
      background-color: var(--color-ink);
      color: var(--color-ink-inverted);
    }

    &:disabled {
      background-color: transparent;
      color: var(--color-text-placeholder);
      cursor: not-allowed;
    }

    &:focus-visible {
      outline: var(--focus-ring);
      outline-offset: var(--focus-ring-offset);
    }
  }

  /* On a phone, a sheet standing on the bar at the foot of the screen, under a shade that darkens towards it. Its
     title and close button are set off by a rule across it, and the days spread to its width, 8% in from each side. */
  @media (width < 640px) {
    .date-picker:is(.popup) {
      --date-picker-bar: calc(var(--footer-height) + env(safe-area-inset-bottom));
      --date-picker-day: 100%;
      --panel-border-radius: 16px 16px 0 0;
      --panel-border-size: 0;
      --panel-padding: 0 8% 20px;

      box-shadow: none;
      inline-size: 100%;
      inset: auto 0 var(--date-picker-bar);
      margin: 0;
      max-block-size: calc(100dvh - var(--date-picker-bar));
      max-inline-size: none;
      position: fixed;

      &::backdrop {
        background: linear-gradient(transparent, oklch(var(--lch-black) / 75%)) top / 100% calc(100% - var(--date-picker-bar)) no-repeat;
      }
    }

    .date-picker__sheet-head {
      align-items: center;
      border-block-end: 1px solid var(--tint-ink-10);
      display: flex;
      justify-content: space-between;
      margin: 0 calc(-8% / 0.84) 20px;                   /* the sheet's 8%, out of the 84% inside it */
      padding: 12px 20px;
    }

    .date-picker__close {
      --icon-size: 14px;

      block-size: 24px;
      inline-size: 24px;
      justify-content: center;
      padding: 0;
    }

    .date-picker__head {
      margin-block-end: 16px;
    }

    .date-picker__step {
      block-size: 40px;
      inline-size: 40px;
    }

    .date-picker__month {
      table-layout: fixed;
    }
  }
}
