.avatar {
  --avatar-border-radius: 50%;

  aspect-ratio: 1;
  border-radius: var(--avatar-border-radius);
  display: grid;
  inline-size: var(--avatar-size, 5ch);
  margin: 0;
  place-items: center;
  position: relative;

  img {
    aspect-ratio: 1;
    block-size: auto;
    border-radius: var(--avatar-border-radius);
    grid-area: 1/1;
    inline-size: var(--avatar-size, 5ch);
    max-inline-size: 100%;
    object-fit: cover;

    .banned & {
      opacity: 0.5;
    }
  }

  .banned &:after {
    background: url("/assets/cancel-c6dfeb78.svg") no-repeat center center;
    block-size: auto;
    content: "";
    filter: invert(0%);
    inline-size: var(--avatar-size, 5ch);
    inset: 0;
    max-inline-size: 100%;
    position: absolute;

    @media (prefers-color-scheme: dark) {
      filter: invert(100%);
    }
  }
}

.avatar--icon {
  border: 1px solid var(--color-border-darker);
  background-color: var(--color-text-reversed);

  img {
    background-color: transparent;
    border-radius: 0;
    inline-size: auto;
    margin: var(--inline-space);
  }

  @media (any-hover: hover) {
    &:where(:not(:active):hover) {
      box-shadow: 0 0 0 var(--hover-size) var(--hover-color);
    }
  }
}

.avatar__group {
  --avatar-size: 2.5ch;

  block-size: 5ch;
  display: grid;
  gap: 1px;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: min-content;
  inline-size: 5ch;
  place-content: center;

  .avatar {
    margin: auto;
  }

  &:where(:has(> :last-child:nth-child(2))) {
    --avatar-size: 3.5ch;

    > :first-child {
      margin-block-end: 1.5ch;
      margin-inline-end: -0.75ch;
    }

    > :last-child {
      margin-block-start: 1.5ch;
      margin-inline-start: -0.75ch;
    }
  }

  &:where(:has(> :last-child:nth-child(3))) {
    > :last-child {
      margin-inline: 1.25ch -1.25ch;
    }
  }
}

.avatar__form {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
}

/* Account logo */
.account-logo {
  --avatar-border-radius: 0.5em;

  #nav & {
    block-size: var(--btn-size);
    inline-size: auto;
  }
}

/* Tier / role badge — a solid tier-colour chip with a white icon in the
   avatar's corner, the way Discord shows roles. White-on-colour reads at any
   size (a multicolour emoji never did), and a notch is masked out of the avatar
   so it never crowds or blends into the face. */
.avatar__badge {
  position: absolute;
  right: 0;
  bottom: 0; /* physical: message panes flip direction for scrollbar placement */
  transform: translate(18%, 18%); /* straddle the corner so it hangs off, not over the face */

  /* Sized purely as a fraction of the avatar so it scales with it everywhere —
     NO pixel floor, or a tiny avatar (a reaction chip, an autocomplete token)
     would keep a full-size badge and swallow the face. Only a max cap, for the
     rare oversized avatar. */
  container-type: size;
  display: grid;
  place-items: center;
  inline-size: 30%; /* best-practice ~0.3× the avatar (Discord/Slack presence scale) */
  block-size: 30%;
  max-inline-size: 20px;
  max-block-size: 20px;

  background-color: var(--badge-color, #3B4B59);
  border-radius: 50%;
  box-shadow:
    0 0 0 1px oklch(0% 0 0 / 0.15),
    0 1px 2px oklch(0% 0 0 / 0.35);
  pointer-events: auto; /* hoverable for its tooltip; clicks still bubble to the avatar link */
  transform-origin: center;
  transition: transform 0.14s ease;
}

/* Hovering (or focusing) the avatar grows the pip so the icon is easy to read;
   the badge-tooltip controller floats the label chip (below) beside it. */
.avatar:where(:hover, :focus-visible, :focus-within) .avatar__badge {
  transform: translate(18%, 18%) scale(1.6);
  z-index: 2;
}

/* Floating badge label — positioned by badge_tooltip_controller (fixed +
   viewport-clamped), so it never clips inside a scroll container. */
.badge-tip {
  position: fixed;
  z-index: 1000;
  padding: 0.14em 0.6em;
  border-radius: 0.7em;
  background-color: var(--tip-color, #22232b);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 600;
  line-height: 1.5;
  white-space: nowrap;
  pointer-events: none;
  box-shadow: 0 2px 8px oklch(0% 0 0 / 0.3);
  text-shadow: 0 1px 1px oklch(0% 0 0 / 0.35);
  opacity: 0;
  transform: translateY(3px);
  transition: opacity 0.12s ease, transform 0.12s ease;
}

.badge-tip--shown {
  opacity: 1;
  transform: translateY(0);
}

.avatar__badge-glyph {
  inline-size: 60cqmin;
  block-size: 60cqmin;
  background-color: #fff; /* the icon, painted white via the mask */
  -webkit-mask: var(--badge-icon) center / contain no-repeat;
  mask: var(--badge-icon) center / contain no-repeat;
  /* a hair of shadow so the white icon keeps its edge even on a light chip */
  filter: drop-shadow(0 0.5px 0.5px oklch(0% 0 0 / 0.45));
}

/* Discord's trick: punch a notch out of the avatar where the badge sits, so it
   always lands in a gap of surface colour and never crowds or blends into a
   same-coloured avatar. Centred on the badge (~91%,91% with the translate) and
   just big enough to clear it, so it bites only the corner — never the face. */
.avatar:has(> .avatar__badge) > img {
  -webkit-mask-image: radial-gradient(circle at 91% 91%, #0000 19%, #000 20%);
  mask-image: radial-gradient(circle at 91% 91%, #0000 19%, #000 20%);
}
