/* © 2026 MEK.txt. All rights reserved. Part of the MEK design system; not licensed for reuse. */
/* MEK design system · part: chat
   A messenger for any app: the list of chats, a chat's head, the thread (runs of bubbles with a stepped tail, a line
   for each day and for "New messages", replies, reactions, sent and seen) and the box to write in. Opt-in: link it
   after mek.css, with js/chat.js:
     <ol class="chat-flow" aria-label="Messages"></ol>
     mekChat.draw(flow, rows, { me, name, day, time, links, body, end })   // rows from the code bank's util/thread
   The look is here; the behaviour (what runs together, which day, what's new) is the code bank's util/thread, and
   finding links in text is its util/links. Needs the core box, avatar, field, button and tag. In the mek.parts layer,
   so a project's CSS wins. From Den and Meknegement, which each built one first (board t74). */

@layer mek.parts {

/* ---------- Dials ----------
   On every themed area too, so a chat in another theme works these out from its own colours. Theme-neutral by
   default: theirs on the ground with a line, mine on the surface. Mine in the accent is a project's choice:
     --chat-mine-fill: var(--accent); --chat-mine-edge: var(--accent); --chat-mine-ink: var(--100);
   Moving the ground (--chat-ground: var(--surface)) hides the default mine: move mine too. */
:root, [data-theme] {
  --chat-ground: var(--bg);              /* the thread's ground, and the day line's backing */
  --chat-theirs-fill: var(--chat-ground);
  --chat-theirs-edge: var(--line);
  --chat-theirs-ink: var(--text);
  --chat-mine-fill: var(--surface);
  --chat-mine-edge: var(--line);
  --chat-mine-ink: var(--text-strong);
  --chat-bubble-w: 78%;                  /* a message, at most, of the thread's width */
  --chat-say: var(--text-md);            /* a message's words, the box to write in */
  --chat-say-line: 18px;               /* their line, in px: the box to write in counts its lines by it */
  --chat-small: var(--text-sm);          /* names over messages, quotes, a row's last line */
  --chat-press: 36px;                    /* what a finger or pointer presses: Send, +, the chips' areas */
  --chat-compose-lines: 5;               /* the box to write in grows a line at a time to this, then scrolls */
}
@media (pointer: coarse) { :root, [data-theme] { --chat-press: var(--press-min); } }   /* a thumb's: 44 */

/* ---------- The list of chats ----------
   <nav class="chat-list"> of a.chat-row: a face, the name and when, the last line and an unread count. Unread rows
   have their name and last line in the strongest ink. */
.chat-list { display: grid; }
.chat-row { --ink: var(--text); --ink-to: var(--text-strong); display: grid; grid-template-columns: auto minmax(0, 1fr) auto;
  grid-template-areas: "face name time" "face last count"; align-items: center; column-gap: var(--space-3); row-gap: 2px;
  min-height: calc(var(--chat-press) + var(--space-4)); padding: var(--space-2) var(--space-1);
  border-bottom: var(--stroke) var(--line-style) var(--line); color: inherit; text-decoration: none; }
.chat-row > .avatar { grid-area: face; }
.chat-row-name { grid-area: name; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-row-time { grid-area: time; font: var(--text-crisp) var(--font-mono); color: var(--text-muted); }
.chat-row-last { grid-area: last; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: var(--chat-small); color: var(--text-muted); }
.chat-row > .tag { grid-area: count; justify-self: end; }
.chat-row.unread :is(.chat-row-name, .chat-row-last) { color: var(--text-strong); }
.chat-row .chat-draft { color: var(--text-strong); }   /* "Draft:" before what I'd started writing */
.chat-row[aria-current="page"] { background: var(--surface); }

/* ---------- A chat's head ----------
   Back (on a phone), a face, the name and a second line (who's in it, or "Ana is writing"). */
.chat-head { display: flex; align-items: center; gap: var(--space-3); min-width: 0; min-height: var(--chat-press); }
.chat-head-who { display: grid; min-width: 0; }
.chat-head-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-strong); }
.chat-head-sub { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: var(--chat-small); color: var(--text-muted); }
.chat-head-sub.typing::after { content: "..."; display: inline-block; clip-path: inset(0 100% 0 0);
  animation: chat-dots calc(4 * var(--dur-beat)) steps(4, jump-none) infinite; }
