/* The attachment look agreed with Felix on 4 Nov 2026 (2:15 PM SGT).
   Pictures: small same-height thumbnails that wrap, sit to the right, above the message. Everything else: one compact chip.
   Annotations: a small chip with a pen and a count; hovering it shows each selected text and comment with edit and delete.
   The message box and the sent message use the same rules. Phone sizes are the :root values in the media block. */
:root {
  --attach-thumb-h: 56px;
  --attach-chip-h: 28px;
  --attach-chip-max: 190px;
  --attach-gap: 6px;
}
@media (max-width: 600px) {
  :root {
    --attach-thumb-h: 44px;
    --attach-chip-max: 150px;
    --attach-gap: 4px;
  }
}

/* The row of attachments above a message, aligned to the left (Felix, 4 Nov 3:14 PM SGT). */
.attachment-list, .msg-wrapper > .attachment-list {
  display: flex; flex-direction: row; flex-wrap: wrap; justify-content: flex-start; align-items: flex-start;
  gap: var(--attach-gap); width: 100%; max-width: 100%; box-sizing: border-box; margin: 0 0 var(--space-2);
}
.team-msg:not(.team-user) .attachment-list { justify-content: flex-start; }
#image-preview { gap: var(--attach-gap); align-items: flex-end; }
#image-preview .pending-file-preview { min-width: 0; min-height: 0; }

/* Picture thumbnail */
.attachment-card-thumb, #image-preview .attachment-card-thumb {
  display: inline-flex; flex-direction: row; width: auto; max-width: none; height: var(--attach-thumb-h);
  border: 1px solid var(--border); border-radius: 8px; overflow: hidden; box-sizing: content-box;
}
.attachment-card.attachment-card-thumb .attachment-media, #image-preview .attachment-card.attachment-card-thumb .attachment-media {
  display: block; width: auto; height: var(--attach-thumb-h); max-height: var(--attach-thumb-h);
  max-width: calc(var(--attach-thumb-h) * 1.5); min-width: calc(var(--attach-thumb-h) * 0.6);
  object-fit: cover; border: 0; border-radius: 0; cursor: zoom-in;
}

/* Compact chip: icon, name, size or length */
.attachment-card-chip, #image-preview .attachment-card-chip {
  display: inline-flex; flex-direction: row; align-items: center; width: auto; max-width: var(--attach-chip-max); min-width: 0;
}
.attachment-card-chip .pdf-attachment, #image-preview .attachment-card-chip .pdf-attachment {
  display: inline-flex; align-items: center; gap: var(--space-2); width: auto; max-width: var(--attach-chip-max); min-width: 0;
  height: var(--attach-chip-h); padding: 0 var(--space-3) 0 var(--space-2); box-sizing: border-box; border-radius: calc(var(--attach-chip-h) / 2); font-size: 12px;
}
.attachment-card-chip .pdf-attachment-icon, #image-preview .attachment-card-chip .pdf-attachment-icon {
  flex: 0 0 16px; width: 16px; height: 16px; background: transparent; border-radius: 0; font-size: 0;
}
.attachment-card-chip .pdf-attachment-icon svg, #image-preview .attachment-card-chip .pdf-attachment-icon svg { width: 14px; height: 14px; }
.attachment-card-chip .pdf-attachment-name, #image-preview .attachment-card-chip .pdf-attachment-name {
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; opacity: 1;
}
.attachment-card-chip .pdf-attachment-size { flex: none; font-size: 11px; opacity: 0.55; }
.attachment-card .attachment-media[hidden], .attachment-card audio.attachment-media[hidden], .attachment-card video.attachment-media[hidden] { display: none; }
/* A voice note or video opens its player under the chip when the chip is tapped */
.attachment-card-chip.attachment-card-playing { flex-direction: column; align-items: stretch; max-width: min(300px, 100%); }
.attachment-card-chip.attachment-card-playing .attachment-media { width: 100%; max-height: 160px; }
.attachment-card-chip .attachment-unavailable { display: none; }
.attachment-card-chip .pdf-attachment[aria-label^="File unavailable"] { border-color: var(--pc07-preserved-paint-1); }

