/* retoor <retoor@molodetz.nl> */

.page-messages {
    flex: 1;
    min-height: 0;
    display: flex;
    width: 100%;
    --kb-inset: 0px;
}

dp-chat {
    display: grid;
    flex: 1;
    min-width: 0;
    min-height: 0;
    width: 100%;
}

.messages-layout {
    display: grid;
    grid-template-columns: minmax(0, 320px) minmax(0, 1fr);
    gap: 0;
    flex: 1;
    min-width: 0;
    min-height: 0;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    --kb-inset: 0px;
}

dp-chat[mode="embed"] {
    display: flex;
    width: 100%;
    height: 100%;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    --kb-inset: 0px;
}

.messages-list {
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
}

.messages-list-header {
    padding: var(--space-sm) var(--space-lg);
    border-bottom: 1px solid var(--border);
    position: relative;
}

.messages-list-header input {
    font-size: 0.8125rem;
}

.search-dropdown {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 var(--radius) var(--radius);
    z-index: var(--z-popover);
    max-height: 240px;
    overflow-y: auto;
}

.search-dropdown-item {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding: var(--space-sm) var(--space-lg);
    font-size: 0.875rem;
    color: var(--text-primary);
    text-decoration: none;
}

.search-dropdown-item:hover,
.search-dropdown-item.active {
    background: var(--bg-card-hover);
}

.messages-conversations {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.messages-conversations::-webkit-scrollbar {
    width: 0;
    height: 0;
}

.conversation-item {
    display: flex;
    align-items: center;
    gap: var(--space-md);
    padding: var(--space-sm) var(--space-lg);
    cursor: pointer;
}

.conversation-item:hover {
    background: var(--bg-card-hover);
}

.conversation-item.active {
    background: var(--accent-light);
}

.conversation-info {
    flex: 1;
    min-width: 0;
}

.conversation-name {
    font-size: 0.875rem;
    font-weight: 600;
    margin-bottom: 0.125rem;
}

.conversation-preview {
    font-size: 0.75rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conversation-time {
    font-size: 0.6875rem;
    color: var(--text-muted);
    flex-shrink: 0;
}

.messages-main {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    position: relative;
}

.messages-jump-new {
    position: absolute;
    left: 50%;
    bottom: 72px;
    transform: translateX(-50%);
    z-index: var(--z-popover);
    padding: var(--space-xs) var(--space-md);
    border: none;
    border-radius: 999px;
    background: var(--accent);
    color: var(--on-accent);
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    box-shadow: var(--shadow-sm);
}

.messages-jump-new[hidden] {
    display: none;
}

.messages-connection-banner {
    flex-shrink: 0;
    padding: var(--space-xs) var(--space-lg);
    background: var(--warning);
    color: var(--white);
    font-size: 0.75rem;
    font-weight: 600;
    text-align: center;
}

.messages-connection-banner[hidden] {
    display: none;
}

.messages-main-header {
    padding: var(--space-lg);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: var(--space-md);
    flex-shrink: 0;
}

.messages-main-header h3 {
    font-size: 1rem;
    font-weight: 600;
}

.messages-header-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.messages-presence {
    font-size: 0.6875rem;
    color: var(--text-muted);
}

.messages-presence.online {
    color: var(--success);
}

.messages-presence.online::before {
    content: "";
    display: inline-block;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--success);
    margin-right: 0.3125rem;
    vertical-align: middle;
}

.conversation-unread-dot {
    flex-shrink: 0;
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: var(--accent);
    align-self: center;
}

.conversation-unread-dot[hidden] {
    display: none;
}

.messages-thread {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding: var(--space-lg);
    padding-bottom: var(--space-sm);
    display: flex;
    flex-direction: column;
    gap: var(--space-md);
    min-height: 0;
}

.message-bubble {
    max-width: 70%;
    padding: 0.625rem 0.875rem;
    border-radius: var(--radius-lg);
    font-size: 0.875rem;
    line-height: 1.5;
    position: relative;
    overflow-wrap: anywhere;
    outline: none;
}

.message-bubble.grouped {
    margin-top: calc(var(--space-xs) - var(--space-md));
}

.message-bubble.mine {
    align-self: flex-end;
    background: var(--accent);
    color: var(--on-accent);
    border-bottom-right-radius: 4px;
}

.message-bubble.theirs {
    align-self: flex-start;
    background: var(--bg-card-hover);
    color: var(--text-primary);
    border-bottom-left-radius: 4px;
}

.message-bubble.pending {
    opacity: 0.6;
}

.message-bubble.failed {
    outline: 1px solid var(--danger);
    cursor: pointer;
}

.retry-hint {
    display: block;
    margin-top: 0.25rem;
    font-size: 0.6875rem;
    color: var(--danger);
}

.message-bubble.mine .content-copy-btn,
.message-bubble.theirs .content-copy-btn {
    opacity: 0;
}

.message-bubble:hover .content-copy-btn,
.message-bubble:focus-within .content-copy-btn,
.message-bubble .content-copy-btn:focus {
    opacity: 1;
}

.message-time {
    font-size: 0.6875rem;
    color: var(--text-muted);
    margin-top: 0.25rem;
    display: block;
    transition: opacity 0.1s ease;
}

.message-bubble.grouped .message-time {
    opacity: 0;
}

.message-bubble.grouped:hover .message-time,
.message-bubble.grouped:focus-within .message-time {
    opacity: 1;
}

.message-bubble.mine .message-time {
    color: rgba(255, 255, 255, 0.7);
}

.message-receipt {
    font-size: 0.6875rem;
    color: rgba(255, 255, 255, 0.85);
    margin-left: 0.375rem;
    letter-spacing: -2px;
}

.message-receipt[hidden] {
    display: none;
}

.ai-adjusted-note {
    margin-top: 0.25rem;
    font-size: 0.6875rem;
    color: var(--text-muted);
    font-style: italic;
    opacity: 1;
    transition: opacity 0.4s ease;
}

.ai-adjusted-note.fading {
    opacity: 0;
}

.attachment-pending {
    margin-top: 0.35rem;
    font-size: 0.8rem;
    font-style: italic;
    color: var(--text-secondary);
}

.date-separator {
    position: sticky;
    top: 0;
    z-index: var(--z-popover);
    display: flex;
    justify-content: center;
    pointer-events: none;
}

.date-separator span {
    background: var(--bg-card-hover);
    color: var(--text-muted);
    font-size: 0.6875rem;
    font-weight: 600;
    padding: 0.25rem 0.75rem;
    border-radius: 999px;
    box-shadow: var(--shadow-sm);
}

.typing-indicator {
    align-self: flex-start;
    display: inline-flex;
    align-items: center;
    gap: var(--space-xs);
    padding: var(--space-sm) var(--space-md);
    background: var(--bg-card-hover);
    border-radius: var(--radius-lg);
    border-bottom-left-radius: 4px;
}

.typing-indicator[hidden] {
    display: none;
}

.typing-indicator span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-muted);
    animation: chat-typing-bounce 1.2s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes chat-typing-bounce {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.4;
    }
    30% {
        transform: translateY(-4px);
        opacity: 1;
    }
}

