:root {
  --bg: #f4f6fa;
  --card: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --soft: #f1f5f9;
  --wa: #1fa855;
  --tg: #2a9de0;
  --ok: #15803d;
  --warn: #b45309;
  --bad: #b91c1c;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 4px 16px rgba(15, 23, 42, .05);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --card: #111a2e;
    --ink: #e2e8f0;
    --muted: #94a3b8;
    --line: #22304a;
    --soft: #172238;
    --ok: #4ade80;
    --warn: #fbbf24;
    --bad: #f87171;
    --accent: #3b82f6;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
code { background: var(--soft); padding: 0 4px; border-radius: 4px; font-size: .9em; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.error { color: var(--bad); min-height: 1em; margin: 6px 0 0; white-space: pre-wrap; }
.boot { display: grid; place-items: center; min-height: 100vh; color: var(--muted); }

/* ---------- buttons and inputs ---------- */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--card); color: var(--ink);
  padding: 8px 14px; border-radius: 10px; font: inherit; font-weight: 600; cursor: pointer;
  transition: background .12s, border-color .12s, opacity .12s; white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.btn.wide { width: 100%; }
input[type=text], input[type=password], input[type=search], input[type=number], input[type=tel], select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); color: var(--ink); font: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 0; border-color: var(--accent); }
textarea { resize: vertical; min-height: 44px; }
input[type=number] { width: 110px; }
.inline { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
.form { display: grid; gap: 12px; }
.form label { display: grid; gap: 2px; font-weight: 600; }
.hint { display: block; font-weight: 400; color: var(--muted); font-size: 13px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row > input { flex: 1 1 180px; }

/* ---------- login ---------- */
.login { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-box { width: min(380px, 100%); background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 28px 24px; box-shadow: var(--shadow); display: grid; gap: 12px; text-align: center; }
.login-box h1 { margin: 4px 0 0; font-size: 22px; }
.login-box p { margin: 0; }

/* ---------- top bar ---------- */
.brand-mark { display: inline-flex; align-items: center; gap: 2px; font-weight: 800; font-size: 15px; }
.brand-mark span { display: inline-grid; place-items: center; width: 26px; height: 26px; border-radius: 8px; color: #fff; }
.brand-mark .wa { background: var(--wa); }
.brand-mark .tg { background: var(--tg); }
.brand-mark .arrows { background: none; color: var(--muted); width: auto; font-size: 14px; }
.brand-mark.big { justify-self: center; font-size: 22px; }
.brand-mark.big span { width: 40px; height: 40px; border-radius: 12px; }
.brand-mark.big .arrows { width: auto; }
.top {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: 10px max(16px, env(safe-area-inset-left)); background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-name { font-weight: 700; }
.brand-sub { font-size: 12px; color: var(--muted); }
.pills { display: flex; gap: 6px; flex-wrap: wrap; margin-left: auto; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; background: var(--card); border: 1px solid var(--line); font-size: 12.5px; font-weight: 600; }
.pill i, .state i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.pill.ok i, .state.ok i { background: var(--ok); }
.pill.warn i, .state.warn i { background: var(--warn); }
.pill.bad i, .state.bad i { background: var(--bad); }

/* ---------- layout ---------- */
.main { max-width: 1180px; margin: 0 auto; padding: 16px; display: grid; gap: 16px; }
.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); min-width: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.card h2 { margin: 0; font-size: 17px; display: flex; align-items: center; gap: 8px; }
.dot-wa, .dot-tg { width: 10px; height: 10px; border-radius: 3px; background: var(--wa); display: inline-block; }
.dot-tg { background: var(--tg); }
.state { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.state.ok { color: var(--ok); }
.state.warn { color: var(--warn); }
.state.bad { color: var(--bad); }
.foot { text-align: center; padding: 4px 0 12px; }
.sub { margin-top: 12px; border-top: 1px dashed var(--line); padding-top: 10px; }
.sub summary { cursor: pointer; font-weight: 600; color: var(--muted); }
.sub[open] summary { margin-bottom: 10px; }

/* ---------- account cards ---------- */
.who { display: flex; align-items: center; gap: 12px; margin: 4px 0 12px; }
.avatar { width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: #fff; background: var(--wa); flex: none; }
.avatar.tg { background: var(--tg); }
.who b { display: block; }
.qr { display: grid; justify-items: center; gap: 8px; margin: 8px 0; }
.qr .code { width: min(260px, 100%); background: #fff; padding: 10px; border-radius: 12px; border: 1px solid var(--line); }
.qr .code svg { display: block; width: 100%; height: auto; }
.pair { font: 700 30px/1.2 ui-monospace, "Cascadia Mono", Consolas, monospace; letter-spacing: 4px; text-align: center; padding: 10px; background: var(--soft); border-radius: 12px; margin: 8px 0; user-select: all; }
.steps { margin: 6px 0 10px; padding-left: 20px; color: var(--muted); font-size: 13.5px; }
.steps li { margin: 2px 0; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { appearance: none; border: 0; background: transparent; color: var(--muted); padding: 6px 12px; font: inherit; font-size: 13px; font-weight: 600; cursor: pointer; }
.seg button.on { background: var(--soft); color: var(--ink); }
.note { background: var(--soft); border-radius: 10px; padding: 8px 10px; font-size: 13px; color: var(--muted); margin: 8px 0; }
.note.bad { color: var(--bad); }
.note.warn { color: var(--warn); }

/* ---------- routes ---------- */
.routes { display: grid; gap: 10px; }
.route { display: grid; grid-template-columns: 1fr auto; gap: 8px 14px; align-items: center; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.route.off { opacity: .55; }
.route .chats { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-width: 0; }
.chat { display: inline-flex; align-items: center; gap: 6px; min-width: 0; max-width: 100%; }
.chat .nm { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 300px; }
.chat .kd { font-size: 11.5px; color: var(--muted); border: 1px solid var(--line); border-radius: 6px; padding: 0 5px; white-space: nowrap; }
.arrow { font-size: 18px; color: var(--muted); font-weight: 700; }
.route .meta { grid-column: 1 / -1; font-size: 12.5px; color: var(--muted); display: flex; gap: 12px; flex-wrap: wrap; }
.route .acts { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.switch { position: relative; width: 38px; height: 22px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; background: var(--line); border-radius: 999px; transition: background .15s; cursor: pointer; }
.switch span::after { content: ""; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0,0,0,.3); }
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::after { transform: translateX(16px); }
.empty { text-align: center; padding: 18px; color: var(--muted); border: 1px dashed var(--line); border-radius: 12px; }

/* ---------- activity ---------- */
.table-wrap { overflow-x: auto; }
table.act { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.act th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.act td { padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.act td.t { white-space: nowrap; color: var(--muted); font-variant-numeric: tabular-nums; }
table.act td.r { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
table.act td.m { max-width: 420px; overflow-wrap: anywhere; }
table.act .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
table.act tr.failed td.m { color: var(--bad); }
table.act tr.skipped td { color: var(--muted); }
.dir { font-weight: 700; white-space: nowrap; }
.dir.wa2tg, .dir.tg2tg { color: var(--tg); }
.dir.tg2wa, .dir.wa2wa { color: var(--wa); }

/* ---------- route editor ---------- */
dialog { border: 1px solid var(--line); border-radius: 16px; padding: 0; background: var(--card); color: var(--ink); width: min(980px, calc(100vw - 24px)); max-height: calc(100vh - 24px); box-shadow: 0 24px 60px rgba(0,0,0,.35); }
dialog::backdrop { background: rgba(2, 6, 23, .55); }
.route-form { padding: 16px 18px; display: grid; gap: 14px; }
.dialog-head, .dialog-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.dialog-foot { justify-content: flex-end; }
.dialog-head h2 { margin: 0; font-size: 18px; }
.pick-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 340px), 1fr)); gap: 14px; }
.pick { border: 1px solid var(--line); border-radius: 12px; padding: 12px; display: grid; gap: 8px; align-content: start; min-width: 0; }
.pick h3 { margin: 0; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.pick-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.seg.net button { display: inline-flex; align-items: center; gap: 6px; }
.direction .btn { margin-left: auto; }
.chosen { min-height: 38px; display: flex; align-items: center; gap: 8px; padding: 6px 10px; border-radius: 10px; background: var(--soft); font-weight: 600; overflow: hidden; }
.chosen.none { color: var(--muted); font-weight: 400; }
.results { max-height: 230px; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px; }
.results:empty { display: none; }
.results button { display: flex; width: 100%; align-items: center; gap: 8px; padding: 8px 10px; border: 0; border-bottom: 1px solid var(--line); background: transparent; color: var(--ink); font: inherit; text-align: left; cursor: pointer; }
.results button:last-child { border-bottom: 0; }
.results button:hover, .results button.sel { background: var(--soft); }
.results .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.results .msg { padding: 10px; color: var(--muted); font-size: 13px; }
.resolve { display: flex; gap: 6px; }
.direction { border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px; display: flex; gap: 18px; flex-wrap: wrap; margin: 0; }
.direction legend { font-weight: 600; padding: 0 4px; }
.direction label { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.opts { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 280px), 1fr)); gap: 10px 16px; }
.opts label { display: grid; gap: 3px; font-weight: 600; font-size: 14px; }
.opts label.check { display: flex; align-items: center; gap: 8px; font-weight: 500; }
.opts label.full { grid-column: 1 / -1; }

/* ---------- toast ---------- */
.toast { position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%) translateY(16px); opacity: 0; visibility: hidden; pointer-events: none; background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600; max-width: calc(100vw - 32px); transition: transform .2s, opacity .2s, visibility .2s; z-index: 50; box-shadow: var(--shadow); }
.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; visibility: visible; }
.toast.bad { background: var(--bad); color: #fff; }

@media (max-width: 640px) {
  .main { padding: 12px; gap: 12px; }
  .card { padding: 14px; }
  .pills { margin-left: 0; width: 100%; }
  .route { grid-template-columns: 1fr; }
  .route .acts { justify-content: flex-start; }
  table.act td.r { max-width: 120px; }
  .chat .nm { max-width: 200px; }
}
