:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1e222b;
  --border: #2a2f3a;
  --text: #e7eaf0;
  --muted: #8b93a4;
  --accent: #4f7cff;
  --accent-2: #6c8bff;
  --green: #2ecc71;
  --red: #ff5d5d;
  --amber: #f5a623;
  --radius: 14px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
  font-family: -apple-system, "Segoe UI", Roboto, Inter, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(1200px 600px at 80% -10%, #1b2030 0%, var(--bg) 55%) fixed;
  color: var(--text);
  font-size: 15px;
}
a { color: var(--accent-2); text-decoration: none; }
button { font-family: inherit; cursor: pointer; }

/* ===== Login ===== */
.login-wrap {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 32px 28px;
}
.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 22px; }
.brand .logo {
  width: 36px; height: 36px; border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), #9b6bff);
  display: grid; place-items: center; font-weight: 800; color: #fff;
}
.brand h1 { font-size: 18px; margin: 0; }
.brand small { color: var(--muted); }

/* ===== Layout ===== */
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  background: rgba(23, 26, 33, 0.7);
  backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 10;
}
.topbar .brand { margin: 0; }
.topbar .spacer { flex: 1; }
.container { max-width: 1100px; margin: 0 auto; padding: 24px; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 14px; margin-bottom: 24px; }
.stat {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px 18px;
}
.stat .n { font-size: 26px; font-weight: 700; }
.stat .l { color: var(--muted); font-size: 13px; margin-top: 2px; }

.grid { display: grid; grid-template-columns: 1.1fr 1fr; gap: 20px; align-items: start; }
@media (max-width: 860px) { .grid { grid-template-columns: 1fr; } }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px; margin-bottom: 20px;
}
.card h2 { margin: 0 0 14px; font-size: 16px; }
.card .hint { color: var(--muted); font-size: 13px; margin: -8px 0 14px; }

/* ===== Forms ===== */
label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 6px; }
input, select, textarea {
  width: 100%; padding: 11px 12px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 10px; color: var(--text); font-size: 14px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: vertical; min-height: 64px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 16px; border-radius: 10px; border: 1px solid transparent;
  background: var(--accent); color: #fff; font-weight: 600; font-size: 14px;
}
.btn:hover { background: var(--accent-2); }
.btn:disabled { opacity: 0.6; cursor: default; }
.btn.full { width: 100%; }
.btn.ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { padding: 7px 10px; font-size: 13px; }
.btn.danger { background: transparent; border-color: var(--red); color: var(--red); }
.btn.danger:hover { background: rgba(255,93,93,.1); }

.row { display: flex; gap: 10px; align-items: center; }
.row > * { flex: 1; }
.row .auto { flex: 0 0 auto; }

/* ===== Clients list ===== */
.client {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; border: 1px solid var(--border);
  border-radius: 12px; margin-bottom: 10px; background: var(--surface-2);
}
.client .name { font-weight: 600; }
.client .meta { color: var(--muted); font-size: 12px; }
.client .spacer { flex: 1; }
.client.off { opacity: 0.55; }

.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 600;
  border: 1px solid var(--border);
}
.badge.bitrix { color: #4ab3ff; border-color: #2b5e80; }
.badge.amo { color: #56d39a; border-color: #2f6b52; }
.badge.email { color: #f5a623; border-color: #7a5b1e; }

.tag { font-size: 11px; padding: 2px 8px; border-radius: 999px; font-weight: 700; }
.tag.queued { background: #2b3450; color: #9db4ff; }
.tag.sent { background: #1f3b2c; color: #7be0a5; }
.tag.failed { background: #3d2226; color: #ff8d8d; }
.tag.duplicate { background: #3a3320; color: #f0cd7a; }

/* ===== Table ===== */
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 600; font-size: 12px; }
td .err { color: var(--red); font-size: 12px; }
.filters { display: flex; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.filters input, .filters select { width: auto; min-width: 130px; }
.link-btn {
  margin: 14px auto 0;
  display: block;
  border: 0;
  background: transparent;
  color: var(--accent-2);
  font: inherit;
  font-weight: 600;
  padding: 6px 10px;
}
.link-btn:hover { color: #fff; }

/* ===== Modal ===== */
.modal-bg {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: none; place-items: center; padding: 20px; z-index: 50;
}
.modal-bg.open { display: grid; }
.modal {
  width: 100%; max-width: 480px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 24px; max-height: 90vh; overflow: auto;
}
.modal h2 { margin-top: 0; }
.modal .actions { display: flex; gap: 10px; margin-top: 20px; }

.key {
  font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px;
  background: var(--surface-2); border: 1px solid var(--border);
  padding: 6px 8px; border-radius: 8px; word-break: break-all;
}

/* ===== Toast ===== */
#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px; z-index: 100;
}
.toast {
  padding: 12px 18px; border-radius: 10px; color: #fff; font-weight: 600;
  box-shadow: var(--shadow); animation: pop .2s ease;
}
.toast.ok { background: #1f6b43; }
.toast.err { background: #8a2b2b; }
.toast.info { background: #2b3f6b; }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }

.muted { color: var(--muted); }
.empty { color: var(--muted); text-align: center; padding: 24px; }
.err-text { color: var(--red); font-size: 13px; margin-top: 10px; min-height: 18px; }

/* ===== Tabs ===== */
.tabs { display: flex; gap: 6px; margin-bottom: 22px; border-bottom: 1px solid var(--border); }
.tab {
  padding: 10px 18px; background: transparent; border: 0; color: var(--muted);
  font-weight: 600; font-size: 14px; border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }

/* ===== Incoming: suppliers + inbox ===== */
.forward-bar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 12px; margin-bottom: 14px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px;
}
.forward-bar .spacer { flex: 1; }
.forward-bar select { width: auto; }

.sup-stats { display: flex; gap: 14px; flex-wrap: wrap; margin-left: auto; }
.sup-stats .s-item { font-size: 12px; color: var(--muted); }
.sup-stats .s-item b { color: var(--text); font-size: 13px; }

td.check, th.check { width: 34px; text-align: center; }
td.check input { width: auto; }
.rec-link { color: var(--accent-2); font-size: 12px; }
.rec-link:hover { color: #fff; }

.tag.new { background: #2b3450; color: #9db4ff; }
.tag.forwarded { background: #1f3b2c; color: #7be0a5; }
.tag.dismissed { background: #33383f; color: #aab3c2; }
