.message-area {
  display: contents;

  &:has(.message) {
    .message-area--empty {
      display: none;
    }
  }

  &:not(:has(.message)) {
    --opacity: 0.2;

    block-size: 100vh;
    display: grid;
    place-content: center stretch;
    place-items: center center;

    > * {
      grid-area: 1/1;
    }

    .message-area--empty > figure img {
      inline-size: 30dvw;
    }
  }
}

.message-area__return-to-latest {
  outline: 0 solid var(--color-selected-dark);
  inset-block-end: var(--footer-height);
  inset-inline-start: calc(50% - calc(var(--btn-size) / 2));
  position: absolute;
  
  &.pulse {
    animation: pulsing-outline 2s infinite both;
  }
}

#system_welcome {
  --avatar-size: 5ch;

  display: block;
  margin-block-start: calc(var(--avatar-size) * 2);
  position: relative;
  z-index: 1;

  .avatar {
    margin-block-start: calc(var(--avatar-size) * -0.5);
  }

  .message__body {
    display: block;
    max-inline-size: unset;
  }

  .message__body-content {
    overflow: unset;
  }

  .system-welcome--translation {
    inset-block-start: var(--block-space);
    inset-inline-start: var(--block-space);
    position: absolute;
  }

  .invite-label,
  .btn--regenerate {
    display: none;
  }
}

.messages {
  --message-column-gap: 0.66ch;
  --message-row-gap: 0.1em;
  --message-space: 1.33em;

  display: grid;
  flex: 1;
  grid-auto-rows: min-content;
  overflow-y: auto;
  overscroll-behavior: contain;
  position: relative;
  row-gap: var(--message-space);

  @media (max-width: 120ch) {
    padding-block-start: var(--navbar-height);
  }
}

.message {
  --content-padding-block: 0.66rem;
  --content-padding-inline: calc(var(--inline-space) * 1.5);

  column-gap: var(--message-column-gap);
  display: grid;
  grid-auto-columns: var(--inline-space-double) 1fr min-content;
  grid-auto-rows: min-content;
  grid-template-areas:
    "loading_up loading_up loading_up"
    "new new new"
    "sep sep sep"
    "avatar body body"
    "loading_down loading_down loading_down";
  padding-inline: calc(var(--inline-space) + 1vw);
  position: relative;
  row-gap: var(--message-row-gap);
  visibility: hidden;
  z-index: 0;

  .message__boost-inline {
    display: none;
  }

  &:last-of-type {
    &:has(.boost__reactions) {
      .message__body {
        padding-block-end: 3.2rem;
      }
    }
  }

  &.message--formatted {
    visibility: visible;
  }

  &:has([open]) {
    z-index: 1;

    [open] .message__options-btn {
      opacity: 1;
    }

    [open] .popup-menu {
      display: block;
    }
  }

  &:has(.boost-item) {
    .message__boost-inline {
      display: inline-flex;
    }
  }

  + .message--threaded {
    margin-block-start: calc((var(--message-space) + (var(--message-row-gap) * 2) - 2px) * -1);
  }

  &.message--failed {
    opacity: 0.5;

    .message__meta::after {
      content: "🚫";
      font-size: 1em;
      line-height: 1;
    }

    .message__body-content {
      animation: wiggle 0.4s both;
      outline: 1px dashed var(--color-negative);
    }
  }
}

.message-separator {
  align-items: center;
  font-size: 0.8rem;
  font-weight: 600;
  grid-template-columns: 1fr auto 1fr;
  inline-size: 100%;
  text-align: center;
  text-transform: uppercase;

  time,
  span {
    padding: 0.66em 2.33ch;
    background-color: var(--color-message-bg);
    border-radius: 3em;
  }

  &::after,
  &::before {
    content: "";
  }
}

.message__loading-indicator {
  display: none;
  visibility: hidden;
  margin-block: var(--message-space);

  span {
    background-color: var(--color-message-bg);
  }
  
  &:first-of-type {
     grid-area: loading_up;
  }

  &:last-of-type {
     grid-area: loading_down;
  }

  .message--loading-up &:first-of-type, .message--loading-down &:last-of-type {
    display: block;
    visibility: visible;
  }
}

