:root {
  --bg: #f7f7fb;
  --surface: #ffffff;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --text: #111827;
  --text-muted: #6b7280;
  --text-soft: #9ca3af;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: #eef2ff;
  --success: #16a34a;
  --success-soft: #dcfce7;
  --warning: #d97706;
  --warning-soft: #fef3c7;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --info: #2563eb;
  --info-soft: #dbeafe;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.06);
  --radius: 10px;
  --radius-sm: 6px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  height: 100%;
}

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

button, input, textarea, select { font-family: inherit; font-size: inherit; }

/* ── Top bar ───────────────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  font-size: 15px;
}
.brand-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #8b5cf6);
}
.nav {
  display: flex;
  gap: 4px;
}
.nav a {
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-weight: 500;
}
.nav a:hover {
  background: var(--bg);
  color: var(--text);
  text-decoration: none;
}
.nav a.active {
  background: var(--accent-soft);
  color: var(--accent);
}
.topbar-right { display: flex; align-items: center; gap: 10px; }

.badge {
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--bg);
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.badge.ready { background: var(--success-soft); color: var(--success); border-color: transparent; }
.badge.dry { background: var(--warning-soft); color: var(--warning); border-color: transparent; }

/* ── Main view container ───────────────────────────────────────────── */
main {
  padding: 24px;
  max-width: 1200px;
  margin: 0 auto;
}
main.full {
  max-width: 100%;
  padding: 0;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  gap: 16px;
}
.page-title {
  font-size: 22px;
  font-weight: 600;
  margin: 0;
}
.muted { color: var(--text-muted); }
.soft { color: var(--text-soft); }

/* ── Buttons ───────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-weight: 500;
  transition: background 0.1s, border-color 0.1s;
}
.btn:hover { background: var(--bg); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger {
  background: white;
  color: var(--danger);
  border-color: var(--border-strong);
}
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn-ghost {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-muted);
  padding: 6px 8px;
}
.btn-ghost:hover { background: var(--bg); color: var(--text); }
.btn-sm { padding: 5px 10px; font-size: 13px; }

/* ── Card / surface ─────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.card-pad { padding: 20px; }
.card-pad-sm { padding: 14px; }

/* ── Upload area (campaigns page) ──────────────────────────────────── */
.upload-card {
  border: 2px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 28px;
  text-align: center;
  background: var(--surface);
  margin-bottom: 24px;
  transition: border-color 0.15s, background 0.15s;
}
.upload-card.dragover {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.upload-card h3 { margin: 0 0 4px 0; font-size: 16px; }
.upload-card p { margin: 4px 0 14px 0; color: var(--text-muted); font-size: 13px; }
.upload-card input[type=file] { display: none; }

/* ── Campaign cards grid ───────────────────────────────────────────── */
.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}
.campaign-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  cursor: pointer;
  transition: border-color 0.1s, box-shadow 0.1s, transform 0.1s;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.campaign-card:hover {
  border-color: var(--accent);
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}
.campaign-card .name {
  font-weight: 600;
  font-size: 15px;
  margin: 0;
  word-break: break-word;
}
.campaign-card .meta {
  font-size: 12px;
  color: var(--text-muted);
}
.campaign-card .stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.stat { text-align: center; }
.stat .n { font-size: 16px; font-weight: 600; }
.stat .l { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.stat .n.success { color: var(--success); }
.stat .n.danger { color: var(--danger); }
.stat .n.warning { color: var(--warning); }
.stat .n.info { color: var(--info); }

.status-chip {
  display: inline-block;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
}
.status-chip.draft { background: var(--bg); color: var(--text-muted); }
.status-chip.active { background: var(--info-soft); color: var(--info); }
.status-chip.completed { background: var(--success-soft); color: var(--success); }

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}
.empty-state .big { font-size: 36px; opacity: 0.4; margin-bottom: 8px; }

/* ── Campaign detail layout ─────────────────────────────────────────── */
.detail-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}
.detail-stats {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 12px;
}
@media (max-width: 900px) {
  .detail-stats { grid-template-columns: repeat(2, 1fr); }
}
.stat-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.stat-box .n { font-size: 22px; font-weight: 600; }
.stat-box .l { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; margin-top: 2px;}

