/*
 * .rich-text — body copy from WYSIWYG fields and Gutenberg post content.
 * Figma: Article body D 337:10776, M 337:10459.
 *
 * One wrapper styles every tag the client can insert. Theme blocks
 * (blocks/<name>/render.php) sit in a .nv-block wrapper: their own markup
 * (lists, tables, figures…) is left alone and only the flow gap applies;
 * a .rich-text inside a block (FAQ answer) is styled again by the paired
 * ":where(.nv-block .rich-text)" selectors (both add no specificity).
 */

.rich-text {
  color: var(--nv-text);
  font-size: clamp(1.125rem, 1.0806rem + 0.1896vw, 1.25rem);
  line-height: 1.2;
  letter-spacing: -0.03em;
  overflow-wrap: anywhere;

  /* @media (width >= 48em) .rich-text */
  @media (width >= 48em) {
    line-height: 1.4;
  }

  & > * + * {
    margin-top: var(--nv-flow-gap);
  }

  & > :first-child {
    margin-top: 0;
  }

  & > :last-child {
    margin-bottom: 0;
  }

  /* Figma measures the flow gap from the cap height and the baseline (Story 1574:13452):
     headings (line-height 1) drop 0.14em, paragraphs (1.2 → 1.4) 0.24em → 0.34em. */
  & > :is(h1, h2, h3, h4, h5, h6):not(.nv-block, .is-style-icon-blue, .is-style-icon-green) {
    margin-top: calc(var(--nv-flow-gap) - 0.14em);
    margin-bottom: -0.14em;

    &:first-child {
      margin-top: -0.14em;
    }
  }

  & > p:not(.nv-block) {
    margin-top: calc(var(--nv-flow-gap) - 0.24em);
    margin-bottom: -0.24em;

    &:first-child {
      margin-top: -0.24em;
    }

    /* @media (width >= 48em) .rich-text > p */
    @media (width >= 48em) {
      margin-top: calc(var(--nv-flow-gap) - 0.34em);
      margin-bottom: -0.34em;

      &:first-child {
        margin-top: -0.34em;
      }
    }
  }

  /* .rich-text > .wp-block-image img — 335 × 340 → 926 × 500 as in Figma (the block's own ratio setting wins) */
  & > .wp-block-image img:not([style*="aspect-ratio"]) {
    width: 100%;
    aspect-ratio: 335 / 340;
    object-fit: cover;

    /* @media (width >= 48em) .rich-text > .wp-block-image img */
    @media (width >= 48em) {
      aspect-ratio: 926 / 500;
    }
  }

  /* h1 pasted by the client looks like h2: the page already has its own h1. */
  & h1:not(:where(.nv-block *)),
  & h2:not(:where(.nv-block *)),
  & :where(.nv-block .rich-text) h1,
  & :where(.nv-block .rich-text) h2 {
    color: var(--nv-navy);
    font-size: clamp(2rem, 1.6001rem + 1.7061vw, 3.125rem);
    font-weight: 400;
    line-height: 1;
    letter-spacing: -0.02em;

    /* @media (width < 48em) .rich-text h1, h2 — phone frames track every heading at -4 % */
    @media (width < 48em) {
      letter-spacing: -0.04em;
    }
  }

  & h3:not(:where(.nv-block *)),
  & h4:not(:where(.nv-block *)),
  & h5:not(:where(.nv-block *)),
  & h6:not(:where(.nv-block *)),
  & :where(.nv-block .rich-text) h3,
  & :where(.nv-block .rich-text) h4,
  & :where(.nv-block .rich-text) h5,
  & :where(.nv-block .rich-text) h6 {
    color: var(--nv-navy);
    font-size: clamp(1.4375rem, 1.2375rem + 0.8531vw, 2rem);
    font-weight: 500;
    line-height: 1;
    letter-spacing: -0.04em;
  }

  & strong,
  & b {
    font-weight: 600;
  }

  & em,
  & i {
    font-style: italic;
  }

  & a:not(:where(.nv-block *)),
  & :where(.nv-block .rich-text) a {
    display: inline;
    color: var(--nv-blue-light);
    text-decoration: underline;
    text-underline-offset: 0.15em;
    transition: color 0.3s ease;

    &:focus-visible {
      outline: 2px solid var(--nv-blue-light);
      outline-offset: 2px;
    }
  }

  /* @media (any-hover: hover) .rich-text a:hover */
  @media (any-hover: hover) {
    & a:not(:where(.nv-block *)):hover,
    & :where(.nv-block .rich-text) a:hover {
      color: var(--nv-blue);
    }
  }

  & ul:not(:where(.nv-block *)),
  & ol:not(:where(.nv-block *)),
  & :where(.nv-block .rich-text) ul,
  & :where(.nv-block .rich-text) ol {
    display: flex;
    flex-direction: column;
    gap: 0.75em;
  }

  & li:not(:where(.nv-block *)),
  & :where(.nv-block .rich-text) li {
    position: relative;
    padding-left: 1.5em;
  }

  & ul:not(:where(.nv-block *)) > li::before,
  & :where(.nv-block .rich-text) ul > li::before {
    content: "";
    position: absolute;
    top: 0.45em;
    left: 0.25em;
    width: 0.5rem;
    height: 0.5rem;
    background: url("../../img/icons/rhomb.svg") center / contain no-repeat;
  }

  & ol:not(:where(.nv-block *)),
  & :where(.nv-block .rich-text) ol {
    counter-reset: nv-rich-text;
  }

  & ol:not(:where(.nv-block *)) > li,
  & :where(.nv-block .rich-text) ol > li {
    padding-left: 2em;
    counter-increment: nv-rich-text;
  }

  & ol:not(:where(.nv-block *)) > li::before,
  & :where(.nv-block .rich-text) ol > li::before {
    content: counter(nv-rich-text, decimal-leading-zero) ".";
    position: absolute;
    top: 0;
    left: 0;
    color: var(--nv-blue-light);
    font-weight: 600;
  }

  & li > ul,
  & li > ol {
    margin-top: 0.75em;
  }

  & blockquote:not(:where(.nv-block *)),
  & :where(.nv-block .rich-text) blockquote {
    padding-left: clamp(0rem, -1.7772rem + 7.5829vw, 5rem);
    color: var(--nv-navy);
    font-size: clamp(1.25rem, 1.1833rem + 0.2844vw, 1.4375rem);
    font-weight: 500;
    line-height: 1.2;
    letter-spacing: -0.04em;

    & p + p {
      margin-top: 0.5em;
    }

    & cite {
      display: block;
      margin-top: 1em;
      font-size: 0.75em;
      font-style: normal;
      font-weight: 400;
    }
  }

  & img:not(:where(.nv-block *)),
  & :where(.nv-block .rich-text) img {
    border-radius: var(--nv-radius-m);
  }

  & figure:not(:where(.nv-block *)),
  & :where(.nv-block .rich-text) figure {
    position: relative;
    margin-left: 0;
    margin-right: 0;
  }

  & figure:not(:where(.nv-block *)) img,
  & :where(.nv-block .rich-text) figure img {
    display: block;
    width: 100%;
  }

  /* Caption sits on the photo as a frosted pill (Figma 337:10816). */
  & figcaption:not(:where(.nv-block *)),
  & :where(.nv-block .rich-text) figcaption {
    position: absolute;
    left: clamp(0.625rem, 0.4028rem + 0.9479vw, 1.25rem);
    bottom: clamp(0.625rem, 0.4028rem + 0.9479vw, 1.25rem);
    z-index: 1;
    isolation: isolate;
    max-width: calc(100% - 2 * clamp(0.625rem, 0.4028rem + 0.9479vw, 1.25rem));
    padding: 0.9375rem;
    border-radius: var(--nv-radius-pill);
    color: var(--nv-navy);
    font-size: 0.875rem;
    font-weight: 500;
    line-height: 1;
    letter-spacing: -0.02em;

    /* Blur on a pseudo-element: Safari fades backdrop-filter on a rounded box. */
    &::before {
      content: "";
      position: absolute;
      inset: 0;
      z-index: -1;
      border-radius: inherit;
      background: rgba(255, 255, 255, 0.8);
      backdrop-filter: blur(0.625rem);
    }
  }

  & table:not(:where(.nv-block *)),
  & :where(.nv-block .rich-text) table {
    display: block;
    max-width: 100%;
    overflow-x: auto;
    border-collapse: collapse;
    font-size: 0.875em;
  }

  & th:not(:where(.nv-block *)),
  & td:not(:where(.nv-block *)),
  & :where(.nv-block .rich-text) th,
  & :where(.nv-block .rich-text) td {
    padding: 0.625rem;
    border: 1px solid var(--nv-steel);
    text-align: left;
  }

  & hr:not(:where(.nv-block *)),
  & :where(.nv-block .rich-text) hr {
    height: 1px;
    background: var(--nv-steel);
  }
}
