/* Lead Desk — the rep console.
   No framework and no build step: this is served straight off the backend, and
   a rep on a phone in a car should not wait for a bundle. */

:root {
  color-scheme: light dark;

  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #15181d;
  --text-dim: #646b76;
  --text-faint: #8b929c;

  --accent: #0b5cff;
  --accent-text: #ffffff;

  /* Queue priorities. Red is "they raised their hand and nobody came". */
  --p-replied: #d6174b;
  --p-sla: #d97706;
  --p-waiting: #0b5cff;
  --p-undeliverable: #7c3aed;
  --p-handled: #10893e;

  --in-bg: #f0f2f5;
  --out-bg: #0b5cff;
  --out-text: #ffffff;
  --system-bg: #fff8e1;
  --system-text: #7a5c00;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 20, 28, .06), 0 4px 12px rgba(16, 20, 28, .04);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #161a21;
    --surface-2: #1d222b;
    --border: #272d37;
    --text: #e9ecf1;
    --text-dim: #9aa3b0;
    --text-faint: #6d7684;

    --accent: #4d8bff;
    --in-bg: #232935;
    --out-bg: #2f6bdb;
    --system-bg: #2a2410;
    --system-text: #e0c879;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }

/* A rule with a class selector outranks the user-agent's [hidden] { display:
   none }, so any element below that sets `display` would stay interactive
   while marked hidden — the token overlay did exactly that, sitting invisible
   over the whole console and swallowing every click. Settle it once, here. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ----------------------------------------------------------------- gate */
.gate {
  position: fixed; inset: 0; display: grid; place-items: center;
  background: var(--bg); padding: 20px; z-index: 50;
}
.gate-card {
  width: 100%; max-width: 340px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 14px;
  padding: 28px 24px; box-shadow: var(--shadow); text-align: center;
}
.gate-card h1 { margin: 0 0 6px; font-size: 20px; }
.gate-card p { margin: 0 0 18px; color: var(--text-dim); font-size: 14px; }
.gate-card input {
  width: 100%; padding: 11px 13px; font-size: 15px; font-family: var(--font);
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface-2); color: var(--text); margin-bottom: 12px;
}
.gate-card button {
  width: 100%; padding: 11px; font-size: 15px; font-weight: 600;
  font-family: var(--font); border: 0; border-radius: var(--radius);
  background: var(--accent); color: var(--accent-text); cursor: pointer;
}
.gate-error { color: var(--p-replied); margin: 12px 0 0 !important; font-size: 13px; }

/* --------------------------------------------------------------- topbar */
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 10px 16px; background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
  padding-top: max(10px, env(safe-area-inset-top));
}
.brand { display: flex; align-items: center; gap: 8px; font-size: 15px; }
.brand-dot {
  width: 9px; height: 9px; border-radius: 50%; background: var(--p-handled);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--p-handled) 20%, transparent);
}
.views { display: flex; gap: 4px; background: var(--surface-2); padding: 3px; border-radius: 9px; }
.view-btn {
  border: 0; background: transparent; color: var(--text-dim);
  font: inherit; font-size: 13px; font-weight: 500;
  padding: 6px 12px; border-radius: 7px; cursor: pointer; white-space: nowrap;
}
.view-btn.is-active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 12px; }
#rep-filter {
  font: inherit; font-size: 13px; padding: 6px 8px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
}
.live { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-faint); }
.live-dot {
  width: 7px; height: 7px; border-radius: 50%; background: var(--p-handled);
  animation: pulse 2.4s ease-in-out infinite;
}
.live.is-stale .live-dot { background: var(--p-sla); animation: none; }
.live.is-error .live-dot { background: var(--p-replied); animation: none; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .25; } }
@media (prefers-reduced-motion: reduce) { .live-dot { animation: none; } }

/* --------------------------------------------------------------- counts */
.counts { display: flex; gap: 8px; padding: 10px 16px 0; flex-wrap: wrap; }
.count {
  display: flex; align-items: baseline; gap: 6px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 999px; padding: 4px 11px; font-size: 12px; color: var(--text-dim);
}
.count b { font-size: 14px; color: var(--text); }
.count::before {
  content: ''; width: 7px; height: 7px; border-radius: 50%;
  background: var(--c, var(--text-faint)); align-self: center;
}
.count[data-k="repliedWaiting"] { --c: var(--p-replied); }
.count[data-k="slaBreached"]    { --c: var(--p-sla); }
.count[data-k="waiting"]        { --c: var(--p-waiting); }
.count[data-k="undeliverable"]  { --c: var(--p-undeliverable); }
.count[data-k="handled"]        { --c: var(--p-handled); }

/* --------------------------------------------------------------- layout */
.layout {
  display: grid; grid-template-columns: minmax(300px, 380px) 1fr;
  gap: 14px; padding: 14px 16px 16px; align-items: start;
  height: calc(100dvh - 104px);
}
.queue, .detail {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: var(--shadow); height: 100%; overflow: hidden;
}
.queue { overflow-y: auto; }

