:root {
    --bg: #121212;
    --surface: #1c1c1e;
    --surface-variant: #2a2a2d;
    --primary: #8B5CF6;
    --bubble-out: #3a2f5c;
    --bubble-in: #2a2a2d;
    --text-primary: #ffffff;
    --text-secondary: #a0a0a5;
    --error: #e05a5a;
    --online: #4CAF50;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    background: var(--bg);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
}

#app { height: 100vh; }

.screen {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* ---------- Auth ---------- */
#screen-auth { align-items: center; justify-content: center; }

.auth-box {
    width: 320px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.app-title {
    text-align: center;
    font-weight: 600;
    margin-bottom: 8px;
}

.tabs {
    display: flex;
    gap: 6px;
    margin-bottom: 6px;
}

.tab {
    flex: 1;
    padding: 8px;
    background: var(--surface);
    border: none;
    color: var(--text-secondary);
    border-radius: 10px;
    cursor: pointer;
}

.tab.active {
    background: var(--primary);
    color: white;
}

.input {
    width: 100%;
    padding: 12px 14px;
    background: var(--surface);
    border: 1px solid #3a3a3d;
    border-radius: 18px;
    color: var(--text-primary);
    font-size: 14px;
    outline: none;
}

.input:focus { border-color: var(--primary); }

.btn {
    padding: 12px;
    border-radius: 18px;
    border: none;
    font-size: 14px;
    cursor: pointer;
}

.btn-primary { background: var(--primary); color: white; }
.btn-primary:disabled { opacity: 0.6; cursor: default; }

.error-text {
    color: var(--error);
    font-size: 13px;
    min-height: 16px;
    text-align: center;
}

/* ---------- Top bar ---------- */
.topbar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    background: var(--surface);
    flex-shrink: 0;
}

.topbar-title { font-weight: 600; font-size: 17px; }
.topbar-actions { margin-left: auto; display: flex; gap: 4px; }

.icon-btn {
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 18px;
    cursor: pointer;
    padding: 6px 10px;
    border-radius: 8px;
}
.icon-btn:hover { background: rgba(255,255,255,0.08); }

.menu-wrap { position: relative; }
.dropdown {
    position: absolute;
    right: 0;
    top: 36px;
    background: var(--surface-variant);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 4px 16px rgba(0,0,0,0.4);
    z-index: 10;
    min-width: 160px;
}
.dropdown-item {
    padding: 10px 14px;
    cursor: pointer;
    font-size: 14px;
}
.dropdown-item:hover { background: rgba(255,255,255,0.08); }

.connection-bar {
    padding: 4px 14px;
    font-size: 12px;
    color: var(--text-secondary);
    background: rgba(255,255,255,0.03);
    flex-shrink: 0;
}
.connection-bar.online { color: var(--online); }
.connection-bar.reconnecting { color: #E8A33D; }
.connection-bar.offline { color: var(--error); }

/* ---------- Chat list ---------- */
.chat-list { flex: 1; overflow-y: auto; }

.chat-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    cursor: pointer;
}
.chat-row:hover { background: rgba(255,255,255,0.04); }

.avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    flex-shrink: 0;
    overflow: hidden;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.small { width: 32px; height: 32px; font-size: 13px; }

.chat-row-info { flex: 1; min-width: 0; }
.chat-row-name { font-weight: 500; }
.chat-row-preview {
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.unread-badge {
    background: var(--primary);
    color: white;
    font-size: 12px;
    font-weight: 700;
    border-radius: 11px;
    min-width: 22px;
    height: 22px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 6px;
}

/* ---------- Chat screen ---------- */
.chat-header-info { display: flex; flex-direction: column; }
.chat-status { font-size: 12px; color: var(--text-secondary); }

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 8px 10px;
}
#messages {
    display: flex;
    flex-direction: column;
    min-height: 100%;
    justify-content: flex-end;
}

.date-header {
    align-self: center;
    background: var(--surface-variant);
    color: var(--text-secondary);
    font-size: 12px;
    padding: 4px 12px;
    border-radius: 12px;
    margin: 10px 0;
}

.msg-row {
    display: flex;
    margin: 3px 0;
}
.msg-row.own { justify-content: flex-end; }

.bubble {
    max-width: 70%;
    padding: 8px 10px;
    border-radius: 14px;
    font-size: 14px;
    line-height: 1.35;
    word-wrap: break-word;
    position: relative;
}
.bubble.in { background: var(--bubble-in); border-bottom-left-radius: 4px; }
.bubble.out { background: var(--bubble-out); border-bottom-right-radius: 4px; }