@keyframes chat-dots { to { clip-path: inset(0 0 0 0); } }
@media (prefers-reduced-motion: reduce) { .chat-head-sub.typing::after { animation: none; clip-path: none; } }

/* ---------- The thread ----------
   ol.chat-flow, scrolling inside its own box. One sender's run together, 4px apart, runs 12px apart: their name over
   the first (in a group chat), their face beside the last, the tail and the time under the last. 8px at the sides:
   room for the tails, which stand 4px out. */
.chat-flow { display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; margin: 0; padding: var(--space-2);
  list-style: none; overflow-x: hidden; overflow-y: auto; background: var(--chat-ground); outline-offset: var(--focus-offset); }
:where(.chat-flow > li) { min-width: 0; }   /* :where, so a row's own width (the day line's) wins */

/* A day: sticky at the top while its day scrolls by, every one the same width so the one stuck on top covers the last. */
.chat-day { align-self: center; position: sticky; top: var(--space-2); z-index: var(--z-sticky); margin-block: var(--space-3);
  min-width: 22ch; box-sizing: border-box; padding: var(--space-1) var(--space-2); text-align: center;
  background-color: var(--surface); font: var(--text-crisp) var(--font-mono); color: var(--text-muted); }
/* What happened (someone joined, a task was done), centred between the messages. */
.chat-event { align-self: center; max-width: var(--chat-bubble-w); padding-block: var(--space-2); text-align: center;
  font-size: var(--chat-small); color: var(--text-muted); }
/* "New messages": where I'd read to when I opened the chat, a rule each side. */
.chat-new { align-self: stretch; display: flex; align-items: center; gap: var(--space-2); padding-block: var(--space-2);
  font: var(--text-crisp) var(--font-mono); color: var(--text-muted); }
.chat-new::before, .chat-new::after { content: ""; flex: 1 1 0; height: var(--stroke); background: var(--line); }
/* After the last message, the page's to fill (how a chat ended): a box in the middle, its lines centred. */
.chat-end { align-self: center; display: grid; justify-items: center; gap: var(--space-2); max-width: var(--chat-bubble-w);
  margin-block: var(--space-2); padding: var(--space-3) var(--space-4); text-align: center; color: var(--text); }
/* Nothing said yet. */
.chat-empty { align-self: center; margin-block: auto; padding: var(--space-8) var(--space-4); text-align: center; color: var(--text-muted); }

/* A message. */
.msg { display: grid; grid-template-columns: minmax(0, auto); justify-items: start; gap: var(--space-1); align-self: flex-start;
  max-width: round(down, var(--chat-bubble-w), 1px); min-width: 0; }   /* on a whole pixel, so a capped bubble's tail meets its edge */
.msg.last { margin-bottom: var(--space-2); }
.msg.mine { align-self: flex-end; justify-items: end; }
.msg.with-face { grid-template-columns: var(--space-6) minmax(0, auto); column-gap: var(--space-2);
  grid-template-areas: ". who" "face bubble" ". reacts" ". time"; max-width: calc(var(--chat-bubble-w) + var(--space-8)); }
.msg.with-face > .msg-who { grid-area: who; }
.msg.with-face > .msg-face { grid-area: face; align-self: end; display: inline-flex; }
.msg.with-face > .msg-bubble { grid-area: bubble; }
.msg.with-face > .msg-reacts { grid-area: reacts; }
.msg.with-face > .msg-time { grid-area: time; }
.msg-who { padding-top: var(--space-1); font-size: var(--chat-small); color: var(--text-muted); }
.msg-bubble { --fill: var(--chat-theirs-fill); --fill-to: var(--chat-theirs-fill); --edge: var(--chat-theirs-edge); --edge-to: var(--text-soft);
  position: relative; display: grid; gap: var(--space-1); max-width: 100%; min-width: 0; padding: var(--space-2) var(--space-3);
  color: var(--chat-theirs-ink); outline-offset: var(--focus-offset); }
.msg.mine > .msg-bubble { --fill: var(--chat-mine-fill); --fill-to: var(--chat-mine-fill); --edge: var(--chat-mine-edge); --edge-to: var(--chat-mine-edge);
  color: var(--chat-mine-ink); }
