:root {
  color-scheme: light;
  --bg: #f4f4f5;
  --panel: #ffffff;
  --panel-soft: #f4f7fb;
  --chat-bg: #cfd9c4;
  --bubble-in: #ffffff;
  --bubble-out: #eeffde;
  --accent: #3390ec;
  --text: #1c1c1e;
  --muted: #707579;
  --line: #e6e6e6;
  --bubble-edge: #e8e8e8;
  --danger: #d93025;
  --check: #4dab50;
  --deleted-overlay: rgba(112, 117, 121, .20);
}

* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; max-width: 100%; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); font-family: Roboto, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }
button, input { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

.app-shell { position: fixed; inset: 0; width: 100%; max-width: 100%; height: 100dvh; overflow: hidden; }
.login-view { width: 100%; max-width: 100%; min-height: 100dvh; display: grid; align-content: center; justify-items: center; padding: 24px; gap: 14px; overflow-x: hidden; text-align: center; background: var(--chat-bg) url('/wallpaper.svg') repeat; background-size: 360px 360px; }
.brand-mark { width: 74px; height: 74px; display: grid; place-items: center; border-radius: 22px; background: linear-gradient(135deg, #3390ec, #65aadd); color: white; font-weight: 800; font-size: 26px; box-shadow: 0 18px 50px rgba(51, 144, 236, .24); }
h1, h2, p { margin: 0; }
h1, h2 { color: var(--text); }
.login-view p { color: var(--muted); }
.card { width: min(420px, 100%); margin-top: 14px; padding: 18px; border: 1px solid var(--line); border-radius: 22px; background: rgba(255, 255, 255, .94); box-shadow: 0 24px 80px rgba(80, 96, 74, .18); text-align: left; backdrop-filter: blur(4px); }
label { display: grid; gap: 7px; margin: 0 0 14px; color: var(--muted); }
input { width: 100%; border: 1px solid var(--line); border-radius: 14px; padding: 13px 14px; background: #ffffff; color: var(--text); outline: none; }
input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(51, 144, 236, .16); }
button { border: 0; border-radius: 14px; padding: 12px 15px; background: var(--accent); color: white; font-weight: 700; }
button.ghost { background: transparent; color: var(--accent); border: 1px solid rgba(51, 144, 236, .28); }
.status { min-height: 22px; margin-top: 12px; color: var(--muted); }
.status.error { color: var(--danger); }

.main-view { width: 100%; max-width: 100%; height: 100dvh; display: grid; grid-template-columns: minmax(0, 1fr); overflow: hidden; }
.sidebar { width: 100%; max-width: 100%; min-width: 0; overflow: hidden; background: var(--panel); display: grid; grid-template-rows: auto auto auto minmax(0, 1fr); height: 100dvh; }
.main-view.view-thread .sidebar { display: none; }
.main-view.view-list .thread { display: none; }
.topbar, .thread-header { width: 100%; max-width: 100%; min-width: 0; display: flex; align-items: center; gap: 12px; padding: 12px 14px; overflow-x: hidden; border-bottom: 1px solid var(--line); background: #ffffff; }
.topbar { justify-content: space-between; }
.topbar > div { min-width: 0; }
.topbar h2 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-header { min-height: 68px; }
.back-btn { width: 40px; height: 40px; padding: 0; display: grid; place-items: center; border-radius: 50%; background: transparent; color: var(--accent); font-size: 34px; line-height: 1; font-weight: 500; }
.thread-title { flex: 1 1 auto; min-width: 0; max-width: 100%; overflow: hidden; }
.thread-title h2 { width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-title p { min-width: 0; max-width: 100%; display: flex; align-items: center; gap: 6px; overflow: hidden; white-space: nowrap; }
.thread-title p > span { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.client-info-line { flex-wrap: wrap; row-gap: 2px; color: var(--muted); }
.client-info-line > span:not(.badge):not(.client-info-dot) { min-width: 0; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
.client-info-dot { flex: 0 0 auto; color: rgba(112, 117, 121, .72); }
.kicker { color: var(--accent); font-size: 12px; text-transform: uppercase; letter-spacing: .12em; }
.build-marker { display: inline-flex; align-items: center; width: fit-content; border: 1px solid rgba(51, 144, 236, .24); border-radius: 999px; padding: 3px 8px; color: var(--accent); background: rgba(255, 255, 255, .72); font-size: 11px; font-weight: 750; letter-spacing: .02em; }
.build-marker.inline { margin-left: 6px; vertical-align: middle; }
.search-wrap { margin: 0; padding: 10px 12px; border-bottom: 1px solid var(--line); background: #ffffff; }
.search-wrap span { font-size: 12px; color: var(--muted); }
.search-wrap input { padding: 10px 12px; border-radius: 999px; background: var(--panel-soft); }
.filters { width: 100%; max-width: 100%; display: flex; gap: 8px; overflow-x: auto; padding: 10px 12px; border-bottom: 1px solid var(--line); background: #ffffff; }
.filter { flex: 0 0 auto; padding: 9px 12px; background: var(--panel-soft); color: var(--muted); }
.filter.active { background: var(--accent); color: white; }
.business-toggle { margin: 0; padding: 9px 12px; border-bottom: 1px solid var(--line); background: #ffffff; display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.business-toggle input { width: auto; margin: 0; }
.conversation-list { width: 100%; max-width: 100%; min-width: 0; min-height: 0; overflow-y: auto; overflow-x: hidden; background: #ffffff; overscroll-behavior: contain; }
.row { width: 100%; max-width: 100%; min-width: 0; display: grid; grid-template-columns: 52px minmax(0, 1fr) auto; gap: 12px; align-items: center; padding: 11px 14px; overflow: hidden; border: 0; border-bottom: 1px solid var(--line); border-radius: 0; background: transparent; color: var(--text); text-align: left; }
.row:hover, .row.active { background: rgba(51, 144, 236, .08); }
.avatar { width: 52px; height: 52px; border-radius: 50%; background: var(--avatar-bg, #65aadd); display: grid; place-items: center; font-weight: 600; color: white; flex: 0 0 auto; overflow: hidden; }
.avatar.avatar-photo { position: relative; background: var(--avatar-bg, #65aadd); }
.avatar.avatar-photo img { width: 100%; height: 100%; display: block; object-fit: cover; border-radius: 50%; }
.avatar.avatar-photo .avatar-fallback { display: none; }
.avatar.avatar-photo.failed img { display: none; }
.avatar.avatar-photo.failed .avatar-fallback { display: grid; place-items: center; width: 100%; height: 100%; }
.row.skeleton { pointer-events: none; cursor: default; }
.row.skeleton:hover { background: transparent; }
.row.skeleton .avatar { width: 52px; height: 52px; }
.skeleton-avatar, .skeleton-line { position: relative; overflow: hidden; background: #eef1f5; color: transparent; }
.skeleton-avatar { border-radius: 50%; }
.skeleton-line { height: 12px; border-radius: 999px; }
.skeleton-main { display: grid; gap: 10px; align-content: center; }
.skeleton-name { width: 60%; }
.skeleton-preview { width: 80%; }
.skeleton-meta { align-self: start; padding-top: 4px; }
.skeleton-time { width: 36px; height: 10px; }
.skeleton-avatar::after, .skeleton-line::after { content: ''; position: absolute; inset: 0; transform: translateX(-100%); background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .74), transparent); animation: skeleton-shimmer 1.35s infinite; }
@keyframes skeleton-shimmer {
  100% { transform: translateX(100%); }
}
.avatar.tg-red { --avatar-bg: #e17076; }
.avatar.tg-orange { --avatar-bg: #faa774; }
.avatar.tg-purple { --avatar-bg: #a695e7; }
.avatar.tg-green { --avatar-bg: #7bc862; }
.avatar.tg-cyan { --avatar-bg: #6ec9cb; }
.avatar.tg-blue { --avatar-bg: #65aadd; }
.avatar.tg-pink { --avatar-bg: #ee7aae; }
.row-main { min-width: 0; max-width: 100%; overflow: hidden; }
.row-title { min-width: 0; max-width: 100%; display: flex; align-items: center; gap: 8px; margin-bottom: 4px; overflow: hidden; }
.name, .preview { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.name { min-width: 0; flex: 1 1 auto; font-weight: 600; }
.time { flex: 0 0 auto; white-space: nowrap; }
.preview, .time, .thread-header p { color: var(--muted); font-size: 13px; }
.badge { flex: 0 0 auto; display: inline-flex; align-items: center; gap: 4px; border: 1px solid rgba(51, 144, 236, .34); color: var(--accent); background: #ffffff; border-radius: 999px; padding: 2px 7px; font-size: 11px; line-height: 1.2; white-space: nowrap; }
.badge-unknown { border-color: rgba(112, 117, 121, .28); color: var(--muted); background: rgba(112, 117, 121, .08); }
.badge svg { width: 14px; height: 14px; display: block; fill: currentColor; }
.row-title .badge { width: 18px; height: 18px; justify-content: center; gap: 0; padding: 0; border: 0; background: transparent; opacity: .76; }
.row-title .badge .badge-text { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.row-title .badge-telegram { color: #3390ec; }
.row-title .badge-vk { color: #2787f5; }
.row-title .badge-whatsapp { color: #25d366; }
.row-title .badge-unknown { display: none; }
.thread { width: 100%; max-width: 100%; min-width: 0; height: 100dvh; display: grid; grid-template-rows: auto minmax(0, 1fr) auto; overflow: hidden; background: var(--chat-bg) url('/wallpaper.svg') repeat; background-size: 360px 360px; }
.messages { width: 100%; max-width: 100%; min-width: 0; overflow: auto; overflow-x: hidden; padding: 16px; display: flex; flex-direction: column; gap: 7px; }
.messages.empty { align-items: center; justify-content: center; color: var(--muted); }
.day { align-self: center; margin: 12px 0; padding: 5px 10px; border-radius: 999px; background: rgba(0, 0, 0, .28); color: #fff; font-size: 12px; box-shadow: none; backdrop-filter: blur(8px); }
.bubble { position: relative; min-width: 0; max-width: min(680px, 78%); overflow: visible; overflow-wrap: anywhere; word-break: break-word; padding: 7px 10px 6px; border-radius: 16px; line-height: 1.3125; font-size: 14px; color: var(--text); box-shadow: 0 1px 1px rgba(0, 0, 0, .08); }
.bubble.incoming { align-self: flex-start; background: var(--bubble-in); }
.bubble.outgoing { align-self: flex-end; background: var(--bubble-out); }
.bubble.deleted { opacity: .76; border: 1px dashed rgba(112, 117, 121, .62); background-image: linear-gradient(var(--deleted-overlay), var(--deleted-overlay)); }
.bubble.deleted::after { content: 'удалено'; display: block; width: fit-content; margin: 4px 0 0 auto; padding: 1px 6px; border-radius: 999px; background: var(--panel); color: var(--muted); font-size: 10px; font-weight: 700; white-space: nowrap; }
.bubble.series-middle, .bubble.series-last { margin-top: 2px; }
.bubble.has-tail.incoming { border-bottom-left-radius: 6px; }
.bubble.has-tail.outgoing { border-bottom-right-radius: 6px; }
.bubble.has-tail.incoming::before { content: ''; position: absolute; left: -7px; bottom: -1px; width: 12px; height: 14px; background: var(--bubble-in); clip-path: polygon(100% 0, 100% 100%, 0 100%); }
.bubble.has-tail.outgoing::before { content: ''; position: absolute; right: -7px; bottom: 0; width: 12px; height: 14px; background: var(--bubble-out); clip-path: polygon(0 0, 100% 100%, 0 100%); }
.body { max-width: 100%; white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word; color: var(--text); }
.bubble.incoming .body, .bubble.outgoing .body { color: var(--text); }
.message-meta { display: flex; justify-content: flex-end; align-items: baseline; gap: 3px; margin-top: 3px; line-height: 1; color: var(--muted); font-size: 11px; white-space: nowrap; }
.message-meta .tick { letter-spacing: -3px; }
.message-meta .tick.sent, .message-meta .tick.delivered { color: var(--muted); }
.message-meta .tick.read { color: var(--check); }
.message-meta .tick.failed { letter-spacing: 0; color: var(--danger); font-weight: 700; }
.revision-history { margin-top: 5px; color: var(--muted); font-size: 11px; }
.revision-history summary { width: fit-content; color: var(--accent); cursor: pointer; }
.revision-row { margin-top: 4px; padding: 5px 7px; border-left: 2px solid var(--accent); background: rgba(51, 144, 236, .07); }
.revision-row time { display: block; margin-bottom: 2px; color: var(--muted); }
.message-reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 5px; }
.message-reaction { padding: 2px 7px; border: 1px solid rgba(51, 144, 236, .32); border-radius: 999px; background: rgba(51, 144, 236, .10); color: var(--text); font-size: 12px; }
.reply-quote { width: 100%; max-width: 100%; min-width: 0; display: grid; grid-template-columns: 3px minmax(0, 1fr); gap: 7px; align-items: stretch; margin: 0 0 5px; padding: 4px 6px 4px 0; border-radius: 8px; background: rgba(51, 144, 236, .08); color: inherit; text-align: left; cursor: pointer; }
.reply-quote[aria-disabled='true'] { cursor: default; }
.reply-quote-bar { display: block; width: 3px; border-radius: 3px; background: var(--accent); }
.reply-quote-main { min-width: 0; display: grid; gap: 1px; }
.reply-quote-author { min-width: 0; color: var(--accent); font-size: 12px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reply-quote-text { min-width: 0; color: var(--muted); font-size: 12px; line-height: 1.18; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bubble.quote-target { animation: quote-flash 1.35s ease-out; }
@keyframes quote-flash {
  0%, 35% { box-shadow: 0 0 0 3px rgba(51, 144, 236, .42), 0 1px 1px rgba(0, 0, 0, .08); }
  100% { box-shadow: 0 1px 1px rgba(0, 0, 0, .08); }
}
.row-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.name.unread { font-weight: 600; color: var(--text); }
.unread-badge { min-width: 18px; height: 18px; line-height: 18px; padding: 0 5px; border-radius: 9px; background: var(--accent); color: #fff; font-size: 11px; font-weight: 700; text-align: center; box-sizing: border-box; }
.content-photo { position: relative; width: min(100%, 320px); max-width: 100%; margin: 6px 0; }
.photo-open { display: block; width: min(100%, 320px); max-width: 100%; aspect-ratio: 4 / 3; min-height: 140px; max-height: 320px; overflow: hidden; background: #eef1f5; border-radius: 16px; }
.content-photo img, .attachments img { width: 100%; height: 100%; object-fit: contain; border-radius: 16px; display: block; }
.photo-fallback { position: absolute; left: 10px; bottom: 10px; display: inline-block; color: var(--accent); padding: 8px 11px; border-radius: 999px; background: rgba(255, 255, 255, .88); text-decoration: none; font-weight: 700; line-height: 1.2; box-shadow: 0 1px 2px rgba(0, 0, 0, .08); }
.content-photo.loaded .photo-fallback { display: none; }
.content-photo.failed { width: min(100%, 320px); aspect-ratio: 4 / 3; min-height: 140px; max-height: 320px; overflow: hidden; background: #eef1f5; border-radius: 16px; }
.content-photo.failed .photo-open { display: none; }
.content-photo.failed .photo-fallback { display: inline-block; }
.attachments { max-width: 100%; margin: 6px 0; padding: 0; display: grid; gap: 8px; overflow-wrap: anywhere; }
.attachments .content-photo { width: 100%; margin: 0; }
.media-box { width: min(100%, 320px); max-width: 100%; aspect-ratio: 4 / 3; min-height: 140px; max-height: 320px; overflow: hidden; background: #eef1f5; border-radius: 16px; display: grid; align-items: center; }
.attachments video, .attachments audio { width: 100%; max-width: 100%; border-radius: 16px; display: block; }
.attachments video { height: 100%; object-fit: contain; }
.file-link { display: inline-block; color: var(--accent); padding: 8px 10px; border-radius: 10px; background: rgba(51, 144, 236, .08); text-decoration: none; }
.notice { padding: 16px; color: var(--muted); }
.notice.error { color: var(--danger); background: #fff4f3; border: 1px solid #ffd7d2; margin: 12px; border-radius: 14px; text-align: left; }
.notice.retry { width: calc(100% - 24px); font-weight: 700; }
.notice.subtle { font-size: 12px; text-align: center; }
.readonly-footer { padding: 9px 14px; color: var(--muted); background: rgba(255, 255, 255, .68); border-top: 1px solid rgba(230, 230, 230, .7); font-size: 13px; text-align: center; backdrop-filter: blur(2px); }

@media (max-width: 760px) {
  .main-view { grid-template-columns: minmax(0, 1fr); }
  .sidebar { height: 100dvh; border-right: 0; }
  .thread { height: 100dvh; }
  .bubble { max-width: 78%; }
}

.reply-composer { padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); background: rgba(255, 255, 255, .86); border-top: 1px solid rgba(230, 230, 230, .76); backdrop-filter: blur(6px); }
.reply-row { display: grid; grid-template-columns: minmax(0, 1fr) 42px; gap: 8px; align-items: end; }
.reply-attach, .reply-submit { width: 42px; height: 42px; padding: 0; display: grid; place-items: center; border-radius: 50%; }
.reply-attach { background: transparent; color: var(--accent); border: 1px solid rgba(51, 144, 236, .22); font-size: 20px; }
.reply-submit:disabled { opacity: .46; cursor: default; }
.reply-composer textarea { width: 100%; min-height: 42px; max-height: 120px; resize: none; overflow-y: auto; border: 1px solid var(--line); border-radius: 21px; padding: 10px 14px; background: #ffffff; color: var(--text); outline: none; font: inherit; line-height: 20px; }
.reply-composer textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(51, 144, 236, .16); }
.reply-composer textarea:disabled { background: rgba(244, 247, 251, .86); color: var(--muted); }
.reply-attachments { display: flex; gap: 6px; overflow-x: auto; padding: 0 0 7px 50px; }
.reply-chip { flex: 0 0 auto; display: inline-flex; align-items: center; gap: 6px; max-width: 220px; border: 1px solid rgba(51, 144, 236, .24); border-radius: 999px; padding: 5px 6px 5px 10px; background: rgba(51, 144, 236, .08); color: var(--text); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reply-chip-remove { width: 20px; height: 20px; min-width: 20px; padding: 0; border-radius: 50%; background: rgba(112, 117, 121, .18); color: var(--muted); font-size: 13px; line-height: 1; }
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #101416;
    --panel: #182126;
    --panel-soft: #202b31;
    --chat-bg: #17201d;
    --bubble-in: #202b31;
    --bubble-out: #21452f;
    --text: #eef4f6;
    --muted: #a9b6bc;
    --line: #344047;
    --bubble-edge: #344047;
    --deleted-overlay: rgba(206, 216, 221, .18);
  }
  .topbar, .thread-header, .search-wrap, .filters, .business-toggle, .conversation-list { background: var(--panel); }
  input, .reply-composer textarea { background: var(--panel-soft); color: var(--text); }
  .card, .badge { background: rgba(24, 33, 38, .96); }
  .reply-composer, .readonly-footer { background: rgba(24, 33, 38, .88); }
  .bubble.deleted { opacity: .80; border-color: rgba(218, 228, 233, .62); }
}

@media (min-width: 761px) { .app-shell { max-width: 440px; margin: 0 auto; } }
