/* Messages (#messages) — docs/design/Atlas_Experience_Redesign.md §7.3, §8.5.
 * Module layout only: buttons, inputs, avatars, badges, alerts, empty states,
 * sheets and dialogs are the shared components from atlas-components.css.
 * No tokens are defined here. */
@layer atlas.modules {

/* The page is a full-height page (.page--full-height, atlas-shell.css): the
   conversation list and the thread fill the content area under the top bar. */

.msg { display: grid; grid-template-columns: 280px minmax(0, 1fr); height: 100%; min-height: 0; background: var(--bg); }

/* ---------- conversation list ---------- */
.msg-side { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); background: var(--bg-subtle); }
.msg-side__head.page-head { margin: 0; padding: var(--s-5) var(--s-4) var(--s-3); }
.msg-side__head .page-head__title { font: var(--type-section); letter-spacing: var(--track-section); }
.msg-side__search { width: auto; max-width: none; margin: 0 var(--s-3) var(--s-2); }
.msg-side__list { flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 0 var(--s-2) var(--s-4); }
.msg-side__group { margin: var(--s-3) var(--s-2) var(--s-1); font: var(--type-caption); font-weight: 500; color: var(--text-3); }
.msg-side__empty { margin: var(--s-4) var(--s-2); font: var(--type-meta); color: var(--text-2); }
.msg-side__skel { display: grid; gap: var(--s-2); padding: var(--s-2); }

.msg-channel { display: flex; align-items: center; gap: var(--s-2); min-height: 52px; padding: var(--s-2); border-radius: var(--r-md); color: var(--text); text-decoration: none; }
.msg-channel:hover { background: var(--bg-muted); }
.msg-channel[aria-current="page"] { background: var(--surface); box-shadow: var(--shadow-1); }
.msg-channel__icon { display: grid; place-items: center; width: 28px; height: 28px; flex: none; border-radius: var(--r-sm); color: var(--text-2); }
.msg-channel__icon svg { width: var(--icon-sm); height: var(--icon-sm); }
.msg-channel__body { display: grid; flex: 1 1 auto; min-width: 0; }
.msg-channel__name { display: flex; align-items: center; gap: var(--s-1); min-width: 0; font: var(--type-label); overflow-wrap: anywhere; }
.msg-channel__name svg { width: 12px; height: 12px; color: var(--text-3); }
.msg-channel.has-unread .msg-channel__name { font-weight: 600; }
.msg-channel__preview { overflow: hidden; font: var(--type-meta); color: var(--text-2); white-space: nowrap; text-overflow: ellipsis; }
.msg-channel__end { display: grid; justify-items: end; gap: 2px; flex: none; }
.msg-channel__time { font: var(--type-caption); color: var(--text-3); font-variant-numeric: tabular-nums; }

