:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --ink: #16181d;
  --muted: #6b7280;
  --line: #e4e6eb;
  --brand: #1877f2;
  --brand-dark: #0f5bc4;
  --ok: #12855a;
  --warn: #b4690e;
  --danger: #c0392b;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
}

header.topbar {
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: 12px 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 5;
}

.logo { font-weight: 700; font-size: 17px; }
.logo span { color: var(--brand); }

nav { display: flex; gap: 4px; flex-wrap: wrap; }

nav button {
  background: none;
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 7px 13px;
  font: inherit;
  color: var(--muted);
  cursor: pointer;
}
nav button:hover { background: var(--bg); }
nav button.active { background: #e8f1fe; color: var(--brand-dark); font-weight: 600; }

.spacer { flex: 1; }

.status-pill {
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  white-space: nowrap;
}
.status-pill.on { color: var(--ok); border-color: #bfe6d3; background: #eefaf4; }
.status-pill.off { color: var(--warn); border-color: #f0dcbb; background: #fdf6ec; }

main { padding: 20px; max-width: 1180px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }

h2 { margin: 0 0 14px; font-size: 19px; }
h3 { margin: 0 0 10px; font-size: 15px; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 20px; }
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.card .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.card .value { font-size: 26px; font-weight: 650; margin-top: 4px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 8px; border-bottom: 1px solid var(--line); font-size: 14px; }
th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
tbody tr:hover { background: #fafbfc; cursor: pointer; }

.tag {
  display: inline-block;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: #eef0f3;
  color: var(--muted);
  margin-right: 4px;
}
.tag.new { background: #e8f1fe; color: var(--brand-dark); }
.tag.qualified { background: #eefaf4; color: var(--ok); }
.tag.customer { background: #e7f6ee; color: var(--ok); }
.tag.lost { background: #fdeceb; color: var(--danger); }
.tag.contacted { background: #fdf6ec; color: var(--warn); }

input, select, textarea {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: inherit;
  width: 100%;
}
textarea { resize: vertical; min-height: 60px; }

.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.row > * { flex: 1 1 160px; }
.row > .narrow { flex: 0 0 auto; width: auto; }

button.btn {
  font: inherit;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--brand);
  background: var(--brand);
  color: #fff;
  cursor: pointer;
  white-space: nowrap;
}
button.btn:hover { background: var(--brand-dark); }
button.btn.ghost { background: #fff; color: var(--brand-dark); }
button.btn.ghost:hover { background: #eef4ff; }
button.btn:disabled { opacity: .55; cursor: not-allowed; }

.board { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
.column { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.column h3 { display: flex; justify-content: space-between; align-items: baseline; }
.column h3 small { color: var(--muted); font-weight: 500; }
.deal {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 10px;
  margin-bottom: 8px;
  background: #fff;
}
.deal .title { font-weight: 600; font-size: 14px; }
.deal .meta { color: var(--muted); font-size: 12px; margin-top: 2px; }
.deal select { margin-top: 6px; font-size: 12px; padding: 4px 6px; }

.inbox { display: grid; grid-template-columns: minmax(220px, 320px) 1fr; gap: 16px; align-items: start; }
.thread-list { max-height: 70vh; overflow-y: auto; }
.thread {
  padding: 10px;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
}
.thread:hover { background: #fafbfc; }
.thread.active { background: #eef4ff; border-color: #cfe0fb; }
.thread .name { font-weight: 600; font-size: 14px; }
.thread .preview { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.messages { max-height: 52vh; overflow-y: auto; padding-right: 6px; margin-bottom: 12px; }
.bubble {
  max-width: 78%;
  padding: 9px 12px;
  border-radius: 14px;
  margin-bottom: 8px;
  font-size: 14px;
  white-space: pre-wrap;
  word-break: break-word;
}
.bubble.inbound { background: #eef0f3; border-bottom-left-radius: 4px; }
.bubble.outbound { background: var(--brand); color: #fff; margin-left: auto; border-bottom-right-radius: 4px; }
.bubble.outbound.bot { background: #5b3fd6; }
.bubble .who { font-size: 11px; opacity: .75; display: block; margin-bottom: 2px; }

.chat-log { max-height: 56vh; overflow-y: auto; margin-bottom: 12px; }
.tool-trace {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  background: #f7f8fa;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 8px;
  color: var(--muted);
  overflow-x: auto;
}

.muted { color: var(--muted); }
.empty { color: var(--muted); padding: 18px 4px; text-align: center; font-size: 14px; }
.error { color: var(--danger); font-size: 13px; }
details summary { cursor: pointer; color: var(--muted); font-size: 13px; }

.toast {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  background: #16181d;
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
  z-index: 20;
}
.toast.show { opacity: 1; }

@media (max-width: 760px) {
  .inbox { grid-template-columns: 1fr; }
  main { padding: 14px; }
}
