:root {
  --navy: #131b2e;
  --navy-light: #1c2740;
  --teal: #14b8a6;
  --teal-dark: #0f9488;
  --bg: #f4f6f8;
  --card: #ffffff;
  --border: #e3e7ed;
  --text: #1c2333;
  --text-muted: #6b7280;
  --danger: #e0553f;
  --warn: #d9a441;
  --ok: #2f9e5b;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* ---------- Login ---------- */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(160deg, var(--navy) 0%, #0c1220 100%);
}
.login-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 40px 36px;
  width: 320px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.35);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-logo { font-size: 19px; font-weight: 700; color: var(--navy); }
.login-sub { margin: -8px 0 4px; font-size: 13px; color: var(--text-muted); }
.login-card input {
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
}
.login-card input:focus { outline: 2px solid var(--teal); border-color: var(--teal); }
.login-card button {
  padding: 11px 12px;
  background: var(--teal);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.login-card button:hover { background: var(--teal-dark); }
.login-error { color: var(--danger); font-size: 13px; min-height: 16px; }

/* ---------- App shell ---------- */
.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--navy);
  color: #cbd3e1;
  display: flex;
  flex-direction: column;
  padding: 20px 0;
}
.sidebar-logo {
  font-size: 15px;
  font-weight: 700;
  color: white;
  padding: 0 20px 20px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  margin-bottom: 12px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 20px;
  cursor: pointer;
  font-size: 14px;
  color: #aeb7c9;
  border-left: 3px solid transparent;
}
.nav-item:hover { background: rgba(255,255,255,0.04); color: white; }
.nav-item.active {
  background: rgba(20,184,166,0.12);
  color: white;
  border-left-color: var(--teal);
}
.nav-icon { width: 18px; text-align: center; }
.sidebar-footer {
  margin-top: auto;
  padding: 14px 20px 0;
  border-top: 1px solid rgba(255,255,255,0.08);
}
.logout-btn {
  background: none;
  border: none;
  color: #aeb7c9;
  font-size: 13px;
  cursor: pointer;
  padding: 0;
}
.logout-btn:hover { color: white; }

.main {
  flex: 1;
  padding: 28px 36px;
  max-width: 1200px;
}

.page-title {
  font-size: 22px;
  font-weight: 700;
  margin: 0 0 4px;
}
.page-sub {
  color: var(--text-muted);
  font-size: 13px;
  margin: 0 0 24px;
}

.hidden { display: none !important; }

/* ---------- Stat cards ---------- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 14px;
  margin-bottom: 28px;
}
.stat-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
}
.stat-value { font-size: 26px; font-weight: 700; }
.stat-label { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.stat-card.clickable { cursor: pointer; }
.stat-card.clickable:hover { border-color: var(--danger); }

/* ---------- Card / table ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
}
.card-title {
  font-size: 15px;
  font-weight: 700;
  margin: 0 0 14px;
}

.toolbar {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  flex-wrap: wrap;
  align-items: center;
}
.toolbar input[type="text"], .toolbar select {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 13px;
}
.btn {
  padding: 8px 14px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--card);
  font-size: 13px;
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
}
.btn:hover { background: #f0f2f5; }
.btn-primary {
  background: var(--teal);
  border-color: var(--teal);
  color: white;
}
.btn-primary:hover { background: var(--teal-dark); }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  text-align: left;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
td {
  padding: 10px 10px;
  border-bottom: 1px solid var(--border);
}
tr:last-child td { border-bottom: none; }
tr.clickable { cursor: pointer; }
tr.clickable:hover { background: #f7f9fb; }

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
/* Contact-sentiment badges follow a 3-color scheme: green = interested,
   yellow = unclear, red = DNC/opted-out. Statuses unrelated to sentiment
   (new, uploaded, sending, sent) stay neutral/unchanged. call_now keeps its
   🔥 label and bold/white styling for urgency, but is now green (not red)
   since red is reserved for DNC -- urgency reads through the emoji and
   weight instead of the color. */
