:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --border: #e2e5ea;
  --text: #1a1d23;
  --muted: #6b7280;
  --accent: #4f46e5;
  --danger: #dc2626;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

header {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 14px 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 10;
}
header h1 { font-size: 18px; margin: 0; font-weight: 700; white-space: nowrap; }
header .spacer { flex: 1; }
.summary { display: flex; gap: 18px; font-size: 13px; color: var(--muted); white-space: nowrap; }
.summary b { color: var(--text); }

/* Вкладки */
.tabs { display: flex; gap: 4px; background: #eceef1; border-radius: 8px; padding: 3px; }
.tab {
  border: none; background: transparent; color: var(--muted);
  padding: 6px 14px; border-radius: 6px; cursor: pointer; font-size: 14px; font-weight: 600;
}
.tab:hover { color: var(--text); }
.tab.active { background: #fff; color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,0.08); }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border); background: var(--panel);
  padding: 8px 14px; border-radius: 8px; cursor: pointer;
  font-size: 14px; color: var(--text); font-weight: 600; white-space: nowrap;
}
.btn:hover { background: #f0f1f4; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #4338ca; }
.btn.danger { color: var(--danger); border-color: #fecaca; }
.btn.danger:hover { background: #fef2f2; }
.btn.sm { padding: 5px 10px; font-size: 13px; }

.board {
  display: flex; gap: 12px; padding: 16px 20px;
  overflow-x: auto; align-items: flex-start; min-height: 70vh;
}
.board.terminal { padding-top: 0; border-top: 1px dashed var(--border); margin-top: 4px; padding-top: 16px; }

.column {
  background: #eceef1; border-radius: 10px;
  min-width: 250px; width: 250px; flex: 0 0 auto;
  padding: 10px; display: flex; flex-direction: column;
  max-height: calc(100vh - 150px);
}
.column .col-head {
  display: flex; align-items: center; gap: 8px;
  padding: 2px 6px 10px; font-weight: 700; font-size: 14px;
}
.column .dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto; }
.column .col-count { color: var(--muted); font-weight: 500; margin-left: auto; font-size: 12px; }
.column .col-total { color: var(--muted); font-size: 11px; }

.cards { display: flex; flex-direction: column; gap: 8px; overflow-y: auto; flex: 1; }
.column.drag-over { outline: 2px dashed var(--accent); outline-offset: -2px; }

.card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 12px; cursor: pointer;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}
.card:hover { border-color: #c7cbd2; box-shadow: 0 2px 6px rgba(0,0,0,0.08); }
.card.dragging { opacity: 0.5; }
.card .t { font-weight: 600; font-size: 14px; line-height: 1.3; }
.card .c { color: var(--muted); font-size: 12px; margin-top: 2px; }
.card .meta { display: flex; align-items: center; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.badge {
  font-size: 11px; padding: 2px 7px; border-radius: 20px;
  background: #eef0f3; color: var(--muted); white-space: nowrap;
}
.badge.amount { background: #eef2ff; color: #4338ca; font-weight: 600; }
.badge.source { background: #ecfdf5; color: #047857; }
.badge.type { background: #fdf4ff; color: #a21caf; }
.badge.ok { background: #dcfce7; color: #15803d; }
.card .time { color: #9aa1ab; font-size: 11px; margin-top: 6px; }
.card .time.overdue { color: var(--danger); font-weight: 600; }

.empty { color: var(--muted); font-size: 12px; text-align: center; padding: 16px 0; }
.muted { color: var(--muted); }

/* Поиск и список клиентов */
.toolbar { padding: 16px 20px 0; max-width: 900px; }
.toolbar input {
  width: 100%; border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; font-size: 14px; background: #fff; color: var(--text);
}
.client-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; padding: 16px 20px; max-width: 1100px; }
.client-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px; cursor: pointer;
}
.client-card:hover { border-color: #c7cbd2; box-shadow: 0 2px 6px rgba(0,0,0,0.08); }
.client-card .t { font-weight: 600; font-size: 15px; }
.client-card .c { color: var(--muted); font-size: 13px; margin-top: 2px; }
.client-card .meta { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }

/* Модальное окно */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15,18,25,0.5);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; z-index: 100; overflow-y: auto;
}
.modal {
  background: var(--panel); border-radius: 12px; width: 560px;
  max-width: 100%; box-shadow: 0 12px 40px rgba(0,0,0,0.2);
  padding: 20px 22px;
}
.modal h2 { margin: 0 0 14px; font-size: 17px; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; font-weight: 600; }
.field input, .field select, .field textarea {
  width: 100%; border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; font-size: 14px; font-family: inherit; background: #fff; color: var(--text);
}
.field textarea { min-height: 70px; resize: vertical; }
.row2 { display: flex; gap: 12px; }
.row2 .field { flex: 1; }
.modal .actions { display: flex; gap: 10px; margin-top: 16px; align-items: center; }
.modal .actions .spacer { flex: 1; }

.history { margin-top: 20px; border-top: 1px solid var(--border); padding-top: 14px; }
.history h3, .tasks h3, .rel-section h3 { font-size: 13px; margin: 0 0 10px; color: var(--muted); }
.hist-item { padding: 8px 0; border-bottom: 1px solid #f0f1f4; font-size: 13px; }
.hist-item .when { color: #9aa1ab; font-size: 11px; }
.hist-item .c { color: var(--muted); font-size: 12px; margin-top: 3px; }
.hist-item .att { color: var(--accent); text-decoration: none; margin-right: 8px; font-size: 12px; }
.hist-item .att:hover { text-decoration: underline; }
.hist-item.kind-stage_change .body { color: #4338ca; }
.add-note { display: flex; gap: 8px; margin-top: 12px; }
.add-note input { flex: 1; border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 14px; }

.stage-pills { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.stage-pills button {
  border: 1px solid var(--border); background: #fff; color: var(--muted);
  border-radius: 20px; padding: 5px 12px; font-size: 12px; cursor: pointer; font-weight: 600;
}
.stage-pills button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.stage-pills button:hover { border-color: #b8bcc4; }

/* Связанные объекты (клиент → сделки/проекты) */
.rel-section { margin-top: 16px; border-top: 1px solid var(--border); padding-top: 12px; }
.rel-item { padding: 7px 0; border-bottom: 1px solid #f0f1f4; font-size: 13px; cursor: pointer; }
.rel-item:hover { color: var(--accent); }

/* Задачи проекта */
.tasks { margin-top: 20px; border-top: 1px solid var(--border); padding-top: 14px; }
.task-item {
  display: flex; align-items: center; gap: 10px; padding: 7px 0;
  border-bottom: 1px solid #f0f1f4; font-size: 14px;
}
.task-item input[type="checkbox"] { width: 16px; height: 16px; cursor: pointer; flex: 0 0 auto; }
.task-item span { flex: 1; }
.task-item span.done { text-decoration: line-through; color: var(--muted); }
.task-del { border: none; background: transparent; color: #9aa1ab; cursor: pointer; font-size: 14px; }
.task-del:hover { color: var(--danger); }

/* Письма в карточке клиента */
.email-item { padding: 8px 0; border-bottom: 1px solid #f0f1f4; font-size: 13px; }
.email-item .t { font-weight: 600; }
.email-item .c { color: var(--muted); font-size: 12px; margin-top: 2px; }
.email-item .when { color: #9aa1ab; font-size: 11px; margin-top: 4px; }

/* Сообщения (двухпанельный вид) */
.messages { display: flex; height: calc(100vh - 66px); }
.msg-left { width: 320px; flex: 0 0 auto; border-right: 1px solid var(--border); display: flex; flex-direction: column; background: #fff; }
.msg-left .toolbar { padding: 10px 12px; }
.msg-left .toolbar input { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; }
#conv-list { flex: 1; overflow-y: auto; }
.conv-item { padding: 12px 14px; border-bottom: 1px solid #f0f1f4; cursor: pointer; }
.conv-item:hover { background: #f7f8fa; }
.conv-item.active { background: #eef2ff; border-left: 3px solid var(--accent); }
.conv-item .t { font-weight: 600; font-size: 14px; }
.conv-item .preview { color: var(--muted); font-size: 12px; margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-item .snip { color: #9aa1ab; font-size: 12px; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-item .when { color: #9aa1ab; font-size: 11px; margin-top: 4px; }
.msg-right { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.thread-head { padding: 14px 18px; border-bottom: 1px solid var(--border); background: #fff; }
.thread-head .t { font-weight: 700; font-size: 16px; }
.thread-head .c { color: var(--muted); font-size: 13px; }
.thread { flex: 1; overflow-y: auto; padding: 16px 18px; }
.thread-msg { margin-bottom: 14px; }
.thread-msg.in .tm-head { color: var(--accent); }
.thread-msg.out .tm-head { color: #047857; }
.tm-head { font-size: 12px; font-weight: 600; margin-bottom: 3px; }
.tm-subj { font-weight: 600; font-size: 14px; margin-bottom: 4px; }
.tm-body { font-size: 13px; line-height: 1.45; white-space: pre-wrap; background: #fff; border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; }
.tm-atts { margin-top: 6px; }
.tm-atts .att { color: var(--accent); text-decoration: none; margin-right: 8px; font-size: 12px; }
.tm-atts .att:hover { text-decoration: underline; }
.reply { border-top: 1px solid var(--border); padding: 12px 18px; background: #fff; }
.reply input[type="text"], .reply input:not([type="file"]), .reply textarea { width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 14px; margin-bottom: 8px; }
.reply textarea { min-height: 80px; resize: vertical; }
.reply .actions { display: flex; justify-content: flex-end; gap: 8px; }

/* Фильтр каналов в «Сообщениях» + привязка Telegram-чата */
.channels { display: flex; gap: 4px; padding: 0 12px 8px; }
.channels .ch {
  flex: 1; border: 1px solid var(--border); background: #fff; color: var(--muted);
  padding: 6px 4px; border-radius: 8px; cursor: pointer; font-size: 12px; font-weight: 600;
}
.channels .ch.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.tg-link { display: flex; gap: 8px; margin-top: 8px; align-items: center; }
.tg-link select { flex: 1; border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px; font-size: 13px; background: #fff; }

/* Аналитика */
.analytics { padding: 16px 20px; max-width: 1000px; }
.metric-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.metric { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; }
.metric .mv { font-size: 22px; font-weight: 700; }
.metric .ml { color: var(--muted); font-size: 12px; margin-top: 2px; }
.apanel { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 16px 18px; margin-bottom: 16px; }
.apanel h3 { font-size: 14px; margin: 0 0 12px; }
.arow { display: flex; align-items: center; gap: 12px; padding: 5px 0; }
.arow .alabel { width: 170px; flex: 0 0 auto; font-size: 13px; display: flex; align-items: center; gap: 6px; }
.arow .alabel .dot { width: 8px; height: 8px; border-radius: 50%; flex: 0 0 auto; }
.arow .abar { flex: 1; }
.bar-track { background: #eef0f3; border-radius: 6px; height: 14px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 6px; }
.arow .aval { width: 150px; flex: 0 0 auto; text-align: right; font-size: 12px; color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { background: #eef0f3; border-radius: 20px; padding: 5px 12px; font-size: 13px; }

/* Календарь (брони) */
.cal-toolbar { display: flex; align-items: center; gap: 10px; padding: 16px 20px 8px; }
.cal-title { font-size: 17px; font-weight: 700; flex: 1; text-align: center; }
.cal-weekdays { display: grid; grid-template-columns: repeat(7, 1fr); padding: 0 20px; color: var(--muted); font-size: 12px; font-weight: 600; }
.cal-weekdays div { text-align: center; padding: 6px 0; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; padding: 8px 20px 20px; }
.cal-cell { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; min-height: 96px; padding: 6px; cursor: pointer; overflow: hidden; }
.cal-cell:hover { border-color: #c7cbd2; }
.cal-cell.other { background: transparent; border: none; cursor: default; }
.cal-cell.today { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.cal-day { font-size: 12px; font-weight: 700; color: var(--muted); margin-bottom: 4px; }
.cal-cell.today .cal-day { color: var(--accent); }
.cal-chips { display: flex; flex-direction: column; gap: 3px; }
.cal-chip { font-size: 11px; color: #fff; border-radius: 4px; padding: 2px 5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cal-more { font-size: 11px; color: var(--muted); margin-top: 2px; }