/* Pasted-text card in a sent message: the same chip */
.sent-paste-card { height: var(--attach-chip-h); padding: 0 var(--space-3); margin: 0 var(--space-1); border-radius: calc(var(--attach-chip-h) / 2); max-width: var(--attach-chip-max); box-sizing: border-box; font-size: 12px; }
.sent-paste-card .sent-paste-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Chips in the queue list */
#durable-conversation-queue .queue-attachment-chip { height: var(--attach-chip-h); box-sizing: border-box; border-radius: calc(var(--attach-chip-h) / 2); max-width: var(--attach-chip-max); }

/* Annotation chip: small, a pen and a count; the quoted text is in the hover card */
.response-annotation-pending, .response-annotation-sent,
#image-preview .response-annotation-pending, .msg.user .response-annotation-sent, .msg.user .response-annotation-sent:first-child {
  position: relative; display: inline-flex; width: auto; max-width: var(--attach-chip-max); margin: 0; padding: 0; background: none; border: 0; box-shadow: none;
}
.response-annotation-pending .response-annotation-pending__open, #image-preview .response-annotation-pending .response-annotation-pending__open,
.response-annotation-sent .response-annotation-sent__open, .msg.user .response-annotation-sent .response-annotation-sent__open {
  display: inline-flex; align-items: center; flex-wrap: nowrap; gap: var(--space-2); width: auto; max-width: 100%; height: var(--attach-chip-h);
  padding: 0 var(--space-3) 0 var(--space-2); box-sizing: border-box; border-radius: calc(var(--attach-chip-h) / 2); white-space: nowrap; font-size: 12px;
}
.response-annotation-pending .response-annotation-pending__icon, #image-preview .response-annotation-pending .response-annotation-pending__icon,
.response-annotation-sent .response-annotation-sent__icon, .msg.user .response-annotation-sent .response-annotation-sent__icon {
  flex: 0 0 16px; width: 16px; height: 16px; background: transparent; border-radius: 0;
}
.response-annotation-pending__icon svg, .response-annotation-sent__icon svg { width: 14px; height: 14px; }
.response-annotation-pending .response-annotation-pending__preview, #image-preview .response-annotation-pending .response-annotation-pending__preview,
.response-annotation-sent .response-annotation-sent__preview, .msg.user .response-annotation-sent .response-annotation-sent__preview { display: none; }
.response-annotation-pending .pdf-attachment-name, .response-annotation-sent .pdf-attachment-name { font-size: 12px; max-width: none; }

/* The hover card */
.annotation-hover-card {
  display: none; position: absolute; bottom: 100%; left: 0; z-index: 60; width: max-content; max-width: min(290px, calc(100vw - 24px));
  box-sizing: border-box; padding: var(--space-2) var(--space-3); border: 1px solid var(--border); border-radius: 10px; background: var(--surface-raised, var(--pc07-preserved-paint-2));
  color: var(--text, var(--pc07-preserved-paint-3)); font-size: 12px; line-height: 1.4; text-align: left; white-space: normal; box-shadow: 0 6px 18px var(--pc07-preserved-paint-4);
}
/* the hover card opens from the left edge of its chip */
.response-annotation-pending:hover .annotation-hover-card, .response-annotation-pending:focus-within .annotation-hover-card,
.response-annotation-sent:hover .annotation-hover-card, .response-annotation-sent:focus-within .annotation-hover-card { display: block; }

/* The sent annotation chip sits above its message, left aligned, like the picture row (the chip stays inside the bubble in the DOM) */
.msg.user:has(> .response-annotation-sent) { position: relative; margin-top: calc(var(--attach-chip-h) + var(--space-2)); }
.msg.user > .response-annotation-sent, .msg.user > .response-annotation-sent:first-child { position: absolute; left: 0; bottom: 100%; margin: 0 0 var(--space-1); }
/* A message sent with only an annotation has no text, so there is no bubble: only the chip is shown (REPA-2026-11-23-29, Felix 23 Nov 2026 8:42 PM SGT). What was annotated is in the chip's hover card. */
.msg.user[data-chips-only="1"]:has(> .response-annotation-sent) { margin-top: 0; padding: 0; background: none; border: 0; box-shadow: none; }
.msg.user[data-chips-only="1"] > .response-annotation-sent, .msg.user[data-chips-only="1"] > .response-annotation-sent:first-child { position: static; margin: 0; }
/* Opened by a tap or click on the chip (a phone has no hover) */
.response-annotation-pending.is-open .annotation-hover-card, .response-annotation-sent.is-open .annotation-hover-card { display: block; }

