/**
 * @interface
 * var(--prose-size: small) - smaller prose font size
 * var(--linked-heading: {h2,h3}) - styles h2 or h3 as linked main titles
 */

@layer components {
  @scope (blog-prose) to ([data-component]) {
    :scope {
      --prose-body: #292524;
      --prose-heading: #1c1917;
      --prose-muted: #78716c;
      --prose-bullet: #d1bdae;
      --prose-rule: #e7e5e4;
      --prose-code-bg: #292524;
      --prose-code-text: #e7e5e4;
      color: var(--prose-body);
      font-size: 1em;
      line-height: 1.75;
      overflow-wrap: break-word;

      :root[data-color-scheme="dark"] & {
        --prose-body: #f5f5f4;
        --prose-heading: #fff;
        --prose-muted: #a8a29e;
        --prose-bullet: #988698;
        --prose-rule: #44403c;
        --prose-code-bg: rgb(0 0 0 / 0.5);
        --prose-code-text: #d6d3d1;
      }

      @container style(--prose-size: small) {
        font-size: 0.875rem;
        line-height: 1.7143;
      }
    }

    @container style(--prose-linked-heading) {
      :is(h1, h2, h3, h4, h5, h6) a:not(.external) {
        text-decoration-line: none;

        &:hover {
          text-decoration-line: underline;
        }
      }
    }

    :where(h1, h2, h3, h4, h5, h6) {
      color: var(--prose-heading);
      font-family: var(--font-serif);
      font-weight: 600;
      text-wrap: balance;
    }

    h1 {
      font-size: 1.5rem;
      line-height: 1.12;
      margin: 0 0 0.8889em;

      @media (min-width: 640px) {
        font-size: 1.875rem;
      }
    }

    h2 {
      font-size: 1.5em;
      line-height: 1.3333;
      margin: 2em 0 1em;

      @container style(--prose-linked-heading: h2) {
        font-size: 1.5rem;
        line-height: 1.12;
        margin: 0 0 0.8889em;

        @media (min-width: 640px) {
          font-size: 1.875rem;
        }
      }
    }

    h3 {
      font-size: 1.25em;
      line-height: 1.6;
      margin: 1.6em 0 0.6em;
    }

    h4 {
      font-size: 1em;
      line-height: 1.5;
      margin: 1.5em 0 0.5em;
    }

    :is(h1, h2, h3, h4, h5, h6) a {
      font-weight: 600;
      text-decoration-color: oklch(0.8111 0.0308 59.21);
      text-underline-offset: 0.25em;

      :root[data-color-scheme="dark"] & {
        text-decoration-color: #d8b4fe;
      }
    }

    .heading-anchor {
      margin-inline-start: 0.35em;
      color: color-mix(in srgb, var(--prose-muted) 60%, transparent);
      font-weight: 400;
      text-decoration: none;

      &:hover,
      &:focus-visible {
        color: var(--prose-heading);
        text-decoration: underline;
      }
    }

    section {
      margin: 2.5em 0;
    }

    p {
      margin: 1.25em 0;
      text-wrap: pretty;
    }

    a {
      color: var(--prose-heading);
      font-weight: 500;
      overflow-wrap: anywhere;
      text-underline-offset: 0.125em;

      &.external {
        overflow-wrap: anywhere;

        .external-end {
          white-space: nowrap;
        }

        svg {
          position: relative;
          top: -0.1ex;
          display: inline-block;
          width: 1em;
          height: 1em;
          max-width: 24px;
          max-height: 24px;
          margin-left: 0.2ch;
          margin-right: -0.125rem;
          overflow: visible;
          color: color-mix(in srgb, currentColor 60%, transparent);
          vertical-align: middle;

          @media (prefers-reduced-motion: no-preference) {
            /* arrow */
            g {
              transition: transform 200ms;

              a:hover & {
                transform: translate(1px, -1px);
              }

              a:active & {
                transform: translate(-1px, 1px);
              }
            }
          }
        }
      }
    }

    :where(strong, em) {
      color: var(--prose-heading);
    }

    strong {
      font-weight: 600;
    }

    .lead {
      color: var(--prose-muted);
      font-size: 1.25em;
      line-height: 1.6;
      margin: 1.2em 0;
    }

    .embed {
      margin: 1.25rem 0;

      @media (min-width: 768px) {
        margin-inline: -1rem;
      }

      > iframe {
        display: block;
        width: 100%;
        border: 0;
        border-radius: 0.5rem;
        background: transparent;
        color-scheme: light;

        &[data-embed-kind="video"] {
          height: auto;
          aspect-ratio: 16 / 9;
        }

        &[data-embed-kind="audio"] {
          height: 352px;
        }

        &[data-embed-kind="compact"] {
          height: 166px;
        }
      }
    }

    blockquote {
      position: relative;
      color: inherit;
      font-weight: inherit;
      font-style: normal;
      border-inline-start: 2px solid rgb(31 41 55 / 25%);
      margin: 1.6em 0;
      padding-inline-start: 1em;

      :root[data-color-scheme="dark"] & {
        border-color: rgb(255 255 255 / 15%);
      }

      p:first-of-type::before,
      p:last-of-type::after {
        content: none;
      }
    }

    :where(ul, ol) {
      margin: 1.25em 0;
      padding-inline-start: 1.625em;
    }

    li {
      margin: 0.5em 0;
      padding-inline-start: 0.375em;

      &::marker {
        color: var(--prose-bullet);
      }
    }

    .footnotes {
      border-top: 1px solid var(--prose-rule);
      padding-top: 1rem;
      font-size: 0.875em;

      ol {
        margin: 0 0 1.25em;
      }

      li > a:last-child {
        font-family: var(--font-mono);
        text-decoration: none;
      }
    }

    .footnote-ref {
      a {
        color: var(--prose-muted);
        font-size: 0.875em;
        font-weight: 400;
        font-family: var(--font-mono);
        text-decoration: none;
      }
    }

    :is(.footnote-ref, .footnotes li, h2, h3, h4, h5, h6):target {
      background: rgb(56 189 248 / 0.2);
    }

    :where(ul ul, ul ol, ol ul, ol ol) {
      margin: 0.75em 0;
    }

    dl {
      margin: 1.25em 0;
    }

    dt {
      color: var(--prose-heading);
      font-weight: 600;
      margin-top: 1.25em;
    }

    dd {
      margin: 0.5em 0 0;
      padding-inline-start: 1.625em;
    }

    hr {
      width: 100%;
      max-width: 120px;
      border: 0;
      border-top: 2px solid rgba(145, 145, 145, 0.3);
      margin: 40px auto;
    }

    :where(hr, h2, h3, h4) + * {
      margin-top: 0;
    }

    :where(img, video, picture) {
      max-width: 100%;
      height: auto;
      margin: 2em 0;
    }

    img {
      border-radius: 0.375rem;
    }

    img[data-fancybox] {
      cursor: zoom-in;
    }

    .aside,
    figure {
      margin: 2em 0;

      @media (min-width: 768px) {
        margin-inline: -1rem;
      }
    }

    figure {
      > * {
        margin: 0
      }
    }

    figcaption {
      color: var(--prose-muted);
      font-size: 0.875em;
      line-height: 1.4286;
      margin-top: 0.75rem;

      @media (min-width: 768px) {
        padding-inline: 1rem;
      }
    }

    picture {
      > img {
        margin: 0;
      }
    }

    :where(code, kbd, pre, samp) {
      font-family: var(--font-mono);
    }

    code {
      color: var(--prose-heading);
      font-size: 0.875em;
      font-weight: 600;
      overflow-wrap: anywhere;
    }

    pre {
      color: var(--prose-code-text);
      background: var(--prose-code-bg);
      font-size: 0.875em;
      line-height: 1.7143;
      border-radius: 0.375rem;
      margin: 1.7143em 0;
      padding: 0.8571em 1.1429em;
      overflow-x: auto;

      code {
        color: inherit;
        font-size: inherit;
        font-weight: inherit;
        overflow-wrap: normal;
      }
    }

    :not(pre) > code {
      margin-block: -0.25rem;
      padding: 0.25rem 0.375rem;
      border: 1px solid rgb(156 163 175 / 50%);
      border-radius: 0.375rem;
      background: rgb(255 255 255 / 30%);
      color: inherit;
      font-size: 0.9em;
      font-weight: 600;

      :root[data-color-scheme="dark"] & {
        border-color: rgb(255 255 255 / 10%);
        background: rgb(75 85 99 / 25%);
        font-weight: 400;
      }
    }

    kbd {
      color: var(--prose-heading);
      background: rgb(255 255 255 / 0.3);
      border-radius: 0.3125rem;
      box-shadow:
        0 0 0 1px rgb(0 0 0 / 0.1),
        0 3px 0 rgb(0 0 0 / 0.1);
      font-size: 0.875em;
      font-weight: 500;
      padding: 0.1875em 0.375em;

      :root[data-color-scheme="dark"] & {
        background: rgb(0 0 0 / 0.2);
        box-shadow:
          0 0 0 1px rgb(255 255 255 / 0.1),
          0 3px 0 rgb(255 255 255 / 0.1);
      }
    }

    table {
      border-collapse: collapse;
      width: 100%;
      font-size: 0.875em;
      line-height: 1.7143;
    }

    thead {
      border-bottom: 1px solid var(--prose-rule);
    }

    th {
      color: var(--prose-heading);
      font-weight: 600;
      text-align: start;
    }

    tr {
      border-bottom: 1px solid var(--prose-rule);

      &:last-child {
        border-bottom: 0;
      }
    }

    :where(th, td) {
      padding: 0.5714em;
      vertical-align: top;

      &:first-child {
        padding-inline-start: 0;
      }

      &:last-child {
        padding-inline-end: 0;
      }
    }

    > :first-child:not(.embed) {
      margin-top: 0;
    }

    > :last-child:not(.embed) {
      margin-bottom: 0;
    }

    .code-block {
      position: relative;
      border: 1px solid rgb(209 189 174 / 30%);
      border-radius: 0.375rem;
      box-shadow: 0 0 24px rgb(209 189 174 / 20%);
      font-weight: 400;
      font-size: 0.875rem;
      line-height: 1.5rem;
      font-family: var(--font-mono);

      &:not(figure .code-block) {
        margin: 2rem 0;
      }

      @media (min-width: 768px) {
        &:not(figure .code-block) {
          margin-inline: -1rem;
        }
      }

      :root[data-color-scheme="dark"] & {
        border-color: rgb(255 255 255 / 10%);
        box-shadow: 0 0 24px rgb(216 180 254 / 2%);
      }

      pre.shiki {
        position: relative;
        overflow: auto;
        margin: 0;
        padding: 0;
        border-radius: 0.375rem;
        font: inherit;
      }

      .code-block-lang {
        position: absolute;
        top: 0;
        right: 0;
        padding: 0.25rem 0.375rem;
        border-radius: 0 0.25rem 0 0.25rem;
        background: rgb(187 154 247 / 80%);
        color: #1a1b26;
        font-weight: 700;
        font-size: 0.625rem;
        line-height: 1;
        font-family: "Inter Variable", system-ui, sans-serif;
        text-transform: uppercase;
        user-select: none;

        &[data-language="css"] {
          background: rebeccapurple;
          color: #d2b7ed;
        }

        &[data-language="html"] {
          background: #f06529;
        }

        &:is(
          [data-language="js"],
          [data-language="jsx"],
          [data-language="ts"],
          [data-language="tsx"]
        ) {
          background: #f7df1e;
        }

        &:is([data-language="ruby"], [data-language="rb"]) {
          background: #a91401;
          color: #f2d6d3;
        }

        &[data-language="elixir"] {
          background: #48205d;
          color: #e1c5f0;
        }
      }
    }
  }
}
