:root {
  color-scheme: light dark;
  --accent: #3390ec;          /* синий Telegram */
  --bg: #ffffff;
  --bg-chat: #e6ebee;         /* фон ленты сообщений */
  --bubble-in: #ffffff;
  --bubble-out: #eeffde;      /* зелёный «свои» как в Telegram */
  --text: #000000;
  --muted: #707579;
  --line: #e4e4e5;
  --header: #ffffff;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17212b;
    --bg-chat: #0e1621;
    --bubble-in: #182533;
    --bubble-out: #2b5278;
    --text: #ffffff;
    --muted: #8a949d;
    --line: #101921;
    --header: #17212b;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* hidden обязан побеждать: любое display в правилах ниже перебивает его
   по специфичности, и «скрытый» элемент остаётся на экране пустым. */
[hidden] { display: none !important; }
/* Двойной тап больше не зумит страницу — это мессенджер, а не документ. */
html { touch-action: manipulation; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.35 -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
}

/* ---------- вход ---------- */
.center { display: flex; min-height: 100vh; align-items: center; justify-content: center; }
.card { display: flex; flex-direction: column; gap: 14px; padding: 28px; min-width: 280px; }
.card h1 { text-align: center; }
.error { color: #e53935; margin: 0; text-align: center; }

/* ---------- шапка ---------- */
header {
  display: flex; gap: 10px; align-items: center;
  padding: 10px 14px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--header); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
h1 { font-size: 19px; margin: 0; flex: 1; font-weight: 600; }
.badge:not(:empty) {
  background: var(--accent); color: #fff; border-radius: 12px;
  padding: 1px 8px; font-size: 13px; font-weight: 500; vertical-align: middle;
}

input, button, select { font: inherit; }
input, select {
  padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
}
button {
  padding: 10px 14px; border: 0; border-radius: 10px;
  background: var(--accent); color: #fff; cursor: pointer;
}
select { padding: 6px 10px; font-size: 14px; }

#enable-push {
  display: block; width: calc(100% - 28px); margin: 10px 14px;
  background: var(--accent); font-size: 15px;
}
#enable-push.hint { background: transparent; color: var(--muted); font-size: 14px;
                    border: 1px solid var(--line); cursor: default; }

/* ---------- список чатов ---------- */
ul { list-style: none; margin: 0; padding: 0; }
.chat a {
  display: grid; grid-template-columns: 48px 1fr;
  grid-template-areas: "av row1" "av row2" "av acc";
  column-gap: 12px; align-items: center;
  padding: 9px 14px; text-decoration: none; color: inherit;
}
.chat + .chat a { border-top: 1px solid var(--line); }
.chat a:active { background: var(--bg-chat); }

.av-wrap { grid-area: av; position: relative; width: 48px; height: 48px;
           flex-shrink: 0; }
.chan {
  position: absolute; right: -3px; bottom: -3px;
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--bg); padding: 1.5px;
  display: flex; align-items: center; justify-content: center;
}
.chan svg { width: 100%; height: 100%; display: block; border-radius: 50%; }
.chatpage header .av-wrap { width: 36px; height: 36px; }
.chatpage header .chan { width: 15px; height: 15px; font-size: 8px; }

.avatar {
  width: 48px; height: 48px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 17px; font-weight: 600; letter-spacing: .5px;
  flex-shrink: 0;
}
.avatar.photo { background-size: cover; background-position: center; }
.avatar.small { width: 36px; height: 36px; font-size: 14px; }
/* min-width:0 обязателен: без него flex-элемент с длинным текстом отказывается
   сжиматься, строка распирается и залезает на соседей. */
