@layer page {
  /* ---------- Page head (courses list and compare) ---------- */
  .courses-head {
    padding-block: var(--space-12) var(--space-10);
    border-bottom: 1px solid var(--line);
    background: var(--surface);
  }

  .courses-head__title {
    margin-top: var(--space-3);
    font: var(--type-page-title);
  }

  .courses-head__intro {
    max-width: 60ch;
    margin-top: var(--space-3);
    font: var(--type-body);
    color: var(--text-2);
  }

  .courses-crumbs ol {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    font: var(--type-interface);
    color: var(--text-2);

    & li + li::before {
      content: "/";
      margin-right: var(--space-2);
      color: var(--text-3);
    }

    & a {
      color: var(--text-2);
      text-decoration: none;

      &:hover {
        color: var(--accent-text);
        text-decoration: underline;
      }
    }
  }

  .courses-head__split {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    justify-content: space-between;
    gap: var(--space-4) var(--space-8);
  }

  .courses-head__text {
    min-width: 0;
  }

  .courses-head__back {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: var(--target-public);
    font: var(--type-interface);
    font-weight: 600;
    text-decoration: none;

    & .icon {
      width: 16px;
      height: 16px;
    }

    &:hover {
      text-decoration: underline;
    }
  }

  /* ---------- Layout: filters beside the results ---------- */
  .courses {
    display: grid;
    grid-template-columns: 288px minmax(0, 1fr);
    align-items: start;
    gap: var(--space-8);
    padding-block: var(--space-8) var(--section-space);
  }

  /* ---------- Filters ---------- */
  .course-filters {
    min-width: 0;
  }

  .course-filters__toggle {
    display: none;
    align-items: center;
    min-height: var(--target-public);
    padding: 0 var(--space-4);
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-control);
    background: var(--surface);
    color: var(--text-1);
    font: 600 15px / 20px var(--font-body);
    cursor: pointer;

    & .icon {
      width: 18px;
      height: 18px;
    }
  }

  .course-filters__toggle .course-filters__open,
  .course-filters__toggle .course-filters__close {
    align-items: center;
    gap: var(--space-2);
  }

  .course-filters__toggle .course-filters__open {
    display: inline-flex;
  }

  .course-filters__toggle .course-filters__close,
  .course-filters__state:checked + .course-filters__toggle .course-filters__open {
    display: none;
  }

  .course-filters__state:checked + .course-filters__toggle .course-filters__close {
    display: inline-flex;
  }

  .course-filters__state:focus-visible + .course-filters__toggle {
    outline: 2px solid var(--accent-text);
    outline-offset: 2px;
  }

  .course-filters__form {
    display: grid;
    gap: var(--space-6);
    padding: var(--space-6);
    border: 1px solid var(--line);
    border-radius: var(--radius-card);
    background: var(--surface);
  }

  .course-filters__head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--space-3);
  }

  .course-filters__title {
    font: var(--type-subsection);
  }

  .course-filters__clear {
    font: var(--type-interface);
    font-weight: 600;
  }

  .course-filters__group {
    display: grid;
    gap: 10px;
    min-width: 0;
    margin: 0;
    padding: 0;
    border: 0;

    & legend {
      float: left;
      width: 100%;
      margin-bottom: 10px;
      padding: 0;
    }
  }

  .course-filters__levels {
    display: grid;
    clear: both;

    & li {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: var(--space-3);
    }

    & .check {
      flex: 1;
      align-items: center;
      min-height: var(--target-public);
    }
  }

  .course-filters__count {
    font: var(--type-micro);
    color: var(--text-3);
  }

  .course-filters__months {
    display: flex;
    flex-wrap: wrap;
    gap: 0 var(--space-2);
    clear: both;
  }

  /* The capsule is 36px; the label's padding makes the 44px target. */
  .month-chip {
    position: relative;
    display: inline-flex;
    padding-block: 4px;
    cursor: pointer;

    & input {
      position: absolute;
      inset: 0;
      width: 100%;
      height: 100%;
      margin: 0;
      opacity: 0;
      cursor: pointer;
    }

    & > span {
      display: inline-flex;
      align-items: center;
      gap: 4px;
      min-height: 36px;
      padding: 0 14px;
      border: 1px solid var(--line-strong);
      border-radius: var(--radius-capsule);
      background: var(--surface);
      color: var(--text-1);
      font: var(--type-interface);
    }

    & abbr {
      text-decoration: none;
    }

    &:hover > span {
      border-color: var(--accent-text);
    }

    &:has(input:checked) > span {
      border-color: var(--accent-text);
      background: var(--accent-tint);
      color: var(--accent-text);
      font-weight: 600;
    }

    &:has(input:checked) > span::before {
      content: "";
      width: 8px;
      height: 4px;
      margin: -3px 2px 0 0;
      border-bottom: 2px solid currentColor;
      border-left: 2px solid currentColor;
      transform: rotate(-45deg);
    }

    &:has(input:focus-visible) > span {
      outline: 2px solid var(--accent-text);
      outline-offset: 2px;
    }
  }

  /* ---------- Results ---------- */
  .course-results {
    display: grid;
    gap: var(--space-4);
    min-width: 0;
  }

  .course-results__bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3) var(--space-4);
  }

  .course-results__count {
    font: var(--type-subsection);
  }

  .course-sort {
    display: flex;
    align-items: center;
    gap: 10px;

    & .select {
      width: 220px;
    }
  }

  .course-sort__label {
    font: var(--type-interface);
    font-weight: 600;
    color: var(--text-2);
    white-space: nowrap;
  }

  .course-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 12px var(--space-2);

    & .chip {
      position: relative;
      min-height: 32px;
      padding: 5px 10px 5px 14px;
      font: var(--type-label);

      &::after {
        content: "";
        position: absolute;
        inset: -6px 0;
      }
    }
  }

  .course-compare {
    display: contents;
  }

  .course-list {
    display: grid;
    gap: var(--space-4);
    container-type: inline-size;
  }

  /* ---------- Course card ---------- */
  .course-card {
    display: grid;
    grid-template-columns: 64px minmax(0, 1fr) 170px auto;
    align-items: start;
    gap: var(--space-6);
    padding: var(--space-6);
    border: 1px solid var(--line);
    border-radius: var(--radius-card);
    background: var(--surface);
    transition:
      background-color 120ms ease-out,
      border-color 120ms ease-out;

    &:has(input[name="ids"]:checked) {
      border-color: var(--accent-text);
      background: var(--accent-tint);
    }
  }

  .course-card__info {
    display: grid;
    gap: var(--space-2);
    min-width: 0;
  }

  .course-card__kicker {
    display: flex;
    flex-wrap: wrap;
    gap: 2px 10px;
    font: var(--type-interface);
    color: var(--text-2);

    & > span[aria-hidden] {
      color: var(--text-3);
    }
  }

  .course-card__university {
    font-weight: 600;
  }

  .course-card__name {
    font: var(--type-subsection);
    overflow-wrap: anywhere;
  }

  .course-card__meta {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2) var(--space-5);
    padding-top: var(--space-1);
    font: 500 13px / 16px var(--font-body);
    color: var(--text-2);

    & li {
      display: inline-flex;
      align-items: center;
      gap: 6px;
    }

    & .icon {
      width: 14px;
      height: 14px;
      color: var(--text-3);
    }
  }

  .course-card__fees {
    display: grid;
    align-content: start;
    gap: var(--space-2);
    min-height: 100%;
    padding-left: var(--space-6);
    border-left: 1px solid var(--line);
  }

  .course-card__fee {
    display: grid;
    gap: 2px;

    & dt {
      font: var(--type-micro);
      color: var(--text-3);
    }

    & dd {
      font: var(--type-subsection);
      white-space: nowrap;
    }
  }

  .course-card__fee--minor dd {
    font: 600 15px / 20px var(--font-body);
  }

  .course-card__actions {
    display: grid;
    justify-items: end;
    gap: 14px;

    & .btn {
      min-width: 120px;
    }
  }

  .course-card__compare {
    align-items: center;
    min-height: var(--target-public);
  }

  @container (max-width: 719px) {
    .course-card {
      grid-template-columns: 48px minmax(0, 1fr);
      gap: var(--space-4);
      padding: var(--space-5) var(--space-4);

      & > .logo-tile,
      & > .monogram {
        width: 48px;
        height: 48px;
        padding: 6px;
        font-size: 18px;
      }
    }

    .course-card__fees {
      grid-column: 1 / -1;
      grid-auto-flow: column;
      justify-content: start;
      gap: var(--space-8);
      min-height: 0;
      padding: var(--space-3) 0 0;
      border-top: 1px solid var(--line);
      border-left: 0;
    }

    .course-card__actions {
      grid-column: 1 / -1;
      grid-template-columns: minmax(0, 1fr) auto;
      align-items: center;
      gap: var(--space-4);

      & .btn {
        width: 100%;
      }
    }
  }

  .course-pages {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: var(--space-4);
    padding-top: var(--space-4);

    & .icon {
      width: 16px;
      height: 16px;
    }
  }

  /* ---------- Compare tray: the list is a form; without JavaScript it shows once a box is ticked ---------- */
  .compare-tray {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 15;
    display: none;
    border-top: 1px solid var(--line);
    background: var(--surface-raised);
    box-shadow: 0 -8px 24px rgb(9 16 53 / 12%);
  }

  .compare-tray[data-open],
  .course-compare:has(.course-list input[name="ids"]:checked) .compare-tray {
    display: block;
  }

  .compare-tray__inner {
    display: flex;
    align-items: center;
    gap: var(--space-5);
    min-height: 88px;
    padding-block: var(--space-3);
  }

  .compare-tray__count {
    display: grid;
    gap: 2px;
    flex: none;
  }

  .compare-tray__status {
    font: 600 16px / 20px var(--font-body);
  }

  .compare-tray__hint {
    font: var(--type-micro);
    color: var(--text-3);
  }

  .compare-tray__chips {
    display: flex;
    flex: 1;
    gap: 10px;
    min-width: 0;
  }

  .compare-tray__actions {
    display: flex;
    flex: none;
    align-items: center;
    gap: var(--space-4);
    margin-left: auto;
  }

  .compare-chip {
    display: inline-flex;
    flex: 0 1 auto;
    align-items: center;
    gap: var(--space-2);
    min-width: 0;
    max-width: 300px;
    min-height: 36px;
    padding: 0 6px 0 16px;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-capsule);
    background: var(--surface);
    font: var(--type-interface);
  }

  .compare-chip__label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .compare-chip__remove {
    position: relative;
    display: grid;
    flex: none;
    place-items: center;
    width: 28px;
    height: 28px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--text-2);

    &::after {
      content: "";
      position: absolute;
      inset: -8px;
    }

    &:hover {
      background: var(--surface-sunk);
      color: var(--text-1);
    }

    & .icon {
      width: 16px;
      height: 16px;
    }
  }

  .compare-chip--empty {
    flex: none;
    padding-right: 16px;
    border-color: var(--line);
    color: var(--text-3);
  }

  /* The tray's Compare is the page's one next step while it is open, so the header Apply steps down to Outline. */
  body:has(.compare-tray[data-open], .course-list input[name="ids"]:checked) .site-header__actions .btn--primary {
    --btn-bg: var(--surface);
    --btn-fg: var(--accent-text);
    --btn-border: var(--line-strong);

    &:hover {
      --btn-bg: var(--accent-tint);
      --btn-border: var(--accent-text);
    }
  }

  body:has(.compare-tray[data-open], .course-list input[name="ids"]:checked) .site-footer {
    padding-bottom: calc(var(--space-10) + 120px);
  }

  /* ---------- Compare page ---------- */
  .compare-legend {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--accent-text);
    border-radius: var(--radius-control);
    background: var(--accent-tint);
    color: var(--accent-text);
    font: var(--type-label);

    & .icon {
      width: 16px;
      height: 16px;
    }
  }

  .compare {
    display: grid;
    gap: var(--space-4);
    padding-block: var(--space-8) var(--section-space);
  }

  .compare-note {
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-card);
    background: var(--surface-sunk);
    font: var(--type-interface);
    font-weight: 400;
    color: var(--text-2);
  }

  .compare-more {
    font: var(--type-interface);
    font-weight: 400;
    color: var(--text-2);
  }

  .compare-scroll {
    overflow-x: auto;
    border: 1px solid var(--line);
    border-radius: var(--radius-card);
    background: var(--surface);
  }

  .compare-table {
    --attr: 240px;
    --cols: 4;
    width: 100%;
    table-layout: fixed;
    font: 500 15px / 21px var(--font-body);
    font-variant-numeric: tabular-nums;

    & th,
    & td {
      padding: var(--space-4) var(--space-6);
      border-top: 1px solid var(--line);
      text-align: left;
      vertical-align: top;
      overflow-wrap: anywhere;
    }

    & td,
    & thead th + th {
      border-left: 1px solid var(--line);
    }

    & thead th {
      border-top: 0;
    }

    & th[scope="row"],
    & .compare-table__corner {
      position: sticky;
      left: 0;
      z-index: 1;
      width: var(--attr);
      background: var(--surface);
    }

    & th[scope="row"] {
      font: 600 14px / 18px var(--font-body);
      color: var(--text-1);
    }
  }

  .compare-table--1 {
    --cols: 1;
  }

  .compare-table--2 {
    --cols: 2;
  }

  .compare-table--3 {
    --cols: 3;
  }

  .compare-table .compare-table__corner {
    padding: var(--space-6);
    background: var(--surface-sunk);
    font: var(--type-label);
    color: var(--text-2);
  }

  .compare-table__course {
    padding: var(--space-6);
    font: inherit;
  }

  .compare-course__top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--space-3);
    margin-bottom: var(--space-3);

    & .logo-tile,
    & .monogram {
      width: 56px;
      height: 56px;
    }
  }

  .compare-course__remove {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    min-height: var(--target-public);
    color: var(--text-2);
    font: var(--type-label);
    text-decoration: none;

    & .icon {
      width: 14px;
      height: 14px;
    }

    &:hover {
      color: var(--accent-text);
      text-decoration: underline;
    }
  }

  .compare-course__name {
    font: 600 17px / 23px var(--font-body);
    color: var(--text-1);
  }

  .compare-table__label {
    display: block;
  }

  .compare-differs {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin-top: var(--space-1);
    font: 600 12px / 15px var(--font-body);
    color: var(--accent-text);

    & .icon {
      width: 12px;
      height: 12px;
      stroke-width: 2.25;
    }
  }

  .compare-table .compare-table__row--differs {
    & th,
    & td {
      background: var(--accent-tint);
    }

    & th {
      box-shadow: inset 3px 0 0 var(--accent-text);
    }
  }

  .compare-table__row--total td {
    font: 600 16px / 22px var(--font-body);
  }

  .compare-table .compare-table__apply {
    & th {
      background: var(--surface-sunk);
    }

    & th,
    & td {
      padding: var(--space-6);
    }
  }

  /* ---------- Narrow screens ---------- */
  @media (max-width: 1023px) {
    .courses {
      grid-template-columns: minmax(0, 1fr);
      gap: var(--space-4);
      padding-top: var(--space-6);
    }

    .course-filters__toggle {
      display: inline-flex;
    }

    .course-filters__form {
      display: none;
    }

    /* Checked, the filters become a full-height sheet; the toggle stays on top as the close control. */
    .course-filters:has(.course-filters__state:checked) {
      position: fixed;
      inset: 0;
      z-index: 30;
      overflow-y: auto;
      overscroll-behavior: contain;
      background: var(--surface);

      & .course-filters__toggle {
        position: sticky;
        top: 0;
        z-index: 1;
        display: flex;
        width: 100%;
        min-height: 56px;
        border: 0;
        border-bottom: 1px solid var(--line);
        border-radius: 0;
      }

      & .course-filters__form {
        display: grid;
        padding: var(--space-5) var(--space-4) var(--space-8);
        border: 0;
        border-radius: 0;
      }
    }

    body:has(.course-filters__state:checked) {
      overflow: hidden;
    }

    .course-sort .select {
      flex: 1 1 auto;
      width: auto;
      min-width: 0;
    }
  }

  @media (max-width: 599px) {
    .course-sort {
      width: 100%;
    }
  }

  @media (max-width: 719px) {
    .compare-tray__inner {
      display: grid;
      grid-template-columns: minmax(0, 1fr) auto;
      gap: var(--space-3);
      padding-block: var(--space-3) var(--space-4);
    }

    .compare-tray__chips {
      grid-column: 1 / -1;
      grid-row: 2;
      overflow-x: auto;
      padding-block: 4px;
    }

    .compare-chip {
      flex: none;
      max-width: 240px;
    }

    .compare-chip--empty {
      display: none;
    }

    .compare-tray__actions {
      display: contents;

      & .btn--primary {
        grid-column: 1 / -1;
        grid-row: 3;
      }

      & .btn--text {
        grid-column: 2;
        grid-row: 1;
      }
    }

    .compare-table {
      --attr: 112px;
      width: calc(var(--attr) + var(--cols) * (100vw - 2 * 16px - 2px - var(--attr)) / 2);
      font-size: 14px;

      & th,
      & td {
        padding: var(--space-3);
      }
    }

    .compare-table--1 {
      width: 100%;
    }

    .compare-table .compare-table__corner,
    .compare-table__course,
    .compare-table .compare-table__apply th,
    .compare-table .compare-table__apply td {
      padding: var(--space-3);
    }

    .compare-course__top {
      flex-direction: column-reverse;

      & .logo-tile,
      & .monogram {
        width: 44px;
        height: 44px;
        padding: 4px;
        font-size: 16px;
      }
    }

    .compare-course__name {
      font-size: 15px;
      line-height: 20px;
    }

    .compare-table__apply .btn {
      white-space: normal;
    }
  }
}
