@layer components {
  /* Six weeks at a time as rows of seven days, Monday first, from one edge of the sheet to the other. */
  /* A work permit's tint, for its bar on the month and its mark on the agenda; a to-do's, for its days on the month. */
  :is(.weeks, .agenda) {
    --weeks-work-permit-fill: color-mix(in srgb, var(--color-link) 16%, var(--color-canvas));
    --weeks-todo-fill: color-mix(in srgb, var(--color-highlight) 45%, var(--color-canvas));
  }

  .weeks {
    border-block-start: var(--border);
    margin-inline: calc(var(--sheet-padding) * -1);
  }

  .weeks__nav {
    align-items: center;
    display: flex;
    gap: 16px;
    justify-content: center;
    min-block-size: 60px;
    position: relative;
  }

  .weeks__title {
    font-size: 16px;
    font-weight: 600;
    line-height: 24px;
    margin: 0;
    min-inline-size: 180px;
    text-align: center;
  }

  .weeks__step:is(.btn) {
    --btn-border-radius: var(--radius-lg);
    --btn-size: 40px;
    --icon-size: 16px;

    block-size: var(--btn-size);
    inline-size: var(--btn-size);
    padding: 0;
  }

  /* Off this week, the way back sits at the row's end. */
  .weeks__back {
    font-size: var(--text-control);
    inset-inline-end: var(--sheet-padding);
    position: absolute;
  }

  .weeks__names,
  .weeks__week {
    display: grid;
    grid-template-columns: repeat(7, minmax(0, 1fr));
    list-style: none;
    margin: 0;
    padding: 0;
  }

  .weeks__names {
    color: var(--color-meta);
    font-size: 12px;
    letter-spacing: 0.05em;
    line-height: 20px;
    text-align: center;
    text-transform: uppercase;
  }

  /* A week's windows of several days lie over its days on lanes of their own, under the days' numbers, each day keeping
     room for them above its lines. */
  .weeks__row {
    --lane-gap: 2px;
    --lane-size: 20px;
    --lanes-top: 29px;

    display: grid;

    > * {
      grid-area: 1 / 1;
    }
  }

  .weeks__bars {
    align-self: start;
    display: grid;
    gap: var(--lane-gap) 0;
    grid-auto-rows: var(--lane-size);
    grid-template-columns: repeat(7, minmax(0, 1fr));
    list-style: none;
    margin: var(--lanes-top) 0 0;
    padding: 0;
    pointer-events: none;
    position: relative;
  }

  .weeks__bar {
    background-color: var(--weeks-work-permit-fill);
    border-radius: var(--radius-sm);
    margin-inline: 4px;
    min-inline-size: 0;
    pointer-events: auto;
  }

  .weeks__bar--continued {
    border-end-start-radius: 0;
    border-start-start-radius: 0;
    margin-inline-start: 0;
  }

  .weeks__bar--continues {
    border-end-end-radius: 0;
    border-start-end-radius: 0;
    margin-inline-end: 0;
  }

  /* One line, cut where the days end. */
  .weeks__bar-link {
    color: var(--color-ink);
    display: block;
    font-size: 12px;
    line-height: var(--lane-size);
    overflow: hidden;
    padding-inline: 6px;
    text-decoration: none;
    text-overflow: ellipsis;
    white-space: nowrap;

    .weeks__name {
      display: inline;
    }

    .weeks__context {
      margin-inline-start: 0.5em;
    }

    @media (any-hover: hover) {
      &:hover .weeks__name {
        text-decoration: underline;
      }
    }
  }

  /* A to-do's days: its mark, its name and where, and who does it at the end. Late, it is told in red; done, it is
     ticked. */
  .weeks__bar--todo {
    background-color: var(--weeks-todo-fill);

    .weeks__bar-link {
      align-items: center;
      display: flex;
      gap: 6px;
      padding-inline: 4px 2px;
    }

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

    .weeks__mark {
      background-color: var(--color-canvas);
      margin-block-start: 0;
    }

    &.series--highlighted .weeks__name {
      text-decoration: underline;
    }
  }

  .weeks__bar--late {
    background-color: color-mix(in srgb, var(--color-error-text) 14%, var(--color-canvas));
  }

  /* A day grows with what it holds: its number at the top corner, then a line for each thing. */
  .weeks__day {
    border-block-start: var(--border);
    border-inline-start: var(--border);
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-block-size: 112px;
    min-inline-size: 0;
    padding: 0 0 8px;

    &:first-child {
      border-inline-start: 0;
    }
  }

  .weeks__day--rest {
    background-color: var(--color-meta-fill);
  }

  /* Today is a pale day under a filled band that says its date in full; the first of a month has the band too. */
  .weeks__day--today {
    background-color: color-mix(in srgb, var(--color-highlight) 30%, var(--color-canvas));
  }

  .weeks__day--marked {
    box-shadow: inset 2px 0 0 var(--color-meta);
  }

  .weeks__head {
    color: var(--color-ink);
    display: block;
    font-size: 12px;
    font-weight: 700;
    line-height: 20px;
    margin-block-end: calc(var(--lanes, 0) * (var(--lane-size) + var(--lane-gap)));
    padding: 2px 8px 0;
    text-align: end;
    text-decoration: none;

    .weeks__day--marked & {
      background-color: var(--color-meta);
      color: var(--color-canvas);
      padding-block: 1px;
    }
  }

  .weeks__date--short {
    display: none;
  }

  /* One line of a day: the mark of its kind, its name, and under it where or whose it is. */
  .weeks__item {
    align-items: start;
    border-radius: var(--radius-sm);
    color: var(--color-ink);
    display: flex;
    font-size: 12px;
    gap: 6px;
    line-height: 1.25;
    margin-inline: 4px;
    padding: 2px 4px;
    text-decoration: none;

    @media (any-hover: hover) {
      &:is(a):hover .weeks__name {
        text-decoration: underline;
      }
    }

    &.series--highlighted .weeks__name {
      text-decoration: underline;
    }
  }

  .weeks__mark {
    --icon-size: 8px;

    block-size: 12px;
    border: 1.5px solid var(--color-ink-dark);
    border-radius: 50%;
    display: grid;
    flex: none;
    inline-size: 12px;
    margin-block-start: 1px;
    place-items: center;
  }

  .weeks__item--due .weeks__mark {
    border-radius: 2px;
  }

  .weeks__item--finding .weeks__mark {
    block-size: 10px;
    border-radius: 1px;
    inline-size: 10px;
    margin: 2px 1px 0;
    rotate: 45deg;
  }

  .weeks__item--work-permit .weeks__mark {
    background-color: var(--weeks-work-permit-fill);
    block-size: 8px;
    border-color: var(--color-link);
    border-radius: 2px;
    inline-size: 14px;
    margin-block-start: 3px;
  }

  :is(.weeks__item--done, .weeks__bar--done .weeks__bar-link) {
    color: var(--color-ink-dark);

    .weeks__mark {
      background-color: var(--color-positive);
      border-color: var(--color-positive);
      color: var(--color-canvas);
    }
  }

  :is(.weeks__item--late, .weeks__bar--late) {
    .weeks__mark {
      border-color: var(--color-error-text);
    }

    .weeks__context {
      color: var(--color-error-text);
    }
  }

  .weeks__words {
    display: flex;
    flex: 1;
    flex-direction: column;
    min-inline-size: 0;
  }

  .weeks__name {
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;
    display: -webkit-box;
    font-weight: 700;
    overflow: hidden;
    overflow-wrap: anywhere;
  }

  .weeks__context {
    color: var(--color-meta);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .weeks__avatar {
    --avatar-size: 16px;

    flex: none;
    font-size: 7px;
  }

  /* On a phone a day has no room for words: its number in the middle and a mark for each thing, and the day leads
     to its lines on the agenda. */
  @media (width < 800px) {
    /* A work permit is a thin band across its days, and a tap on it is one on the day under it. */
    .weeks__row {
      --lane-size: 4px;
      --lanes-top: 32px;
    }

    .weeks__bar {
      background-color: var(--color-link);
      pointer-events: none;
    }

    .weeks__bar--todo {
      background-color: var(--color-ink-dark);
    }

    .weeks__bar--late {
      background-color: var(--color-error-text);
    }

    .weeks__bar--done {
      background-color: var(--color-positive);
    }

    .weeks__bar .weeks__bar-link {
      display: none;
    }

    .weeks__day {
      align-content: start;
      align-items: center;
      flex-flow: row wrap;
      gap: 3px;
      justify-content: center;
      min-block-size: 56px;
      padding: 0 2px 4px;
      position: relative;
    }

    .weeks__head {
      flex: 0 0 100%;
      font-size: var(--text-control);
      font-weight: 600;
      line-height: 24px;
      padding: 4px 0 0;
      text-align: center;

      .weeks__day--marked & {
        background-color: transparent;
        color: var(--color-ink);
        padding-block: 4px 0;
      }

      &:is(a)::after {
        content: "";
        inset: 0;
        position: absolute;
      }
    }

    .weeks__date:not(.weeks__date--short) {
      display: none;
    }

    .weeks__date--short {
      display: inline;
    }

    .weeks .weeks__item {
      margin: 0;
      padding: 0;

      .weeks__words,
      .weeks__avatar {
        display: none;
      }

      .weeks__mark {
        --icon-size: 0;

        background-color: var(--color-link);
        block-size: 7px;
        border-width: 0;
        inline-size: 7px;
        margin: 0;
      }

      &.weeks__item--finding .weeks__mark {
        block-size: 6px;
        inline-size: 6px;
        margin: 0 1px;
      }

      &.weeks__item--late .weeks__mark {
        background-color: var(--color-error-text);
      }

      &.weeks__item--done .weeks__mark {
        background-color: var(--color-positive);
      }
    }

    .weeks__title {
      min-inline-size: 0;
    }
  }
}
