/* message.css: one message row: its states, the link card, the media player, the text, the pictures, GIFs, videos
   and voice, the action bar, the reactions, the editor, the More menu and the dialogs (js/ui/message.js, js/ui/video.js,
   js/ui/voice.js and js/ui/confirm.js; templates t-message, t-link-card, t-more-menu and t-delete-dialog).
   css/README.md has the map of the stylesheets. */

/* ==== Pending, failed and touched rows ==== */
/* js/ui/message.js; t-message */

/* Keep this section first in the file. These rules lived in app.css, which loads earlier, so every rule below wins
   a tie with them: a highlighted or reply-target row keeps its own tint over .touched, and an open picker or menu
   keeps pointer-events: auto on the bar of an edited row. */

/* A pending row is a full row, faded, until the server accepts it (MR-69). A failed row shows its reason (MR-71). */
.message-group.pending { opacity: 0.55; }
.message-group.failed { opacity: 1; }
.message-pending { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; font-size: 0.8125rem; color: var(--text-secondary); margin-top: 0.25rem; }
.message-pending .upload-progress-track { flex: 1 1 100%; }
.message-pending button { background: none; border: 1px solid var(--bg-muted); border-radius: 4px; color: inherit; padding: 0.125rem 0.5rem; cursor: pointer; font-size: inherit; }
.message-pending .pending-status { color: var(--color-danger); }
.message-pending .pending-reason { white-space: pre-line; }   /* one line for each refused file */

/* The files of a pending row while they upload (IB-34): the squares of the tray (css/composer.css), each with the
   state of its file. Waiting is dimmed, uploading has a bar along the bottom edge, done has a check mark, and a
   file the server refused has a warning mark. */