/* ===== Many attachments (Felix, 4 Nov 2026, 7:49 PM SGT; PC-2026-11-04-101) =====
   The message box shows one row and a sent message two; the rest folds into a "+N" tile (attachment-overflow.js) that opens the grid of every file:
   a bottom sheet on a phone, a centred dialog on a desktop. The viewer steps through pictures (lightbox.js). */
.attach-folded { display: none !important; }
.attach-more-tile {
  position: relative; flex: none; display: inline-flex; align-items: center; justify-content: center;
  height: calc(var(--attach-thumb-h) + 2px); min-width: calc(var(--attach-thumb-h) + 2px); padding: 0 var(--space-2); box-sizing: border-box;
  border: 1px solid var(--border); border-radius: 8px; overflow: hidden; cursor: pointer;
  background: var(--surface) center / cover no-repeat; color: var(--text); font-family: inherit;
}
.attach-more-tile b { position: relative; font-size: 15px; font-weight: 600; line-height: 1; letter-spacing: 0.2px; }
.attach-more-tile.has-bg { color: var(--k-fg-005); }
.attach-more-tile.has-bg::before { content: ""; position: absolute; inset: 0; background: var(--k-fg-004); opacity: 0.55; }
.attach-more-tile:hover { border-color: var(--k-bd-007); }
.attach-more-tile:focus-visible { outline: 2px solid var(--k-bd-007); outline-offset: 2px; }

#attach-backdrop { position: fixed; inset: 0; z-index: 1000; background: var(--k-fx-033); display: none; }
#attach-backdrop.open { display: block; }
#attach-sheet {
  position: fixed; z-index: 1001; left: 0; right: 0; bottom: 0; max-height: 78vh; max-height: 78dvh;
  display: none; flex-direction: column; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-bottom: 0; border-radius: 14px 14px 0 0; box-shadow: 0 -8px 28px var(--k-fx-001);
  padding-bottom: env(safe-area-inset-bottom, 0);
}
#attach-sheet.open { display: flex; animation: attach-sheet-in 180ms ease-out; }
@keyframes attach-sheet-in { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (min-width: 601px) {
  #attach-sheet { left: 50%; right: auto; bottom: auto; top: 50%; width: min(620px, calc(100vw - 48px)); max-height: 80vh; transform: translate(-50%, -50%); border-bottom: 1px solid var(--border); border-radius: 14px; }
  #attach-sheet.open { animation: attach-dialog-in 160ms ease-out; }
  @keyframes attach-dialog-in { from { opacity: 0; transform: translate(-50%, -48%); } to { opacity: 1; transform: translate(-50%, -50%); } }
}
@media (prefers-reduced-motion: reduce) { #attach-sheet.open { animation: none; } }
#attach-sheet header { flex: none; display: flex; align-items: center; gap: var(--space-2); padding: var(--space-3) var(--space-2) var(--space-3) var(--space-4); border-bottom: 1px solid var(--border); }
#attach-sheet h2 { flex: 1; margin: 0; font-size: 15px; font-weight: 600; }
#attach-sheet h2 small { font-weight: 400; color: var(--text-dim); margin-left: var(--space-2); }
.attach-sheet-btn { min-width: 44px; height: 44px; padding: 0 var(--space-3); border: 0; border-radius: 8px; background: none; color: var(--text); font: inherit; font-size: 14px; cursor: pointer; }
.attach-sheet-btn[hidden] { display: none; }
.attach-sheet-btn:hover { background: var(--k-bg-026); }
.attach-sheet-btn.danger { color: var(--danger); }
.attach-sheet-btn.primary { color: var(--accent); font-weight: 600; }
.attach-sheet-btn:focus-visible { outline: 2px solid var(--k-bd-007); outline-offset: -2px; }
.attach-grid {
  --attach-thumb-h: 104px;
  flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: var(--space-4) var(--space-4) var(--space-5);
  display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: var(--space-4) var(--space-3); align-content: start;
}
@media (max-width: 600px) { .attach-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--space-4) var(--space-3); padding: var(--space-4) var(--space-4) var(--space-4); } }
.attach-grid-item { position: relative; aspect-ratio: 1; min-width: 0; }
.attach-grid-item.is-chip { aspect-ratio: auto; grid-column: 1 / -1; align-self: center; }
.attach-grid .attachment-card.attachment-card-thumb { width: 100%; height: 100%; box-sizing: border-box; display: flex; cursor: zoom-in; }
.attach-grid .attachment-card.attachment-card-thumb .attachment-media { width: 100%; height: 100%; max-height: none; max-width: none; min-width: 0; }
.attach-grid .attachment-card-chip { max-width: none; width: 100%; }
.attach-grid .attachment-card-chip .pdf-attachment { max-width: none; width: 100%; height: 44px; border-radius: 22px; font-size: 13px; }
.attach-grid .attachment-card-chip .pdf-attachment-name { font-size: 13px; }
.attach-grid .remove-img { position: absolute; top: -22px; right: -22px; width: 44px; height: 44px; padding: 0; z-index: 1; border: 0; background: transparent; color: transparent; font-size: 0; cursor: pointer; }
.attach-grid .remove-img::after { content: "x"; position: absolute; top: 12px; right: 12px; width: 20px; height: 20px; border-radius: 50%; background: var(--danger); color: var(--k-fg-005); font-size: 12px; display: flex; align-items: center; justify-content: center; }
.attach-grid .remove-img:focus-visible { outline: 2px solid var(--k-bd-007); outline-offset: 2px; }

