/* Telegram Web–like dark theme */
:root {
    --tg-blue: #3390ec;
    --tg-blue-deep: #2b5278;
    --tg-green: #4dcd5e;
    --tg-bg: #0e1621;
    --tg-sidebar: #17212b;
    --tg-panel: #17212b;
    --tg-hover: #202b36;
    --tg-active: #2b5278;
    --tg-input: #242f3d;
    --tg-bubble-in: #182533;
    --tg-bubble-out: #2b5278;
    --tg-text: #f5f5f5;
    --tg-meta: #708499;
    --tg-divider: rgba(255, 255, 255, 0.06);
    --tg-shadow: rgba(0, 0, 0, 0.35);
    --font: "Roboto", "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    overflow: hidden;
    background: var(--tg-bg);
    color: var(--tg-text);
    font-family: var(--font);
    -webkit-font-smoothing: antialiased;
    -webkit-text-size-adjust: 100%;
}

.app-container {
    display: flex;
    height: 100vh;
    height: 100dvh;
    background: var(--tg-bg);
}

.loading-overlay {
    position: fixed;
    inset: 0;
    background: var(--tg-bg);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
    z-index: 1000;
    transition: opacity 0.25s ease;
}

.loading-overlay.is-hidden {
    opacity: 0;
    pointer-events: none;
}

.spinner {
    width: 36px;
    height: 36px;
    border: 3px solid rgba(51, 144, 236, 0.2);
    border-top-color: var(--tg-blue);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

.loading-hint {
    color: var(--tg-meta);
    font-size: 14px;
}

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

/* —— Sidebar (chat list) —— */
.sidebar {
    width: 420px;
    max-width: 100%;
    background: var(--tg-sidebar);
    border-right: 1px solid var(--tg-divider);
    display: flex;
    flex-direction: column;
    position: relative;
    z-index: 10;
}

.sidebar-top {
    flex-shrink: 0;
    background: var(--tg-panel);
    border-bottom: 1px solid var(--tg-divider);
}

.sidebar-toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px 8px;
}

.icon-btn {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--tg-meta);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 18px;
}

.icon-btn:hover {
    background: var(--tg-hover);
    color: var(--tg-text);
}

.search-container {
    position: relative;
    flex: 1;
}

.search-container i {
    position: absolute;
    left: 14px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--tg-meta);
    font-size: 13px;
    pointer-events: none;
}

.search-container input {
    width: 100%;
    height: 40px;
    padding: 0 14px 0 38px;
    border: none;
    border-radius: 22px;
    background: var(--tg-input);
    color: var(--tg-text);
    font-size: 14px;
    outline: none;
    font-family: inherit;
}

.search-container input::placeholder { color: var(--tg-meta); }
.search-container input:focus { box-shadow: inset 0 0 0 1px rgba(51, 144, 236, 0.45); }

.accounts-scroll-container {
    width: 100%;
    overflow-x: auto;
    padding: 2px 10px 8px;
    scrollbar-width: none;
}

.accounts-scroll-container::-webkit-scrollbar { display: none; }

.accounts-scroller {
    display: flex;
    gap: 10px;
    padding: 2px 0;
}

.account-badge {
    display: flex;
    flex-direction: column;
    align-items: center;
    min-width: 54px;
    cursor: pointer;
    padding: 4px;
    border-radius: 10px;
}

.account-badge:hover { background: var(--tg-hover); }

.account-badge-avatar,
.account-badge-avatar-ph,
.account-badge .avatar-ph {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid transparent;
}

.account-badge.active .account-badge-avatar,
.account-badge.active .avatar-ph {
    border-color: var(--tg-blue);
}

.account-badge-name {
    font-size: 11px;
    margin-top: 4px;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 58px;
    color: var(--tg-meta);
}

.account-badge.active .account-badge-name { color: var(--tg-text); }

.avatar-ph {
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: var(--tg-blue);
    color: #fff;
    font-weight: 600;
    flex-shrink: 0;
    font-size: 15px;
    text-transform: uppercase;
}

.filter-tabs {
    display: flex;
    gap: 0;
    overflow-x: auto;
    padding: 0 8px 0;
    scrollbar-width: none;
    border-bottom: 1px solid var(--tg-divider);
}

