/* Glampfire messages: flat, left-aligned rows (avatar, name, time, text).
   Replaces Campfire's messages.css. Classes set by the messages controller
   (--formatted, --first-of-day, --threaded, --me, --mentioned) keep their meaning. */
.message-area {
  display: contents;

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

  &:not(:has(.message)) {
    block-size: 100%;
    display: grid;
    place-content: center stretch;
    place-items: center;

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

    .message-area--empty > figure img {
      inline-size: min(30dvw, 160px);
      opacity: 0.15;
    }
  }
}

.messages {
  --gutter: 24px;
  --avatar: 36px;

  align-content: start;
  display: grid;
  flex: 1;
  grid-auto-rows: min-content;
  min-block-size: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-block: 12px 8px;
  position: relative;

  @media (max-width: 100ch) { --gutter: 14px; }
}

.message {
  column-gap: 12px;
  display: grid;
  grid-template-areas:
    "sep sep"
    "avatar body";
  grid-template-columns: var(--avatar) minmax(0, 1fr);
  margin-block-start: 6px;
  padding: 8px var(--gutter) 6px;
  position: relative;
  visibility: hidden;
  z-index: 0;

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

  &:hover,
  &:has([open]),
  &:focus-within { background-color: var(--gf-hover); }

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

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

  &.message--failed {
    opacity: 0.55;

    .message__heading::after {
      color: var(--gf-negative);
      content: "Not sent";
      font-size: 12px;
      font-weight: 600;
    }
  }
}

/* Follow-up messages from the same person: no avatar or name; time on hover. */
.message--threaded {
  margin-block-start: 0;
  padding-block-start: 2px;

  .message__avatar,
  .message__author { display: none; }

  .message__heading { display: contents; }

  /* The time sits in the avatar gutter (avatar + gap wide) and shows on hover. */
  .message__permalink {
    inline-size: calc(var(--avatar) + 10px);
    inset-block-start: 6px;
    inset-inline-start: calc(var(--gutter) - 6px);
    opacity: 0;
    overflow: hidden;
    position: absolute;
    text-align: center;
  }

  &:hover .message__permalink { opacity: 1; }

  .message__timestamp { font-size: 10.5px; letter-spacing: -0.01em; }
}

/* Day separator */
.message__day-separator {
  align-items: center;
  color: var(--gf-ink-subtle);
  display: none;
  font-size: 12px;
  font-weight: 600;
  gap: 12px;
  grid-area: sep;
  grid-template-columns: 1fr auto 1fr;
  margin: 10px 0 14px;
  visibility: hidden;

  &::before,
  &::after {
    border-block-start: 1px solid var(--gf-border);
    content: "";
  }

  time,
  span {
    background-color: var(--gf-surface);
    border: 1px solid var(--gf-border);
    border-radius: 999px;
    padding: 3px 12px;
  }

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

.message--first-of-day {
  background-color: transparent !important;
  margin-block-start: 8px;
}

.message__avatar {
  --avatar-size: var(--avatar);

  align-self: start;
  grid-area: avatar;
  z-index: 1;

  img {
    outline: 1px solid oklch(0% 0 0 / 0.1);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) { outline-color: oklch(100% 0 0 / 0.1); }
  }
}

/* Not a positioning context: the gutter time and the actions position against the row. */
.message__body {
  grid-area: body;
  min-inline-size: 0;
}

.message__body-content {
  color: var(--gf-ink);
  font-size: 15px;
  line-height: 1.55;
  min-inline-size: 0;
  word-break: break-word;

  /* Paragraph spacing only between paragraphs, never around a message. */
  :is(p, ul, ol, blockquote, pre, h1, h2, h3):first-child { margin-block-start: 0; }
  :is(p, ul, ol, blockquote, pre, h1, h2, h3):last-child { margin-block-end: 0; }
  p { margin-block: 0 0.5em; }
}