/* The viewer: previous, next and a counter when the message has several pictures */
#lightbox-overlay .lb-nav { position: absolute; top: 50%; transform: translateY(-50%); width: 48px; height: 64px; border: 0; background: var(--k-fx-001); color: var(--k-fg-005); font-size: 30px; line-height: 1; cursor: pointer; border-radius: 10px; display: none; align-items: center; justify-content: center; }
#lightbox-overlay.gallery .lb-nav { display: flex; }
#lightbox-overlay #lb-prev { left: 10px; }
#lightbox-overlay #lb-next { right: 10px; }
#lightbox-overlay .lb-nav:disabled { opacity: 0.25; cursor: default; }
#lightbox-overlay .lb-nav:focus-visible { outline: 2px solid var(--k-fg-005); outline-offset: 2px; }
#lb-count { position: absolute; top: 24px; left: 20px; color: var(--k-fg-005); font-size: 14px; opacity: 0.85; display: none; }
#lightbox-overlay.gallery #lb-count { display: block; }
#lightbox-overlay #lightbox-img { touch-action: pan-y; }

/* REPA-2026-11-24-59: a message of attachments with no typed text shows the attachments alone, never an empty bubble. */
.msg.user[hidden] { display: none !important; }
.msg.user[data-attachments-only="1"] { background: none; border: 0; box-shadow: none; padding: 0; }
.team-bubble.team-bubble-attachments-only { background: none; border: 0; box-shadow: none; padding: 0; }

/* Pasted-text chip in the message box (REPA-2026-11-26-84, Felix 26 Nov 2026 10:27 PM SGT).
   One row: the file icon, then the name over one preview line, then the pencil. Nothing sits on top of the text,
   the name shows in full, and the icon has no square behind it (the paste chip contract in ui-behaviour-contracts). */
