/* thread-history */
/* Older messages fold behind an avatar-aligned count without loading their bodies. */
.mail .thread .thread-history {
    position: relative; display: flex; align-items: center; height: 48px; flex-shrink: 0;
}
.mail .thread .thread-history::before, .mail .thread .thread-history::after {
    content: ''; position: absolute; left: 20px; right: 0; top: calc(50% - 2px); height: 1px; background: #e8eaed;
}
.mail .thread .thread-history::after { top: calc(50% + 2px); }
.mail .thread .thread-history button {
    position: relative; z-index: 1; display: grid; place-items: center; width: 40px; height: 40px;
    border: 1px solid #dadce0; border-radius: 50%; background: #fff; color: #5f6368; font: inherit;
    font-size: calc(12px * var(--text-scale, 1)); cursor: pointer;
}
.mail .thread .thread-history button:hover { background: #f1f3f4; }
.mail .thread .thread-history button:focus-visible { outline: 2px solid #0b57d0; outline-offset: 2px; }
/* Collapsed previews retain only their star action and hide files whose bodies were opened earlier. */
.mail .thread .thread-stack .message.collapsed .message-head .actions :is(.reply, .options-toggle),
.mail .thread .thread-stack .message.collapsed > .message-attachments { display: none; }
@media (width >= 800px) {
    .mail .thread .thread-stack .message.collapsed .message-head { height: 20px; }
}
@media (width < 800px) {
    .mail .thread .thread-history button { width: 44px; height: 44px; }
    .mail .thread .thread-history { padding-left: 0; }
    :root:not([data-theme='light']) .mail .thread .thread-history button {
        background: var(--gmail-surface); color: var(--gmail-muted); border-color: var(--gmail-line);
    }
    :root:not([data-theme='light']) .mail .thread .thread-history::before,
    :root:not([data-theme='light']) .mail .thread .thread-history::after { background: var(--gmail-line); }
    :root:not([data-theme='light']) .mail .thread .thread-history button:hover { background: var(--navigation-hover); }
}

/* email */
/* thread view container */
.mail {
    position: absolute;
    inset: 0;
    opacity: 0;
    pointer-events: none;
    background-color: var(--gmail-surface);
    transition: opacity 150ms ease-out;
    &.visible { opacity: 1; pointer-events: auto; }
}

/* toolbar: back, archive, spam, trash, unread */
.mail .thread-header {
    display: flex;
    gap: 4px;
    padding: 3px 8px;
    border-bottom: 1px solid #3c3c3c;
    .thread-action {
        display: grid;
        place-items: center;
        width: 40px;
        height: 40px;
        border: none;
        border-radius: 50%;
        background: none;
        color: #c4c7c5;
        cursor: pointer;
        &.back { margin-right: 10px; }
        &:hover { background-color: #3c3c3c; }
        svg { width: 14px; height: 14px; fill: #c0c0c0; }
    }
}

/* scrollable email stack */
.mail .thread {
    height: fit-content;
    min-height: min(550px, calc(100% - 47px));
    max-height: calc(100% - 47px);
    margin-right: 16px;
    padding: 20px 16px;
    overflow-y: auto;
    background-color: #ffffff;
    .thread-heading {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        gap: 8px;
        margin-left: 56px;
    }
    .thread-subject {
        min-width: 0;
        font-size: calc(22px * var(--text-scale, 1));
        font-weight: 400;
        color: #1f1f1f;
        overflow-wrap: anywhere;
    }
}

/* reply / forward bar */
.mail .thread .thread-footer {
    &[hidden] { display: none; }
    display: flex;
    gap: 8px;
    padding: 16px 0 16px 56px;
    font-family: var(--display-font, "Google Sans", Roboto, RobotoDraft, Helvetica, Arial, sans-serif);
    font-size: calc(14px * var(--text-scale, 1));
    font-weight: 500;
    line-height: 20px;
    color: #444746;
    .thread-reply,
    .thread-forward {
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 6px 16px;
        border: 1px solid #5f6368;
        border-radius: 18px;
        background: none;
        color: #444746;
        cursor: pointer;
        &:hover { background-color: #f2f2f2; }
        &.emoji { padding: 8px; }
        svg { width: 14px; height: 14px; fill: #444746; }
    }
}

/* single email within a thread */
.mail .thread .thread-stack .message {
    display: grid;
    grid-template-areas: "avatar header" "avatar body";
    grid-template-columns: auto 1fr;
    position: relative;
    padding: 16px 0;
    border-bottom: 1px solid #64798f1f;
    .message-body {
        grid-area: body;
        min-width: 0;
        .message-summary { display: none; }
        .message-content {
            &.plain-text { white-space: pre-wrap; overflow-wrap: anywhere; }
            margin-top: 6px;
            font-family: var(--display-font, Arial, Helvetica, sans-serif);
            font-size: calc(14px * var(--text-scale, 1));
            line-height: 1.5;
            color: #222222;
        }
    }
    &:last-child { border: none; }
    &.collapsed {
        cursor: pointer;
        .email,
        .recipient,
        .message-content,
        .actions .reply,
        .actions .options-toggle { display: none; }
        .message-summary {
            display: block;
            font-size: calc(13px * var(--text-scale, 1));
            color: #555555;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
    }
    .avatar {
        grid-area: avatar;
        flex-shrink: 0;
        min-width: 40px;
        width: 40px;
        height: 40px;
        margin-right: 16px;
        border-radius: 50%;
        object-fit: cover;
    }
    .options {
        position: fixed;
        inset: auto;
        margin: 0;
        border: none;
        display: none;
        flex-direction: column;
        gap: 11px;
        width: 200px;
        max-width: calc(100vw - 16px);
        max-height: calc(100dvh - 16px);
        overflow-y: auto;
        padding: 4px 0;
        border-radius: 4px;
        background: #ffffff;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.15);
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
        transform: translateY(-4px);
        transition: opacity 100ms ease-out, transform 100ms ease-out, visibility 100ms;
        &:popover-open { display: flex; }
        &:not(.hidden) {
            opacity: 1;
            visibility: visible;
            pointer-events: auto;
            transform: translateY(0);
        }
        button {
            display: flex;
            align-items: center;
            gap: 16px;
            width: 100%;
            padding: 0 24px;
            border: none;
            background: none;
            font-family: var(--display-font, "Google Sans", Roboto, sans-serif);
            font-size: calc(14px * var(--text-scale, 1));
            color: #202124;
            cursor: pointer;
            &:hover { background-color: #f2f2f2; }
            &:focus-visible { outline: 2px solid #1a73e8; outline-offset: -2px; }
            svg { flex-shrink: 0; width: 14px; height: 14px; fill: #444746; }
        }
    }
    .message-head {
        grid-area: header;
        display: flex;
        column-gap: 8px;
        align-items: flex-start;
        font-family: var(--display-font, "Google Sans", Roboto, RobotoDraft, Helvetica, Arial, sans-serif);
        .contacts {
            display: flex;
            flex-direction: column;
            flex: 1;
            font-size: calc(14px * var(--text-scale, 1));
            .sender {
                display: flex;
                gap: 6px;
                .name { font-weight: 700; color: #1f1f1f; }
                .email { font-size: calc(12px * var(--text-scale, 1)); color: #5e5e5e; }
            }
            .recipient { font-size: calc(12px * var(--text-scale, 1)); color: #5e5e5e; }
        }
        .timestamp {
            font-size: calc(12px * var(--text-scale, 1));
            line-height: 20px;
            color: #5e5e5e;
            white-space: nowrap;
        }
        .actions {
            display: flex;
            transform: translateY(-10px);
            .icon {
                display: flex;
                align-items: center;
                justify-content: center;
                width: 40px;
                height: 40px;
                border: none;
                background: none;
                cursor: pointer;
                &:hover { border-radius: 50%; background-color: #f2f2f2; }
                svg { width: 16px; height: 16px; fill: #4a4a4a; }
                &.star {
                    svg { width: 18px; height: 18px; }
                    svg.inactive { fill: #333333; }
                    svg.active { display: none; }
                    &.starred {
                        svg.inactive { display: none; }
                        svg.active { display: block; fill: gold; }
                    }
                }
            }
        }
    }
}

.mail .thread .reply-form {
    margin: 16px 0 16px 56px;
    border: 1px solid #dadce0;
    border-radius: 8px;
    color: #202124;
    font-size: calc(14px * var(--text-scale, 1));
    &:focus-within { border-color: #8ab4f8; }
    .message-sender { margin-inline: 16px; }
    .reply-recipient {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 12px 16px;
        border-bottom: 1px solid #dadce0;
        color: #5e5e5e;
        overflow-wrap: anywhere;
        svg { flex-shrink: 0; width: 16px; height: 16px; fill: #444746; }
    }
}

.storage-notice {
    padding: 12px 16px;
    color: #e8eaed;
    font-size: calc(14px * var(--text-scale, 1));
}

/* A locally saved message stays visible until the server acknowledges it. */
.message-sync {
    display: inline-flex; align-items: center; justify-content: center; vertical-align: middle;
    flex: 0 0 24px; width: 24px; height: 24px; padding: 3px; margin-inline: 2px;
    border: 0; border-radius: 50%; background: transparent; color: #a66700; cursor: pointer;
}
.message-sync svg { width: 18px; height: 18px; fill: currentColor; stroke: none; }
.message-sync:disabled { cursor: progress; opacity: 0.7; }
.message-sync:focus-visible { outline: 2px solid currentColor; outline-offset: 2px; }
:root:not([data-theme='light']) .message-sync { color: #ffca68; }
.email .title-summary .message-sync { display: inline-flex; }

[data-sync-status]:empty, [data-thread-sync-status]:empty { display: none; }
