:root {
    --bg: #eeeeee;
    --panel: #ffffff;
    --line: #e2e2e2;
    --text: #1d1d1f;
    --muted: #8a8a8e;
    --accent: #2b7de9;
    --accent-soft: #e7f0fd;
    --ok: #34c759;
    --warn: #ff9500;
}
@media (prefers-color-scheme: dark) {
    :root {
        --bg: #121212;
        --panel: #1e1e1e;
        --line: #2c2c2e;
        --text: #f2f2f7;
        --muted: #8e8e93;
        --accent: #4a90f0;
        --accent-soft: #1d2a3d;
    }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
    background: var(--bg);
    color: var(--text);
    font: 15px/1.4 -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
}
[hidden] { display: none !important; }
.screen { height: 100%; }

/* ---------- вход ---------- */
#login { display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card {
    background: var(--panel);
    border-radius: 18px;
    box-shadow: 0 4px 24px rgba(0, 0, 0, .08);
    padding: 40px;
    display: flex;
    gap: 48px;
    max-width: 900px;
    width: 100%;
    align-items: center;
}
.login-text { flex: 1; }
.login-text h1 { margin: 0 0 20px; font-size: 28px; }
.login-text ol { padding-left: 20px; margin: 0 0 20px; }
.login-text li { margin-bottom: 10px; font-size: 17px; }
.hint { color: var(--muted); font-size: 14px; margin: 0; }
.login-qr { text-align: center; }
.qr-box {
    width: 264px; height: 264px;
    background: #fff;
    border-radius: 12px;
    display: flex; align-items: center; justify-content: center;
    position: relative;
    border: 1px solid var(--line);
}
.qr-box svg { width: 248px; height: 248px; display: block; }
.qr-box .qr-logo {
    position: absolute; left: 50%; top: 50%;
    width: 52px; height: 52px; margin: -26px 0 0 -26px;
    border-radius: 14px; background: #fff;
    box-shadow: 0 0 0 4px #fff;
    display: flex; align-items: center; justify-content: center;
    color: var(--accent); font-weight: 700; font-size: 22px;
}
.qr-box.expired svg { opacity: .15; }
.qr-box .refresh {
    position: absolute; inset: 0; margin: auto; width: 150px; height: 44px;
    border: 0; border-radius: 22px; background: var(--accent); color: #fff; font-size: 15px; cursor: pointer;
}
.login-state { margin-top: 12px; color: var(--muted); font-size: 14px; min-height: 20px; }
.spinner {
    width: 36px; height: 36px; border-radius: 50%;
    border: 3px solid var(--line); border-top-color: var(--accent);
    animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- основной ---------- */
#main { display: flex; }
.side {
    width: 340px; min-width: 260px;
    background: var(--panel);
    border-right: 1px solid var(--line);
    display: flex; flex-direction: column;
}
.side-head { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.me { flex: 1; min-width: 0; }
.me-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.net-state { font-size: 12px; color: var(--muted); }
.net-state.ok { color: var(--ok); }
.net-state.bad { color: var(--warn); }
.icon-btn {
    border: 0; background: none; color: var(--muted); cursor: pointer;
    width: 38px; height: 38px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--bg); color: var(--text); }
.search { padding: 10px 14px; }
.search input {
    width: 100%; height: 36px; border-radius: 18px; border: 0;
    background: var(--bg); color: var(--text); padding: 0 14px; font-size: 14px; outline: none;
}
.list { flex: 1; overflow-y: auto; }
.row { display: flex; align-items: center; gap: 12px; padding: 9px 14px; cursor: pointer; }
.row:hover { background: var(--bg); }
.row.active { background: var(--accent-soft); }
.row-text { min-width: 0; flex: 1; }
.row-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.group-title { padding: 12px 14px 4px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.empty-list { padding: 24px 14px; color: var(--muted); text-align: center; }
.avatar {
    width: 42px; height: 42px; border-radius: 50%; flex: none;
    display: flex; align-items: center; justify-content: center;
    color: #fff; font-weight: 600; font-size: 17px;
    background: var(--accent);
}

.chat { flex: 1; display: flex; min-width: 0; }
.chat-empty { margin: auto; text-align: center; color: var(--muted); }
.chat-empty .logo { font-size: 40px; font-weight: 700; color: var(--accent); opacity: .6; margin-bottom: 8px; }
.chat-view { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-head { display: flex; align-items: center; gap: 12px; padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-name { font-weight: 600; }
.chat-uid { font-size: 12px; color: var(--muted); }
.back { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; }
.soon { margin: auto; color: var(--muted); text-align: center; max-width: 320px; }

/* ---------- телефон ---------- */
@media (max-width: 720px) {
    .login-card { flex-direction: column; padding: 24px 16px; gap: 24px; }
    .side { width: 100%; border-right: 0; }
    #main.in-chat .side { display: none; }
    #main:not(.in-chat) .chat { display: none; }
    .back { display: flex; }
}

/* ---------- этапы 2–4: чаты ---------- */
.row-top { display: flex; align-items: center; gap: 8px; }
.row-top .row-name, .row-top .row-sub { flex: 1; min-width: 0; }
.row-time { font-size: 12px; color: var(--muted); flex: none; }
.badge {
    flex: none; min-width: 20px; height: 20px; border-radius: 10px; padding: 0 6px;
    background: var(--accent); color: #fff; font-size: 12px; line-height: 20px; text-align: center;
}
.chat-title { min-width: 0; }
.chat-uid.ok { color: var(--ok); }
.chat-view { position: relative; background: var(--bg); }
.messages { gap: 2px; }
.day {
    align-self: center; margin: 12px 0 8px; padding: 3px 12px; border-radius: 12px;
    background: var(--panel); color: var(--muted); font-size: 12px;
}
.load-more {
    align-self: center; border: 0; background: var(--panel); color: var(--accent);
    border-radius: 14px; padding: 5px 14px; cursor: pointer; margin-bottom: 8px;
}
.msg { display: flex; align-items: flex-end; gap: 4px; max-width: 72%; position: relative; }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; flex-direction: row-reverse; }
.bubble {
    background: var(--panel); border-radius: 14px; padding: 7px 10px 5px;
    box-shadow: 0 1px 1px rgba(0, 0, 0, .06); min-width: 64px; max-width: 100%;
    overflow-wrap: anywhere;
}
.msg.out .bubble { background: #d9eafd; }
@media (prefers-color-scheme: dark) { .msg.out .bubble { background: #22406a; } }
.msg.flash .bubble { box-shadow: 0 0 0 2px var(--accent); }
.text { white-space: pre-wrap; }
.text a { color: var(--accent); }
.text.muted { color: var(--muted); font-style: italic; }
.text.once { color: var(--accent); cursor: pointer; }
.fwd { font-size: 12px; color: var(--accent); margin-bottom: 2px; }
.meta { display: flex; justify-content: flex-end; gap: 4px; font-size: 11px; color: var(--muted); margin-top: 2px; }
.st-delivered { color: var(--accent); }
.st-failed { color: #e53935; }
.reacts { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 4px; cursor: pointer; }
.react { background: var(--bg); border-radius: 10px; padding: 0 6px; font-size: 14px; }
.retry {
    margin-top: 4px; border: 0; background: none; color: #e53935; cursor: pointer; padding: 0; font-size: 13px;
}
.more {
    opacity: 0; border: 0; background: none; color: var(--muted); cursor: pointer;
    font-size: 18px; width: 28px; height: 28px; border-radius: 50%;
}
.msg:hover .more { opacity: 1; }
.more:hover { background: var(--panel); }
.quote {
    border-left: 3px solid var(--accent); padding: 2px 8px; margin: 0 0 4px;
    background: rgba(43, 125, 233, .08); border-radius: 4px; cursor: pointer; max-width: 360px;
}
.quote-name { font-size: 12px; color: var(--accent); font-weight: 600; }
.quote-text { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.file .media { display: block; max-width: 340px; max-height: 340px; border-radius: 10px; cursor: pointer; background: #000; }
.file video.media { cursor: default; }
.file .audio { width: 260px; max-width: 100%; }
.file-card { display: flex; align-items: center; gap: 10px; cursor: pointer; padding: 2px 0; }
.file-icon { font-size: 28px; }
.file-info { min-width: 0; }
.file-name { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 240px; }
.file-size { font-size: 12px; color: var(--muted); }
.prog { height: 4px; background: var(--line); border-radius: 2px; margin-top: 6px; overflow: hidden; }
.prog-bar { height: 100%; width: 0; background: var(--accent); transition: width .2s; }

.quote-bar {
    display: flex; align-items: center; gap: 8px; padding: 8px 14px;
    background: var(--panel); border-top: 1px solid var(--line);
}
.quote-bar-text { flex: 1; min-width: 0; border-left: 3px solid var(--accent); padding-left: 8px; }
.composer-row {
    display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px;
    background: var(--panel); border-top: 1px solid var(--line);
}
#composer {
    flex: 1; resize: none; border: 0; outline: none; border-radius: 18px;
    background: var(--bg); color: var(--text); font: inherit; padding: 8px 14px; max-height: 160px; min-height: 38px;
}
.send-btn {
    flex: none; width: 38px; height: 38px; border-radius: 50%; border: 0; cursor: pointer;
    background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center;
}
.drop-hint {
    display: none; position: absolute; inset: 0; background: rgba(43, 125, 233, .12);
    border: 2px dashed var(--accent); border-radius: 12px; margin: 8px;
    align-items: center; justify-content: center; font-size: 18px; color: var(--accent); pointer-events: none;
}
.chat-view.drop .drop-hint { display: flex; }

.menu {
    position: fixed; z-index: 50; background: var(--panel); border-radius: 12px; min-width: 200px;
    box-shadow: 0 6px 28px rgba(0, 0, 0, .18); padding: 6px; display: flex; flex-direction: column;
}
.menu-reacts { display: flex; gap: 2px; padding: 2px 2px 6px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.menu-react { border: 0; background: none; font-size: 22px; cursor: pointer; border-radius: 8px; width: 36px; height: 36px; }
.menu-react:hover, .menu-react.on { background: var(--accent-soft); }
.menu-item {
    border: 0; background: none; color: var(--text); text-align: left; padding: 8px 10px;
    border-radius: 8px; cursor: pointer; font: inherit;
}
.menu-item:hover { background: var(--bg); }

.viewer {
    position: fixed; inset: 0; z-index: 60; background: rgba(0, 0, 0, .88);
    display: flex; align-items: center; justify-content: center; cursor: zoom-out;
}
.viewer img { max-width: 94vw; max-height: 94vh; }

@media (max-width: 720px) {
    .msg { max-width: 88%; }
    .more { opacity: .6; }
    .file .media { max-width: 70vw; }
}

/* ---------- звонки ---------- */
.head-btn { margin-left: auto; }
.head-btn + .head-btn { margin-left: 0; }
.chat-title { flex: 1; }
.call-mark .text { cursor: pointer; color: var(--text); }
.call-mark .text.missed { color: #e53935; }
.call-overlay {
    position: fixed; inset: 0; z-index: 70; background: #10151c; color: #fff;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
}
/* участники: 1:1 — один на весь экран; групповой — крупно говорящий + полоса остальных */
.call-grid { position: absolute; inset: 0; display: none; }
.call-overlay.with-video .call-grid { display: block; }
.tile { position: absolute; background: #1b222c; border-radius: 12px; overflow: hidden; }
.tile .tile-video { width: 100%; height: 100%; object-fit: contain; background: #000; display: none; }
.tile.has-video .tile-video { display: block; }
.tile .tile-av { position: absolute; left: 50%; top: 50%; width: 64px; height: 64px; margin: -40px 0 0 -32px; font-size: 24px; }
.tile.has-video .tile-av { display: none; }
.tile .tile-name { position: absolute; left: 8px; bottom: 6px; font-size: 13px; text-shadow: 0 1px 3px rgba(0,0,0,.8); }
.tile .tile-state { position: absolute; left: 0; right: 0; top: calc(50% + 34px); text-align: center; font-size: 12px; color: rgba(255,255,255,.7); }
.call-overlay:not(.group) .tile { inset: 0; border-radius: 0; }
.call-overlay:not(.group) .tile .tile-name { display: none; }
.call-overlay.group .call-grid { display: flex; align-items: flex-end; justify-content: center; gap: 8px; padding-bottom: 128px; }
.call-overlay.group .tile.main { position: absolute; left: 0; right: 0; top: 0; bottom: 250px; border-radius: 0; }
.call-overlay.group .tile:not(.main) { position: relative; width: 150px; height: 110px; flex: none; }
.call-overlay.group .tile:not(.main) .tile-av { width: 44px; height: 44px; margin: -30px 0 0 -22px; font-size: 18px; }
.call-overlay.group .call-local { top: 70px; bottom: auto; width: 150px; }
.call-picker { position: absolute; inset: 0; background: rgba(0,0,0,.5); display: flex; align-items: center; justify-content: center; z-index: 5; }
.call-picker-box { background: var(--panel); color: var(--text); border-radius: 14px; width: 360px; max-width: 92vw; max-height: 70vh; display: flex; flex-direction: column; overflow: hidden; }
.call-picker-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; font-weight: 600; }
.call-picker-box input { margin: 0 14px 8px; height: 36px; border-radius: 18px; border: 0; background: var(--bg); color: var(--text); padding: 0 14px; outline: none; }
.call-picker-box .list { overflow-y: auto; }
.call-info { position: relative; text-align: center; margin-bottom: 80px; }
.call-overlay.with-video .call-info { position: absolute; top: 24px; left: 0; right: 0; margin: 0; text-shadow: 0 1px 4px rgba(0,0,0,.6); }
.call-overlay.with-video .call-avatar { display: none; }
.call-avatar { width: 120px; height: 120px; font-size: 44px; margin: 0 auto 18px; }
.call-name { font-size: 26px; font-weight: 600; }
.call-state { margin-top: 6px; color: rgba(255,255,255,.75); font-size: 16px; min-height: 22px; }
.call-local {
    position: absolute; right: 20px; bottom: 120px; width: 200px; max-width: 32vw; border-radius: 12px;
    background: #000; box-shadow: 0 4px 18px rgba(0,0,0,.5); transform: scaleX(-1);
}
.call-buttons { position: absolute; bottom: 32px; left: 0; right: 0; display: flex; justify-content: center; gap: 28px; }
.call-btn {
    width: 64px; height: 64px; border-radius: 50%; border: 0; cursor: pointer; position: relative;
    background: rgba(255,255,255,.16); color: #fff; display: flex; align-items: center; justify-content: center;
}
.call-btn span {
    position: absolute; top: 70px; left: 50%; transform: translateX(-50%); white-space: nowrap;
    font-size: 12px; color: rgba(255,255,255,.8);
}
.call-btn:hover { background: rgba(255,255,255,.26); }
.call-btn.off { background: #fff; color: #10151c; }
.call-btn.red { background: #e53935; }
.call-btn.green { background: #34c759; }
.toast {
    position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); z-index: 80;
    background: rgba(30,30,30,.92); color: #fff; padding: 10px 18px; border-radius: 20px; font-size: 14px; max-width: 90vw;
}
@media (max-width: 720px) {
    .call-local { width: 110px; bottom: 130px; right: 12px; }
    .call-buttons { gap: 18px; }
}