.msg-bubble[data-acting] { --edge: var(--text-soft); --edge-to: var(--text-soft); }   /* its actions are open */
.msg-text { margin: 0; font: var(--chat-say)/var(--chat-say-line) var(--font-ui); color: inherit; overflow-wrap: anywhere; white-space: pre-wrap; }
.msg-text a { color: inherit; text-decoration: underline var(--stroke) solid; text-underline-offset: 2px; overflow-wrap: anywhere; }
.msg.gone .msg-text { color: var(--text-muted); }   /* "Message removed" */
/* The tail: 9 × 6 pixels at the foot of a run's last bubble, on the sender's side, in the bubble's own edge and fill. */
.msg-tail { position: absolute; left: -4px; bottom: 0; width: 9px; height: 6px; overflow: visible; pointer-events: none; }
.msg.mine .msg-tail { left: auto; right: -4px; scale: -1 1; }
.msg-tail .e { fill: var(--edge); }
.msg-tail .f { fill: var(--fill); }
/* The message a reply answers, at the top of its bubble: a line at its side, who, and what they said, one line. */
.msg-quote { display: grid; min-width: 0; max-width: 100%; padding: 0 0 0 var(--space-2); border: 0;
  border-inline-start: var(--stroke) solid currentColor; background: none; color: inherit; font: inherit; font-size: var(--chat-small);
  text-align: start; }
