/* ===== Thread page (messaging/thread) ===== */
/* ГЛОБАЛНИ НАСТРОЙКИ (потребителят може да ги override-не на :root, body или .thread-page) */
:root {
    /* ширина на целия thread контейнер */
    --thread-max-width: 860px;

    /* разстояния */
    --thread-gap: 6px;

    /* ширина на балончето като % от контейнера */
    --thread-bubble-maxw: 50%;
    --thread-bubble-minw: 120px;

    /* падинги и радиуси */
    --thread-bubble-pad-y: 8px;
    --thread-bubble-pad-x: 11px;
    --thread-bubble-radius: var(--radius, 10px);

    /* типография в балончето */
    --thread-bubble-fs: 0.92rem;
    --thread-bubble-lh: 1.35;

    /* размери за composer textarea (drag-resize е включен) */
    --thread-textarea-minh: 64px;
    --thread-textarea-maxh: 340px;
    --thread-textarea-minw: 180px;
}

:root {
    --thread-textarea-width: 655px;
    --thread-textarea-minh: 80px;
    --thread-textarea-maxh: 280px;
}

/* Контейнер */
.thread-page {
    margin-top: 20px;
    margin-left: auto;
    margin-right: auto; /* центриране */
    max-width: var(--thread-max-width); /* регулируема ширина на целия thread */
    display: flex;
    flex-direction: column;
    gap: 16px;
    color: var(--txt, #222);
    font-size: 0.95rem;
}

/* Header */
.thread-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 14px;
}

.thread-header .avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    overflow: hidden;
    background: var(--primary-soft, #eee);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    color: var(--primary, #333);
}