.message__meta {
  align-items: baseline;
  display: flex;
  gap: 8px;
  line-height: 1.4;
  margin: 0 0 1px;
  min-inline-size: 0;

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

.message__heading {
  align-items: baseline;
  display: inline-flex;
  gap: 8px;
  margin: 0;
  min-inline-size: 0;
}

.message__author {
  font-size: 14.5px;
  font-weight: 650;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;

  strong { font-weight: inherit; }
}

.message__permalink {
  color: var(--gf-ink-faint);
  font-size: 12.5px;
  font-variant-numeric: tabular-nums;
  text-decoration: none;
  white-space: nowrap;

  &:hover { color: var(--gf-ink-subtle); text-decoration: underline; }
}

/* Floating actions (top right of the row on hover) */
.message__actions {
  inset-block-start: -14px;
  inset-inline-end: var(--gutter);
  opacity: 0;
  position: absolute;
  z-index: 3;

  .message:hover &,
  .message:focus-within &,
  .message:has([open]) & { opacity: 1; }

  @media (hover: none) { opacity: 1; inset-block-start: 4px; }
}

.message__options-btn {
  --btn-background: var(--gf-surface);
  --btn-border-color: var(--gf-border);
  --btn-size: 30px;
  --hover-size: 0;

  border-radius: var(--gf-radius-item);
  box-shadow: var(--gf-shadow-card);

  img { block-size: 16px; inline-size: 16px; opacity: 0.7; }
  &:hover img { opacity: 1; }
}

.message__actions-menu {
  background-color: var(--gf-surface-raised);
  border: 0 !important;
  border-radius: 12px;
  box-shadow: var(--gf-shadow-pop) !important;
  display: none;
  inset: calc(100% + 6px) 0 auto auto;
  padding: 6px;
  position: absolute;
  min-inline-size: 180px;

  .message:has([open]) & { display: block; }

  .popup-orientation-top & {
    inset-block-end: calc(100% + 6px);
    inset-block-start: auto;
  }
}

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

  border-radius: var(--gf-radius-item);
  flex-basis: var(--width);
  font-size: 13px;

  &:hover { --btn-background: var(--gf-hover); }

  img { block-size: 18px; inline-size: 18px; }

  .message__actions-grid & {
    padding: 8px;

    &:where(:has(> :last-child:nth-child(1))) & { --width: 100%; }
    &:where(:has(> :last-child:nth-child(even))) & { --width: 50%; }
  }
}

.message__actions-grid { border-color: var(--gf-border) !important; }

.message__edit-btn {
  display: none;

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

/* Attachments */
.message__attachment {
  block-size: auto;
  border-radius: 10px;
  max-inline-size: min(100%, 520px);
  outline: 1px solid oklch(0% 0 0 / 0.08);
  outline-offset: -1px;
}

img.message__attachment { cursor: zoom-in; }

/* Mentions of me: a quiet tint and an accent bar */
.message--mentioned {
  background-color: var(--gf-mention-soft);
  box-shadow: inset 3px 0 0 var(--gf-mention);

  &:hover { background-color: color-mix(in oklch, var(--gf-mention-soft) 80%, var(--gf-mention)); }
}

/* Emoji-only messages */
.message--emoji:not(:has(.composer--edit)) .message__body-content .lexxy-content {
  font-size: 2.2em;
  line-height: 1.3;
}

.message.search-highlight {
  animation: gf-highlight 800ms var(--gf-ease-out, ease-out) both;
}

@keyframes gf-highlight {
  from { background-color: var(--gf-brand-soft); }
  to { background-color: transparent; }
}

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

/* Editing in place */
.message__body:has(.composer--edit) .message__body-content--editing { flex-grow: 1; }

/* Jump to newest */
.message-area__return-to-latest {
  --btn-background: var(--gf-surface);
  --btn-border-color: var(--gf-border);

  box-shadow: var(--gf-shadow-pop);
  inset-block-end: 96px;
  inset-inline-start: calc(50% - (var(--btn-size) / 2));
  position: absolute;
  z-index: 4;
}

/* Welcome card for the first room */
#system_welcome {
  display: block;
  margin: 24px auto;
  max-inline-size: 520px;
  padding: 0 var(--gutter);
  position: relative;
  visibility: visible;

  &:hover { background: none; }

  .message__body {
    background-color: var(--gf-surface);
    border: 1px solid var(--gf-border);
    border-radius: var(--gf-radius-card);
    padding: 28px 24px 22px;
  }

  .account-logo {
    --avatar-size: 56px;

    margin-block: 0 12px !important;
  }

  .system-welcome--translation {
    inset-block-start: 14px;
    inset-inline-start: 14px;
    position: absolute;
  }

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

/* Pending uploads */
.message__pending-upload {
  border-radius: 10px;
  overflow: hidden;
  padding: 8px 12px;
  position: relative;

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

  > div { position: relative; z-index: 2; }

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

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

/* Mentions inside messages read as links */
.message__body-content .mention {
  color: var(--gf-mention);
  font-weight: 550;

  .avatar { display: none; }
  &::before { content: "@"; }
}