.message__day-separator {
  display: none;
  grid-area: sep;
  margin-block: var(--message-space);
  visibility: hidden;

  time,
  span {
    background-color: var(--color-message-bg);
  }

  &::after,
  &::before {
    border-top: 2px solid var(--color-message-bg);
  }

  .message--first-of-day & {
    display: grid;
    visibility: visible;
  }
}

.message__new-separator + .message__day-separator {
  display: grid !important;
  visibility: visible !important;
  margin-top: 0; /* Reduce top margin since it's right after the NEW separator */
}

.message__new-separator {
  grid-area: new;
  margin-block: calc(var(--message-space) / 2);
  display: grid;

  span {
    background-color: transparent;
    color: var(--color-contrast);
  }

  &::after,
  &::before {
    border-top: 2px solid var(--color-contrast);
  }

  .message:first-child & {
    margin-block-start: var(--message-space);
  }
}

.message__avatar {
  --avatar-border-size: 1px;
  --avatar-size: 3.5ch;

  grid-area: avatar;
  z-index: 2;
  display: flex;
  box-shadow: 0 0 0 var(--hover-size) var(--color-bg);
}

.message__meta {
  align-items: center;
  column-gap: calc(var(--message-column-gap) / 2);
  display: flex;
  justify-content: start;
  line-height: 1.5;
  min-inline-size: 0;
  margin: 0;
  padding-inline-end: calc(var(--btn-size) / 2);

  .message__meta-block {
    font-size: 0.8rem;
    font-weight: 400;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .message__answered {
    color: var(--color-border-darkest); 

    &::before {
      content: "✓";
    }
     
    &.message__answered-by-me {
      color: var(--color-positive);
    } 
  }

  .message__original-room, .message__room {
    &::before {
      content: "→";
    }

    .message--me & {
      &::after {
        content: "←";
      }
    
      &::before {
        content: "";
      }
    }
  }

  .message__room {
    display: none;
    visibility: hidden;
  }

  .message__original-room {
    display: inline-block;
    visibility: visible;
  }

  .message__heading {
    align-items: center;
    column-gap: calc(var(--message-column-gap) / 2);
    display: inline-flex;
    margin: 0;
    min-inline-size: 0;
  }

  .message__actions {
    @media (max-width: 120ch) {
      .message__options-btn {
        --btn-padding: 0 0.5em;

        aspect-ratio: unset;
        block-size: auto;
        border-radius: 0.3em;
      }
    }

    @media (min-width: 120ch) {
      inset-block-start: 50%;
      inset-inline-start: calc(100% + var(--message-column-gap) / 2);
      position: absolute;
      transform: translate(0, -50%);
    }
  }
}

.message__author {
  font-size: 0.8rem;
  font-weight: 400;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message__permalink {
  color: var(--color-text);
  font-size: 0.8rem;
  font-weight: 400;
  opacity: 0.5;
  text-decoration: none;
  white-space: nowrap;
}

.message__body {
  column-gap: var(--inline-space-half);
  display: flex;
  grid-area: body;
  max-inline-size: min(80ch, 85%);
  position: relative;

  .message--me & {
    flex-direction: row-reverse;
  }

  .thread__link {
    display: block;
    background-color: var(--color-bg);
    border-radius: 0.5em;
    border: 1px solid var(--color-border);
    font-family: sans-serif;
    font-size: 0.9rem;
    margin: var(--block-space-half) 0 calc(var(--block-space-half) / 2) 0;
    padding: 1em;
    word-wrap: break-word;
    text-decoration: none;
    color: var(--link-color, var(--color-link));

    @media (any-hover: hover) {
      &:where(:not(:active):hover) {
        --hover-filter: brightness(1.3);
      
        filter: var(--hover-filter);
      }
    }

    span {
      text-decoration: underline;
    }
  }

  @media (max-width: 120ch) {
    max-inline-size: 100%;
  }
}

.message__body-content {
  background-color: var(--message-background, var(--color-message-bg));
  border-radius: 0.66em;
  color: var(--message-color, var(--color-text));
  font-weight: 400;
  inline-size: fit-content;
  margin-block-end: var(--message-row-gap);
  max-inline-size: 100%;
  padding: var(--content-padding-block) var(--content-padding-inline);
  position: relative;
  word-break: break-word;

  &:has(.message__attachment) {
    margin-block-end: 0.33em;
    padding: 0;

    &:has(.boost) {
      padding-block-end: var(--block-space-half);

      .boosts {
        padding-inline: var(--inline-space);
      }
    }

    .message__meta {
      padding: var(--content-padding-block) var(--content-padding-inline);
    }
  }

  &.message__body-content--editing:has(.message__attachment) {
    max-inline-size: calc(100% - var(--btn-size));
  }

  &:has(.message__pending-upload) {
    padding: 0;
  }
}

.message__attachment {
  block-size: auto;
  border: 1px solid var(--color-border);
  border-radius: 0.66em;

  .message__body-content:has(.boost) & {
    border-end-start-radius: 0;
    border-end-end-radius: 0;
  }

  .message:not(.message--threaded) & {
    border-start-end-radius: 0;
    border-start-start-radius: 0;
  }
}

img.message__attachment {
  cursor: zoom-in;
}

.message__actions {
  align-content: center;
  display: flex;
  flex-wrap: wrap;
  justify-content: start;
}

.message__action-btn {
  --btn-background: transparent;
  --btn-border-color: transparent;
  --outline-offset: 0;
  --width: 25%;

  flex-basis: var(--width);
  font-size: 0.8rem;

  @media (hover: hover) and (pointer: fine) {
    &:where(:not(:active):hover) {
      --btn-background: var(--color-border-dark);
      --hover-size: 0;

      opacity: 1;
    }
  }

  &:where(:not(:active)):focus-visible {
    opacity: 1;
  }

  .message__actions-menu & {
    --width: 100%;
    --btn-padding: calc(var(--inline-space-half) / 2) var(--inline-space-half);
    --btn-border-radius: calc(var(--arrow-size) / 2 - var(--inline-space-half));
    --busy-inset: calc(var(--inline-space) * 1.5);
  
    justify-content: start;
    font-weight: 500;
  } 
}

.message__edit-btn {
  display: none;

  .admin &,
  .message--me & {
    display: inline-flex;
  }
}

.popup-menu {
  --arrow-size: 1.5em;
  --boost-border-color: var(--color-selected-dark);
  --border-color: var(--color-border-darker);
  --border-size: 1px;

  background-color: var(--color-bg);
  border-radius: calc(var(--arrow-size) / 2);
  display: none;
  padding: var(--inline-space-half);
  position: absolute;

  &::before {
     aspect-ratio: 2;
     background: var(--border-color);
     clip-path: polygon(50% 0, 100% 100%, 0 100%);
     inline-size: var(--arrow-size);
     content: "";
     display: block;
     inset-block-start: calc(var(--arrow-size) / -2);
     inset-inline-start: 50%;
     position: absolute;
     transform: translateX(-50%);
   }
  
  &::after {
     aspect-ratio: 2;
     background: var(--color-bg);
     clip-path: polygon(50% 0, 100% 100%, 0 100%);
     inline-size: var(--arrow-size);
     content: "";
     display: block;
     inset-block-start: calc((var(--arrow-size) / -2) + var(--border-size) * 1.5);
     inset-inline-start: 50%;
     position: absolute;
     transform: translateX(-50%);
   }

  /*@media (max-width: 100ch) {*/
  /*  inset-block-start: calc(var(--btn-size) / 1.5);*/
  /*}*/

  .popup-orientation-top & {
    inset-block-end: var(--btn-size);
    inset-block-start: auto;
    transform-origin: bottom left;

    &::before {
      clip-path: polygon(50% 100%, 100% 0, 0 0);
      inset-block-end: auto;
      inset-block-start: 100%;
    }

    &::after {
      clip-path: polygon(50% 100%, 100% 0, 0 0);
      inset-block-end: auto;
      inset-block-start: calc(100% - var(--border-size) * 1.5);
    }

    /*@media (max-width: 100ch) {*/
    /*  inset-block-end: calc(var(--btn-size) / 1.5);*/
    /*}*/
  }
}

.message__actions-menu {
  --arrow-size: 1.5em;
  
  inset: var(--btn-size) auto auto 50%;
  transform: translateX(-50%);
  transform-origin: top left;
  z-index: 3;
}

.message__avatar-menu {
  --block-space: 1rem;
  --arrow-size: 1.1em;
  --avatar-border-size: 1px;

  inline-size: fit-content;
  width: 20ch;
  padding: calc(var(--block-space) * 2/3);
  
  inset: calc(var(--avatar-size) + var(--avatar-border-size) * 2 + var(--arrow-size) / 2) auto auto auto;
  transform-origin: top left;
  z-index: 4;

  &::before {
     inset-block-start: calc(var(--arrow-size) / -2);
     inset-inline-start: 0;
     transform: translateX(calc(var(--avatar-size) / 2 + var(--arrow-size) / -2));
   }
  
  &::after {
     inset-block-start: calc((var(--arrow-size) / -2) + var(--border-size) * 1.5);
     inset-inline-start: 0;
     transform: translateX(calc(var(--avatar-size) / 2 + var(--arrow-size) / -2));
  }
  
  .popup-orientation-top & {
    inset-block-end: calc(var(--avatar-size) + var(--avatar-border-size) * 2 + var(--arrow-size) / 2);
    inset-block-start: auto;
    transform-origin: bottom left;
  }
}

.boost__avatar .message__avatar-menu {
  --arrow-size: 0.9em;

  .btn {
    padding: var(--btn-padding, 0.5em 1.1em);
  }
}

.mention .message__avatar-menu {
  --avatar-size: 1em;
  --avatar-border-size: 0px;
  --arrow-size: 0.7em;

  padding: var(--block-space);
}

.trix-content .message__avatar-menu {
  padding: var(--block-space);
}

/* Me */
.message--me {
  grid-auto-columns: min-content 1fr var(--inline-space-double);
  grid-template-areas:
    "loading_up loading_up loading_up"
    "new new new"
    "sep sep sep"
    "avatar body body"
    "loading_down loading_down loading_down";

  :is(.message__body, .message__meta, .message__heading) {
    justify-self: end;
  }

  :is(.message__meta, .message__heading) {
    flex-direction: row-reverse;
    justify-content: end;
  }

  .message__meta {
    padding-inline: calc(var(--btn-size) / 2) 0;
  }

  .message__avatar {
    margin-inline: calc(var(--content-padding-inline) * -1) 0;
  }

  .message__actions {
    justify-content: end;

    @media (min-width: 120ch) {
      inset-inline-end: calc(100% + var(--message-column-gap) / 2);
      inset-inline-start: auto;
    }
  }
}

.message--mentioned {
  .message__body-content {
    --message-background: oklch(var(--lch-red) / 0.1);

    animation: wiggle 0.4s both;

    @media (prefers-color-scheme: dark) {
      --message-background: oklch(var(--lch-red) / 0.2);
    }
  }
}

.message--threaded {
  .message__author,
  .message__avatar {
    display: none;
  }
}

.message__edit-btns div:only-of-type {
  margin-inline: auto;
}

.message--emoji:not(:has(.composer--edit)) {
  .message__body-content {
    --message-background: transparent;

    border-radius: 0;
    margin-block-end: 0;
    padding: var(--content-padding-block) 0 var(--content-padding-block) var(--content-padding-inline);
    text-shadow: 0 0 3px var(--color-text-reversed);

    .trix-content {
      font-size: 2.4em;
      line-height: 1.4;
    }

    .message--me & {
      padding: var(--content-padding-block) var(--content-padding-inline) var(--content-padding-block) 0;
      text-align: right;
    }

    :is(.message--threaded) & {
      padding-block-start: 0;
    }
  }
}

.message.search-highlight {
  .message__body-content {
    border: 2px solid var(--color-text);
    animation: border-fade-out 0.5s 1s forwards;
  }
}

/* Pending uploads */
.message__pending-upload {
  border-radius: 0.66em;
  overflow: hidden;
  padding: var(--content-padding-block) var(--content-padding-inline);
  position: relative;

  &::before {
    background-color: var(--color-border-dark);
    block-size: 100%;
    content: "";
    inline-size: 100%;
    inset-inline-start: var(--percentage);
    position: absolute;
    transform: translateX(-100%);
    z-index: 1;
  }

  > div {
    z-index: 2;
  }

  span {
    inline-size: 3rem;
    display: inline-block;
  }

  .composer__file-thumbnail {
    aspect-ratio: 1;
    background-size: contain;
    inline-size: 1.4em;
  }
}