.thread-header .avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.thread-header .initial {
    font-size: 1.1rem;
    color: var(--primary, #333);
}

.thread-header h2 {
    margin: 0;
    font-size: 1.2rem;
    font-weight: 600;
}

/* Messages */
.messages {
    display: flex;
    flex-direction: column;
    gap: var(--thread-gap); /* регулируемо разстояние между съобщенията */
}

.msg {
    display: flex;
}

.msg.in {
    justify-content: flex-start;
}

.msg.out {
    justify-content: flex-end;
}

.bubble {
    max-width: var(--thread-bubble-maxw); /* регулируема ширина на балончето */
    min-width: var(--thread-bubble-minw);
    padding: var(--thread-bubble-pad-y) var(--thread-bubble-pad-x);
    font-size: var(--thread-bubble-fs);
    line-height: var(--thread-bubble-lh);
    border-radius: var(--thread-bubble-radius);
    word-wrap: break-word;
    word-break: break-word;
}

.msg.in .bubble {
    background: #f4f4f4;
    color: var(--txt, #222);
    border-top-left-radius: 4px;
}

.msg.out .bubble {
    background: var(--primary, #007bff);
    color: #fff;
    border-top-right-radius: 4px;
}

.bubble .body {
    margin-bottom: 4px;
}

.bubble .meta {
    font-size: 0.75rem;
    color: var(--txt-dim, #777);
}

.msg.out .bubble .meta {
    color: rgba(255, 255, 255, 0.85);
}

/* ===== Composer — anti-overflow fix ===== */
.composer {
    gap: 8px;
    align-items: flex-start;
    margin-top: auto;
    background: #fff;
    border: 1px solid var(--border, #ddd);
    border-radius: var(--radius, 8px);
    padding: 8px;
    box-shadow: var(--shadow, 0 2px 4px rgba(0, 0, 0, .08));
    min-width: 0; /* ключово: позволява свиване в картата */
}

.composer textarea {
    flex: 1 1 auto; /* заема остатъка, но се свива при нужда */
    width: auto; /* не фиксирай 100% ширина */
    min-width: 0; /* критично за да няма хор. скрол */
    max-width: 100%;
    box-sizing: border-box;

    border: 1px solid var(--border, #ddd);
    border-radius: var(--radius-sm, 6px);
    font: inherit;
    padding: 6px 8px;

    resize: vertical; /* забрани хоризонтален resize */
    min-height: var(--thread-textarea-minh, 64px);
    max-height: var(--thread-textarea-maxh, 340px);
}

/* ако имаш глобално .input { width:100% }, това го неутрализира тук */
.composer textarea.input {
    width: auto;
}

.composer textarea:focus {
    outline: none;
    border-color: var(--primary, #007bff);
    box-shadow: 0 0 0 2px hsl(var(--p-h) 90% 85% / 0.7);
}

.composer button {
    flex: 0 0 auto; /* бутонът не расте и не бута */
    padding: 8px 16px;
    border: none;
    border-radius: var(--radius-sm, 6px);
    background: var(--primary, #007bff);
    color: #fff;
    cursor: pointer;
    font-weight: 600;
    white-space: nowrap; /* етикетът остава на един ред */
    transition: background-color .15s ease, filter .15s ease;
}

.composer button:hover {
    filter: brightness(0.95);
}

/* На много тесни екрани позволяваме пренасяне: бутонът пада отдолу, 100% ширина */
@media (max-width: 560px) {
    .composer {
        gap: 6px;
        flex-wrap: wrap;
    }

    /*.composer textarea{ order: 1; }*/
    /*.composer button{ order: 2; width: 100%; }*/
}

/* Допълнителна безопасност за контейнерите да могат да се свиват */
.messaging-card,
.thread-page,
.messages {
    min-width: 0;
}

/* 🔎 По-малки екрани – увеличи леко ширината на балончетата за по-удобно четене */
@media (max-width: 640px) {
    .thread-page {
        padding: 0 10px;
    }

    .bubble {
        max-width: 70%;
    }
}


.msg .avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    overflow: hidden;
    flex-shrink: 0;
    background: var(--primary-soft, #eee);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--primary, #333);
}

.msg .avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}


/* ===== Messaging → Inbox: фиксове за преливане и responsive ===== */

/* Лейаутът: сайдбар + основно; на мобилно става на един стълб */
.messaging-container {
    display: grid;
    grid-template-columns: 220px 1fr;
    gap: 12px;
    align-items: start;
}

@media (max-width: 860px) {
    .messaging-container {
        grid-template-columns: 1fr;
    }

    /* Сайдбарът да е ОТГОРЕ на мобилно */
    .messaging-sidebar {
        order: 1;
    }

    .messaging-main {
        order: 2;
    }
}

/* Критично: позволява свиване вътре (иначе се появява хор. скрол) */
.messaging-main,
.messaging-card,
.inbox-link,
.inbox-main,
.inbox-row {
    min-width: 0;
}

/* Търсачката – input + бутон винаги на един ред */
.search-container form {
    display: grid;
    grid-template-columns: 1fr auto; /* поле запълва, бутон по съдържание */
    align-items: center;
}

.search-container input[type="search"] {
    min-width: 0;
    width: 100%;
}

.search-container button {
    white-space: nowrap; /* не чупи „Търси“ */
}

@media (max-width: 480px) {
    .search-container button {
        height: 40px;
    }
}

/* Списъкът */
.inbox-list {
    list-style: none;
    margin: 10px 0 0;
    padding: 0;
    display: grid;
    gap: 8px;
}

/* Елемент от списъка: чекбокс колона + линк (аватар + съдържание) */
.inbox-item {
    display: grid;
    grid-template-columns: 28px 1fr;
    gap: 8px;
    align-items: start;
}

/* Чекбоксът да е компактна фиксирана колона */
.checkbox-label {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    flex: 0 0 28px;
}

/* Линкът вътре: аватар + текстова част; важно: min-width:0 за свиване */
.inbox-link {
    display: grid;
    grid-template-columns: 48px 1fr;
    gap: 10px;
    align-items: start;
    text-decoration: none;
    color: inherit;
    min-width: 0;
}

/* Аватар – фиксиран размер + cover */
.inbox-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    overflow: hidden;
    background: #eee;
    flex-shrink: 0;
}

.inbox-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.inbox-avatar .initial {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    font-weight: 700;
}

/* Текстовата част да се свива и да не прелива */
.inbox-main {
    min-width: 0;
}

/* Заглавен ред: име вляво (елипсис), дата вдясно (no-wrap) */
.inbox-row {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
    min-width: 0;
}

.inbox-row .user {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow-wrap: anywhere;
}

.inbox-row .ts {
    flex: 0 0 auto;
    white-space: nowrap;
    color: var(--txt-dim);
    font-size: .9rem;
}

/* Тема и преглед – да не излизат извън картата */
.inbox-subject,
.inbox-preview {
    overflow: hidden;
    text-overflow: ellipsis;
    overflow-wrap: anywhere;
    word-break: break-word;
    display: -webkit-box;
    -webkit-box-orient: vertical;
}

.inbox-subject {
    -webkit-line-clamp: 1;
}

.inbox-preview {
    -webkit-line-clamp: 2;
    line-height: 1.3;
}

/* По-малки екрани: по-малък аватар и дата под името */
@media (max-width: 560px) {
    .inbox-link {
        grid-template-columns: 40px 1fr;
    }

    .inbox-avatar {
        width: 40px;
        height: 40px;
    }

    .inbox-row {
        flex-wrap: wrap;
    }

    .inbox-row .user {
        width: 100%;
        white-space: normal;
    }

    .inbox-row .ts {
        width: 100%;
        order: 1;
        font-size: .85rem;
    }
}

.composer-actions {
    display: flex;
    gap: 8px;
    align-items: center;
    justify-content: flex-end;
    margin-top: 8px;
}

/* Контейнерът на формата */
.composer {
    display: grid;
    gap: 8px;
}

/* Редът с бутоните */
.composer-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    align-items: center;
}

/* Важно: да не се раздуват бутоните в flex */
.composer-actions .btn {
    flex: 0 0 auto;
    width: auto;
}

/* <= 960px: Изпрати под емоджи (вертикално, вдясно) */
@media (max-width: 960px) {
    .composer-actions {
        flex-direction: column;
        align-items: flex-end;
    }
}

/* <= 560px: textarea отгоре, бутоните отдолу */
@media (max-width: 560px) {
    .composer {
        display: grid;
        grid-template-columns: 1fr;
        gap: 8px;
    }

    .composer textarea {
        width: 100%;
    }

    /* ако бутоните са вътре в .composer-actions */
    .composer-actions {
        display: flex;
        flex-direction: column; /* емоджи над изпрати */
        align-items: stretch;
        gap: 8px;
    }

    .composer-actions .btn,
    .composer button {
        width: 100%;
    }
}

