:root {
  --bg: #eef2f7;
  --surface: #ffffff;
  --surface-2: #f5f7fa;
  --border: #dde3ea;
  --text: #1c2430;
  --muted: #65717f;
  --accent: #2aabee;
  --accent-strong: #1e8fd0;
  --accent-soft: #e4f3fc;
  --danger: #d9534f;
  --danger-soft: #fdecec;
  --ok: #2e9e5b;
  --warn: #b97a0a;
  --chat-bg: #d7e4ec;
  --bubble: #ffffff;
  --kb-btn: rgba(255, 255, 255, 0.85);
  --kb-text: #3b7bb3;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1419;
    --surface: #171d25;
    --surface-2: #1e262f;
    --border: #2a3440;
    --text: #e6ebf0;
    --muted: #8a96a3;
    --accent: #3fb1ef;
    --accent-strong: #5cbdf2;
    --accent-soft: #17344a;
    --danger: #ef6b67;
    --danger-soft: #3a1f1f;
    --ok: #4fc27a;
    --warn: #e2a93b;
    --chat-bg: #0e1621;
    --bubble: #182533;
    --kb-btn: rgba(32, 46, 62, 0.92);
    --kb-text: #6ab3f3;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}

:root[data-theme="dark"] {
  --bg: #0f1419;
  --surface: #171d25;
  --surface-2: #1e262f;
  --border: #2a3440;
  --text: #e6ebf0;
  --muted: #8a96a3;
  --accent: #3fb1ef;
  --accent-strong: #5cbdf2;
  --accent-soft: #17344a;
  --danger: #ef6b67;
  --danger-soft: #3a1f1f;
  --ok: #4fc27a;
  --warn: #e2a93b;
  --chat-bg: #0e1621;
  --bubble: #182533;
  --kb-btn: rgba(32, 46, 62, 0.92);
  --kb-text: #6ab3f3;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent-strong); text-decoration: none; }
a:hover { text-decoration: underline; }

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 0 5px;
}

h1, h2, h3 { margin: 0; font-weight: 650; }
h2 { font-size: 17px; }

/* ---------- top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-icon { font-size: 26px; }
.brand-name { font-weight: 650; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-sub { font-size: 12px; color: var(--muted); }

.topbar-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }

/* ---------- layout ---------- */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 24px;
  max-width: 1180px;
  margin: 0 auto;
  padding: 24px 16px 60px;
}

.editor { display: flex; flex-direction: column; gap: 18px; min-width: 0; }

.preview { position: sticky; top: 76px; align-self: start; }
.preview-title { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin-bottom: 10px; }

/* ---------- cards & stats ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.hint { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.55; }
.hint code { font-size: 12px; }
.empty { margin: 0; color: var(--muted); font-size: 14px; text-align: center; padding: 14px 0; }
.muted { color: var(--muted); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
}
.stat-value { font-size: 26px; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 12px; color: var(--muted); }

/* ---------- form controls ---------- */

input[type="text"], input[type="url"], input[type="password"], textarea, select {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 9px 11px;
  outline: none;
  transition: border-color 0.12s, box-shadow 0.12s;
}
input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
textarea { resize: vertical; min-height: 80px; line-height: 1.5; }
select { cursor: pointer; }

.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); }
.field input { font-size: 15px; }

.check { display: inline-flex; align-items: center; gap: 7px; font-size: 13px; color: var(--text); cursor: pointer; user-select: none; }
.check input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); cursor: pointer; }
.check.big { font-size: 15px; }

.inline-form { display: flex; gap: 8px; }
.inline-form input { flex: 1; }

