/* Тема — токенами: светлые значения здесь, тёмные — в медиазапросе ниже.
   --accent — акцент линией и рамкой; --accent-fill — заливка под белый текст;
   --link — акцентный текст (на цветной подложке --accent не дотягивает до контраста 4.5). */
:root {
  --bg: #f3f5f8; --panel: #ffffff; --line: #e1e5eb; --text: #1b1f24; --muted: #5d6672;
  --accent: #2a6fdb; --accent-fill: #2a6fdb; --accent-fill-hover: #2462c6; --accent-text: #ffffff;
  --accent-soft: #e6f0fe; --link: #1d5bbf;
  --menu: #e8f1fd; --menu-hover: #d6e6fb; --chat-bg: #f1efeb; --mine: #d9e9ff; --theirs: #ffffff;
  --ok: #2e9d57; --warn: #c98a00; --bad: #c93a3a;
  --ok-soft: #e2f4e9; --ok-text: #1c6b3a; --warn-soft: #fdf1d6; --warn-text: #7a5200;
  --neutral-soft: #eceff3; --toast-ok: #1f7a45; --toast-bad: #c0392b; --avatar-text: #1b2430;
  --radius: 12px; --radius-s: 10px; --menu-width: 176px;
  color-scheme: light dark;   /* штатные полосы прокрутки и списки — в цвет темы */
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15181c; --panel: #1d2126; --line: #2e343b; --text: #e6e9ed; --muted: #9aa3ad;
    --accent: #6aa3f5; --accent-fill: #2f6fd6; --accent-fill-hover: #2a65c8;
    --accent-soft: #1f3350; --link: #9cc3fa;
    --menu: #18222f; --menu-hover: #223247; --chat-bg: #111418; --mine: #1e3554; --theirs: #262b31;
    --ok: #3fb76b; --warn: #e0a526; --bad: #f07878;
    --ok-soft: #173524; --ok-text: #7fd6a0; --warn-soft: #3a2e12; --warn-text: #f0c060;
    --neutral-soft: #2a3037; --toast-ok: #1f7a45; --toast-bad: #b93030;
  }
}
* { box-sizing: border-box; scrollbar-width: thin; scrollbar-color: var(--line) transparent; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.4 system-ui, "Segoe UI", sans-serif; }
[hidden] { display: none !important; }
button, input, textarea, select { font: inherit; color: inherit; }
button { border: 0; border-radius: var(--radius-s); padding: 8px 16px; background: var(--accent-fill);
         color: var(--accent-text); cursor: pointer; }
/* :where — чтобы наведение не перебивало фон у кнопок-вариантов (меню, чипы, диалоги). */
button:where(:hover:not(:disabled)) { background: var(--accent-fill-hover); }
button:disabled { opacity: .5; cursor: default; }
button.ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); }
button.ghost:hover:not(:disabled) { background: var(--neutral-soft); color: var(--text); }
input, textarea, select { border: 1px solid var(--line); border-radius: var(--radius-s);
                          padding: 8px 12px; background: var(--panel); min-width: 0; }
