/* Раздел Telegram. */

.telegram {
    display: grid;
    grid-template-columns: 260px minmax(0, 1fr);

    width: 100%;
    height: calc(100vh - 190px);
    min-height: 0;

    background: var(--tg-surface);

    border: 1px solid var(--tg-border);
    border-radius: 14px;

    overflow: hidden;
}

/* Панель списка чатов. */

.telegram_sidebar {
    display: flex;
    flex-direction: column;

    min-width: 0;
    min-height: 0;

    overflow: hidden;

    background: var(--tg-surface);

    border-right: 1px solid var(--tg-border);
}

.telegram_sidebar_header {
    flex: 0 0 auto;

    display: flex;
    align-items: center;

    min-height: 58px;
    padding: 0 16px;

    background: var(--tg-surface-secondary);

    border-bottom: 1px solid var(--tg-border);
}

.telegram_sidebar_header h2 {
    margin: 0;

    font-size: 17px;
    font-weight: 600;

    color: var(--tg-text);
}

/* Список чатов. */

.telegram_chats {
    flex: 1 1 auto;

    min-height: 0;

    overflow-y: auto;
    overflow-x: hidden;

    background: var(--tg-surface);

    overscroll-behavior: contain;
}

.telegram_chat_item {
    display: block;

    width: 100%;
    padding: 12px 16px;

    background: var(--tg-surface);

    border: 0;
    border-bottom: 1px solid var(--tg-border-light);

    text-align: left;

    cursor: pointer;

    transition:
        background-color 0.15s ease,
        border-color 0.15s ease,
        color 0.15s ease;
}

.telegram_chat_item:hover {
    background: var(--tg-surface-hover);
}

.telegram_chat_item.active {
    padding-left: 13px;

    background: var(--tg-active-bg);

    border-left: 3px solid var(--tg-accent);
}

.telegram_chat_item_title {
    margin: 0;

    overflow: hidden;

    font-size: 14px;
    font-weight: 600;
    line-height: 1.35;

    color: var(--tg-text);

    text-overflow: ellipsis;
    white-space: nowrap;
}

.telegram_chat_item.active
.telegram_chat_item_title {
    color: var(--tg-accent-text);
}

.telegram_chat_item_thread {
    overflow: hidden;

    font-size: 13px;

    color: var(--tg-text-secondary);

    text-overflow: ellipsis;
    white-space: nowrap;
}

.telegram_chat {
    display: flex;
    flex-direction: column;

    min-width: 0;
    min-height: 0;

    overflow: hidden;

    background: var(--tg-bg);
}

/* Шапка выбранного чата. */

.telegram_chat_header {
    flex: 0 0 auto;

    display: flex;
    align-items: center;

    min-height: 58px;
    padding: 0 18px;

    background: var(--tg-surface);

    border-bottom: 1px solid var(--tg-border);
}

.telegram_chat_header > div {
    min-width: 0;
}

.telegram_chat_title {
    overflow: hidden;

    font-size: 15px;
    font-weight: 600;

    color: var(--tg-text);

    text-overflow: ellipsis;
    white-space: nowrap;
}

.telegram_chat_subtitle {
    margin-top: 3px;

    overflow: hidden;

    font-size: 13px;

    color: var(--tg-text-secondary);

    text-overflow: ellipsis;
    white-space: nowrap;
}

.telegram_back_button {
    display: none;

    flex: 0 0 auto;

    width: 38px;
    height: 38px;

    margin-right: 10px;
    padding: 0;

    background: transparent;

    border: 0;
    border-radius: 50%;

    font-size: 24px;
    line-height: 1;

    color: var(--tg-accent-text);

    cursor: pointer;

    transition:
        background-color 0.15s ease,
        color 0.15s ease;
}

.telegram_back_button:hover {
    background: var(--tg-surface-hover);
    color: var(--tg-accent-text-hover);
}

/* Область сообщений. */

.telegram_messages {
    flex: 1 1 auto;

    min-width: 0;
    min-height: 0;

    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 18px;

    overflow-y: auto;
    overflow-x: hidden;

    background: var(--tg-bg);

    overscroll-behavior: contain;

    scrollbar-gutter: stable;
}

.telegram_message {
    position: relative;

    flex: 0 0 auto;

    align-self: flex-start;

    width: fit-content;
    max-width: min(620px, 75%);

    padding: 9px 12px 7px;

    background: var(--tg-message-bg);

    border: 1px solid var(--tg-border-light);
    border-radius: 4px 14px 14px 14px;

    box-shadow: var(--tg-shadow);
}

