/* =============================================================================
   Messages — channel list + full-screen thread ("feel like WhatsApp, very
   native"). Token-driven, both themes, motion per DESIGN.md (transform/
   opacity only, no ease-in on enter, exit ~75% of enter duration).
   Layers on top of colors_and_type.css + components.css + ios-kit.css.
   ============================================================================= */

/* -----------------------------------------------------------------------
   CHANNEL LIST
   ----------------------------------------------------------------------- */
.msg-chlist {
  background: var(--surface);
  border-radius: 18px;
  border: 1px solid var(--line);
  overflow: hidden;
  box-shadow: var(--shadow-xs);
}
.msg-chrow {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  text-align: left;
  cursor: pointer;
  font: inherit;
  color: inherit;
  transition: background var(--dur-press) var(--ease-out), transform var(--dur-press) var(--ease-out);
}
.msg-chlist > .msg-chrow:last-child { border-bottom: 0; }
.msg-chrow:active { background: var(--accent-action-soft); transform: scale(0.99); }
.msg-chrow--skel { cursor: default; }
.msg-chrow--skel:active { background: transparent; transform: none; }

.msg-chrow__avatar {
  flex: 0 0 52px;
  width: 52px; height: 52px;
  border-radius: 50%;
  display: grid; place-items: center;
  font: 700 1.02rem/1 var(--font-display);
  letter-spacing: -0.02em;
  color: #fdfdfd;
}
.msg-chrow__body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.msg-chrow__top,
.msg-chrow__bottom {
  display: flex;
  align-items: center;
  gap: 8px;
}
.msg-chrow__name {
  flex: 1;
  min-width: 0;
  font: 700 0.95rem/1.25 var(--font-display);
  color: var(--fg-strong);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.msg-chrow__time {
  flex: 0 0 auto;
  font: 500 0.72rem/1 var(--font-body);
  color: var(--fg-subtle);
}
.msg-chrow__preview {
  flex: 1;
  min-width: 0;
  font-size: 0.83rem;
  line-height: 1.35;
  color: var(--fg-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.msg-badge {
  flex: 0 0 auto;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: var(--radius-pill);
  background: var(--accent-action);
  color: var(--fg-on-accent);
  font: 700 0.7rem/20px var(--font-display);
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* Skeleton row shape — matches .msg-chrow's geometry so the fetch lands
   without a layout jump (DESIGN.md: skeletons, not spinners). */
.msg-chrow__avatar.bk-skeleton { border-radius: 50%; }

/* Inline error banner (channel list + thread). */
.msg-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 22px 16px;
  margin: 0 16px;
  background: var(--status-danger-bg);
  border-radius: var(--radius-md);
  color: var(--status-danger);
  text-align: center;
}
.msg-error__text { font-size: 0.85rem; color: var(--fg-muted); max-width: 34ch; }
.msg-error--centre { margin: 24px 20px; }

/* -----------------------------------------------------------------------
   THREAD OVERLAY — a full-screen push above everything (including the
   floating tab bar), portaled to .ios-app-root. z-index sits above the
   photo cropper (400) and every sheet (≤330) so it always reads as the
   topmost surface, matching a native chat push.
   ----------------------------------------------------------------------- */
.msg-thread {
  position: absolute;
  inset: 0;
  z-index: 500;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  animation: msg-thread-in 300ms var(--ease-out) both;
  will-change: transform;
}
.msg-thread.is-closing {
  animation: msg-thread-out 220ms var(--ease-in, cubic-bezier(0.4, 0, 1, 1)) both;
}
@keyframes msg-thread-in { from { opacity: 0.7; transform: translateX(100%); } to { opacity: 1; transform: translateX(0); } }
@keyframes msg-thread-out { from { opacity: 1; transform: translateX(0); } to { opacity: 0.7; transform: translateX(100%); } }

/* Header — liquid-glass chrome per DESIGN.md: backdrop blur+saturate, a 1px
   specular top-edge highlight, canvas-tinted inner shadow. */
.msg-thread__header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(var(--ios-safe-top, 12px) + 6px) 8px 12px 4px;
  background: var(--glass-bg);
  border-bottom: 1px solid var(--glass-edge);
  box-shadow:
    inset 0 1px 0 var(--glass-spec),
    0 8px 24px -16px rgba(15, 23, 42, 0.3);
  -webkit-backdrop-filter: blur(var(--glass-blur)) saturate(var(--glass-sat));
  backdrop-filter: blur(var(--glass-blur)) saturate(var(--glass-sat));
}
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .msg-thread__header { background: var(--glass-bg-solid); }
}
.msg-thread__back {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 40px; height: 40px;
  border: 0;
  background: transparent;
  color: var(--accent-action);
  cursor: pointer;
  border-radius: 50%;
  transition: background var(--dur-fast) var(--ease-out), transform var(--dur-press) var(--ease-out);
}
.msg-thread__back:active { transform: scale(0.92); background: var(--accent-action-soft); }
.msg-thread__havatar {
  flex: 0 0 34px;
  width: 34px; height: 34px;
  border-radius: 50%;
  display: grid; place-items: center;
  font: 700 0.8rem/1 var(--font-display);
  color: #fdfdfd;
}
.msg-thread__htext {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.msg-thread__hname {
  font: 700 1rem/1.2 var(--font-display);
  color: var(--fg-strong);
  letter-spacing: -0.01em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.msg-thread__hsub {
  font: 500 0.72rem/1.2 var(--font-body);
  color: var(--fg-muted);
}

.msg-thread__scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding: 12px 12px 6px;
  display: flex;
  flex-direction: column;
}
.msg-thread__scroll::-webkit-scrollbar { width: 0; display: none; }

/* Day separator */
.msg-daysep {
  display: flex;
  justify-content: center;
  margin: 14px 0 10px;
}
.msg-daysep:first-child { margin-top: 2px; }
.msg-daysep span {
  padding: 4px 12px;
  border-radius: var(--radius-pill);
  background: var(--bg-sunken);
  color: var(--fg-muted);
  font: 700 0.68rem/1 var(--font-display);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

/* Loading skeleton bubbles */
.msg-skel-list { display: flex; flex-direction: column; gap: 10px; padding: 6px 2px; }
.msg-skel-bubble { display: flex; }
.msg-skel-bubble.is-own { justify-content: flex-end; }
.msg-skel-bubble .bk-skeleton { border-radius: 16px; }

/* -----------------------------------------------------------------------
   MESSAGE ROWS / BUBBLES
   ----------------------------------------------------------------------- */
.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin-top: 2px;
  max-width: 100%;
  animation: msg-row-in 220ms var(--ease-out) both;
}
.msg-row.is-first { margin-top: 12px; }
.msg-row.is-own { justify-content: flex-end; }
@keyframes msg-row-in { from { opacity: 0; transform: translateY(8px) scale(0.98); } to { opacity: 1; transform: translateY(0) scale(1); } }

.msg-row__avatar {
  flex: 0 0 28px;
  width: 28px; height: 28px;
  border-radius: 50%;
  display: grid; place-items: center;
  font: 700 0.62rem/1 var(--font-display);
  color: #fdfdfd;
  margin-bottom: 2px;
}
.msg-row__col {
  max-width: 78%;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-width: 0;
}
.msg-row.is-own .msg-row__col { align-items: flex-end; }
.msg-row__author {
  font: 700 0.72rem/1.3 var(--font-display);
  color: var(--accent-action);
  margin: 0 2px 2px;
}

.msg-bubble {
  position: relative;
  padding: 8px 12px;
  border-radius: 18px;
  max-width: 100%;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  transition: transform 140ms var(--ease-out-quart), box-shadow 140ms var(--ease-out-quart);
}
.msg-bubble.is-other {
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--fg);
  border-bottom-left-radius: 6px;
}
.msg-row.is-first .msg-bubble.is-other { border-top-left-radius: 6px; }
.msg-bubble.is-own {
  background: var(--accent-action);
  color: var(--fg-on-accent);
  border-bottom-right-radius: 6px;
}
.msg-row.is-first .msg-bubble.is-own { border-top-right-radius: 6px; }
.msg-bubble:active { transform: scale(0.985); }
.msg-bubble.is-active { transform: scale(1.03); box-shadow: var(--shadow-md); }
.msg-bubble.is-pending { opacity: 0.72; }
.msg-bubble.is-failed { background: var(--status-danger-bg); border: 1px solid var(--status-danger); color: var(--fg); }

.msg-bubble__text {
  display: block;
  font: 400 0.92rem/1.4 var(--font-body);
  white-space: pre-wrap;
  word-break: break-word;
}
.msg-bubble__meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  margin-top: 3px;
}
.msg-bubble__time {
  font: 500 0.66rem/1 var(--font-body);
  opacity: 0.68;
}
.msg-bubble__failed {
  font: 700 0.66rem/1 var(--font-display);
  color: var(--status-danger);
  margin-right: 2px;
}
.msg-tick {
  display: inline-flex;
  opacity: 0.68;
  color: currentColor;
}
.msg-tick--read { opacity: 1; color: var(--brand-cyan-soft, #67e8f9); }
.msg-tick--pending {
  width: 8px; height: 8px;
  border-radius: 50%;
  border: 1.5px solid currentColor;
  border-top-color: transparent;
  opacity: 0.75;
  animation: msg-tick-spin 0.7s linear infinite;
}
@keyframes msg-tick-spin { to { transform: rotate(360deg); } }

.msg-retry {
  margin: 4px 2px 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--status-danger);
  font: 600 0.72rem/1.3 var(--font-body);
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* Reply-quote block shown above a bubble when the message replies to another. */
.msg-replyquote {
  display: flex;
  align-items: stretch;
  gap: 8px;
  width: 100%;
  margin-bottom: 6px;
  padding: 4px 8px 4px 0;
  border: 0;
  background: rgba(0, 0, 0, 0.06);
  border-radius: 10px;
  cursor: pointer;
  text-align: left;
  font: inherit;
  color: inherit;
}
.msg-bubble.is-own .msg-replyquote { background: rgba(0, 0, 0, 0.14); }
[data-theme="dark"] .msg-bubble.is-other .msg-replyquote { background: rgba(255, 255, 255, 0.06); }
.msg-replyquote:disabled { cursor: default; }
.msg-replyquote:active:not(:disabled) { filter: brightness(0.96); }
.msg-replyquote__bar {
  flex: 0 0 2.5px;
  border-radius: var(--radius-pill);
  background: currentColor;
  opacity: 0.55;
}
.msg-replyquote__text {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 2px 0;
}
.msg-replyquote__author {
  font: 700 0.72rem/1.2 var(--font-display);
  opacity: 0.85;
}
.msg-replyquote__body {
  font-size: 0.78rem;
  line-height: 1.3;
  opacity: 0.75;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Scroll-to flash — a brief highlight pulse when jumping to a quoted message. */
.msg-row.is-flash .msg-bubble { animation: msg-flash 900ms var(--ease-out); }
@keyframes msg-flash {
  0% { box-shadow: 0 0 0 2px var(--accent-action); }
  70% { box-shadow: 0 0 0 2px var(--accent-action); }
  100% { box-shadow: 0 0 0 0 transparent; }
}

/* Reaction chips */
.msg-reactions {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 5px;
}
.msg-reactions.is-own { justify-content: flex-end; }
.msg-reaction {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: var(--radius-pill);
  background: var(--surface);
  border: 1px solid var(--line);
  font-size: 0.78rem;
  line-height: 1.4;
  cursor: pointer;
  transition: transform var(--dur-press) var(--ease-out), background var(--dur-fast) var(--ease-out);
}
.msg-reaction:active { transform: scale(0.94); }
.msg-reaction.is-mine {
  background: var(--accent-action-soft);
  border-color: var(--accent-action);
}
.msg-reaction__count {
  font: 700 0.7rem/1 var(--font-display);
  color: var(--fg-muted);
}
.msg-reaction.is-mine .msg-reaction__count { color: var(--accent-action); }

/* -----------------------------------------------------------------------
   LONG-PRESS CONTEXT MENU — quick reactions + Reply / Copy / Report,
   anchored near the tapped bubble. Backdrop is a light dim (matches the
   house .ios-subscreen-scrim treatment), not a heavy scrim, so the bubble
   underneath stays legible without needing to be cloned into this layer.
   ----------------------------------------------------------------------- */
.msg-ctx-backdrop {
  position: absolute;
  inset: 0;
  z-index: 510;
  background: rgba(15, 23, 42, 0.16);
  animation: msg-ctx-fade 160ms var(--ease-out) both;
}
[data-theme="dark"] .msg-ctx-backdrop { background: rgba(0, 0, 0, 0.32); }
@keyframes msg-ctx-fade { from { opacity: 0; } to { opacity: 1; } }

/* Positioning wrapper — plain (unanimated) so its .is-above flip transform
   (translateY(-100%), applied via this static class, not inline+animated)
   is never clobbered by the glass panel's own entrance-animation transform.
   A CSS `animation` wins over an element's OWN inline transform for the
   duration of the animation (and after, under fill-mode: both), so the two
   transforms must live on separate nested elements. */
.msg-ctx-wrap {
  position: absolute;
  z-index: 520;
}
.msg-ctx-wrap.is-above {
  transform: translateY(-100%);
}

/* One shared glass container (matches .ios-action-sheet's technique) rather
   than blurring each row separately — glass used with intent, not repeated
   as a lazy default (DESIGN.md). */
.msg-ctx {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--glass-bg);
  border: 1px solid var(--glass-edge);
  border-radius: 18px;
  box-shadow: inset 0 1px 0 var(--glass-spec), var(--shadow-lg);
  -webkit-backdrop-filter: blur(var(--glass-blur)) saturate(var(--glass-sat));
  backdrop-filter: blur(var(--glass-blur)) saturate(var(--glass-sat));
  animation: msg-ctx-in 160ms var(--ease-out-quart) both;
  transform-origin: top center;
}
.msg-ctx-wrap.is-above .msg-ctx { transform-origin: bottom center; }
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .msg-ctx { background: var(--glass-bg-solid); }
}
@keyframes msg-ctx-in { from { opacity: 0; transform: scale(0.95) translateY(4px); } to { opacity: 1; transform: scale(1) translateY(0); } }

.msg-ctx__reactions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 2px;
  padding: 8px 6px;
  border-bottom: 1px solid var(--glass-edge);
}
.msg-ctx__emoji {
  flex: 1;
  display: grid;
  place-items: center;
  min-width: 32px;
  height: 34px;
  border: 0;
  background: transparent;
  border-radius: 50%;
  font-size: 1.15rem;
  line-height: 1;
  cursor: pointer;
  transition: transform 140ms var(--ease-out-quart), background var(--dur-fast) var(--ease-out);
}
.msg-ctx__emoji:active { transform: scale(1.3); background: var(--accent-action-soft); }

