.section {
  --section-space: var(--space-xl);
  padding-block: var(--section-space);
}

.section--spacing-none { --section-space: 0; }
.section--spacing-small { --section-space: var(--space-m); }
.section--spacing-medium { --section-space: var(--space-xl); }
.section--spacing-large { --section-space: var(--space-2xl); }

.section__inner {
  display: flex;
  flex-direction: column;
  gap: var(--gap-medium);
  width: 100%;
  max-width: var(--page-max-width);
  margin-inline: auto;
  padding-inline: var(--page-gutter);
  container-type: inline-size;
}

.section--width-narrow > .section__inner {
  max-width: calc(var(--reading-max-width) + 2 * var(--page-gutter));
}

.section--width-full > .section__inner {
  max-width: none;
  padding-inline: 0;
}

/* Two neighbouring sections with the same background get a dividing line. */
.section[data-surface="white"] + .section[data-surface="white"],
.section[data-surface="gray"] + .section[data-surface="gray"],
.section[data-surface="dark-gray"] + .section[data-surface="dark-gray"],
.section[data-surface="black"] + .section[data-surface="black"] {
  padding-block-start: 0;
}

.section[data-surface="white"] + .section[data-surface="white"] > .section__inner,
.section[data-surface="gray"] + .section[data-surface="gray"] > .section__inner,
.section[data-surface="dark-gray"] + .section[data-surface="dark-gray"] > .section__inner,
.section[data-surface="black"] + .section[data-surface="black"] > .section__inner {
  /* A 2px line drawn at the content width (inside the page gutters). */
  padding-block-start: var(--section-space);
  background: linear-gradient(var(--rule-color), var(--rule-color)) no-repeat center top / calc(100% - 2 * var(--section-rule-inset, var(--page-gutter))) 2px;
}

/*
 * Alternating sections (white and gray in turn, see [data-stripe] in
 * css/surface.css) carry data-surface="white" whichever colour they come out,
 * so the rules above would take two of them for white neighbours. They
 * differ from the section before them, so they get no line; nor does a white
 * section that follows a gray one of them.
 */
.section[data-surface] + .section[data-stripe],
.section[data-stripe]:nth-child(even of [data-stripe]) + .section[data-surface="white"] {
  padding-block-start: var(--section-space);
}

.section[data-surface] + .section[data-stripe] > .section__inner,
.section[data-stripe]:nth-child(even of [data-stripe]) + .section[data-surface="white"] > .section__inner {
  padding-block-start: 0;
  background: none;
}

/* Full-width sections have no gutter, so the line spans the full width. */
.section--width-full {
  --section-rule-inset: 0px;
}
