/* r107-r111: picture albums (r107-r112: and mixed albums with videos) laid out by qchat-album-justify.js (Telegram-like
   justified rows). The script writes pixel geometry into --qchat-justify-*
   variables; these rules place the tiles. The selectors repeat #appSection so
   they win over the older album grids, which use up to five ids. */

html body #appSection#appSection#appSection#appSection#appSection#appSection#appSection#appSection [data-qchat-justified] {
    display: block !important;
    position: relative !important;
    width: var(--qchat-justify-w) !important;
    min-width: 0 !important;
    max-width: 100% !important;
    height: var(--qchat-justify-h) !important;
    min-height: 0 !important;
    max-height: none !important;
    aspect-ratio: auto !important;
    grid-template-columns: none !important;
    grid-template-rows: none !important;
    padding: 0 !important;
    margin-left: 0 !important;
    margin-right: 0 !important;
    overflow: hidden !important;
    border-radius: 12px !important;
    box-sizing: border-box !important;
}

html body #appSection#appSection#appSection#appSection#appSection#appSection#appSection#appSection [data-qchat-justified] > [data-qchat-justify-item] {
    display: block !important;
    position: absolute !important;
    left: var(--qchat-justify-x) !important;
    top: var(--qchat-justify-y) !important;
    width: var(--qchat-justify-iw) !important;
    height: var(--qchat-justify-ih) !important;
    min-width: 0 !important;
    max-width: none !important;
    min-height: 0 !important;
    max-height: none !important;
    aspect-ratio: auto !important;
    grid-area: auto !important;
    margin: 0 !important;
    padding: 0 !important;
    transform: none !important;
    overflow: hidden !important;
    border-radius: 0 !important;
    box-sizing: border-box !important;
}

html body #appSection#appSection#appSection#appSection#appSection#appSection#appSection#appSection [data-qchat-justified] > [data-qchat-justify-item] :is(.profile-blog-gallery-tile, .profile-blog-attachment-preview, .file-preview, .qchat-chat-image-tile, .qchat-file-bundle-image-tile, .qchat-file-bundle-video-tile) {
    display: block !important;
    position: relative !important;
    width: 100% !important;
    height: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
    min-height: 0 !important;
    max-height: none !important;
    aspect-ratio: auto !important;
    margin: 0 !important;
    padding: 0 !important;
    border-radius: 0 !important;
    box-sizing: border-box !important;
}

html body #appSection#appSection#appSection#appSection#appSection#appSection#appSection#appSection [data-qchat-justified] > [data-qchat-justify-item] :is(img.chat-image-preview, video.qchat-file-bundle-video-thumb, img.qchat-video-poster) {
    display: block !important;
    width: 100% !important;
    height: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
    min-height: 0 !important;
    max-height: none !important;
    aspect-ratio: auto !important;
    object-fit: cover !important;
    object-position: center center !important;
    margin: 0 !important;
    border-radius: 0 !important;
}

html body #appSection#appSection#appSection#appSection#appSection#appSection#appSection#appSection [data-qchat-justify-lane]:not([data-qchat-justified]),
html body #appSection#appSection#appSection#appSection#appSection#appSection#appSection#appSection [data-qchat-justify-lane] :is(.profile-blog-file-bundle-message, .profile-blog-file-bundle-groups, .profile-blog-gallery-group, .qchat-file-bundle-groups, .qchat-file-bundle-group-image) {
    width: var(--qchat-justify-w) !important;
    min-width: 0 !important;
    max-width: 100% !important;
}

/* r107-r113: a video tile with its first-frame poster (video on demand). Only
   the size is set: the button keeps its own centring of the play icon. */
html body #appSection#appSection#appSection#appSection#appSection#appSection#appSection#appSection [data-qchat-justified] > [data-qchat-justify-item] :is(.qchat-deferred-video, .qchat-deferred-video-surface) {
    width: 100% !important;
    height: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
    min-height: 0 !important;
    max-height: none !important;
    aspect-ratio: auto !important;
    margin: 0 !important;
    border-radius: 0 !important;
    box-sizing: border-box !important;
}

/* r107-r113: the play mark fits a narrow tile (portrait videos on a phone). */
html body #appSection#appSection#appSection#appSection#appSection#appSection#appSection#appSection [data-qchat-justified] > [data-qchat-justify-item] .qchat-deferred-video .qchat-deferred-video-play {
    width: min(44px, calc(var(--qchat-justify-iw) * 0.6)) !important;
    height: min(44px, calc(var(--qchat-justify-iw) * 0.6)) !important;
    font-size: min(20px, calc(var(--qchat-justify-iw) * 0.27)) !important;
}

