:root {
    color-scheme: light;
    --vk-blue: #447bba;
    --vk-blue-strong: #2d6fb7;
    --vk-blue-soft: #e7f0fa;
    --page: #edeef0;
    --surface: #fff;
    --surface-soft: #f5f6f8;
    --border: #dce1e6;
    --border-soft: #e9edf1;
    --text: #1d2733;
    --muted: #6d7885;
    --subtle: #99a2ad;
    --hover: #f0f2f5;
    --selected: #e6eff9;
    --bubble-out: #dcecff;
    --system: #f0f2f5;
    --danger: #d94d4d;
    --success: #50a96b;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { width: 100%; height: 100%; }
body { margin: 0; overflow: hidden; color: var(--text); background: var(--page); font-size: 14px; }
button, input, select { font: inherit; }
button, select { cursor: pointer; }
input[type="search"] { appearance: none; -webkit-appearance: none; }
input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-decoration { display: none; width: 0; height: 0; -webkit-appearance: none; }
input[type="search"]::-ms-clear,
input[type="search"]::-ms-reveal { display: none; width: 0; height: 0; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

.auth-screen { min-height: 100%; display: grid; place-items: center; padding: 24px; background: linear-gradient(145deg, #e8eef5, #f4f5f7); }
.auth-card { width: min(390px, 100%); padding: 32px; border: 1px solid var(--border); border-radius: 16px; background: var(--surface); box-shadow: 0 10px 35px rgba(40, 65, 90, .12); }
.auth-card h1 { margin: 18px 0 7px; font-size: 24px; }
.auth-card > p { margin: 0 0 24px; color: var(--muted); line-height: 1.45; }
.auth-label { display: block; margin-bottom: 7px; color: var(--muted); font-size: 12px; font-weight: 650; }
.auth-input-row { height: 42px; display: flex; border: 1px solid var(--border); border-radius: 9px; background: #fff; }
.auth-input-row:focus-within { border-color: #8db5dd; box-shadow: 0 0 0 3px rgba(68, 123, 186, .11); }
.auth-input-row input { min-width: 0; flex: 1; padding: 0 12px; border: 0; outline: 0; background: transparent; }
.icon-button { width: 42px; border: 0; color: var(--muted); background: transparent; }
.primary-button { width: 100%; height: 42px; margin-top: 14px; border: 0; border-radius: 9px; color: #fff; background: var(--vk-blue-strong); font-weight: 650; }
.primary-button:disabled { opacity: .6; cursor: default; }
.auth-error { margin-top: 12px; color: var(--danger); font-size: 13px; }

.app { height: 100%; }
.app-header { height: 58px; display: flex; align-items: center; gap: 11px; padding: 0 16px; color: #fff; background: var(--vk-blue); box-shadow: 0 1px 0 rgba(0,0,0,.12); }
.brand-mark { width: 34px; height: 34px; display: grid; place-items: center; flex: 0 0 auto; border-radius: 10px; color: var(--vk-blue); background: #fff; font-size: 13px; font-weight: 800; }
.brand-mark-large { width: 45px; height: 45px; border: 1px solid #c9daeb; color: #fff; background: var(--vk-blue); font-size: 15px; }
.brand-copy { min-width: 0; }
.brand-copy h1 { margin: 0; font-size: 16px; font-weight: 650; }
.brand-copy p { margin: 2px 0 0; overflow: hidden; color: rgba(255,255,255,.76); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.sync-status { min-width: 0; display: flex; align-items: center; gap: 7px; margin-left: auto; color: rgba(255,255,255,.86); font-size: 12px; }
.sync-status span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status-dot { width: 8px; height: 8px; flex: 0 0 auto; border-radius: 50%; background: #78d990; }
.sync-status.is-working .status-dot { background: #ffd36b; animation: pulse 1.1s infinite; }
.sync-status.is-error .status-dot { background: #ff8686; }
.header-button { height: 30px; padding: 0 10px; border: 1px solid rgba(255,255,255,.3); border-radius: 7px; color: #fff; background: rgba(255,255,255,.08); font-size: 12px; }
.header-button:hover { background: rgba(255,255,255,.16); }

.workspace { height: calc(100vh - 58px); display: grid; grid-template-columns: minmax(270px, 330px) minmax(280px, 350px) minmax(410px, 1fr); gap: 1px; background: var(--border); }
.panel { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--surface); }
.panel-heading { min-height: 64px; display: flex; align-items: center; gap: 10px; padding: 12px 14px 10px; border-bottom: 1px solid var(--border-soft); }
.heading-grow { min-width: 0; flex: 1; }
.panel-heading h2 { margin: 1px 0 0; font-size: 17px; font-weight: 650; }
.eyebrow { margin: 0; color: var(--muted); font-size: 10px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
.count-pill { min-width: 26px; margin-left: auto; padding: 3px 8px; border-radius: 999px; color: var(--muted); background: var(--surface-soft); font-size: 12px; text-align: center; }
.back-button { width: 34px; height: 34px; display: none; flex: 0 0 auto; align-items: center; justify-content: center; padding: 0 1px 3px 0; border: 0; border-radius: 50%; color: var(--vk-blue-strong); background: transparent; font-size: 29px; line-height: 1; }
.back-button:hover { background: var(--hover); }
.panel-tools { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; padding: 9px 11px; border-bottom: 1px solid var(--border-soft); }
.search-field, .chat-search-field { min-width: 0; height: 34px; display: flex; align-items: center; gap: 7px; padding: 0 7px 0 10px; border: 1px solid transparent; border-radius: 8px; background: var(--surface-soft); }
.search-field:focus-within, .chat-search-field:focus-within { border-color: #abc6e4; background: #fff; }
.search-icon { width: 13px; height: 13px; position: relative; flex: 0 0 auto; border: 1.6px solid var(--subtle); border-radius: 50%; }
.search-icon::after { content: ""; width: 5px; height: 1.6px; position: absolute; right: -4px; bottom: -2px; border-radius: 2px; background: var(--subtle); transform: rotate(45deg); }
.search-field input, .chat-search-field input { min-width: 0; flex: 1; border: 0; outline: 0; color: var(--text); background: transparent; }
.clear-search { width: 22px; height: 22px; display: none; padding: 0 0 2px; border: 0; border-radius: 50%; color: #fff; background: #aeb7c1; font-size: 17px; line-height: 1; }
.search-field:has(input:not(:placeholder-shown)) .clear-search,
.chat-search-field:has(input:not(:placeholder-shown)) .clear-search { display: block; }
.sort-field select { height: 34px; max-width: 145px; padding: 0 26px 0 9px; border: 1px solid var(--border); border-radius: 8px; color: var(--muted); background: #fff; }
.list-scroll { min-height: 0; flex: 1; overflow-y: auto; overscroll-behavior: contain; }
.entity-list { padding: 4px 0; }
.entity-item { width: 100%; min-height: 72px; display: grid; grid-template-columns: auto minmax(0,1fr) auto; align-items: center; gap: 10px; padding: 8px 11px; border: 0; color: inherit; background: transparent; text-align: left; }
.entity-item:hover { background: var(--hover); }
.entity-item.is-selected { background: var(--selected); }
.avatar { position: relative; display: grid; place-items: center; flex: 0 0 auto; overflow: hidden; border-radius: 50%; color: #fff; background: linear-gradient(145deg, #83a9cf, #527daa); font-weight: 700; }
.avatar img { width: 100%; height: 100%; position: absolute; inset: 0; object-fit: cover; }
.avatar-small { width: 42px; height: 42px; }
.avatar-medium { width: 40px; height: 40px; }
.avatar-system { background: linear-gradient(145deg, #607f9e, #385876); }
.entity-copy { min-width: 0; }
.entity-title-row { display: flex; align-items: baseline; gap: 5px; }
.entity-title { min-width: 0; flex: 1; overflow: hidden; font-weight: 650; text-overflow: ellipsis; white-space: nowrap; }
.entity-time { flex: 0 0 auto; color: var(--subtle); font-size: 11px; }
.entity-id, .entity-preview { overflow: hidden; color: var(--muted); font-size: 12px; line-height: 17px; text-overflow: ellipsis; white-space: nowrap; }
.entity-preview-row { display: flex; gap: 4px; }
.entity-preview mark { padding: 0; color: inherit; background: #ffeb94; }
.entity-metrics { align-self: end; display: flex; flex-direction: column; align-items: flex-end; gap: 3px; color: var(--subtle); font-size: 10px; }
.message-badge { min-width: 23px; padding: 2px 6px; border-radius: 10px; color: var(--vk-blue-strong); background: var(--vk-blue-soft); font-size: 11px; text-align: center; }
.empty-list, .inline-loader { padding: 32px 16px; color: var(--muted); text-align: center; }
.inline-loader, .older-loader { display: flex; align-items: center; justify-content: center; gap: 8px; }
.scroll-sentinel { height: 2px; }
.selected-card { min-height: 58px; display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-bottom: 1px solid var(--border-soft); }
.selected-card strong, .selected-card span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.selected-card span { margin-top: 2px; color: var(--muted); font-size: 12px; }
.muted-card { color: var(--muted); }

.chat-panel { position: relative; }
.chat-welcome { max-width: 330px; margin: auto; padding: 30px; color: var(--muted); text-align: center; }
.chat-welcome h2 { margin: 12px 0 6px; color: var(--text); font-size: 19px; }
.chat-welcome p { margin: 0; line-height: 1.5; }
.welcome-icon { font-size: 38px; filter: grayscale(.2); }
.chat-view { min-height: 0; display: flex; flex: 1; flex-direction: column; }
.chat-header { min-height: 64px; display: flex; align-items: center; gap: 10px; padding: 9px 14px; border-bottom: 1px solid var(--border-soft); }
.chat-heading { min-width: 0; flex: 1; }
.chat-heading h2 { margin: 0; overflow: hidden; font-size: 16px; text-overflow: ellipsis; white-space: nowrap; }
.chat-heading p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.chat-search-field { width: min(230px, 34%); }
.message-search-status { padding: 6px 14px; border-bottom: 1px solid var(--border-soft); color: var(--muted); background: #fffbea; font-size: 12px; }
.messages-scroll { min-height: 0; flex: 1; overflow-y: auto; padding: 10px 16px 20px; background: #f2f3f5; overscroll-behavior: contain; }
.messages-list { width: min(760px, 100%); margin: 0 auto; }
.day-divider { display: flex; align-items: center; gap: 10px; margin: 13px 0 9px; color: var(--muted); font-size: 11px; }
.day-divider::before, .day-divider::after { content: ""; height: 1px; flex: 1; background: #dfe3e7; }
.message-row { display: flex; margin: 5px 0; }
.message-row.is-out { justify-content: flex-end; }
.message-row.is-system { justify-content: center; margin: 10px 0; }
.message-bubble { max-width: min(76%, 580px); padding: 8px 11px 6px; border: 1px solid #dfe3e7; border-radius: 13px; background: #fff; box-shadow: 0 1px 1px rgba(0,0,0,.025); }
.message-row.is-out .message-bubble { border-color: #c8ddf3; background: var(--bubble-out); }
.message-row.is-system .message-bubble { width: min(88%, 650px); max-width: 650px; border-color: #dfe3e7; background: var(--system); }
.message-title { margin-bottom: 3px; color: var(--vk-blue-strong); font-size: 12px; font-weight: 650; }
.message-text { line-height: 1.42; overflow-wrap: anywhere; white-space: pre-wrap; }
.message-text mark { padding: 0; background: #ffe47c; }
.message-meta { display: flex; align-items: center; justify-content: flex-end; gap: 7px; margin-top: 4px; color: var(--subtle); font-size: 10px; }
.payload-chip { padding: 1px 5px; border-radius: 6px; color: #55799d; background: rgba(255,255,255,.65); }
.message-context { margin: 2px 0 5px; color: var(--muted); font-size: 11px; }
.message-context a { color: #2688eb; font-weight: 600; text-decoration: none; }
.message-context a:hover { text-decoration: underline; }
.raw-details { margin-top: 6px; }
.raw-details summary { color: var(--muted); font-size: 11px; cursor: pointer; }
.raw-details pre { max-height: 220px; margin: 7px 0 0; overflow: auto; padding: 8px; border-radius: 7px; color: #45515e; background: rgba(255,255,255,.72); font: 11px/1.4 Consolas, monospace; white-space: pre-wrap; }
.older-loader { width: min(760px,100%); margin: 4px auto 10px; color: var(--muted); font-size: 12px; }
.messages-empty { min-height: 60%; display: flex; align-items: center; justify-content: center; flex-direction: column; gap: 4px; color: var(--muted); text-align: center; }
.spinner { width: 14px; height: 14px; border: 2px solid #c5d3df; border-top-color: var(--vk-blue); border-radius: 50%; animation: spin .75s linear infinite; }
.toast { max-width: min(430px, calc(100vw - 30px)); position: fixed; right: 16px; bottom: 16px; z-index: 20; padding: 10px 13px; border-radius: 8px; color: #fff; background: rgba(32,42,52,.94); box-shadow: 0 6px 20px rgba(0,0,0,.18); }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 50% { opacity: .45; } }

@media (max-width: 1050px) {
    .workspace { grid-template-columns: minmax(250px, 300px) minmax(260px, 320px) minmax(360px, 1fr); }
    .sort-field select { max-width: 105px; }
    .sync-status span:last-child { display: none; }
}

@media (max-width: 760px) {
    .app-header { padding: 0 10px; }
    .brand-copy p, .header-button#refreshButton, .sync-status { display: none; }
    .brand-copy { flex: 1; }
    .workspace { display: block; height: calc(100vh - 58px); }
    .panel { display: none; height: 100%; }
    body[data-mobile-view="users"] .users-panel,
    body[data-mobile-view="dialogs"] .dialogs-panel,
    body[data-mobile-view="chat"] .chat-panel { display: flex; }
    .back-button { display: flex; }
    .chat-search-field { width: 42px; padding: 0 12px; transition: width .18s; }
    .chat-search-field input, .chat-search-field .clear-search { display: none; }
    .chat-search-field:focus-within { width: 45%; padding: 0 7px 0 10px; }
    .chat-search-field:focus-within input { display: block; }
    .chat-search-field:focus-within .clear-search { display: block; }
    .message-bubble { max-width: 88%; }
}