.pending-thumbs { display: flex; flex: 1 1 100%; flex-wrap: wrap; gap: 6px; }
.pending-thumb .upload-progress-track { position: absolute; left: 4px; right: 4px; bottom: 4px; min-width: 0; background: rgba(0, 0, 0, 0.55); }
.pending-thumb[data-state="waiting"] .tray-media,
.pending-thumb[data-state="waiting"] .tray-play { opacity: 0.45; }
.pending-thumb[data-state="done"] .upload-progress-track,
.pending-thumb[data-state="failed"] .upload-progress-track { display: none; }
.pending-mark { display: none; position: absolute; top: 4px; right: 4px; width: 18px; height: 18px; border-radius: 50%; color: #fff; font-size: 12px; font-weight: 700; line-height: 18px; text-align: center; }
.pending-thumb[data-state="done"] .pending-mark { display: block; background: var(--accent-primary); }
.pending-thumb[data-state="done"] .pending-mark::after { content: "✓"; }
.pending-thumb[data-state="failed"] .pending-mark { display: block; background: var(--color-danger); }
.pending-thumb[data-state="failed"] .pending-mark::after { content: "!"; }

/* A row's reason for two seconds (MR-75). */
.row-reason { position: absolute; right: 1rem; top: 0.25rem; background: var(--bg-tooltip); color: #fff; font-size: 0.75rem; padding: 0.25rem 0.5rem; border-radius: 4px; z-index: 6; }

/* A tapped row shows its bar and its tint on a touch screen (MR-50); the class goes away 5 s after the last
   touch or 50 px of scrolling (MR-51). The hover rules in message.css apply only where a pointer can hover. */
.message-group.touched { background: var(--bg-hover); }
.message-group.touched .message-actions { opacity: 1; pointer-events: auto; }
.message-group.editing .message-actions { opacity: 0 !important; pointer-events: none; }

/* ==== Link card ==== */
/* js/ui/message.js; t-link-card */

.link-preview-card {
    display: flex;
    gap: 16px;
    max-width: 520px;
    margin-top: 4px;
    padding: 12px 16px;
    background: var(--bg-sidebar);
    border-left: 4px solid var(--accent-primary);
    border-radius: 4px;
    text-decoration: none;
    color: inherit;
    overflow: hidden;
    transition: background 0.15s;
}

.link-preview-card:hover {
    background: var(--bg-hover);
}

.link-preview-content {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
    flex: 1;
}

.link-preview-site {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.02em;
}

.link-preview-title {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--accent-focus);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.link-preview-card:hover .link-preview-title {
    text-decoration: underline;
}

.link-preview-description {
    font-size: 0.85rem;
    color: var(--text-secondary);
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    line-height: 1.4;
}

.link-preview-thumbnail {
    flex-shrink: 0;
    width: 80px;
    height: 80px;
    border-radius: 4px;
    overflow: hidden;
}

.link-preview-thumbnail img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

@media (max-width: 600px) {
    .link-preview-card {
        max-width: 100%;
    }

    .link-preview-thumbnail {
        width: 60px;
        height: 60px;
    }
}

/* ==== Cached media player of a link: video or audio ==== */
/* js/ui/message.js; t-media-player */

.media-player-container {
    max-width: 520px;
    margin-top: 4px;
    border-radius: 8px;
    overflow: hidden;
}

.cached-video-player {
    /* max-width (not width: 100%) so per-video aspect-ratio can drive the box —
       portrait videos shrink width to satisfy max-height instead of being
       letterboxed in a too-wide container. */
    max-width: 100%;
    max-height: 400px;
    aspect-ratio: 16 / 9;
    border-radius: 8px;
    background: #000;
    display: block;
}

.video-info {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 8px 10px;
    background: var(--bg-sidebar);
    border-radius: 8px 8px 0 0;
}

/* When a title bar precedes the video, square the video's top so the two join seamlessly. */
.video-info + .cached-video-player {
    border-top-left-radius: 0;
    border-top-right-radius: 0;
}

.video-site {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.video-title {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-primary);
    text-decoration: none;
    /* up to 2 lines, then ellipsis */
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.video-title:hover {
    text-decoration: underline;
}

.cached-audio-player {
    display: flex;
    gap: 12px;
    padding: 12px;
    background: var(--bg-sidebar);
    border-radius: 8px;
    align-items: center;
}

.audio-thumbnail {
    width: 64px;
    height: 64px;
    border-radius: 4px;
    object-fit: cover;
    flex-shrink: 0;
}

.audio-info {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
    flex: 1;
}

.audio-title {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.audio-site {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.audio-element {
    width: 100%;
    height: 32px;
    margin-top: 4px;
}

@media (max-width: 600px) {
    .media-player-container {
        max-width: 100%;
    }

    .audio-thumbnail {
        width: 48px;
        height: 48px;
    }
}

/* ==== Message row: header, reply, text, pictures, GIFs, videos, voice, action bar, reactions, editor, menus, dialogs ==== */
/* js/ui/message.js, js/ui/video.js, js/ui/voice.js, js/ui/confirm.js; t-message, t-more-menu, t-delete-dialog, t-edit-box */

/* Message group container - Discord-style with avatar on left */
.message-group {
    display: flex;
    gap: 1rem;
    padding: 0.125rem 1rem 0.125rem calc(1rem + 40px + 1rem); /* Always reserve avatar space on left */
    position: relative;
    min-width: 0;
    max-width: 100%;
}

/* Only where a pointer can hover. A touch browser keeps the tapped row in :hover until the next tap, so an
   ungated rule showed a second bar and tint on the previous row (MR-50: one bar at a time). */
@media (hover: hover) {
    .message-group:hover:not(.system-message) {
        background: var(--bg-hover);
    }

    /* MR-81: a row under a shared header shows its time in the avatar column while the mouse is over it, so the
       reader sees how far the row is from the time in the header. The text is the row's data-time (ui/message.js).
       A pseudo-element draws it: the box exists only under the mouse, and a selection across rows does not copy it.
       A reply always has a header (MR-01), so this ::before never meets the reply line's ::before below.
       width: the avatar column (the row's left padding, 1rem + 40px + 1rem) less 0.5rem, so the time ends 8px
       before the text. The 600px tier has a narrower column and sets its own width.
       top: the row's top padding plus 0.11rem. The box is as tall as the first text line. A smaller font, centred
       in that line, stands above the baseline of the text by 0.36 of the size difference (Inter, Arial and
       Helvetica agree within a tenth of a pixel), and the 0.11rem puts the digits back on that baseline. */
    .message-group:not(.has-header, .system-message):hover::before {
        content: attr(data-time);
        position: absolute;
        left: 0;
        top: calc(0.125rem + 0.11rem);
        width: calc(1rem + 40px + 0.5rem);
        text-align: right;
        font-size: 0.6875rem;
        line-height: 1.375rem;
        font-variant-numeric: tabular-nums;
        white-space: nowrap;
        color: var(--text-muted);
        pointer-events: none;
    }
}

/* Messages with header show avatar in the reserved space */
.message-group.has-header {
    padding-left: 1rem;
    margin-top: 1.0625rem;
    padding-top: 0.25rem;
}

/* Reply messages: flex-wrap so reply-preview takes its own row above avatar+body */
.message-group.has-reply {
    flex-wrap: wrap;
    row-gap: 0;
}

/* Reply preview - full-width first row, indented to align with message text */
.reply-preview {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 0.25rem;
    cursor: pointer;
    font-size: 0.8125rem;
    color: var(--text-secondary);
    padding-left: calc(40px + 1rem); /* avatar + gap */
    margin-bottom: 0.25rem;
}

.reply-preview:hover .reply-author {
    text-decoration: underline;
}

/* Small avatar in reply preview */
.reply-preview .avatar {
    width: 16px !important;
    height: 16px !important;
    font-size: 0.45rem !important;
    flex-shrink: 0;
}

.reply-author {
    font-weight: 600;
    color: var(--text-white);
    flex-shrink: 0;
}

.reply-text {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/*
 * Connector: curves from reply-preview down to the main avatar.
 *        ╭── [16px] @name  text
 *        │
 * [40px]  username  time
 *
 * Drawn on message-group (not a child) so it can't be clipped.
 * Avatar z-index sits above the line so it appears to go behind the circle.
 */
.message-group.has-reply::before {
    content: '';
    position: absolute;
    left: calc(1rem + 19px); /* avatar center, minus 1px to center 2px border */
    top: calc(0.25rem + 7px); /* vertical center of 16px reply avatar */
    width: calc(20px + 0.75rem); /* half avatar + most of gap */
    height: calc(0.25rem + 32px); /* down to main avatar center */
    border-left: 2px solid var(--bg-muted);
    border-top: 2px solid var(--bg-muted);
    border-top-left-radius: 6px;
    z-index: 1;
}

.message-group.has-reply .avatar {
    position: relative;
    z-index: 2;
}

/* Highlight animation for scroll-to-message */
.message-group.highlight-message {
    background: color-mix(in srgb, var(--accent-primary) 12%, transparent);
    transition: background-color 1.5s ease;
}

/* Highlight the source message while composing a reply */
.message-group.reply-target {
    background: var(--accent-tint);
}

.message-group:first-child {
    margin-top: 0;
}

/* Message body - contains meta and content */
.message-body {
    flex: 1;
    min-width: 0;
}

/* Message meta (username + time) */
.message-meta {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
    margin-bottom: 0.125rem;
}

.message-username {
    color: var(--text-white);
    font-size: 1rem;
    font-weight: 500;
    /* A flex item never shrinks below its longest word, and a display name can be 64 characters with no space.
       Beside "Yesterday 14:32" such a name pushed the header past the row on a phone, and the timeline became a
       sideways scroller. The name is clipped at the row edge: not wrapped, and no ellipsis, because the user
       would rather see three more letters of the name than three dots. */
    min-width: 0;
    overflow: hidden;
    white-space: nowrap;
}

/* The robot of a bot author (the colour is with "Role badges" in frame.css). The header row aligns its items on
   the text baseline, and an icon has none: it would stand on the baseline, above the capitals. Centred, it sits
   at the middle of the name. */
.message-group .bot-badge {
    align-self: center;
}

/* Clickable action links in bot messages (e.g., PWA install) */
.bot-action-link {
    color: var(--accent-primary);
    text-decoration: underline;
    cursor: pointer;
    font-weight: 500;
}

.bot-action-link:hover {
    color: var(--accent-hover);
}

.message-time {
    color: var(--text-muted);
    font-size: 0.75rem;
    font-weight: 400;
    /* The time keeps its full width on one line; the name is the part that gives way (see .message-username). */
    flex-shrink: 0;
    white-space: nowrap;
}

/* Message content */
.message-content {
    color: var(--text-primary);
    font-size: 1rem;
    line-height: 1.375;
    padding-left: 0;
    word-break: break-word;
    overflow-wrap: break-word;
    white-space: pre-wrap; /* Preserve newlines from multiline input */
    margin: 0;
    max-width: 100%;
    overflow-x: hidden;
}

.message-content a.message-link,
.pin-text a.message-link {   /* a pin's text (frame.css) shows its links as the row does */
    color: var(--accent-focus);
    text-decoration: none;
}

.message-content a.message-link:hover,
.pin-text a.message-link:hover {
    text-decoration: underline;
}

/* MR-86: a link to a message of this server, after the server said where the message is (js/message-links.js).
   The room, the author and the time stand in the place of the address. These rules come after the link rules
   above and have the same weight, so they win.
   Inline, not inline-flex: a long room name and a long display name must wrap inside a narrow row, and a box
   that cannot break would push the row wider than a phone. */
.message-content a.message-link-label {
    padding: 0 0.375em;
    border-radius: 4px;
    background: var(--accent-tint);
    color: var(--text-white);
    font-weight: 500;
    /* each line of a wrapped label keeps its own padding and corners */
    -webkit-box-decoration-break: clone;
    box-decoration-break: clone;
}

.message-content a.message-link-label:hover {
    text-decoration: none;
    background: color-mix(in srgb, var(--accent-primary) 30%, transparent);
}

.message-content a.message-link-label svg {
    margin-right: 0.3em;
    vertical-align: -0.125em;   /* the middle of the icon on the middle of the small letters */
}

/* System messages */
.system-message {
    display: block;
    text-align: center;
    margin: 0 !important;
    padding: 0.0625rem 1rem !important;
}

.system-message .message-body {
    display: inline-block;
}

.system-message .message-content {
    display: inline-block;
    color: var(--text-muted);
    font-style: italic;
    font-size: 0.8125rem;
    padding: 0;
    background: transparent;
    line-height: 1.4;
}

/* Image gallery styles */
/* Discord-style: rows of max 3, each row fills same width */
.image-gallery {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-top: 0.25rem;
    border-radius: 8px;
    overflow: hidden;
    max-width: 400px; /* Consistent width for all image content */
}

/* Each row of images */
.gallery-row {
    display: flex;
    gap: 4px;
}

/* Items in a row stretch equally */
.gallery-row .gallery-item {
    flex: 1 1 0%;
    min-width: 0;
}

.gallery-item {
    position: relative;
    cursor: pointer;
}

/* iOS: no save menu on a long press of the picture (MR-53); message.js blocks the contextmenu event for the other
   browsers. The long press was meant for the row's bar, and the viewer's Download control replaces the menu. */
.gallery-image {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

/* Multi-image: square thumbnails that fill their container */
.gallery-row .gallery-image {
    width: 100%;
    height: auto; /* Override HTML height attribute */
    aspect-ratio: 1;
    object-fit: cover;
    border-radius: 4px;
    transition: opacity 0.15s;
    display: block;
}

/* Single image: preserve aspect ratio, constrain size */
/* Discord uses ~350px height for vertical images, ~400px width for horizontal */
.gallery-single .gallery-image {
    height: auto;
    width: auto;
    max-height: 350px;
    max-width: 100%;
    aspect-ratio: auto 4 / 3; /* Fallback ratio until image loads, then use intrinsic */
    object-fit: contain;
    background: var(--bg-sidebar);
    border-radius: 4px;
    transition: opacity 0.15s;
    display: block;
}

/* The box before the picture loads (MR-24): the server sends the medium picture's size, and --w/--h give the box
   its final size at once, so nothing moves when the file arrives. The width is the smallest of the gallery, the
   picture itself and the width at the 350 px cap; the ratio sets the height, so the box equals the loaded picture.
   Without a size (a row stored before the size existed, a file that is gone) the rule above applies and the box
   takes shape on load: the picture is 0 px tall until then, and a row at the top edge of the list grows under the
   scroll anchor and moves every row below it. */
.gallery-single .gallery-image.sized {
    aspect-ratio: var(--w) / var(--h);
    width: min(100%, calc(var(--w) * 1px), calc(350px * var(--w) / var(--h)));
}

.gallery-item:hover .gallery-image {
    opacity: 0.85;
}

/* GIF message styles */
.gif-message-gallery {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-top: 0.25rem;
}

.gif-message {
    position: relative;
    border-radius: 8px;
    overflow: hidden;
    background: var(--bg-sidebar);
}

.gif-message video,
.gif-message img {
    display: block;
    width: 100%;
    height: 100%;
    /* contain (not cover) so a GIF never gets cropped if the reserved box ratio is ever off
       (e.g. the rare upload we still couldn't measure) — it letterboxes against the bg instead. */
    object-fit: contain;
}

/* Loading spinner: the .gif-message box reserves space via its inline aspect-ratio and shows
   this spinner over the placeholder background until the media paints. message.js adds .gif-loaded
   on the <img> load / <video> canplay event (and on error, so a broken URL never spins forever).
   The fade-in is delayed so fast/cached GIFs don't flash a spinner. */
.gif-message::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 28px;
    height: 28px;
    margin: -14px 0 0 -14px;
    border: 3px solid var(--bg-muted);
    border-top-color: var(--text-secondary);
    border-radius: 50%;
    pointer-events: none;
    opacity: 0;
    animation: spin 0.7s linear infinite, gif-spinner-in 0.01s linear 0.25s forwards;
}

.gif-message.gif-loaded::after,
.gif-message:has(.gif-message-missing)::after {
    display: none;
}

@keyframes gif-spinner-in {
    to { opacity: 1; }
}

.gif-message-missing {
    padding: 1rem;
    text-align: center;
    color: var(--text-placeholder);
    font-size: 0.875rem;
}

.gif-message-fav {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 26px;
    height: 26px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.5);
    border: none;
    border-radius: 50%;
    color: white;
    opacity: 0;
    cursor: pointer;
    transition: opacity 0.15s, background-color 0.15s;
    padding: 0;
}

.gif-message:hover .gif-message-fav {
    opacity: 1;
}

.gif-message-fav.favorited {
    opacity: 1;
    color: #ffc107;
}

.gif-message-fav:hover {
    background: rgba(0, 0, 0, 0.75);
}

.gif-message-uploader {
    position: absolute;
    bottom: 4px;
    left: 6px;
    font-size: 0.65rem;
    color: white;
    background: rgba(0, 0, 0, 0.55);
    padding: 2px 6px;
    border-radius: 3px;
    opacity: 0;
    transition: opacity 0.15s;
    pointer-events: none;
}

.gif-message:hover .gif-message-uploader {
    opacity: 1;
}

/* Video gallery styles */
.video-gallery {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-top: 0.25rem;
    max-width: 400px;
}

/* A voice message (MR-76): play, the bars, the time, the speed. A fixed height, so the list never moves when a
   row's file loads. The bars are one canvas; voice.js reads the two colour variables when it draws. */
.voice-message {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 320px;
    max-width: 100%;
    margin-top: 0.25rem;
    padding: 6px 12px 6px 6px;
    border-radius: 24px;
    background: var(--bg-sidebar);
}

.voice-play {
    flex: 0 0 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: var(--accent-primary);
    color: var(--accent-ink);
    cursor: pointer;
}

.voice-play:hover {
    background: var(--accent-hover);
}

/* One of the two icons shows: play, and pause while the message plays. */
.voice-play .play-icon,
.voice-message.playing .voice-play .pause-icon {
    display: flex;
}

.voice-play .pause-icon,
.voice-message.playing .voice-play .play-icon {
    display: none;
}

.voice-bars {
    flex: 1 1 0%;
    min-width: 0;
    height: 32px;
    cursor: pointer;
    --voice-bar: var(--text-placeholder);
    --voice-bar-played: var(--accent-primary);
}

.voice-time {
    flex: 0 0 auto;
    min-width: 2.6em;
    font-size: 0.8125rem;
    font-variant-numeric: tabular-nums;
    color: var(--text-secondary);
    text-align: right;
}

.voice-speed {
    flex: 0 0 auto;
    padding: 2px 7px;
    border: none;
    border-radius: 10px;
    background: var(--bg-muted);
    color: var(--text-secondary);
    font-size: 0.75rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
}

.voice-speed:hover {
    color: var(--text-primary);
}

.video-container {
    position: relative;
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
}

.video-player {
    width: 100%;
    height: auto;
    max-height: 350px;
    display: block;
    object-fit: contain;
    background: var(--bg-sidebar);
}

/* The poster's shape before it loads (MR-27): with --w/--h the box is final at once. Without them the player is
   300 x 150 until the poster arrives, and the row grows. */
.video-player.sized {
    aspect-ratio: var(--w) / var(--h);
}

.video-play-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0,0,0,0.3);
    transition: background 0.2s;
}

.video-play-overlay:hover {
    background: rgba(0,0,0,0.15);
}

/* Scoped to the overlay: the voice button (voice.js) also has a .play-icon, and this 64 px box pushed its triangle
   out of the 36 px circle. */
.video-play-overlay .play-icon {
    width: 64px;
    height: 64px;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.5));
}

/* Message actions popup - hidden by default, shown via CSS :hover */
.message-actions {
    position: absolute;
    top: -16px;
    right: 16px;
    display: flex;
    gap: 2px;
    background: var(--bg-sidebar);
    border-radius: 4px;
    padding: 2px;
    box-shadow: 0 0 0 1px rgba(4,4,5,0.15), 0 4px 4px rgba(0,0,0,0.1);
    z-index: 100;
    /* Hidden by default, smooth transition */
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.1s ease;
}

/* Show on hover (where a pointer can hover, see the tint rule above) or when picker/menu is open - pure CSS */
@media (hover: hover) {
    .message-group:hover:not(.system-message) .message-actions {
        opacity: 1;
        pointer-events: auto;
    }
}
.message-group.picker-open .message-actions,
.message-group.menu-open .message-actions {
    opacity: 1;
    pointer-events: auto;
}

/* Hide actions while the pointer is over the reply reference (sibling combinator),
   so the reference stays readable/clickable under where the bar overlaps it. */
.reply-preview:hover ~ .message-actions {
    opacity: 0 !important;
}

/* Touch only: also make the hidden bar click-through, so sticky tap-:hover on the
   reply-preview can't leave an invisible bar stealing the tap meant for it.
   Deliberately NOT applied for mouse input: with hit-testing disabled, crossing the
   ~1px strip of reply-preview that peeks below the bar latched it hidden — the cursor
   then hovered the preview *through* the ghost bar and the bar never came back,
   making Reply unreachable. With hit-testing kept, the bar re-appears the moment
   the cursor enters its box (the bar, not the preview, becomes the hovered element). */
@media (hover: none), (pointer: coarse) {
    .reply-preview:hover ~ .message-actions {
        pointer-events: none !important;
    }
}

.action-btn {
    background: transparent;
    border: none;
    padding: 6px 8px;
    cursor: pointer;
    border-radius: 3px;
    font-size: 1rem;
    line-height: 1;
    transition: background-color 0.1s, transform 0.08s;
}

.action-btn:active {
    transform: scale(0.9); /* instant press feedback — the toggle itself takes a round trip */
}

.action-btn:hover {
    background: var(--bg-primary);
}

/* Divider between reactions and actions */
.action-divider {
    width: 1px;
    height: 24px;
    background: var(--bg-muted);
    margin: 0 4px;
    align-self: center;
}

/* Quick reaction and grey icon buttons (picker, edit, more, reply) */
.quick-reaction,
.action-picker,
.action-edit,
.action-more,
.action-reply {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
}

.action-picker:hover,
.action-edit:hover,
.action-more:hover,
.action-reply:hover {
    background: var(--bg-primary);
    color: var(--text-primary);
}

/* GIF favorite toggle in the bar — mobile-only (revealed in the mobile media query).
   Desktop keeps the hover star on the GIF itself, where there's no bar collision. */
.action-gif-fav {
    display: none;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
}

.action-gif-fav.favorited {
    color: #ffc107;
}

/* Keep hover state when picker or more-menu is open */
.message-group.picker-open,
.message-group.menu-open {
    background: var(--bg-hover);
}

/* Invisible backdrop to catch clicks outside emoji picker */
.message-group .emoji-picker-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 999;
}