input:hover, textarea:hover, select:hover { border-color: var(--muted); }
::placeholder { color: var(--muted); opacity: 1; }
input[type="checkbox"] { width: 18px; height: 18px; margin: 0; accent-color: var(--accent-fill); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.muted { color: var(--muted); font-size: 13px; }
.error { color: var(--bad); min-height: 1.4em; margin: 0; }
.sprite { position: absolute; }
.icon { flex: none; width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8;
        stroke-linecap: round; stroke-linejoin: round; }
/* Пустое состояние: значок и пояснение по центру. */
.empty { display: flex; flex-direction: column; align-items: center; gap: 12px; max-width: 440px;
         color: var(--muted); font-size: 14px; text-align: center; }
.empty .icon { width: 48px; height: 48px; stroke-width: 1.3; color: var(--accent); }
.notice { padding: 8px 12px; border-radius: var(--radius-s); background: var(--warn-soft);
          color: var(--warn-text); }

.login { max-width: 340px; margin: 15vh auto 0; padding: 24px; background: var(--panel);
         border: 1px solid var(--line); border-radius: var(--radius);
         display: flex; flex-direction: column; gap: 12px; }
.login h1 { font-size: 20px; margin: 0 0 4px; }

/* Три колонки: меню разделов на всю высоту, список диалогов, чат. Шапка с аккаунтами
   и страницы разделов занимают всё, что правее меню. Ширину меню задаёт --menu-width. */
.app { height: 100%; display: grid;
       grid-template: auto minmax(0, 1fr) / var(--menu-width) 360px minmax(0, 1fr); }
.topbar { grid-column: 2 / -1; grid-row: 1; display: flex; align-items: center; gap: 8px;
          padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }

/* --- меню разделов --- */
.tabs { grid-column: 1; grid-row: 1 / -1; display: flex; flex-direction: column; gap: 4px;
        min-height: 0; padding: 12px 8px; overflow-y: auto; background: var(--menu);
        border-right: 1px solid var(--line); }
.tab, .menu-toggle { position: relative; display: flex; align-items: center; gap: 12px; width: 100%;
                     min-height: 40px; padding: 8px 10px; background: transparent;
                     color: var(--text); text-align: left; white-space: nowrap; }
.tab { font-weight: 500; }
.tab .icon { color: var(--link); }
.tab:hover, .menu-toggle:hover { background: var(--menu-hover); }
.tab[aria-current="page"] { background: var(--accent-fill); color: var(--accent-text); }
.tab[aria-current="page"] .icon { color: inherit; }
.tab-label { overflow: hidden; text-overflow: ellipsis; }
.menu-toggle { margin-top: auto; color: var(--muted); font-size: 13px; }
.badge { min-width: 20px; padding: 0 6px; border-radius: 10px; font-size: 12px; line-height: 20px;
         text-align: center; background: var(--accent-fill); color: var(--accent-text); }
.tab .badge { margin-left: auto; }
.tab[aria-current="page"] .badge { background: var(--panel); color: var(--link); }

@media (min-width: 761px) {
  /* Свёрнутое меню: только значки; подписи остаются в title и aria-label кнопок. */
  .app[data-menu="collapsed"] { --menu-width: 56px; }
  .app[data-menu="collapsed"] .tab-label { display: none; }
  .app[data-menu="collapsed"] :is(.tab, .menu-toggle) { justify-content: center; padding: 8px 0; }
  .app[data-menu="collapsed"] .menu-toggle .icon { transform: rotate(180deg); }
  .app[data-menu="collapsed"] .tab .badge { position: absolute; top: 1px; right: 1px; min-width: 16px;
                                             margin: 0; padding: 0 4px; font-size: 10px;
                                             line-height: 16px; }
}

/* --- аккаунты в шапке --- */
.accounts { display: flex; gap: 8px; flex: 1; min-width: 0; overflow-x: auto; padding: 2px; }
/* min-content: в тесноте сжимается и переносится примечание, а имя с номером остаётся целым. */
.chip { display: flex; flex-direction: column; justify-content: center; flex: 0 1 auto;
        min-width: min-content; max-width: 232px; min-height: 44px; padding: 4px 12px;
        background: transparent; color: var(--text); border: 1px solid var(--line);
        text-align: left; }
.chip:hover { background: var(--neutral-soft); }
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); }
.chip-name { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
             font-size: 14px; font-weight: 600; line-height: 1.3; }
.chip-name::before { content: ""; display: inline-block; width: 8px; height: 8px; margin-right: 6px;
                     border-radius: 50%; background: var(--warn); }
.chip[data-status="ok"] .chip-name::before { background: var(--ok); }
.chip[data-status="error"] .chip-name::before, .chip[data-status="dead"] .chip-name::before,
.chip[data-status="banned"] .chip-name::before, .chip[data-status="stopped"] .chip-name::before,
.chip[data-status="frozen"] .chip-name::before { background: var(--bad); }
/* Примечание переносится на вторую строку, а не обрезается: предупреждение должно читаться. */
.chip-note { padding-left: 14px; color: var(--muted); font-size: 12px; line-height: 1.25;
             overflow-wrap: anywhere; }
.chip-note:empty { display: none; }
.chip-note.warn { color: var(--text); }

/* --- список диалогов --- */
.sidebar { grid-column: 2; grid-row: 2; display: flex; flex-direction: column; gap: 8px;
           padding: 12px 8px 0; min-height: 0; background: var(--panel);
           border-right: 1px solid var(--line); }