/* ---------- thread ---------- */
.msg-thread { position: relative; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.msg-thread__head { display: flex; align-items: center; gap: var(--s-3); min-height: 60px; padding: var(--s-3) var(--s-6); border-bottom: 1px solid var(--line); }
.msg-thread__text { flex: 1 1 auto; min-width: 0; }
.msg-thread__title { display: flex; align-items: center; gap: var(--s-2); margin: 0; font: var(--type-heading); color: var(--text); }
.msg-thread__title svg { width: var(--icon-sm); height: var(--icon-sm); color: var(--text-3); }
.msg-thread__desc { margin: 0; overflow: hidden; font: var(--type-meta); color: var(--text-2); white-space: nowrap; text-overflow: ellipsis; }
.msg-thread__actions { display: flex; align-items: center; gap: var(--s-2); flex: none; }
.msg-thread__alert:empty { display: none; }
.msg-thread__alert { padding: var(--s-3) var(--s-6) 0; }
.msg-thread__scroll { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }

.msg-log { width: 100%; max-width: calc(720px + 2 * var(--s-6)); margin: 0 auto; padding: var(--s-4) var(--s-6) var(--s-6); }
.msg-log__skel { display: grid; gap: var(--s-5); padding-top: var(--s-4); }
.msg-skel { display: grid; grid-template-columns: 28px minmax(0, 1fr); gap: var(--s-3); align-items: center; }
.msg-skel .atlas-skel--circle { width: 28px; height: 28px; }
.msg-log__empty { margin-top: var(--s-10); }

.msg-divider { display: flex; align-items: center; gap: var(--s-3); margin: var(--s-5) 0 var(--s-2); font: var(--type-caption); font-weight: 500; color: var(--text-3); }
.msg-divider::before, .msg-divider::after { content: ''; flex: 1 1 auto; height: 1px; background: var(--line); }
.msg-divider--new { color: var(--accent-text); }
.msg-divider--new::before, .msg-divider--new::after { background: var(--accent-brand); }

.msg-item { display: grid; grid-template-columns: 28px minmax(0, 1fr); gap: var(--s-3); padding-top: var(--s-3); }
.msg-item.is-grouped { padding-top: var(--s-1); }
.msg-item__gutter { display: block; min-height: 1px; }
.msg-avatar { overflow: hidden; }
.msg-avatar--atlas { background: var(--bg-muted); color: var(--text-2); }
.msg-avatar--atlas svg, .msg-avatar > svg { width: 14px; height: 14px; }
/* Chat layout (S91a, S92): other people's messages on the left in a neutral
   bubble, the viewer's own on the right in an accent-tinted one; both carry
   the sender's name and avatar, own avatar in a gutter on the right. */
.msg-item__body { position: relative; justify-self: start; min-width: 0; max-width: min(88%, 600px); padding: var(--s-2) var(--s-3); border: 1px solid transparent; border-radius: var(--r-lg); background: var(--bg-muted); }
.msg-item.is-own { grid-template-columns: minmax(0, 1fr) 28px; }
.msg-item.is-own > .msg-item__gutter { grid-column: 2; grid-row: 1; }
.msg-item.is-own > .msg-item__body { grid-column: 1; grid-row: 1; justify-self: end; max-width: min(80%, 600px); background: var(--accent-soft); }
.msg-item.is-own .msg-item__meta { justify-content: flex-end; }
.msg-item.is-deleted > .msg-item__body { border: 1px dashed var(--line); background: var(--bg); }
.msg-item__meta { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 var(--s-2); }
.msg-item__name { font: 600 13px/18px var(--font-sans); color: var(--text); }
.msg-item__role { font: var(--type-caption); color: var(--text-3); }
.msg-item__time { font: var(--type-caption); color: var(--text-3); font-variant-numeric: tabular-nums; }
.msg-item__text { margin: 2px 0 0; font: 400 15px/22px var(--font-sans); color: var(--text); overflow-wrap: anywhere; }
.msg-item.is-system .msg-item__text { color: var(--text-2); }
.msg-item.is-deleted .msg-item__text { font-style: italic; color: var(--text-3); }
.msg-item.is-deleted .msg-item__time { display: block; }
.msg-item__foot { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-2); margin-top: var(--s-1); min-height: 24px; }
.msg-item__read, .msg-item__failed { display: inline-flex; align-items: center; gap: var(--s-1); font: var(--type-caption); color: var(--text-3); }
.msg-item__read svg, .msg-item__failed svg { width: 14px; height: 14px; }
.msg-item__failed { color: var(--danger); font-weight: 500; }
.msg-item { position: relative; }
/* Edit / Delete: a small toolbar above the bubble's top edge on hover or focus (it never covers the time); inline on touch screens. */
.msg-item__actions { position: absolute; bottom: calc(100% - var(--s-2)); right: var(--s-2); z-index: 1; display: inline-flex; gap: 2px; padding: 2px; border: 1px solid var(--line); border-radius: var(--r-md); background: var(--surface); box-shadow: var(--shadow-1); opacity: 0; pointer-events: none; }
.msg-item:hover > .msg-item__body > .msg-item__actions, .msg-item:focus-within > .msg-item__body > .msg-item__actions { opacity: 1; pointer-events: auto; }
.msg-item.is-failed .msg-item__actions { position: static; padding: 0; border: 0; box-shadow: none; background: none; opacity: 1; pointer-events: auto; }
.msg-item__more { position: absolute; top: 0; right: 0; display: none; }
@media (hover: none) {
  .msg-item:not(.is-failed) .msg-item__actions { display: none; }
  .msg-item__more { display: inline-grid; }
  .msg-item:has(> .msg-item__body > .msg-item__more) > .msg-item__body { min-height: var(--touch); padding-right: var(--touch); }
}
.msg-actions__list { gap: var(--s-2); }

/* Linked record chip (spec §6.28 record chip pattern). */
/* The record chip itself is the shared .atlas-record-chip (atlas-components.css). */
.msg-link { margin-top: var(--s-2); }
.msg-link.is-locked { color: var(--text-2); }

.msg-jump { position: absolute; left: 50%; bottom: 112px; z-index: 2; transform: translateX(-50%); box-shadow: var(--shadow-pop); }
.msg-jump[hidden] { display: none; }