/* Fixed-position wrapper for emoji picker (JS-positioned on desktop, CSS bottom sheet on mobile) */
.emoji-picker-wrapper {
    position: fixed;
    z-index: 1000;
}

/* Reactions display */
.reactions-display {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-top: 4px;
}

.reaction-pill {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: var(--bg-sidebar);
    border: 1px solid transparent;
    border-radius: 8px;
    padding: 2px 8px;
    font-size: 0.875rem;
    cursor: pointer;
    transition: background-color 0.1s, border-color 0.1s, transform 0.08s;
}

.reaction-pill:active {
    transform: scale(0.9); /* instant press feedback — the toggle itself takes a round trip */
}

.reaction-pill:hover {
    background: var(--bg-primary);
    border-color: var(--bg-muted);
}

.reaction-pill.reacted {
    background: color-mix(in srgb, var(--accent-primary) 30%, transparent);
    border-color: var(--accent-primary);
}

.reaction-count {
    color: var(--text-secondary);
    font-size: 0.75rem;
}

.reaction-pill.reacted .reaction-count {
    color: var(--accent-light);
}

/* Edit mode */
.edit-container {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* The edit box is deliberately a small message input: the text, line height, min/max height
   and scrolling repeat .message-input (composer.css), so editing feels like composing. Keep the
   two in step. Its own box (background, border, radius, padding) stands in for the composer's
   .input-box, which the row does not have. */
.edit-input {
    width: 100%;
    padding: 0.6875rem 1rem;
    background: var(--bg-input);
    border: 1px solid transparent;
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 1rem;
    font-family: inherit;
    line-height: 1.375;
    resize: none;
    min-height: 44px;
    max-height: 200px;
    overflow-y: auto;
    word-break: break-word;
    overflow-wrap: break-word;
    scrollbar-width: none;
}

.edit-input::-webkit-scrollbar {
    display: none;
}

.edit-input:focus {
    outline: none;
    border-color: var(--accent-primary);
}

.edit-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.edit-save,
.edit-cancel {
    padding: 6px 14px;
    min-height: 36px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.875rem;
    transition: background-color 0.1s;
}

/* Save is the primary action — accent like the send button, themed by every theme
   (green stays reserved for status semantics, which themes never override). */
.edit-save {
    background: var(--accent-primary);
    color: var(--accent-ink);
}

.edit-save:hover {
    background: var(--accent-hover);
}

.edit-cancel {
    background: var(--bg-muted);
    color: var(--text-primary);
}

.edit-cancel:hover {
    background: var(--bg-hover);
}

.edit-hint {
    color: var(--text-placeholder);
    font-size: 0.75rem;
}

/* Touch: no hardware keys — hide the hint, grow the tap targets */
@media (hover: none), (pointer: coarse) {
    .edit-hint {
        display: none;
    }

    .edit-save,
    .edit-cancel {
        min-height: 40px;
        padding: 8px 16px;
    }
}

/* Edited indicator */
.edited-indicator {
    color: var(--text-placeholder);
    font-size: 0.625rem;
    margin-left: 4px;
}

/* ⋯ More overflow menu (copy / delete) */
.more-menu-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 999;
}

