:root {
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --border: rgba(11, 11, 11, 0.1);
  --border-strong: #e1e0d9;
  --text: #0b0b0b;
  --text-secondary: #52514e;
  --muted: #898781;
  --accent: #2a78d6;
  --accent-soft: #eaf1fb;
  --accent-2: #1baf7a;
  --accent-2-soft: #e7f7f0;
  --violet: #4a3aa7;
  --violet-soft: #ece9f8;
  --orange: #eb6834;
  --good: #0ca30c;
  --warning: #eda100;
  --critical: #d03b3b;
  --critical-soft: #fbeaea;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text);
}

.hidden { display: none !important; }
.muted { color: var(--muted); }

/* Login */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px;
  width: 320px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 1px 2px rgba(11, 11, 11, 0.04);
}
.login-card h1 { margin: 0; font-size: 18px; }
.login-card input {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--text);
  font-size: 14px;
}
.login-card button {
  background: var(--accent);
  border: none;
  border-radius: 8px;
  padding: 10px;
  color: white;
  font-weight: 600;
  cursor: pointer;
}
.error { color: var(--critical); font-size: 13px; min-height: 16px; margin: 0; }

/* App shell */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 28px;
  border-bottom: 1px solid var(--border-strong);
  background: var(--surface);
}
.brand { font-weight: 600; font-size: 15px; letter-spacing: -0.01em; }
.ghost {
  background: transparent;
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: 7px;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 13px;
}
.ghost:hover { background: var(--surface-2); }

.tabs {
  display: flex;
  gap: 4px;
  padding: 10px 28px 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border-strong);
}
.tab {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 8px 14px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  font-size: 13px;
  font-weight: 500;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab:hover:not(.active) { color: var(--text-secondary); }

.tab-panel { display: none; padding: 24px 28px; max-width: 1200px; }
.tab-panel.active { display: block; }

.filters {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.filters input, .filters select {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  padding: 7px 10px;
  color: var(--text);
  font-size: 13px;
}
.filters input { flex: 1; min-width: 200px; }
.total { margin-left: auto; color: var(--muted); font-size: 13px; }

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  overflow: hidden;
}
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  text-align: left;
  padding: 9px 14px;
  color: var(--muted);
  font-weight: 500;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border-bottom: 1px solid var(--border-strong);
}
td { padding: 9px 14px; border-bottom: 1px solid var(--border-strong); color: var(--text-secondary); }
td:first-child { color: var(--text); font-weight: 500; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: none; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 5px;
  font-size: 11px;
  font-weight: 500;
  background: var(--surface-2);
  color: var(--text-secondary);
}
.badge.utm { background: var(--accent-soft); color: var(--accent); }
.badge.stage { background: var(--accent-2-soft); color: var(--accent-2); }
.badge.tag { background: transparent; border: 1px solid var(--border-strong); color: var(--text-secondary); font-size: 10px; }
.badge.tag a.tag-remove { color: var(--critical); text-decoration: none; margin-left: 4px; }
.badge.good { background: transparent; color: var(--good); }
.badge.critical { background: var(--critical-soft); color: var(--critical); }
.badge.referral { background: var(--violet-soft); color: var(--violet); }

/* Fixed-order categorical palette for UTM source badges/tiles — same hue always
   means the same source, never reassigned when the filtered set changes. */
