.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"
    "replies_sep replies_sep replies_sep"
    "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,
.message__replies-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__replies-separator {
  grid-area: replies_sep;
  margin-block: var(--message-space);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  font-size: 0.8rem;
  font-weight: 600;
  text-align: center;
  text-transform: uppercase;
  visibility: visible;

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

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

.message__day-separator--parent {
  display: grid !important;
  visibility: visible !important;
}

.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: space-between;
  line-height: 1.5;
  inline-size: 100%;
  margin: 0;

  .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 {
    a {
      color: inherit;
      text-decoration: none;
    }
  }

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

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

  .message__original-room--with-in {
    &::before {
      content: "";
    }
  }

  .message__room-in {
    margin-inline-start: 0.25em;
  }

  .message__room-link {
    color: inherit;
    text-decoration: none;
    margin-inline-start: 0.25em;

    @media (any-hover: hover) {
      &:where(:not(:active):hover) {
        text-decoration: underline;
      }
    }
  }

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

  .message__actions {
    .message__options-btn {
      --btn-padding: 0 0.5em;

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

.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;
  inline-size: 100%;

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

  .threads {
    margin-block-start: calc(var(--block-space-half) / 2);

    &:empty {
      display: none;
    }
  }

  .thread__link {
    font-family: sans-serif;
    font-size: 0.85em;
    text-decoration: none;
    color: inherit;
    gap: 0.5em;
    align-items: center;
    padding: 0.25em 0.5em;
    margin: -0.25em -0.5em;
    border-radius: 0.3em;
    flex-wrap: wrap;

    @media (any-hover: hover) {
      &:where(:not(:active):hover) {
        background-color: var(--color-message-bg);
        filter: brightness(0.9);
      }
    }

    .txt-muted {
      color: var(--color-text-muted, #666);
      font-size: 0.95em;
    }
  }

  .thread__avatar {
    --avatar-size: 20px;

    inline-size: auto;
  }

  @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: 100%;
  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;

  p:last-child {
    margin-block-end: 0;
  }

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

img.message__attachment {
  cursor: zoom-in;
}

.message__hd-badge {
  background: var(--color-text);
  border-radius: 0.3em;
  color: var(--color-text-reversed);
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1;
  padding: 0.25em 0.4em;
  position: absolute;
  inset-block-start: 0.5em;
  inset-inline-end: 0.5em;
  pointer-events: none;
}

.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) 0 auto auto;
  transform-origin: top right;
  z-index: 3;
  min-width: 180px;

  &::before {
    inset-inline-start: auto;
    inset-inline-end: calc(var(--btn-size) / 2);
    transform: translateX(0);
  }

  &::after {
    inset-inline-start: auto;
    inset-inline-end: calc(var(--btn-size) / 2);
    transform: translateX(0);
  }
}

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


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

  padding: var(--block-space);

  /* Constrain all images inside the popup by default */
  section img {
    max-inline-size: 20px;
    max-block-size: 20px;
    min-block-size: 0;
    min-inline-size: 0;
    inline-size: auto;
    block-size: auto;
  }

  /* Profile card avatar: restore proper size */
  .avatar.txt-xx-large {
    --avatar-size: 3.5ch;
    inline-size: 3.5ch;
    block-size: 3.5ch;

    img {
      inline-size: 100%;
      block-size: 100%;
      max-inline-size: none;
      max-block-size: none;
      object-fit: cover;
      border-radius: 50%;
    }
  }
}

.lexxy-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"
    "replies_sep replies_sep replies_sep"
    "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;
  }
}

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

    .lexxy-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-block-start: 0;
    inset-inline-start: 0;
    position: absolute;
    transform: translateX(calc(var(--percentage) - 100%));
    transition: transform 300ms ease-out;
    z-index: 1;
  }

  &[data-phase="compressing"]::before,
  &[data-phase="finishing"]::before {
    transform: translateX(0);
    opacity: 0.4;
    animation: pulse-bg 1.5s ease-in-out infinite;
  }

  > div:not(.message__pending-upload-overlay) {
    position: relative;
    z-index: 2;
  }

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

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

/* Image preview variant */
.message__pending-upload--image {
  padding: 0;

  &::before {
    display: none;
  }

  .message__attachment, .message__pending-upload-preview {
    border: 1px solid var(--color-border);
    border-radius: 0.66em;
  }
}

