:root {
  --bg: #eef2f4;
  --surface: #ffffff;
  --surface-2: #f7f9fa;
  --border: #dfe3e8;
  --text: #2f3640;
  --muted: #6b7684;
  --accent: #1677b8;
  --accent-soft: #e5f2fb;
  --success: #2e8a3e;
  --success-soft: #e6f4e8;
  --warn: #a35f00;
  --warn-soft: #fff2dc;
  --danger: #c4302b;
  --danger-soft: #fdeaea;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 30, 40, .06), 0 2px 8px rgba(20, 30, 40, .05);
  font-family: "Open Sans", system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); min-height: 100vh; padding-bottom: 80px; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
[hidden] { display: none !important; }

/* ---------- шапка и вкладки ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 16px 24px 8px;
}
.topbar__title { display: flex; align-items: center; gap: 12px; }
.topbar h1 { font-size: 20px; margin: 0; font-weight: 600; }
.topbar__sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.logo { width: 34px; height: 34px; color: var(--accent); flex: none; }

.pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 12px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border); font-size: 12.5px; white-space: nowrap;
}
.pill::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.pill.is-ok::before { background: var(--success); }
.pill.is-error { color: var(--danger); border-color: #f2c2c0; background: var(--danger-soft); }
.pill.is-error::before { background: var(--danger); }

.tabs { display: flex; gap: 4px; padding: 0 24px; border-bottom: 1px solid var(--border); overflow-x: auto; overflow-y: hidden; }
.tab {
  border: 0; background: none; padding: 10px 14px; cursor: pointer; color: var(--muted); font-weight: 600;
  border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.is-active { color: var(--accent); border-bottom-color: var(--accent); }
.badge {
  display: inline-block; min-width: 20px; padding: 1px 6px; border-radius: 999px; background: var(--danger);
  color: #fff; font-size: 11.5px; text-align: center; margin-left: 4px;
}

main { padding: 20px 24px; max-width: 1180px; }

/* ---------- общие элементы ---------- */
.intro { color: var(--muted); margin: 0 0 16px; max-width: 780px; line-height: 1.5; }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 16px; }
.card__head {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 14px 16px; border-bottom: 1px solid var(--border);
}
.card__body { padding: 8px 16px 14px; }
.card__title { font-weight: 600; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }

