* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #f3f5f7;
  color: #1f2933;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 28px;
  background: #128c7e;
  color: white;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 18px; margin: 0; font-weight: 600; }
.dot {
  width: 10px; height: 10px; background: #25d366; border-radius: 50%;
  box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.18);
}

.meta { display: flex; align-items: center; gap: 10px; font-size: 13px; flex-wrap: wrap; }
.user-info { display: flex; align-items: center; gap: 8px; }
.user-info.hidden { display: none; }
.btn-logout {
  background: rgba(255,255,255,0.18); color: white;
  border: 0; padding: 4px 10px; border-radius: 6px;
  cursor: pointer; font-size: 12px; font-weight: 600;
}
.btn-logout:hover { background: rgba(255,255,255,0.28); }
.pill { padding: 4px 10px; border-radius: 999px; background: rgba(255,255,255,0.18); font-size: 12px; font-weight: 600; }
.pill-ok { background: #25d366; }
.pill-warn { background: #f59e0b; }
.pill-err { background: #ef4444; }
.pill-info { background: rgba(255,255,255,0.18); }
.pill-stale { background: #ef4444; }
.muted { opacity: 0.85; font-size: 12px; }

/* ─── sub-nav (Leads / Users) ─── */
.subnav {
  display: flex; gap: 4px; padding: 12px 28px 0;
  border-bottom: 1px solid #e4e7eb;
}
.nav-btn {
  border: 0; background: transparent; padding: 8px 18px;
  font-size: 14px; font-weight: 600; color: #6b7280;
  cursor: pointer; border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.nav-btn:hover:not(.active) { color: #128c7e; }
.nav-btn.active {
  color: #128c7e; border-bottom-color: #128c7e;
}
.nav-btn.hidden { display: none; }

/* ─── Users view ─── */
.users-view { padding: 18px 28px 40px; }
.users-view.hidden { display: none; }
.users-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 14px;
}
.users-header h2 { margin: 0; font-size: 18px; color: #1f2937; }
.users-note {
  background: #fef3c7; padding: 8px 12px; border-radius: 6px;
  margin: 0 0 14px; font-size: 12px; color: #92400e;
  border: 1px solid #fde68a;
}
.users-note code { background: rgba(0,0,0,0.06); padding: 1px 5px; border-radius: 3px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px; }

.users-table {
  width: 100%; border-collapse: collapse; background: white;
  border-radius: 10px; overflow: hidden;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}
.users-table th, .users-table td {
  text-align: left; padding: 12px 14px; font-size: 14px;
  border-bottom: 1px solid #eef0f3;
}
.users-table th {
  background: #f0f4f8; font-weight: 600; color: #52606d;
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em;
}
.users-table .actions-col { width: 180px; text-align: right; }
.users-table .role-admin { color: #b45309; font-weight: 600; }
.users-table .role-agent { color: #128c7e; font-weight: 600; }
.user-action-btn {
  border: 0; background: transparent; padding: 4px 10px;
  font-size: 13px; cursor: pointer; border-radius: 4px;
  color: #128c7e; font-weight: 600;
}
.user-action-btn:hover { background: #f0f4f8; }
.user-action-btn.danger { color: #b91c1c; }
.user-action-btn.danger:hover { background: #fee2e2; }
.user-action-btn:disabled { color: #9ca3af; cursor: not-allowed; background: transparent; }
.users-table tr.empty td { text-align: center; color: #9aa5b1; font-style: italic; padding: 40px; }

/* User modal: agent-name field hides when role=admin */
#userModalAgentNameWrap.hidden { display: none; }
.modal-save-status.muted { color: #6b7280; }

/* Campaigns */
#campaignsView .modal textarea {
  padding: 9px 11px; font-size: 14px; border: 1px solid #cbd2d9;
  border-radius: 6px; font-family: inherit; resize: vertical; width: 100%;
}
#campaignsView .modal input[type="number"] {
  padding: 9px 11px; font-size: 14px; border: 1px solid #cbd2d9;
  border-radius: 6px; width: 100%;
}
.campaign-status-queued    { color: #6b7280; font-weight: 600; }
.campaign-status-running   { color: #128c7e; font-weight: 600; }
.campaign-status-paused    { color: #b45309; font-weight: 600; }
.campaign-status-done      { color: #047857; font-weight: 600; }
.campaign-status-cancelled { color: #991b1b; font-weight: 600; }
.job-status-pending     { color: #6b7280; }
.job-status-in_progress { color: #b45309; }
.job-status-sent        { color: #047857; font-weight: 600; }
.job-status-failed      { color: #b91c1c; font-weight: 600; }
.job-status-skipped     { color: #6b7280; font-style: italic; }

/* ─── stats cards ─── */
.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  padding: 18px 28px 0;
}
.stat-card {
  background: white;
  border-radius: 10px;
  padding: 16px 18px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  border-left: 4px solid #cbd2d9;
}
.stat-card .stat-label { font-size: 12px; color: #6b7280; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.stat-card .stat-value { font-size: 28px; font-weight: 700; color: #1f2937; margin-top: 4px; }
.stat-total    { border-left-color: #128c7e; }
.stat-hot      { border-left-color: #ef4444; }
.stat-followup { border-left-color: #f59e0b; }
.stat-converted{ border-left-color: #22c55e; }

/* ─── controls ─── */
.controls {
  display: flex; gap: 10px; padding: 18px 28px;
  background: transparent; flex-wrap: wrap; align-items: center;
}
#search {
  flex: 1; min-width: 220px; padding: 10px 14px; font-size: 14px;
  border: 1px solid #cbd2d9; border-radius: 8px; outline: none;
  background: white;
}
#search:focus { border-color: #128c7e; }
#agentFilter, #statusFilter, #dateFilter {
  padding: 10px 12px; font-size: 14px;
  border: 1px solid #cbd2d9; border-radius: 8px; background: white;
}
.btn-secondary, .btn-primary {
  padding: 10px 16px; font-size: 14px; border-radius: 8px;
  cursor: pointer; font-weight: 600;
}
.btn-secondary { border: 1px solid #cbd2d9; background: white; color: #374151; }
.btn-secondary:hover { background: #f0f4f8; }
.btn-primary { border: 0; background: #128c7e; color: white; }
.btn-primary:hover { background: #0e7468; }

main { padding: 0 28px 40px; }

/* ─── table ─── */
table {
  width: 100%; border-collapse: collapse; background: white;
  border-radius: 10px; overflow: hidden;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}
thead { background: #f0f4f8; }
th, td {
  text-align: left; padding: 10px 12px; font-size: 13px;
  border-bottom: 1px solid #eef0f3; vertical-align: top;
}
th {
  font-weight: 600; color: #52606d; font-size: 11px;
  text-transform: uppercase; letter-spacing: 0.04em;
}
tbody tr { cursor: pointer; transition: background 0.12s; }
tbody tr:hover { background: #f9fafb; }
tbody tr.new-row { animation: flash 1.4s ease-out; }
@keyframes flash { 0% { background: #d1fadf; } 100% { background: transparent; } }
tr.empty td { text-align: center; padding: 40px; color: #9aa5b1; font-style: italic; cursor: default; }
tr.empty:hover { background: transparent; }

td.message, td.remarks { max-width: 320px; word-wrap: break-word; white-space: pre-wrap; }
td.phone { font-variant-numeric: tabular-nums; color: #334e68; white-space: nowrap; }
td.agent { font-weight: 600; color: #128c7e; white-space: nowrap; }
td.time, td.date, td.followup { color: #7b8794; white-space: nowrap; }

/* ─── color coding ─── */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 600;
}
.badge-status-new        { background: #e5e7eb; color: #374151; }
.badge-status-existing   { background: #dbeafe; color: #1e40af; }
.badge-status-contacted  { background: #fef3c7; color: #92400e; }
.badge-status-followup   { background: #ffedd5; color: #9a3412; }
.badge-status-converted  { background: #dcfce7; color: #14532d; }
.badge-status-noresponse { background: #fee2e2; color: #991b1b; }

.badge-level-hot     { background: #fecaca; color: #7f1d1d; }
.badge-level-warm    { background: #fed7aa; color: #7c2d12; }
.badge-level-cold    { background: #bfdbfe; color: #1e3a8a; }
.badge-level-unknown { background: #e5e7eb; color: #374151; }

/* row tinting */
tr.row-hot       { background: rgba(239, 68, 68, 0.06); }
tr.row-warm      { background: rgba(245, 158, 11, 0.06); }
tr.row-converted { background: rgba(34, 197, 94, 0.07); }
tr.row-hot:hover       { background: rgba(239, 68, 68, 0.12); }
tr.row-warm:hover      { background: rgba(245, 158, 11, 0.12); }
tr.row-converted:hover { background: rgba(34, 197, 94, 0.13); }

/* soft-deleted rows: only visible to admin in "Show deleted" mode */
tr.row-deleted {
  background: rgba(220, 38, 38, 0.08);
  color: #991b1b;
}
tr.row-deleted:hover { background: rgba(220, 38, 38, 0.15); }
tr.row-deleted td:not(.actions-col) {
  text-decoration: line-through;
  opacity: 0.7;
}

/* leads-table Actions column (admin only) */
#leadsTable th.actions-col,
#leadsTable td.actions-col {
  width: 90px; text-align: center; white-space: nowrap;
}
.row-action-btn {
  background: transparent; border: 1px solid transparent;
  padding: 3px 8px; border-radius: 5px;
  cursor: pointer; font-size: 14px; line-height: 1;
  color: #374151;
}
.row-action-btn:hover { background: #f0f4f8; border-color: #d1d5db; }
.row-action-btn.danger { color: #b91c1c; }
.row-action-btn.danger:hover { background: #fee2e2; border-color: #fecaca; }

/* Show-deleted toggle in the filter bar */
.show-deleted-toggle {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; color: #6b7280; cursor: pointer;
  user-select: none;
}
.show-deleted-toggle input { cursor: pointer; }

/* Generic admin-only hide. Stylesheet otherwise has per-element `.hidden`
   rules; this handles the new soft-delete UI without touching them. */
.admin-only.hidden { display: none !important; }

#resultCount { padding: 10px 4px; }

/* ─── pagination bar ─── */
.pagination {
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: 12px;
  padding: 14px 4px 4px;
  font-size: 13px;
}
.pagination-controls {
  display: flex; align-items: center; gap: 4px;
}
.page-list { display: inline-flex; align-items: center; gap: 4px; }
.page-btn {
  min-width: 32px; height: 32px;
  padding: 0 10px; line-height: 1;
  border: 1px solid #cbd2d9; background: white;
  border-radius: 6px; cursor: pointer;
  font-size: 13px; font-weight: 500; color: #374151;
  font-variant-numeric: tabular-nums;
}
.page-btn:hover:not(:disabled):not(.active) { background: #f0f4f8; }
.page-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.page-btn.active {
  background: #128c7e; color: white; border-color: #128c7e;
  cursor: default;
}
.page-ellipsis {
  padding: 0 4px; color: #6b7280;
  display: inline-flex; align-items: center; height: 32px;
}
.per-page {
  display: inline-flex; align-items: center; gap: 6px;
  color: #4b5563; font-size: 13px;
}
.per-page select {
  padding: 6px 10px; border: 1px solid #cbd2d9; border-radius: 6px;
  background: white; font-size: 13px;
}
#paginationInfo { font-size: 13px; }

@media (max-width: 720px) {
  .pagination { flex-direction: column; align-items: stretch; }
  .pagination-controls { justify-content: center; flex-wrap: wrap; }
}

/* ─── modal ─── */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.45);
  display: flex; justify-content: center; align-items: center;
  z-index: 1000; animation: fade-in 0.15s;
}
.modal-backdrop.hidden { display: none; }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
.modal {
  background: white; border-radius: 12px; width: 480px;
  max-width: 92vw; max-height: 92vh; overflow: auto;
  box-shadow: 0 20px 50px rgba(0,0,0,0.3);
  display: flex; flex-direction: column;
}
.modal header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid #eef0f3;
}
.modal header h2 { margin: 0; font-size: 16px; color: #128c7e; }
.modal-close {
  border: 0; background: transparent; cursor: pointer;
  font-size: 22px; color: #6b7280; line-height: 1; padding: 2px 8px; border-radius: 4px;
}
.modal-close:hover { background: #f3f4f6; }

.modal-body { padding: 18px 20px; display: flex; flex-direction: column; gap: 14px; }
.modal-info {
  background: #f9fafb; padding: 12px 14px; border-radius: 8px;
  font-size: 13px; color: #374151; line-height: 1.55;
}
.modal-info > div { margin-bottom: 3px; }
.modal-info .modal-message { margin-top: 6px; max-height: 120px; overflow: auto; white-space: pre-wrap; }

.modal-body label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; font-weight: 600; color: #4b5563; }
.modal-body select, .modal-body textarea, .modal-body input[type="date"] {
  padding: 9px 11px; font-size: 14px; border: 1px solid #cbd2d9;
  border-radius: 6px; background: white; font-family: inherit; resize: vertical;
}
.modal-body select:focus, .modal-body textarea:focus, .modal-body input[type="date"]:focus {
  border-color: #128c7e; outline: none;
}
.modal-save-status { font-size: 12px; min-height: 16px; color: #6b7280; }
.modal-save-status.ok  { color: #047857; }
.modal-save-status.err { color: #b91c1c; }

.modal footer {
  display: flex; justify-content: flex-end; gap: 8px;
  padding: 14px 20px; border-top: 1px solid #eef0f3; background: #fafafa;
}

@media (max-width: 980px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  td.message, td.remarks { max-width: 200px; }
}

/* ─── chat panel (slides in from right) ─── */
th.numeric, td.numeric { text-align: center; font-variant-numeric: tabular-nums; }
td.msgcount { font-weight: 600; color: #128c7e; }

.chat-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.25);
  z-index: 90; animation: fade-in 0.15s;
}
.chat-backdrop.hidden { display: none; }

.chat-panel {
  position: fixed; top: 0; right: 0;
  width: 440px; max-width: 100vw; height: 100vh;
  background: #efeae2;
  box-shadow: -10px 0 30px rgba(0, 0, 0, 0.18);
  display: flex; flex-direction: column;
  transform: translateX(110%); transition: transform 0.25s ease-out;
  z-index: 100;
}
.chat-panel.open { transform: translateX(0); }

.chat-header {
  background: #128c7e; color: white;
  padding: 14px 18px;
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px;
}
.chat-header-text { min-width: 0; }
.chat-name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-phone { font-size: 12px; opacity: 0.85; font-variant-numeric: tabular-nums; }
.chat-header-actions { display: flex; gap: 4px; }
.chat-icon-btn {
  background: rgba(255, 255, 255, 0.15); color: white; border: 0;
  padding: 6px 10px; border-radius: 6px; cursor: pointer;
  font-size: 14px; line-height: 1;
}
.chat-icon-btn:hover { background: rgba(255, 255, 255, 0.28); }

.chat-body {
  flex: 1; overflow-y: auto; padding: 14px;
  background:
    linear-gradient(rgba(239, 234, 226, 0.92), rgba(239, 234, 226, 0.92)),
    repeating-radial-gradient(circle at 30% 20%, #d9d2c6 0px, #d9d2c6 1px, transparent 1px, transparent 8px);
  display: flex; flex-direction: column; gap: 6px;
}
.chat-body.loading, .chat-body.empty {
  align-items: center; justify-content: center;
  color: #6b7280; font-style: italic; font-size: 13px;
}

.chat-note {
  align-self: center;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(0, 0, 0, 0.06);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 12px;
  color: #4b5563;
  font-style: italic;
  margin-bottom: 8px;
  text-align: center;
  max-width: 92%;
}

.chat-day-separator {
  align-self: center; background: rgba(255, 255, 255, 0.85);
  padding: 2px 10px; border-radius: 8px; font-size: 11px;
  color: #4b5563; font-weight: 600; margin: 6px 0;
}

.chat-bubble {
  padding: 7px 10px 4px;
  border-radius: 8px; max-width: 84%;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.08);
  position: relative;
}
/* Incoming (from the lead) — LEFT-aligned, white bubble (WhatsApp style) */
.chat-bubble.incoming {
  align-self: flex-start;
  background: white;
  border-top-left-radius: 2px;
}
/* Outgoing (agent reply) — RIGHT-aligned, green bubble. Reserved for future
   use; we don't capture agent replies today. */
.chat-bubble.outgoing {
  align-self: flex-end;
  background: #d9fdd3;
  border-top-right-radius: 2px;
}
.chat-bubble .agent-tag {
  font-size: 11px; font-weight: 600; color: #128c7e; margin-bottom: 2px;
  display: inline-flex; align-items: center; gap: 6px;
}
.src-badge {
  display: inline-block;
  padding: 0 6px;
  font-size: 10px; font-weight: 600;
  border-radius: 999px;
  background: #f3f4f6; color: #4b5563;
  font-style: normal;
}
.src-historical { background: #fef3c7; color: #92400e; }
.src-import     { background: #dbeafe; color: #1e40af; }
.chat-bubble.outgoing .agent-tag { color: #15803d; }
.chat-bubble .msg-text {
  font-size: 14px; color: #1f2937; line-height: 1.4;
  white-space: pre-wrap; word-wrap: break-word;
}
.chat-bubble .msg-time {
  font-size: 10px; color: #7b8794; text-align: right; margin-top: 2px;
}

.chat-footer {
  background: white; border-top: 1px solid #e5e7eb;
  padding: 10px 16px; font-size: 12px; color: #6b7280; text-align: center;
}

@media (max-width: 540px) {
  .chat-panel { width: 100vw; }
}

/* ─── Login overlay ─── */
.login-overlay {
  position: fixed; inset: 0;
  background: linear-gradient(135deg, #0e7468 0%, #128c7e 50%, #25d366 100%);
  display: flex; align-items: center; justify-content: center;
  z-index: 200;
  padding: 20px;
}
.login-overlay.hidden { display: none; }
.login-card {
  background: white; padding: 30px 32px;
  border-radius: 12px; width: 100%; max-width: 360px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.25);
  display: flex; flex-direction: column; gap: 14px;
}
.login-brand {
  display: flex; align-items: center; gap: 10px;
}
.login-brand .dot {
  width: 12px; height: 12px; background: #25d366; border-radius: 50%;
  box-shadow: 0 0 0 4px rgba(37, 211, 102, 0.18);
}
.login-brand h2 {
  margin: 0; font-size: 20px; color: #128c7e;
}
.login-sub { margin: 0 0 4px; color: #6b7280; font-size: 13px; }
.login-card label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 12px; font-weight: 600; color: #4b5563;
}
.login-card input {
  padding: 10px 12px; font-size: 14px;
  border: 1px solid #d1d5db; border-radius: 6px; outline: none;
  font-family: inherit;
}
.login-card input:focus { border-color: #128c7e; }
.login-error {
  font-size: 12px; color: #b91c1c; min-height: 14px;
  font-weight: 500;
}
.login-submit {
  margin-top: 4px; padding: 11px;
  font-size: 14px; font-weight: 600;
}