.row1 { grid-area: row1; display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.row2 { grid-area: row2; display: flex; align-items: center; gap: 8px; min-width: 0; }
/* Обычный вес у всех: раньше было 600 против 700 у непрочитанного — разница
   на телефоне неразличима, и весь список выглядел непрочитанным.
   Признак непрочитанного — синий счётчик, как в Telegram. */
.chat .title { font-weight: 500; flex: 1; min-width: 0; overflow: hidden;
               text-overflow: ellipsis; white-space: nowrap; }
.chat .time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.chat .preview {
  flex: 1; min-width: 0; font-size: 15px; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat .preview .mine { color: var(--accent); }
.chat .count {
  background: var(--accent); color: #fff; border-radius: 11px;
  min-width: 22px; height: 22px; padding: 0 6px; font-size: 13px; font-weight: 500;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.chat .account { grid-area: acc; font-size: 12px; color: var(--muted); opacity: .8; }
.chat.unread .title { font-weight: 700; }
.chat.unread .preview { color: var(--text); }
/* Кнопка — сосед ссылки во flex, а не наложение поверх неё:
   иначе она перекрывает превью и счётчик непрочитанного. */
.chat { display: flex; align-items: center; }
.chat > a { flex: 1; min-width: 0; }
.noreply {
  flex-shrink: 0; margin: 0 14px 0 4px;
  width: 36px; height: 36px; padding: 0; border-radius: 50%;
  font-size: 17px; line-height: 1;
  background: transparent; color: var(--muted); border: 1px solid var(--line);
}
.noreply:active { background: var(--accent); color: #fff; border-color: var(--accent); }
.gear { text-decoration: none; font-size: 20px; color: var(--muted); padding: 0 2px; }

/* ---------- настройки ---------- */
.settings section { padding: 14px; border-bottom: 1px solid var(--line); }
.settings h2 { font-size: 14px; text-transform: uppercase; letter-spacing: .5px;
               color: var(--muted); margin: 0 0 10px; font-weight: 600; }
.settings .note { font-size: 13px; color: var(--muted); margin: 8px 0 0; }
.settings code { font-size: 12px; word-break: break-all; }
.settings #enable-push { width: 100%; margin: 0; }
.accounts li { display: grid; grid-template-columns: 10px 1fr auto;
               grid-template-areas: "dot label state" ". phone state";
               gap: 2px 10px; align-items: center; padding: 8px 0;
               border-bottom: 1px solid var(--line); }
.accounts li:last-child { border-bottom: 0; }
.accounts .dot { grid-area: dot; width: 8px; height: 8px; border-radius: 50%; }
.accounts .dot.ok { background: #4caf50; }
.accounts .dot.bad { background: #e53935; }
.accounts .label { grid-area: label; font-weight: 600; }
.accounts .phone { grid-area: phone; font-size: 13px; color: var(--muted); }
.accounts .state { grid-area: state; font-size: 13px; color: var(--muted); }
button.danger { background: transparent; color: #e53935;
                border: 1px solid var(--line); width: 100%; }

/* ---------- переписка ---------- */
.chatpage { display: flex; flex-direction: column; height: 100dvh; }
.chatpage header .who { display: flex; flex-direction: column; line-height: 1.2; }
.chatpage header .name { font-weight: 600; font-size: 17px; }
.chatpage header .account { font-size: 12px; color: var(--muted); }
.back { text-decoration: none; font-size: 30px; color: var(--accent);
        line-height: 1; padding: 0 6px 4px 0; }

#feed {
  flex: 1; overflow-y: auto; background: var(--bg-chat);
  padding: 12px 10px; display: flex; flex-direction: column; gap: 4px;
}
.msg {
  position: relative; max-width: 80%; width: fit-content;
  padding: 6px 10px 6px 10px; border-radius: 12px;
  background: var(--bubble-in); box-shadow: 0 1px 1px rgba(0,0,0,.08);
  font-size: 16px; word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.in  { align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.out { align-self: flex-end; background: var(--bubble-out);
           border-bottom-right-radius: 4px; }
.msg .text { white-space: pre-wrap; }
.msg .meta { font-size: 11px; color: var(--muted); float: right;
             margin: 6px 0 0 8px; user-select: none; }
.msg.out.pending { opacity: .6; }
.msg.out.failed { background: #e57373; color: #fff; }
.msg.out.failed .meta, .msg.out.failed .err { color: rgba(255,255,255,.85); }
/* Постоянный размер рамки: картинка грузится лениво, и без фиксированной
   высоты лента дёргалась бы под пальцем по мере подгрузки. */
img.media, video.media {
  width: 240px; max-width: 100%; height: 180px; object-fit: cover;
  border-radius: 8px; display: block; margin: -2px 0 4px;
  background: rgba(128,128,128,.18);
}
audio.media { width: 220px; }

#scroll-down {
  position: absolute; right: 12px; bottom: 74px;
  width: 40px; height: 40px; padding: 0; border-radius: 50%;
  font-size: 20px; line-height: 1;
  background: var(--header); color: var(--accent);
  box-shadow: 0 2px 8px rgba(0,0,0,.2);
  opacity: 0; pointer-events: none; transition: opacity .15s;
}
#scroll-down.shown { opacity: 1; pointer-events: auto; }
.chatpage { position: relative; }

/* Стикер — без пузыря, как в Telegram */
.msg.bare { background: none; box-shadow: none; padding: 2px 0; max-width: 60%; }
.msg.bare .meta { color: var(--muted); }
.sticker { width: 160px; height: 160px; object-fit: contain; display: block; }
.sticker-stub {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  font-size: 56px; line-height: 1;
}
.sticker-stub small { font-size: 12px; color: var(--muted); }
.file { color: var(--accent); text-decoration: none; }
.msg .err { display: block; font-size: 12px; margin-top: 2px; }
.retry { font-size: 12px; padding: 3px 10px; margin-top: 6px;
         background: rgba(255,255,255,.25); }

#composer {
  display: flex; gap: 6px; align-items: center; padding: 8px 10px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--header); border-top: 1px solid var(--line);
}
#composer input[type=text], #composer #text { flex: 1; min-width: 0;
                                              border-radius: 20px; padding: 11px 16px; }
#composer button { border-radius: 50%; width: 42px; height: 42px; padding: 0;
                   font-size: 17px; flex-shrink: 0; }
#composer #attach, #composer #schedule {
  background: transparent; color: var(--muted); font-size: 19px;
}

#attach-bar, #schedule-bar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; font-size: 14px;
  background: var(--header); border-top: 1px solid var(--line); color: var(--muted);
}
#attach-bar span, #schedule-bar label { flex: 1; min-width: 0;
                                        overflow: hidden; text-overflow: ellipsis;
                                        white-space: nowrap; }
