.slogs-chat {
    --chat-inset: max(1rem, env(safe-area-inset-right));
    --chat-bottom: max(1rem, env(safe-area-inset-bottom));
    position: fixed;
    inset-inline-end: var(--chat-inset);
    bottom: calc(var(--chat-bottom) + var(--chat-viewport-bottom, 0px));
    /* Above the closed header (20), below open menus and the mobile backdrop (35). */
    z-index: 31;
    color: var(--theme-text);
}

.slogs-chat [hidden] { display: none !important; }
.slogs-chat .chat-icon { width: 1.125rem; height: 1.125rem; flex: none; }
.slogs-chat button, .chat-file-button { cursor: pointer; }

.slogs-chat :is(button, a, textarea):focus-visible,
.chat-file-button:focus-within, .chat-messages:focus-visible {
    outline: 2px solid var(--theme-focus);
    outline-offset: 2px;
}

.chat-launcher {
    position: relative;
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: .625rem .875rem;
    min-height: 2.75rem;
    background: var(--theme-surface);
    border: 1px solid var(--theme-border);
    border-radius: .75rem;
    box-shadow: 0 5px 18px rgb(0 0 0 / .13);
    font-size: .875rem;
    font-weight: 700;
}

.chat-launcher:hover, .chat-close:hover, .chat-file-button:hover { background: var(--theme-quiet-hover); }
.chat-launcher .chat-icon { color: var(--theme-accent); }

.chat-launcher-icon { position: relative; display: flex; flex: none; }
.chat-launcher-presence {
    position: absolute;
    inset: -.05rem 0 0 .045rem;
    display: grid;
    place-items: center;
    color: var(--theme-accent);
    font-size: .625rem;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    line-height: 1;
}
.chat-launcher-presence[data-overflow] { transform: translateY(-.15rem); }

.chat-unread {
    position: absolute;
    inset-block-start: -.45rem;
    inset-inline-end: -.4rem;
    display: grid;
    place-items: center;
    min-height: 1.25rem;
    min-width: 1.25rem;
    padding-inline: .3rem;
    border: 2px solid var(--theme-surface);
    border-radius: 999px;
    text-align: center;
    color: white;
    background: #b13546;
    line-height: 1;
    font-size: .75rem;
    font-variant-numeric: tabular-nums;
}

.chat-panel {
    display: flex;
    flex-direction: column;
    width: min(25rem, calc(100vw - var(--chat-inset) - 1rem));
    height: min(34rem, calc(var(--chat-viewport-height, 100dvh) - var(--chat-bottom) - 1rem));
    background: var(--theme-surface);
    border: 1px solid var(--theme-border);
    border-radius: 1rem;
    box-shadow: 0 14px 46px rgb(0 0 0 / .23);
    overflow: hidden;
}

.chat-header { display: flex; align-items: center; justify-content: space-between; padding: .75rem 1rem; }
.chat-heading { display: flex; flex-wrap: wrap; align-items: center; gap: .25rem .6rem; }
.chat-header h2 { font-size: 1rem; font-weight: 760; margin: 0; }
.chat-presence { color: var(--theme-text-muted); font-size: .75rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
.chat-identity { font-size: .75rem; color: var(--theme-text-muted); margin: .125rem 0 0; }
.chat-close, .chat-remove-image { width: 2rem; height: 2rem; border-radius: .375rem; font-size: 1.375rem; }

.chat-messages {
    flex: 1;
    min-height: 0;
    overflow: auto;
    overscroll-behavior: contain;
    margin: 0 .5rem;
    padding: .625rem .5rem;
    list-style: none;
    background: var(--theme-page);
    border-radius: .625rem;
    scrollbar-gutter: stable;
}

.chat-message { margin: .85rem 0 0; overflow-wrap: anywhere; }
.chat-message:first-child { margin-top: 0; }
.chat-message-continuation { margin-top: .2rem; }
.chat-message-date {
    display: flex;
    align-items: center;
    gap: .5rem;
    margin-block-end: .65rem;
    color: var(--theme-text-muted);
    font-size: .75rem;
    text-align: center;
}
.chat-message-date::before, .chat-message-date::after {
    content: "";
    flex: 1;
    border-top: 1px solid var(--theme-border);
}
.chat-message-author { display: block; margin: 0 0 .25rem .25rem; font-size: .8125rem; font-weight: 700; }
.chat-message-self .chat-message-author, .chat-message-continuation .chat-message-author {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}
.chat-message-row { display: flex; align-items: flex-end; gap: .35rem; }
.chat-message-self .chat-message-row { flex-direction: row-reverse; }
.chat-message-bubble {
    min-width: 0;
    max-width: 80%;
    padding: .42rem .65rem;
    border-radius: .85rem .85rem .85rem .25rem;
    background: var(--theme-surface-soft);
    color: var(--theme-text);
}
.chat-message-self .chat-message-bubble {
    border-radius: .85rem .85rem .25rem .85rem;
    background: color-mix(in srgb, var(--theme-accent) 20%, var(--theme-surface));
}
.chat-message-time { flex: none; padding-bottom: .15rem; font-size: .68rem; color: var(--theme-text-muted); }
.chat-message-text { margin: 0; font-size: .875rem; line-height: 1.45; white-space: pre-wrap; }
.chat-message-bubble a { display: block; width: fit-content; max-width: 100%; }
.chat-message-text + a { margin-top: .35rem; }
.chat-message-image { display: block; max-width: 100%; max-height: 10rem; object-fit: contain; border-radius: .45rem; }
.chat-empty { font-size: .875rem; text-align: center; color: var(--theme-text-muted); margin-block: 1rem; }
.chat-status { margin: .4rem .875rem 0; color: var(--theme-text-muted); font-size: .75rem; }
.chat-status[data-error] { color: var(--theme-alert); }

.chat-composer {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: .375rem;
    align-items: end;
    padding: .625rem;
}

.chat-composer textarea {
    width: 100%;
    min-height: 2.5rem;
    max-height: 5rem;
    padding: .6rem .5rem;
    resize: vertical;
    font: inherit;
    font-size: max(.875rem, 16px);
    background: var(--theme-surface-input);
    border: 1px solid var(--theme-border);
    border-radius: .5rem;
    line-height: 1.4;
}

.chat-file-button { display: grid; place-items: center; width: 2rem; height: 2.5rem; border-radius: .4rem; color: var(--theme-accent); }
.chat-send { min-height: 2.5rem; padding-inline: .625rem; border-radius: .5rem; background: var(--theme-accent); color: var(--theme-accent-ink); font-size: .8125rem; font-weight: 700; }
.chat-send:hover { background: var(--theme-accent-hover); }
.chat-send:disabled { cursor: wait; opacity: .65; }
.chat-attachment { grid-column: 1 / -1; display: flex; align-items: flex-start; gap: .375rem; }
.chat-attachment img { max-width: 8rem; max-height: 4rem; border-radius: .375rem; }

@media (max-width: 450px) {
    .slogs-chat { --chat-inset: max(.625rem, env(safe-area-inset-right)); --chat-bottom: max(.625rem, env(safe-area-inset-bottom)); }
    .chat-panel { width: min(25rem, calc(100vw - var(--chat-inset) - .625rem)); }
}
