* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #f4f6fa; --panel: #fff; --line: #e6e9f0; --text: #131a2b; --muted: #7c869c;
  --accent: #3b5bfd; --accent-soft: #eef1ff; --green: #16a34a; --red: #dc2626;
  --radius: 12px; --shadow: 0 1px 3px rgba(19,26,43,.06), 0 8px 24px rgba(19,26,43,.05);
}
body { font: 14px/1.5 -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg); color: var(--text); -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
a { color: var(--accent); text-decoration: none; }
.hidden { display: none !important; }

/* ---------- вход ---------- */
#login { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card { width: 100%; max-width: 380px; background: var(--panel); border-radius: 16px;
  padding: 32px; box-shadow: var(--shadow); }
.login-card h1 { font-size: 20px; margin-bottom: 4px; }
.login-card p.sub { color: var(--muted); margin-bottom: 24px; font-size: 13px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.field input, .field select, .field textarea { width: 100%; padding: 10px 12px; border: 1px solid var(--line);
  border-radius: 9px; background: #fff; outline: none; }
.field input:focus, .field textarea:focus, .field select:focus { border-color: var(--accent); }
.btn { background: var(--accent); color: #fff; padding: 10px 18px; border-radius: 9px; font-weight: 500; }
.btn:hover { filter: brightness(1.06); }
.btn.ghost { background: #fff; color: var(--text); border: 1px solid var(--line); }
.btn.ghost:hover { background: #f7f8fc; filter: none; }
.btn.sm { padding: 6px 12px; font-size: 13px; }
.btn.sm.wide { display: block; width: 100%; margin-top: 6px; padding: 5px 10px; font-size: 12px; }
.btn.danger { background: var(--red); }
.err { color: var(--red); font-size: 13px; margin-top: 10px; min-height: 18px; }

/* ---------- каркас ---------- */
#app { display: grid; grid-template-rows: 56px 1fr; height: 100vh; }
header.top { display: flex; align-items: center; gap: 14px; padding: 0 18px;
  background: var(--panel); border-bottom: 1px solid var(--line); }
.logo { font-weight: 700; font-size: 16px; letter-spacing: -.2px; }
.logo span { color: var(--accent); }
.top .spacer { flex: 1; }
.top .who { color: var(--muted); font-size: 13px; }
.sync-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--green); display: inline-block; }
.sync-dot.busy { background: #f59e0b; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

.body { display: grid; grid-template-columns: 248px 1fr; overflow: hidden; }
aside { background: var(--panel); border-right: 1px solid var(--line); overflow-y: auto; padding: 12px 0; }
aside h3 { font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted);
  padding: 10px 18px 6px; }
.mb-item { display: flex; align-items: center; gap: 8px; padding: 8px 18px; cursor: pointer;
  font-size: 13px; border-left: 3px solid transparent; }
.mb-item:hover { background: #f7f8fc; }
.mb-item.active { background: var(--accent-soft); border-left-color: var(--accent); font-weight: 500; }
.mb-item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mb-item .badge { background: var(--accent); color: #fff; border-radius: 20px; padding: 1px 7px;
  font-size: 11px; font-weight: 600; }
.mb-item .cnt { color: var(--muted); font-size: 12px; }
.mb-item.off .name { color: var(--muted); text-decoration: line-through; }
.mb-item .warn { color: #f59e0b; }

main { overflow: hidden; display: grid; grid-template-rows: auto auto 1fr; }
.tabs { display: flex; gap: 4px; padding: 12px 18px 0; overflow-x: auto; }
.tab { padding: 7px 13px; border-radius: 8px; font-size: 13px; color: var(--muted); white-space: nowrap; }
.tab:hover { background: #eef0f6; }
.tab.active { background: var(--text); color: #fff; }
.tab b { font-weight: 600; }
.toolbar { display: flex; gap: 10px; align-items: center; padding: 12px 18px; }
.toolbar input.search { flex: 1; max-width: 380px; padding: 8px 12px; border: 1px solid var(--line);
  border-radius: 9px; background: #fff; }

.list { overflow-y: auto; overflow-x: auto; padding: 0 18px 24px; }
table.leads { width: 100%; border-collapse: separate; border-spacing: 0 6px; table-layout: fixed; }
/* Колонка статуса не должна уезжать за край: тексту отдаём остаток,
   остальным столбцам — фиксированную ширину, лишнее прячем на узких экранах. */
td.c-lead   { width: 230px; }
td.c-text   { width: auto; }
td.c-status { width: 176px; }
td.c-mb     { width: 170px; }
td.c-date   { width: 92px; text-align: right; }
td.c-lead .lead-name, td.c-lead .lead-mail { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td.c-mb { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 1180px) { td.c-mb { display: none; } }
@media (max-width: 980px)  { td.c-date { display: none; } }
@media (max-width: 760px)  { td.c-lead { width: 170px; } td.c-status { width: 140px; } }
table.leads td { background: var(--panel); padding: 12px 14px; vertical-align: middle; }
table.leads tr { cursor: pointer; }
table.leads tr:hover td { background: #fbfcff; }
table.leads td:first-child { border-radius: var(--radius) 0 0 var(--radius); }
table.leads td:last-child { border-radius: 0 var(--radius) var(--radius) 0; }
/* слева у строки — полоса цвета статуса, а не «непрочитанного» */
table.leads td:first-child { box-shadow: inset 3px 0 0 var(--st, transparent); }
tr.unread .lead-name { font-weight: 700; }
.lead-name .dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent); margin-right: 7px; vertical-align: middle; }
.lead-name { font-weight: 600; }
.lead-mail { color: var(--muted); font-size: 12px; }
.snippet { color: var(--muted); font-size: 13px; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; }
.c-text .tiny { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 20px;
  font-size: 12px; font-weight: 500; background: #f0f2f7; }
.chip i { width: 7px; height: 7px; border-radius: 50%; background: currentColor; display: block; }
select.status { border: 1px solid var(--line); border-radius: 20px; padding: 5px 10px; font-size: 12px;
  background: #fff; cursor: pointer; appearance: none; -webkit-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 10 10'><path d='M1 3l4 4 4-4' fill='none' stroke='%23667' stroke-width='1.6'/></svg>");
  background-repeat: no-repeat; background-position: right 9px center; padding-right: 28px; }
select.status:hover { filter: brightness(.97); }
.tiny { font-size: 12px; color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 60px 20px; }

/* ---------- карточка лида ---------- */
#drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(720px, 96vw); background: var(--panel);
  box-shadow: -8px 0 32px rgba(19,26,43,.14); display: grid; grid-template-rows: auto auto 1fr auto;
  transform: translateX(102%); transition: transform .22s ease; z-index: 40; }
#drawer.open { transform: none; }
.dr-head { padding: 16px 20px; border-bottom: 1px solid var(--line); display: flex; gap: 12px; align-items: flex-start; }
.dr-head .x { font-size: 22px; color: var(--muted); line-height: 1; }
.dr-meta { display: flex; flex-wrap: wrap; gap: 8px; padding: 12px 20px; border-bottom: 1px solid var(--line);
  align-items: center; }
.dr-meta input { border: 1px solid var(--line); border-radius: 8px; padding: 5px 9px; font-size: 12px; width: 150px; }
.thread { overflow-y: auto; padding: 18px 20px; background: #f7f8fc; }
.msg { background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; margin-bottom: 12px; }
.msg.out { background: var(--accent-soft); border-color: #dfe4ff; margin-left: 40px; }
.msg .m-head { display: flex; gap: 8px; font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.msg .m-subj { font-weight: 600; font-size: 13px; margin-bottom: 6px; }
.msg .m-body { white-space: pre-wrap; word-break: break-word; font-size: 13.5px; }
.msg .m-body .quoted { color: var(--muted); border-left: 2px solid var(--line); padding-left: 10px; margin-top: 8px; }
.msg .more { color: var(--accent); font-size: 12px; margin-top: 6px; }
.m-acts { display: flex; gap: 8px; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line); }
.msg.out .m-acts { border-top-color: #dfe4ff; }
.reply { border-top: 1px solid var(--line); padding: 12px 20px 16px; }
.reply textarea { width: 100%; min-height: 92px; border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; resize: vertical; outline: none; }
.reply textarea:focus { border-color: var(--accent); }
.reply .row { display: flex; gap: 10px; align-items: center; margin-top: 10px; }
.reply select { border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; font-size: 12px; background: #fff; }

/* ---------- модалки ---------- */
.overlay { position: fixed; inset: 0; background: rgba(19,26,43,.42); z-index: 50; display: grid;
  place-items: center; padding: 24px; }
.modal { background: var(--panel); border-radius: 16px; width: min(860px, 96vw); max-height: 90vh;
  overflow: auto; box-shadow: var(--shadow); }
.modal h2 { font-size: 17px; padding: 20px 24px 0; }
.modal .mtabs { display: flex; gap: 4px; padding: 14px 24px 0; border-bottom: 1px solid var(--line); }
.modal .mtab { padding: 9px 14px; font-size: 13px; color: var(--muted); border-bottom: 2px solid transparent; }
.modal .mtab.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 500; }
.modal .mbody { padding: 20px 24px 24px; }
.modal .foot { display: flex; gap: 10px; justify-content: flex-end; padding: 0 24px 22px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
table.simple { width: 100%; border-collapse: collapse; font-size: 13px; }
table.simple th { text-align: left; color: var(--muted); font-weight: 500; font-size: 12px;
  padding: 8px 10px; border-bottom: 1px solid var(--line); }
table.simple td { padding: 9px 10px; border-bottom: 1px solid var(--line); }
.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: var(--text);
  color: #fff; padding: 11px 20px; border-radius: 10px; font-size: 13px; z-index: 90; box-shadow: var(--shadow); }
.toast.bad { background: var(--red); }
@media (max-width: 860px) { .body { grid-template-columns: 1fr; } aside { display: none; } }