#image-preview .large-paste-card .large-paste-attachment {
  display: inline-grid; grid-template-columns: 20px minmax(0, 1fr) 14px; grid-template-rows: auto auto; column-gap: var(--space-2); align-items: center;
  width: fit-content; max-width: min(240px, 100%); min-height: 36px; height: auto; box-sizing: border-box;
  padding: var(--space-1) var(--space-3) var(--space-1) var(--space-2); border-radius: 12px; text-align: left;
}
#image-preview .large-paste-card .large-paste-attachment .pdf-attachment-icon {
  grid-column: 1; grid-row: 1 / span 2; width: 20px; height: 20px; flex: none; background: transparent !important; border-radius: 0;
}
#image-preview .large-paste-card .large-paste-attachment .pdf-attachment-icon svg { width: 16px; height: 16px; }
#image-preview .large-paste-card .large-paste-attachment .pdf-attachment-name {
  grid-column: 2; grid-row: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; opacity: 1;
}
#image-preview .large-paste-card .large-paste-attachment .large-paste-preview { grid-column: 2; grid-row: 2; min-width: 0; max-width: none; }
#image-preview .large-paste-card .large-paste-attachment .large-paste-preview-line:nth-child(n+2) { display: none; }
#image-preview .large-paste-card .large-paste-attachment .attachment-edit-mark {
  position: static; grid-column: 3; grid-row: 1 / span 2; align-self: center; justify-self: end; width: 14px; height: 14px; margin: 0;
}
#image-preview .large-paste-card .large-paste-attachment .attachment-edit-mark svg { width: 12px; height: 12px; }
/* end of the pasted-text chip rules (REPA-2026-11-26-84) */

/* The pasted-text card hugs its chip (REPA-2026-11-27-37). The red remove x is anchored to the card corner, and the card used to size to the first pasted line
   (hundreds of px) while the chip is capped at 240px, so the x floated beside the chip. The card takes the chip's cap and the chip fills the card. */
#image-preview .preview-item.large-paste-card { max-width: min(240px, 100%); }
#image-preview .large-paste-card .large-paste-attachment { width: 100%; max-width: none; }
/* end of the card-hugs-chip rules (REPA-2026-11-27-37) */

/* The attachment row holds still while the person types (Felix, 26 Nov 2026, REPA-2026-11-26-85). */
/* #large-paste-save-status is a polite live region for screen readers. On each keystroke the composer writes 'Saving draft text locally...' into it, and as the first */
/* flex item of the row its text took room and pushed the chips right and back. It leaves the layout (absolute, one clipped pixel) and stays readable to a screen reader. */
#image-preview #large-paste-save-status {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; border: 0;
  overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap;
}
/* end of the attachment row rule (REPA-2026-11-26-85) */

/* r3 row 3 (BUIL-2026-12-01-10, Felix-approved prototype and its sheet text): the pictures of a sent message form one album above the message, on the right,
   three to a row, rows wrapping. One picture is 56% of the album width, two are 38% each, three or more a third each; each picture keeps its own shape
   (a tall one is narrower). The list carries data-picture-count (ui-helpers.js) and each picture its ratio --ar (ui-helpers.js). */
#messages .msg-wrapper > .attachment-list {
  container-type: inline-size; width: min(100%, 640px); max-width: 100%; align-self: flex-end; margin-left: auto; justify-content: flex-end; --attach-gap: 8px;
  --cw: calc((100cqw - 2 * var(--attach-gap) - 1px) / 3); --attach-thumb-h: calc(var(--cw) * 0.68);
}
#messages .msg-wrapper > .attachment-list[data-picture-count="1"] { --cw: 56cqw; }
#messages .msg-wrapper > .attachment-list[data-picture-count="2"] { --cw: calc((100cqw - var(--attach-gap)) * 0.38); }
#messages .msg-wrapper > .attachment-list .attachment-card-thumb { height: auto; width: auto; border: 0; border-radius: 14px; box-sizing: border-box; }
#messages .msg-wrapper > .attachment-list .attachment-card.attachment-card-thumb .attachment-media {
  display: block; box-sizing: border-box; height: auto; max-height: none; min-width: 0; max-width: none; aspect-ratio: var(--ar, 1.5);
  width: min(var(--cw), calc(var(--cw) * 0.68 * var(--ar, 1.5))); border: 1px solid var(--border); border-radius: 14px; object-fit: cover;
}

/* r3 row 3 (BUIL-2026-12-01-10, Felix 3 Dec 2026 7:23 PM SGT): the pasted-text card of a sent message is one of the attachments, in the group on the right.
   Its eye follows the size label instead of sitting on top of it. */
#messages .msg-wrapper > .attachment-list .sent-paste-card { position: static; display: inline-flex; align-items: center; gap: var(--space-2); margin: 0; padding: 0 var(--space-3); }
#messages .msg-wrapper > .attachment-list .sent-paste-card .attachment-edit-mark { position: static; flex: none; }