/* Chat photo cards share one inset. Older mobile rules add their own text
   gutters, so clear those on the wrappers inside these media bubbles. */
html body :is(#messagesContainer#messagesContainer#messagesContainer#messagesContainer#messagesContainer, #qcgMessages#qcgMessages#qcgMessages#qcgMessages#qcgMessages) > .message:not(.qh-3, .qh-10) .message-bubble:has(:is(.qchat-chat-blog-image-host, .qchat-file-bundle-grid-images, .qchat-chat-image-tile)):not(:has(.qchat-sticker, .qchat-chat-blog-single-image-host img[data-download-name$=".gif" i])) {
    --qchat-mobile-message-inset: 0px;
    box-sizing: border-box !important;
    width: fit-content !important;
    min-width: 0 !important;
    padding: 8px !important;

    & :is(.message-content-wrapper, .message-content, .decrypted-file-content, .qchat-chat-blog-image-host, .qchat-chat-blog-file-bundle-message, .qchat-chat-blog-file-bundle-groups, .qchat-file-bundle-message, .qchat-file-bundle-groups, .qchat-file-bundle-group-image, .qchat-chat-blog-gallery-group, .qchat-chat-blog-gallery, .qchat-file-bundle-grid-images) {
        margin: 0 !important;
        padding: 0 !important;
    }

    & > .message-content-wrapper > .message-header,
    & :is(.qchat-chat-blog-title, .qchat-file-bundle-title) {
        margin: 0 0 4px !important;
        padding: 0 !important;
    }

    & > .message-content-wrapper > .message-footer {
        margin: 4px 0 0 !important;
        padding: 0 !important;
    }
}

/* Forward headers can make the bubble wider than its photo. Stretch the
   content lane to the bubble before centering the ratio-limited media. */
@media (max-width: 767.98px) {
    html body #messagesContainer#messagesContainer#messagesContainer#messagesContainer#messagesContainer
        > .message:not(.qh-3, .qh-10)
        .message-bubble:has(:is(.forwarded-header, .forwarded-message-header, .forwarded-file-header))
        :is(
            .message-content-wrapper,
            .message-content,
            .decrypted-file-content,
            .forwarded-file-content
        ):has(:is(.qchat-chat-blog-single-image-host, .qchat-chat-image-tile[data-qchat-single-image="1"], .qchat-file-bundle-grid-images[data-file-group-count="1"])) {
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;
        align-self: stretch !important;
        box-sizing: border-box !important;
        padding-inline: 0 !important;
        margin-inline: 0 !important;
    }

    html body #messagesContainer#messagesContainer#messagesContainer#messagesContainer#messagesContainer
        > .message:not(.qh-3, .qh-10)
        .message-bubble:has(:is(.forwarded-header, .forwarded-message-header, .forwarded-file-header))
        :is(
            .qchat-chat-blog-single-image-host,
            .qchat-chat-blog-single-image-host :is(.qchat-chat-blog-file-bundle-message, .qchat-chat-blog-file-bundle-groups, .qchat-chat-blog-gallery-group, .qchat-chat-blog-gallery),
            .qchat-file-bundle-grid-images[data-file-group-count="1"],
            .qchat-chat-image-tile[data-qchat-single-image="1"],
            .qchat-single-image-normalized img.chat-image-preview
        ) {
        margin-left: auto !important;
        margin-right: auto !important;
        justify-self: center !important;
        align-self: center !important;
    }
}

/* The source renderer caps the gallery separately from its enclosing lane.
   Center that gallery in the full-width single-photo group on phones. */
@media (max-width: 767.98px) {
    html body #messagesContainer#messagesContainer#messagesContainer#messagesContainer#messagesContainer
        > .message .qchat-chat-blog-single-image-host[data-qchat-chat-blog-renderer="source"]
        > .qchat-chat-blog-file-bundle-message,
    html body #messagesContainer#messagesContainer#messagesContainer#messagesContainer#messagesContainer
        > .message .qchat-chat-blog-single-image-host[data-qchat-chat-blog-renderer="source"]
        :is(.qchat-chat-blog-file-bundle-groups, .qchat-chat-blog-gallery-group[data-file-group-count="1"]) {
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
        padding-inline: 0 !important;
        margin-inline: 0 !important;
    }

    html body #messagesContainer#messagesContainer#messagesContainer#messagesContainer#messagesContainer
        > .message .qchat-chat-blog-single-image-host[data-qchat-chat-blog-renderer="source"]
        .qchat-chat-blog-gallery-group[data-file-group-count="1"] {
        display: flex !important;
        flex-direction: column !important;
        align-items: center !important;
    }
}
