/*
  HALIA Messages workspace — Phase C (REF7 desktop / REF8 mobile).
  Desktop: folders & labels · conversation list · thread · context. Narrower screens drop panels in that order;
  phones get a list screen and a full-screen conversation (not a shrunk desktop). Page layer; tokens only.
*/
@layer pages {
  :root[data-route="messages"] .community.container { padding-block: var(--space-2) var(--space-4); }

  .msg-app {
    position: relative;
    display: grid;
    grid-template-columns: 208px 330px minmax(0, 1fr) 284px;
    gap: var(--space-4);
    height: calc(100dvh - var(--header-height) - var(--space-6));
    min-height: 600px;
  }
  .msg-nav-pane, .msg-list-pane, .msg-thread-pane, .msg-context-pane { min-height: 0; }
  .msg-nav-pane, .msg-list-pane, .msg-thread-pane {
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-xl);
    background: var(--surface);
    box-shadow: var(--shadow-card);
  }

  /* ---------- folders & labels ---------- */
  .msg-nav-pane { display: flex; flex-direction: column; gap: var(--space-3); padding: var(--space-5) var(--space-3) var(--space-3); overflow-y: auto; }
  .msg-nav-head { padding: 0 var(--space-2); }
  .msg-title { margin: 0; font: 800 22px/1.3 var(--font-sans); color: var(--text-primary); }
  .msg-subtitle { margin: 2px 0 0; font: var(--text-body-sm); color: var(--text-muted); }
  .msg-nav-pane > .msg-new { justify-content: center; gap: var(--space-2); min-height: var(--control-lg); border-radius: var(--radius-lg); font-size: 15px; }
  .msg-folders, .msg-label-list { margin: 0; padding: 0; list-style: none; }
  .msg-folder, .msg-label-item {
    display: flex; align-items: center; gap: var(--space-3); width: 100%; min-height: 44px; padding: 0 var(--space-3);
    border: 0; border-radius: var(--radius-md); background: transparent; color: var(--text-primary); font: var(--text-label); font-size: 15px; text-align: left; cursor: pointer;
  }
  .msg-folder .icon { color: var(--accent); flex: none; }
  .msg-folder > span:nth-child(2), .msg-label-item > span:nth-child(2) { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  @media (hover: hover) { .msg-folder:hover, .msg-label-item:hover { background: var(--surface-hover); } }
  .msg-folder.is-current, .msg-label-item.is-current { background: var(--surface-mint); }
  .msg-folder-count { flex: none; min-width: 26px; height: 24px; padding: 0 7px; border-radius: var(--radius-pill); background: var(--bg-secondary); color: var(--text-secondary); font: var(--text-caption); font-weight: 700; line-height: 24px; text-align: center; }
  .msg-folder-count.is-strong { background: var(--accent); color: var(--on-accent); }
  .msg-labels { padding-top: var(--space-3); border-top: 1px solid var(--border-subtle); }
  .msg-labels-head { display: flex; align-items: center; justify-content: space-between; padding: 0 var(--space-2) var(--space-1) var(--space-3); }
  .msg-labels-head h2 { margin: 0; font: var(--text-label); font-size: 15px; }
  .msg-label-item { min-height: 38px; font-weight: 500; }
  .msg-label-empty { padding: var(--space-2) var(--space-3); font: var(--text-caption); color: var(--text-muted); }
  .msg-label-dot { display: inline-block; flex: none; width: 12px; height: 12px; border-radius: var(--radius-pill); background: var(--label-gray); }
  .msg-label-blue { --msg-label: var(--label-blue); } .msg-label-green { --msg-label: var(--label-green); } .msg-label-teal { --msg-label: var(--label-teal); }
  .msg-label-pink { --msg-label: var(--label-pink); } .msg-label-orange { --msg-label: var(--label-orange); } .msg-label-purple { --msg-label: var(--label-purple); } .msg-label-gray { --msg-label: var(--label-gray); }
  .msg-label-dot[class*="msg-label-"] { background: var(--msg-label, var(--label-gray)); }
  .msg-promo { position: relative; isolation: isolate; display: grid; align-content: start; gap: var(--space-2); min-height: 220px; margin-top: auto; overflow: hidden; padding: var(--space-5) var(--space-4); border-radius: var(--radius-lg); color: var(--on-media); }
  .msg-promo > img:first-child { position: absolute; z-index: -1; inset: 0; width: 100%; height: 100%; object-fit: cover; }
  .msg-promo-leaf { width: 32px; height: auto; filter: brightness(1.6) saturate(.8); }
  .msg-promo p { margin: 0; font: 700 17px/1.5 var(--font-sans); }
  .msg-promo small { font: var(--text-caption); color: var(--on-media-muted); }
  .msg-push { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-3); border-radius: var(--radius-md); background: var(--bg-secondary); }
  .msg-push[hidden] { display: none; }
  .msg-push-icon { display: grid; place-items: center; flex: none; width: 36px; height: 36px; border-radius: var(--radius-pill); background: var(--accent-subtle); color: var(--accent); }
  .msg-push-text { flex: 1; min-width: 0; display: grid; font: var(--text-caption); color: var(--text-muted); }
  .msg-push-text strong { font: var(--text-label); color: var(--text-primary); }

  /* ---------- conversation list ---------- */
  .msg-list-pane { display: flex; flex-direction: column; overflow: hidden; }
  .msg-list-head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); padding: var(--space-5) var(--space-5) var(--space-3); }
  .msg-list-title { margin: 0; font: 800 20px/1.3 var(--font-sans); }
  .msg-mobile-title, .msg-new-compact { display: none; }
  .msg-search { position: relative; margin: 0 var(--space-4) var(--space-3); }
  .msg-search > .icon { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); color: var(--text-muted); pointer-events: none; }
  .msg-search input { width: 100%; height: 44px; padding: 0 var(--space-3) 0 40px; border: 1px solid var(--border-subtle); border-radius: var(--radius-lg); background: var(--surface-muted); color: var(--text-primary); font: var(--text-body-sm); font-size: 15px; }
  .msg-search input:focus { border-color: var(--accent); outline: var(--focus-width) solid var(--focus-ring); outline-offset: 1px; }
  .msg-folder-chips { display: none; gap: var(--space-2); padding: 0 var(--space-4) var(--space-3); overflow-x: auto; scrollbar-width: none; }
  .msg-folder-chips::-webkit-scrollbar { display: none; }
  .msg-folder-chip { display: inline-flex; align-items: center; gap: 6px; flex: none; min-height: 36px; padding: 0 var(--space-3); border: 1px solid var(--border-subtle); border-radius: var(--radius-pill); background: var(--surface); color: var(--text-secondary); font: var(--text-label); font-size: 13px; cursor: pointer; }
  .msg-folder-chip[aria-pressed="true"] { border-color: transparent; background: var(--accent); color: var(--on-accent); }
  .msg-filters { display: flex; align-items: center; gap: var(--space-2); padding: 0 var(--space-4) var(--space-3); border-bottom: 1px solid var(--border-subtle); }
  .msg-filters button {
    flex: none; min-height: 36px; padding: 0 var(--space-3); white-space: nowrap; border: 0; border-radius: var(--radius-md);
    background: var(--bg-secondary); color: var(--text-secondary); font: var(--text-label); font-size: 14px; cursor: pointer;
  }
  .msg-filters button[aria-pressed="true"] { background: linear-gradient(135deg, var(--accent-strong-start), var(--accent-strong-end)); color: var(--on-accent); }
  @media (hover: hover) { .msg-filters button[aria-pressed="false"]:hover { background: var(--surface-hover); color: var(--text-primary); } }
  .msg-sort { flex: none; margin-left: auto; min-width: 96px; min-height: 36px; padding: 0 28px 0 var(--space-3); border: 1px solid var(--border-subtle); border-radius: var(--radius-md); background-color: var(--surface); color: var(--text-primary); font: var(--text-label); font-size: 13px; }
  .msg-conversations { flex: 1; min-height: 0; overflow: auto; list-style: none; margin: 0; padding: var(--space-2); }
  .msg-row-item { position: relative; }
  .msg-row-item + .msg-row-item { border-top: 1px solid var(--border-subtle); }
  .msg-row-pin { position: absolute; left: 2px; top: 22px; z-index: 1; color: var(--accent); pointer-events: none; }
  .msg-row {
    display: flex; align-items: flex-start; gap: var(--space-3); padding: var(--space-4) var(--space-3) var(--space-4) var(--space-5); border-radius: var(--radius-lg);
    color: var(--text-primary); text-decoration: none; transition: background-color var(--dur-fast) var(--ease-standard);
  }
  @media (hover: hover) { .msg-row:hover { background: var(--surface-hover); } }
  .msg-row[aria-current="true"] { background: var(--surface-mint); }
  .msg-row-main { flex: 1; min-width: 0; display: grid; gap: 3px; }
  .msg-row-top, .msg-row-bottom { display: flex; align-items: center; gap: var(--space-2); min-width: 0; }
  .msg-row-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font: var(--text-label); font-size: 15px; }
  .msg-row-star { color: var(--warning); display: inline-grid; }
  .msg-row-star .icon { fill: currentColor; }
  .msg-row-muted { color: var(--text-muted); display: inline-grid; }
  .msg-row-time { flex: none; margin-left: auto; font: var(--text-caption); color: var(--text-muted); }
  .msg-row-preview { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font: var(--text-body-sm); color: var(--text-muted); }
  /* Unread is shown by weight + a count, not by color alone. */
  .msg-row.is-unread .msg-row-name { font-weight: 800; }
  .msg-row.is-unread .msg-row-preview { color: var(--text-primary); font-weight: 600; }
  .msg-row-count { flex: none; min-width: 24px; height: 24px; padding: 0 7px; border-radius: var(--radius-pill); background: var(--accent); color: var(--on-accent); font: var(--text-caption); font-weight: 700; line-height: 24px; text-align: center; }
  .msg-row-chips { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; min-width: 0; }
  .msg-row-project, .msg-row-label { display: inline-flex; align-items: center; gap: 4px; max-width: 100%; padding: 2px 8px; border-radius: var(--radius-sm); background: var(--surface-muted); color: var(--text-secondary); font: var(--text-caption); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
  .msg-row[aria-current="true"] .msg-row-project { background: var(--surface); }
  .msg-row-project .icon { width: 13px; height: 13px; flex: none; }
  .msg-row-label::before { content: ""; flex: none; width: 8px; height: 8px; border-radius: var(--radius-pill); background: var(--msg-label, var(--label-gray)); }
  .msg-list-empty { padding: var(--space-4); }
  .msg-list-foot { padding: 0 var(--space-3) var(--space-2); text-align: center; }
  .msg-list-foot:empty { display: none; }
  .msg-connection { margin: var(--space-2) var(--space-4) 0; padding: var(--space-2) var(--space-3); border-radius: var(--radius-sm); background: var(--warning-subtle); color: var(--text-primary); font: var(--text-caption); }

  /* ---------- avatars (identity: real photo, else initial) ---------- */
  .msg-avatar { position: relative; display: inline-grid; place-items: center; flex: none; overflow: hidden; border-radius: var(--radius-pill); background: var(--surface-mint-strong); color: var(--accent); font-weight: 700; }
  .msg-avatar img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
  .msg-avatar-xs { width: 20px; height: 20px; font-size: 11px; }
  .msg-avatar-sm { width: 28px; height: 28px; font-size: 12px; }
  .msg-avatar-md { width: 40px; height: 40px; font-size: 15px; }
  .msg-avatar-lg { width: 52px; height: 52px; font-size: 19px; }
  .msg-avatar-xl { width: 72px; height: 72px; font-size: 26px; }
  .msg-group-glyph { background: var(--surface-mint); }
  .msg-faces { position: relative; display: inline-block; flex: none; }
  .msg-faces-lg { width: 52px; height: 52px; }
  .msg-faces-md { width: 40px; height: 40px; }
  .msg-faces-xl { width: 72px; height: 72px; }
  .msg-faces .msg-avatar { position: absolute; box-shadow: 0 0 0 2px var(--surface); }
  .msg-faces-lg .msg-avatar { width: 32px; height: 32px; }
  .msg-faces-1 .msg-avatar { inset: 0; width: 100%; height: 100%; }
  .msg-faces-2 .msg-avatar:nth-child(1), .msg-faces-3 .msg-avatar:nth-child(1) { left: 0; top: 0; }
  .msg-faces-2 .msg-avatar:nth-child(2) { right: 0; bottom: 0; }
  .msg-faces-3 .msg-avatar:nth-child(2) { right: 0; top: 4px; }
  .msg-faces-3 .msg-avatar:nth-child(3) { left: 10px; bottom: 0; }

  /* ---------- thread ---------- */
  .msg-thread-pane { position: relative; display: flex; flex-direction: column; overflow: hidden; }
  .msg-thread-head { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-4) var(--space-5); border-bottom: 1px solid var(--border-subtle); }
  .msg-back { display: none; }
  .msg-thread-who { flex: 1; min-width: 0; }
  .msg-thread-name { display: flex; align-items: center; gap: var(--space-1); margin: 0; font: 800 19px/1.35 var(--font-sans); color: var(--text-primary); overflow-wrap: anywhere; }
  .msg-star { display: inline-grid; place-items: center; width: 32px; height: 32px; padding: 0; border: 0; border-radius: var(--radius-pill); background: transparent; color: var(--text-muted); cursor: pointer; }
  .msg-star[aria-pressed="true"] { color: var(--warning); }
  .msg-star[aria-pressed="true"] .icon { fill: currentColor; }
  .msg-thread-meta { margin: 2px 0 0; font: var(--text-body-sm); color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .msg-muted-note { display: inline-flex; align-items: center; gap: 2px; vertical-align: bottom; }
  .msg-thread-actions { display: flex; align-items: center; gap: 2px; flex: none; }
  .msg-info-toggle { display: none; }
  .msg-menu-wrap { position: relative; }
  .msg-menu { position: absolute; z-index: var(--z-dropdown); right: 0; top: calc(100% + 6px); display: grid; min-width: 200px; padding: var(--space-1); border: 1px solid var(--border-subtle); border-radius: var(--radius-md); background: var(--surface-raised); box-shadow: var(--shadow-float); }
  .msg-menu[hidden] { display: none; }
  .msg-menu button { display: flex; align-items: center; gap: var(--space-3); min-height: 40px; padding: 0 var(--space-3); border: 0; border-radius: var(--radius-sm); background: transparent; color: var(--text-primary); font: var(--text-body-sm); text-align: left; cursor: pointer; }
  .msg-menu button .icon { color: var(--text-secondary); }
  .msg-menu button:hover, .msg-menu button:focus-visible { background: var(--surface-hover); }
  .msg-menu .is-danger, .msg-menu .is-danger .icon { color: var(--danger); }
  .msg-thread-search { position: relative; display: flex; align-items: center; gap: var(--space-2); margin: var(--space-3) var(--space-5) 0; }
  .msg-thread-search[hidden] { display: none; }
  .msg-thread-search > .icon { position: absolute; left: 12px; color: var(--text-muted); }
  .msg-thread-search input { flex: 1; height: 40px; padding: 0 var(--space-3) 0 38px; border: 1px solid var(--border-default); border-radius: var(--radius-md); background: var(--surface); color: var(--text-primary); font-size: 15px; }
  .msg-find-results { position: absolute; z-index: var(--z-dropdown); left: 0; right: 44px; top: calc(100% + 4px); max-height: 320px; overflow: auto; margin: 0; padding: var(--space-1); list-style: none; border: 1px solid var(--border-subtle); border-radius: var(--radius-md); background: var(--surface-raised); box-shadow: var(--shadow-float); }
  .msg-find-results[hidden] { display: none; }
  .msg-find-results button { display: grid; grid-template-columns: auto 1fr auto; gap: var(--space-2); width: 100%; padding: var(--space-2) var(--space-3); border: 0; border-radius: var(--radius-sm); background: transparent; color: var(--text-primary); font: var(--text-body-sm); text-align: left; cursor: pointer; }
  .msg-find-results button:hover { background: var(--surface-hover); }
  .msg-find-results span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-secondary); }
  .msg-find-results time, .msg-find-empty { font: var(--text-caption); color: var(--text-muted); }
  .msg-find-empty { padding: var(--space-3); }
  .msg-project-card {
    display: flex; align-items: center; gap: var(--space-3); margin: var(--space-3) var(--space-5) 0; padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-lg); background: var(--surface-muted); color: var(--text-primary); text-decoration: none; border: 1px solid var(--border-subtle);
  }
  @media (hover: hover) { .msg-project-card:hover { border-color: var(--border-default); } }
  .msg-project-icon { display: grid; place-items: center; flex: none; width: 44px; height: 44px; border-radius: var(--radius-md); background: var(--accent-subtle); color: var(--accent); }
  .msg-project-text { flex: 1; min-width: 0; display: grid; }
  .msg-project-label { font: var(--text-caption); color: var(--text-muted); }
  .msg-project-text strong { font: var(--text-label); font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .msg-thread-connection { margin-inline: var(--space-5); }
  .msg-scroll { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: var(--space-3) var(--space-5) var(--space-4); scroll-padding-bottom: var(--space-4); }
  .msg-scroll:focus-visible { outline-offset: -2px; }
  .msg-older, .msg-newer { display: flex; justify-content: center; }
  .msg-older:empty, .msg-newer:empty { display: none; }
  .msg-newer { margin-top: var(--space-3); }
  .msg-items { list-style: none; margin: 0; padding: 0; }
  .msg-day { display: flex; align-items: center; gap: var(--space-3); margin: var(--space-5) 0 var(--space-2); }
  .msg-day::before, .msg-day::after { content: ""; flex: 1; border-top: 1px solid var(--border-subtle); }
  .msg-day span { padding: 4px var(--space-4); border-radius: var(--radius-pill); background: var(--surface-muted); color: var(--text-secondary); font: var(--text-caption); font-size: 13px; }
  .msg-unread-divider { display: flex; align-items: center; gap: var(--space-3); margin: var(--space-4) 0; color: var(--accent); font: var(--text-caption); font-weight: 600; }
  .msg-unread-divider::before, .msg-unread-divider::after { content: ""; flex: 1; border-top: 1px solid var(--accent); opacity: .5; }
  .msg-system { display: flex; justify-content: center; margin: var(--space-3) 0; }
  .msg-system p { margin: 0; padding: 4px var(--space-3); border-radius: var(--radius-pill); background: var(--surface-muted); color: var(--text-muted); font: var(--text-caption); text-align: center; }
  .msg { position: relative; display: flex; gap: var(--space-3); margin-top: var(--space-5); }
  .msg.is-continued { margin-top: 6px; }
  .msg.is-found .msg-bubble { box-shadow: 0 0 0 2px var(--accent); }
  .msg-side { flex: none; width: 40px; }
  .msg.mine { flex-direction: row-reverse; }
  .msg-content { position: relative; display: flex; flex-direction: column; align-items: flex-start; gap: 6px; min-width: 0; max-width: min(78%, 620px); }
  .msg.mine .msg-content { align-items: flex-end; }
  .msg-author { display: flex; align-items: baseline; gap: var(--space-2); margin: 0; font: var(--text-label); font-size: 14px; color: var(--text-primary); }
  .msg-author time { font: var(--text-caption); color: var(--text-muted); }
  .msg-bubble { padding: 12px 16px; border-radius: 18px; background: var(--surface-muted); color: var(--text-primary); border: 1px solid var(--border-subtle); }
  .msg:not(.mine):not(.is-continued) .msg-bubble { border-top-left-radius: 6px; }
  .msg.mine .msg-bubble { background: var(--surface-mint); border-color: var(--surface-mint-strong); }
  .msg.mine:not(.is-continued) .msg-bubble { border-top-right-radius: 6px; }
  .msg-text { margin: 0; font: var(--text-body); white-space: pre-wrap; overflow-wrap: anywhere; word-break: normal; }
  .msg-text a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
  .msg-mention { padding: 1px 6px; border-radius: var(--radius-sm); background: var(--accent-subtle); color: var(--accent); font-weight: 600; }
  .msg-mention.is-me { background: var(--accent); color: var(--on-accent); }
  .msg-meta { display: flex; flex-wrap: wrap; align-items: center; column-gap: var(--space-2); row-gap: 0; font: var(--text-caption); color: var(--text-muted); }
  .msg-meta > * { white-space: nowrap; }
  .msg:not(.mine) .msg-meta .msg-time { display: none; }
  .msg:not(.mine).is-continued .msg-meta .msg-time { display: inline; }
  .msg.mine .msg-meta { justify-content: flex-end; }
  /* Time and receipt only on the last bubble of a group (a failed state always shows). */
  .msg:has(+ .msg.is-continued) .msg-meta:not(:has(.msg-failed)):not(:has(.msg-receipt.is-read)) { display: none; }
  .msg-receipt { display: inline-flex; align-items: center; gap: 3px; padding: 0; border: 0; background: transparent; color: var(--text-muted); font: var(--text-caption); font-weight: 600; }
  button.msg-receipt { cursor: pointer; min-height: 24px; }
  button.msg-receipt:hover span { text-decoration: underline; }
  .msg-receipt.is-read { color: var(--accent); }
  .msg-receipt:empty { display: none; }
  .msg-receipt .icon { width: 14px; height: 14px; }
  .msg.is-sending .msg-bubble { opacity: .65; }
  .msg.is-failed .msg-bubble { box-shadow: inset 0 0 0 1px var(--danger); }
  .msg-failed { color: var(--danger); }
  .msg-retry, .msg-discard { min-height: 28px; padding: 0 var(--space-2); border: 0; border-radius: var(--radius-sm); background: transparent; color: var(--accent); font: var(--text-caption); font-weight: 700; cursor: pointer; }
  .msg-discard { color: var(--text-muted); }
  .msg-tools { position: absolute; top: -6px; right: -44px; display: flex; opacity: 0; transition: opacity var(--dur-fast) var(--ease-standard); }
  .msg.mine .msg-tools { right: auto; left: -44px; }
  .msg:hover .msg-tools, .msg:focus-within .msg-tools { opacity: 1; }
  .msg-tool { display: grid; place-items: center; width: 34px; height: 34px; border: 1px solid var(--border-subtle); border-radius: var(--radius-pill); background: var(--surface); color: var(--text-secondary); box-shadow: var(--shadow-1); cursor: pointer; }
  .msg-reactions { display: flex; flex-wrap: wrap; gap: 6px; }
  .msg-reactions[hidden] { display: none; }
  .msg-reaction { display: inline-flex; align-items: center; gap: 4px; min-height: 30px; padding: 0 10px; border: 1px solid var(--border-subtle); border-radius: var(--radius-pill); background: var(--surface); color: var(--text-secondary); font: var(--text-caption); font-size: 13px; cursor: pointer; }
  .msg-reaction[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-subtle); color: var(--accent); }
  .msg-files { display: grid; gap: var(--space-2); max-width: 100%; }
  .msg-image { display: block; overflow: hidden; max-width: 320px; border-radius: var(--radius-lg); border: 1px solid var(--border-subtle); background: var(--surface-muted); }
  .msg-image img { display: block; width: 100%; height: auto; max-height: 280px; object-fit: cover; }
  .msg-file-card { display: flex; align-items: center; gap: var(--space-3); min-width: 260px; max-width: 100%; padding: var(--space-3) var(--space-3) var(--space-3) var(--space-4); border: 1px solid var(--border-subtle); border-radius: var(--radius-lg); background: var(--surface); }
  .msg-file-card.is-local { opacity: .7; }
  .msg-file-icon { display: grid; place-items: center; flex: none; width: 44px; height: 44px; border-radius: var(--radius-md); font: 800 13px/1 var(--font-sans); }
  .msg-tone-green { background: var(--tone-green-bg); color: var(--tone-green-fg); } .msg-tone-red { background: var(--tone-red-bg); color: var(--tone-red-fg); }
  .msg-tone-orange { background: var(--tone-orange-bg); color: var(--tone-orange-fg); } .msg-tone-blue { background: var(--tone-blue-bg); color: var(--tone-blue-fg); }
  .msg-tone-teal { background: var(--tone-teal-bg); color: var(--tone-teal-fg); } .msg-tone-purple { background: var(--tone-purple-bg); color: var(--tone-purple-fg); }
  .msg-tone-gray { background: var(--tone-gray-bg); color: var(--tone-gray-fg); }
  .msg-file-text { display: grid; flex: 1; min-width: 0; }
  .msg-file-text strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font: var(--text-label); font-size: 14px; color: var(--text-primary); }
  .msg-file-text small { font: var(--text-caption); color: var(--text-muted); }
  .msg-file-download { background: var(--surface-muted); color: var(--text-secondary); }
  .msg-link-card { display: flex; align-items: center; gap: var(--space-3); min-width: 240px; max-width: 100%; padding: var(--space-2) var(--space-3); border: 1px solid var(--border-subtle); border-radius: var(--radius-lg); background: var(--surface); color: var(--text-primary); text-decoration: none; }
  .msg-link-card span { display: grid; min-width: 0; flex: 1; }
  .msg-link-card strong { font: var(--text-label); font-size: 13px; }
  .msg-link-card small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font: var(--text-caption); color: var(--text-muted); }
  .msg-thread-empty { display: grid; justify-items: center; gap: var(--space-2); padding: var(--space-12) var(--space-4); text-align: center; color: var(--text-muted); font: var(--text-body-sm); }
  .msg-thread-empty strong { font: var(--text-h3); color: var(--text-primary); }
  .msg-jump {
    position: absolute; left: 50%; bottom: 96px; transform: translateX(-50%); z-index: 2;
    display: inline-flex; align-items: center; gap: var(--space-1); min-height: 36px; padding: 0 var(--space-4);
    border: 0; border-radius: var(--radius-pill); background: var(--accent); color: var(--on-accent); font: var(--text-label); font-size: 13px; box-shadow: var(--shadow-2); cursor: pointer;
  }
  .msg-jump[hidden] { display: none; }
  .msg-jump .icon { width: 16px; height: 16px; }

  /* Popover (reactions, readers): positioned through CSS custom properties set via CSSOM. */
  .msg-popover { position: absolute; z-index: var(--z-dropdown); left: var(--pop-left, 16px); top: var(--pop-top, 16px); width: max-content; max-width: 280px; max-height: 320px; overflow: auto; padding: var(--space-2); border: 1px solid var(--border-subtle); border-radius: var(--radius-md); background: var(--surface-raised); box-shadow: var(--shadow-float); }
  .msg-popover[data-placement="above"] { transform: translateY(-100%); }
  .msg-popover[hidden] { display: none; }
  .msg-pop-reactions { display: flex; gap: 2px; }
  .msg-pop-reactions button { display: grid; place-items: center; width: 40px; height: 40px; border: 0; border-radius: var(--radius-pill); background: transparent; font-size: 20px; cursor: pointer; }
  .msg-pop-reactions button:hover, .msg-pop-reactions button:focus-visible { background: var(--surface-hover); }
  .msg-pop-title { margin: var(--space-1) var(--space-2); font: var(--text-caption); font-weight: 700; color: var(--text-secondary); }
  .msg-pop-empty { margin: var(--space-1) var(--space-2) var(--space-2); font: var(--text-caption); color: var(--text-muted); list-style: none; }
  .msg-pop-people { display: grid; gap: 2px; margin: 0 0 var(--space-2); padding: 0; list-style: none; }
  .msg-pop-people li { display: flex; align-items: center; gap: var(--space-2); padding: 4px var(--space-2); font: var(--text-body-sm); }
  .msg-pop-close { width: 100%; min-height: 32px; border: 0; border-radius: var(--radius-sm); background: var(--bg-secondary); color: var(--text-secondary); font: var(--text-caption); cursor: pointer; }

  /* Composer */
  .msg-composer { position: relative; display: grid; gap: var(--space-2); padding: var(--space-3) var(--space-4); border-top: 1px solid var(--border-subtle); background: var(--surface); }
  .msg-composer.is-dragging { box-shadow: inset 0 0 0 2px var(--accent); }
  .msg-composer-row { display: flex; align-items: flex-end; gap: var(--space-2); }
  .msg-composer textarea {
    flex: 1; min-width: 0; min-height: 46px; max-height: 160px; resize: none; margin: 0; padding: 11px var(--space-4);
    border: 1px solid var(--border-default); border-radius: var(--radius-lg); background: var(--surface-muted); color: var(--text-primary);
    font: var(--text-body); font-size: 16px; line-height: 22px;
  }
  .msg-composer textarea:focus { border-color: var(--accent); background: var(--surface); outline: var(--focus-width) solid var(--focus-ring); outline-offset: 1px; }
  .msg-composer textarea:disabled { background: var(--bg-secondary); color: var(--text-muted); }
  .msg-composer .app-icon-button { color: var(--text-secondary); }
  .msg-counter { position: absolute; right: 72px; top: -22px; padding: 1px 8px; border-radius: var(--radius-pill); background: var(--surface-raised); box-shadow: var(--shadow-1); font: var(--text-caption); color: var(--text-muted); }
  .msg-counter.is-over { color: var(--danger); }
  .msg-counter[hidden] { display: none; }
  .msg-send { display: grid; place-items: center; flex: none; width: 52px; height: 46px; border: 0; border-radius: var(--radius-lg); background: linear-gradient(135deg, var(--accent-strong-start), var(--accent-strong-end)); color: var(--on-accent); cursor: pointer; }
  @media (hover: hover) { .msg-send:not(:disabled):hover { filter: brightness(1.08); } }
  .msg-send:disabled { opacity: .45; cursor: not-allowed; }
  .msg-uploads { display: flex; flex-wrap: wrap; gap: var(--space-2); }
  .msg-uploads[hidden] { display: none; }
  .msg-upload { display: flex; align-items: center; gap: var(--space-2); max-width: 280px; padding: var(--space-2); border: 1px solid var(--border-subtle); border-radius: var(--radius-md); background: var(--surface-muted); }
  .msg-upload .msg-file-icon { width: 36px; height: 36px; font-size: 11px; }
  .msg-upload-text { display: grid; min-width: 0; flex: 1; }
  .msg-upload-text strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font: var(--text-caption); font-weight: 700; color: var(--text-primary); }
  .msg-upload-text small { font: var(--text-caption); color: var(--text-muted); }
  .msg-upload progress { width: 100%; height: 4px; accent-color: var(--accent); }
  .msg-upload.is-failed { border-color: var(--danger); }
  .msg-upload.is-failed small { color: var(--danger); }
  .msg-upload-retry { border: 0; background: transparent; color: var(--accent); font: var(--text-caption); font-weight: 700; cursor: pointer; }
  .msg-upload .app-icon-button { width: 32px; height: 32px; }
  .msg-mention-list { position: absolute; z-index: var(--z-dropdown); left: var(--space-4); bottom: calc(100% - 4px); width: min(320px, calc(100% - 32px)); margin: 0; padding: var(--space-1); list-style: none; border: 1px solid var(--border-subtle); border-radius: var(--radius-md); background: var(--surface-raised); box-shadow: var(--shadow-float); }
  .msg-mention-list[hidden] { display: none; }
  .msg-mention-list li { display: flex; align-items: center; gap: var(--space-2); padding: var(--space-2); border-radius: var(--radius-sm); font: var(--text-body-sm); cursor: pointer; }
  .msg-mention-list li small { margin-left: auto; font: var(--text-caption); color: var(--text-muted); }
  .msg-mention-list li[aria-selected="true"], .msg-mention-list li:hover { background: var(--surface-mint); }
  .msg-placeholder { display: grid; place-content: center; justify-items: center; gap: var(--space-2); height: 100%; padding: var(--space-8); text-align: center; color: var(--text-muted); }
  .msg-placeholder-icon { display: grid; place-items: center; width: 72px; height: 72px; border-radius: var(--radius-pill); background: var(--surface-mint); color: var(--accent); }
  .msg-placeholder h2 { margin: var(--space-2) 0 0; font: var(--text-h3); color: var(--text-primary); }
  .msg-placeholder p { margin: 0; font: var(--text-body-sm); }
  .msg-thread-loading { padding: var(--space-6); }

  /* ---------- context ---------- */
  .msg-context-pane { display: flex; flex-direction: column; gap: var(--space-4); overflow: auto; }
  .msg-context-pane:empty { display: none; }
  .msg-context-top { display: none; }
  .msg-card { padding: var(--space-5); border: 1px solid var(--border-subtle); border-radius: var(--radius-xl); background: var(--surface); box-shadow: var(--shadow-card); }
  .msg-card-head { display: flex; align-items: center; gap: var(--space-2); margin-bottom: var(--space-3); }
  .msg-card-title { display: flex; align-items: center; gap: var(--space-2); flex: 1; margin: 0; font: var(--text-section); font-size: 17px; color: var(--text-primary); }
  .msg-card-title .icon { color: var(--accent); }
  .msg-card-count { padding: 0 8px; border-radius: var(--radius-pill); background: var(--bg-secondary); font: var(--text-caption); color: var(--text-secondary); }
  .msg-card-more { display: inline-flex; align-items: center; gap: 2px; min-height: 32px; padding: 0; border: 0; background: transparent; color: var(--text-muted); font: var(--text-caption); font-size: 13px; text-decoration: none; cursor: pointer; white-space: nowrap; }
  .msg-card-more:hover { color: var(--accent); }
  .msg-card-text { margin: 0; font: var(--text-body-sm); color: var(--text-secondary); overflow-wrap: anywhere; }
  .msg-person { display: grid; justify-items: center; gap: var(--space-1); color: inherit; text-align: center; text-decoration: none; }
  .msg-person strong { margin-top: var(--space-2); font: var(--text-h3); }
  .msg-person span { font: var(--text-body-sm); color: var(--text-secondary); }
  .msg-project-info { display: grid; gap: var(--space-3); padding: var(--space-3); border: 1px solid var(--border-subtle); border-radius: var(--radius-lg); }
  .msg-project-top { display: grid; grid-template-columns: 80px minmax(0, 1fr); gap: var(--space-3); align-items: start; }
  .msg-project-cover { display: block; width: 80px; height: 80px; overflow: hidden; border-radius: var(--radius-md); background: var(--surface-mint); }
  .msg-project-cover img { width: 100%; height: 100%; object-fit: cover; }
  .msg-project-cover-empty { display: grid; place-items: center; width: 100%; height: 100%; }
  .msg-project-cover-empty img { width: 36px; height: auto; object-fit: contain; }
  .msg-project-info h3 { margin: 0 0 var(--space-2); font: var(--text-card-title); overflow-wrap: anywhere; }
  .msg-card-tags { display: flex; flex-wrap: wrap; gap: var(--space-1); margin: 0; }
  .msg-project-facts { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2); margin: 0; padding-top: var(--space-3); border-top: 1px solid var(--border-subtle); }
  .msg-project-facts dt { font: var(--text-caption); color: var(--text-muted); }
  .msg-project-facts dd { margin: 0; font: var(--text-label); }
  .msg-event { display: flex; gap: var(--space-3); padding: var(--space-3); border: 1px solid var(--border-subtle); border-radius: var(--radius-lg); }
  .msg-event-icon { display: grid; place-items: center; flex: none; width: 44px; height: 44px; border-radius: var(--radius-pill); background: var(--surface-muted); color: var(--accent); }
  .msg-event > span:last-child { display: grid; gap: 2px; min-width: 0; }
  .msg-event strong { font: var(--text-label); }
  .msg-event small { font: var(--text-caption); color: var(--text-secondary); }
  .community .msg-join { width: 100%; margin-top: var(--space-2); border: 0; background: var(--surface-mint); color: var(--accent); }
  .msg-file-list { display: grid; gap: 2px; margin: 0; padding: 0; list-style: none; }
  .msg-file-row { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-2); border-radius: var(--radius-md); color: inherit; text-decoration: none; }
  .msg-file-row:hover { background: var(--surface-hover); }
  .msg-member-mini { display: grid; gap: var(--space-2); margin: 0; padding: 0; list-style: none; }
  .msg-member-mini li { display: flex; align-items: center; gap: var(--space-2); font: var(--text-body-sm); }
  .msg-member-mini small { margin-left: auto; font: var(--text-caption); color: var(--accent); }
  .msg-shortcuts { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2); }
  .msg-shortcuts > a, .msg-shortcuts > button { display: flex; align-items: center; gap: var(--space-2); min-height: 48px; padding: 0 var(--space-3); border: 1px solid var(--border-subtle); border-radius: var(--radius-md); background: var(--surface); color: var(--text-primary); font: var(--text-label); font-size: 13px; text-align: left; text-decoration: none; cursor: pointer; }
  .msg-shortcuts .icon { flex: none; color: var(--accent); }
  .msg-shortcuts > a:hover, .msg-shortcuts > button:hover { background: var(--surface-hover); }

  /* ---------- dialogs ---------- */
  .community dialog.msg-dialog { width: min(440px, calc(100% - 32px)); max-width: none; padding: 0; border-radius: var(--radius-xl); }
  .community dialog.msg-dialog-wide { width: min(560px, calc(100% - 32px)); }
  .msg-dialog-head { display: flex; align-items: center; justify-content: space-between; padding: var(--space-4) var(--space-4) var(--space-2) var(--space-6); }
  .community .msg-dialog-head h2 { margin: 0; font: var(--text-h2); }
  .msg-dialog-body { display: grid; gap: var(--space-2); max-height: min(70dvh, 640px); overflow: auto; padding: 0 var(--space-6) var(--space-6); }
  .msg-dialog-body form { display: grid; gap: var(--space-2); }
  .msg-dialog-body p { margin: 0; font: var(--text-body-sm); color: var(--text-secondary); }
  .community .msg-dialog-sub { margin: var(--space-4) 0 0; font: var(--text-label); }
  .msg-tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; border-radius: var(--radius-md); background: var(--bg-secondary); }
  .community .msg-tabs button { min-height: 40px; border: 0; border-radius: var(--radius-sm); background: transparent; color: var(--text-secondary); font: var(--text-label); }
  .community .msg-tabs button[aria-selected="true"] { background: var(--surface); color: var(--accent); box-shadow: var(--shadow-1); }
  .msg-people-results { display: grid; gap: 2px; max-height: 300px; overflow: auto; margin: 0; padding: 0; list-style: none; }
  .community .msg-people-results button { display: flex; align-items: center; gap: var(--space-3); width: 100%; min-height: 56px; padding: var(--space-2); border: 0; border-radius: var(--radius-md); background: transparent; color: var(--text-primary); text-align: left; }
  .community .msg-people-results button:hover { background: var(--surface-hover); }
  .msg-people-results button > span:nth-child(2) { display: grid; flex: 1; min-width: 0; }
  .msg-people-results small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font: var(--text-caption); color: var(--text-muted); }
  .msg-check { display: grid; place-items: center; width: 24px; height: 24px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); color: transparent; }
  .msg-people-results [aria-pressed="true"] .msg-check { border-color: var(--accent); background: var(--accent); color: var(--on-accent); }
  .msg-chosen { display: flex; flex-wrap: wrap; gap: var(--space-1); }
  .msg-chosen:empty { display: none; }
  .community .msg-chip { display: inline-flex; align-items: center; gap: 4px; min-height: 32px; padding: 0 var(--space-2) 0 4px; border: 0; border-radius: var(--radius-pill); background: var(--surface-mint); color: var(--accent); font: var(--text-caption); font-weight: 700; }
  .msg-member-list { display: grid; gap: 2px; margin: 0; padding: 0; list-style: none; }
  .msg-member-list li { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-2) 0; }
  .msg-member-list li + li { border-top: 1px solid var(--border-subtle); }
  .msg-member-list li > span:nth-child(2) { display: grid; flex: 1; min-width: 0; }
  .msg-member-list small { font: var(--text-caption); color: var(--text-muted); }
  .msg-member-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 4px; }
  .msg-label-pick { display: grid; gap: 2px; margin: var(--space-2) 0; padding: 0; list-style: none; }
  .msg-label-pick label { display: flex; align-items: center; gap: var(--space-2); min-height: 40px; margin: 0; font: var(--text-body-sm); }
  .msg-color-pick { display: flex; gap: var(--space-2); margin: 0; padding: 0; border: 0; }
  .msg-color-pick label { display: grid; place-items: center; width: 36px; height: 36px; margin: 0; border-radius: var(--radius-pill); cursor: pointer; }
  .msg-color-pick input { position: absolute; opacity: 0; }
  .msg-color-pick .msg-label-dot { width: 22px; height: 22px; }
  .msg-color-pick label:has(input:checked) { box-shadow: 0 0 0 2px var(--accent); }
  .msg-color-pick label:has(input:focus-visible) { outline: var(--focus-width) solid var(--focus-ring); }

  /* ---------- responsive ---------- */
  @media (max-width: 1439px) {
    .msg-app { grid-template-columns: 208px 330px minmax(0, 1fr); }
    .msg-info-toggle { display: inline-grid; }
    .msg-context-pane { position: absolute; z-index: var(--z-dropdown); top: 0; right: 0; bottom: 0; width: min(340px, 90%); padding: var(--space-3); border-radius: var(--radius-xl); background: var(--bg-secondary); box-shadow: var(--shadow-3); }
    .msg-app:not([data-context="open"]) .msg-context-pane { display: none; }
    .msg-context-top { display: flex; justify-content: flex-end; }
  }
  @media (max-width: 1099px) {
    .msg-app { grid-template-columns: 320px minmax(0, 1fr); }
    .msg-nav-pane { display: none; }
    .msg-folder-chips { display: flex; }
    .msg-filters { display: none; }
    .msg-list-head .msg-new-compact { display: inline-grid; }
  }

  /* ---------- phone: list → conversation (not a shrunk desktop) ---------- */
  @media (max-width: 767px) {
    :root[data-route="messages"] .community.container { width: 100%; padding: 0; }
    :root[data-route="messages"] .site-footer { display: none; }
    .msg-app { display: block; height: auto; min-height: 0; }
    .msg-list-pane { border: 0; border-radius: 0; background: transparent; box-shadow: none; overflow: visible; }
    .msg-list-head { padding: var(--space-4) var(--space-4) var(--space-3); }
    .msg-list-title { display: none; }
    .msg-mobile-title { display: block; margin: 0; font: 800 26px/1.2 var(--font-sans); }
    .msg-list-head .msg-new-compact { display: inline-grid; background: var(--surface-mint); color: var(--accent); }
    .msg-search input { background: var(--surface); }
    .msg-folder-chips { padding-inline: var(--space-4); }
    .msg-conversations { overflow: visible; padding: 0 var(--space-3); }
    .msg-row { padding: var(--space-3) var(--space-2) var(--space-3) var(--space-4); }
    .msg-row-pin { top: 18px; left: 0; }
    .msg-thread-pane, .msg-context-pane { display: none; }
    .msg-app[data-view="thread"] .msg-list-pane { display: none; }
    .msg-app[data-view="thread"] .msg-thread-pane {
      display: flex; position: fixed; inset: 0; top: var(--msg-vvt, 0); z-index: calc(var(--z-header) + 2);
      height: var(--msg-vvh, 100dvh); border: 0; border-radius: 0; box-shadow: none;
    }
    .msg-app[data-view="thread"][data-context="open"] .msg-context-pane { display: flex; position: fixed; z-index: calc(var(--z-header) + 3); inset: 0; width: auto; border-radius: 0; padding: var(--space-3); padding-top: calc(var(--space-3) + env(safe-area-inset-top)); }
    :root[data-chat="open"] .bottom-nav { display: none; }
    :root[data-chat="open"] body { padding-bottom: 0; overflow: hidden; }
    .msg-back { display: inline-grid; margin-left: calc(-1 * var(--space-2)); }
    .msg-thread-head { gap: var(--space-2); padding: var(--space-2) var(--space-3); padding-top: calc(var(--space-2) + env(safe-area-inset-top)); }
    .msg-thread-head > .msg-avatar, .msg-thread-head > .msg-faces { width: 40px; height: 40px; }
    .msg-thread-head .msg-faces-lg .msg-avatar { width: 26px; height: 26px; }
    .msg-thread-name { font-size: 17px; }
    .msg-thread-meta { font-size: 12px; }
    .msg-thread-actions [data-thread-search] { display: none; }
    .msg-project-card { margin: var(--space-2) var(--space-3) 0; padding: var(--space-2) var(--space-3); }
    .msg-project-icon { width: 36px; height: 36px; }
    .msg-thread-search { margin-inline: var(--space-3); }
    .msg-scroll { padding: var(--space-2) var(--space-3) var(--space-3); }
    .msg-side { width: 32px; }
    .msg-side .msg-avatar-md { width: 32px; height: 32px; font-size: 13px; }
    .msg.mine .msg-side { display: none; }
    .msg-content { max-width: 84%; }
    .msg-bubble { border-radius: 20px; padding: 10px 14px; }
    .msg.mine .msg-bubble { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
    .msg.mine .msg-text a { color: var(--on-accent); }
    .msg.mine .msg-mention { background: var(--on-accent); color: var(--accent); }
    .msg-tools { display: none; position: static; opacity: 1; }
    .msg.is-active .msg-tools { display: flex; }
    .msg-file-card { min-width: 0; width: 100%; }
    .msg-image { max-width: 240px; }
    .msg-composer { padding: var(--space-2) var(--space-3) calc(var(--space-2) + env(safe-area-inset-bottom)); }
    .msg-composer-row { gap: 4px; }
    .msg-composer .app-icon-button { width: 40px; height: 40px; }
    .msg-send { width: 46px; }
    .msg-jump { bottom: calc(84px + env(safe-area-inset-bottom)); }
    .msg-counter { right: 60px; }
  }
}
