/* Стили диалогов */
.loading {
    text-align: center;
    padding: 20px;
    color: var(--text-secondary);
    font-style: italic;
}

.error {
    text-align: center;
    padding: 20px;
    color: var(--danger-color);
    font-style: italic;
    border: none !important; /* Принудительно убираем границы */
}

/* Локальная нормализация только для области диалогов */
.dialogs-full-container,
.dialogs-full-container * {
    box-sizing: border-box;
}

.unread-pill {
    min-width: 20px;
    height: 18px;
    padding: 0 6px;
    border-radius: 999px;
    background: rgba(59, 130, 246, 0.18);
    color: var(--text-primary);
    font-size: 12px;
    line-height: 18px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* Стили сообщений */
.message {
    margin-bottom: 12px;
    display: flex;
    position: relative;
}

.message-body {
    display: flex;
    flex-direction: column;
    gap: 2px;
    max-width: 72%;
}

.message.sent {
    justify-content: flex-end;
}

.message.received {
    justify-content: flex-start;
}

.message-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--primary-color);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 11px;
    font-weight: bold;
    flex-shrink: 0;
    position: relative;
    overflow: hidden;
}

.message-avatar-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    position: absolute;
    inset: 0;
}

.message-avatar-fallback {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
}

.message-sender {
    font-size: 11px;
    color: var(--text-secondary);
    margin-bottom: 2px;
}

.message.sent .message-sender {
    text-align: right;
}

.message-bubble {
    padding: 8px 12px;
    border-radius: 12px;
    background: var(--card-bg);
    color: var(--text-primary);
    box-shadow: 0 1px 2px rgba(0,0,0,0.06);
    word-break: break-word;
    white-space: pre-wrap;
    font-size: 14px;
    line-height: 1.25;
}

/* Media inside message bubble */
.message-media {
    margin-top: 8px;
}

.voice-media {
    margin-top: 4px;
    white-space: normal;
}

.voice-media audio {
    width: 180px !important;
}

.message-bubble:has(.voice-media) {
    padding: 4px 6px;
    white-space: normal;
}

.voice-duration {
    margin: 0 0 2px;
    font-size: 11px;
    opacity: 0.75;
}

/* Avito resolves audio on click; keep its placeholder and player equally small. */
.message-bubble:has([data-voice-proxy-url]) {
    padding: 2px 4px;
    white-space: normal;
}

.message-media[data-voice-proxy-url] {
    margin-top: 0 !important;
    white-space: normal;
}

[data-voice-proxy-url] audio {
    width: 180px !important;
    height: 32px;
    margin-top: 0 !important;
}

[data-voice-proxy-url] audio:not([src]) {
    display: none !important;
}

@media (max-width: 1000px) {
    [data-voice-proxy-url] button, [data-voice-proxy-url] audio { min-height: 44px; }
}

.message-media-img {
    max-width: 320px;
    max-height: 240px;
    width: auto;
    height: auto;
    object-fit: cover;
    border-radius: 10px;
    display: block;
    background: rgba(0,0,0,0.06);
    cursor: zoom-in;
}

.media-lightbox {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.72);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
    z-index: 99999;
}

.media-lightbox.visible {
    display: flex;
}

.media-lightbox img {
    max-width: min(100%, 1100px);
    max-height: min(100%, 90vh);
    width: auto;
    height: auto;
    border-radius: 12px;
    box-shadow: 0 8px 30px rgba(0,0,0,0.35);
    cursor: zoom-out;
}

.message-media-placeholder {
    padding: 10px 12px;
    border-radius: 10px;
    background: rgba(0,0,0,0.06);
    color: var(--text-secondary);
    font-size: 13px;
}

#chatBadges {
    display: none;
    gap: 6px;
    flex-wrap: wrap;
}

.chat-header-badge {
    font-size: 11px;
    opacity: 0.75;
    border: 1px solid var(--border-color);
    border-radius: 999px;
    padding: 1px 6px;
    line-height: 1.4;
}

.message-reactions {
    margin-top: 6px;
    font-size: 12px;
    color: var(--text-secondary);
}

.message-bubble.message-failed {
    outline: 1px solid rgba(239, 68, 68, 0.55);
}

.message.sent .message-bubble {
    background: var(--primary-color);
    color: white;
    border-radius: 12px 12px 4px 12px;
}

.message.received .message-bubble {
    background: var(--card-bg);
    color: var(--text-primary);
    border-radius: 12px 12px 12px 4px;
    border: 1px solid var(--border-color);
    box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}

.message-status {
    font-size: 9px;
    color: var(--text-secondary);
    margin-top: 2px;
}

.message-meta {
    font-size: 11px;
    color: var(--text-secondary);
}

.message.sent .message-meta {
    text-align: right;
}

.message.sent .message-status {
    text-align: right;
}

/* Стили элементов диалога */
.chat-item {
    padding: 6px 4px 6px 6px;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    display: flex;
    align-items: center;
    transition: background 0.2s;
}

.chat-item:hover {
    background: var(--hover-bg);
}

.chat-item.active {
    background: rgba(59, 130, 246, 0.12);
    color: var(--text-primary);
}