/* ---------------------------------------------------------------- queue */
.queue-list { list-style: none; margin: 0; padding: 6px; }
.queue-item {
  display: block; width: 100%; text-align: left; font: inherit;
  background: transparent; border: 0; border-radius: var(--radius);
  padding: 10px 11px 10px 14px; cursor: pointer; position: relative;
}
.queue-item:hover { background: var(--surface-2); }
.queue-item.is-selected { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.queue-item::before {
  content: ''; position: absolute; left: 5px; top: 13px;
  width: 4px; height: calc(100% - 26px); min-height: 18px;
  border-radius: 2px; background: var(--p, var(--text-faint));
}
.queue-item[data-p="0"] { --p: var(--p-replied); }
.queue-item[data-p="1"] { --p: var(--p-sla); }
.queue-item[data-p="2"] { --p: var(--p-waiting); }
.queue-item[data-p="3"] { --p: var(--p-undeliverable); }
.queue-item[data-p="4"] { --p: var(--p-handled); }

.qi-top { display: flex; align-items: baseline; gap: 8px; }
.qi-name { font-weight: 600; font-size: 14px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.qi-wait { font-size: 12px; color: var(--text-faint); font-variant-numeric: tabular-nums; white-space: nowrap; }
.qi-sub { font-size: 12px; color: var(--text-dim); margin-top: 1px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.qi-preview {
  font-size: 12px; color: var(--text); margin-top: 4px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  padding-left: 7px; border-left: 2px solid var(--border);
}
.qi-badges { display: flex; gap: 4px; margin-top: 5px; flex-wrap: wrap; }
.badge {
  font-size: 10px; font-weight: 700; letter-spacing: .03em; text-transform: uppercase;
  padding: 2px 6px; border-radius: 4px;
  background: color-mix(in srgb, var(--bc, var(--text-faint)) 15%, transparent);
  color: var(--bc, var(--text-dim));
}
.badge.replied        { --bc: var(--p-replied); }
.badge.sla            { --bc: var(--p-sla); }
.badge.undeliverable  { --bc: var(--p-undeliverable); }
.badge.optout         { --bc: var(--p-undeliverable); }
.badge.handled        { --bc: var(--p-handled); }

.queue-empty, .detail-placeholder {
  padding: 48px 24px; text-align: center; color: var(--text-dim); font-size: 14px;
}
.queue-empty p { margin: 4px 0; }

/* --------------------------------------------------------------- detail */
.detail { display: flex; flex-direction: column; }
.detail-body { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.back-btn {
  display: none; border: 0; background: transparent; color: var(--accent);
  font: inherit; font-size: 14px; padding: 10px 14px 0; cursor: pointer; text-align: left;
}
.lead-head {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 14px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.lead-head-main { flex: 1; min-width: 180px; }
.lead-head h2 { margin: 0; font-size: 17px; }
.lead-meta { font-size: 12.5px; color: var(--text-dim); margin-top: 3px; }
.lead-meta span + span::before { content: '·'; margin: 0 6px; color: var(--text-faint); }
.lead-meta a { color: var(--accent); text-decoration: none; }
.lead-actions { display: flex; gap: 8px; }
.btn {
  font: inherit; font-size: 13px; font-weight: 600; padding: 7px 14px;
  border-radius: 8px; border: 1px solid transparent; cursor: pointer;
  text-decoration: none; display: inline-flex; align-items: center; white-space: nowrap;
}
.btn-call { background: var(--p-handled); color: #fff; }
.btn-ghost { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn-send { background: var(--accent); color: var(--accent-text); }
.btn[disabled], .btn.is-disabled { opacity: .45; cursor: not-allowed; }

.window-pill {
  margin: 10px 16px 0; padding: 7px 11px; border-radius: 8px;
  font-size: 12.5px; display: flex; align-items: center; gap: 7px;
}
.window-pill.is-open { background: color-mix(in srgb, var(--p-handled) 12%, transparent); color: var(--p-handled); }
.window-pill.is-closed { background: var(--system-bg); color: var(--system-text); }

/* --------------------------------------------------------------- thread */
.thread { flex: 1; overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 8px; min-height: 0; }
.msg { max-width: min(78%, 460px); padding: 8px 12px; border-radius: 14px; font-size: 14px; word-wrap: break-word; }
.msg-in  { align-self: flex-start; background: var(--in-bg); border-bottom-left-radius: 4px; }
.msg-out { align-self: flex-end; background: var(--out-bg); color: var(--out-text); border-bottom-right-radius: 4px; }
.msg-system {
  align-self: center; background: var(--system-bg); color: var(--system-text);
  font-size: 12px; max-width: 90%; text-align: center; border-radius: 8px;
}
.msg-foot { font-size: 10.5px; opacity: .7; margin-top: 3px; display: flex; gap: 6px; }
.msg-out .msg-foot { justify-content: flex-end; }
.thread-empty { color: var(--text-faint); font-size: 13px; text-align: center; padding: 20px; }

/* ------------------------------------------------------------- composer */
.composer { border-top: 1px solid var(--border); padding: 10px 16px; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
.composer textarea {
  width: 100%; resize: none; font: inherit; font-size: 14px; padding: 9px 11px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface-2); color: var(--text);
}
.composer textarea:disabled { opacity: .5; }
.composer-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 8px; }
.composer-hint { font-size: 12px; color: var(--text-faint); }

/* ---------------------------------------------------------------- toast */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: max(20px, env(safe-area-inset-bottom)); z-index: 40;
  background: var(--text); color: var(--bg);
  padding: 9px 16px; border-radius: 999px; font-size: 13px; font-weight: 500;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .25); max-width: calc(100vw - 32px);
}
.toast.is-error { background: var(--p-replied); color: #fff; }

/* --------------------------------------------------------------- mobile */
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; height: auto; }
  .queue, .detail { height: auto; }
  .queue { max-height: calc(100dvh - 150px); }
  .detail { display: none; }
  .back-btn { display: block; }
  /* On a phone the two panes become two screens: a rep taps a lead and gets
     the conversation full-width, which is how they will actually use this. */
  body.is-detail .queue { display: none; }
  /* A fixed height, not a min-height: the thread and composer are flex
     children and need a bounded parent to distribute into, otherwise the
     composer floats halfway up the screen with dead space beneath it. */
  body.is-detail .detail { display: flex; height: calc(100dvh - 160px); }
  .topbar { flex-wrap: wrap; gap: 10px; }
  .topbar-right { width: 100%; margin-left: 0; }
}