.filter-tabs::-webkit-scrollbar { display: none; }

.filter-tab {
    flex: 0 0 auto;
    padding: 10px 12px;
    border: none;
    background: transparent;
    color: var(--tg-meta);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    font-family: inherit;
    position: relative;
    white-space: nowrap;
}

.filter-tab:hover { color: var(--tg-text); }

.filter-tab.active {
    color: var(--tg-blue);
}

.filter-tab.active::after {
    content: "";
    position: absolute;
    left: 8px;
    right: 8px;
    bottom: 0;
    height: 3px;
    border-radius: 3px 3px 0 0;
    background: var(--tg-blue);
}

.sidebar-footer {
    padding: 8px 12px;
    border-top: 1px solid var(--tg-divider);
}

.cabinet-btn {
    width: 100%;
    padding: 10px 12px;
    border: none;
    border-radius: 10px;
    background: var(--tg-input);
    color: var(--tg-text);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    font-family: inherit;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.cabinet-btn:hover { background: var(--tg-hover); }
body.panel-embed .cabinet-btn,
body.panel-embed .sidebar-footer { display: none; }

.dialogs-list {
    overflow-y: auto;
    flex: 1;
    -webkit-overflow-scrolling: touch;
}

.dialog-empty,
.dialogs-loading {
    padding: 40px 20px;
    color: var(--tg-meta);
    text-align: center;
    line-height: 1.5;
    font-size: 14px;
}

.dialog-item {
    padding: 9px 12px;
    display: flex;
    align-items: center;
    cursor: pointer;
    gap: 10px;
    position: relative;
    transition: background 0.12s;
}

.dialog-item:hover { background: var(--tg-hover); }

.dialog-item.active {
    background: var(--tg-active);
}

.dialog-avatar {
    width: 54px;
    height: 54px;
    min-width: 54px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.dialog-avatar img,
.dialog-avatar .avatar-ph {
    width: 54px;
    height: 54px;
    border-radius: 50%;
    object-fit: cover;
    font-size: 18px;
}

.dialog-info {
    flex: 1;
    overflow: hidden;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.dialog-row-top,
.dialog-row-bottom {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
    min-width: 0;
}

.dialog-name {
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 15px;
    color: var(--tg-text);
    flex: 1;
    min-width: 0;
}

.dialog-time {
    font-size: 12px;
    color: var(--tg-meta);
    flex-shrink: 0;
}

.dialog-item.active .dialog-time {
    color: rgba(255, 255, 255, 0.65);
}

.dialog-last-message {
    font-size: 14px;
    color: var(--tg-meta);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
}

.dialog-item.active .dialog-last-message {
    color: rgba(255, 255, 255, 0.75);
}

.dialog-meta {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

.dialog-kind {
    font-size: 11px;
    color: var(--tg-meta);
    opacity: 0.85;
}

.unread-badge {
    background: var(--tg-green);
    color: #fff;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 10px;
    font-size: 12px;
    font-weight: 600;
    display: none;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

.dialog-item.active .unread-badge {
    background: #fff;
    color: var(--tg-blue-deep);
}

#accountSelector { display: none; }

/* —— Chat pane —— */
.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--tg-bg);
    position: relative;
    min-width: 0;
}

.chat-empty-state {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background:
        radial-gradient(ellipse at 30% 20%, rgba(51, 144, 236, 0.08), transparent 45%),
        var(--tg-bg);
}

.chat-empty-card {
    text-align: center;
    padding: 28px 32px;
    border-radius: 16px;
    background: rgba(23, 33, 43, 0.85);
    box-shadow: 0 8px 32px var(--tg-shadow);
    max-width: 320px;
}

.chat-empty-icon {
    width: 64px;
    height: 64px;
    margin: 0 auto 14px;
    border-radius: 50%;
    background: var(--tg-blue);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 30px;
}

.chat-empty-title {
    font-size: 18px;
    font-weight: 500;
    margin-bottom: 6px;
}

.chat-empty-text {
    font-size: 14px;
    color: var(--tg-meta);
    line-height: 1.4;
}

.chat-active {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.chat-active[hidden] { display: none !important; }

.chat-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    background: var(--tg-panel);
    border-bottom: 1px solid var(--tg-divider);
    min-height: 56px;
    flex-shrink: 0;
    z-index: 2;
}

.back-button {
    display: none;
    width: 40px;
    height: 40px;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: none;
    background: transparent;
    color: var(--tg-meta);
    cursor: pointer;
    font-size: 16px;
}

.back-button:hover {
    background: var(--tg-hover);
    color: var(--tg-text);
}

.chat-peer {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
    flex: 1;
}

.chat-avatar-wrap {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    overflow: hidden;
    flex-shrink: 0;
}

.chat-avatar-wrap img,
.chat-avatar-wrap .avatar-ph {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    object-fit: cover;
    font-size: 15px;
}

.chat-peer-text {
    min-width: 0;
}

.chat-peer-name {
    font-weight: 500;
    font-size: 15px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-peer-status {
    font-size: 13px;
    color: var(--tg-meta);
    margin-top: 1px;
}

.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 12px 10% 16px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    position: relative;
    z-index: 1;
    -webkit-overflow-scrolling: touch;
    background-color: var(--tg-bg);
    background-image:
        url("data:image/svg+xml,%3Csvg width='60' height='60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='%231a2736' fill-opacity='0.45'%3E%3Ccircle cx='6' cy='6' r='1.2'/%3E%3Ccircle cx='36' cy='22' r='1'/%3E%3Ccircle cx='18' cy='42' r='1.1'/%3E%3Ccircle cx='48' cy='50' r='0.9'/%3E%3C/g%3E%3C/svg%3E");
}

.message {
    max-width: min(78%, 420px);
    padding: 6px 10px 4px;
    border-radius: 12px;
    position: relative;
    word-break: break-word;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.message.incoming {
    align-self: flex-start;
    background: var(--tg-bubble-in);
    border-bottom-left-radius: 4px;
}

.message.outgoing {
    align-self: flex-end;
    background: var(--tg-bubble-out);
    border-bottom-right-radius: 4px;
}

.message-text {
    font-size: 15px;
    line-height: 1.35;
    white-space: pre-wrap;
    color: var(--tg-text);
}

.message-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    margin-top: 2px;
    float: right;
    margin-left: 10px;
    position: relative;
    top: 4px;
}

.message-time {
    font-size: 11px;
    color: var(--tg-meta);
    white-space: nowrap;
}

.message.outgoing .message-time {
    color: rgba(255, 255, 255, 0.55);
}

.message-checks {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.55);
}

.load-older-btn {
    display: block;
    margin: 4px auto 12px;
    padding: 8px 16px;
    border-radius: 18px;
    border: none;
    background: rgba(23, 33, 43, 0.9);
    color: var(--tg-blue);
    cursor: pointer;
    font-size: 13px;
    font-family: inherit;
    box-shadow: 0 2px 8px var(--tg-shadow);
}

.load-older-btn:hover { filter: brightness(1.1); }

.message-input-container {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px 10px;
    background: var(--tg-panel);
    border-top: 1px solid var(--tg-divider);
    flex-shrink: 0;
    z-index: 2;
}

#messageInput {
    flex: 1;
    height: 44px;
    padding: 0 16px;
    border-radius: 22px;
    border: none;
    background: var(--tg-input);
    color: var(--tg-text);
    font-size: 15px;
    outline: none;
    font-family: inherit;
}

#messageInput::placeholder { color: var(--tg-meta); }

.send-button {
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 50%;
    background: var(--tg-blue);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 16px;
}

.send-button:hover { filter: brightness(1.08); }

@media (max-width: 900px) {
    .messages-container { padding: 12px 16px 16px; }
}

@media (max-width: 768px) {
    .sidebar {
        width: 100%;
        position: absolute;
        inset: 0;
        z-index: 20;
    }
    .sidebar.hidden { display: none; }
    .chat-container {
        width: 100%;
    }
    .chat-empty-state { display: none; }
    .chat-active:not([hidden]) { display: flex; }
    body.chat-open .chat-empty-state { display: none; }
    .back-button { display: flex; }
    .messages-container { padding: 10px 10px 14px; }
}