.messages-input-area {
    padding: var(--space-md) var(--space-lg);
    padding-bottom: calc(var(--space-md) + env(safe-area-inset-bottom));
    border-top: 1px solid var(--border);
    display: flex;
    align-items: flex-end;
    gap: var(--space-sm);
    flex-shrink: 0;
    min-height: 56px;
    background: var(--bg-card);
}

.messages-input-area .mention-wrapper {
    flex: 1;
    min-width: 0;
    width: auto;
}

.messages-input-area .mention-wrapper textarea {
    width: 100%;
}

.messages-input-area textarea {
    flex: 1;
    min-width: 0;
    margin: 0;
    resize: none;
    overflow-y: auto;
    max-height: 160px;
    line-height: 1.5;
    padding: var(--space-sm) var(--space-md);
    font-family: inherit;
    font-size: 0.875rem;
    border-radius: var(--radius);
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    color: var(--text-primary);
    outline: none;
}

@supports (field-sizing: content) {
    .messages-input-area textarea {
        field-sizing: content;
        min-height: 1lh;
    }
}

.messages-input-area textarea:focus {
    border-color: var(--accent);
}

.messages-input-area textarea::placeholder {
    color: var(--text-muted);
}

.messages-input-area dp-upload {
    flex-shrink: 0;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
}