.badge-new { background: #eef1f6; color: #4b5566; }
.badge-interested_awaiting_booking { background: #dcf5e6; color: var(--ok); }
.badge-booked { background: #dcf5e6; color: var(--ok); }
.badge-not_interested { background: #f1f1f1; color: #7a7a7a; }
.badge-opted_out { background: #fde3de; color: var(--danger); }
.badge-unclear { background: #fff3d6; color: #8a640a; }
.badge-scheduled { background: #dcf5e6; color: var(--ok); }
.badge-canceled { background: #fde3de; color: var(--danger); }
.badge-awaiting_qualifiers { background: #fff3d6; color: #8a640a; }
.badge-autoreply_paused { background: #e6ecff; color: #3b53c9; }
.badge-needs-reply { background: #ffe1e1; color: #b3261e; }
.badge-call_now_requested { background: #dcf5e6; color: var(--ok); }
.badge-call_now { background: var(--ok); color: white; font-weight: 700; }
.badge-uploaded { background: #eef1f6; color: #4b5566; }
.badge-sending { background: #fff3d6; color: #8a640a; }
.badge-paused { background: #fde3d0; color: #a3540a; }
.badge-sent { background: #dcf5e6; color: var(--ok); }
.badge-called { background: #dcf5e6; color: var(--ok); }

/* Show-Rate Shield workflow-stage badges: outlined rather than filled, so
   they read as a sub-status next to the main contact/appointment badge
   (e.g. "Booked" + "Day-of pending") instead of competing with it. */
.badge-wf {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 600;
  background: transparent;
  border: 1px solid var(--border);
  color: #5b6472;
}
.badge-wf-day_of_pending { border-color: #c9a227; color: #8a640a; }
.badge-wf-no_show_rescued { border-color: var(--danger); color: var(--danger); }
.badge-wf-confirmed { border-color: var(--ok); color: var(--ok); }
.badge-wf-running_late { border-color: #c9a227; color: #8a640a; }
.badge-wf-rescheduled { border-color: #3b53c9; color: #3b53c9; }

/* Manually-set ground-truth outcome (see Appointment.outcome) -- filled and
   bold like the main status badges, since this is a settled fact, not an
   in-progress state. */
.badge-outcome-showed { background: #dcf5e6; color: var(--ok); font-weight: 700; }
.badge-outcome-no_show { background: #fde3de; color: var(--danger); font-weight: 700; }

.tag-color-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.tag-color-row:last-child { border-bottom: none; }
.tag-color-name { flex: 1; font-size: 13px; }
.tag-color-row input[type="color"] {
  width: 36px;
  height: 28px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  background: none;
}

.wait-ok { color: var(--ok); font-weight: 600; }
.wait-warn { color: var(--warn); font-weight: 600; }
.wait-danger { color: var(--danger); font-weight: 700; }

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 40px 0;
  font-size: 13px;
}

.form-row { margin-bottom: 14px; display: flex; flex-direction: column; gap: 6px; }
.form-row label { font-size: 13px; font-weight: 600; }
.form-row input[type="text"], .form-row textarea, .form-row input[type="file"] {
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 13px;
  font-family: inherit;
}
.form-row textarea { resize: vertical; min-height: 70px; }
.hint { font-size: 12px; color: var(--text-muted); }

.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: var(--navy);
  color: white;
  padding: 12px 18px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.25);
  opacity: 0;
  transform: translateY(8px);
  transition: all 0.2s ease;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateY(0); }

.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(15,20,30,0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
}
.modal {
  background: var(--card);
  border-radius: var(--radius);
  padding: 24px;
  width: 480px;
  max-width: 92vw;
  max-height: 80vh;
  overflow-y: auto;
}
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.modal-header h3 { margin: 0; font-size: 16px; }
.modal-close { background: none; border: none; cursor: pointer; font-size: 18px; color: var(--text-muted); }
.thread { display: flex; flex-direction: column; gap: 8px; }
.msg { padding: 8px 12px; border-radius: 10px; font-size: 13px; max-width: 85%; }
.msg-inbound { background: #eef1f6; align-self: flex-start; }
.msg-outbound { background: var(--teal); color: white; align-self: flex-end; }
.msg-call { background: rgba(217, 164, 65, 0.14); border: 1px solid rgba(217, 164, 65, 0.35); color: #8a6113; align-self: center; text-align: center; max-width: 100%; font-style: italic; }
.msg-meta { font-size: 10px; opacity: 0.7; margin-top: 3px; }

/* ---------- Chat ---------- */
.chat-shell {
  display: flex;
  height: calc(100vh - 190px);
  min-height: 440px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.chat-list-pane {
  width: 280px;
  flex-shrink: 0;
  min-height: 0;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}
.chat-list-pane input {
  margin: 12px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 13px;
}
.chat-list { overflow-y: auto; flex: 1; }
#chatListWrap { flex: 1; min-height: 0; overflow-y: auto; }
.chat-list-item {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  border-left: 3px solid transparent;
  cursor: pointer;
}
.chat-list-item:hover { background: #f7f9fb; }
.chat-list-item.active { background: rgba(20,184,166,0.08); border-left-color: var(--teal); }
.chat-list-name { font-weight: 600; font-size: 13px; }
.chat-list-snippet {
  font-size: 12px; color: var(--text-muted); margin-top: 3px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-list-meta { font-size: 11px; color: var(--text-muted); margin-top: 5px; display: flex; align-items: center; gap: 6px; }
.chat-thread-pane { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-thread-header {
  padding: 14px 20px; border-bottom: 1px solid var(--border);
  font-weight: 700; font-size: 14px; color: var(--text);
}
.chat-thread-sub { font-weight: 400; font-size: 12px; color: var(--text-muted); margin-top: 3px; }
.chat-thread-body { flex: 1; overflow-y: auto; padding: 16px 20px; display: flex; flex-direction: column; gap: 8px; }
.chat-send-form { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); }
.chat-send-form textarea {
  flex: 1; resize: none; padding: 9px 10px; border: 1px solid var(--border);
  border-radius: 7px; font-size: 13px; font-family: inherit; min-height: 40px; max-height: 100px;
}
.chat-empty { padding: 40px 20px; text-align: center; color: var(--text-muted); font-size: 13px; }

/* ---------- Appointments calendar ---------- */
.cal-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}
.cal-month-label { font-weight: 700; font-size: 15px; min-width: 160px; text-align: center; }
.cal-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 0 0 8px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 6px;
}
.cal-weekdays > div { text-align: center; }
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-auto-rows: minmax(92px, auto);
  gap: 4px;
}
.cal-cell {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px;
  overflow: hidden;
  background: var(--card);
}
.cal-cell-outside { background: #fafbfc; }
.cal-cell-outside .cal-cell-num { color: var(--text-muted); }
.cal-cell-today { border-color: var(--teal); box-shadow: inset 0 0 0 1px var(--teal); }
.cal-cell-num { font-size: 12px; font-weight: 700; margin-bottom: 4px; }
.cal-cell-appts { display: flex; flex-direction: column; gap: 3px; }
.cal-chip {
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 5px;
  background: #eef1f6;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cal-chip:hover { filter: brightness(0.96); }
.cal-chip-scheduled { background: #dcf5e6; color: var(--ok); }
.cal-chip-canceled { background: #fde3de; color: var(--danger); text-decoration: line-through; }
.cal-chip-more {
  font-size: 11px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px 6px;
}
.cal-chip-more:hover { color: var(--text); }
.day-modal-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.day-modal-row:last-child { border-bottom: none; }
.day-modal-row:hover { background: #f7f9fb; }