.telegram_message_sender {
    overflow-wrap: anywhere;
    margin-bottom: 4px;
    font-size: 12px;
    font-weight: 600;
    color: var(--tg-accent-text);
}

.telegram_message_text {
    font-size: 14px;
    line-height: 1.4;

    color: var(--tg-text);

    white-space: pre-wrap;

    overflow-wrap: anywhere;
    word-break: break-word;
}

.telegram_message_time {
    margin-top: 4px;

    font-size: 10px;

    color: var(--tg-text-muted);

    text-align: right;

    user-select: none;
}

.telegram_chats::-webkit-scrollbar,
.telegram_messages::-webkit-scrollbar {
    width: 6px;
}

.telegram_chats::-webkit-scrollbar-track,
.telegram_messages::-webkit-scrollbar-track {
    background: transparent;
}

.telegram_chats::-webkit-scrollbar-thumb,
.telegram_messages::-webkit-scrollbar-thumb {
    background: var(--tg-scrollbar);

    border-radius: 10px;
}

.telegram_chats::-webkit-scrollbar-thumb:hover,
.telegram_messages::-webkit-scrollbar-thumb:hover {
    background: var(--tg-scrollbar-hover);
}

.telegram_chats,
.telegram_messages {
    scrollbar-width: thin;
    scrollbar-color:
        var(--tg-scrollbar)
        transparent;
}

/* Фотографии в сообщениях. */

.telegram_message_photos {
    display: flex;
    flex-wrap: wrap;

    gap: 4px;

    margin-top: 6px;
    margin-bottom: 6px;

    overflow: hidden;

    border-radius: 10px;
}

/* Одиночная фотография сохраняет пропорции и ограниченную ширину. */

.telegram_message_photos:not(
    .telegram_message_photos_album
) {
    width: fit-content;
    max-width: 420px;
}

.telegram_message_photo {
    display: block;

    width: auto;
    height: auto;

    max-width: 420px;
    max-height: 380px;

    object-fit: contain;

    border-radius: 8px;
}

/* Альбом шире текстового сообщения, но остаётся внутри чата. */

.telegram_message_album {
    width: min(480px, 75%);
    max-width: 480px;
}

.telegram_message_photos_album {
    display: grid;

    grid-template-columns:
        repeat(2, minmax(0, 1fr));

    width: 100%;

    gap: 3px;

    overflow: hidden;

    border-radius: 10px;
}

.telegram_message_photos_album
.telegram_message_photo {
    width: 100%;
    height: 200px;

    max-width: none;
    max-height: none;

    object-fit: cover;

    border-radius: 3px;
}

/* Цитата отделена от текста более мягким фоном. */

.telegram_message_reply {
    margin: 4px 0 8px;
    padding: 7px 10px;

    background: var(--tg-reply-bg);

    border-left: 3px solid var(--tg-reply-accent);
    border-radius: 4px;
}

.telegram_message_reply_sender {
    margin-bottom: 4px;

    font-size: 13px;
    font-weight: 600;

    color: var(--tg-reply-accent);
}

.telegram_message_reply_content {
    display: flex;
    align-items: center;

    gap: 8px;

    min-width: 0;
}

.telegram_message_reply_photo {
    width: 42px;
    height: 42px;

    flex: 0 0 42px;

    object-fit: cover;

    border-radius: 4px;
}

.telegram_message_reply_text {
    min-width: 0;

    font-size: 13px;
    line-height: 1.35;

    color: var(--tg-text-secondary);

    overflow: hidden;

    white-space: nowrap;
    text-overflow: ellipsis;
}

/* Раскладка альбома зависит от количества фотографий. */

.telegram_message_photos_2 {
    grid-template-columns:
        repeat(2, minmax(0, 1fr));
}

.telegram_message_photos_2
.telegram_message_photo {
    height: 220px;
}

.telegram_message_photos_3 {
    grid-template-columns:
        repeat(2, minmax(0, 1fr));
}

.telegram_message_photos_3
.telegram_message_photo:first-child {
    grid-column: 1 / -1;

    height: 210px;
}

.telegram_message_photos_3
.telegram_message_photo:not(:first-child) {
    height: 145px;
}

