@layer components {
  /* The outside of a mailpiece, laid out for whoever sorts it. Its status class lends the colour. */
  .envelope {
    --envelope-ink: var(--color-ink-darker);
    --stamp-size: 5.75rem;

    background-color: var(--color-canvas);
    border: var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-card);
    color: var(--color-ink);
    column-gap: 1rem;
    container-type: inline-size;
    display: grid;
    grid-template-areas:
      "return postmark stamp"
      "status postmark stamp"
      "address address stamp"
      "foot foot foot";
    grid-template-columns: minmax(0, 1fr) auto auto;
    grid-template-rows: auto auto 1fr auto;
    overflow: hidden;
    padding: 1.375rem 1.5rem 1.125rem;
    row-gap: 0.75rem;

    /* On a phone it keeps this arrangement, scaled down with its width: text stops at a readable floor
       and is cut short past it, and the stamp keeps only its verdict and the DMARC policy. */
    @media (width < 640px) {
      --stamp-size: clamp(3.625rem, 15cqi, 5.75rem);

      column-gap: 0.5rem;
      padding: 0.75rem 0.875rem 0.625rem;
      row-gap: 0.375rem;
    }

    p {
      margin: 0;
    }
  }

  /* Neutral, such as accepted mail, inks in the envelope's own ink, darker than the shared grey. */
  .envelope.status--neutral {
    --status-color: var(--envelope-ink);
    --status-text: var(--envelope-ink);
  }

  .envelope__return {
    color: var(--envelope-ink);
    display: flex;
    flex-direction: column;
    font-size: 0.8125rem;
    gap: 0.2em;
    grid-area: return;
    line-height: 1.3;
    min-inline-size: 0;
    overflow-wrap: anywhere;

    strong {
      font-size: 0.875rem;
    }

    @media (width < 640px) {
      font-size: 0.7rem;
      gap: 0.1em;
      line-height: 1.25;

      > * {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      strong {
        font-size: 0.75rem;
      }
    }
  }

  .envelope__status {
    align-self: start;
    color: var(--envelope-ink);
    font-size: 0.75rem;
    font-weight: 800;
    grid-area: status;
    letter-spacing: 0.06em;
    overflow-wrap: anywhere;
    text-transform: uppercase;

    @media (width < 640px) {
      font-size: 0.7rem;
      letter-spacing: 0.04em;
    }

    .envelope:is(.status--warning, .status--error) & {
      color: var(--status-text);
    }
  }

  .envelope__postmark {
    align-items: center;
    align-self: start;
    color: var(--envelope-ink);
    display: flex;
    grid-area: postmark;
    margin-block-start: -0.25rem;

    @media (width < 640px) {
      margin-block-start: 0;
    }
  }

  /* A fixed ring, whatever the font: the date sits clear of the name on its arc. */
  .envelope__cancellation {
    --cancellation-size: 5.75rem;

    align-items: center;
    block-size: var(--cancellation-size);
    border: 2px solid currentColor;
    border-radius: 50%;
    display: flex;
    flex: none;
    flex-direction: column;
    font-size: 0.625rem;
    font-variant-numeric: tabular-nums;
    gap: 0;
    inline-size: var(--cancellation-size);
    justify-content: center;
    line-height: 1.05;
    padding: calc(var(--cancellation-size) * 0.2);
    position: relative;
    rotate: -8deg;
    text-align: center;
    white-space: nowrap;

    strong {
      font-size: 0.75rem;
      text-transform: uppercase;
    }

    @media (width < 640px) {
      --cancellation-size: clamp(3.75rem, 15cqi, 5.75rem);

      font-size: 0.5rem;

      strong {
        font-size: 0.625rem;
      }
    }
  }

  /* The office's name runs round the inside of the ring, as on a real postmark. */
  .envelope__office {
    fill: currentColor;
    font-size: 9.5px;
    font-weight: 700;
    inset: 0;
    letter-spacing: 0.02em;
    pointer-events: none;
    position: absolute;
    text-anchor: middle;
    text-transform: uppercase;

    path {
      fill: none;
    }
  }

  .envelope__year {
    font-weight: 700;
  }

  /* The cancellation runs on towards the stamp. */
  .envelope__waves {
    background-color: currentColor;
    block-size: 3.5rem;
    flex: none;
    inline-size: 6rem;
    margin-inline: -0.375rem -0.25rem;
    mask: url("/assets/postmark-waves-4e8ecd49.svg") center / contain no-repeat;
    position: relative;
    rotate: -8deg;
    z-index: 1;

    @media (width < 640px) {
      block-size: 2.25rem;
      inline-size: clamp(2rem, 10cqi, 6rem);
    }

    /* Wider by as much as its margin gives back, so it runs onto the stamp's paper without moving it. */
    @media (width >= 640px) {
      --waves-reach: calc(0.7rem + 10px);

      inline-size: calc(6rem + var(--waves-reach));
      margin-inline-end: calc(-0.25rem - var(--waves-reach));
      mask: url("/assets/postmark-waves-4e8ecd49.svg") center / 100% 100% no-repeat;
    }
  }

  /* The mask clips borders and box shadows, so the outline and lift are drop shadows on the paper's holder. */
  .envelope__stamp {
    --perforation: 3px;
    --stamp-outline: var(--color-ink-lighter);

    align-self: start;
    display: grid;
    filter:
      drop-shadow(1px 0 0 var(--stamp-outline)) drop-shadow(-1px 0 0 var(--stamp-outline))
      drop-shadow(0 1px 0 var(--stamp-outline)) drop-shadow(0 -1px 0 var(--stamp-outline))
      drop-shadow(0 1px 2px oklch(var(--lch-black) / 12%));
    grid-area: stamp;
    inline-size: var(--stamp-size);
    min-block-size: 7rem;
    rotate: 2deg;

    @media (width < 640px) {
      --perforation: calc(var(--stamp-size) / 38);

      min-block-size: calc(var(--stamp-size) * 1.2);
    }
  }

  /* Perforated paper: half circles bitten out along every edge, the middle kept whole. */
  .envelope__paper {
    background-color: color-mix(in oklab, var(--status-color) 7%, var(--color-canvas));
    display: grid;
    mask:
      radial-gradient(var(--perforation), #0000 98%, #000) round
        calc(var(--perforation) * -1.5) calc(var(--perforation) * -1.5) / calc(var(--perforation) * 3) calc(var(--perforation) * 3),
      linear-gradient(#000 0 0) no-repeat 50% / calc(100% - var(--perforation) * 3) calc(100% - var(--perforation) * 3);
    padding: calc(var(--perforation) * 2.75);
  }

  .envelope__stamp-face {
    --icon-size: 1.625rem;

    align-items: center;
    background-color: color-mix(in oklab, var(--status-color) 14%, var(--color-canvas));
    border: 1px solid color-mix(in oklab, var(--status-color) 45%, var(--color-canvas));
    color: var(--status-text);
    display: flex;
    flex-direction: column;
    font-size: 0.5625rem;
    justify-content: space-between;
    gap: 0.25rem;
    line-height: 1.25;
    overflow-wrap: anywhere;
    padding: 0.5rem 0.125rem;
    text-align: center;

    @media (width < 640px) {
      --icon-size: calc(var(--stamp-size) * 0.26);

      font-size: max(0.55rem, calc(var(--stamp-size) * 0.1));
      gap: 0.1rem;
      justify-content: center;
      padding: 0.2rem 0;

      .envelope__stamp-note {
        display: none;
      }
    }
  }

  .envelope__verdict {
    font-size: 0.8125rem;
    overflow-wrap: normal;
    font-weight: 900;
    letter-spacing: 0.02em;
    line-height: 1.1;
    text-transform: uppercase;

    @media (width < 640px) {
      font-size: max(0.55rem, calc(var(--stamp-size) * 0.11));
      letter-spacing: 0;
    }
  }

  .envelope__checks {
    display: flex;
    flex-direction: column;
    font-weight: 700;

    @media (width < 640px) {
      > :not(.envelope__policy) {
        display: none;
      }
    }
  }

  /* Each line of the address stays on one, cut short with its whole text in a title. */
  .envelope__address {
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
    grid-area: address;
    min-inline-size: 0;

    padding-inline-start: 36%;

    > * {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    > span {
      color: var(--envelope-ink);
      font-size: 0.75rem;
    }

    @media (width < 640px) {
      gap: 0.2rem;
      padding-inline-start: 18%;

      > span {
        font-size: 0.7rem;
      }
    }
  }

  .envelope__subject {
    color: var(--envelope-ink);
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;

    @media (width < 640px) {
      letter-spacing: 0.04em;
    }
  }

  .envelope__recipient {
    font-size: 1.375rem;
    line-height: 1.15;

    @media (width < 640px) {
      font-size: clamp(0.875rem, 4.2cqi, 1.375rem);
    }
  }

  /* Inks without room beside the barcode wrap above it; under wrap-reverse, flex-start is the bottom. */
  .envelope__foot {
    align-items: flex-start;
    display: flex;
    flex-wrap: wrap-reverse;
    gap: 0.75rem 1rem;
    grid-area: foot;
    min-inline-size: 0;

    @media (width < 640px) {
      align-items: flex-end;
      flex-wrap: nowrap;
      gap: 0.5rem;
    }
  }

  .envelope__inks {
    align-items: center;
    display: flex;
    flex: 1 1 auto;
    gap: 1rem;
    justify-content: flex-end;
    min-inline-size: 0;

    @media (width < 640px) {
      flex: none;
      gap: 0.375rem;
    }
  }

  /* A rubber stamp, inked in its status colour. */
  .envelope__ink {
    align-items: flex-end;
    border: 2px solid color-mix(in oklab, var(--status-color) 75%, transparent);
    border-radius: 3px;
    color: color-mix(in oklab, var(--status-text) 90%, transparent);
    display: flex;
    flex-direction: column;
    font-variant-numeric: tabular-nums;
    line-height: 1.15;
    padding: 0.25rem 0.5rem;
    rotate: -2deg;

    span {
      font-size: 0.5625rem;
      font-weight: 800;
      letter-spacing: 0.08em;
      text-transform: uppercase;
    }

    strong {
      font-size: 1rem;
    }

    &.status--neutral {
      --status-color: var(--envelope-ink);
      --status-text: var(--envelope-ink);
    }

    @media (width < 640px) {
      border-width: 1.5px;
      padding: 0.15rem 0.3rem;

      span {
        font-size: 0.55rem;
      }

      strong {
        font-size: 0.75rem;
      }
    }
  }

  .envelope__ink--outcome {
    align-items: center;
    background-color: color-mix(in oklab, var(--color-canvas) 70%, transparent);
    border: 3px solid color-mix(in oklab, var(--status-color) 80%, transparent);
    border-radius: var(--radius-sm);
    padding: 0.3rem 0.75rem;
    rotate: -6deg;

    @media (width < 640px) {
      border-width: 1.5px;
      padding: 0.15rem 0.4rem;
    }

    /* Beside the spam check it is struck nearer the middle, and in a double frame. */
    &:not(:last-child) {
      border: 4px double color-mix(in oklab, var(--status-color) 80%, transparent);
      margin-inline: auto;
      padding: 0.375rem 1rem;
      rotate: -9deg;

      @media (width < 640px) {
        border-width: 3px;
        margin-inline: 0;
        padding: 0.2rem 0.45rem;
      }
    }

    span {
      font-size: 0.6875rem;
      font-weight: 700;
      letter-spacing: 0.04em;

      @media (width < 640px) {
        font-size: 0.55rem;
      }
    }

    strong {
      font-size: 1.15rem;
      font-weight: 900;
      letter-spacing: 0.07em;
      line-height: 1.05;
      text-transform: uppercase;

      @media (width < 640px) {
        font-size: clamp(0.6875rem, 3.4cqi, 1.15rem);
      }
    }
  }

  /* On a wide envelope the inks rise beside the address; a held stamp stays down, off the recipient.
     An outcome rises level with the MX line when there is one. The address's lines end short of
     the widest the ink beside them can be, so no text sits under one. */
  @media (width >= 640px) {
    .envelope {
      --ink-reach: 7rem;

      &:has(.envelope__mx) {
        --ink-reach: 10.5rem;
      }
    }

    .envelope__address {
      padding-inline-end: calc(var(--ink-reach) - var(--stamp-size) - 0.5rem);
    }

    .envelope__inks {
      align-items: flex-end;
    }

    .envelope__ink {
      margin-block-start: -2.75rem;

      &:last-child {
        max-inline-size: 7rem;
        text-align: end;
      }
    }

    .envelope__ink--outcome:only-child {
      max-inline-size: 10.5rem;

      .envelope:has(.envelope__mx) & {
        margin-block-start: -4rem;
      }
    }

    .envelope__ink--outcome:not(:last-child) {
      margin-block-start: 0.375rem;

      strong {
        font-size: 1.875rem;
        letter-spacing: 0.12em;
      }
    }
  }

  .envelope__barcode {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    min-inline-size: 0;

    @media (width < 640px) {
      flex: 1 1 0;
      gap: 0.2rem;
    }
  }

  .envelope__bars {
    align-items: flex-start;
    block-size: 16px;
    display: flex;
    gap: 2px;
    overflow: hidden;

    @media (width < 640px) {
      block-size: 10px;
      gap: 1px;
    }
  }

  /* The four bar shapes of a postal barcode: full, ascender, descender, tracker. */
  .envelope__bar {
    background-color: var(--color-ink);
    block-size: 16px;
    flex: none;
    inline-size: 2px;

    @media (width < 640px) {
      block-size: 10px;
      inline-size: 1px;
    }
  }

  .envelope__bar--1 {
    block-size: 11px;

    @media (width < 640px) {
      block-size: 7px;
    }
  }

  .envelope__bar--2 {
    block-size: 11px;
    margin-block-start: 5px;

    @media (width < 640px) {
      block-size: 7px;
      margin-block-start: 3px;
    }
  }

  .envelope__bar--3 {
    block-size: 6px;
    margin-block-start: 5px;

    @media (width < 640px) {
      block-size: 4px;
      margin-block-start: 3px;
    }
  }

  /* Numbers and ids take tabular figures, addresses not: Adwaita Sans widens a hyphen under them. */
  .envelope__ids {
    color: var(--envelope-ink);
    font-size: 0.625rem;
    font-variant-numeric: tabular-nums;
    letter-spacing: 0.12em;
    overflow-wrap: anywhere;

    @media (width < 640px) {
      font-size: 0.55rem;
      letter-spacing: 0.06em;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }

  .envelope__queue-id {
    text-transform: uppercase;
  }

  /* A look-alike sender, shown as punycode, is flagged as phishing before the address. */
  .envelope__phishing {
    color: var(--color-warning-text);
    font-size: 0.75em;
    font-weight: 800;
    letter-spacing: 0.04em;
    margin-inline-end: 0.4em;
    text-transform: uppercase;
  }
}
