@layer layout {
  /* 1176 px container with a fluid gutter */
  .l-container {
    inline-size: min(100% - 2 * var(--gutter), var(--container));
    margin-inline: auto;
  }

  /* page: content column + side rail. One column below 64em, the rail then follows the content. */
  .l-page {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--space-12);
    padding-block: var(--space-8) var(--space-16);
  }
  .l-page--after-head { padding-block-start: var(--space-12); }
  .l-page--tight { padding-block-start: var(--space-8); }
  @media (min-width: 48em) { .l-page--tight { padding-block-start: 3.5rem; } }
  .l-main { min-inline-size: 0; display: grid; gap: var(--space-12); align-content: start; }
  .l-main > * { min-inline-size: 0; }
  /* the feeds under an article or static page (see _Feeds): same spacing as the sections of a listing's main column */
  .l-feeds { display: grid; gap: var(--space-12); margin-block-start: var(--space-12); }
  @media (min-width: 64em) { .l-feeds { gap: var(--space-16); margin-block-start: var(--space-16); } }
  .l-article { inline-size: 100%; max-inline-size: var(--content); min-inline-size: 0; }

  @media (min-width: 64em) {
    .l-page--rail {
      grid-template-columns: minmax(0, 1fr) var(--rail);
      column-gap: var(--rail-gap);
      align-items: start;
    }
    .l-main { gap: var(--space-16); }
    /* the rail is as tall as the row, so its last module can follow the reader down a main column that is longer */
    .l-page--balance { align-items: stretch; }
  }
  @media (min-width: 64em) and (min-height: 45em) {
    /* only a module (tags, Facebook box) follows the reader, never an ad unit; one taller than the viewport scrolls inside itself */
    .l-rail--sticky-end > .c-module:last-child {
      position: sticky; inset-block-start: var(--space-6); align-self: flex-start; inline-size: 100%;
      max-block-size: calc(100dvh - 2 * var(--space-6)); overflow-y: auto; overscroll-behavior: contain;
    }
  }

  /* side rail: modules and ad units; modules first on small screens, units after them */
  .l-rail { display: flex; flex-direction: column; gap: var(--space-8); min-inline-size: 0; }
  .l-rail > .ad-slot { order: 2; }
  @media (min-width: 64em) {
    .l-rail { gap: var(--space-8); }
    .l-rail > .ad-slot { order: 0; }
  }
  /* a module that is empty leaves no hole: the engine omits it, but an empty wrapper must collapse too */
  .l-rail > :empty { display: none; }

  /* Align the labels and the top of both banners even when the frames have different heights. */
  .l-adgroup {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: flex-start;
    gap: var(--space-4) var(--ad-gap);
  }
  .l-adgroup:not(:has(> .ad-slot:not(.is-empty))) { display: none; }
  .l-adgroup:has(> .ad-slot:not(.is-empty)) { padding-block: var(--space-5) 0; }


  /* home: hero | featured list, then the rail runs alongside everything below. One column on phones, in DOM order
     (hero, featured, latest ... rail last). Spacing sits on the items, so the rail may span unused rows without gaps. */
  .l-home { display: grid; grid-template-columns: minmax(0, 1fr); gap: 0; }
  .l-home > * { min-inline-size: 0; margin-block-end: var(--space-12); }
  .l-home > .l-rail { margin-block-end: 0; }
  @media (min-width: 64em) {
    .l-home { grid-template-columns: minmax(0, 1fr) var(--rail); column-gap: var(--rail-gap); }
    .l-home > * { grid-column: 1; margin-block-end: var(--space-16); }
    .l-home > .l-home__hero { grid-row: 1; align-self: start; } /* 16:9, does not stretch to the featured list */
    .l-home > .l-home__featured { grid-column: 2; grid-row: 1; align-self: start; margin-block-end: 0; }
    .l-home > .l-rail { grid-column: 2; grid-row: 2 / span 40; align-self: stretch; }
  }

  /* grids used by cards */
  .l-grid { display: grid; gap: var(--space-8) var(--space-6); grid-template-columns: minmax(0, 1fr); }
  @media (min-width: 30em) {
    .l-grid--2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  }
  @media (min-width: 48em) {
    .l-grid--3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  }
  .l-stack { display: grid; gap: var(--space-4); align-content: start; }
  .l-stack--lg { gap: var(--space-8); }
}
