#comments-popup {
    display: none;
    position: fixed;
    right: 2em;
    top: 10em;
    z-index: var(--layer-modal);
    width: 420px;
    height: 640px;
    max-height: 80vh;
    min-width: 200px;
    min-height: 200px;
    background: var(--surface-bg);
    border: var(--border-1) solid var(--color-active);
    border-radius: var(--radius-3);
    box-shadow: var(--shadow-3),
                0 0 6px color-mix(in srgb, var(--color-active) 25%, transparent),
                0 0 14px color-mix(in srgb, var(--color-active) 12%, transparent);
    padding: 1em;
    flex-direction: column;
    transition: top 0.25s ease, left 0.25s ease, width 0.25s ease, height 0.25s ease,
                right 0.25s ease, bottom 0.25s ease, border-radius 0.25s ease,
                box-shadow 0.25s ease, padding 0.25s ease, opacity 0.3s ease;
    max-width: calc(100vw - 0.5em) !important;
    overscroll-behavior: contain;
    overflow: hidden;
}

#comments-popup.editor-behind {
    opacity: 0.15;
}

#comments-popup.editor-behind:hover {
    opacity: 1;
}

body.chat-fullscreen {
    overflow: hidden;
}

#comments-popup[data-fullscreen="true"] {
    display: flex !important;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    max-width: none !important;
    max-height: none !important;
    border-radius: 0;
    box-shadow: none;
    border: none;
    z-index: var(--layer-fullscreen);
    box-sizing: border-box;
    padding: 0.5em;
}

.comments-popup-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-1);
    padding-bottom: var(--space-1);
}

.comments-popup-header h3 {
    margin: 0;
    line-height: 1.3;
    min-width: 0;
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    text-align: left;
}

/* Chat navigation back/forward buttons */
.chat-nav-buttons {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    flex-shrink: 0;
    position: relative;
}

.chat-nav-btn {
    background: none;
    border: none;
    color: var(--color-text);
    cursor: pointer;
    padding: var(--space-1);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-1);
    transition: opacity 0.15s var(--ease-out-2), background-color 0.15s var(--ease-out-2);
}

.chat-nav-btn:hover:not(:disabled) {
    background-color: var(--surface-hover);
}

.chat-nav-btn:disabled {
    opacity: 0.25;
    cursor: default;
    pointer-events: none;
}

.chat-nav-icon {
    width: var(--space-px-4);
    height: var(--space-px-4);
    stroke: currentColor;
    fill: none;
}

/* Recent chats dropdown */
.chat-nav-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    min-width: 200px;
    max-width: 300px;
    max-height: 320px;
    overflow-y: auto;
    background: var(--surface-section);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-2);
    box-shadow: var(--shadow-3);
    z-index: var(--layer-modal);
    padding: var(--space-1) 0;
}

.chat-nav-dropdown-item {
    display: flex;
    align-items: center;
    width: 100%;
    padding: 0;
    background: none;
    border: none;
    font-size: var(--text-1);
    color: var(--color-text);
    line-height: 1.4;
}

.chat-nav-dropdown-item:hover {
    background-color: var(--surface-hover);
}

.chat-nav-dropdown-item.current {
    font-weight: var(--weight-7);
}

.chat-nav-dropdown-label {
    flex: 1;
    min-width: 0;
    padding: var(--space-1) var(--space-3);
    background: none;
    border: none;
    text-align: left;
    cursor: pointer;
    font: inherit;
    color: inherit;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-nav-dropdown-remove {
    flex-shrink: 0;
    background: none;
    border: none;
    color: var(--color-muted);
    cursor: pointer;
    font-size: var(--text-2);
    padding: var(--space-1) var(--space-2);
    line-height: 1;
    transition: color 0.15s var(--ease-out-2);
}

.chat-nav-dropdown-remove:hover {
    color: var(--color-text);
}

.comments-popup-actions {
    display: inline-flex;
    align-items: center;
    gap: 0.4em;
}

.comments-popup-action {
    background: none;
    border: none;
    color: var(--color-text);
    cursor: pointer;
    padding: 0;
    width: var(--space-px-5);
    height: var(--space-px-5);
    flex: 0 0 var(--space-px-5);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2em;
    line-height: 1;
}

.comments-popup-action > span {
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.comments-popup-action-icon {
    display: block;
    width: var(--space-px-3);
    height: var(--space-px-3);
    fill: currentColor;
}

#comments-popup .resize-handle {
    position: absolute;
    width: 10px;
    height: 10px;
    top: -5px;
    z-index: 1001;
}

#comments-popup .resize-handle-left {
    left: -5px;
    cursor: nwse-resize;
}

#comments-popup .resize-handle-right {
    right: -5px;
    cursor: nesw-resize;
}

#comments-list {
    padding-top: 0.5em;
    overflow-y: auto;
    overscroll-behavior: contain;
    /* height: 380px; */
    flex-grow: 1;
    /* align to vertical bottom */
    display: flex;
    flex-direction: column;
    background: linear-gradient(to bottom,
            color-mix(in srgb, var(--color-border), transparent 60%) 0%,
            transparent 100%);
    border-radius: 10px;
}

.comment-item:first-child {
    border-top: none;
}

.comment-topics-list {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: 0.5em;
    padding: var(--space-1) 0;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none; /* IE/Edge */
    min-height: 36px; /* Prevent height collapse during scroll */
}

.comment-topics-list::-webkit-scrollbar {
    display: none; /* Chrome/Safari */
}

#comments-list::before {
    content: '';
    margin-top: auto;
    /* This pushes content to the bottom when there's few items, 
       but collapses when content overflows, preserving scroll behavior. 
    */
}

#new-comment-form {
    margin-top: auto;
}

#new-comment-form textarea {
    box-sizing: border-box;
    width: 100%;
    resize: none;
    font-size: var(--text-1);
    overflow-y: auto;
    min-height: calc(var(--text-1) * 1.5 * 2); /* 2 lines minimum */
    max-height: calc(var(--text-1) * 1.5 * 10); /* 10 lines maximum */
    transition: height 0.1s ease-out, border-color 0.15s ease-out, box-shadow 0.15s ease-out;
    /* Prevent iOS zoom on focus */
}

#new-comment-form.dnd-over-into textarea {
    border-color: var(--link);
    box-shadow: 0 0 0 2px color-mix(in srgb, var(--link) 25%, transparent);
}

#new-comment-form button[type="submit"] {
    float: right;
}

#new-comment-form #cancel-edit-btn {
    float: right;
    margin-right: 0.5em;
}