.msg-ctx__btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 12px 14px;
  border: 0;
  border-top: 1px solid var(--glass-edge);
  background: transparent;
  color: var(--fg-strong);
  font: 600 0.92rem/1 var(--font-display);
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease-out), transform var(--dur-press) var(--ease-out);
}
.msg-ctx__btn:first-of-type { border-top: 0; }
.msg-ctx__btn:active { transform: scale(0.98); background: var(--accent-action-soft); }
.msg-ctx__btn--danger { color: var(--status-danger); }

/* -----------------------------------------------------------------------
   COMPOSER — pinned to the bottom of the thread, above the keyboard once
   Capacitor's Keyboard(resize:"native") shrinks the webview.
   ----------------------------------------------------------------------- */
.msg-composer {
  flex: 0 0 auto;
  background: var(--glass-bg);
  border-top: 1px solid var(--glass-edge);
  box-shadow: inset 0 1px 0 var(--glass-spec);
  -webkit-backdrop-filter: blur(var(--glass-blur)) saturate(var(--glass-sat));
  backdrop-filter: blur(var(--glass-blur)) saturate(var(--glass-sat));
  padding: 8px 10px calc(10px + env(safe-area-inset-bottom, 0px));
}
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .msg-composer { background: var(--glass-bg-solid); }
}
.msg-composer__reply {
  display: flex;
  align-items: stretch;
  gap: 8px;
  margin: 2px 2px 8px;
  padding: 6px 8px;
  background: var(--bg-sunken);
  border-radius: 10px;
}
.msg-composer__reply-bar {
  flex: 0 0 2.5px;
  border-radius: var(--radius-pill);
  background: var(--accent-action);
}
.msg-composer__reply-text {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.msg-composer__reply-author {
  font: 700 0.74rem/1.2 var(--font-display);
  color: var(--accent-action);
}
.msg-composer__reply-body {
  font-size: 0.78rem;
  color: var(--fg-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.msg-composer__reply-close {
  flex: 0 0 auto;
  align-self: center;
  display: grid;
  place-items: center;
  width: 26px; height: 26px;
  border: 0;
  background: transparent;
  color: var(--fg-muted);
  border-radius: 50%;
  cursor: pointer;
}
.msg-composer__reply-close:active { background: var(--line); }

.msg-composer__row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}
.msg-composer__input {
  flex: 1;
  min-width: 0;
  max-height: 116px;
  resize: none;
  padding: 10px 14px;
  border-radius: 20px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--fg);
  font: 400 0.95rem/1.35 var(--font-body);
  /* iOS zoom-on-focus guard: inputs floor at 16px (see components.css). */
  font-size: max(0.95rem, 16px);
}
.msg-composer__input:focus {
  outline: none;
  border-color: var(--accent-action);
  box-shadow: 0 0 0 3px var(--focus-ring-soft);
}
.msg-composer__send {
  flex: 0 0 auto;
}

/* -----------------------------------------------------------------------
   Responsive floor — never clip on a 375px phone.
   ----------------------------------------------------------------------- */
@media (max-width: 380px) {
  .msg-row__col { max-width: 84%; }
  .msg-chrow__name { font-size: 0.9rem; }
}

/* =============================================================================
   ---- Comms 1.1 ----
   DM compose, archive, avatars, inline history search. New classes only;
   layers on the tokens/blocks above. (docs/COMMS-1.1-PLAN.md section 5.1 —
   styles land here per WP-C's file ownership, not ios-kit.css.)
   ============================================================================= */

/* Avatar image — fills any of the existing circular avatar wrappers
   (.msg-chrow__avatar / .msg-thread__havatar / .msg-row__avatar) when a
   users.avatar_url is set; MsgAvatar falls back to the initials text node
   already styled by those wrappers when there is no url, or it fails to load. */
.msg-avatar__img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
  display: block;
}