.message__pending-upload-preview {
  display: block;
  filter: blur(2px) brightness(0.7);
  inline-size: 100%;
  block-size: 100%;
  object-fit: cover;
}

.message__pending-upload-overlay {
  align-items: center;
  display: flex;
  flex-direction: column;
  gap: 0.5em;
  inset: 0;
  justify-content: center;
  position: absolute;
  z-index: 2;
}

.message__pending-upload-status {
  background: oklch(0% 0 0 / 0.55);
  border-radius: 2em;
  color: white;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0.5em 1.4em;

}

.message__pending-upload-bar {
  background: oklch(100% 0 0 / 0.25);
  block-size: 4px;
  border-radius: 2px;
  inline-size: 60%;
  overflow: hidden;

  &::after {
    background: white;
    block-size: 100%;
    content: "";
    display: block;
    inline-size: var(--percentage);
    transition: inline-size 300ms ease-out;
  }
}

.message__upload-spinner {
  display: inline-block;
}

@keyframes pulse-bg {
  0%, 100% { opacity: 0.25; }
  50% { opacity: 0.5; }
}

/* Messages — msg- prefix */

/* Messages container spacing */
[id^="messages_"] {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-bottom: 4px;
}

/* .messages grid holding .msg elements (inbox, search results) */
.messages:has(.msg) {
  --message-space: 4px;
}

/* Ensure short messages have enough width for action buttons */
.msg > .min-w-0 {
  min-width: 12rem;
}

/* Animation */
.msg { animation: fadeUp 0.2s ease both; }
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Day separator: hidden by default, shown by Stimulus via msg--first-of-day */
.msg-day-sep { display: none; }
.msg--first-of-day .msg-day-sep { display: flex; flex-basis: 100%; }

/* Unread + day separators span full width in flex container */
.msg > .day-sep { flex-basis: 100%; }
.msg { flex-wrap: wrap; }

/* Avatar — overlaps bubble top-left */
.msg .msg-avatar {
  width: 32px; height: 32px; border-radius: 50%;
  overflow: hidden; flex-shrink: 0; cursor: pointer;
  margin-top: -4px; margin-right: -8px;
  position: relative; z-index: 2;
  border: 2px solid var(--bg-pure);
}
.msg .msg-avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; }
.msg .msg-avatar details { display: contents; }

/* Content column — shrinks to fit text, doesn't stretch full width */
.msg-content {
  display: flex; flex-direction: column;
  min-width: 0; max-width: 650px;
}

/* Smiley trigger — centered with bubble via msg-bubble-row wrapper */
.msg-smiley { flex-shrink: 0; margin-left: 8px; }
.msg-bubble-row { display: flex; align-items: center; gap: 0; }

/* Boosts — overlapping bottom edge of bubble */
.msg-boosts {
  margin-top: -8px; padding: 0 8px;
  position: relative; z-index: 1;
}

/* Header inside bubble */
.msg-header { line-height: 1.2; }

/* Action buttons */
.msg-actions { line-height: 0; }

/* Own messages (Stimulus adds msg--me) */
.msg--me { flex-direction: row-reverse; }
.msg--me .msg-avatar { display: none; }
.msg--me .msg-content { align-items: flex-end; }
.msg--me .msg-body {
  background: var(--color-msg-me);
  color: var(--color-msg-me-text);
  border-radius: 16px;
  text-align: left;
}
.msg--me .msg-body a:not(.msg-dropdown__item):not(.msg-action-btn):not(.msg-header a):not(.msg-indicators a):not(.no-underline) { color: var(--color-msg-me-text); text-decoration: underline; }
.msg--me .msg-header a { text-decoration: none; }
.msg--me .msg-body code { background: oklch(100% 0 0 / 0.15); color: var(--color-msg-me-text); }
.msg--me .msg-indicators,
.msg--me .msg-indicators a,
.msg--me .msg-indicators .text-accent,
.msg--me .msg-indicators [id^="answered_by"]::before { color: oklch(100% 0 0 / 0.85) !important; text-decoration: none !important; }
.msg--me .msg-body .lexxy-content blockquote {
  border-left-color: oklch(100% 0 0 / 0.5) !important;
  background: oklch(100% 0 0 / 0.1);
  color: oklch(100% 0 0 / 0.75);
  opacity: 1;
}
.msg--me .msg-body a.no-underline { color: oklch(100% 0 0 / 0.85); text-decoration: none; }
.msg--me .msg-body a.no-underline .text-txt-muted { color: oklch(100% 0 0 / 0.6); }
.msg--me .msg-body a.no-underline .text-accent { color: oklch(100% 0 0 / 0.9); }
.msg--me .msg-author { display: none; }
.msg--me .msg-time { color: oklch(100% 0 0 / 0.65) !important; text-decoration: none !important; }
.msg--me .msg-action-btn { color: oklch(100% 0 0 / 0.6); }
.msg--me .msg-header { justify-content: flex-end; flex-wrap: nowrap; }
.msg--me .msg-smiley { order: -1; margin-left: 0; margin-right: 8px; }
.msg--me .msg-boosts { align-self: flex-end; }
.msg--me .msg-boost-btn {
  border: 1px solid oklch(100% 0 0 / 0.25);
  background: oklch(100% 0 0 / 0.1);
  color: oklch(100% 0 0 / 0.7);
}
.msg--me .msg-boost-btn:hover {
  border-color: oklch(100% 0 0 / 0.5);
  color: oklch(100% 0 0 / 0.9);
}

