* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body { font-family: -apple-system, Segoe UI, Roboto, sans-serif; color: #1c1e21; background: #f0f2f5; }
.app { display: grid; grid-template-columns: 340px 1fr; height: 100vh; height: 100dvh; }

/* Список клиентов */
.list { background: #fff; border-right: 1px solid #e0e0e0; display: flex; flex-direction: column; overflow: hidden; min-height: 0; }
.list-head { padding: 14px 16px; border-bottom: 1px solid #eee; display: flex; align-items: baseline; gap: 10px; flex: none; position: sticky; top: 0; background: #fff; z-index: 30; }
.logo { font-weight: 700; font-size: 18px; color: #0a7cff; }
.count { font-size: 12px; color: #888; margin-left: auto; }
.search { margin: 10px 12px; padding: 9px 12px; border: 1px solid #ddd; border-radius: 8px; font-size: 14px; outline: none; flex: none; position: sticky; top: 49px; background: #fff; z-index: 5; }
.search:focus { border-color: #0a7cff; }
.clients { overflow-y: auto; -webkit-overflow-scrolling: touch; flex: 1; min-height: 0; }
.client { padding: 11px 16px; border-bottom: 1px solid #f2f2f2; cursor: pointer; }
.client:hover { background: #f7f9fc; }
.client.active {
  background: #d9e9ff;
  border-left: 5px solid #1f6feb;
  box-shadow: inset 0 0 0 2px #1f6feb;
}
.c-top { display: flex; justify-content: space-between; }
.c-name { font-weight: 600; font-size: 14px; }
.c-time { font-size: 11px; color: #999; }
.c-last { font-size: 13px; color: #666; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin: 3px 0; }
.c-meta { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.amt { font-size: 12px; color: #0a7cff; font-weight: 600; }
.mc { font-size: 11px; color: #aaa; }

/* Бейджи статусов */
.badge { font-size: 11px; padding: 1px 7px; border-radius: 10px; background: #eee; color: #555; }
.s-оплатил { background: #d7f5dd; color: #1a7f37; }
.s-отклонён { background: #ffe0e0; color: #c62828; }
.s-остановился { background: #fff0d4; color: #b26a00; }

/* Диалог */
.dialog { display: flex; flex-direction: column; overflow: hidden; min-height: 0; }
.chat-head { padding: 14px 20px; background: #fff; border-bottom: 1px solid #e0e0e0; position: sticky; top: 0; z-index: 5; flex: none; }
.ch-name { font-weight: 700; font-size: 16px; }
.ch-info { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; color: #555; margin-top: 4px; }
.messages { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 20px; display: flex; flex-direction: column; gap: 10px; min-height: 0; }
.msg { max-width: 70%; display: flex; flex-direction: column; }
.msg.user { align-self: flex-start; }
.msg.assistant { align-self: flex-end; align-items: flex-end; }
.bubble { padding: 9px 13px; border-radius: 14px; font-size: 14px; line-height: 1.4; white-space: pre-wrap; word-wrap: break-word; }
.msg.user .bubble { background: #fff; border: 1px solid #e2e2e2; border-bottom-left-radius: 4px; }
.msg.assistant .bubble { background: #0a7cff; color: #fff; border-bottom-right-radius: 4px; }
.m-time { font-size: 10px; color: #aaa; margin: 2px 6px; }
.m-check { color: #4a9c4a; font-weight: bold; }
.msg.has-menu { cursor: context-menu; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.msg.deleted .bubble { opacity: 0.5; font-style: italic; }
.del-chip { font-size: 11px; padding: 1px 7px; border-radius: 9px; background: #9a9a9a; color: #fff; }
.ai-chip { font-size: 9px; padding: 0 5px; border-radius: 8px; color: #fff; vertical-align: middle; cursor: pointer; user-select: none; }
.ai-chip.on { background: #2e9b48; }
.ai-chip.off { background: #d23c3c; }
.msg.btns { max-width: 78%; align-items: flex-end; gap: 4px; margin-top: 1px; }
.kbd-btn { display: inline-block; text-align: center; background: #0a7cff; color: #fff;
  border: none; border-radius: 14px; padding: 8px 14px; font-size: 13px; }
body.dark .kbd-btn { background: #2b5278; color: #e4e6eb; }

.empty { color: #999; padding: 30px; text-align: center; }
.empty.big { margin: auto; font-size: 15px; }

/* Карточка клиента + источник (этап 2+3, 12.07.2026) */
.src-badge, .src-chip {
  background: #2aabee; color: #fff; border-radius: 8px;
  font-size: 11px; padding: 1px 7px; vertical-align: middle;
}
.src-chip { font-size: 9px; padding: 0 5px; }
.ch-card { margin-top: 6px; font-size: 13px; }
.ch-card summary { cursor: pointer; color: #3390ec; user-select: none; }
.ch-card table { margin-top: 6px; border-collapse: collapse; width: 100%; }
.ch-card td { padding: 3px 8px 3px 0; vertical-align: top; }
.ch-card td:first-child { color: #888; white-space: nowrap; width: 130px; }

/* Шестерёнка настроек + тёмная тема (12.07.2026) */
.gear-wrap { position: relative; }
.gear-btn { background: none; border: none; font-size: 17px; cursor: pointer; padding: 0 2px; }
.gear-menu {
  display: none; position: absolute; left: 0; top: 26px; z-index: 20;
  background: #fff; border: 1px solid #ddd; border-radius: 10px;
  box-shadow: 0 4px 16px rgba(0,0,0,.12); padding: 10px 14px; min-width: 190px;
}
.gear-menu.open { display: block; }
.gear-item { display: flex; justify-content: space-between; align-items: center; gap: 12px; font-size: 13px; }
.switch { position: relative; width: 38px; height: 21px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider-t {
  position: absolute; inset: 0; background: #ccc; border-radius: 21px;
  cursor: pointer; transition: .2s;
}
.slider-t:before {
  content: ""; position: absolute; height: 17px; width: 17px; left: 2px; top: 2px;
  background: #fff; border-radius: 50%; transition: .2s;
}
.switch input:checked + .slider-t { background: #0a7cff; }
.switch input:checked + .slider-t:before { transform: translateX(17px); }

/* Тёмная тема */
body.dark { color: #e4e6eb; background: #18191a; }
body.dark .list { background: #242526; border-color: #3a3b3c; }
body.dark .list-head { border-color: #3a3b3c; background: #242526; }
body.dark .search { background: #3a3b3c; }
body.dark .slider-t { background: #3a3b3c; }
body.dark .slider-t:before { background: #888; }
body.dark .search { background: #3a3b3c; border-color: #3a3b3c; color: #e4e6eb; }
body.dark .client { border-color: #303132; }
body.dark .client:hover { background: #303132; }
/* Выбранный клиент: рамка и зелёный оттенок, чтобы не путать
   с непрочитанным, у того синий фон без рамки */
body.dark .client.active {
  background: #1f4034;
  border-left: 5px solid #34d17f;
  box-shadow: inset 0 0 0 2px #34d17f;
}
body.dark .client.active .c-name { color: #fff; font-weight: 700; }
/* Выбранный и непрочитанный одновременно: рамка выбора важнее */
body.dark .client.active.unread { background: #1f4034; border-left-color: #34d17f; }
body.dark .c-last { color: #b0b3b8; }
body.dark .chat-head { background: #242526; border-color: #3a3b3c; }
body.dark .ch-info { color: #b0b3b8; }
body.dark .msg.user .bubble { background: #3a3b3c; border-color: #3a3b3c; color: #e4e6eb; }
body.dark .badge { background: #3a3b3c; color: #ccc; }
body.dark .s-оплатил { background: #1f3d28; color: #7ee2a8; }
body.dark .s-отклонён { background: #46201f; color: #ff9d9d; }
body.dark .s-остановился { background: #453516; color: #ffce7a; }
body.dark .gear-menu { background: #242526; border-color: #3a3b3c; }
body.dark .ch-card td:first-child { color: #9aa0a6; }
body.dark .msg.assistant .bubble { background: #2b5278; color: #e4e6eb; }

/* Категории (папки) + ручная правка карточки (14.07.2026) */
.cat-tabs { display: flex; gap: 6px; padding: 0 12px 8px; flex-wrap: wrap; flex: none; }
.cat-tab { font-size: 12px; padding: 3px 10px; border-radius: 12px; background: #eef0f2; color: #444; text-decoration: none; }
.cat-tab.on { background: #0a7cff; color: #fff; }
.cat-chip { font-size: 10px; padding: 1px 7px; border-radius: 9px; background: #e7d9ff; color: #6b3fa0; }
.ch-edit { margin-top: 8px; font-size: 13px; }
.ch-edit summary { cursor: pointer; color: #3390ec; user-select: none; }
.ch-edit form { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; max-width: 420px; }
.ch-edit label { display: flex; flex-direction: column; font-size: 12px; color: #888; gap: 2px; }
.ch-edit input, .ch-edit select { padding: 7px 9px; border: 1px solid #ddd; border-radius: 7px; font-size: 14px; color: #1c1e21; }
.ch-edit button { align-self: flex-start; padding: 7px 16px; border: none; border-radius: 7px; background: #0a7cff; color: #fff; font-size: 14px; cursor: pointer; }
.saved-ok { color: #1a7f37; font-size: 13px; }
body.dark .cat-tab { background: #3a3b3c; color: #ccc; }
body.dark .cat-tab.on { background: #0a7cff; color: #fff; }
body.dark .cat-chip { background: #3d2f52; color: #d6bcff; }
body.dark .ch-edit input, body.dark .ch-edit select { background: #3a3b3c; border-color: #3a3b3c; color: #e4e6eb; }

/* Гибкие категории: кнопки управления + контекстное меню (14.07.2026) */
.cat-add { border: none; background: #eef0f2; color: #666; border-radius: 12px; font-size: 13px; padding: 2px 9px; cursor: pointer; }
body.dark .cat-add { background: #3a3b3c; color: #ccc; }
.client-add { align-self: flex-start; margin: 0 12px 8px; border: none; background: #0a7cff; color: #fff; border-radius: 8px; font-size: 13px; padding: 6px 12px; cursor: pointer; }
.client-add:hover { background: #0966d6; }
.ctx-menu { display: none; position: fixed; z-index: 100; background: #fff; border: 1px solid #ddd; border-radius: 10px; box-shadow: 0 6px 20px rgba(0,0,0,.18); padding: 6px; min-width: 160px; }
.ctx-menu.open { display: block; }
.ctx-title { font-size: 11px; color: #999; padding: 4px 10px; }
.ctx-item { padding: 8px 12px; font-size: 14px; border-radius: 6px; cursor: pointer; }
.ctx-item:hover { background: #f0f4fa; }
.ctx-sep { height: 1px; background: #e0e0e0; margin: 4px 6px; }
body.dark .ctx-sep { background: #3a3b3c; }
.ctx-del { color: #e0245e; font-weight: 600; }
body.dark .ctx-menu { background: #242526; border-color: #3a3b3c; }
body.dark .ctx-item:hover { background: #303132; }

/* Единая редактируемая карточка клиента */
.card-form { margin-top: 8px; }
.cf-row { display: flex; align-items: center; gap: 8px; padding: 4px 0; border-bottom: 1px solid #f0f0f0; }
.cf-k { flex: none; width: 120px; font-size: 12px; color: #888; }
.cf-v { flex: 1; font-size: 13px; color: #1c1e21; }
input.cf-v, select.cf-v { padding: 6px 8px; border: 1px solid #ddd; border-radius: 6px; }
.card-form button { margin-top: 10px; padding: 8px 18px; border: none; border-radius: 7px; background: #0a7cff; color: #fff; font-size: 14px; cursor: pointer; }
body.dark .cf-row { border-color: #303132; }
body.dark .cf-k { color: #9aa0a6; }
body.dark .cf-v { color: #e4e6eb; }
body.dark input.cf-v, body.dark select.cf-v { background: #3a3b3c; border-color: #3a3b3c; color: #e4e6eb; }

/* Цветная точка в контекстном меню категорий (14.07.2026) */
.ctx-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 8px; vertical-align: middle; }
.cat-tab { border-left: 4px solid transparent; }

/* Полосы прокрутки (скроллбары) списка и переписки - под тему (14.07.2026) */
.clients, .messages { scrollbar-width: thin; scrollbar-color: #c4c4c4 transparent; }
.clients::-webkit-scrollbar, .messages::-webkit-scrollbar { width: 9px; }
.clients::-webkit-scrollbar-track, .messages::-webkit-scrollbar-track { background: transparent; }
.clients::-webkit-scrollbar-thumb, .messages::-webkit-scrollbar-thumb { background: #c4c4c4; border-radius: 5px; }
.clients::-webkit-scrollbar-thumb:hover, .messages::-webkit-scrollbar-thumb:hover { background: #a8a8a8; }
body.dark .clients, body.dark .messages { scrollbar-color: #4a4b4c transparent; }
body.dark .clients::-webkit-scrollbar-thumb, body.dark .messages::-webkit-scrollbar-thumb { background: #4a4b4c; }
body.dark .clients::-webkit-scrollbar-thumb:hover, body.dark .messages::-webkit-scrollbar-thumb:hover { background: #5a5b5c; }

/* Поле ответа клиенту снизу переписки (14.07.2026) */
.chat-input { flex: none; display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid #e0e0e0; background: #fff; }
.chat-input input, .chat-input textarea { flex: 1; padding: 10px 14px; border: 1px solid #ddd; border-radius: 6px; font-size: 14px; outline: none; font-family: inherit; resize: vertical; }
.chat-input input:focus, .chat-input textarea:focus { border-color: #0a7cff; }
.chat-input textarea::-webkit-scrollbar { width: 10px; }
.chat-input textarea::-webkit-scrollbar-track { background: #2b2b2b; border-radius: 6px; }
.chat-input textarea::-webkit-scrollbar-thumb { background: #555; border-radius: 6px; }
.chat-input button { flex: none; width: 42px; height: 42px; border: none; border-radius: 8px; background: #0a7cff; color: #fff; font-size: 16px; cursor: pointer; }
body.dark .chat-input { background: #242526; border-color: #3a3b3c; }
body.dark .chat-input input, body.dark .chat-input textarea { background: #3a3b3c; border-color: #3a3b3c; color: #e4e6eb; }

/* Экран входа + модальные окна в тёмном стиле (14.07.2026) */
.login-body { height: 100vh; height: 100dvh; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.login-box { display: flex; flex-direction: column; gap: 12px; width: 300px; padding: 28px; background: #242526; border: 1px solid #3a3b3c; border-radius: 14px; box-shadow: 0 8px 30px rgba(0,0,0,.4); }
body:not(.dark) .login-box { background: #fff; border-color: #e0e0e0; }
.login-logo { font-size: 24px; font-weight: 700; color: #0a7cff; text-align: center; }
.login-sub { font-size: 13px; color: #888; text-align: center; margin-bottom: 6px; }
.login-box input { padding: 11px 14px; border: 1px solid #3a3b3c; border-radius: 9px; font-size: 15px; background: #3a3b3c; color: #e4e6eb; outline: none; }
body:not(.dark) .login-box input { background: #f0f2f5; border-color: #ddd; color: #1c1e21; }
.login-box input:focus { border-color: #0a7cff; }
.login-box button { padding: 11px; border: none; border-radius: 9px; background: #0a7cff; color: #fff; font-size: 15px; font-weight: 600; cursor: pointer; }
.login-err { color: #ff9d9d; font-size: 13px; text-align: center; }

.modal-bg { display: none; position: fixed; inset: 0; z-index: 200; background: rgba(0,0,0,.55); align-items: center; justify-content: center; }
.modal-bg.open { display: flex; }
.modal { width: 320px; padding: 22px; background: #242526; border: 1px solid #3a3b3c; border-radius: 14px; box-shadow: 0 8px 30px rgba(0,0,0,.5); }
body:not(.dark) .modal { background: #fff; border-color: #e0e0e0; }
.modal-title { font-size: 15px; color: #e4e6eb; margin-bottom: 14px; }
body:not(.dark) .modal-title { color: #1c1e21; }
.modal-input { width: 100%; padding: 10px 13px; border: 1px solid #3a3b3c; border-radius: 9px; font-size: 15px; background: #3a3b3c; color: #e4e6eb; outline: none; margin-bottom: 14px; }
body:not(.dark) .modal-input { background: #f0f2f5; border-color: #ddd; color: #1c1e21; }
.modal-input:focus { border-color: #0a7cff; }
.modal-btns { display: flex; justify-content: flex-end; gap: 8px; }
.modal-cancel, .modal-ok { padding: 8px 18px; border: none; border-radius: 8px; font-size: 14px; cursor: pointer; }
.modal-cancel { background: #3a3b3c; color: #ccc; }
body:not(.dark) .modal-cancel { background: #eef0f2; color: #555; }
.modal-ok { background: #0a7cff; color: #fff; }

/* Индикатор паузы ИИ (беру на себя) (14.07.2026) */
.takeover-bar { flex: none; font-size: 13px; padding: 8px 16px; border-top: 1px solid transparent; }
.takeover-bar .tb-paused { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.takeover-bar button { border: none; border-radius: 7px; background: #b26a00; color: #fff; font-size: 13px; padding: 6px 12px; cursor: pointer; flex: none; }
.takeover-bar.paused { background: #fff3d4; color: #7a5a00; border-color: #ffe4a0; }
.takeover-bar.paused .tb-live { display: none; }
.takeover-bar.live { background: #dcf5e3; color: #1c6b34; border-color: #b8e6c6; }
.takeover-bar.live .tb-paused { display: none; }
body.dark .takeover-bar.paused { background: #3a2f14; color: #ffce7a; border-color: #4a3b18; }
body.dark .takeover-bar.live { background: #16351f; color: #7ad898; border-color: #1d4a2b; }

/* Гамбургер (три полоски) слева от имени клиента - только на телефоне */
.ch-top { display: flex; align-items: center; gap: 10px; }
.chat-menu { display: none; align-items: center; justify-content: center; line-height: 1; border: none; background: none; color: inherit; font-size: 22px; padding: 0; cursor: pointer; flex: none; }
.drawer-close { display: none; align-items: center; justify-content: center; line-height: 1; border: none; background: none; color: inherit; font-size: 20px; cursor: pointer; margin-left: 4px; }

/* Мобильная раскладка: переписка всегда основа, список - панель на весь экран */
@media (max-width: 768px) {
  .app { grid-template-columns: 1fr; }
  .chat-menu, .drawer-close { display: inline-flex; }
  .list {
    position: fixed; top: 0; left: 0; height: 100%; width: 100%;
    z-index: 50; transform: translateX(-100%); transition: transform 0.22s ease;
  }
  body.drawer-open .list { transform: translateX(0); }
  .msg { max-width: 85%; }
}

/* Вкладки каналов: вид как в браузере, активная сливается с полем переписки */
.ch-tabs {
  display: flex; gap: 2px; padding: 8px 10px 0;
  border-bottom: 2px solid #4f9cf9; flex-wrap: wrap;
}
.ch-tab {
  position: relative; bottom: -2px;
  background: #262b35; color: #9aa5b8;
  border: 1px solid #353c49; border-bottom: none;
  border-radius: 10px 10px 0 0; padding: 8px 16px;
  font-size: 13px; cursor: pointer; user-select: none;
  transition: background .15s;
}
.ch-tab:hover { background: #2f3542; color: #cfd6e4; }
.ch-tab.active {
  background: #1e2229; color: #fff; font-weight: 600;
  border-color: #4f9cf9; border-bottom: 2px solid #1e2229;
}
.ch-tab.dragging { opacity: .45; }
.ch-tab .ch-pin { display: none; margin-right: 5px; font-size: 11px; }
.ch-tab.pinned .ch-pin { display: inline; }
.ch-menu {
  display: none; position: fixed; z-index: 100;
  background: #2a2f3a; border: 1px solid #3d4553; border-radius: 8px;
  padding: 4px; box-shadow: 0 6px 18px rgba(0,0,0,.45);
}
.ch-menu button {
  background: none; border: none; color: #cfd6e4;
  padding: 7px 14px; font-size: 13px; cursor: pointer;
  width: 100%; text-align: left; border-radius: 5px;
}
.ch-menu button:hover { background: #3a4150; }

/* Непрочитанное: карточка подсвечена, число справа как в Telegram */
.client.unread {
  background: #2c4054;
  border-left: 4px solid #59a9ff;
  box-shadow: inset 0 0 0 1px rgba(89,169,255,.25);
}
.client.unread .c-name { color: #fff; font-weight: 600; }
.unread-num {
  display: inline-block; background: #4f9cf9; color: #fff;
  border-radius: 10px; padding: 0 7px; margin-left: 5px;
  font-size: 11px; font-weight: 600;
}
.ch-from { color: #7f8ea3; font-size: 11px; }
.ch-tab .ch-num {
  background: #4f9cf9; color: #fff; border-radius: 10px;
  padding: 0 6px; margin-left: 5px; font-size: 11px; font-weight: 600;
}

/* Строка фильтра: кнопка сброса, выпадающий список, создание клиента.
   Категория одна на клиента, показ ручных и удалённых - независимые галочки. */
.filter-bar {
  display: flex; gap: 6px; align-items: stretch;
  padding: 6px 8px; position: relative;
}
.flt-all, .flt-btn, .filter-bar .client-add {
  display: flex; align-items: center; justify-content: center;
  height: 38px; box-sizing: border-box;
  border-radius: 8px; font-size: 13px; cursor: pointer;
  text-decoration: none; border: 1px solid #3a4150;
  background: #2a2f3a; color: #cfd6e4; padding: 0 14px;
}
.flt-all.on { background: #1f6feb; color: #fff; border-color: #1f6feb; }
.flt-btn { flex: 1; justify-content: flex-start; gap: 7px; }
.flt-caret { color: #9aa5b8; font-size: 15px; line-height: 1; }
.filter-bar .client-add {
  background: #2a2f3a; border-color: #3a4150; color: #8fb6ef;
  font-size: 15px; padding: 0 11px; font-weight: 400;
}
.filter-bar .client-add:hover { background: #333a47; color: #b6d0ff; }
/* Меню всплывает поверх списка и не двигает клиентов */
.flt-menu {
  display: none; position: absolute; z-index: 60;
  top: calc(100% - 2px); left: 8px; right: 8px;
  padding: 6px; background: #232833;
  border: 1px solid #3a4150; border-radius: 10px;
  box-shadow: 0 10px 28px rgba(0,0,0,.55);
}
.flt-menu.open { display: block; }
.flt-head {
  font-size: 10px; letter-spacing: .8px; text-transform: uppercase;
  color: #7f8ea3; padding: 8px 10px 4px;
}
.flt-item {
  display: flex; align-items: center; gap: 8px; width: 100%;
  background: none; border: none; color: #cfd6e4; text-decoration: none;
  padding: 8px 10px; border-radius: 7px; font-size: 13px;
  cursor: pointer; text-align: left;
}
.flt-item:hover { background: #313846; }
.flt-item.on { background: #1f6feb; color: #fff; }
.flt-item.add { color: #7f8ea3; }
.flt-dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: #7f8ea3; flex: none;
}
.flt-box {
  width: 13px; height: 13px; border-radius: 3px;
  border: 1.5px solid #6b7688; flex: none;
}
.flt-item.chk.on .flt-box { background: #fff; border-color: #fff; }