.sidebar > input, .find { margin: 0 4px; }
.find { display: flex; gap: 8px; }
.find input { flex: 1; }
.dialogs { list-style: none; margin: 0; padding: 0 0 8px; overflow-y: auto; flex: 1; min-height: 0; }
.dialog { display: grid; grid-template-columns: 40px minmax(0, 1fr) auto;
          grid-template-areas: "avatar head time" "avatar preview count"; gap: 2px 8px;
          align-items: center; width: 100%; padding: 8px; background: transparent;
          color: var(--text); text-align: left; }
.dialog:hover { background: var(--bg); }
.dialog[aria-current="true"] { background: var(--accent-soft); }
.dialog .avatar { grid-area: avatar; }
.dialog-head { grid-area: head; display: flex; align-items: center; gap: 4px; min-width: 0;
               overflow: hidden; }
.dialog-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
                min-width: 3ch; unicode-bidi: isolate; }
.dialog-preview { grid-area: preview; color: var(--muted); font-size: 13px; overflow: hidden;
                  text-overflow: ellipsis; white-space: nowrap; }
.dialog:not(:has(.badge)) .dialog-preview { grid-column: 2 / -1; }
.dialog-time { grid-area: time; color: var(--muted); font-size: 12px; text-align: right; }
.dialog .badge { grid-area: count; justify-self: end; }
/* Круг с инициалами. Буквы — из чужого названия: isolate и overflow не дают им вылезти. */
.avatar { display: inline-flex; align-items: center; justify-content: center; flex: none;
          width: 40px; height: 40px; border-radius: 50%; overflow: hidden; background: var(--neutral-soft);
          color: var(--avatar-text); font-size: 14px; font-weight: 600; line-height: 1;
          white-space: nowrap; unicode-bidi: isolate; user-select: none; }