/* Header row — eyebrow/title/sub on the left, search + compose actions
   top-right (docs/COMMS-1.1-PLAN.md 5.1.1). Added alongside .ios-screen__header
   (not replacing it), so its padding/type rules still apply. */
.msg-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}
.msg-header__text { min-width: 0; }
.msg-header__actions {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 2px;
  margin-top: 2px;
}
.msg-header__btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  background: transparent;
  color: var(--accent-action);
  border-radius: 50%;
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease-out), transform var(--dur-press) var(--ease-out);
}
.msg-header__btn:active { transform: scale(0.92); background: var(--accent-action-soft); }
.msg-header__btn.is-active { background: var(--accent-action-soft); }

/* Inline history search hint (below min-length). */
.msg-search-hint {
  margin: 4px 16px 0;
  color: var(--fg-subtle);
  font-size: 0.82rem;
}

/* "< Chats" back link shown above the archived-chats view. */
.msg-archived-back {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  min-height: 44px;
  margin: 0 4px 4px;
  padding: 6px 6px;
  border: 0;
  background: transparent;
  color: var(--accent-action);
  font: 600 0.95rem/1 var(--font-display);
  cursor: pointer;
  transition: opacity var(--dur-fast) var(--ease-out);
}
.msg-archived-back:active { opacity: 0.6; }

/* Trailing "Archived (n)" row inside the active chat list — reuses .msg-chrow
   so it sits flush with the real rows, just with a muted icon/badge instead
   of a person's avatar/unread count. */
.msg-archived-link__icon {
  background: var(--bg-sunken);
  color: var(--fg-muted);
}
.msg-badge--muted {
  background: var(--bg-sunken);
  color: var(--fg-muted);
}
.msg-archived-link__chev {
  flex: 0 0 auto;
  color: var(--fg-subtle);
  margin-left: -2px;
}

/* Compose sheet — colleague rows dim while a startDm() call is in flight
   (button disabled, no spinner — DESIGN.md: skeletons, not spinners). */
.msg-colleague-row:disabled {
  opacity: 0.55;
  cursor: default;
}