.messages-input-area dp-upload .dp-upload-chips {
    flex: 0 0 100%;
    order: -1;
    margin-top: 0;
    margin-bottom: var(--space-sm);
}

.messages-send-btn {
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    padding: 0;
    background: var(--accent);
    color: var(--on-accent);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
}

.messages-send-btn:hover {
    background: var(--accent-hover);
}

.messages-send-btn .send-spinner {
    display: none;
    width: 16px;
    height: 16px;
    border: 2px solid var(--white);
    border-right-color: transparent;
    border-radius: 50%;
    animation: chat-send-spin 0.6s linear infinite;
}

.messages-send-btn.is-sending {
    cursor: default;
    opacity: 0.75;
}

.messages-send-btn.is-sending .send-icon {
    display: none;
}

.messages-send-btn.is-sending .send-spinner {
    display: block;
}

.messages-send-btn:disabled {
    cursor: default;
}

@keyframes chat-send-spin {
    to {
        transform: rotate(360deg);
    }
}

.messages-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-muted);
    gap: var(--space-sm);
}

.messages-empty p {
    font-size: 0.875rem;
}

.messages-back-btn {
    display: none;
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 1.25rem;
    padding: var(--space-xs);
    cursor: pointer;
    line-height: 1;
    margin-right: var(--space-xs);
}

.messages-back-btn:hover {
    color: var(--accent);
}

/* ── Mobile (<= 768px) ─────────────────────────────────── */

@media (max-width: 768px) {
    .page:has(.page-messages) {
        padding-left: 0;
        padding-right: 0;
        padding-top: 0;
    }

    .page-messages {
        padding-bottom: 0;
    }

    .messages-layout,
    dp-chat[mode="embed"] {
        padding-bottom: var(--kb-inset, 0px);
    }

    .messages-layout {
        display: flex;
        flex-direction: column;
        min-height: 0;
        flex: 1;
        border-radius: 0;
        border-left: none;
        border-right: none;
    }

    .messages-list {
        flex: 1;
        min-height: 0;
    }

    dp-chat[with-uid]:not(.show-list) .messages-list,
    .messages-list.hide {
        display: none;
    }

    .messages-main {
        flex: 1;
        min-height: 0;
    }

    dp-chat.show-list .messages-main,
    dp-chat:not([with-uid]) .messages-main,
    .messages-main.hide {
        display: none;
    }

    .messages-back-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .messages-input-area textarea {
        font-size: 16px;
    }

    .message-bubble.grouped .message-time {
        opacity: 1;
    }
}

/* ── Small mobile (<= 480px) ────────────────────────────── */

@media (max-width: 480px) {
    .message-bubble {
        max-width: 82%;
    }

    .messages-main-header {
        padding: var(--space-md);
    }
}

/* ── Smallest mobile (<= 360px) ─────────────────────────── */

@media (max-width: 360px) {
    .messages-thread {
        padding: var(--space-sm);
    }

    .messages-input-area {
        padding: 0.375rem var(--space-sm);
        padding-bottom: calc(0.375rem + env(safe-area-inset-bottom));
        min-height: 48px;
    }

    .messages-input-area textarea {
        font-size: 16px;
    }

    .message-bubble {
        max-width: 90%;
        padding: var(--space-sm) 0.625rem;
        font-size: 0.8125rem;
    }

    .messages-main-header {
        padding: 0.625rem 0.75rem;
    }
}

/* ── Touch devices: larger tap targets (min 44x44px) ────── */

@media (hover: none) and (pointer: coarse) {
    .messages-input-area .emoji-toggle-btn {
        min-width: 44px;
        min-height: 44px;
    }

    .messages-send-btn {
        width: 44px;
        height: 44px;
    }

    .messages-back-btn {
        min-width: 44px;
        min-height: 44px;
    }

    .conversation-item {
        min-height: 44px;
    }

    .message-bubble .content-copy-btn {
        opacity: 1;
        min-width: 44px;
        min-height: 32px;
    }

    .message-bubble.grouped .message-time {
        opacity: 1;
    }
}

.message-report-btn {
    display: inline-flex;
    align-items: center;
    gap: var(--space-xs);
    margin-top: var(--space-xs);
    padding: 0;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 0.7rem;
    opacity: 1;
    cursor: pointer;
}

.message-report-btn:hover,
.message-report-btn:focus {
    color: var(--danger);
}