.av-0 { background: #ffd6d1; } .av-1 { background: #ffe2bd; } .av-2 { background: #fff0b3; }
.av-3 { background: #d3f0c8; } .av-4 { background: #c5ede6; } .av-5 { background: #cfe4ff; }
.av-6 { background: #ddd6ff; } .av-7 { background: #ffd3ea; }
/* Метка-пилюля: тихая по умолчанию, цветная — для статусов. */
.tag { flex: none; padding: 1px 8px; border-radius: 10px; background: var(--neutral-soft);
       color: var(--muted); font-size: 11px; font-weight: 400; line-height: 16px;
       white-space: nowrap; }
.dialog .tag { padding: 0 5px; }
.dialog-title + .tag { margin-left: 2px; }
.dialog[aria-current="true"] .tag { background: var(--panel); }
.tag-new { background: var(--accent-soft); color: var(--link); }
.tag-done { background: var(--ok-soft); color: var(--ok-text); }
.tag-off, .tag-history { background: var(--warn-soft); color: var(--warn-text); }

/* --- чат --- */
.chat { grid-column: 3; grid-row: 2; min-width: 0; min-height: 0; display: flex;
        flex-direction: column; background: var(--chat-bg); }
.chat-empty { margin: auto; }
.chat-open { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.chat-head { display: flex; align-items: center; gap: 12px; padding: 8px 16px;
             background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-head-text { display: flex; flex-direction: column; min-width: 0; }
.chat-head-text > * { overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
                      unicode-bidi: isolate; }
.messages { flex: 1; min-height: 0; overflow-y: auto; padding: 16px;
            display: flex; flex-direction: column; gap: 8px; }
.older { align-self: center; font-size: 13px; padding: 4px 12px; }
.messages > .older { background: var(--panel); }
.message { max-width: min(640px, 80%); padding: 8px 12px; border-radius: 16px;
           background: var(--theirs); box-shadow: 0 1px 1px rgba(0, 0, 0, .06);
           align-self: flex-start; white-space: pre-wrap; overflow-wrap: anywhere; }
.message.out { background: var(--mine); align-self: flex-end; }
.message-sender { display: block; font-size: 12px; font-weight: 600; color: var(--link); }
.message-quote { display: block; margin-bottom: 4px; padding-left: 8px; font-size: 13px;
                 color: var(--muted); border-left: 2px solid var(--accent); white-space: nowrap;
                 overflow: hidden; text-overflow: ellipsis; }
.message-media { display: block; color: var(--muted); }
.message-text { display: block; }
.message-image { display: block; max-width: 100%; max-height: 360px; border-radius: 8px; }
.message-file { color: var(--link); }
.message-foot { display: flex; justify-content: flex-end; align-items: baseline; gap: 12px;
                margin-top: 2px; }
.message-time { font-size: 11px; color: var(--muted); }
button.link { padding: 0; background: transparent; color: var(--muted); font-size: 12px;
              opacity: 0; }
.message:hover button.link, button.link:focus-visible { opacity: 1; }
button.small { padding: 4px 12px; font-size: 13px; }
.pending { display: flex; align-items: center; gap: 8px; padding: 8px 16px; font-size: 13px;
           background: var(--panel); border-top: 1px solid var(--line); }
.pending span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis;
                white-space: nowrap; }
@media (hover: none) { button.link { opacity: 1; } }
.composer { display: flex; align-items: flex-end; gap: 8px; padding: 12px 16px;
            background: var(--panel); border-top: 1px solid var(--line); }
.composer textarea { flex: 1; resize: none; padding: 8px 16px; border-radius: 18px;
                     background: var(--bg); }
.composer button { min-height: 40px; border-radius: 20px; }
button.icon-button { display: inline-flex; align-items: center; justify-content: center;
                     width: 40px; padding: 0; }

/* --- окна --- */
.modal { width: min(400px, 92vw); padding: 24px; border: 1px solid var(--line);
         border-radius: var(--radius); background: var(--panel); color: var(--text); }
.modal.wide { width: min(560px, 92vw); }
.modal::backdrop { background: rgba(0, 0, 0, .45); }
.modal form, .form-col { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.modal h2 { margin: 0 0 4px; font-size: 20px; }
.modal h3 { margin: 12px 0 0; padding-top: 12px; font-size: 15px; border-top: 1px solid var(--line); }
.modal label { margin-top: 6px; font-size: 13px; color: var(--muted); }
.modal label.check { display: flex; align-items: center; gap: 8px; margin-top: 12px;
                     font-size: 15px; color: var(--text); }
.modal textarea { resize: vertical; }
.modal-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
button.danger { margin-left: auto; color: var(--bad); }
.profile-photo { width: 96px; height: 96px; border-radius: 50%; object-fit: cover; }
@media (min-width: 900px) {
  /* Окно проекта в две колонки: слева основное, справа блок ИИ-агента; кнопки — на всю ширину. */
  .modal.wide { width: min(880px, 92vw); }
  .modal .project-form { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
                         gap: 8px 24px; align-items: start; }
  .project-form > :not(.form-col) { grid-column: 1 / -1; }
  .form-aside { padding: 16px; border-radius: var(--radius); background: var(--bg); }
  .form-aside h3 { margin: 0; padding: 0; border: 0; }
}

/* --- страницы разделов: на всю ширину под шапкой, вместо списка диалогов и чата --- */
.page { grid-column: 2 / -1; grid-row: 2 / -1; min-height: 0; overflow-y: auto; padding: 20px 24px; }
.page-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px;
             margin-bottom: 16px; }
.page-head h2 { flex: 1; margin: 0; font-size: 22px; }
.page-head select { max-width: 100%; }
.page > .muted, .group-body > .muted { margin: 0 0 12px; }
.page > .empty, .group-body > .empty { margin: 48px auto; }
.group-status { margin-bottom: 8px; }
.group-status .card-line { margin-bottom: 2px; }
.group-forms { margin-bottom: 8px; }
.card textarea { resize: vertical; }
.group-title { display: flex; align-items: center; gap: 8px; margin: 20px 0 8px; font-size: 15px; }
/* Статус раздела групп — цветной точкой у заголовка. */
.group-title::before { content: ""; width: 8px; height: 8px; border-radius: 50%;
                       background: var(--muted); }
.group-section[data-status="candidate"] .group-title::before,
.group-section[data-status="watching"] .group-title::before,
.group-section[data-status="approved"] .group-title::before { background: var(--accent); }
.group-section[data-status="joined"] .group-title::before { background: var(--ok); }
.group-section[data-status="requested"] .group-title::before { background: var(--warn); }
.group-section[data-status="failed"] .group-title::before { background: var(--bad); }
.page > .older { display: block; margin: 12px auto 0; }
.cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px;
         grid-template-columns: repeat(auto-fill, minmax(min(320px, 100%), 1fr)); }
.card { display: flex; flex-direction: column; gap: 8px; min-width: 0; padding: 16px;
        background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.card-head { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; }
/* isolate: чужое название с символами «переворота текста» не трогает соседние метки. */
/* overflow: стопка надстрочных знаков в чужом названии не должна закрашивать соседние карточки. */
.card-title { font-weight: 600; overflow-wrap: anywhere; unicode-bidi: isolate; overflow: hidden; }
#groups-lists[inert] { opacity: .6; }
.card-text { margin: 0; font-size: 13px; color: var(--muted); overflow-wrap: anywhere;
             display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3;
             overflow: hidden; }
.card button { align-self: flex-start; margin-top: auto; }
.card-time { margin-left: auto; font-size: 12px; color: var(--muted); }
/* Чужой текст целиком, с его переносами строк: по обрезку лид не оценить. */
/* Высота ограничена: сообщение из сотен пустых строк не должно вытеснить остальные лиды. */
.lead-text { margin: 0; max-height: 12em; overflow-y: auto; white-space: pre-wrap;
             overflow-wrap: anywhere; }
.card-line { margin: 0; font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.card-line a, .card-actions a { color: var(--link); }
.card-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: auto;
                padding-top: 4px; }
.card-actions button { margin-top: 0; }
.card-actions a { margin-right: auto; font-size: 14px; }

.profile-premium { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line);
                   display: flex; flex-direction: column; gap: 6px; }
.profile-premium-links { display: flex; gap: 16px; }
.profile-premium-links a { color: var(--link); font-size: 14px; }

/* Сверху, а не снизу: внизу поле ввода, в котором при отказе остался текст. */
.toast { position: fixed; left: 50%; top: 12px; transform: translateX(-50%); max-width: 90vw;
         padding: 10px 16px; border-radius: var(--radius); background: var(--toast-bad);
         color: #fff; z-index: 10; box-shadow: 0 4px 16px rgba(0, 0, 0, .18); }
.toast.ok { background: var(--toast-ok); }

@media (max-width: 760px) {
  /* Узкий экран: бокового меню нет — разделы строкой сверху, под ними шапка с аккаунтами,
     список диалогов и чат друг под другом. Меню здесь не сворачивается. */
  .app { grid-template: auto auto minmax(0, 40%) minmax(0, 1fr) / minmax(0, 1fr); }
  .tabs { grid-column: 1; grid-row: 1; flex-direction: row; padding: 4px 8px;
          overflow-y: visible; border-right: 0; border-bottom: 1px solid var(--line); }
  .tab { flex: 1 1 0; flex-direction: column; justify-content: center; gap: 2px; min-width: 0;
         padding: 4px; font-size: 12px; }
  .tab-label { max-width: 100%; }
  .tab .badge { position: absolute; top: 2px; left: calc(50% + 6px); min-width: 16px; margin: 0;
                padding: 0 4px; font-size: 10px; line-height: 16px; }
  .menu-toggle { display: none; }
  .topbar { grid-column: 1; grid-row: 2; flex-wrap: wrap; justify-content: flex-end;
            padding: 8px 12px; }
  .sidebar { grid-column: 1; grid-row: 3; border-right: 0; border-bottom: 1px solid var(--line); }
  .chat { grid-column: 1; grid-row: 4; }
  .page { grid-column: 1; grid-row: 3 / -1; padding: 16px 12px; }
  /* Аккаунты — своей строкой во всю ширину, по три чипа в ряд; кнопки шапки — строкой ниже. */
  .accounts { flex: 1 1 100%; flex-wrap: wrap; gap: 6px; padding: 0; overflow-x: visible; }
  .chip { flex: 1 1 auto; min-width: calc((100% - 13px) / 3); max-width: 100%; padding: 4px 8px; }
  .chip-name { font-size: 13px; }
  .chip-note { padding-left: 0; }
  /* 16px: при меньшем шрифте iOS сам приближает страницу на фокусе поля. */
  input, textarea, select { font-size: 16px; }
}