.btn {
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 8px 14px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s, border-color 0.12s, opacity 0.12s;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.wide { width: 100%; }

.icon-btn {
  font: inherit;
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.icon-btn:hover { border-color: var(--accent); }
.icon-btn:disabled { opacity: 0.35; cursor: default; }
.icon-btn:disabled:hover { border-color: var(--border); }
.icon-btn.danger { color: var(--danger); }
.icon-btn.danger:hover { background: var(--danger-soft); border-color: var(--danger); }

.status { font-size: 13px; color: var(--muted); margin: 0; min-height: 1.4em; }
.status.ok { color: var(--ok); }
.status.warn { color: var(--warn); }
.status.error { color: var(--danger); }
.error { color: var(--danger); font-size: 14px; margin: 0; }

/* ---------- button editor ---------- */

.button-list { display: flex; flex-direction: column; gap: 10px; }

.b-row {
  display: grid;
  grid-template-columns: 28px minmax(140px, 1fr) 170px minmax(160px, 1.4fr) auto;
  gap: 8px;
  align-items: center;
  padding: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.b-row.disabled { opacity: 0.6; }
.b-row input, .b-row select, .b-row textarea { background: var(--surface); }
.b-index {
  width: 28px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700; color: var(--muted);
  background: var(--surface); border: 1px solid var(--border); border-radius: 50%;
}
.b-actions { display: flex; gap: 6px; grid-column: 5; grid-row: 1; }
.b-opts { grid-column: 2 / -1; display: flex; gap: 18px; flex-wrap: wrap; }
.b-value-long { grid-column: 2 / 5; min-height: 60px; }
.b-value-long[hidden], .b-value[hidden] { display: none; }

/* ---------- preview phone ---------- */

.phone {
  background: var(--chat-bg);
  border: 1px solid var(--border);
  border-radius: 22px;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.phone-top {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  font-weight: 600; font-size: 14px;
}
.phone-avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--accent-soft);
  display: inline-flex; align-items: center; justify-content: center;
}
.chat { padding: 14px 12px 10px; min-height: 260px; }
.bubble {
  background: var(--bubble);
  border-radius: 14px 14px 14px 4px;
  padding: 8px 10px 6px;
  max-width: 92%;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.08);
  font-size: 14px;
  line-height: 1.45;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.bubble-image { display: block; width: 100%; max-height: 180px; object-fit: cover; border-radius: 10px; margin-bottom: 6px; }
.bubble-image[hidden] { display: none; }
.bubble-text { white-space: normal; }
.bubble-text pre { white-space: pre-wrap; margin: 4px 0; font-size: 13px; }
.bubble-text blockquote { margin: 4px 0; padding-left: 8px; border-left: 3px solid var(--accent); }
.bubble-time { text-align: right; font-size: 11px; color: var(--muted); margin-top: 2px; }

.kb { display: flex; flex-direction: column; gap: 4px; margin-top: 6px; max-width: 92%; }
.kb-row { display: flex; gap: 4px; }
.kb-btn {
  flex: 1;
  min-width: 0;
  display: flex; align-items: center; justify-content: center; gap: 4px;
  background: var(--kb-btn);
  color: var(--kb-text);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 14px;
  font-weight: 500;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
}
.kb-btn > span:first-child { overflow: hidden; text-overflow: ellipsis; }
.kb-icon { font-size: 11px; opacity: 0.7; flex-shrink: 0; }

.composer {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.menu-btn {
  background: var(--accent); color: #fff;
  font-size: 13px; font-weight: 600;
  padding: 6px 12px; border-radius: 14px;
  white-space: nowrap; max-width: 45%; overflow: hidden; text-overflow: ellipsis;
}
.menu-btn[hidden] { display: none; }
.composer-input {
  flex: 1;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 6px 12px;
  color: var(--muted);
  font-size: 14px;
}

/* ---------- users table ---------- */

.table-wrap { overflow-x: auto; }
.users { width: 100%; border-collapse: collapse; font-size: 14px; }
.users th, .users td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.users th { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); font-weight: 600; }
.users tbody tr:last-child td { border-bottom: 0; }
code.copy { cursor: pointer; }
code.copy:hover { border-color: var(--accent); }

/* ---------- login ---------- */

.login-body { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 28px 24px;
  display: flex; flex-direction: column; gap: 12px;
  text-align: center;
}
.login-logo { font-size: 42px; }
.login-card h1 { font-size: 20px; }
.login-card input { text-align: center; font-size: 16px; }

/* ---------- responsive ---------- */

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .preview { position: static; order: -1; }
  .phone { max-width: 420px; }
}

@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; }
  .brand-sub { display: none; }
  .stats { grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
  .stat { padding: 10px 12px; }
  .stat-value { font-size: 20px; }
  .b-row { grid-template-columns: 28px 1fr; }
  .b-row .b-text { grid-column: 2; }
  .b-row .b-type, .b-row .b-value, .b-row .b-value-long, .b-row .b-actions, .b-row .b-opts { grid-column: 1 / -1; }
  .b-row .b-actions { grid-row: auto; justify-content: flex-end; }
}

