:root {
  --bg: #f1f5f9;
  --panel: #ffffff;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --primary-soft: #eef2ff;
  --green: #16a34a;
  --green-soft: #dcfce7;
  --red: #dc2626;
  --red-soft: #fee2e2;
  --amber: #d97706;
  --amber-soft: #fef3c7;
  --blue: #2563eb;
  --blue-soft: #dbeafe;
  --radius: 14px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 6px 18px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 24px 60px rgba(15, 23, 42, 0.22);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: 'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font-family: inherit; font-size: 14px; }
a { color: var(--primary); }
.hidden { display: none !important; }

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(1200px 600px at 10% 10%, #312e81 0%, #0f172a 55%, #020617 100%);
  padding: 24px;
}
.login-card {
  background: rgba(255, 255, 255, 0.98);
  padding: 40px;
  border-radius: 20px;
  width: 380px;
  max-width: 100%;
  box-shadow: var(--shadow-lg);
}
.login-card .logo {
  width: 46px; height: 46px; border-radius: 13px; margin-bottom: 18px;
  background: linear-gradient(135deg, #6366f1, #4f46e5);
  display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 800; font-size: 20px;
}
.login-card h1 { margin: 0 0 6px; font-size: 22px; letter-spacing: -0.3px; }
.login-card p { margin: 0 0 26px; color: var(--muted); }

/* ---------- Layout ---------- */
.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: 252px;
  background: linear-gradient(180deg, #0f172a 0%, #1e1b4b 100%);
  color: #cbd5e1;
  padding: 22px 14px;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand { display: flex; align-items: center; gap: 10px; padding: 0 10px 4px; color: #fff; font-size: 16px; font-weight: 700; letter-spacing: -0.2px; }
.brand .mark {
  width: 30px; height: 30px; border-radius: 9px; flex: 0 0 auto;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  display: flex; align-items: center; justify-content: center; font-size: 14px; font-weight: 800; color: #fff;
}
.brand span { color: #a5b4fc; }
.brand-sub { font-size: 11px; color: #64748b; padding: 4px 10px 22px 50px; }

.nav-item {
  display: flex; align-items: center; gap: 11px;
  padding: 10px 12px; border-radius: 10px; cursor: pointer;
  color: #cbd5e1; margin-bottom: 3px; user-select: none;
  font-weight: 500; transition: background 0.15s, color 0.15s;
}
.nav-item svg { width: 18px; height: 18px; flex: 0 0 auto; opacity: 0.85; }
.nav-item:hover { background: rgba(255, 255, 255, 0.07); color: #fff; }
.nav-item.active { background: linear-gradient(135deg, #4f46e5, #6366f1); color: #fff; box-shadow: 0 6px 16px rgba(79, 70, 229, 0.4); }
.nav-item.active svg { opacity: 1; }

.sidebar-footer { margin-top: auto; padding: 12px 10px 0; font-size: 12px; color: #94a3b8; border-top: 1px solid rgba(255, 255, 255, 0.08); }
.sidebar-footer #currentUser { margin-top: 8px; }

.main { flex: 1; padding: 28px 34px; overflow-x: auto; min-width: 0; }

/* ---------- Page header ---------- */
.page-head { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 22px; gap: 16px; flex-wrap: wrap; }
.page-head h2 { margin: 0; font-size: 24px; letter-spacing: -0.4px; }
.page-head .sub { color: var(--muted); font-size: 13px; margin-top: 4px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 9px 15px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 600;
  transition: all 0.15s;
  box-shadow: var(--shadow-sm);
  display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { background: #f8fafc; border-color: var(--border-strong); }
.btn.primary { background: linear-gradient(135deg, #4f46e5, #6366f1); border-color: transparent; color: #fff; box-shadow: 0 4px 12px rgba(79, 70, 229, 0.3); }
.btn.primary:hover { background: linear-gradient(135deg, #4338ca, #4f46e5); }
.btn.danger { color: var(--red); border-color: #fecaca; }
.btn.danger:hover { background: #fef2f2; }
.btn.small { padding: 5px 11px; font-size: 12px; border-radius: 8px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- Forms ---------- */
.input, .select, .textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.input:focus, .select:focus, .textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.14); }
.textarea { min-height: 120px; resize: vertical; font-family: inherit; }

.field { margin-bottom: 15px; }
.field label { display: block; margin-bottom: 6px; font-weight: 600; font-size: 13px; }
.field .hint, .hint { color: var(--muted); font-size: 12px; margin-top: 5px; }

/* ---------- Grid & cards ---------- */
.grid { display: grid; gap: 16px; }
.grid.cols-4 { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow-sm);
}

.stat-label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.6px; font-weight: 700; }
.stat-value { font-size: 30px; font-weight: 800; margin-top: 8px; letter-spacing: -0.6px; }
.stat-sub { color: var(--muted); font-size: 12px; margin-top: 5px; }

/* ---------- Chart ---------- */
.chart { display: flex; align-items: flex-end; gap: 4px; height: 150px; margin-top: 16px; }
.chart .bar { flex: 1; background: linear-gradient(180deg, #a5b4fc, #4f46e5); border-radius: 5px 5px 0 0; min-height: 3px; position: relative; transition: filter 0.15s; }
.chart .bar:hover { filter: brightness(1.1); }
.chart .bar:hover::after {
  content: attr(data-label); position: absolute; bottom: 100%; left: 50%; transform: translateX(-50%);
  background: #0f172a; color: #fff; padding: 4px 8px; border-radius: 6px; font-size: 11px; white-space: nowrap; margin-bottom: 6px;
}

/* ---------- Tables ---------- */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 12px; border-bottom: 1px solid var(--border); }
th { font-size: 11px; text-transform: uppercase; color: var(--muted); letter-spacing: 0.6px; font-weight: 700; }
tbody tr { transition: background 0.12s; }
tbody tr:hover { background: #f8fafc; }
tbody tr:last-child td { border-bottom: none; }

/* Responsive data tables: scroll horizontally instead of squishing/wrapping */
#botList, #contactsTable, #campaignList, #groupList, #healthList { overflow-x: auto; }
#botList table, #contactsTable table, #campaignList table, #healthList table { min-width: 900px; }
td.row { display: table-cell; white-space: nowrap; vertical-align: middle; }
td.row .btn { white-space: nowrap; margin-right: 5px; }
.bot-cell { white-space: nowrap; }

/* ---------- Badges & chips ---------- */
.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 11px; font-weight: 700; background: #e2e8f0; color: #334155; }
.badge.green { background: var(--green-soft); color: #166534; }
.badge.red { background: var(--red-soft); color: #991b1b; }
.badge.amber { background: var(--amber-soft); color: #92400e; }
.badge.blue { background: var(--blue-soft); color: #1e40af; }
.badge.indigo { background: var(--primary-soft); color: #3730a3; }

.tag-chip { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 11px; background: var(--primary-soft); color: #4338ca; margin: 1px 3px 1px 0; font-weight: 600; }

/* ---------- Modal ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15, 23, 42, 0.55); backdrop-filter: blur(2px); display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; overflow-y: auto; z-index: 50; }
.modal { background: var(--panel); border-radius: 18px; width: 660px; max-width: 100%; padding: 26px; box-shadow: var(--shadow-lg); }
.modal.wide { width: 900px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.modal-head h3 { margin: 0; font-size: 18px; letter-spacing: -0.2px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 22px; }

/* ---------- Toast ---------- */
.toast { position: fixed; bottom: 26px; right: 26px; background: #0f172a; color: #fff; padding: 13px 19px; border-radius: 11px; z-index: 100; box-shadow: var(--shadow-lg); font-weight: 500; animation: toast-in 0.2s ease; }
.toast.error { background: var(--red); }
.toast.success { background: var(--green); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ---------- Utilities ---------- */
.empty { text-align: center; color: var(--muted); padding: 40px 12px; }
.muted { color: var(--muted); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.mt { margin-top: 16px; }
.progress { height: 7px; background: #e2e8f0; border-radius: 999px; overflow: hidden; }
.progress > div { height: 100%; background: linear-gradient(90deg, #6366f1, #4f46e5); border-radius: 999px; }

/* ---------- Composer ---------- */
.editor-toolbar { display: flex; flex-wrap: wrap; gap: 4px; padding: 7px; background: #f8fafc; border: 1px solid var(--border); border-bottom: none; border-radius: 10px 10px 0 0; }
.editor-toolbar button { border: 1px solid transparent; background: transparent; color: var(--text); padding: 5px 10px; border-radius: 7px; cursor: pointer; font-weight: 600; line-height: 1.4; }
.editor-toolbar button:hover { background: #e2e8f0; }
.var-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.toolbar-sep { width: 1px; background: var(--border); margin: 2px 4px; }
.textarea.composer { border-radius: 0 0 10px 10px; min-height: 150px; }
.preview-label { margin: 14px 0 7px; font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.6px; }

/* ---------- Telegram preview ---------- */
.tg-preview { min-height: 40px; }
.tg-phone { background: linear-gradient(160deg, #e3ecf5, #cbd9e8); border: 1px solid var(--border); border-radius: 14px; padding: 16px; }
.tg-bubble { background: #fff; border-radius: 14px; padding: 8px; max-width: 440px; box-shadow: 0 2px 6px rgba(15, 23, 42, 0.16); }
.tg-media { display: block; width: 100%; border-radius: 9px; margin-bottom: 6px; }
.tg-doc { background: #f1f5f9; border-radius: 9px; padding: 11px; margin-bottom: 6px; }
.tg-text { padding: 4px 6px; line-height: 1.5; white-space: normal; word-break: break-word; }
.tg-text a { color: #2563eb; }
.tg-buttons { display: flex; flex-direction: column; gap: 6px; margin-top: 6px; }
.tg-btn-row { display: flex; gap: 6px; }
.tg-btn { flex: 1; text-align: center; padding: 9px 10px; border-radius: 9px; font-weight: 600; font-size: 13px; background: #f1f5f9; color: #1f2937; border: 1px solid rgba(0, 0, 0, 0.05); }
.tg-btn.primary { background: #4f8ef7; color: #fff; }
.tg-btn.success { background: #43a047; color: #fff; }
.tg-btn.danger { background: #e53935; color: #fff; }

/* ---------- Emoji & buttons builder ---------- */
.emoji-pop { display: flex; flex-wrap: wrap; gap: 2px; padding: 8px; margin-top: 6px; border: 1px solid var(--border); border-radius: 10px; background: #fff; box-shadow: var(--shadow-md); }
.emoji-pop .emoji { border: none; background: transparent; font-size: 20px; padding: 3px 5px; border-radius: 7px; cursor: pointer; line-height: 1; }
.emoji-pop .emoji:hover { background: #f1f5f9; }
.emoji-wrap { position: relative; }
.chat-emoji-pop { position: absolute; bottom: 100%; left: 0; margin-bottom: 6px; width: 330px; max-height: 250px; overflow-y: auto; z-index: 30; }
.btn-builder-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }

/* ---------- Chats ---------- */
.chat-layout { display: grid; grid-template-columns: 330px 1fr; gap: 16px; align-items: start; }
@media (max-width: 900px) { .chat-layout { grid-template-columns: 1fr; } }

.chat-list { padding: 0; max-height: calc(100vh - 150px); overflow-y: auto; }
.chat-item { display: flex; gap: 11px; align-items: center; padding: 12px 15px; border-bottom: 1px solid var(--border); cursor: pointer; transition: background 0.12s; }
.chat-item:hover { background: #f8fafc; }
.chat-item.active { background: var(--primary-soft); }
.chat-avatar { width: 38px; height: 38px; border-radius: 50%; background: linear-gradient(135deg, #6366f1, #4f46e5); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; flex: 0 0 auto; }

.chat-thread { display: flex; flex-direction: column; padding: 0; height: calc(100vh - 150px); overflow: hidden; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.chat-context { padding: 7px 18px; font-size: 12px; color: var(--muted); background: var(--primary-soft); border-bottom: 1px solid var(--border); }
.chat-messages { flex: 1; overflow-y: auto; padding: 18px; background: #f8fafc; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 76%; padding: 9px 13px; border-radius: 14px; font-size: 14px; line-height: 1.5; word-break: break-word; box-shadow: var(--shadow-sm); }
.msg.in { align-self: flex-start; background: #fff; border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.out { align-self: flex-end; background: #dcf8c6; border-bottom-right-radius: 4px; }
.msg .tg-media { max-height: 220px; object-fit: cover; }
.msg .tg-text { padding: 0; }
.msg .tg-buttons { margin-top: 8px; }
.msg .tg-btn { font-size: 12px; padding: 7px 9px; }
.msg-meta { font-size: 10px; color: #94a3b8; margin-top: 5px; }
.msg-check { font-size: 11px; letter-spacing: -3px; margin-right: 3px; color: #94a3b8; }
.msg-check.seen { color: #2563eb; }
.msg-media { font-size: 11px; color: #94a3b8; }
.chat-compose { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--border); background: #fff; }

.canned-wrap { position: relative; }
.canned-pop { position: absolute; bottom: 100%; left: 0; margin-bottom: 6px; width: 300px; max-height: 280px; overflow-y: auto; background: #fff; border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow-md); z-index: 30; }
.canned-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 12px; border-bottom: 1px solid var(--border); font-weight: 700; font-size: 12px; }
.canned-item { padding: 9px 12px; cursor: pointer; border-bottom: 1px solid var(--border); }
.canned-item:hover { background: #f8fafc; }
.canned-item:last-child { border-bottom: none; }
.chat-attach-status { padding: 8px 12px; font-size: 12px; color: var(--muted); display: flex; gap: 8px; align-items: center; border-top: 1px solid var(--border); }

.bot-cell { display: flex; align-items: center; gap: 10px; }
.bot-avatar { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; background: #e2e8f0; flex: none; }
.bot-avatar.placeholder { display: inline-flex; align-items: center; justify-content: center; font-size: 18px; }

@media (max-width: 760px) {
  .sidebar { display: none; }
  .main { padding: 20px; }
}

/* ---------- Dark theme ---------- */
[data-theme="dark"] {
  --bg: #0b1120;
  --panel: #131c31;
  --border: #22304a;
  --border-strong: #33465f;
  --text: #e6edf7;
  --muted: #93a4bd;
  --primary-soft: #1e2150;
  --green-soft: #0b3a22;
  --red-soft: #4a1416;
  --amber-soft: #43290a;
  --blue-soft: #0b2f4a;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 6px 18px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 24px 60px rgba(0, 0, 0, 0.6);
}
[data-theme="dark"] .btn:hover { background: #1b2740; }
[data-theme="dark"] .input, [data-theme="dark"] .select, [data-theme="dark"] .textarea { background: #0f1829; color: var(--text); }
[data-theme="dark"] .input::placeholder, [data-theme="dark"] .textarea::placeholder { color: #5f7191; }
[data-theme="dark"] .badge { background: #334155; color: #e2e8f0; }
[data-theme="dark"] .badge.green { color: #86efac; }
[data-theme="dark"] .badge.red { color: #fca5a5; }
[data-theme="dark"] .badge.amber { color: #fcd34d; }
[data-theme="dark"] .badge.blue { color: #93c5fd; }
[data-theme="dark"] .badge.indigo { color: #a5b4fc; }
[data-theme="dark"] .tag-chip { color: #a5b4fc; }
[data-theme="dark"] .progress { background: #1e293b; }
[data-theme="dark"] tbody tr:hover { background: #182338; }
[data-theme="dark"] .chat-item:hover { background: #182338; }
[data-theme="dark"] .chat-messages { background: #0b1120; }
[data-theme="dark"] .msg.in { background: #1a2438; }
[data-theme="dark"] .msg.out { background: #14532d; }
[data-theme="dark"] .chat-compose { background: var(--panel); }
[data-theme="dark"] .canned-pop, [data-theme="dark"] .emoji-pop { background: #131c31; }
[data-theme="dark"] .canned-item:hover, [data-theme="dark"] .emoji-pop .emoji:hover { background: #1b2740; }
[data-theme="dark"] .tg-doc, [data-theme="dark"] .tg-btn { background: #1b2740; color: #e2e8f0; }
[data-theme="dark"] .btn.danger { border-color: #7f1d1d; }
[data-theme="dark"] .btn.danger:hover { background: #2a1416; }
[data-theme="dark"] .bot-avatar { background: #22304a; }
[data-theme="dark"] .login-card { background: #131c31; }
[data-theme="dark"] .editor-toolbar { background: #0f1829; }
[data-theme="dark"] .editor-toolbar button { color: #cbd5e1; }
[data-theme="dark"] .editor-toolbar button:hover { background: #1b2740; }