#attach-bar button, #schedule-bar button {
  width: 28px; height: 28px; padding: 0; font-size: 14px;
  background: transparent; color: var(--muted); border: 1px solid var(--line);
  border-radius: 50%; flex-shrink: 0;
}
#schedule-bar input { padding: 6px 8px; font-size: 14px; margin-left: 6px; }
.msg .scheduled { display: block; font-size: 12px; opacity: .85; margin-top: 2px; }

/* Отложенная отправка: таймер рядом с «отправить», как в Telegram */
#composer #schedule { position: relative; }
#sched-badge {
  position: absolute; top: -2px; right: -4px;
  background: var(--accent); color: #fff; border-radius: 9px;
  font-size: 10px; line-height: 1; padding: 3px 5px; font-weight: 600;
}
#schedule-pop {
  position: absolute; right: 10px; bottom: 68px; z-index: 10;
  min-width: 210px; max-width: calc(100vw - 20px);
  background: var(--header); border: 1px solid var(--line); border-radius: 14px;
  padding: 10px; box-shadow: 0 6px 24px rgba(0,0,0,.28);
  display: flex; flex-direction: column; gap: 6px; font-size: 15px;
}
#schedule-pop .title { font-size: 12px; color: var(--muted);
                       text-transform: uppercase; letter-spacing: .4px;
                       padding: 2px 4px 4px; }
#schedule-pop .quick {
  background: transparent; color: var(--text); text-align: left;
  padding: 10px 12px; border-radius: 10px; font-size: 15px;
}
#schedule-pop .quick:active { background: var(--bg-chat); }
#schedule-pop .custom { display: flex; flex-direction: column; gap: 6px;
                        font-size: 12px; color: var(--muted);
                        padding: 6px 4px 2px; border-top: 1px solid var(--line); }