/* ---------- connection card / misc ---------- */

.kv { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.kv-row { display: flex; gap: 12px; flex-wrap: wrap; }
.kv-key { color: var(--muted); min-width: 150px; }
.kv-val { word-break: break-all; }
.badge { font-size: 12px; padding: 2px 8px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--border); }
.badge.warn { color: var(--warn); border-color: var(--warn); }
.badge[hidden] { display: none; }
.problems { text-align: left; margin: 0; padding-left: 18px; color: var(--danger); font-size: 14px; }
.problems li { margin: 4px 0; }
.inline-form.wrap { flex-wrap: wrap; }
.inline-form.wrap input { min-width: 200px; }

/* ---------- broadcast & progress styles ---------- */
.progress-bar-wrap {
  width: 100%;
  height: 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 7px;
  overflow: hidden;
  margin: 12px 0 8px;
}
.progress-bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.3s ease;
}
.progress-bar.done {
  background: var(--ok);
}
.broadcast-metrics {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  font-size: 13px;
  margin-top: 8px;
}
.metric-pill {
  padding: 4px 10px;
  border-radius: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.metric-pill.sent { border-color: var(--ok); color: var(--ok); font-weight: 600; }
.metric-pill.blocked { border-color: var(--warn); color: var(--warn); }
.metric-pill.failed { border-color: var(--danger); color: var(--danger); }

/* ---------- direct message modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
}
.modal-overlay[hidden] {
  display: none;
}
.modal-card {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 22px;
  width: 100%;
  max-width: 520px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.25);
}
.modal-card h3 {
  margin-bottom: 12px;
  font-size: 18px;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 16px;
}


/* ---------- broadcast: button picker & history ---------- */
.bc-buttons { display: flex; flex-direction: column; gap: 6px; padding: 10px 12px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; }
.bc-buttons .check { justify-content: flex-start; }
.bc-buttons .bc-btn-meta { color: var(--muted); font-size: 12px; }
.bc-buttons .bc-btn-off { color: var(--warn); font-size: 12px; }
.bc-buttons .empty { margin: 0; }
.bc-history { margin-top: 16px; }
.bc-history h3 { font-size: 14px; margin: 0 0 8px; }
.bc-history-row { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-top: 1px solid var(--border); font-size: 13px; }
.bc-history-row .bc-h-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.bc-history-row .bc-h-stats { color: var(--muted); white-space: nowrap; }
.bc-extra-list { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.bc-extra-list .empty { margin: 0; }
.bc-extra-row { display: grid; grid-template-columns: 1fr 140px 1.4fr auto; gap: 8px; align-items: center; }
@media (max-width: 720px) { .bc-extra-row { grid-template-columns: 1fr auto; } .bc-extra-row .bx-type, .bc-extra-row .bx-url { grid-column: 1 / -1; } }
.bc-preview { margin-top: 14px; background: var(--chat-bg); border-radius: 10px; padding: 10px; }
.bc-preview .chat { min-height: 0; padding: 0; }
.confirm-text { white-space: pre-line; margin: 8px 0 4px; }
.js-error { position: fixed; left: 16px; right: 16px; bottom: 16px; z-index: 1000; padding: 12px 14px; border-radius: 10px; background: var(--danger); color: #fff; font-weight: 600; box-shadow: var(--shadow); }
.js-error[hidden] { display: none; }

/* ---------- photo upload field ---------- */
.image-field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); margin-top: 12px; }
.image-row { display: flex; gap: 8px; align-items: center; }
.image-row input { flex: 1; min-width: 0; font-size: 15px; }
.upload-btn { white-space: nowrap; cursor: pointer; }
.upload-btn.busy { opacity: 0.6; pointer-events: none; }
.image-thumb { display: block; max-width: 220px; max-height: 140px; border-radius: 8px; border: 1px solid var(--border); object-fit: cover; }
.image-thumb[hidden] { display: none; }
.image-status:empty { display: none; }
@media (max-width: 560px) { .image-row { flex-wrap: wrap; } .image-row input { flex-basis: 100%; } }