.chat-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: var(--primary-color);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 6px;
    color: white;
    font-weight: bold;
    flex-shrink: 0;
    font-size: 14px;
    overflow: hidden;
    position: relative;
}

.chat-avatar-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.chat-avatar-fallback {
    position: absolute;
    inset: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* По умолчанию показываем fallback (инициалы), скрываем только когда картинка точно загрузилась */
.chat-avatar.has-image .chat-avatar-fallback {
    opacity: 0;
}

.chat-avatar.fallback .chat-avatar-img {
    opacity: 0;
}

/* Заголовок чата использует #chatAvatar как контейнер аватара */
#chatAvatar {
    overflow: hidden;
    position: relative;
}

#chatAvatar .chat-avatar-fallback {
    position: absolute;
    inset: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

#chatAvatar.has-image .chat-avatar-fallback {
    opacity: 0;
}

#chatAvatar.fallback .chat-avatar-img {
    opacity: 0;
}

.chat-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 3px;
    margin-left: 4px;
    flex-shrink: 0;
}

.unread-badge {
    min-width: 18px;
    height: 18px;
    padding: 0 6px;
    border-radius: 999px;
    background: var(--primary-color);
    color: #fff;
    font-size: 12px;
    line-height: 18px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.chat-info {
    flex: 1;
    min-width: 0;
}

.chat-name {
    font-weight: 500;
    margin-bottom: 1px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 14px;
}

.chat-preview {
    font-size: 12px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-time {
    font-size: 11px;
    color: var(--text-secondary);
    margin-left: 0;
    flex-shrink: 0;
}

.chat-item.active .chat-preview,
.chat-item.active .chat-time {
    color: var(--text-secondary);
}

.ai-reply-panel {
    flex: 0 0 auto;
    margin: 0 6px 4px;
    padding: 9px 11px;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background: var(--card-bg);
    box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.12);
}

.ai-reply-panel__header,
.ai-reply-panel__actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.ai-reply-panel__title {
    font-weight: 600;
    white-space: nowrap;
}

.ai-reply-panel__status {
    color: var(--text-secondary);
    font-size: 13px;
}

.ai-reply-panel__status[data-tone="ready"] {
    color: var(--accent-color);
}

.ai-reply-panel__status[data-tone="loading"] {
    display: inline-flex;
    align-items: center;
    gap: 5px;
}

.ai-reply-panel__status[data-tone="loading"]::after {
    content: '';
    width: 8px;
    height: 8px;
    border: 2px solid var(--primary-color);
    border-right-color: transparent;
    border-radius: 50%;
    animation: ai-reply-loading-spin 0.8s linear infinite;
}

@keyframes ai-reply-loading-spin {
    to { transform: rotate(360deg); }
}

.ai-reply-panel__primary,
.ai-reply-panel__secondary,
.ai-reply-panel__link,
.ai-reply-panel__collapse,
.ai-reply-panel__toggle {
    border: 0;
    cursor: pointer;
    font: inherit;
}

.ai-reply-panel__primary {
    padding: 7px 13px;
    border-radius: 7px;
    background: var(--primary-color);
    color: white;
}

.ai-reply-panel__secondary {
    padding: 7px 13px;
    border: 1px solid var(--border-color);
    border-radius: 7px;
    background: transparent;
    color: var(--text-primary);
}

.ai-reply-panel__link,
.ai-reply-panel__collapse {
    padding: 5px;
    background: transparent;
    color: var(--primary-color);
}

.ai-reply-panel__collapse {
    margin-left: 2px;
}

.ai-reply-panel__toggle {
    margin-left: auto;
    padding: 5px 8px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: transparent;
    color: var(--text-primary);
    white-space: nowrap;
}

.ai-reply-panel__body {
    margin-top: 9px;
}

.ai-reply-panel__response {
    min-height: 48px;
    padding: 10px;
    border: 1px solid var(--border-color);
    border-radius: 7px;
    background: var(--secondary-color);
    line-height: 1.35;
}

.ai-reply-panel__usage,
.ai-reply-panel__metrics,
.ai-reply-panel__model {
    color: var(--text-secondary);
    font-size: 12px;
    white-space: nowrap;
}

.ai-reply-panel__runtime {
    display: inline-flex;
    align-items: center;
    gap: 10px;
}

.ai-reply-panel__actions {
    margin-top: 9px;
}

.ai-reply-panel__note {
    margin-left: 0;
    color: var(--text-secondary);
    font-size: 12px;
}

.ai-reply-panel__note[data-tone="accepted"] { color: var(--accent-color); font-weight: 600; }
.ai-reply-panel__note[data-tone="rejected"] { color: var(--danger-color); font-weight: 600; }
.ai-reply-panel__note[data-tone="loading"] { font-weight: 600; }

.ai-reply-panel__edit-note {
    margin-left: auto;
    color: var(--text-secondary);
    font-size: 12px;
}

.ai-reply-panel button:disabled {
    cursor: not-allowed;
    opacity: 0.55;
}

.ai-reply-panel button[data-pending="true"]:disabled { cursor: wait; }

#messageText {
    min-height: 30px;
    max-height: 35vh;
    resize: none;
    overflow-y: auto;
    line-height: 1.35;
}