.telegram_message_photos_4 {
    grid-template-columns:
        repeat(2, minmax(0, 1fr));
}

.telegram_message_photos_4
.telegram_message_photo {
    height: 165px;
}

.telegram_message_photos_many {
    grid-template-columns:
        repeat(2, minmax(0, 1fr));
}

.telegram_message_photos_many
.telegram_message_photo {
    height: 145px;
}
/* Панель отправки сообщения. */

.telegram_composer {
    flex: 0 0 auto;

    display: flex;
    align-items: flex-end;

    gap: 8px;

    padding: 10px 14px;

    background: var(--tg-surface);

    border-top: 1px solid var(--tg-border);
}

.telegram_composer_attach {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    cursor: pointer;
    color: var(--tg-text);
}

.telegram_composer_photo_preview {
    position: relative;
    flex: 0 0 44px;
    width: 44px;
    height: 44px;
}

.telegram_composer_photo_preview[hidden] {
    display: none;
}

.telegram_composer_photo_preview img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 6px;
}

.telegram_composer_photo_preview button {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 20px;
    height: 20px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: var(--tg-surface);
    color: var(--tg-text);
    cursor: pointer;
}

.telegram_composer_input {
    flex: 1 1 0;
    text-transform: none;
    min-width: 0;
    min-height: 38px;
    max-height: 120px;

    padding: 9px 12px;

    background: var(--tg-surface-secondary);

    border: 1px solid var(--tg-border);
    border-radius: 10px;

    outline: none;

    resize: none;

    overflow-y: auto;

    font: inherit;
    font-size: 14px;
    line-height: 18px;

    color: var(--tg-text);

    transition:
        border-color 0.15s ease,
        background-color 0.15s ease;
}

.telegram_composer_input::placeholder {
    color: var(--tg-text-muted);
}

.telegram_composer_input:focus {
    background: var(--tg-surface);

    border-color: var(--tg-accent);
}

.telegram_composer_input:disabled {
    cursor: not-allowed;
    opacity: 0.6;
}

.telegram_composer_send {
    flex: 0 0 38px;
    flex-shrink: 0;

    display: flex;
    align-items: center;
    justify-content: center;

    width: 38px;
    height: 38px;

    padding: 0;

    background: var(--tg-accent);

    border: 0;
    border-radius: 50%;

    font-size: 17px;
    line-height: 1;

    color: var(--tg-control-foreground);

    cursor: pointer;

    transition:
        background-color 0.15s ease,
        transform 0.1s ease,
        opacity 0.15s ease;
}

.telegram_composer_send:hover:not(:disabled) {
    background: var(--tg-accent-hover);
}

.telegram_composer_send:active:not(:disabled) {
    transform: scale(0.94);
}

.telegram_composer_send:disabled {
    cursor: not-allowed;
    opacity: 0.45;
}

/* Адаптация для планшета. */

@media (max-width: 900px) {

    .telegram {
        grid-template-columns:
            240px minmax(0, 1fr);

        height: calc(100vh - 170px);
    }

    .telegram_sidebar_header {
        padding: 0 16px;
    }

    .telegram_chat_item {
        padding: 13px 16px;
    }

    .telegram_chat_item.active {
        padding-left: 13px;
    }

    .telegram_chat_header {
        padding: 0 18px;
    }

    .telegram_messages {
        padding: 18px;
    }

    .telegram_message {
        max-width: 90%;
    }
}

/* Адаптация для телефона. */