.template-card { display: flex; flex-direction: column; gap: 10px; }
.template-card label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}
.template-card textarea {
  width: 100%;
  min-height: 120px;
  padding: 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  resize: vertical;
  font-size: 14px;
  line-height: 1.5;
}
.template-hint { font-size: 12px; color: var(--text-muted); }
.template-actions { display: flex; gap: 8px; align-items: center; justify-content: flex-end; flex-wrap: wrap; }

/* ── Tables ─────────────────────────────────────────────────────────── */
.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.table-header {
  padding: 14px 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border);
}
table.profiles {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
table.profiles th {
  text-align: left;
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
table.profiles td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}
table.profiles tr:last-child td { border-bottom: none; }
table.profiles tr:hover td { background: var(--bg); }
table.profiles td .row-actions { display: flex; gap: 6px; }
.dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: middle;
}
.dot.sent { background: var(--success); }
.dot.pending { background: var(--text-soft); }
.dot.failed { background: var(--danger); }
.dot.replied { background: var(--info); }
.dot.none { background: transparent; border: 1px solid var(--border-strong); }

/* ── Inbox layout ──────────────────────────────────────────────────── */
.inbox-shell {
  display: grid;
  grid-template-columns: 320px 1fr;
  height: calc(100vh - 56px);
  background: var(--surface);
}
.inbox-side {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.inbox-filters {
  padding: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  border-bottom: 1px solid var(--border);
}
.filter-chip {
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 12px;
  cursor: pointer;
}
.filter-chip:hover { background: var(--bg); }
.filter-chip.active { background: var(--accent); color: white; border-color: var(--accent); }

.convo-list {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}
.convo-item {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.convo-item:hover { background: var(--bg); }
.convo-item.active { background: var(--accent-soft); }
.convo-item-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}
.convo-name { font-weight: 600; color: var(--text); }
.convo-name.muted { font-weight: 500; color: var(--text-muted); }
.convo-time { font-size: 11px; color: var(--text-soft); flex-shrink: 0; margin-left: 8px; }
.convo-preview {
  font-size: 13px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.unread-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  margin-right: 6px;
  vertical-align: middle;
}

.thread {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg);
}
.thread-header {
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.thread-header .name { font-weight: 600; font-size: 15px; }
.thread-header .phone { color: var(--text-muted); font-size: 13px; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
}
.message {
  max-width: 70%;
  padding: 9px 13px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.45;
  word-wrap: break-word;
}
.message .body { white-space: pre-wrap; }
.message .meta {
  margin-top: 4px;
  font-size: 11px;
  opacity: 0.7;
}
.message.outbound {
  align-self: flex-end;
  background: var(--accent);
  color: white;
  border-bottom-right-radius: 4px;
}
.message.inbound {
  align-self: flex-start;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.message.failed {
  background: var(--danger-soft);
  color: var(--danger);
}

.composer {
  padding: 12px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
.composer textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  font-size: 14px;
  min-height: 38px;
  max-height: 120px;
}

.empty-thread {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-style: italic;
}

/* ── Modal ─────────────────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 22px;
  width: 100%;
  max-width: 460px;
  box-shadow: 0 12px 32px rgba(0,0,0,0.18);
}
.modal h3 { margin: 0 0 12px 0; }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.modal label { display: block; font-size: 13px; font-weight: 600; margin-top: 12px; margin-bottom: 4px; }
.modal input[type=text], .modal textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
}
.modal textarea { min-height: 90px; resize: vertical; }

/* ── Toast ─────────────────────────────────────────────────────────── */
#toast-host {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  background: var(--text);
  color: white;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  max-width: 340px;
  box-shadow: var(--shadow-md);
  animation: toast-in 0.15s ease-out;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
.toast.warning { background: var(--warning); }
@keyframes toast-in {
  from { transform: translateY(8px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* ── Loading bar ───────────────────────────────────────────────────── */
.loading {
  text-align: center;
  padding: 40px;
  color: var(--text-muted);
}

/* ── Back link ─────────────────────────────────────────────────────── */
.back-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 12px;
}
.back-link:hover { color: var(--accent); text-decoration: none; }

.flex-row { display: flex; align-items: center; gap: 10px; }
.flex-row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }

/* Editable campaign name */
.editable-name {
  font-size: 22px;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  width: 100%;
  max-width: 500px;
  color: var(--text);
}
.editable-name:hover { border-color: var(--border); }
.editable-name:focus { border-color: var(--accent); background: var(--surface); outline: none; }