.more-menu {
    position: fixed;
    z-index: 1000;
    min-width: 190px;
    padding: 6px;
    background: var(--bg-sidebar);
    border-radius: 8px;
    box-shadow: 0 0 0 1px rgba(4,4,5,0.15), 0 8px 16px rgba(0,0,0,0.24);
    display: flex;
    flex-direction: column;
    gap: 2px;
    /* Revealed by overlays.place after placement (desktop);
       the mobile bottom-sheet override makes it visible directly */
    visibility: hidden;
}

.more-menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 8px 10px;
    background: transparent;
    border: none;
    border-radius: 4px;
    color: var(--text-primary);
    font-size: 0.9375rem;
    font-family: inherit;
    text-align: left;
    cursor: pointer;
    transition: background-color 0.1s, color 0.1s;
}

.more-menu-item:hover {
    background: var(--bg-primary);
}

.more-menu-item svg {
    flex-shrink: 0;
}

/* Delete stays visibly dangerous: red at rest, filled red on hover */
.menu-item-danger {
    color: var(--color-danger);
}

.menu-item-danger:hover {
    background: var(--color-danger);
    color: white;
}

/* Delete confirmation modal */
.delete-confirm-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.65);
    z-index: 1100; /* above pickers and the more-menu (999-1000) */
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.delete-confirm-dialog {
    background: var(--bg-primary);
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
    width: min(440px, 100%);
    max-height: calc(100dvh - 2rem);   /* a long list of names (the inactive sweep, AD-39) scrolls inside; the buttons stay in reach */
    overflow-y: auto;
    padding: 1.25rem;
}