@media (max-width: 650px) {

    body:has(.telegram) .main {
        position: fixed;
        top: 64px;
        left: 0;
        right: 0;
        height: calc(100dvh - 64px);
        margin: 0;
        overflow: hidden;
    }

    body:has(.telegram) .main_container,
    body:has(.telegram) .telegram_page {
        height: 100%;
        margin: 0;
        padding: 0;
    }

    body:has(.telegram) .telegram {
        height: 100%;
        min-height: 0;
    }

    body:has(.telegram) .telegram_composer {
        padding-bottom: max(8px, env(safe-area-inset-bottom));
    }

    .main_container {
        width: 100%;
        max-width: none;
        margin: 0;
        padding: 0;
    }

    .telegram_page {
        width: 100%;
        margin: 0;
        padding: 0;
    }

    .telegram {
        display: block;

        width: 100%;

        /* dvh учитывает изменение высоты области браузера на телефоне. */

        height: calc(100dvh - 64px - env(safe-area-inset-bottom));
        min-height: 0;

        border: 0;
        border-radius: 0;

        overflow: hidden;
    }

    .telegram_sidebar {
        display: flex;

        width: 100%;
        height: 100%;

        border-right: 0;
    }

    .telegram_sidebar_header {
        min-height: 56px;

        padding: 0 16px;
    }

    .telegram_sidebar_header h2 {
        font-size: 18px;
    }

    .telegram_chat_item {
        padding: 13px 16px;
    }

    .telegram_chat_item.active {
        padding-left: 13px;
    }

    .telegram_chat_item_title {
        margin: 0;

        font-size: 14px;
        line-height: 1.3;
    }

    .telegram_chat {
        display: none;

        width: 100%;
        height: 100%;

        min-height: 0;
    }

    /* После выбора чата JavaScript добавляет класс открытия. */

    .telegram.telegram_chat_open
    .telegram_sidebar {
        display: none;
    }

    .telegram.telegram_chat_open
    .telegram_chat {
        display: flex;
    }

    .telegram_chat_header {
        min-height: 54px;

        padding: 0 10px;
    }

    .telegram_chat_title {
        font-size: 15px;
    }

    .telegram_chat_subtitle {
        font-size: 12px;
    }

    .telegram_back_button {
        display: flex;

        align-items: center;
        justify-content: center;
    }

    .telegram_messages {
        gap: 8px;

        padding: 14px;
    }

    .telegram_message {
        max-width: 95%;

        padding: 9px 12px 7px;

        border-radius:
            4px 12px 12px 12px;
    }

    .telegram_message_sender {
        font-size: 12px;
    }

    .telegram_message_text {
        font-size: 14px;
        line-height: 1.4;
    }

    .telegram_message_time {
        font-size: 10px;
    }

    .telegram_message_photos:not(
        .telegram_message_photos_album
    ) {
        width: 100%;
        max-width: 100%;
    }

    .telegram_message_photos:not(
        .telegram_message_photos_album
    )
    .telegram_message_photo {
        width: 100%;
        height: auto;

        max-width: 100%;
        max-height: 320px;

        object-fit: contain;
    }

    .telegram_message_album {
        width: 95%;
        max-width: 95%;
    }

    .telegram_message_photos_album {
        width: 100%;
    }

    .telegram_message_photos_album
    .telegram_message_photo {
        width: 100%;
        height: 140px;

        max-width: none;
        max-height: none;

        object-fit: cover;
    }

    /* Раскладка альбома на телефоне. */

    .telegram_message_photos_2
    .telegram_message_photo {
        height: 180px;
    }

    .telegram_message_photos_3
    .telegram_message_photo:first-child {
        height: 190px;
    }

    .telegram_message_photos_3
    .telegram_message_photo:not(:first-child) {
        height: 130px;
    }

    .telegram_message_photos_4
    .telegram_message_photo {
        height: 140px;
    }

    .telegram_message_photos_many
    .telegram_message_photo {
        height: 125px;
    }
        /* Компактная панель отправки на телефоне. */

    .telegram_composer {
        gap: 7px;

        padding: 8px 10px;
    }

    .telegram_composer_input {
        min-height: 36px;

        padding: 8px 10px;

        font-size: 14px;
        line-height: 18px;

        border-radius: 9px;
    }

    .telegram_composer_send {
        flex-basis: 36px;

        width: 36px;
        height: 36px;

        font-size: 16px;
    }
}

@media (max-width: 400px) {

    .telegram {
        height: calc(100dvh - 64px - env(safe-area-inset-bottom));
        min-height: 0;
    }

    .telegram_messages {
        padding: 10px;
    }

    .telegram_message {
        max-width: 98%;
    }

}

/* Переключатель регионов Telegram. */

.telegram_region_switcher {
    display: flex;
    gap: 4px;
    margin: 10px 12px;
    padding: 4px;
    border-radius: 10px;
    background: var(--tg-active-bg);
}

.telegram_region_button {
    flex: 1;
    padding: 9px 12px;
    border-radius: 7px;
    background: transparent;
    color: var(--tg-empty-text);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
}

.telegram_region_button:hover {
    background: var(--tg-tab-hover-surface);
}

.telegram_region_button.active {
    background: var(--tg-control-surface);
    color: var(--tg-tab-active-text);
    box-shadow: 0 1px 4px var(--tg-tab-shadow-color);
}