.bubble-meta {
    font-size: 11px;
    color: var(--text-secondary);
    margin-top: 3px;
    text-align: right;
    display: flex;
    justify-content: flex-end;
    gap: 4px;
    align-items: center;
}
.bubble-meta .edited { opacity: 0.7; }
.ticks { color: var(--text-secondary); }
.ticks.read { color: #4FA8F5; }

.typing-indicator {
    padding: 4px 14px;
    font-size: 12px;
    color: var(--primary);
    flex-shrink: 0;
}

.compose-bar {
    display: flex;
    gap: 8px;
    padding: 10px 12px;
    background: var(--surface);
    flex-shrink: 0;
    align-items: center;
}
.compose-input { flex: 1; border-radius: 22px; }
.send-btn { background: var(--primary); border-radius: 50%; width: 40px; height: 40px; }

.btn-secondary { background: var(--surface-variant); color: var(--text-primary); }
.btn-danger { background: rgba(224, 90, 90, 0.15); color: var(--error); }

/* ---------- Поиск по чату ---------- */
.chat-search-bar {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 10px;
    background: var(--surface);
    flex-shrink: 0;
}
.chat-search-bar .input { padding: 8px 12px; }
.chat-search-count {
    font-size: 12px;
    color: var(--text-secondary);
    white-space: nowrap;
    min-width: 40px;
    text-align: center;
}

.top-loader {
    text-align: center;
    font-size: 12px;
    color: var(--text-secondary);
    padding: 10px 0;
}

/* ---------- Медиа в сообщениях ---------- */
.bubble-media {
    border-radius: 10px;
    overflow: hidden;
    margin-bottom: 4px;
    max-width: 260px;
    cursor: pointer;
    position: relative;
    background: #000;
}
.bubble-media img, .bubble-media video {
    display: block;
    width: 100%;
    max-height: 320px;
    object-fit: cover;
}
.bubble-media .gif-badge {
    position: absolute;
    bottom: 6px;
    left: 6px;
    background: rgba(0,0,0,0.6);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    padding: 2px 6px;
    border-radius: 4px;
    letter-spacing: 0.5px;
}
.bubble-audio {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 220px;
}
.bubble-audio audio { flex: 1; height: 32px; }
.bubble.has-media { padding: 6px; }
.bubble.has-media .bubble-text { padding: 2px 6px; }
.bubble.has-media .bubble-meta { padding: 0 6px 2px; }
.bubble-caption-empty { display: none; }

/* ---------- Reply ---------- */
.bubble-reply {
    display: flex;
    gap: 6px;
    padding: 5px 8px;
    margin-bottom: 5px;
    border-radius: 8px;
    background: rgba(255,255,255,0.06);
    cursor: pointer;
    font-size: 12px;
}
.bubble-reply-bar { width: 2px; background: var(--primary); border-radius: 1px; flex-shrink: 0; }
.bubble-reply-body { min-width: 0; }
.bubble-reply-name { color: var(--primary); font-weight: 600; }
.bubble-reply-text {
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.reply-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--surface);
    flex-shrink: 0;
    border-top: 1px solid #2a2a2d;
}
.reply-bar-line { width: 3px; align-self: stretch; background: var(--primary); border-radius: 2px; }
.reply-bar-body { flex: 1; min-width: 0; }
.reply-bar-name { font-size: 12px; color: var(--primary); font-weight: 600; }
.reply-bar-text { font-size: 12px; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- Вложение перед отправкой ---------- */
.attach-preview {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: var(--surface);
    flex-shrink: 0;
    border-top: 1px solid #2a2a2d;
}
.attach-preview-thumb {
    width: 44px;
    height: 44px;
    border-radius: 8px;
    background: var(--surface-variant);
    background-size: cover;
    background-position: center;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
}
.attach-preview-info { flex: 1; min-width: 0; }
.attach-preview-name {
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.attach-progress {
    height: 3px;
    background: var(--surface-variant);
    border-radius: 2px;
    margin-top: 5px;
    overflow: hidden;
    display: none;
}
.attach-progress.active { display: block; }
.attach-progress-fill { height: 100%; width: 0%; background: var(--primary); transition: width 0.15s; }

.btn-record.recording { color: var(--error); animation: pulse 1s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.4; } }

.flash-highlight .bubble { animation: flash-bubble 1.2s ease-out; }
@keyframes flash-bubble {
    0% { box-shadow: 0 0 0 2px var(--primary); }
    100% { box-shadow: 0 0 0 0 rgba(139,92,246,0); }
}

/* ---------- Highlight поиска ---------- */
mark.search-hit { background: rgba(139, 92, 246, 0.45); color: inherit; border-radius: 2px; }
mark.search-hit.current { background: var(--primary); color: #fff; }

/* ---------- Медиа и файлы ---------- */
.media-tabs {
    display: flex;
    overflow-x: auto;
    gap: 4px;
    padding: 8px 10px;
    background: var(--surface);
    flex-shrink: 0;
}
.media-tab {
    flex-shrink: 0;
    padding: 7px 14px;
    background: var(--surface-variant);
    border: none;
    color: var(--text-secondary);
    border-radius: 14px;
    font-size: 13px;
    cursor: pointer;
    white-space: nowrap;
}
.media-tab.active { background: var(--primary); color: #fff; }

.media-grid {
    flex: 1;
    overflow-y: auto;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 2px;
    padding: 2px;
    align-content: start;
}
.media-grid.links { display: flex; flex-direction: column; gap: 0; padding: 0; }

.media-cell {
    aspect-ratio: 1;
    background: var(--surface-variant);
    cursor: pointer;
    overflow: hidden;
    position: relative;
}
.media-cell img, .media-cell video { width: 100%; height: 100%; object-fit: cover; }
.media-cell .gif-badge {
    position: absolute; bottom: 4px; left: 4px;
    background: rgba(0,0,0,0.6); color: #fff; font-size: 9px; font-weight: 700;
    padding: 1px 5px; border-radius: 4px;
}
.media-cell.audio-cell {
    aspect-ratio: auto;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    grid-column: 1 / -1;
}

.link-row {
    padding: 12px 14px;
    border-bottom: 1px solid var(--surface-variant);
    cursor: pointer;
    font-size: 13px;
    color: var(--primary);
    word-break: break-all;
}

.media-empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    font-size: 14px;
}

/* ---------- Профиль ---------- */
.profile-body {
    flex: 1;
    overflow-y: auto;
    padding: 24px 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.profile-avatar-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
}
.profile-avatar { width: 96px; height: 96px; font-size: 32px; }
.profile-label { font-size: 12px; color: var(--text-secondary); margin-top: 6px; }
.profile-saved-hint { font-size: 12px; color: var(--online); min-height: 16px; text-align: center; }

/* ---------- Настройки ---------- */
.settings-body {
    flex: 1;
    overflow-y: auto;
    padding: 10px 16px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.settings-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 16px 4px;
    border-bottom: 1px solid var(--surface-variant);
}
.settings-row-column { flex-direction: column; align-items: stretch; }
.settings-row-title { font-size: 14px; font-weight: 500; }
.settings-row-hint { font-size: 12px; color: var(--text-secondary); margin-top: 3px; line-height: 1.4; }
.settings-row .btn { padding: 8px 14px; font-size: 13px; flex-shrink: 0; }
.settings-row-column .btn { margin-top: 10px; align-self: flex-start; }
.settings-about { text-align: center; color: var(--text-secondary); font-size: 12px; padding: 20px 0; }

.switch { position: relative; width: 40px; height: 22px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch-slider {
    position: absolute; inset: 0; background: var(--primary);
    border-radius: 22px; opacity: 0.5;
}
.switch-slider::before {
    content: ''; position: absolute; width: 16px; height: 16px; left: 20px; top: 3px;
    background: #fff; border-radius: 50%;
}

/* ---------- Контекстное меню ---------- */
.ctx-menu-backdrop {
    position: fixed; inset: 0; z-index: 40; background: transparent;
}
.ctx-menu {
    position: fixed;
    z-index: 41;
    background: var(--surface-variant);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 8px 24px rgba(0,0,0,0.5);
    min-width: 190px;
}
.ctx-menu-item {
    padding: 12px 16px;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
}
.ctx-menu-item:hover { background: rgba(255,255,255,0.08); }
.ctx-menu-item.danger { color: var(--error); }

/* ---------- Лайтбокс ---------- */
.lightbox {
    position: fixed; inset: 0; z-index: 50;
    background: rgba(0,0,0,0.92);
    display: flex; align-items: center; justify-content: center;
}
.lightbox-content img, .lightbox-content video {
    max-width: 92vw; max-height: 88vh; display: block; border-radius: 4px;
}
.lightbox-close { position: absolute; top: 14px; right: 14px; font-size: 22px; z-index: 51; }

/* ---------- Confirm / edit диалоги ---------- */
.confirm-overlay {
    position: fixed; inset: 0; z-index: 55;
    background: rgba(0,0,0,0.55);
    display: flex; align-items: center; justify-content: center;
    padding: 20px;
}
.confirm-box {
    background: var(--surface-variant);
    border-radius: 14px;
    padding: 18px;
    width: 100%;
    max-width: 320px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.confirm-message { font-size: 14px; line-height: 1.4; }
.confirm-actions { display: flex; gap: 8px; justify-content: flex-end; }
.confirm-actions .btn { padding: 9px 16px; font-size: 13px; }
.edit-textarea {
    min-height: 80px;
    resize: vertical;
    font-family: inherit;
    border-radius: 10px;
}

/* ---------- Toasts ---------- */
.toast-host {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 60;
    align-items: center;
}
.toast {
    background: var(--surface-variant);
    color: var(--text-primary);
    padding: 10px 16px;
    border-radius: 10px;
    font-size: 13px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.4);
    animation: toast-in 0.15s ease-out;
    max-width: 320px;
    text-align: center;
}
.toast.toast-error { color: var(--error); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ---------- Misc ---------- */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-thumb { background: #3a3a3d; border-radius: 3px; }