/* No more grouping — every message shows avatar+name */

/* Hide empty indicator wrappers so they don't create phantom flex gaps */
.msg-indicators > [id^="bookmarked_indicator"]:not(:has(.inline-flex)) { display: none; }
.msg-indicators > [id^="answered_by"]:not(:has(.inline-flex)) { display: none; }

/* Middot divider between bookmark + answered indicators — only when both are visible */
.msg-indicators:has([id^="bookmarked_indicator"] .inline-flex) > [id^="answered_by"]:has(.inline-flex)::before {
  content: "·"; color: var(--color-txt-muted); line-height: normal;
}
.msg-action-btn {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: none;
  background: none;
  cursor: pointer;
  display: grid;
  place-items: center;
  color: var(--color-txt);
  transition: color 100ms, background 100ms;
  text-decoration: none;
  padding: 0;
  outline: none;
  box-shadow: none;
}
.msg-action-btn:hover,
.msg-action-btn:focus,
.msg-action-btn:focus-visible {
  color: var(--color-txt);
  outline: none;
  box-shadow: none;
}

/* Boost smile button (add reaction) — same style as msg--me */
.msg-boost-btn {
  border: 1px solid oklch(100% 0 0 / 0.25);
  background: oklch(100% 0 0 / 0.1);
  color: oklch(100% 0 0 / 0.7);
  box-shadow: none;
  outline: none;
}
.msg-boost-btn:hover {
  border-color: oklch(100% 0 0 / 0.5);
  color: oklch(100% 0 0 / 0.9);
}

/* Actions always visible inside msg-body */

/* When dropdown is open, lift the entire message above siblings */
.msg:has(details[open]) { z-index: 20; }

/* Avatar sizing inside message */
.msg .msg-avatar img,
.msg .msg-avatar .avatar__image,
.msg .msg-avatar .avatar {
  width: 32px !important;
  height: 32px !important;
  border-radius: 9999px;
}
.msg .msg-avatar summary,
.msg .msg-avatar details { display: contents; }
.msg .msg-avatar .btn { padding: 0; border: none; background: none; }

/* Message body: bubble for all messages */
.msg-body {
  overflow-wrap: break-word; word-break: break-word;
  background: var(--color-surface);
  padding: 6px 12px 8px;
  border-radius: 16px;
}
.msg-body img, .msg-body video { max-width: min(100%, 480px); height: auto; border-radius: 12px; }
.msg-body .attachment,
.msg-body .message__attachment { max-width: min(100%, 480px); overflow: hidden; border-radius: 12px; }
.msg-body .message__attachment img { width: 100%; height: auto; display: block; }
.msg-body a:has(> .message__hd-badge) { max-width: 100%; overflow: hidden; border-radius: 12px; }
/* Center and constrain images/attachments in message body */
.msg-body img.message__attachment,
.msg-body a:has(> .message__attachment),
.msg-body a:has(> .message__hd-badge) { display: block; margin-inline: auto; }
.msg-body .max-inline-size { max-width: 100% !important; width: auto !important; aspect-ratio: auto !important; margin-inline: auto; }
/* Quoted messages (Citar) */
.msg-body .lexxy-content blockquote {
  border-left: 2px solid var(--color-border) !important;
  padding-left: 0.75em !important;
  margin: 2px 0 6px !important;
  font-style: italic;
  opacity: 0.8;
  line-height: 1.4;
}

