.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;

  > 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;
  }
}

.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;
  }
}

/* Monogram avatars for users without images */
.avatar-monogram {
  /* Match the same structure as .avatar */
  aspect-ratio: 1;
  border-radius: var(--avatar-border-radius);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  
  /* Text styling */
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-weight: 900;
  color: white;
  text-transform: uppercase;
  user-select: none;
  white-space: nowrap;
  overflow: hidden;
  
  /* Make text fit within container */
  font-size: calc(var(--avatar-size) / 2);
  
  /* Performance optimizations */
  contain: layout style paint;
  
  /* Ensure crisp text rendering */
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;

  box-shadow: rgb(255, 255, 255) 0px 0px 0px 0px, rgb(0, 0, 0) 0px 0px 0px 1px, rgba(0, 0, 0, 0) 0px 0px 0px 0px;
}

/* Generate color classes for each avatar color */
.avatar-monogram--0 { background-color: #AF2E1B !important; }
.avatar-monogram--1 { background-color: #CC6324 !important; }
.avatar-monogram--2 { background-color: #3B4B59 !important; }
.avatar-monogram--3 { background-color: #BFA07A !important; }
.avatar-monogram--4 { background-color: #ED8008 !important; }
.avatar-monogram--5 { background-color: #ED3F1C !important; }
.avatar-monogram--6 { background-color: #BF1B1B !important; }
.avatar-monogram--7 { background-color: #736B1E !important; }
.avatar-monogram--8 { background-color: #D07B53 !important; }
.avatar-monogram--9 { background-color: #736356 !important; }
.avatar-monogram--10 { background-color: #AD1D1D !important; }
.avatar-monogram--11 { background-color: #BF7C2A !important; }
.avatar-monogram--12 { background-color: #C09C6F !important; }
.avatar-monogram--13 { background-color: #698F9C !important; }
.avatar-monogram--14 { background-color: #7C956B !important; }
.avatar-monogram--15 { background-color: #5D618F !important; }
.avatar-monogram--16 { background-color: #3B3633 !important; }
.avatar-monogram--17 { background-color: #67695E !important; }

/* Dark mode adjustments */
@media (prefers-color-scheme: dark) {
  .avatar-monogram {
    color: rgba(255, 255, 255, 0.95);
  }
  
  /* Slightly brighten dark backgrounds in dark mode */
  .avatar-monogram--2,
  .avatar-monogram--16 {
    filter: brightness(1.2);
  }
}