.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface);
  border-radius: 8px; padding: 7px 14px; cursor: pointer; font-weight: 600; line-height: 1.2;
}
.btn:hover:not(:disabled) { border-color: #b9c1ca; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn--primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn--primary:hover:not(:disabled) { background: #12679f; border-color: #12679f; }
.btn--ghost { background: transparent; }
.btn--link { border: 0; background: none; color: var(--accent); padding: 6px 4px; }
.btn--danger { color: var(--danger); }
.icon-btn {
  width: 30px; height: 30px; display: inline-grid; place-items: center; border: 1px solid transparent; border-radius: 6px;
  background: none; cursor: pointer; color: var(--muted);
}
.icon-btn:hover:not(:disabled) { background: var(--surface-2); border-color: var(--border); color: var(--text); }
.icon-btn:disabled { opacity: .3; cursor: default; }
.icon-btn.is-danger:hover:not(:disabled) { color: var(--danger); }
.icon-btn svg, .btn svg { width: 16px; height: 16px; }

.input, .select, .textarea {
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; background: var(--surface); min-width: 0;
}
.input:focus, .select:focus, .textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.textarea { width: 100%; min-height: 150px; resize: vertical; font-family: ui-monospace, Consolas, monospace; font-size: 13px; line-height: 1.5; }

.switch { position: relative; display: inline-block; width: 36px; height: 20px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: #c7ced6; border-radius: 999px; cursor: pointer; transition: .15s; }
.switch span::after { content: ""; position: absolute; width: 16px; height: 16px; left: 2px; top: 2px; background: #fff; border-radius: 50%; transition: .15s; }
.switch input:checked + span { background: var(--success); }
.switch input:checked + span::after { transform: translateX(16px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); }

/* ---------- правила ---------- */
.group-line { display: flex; align-items: center; gap: 8px; }
.group-line .select { min-width: 240px; font-weight: 600; }
.counter { font-size: 12.5px; color: var(--muted); }
.rule {
  display: grid; grid-template-columns: 42px minmax(210px, auto) 1fr auto; gap: 12px; align-items: start;
  padding: 10px 0; border-bottom: 1px dashed var(--border);
}
.rule:last-of-type { border-bottom: 0; }
.rule.is-disabled > :not(.rule__toggle) { opacity: .5; }
.rule__toggle, .rule__wait, .rule__actions { padding-top: 4px; }
.rule__wait { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.rule__wait .input { width: 72px; }
.rule__label { color: var(--muted); font-size: 12.5px; }
.rule__actions { display: flex; gap: 2px; }
.rule-head {
  display: grid; grid-template-columns: 42px minmax(210px, auto) 1fr auto; gap: 12px; padding: 8px 0 2px;
  color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em;
}
.rule-head span:last-child { width: 96px; }
.empty-rules { padding: 14px 0; color: var(--muted); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; min-height: 32px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 6px 4px 10px; border-radius: 999px; font-size: 13px;
  background: var(--accent-soft); color: #0f4f7a; max-width: 100%;
}
.chip__type { font-size: 11px; opacity: .75; }
.chip button { border: 0; background: none; cursor: pointer; padding: 0 2px; color: inherit; opacity: .6; font-size: 15px; line-height: 1; }
.chip button:hover { opacity: 1; }
.chip--role { background: #efe8fb; color: #4b2a8a; }
.chip--department { background: #e7f5ec; color: #1f6331; }
.chip--group { background: #fff1de; color: #7a4300; }

/* ---------- модальное окно выбора получателей ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(20, 30, 40, .45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--surface); border-radius: 12px; width: min(620px, 100%); height: min(640px, 100%); display: flex; flex-direction: column; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.modal__head { display: flex; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.modal__head h2 { margin: 0; font-size: 16px; flex: 1; }
.modal__selected { padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--surface-2); }
.modal__tabs { display: flex; gap: 4px; padding: 8px 16px 0; border-bottom: 1px solid var(--border); overflow-x: auto; overflow-y: hidden; flex: none; }
.modal__search { padding: 10px 16px; }
.modal__search .input { width: 100%; }
.modal__list { overflow-y: auto; padding: 0 8px 8px; flex: 1; min-height: 200px; }
.modal__foot { padding: 12px 16px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; }
.option {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; border: 0; background: none;
  padding: 8px 10px; border-radius: 8px; cursor: pointer;
}
.option:hover { background: var(--surface-2); }
.option.is-selected { background: var(--accent-soft); }
.option__check { width: 18px; height: 18px; border: 1.5px solid #b9c1ca; border-radius: 4px; display: grid; place-items: center; flex: none; background: #fff; }
.option.is-selected .option__check { background: var(--accent); border-color: var(--accent); color: #fff; }
.option__text { min-width: 0; }
.option__title { font-weight: 600; }
.option__sub { color: var(--muted); font-size: 12.5px; }
.list-empty { color: var(--muted); padding: 24px; text-align: center; }

/* ---------- таблицы ---------- */
.table-wrap { overflow-x: auto; }
table.table { width: 100%; border-collapse: collapse; background: var(--surface); }
.table th, .table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; background: var(--surface-2); white-space: nowrap; }
.table tr.is-clickable { cursor: pointer; }
.table tr.is-clickable:hover td { background: #fafcfd; }
.table tr.details td { background: var(--surface-2); }
.nowrap { white-space: nowrap; }
.status { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.status--sent { background: var(--success-soft); color: var(--success); }
.status--partial, .status--no_recipients { background: var(--warn-soft); color: var(--warn); }
.status--failed { background: var(--danger-soft); color: var(--danger); }
.wait-time { font-weight: 700; font-variant-numeric: tabular-nums; }
.wait-time.is-hot { color: var(--danger); }
.rule-marks { display: flex; flex-wrap: wrap; gap: 4px; }
.rule-mark { font-size: 12px; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); white-space: nowrap; }
.rule-mark.is-fired { background: var(--danger-soft); border-color: #f2c2c0; color: var(--danger); }
.quote { color: var(--muted); font-size: 12.5px; margin-top: 3px; max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.details-grid { display: grid; grid-template-columns: 160px 1fr; gap: 6px 16px; }
.details-grid pre { margin: 0; white-space: pre-wrap; font-family: inherit; }
.recipient-line .err { color: var(--danger); }
.recipient-line .ok { color: var(--success); }

.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.filters .input[type="search"] { flex: 1 1 260px; }
.pager { display: flex; align-items: center; gap: 8px; justify-content: flex-end; padding: 12px 0; }

/* ---------- настройки ---------- */
.form { display: grid; gap: 18px; max-width: 760px; }
.field label { display: block; font-weight: 600; margin-bottom: 6px; }
.field .hint { color: var(--muted); font-size: 12.5px; margin-top: 4px; line-height: 1.45; }
.field-row { display: flex; gap: 24px; flex-wrap: wrap; }
.field-row .field { flex: 1 1 200px; }
.placeholders { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.placeholders code { background: var(--surface-2); border: 1px solid var(--border); padding: 2px 6px; border-radius: 6px; cursor: pointer; font-size: 12px; }
.kv { display: grid; grid-template-columns: 220px 1fr; gap: 8px 16px; }

/* ---------- панель сохранения, тосты, заглушки ---------- */
.savebar {
  position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%); z-index: 40;
  display: flex; align-items: center; gap: 24px; padding: 10px 12px 10px 18px; border-radius: 12px;
  background: #24303c; color: #fff; box-shadow: 0 8px 30px rgba(0,0,0,.25); width: max-content; max-width: calc(100% - 32px);
}
.savebar__actions { display: flex; gap: 8px; }
.savebar .btn--ghost { color: #fff; border-color: rgba(255,255,255,.3); }
.toast {
  position: fixed; top: 16px; right: 16px; z-index: 60; padding: 10px 16px; border-radius: 8px; background: #24303c; color: #fff;
  box-shadow: var(--shadow); max-width: 420px;
}
.toast.is-error { background: var(--danger); }
.banner { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; background: var(--warn-soft); color: var(--warn); }
.banner.is-error { background: var(--danger-soft); color: var(--danger); }
.message-page { max-width: 560px; margin: 80px auto; padding: 32px; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.message-page h1 { font-size: 20px; margin-top: 0; }

@media (max-width: 760px) {
  .topbar, .tabs, main { padding-left: 16px; padding-right: 16px; }
  .topbar { flex-wrap: wrap; }
  .rule, .rule-head { grid-template-columns: 36px 1fr; }
  .rule-head span:nth-child(n+3) { display: none; }
  .rule__recipients, .rule__actions { grid-column: 2; }
  .group-line .select { min-width: 0; width: 100%; }
  .details-grid, .kv { grid-template-columns: 1fr; }
  .savebar { flex-direction: column; gap: 8px; align-items: stretch; }
}