.msg-body .lexxy-content blockquote p {
  margin: 0;
}

.msg-body .lexxy-content blockquote ul,
.msg-body .lexxy-content blockquote ol {
  margin: 2px 0;
  padding-left: 1.2em;
}
/* Lexxy/Action Text content */
.lexxy-content { line-height: 1.625; }
.lexxy-content a { color: var(--color-accent-strong); font-weight: 500; }
.lexxy-content code {
  font-family: ui-monospace, "SF Mono", "Cascadia Code", Menlo, monospace;
  font-size: 0.88em;
  background: var(--color-surface);
  padding: 2px 6px;
  border-radius: 5px;
  color: var(--color-accent-strong);
}
.lexxy-content img, .lexxy-content video { max-width: min(100%, 480px); height: auto; border-radius: 12px; }
.lexxy-content .attachment { display: block; margin: 8px auto; max-width: min(100%, 480px); }
.lexxy-content .attachment__caption { font-size: 0.85rem; color: var(--color-txt-muted); margin-top: 4px; }

/* HD badge on image attachments */
.position-relative { position: relative; }
.message__hd-badge {
  background: var(--color-txt);
  border-radius: 0.3em;
  color: var(--color-bg-pure);
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1;
  padding: 0.25em 0.4em;
  position: absolute;
  top: 0.5em;
  right: 0.5em;
  pointer-events: none;
}

/* Edit/delete: own messages or admin */
.msg-edit-btn { display: none; }
.msg--me .msg-edit-btn,
.admin .msg-edit-btn { display: block; }

/* Mentioned highlight — border on bubble instead of full-width background */
.msg--mentioned .msg-body {
  border-left: 3px solid var(--color-accent);
}

/* Message actions dropdown */
.msg-dropdown {
  display: none;
  position: absolute;
  line-height: normal;
  right: 0;
  top: 100%;
  margin-top: 4px;
  z-index: 30;
  background: var(--color-bg-pure);
  border: 1px solid var(--color-border);
  border-radius: 10px;
  box-shadow: 0 4px 16px oklch(25% 0.02 145 / 0.12);
  width: max-content;
  max-width: var(--max-width, 320px);
}
details[open] > .msg-dropdown { display: block; }

/* Open upward when near bottom */
.msg-dropdown--top .msg-dropdown {
  top: auto;
  bottom: 100%;
  margin-top: 0;
  margin-bottom: 4px;
}

/* Emoji reactions grid — 5 columns */
.msg-dropdown__reactions {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 2px;
  padding: 6px;
  border-bottom: 1px solid var(--color-border);
}
.msg-dropdown__emoji {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  border-radius: 6px;
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 1.1rem;
}
.msg-dropdown__emoji:hover { background: var(--color-hover); }

/* Emoji popover — triggered by smiley button below message */
.msg-emoji-popover {
  display: none;
  position: absolute;
  bottom: 100%;
  left: calc(50% + var(--popover-offset-x, 0px));
  transform: translateX(-50%);
  margin-bottom: 4px;
  z-index: 30;
  background: var(--color-bg-pure);
  border: 1px solid var(--color-border);
  border-radius: 10px;
  box-shadow: 0 4px 16px oklch(25% 0.02 145 / 0.12);
  padding: 6px;
  grid-template-columns: repeat(5, 1fr);
  gap: 2px;
}
details[open] > .msg-emoji-popover { display: grid; }

/* Open downward when near top */
.msg-emoji-popover--top .msg-emoji-popover {
  bottom: auto;
  top: 100%;
  margin-bottom: 0;
  margin-top: 4px;
}

/* Compact grid for very narrow viewports */
.msg-emoji-popover--compact {
  grid-template-columns: repeat(4, 1fr);
}

/* Menu items */
.msg-dropdown__items {
  padding: 4px 0;
}
.msg-dropdown__item {
  display: block;
  width: 100%;
  padding: 5px 12px;
  border: none;
  background: transparent;
  cursor: pointer;
  text-align: left;
  font-size: 0.82rem;
  color: var(--color-txt);
  text-decoration: none;
  white-space: nowrap;
}
.msg-dropdown__item:hover { background: var(--color-hover); }
.msg-dropdown__item--danger { color: var(--color-negative); }

/* ── Inline message edit ── */
.msg-edit {
  padding: 8px;
  animation: fadeUp 0.15s ease both;
}

