:root {
  --red: #d0281e;
  --red-dark: #8f1a12;
  --red-soft: rgba(208, 40, 30, 0.14);
  --bg: #101013;
  --panel: #1a1b1e;
  --panel-2: #222327;
  --line: #2c2d31;
  --text: #f5f5f6;
  --muted: #9a9ba1;
  --ok: #3fae6a;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  min-height: 100vh;
}
a { color: #ff5c4d; text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- header / nav ---------- */
.topbar {
  background: #000;
  border-bottom: 2px solid var(--red);
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 18px;
  flex-wrap: wrap;
}
.topbar .brand { display: flex; align-items: center; gap: 10px; }
.topbar .brand img { height: 34px; width: auto; display: block; }
.topbar .brand .subtitle { color: var(--muted); font-size: 12px; letter-spacing: 0.12em; text-transform: uppercase; }
.nav { display: flex; gap: 4px; overflow-x: auto; flex: 1; -webkit-overflow-scrolling: touch; }
.nav a {
  color: var(--muted);
  padding: 8px 14px;
  border-radius: 6px;
  white-space: nowrap;
  font-weight: 600;
  font-size: 14px;
}
.nav a:hover { color: var(--text); text-decoration: none; background: var(--panel); }
.nav a.active { color: #fff; background: var(--red); }
.userchip { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.userchip .role { border: 1px solid var(--line); padding: 2px 8px; border-radius: 10px; font-size: 11px; letter-spacing: 0.08em; text-transform: uppercase; }
.userchip .role.admin { border-color: var(--red); color: #ff6a5b; }
.userchip form { margin: 0; }

/* ---------- layout ---------- */
.wrap { max-width: 1180px; margin: 0 auto; padding: 18px 16px 60px; }
.pagehead { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin: 6px 0 14px; }
.pagehead h1 { font-size: 22px; margin: 0; }
.pagehead .sub { color: var(--muted); font-size: 13px; }
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 16px;
}
.panel h2 { font-size: 15px; margin: 0 0 12px; color: #fff; }
.panel h2 .hint { color: var(--muted); font-weight: 400; font-size: 12px; margin-left: 8px; }

/* ---------- stat cards ---------- */
.stats { display: grid; grid-template-columns: repeat(6, 1fr); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px; }
.stat .num { font-size: 28px; font-weight: 700; color: #fff; line-height: 1.1; }
.stat .num.red { color: var(--red); }
.stat .label { color: var(--muted); font-size: 12px; margin-top: 4px; }

/* ---------- filter bar ---------- */
.filterbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: end; margin-bottom: 14px; }
.filterbar .f { display: flex; flex-direction: column; gap: 4px; }
.filterbar label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; }
.filterbar input, .filterbar select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
  min-width: 130px;
}
.btn {
  background: var(--red);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 9px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.btn:hover { background: var(--red-dark); }
.btn.ghost { background: transparent; border: 1px solid var(--line); color: var(--muted); }
.btn.ghost:hover { color: var(--text); }

/* ---------- tables ---------- */
.tablewrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }
table { border-collapse: collapse; width: 100%; min-width: 720px; }
th {
  text-align: left;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--panel-2);
  white-space: nowrap;
}
td { padding: 10px 12px; border-bottom: 1px solid var(--line); font-size: 14px; vertical-align: top; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: rgba(255, 255, 255, 0.02); }
td .sub { color: var(--muted); font-size: 12px; }

/* ---------- chips / badges ---------- */
.chip {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.05em;
  border: 1px solid var(--line);
  color: var(--muted);
  white-space: nowrap;
}
.chip.chat { border-color: var(--red); color: #ff6a5b; }
.chip.sms { color: #cfd0d4; }
.chip.leadpost { background: var(--red-soft); border-color: var(--red); color: #ff6a5b; }
.chip.email { color: #8fd0ff; border-color: #2b4a5e; }
.chip.status-new { color: #ffd166; border-color: #4d4425; }
.chip.status-engaged { color: #8fd0ff; border-color: #2b4a5e; }
.chip.status-appointment_set, .chip.status-confirmed, .chip.status-scheduled { color: #7ddb9a; border-color: #2c5238; }
.chip.status-converted { color: #3fae6a; border-color: #2c5238; }
.chip.status-lost, .chip.status-opted_out, .chip.status-cancelled { color: #ff6a5b; border-color: var(--red-dark); }
.chip.warn { color: #ffd166; border-color: #4d4425; }

/* ---------- chat bubbles ---------- */
.msgs { display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 72%; padding: 10px 14px; border-radius: 12px; font-size: 14px; white-space: pre-wrap; word-wrap: break-word; }
.msg .meta { color: var(--muted); font-size: 11px; margin-top: 5px; }
.msg.user { align-self: flex-start; background: var(--panel-2); border-left: 3px solid var(--red); }
.msg.agent { align-self: flex-end; background: rgba(208, 40, 30, 0.10); border-right: 3px solid var(--red); }
.msg.system { align-self: center; background: transparent; color: var(--muted); font-size: 12px; padding: 2px; }

/* ---------- timeline ---------- */
.timeline { border-left: 2px solid var(--line); margin-left: 10px; padding-left: 18px; }
.tl-day { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; margin: 18px 0 8px; }
.tl-event { position: relative; padding: 8px 0 10px; }
.tl-event::before {
  content: "";
  position: absolute;
  left: -24px;
  top: 14px;
  width: 9px; height: 9px;
  border-radius: 50%;
  background: var(--red);
  border: 2px solid var(--bg);
}
.tl-event .when { color: var(--muted); font-size: 12px; }
.tl-event .title { font-weight: 600; margin: 2px 0; }
.tl-event .detail { color: var(--muted); font-size: 13px; white-space: pre-wrap; word-wrap: break-word; }

/* ---------- login ---------- */
.loginwrap { min-height: calc(100vh - 40px); display: flex; align-items: center; justify-content: center; padding: 20px; }
.loginbox {
  background: var(--panel);
  border: 1px solid var(--line);
  border-top: 3px solid var(--red);
  border-radius: 12px;
  padding: 28px;
  width: 360px;
  max-width: 94vw;
}
.loginbox img { height: 44px; margin-bottom: 4px; }
.loginbox .tag { color: var(--muted); font-size: 12px; letter-spacing: 0.14em; text-transform: uppercase; margin-bottom: 20px; }
.loginbox label { display: block; color: var(--muted); font-size: 12px; margin: 12px 0 4px; }
.loginbox input {
  width: 100%;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 15px;
}
.loginbox .btn { width: 100%; margin-top: 18px; }
.alert { background: var(--red-soft); border: 1px solid var(--red); color: #ffb3ab; padding: 9px 12px; border-radius: 6px; font-size: 13px; margin-bottom: 10px; }
.note { color: var(--muted); font-size: 12px; margin-top: 14px; }

.kv { margin: 0; }
.kv div { display: flex; gap: 10px; padding: 3px 0; font-size: 14px; }
.kv dt { color: var(--muted); min-width: 110px; }
.kv dd { margin: 0; word-break: break-word; }

.footer { color: #55565c; font-size: 12px; text-align: center; padding: 20px 0 30px; }

@media (max-width: 900px) {
  .stats { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 560px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .topbar { gap: 10px; }
  .msg { max-width: 88%; }
}