#schedule-pop .custom input { width: 100%; font-size: 15px; color: var(--text); }
#schedule-pop .row { display: flex; gap: 8px; }
#schedule-pop button { width: auto; height: auto; border-radius: 8px;
                       padding: 8px 14px; font-size: 14px; }
#sched-clear { background: transparent; color: var(--muted);
               border: 1px solid var(--line); }
.msg .scheduled button { width: auto; height: auto; border-radius: 8px;
                         padding: 2px 8px; font-size: 11px; margin-left: 6px;
                         background: rgba(255,255,255,.3); color: inherit; }

/* Настройки */
.toggle { display: flex; align-items: center; gap: 10px; font-size: 15px; }
.toggle input { width: 20px; height: 20px; }
button.wide { width: 100%; }
.settings textarea, .settings input:not([type=checkbox]), .settings select {
  width: 100%; font: inherit; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
}

.msg .ticks { opacity: .7; }
.msg .ticks.read { color: #4fae4e; opacity: 1; }
.msg.out .ticks.read { color: #59b96a; }
.msg .edited { font-size: 11px; color: var(--muted); margin-left: 6px; }
/* Долгое нажатие вместо клика: курсор-указатель обманывал бы. */
.msg.out[data-sent="1"] { cursor: default; -webkit-touch-callout: none; }

/* Окно ввода: prompt() браузера однострочный и режет длинный текст */
.modal-back {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,.45);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 12px; padding-bottom: max(12px, env(safe-area-inset-bottom));
}
@media (min-height: 600px) { .modal-back { align-items: center; } }
.modal {
  width: 100%; max-width: 460px;
  background: var(--bg); border-radius: 16px; padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
  box-shadow: 0 10px 40px rgba(0,0,0,.35);
}
.modal-title { font-weight: 600; font-size: 16px; }
.modal textarea {
  width: 100%; font: inherit; font-size: 16px; line-height: 1.4;
  padding: 12px; border-radius: 10px; resize: vertical;
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
}
.modal-row { display: flex; gap: 10px; justify-content: flex-end; }
.modal-row button { padding: 10px 18px; }
.modal-row .cancel { background: transparent; color: var(--muted);
                     border: 1px solid var(--line); }

/* Ссылки в сообщениях */
.msg a { color: var(--accent); word-break: break-all; }
.msg.out a { color: #1a6ea8; }
@media (prefers-color-scheme: dark) { .msg.out a { color: #8fd0ff; } }

/* Прежний текст отредактированного сообщения — раскрывается по стрелке */
.msg .orig { margin-top: 4px; }
.msg .orig summary { font-size: 11px; color: var(--muted); cursor: pointer;
                     list-style: none; user-select: none; }
.msg .orig summary::-webkit-details-marker { display: none; }
.msg .orig summary::before { content: "▸ "; }
.msg .orig[open] summary::before { content: "▾ "; }
.msg .was { display: block; font-size: 14px; opacity: .75;
            text-decoration: line-through; margin-top: 2px; white-space: pre-wrap; }

/* Название сверху, поле снизу: на 375 px длинное «Квартира-студия, 65 м²,
   8 кроватей» рядом с полем не помещается и обрезается до бессмыслицы. */
.adverts li { display: block; padding: 10px 0;
              border-bottom: 1px solid var(--line); }
.adverts li:last-child { border-bottom: 0; }
.adv-title { display: block; font-size: 14px; line-height: 1.3;
             margin-bottom: 6px; color: var(--text); }
/* Специфичнее общего правила «.settings input { width: 100% }» — иначе поле
   занимало всю строку и выдавливало название. */
.adverts li .adv-no { width: 150px; padding: 8px 12px; font-size: 15px; }

/* Разделитель дня в переписке */
.day-sep {
  align-self: center; margin: 10px 0 6px;
  padding: 3px 12px; border-radius: 12px;
  background: rgba(0,0,0,.10); color: var(--muted);
  font-size: 12px; font-weight: 500;
}
@media (prefers-color-scheme: dark) { .day-sep { background: rgba(255,255,255,.10); } }

/* Автоответы */
.head-btn { padding: 8px 12px; font-size: 14px; border-radius: 10px; }
.ar-list { padding: 8px 0; }
.ar-item { display: grid; grid-template-columns: 10px 1fr auto; gap: 4px 10px;
           align-items: center; padding: 12px 14px;
           border-bottom: 1px solid var(--line); cursor: pointer; }
.ar-item.active { background: var(--bg-chat); }
.ar-item .dot { width: 8px; height: 8px; border-radius: 50%; }
.ar-item .dot.ok { background: #4caf50; }
.ar-item .dot.off { background: #bbb; }
.ar-item .ar-name { font-weight: 600; }
.ar-item .ar-kind { font-size: 12px; color: var(--muted); }
.row-field { display: flex; align-items: center; justify-content: space-between;
             gap: 12px; padding: 8px 0; font-size: 15px; }
.row-field input { width: 120px; }
.ar-preview { font-style: italic; }
.muted { color: var(--muted); }

.wide-link { display: block; text-align: center; padding: 12px;
             background: var(--accent); color: #fff; border-radius: 10px;
             text-decoration: none; font-size: 15px; }

/* Закрепление чата свайпом */
.chat { transition: transform .18s ease; position: relative; }
.chat.will-pin { background: rgba(51,144,236,.12); }
.chat.pinned { background: rgba(128,128,128,.06); }
.pin-mark { font-size: 11px; flex-shrink: 0; }

/* Статистика */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(90px, 1fr));
         gap: 8px; margin: 4px 0 8px; }
.tile { background: var(--bg-chat); border-radius: 12px; padding: 12px 10px;
        text-align: center; }
.tile b { display: block; font-size: 20px; font-weight: 700; }
.tile span { font-size: 12px; color: var(--muted); }
.tile.warn b { color: #e8890c; }
.tile.bad b { color: #e53935; }
.bar-row { display: grid; grid-template-columns: 88px 1fr 40px; gap: 8px;
           align-items: center; padding: 5px 0; font-size: 14px; }
.bar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { background: var(--bg-chat); border-radius: 6px; height: 18px; }
.bar-fill { display: block; height: 100%; border-radius: 6px; background: var(--accent); }
.bar-fill.alt { background: #7bc862; }
.bar-value { text-align: right; color: var(--muted); font-size: 13px; }
.hours { display: flex; align-items: flex-end; gap: 2px; height: 70px; margin-top: 6px; }
.hour { flex: 1; height: 100%; display: flex; align-items: flex-end; }
.hour-fill { width: 100%; background: var(--accent); border-radius: 2px 2px 0 0;
             min-height: 1px; }
.hours-axis { display: flex; justify-content: space-between; font-size: 11px;
              color: var(--muted); margin-top: 4px; }
.ar-tabs a.ar-tab { text-decoration: none; }

/* Вкладки периода (стили потерялись при переделке экрана автоответов) */
.ar-tabs { display: flex; gap: 8px; padding: 12px 14px 4px; }
.ar-tab {
  flex: 1; text-align: center; padding: 10px 8px; font-size: 14px;
  border: 1px solid var(--line); border-radius: 10px;
  background: transparent; color: var(--muted); text-decoration: none;
  display: flex; align-items: center; justify-content: center; gap: 6px;
}
.ar-tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Подпись НАД полосой: рядом с ней длинные названия каналов обрезались
   до неразличимого «Авито AV A…» — а начинаются они одинаково. */
.bar-block { padding: 6px 0; }
.bar-name { display: flex; justify-content: space-between; gap: 10px;
            font-size: 14px; margin-bottom: 4px; }
.bar-num { color: var(--muted); flex-shrink: 0; }
/* span внутри блока остаётся строчным, и height к нему не применяется —
   полосы просто исчезали. */
.bar-block .bar-track { display: block; width: 100%; }
