/* Внешний вид — как у Telegram: цвета берутся из темы Telegram (переменные --tg-theme-*),
   ниже — запасные значения для светлой темы. */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #000000);
  --hint: var(--tg-theme-hint-color, #8e8e93);
  --link: var(--tg-theme-link-color, #2481cc);
  --accent: var(--tg-theme-accent-text-color, var(--link));
  --button: var(--tg-theme-button-color, #2481cc);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #efeff4);
  --header-bg: var(--tg-theme-header-bg-color, var(--bg));
  --section-bg: var(--tg-theme-section-bg-color, var(--bg));
  --separator: var(--tg-theme-section-separator-color, rgba(0, 0, 0, .09));
  --destructive: var(--tg-theme-destructive-text-color, #e53935);
  --subtitle: var(--tg-theme-subtitle-text-color, var(--hint));
  --chat-bg: var(--secondary-bg);
  --bubble-in: var(--bg);
  --bubble-out: #e3fee0;
  --bubble-out-meta: #4fae4e;
  --read: #4fae4e;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
html.tg-dark {
  --bubble-out: color-mix(in srgb, var(--button) 38%, var(--bg));
  --bubble-out-meta: color-mix(in srgb, var(--text) 60%, transparent);
  --read: var(--accent);
  --separator: var(--tg-theme-section-separator-color, rgba(255, 255, 255, .08));
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased; overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
svg { fill: currentColor; }
.hidden { display: none !important; }

.app { position: fixed; inset: 0; display: flex; }
.screen { position: absolute; inset: 0; display: flex; flex-direction: column; background: var(--bg); }
@media (min-width: 900px) {
  .screen-list { position: relative; width: 360px; flex: none; border-right: 1px solid var(--separator); }
  .screen-chat, .screen-profile, .placeholder { position: relative; flex: 1; inset: auto; }
  .screen-profile { position: absolute; left: 360px; right: 0; top: 0; bottom: 0; z-index: 3; }
  #chat-back { visibility: hidden; width: 8px; }
}
.placeholder { display: flex; align-items: center; justify-content: center; color: var(--hint); background: var(--chat-bg); }

.icon-btn { width: 40px; height: 40px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center;
  color: var(--hint); font-size: 20px; flex: none; }
.icon-btn svg { width: 24px; height: 24px; }
.icon-btn:active { background: var(--secondary-bg); }
.icon-btn-placeholder { width: 40px; }

/* ---------- список ---------- */
.list-header { display: flex; align-items: center; gap: 6px; padding: 8px 8px 6px 12px; background: var(--header-bg); }
.search { flex: 1; display: flex; align-items: center; gap: 6px; height: 36px; padding: 0 6px 0 10px;
  background: var(--secondary-bg); border-radius: 18px; }
.search-icon { width: 18px; height: 18px; color: var(--hint); flex: none; }
.search input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; height: 100%; }
.search input::-webkit-search-cancel-button { display: none; }
.search .icon-btn { width: 28px; height: 28px; font-size: 14px; }

.tabs { display: flex; gap: 2px; overflow-x: auto; padding: 0 8px; background: var(--header-bg);
  border-bottom: 1px solid var(--separator); scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab { padding: 10px 10px 9px; color: var(--hint); font-weight: 500; white-space: nowrap; border-bottom: 2px solid transparent;
  display: inline-flex; align-items: center; gap: 6px; }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.tab .badge { min-width: 18px; height: 18px; font-size: 12px; padding: 0 5px; }

.list-scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.conv-list { list-style: none; margin: 0; padding: 0; }
.conv { display: flex; gap: 12px; padding: 7px 10px 7px 10px; align-items: center; cursor: pointer; position: relative; }
.conv:active, .conv.selected { background: var(--secondary-bg); }
.conv-main { flex: 1; min-width: 0; border-bottom: 1px solid var(--separator); padding: 4px 0 9px; align-self: stretch;
  display: flex; flex-direction: column; justify-content: center; gap: 2px; }
.conv:last-child .conv-main { border-bottom: 0; }
.conv-row { display: flex; align-items: center; gap: 6px; min-width: 0; }
.conv-title { flex: 1; min-width: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-time { color: var(--hint); font-size: 13px; flex: none; display: inline-flex; align-items: center; gap: 3px; }
.conv-preview { flex: 1; min-width: 0; color: var(--hint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-preview .you { color: var(--text); }
.badge { display: inline-flex; align-items: center; justify-content: center; min-width: 22px; height: 22px; padding: 0 7px;
  border-radius: 11px; background: var(--button); color: var(--button-text); font-size: 13px; font-weight: 600; flex: none; }
.badge.muted { background: var(--hint); }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--secondary-bg); color: var(--hint); flex: none; }
.tag.danger { color: var(--destructive); }
.pin { color: var(--hint); width: 16px; height: 16px; flex: none; }

.avatar { width: 54px; height: 54px; border-radius: 50%; flex: none; display: inline-flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 600; font-size: 19px; background-size: cover; background-position: center; overflow: hidden; }
.avatar-sm { width: 40px; height: 40px; font-size: 15px; }
.avatar-lg { width: 96px; height: 96px; font-size: 34px; }
.av0 { background-color: #e17076; } .av1 { background-color: #faa774; } .av2 { background-color: #a695e7; }
.av3 { background-color: #7bc862; } .av4 { background-color: #6ec9cb; } .av5 { background-color: #65aadd; }
.av6 { background-color: #ee7aae; }

.section-title { padding: 10px 14px 4px; color: var(--hint); font-size: 13px; text-transform: uppercase; letter-spacing: .02em; }
.search-hit { padding: 9px 14px; border-bottom: 1px solid var(--separator); cursor: pointer; }
.search-hit .conv-row { margin-bottom: 2px; }
.empty, .loader { padding: 24px 16px; text-align: center; color: var(--hint); }

.banner { padding: 8px 14px; font-size: 14px; text-align: center; background: var(--secondary-bg); color: var(--hint); }
.banner-warn { color: var(--destructive); }
.banner button { color: var(--link); font-weight: 500; margin-left: 6px; }

/* ---------- переписка ---------- */
.chat-header { display: flex; align-items: center; gap: 4px; height: 56px; padding: 0 6px; background: var(--header-bg);
  border-bottom: 1px solid var(--separator); flex: none; }
.chat-header > .chat-title { flex: 1; text-align: center; font-weight: 600; }
.chat-title-btn { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; text-align: left; padding: 4px; border-radius: 10px; }
.chat-title-text { min-width: 0; display: flex; flex-direction: column; }
.chat-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-subtitle { font-size: 13px; color: var(--hint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.messages-scroll { flex: 1; overflow-y: auto; background: var(--chat-bg); -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain; padding: 6px 0 4px; display: flex; flex-direction: column; }
.messages { display: flex; flex-direction: column; padding: 0 8px; max-width: 760px; width: 100%; margin: auto auto 0; }
.day { align-self: center; margin: 8px 0; padding: 3px 10px; border-radius: 12px; font-size: 13px; font-weight: 500;
  background: color-mix(in srgb, var(--bg) 92%, transparent); color: var(--text); position: sticky; top: 4px; z-index: 1;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .08); }
.import-divider { align-self: center; margin: 10px 0; font-size: 12px; color: var(--hint); }

.msg { display: flex; margin: 1px 0; }
.msg.out { justify-content: flex-end; }
.msg-col { display: flex; flex-direction: column; max-width: min(82%, 520px); min-width: 0; }
.msg.out .msg-col { align-items: flex-end; }
.msg-col .bubble { max-width: 100%; }
.msg-col .kbd { align-self: stretch; }
.msg + .msg.first { margin-top: 6px; }
.bubble { position: relative; max-width: min(82%, 520px); padding: 6px 9px 6px 10px; border-radius: var(--radius);
  background: var(--bubble-in); box-shadow: 0 1px 1px rgba(0, 0, 0, .08); word-wrap: break-word; overflow-wrap: anywhere; }
.msg.out .bubble { background: var(--bubble-out); }
.msg.in.last .bubble { border-bottom-left-radius: 4px; }
.msg.out.last .bubble { border-bottom-right-radius: 4px; }
.msg.highlight .bubble { animation: flash 1.6s ease; }
@keyframes flash { 0%, 40% { box-shadow: 0 0 0 3px var(--accent); } 100% { box-shadow: 0 1px 1px rgba(0, 0, 0, .08); } }
.bubble.media-only { padding: 3px; }
.text { white-space: pre-wrap; }
.text::after { content: ""; display: block; clear: both; }
.text a, .link { color: var(--link); text-decoration: none; cursor: pointer; }
.text code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13.5px;
  background: color-mix(in srgb, var(--text) 8%, transparent); border-radius: 4px; padding: 0 3px; }
.text pre { margin: 4px 0; padding: 6px 8px; border-radius: 8px; overflow-x: auto;
  background: color-mix(in srgb, var(--text) 7%, transparent); }
.text pre code { background: none; padding: 0; }
.text blockquote { margin: 3px 0; padding: 2px 8px; border-left: 3px solid var(--accent); border-radius: 4px;
  background: color-mix(in srgb, var(--accent) 10%, transparent); }
.spoiler { background: var(--hint); color: transparent; border-radius: 4px; cursor: pointer; transition: .2s; }
.spoiler.shown { background: transparent; color: inherit; }
.accent { color: var(--accent); }

.meta { float: right; margin: 6px 0 -4px 10px; font-size: 12px; color: var(--hint); display: inline-flex; align-items: center;
  gap: 3px; white-space: nowrap; position: relative; top: 2px; }
.msg.out .meta { color: var(--bubble-out-meta); }
.meta .edited { cursor: pointer; }
.meta .src { font-style: italic; }
.ticks { width: 17px; height: 11px; flex: none; }
.ticks.read { color: var(--read); }
.ticks.failed { color: var(--destructive); }
.media-only .meta { position: absolute; right: 8px; bottom: 7px; margin: 0; padding: 1px 6px; border-radius: 10px;
  background: rgba(0, 0, 0, .45); color: #fff; float: none; }
.media-only .meta .ticks { color: #fff; }

.fwd { font-size: 13px; color: var(--accent); margin-bottom: 2px; font-weight: 500; }
.reply-quote { display: block; border-left: 3px solid var(--accent); padding: 2px 8px; margin: 2px 0 4px; border-radius: 4px;
  background: color-mix(in srgb, var(--accent) 10%, transparent); font-size: 13.5px; cursor: pointer; max-width: 100%; text-align: left; }
.reply-quote .rq-name { color: var(--accent); font-weight: 600; display: block; }
.reply-quote .rq-text { color: var(--text); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.deleted-label { font-size: 12px; color: var(--destructive); margin-bottom: 2px; }
.msg.deleted .bubble { opacity: .7; }
.service { align-self: center; margin: 6px 0; padding: 3px 10px; border-radius: 12px; font-size: 13px; text-align: center;
  background: color-mix(in srgb, var(--bg) 70%, transparent); color: var(--text); }

.photo { display: block; border-radius: 11px; max-width: 100%; min-width: 120px; min-height: 80px; background: var(--secondary-bg);
  cursor: pointer; object-fit: cover; }
.photo-ph { width: 260px; max-width: 100%; aspect-ratio: 4/3; border-radius: 11px; background: var(--secondary-bg);
  display: flex; align-items: center; justify-content: center; color: var(--hint); font-size: 13px; cursor: pointer; }
.file { display: flex; align-items: center; gap: 10px; padding: 2px 0 4px; cursor: pointer; min-width: 200px; }
.file-icon { width: 44px; height: 44px; border-radius: 50%; background: var(--button); color: var(--button-text);
  display: flex; align-items: center; justify-content: center; flex: none; font-size: 18px; }
.file.unavailable .file-icon { background: var(--hint); }
.file-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 230px; }
.file-size { font-size: 13px; color: var(--hint); }
.caption { margin-top: 4px; }
audio, video { max-width: 100%; display: block; margin: 2px 0; }
video { border-radius: 11px; }

.kbd { display: grid; gap: 4px; margin-top: 3px; }
.kbd-row { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; }
.kbd-btn { padding: 9px 6px; border-radius: 10px; text-align: center; font-size: 14px; font-weight: 500;
  background: color-mix(in srgb, var(--button) 12%, var(--bg)); color: var(--link); box-shadow: 0 1px 1px rgba(0, 0, 0, .06); }
html.tg-dark .kbd-btn { background: rgba(255, 255, 255, .1); color: var(--text); }
.kbd-btn.disabled { opacity: .75; cursor: default; }
.kbd-btn .arrow { font-size: 11px; opacity: .7; margin-left: 3px; }
.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.reaction { font-size: 13px; padding: 1px 7px; border-radius: 11px; background: color-mix(in srgb, var(--accent) 14%, transparent); }

/* ---------- ввод ---------- */
.composer-wrap { flex: none; background: var(--header-bg); border-top: 1px solid var(--separator); padding-bottom: var(--safe-bottom); }
.composer { display: flex; align-items: flex-end; gap: 6px; padding: 6px 8px; }
.composer textarea { flex: 1; resize: none; border: 0; outline: 0; background: var(--secondary-bg); border-radius: 20px;
  padding: 9px 14px; min-height: 40px; max-height: 160px; line-height: 1.35; }
.send-btn { width: 40px; height: 40px; border-radius: 50%; background: var(--button); color: var(--button-text);
  display: inline-flex; align-items: center; justify-content: center; flex: none; }
.send-btn svg { width: 22px; height: 22px; }
.send-btn:disabled { background: var(--secondary-bg); color: var(--hint); cursor: default; }
.reply-bar { display: flex; align-items: center; gap: 8px; padding: 6px 8px 0 12px; }
.reply-bar-line { width: 3px; align-self: stretch; background: var(--accent); border-radius: 2px; }
.reply-bar-body { flex: 1; min-width: 0; font-size: 13.5px; }
.reply-bar-title { color: var(--accent); font-weight: 600; }
.reply-bar-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- профиль ---------- */
.profile-body { flex: 1; overflow-y: auto; background: var(--secondary-bg); padding-bottom: 24px; }
.profile-head { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 20px 16px 16px; background: var(--section-bg); }
.profile-name { font-size: 20px; font-weight: 600; text-align: center; }
.profile-sub { color: var(--hint); font-size: 14px; }
.group { margin: 12px 0 0; background: var(--section-bg); }
.group-title { padding: 14px 16px 6px; color: var(--accent); font-size: 14px; font-weight: 600; background: var(--section-bg); }
.row { display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--separator); min-height: 48px; }
.row:last-child { border-bottom: 0; }
.row-main { flex: 1; min-width: 0; }
.row-value { overflow-wrap: anywhere; }
.row-label { font-size: 13px; color: var(--hint); }
.row-btn { color: var(--link); font-weight: 500; }
.row-btn.danger { color: var(--destructive); }
button.row { width: 100%; text-align: left; }
.copy-btn { color: var(--link); font-size: 14px; padding: 6px 10px; border-radius: 8px; flex: none; }
.copy-btn:active { background: var(--secondary-bg); }
.check { width: 20px; height: 20px; accent-color: var(--button); }

.sub-form { padding: 12px 16px 14px; display: flex; flex-direction: column; gap: 10px; }
.sub-days { display: flex; align-items: center; gap: 8px; }
.sub-days input { width: 90px; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--separator); background: var(--bg); }
.sub-bots { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.btn { padding: 11px 12px; border-radius: 10px; background: var(--button); color: var(--button-text); font-weight: 600; text-align: center; }
.btn.secondary { background: var(--secondary-bg); color: var(--link); }
.btn:disabled { opacity: .45; cursor: default; }
.sub-op { display: flex; flex-direction: column; gap: 2px; padding: 10px 16px; border-bottom: 1px solid var(--separator); }
.sub-op-line { display: flex; justify-content: space-between; gap: 10px; }
.sub-op-line > span:last-child { text-align: right; }
.status-ok { color: var(--read); } .status-bad { color: var(--destructive); } .status-wait { color: var(--hint); }
.sub-op-client { font-size: 14px; color: var(--hint); }
.link-btn { color: var(--link); font-weight: 500; padding: 0; }
.sub-op-reason { font-size: 13px; color: var(--hint); overflow-wrap: anywhere; }
.sub-op-actions { display: flex; gap: 8px; margin-top: 4px; }
.sub-op-actions .btn { padding: 7px 10px; font-size: 14px; }

/* ---------- всплывающее ---------- */
.overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 10; }
.menu { position: fixed; z-index: 11; min-width: 200px; background: var(--section-bg); border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .25); padding: 4px 0; }
.menu button { display: block; width: 100%; text-align: left; padding: 11px 16px; }
.menu button:active { background: var(--secondary-bg); }
.menu button.danger { color: var(--destructive); }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 11; max-height: 80vh; overflow-y: auto; background: var(--section-bg);
  border-radius: 16px 16px 0 0; padding: 8px 0 calc(12px + var(--safe-bottom)); box-shadow: 0 -4px 20px rgba(0,0,0,.2); }
@media (min-width: 900px) { .sheet { left: 50%; right: auto; width: 440px; transform: translateX(-50%); bottom: 10vh; border-radius: 16px; } }
.sheet h3 { margin: 6px 16px 10px; font-size: 17px; }
.sheet .row input[type=text] { flex: 1; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--separator); background: var(--bg); }
.version { padding: 10px 16px; border-bottom: 1px solid var(--separator); }
.version .row-label { margin-bottom: 3px; }
.viewer { position: fixed; inset: 0; z-index: 12; background: rgba(0, 0, 0, .92); display: flex; align-items: center; justify-content: center; }
.viewer img, .viewer video { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer .close { position: absolute; top: 10px; right: 10px; color: #fff; font-size: 26px; width: 44px; height: 44px; }
.toast { position: fixed; left: 50%; bottom: calc(80px + var(--safe-bottom)); transform: translateX(-50%); z-index: 13;
  max-width: min(92vw, 440px); padding: 10px 14px; border-radius: 12px; background: rgba(30, 30, 30, .92); color: #fff;
  font-size: 14px; text-align: center; }
.fatal { position: fixed; inset: 0; z-index: 20; background: var(--bg); display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 10px; padding: 24px; text-align: center; color: var(--hint); }
.fatal b { color: var(--text); font-size: 17px; }