#messageText:disabled {
    cursor: not-allowed;
    opacity: 0.6;
}

body.dark-theme #messageText:disabled {
    background-color: var(--card-bg) !important;
    color: var(--text-secondary) !important;
}

#sendMessageButton:disabled {
    cursor: not-allowed;
    opacity: 0.55;
}

@media (max-width: 900px) {
    .ai-reply-panel__actions {
        flex-wrap: wrap;
    }

    .ai-reply-panel__note {
        width: 100%;
        margin-left: 0;
    }
}

.attention-dot {
    display: inline-block;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: #f59e0b;
    flex-shrink: 0;
}
.dialog-attention button {
    min-height: 44px;
    padding: 4px 12px;
    border: 1px solid #f59e0b;
    border-radius: 8px;
    background: var(--secondary-color);
    color: var(--text-primary);
    cursor: pointer;
}
.dialog-attention button:disabled { opacity: .6; cursor: wait; }

/* Short attempt journal stays inside the single outgoing card. */
.message.sent .message-meta { white-space: pre-line; }

/* MAX albums, native playback and file names share the message bubble width. */
.max-attachment { margin-top: 8px; max-width: 100%; overflow-wrap: anywhere; }
.max-attachment audio { display: block; width: 260px; max-width: 100%; }
.max-attachment video { display: block; max-width: 100%; border-radius: 12px; }

/* Telegram reply previews and pins; constrained in both mobile and desktop chat. */
.telegram-quote { display: block; width: 100%; min-width: 0; text-align: left; border: 0; border-left: 3px solid var(--primary-color); border-radius: 4px; padding: 6px 9px; margin-bottom: 5px; background: rgba(90,160,220,.14); color: inherit; cursor: pointer; }
.telegram-quote strong, .telegram-quote span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.telegram-quote strong { color: var(--primary-color); font-size: 12px; }
.telegram-quote span { font-size: 13px; }
.telegram-message-actions { display: flex; gap: 10px; margin-top: 4px; }
.telegram-message-actions button, #telegramPins button, #telegramReplyDraft > button { color: var(--text-secondary); border: 0; background: transparent; cursor: pointer; font-size: 12px; }
#telegramPins { flex: 0 0 auto; padding: 5px 12px; border-bottom: 1px solid var(--border-color); min-width: 0; color: var(--text-secondary); font-size: 13px; }
#telegramPins summary { cursor: pointer; }
.telegram-pins-list { max-height: 180px; overflow-y: auto; padding-top: 6px; }
.telegram-pins-list > div { display: flex; align-items: center; gap: 6px; }
.telegram-pins-list .telegram-quote { flex: 1; color: var(--text-primary); }
#telegramReplyDraft { display: flex; gap: 8px; min-width: 0; margin-bottom: 5px; }
#telegramReplyDraft[hidden], #telegramPins[hidden] { display: none; }
#telegramReplyDraft .telegram-quote { flex: 1; margin: 0; }
.telegram-message-highlight .message-bubble { outline: 2px solid var(--primary-color); }
.message.sent .telegram-quote { border-left-color: currentColor; background: rgba(255,255,255,.14); }
.message.sent .telegram-quote strong { color: inherit; }
.telegram-pin-current { display: flex; align-items: center; gap: 10px; min-width: 0; }
#telegramPins .telegram-pin-jump { flex: 1; min-width: 0; text-align: left; padding: 4px 8px; border-left: 3px solid var(--primary-color); color: var(--text-primary); }
.telegram-pin-jump strong, .telegram-pin-jump span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.telegram-pin-jump strong { color: var(--primary-color); font-size: 12px; }
#telegramPins .telegram-pins-list[hidden] { display: none; }
#telegramHistoryPosition { padding: 6px 10px; display: flex; gap: 8px; align-items: center; justify-content: space-between; background: var(--card-bg); border-bottom: 1px solid var(--border-color); color: var(--text-secondary); font-size: 12px; }
#telegramHistoryPosition button { border: 0; border-radius: 6px; background: var(--primary-color); color: white; padding: 7px; cursor: pointer; }

/* MAX embedded forwards and replies; no Telegram actions on these previews. */
.max-message-reference { border-left: 3px solid var(--primary-color); border-radius: 4px; padding: 6px 9px; margin-bottom: 6px; background: rgba(90,160,220,.14); overflow-wrap: anywhere; }
.max-message-reference small, .max-message-reference strong, .max-message-reference span { display: block; }
.max-message-reference small { font-size: 11px; opacity: .8; }
.max-message-reference strong { font-size: 12px; color: var(--primary-color); }
.max-message-reference span { font-size: 13px; max-height: 4.5em; overflow: hidden; }
.message.sent .max-message-reference { border-left-color: currentColor; }
.message.sent .max-message-reference strong { color: inherit; }
.message[data-message-key^="max:"] .message-sender { font-size: 13px; font-weight: 600; color: var(--primary-color); }
