/*
 * Content sections (layout field "Abschnitte"): every row is a .section (tone, width, waves from section.css) with a
 * column grid inside. Desktop: column widths as fractions (1fr, 1fr+1fr, 2fr+1fr, 1fr+2fr), chosen by the row's
 * data-columns (spans "12", "6-6", "8-4", "4-8" set by layouts.php; no inline style because of the CSP), stacked on
 * mobile in reading order.
 * Straddle: an image across the transition to the next section, see the second half of this file.
 */
@layer components {
  .layout {
    display: grid;
    gap: var(--space-32);
    align-items: start;
  }

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

  /* A column without visible blocks takes no room when the columns are stacked. */
  @media (max-width: 1023.98px) {
    .layout__column--empty {
      display: none;
    }
  }

  @media (min-width: 1024px) {
    .layout {
      grid-template-columns: minmax(0, 1fr);
      column-gap: var(--space-48);
    }

    .layout[data-columns="6-6"] { grid-template-columns: minmax(0, 6fr) minmax(0, 6fr); }
    .layout[data-columns="8-4"] { grid-template-columns: minmax(0, 8fr) minmax(0, 4fr); }
    .layout[data-columns="4-8"] { grid-template-columns: minmax(0, 4fr) minmax(0, 8fr); }
  }

  /* Two neighbouring layout sections with the same tone read as one: no double padding between them. */
  .section--layout:not([data-straddle])[data-bg="default"] + .section--layout[data-bg="default"],
  .section--layout:not([data-straddle])[data-bg="subtle"] + .section--layout[data-bg="subtle"],
  .section--layout:not([data-straddle])[data-bg="muted"] + .section--layout[data-bg="muted"],
  .section--layout:not([data-straddle])[data-bg="accent"] + .section--layout[data-bg="accent"],
  .section--layout:not([data-straddle])[data-bg="strong"] + .section--layout[data-bg="strong"],
  .page-head[data-bg="strong"] + .section--layout[data-bg="strong"] {
    padding-block-start: 0;
  }

  /*
   * ---------- straddle ----------
   * The row's last block (image or text-image, last column) has "straddle": its image sits half in this section and
   * half in the next. The section ends flush with the image; behind the lower part of the image the background
   * already has the tone of the next section (--straddle-bg), the transition is the same wave as between sections,
   * drawn behind the image. Everything hangs on [data-straddle] (set by layouts.php only when the option is honoured
   * and a section follows), so the option does nothing on the last section, on blocks that are not last, or on pages
   * without sections.
   * --straddle-split: where the tone changes inside the image (50 % desktop, 60 % mobile = smaller overlap).
   */
  .section--layout[data-straddle] {
    --straddle-split: 60%;
    --straddle-bg: var(--color-background-default);
    padding-block-end: 0;
    overflow-x: clip; /* the full-bleed bands below reach beyond the viewport */
  }

  .section--layout[data-straddle="subtle"] { --straddle-bg: var(--color-background-subtle); }
  .section--layout[data-straddle="muted"] { --straddle-bg: var(--color-background-muted); }
  .section--layout[data-straddle="accent"] { --straddle-bg: var(--color-background-accent); }
  .section--layout[data-straddle="inverse"] { --straddle-bg: var(--color-background-inverse); }
  .section--layout[data-straddle="strong"] { --straddle-bg: var(--color-background-strong, var(--color-sand-300)); }

  @media (min-width: 1024px) {
    .section--layout[data-straddle] {
      --straddle-split: 50%;
    }
  }

  /* The wave at the bottom of the section would be drawn below the image; the split sits inside the image instead. */
  .section.section--layout[data-straddle]::after {
    display: none;
  }

  /* The straddle column sits at the bottom of the row, so its image ends exactly at the section's edge. */
  .section--layout[data-straddle] > .container > .layout > .layout__column:last-child {
    align-self: end;
  }

  /* The full-bleed band of the straddle image reaches under the neighbouring columns: their content stays above it. */
  .section--layout[data-straddle] > .container > .layout > .layout__column:not(:last-child) {
    position: relative;
    z-index: var(--z-content-raised);
  }

  .section--layout[data-straddle] > .container > .layout > .layout__column:last-child > .blocks > .block-straddle:last-child :is(.block-straddle__media) {
    position: relative;
    z-index: var(--z-content);
  }

  .section--layout[data-straddle] > .container > .layout > .layout__column:last-child > .blocks > .block-straddle:last-child .block-straddle__media::before,
  .section--layout[data-straddle] > .container > .layout > .layout__column:last-child > .blocks > .block-straddle:last-child .block-straddle__media::after {
    content: "";
    position: absolute;
    z-index: var(--z-below);
    inset-inline: -100vmax;
    pointer-events: none;
  }

  /* Lower band in the tone of the next section. */
  .section--layout[data-straddle] > .container > .layout > .layout__column:last-child > .blocks > .block-straddle:last-child .block-straddle__media::before {
    inset-block: var(--straddle-split) 0;
    background-color: var(--straddle-bg);
  }

  /* Wave in this section's tone over the band. */
  .section--layout[data-straddle] > .container > .layout > .layout__column:last-child > .blocks > .block-straddle:last-child .block-straddle__media::after {
    top: calc(var(--straddle-split) - 1px);
    block-size: calc(var(--layout-wave-height) + 1px);
    background-color: var(--section-bg);
    -webkit-mask: var(--wave-mask, url("../../svg/wave.svg")) center / 100% 100% no-repeat;
    mask: var(--wave-mask, url("../../svg/wave.svg")) center / 100% 100% no-repeat;
  }

  /* A caption below the image continues the band. */
  .section--layout[data-straddle] > .container > .layout > .layout__column:last-child > .blocks > .block-straddle:last-child .block-image__caption {
    position: relative;
    z-index: var(--z-content);
    padding-block-end: var(--space-8);
  }

  .section--layout[data-straddle] > .container > .layout > .layout__column:last-child > .blocks > .block-straddle:last-child .block-image__caption::before {
    content: "";
    position: absolute;
    z-index: var(--z-below);
    inset: calc(-1 * var(--space-8)) -100vmax 0;
    background-color: var(--straddle-bg);
  }

  /* Text with image: text above the split (padding = half the image height), image flush with the bottom. */
  .section--layout[data-straddle] > .container > .layout > .layout__column:last-child > .blocks > .block-text-image.block-straddle:last-child {
    align-items: end;
  }

  .section--layout[data-straddle] > .container > .layout > .layout__column:last-child > .blocks > .block-text-image.block-straddle:last-child .block-text-image__body {
    align-self: start;
  }

  /* The block's image ratio (data-ratio from text-image.php; the four ratios the field offers). */
  .block-text-image[data-ratio="4/3"] { --straddle-ratio: 1.3333; }
  .block-text-image[data-ratio="1/1"] { --straddle-ratio: 1; }
  .block-text-image[data-ratio="3/2"] { --straddle-ratio: 1.5; }
  .block-text-image[data-ratio="16/9"] { --straddle-ratio: 1.7778; }

  @media (min-width: 1024px) {
    .section--layout[data-straddle] > .container > .layout > .layout__column:last-child > .blocks > .block-text-image.block-straddle:last-child .block-text-image__body {
      /* Percentage = width of the grid cell = width of the image: half its height at the block's ratio. */
      padding-block-end: calc(50% / var(--straddle-ratio, 1.3333) + var(--layout-wave-height) + var(--space-24));
    }
  }

  /* Stacked: text first, image last, so the image can reach over the transition. */
  @media (max-width: 1023.98px) {
    .section--layout[data-straddle] > .container > .layout > .layout__column:last-child > .blocks > .block-text-image.block-straddle:last-child .block-text-image__media {
      order: 2;
    }
  }
}