.delete-confirm-title {
    margin: 0 0 0.5rem;
    font-size: 1.125rem;
    color: var(--text-white);
}

/* The admin's red question (AD-38): the Danger Zone's frame and colour on the dialog. The text keeps its line
   breaks, so the names stand on a line of their own. */
.delete-confirm-dialog.danger {
    border: 1px solid var(--color-danger);
}

.delete-confirm-dialog.danger .delete-confirm-title {
    color: var(--color-danger);
}

.delete-confirm-dialog.danger .delete-confirm-text {
    white-space: pre-line;
}

.delete-confirm-text {
    margin: 0 0 0.75rem;
    color: var(--text-secondary);
    font-size: 0.9375rem;
}

/* Mini preview of the message being deleted */
.delete-confirm-preview {
    background: var(--bg-sidebar);
    border-radius: 6px;
    padding: 10px 12px;
    box-shadow: 0 0 0 1px rgba(4,4,5,0.15);
}

.preview-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
}

.preview-username {
    color: var(--text-white);
    font-size: 0.9375rem;
}

.preview-time {
    color: var(--text-muted);
    font-size: 0.75rem;
}

.preview-content {
    color: var(--text-primary);
    font-size: 0.9375rem;
    word-break: break-word;
    overflow-wrap: break-word;
    white-space: pre-wrap;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.preview-thumb {
    display: block;
    margin-top: 6px;
    max-width: 120px;
    max-height: 80px;
    border-radius: 4px;
    object-fit: cover;
}

.preview-attachment-label {
    margin-top: 4px;
    color: var(--text-muted);
    font-size: 0.8125rem;
    font-style: italic;
}

.delete-confirm-tip {
    margin-top: 0.75rem;
    color: var(--text-muted);
    font-size: 0.75rem;
}

.delete-confirm-tip kbd {
    padding: 1px 5px;
    border-radius: 3px;
    background: var(--bg-sidebar);
    box-shadow: 0 0 0 1px rgba(4,4,5,0.3);
    font-family: inherit;
    font-size: 0.6875rem;
}

.delete-confirm-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 1rem;
}

