/*
 * Section and wave (components.md). A page is a stack of sections, each with one background token.
 * Between two sections with different backgrounds a wave is drawn at the bottom edge of the upper
 * section, in its colour, overlapping the next one. Same background on both sides: no wave.
 * The wave shows when the next sibling has a data-bg attribute (another .section, ...); for the last section
 * in <main> that sibling is the element right after <main> (the footer).
 */
@layer components {
  .section {
    --section-bg: var(--color-background-default);
    --section-padding: var(--layout-section-spacing-m);
    position: relative;
    padding-block: var(--section-padding);
    background-color: var(--section-bg);
    color: var(--color-text-default);
  }

  .section[data-spacing="s"] {
    --section-padding: var(--layout-section-spacing-s);
  }

  .section[data-spacing="m"] {
    --section-padding: var(--layout-section-spacing-m);
  }

  .section[data-spacing="l"] {
    --section-padding: var(--layout-section-spacing-l);
  }

  .section[data-bg="default"] {
    --section-bg: var(--color-background-default);
  }

  .section[data-bg="subtle"] {
    --section-bg: var(--color-background-subtle);
  }

  .section[data-bg="strong"] {
    --section-bg: var(--color-background-strong);
  }

  .section[data-bg="muted"] {
    --section-bg: var(--color-background-muted);
  }

  .section[data-bg="accent"] {
    --section-bg: var(--color-background-accent);
  }

  /*
   * Raised surface for boxed components (facts box, empty state, quote, teaser, cards ...): never the section's own tone.
   * White on tinted sections, light beige on white and dark ones.
   */
  .section { --surface-raised: var(--color-background-subtle); }

  .section:is([data-bg="subtle"], [data-bg="muted"], [data-bg="strong"], [data-bg="accent"]) {
    --surface-raised: var(--color-background-default);
  }

  /*
   * One outline rule for boxes on a raised surface that carry information (facts box, series facts, archive teaser,
   * video card, empty state, newsletter block, join items): 1 px, tone-in-tone like the map frame and the image
   * placeholders, a step darker on tinted sections so a white box never melts into the ground. Not for decorative
   * boxes (quote) and cards that are one big link (they have their own hover shadow).
   */
  .section { --surface-raised-outline: var(--color-border-default); }
  .section[data-bg="muted"] { --surface-raised-outline: var(--color-sand-400); }
  .section:is([data-bg="strong"], [data-bg="accent"]) { --surface-raised-outline: var(--color-sand-500); }

  /*
   * Dark section ("Dunkel", inverse): the semantic tokens are remapped here, so every component that reads text, link, border,
   * focus and shadow tokens works unchanged. Raised boxes stay light (see --surface-raised above) and get the light tokens back.
   * Contrast on olive-950: text white 14.8:1, text/subtle sand-200 11.4:1, outline olive-700 3.7:1, focus ring yellow-300.
   */
  .section[data-bg="inverse"] {
    --section-bg: var(--color-background-inverse);
    --surface-raised-outline: var(--color-olive-700);
    --color-text-default: var(--color-text-inverse);
    --color-text-link: var(--color-text-inverse);
    --color-text-subtle: var(--color-sand-200);
    --color-border-default: var(--color-olive-700);
    --color-border-strong: var(--color-sand-300);
    --color-focus: var(--color-yellow-300);
    --color-shadow-hover-action: var(--color-yellow-300);
    --color-shadow-media: var(--color-sand-400);
    --color-feedback-error: var(--color-orange-300);
    color: var(--color-text-inverse);
  }

  /* Plain text links only; buttons, chips, pagination and image links keep their own hover colours. */
  .section[data-bg="inverse"] :is(a:not([class]), .link):hover {
    color: var(--color-yellow-300);
  }

  /*
   * Light surfaces inside a dark section (raised boxes, notices, fields, chips) get the light-theme tokens back.
   * The focus ring stays yellow where it is drawn outside the surface (chips, stretched card links).
   */
  .section[data-bg="inverse"] :is(
      .empty-state, .notice--success, .notice--error, .archive-teaser, .join-item, .block-newsletter, .series__facts,
      .event-facts, .video-card, .block-quote, .chip, .input, .textarea, .select, .pagination__page, .partner-card, .badge--closed, .badge--open, .badge--private, .badge--past, .block-cta, .download-item, .press-scan__hint
    ) {
    --color-text-default: var(--color-warm-gray-950);
    --color-text-subtle: var(--color-warm-gray-800);
    --color-text-link: var(--color-warm-gray-900);
    --color-border-default: var(--color-sand-300);
    --color-border-strong: var(--color-sand-800);
    --color-feedback-error: var(--color-orange-700);
    --surface-raised-outline: var(--color-border-default);
    color: var(--color-text-default);
  }

  @media (min-width: 1024px) {
    .section[data-bg="inverse"] .archive-card--wide {
      --color-text-default: var(--color-warm-gray-950);
      --color-text-subtle: var(--color-warm-gray-800);
      --color-text-link: var(--color-warm-gray-900);
      --color-border-default: var(--color-sand-300);
      --surface-raised-outline: var(--color-border-default);
      color: var(--color-text-default);
    }
  }

  /* Boxes whose focusable parts sit inside the light surface: dark ring. Chips, fields, pagination and whole-card links draw the ring outside, on the dark ground, and keep yellow. */
  .section[data-bg="inverse"] :is(.empty-state, .notice--success, .notice--error, .archive-teaser, .block-newsletter, .series__facts, .event-facts, .video-card, .block-quote, .block-cta) {
    --color-focus: var(--color-olive-950);
  }

  /* Selected chip and current page: dark fill would melt into the section, so they turn yellow with dark text. */
  .section[data-bg="inverse"] :is(.chip[aria-pressed="true"], .chip[aria-current="true"], .chip:has(.chip__input:checked), .pagination__page[aria-current="page"]) {
    --color-action-selected-background: var(--color-yellow-500);
    --color-action-selected-text: var(--color-warm-gray-950);
    border-color: var(--color-warm-gray-950);
  }

  .section[data-bg="inverse"] .badge--cancelled {
    border-color: var(--color-olive-700);
  }

  /* Info notice is itself dark: an outline keeps it visible on the dark section. */
  .section[data-bg="inverse"] .notice--info {
    border: var(--border-width-thin) solid var(--color-olive-700);
  }

  /*
   * Four wave shapes (assets/svg/wave.svg, wave-2/3/4.svg: same height and amplitude range, different phase and curvature)
   * cycle by the position of the section in <main>, so a page always looks the same (no randomness). The straddle bands in
   * layouts.css and page-head.css and the footer wave (drawn by the last section) read the same custom property.
   */
  .section { --wave-mask: url("../../svg/wave.svg"); }
  main > section:nth-of-type(4n + 2) { --wave-mask: url("../../svg/wave-2.svg"); }
  main > section:nth-of-type(4n + 3) { --wave-mask: url("../../svg/wave-3.svg"); }
  main > section:nth-of-type(4n + 4) { --wave-mask: url("../../svg/wave-4.svg"); }

  .section:has(+ [data-bg])::after,
  main:has(+ [data-bg]) > .section:last-child::after {
    content: "";
    position: absolute;
    z-index: var(--z-content);
    inset-inline: 0;
    top: calc(100% - 1px);
    height: calc(var(--layout-wave-height) + 1px);
    background-color: var(--section-bg);
    pointer-events: none;
    -webkit-mask: var(--wave-mask) center / 100% 100% no-repeat;
    mask: var(--wave-mask) center / 100% 100% no-repeat;
  }

  .section[data-bg="default"]:has(+ [data-bg="default"])::after,
  .section[data-bg="subtle"]:has(+ [data-bg="subtle"])::after,
  .section[data-bg="muted"]:has(+ [data-bg="muted"])::after,
  .section[data-bg="strong"]:has(+ [data-bg="strong"])::after,
  .section[data-bg="accent"]:has(+ [data-bg="accent"])::after,
  .section[data-bg="inverse"]:has(+ [data-bg="inverse"])::after,
  main:has(+ [data-bg="default"]) > .section[data-bg="default"]:last-child::after,
  main:has(+ [data-bg="subtle"]) > .section[data-bg="subtle"]:last-child::after,
  main:has(+ [data-bg="muted"]) > .section[data-bg="muted"]:last-child::after,
  main:has(+ [data-bg="strong"]) > .section[data-bg="strong"]:last-child::after,
  main:has(+ [data-bg="accent"]) > .section[data-bg="accent"]:last-child::after,
  main:has(+ [data-bg="inverse"]) > .section[data-bg="inverse"]:last-child::after {
    display: none;
  }
}