button.msg-quote { cursor: pointer; }
.msg-quote > * { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-quote-who { color: var(--text-muted); }
.msg.mine .msg-quote-who { color: inherit; }
/* When, and for mine whether it's sent (one check) or seen (two). */
.msg-time { display: inline-flex; align-items: center; gap: var(--space-1); max-width: 100%; font: var(--text-crisp) var(--font-mono); color: var(--text-muted); }
.msg-ticks { display: inline-flex; flex: none; }
.msg-ticks .icon { width: 15px; height: 15px; }
.msg-ticks.seen { color: var(--text-strong); }
/* Reactions under a message: no frame, the icon and its count in ink alone; everyone else's muted, mine (.on) the
   strongest, underlined too (in B/W a step of ink shows nothing); a press area round each. */
.msg-reacts { display: flex; flex-wrap: wrap; gap: var(--space-3); padding-inline: var(--space-1); }
.msg-chip { --ink: var(--text-muted); --ink-to: var(--text-strong); position: relative; display: inline-flex; align-items: center; gap: var(--space-1);
  min-height: var(--space-6); padding: 0; border: 0; background: none; font-size: var(--text-crisp); cursor: pointer; }
.msg-chip.on { --ink: var(--text-strong); }
.msg-chip.on .msg-chip-n { text-decoration: underline var(--stroke) solid; text-underline-offset: 2px; }
.msg-chip::before { content: ""; position: absolute; inset: 50% auto auto 50%; width: max(100%, var(--chat-press)); height: var(--chat-press); translate: -50% -50%; }
.msg-chip .icon { width: 15px; height: 15px; }
.msg-chip-n { min-width: 2ch; font-family: var(--font-mono); text-align: start; }
/* A message's own body (draw()'s body()): the page's element where its words would go, a photo or a card. */
.msg-body { min-width: 0; max-width: 100%; }
/* Bare: its body is the bubble (a photo alone): no room round it but the line, no tail, cut to the fill's stepped
   corners so it never paints over them. */
.msg-bubble.bare { padding: var(--stroke); }
.msg-bubble.bare > .msg-body { display: grid; clip-path: polygon(4px 0, calc(100% - 4px) 0, calc(100% - 4px) 1px, calc(100% - 2px) 1px,
  calc(100% - 2px) 2px, calc(100% - 1px) 2px, calc(100% - 1px) 4px, 100% 4px, 100% calc(100% - 4px), calc(100% - 1px) calc(100% - 4px),
  calc(100% - 1px) calc(100% - 2px), calc(100% - 2px) calc(100% - 2px), calc(100% - 2px) calc(100% - 1px), calc(100% - 4px) calc(100% - 1px),
  calc(100% - 4px) 100%, 4px 100%, 4px calc(100% - 1px), 2px calc(100% - 1px), 2px calc(100% - 2px), 1px calc(100% - 2px), 1px calc(100% - 4px),
  0 calc(100% - 4px), 0 4px, 1px 4px, 1px 2px, 2px 2px, 2px 1px, 4px 1px); }
/* Nothing to press (a card, one that didn't go, a removed one, a thread with no actions): no hover. */
.msg-bubble:not([tabindex]) { --edge-to: var(--edge); }
/* A card, not a message: theirs' colours on either side, so mine's fill never frames it. No tail, no ticks. */
.msg.card > .msg-bubble { --fill: var(--chat-theirs-fill); --fill-to: var(--chat-theirs-fill); --edge: var(--chat-theirs-edge); --edge-to: var(--chat-theirs-edge);
  color: var(--chat-theirs-ink); }
/* Mine that didn't go: hollow, mine's edge on the ground, why in --bad and Try again under its time. */
.msg.unsent > .msg-bubble { --fill: var(--chat-theirs-fill); --fill-to: var(--chat-theirs-fill); --edge: var(--chat-mine-edge); --edge-to: var(--chat-mine-edge);
  --edge-gap: var(--edge); color: var(--chat-theirs-ink); }   /* a state of its own: solid, in B/W too */
.msg-unsent { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: flex-end; column-gap: var(--space-2); max-width: 100%;
  font-size: var(--chat-small); }
.msg:not(.mine) > .msg-unsent { justify-content: flex-start; }
.msg-unsent > .link { position: relative; flex: none; font-size: inherit; }
.msg-unsent > .link::after { content: ""; position: absolute; inset: 50% auto auto 50%; width: max(100%, var(--chat-press)); height: var(--chat-press); translate: -50% -50%; }   /* a press area */
/* Who's seen it, in words, a quiet line under a run's last (a group's "Seen by everyone"; a 1-to-1's ticks say it). */
.msg-seen { max-width: 100%; font: var(--text-crisp) var(--font-mono); color: var(--text-muted); overflow-wrap: anywhere; }
.msg.mine > .msg-seen { text-align: end; }
/* A message shown after a jump to it blinks its edge twice. */
.msg-bubble.blink { animation: msg-blink calc(4 * var(--dur-beat)) var(--ease-step) 1; }
@keyframes msg-blink { 0%, 50% { --edge: var(--text-strong); } 25%, 75% { --edge: var(--line); } }
@media (prefers-reduced-motion: reduce) { .msg-bubble.blink { animation: none; } }

/* Jump to the latest: over the thread's foot at the right, with a count of what came since. Put it and the flow in
   one .chat-pane (a grid of one cell) so it sits over the flow. */
.chat-pane { display: grid; min-height: 0; }
.chat-pane > .chat-flow { grid-area: 1 / 1; }
.chat-jump { grid-area: 1 / 1; align-self: end; justify-self: end; position: relative; z-index: var(--z-sticky);
  margin: 0 var(--space-4) var(--space-2) 0; --fill: var(--bg); }
.chat-jump[hidden] { display: none; }
.chat-jump > .tag { position: absolute; top: calc(-1 * var(--space-2)); right: calc(-1 * var(--space-2)); }
.chat-jump > .tag:empty { display: none; }

/* ---------- The box to write in ----------
   .chat-compose: what I'm replying to (if anything) over a row of the box and Send (a project may add buttons, a +
   for a photo, beside them). The box grows a line at a time
   to --chat-compose-lines as I write, then scrolls inside. */
.chat-compose { display: grid; gap: var(--space-2); min-width: 0; }
.chat-replying { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: var(--space-2);
  padding-inline-start: var(--space-2); border-inline-start: var(--stroke) solid var(--text-soft); font-size: var(--chat-small); }
.chat-replying[hidden] { display: none; }
.chat-replying-what { display: grid; min-width: 0; }
.chat-replying-what > * { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-replying-who { color: var(--text-muted); }
.chat-compose-row { display: flex; align-items: flex-end; gap: var(--space-2); min-width: 0; }
.chat-compose-row > .field { flex: 1 1 auto; min-width: 0; }
.chat-compose-row > .field.area { --area-line: var(--chat-say-line);   /* no label notched in its top, so the text sits in the
     middle: the same room above and below, the field as tall as Send (--chat-press) at one line */
  padding-block: calc((var(--chat-press) - var(--area-line)) / 2 - var(--stroke)); }
.chat-compose-row > .field.area > textarea { height: var(--area-line); max-height: calc(var(--chat-compose-lines) * var(--area-line));
  font-size: var(--chat-say); }
.chat-compose-row > .btn { flex: none; min-height: var(--chat-press); }
.chat-compose-row > .btn.icon-only { width: var(--chat-press); }

/* ---------- Loading ----------
   A row or a bubble not drawn yet: the shape, its ink in the line, so nothing moves when it comes. */
.chat-row.loading :is(.chat-row-name, .chat-row-last) { color: transparent; background: var(--surface); }
.chat-row.loading .chat-row-name { width: 12ch; }
.chat-row.loading .chat-row-last { width: 24ch; max-width: 100%; }

/* ---------- A message's menu (mekChat.menu, board t97) ----------
   What a press or Enter on a message opens, by it: a strip of reactions, 8px apart from a menu of commands (the core
   .menu's look: one a row, the > sliding in, <hr> gaps, .danger quiet until reached), and, when the page passes who
   read it, a dithered split and "Seen by 3" with their faces, which swaps the commands for who read it (Back to
   return). A menu you press: rows --chat-press tall, words at --chat-say, an icon at --icon-text, no keys shown. In
   the top layer (popover), on the sender's side of the message, above it if it fits, else under. No read times. */
.chat-menu { --chat-menu-w: 280px;   /* the widest the commands get */
  position: fixed; inset: auto; margin: 0; padding: 0; border: 0; background: none; overflow: visible; color: var(--text);
  flex-direction: column; align-items: flex-start; gap: var(--space-2); }
.chat-menu:popover-open { display: flex; }
.chat-menu.mine { align-items: flex-end; }
.chat-menu:focus { outline: none; }   /* opened by a press: nothing reached yet */
/* It pops in once, whole; going back from who read it to the commands is instant (the core .menu would pop again). */
.chat-menu:popover-open { animation: mek-pop-in var(--dur-beat) var(--ease-frames-2) both; }
.chat-menu > * { animation: none; isolation: isolate; }   /* each box keeps its own frame (px-box draws it under itself) */
.chat-menu-reacts { display: flex; padding: 4px; --fill: var(--bg); --fill-to: var(--bg); --edge: var(--menu-edge); --edge-to: var(--menu-edge); }
/* A reaction: no box at rest (edge and fill on the ground), dissolving to --surface inside a --line edge on hover (an
   edge, so B/W shows it too); mine: a
   solid box, a shape, so it reads in B/W too. */
.chat-menu-react { display: grid; place-items: center; width: var(--chat-press); height: var(--chat-press); padding: 0; border: 0; cursor: pointer;
  --fill: var(--bg); --edge: var(--bg); --fill-to: var(--surface); --edge-to: var(--line); --ink: var(--text-soft); --ink-to: var(--text-strong); }
.chat-menu-react[aria-pressed="true"] { --fill: var(--surface); --fill-to: var(--surface); --edge: var(--text-soft); --edge-to: var(--text-soft); --ink: var(--text-strong); }
.chat-menu > .menu { position: relative; inset: auto; min-width: 200px; width: max-content; max-width: min(var(--chat-menu-w), 100vw - 2 * var(--space-2)); }
/* Rows a press tall, the words' line in the middle on a whole pixel (9 at 36, 13 at 44), the icon sitting in the line as
   any icon beside words. */
.chat-menu > .menu > button { min-height: var(--chat-press); align-items: flex-start; padding: calc((var(--chat-press) - 18px) / 2) 0 0;
  font-size: var(--chat-say); line-height: var(--chat-say-line); }
.chat-menu > .menu > button::before { top: calc((var(--chat-press) - 18px) / 2); }
.chat-menu .menu-label { min-width: 0; white-space: nowrap; }
.chat-menu .menu-label > .icon { width: var(--icon-text); height: var(--icon-text); vertical-align: var(--icon-text-drop); margin-block: var(--icon-text-pull); margin-right: var(--space-3); }
.chat-menu .menu-words { overflow: hidden; text-overflow: ellipsis; }
.chat-menu > .menu > .chat-menu-split { margin: 0 -8px; height: 8px; border: 0; padding: 0;   /* the thick gap, edge to edge inside the box: .dither-25's 2×2 */
  background: conic-gradient(var(--text-muted) 0 25%, var(--bg) 0) 0 0 / 2px 2px; }
.chat-menu-seen .avatars { flex-wrap: nowrap; margin-left: auto; padding-left: var(--space-3); }
.chat-menu-who { max-height: calc(6 * var(--chat-press)); overflow-y: auto; display: grid; }
.chat-menu-reader { display: flex; align-items: center; gap: var(--space-3); min-height: var(--chat-press);
  font: var(--chat-say)/var(--chat-say-line) var(--font-ui); color: var(--text); }
.chat-menu-reader > span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (prefers-reduced-motion: reduce) { .chat-menu:popover-open { animation: none; } }

}