.badge.utm-c1, .tile.utm-c1 { background: #eaf1fb; color: #2a78d6; }
.badge.utm-c2, .tile.utm-c2 { background: #e7f7f0; color: #1baf7a; }
.badge.utm-c3, .tile.utm-c3 { background: #fdf3e0; color: #b8790a; }
.badge.utm-c4, .tile.utm-c4 { background: #e3f3e3; color: #008300; }
.badge.utm-c5, .tile.utm-c5 { background: #ece9f8; color: #4a3aa7; }
.badge.utm-c6, .tile.utm-c6 { background: #fbeaea; color: #d03b3b; }
.badge.utm-c7, .tile.utm-c7 { background: #fbebf1; color: #c05683; }
.badge.utm-c8, .tile.utm-c8 { background: #fce8df; color: #c14e1f; }

.kv-box {
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 4px 12px;
  margin-bottom: 10px;
}
.kv-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 7px 0;
  border-bottom: 1px solid var(--border-strong);
  font-size: 12px;
}
.kv-row:last-child { border-bottom: none; }
.kv-key { color: var(--muted); flex-shrink: 0; }
.kv-val { color: var(--text); text-align: right; word-break: break-word; }

/* Drawer */
.drawer { position: fixed; inset: 0; z-index: 20; }
.drawer-backdrop { position: absolute; inset: 0; background: rgba(11, 11, 11, 0.3); }
.drawer-panel {
  position: absolute;
  right: 0; top: 0; bottom: 0;
  width: 420px;
  max-width: 90vw;
  background: var(--surface);
  border-left: 1px solid var(--border-strong);
  padding: 24px;
  overflow-y: auto;
}
.close-btn { position: absolute; top: 16px; right: 16px; }
.drawer-panel h2 { margin-top: 0; font-size: 17px; }
.event-item {
  border-left: 2px solid var(--border-strong);
  padding: 6px 0 6px 12px;
  margin-bottom: 4px;
  font-size: 13px;
}
.event-item .type { color: var(--accent); font-weight: 600; }
.event-item .time { color: var(--muted); font-size: 11px; }
.event-item pre {
  white-space: pre-wrap;
  word-break: break-word;
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 12px;
}
.section-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin: 22px 0 8px;
  font-weight: 600;
}

.tag-add { display: flex; gap: 6px; margin-top: 8px; }
.tag-add input {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  padding: 6px 10px;
  color: var(--text);
  font-size: 12px;
}

.send-box { display: flex; gap: 8px; margin-bottom: 10px; }
.send-box textarea {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  padding: 8px 10px;
  color: var(--text);
  font-size: 13px;
  resize: vertical;
}

button.primary {
  background: var(--accent);
  border: none;
  border-radius: 7px;
  padding: 8px 16px;
  color: white;
  font-weight: 600;
  cursor: pointer;
  align-self: flex-end;
  font-size: 13px;
}
button.primary:hover { background: #2266ba; }
button.primary:disabled { opacity: 0.5; cursor: not-allowed; }

.broadcast-box { max-width: 560px; }
.broadcast-box textarea {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  padding: 10px;
  color: var(--text);
  font-size: 13px;
  resize: vertical;
  margin-bottom: 10px;
}

.progress-bar {
  width: 100%;
  height: 6px;
  background: var(--surface-2);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 6px;
}
.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent-2);
  transition: width 0.3s ease;
}

.chat-log {
  max-height: 480px;
  min-height: 200px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  margin-bottom: 10px;
}
.chat-msg { display: flex; flex-direction: column; max-width: 85%; }
.chat-msg.msg-in { align-self: flex-start; align-items: flex-start; }
.chat-msg.msg-out { align-self: flex-end; align-items: flex-end; }
.chat-msg .bubble {
  padding: 6px 10px;
  border-radius: 10px;
  font-size: 13px;
  white-space: pre-wrap;
  word-break: break-word;
}
.chat-msg.msg-in .bubble { background: var(--surface); border: 1px solid var(--border-strong); color: var(--text); }
.chat-msg.msg-out .bubble { background: var(--accent); color: white; }
.chat-msg .time { font-size: 10px; color: var(--muted); margin-top: 2px; }

/* Dashboard */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}
.stat-tile {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 14px 16px;
}
.stat-tile .label { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.stat-tile .value { font-size: 26px; font-weight: 600; letter-spacing: -0.02em; }
.stat-tile .delta { font-size: 12px; margin-top: 4px; }
.stat-tile .delta.good { color: var(--good); }

.chart-grid {
  display: grid;
  grid-template-columns: 1.3fr 1fr;
  gap: 16px;
  align-items: start;
}
@media (max-width: 860px) {
  .chart-grid { grid-template-columns: 1fr; }
}
.chart-card {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 18px 20px;
}
.chart-card h3 { margin: 0 0 14px; font-size: 13px; font-weight: 600; color: var(--text-secondary); }

.tile-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
}
.tile {
  border-radius: 10px;
  padding: 12px 14px;
}
.tile .tile-value { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; }
.tile .tile-label { font-size: 12px; font-weight: 500; opacity: 0.85; margin-top: 2px; text-transform: capitalize; }

.daybar-chart { display: flex; align-items: flex-end; gap: 3px; height: 140px; }
.daybar { flex: 1; background: var(--accent); border-radius: 3px 3px 0 0; min-height: 2px; position: relative; }
.daybar:hover { background: var(--violet); }
.daybar-axis { display: flex; justify-content: space-between; margin-top: 6px; font-size: 10px; color: var(--muted); }

.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  margin-top: 14px;
}
.pager button {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 5px 11px;
  font-size: 12px;
  cursor: pointer;
  color: var(--text-secondary);
}
.pager button.active { background: var(--accent); color: white; border-color: var(--accent); }
.pager button:disabled { opacity: 0.4; cursor: not-allowed; }

.link-code {
  font-family: ui-monospace, SFMono-Regular, monospace;
  font-size: 12px;
  background: var(--surface-2);
  padding: 3px 7px;
  border-radius: 5px;
  cursor: pointer;
}
.link-code:hover { background: var(--accent-soft); color: var(--accent); }
