:root {
  --bg: #f5f7fb; --panel: #ffffff; --ink: #101828; --muted: #667085; --line: #e4e7ec;
  --brand: #155eef; --brand-ink: #ffffff; --brand-soft: #eff4ff;
  --ok: #067647; --ok-soft: #ecfdf3; --warn: #b54708; --warn-soft: #fffaeb; --bad: #b42318; --bad-soft: #fef3f2;
  --hot: #c4320a; --hot-soft: #fff4ed; --side: #0b1736; --side-ink: #c7d2fe;
  --radius: 10px; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  --bubble-out: #155eef; --bubble-in: #f2f4f7;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c111d; --panel: #161b26; --ink: #f5f5f6; --muted: #94969c; --line: #2a303c;
    --brand: #528bff; --brand-soft: #1a2540; --ok: #47cd89; --ok-soft: #0f2a1d; --warn: #fdb022; --warn-soft: #2d2410;
    --bad: #f97066; --bad-soft: #2d1414; --hot: #fd853a; --hot-soft: #2d1a10; --side: #070b14; --side-ink: #a4b4e8;
    --bubble-in: #1f2532;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; background: var(--bg); color: var(--ink); }
.hidden { display: none !important; }
a { color: var(--brand); }
code { background: var(--brand-soft); padding: 1px 5px; border-radius: 4px; font-size: 12px; }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 16px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.err { color: var(--bad); min-height: 18px; }

.layout { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar { background: var(--side); color: var(--side-ink); display: flex; flex-direction: column; padding: 16px 12px; position: sticky; top: 0; height: 100vh; }
.logo { display: flex; align-items: center; gap: 10px; font-weight: 700; color: #fff; font-size: 16px; padding: 4px 8px 18px; }
.brand-mark { width: 30px; height: 30px; border-radius: 8px; background: var(--brand); color: #fff; display: grid; place-items: center; font-weight: 800; font-size: 13px; }
.brand-mark.big { width: 48px; height: 48px; font-size: 18px; margin: 0 auto 12px; }
#nav a { display: flex; align-items: center; justify-content: space-between; color: var(--side-ink); text-decoration: none; padding: 9px 12px; border-radius: 8px; margin-bottom: 2px; }
#nav a:hover { background: rgba(255,255,255,.06); color: #fff; }
#nav a.active { background: rgba(82,139,255,.18); color: #fff; font-weight: 600; }
.pill { background: #f04438; color: #fff; border-radius: 999px; padding: 0 7px; font-size: 11px; font-weight: 700; }
.sidebar-foot { margin-top: auto; display: grid; gap: 8px; }
.mode-badge { font-size: 12px; padding: 8px 10px; border-radius: 8px; text-align: center; font-weight: 600; }
.mode-badge.dry { background: #fffaeb; color: #b54708; }
.mode-badge.live { background: #ecfdf3; color: #067647; }
.mode-badge.paused { background: #fef3f2; color: #b42318; }

main { padding: 24px 28px 60px; max-width: 1400px; width: 100%; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 16px; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-size: 26px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.stat .sub { color: var(--muted); font-size: 12px; }

.btn { border: 1px solid var(--line); background: var(--panel); color: var(--ink); border-radius: 8px; padding: 8px 14px; font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap; }
.btn:hover { filter: brightness(.97); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.danger { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.btn.good { background: var(--ok); color: #fff; border-color: var(--ok); }
.btn.ghost { background: transparent; color: inherit; border-color: rgba(255,255,255,.15); }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }

label.f { display: block; font-weight: 600; font-size: 12px; margin: 12px 0 5px; }
label.f .hint { font-weight: 400; color: var(--muted); }
input[type=text], input[type=number], input[type=password], input[type=date], input[type=time], input[type=search], input:not([type]), select, textarea {
  width: 100%; border: 1px solid var(--line); background: var(--panel); color: var(--ink); border-radius: 8px; padding: 8px 10px; font: inherit;
}
textarea { min-height: 96px; resize: vertical; font-family: inherit; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; }
.check { display: flex; align-items: center; gap: 8px; padding: 4px 0; cursor: pointer; }
.check input { width: 16px; height: 16px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--line); border-radius: 999px; padding: 4px 10px; font-size: 12px; cursor: pointer; user-select: none; background: var(--panel); }
.chip.on { background: var(--brand-soft); border-color: var(--brand); color: var(--brand); font-weight: 600; }
.chip.off { background: var(--bad-soft); border-color: transparent; color: var(--bad); text-decoration: line-through; }
.chip .n { color: var(--muted); font-weight: 400; margin-left: 4px; }
.typecards { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 10px; }
.typecard { border: 2px solid var(--line); border-radius: 10px; padding: 12px; cursor: pointer; background: var(--panel); }
.typecard.on { border-color: var(--brand); background: var(--brand-soft); }
.typecard b { display: block; }
.typecard .n { font-size: 20px; font-weight: 700; margin-top: 4px; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--brand-soft); }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.badge { display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--brand-soft); color: var(--brand); text-transform: uppercase; letter-spacing: .02em; }
.badge.active, .badge.DELIVERED, .badge.live { background: var(--ok-soft); color: var(--ok); }
.badge.paused, .badge.dry_run, .badge.draft { background: var(--warn-soft); color: var(--warn); }
.badge.completed { background: var(--line); color: var(--muted); }
.badge.FAILED, .badge.blocked_opt_out, .badge.opt { background: var(--bad-soft); color: var(--bad); }
.badge.hot { background: var(--hot-soft); color: var(--hot); }
.t-funded { background: var(--ok-soft); color: var(--ok); }
.t-quoted { background: #f4f3ff; color: #5925dc; }
.t-declined { background: var(--bad-soft); color: var(--bad); }
.t-incomplete { background: var(--warn-soft); color: var(--warn); }

.editor-grid { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.fields { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }
.fields button { border: 1px dashed var(--line); background: transparent; color: var(--brand); border-radius: 6px; font-size: 12px; padding: 3px 7px; cursor: pointer; font-family: ui-monospace, Consolas, monospace; }
.fields button:hover { background: var(--brand-soft); }
.counter { font-size: 12px; color: var(--muted); margin-top: 4px; }
.counter.warn { color: var(--warn); }
.phone { background: var(--bg); border-radius: 18px; padding: 14px; border: 1px solid var(--line); }
.bubble { max-width: 85%; padding: 9px 12px; border-radius: 16px; margin: 6px 0; white-space: pre-wrap; word-wrap: break-word; }
.bubble.out { background: var(--bubble-out); color: #fff; margin-left: auto; border-bottom-right-radius: 4px; }
.bubble.in { background: var(--bubble-in); border-bottom-left-radius: 4px; }
.bubble-meta { font-size: 11px; color: var(--muted); margin: -2px 4px 8px; }
.bubble-meta.out { text-align: right; }

.inbox { display: grid; grid-template-columns: 340px minmax(0, 1fr) 280px; gap: 0; height: calc(100vh - 140px); background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.inbox-list { border-right: 1px solid var(--line); overflow-y: auto; }
.inbox-item { padding: 12px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.inbox-item:hover { background: var(--bg); }
.inbox-item.sel { background: var(--brand-soft); }
.inbox-item .top { display: flex; justify-content: space-between; gap: 8px; }
.inbox-item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inbox-item .prev { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inbox-item.unread .name::before { content: ""; display: inline-block; width: 8px; height: 8px; background: var(--brand); border-radius: 50%; margin-right: 6px; }
.thread { display: flex; flex-direction: column; min-width: 0; }
.thread-head { padding: 12px 16px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.thread-body { flex: 1; overflow-y: auto; padding: 16px; }
.thread-foot { border-top: 1px solid var(--line); padding: 12px; }
.thread-foot textarea { min-height: 64px; }
.side-info { border-left: 1px solid var(--line); padding: 14px; overflow-y: auto; font-size: 13px; }
.kv { display: grid; grid-template-columns: 110px 1fr; gap: 4px 8px; }
.kv div:nth-child(odd) { color: var(--muted); }
.draft { background: var(--warn-soft); border: 1px solid var(--warn); border-radius: 8px; padding: 10px; margin-bottom: 8px; }
.tabs { display: flex; gap: 4px; padding: 8px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.tabs button { border: 0; background: transparent; padding: 6px 10px; border-radius: 6px; cursor: pointer; color: var(--muted); font: inherit; font-weight: 600; font-size: 12px; }
.tabs button.on { background: var(--brand-soft); color: var(--brand); }

.banner { border-radius: var(--radius); padding: 12px 16px; margin-bottom: 16px; border: 1px solid; }
.banner.warn { background: var(--warn-soft); border-color: #fedf89; color: var(--warn); }
.banner.info { background: var(--brand-soft); border-color: transparent; color: var(--ink); }
.banner.bad { background: var(--bad-soft); border-color: #fecdca; color: var(--bad); }
.banner.ok { background: var(--ok-soft); border-color: transparent; color: var(--ok); }

.bars { display: flex; align-items: flex-end; gap: 6px; height: 140px; padding-top: 10px; }
.bars .col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: 2px; height: 100%; }
.bars .bar { width: 100%; max-width: 28px; border-radius: 4px 4px 0 0; background: var(--brand); min-height: 1px; }
.bars .bar.r { background: var(--ok); }
.bars .bar.d { background: #fdb022; }
.bars .lbl { font-size: 10px; color: var(--muted); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }
.progress { height: 8px; background: var(--line); border-radius: 999px; overflow: hidden; }
.progress > div { height: 100%; background: var(--brand); }

.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; text-align: center; box-shadow: var(--shadow); display: grid; gap: 10px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #101828; color: #fff; padding: 10px 18px; border-radius: 8px; z-index: 50; max-width: 90vw; box-shadow: 0 8px 24px rgba(0,0,0,.2); }
.toast.bad { background: #b42318; }
.modal { position: fixed; inset: 0; background: rgba(16,24,40,.5); display: grid; place-items: center; z-index: 40; padding: 16px; }
.modal-card { background: var(--panel); border-radius: 14px; padding: 22px; width: 100%; max-width: 560px; max-height: 90vh; overflow-y: auto; }
.empty { text-align: center; color: var(--muted); padding: 40px 10px; }
pre.json { background: var(--bg); padding: 10px; border-radius: 8px; overflow: auto; max-height: 300px; font-size: 12px; }

@media (max-width: 1100px) {
  .inbox { grid-template-columns: 300px minmax(0, 1fr); }
  .side-info { display: none; }
  .editor-grid { grid-template-columns: 1fr; }
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 760px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  main { min-width: 0; }
  .sidebar { position: static; height: auto; }
  #nav { display: flex; flex-wrap: wrap; gap: 4px; }
  main { padding: 16px; }
  .g2, .g3, .g4, .typecards { grid-template-columns: 1fr; }
  .inbox { grid-template-columns: 1fr; height: auto; }
}

.bars .bar.k { background: #7a5af8; }
.badge.clicked { background: #f4f3ff; color: #5925dc; }
@media (prefers-color-scheme: dark) { .badge.clicked { background: #1f1a3d; color: #bdb4fe; } }

@media (max-width: 760px) { .typecards.statuscards { grid-template-columns: repeat(2, minmax(0,1fr)); } }