.msg-edit__form { display: contents; }

/* Pill — mirrors composer-pill */
.msg-edit__pill {
  display: flex;
  flex-direction: column;
  background: var(--color-bg-pure);
  border: 1px solid var(--color-border);
  border-radius: 1.3rem;
  padding: 0.6rem 1rem;
  transition: border-color 150ms, box-shadow 150ms;
}

.msg-edit__pill:focus-within {
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px var(--color-accent-soft);
}

/* Lexxy editor inside edit pill */
.msg-edit__pill lexxy-editor {
  --lexxy-editor-rows: 1lh;
  border: none;
  box-shadow: none;
  display: flex;
  flex-direction: column;
}

.msg-edit__pill .lexxy-editor__content {
  padding: 0;
  outline: none;
  order: -1;
}

/* Toolbar always visible below content */
.msg-edit__pill lexxy-toolbar {
  display: flex;
  position: relative;
  gap: 0.2em;
  border: none;
  background: none;
  padding: 0.4em 0 0;
  min-height: 0;
  align-items: center;
  order: 1;
}

.msg-edit__pill .lexxy-editor__toolbar-button {
  --lexxy-toolbar-button-size: 1.75rem;
  background: transparent;
  border-radius: 6px;
}

.msg-edit__pill .lexxy-editor__toolbar-button[aria-pressed="true"],
.msg-edit__pill [open] > .lexxy-editor__toolbar-button {
  background-color: var(--color-hover);
}

@media (any-hover: hover) {
  .msg-edit__pill .lexxy-editor__toolbar-button:hover:not([aria-pressed="true"]) {
    background-color: var(--color-hover);
  }
}

/* Hide buttons not needed in edit */
.msg-edit__pill button[name="upload"],
.msg-edit__pill button[name="table"],
.msg-edit__pill button[name="divider"],
.msg-edit__pill button[name="undo"],
.msg-edit__pill button[name="redo"],
.msg-edit__pill .lexxy-editor__toolbar-spacer,
.msg-edit__pill .lexxy-editor__toolbar-overflow,
.msg-edit__pill .lexxy-editor__toolbar-dropdown:has(> [name="highlight"]) {
  display: none;
}

/* Link dropdown inside edit pill */
.msg-edit__pill .lexxy-editor__toolbar-dropdown summary ~ * {
  background-color: var(--color-bg-pure);
  border: 1px solid var(--color-border);
  border-radius: 0.75em;
  box-shadow: 0 4px 16px oklch(0% 0 0 / 0.15);
  color: var(--color-txt);
}

.msg-edit__pill lexxy-link-dropdown input[type="url"] {
  background-color: transparent;
  border: 1px solid var(--color-border);
  border-radius: 0.5em;
  color: var(--color-txt);
  padding: 0.5em 0.8em;
  font-size: max(16px, 1em);
}

.msg-edit__pill lexxy-link-dropdown input[type="url"]::placeholder {
  color: var(--color-txt-muted);
}

.msg-edit__pill lexxy-link-dropdown input[type="url"]:focus {
  border-color: var(--color-accent);
  outline: none;
}

/* Save button — solid circle like composer-send */
.msg-edit__save-btn {
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 50%;
  background: var(--color-txt);
  color: var(--color-bg-pure);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 100ms, opacity 100ms;
  padding: 0;
  flex-shrink: 0;
}

.msg-edit__save-btn:active { transform: scale(0.92); }

@media (any-hover: hover) {
  .msg-edit__save-btn:hover { opacity: 0.85; }
}

/* Cancel button — subtle circle */
.msg-edit__cancel-btn {
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--color-txt-muted);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: color 100ms, background 100ms;
  padding: 0;
  flex-shrink: 0;
  text-decoration: none;
}

@media (any-hover: hover) {
  .msg-edit__cancel-btn:hover {
    color: var(--color-negative);
    background: var(--color-hover);
  }
}

/* Delete attachment button — danger pill */
.msg-edit__delete-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.3rem;
  height: 34px;
  padding: 0 14px;
  border: 1px solid var(--color-negative);
  border-radius: 1.3rem;
  background: transparent;
  color: var(--color-negative);
  cursor: pointer;
  font-size: 0.85rem;
  transition: background 100ms, color 100ms;
}

@media (any-hover: hover) {
  .msg-edit__delete-btn:hover {
    background: var(--color-negative);
    color: var(--color-bg-pure);
  }
}