.confirm-cancel,
.confirm-delete,
.confirm-ok {
    padding: 8px 18px;
    border: none;
    border-radius: 4px;
    font-size: 0.875rem;
    font-family: inherit;
    cursor: pointer;
    transition: background-color 0.1s, filter 0.1s;
}

.confirm-cancel {
    background: transparent;
    color: var(--text-primary);
}

.confirm-cancel:hover {
    text-decoration: underline;
}

.confirm-delete {
    background: var(--color-danger);
    color: white;
}

.confirm-delete:hover {
    filter: brightness(0.9);
}

/* The one button of a notice: the accent, not the danger red */
.confirm-ok {
    background: var(--accent-primary);
    color: var(--accent-ink);
}

.confirm-ok:hover {
    filter: brightness(0.9);
}

/* Mobile adjustments */
@media (max-width: 600px) {
    .message-actions {
        right: 8px;
        top: -12px;
    }

    /* Larger touch targets — small icons + tight padding caused fat-finger mistaps */
    .action-btn {
        padding: 8px 9px;
        font-size: 1rem;
    }

    /* GIF favoriting moves into the actions bar (the bar covers the GIF's corner
       on narrow screens). The corner star stays as a favorited-only indicator:
       inert so a tap near it can't race the bar, and never revealed while empty. */
    .action-gif-fav {
        display: flex;
    }

    .gif-message-fav {
        pointer-events: none;
    }

    .gif-message:hover .gif-message-fav:not(.favorited) {
        opacity: 0;
    }

    .message-group {
        padding: 0.125rem 0.5rem 0.125rem calc(0.5rem + 40px + 0.75rem);
        gap: 0.75rem;
    }

    .message-group.has-header {
        padding-left: 0.5rem;
    }

    /* The hover time (MR-81) in the narrower avatar column: 0.5rem + 40px + 0.75rem, less the same 0.5rem. Only
       the width is here: the rule that makes the box is behind (hover: hover), so a phone still draws nothing. */
    .message-group:not(.has-header, .system-message):hover::before {
        width: calc(40px + 0.75rem);
    }

    .reply-preview {
        padding-left: calc(40px + 0.75rem);
    }

    .message-group.has-reply::before {
        left: calc(0.5rem + 19px);
        width: calc(20px + 0.5rem);
    }

    .reply-text {
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        white-space: normal;
    }

    /* Mobile: full-width galleries with taller single images/videos */
    .image-gallery,
    .video-gallery {
        max-width: 100%;
    }

    .voice-message {
        width: 100%;
    }

    /* A full-width player reaches under the bar's usual place, which hid the speed button, the time and the
       end of the bars (MR-52). A voice row's bar sits above the row instead. Only here: a 320 px player on a
       wider screen never meets the bar. */
    .message-group:has(.voice-message) .message-actions {
        top: auto;
        bottom: 100%;
    }

    .gallery-single .gallery-image {
        max-height: 450px;
    }

    .gallery-single .gallery-image.sized {
        width: min(100%, calc(var(--w) * 1px), calc(450px * var(--w) / var(--h)));   /* the same box at the phone's 450 px cap */
    }

    /* Mobile: bottom sheet emoji picker */
    .emoji-picker-wrapper {
        bottom: 0;
        left: 0;
        right: 0;
        top: auto;
        transform: none;
    }

    .emoji-picker-wrapper .emoji-picker {
        width: 100%;
        height: 50vh;
        max-height: 400px;
        border-radius: 12px 12px 0 0;
    }

    .message-group .emoji-picker-backdrop {
        background: rgba(0, 0, 0, 0.5);
    }

    /* More menu becomes a bottom sheet (JS positioning skips mobile) */
    .more-menu {
        visibility: visible;
        top: auto;
        left: 0;
        right: 0;
        bottom: 0;
        min-width: 0;
        border-radius: 12px 12px 0 0;
        padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
    }

    .more-menu-item {
        padding: 14px 12px;
        font-size: 1rem;
    }

    .more-menu-backdrop {
        background: rgba(0, 0, 0, 0.5);
    }

    /* Confirm modal: stacked full-width buttons, easy thumb reach */
    .delete-confirm-tip {
        display: none; /* no Shift key on phones */
    }

    .delete-confirm-actions {
        flex-direction: column-reverse;
    }

    .confirm-cancel,
    .confirm-delete {
        width: 100%;
        padding: 12px;
        font-size: 1rem;
    }

    .confirm-cancel {
        background: var(--bg-muted);
    }

    .confirm-cancel:hover {
        text-decoration: none;
    }
}
