:root {
  --side: #0d2940;
  --side-hover: #16385a;
  --side-active: #1f4f7d;
  --bg: #eef1f4;
  --panel: #ffffff;
  --line: #e1e5ea;
  --text: #1c2b36;
  --muted: #6b7a88;
  --accent: #1a8cff;
  --accent-soft: #e6f2ff;
  --warn: #ff9f1a;
  --danger: #e5484d;
  --ok: #2fae62;
  --bot: #7b5cff;
  --bot-soft: #f1edff;
  --note: #fff7d6;
  --radius: 8px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); }

/* ---------- каркас ---------- */
.layout { display: flex; height: 100vh; }
.side { width: 88px; background: var(--side); color: #cfe0ef; display: flex; flex-direction: column; align-items: stretch; flex-shrink: 0; }
.side .logo { font-weight: 700; color: #fff; text-align: center; padding: 16px 4px 12px; font-size: 13px; letter-spacing: .5px; }
.side button { background: none; border: 0; color: inherit; padding: 12px 4px; cursor: pointer; font-size: 12px; position: relative; }
.side button:hover { background: var(--side-hover); }
.side button.active { background: var(--side-active); color: #fff; box-shadow: inset 3px 0 0 var(--accent); }
.side .ico { display: block; font-size: 20px; margin-bottom: 4px; }
.side .badge { position: absolute; top: 6px; right: 14px; }
.side .spacer { flex: 1; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.badge { display: inline-block; min-width: 18px; padding: 1px 6px; border-radius: 9px; background: var(--danger); color: #fff; font-size: 11px; font-weight: 600; text-align: center; }
.tag { display: inline-block; padding: 1px 7px; border-radius: 4px; font-size: 11px; font-weight: 600; background: #e9edf1; color: var(--muted); white-space: nowrap; }
.tag.tg { background: #e3f3fc; color: #1d8ecf; }
.tag.widget { background: #e8f7ee; color: #1f9a55; }
.tag.bot { background: var(--bot-soft); color: var(--bot); }
.tag.wait { background: #fff0db; color: #c46f00; }
.tag.err { background: #fde7e8; color: var(--danger); }

.btn { border: 1px solid var(--line); background: #fff; border-radius: 6px; padding: 7px 12px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: #c5ccd4; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #0f7ee8; }
.btn.bot { background: var(--bot); border-color: var(--bot); color: #fff; }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn.ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn:disabled { opacity: .5; cursor: default; }
input.field, textarea.field, select.field { border: 1px solid var(--line); border-radius: 6px; padding: 7px 10px; background: #fff; width: 100%; }
input.field:focus, textarea.field:focus, select.field:focus { outline: none; border-color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.row { display: flex; gap: 8px; align-items: center; }
.grow { flex: 1; min-width: 0; }
.empty { color: var(--muted); padding: 40px; text-align: center; }

.topbar { height: 56px; background: var(--panel); border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 12px; padding: 0 20px; flex-shrink: 0; }
.topbar h1 { font-size: 18px; margin: 0; font-weight: 600; }

/* ---------- чаты ---------- */
.inbox { flex: 1; display: grid; grid-template-columns: 320px 1fr 320px; min-height: 0; }
.convlist { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.convlist .filters { display: flex; gap: 4px; padding: 10px; flex-wrap: wrap; border-bottom: 1px solid var(--line); }
.convlist .filters button { border: 0; background: #f0f3f6; border-radius: 14px; padding: 4px 10px; cursor: pointer; font-size: 12px; }
.convlist .filters button.active { background: var(--accent); color: #fff; }
.convlist .search { padding: 8px 10px; border-bottom: 1px solid var(--line); }
.convlist .items { overflow-y: auto; flex: 1; }
.conv { padding: 10px 12px; border-bottom: 1px solid #f0f2f4; cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
.conv:hover { background: #f7f9fb; }
.conv.active { background: var(--accent-soft); }
.conv.waiting { box-shadow: inset 3px 0 0 var(--warn); }
.conv .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .time { color: var(--muted); font-size: 12px; }
.conv .preview { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .tags { display: flex; gap: 4px; margin-top: 3px; grid-column: 1 / -1; }

.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-head { background: var(--panel); border-bottom: 1px solid var(--line); padding: 10px 16px; display: flex; align-items: center; gap: 10px; }
.chat-head .title { font-weight: 600; font-size: 15px; }
.feed { flex: 1; overflow-y: auto; padding: 16px 20px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 72%; padding: 8px 12px; border-radius: 12px; white-space: pre-wrap; word-wrap: break-word; line-height: 1.4; position: relative; }
.msg.in { align-self: flex-start; background: #fff; border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg.out { align-self: flex-end; background: #d9ecff; border-bottom-right-radius: 4px; }
.msg.out.bot { background: var(--bot-soft); }
.msg.note { align-self: center; background: var(--note); max-width: 85%; font-size: 13px; border: 1px dashed #e6cf73; }
.msg .meta { font-size: 11px; color: var(--muted); margin-top: 3px; }
.msg.failed { border: 1px solid var(--danger); }
.msg img { max-width: 260px; max-height: 260px; border-radius: 6px; display: block; margin-bottom: 4px; }
.day { align-self: center; font-size: 12px; color: var(--muted); padding: 4px 10px; }

.draft { margin: 0 16px 10px; background: var(--bot-soft); border: 1px solid #d9d0ff; border-radius: var(--radius); padding: 10px 12px; }
.draft .head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; font-size: 12px; color: var(--bot); font-weight: 600; }
.draft .text { white-space: pre-wrap; line-height: 1.4; max-height: 220px; overflow-y: auto; }
.draft .actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.spinner { width: 12px; height: 12px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; display: inline-block; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 10px 16px; }
.composer .modes { display: flex; gap: 12px; margin-bottom: 6px; font-size: 12px; }
.composer .modes button { border: 0; background: none; padding: 2px 0; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; }
.composer .modes button.active { color: var(--text); border-color: var(--accent); }
.composer textarea { resize: none; min-height: 64px; max-height: 240px; }
.composer.note textarea { background: var(--note); }

.side-panel { background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; padding: 14px 16px; }
.section { margin-bottom: 18px; }
.section h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); margin: 0 0 8px; font-weight: 600; }
.kv { display: grid; grid-template-columns: 80px 1fr; gap: 6px 8px; align-items: center; }
.kv label { color: var(--muted); font-size: 12px; }
.kv input, .kv select { border: 1px solid transparent; background: transparent; padding: 4px 6px; border-radius: 4px; width: 100%; }
.kv input:hover, .kv select:hover { border-color: var(--line); }
.kv input:focus { outline: none; border-color: var(--accent); background: #fff; }
.order { border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; margin-bottom: 6px; font-size: 13px; }
.order .lines { color: var(--muted); font-size: 12px; margin-top: 4px; }
.mini { border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px; margin-bottom: 6px; font-size: 13px; display: flex; gap: 8px; align-items: center; cursor: pointer; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex-shrink: 0; }

/* ---------- сделки ---------- */
.board { flex: 1; overflow-x: auto; display: flex; gap: 12px; padding: 16px; align-items: flex-start; min-height: 0; }
.col { width: 260px; flex-shrink: 0; background: #e5eaef; border-radius: var(--radius); display: flex; flex-direction: column; max-height: 100%; }
.col-head { padding: 10px 12px 8px; border-top: 4px solid; border-radius: var(--radius) var(--radius) 0 0; }
.col-head .name { font-weight: 600; font-size: 13px; text-transform: uppercase; letter-spacing: .3px; }
.col-head .sum { color: var(--muted); font-size: 12px; margin-top: 2px; }
.col-body { padding: 0 8px 8px; overflow-y: auto; min-height: 60px; flex: 1; }
.col-body.over { background: #d6e7f7; border-radius: 0 0 var(--radius) var(--radius); }
.card { background: #fff; border-radius: 6px; padding: 9px 10px; margin-bottom: 7px; box-shadow: 0 1px 2px rgba(0,0,0,.08); cursor: grab; }
.card .t { font-weight: 600; margin-bottom: 3px; }
.card .c { color: var(--muted); font-size: 12px; }
.card .a { font-weight: 600; font-size: 13px; margin-top: 4px; }
.card.dragging { opacity: .4; }

/* ---------- таблицы ---------- */
.page { flex: 1; overflow-y: auto; padding: 16px 20px; }
table.list { width: 100%; border-collapse: collapse; background: #fff; border-radius: var(--radius); overflow: hidden; }
table.list th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 10px 12px; border-bottom: 1px solid var(--line); background: #fafbfc; }
table.list td { padding: 10px 12px; border-bottom: 1px solid #f0f2f4; }
table.list tr.click { cursor: pointer; }
table.list tr.click:hover td { background: #f7f9fb; }
.card-block { background: #fff; border-radius: var(--radius); padding: 16px 18px; margin-bottom: 14px; max-width: 760px; }
.card-block h2 { font-size: 15px; margin: 0 0 12px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; background: #fff; padding: 6px 12px; cursor: pointer; border-right: 1px solid var(--line); }
.seg button:last-child { border-right: 0; }
.seg button.active { background: var(--accent); color: #fff; }

/* ---------- модалки ---------- */
.overlay { position: fixed; inset: 0; background: rgba(13, 41, 64, .45); display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px; z-index: 50; overflow-y: auto; }
.modal { background: #fff; border-radius: 10px; width: 100%; max-width: 560px; padding: 18px 20px; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.modal.wide { max-width: 860px; }
.modal h2 { margin: 0 0 14px; font-size: 17px; }
.modal .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.form { display: grid; gap: 10px; }
.form label { font-size: 12px; color: var(--muted); display: grid; gap: 4px; }

.login { max-width: 340px; margin: 14vh auto; background: #fff; border-radius: 10px; padding: 26px; box-shadow: 0 4px 24px rgba(0,0,0,.08); }
.login h1 { font-size: 20px; margin: 0 0 18px; }
.error { color: var(--danger); font-size: 13px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1c2b36; color: #fff; padding: 10px 16px; border-radius: 8px; z-index: 100; }

@media (max-width: 1200px) { .inbox { grid-template-columns: 280px 1fr; } .inbox .side-panel { display: none; } }
@media (max-width: 760px) {
  .side { width: 64px; } .side button { font-size: 10px; }
  .inbox { grid-template-columns: 1fr; }
  .inbox.has-chat .convlist { display: none; }
  .inbox:not(.has-chat) .chat { display: none; }
}

/* ---------- v2: канбан отделов и карточка клиента ---------- */
.card .tags { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 5px; }
.card .preview { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }
.card.waiting { box-shadow: inset 3px 0 0 var(--warn), 0 1px 2px rgba(0,0,0,.08); }
.col { width: 280px; }
.client { flex: 1; display: grid; grid-template-columns: 1fr 340px; min-height: 0; }
.client .chat { border-right: 1px solid var(--line); }
.composer .modes { align-items: center; }
@media (max-width: 900px) { .client { grid-template-columns: 1fr; } .client .side-panel { display: none; } }
.col-tools { display: flex; gap: 2px; opacity: 0; transition: opacity .15s; }
.col-head:hover .col-tools { opacity: 1; }
.col-tools button { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 2px 4px; border-radius: 4px; }
.col-tools button:hover { background: rgba(0,0,0,.07); color: var(--text); }
.col-add { width: 200px; flex-shrink: 0; height: 56px; border: 2px dashed #c5ccd4; background: transparent; border-radius: var(--radius); color: var(--muted); cursor: pointer; font-size: 14px; }
.col-add:hover { border-color: var(--accent); color: var(--accent); }
.msg .sig { font-weight: 600; }

/* ---------- окно заказа ---------- */
.order.click { cursor: pointer; transition: border-color .15s, box-shadow .15s; }
.order.click:hover { border-color: var(--accent); box-shadow: 0 2px 8px rgba(26,140,255,.12); }
.o-wrap { display: grid; gap: 14px; }
.o-head { flex-wrap: wrap; }
.o-total { font-size: 20px; font-weight: 700; }
table.o-lines { width: 100%; border-collapse: collapse; }
.o-lines th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 6px 8px; border-bottom: 1px solid var(--line); }
.o-lines td { padding: 8px; border-bottom: 1px solid #f0f2f4; vertical-align: middle; }
.o-lines .num { text-align: right; white-space: nowrap; }
.o-lines a { color: var(--text); text-decoration: none; font-weight: 500; }
.o-lines a:hover { color: var(--accent); text-decoration: underline; }
.o-img { width: 56px; }
.o-img img, .o-noimg { width: 48px; height: 48px; object-fit: contain; border-radius: 6px; border: 1px solid var(--line); background: #fff; display: grid; place-items: center; }
.o-sum { justify-self: end; width: 300px; display: grid; gap: 4px; font-size: 13px; }
.o-sum > div { display: flex; justify-content: space-between; gap: 12px; }
.o-sum > div span:first-child { color: var(--muted); }
.o-grand { border-top: 1px solid var(--line); padding-top: 6px; margin-top: 2px; font-size: 16px; font-weight: 700; }
.o-grand span:first-child { color: var(--text) !important; }
.o-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.o-box { background: #f7f9fb; border-radius: 8px; padding: 12px 14px; }
.o-box h3 { font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); margin: 0 0 8px; }
.o-kv { display: grid; grid-template-columns: 92px 1fr; gap: 5px 10px; font-size: 13px; }
.o-kv > span { color: var(--muted); }
.o-comment div { white-space: pre-wrap; font-size: 13px; }
.modal .foot a.btn { text-decoration: none; color: var(--text); }
@media (max-width: 700px) { .o-grid { grid-template-columns: 1fr; } .o-sum { width: 100%; } }

/* ---------- вложения ---------- */
.msg .att-photo { max-width: 280px; max-height: 280px; border-radius: 8px; display: block; margin-bottom: 4px; cursor: zoom-in; background: #eef1f4; }
.att-video { max-width: 340px; max-height: 300px; border-radius: 8px; display: block; margin-bottom: 4px; background: #000; }
.att-video.round { width: 220px; height: 220px; border-radius: 50%; object-fit: cover; }
.att-audio { display: grid; gap: 4px; margin-bottom: 4px; }
.att-audio audio { width: 280px; height: 36px; }
.att-file { display: flex; align-items: center; gap: 10px; background: rgba(255,255,255,.7); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; margin-bottom: 4px; min-width: 260px; white-space: normal; }
.att-ico { width: 38px; height: 38px; border-radius: 6px; color: #fff; font-size: 11px; font-weight: 700; display: grid; place-items: center; flex-shrink: 0; }
.att-name { font-weight: 500; word-break: break-all; }
.att-file a.btn { text-decoration: none; color: var(--text); }
.att-err { font-size: 12px; color: var(--danger); margin-bottom: 4px; }

.lightbox { position: fixed; inset: 0; z-index: 200; background: rgba(10,14,20,.92); display: flex; flex-direction: column; }
.lb-top { display: flex; align-items: center; gap: 8px; padding: 10px 14px; color: #fff; }
.lb-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-count { color: #aab4c0; font-size: 13px; font-weight: 400; }
.lb-btn { background: rgba(255,255,255,.1); border: 0; color: #fff; width: 38px; height: 38px; border-radius: 8px; cursor: pointer; font-size: 16px; display: grid; place-items: center; text-decoration: none; }
.lb-btn:hover { background: rgba(255,255,255,.22); }
.lb-stage { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; position: relative; padding: 0 60px 20px; }
.lb-img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; }
.lb-nav { position: absolute; top: 50%; transform: translateY(-50%); width: 48px; height: 64px; border: 0; border-radius: 10px; background: rgba(255,255,255,.1); color: #fff; font-size: 34px; cursor: pointer; }
.lb-nav:hover { background: rgba(255,255,255,.22); }
.lb-nav.left { left: 8px; } .lb-nav.right { right: 8px; }
.viewer .lb-stage { padding: 0 20px 20px; }
.vw-frame { width: 100%; height: 100%; border: 0; border-radius: 6px; background: #fff; }
.vw-media { max-width: 100%; max-height: 100%; }

/* ---------- настройки: вкладки и пользователи ---------- */
.tabs { display: flex; gap: 4px; margin-left: 12px; }
.tab { padding: 7px 14px; border-radius: 6px; color: var(--muted); text-decoration: none; font-size: 14px; }
.tab:hover { background: #f0f3f6; color: var(--text); }
.tab.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.card-block.wide { max-width: 1100px; }
table.users td { vertical-align: middle; }
table.users tr.off td { opacity: .55; }
.u-av { width: 34px; height: 34px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); font-weight: 700; font-size: 12px; display: grid; place-items: center; flex-shrink: 0; }
@media (max-width: 760px) { .tabs { overflow-x: auto; } }
.autofill { color: var(--ok); margin-top: 8px; }

/* ---------- наличие в черновике ---------- */
.stock { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 8px; }
.stock-chip { font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 12px; text-decoration: none; white-space: nowrap; }
.stock-chip.ok { background: #e6f6ec; color: #1f9a55; }
.stock-chip.no { background: #fde7e8; color: var(--danger); }
.stock-chip .sc-note { font-weight: 400; opacity: .85; }
