/* CHAT AND POSTS */

.chat-window {
    display: grid;
    grid-template-rows: minmax(0, 1fr) auto;
    gap: var(--space-sm);
    height: 100%;
    min-height: 0;
    overflow: hidden;

    > div {
        display: flex;
        flex-direction: column-reverse;
        gap: var(--space-sm);
        min-height: 0;
        padding: 0 var(--space-xs);
        overflow-y: auto;

        > .chat-break {
            display: flex;
            gap: var(--space-sm);
            align-items: center;
            color: var(--dark);

            &::before,
            &::after {
                flex: 1;
                content: "";
                border-top: 1px solid var(--light);
            }

            > span {
                font-size: var(--font-size-sm);
                font-weight: 600;
                white-space: nowrap;
            }
        }
    }

    >form {
        position: sticky;
        bottom: 0;
        z-index: 1;
        background-color: var(--white);
    }
}

.chat-sidebar {
    display: grid;
    grid-template-rows: minmax(0, 1fr) auto;
    width: min(320px, 100%);
    min-width: 250px;
    max-width: 320px;
    height: 100%;
    min-height: 0;
    overflow: hidden;

    > main {
        min-height: 0;
        overflow-y: auto;
    }
}

.chat-message {
    display: flex;
    gap: var(--space-sm);

    >.avatar {
        flex-shrink: 0;
    }

    > main {
        display: flex;
        flex-direction: column;

        width: fit-content;
        max-width: min(75ch, 80%);

        > header {
            display: flex;
            gap: var(--space-xs);
            align-items: center;
            padding: var(--space-xs) 0;
            font-size: var(--font-size-sm);
            line-height: calc(var(--space-xs) / 2);
            white-space: nowrap;
        }

        > button {
            user-select: text;
        }
    }

    &.editing > main {
        width: min(75ch, 80%);
    }

    &.from-self {
        flex-direction: row-reverse;

        > main {
            align-items: flex-end;

            md-block {
                padding: var(--space-xs) var(--space-sm);
                word-break: break-word;

                background-color: color-mix(in srgb, var(--secondary) 10%, var(--white));
                border-radius: var(--radius-md);
            }
            > header { align-self: flex-end; }
        }
    }
    &.from-other {
        justify-content: flex-start;

        > main {
            align-items: flex-start;

            md-block {
                padding: var(--space-xs) var(--space-sm);
                word-break: break-word;

                background-color: color-mix(in srgb, var(--secondary) 10%, var(--white));
                border-radius: var(--radius-md);
            }

            > main { background-color: var(--light); }
        }
    }
    &.from-system {
        align-items: center;
        justify-content: center;

        >.avatar {
            display: none;
        }

        > main {
            width: 100%;
            max-width: 100%;
            padding: 0;

            text-align: center;
            align-items: center;

            line-height: calc(var(--space-xs) / 2);

            > header {
                color: var(--dark);
                font-size: var(--font-size-sm);
                padding: 0;
                z-index: 1;
            }

            md-block {
                color: var(--dark);
                font-size: var(--font-size-sm);
                line-height: normal;

                background-color: var(--white);
                border-radius: 0;
            }

            > main {
                color: var(--dark);
                font-size: var(--font-size-sm);
                line-height: normal;

                background-color: var(--white);
                border-radius: 0;
            }
        }
    }
}

.message-edit-form {
    width: 100%;

    > .input-field > textarea {
        box-sizing: border-box;
        min-height: 5em;
        resize: vertical;
    }
}

.post-window {
    > div {
        display: flex;
        flex-direction: column-reverse;
        gap: var(--space-sm);
        min-height: 0;

        padding-top: var(--space-md);

        > .chat-break {
            display: flex;
            gap: var(--space-sm);
            align-items: center;
            color: var(--dark);

            &::before,
            &::after {
                flex: 1;
                content: "";
                border-top: 1px solid var(--light);
            }

            > span {
                font-size: var(--font-size-sm);
                font-weight: 600;
                white-space: nowrap;
            }
        }
    }

    > form {
        position: sticky;
        bottom: 0;
        z-index: 1;
        background-color: var(--white);
    }
}

.post-message {
    &.editing > header > :last-child {
        opacity: 1;
    }

    > header {
        > :last-child {
            opacity: 0;
            transition-duration: var(--transition-fast);
        }
    }

    > main {
        > md-block {
            display: block;
            word-break: break-word;
        }
    }

    &:hover,
    &:focus-within {
        > header > :last-child {
            opacity: 1;
        }
    }

    &.system {
        margin-left: 0;
        color: var(--dark);
        text-align: center;
        background-color: var(--white);

        &::after {
            display: none;
        }
    }
}

.thread {
    position: relative;
    anchor-scope: --thread-head, --thread-child;

    .thread-head {
        min-height: 0;
        anchor-name: --thread-head;
    }

    [data-thread-root] {
        anchor-name: --thread-head;
    }

    .thread-child {
        margin-left: calc(var(--space-xl) * 2);
        anchor-name: --thread-child;
        anchor-scope: --thread-child;

        &::after {
            position: absolute;

            top: anchor(bottom --thread-head);
            right: anchor(left --thread-child);
            bottom: anchor(center --thread-child);
            left: calc(anchor(left --thread-head) + var(--space-xl));
            position-anchor: --thread-child;
            content: "";
            border-bottom: 3px solid var(--light);

            border-left: 3px solid var(--light);
            border-bottom-left-radius: var(--radius-sm);
        }
    }
}
