:root {
  --bg: #F9FAFB;
  --surface: #ffffff;
  --border: #E5E7EB;
  --border-soft: #F3F4F6;
  --input-border: #D1D5DB;
  --text: #111827;
  --text-2: #374151;
  --muted: #6B7280;
  --primary: #1A56DB;
  --primary-hover: #1E429F;
  --primary-soft: #E1EFFE;
  --selected: #EBF5FF;
  --green: #046C4E;
  --green-dot: #0E9F6E;
  --green-soft: #DEF7EC;
  --green-text: #03543F;
  --red: #C81E1E;
  --red-soft: #FDE8E8;
  --red-border: #F8B4B4;
  --amber-text: #723B13;
  --amber-soft: #FDF6B2;
  --code-bg: #1F2937;
  --code-text: #E5E7EB;
  --shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --radius: 8px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body { margin: 0; font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif; font-size: 14px; color: var(--text); background: var(--bg); }
button, input, select, textarea { font-family: inherit; color: inherit; }
a { color: var(--primary); }
svg { flex-shrink: 0; }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.mono { font-family: var(--mono); font-size: 13px; word-break: break-all; }
.muted { color: var(--muted); font-size: 13px; font-weight: 400; }
.hint { font-size: 12px; color: var(--muted); line-height: 1.5; }
.err-text { color: var(--red); font-size: 13px; }
.err-text:empty { display: none; }
.grow { flex-grow: 1; }
.self-start { align-self: flex-start; }

/* ------------------------------------------------------------ каркас */
.layout { display: flex; min-height: 100vh; }

.side {
  width: 248px; flex-shrink: 0; position: sticky; top: 0; height: 100vh; overflow-y: auto;
  padding: 20px 12px; display: flex; flex-direction: column; gap: 4px;
  background: var(--surface); border-right: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 20px; }
.brand-mark { width: 34px; height: 34px; border-radius: var(--radius); background: var(--primary); display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.brand-name { font-size: 15px; font-weight: 700; line-height: 1.2; }
.brand-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }

.nav-links { display: flex; flex-direction: column; gap: 4px; }
.nav-item {
  display: flex; align-items: center; gap: 12px; min-height: 44px; padding: 0 10px;
  border-radius: var(--radius); font-size: 14px; font-weight: 500; color: var(--text); text-decoration: none;
}
.nav-item:hover { background: var(--border-soft); color: var(--text); }
.nav-item svg { width: 20px; height: 20px; fill: none; stroke: var(--muted); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.nav-item.active { background: var(--border-soft); }
.nav-item.active svg { stroke: var(--primary); }
.nav-label { flex-grow: 1; }
.nav-sep { height: 1px; background: var(--border); margin: 12px 4px; }
.nav-item.flash { background: var(--green-soft); transition: none; }
.nav-item.fade { transition: background .8s; }

.side-foot { margin-top: auto; padding-top: 16px; display: flex; flex-direction: column; gap: 10px; }
.recv-card { display: flex; flex-direction: column; gap: 6px; padding: 14px; border-radius: var(--radius); border: 1px solid var(--border); }
.recv-head { display: flex; align-items: center; justify-content: space-between; font-size: 13px; font-weight: 600; }
.recv-sub { font-size: 12px; color: var(--muted); }
.recv-sub:empty { display: none; }
.side-user { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 0 8px; font-size: 12px; color: var(--muted); }
.side-user[hidden] { display: none; }
.side-note { font-size: 11px; color: var(--muted); padding: 0 8px; }

.main { flex-grow: 1; min-width: 0; padding: 32px 40px; }
.view { display: flex; flex-direction: column; gap: 24px; }
.view[hidden] { display: none; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 24px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 24px; font-weight: 700; }
.lead { margin: 6px 0 0; font-size: 14px; color: var(--muted); }
.head-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.title-line { display: flex; align-items: center; gap: 12px; }

/* ------------------------------------------------------------ элементы */
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.panel.pad { padding: 20px; overflow: visible; }
.panel h2 { margin: 0; font-size: 15px; font-weight: 600; }
.panel h3 { margin: 0; font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.panel-foot { padding: 14px 20px; font-size: 13px; color: var(--muted); }
.col { display: flex; flex-direction: column; gap: 12px; }
.sec-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
#sec-access .hint { margin-top: 10px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 40px; padding: 0 16px;
  border-radius: var(--radius); border: 1px solid transparent; font-size: 14px; font-weight: 500;
  cursor: pointer; text-decoration: none; white-space: nowrap; background: var(--surface);
}
.btn svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); color: #fff; }
.btn-ghost { background: var(--surface); color: var(--text); border-color: var(--input-border); }
.btn-ghost:hover { background: var(--bg); color: var(--text); }
.btn-success { background: var(--green); color: #fff; border-color: var(--green); }
.btn-danger { background: var(--red); color: #fff; }
.btn-danger-ghost { background: var(--surface); color: var(--red); border-color: var(--red-border); }
.btn-danger-ghost:hover { background: var(--red-soft); }
.btn-sm { height: 32px; padding: 0 10px; font-size: 13px; }
.btn-icon {
  display: inline-flex; align-items: center; justify-content: center; width: 32px; height: 32px; padding: 0;
  border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--muted); cursor: pointer; font-size: 13px;
}
.btn-icon:hover { color: var(--text); background: var(--bg); }
.btn-icon:disabled { opacity: .4; cursor: default; }

.input, .select {
  height: 40px; padding: 0 12px; border: 1px solid var(--input-border); border-radius: var(--radius);
  background: var(--surface); font-size: 14px; width: 100%;
}
textarea.input { height: auto; min-height: 96px; padding: 10px 12px; font-family: var(--mono); font-size: 13px; resize: vertical; }
.select { width: auto; }
.input.narrow { width: 140px; }
.input:focus, .select:focus, .search:focus-within { outline: 2px solid var(--primary-soft); border-color: var(--primary); }

.search {
  display: flex; align-items: center; gap: 8px; height: 40px; padding: 0 12px; width: 260px;
  background: var(--surface); border: 1px solid var(--input-border); border-radius: var(--radius); color: var(--muted);
}
.search.wide { width: 320px; }
.search svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.search input { border: none; outline: none; background: transparent; font-size: 14px; flex-grow: 1; min-width: 0; color: var(--text); }

.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.field-inline { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.chk { display: flex; align-items: center; gap: 8px; min-height: 40px; font-size: 14px; color: var(--text); cursor: pointer; }
.chk input { width: 18px; height: 18px; margin: 0; accent-color: var(--primary); }
.inline-form { display: flex; gap: 8px; }
.inline-form .input { flex: 1; }

.badge {
  display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 500; line-height: 1.5;
  padding: 1px 8px; border-radius: 999px; background: var(--border-soft); color: #1F2937; white-space: nowrap;
}
.badge:empty { display: none; }
.badge.blue { background: var(--primary-soft); color: var(--primary-hover); }
.badge.green { background: var(--green-soft); color: var(--green-text); }
.badge.red { background: var(--red-soft); color: var(--red); }
.badge.amber { background: var(--amber-soft); color: var(--amber-text); }
.badge.big { padding: 3px 10px; }
.badge i { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.live { background: var(--border-soft); color: var(--muted); }
.badge.live.on { background: var(--green-soft); color: var(--green-text); }
.badge.live.on i { background: var(--green-dot); }

.st-line { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.st-line > span:last-child { min-width: 0; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: 0 0 auto; }
.dot.green { background: var(--green); }
.dot.red { background: var(--red); }
.dot.gray { background: #9CA3AF; }

/* ------------------------------------------------------------ клиенты */
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px 18px; }
.kpi-label { font-size: 13px; color: var(--muted); }
.kpi-val { font-size: 28px; font-weight: 700; margin-top: 6px; }
.kpi-val.warn { color: var(--amber-text); }

.grid-row { display: grid; gap: 16px; align-items: center; padding: 18px 20px; border-bottom: 1px solid var(--border-soft); }
.grid-head {
  padding: 12px 20px; font-size: 12px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase;
  color: var(--text-2); background: var(--bg); border-bottom: 1px solid var(--border);
}
.grid-clients { grid-template-columns: 200px minmax(0, 1fr) 140px 120px 130px; }
.client-name { font-size: 15px; font-weight: 600; }
.client-id { font-size: 12px; color: var(--muted); font-family: var(--mono); margin-top: 4px; }
/* сценарии — сегментированный переключатель: полоска, выбранный сегмент белый с цветной точкой */
.scenarios { display: flex; align-items: flex-start; gap: 10px; min-width: 0; }
.seg { display: inline-flex; flex-wrap: wrap; gap: 2px; padding: 3px; background: var(--border-soft); border-radius: var(--radius); min-width: 0; }
.seg-btn {
  display: inline-flex; align-items: center; gap: 8px; height: 34px; padding: 0 14px; border: none; border-radius: 6px;
  background: transparent; color: #4B5563; font-size: 13px; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.seg-btn:hover { color: var(--text); background: rgba(255, 255, 255, .6); }
.seg-btn .dot { width: 8px; height: 8px; background: var(--input-border); }
.seg-btn.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(17, 24, 39, .12); }
.seg-btn.active .dot { background: var(--green); }
.seg-btn.active.off .dot { background: var(--red); }
.seg-btn svg { width: 13px; height: 13px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; opacity: .7; }
.seg-add { flex-shrink: 0; margin-top: 3px;
  display: inline-flex; align-items: center; justify-content: center; width: 34px; height: 34px; padding: 0;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--muted); cursor: pointer;
}
.seg-add:hover { color: var(--primary); border-color: var(--primary); }
.seg-add svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.silent { color: var(--amber-text); }
.empty { padding: 24px 20px; color: var(--muted); }

/* ------------------------------------------------------------ таблицы */
.tbl-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 12px 16px; border-bottom: 1px solid var(--border-soft); vertical-align: top; }
th { font-size: 12px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--text-2); background: var(--bg); border-bottom-color: var(--border); white-space: nowrap; }
tr.shadowed td { background: #FFFBEB; }
td .actions { display: flex; gap: 6px; align-items: center; }
td.nowrap { white-space: nowrap; word-break: normal; }

/* ------------------------------------------------------------ журнал */
.view-fill { height: calc(100vh - 64px); gap: 20px; }
.filters { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.log-split { display: flex; gap: 20px; flex-grow: 1; min-height: 0; }
.log-list { flex-grow: 1; min-width: 0; display: flex; flex-direction: column; }
.log-rows { overflow-y: auto; flex-grow: 1; }
.grid-log { grid-template-columns: 136px 56px minmax(0, 1fr) 170px 48px 72px; gap: 12px; padding: 0 18px; }
.grid-log.grid-head { padding: 12px 18px; }
.log-row {
  width: 100%; min-height: 48px; border: none; border-bottom: 1px solid var(--border-soft); background: var(--surface);
  text-align: left; font-size: 13px; cursor: pointer;
}
.log-row:hover { background: var(--bg); }
.log-row.sel { background: var(--selected); }
.log-row > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.log-row .m { font-family: var(--mono); }
.log-row .dim { color: var(--muted); }
.log-row .miss { color: var(--amber-text); font-style: italic; }
.code-ok { color: var(--green); font-weight: 500; }
.code-bad { color: var(--amber-text); font-weight: 500; }
.sys-row { padding: 10px 18px; font-size: 13px; color: var(--muted); font-style: italic; background: var(--bg); border-bottom: 1px solid var(--border-soft); display: flex; gap: 12px; }
.sys-row.warn-row { background: #FFFBEB; color: var(--amber-text); }
.sys-row.error-row { background: var(--red-soft); color: var(--red); }

.log-detail { width: 380px; flex-shrink: 0; padding: 20px; display: flex; flex-direction: column; gap: 16px; overflow-y: auto; }
.det-head { display: flex; align-items: center; justify-content: space-between; font-size: 15px; font-weight: 600; }
.det-code { font-family: var(--mono); font-size: 13px; font-weight: 500; padding: 2px 8px; border-radius: 6px; }
.det-code.ok { color: var(--green); background: var(--green-soft); }
.det-code.bad { color: var(--amber-text); background: var(--amber-soft); }
.det-req { font-family: var(--mono); font-size: 13px; background: var(--bg); padding: 10px 12px; border-radius: var(--radius); word-break: break-all; }
.det-grid { display: grid; grid-template-columns: 110px minmax(0, 1fr); gap: 8px 12px; font-size: 13px; }
.det-grid > :nth-child(odd) { color: var(--muted); }
.det-grid > :nth-child(even) { word-break: break-all; }
.det-label { font-size: 12px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); margin-bottom: 6px; }
.det-body { font-family: var(--mono); font-size: 12px; line-height: 1.5; background: var(--code-bg); color: var(--code-text); padding: 12px; border-radius: var(--radius); word-break: break-all; white-space: pre-wrap; }
.log-detail .btn { height: 44px; }

/* ------------------------------------------------------------ настройки */
.cards3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; }
.cards3 .inline-form { flex-wrap: wrap; }
.cols3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 28px; }
#domains-list, #tg-chats, #tg-pending, #users-list { display: flex; flex-direction: column; gap: 8px; }
#tg-notify .chk { min-height: 32px; }
.user-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; font-size: 14px; }

/* ------------------------------------------------------------ диалоги */
dialog { border: none; border-radius: 12px; padding: 0; width: min(820px, 94vw); color: inherit; box-shadow: 0 20px 40px rgba(17, 24, 39, .2); }
dialog::backdrop { background: rgba(17, 24, 39, .45); }
.dlg-head { padding: 16px 20px; border-bottom: 1px solid var(--border); font-size: 16px; font-weight: 600; }
.dlg-body { padding: 16px 20px; max-height: 72vh; overflow: auto; }
.dlg-body label { display: block; font-size: 13px; color: var(--text-2); margin: 12px 0 6px; font-weight: 500; }
.dlg-body h4 { margin: 22px 0 4px; font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; border-top: 1px solid var(--border-soft); padding-top: 16px; }
.dlg-foot { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; }
.row { display: flex; gap: 12px; flex-wrap: wrap; }
.row > div { flex: 1; min-width: 160px; }
.preview { background: var(--bg); padding: 10px 12px; border-radius: var(--radius); white-space: pre-wrap; min-height: 20px; }
.extra-row { display: flex; gap: 6px; margin-bottom: 6px; align-items: center; }
.extra-row .input { flex: 1; }
.extra-row select.input { max-width: 130px; }

#toast-wrap { position: fixed; top: 16px; right: 16px; z-index: 9999; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--text); color: #fff; padding: 11px 15px; border-radius: var(--radius); font-size: 14px; max-width: 360px; box-shadow: 0 6px 20px rgba(17, 24, 39, .28); transition: opacity .35s, transform .35s; }
.toast.error { background: var(--red); }
.toast.ok { background: var(--green); }

/* ------------------------------------------------------------ узкие экраны */
@media (max-width: 1280px) {
  .grid-clients { grid-template-columns: 170px minmax(0, 1fr) 120px 100px; }
  .grid-clients > :nth-child(5) { display: none; }
  .log-detail { width: 320px; }
  .cols3 { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 1080px) {
  .cards3 { grid-template-columns: 1fr 1fr; }
  .log-split { flex-direction: column; }
  .log-detail { width: auto; }
  .view-fill { height: auto; }
  .log-rows { max-height: 60vh; }
}
@media (max-width: 860px) {
  .layout { flex-direction: column; }
  .side { position: static; width: auto; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: 10px 12px; border-right: none; border-bottom: 1px solid var(--border); }
  .brand { padding: 0 8px 0 0; }
  .brand-sub { display: none; }
  .nav-links { flex-direction: row; overflow-x: auto; width: 100%; }
  .nav-item { flex-shrink: 0; }
  .nav-sep, .side-foot { display: none; }
  .main { padding: 20px 16px; }
  .kpis { grid-template-columns: 1fr 1fr; }
  .cards3, .cols3 { grid-template-columns: 1fr; }
  .grid-clients { grid-template-columns: 1fr; gap: 10px; }
  .grid-clients.grid-head { display: none; }
  .search, .search.wide { width: 100%; }
  .grid-log { grid-template-columns: 70px minmax(0, 1fr) 48px; }
  .grid-log > :nth-child(2), .grid-log > :nth-child(4), .grid-log > :nth-child(6) { display: none; }
}