/* ---------- composer ---------- */
.msg-composer { flex: none; padding: 0 var(--s-6) var(--s-4); background: var(--bg); }
.msg-compose { width: 100%; max-width: 720px; margin: 0 auto; }
.msg-compose__context { display: flex; align-items: center; gap: var(--s-2); margin-bottom: var(--s-2); padding: 6px 6px 6px 10px; border-radius: var(--r-md); background: var(--bg-subtle); font: var(--type-meta); color: var(--text-2); }
.msg-compose__context > span { flex: 1 1 auto; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.msg-compose__context > svg { width: var(--icon-sm); height: var(--icon-sm); flex: none; }
.msg-compose__box { display: flex; align-items: flex-end; gap: var(--s-2); padding: var(--s-2); border: 1px solid var(--line-strong); border-radius: var(--r-xl); background: var(--surface); }
.msg-compose__box:focus-within { border-color: var(--accent-brand); box-shadow: var(--focus-ring); }
.msg-compose__input { flex: 1 1 auto; min-width: 0; min-height: 36px; max-height: 160px; padding: 8px 4px; border: 0; background: transparent; font: 400 15px/20px var(--font-sans); color: var(--text); resize: none; outline: none; }
.msg-compose__input::placeholder { color: var(--text-3); }
.msg-compose__send { width: 36px; padding: 0; border-radius: 50%; }
.msg-compose__note { margin: var(--s-2) 0 0; font: var(--type-caption); color: var(--text-3); text-align: center; }
.msg-composer__locked { display: flex; align-items: center; justify-content: center; gap: var(--s-2); max-width: 720px; margin: 0 auto; padding: var(--s-3) var(--s-4); border-radius: var(--r-lg); background: var(--bg-subtle); font: var(--type-meta); color: var(--text-2); }
.msg-composer__locked svg { width: var(--icon-sm); height: var(--icon-sm); flex: none; }

/* ---------- link picker sheet ---------- */
.msg-link__types { width: 100%; }
.msg-link__types button { flex: 1 1 0; }
.msg-link__results { display: grid; gap: var(--s-1); }
.msg-link__state { margin: var(--s-2) 0; font: var(--type-meta); color: var(--text-2); }
.msg-link__option { display: flex; align-items: center; gap: var(--s-3); width: 100%; min-height: 52px; padding: var(--s-2) var(--s-3); border: 1px solid transparent; border-radius: var(--r-md); background: transparent; color: var(--text); text-align: left; cursor: pointer; }
.msg-link__option:hover { background: var(--bg-subtle); }
.msg-link__option[aria-selected="true"] { border-color: var(--accent-brand); background: var(--accent-soft); }
.msg-link__option svg { width: var(--icon-sm); height: var(--icon-sm); flex: none; color: var(--text-2); }
.msg-link__option[aria-selected="true"] svg { color: var(--accent-brand); }
.msg-link__option-body { display: grid; flex: 1 1 auto; min-width: 0; }
.msg-link__option-title { font: var(--type-label); }
.msg-link__option-meta { overflow: hidden; font: var(--type-caption); color: var(--text-2); white-space: nowrap; text-overflow: ellipsis; }

/* ---------- widths ---------- */
@media (min-width: 768px) and (max-width: 1023px) {
  .msg { grid-template-columns: 248px minmax(0, 1fr); }
  .msg-thread__head, .msg-log, .msg-composer, .msg-thread__alert { padding-left: var(--s-4); padding-right: var(--s-4); }
}
@media (max-width: 767px) {
  .msg { display: block; height: auto; }
  .msg[data-msg-view="thread"] .msg-side, .msg[data-msg-view="list"] .msg-thread { display: none; }

  /* Conversation list screen: rows 15/500 name, 13 preview (spec §8.5). */
  .msg-side { min-height: calc(100dvh - 52px - var(--tabbar-h) - env(safe-area-inset-top) - env(safe-area-inset-bottom)); border-right: 0; background: var(--bg); }
  .msg-side__head.page-head { padding: 0; }
  .msg-side__search { margin: var(--s-3) var(--page-gutter) var(--s-2); }
  .msg-side__list { padding: 0 var(--s-2) calc(var(--tabbar-h) + var(--s-10)); overflow: visible; }
  .msg-channel { min-height: 64px; }
  .msg-channel__name { font: 500 15px/20px var(--font-sans); }
  .msg-channel__icon { width: 40px; height: 40px; border-radius: 50%; background: var(--bg-muted); }

  /* Channel screen: top bar with back, tab bar hidden, composer sticky above the keyboard. */
  .msg-thread { height: calc(100dvh - 52px - env(safe-area-inset-top)); }
  .msg-thread__head { display: none; }
  .msg-thread__alert { padding: var(--s-3) var(--page-gutter) 0; }
  .msg-log { padding: var(--s-2) var(--page-gutter) var(--s-4); }
  .msg-composer { position: sticky; bottom: 0; padding: var(--s-2) var(--s-2) calc(var(--s-2) + env(safe-area-inset-bottom)); border-top: 1px solid var(--line-subtle); }
  .msg-compose__box { border-radius: 22px; }
  .msg-compose__input { min-height: 44px; font-size: 16px; line-height: 22px; padding-top: 11px; padding-bottom: 11px; }
  .msg-compose__send { width: 44px; height: 44px; }
  .msg-compose__tool { width: 44px; height: 44px; }
  .msg-compose__note { display: none; }
  .msg-jump { bottom: 88px; }
}
@media (prefers-reduced-motion: reduce) {
  .msg-item__actions { transition: none; }
}
}
