:root {
  --bg: #0d0f14;
  --panel: #151821;
  --panel-2: #1b1f2a;
  --line: rgba(255, 255, 255, .06);
  --text: #e6e8ee;
  --muted: #8b90a0;
  --accent: #8b5cf6;
  --accent-soft: rgba(139, 92, 246, .16);
  --accent-text: #c4b5fd;
  --pink: var(--accent);
  --pink-soft: var(--accent-soft);
  --blue: #7aa2ff;
  --out: #243044;
  --in: #1b1f2a;
  --red: #f07178;
  --red-soft: rgba(240, 113, 120, .12);
  --green: #3dd68c;
  --amber: #e3b341;
  --radius: 14px;
  --nav-w: 232px;
  --list-w: 340px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }
.in-telegram, .in-telegram #app { height: var(--tg-h, 100dvh); max-height: var(--tg-h, 100dvh); }
.in-telegram { overflow: hidden; }
.in-telegram #app { display: flex; flex-direction: column; }
body { font-size: 15px; line-height: 1.45; }
a { color: var(--accent-text); text-decoration: none; }
code { background: var(--panel-2); padding: 1px 6px; border-radius: 6px; font-size: 13px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--red); margin-top: 12px; }

.brand { font-weight: 750; letter-spacing: -.2px; font-size: 16px; color: var(--text); }
.brand span { color: var(--accent-text); font-weight: 600; }
.brand.big { font-size: 26px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  padding: 9px 14px; border-radius: 10px; cursor: pointer; font: inherit; font-weight: 600;
  transition: background .18s ease, border-color .18s ease, transform .15s ease, color .18s ease;
}
.btn:hover { background: #222736; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn.primary:hover { background: #9b74f7; }
.btn.danger.on { background: var(--red); border-color: transparent; color: #fff; }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 10px; font-size: 13px; min-height: 32px; }
.btn.wide { width: 100%; }
.icon-btn { background: none; border: 0; color: var(--text); font-size: 20px; cursor: pointer; padding: 8px 10px; min-width: 40px; min-height: 40px; }

input[type=text], input[type=password], input[type=search] {
  width: 100%; background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  padding: 10px 12px; border-radius: 10px; font: inherit; outline: none;
  transition: border-color .18s ease;
}
input:focus { border-color: rgba(139, 92, 246, .7); }

.screen { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px; background: var(--bg); }
.splash { display: flex; flex-direction: column; align-items: center; gap: 22px; text-align: center; }
.splash-spin { width: 28px; height: 28px; border-radius: 50%; border: 2px solid rgba(139, 92, 246, .25); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.login-block { margin-top: 20px; padding-top: 18px; border-top: 1px solid var(--line); }
.login-block h3 { margin: 0 0 10px; font-size: 16px; }
.login-block summary { cursor: pointer; color: var(--muted); font-weight: 600; }
.steps { margin: 0 0 16px; padding-left: 20px; color: var(--muted); }
.steps li { margin: 4px 0; }
.steps b { color: var(--text); }
#codeForm { display: flex; gap: 8px; margin-top: 12px; }

#app { height: 100%; display: flex; flex-direction: column; }
.layout { flex: 1; display: flex; min-height: 0; min-width: 0; }
.workspace { flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.topbar {
  height: 52px; flex: none; display: flex; align-items: center; gap: 8px; padding: 0 16px;
  background: var(--bg); border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}
.me { margin-left: auto; display: flex; align-items: center; gap: 10px; min-width: 0; }
#meName { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 28vw; color: var(--muted); font-size: 13px; }
.badge { font-size: 11px; padding: 3px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent-text); font-weight: 700; }
.badge.gray { background: var(--panel-2); color: var(--muted); }
.badge.red { background: var(--red-soft); color: var(--red); }
.badge.green { background: rgba(61, 214, 140, .14); color: var(--green); }

.nav {
  width: var(--nav-w); flex: none; background: var(--panel); border-right: 1px solid var(--line);
  padding: 18px 10px 12px; display: flex; flex-direction: column; gap: 2px; overflow-y: auto;
}
.nav .brand { padding: 2px 12px 16px; }
.nav button {
  text-align: left; background: none; border: 0; color: var(--muted); font: inherit;
  padding: 9px 12px; border-radius: 10px; cursor: pointer; display: flex; align-items: center; gap: 10px;
  transition: background .18s ease, color .18s ease;
}
.nav button .ico, .tabbar button .ico { width: 20px; height: 20px; display: inline-flex; align-items: center; justify-content: center; flex: none; color: inherit; }
.nav button .ico svg, .tabbar button .ico svg { width: 20px; height: 20px; display: block; }
.nav button:hover { background: rgba(255, 255, 255, .03); color: var(--text); }
.nav button.active { background: var(--accent-soft); color: var(--accent-text); font-weight: 650; }
.nav-sep { margin: 16px 12px 6px; font-size: 11px; text-transform: uppercase; color: var(--muted); letter-spacing: .8px; }
main { flex: 1; min-width: 0; min-height: 0; display: flex; }
.page { flex: 1; overflow-y: auto; padding: 22px; }
.page h2 { margin: 0 0 6px; font-weight: 650; }
.page .sub { color: var(--muted); margin: 0 0 18px; }

.split { flex: 1; display: flex; min-width: 0; min-height: 0; }
.list-pane { width: var(--list-w); flex: none; border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.list-head { padding: 12px; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 10px; }
.owner-chip { display: flex; align-items: center; gap: 8px; background: var(--panel-2); border-radius: 10px; padding: 8px 10px; font-size: 14px; }
.owner-chip b { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  border: 0; background: transparent; color: var(--muted); padding: 6px 10px; border-radius: 999px;
  cursor: pointer; font: inherit; font-size: 13px; white-space: nowrap; flex: none;
  transition: background .18s ease, color .18s ease;
}
.chip:hover { color: var(--text); }
.chip.active { background: var(--accent-soft); color: var(--accent-text); }
.list { flex: 1; overflow-y: auto; min-height: 0; }
.row {
  display: flex; gap: 12px; align-items: flex-start; padding: 10px 14px; cursor: pointer;
  border-bottom: 1px solid var(--line); transition: background .18s ease;
}
.row:hover { background: rgba(255, 255, 255, .03); }
.row.active { background: var(--accent-soft); }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex: none; display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: #fff; font-size: 15px;
}
.row-main { flex: 1; min-width: 0; }
.row-title { display: flex; gap: 8px; align-items: center; }
.row-title b { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 640; }
.row-title time { color: var(--muted); font-size: 12px; flex: none; }
.row.is-stale .row-title time, .row-age { color: var(--amber); }
.row-preview { color: var(--muted); font-size: 13px; margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.has-unread .row-preview, .has-unread .row-title b { color: var(--text); }
.unread {
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 999px; background: var(--accent); color: #fff;
  font-size: 11px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.wait-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--amber); flex: none; }
.row-age { font-size: 12px; margin-top: 2px; }
.row-sub { color: var(--muted); font-size: 13px; display: flex; gap: 8px; margin-top: 2px; }
.more { padding: 14px; text-align: center; }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }

.skeleton { padding: 6px 0; }
.sk { display: flex; gap: 12px; padding: 12px 14px; }
.sk::before, .sk::after {
  content: ""; display: block; border-radius: 8px; background: var(--panel-2);
  animation: pulse 1.1s ease-in-out infinite;
}
.sk::before { width: 40px; height: 40px; border-radius: 50%; flex: none; }
.sk::after { flex: 1; height: 40px; }

.chat-pane {
  flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; overflow: hidden;
  background: var(--bg);
}
.split.chat-open .chat-pane { animation: fade .2s ease; }
.chat-head {
  padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel); flex: none;
  display: grid; grid-template-columns: auto auto minmax(0, 1fr); gap: 8px 12px; align-items: center;
}
.chat-head .title { min-width: 0; }
.chat-head .title b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head .title small { color: var(--muted); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-actions { grid-column: 1 / -1; display: flex; gap: 8px; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
.chat-actions::-webkit-scrollbar { display: none; }
.chat-actions .btn { flex: 0 0 auto; }
.restore-bar { background: var(--red-soft); color: #f3b4b8; padding: 8px 16px; font-size: 13px; border-bottom: 1px solid rgba(240, 113, 120, .2); }
.messages { flex: 1; min-height: 0; overflow-y: auto; padding: 16px 18px 28px; display: flex; flex-direction: column; gap: 6px; animation: fade .2s ease; }
.day { align-self: center; background: var(--panel); color: var(--muted); font-size: 12px; padding: 3px 10px; border-radius: 999px; margin: 10px 0 4px; }
.msg {
  max-width: min(560px, 78%); padding: 8px 12px 6px; border-radius: 14px; background: var(--in); align-self: flex-start;
  word-wrap: break-word; white-space: pre-wrap; animation: rise .2s ease;
}
.msg.out { background: var(--out); align-self: flex-end; }
.msg.del { background: var(--red-soft); box-shadow: inset 0 0 0 1px rgba(240, 113, 120, .35); }
.msg .from { font-size: 13px; font-weight: 700; color: var(--accent-text); margin-bottom: 2px; }
.msg.out .from { color: #9eb6de; }
.msg .meta { display: flex; gap: 8px; justify-content: flex-end; align-items: center; font-size: 11px; color: rgba(230, 232, 238, .5); margin-top: 4px; }
.msg .tag { font-weight: 700; color: var(--red); }
.msg .orig { margin-top: 6px; padding: 6px 8px; border-left: 2px solid var(--blue); background: rgba(122, 162, 255, .08); border-radius: 6px; font-size: 13px; color: #b7c0d0; }
.msg .orig b { color: var(--blue); font-weight: 700; }
.media { margin: 2px 0 6px; display: flex; flex-direction: column; gap: 6px; }
.media img { max-width: 100%; max-height: 360px; border-radius: 10px; cursor: zoom-in; display: block; }
.media video { max-width: 100%; max-height: 360px; border-radius: 10px; background: #000; }
.media video.round { width: 240px; height: 240px; border-radius: 50%; object-fit: cover; }
.media audio { width: 260px; max-width: 100%; }
.media .file { display: inline-flex; gap: 6px; align-items: center; padding: 6px 10px; background: rgba(0, 0, 0, .25); border-radius: 8px; }
.media-label { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: var(--blue); font-size: 13px; }
.placeholder { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; color: var(--muted); text-align: center; padding: 30px; }
.ph-ico { width: 54px; height: 54px; border-radius: 16px; background: var(--panel); color: var(--muted); display: flex; align-items: center; justify-content: center; font-size: 24px; }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; margin-bottom: 20px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.card .v { font-size: 24px; font-weight: 750; }
.card .k { color: var(--muted); font-size: 13px; margin-top: 2px; }
.card.accent .v { color: var(--accent-text); }
.card.red .v { color: var(--red); }
.section { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.section h3 { margin: 0 0 10px; font-size: 16px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; }
.kv span:nth-child(odd) { color: var(--muted); }

.search-bar { display: flex; gap: 8px; margin-bottom: 16px; max-width: 720px; }
.hit { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; margin-bottom: 8px; cursor: pointer; transition: background .18s ease; }
.hit:hover { background: var(--panel-2); }
.hit .h { display: flex; gap: 8px; font-size: 13px; color: var(--muted); margin-bottom: 4px; flex-wrap: wrap; }
.hit .h b { color: var(--text); }
.hit mark { background: var(--accent-soft); color: var(--text); border-radius: 3px; }

.viewer { position: fixed; inset: 0; background: rgba(0, 0, 0, .88); display: flex; align-items: center; justify-content: center; z-index: 50; }
.viewer img { max-width: 94vw; max-height: 92vh; border-radius: 8px; }
.viewer .close { position: absolute; top: 12px; right: 12px; font-size: 26px; }
.toast {
  position: fixed; bottom: calc(24px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  padding: 10px 16px; border-radius: 12px; z-index: 60; animation: rise .2s ease;
}

.in-telegram #logoutBtn { display: none; }
.only-mobile { display: none; }
.back-btn { display: none; }
.tabbar { display: none; }
.who { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.who .avatar { width: 56px; height: 56px; font-size: 22px; }
.hint { color: var(--muted); font-size: 13px; margin: 0; }

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes pulse { 50% { opacity: .45; } }

@media (max-width: 1280px) {
  :root { --nav-w: 208px; --list-w: 300px; }
}
@media (max-width: 860px) {
  .only-mobile { display: inline-flex; align-items: center; }
  .nav {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 40; transform: translateX(-105%);
    transition: transform .22s ease; width: min(280px, 86vw);
    padding-top: calc(18px + env(safe-area-inset-top));
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
  }
  .nav.open { transform: none; }
  .topbar { height: auto; min-height: 48px; padding: 0 8px; padding-top: env(safe-area-inset-top); }
  .brand { font-size: 15px; }
  .layout { padding-bottom: calc(62px + env(safe-area-inset-bottom)); }
  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 35;
    background: var(--panel); border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .tabbar button {
    flex: 1; border: 0; background: none; color: var(--muted); font: inherit;
    min-height: 52px; padding: 6px 2px 8px; display: flex; flex-direction: column; align-items: center; gap: 1px;
    font-size: 11px; font-weight: 600; transition: color .18s ease;
  }
  .tabbar button .ico, .tabbar button .ico svg { width: 22px; height: 22px; }
  .tabbar button.active { color: var(--accent-text); }
  .list-pane { width: 100%; border-right: 0; }
  .split.chat-open .list-pane { display: none; }
  .split:not(.chat-open) .chat-pane { display: none; }
  .back-btn { display: inline-flex; }
  .msg { max-width: 88%; }
  .page { padding: 14px 12px 20px; }
  #meName { display: none; }
  .row { padding: 12px; }
  .messages { padding-bottom: calc(20px + env(safe-area-inset-bottom)); }
  .cards { grid-template-columns: 1fr 1fr; gap: 8px; }
  .card { padding: 12px; }
  .card .v { font-size: 20px; }
  .chat-head { padding: 8px 10px; }
  .list-head { padding: 10px; }
  .search-bar { flex-direction: column; }
  .kv { grid-template-columns: 1fr; gap: 2px 0; }
  .kv span:nth-child(odd) { margin-top: 8px; }
}
@media (max-width: 380px) {
  .cards { grid-template-columns: 1fr; }
  .tabbar button { font-size: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
  }
}
