:root {
  --teal: #008069;
  --teal-strong: #00a884;
  --green: #25d366;
  --bg: #f0f2f5;
  --panel: #ffffff;
  --panel-2: #f0f2f5;
  --chat-bg: #efeae2;
  --chat-dot: rgba(17, 27, 33, 0.05);
  --bubble-in: #ffffff;
  --bubble-out: #d9fdd3;
  --text: #111b21;
  --muted: #667781;
  --border: #e9edef;
  --hover: #f5f6f6;
  --active: #f0f2f5;
  --danger: #d93025;
  --warn: #9a5b00;
  --warn-bg: #fff4d6;
  --ok-bg: #e7fce3;
  --info-bg: #e3f2fd;
  --info: #0b63ad;
  --bar-ad: #00a884;
  --bar-direct: #a7b6c2;
  --shadow: 0 1px 0.5px rgba(11, 20, 26, 0.13);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --teal: #00a884;
    --teal-strong: #00a884;
    --bg: #0b141a;
    --panel: #111b21;
    --panel-2: #202c33;
    --chat-bg: #0b141a;
    --chat-dot: rgba(233, 237, 239, 0.035);
    --bubble-in: #202c33;
    --bubble-out: #005c4b;
    --text: #e9edef;
    --muted: #8696a0;
    --border: #222d34;
    --hover: #202c33;
    --active: #2a3942;
    --danger: #f15c6d;
    --warn: #ffd279;
    --warn-bg: #3d3214;
    --ok-bg: #103529;
    --info-bg: #10304a;
    --info: #8cc8ff;
    --bar-direct: #4b5c68;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 "Segoe UI", "Helvetica Neue", Helvetica, "Noto Sans Telugu", "Noto Sans", Roboto, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
a { color: var(--teal); }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3, h4 { margin: 0; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.empty { padding: 24px; text-align: center; }
.form-error { color: var(--danger); min-height: 1.4em; margin: 8px 0 0; }

button, .button {
  border: 1px solid transparent;
  border-radius: 20px;
  padding: 7px 14px;
  cursor: pointer;
  background: none;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
button:disabled { opacity: 0.5; cursor: not-allowed; }
.primary { background: var(--teal); color: #fff; }
.primary:hover:not(:disabled) { filter: brightness(1.08); }
.ghost { border-color: var(--border); background: var(--panel); color: var(--text); }
.ghost:hover:not(:disabled) { background: var(--hover); }
:focus-visible { outline: 2px solid var(--teal-strong); outline-offset: 2px; }

input[type="search"], input[type="password"], select, textarea {
  background: var(--panel-2);
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 12px;
  min-width: 0;
}
input:focus, select:focus, textarea:focus { border-color: var(--teal-strong); outline: none; }

/* ----- sign in ----- */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px;
  background: linear-gradient(var(--teal) 0 220px, var(--bg) 220px); }
.login-card { width: min(380px, 100%); background: var(--panel); border-radius: 12px; padding: 32px 28px;
  box-shadow: 0 17px 50px rgba(11, 20, 26, 0.19); display: grid; gap: 10px; }
.login-card h1 { font-size: 22px; }
.login-card label { font-weight: 600; margin-top: 8px; }
.login-card .primary { justify-content: center; padding: 10px; margin-top: 8px; }
.login-mark, .brand-mark, .empty-mark { display: grid; place-items: center; border-radius: 50%; background: var(--teal); color: #fff; font-weight: 700; }
.login-mark { width: 52px; height: 52px; font-size: 18px; }

/* ----- dialogs ----- */
.dialog { border: 0; padding: 0; border-radius: 12px; background: var(--panel); color: var(--text); width: min(420px, calc(100% - 32px));
  box-shadow: 0 17px 50px rgba(11, 20, 26, 0.3); }
.dialog::backdrop { background: rgba(11, 20, 26, 0.45); }
.dialog-card { display: grid; gap: 8px; padding: 24px; }
.dialog-card h2 { font-size: 18px; }
.dialog-card p { margin: 0; }
.dialog-card label { font-weight: 600; margin-top: 6px; }
.dialog-card .pw-suggest { justify-self: start; }
.dialog-card label.check { font-weight: 400; display: flex; align-items: center; gap: 8px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.dialog-card input[type="text"] { background: var(--panel-2); border: 1px solid transparent; border-radius: 8px;
  padding: 8px 12px; min-width: 0; }

/* ----- frame ----- */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 16px; padding: 8px 16px; background: var(--teal); color: #fff; flex-wrap: wrap; }
@media (prefers-color-scheme: dark) { .topbar { background: var(--panel-2); } }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 16px; }
.brand-mark { width: 30px; height: 30px; font-size: 12px; background: rgba(255, 255, 255, 0.2); }
.tabs { display: flex; gap: 4px; }
.tabs button { color: rgba(255, 255, 255, 0.85); border-radius: 18px; }
.tabs button.active, .tabs button:hover { background: rgba(255, 255, 255, 0.16); color: #fff; }
.count-badge { background: var(--green); color: #fff; border-radius: 10px; padding: 0 7px; font-size: 12px; font-weight: 600; }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.top-right .ghost { background: transparent; border-color: rgba(255, 255, 255, 0.4); color: #fff; }
.health { font-size: 12px; padding: 3px 10px; border-radius: 12px; background: rgba(255, 255, 255, 0.15); }
.health::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: #ccc; }
.health.ok::before { background: var(--green); }
.health.warn::before { background: #ffbc2c; }
.health.bad::before { background: #ff5c5c; }
main { flex: 1; min-height: 0; overflow: auto; }
.panel-tab { padding: 20px; max-width: 1400px; margin: 0 auto; }
body[data-tab="chats"] main { overflow: hidden; }
.panel-tab.chats-tab { padding: 0; max-width: none; height: 100%; }

.section-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.section-head h2 { font-size: 20px; }
.section-head p { margin: 4px 0 0; }
.segmented { display: inline-flex; background: var(--panel); border: 1px solid var(--border); border-radius: 20px; padding: 3px; }
.segmented button { padding: 5px 12px; }
.segmented button.active { background: var(--teal); color: #fff; }

/* ----- overview ----- */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-bottom: 14px; }
.kpi { background: var(--panel); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); border-top: 3px solid transparent; }
.kpi.accent { border-top-color: var(--teal-strong); }
.kpi.warn { border-top-color: #ffbc2c; }
.kpi-value { font-size: 28px; font-weight: 600; font-variant-numeric: tabular-nums; }
.kpi-label { font-weight: 600; }
.kpi-note { color: var(--muted); font-size: 12px; margin-top: 2px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 14px; }
.card { background: var(--panel); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); min-width: 0; }
.card.wide { grid-column: span 2; }
@media (max-width: 760px) { .card.wide { grid-column: auto; } }
.card h3 { font-size: 14px; margin-bottom: 12px; }
.explainer { margin-bottom: 14px; }
.explainer summary { cursor: pointer; font-weight: 600; }
.explainer p { margin: 10px 0 0; max-width: 90ch; }
.chart { width: 100%; height: auto; display: block; }
.chart .axis { stroke: var(--border); }
.chart .tick { fill: var(--muted); font-size: 11px; }
.chart .bar-ad { fill: var(--bar-ad); }
.chart .bar-direct { fill: var(--bar-direct); }
.chart .bar-zero { fill: var(--border); }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin-top: 6px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.swatch.ad { background: var(--bar-ad); }
.swatch.direct { background: var(--bar-direct); }
.hbars { display: grid; gap: 8px; }
.hbar { display: grid; grid-template-columns: minmax(90px, 40%) 1fr auto; align-items: center; gap: 10px; font-size: 13px; }
.hbar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar-track { background: var(--panel-2); border-radius: 4px; height: 10px; overflow: hidden; }
.hbar-fill { display: block; height: 100%; background: var(--bar-ad); border-radius: 4px; }
.hbar-value { font-variant-numeric: tabular-nums; color: var(--muted); }
.funnel { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.funnel-row { display: flex; justify-content: space-between; gap: 8px; }
.funnel-track { display: block; background: var(--panel-2); border-radius: 4px; height: 14px; overflow: hidden; margin-top: 4px; }
.funnel-fill { display: block; height: 100%; background: var(--teal-strong); }
.funnel-drop { font-size: 12px; }

/* ----- leads ----- */
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 12px; }
.toolbar input[type="search"] { flex: 1 1 240px; background: var(--panel); border-color: var(--border); }
.field { display: grid; gap: 2px; }
.field select { background: var(--panel); border-color: var(--border); }
.check { display: inline-flex; gap: 6px; align-items: center; padding-bottom: 8px; }
.table-wrap { overflow-x: auto; background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); }
table.leads { width: 100%; border-collapse: collapse; font-size: 13px; }
.leads th { text-align: left; font-weight: 600; color: var(--muted); padding: 10px; border-bottom: 1px solid var(--border); white-space: nowrap; position: sticky; top: 0; background: var(--panel); }
.leads th .sort { padding: 0; border-radius: 0; color: inherit; font-weight: 600; }
.leads td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
.leads tbody tr { cursor: pointer; }
.leads tbody tr:hover, .leads tbody tr:focus { background: var(--hover); outline: none; }
.cell-name { font-weight: 600; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.clip { max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.flags { display: flex; gap: 4px; flex-wrap: wrap; }
.chip { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 12px; background: var(--panel-2); white-space: nowrap; }
.chip.ok { background: var(--ok-bg); color: var(--teal); }
.chip.info { background: var(--info-bg); color: var(--info); }
.chip.warn { background: var(--warn-bg); color: var(--warn); }
.chip.danger { color: var(--danger); }
.chip.muted-chip { color: var(--muted); }
.chip.temp-hot { background: #ffe1e1; color: #b3261e; }
.chip.temp-warm { background: var(--warn-bg); color: var(--warn); }
.chip.temp-cold { background: var(--info-bg); color: var(--info); }

/* ----- chats ----- */
.chat-shell { display: grid; grid-template-columns: minmax(280px, 380px) 1fr; height: 100%; min-height: 0; }
.chat-list-pane { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.list-head { padding: 10px 12px; display: grid; gap: 8px; border-bottom: 1px solid var(--border); }
.filters { display: flex; gap: 6px; flex-wrap: wrap; }
.filter-chip { padding: 4px 12px; background: var(--panel-2); color: var(--muted); font-size: 13px; }
.filter-chip.active { background: var(--ok-bg); color: var(--teal); }
.chat-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.chat-item-button { width: 100%; border-radius: 0; padding: 0 12px; display: flex; align-items: center; gap: 12px; text-align: left; }
.chat-item-button:hover { background: var(--hover); }
.chat-item.active .chat-item-button { background: var(--active); }
.chat-item-main { flex: 1; min-width: 0; padding: 12px 0; border-bottom: 1px solid var(--border); display: grid; gap: 2px; }
.row1, .row2 { display: flex; justify-content: space-between; gap: 8px; align-items: center; min-width: 0; }
.row1 > :first-child, .row2 > :first-child { min-width: 0; flex: 1; }
.chat-name { font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-time { font-size: 12px; color: var(--muted); flex: none; }
.chat-time.unread { color: var(--green); font-weight: 600; }
.chat-preview { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-badges { display: flex; gap: 4px; align-items: center; flex: none; }
.mini { font-size: 10px; font-weight: 700; border-radius: 4px; padding: 0 4px; }
.mini.ad { background: var(--ok-bg); color: var(--teal); }
.mini.paused { background: var(--panel-2); color: var(--muted); }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--green); }
.avatar { flex: none; width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 600; font-size: 15px; }
.avatar.large { width: 40px; height: 40px; }
.av-0 { background: #00a884; } .av-1 { background: #3d8bd4; } .av-2 { background: #c45c9c; }
.av-3 { background: #d98a1f; } .av-4 { background: #7c62c9; } .av-5 { background: #4f8f5a; }

.thread-pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--chat-bg); }
.thread-empty { margin: auto; text-align: center; padding: 24px; max-width: 420px; }
.thread-empty h2 { font-size: 22px; font-weight: 300; margin: 16px 0 8px; }
.empty-mark { width: 72px; height: 72px; margin: 0 auto; font-size: 24px; }
.thread-head { display: flex; align-items: center; gap: 12px; padding: 8px 16px; background: var(--panel-2); border-left: 1px solid var(--border); flex-wrap: wrap; }
.thread-title { flex: 1; min-width: 140px; }
.thread-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.back { display: none; font-size: 26px; padding: 0 8px; }
.thread-banner { background: var(--warn-bg); color: var(--warn); padding: 8px 16px; font-size: 13px; }
.thread-body { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr; }
.thread-body.info-open { grid-template-columns: 1fr minmax(260px, 320px); }
.thread-body:not(.info-open) .info-pane { display: none; }
.messages {
  overflow-y: auto; padding: 16px 6%; display: flex; flex-direction: column; gap: 2px;
  background-image: radial-gradient(var(--chat-dot) 1px, transparent 1px);
  background-size: 18px 18px;
}
.day-sep { align-self: center; margin: 10px 0; }
.day-sep span { background: var(--panel); color: var(--muted); font-size: 12px; padding: 4px 12px; border-radius: 8px; box-shadow: var(--shadow); }
.msg { display: flex; margin: 2px 0; }
.msg.out { justify-content: flex-end; }
.bubble { max-width: min(560px, 78%); padding: 6px 8px 4px; border-radius: 8px; box-shadow: var(--shadow); background: var(--bubble-in); position: relative; overflow-wrap: anywhere; }
.msg.out .bubble { background: var(--bubble-out); }
.msg.in .bubble { border-top-left-radius: 0; }
.msg.out .bubble { border-top-right-radius: 0; }
.who { font-size: 12px; font-weight: 600; color: var(--teal); margin-bottom: 2px; }
.src-agent .who { color: #c45c9c; }
.src-ai .who { color: #7c62c9; }
.text { white-space: pre-wrap; }
.attachment { font-style: italic; color: var(--muted); padding: 4px 0; }
.attachment::before { content: "📎 "; font-style: normal; }
.wa-buttons { display: grid; gap: 4px; margin-top: 6px; }
.wa-buttons span { text-align: center; color: var(--info); border-top: 1px solid var(--border); padding-top: 4px; font-size: 13px; }
.meta { display: flex; justify-content: flex-end; gap: 6px; font-size: 11px; color: var(--muted); margin-top: 2px; }
.tick.st-sent { color: #53bdeb; }
.tick.st-failed { color: var(--danger); font-weight: 600; }
.st-cancelled .bubble { opacity: 0.55; }
.st-failed .bubble { outline: 1px solid var(--danger); }
.info-pane { background: var(--panel); border-left: 1px solid var(--border); overflow-y: auto; padding: 16px; display: grid; gap: 8px; align-content: start; }
.info-pane h4 { font-size: 13px; color: var(--teal); margin-top: 8px; }
.info-pane dl { margin: 0; display: grid; gap: 6px; }
.fact { display: grid; grid-template-columns: 100px 1fr; gap: 8px; font-size: 13px; }
.fact dt { color: var(--muted); }
.fact dd { margin: 0; overflow-wrap: anywhere; }
.notes { margin: 0; white-space: pre-wrap; font-size: 13px; }
.info-pane select, .info-pane textarea { width: 100%; }
.audit { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; font-size: 13px; }
.composer { background: var(--panel-2); padding: 6px 16px 10px; display: grid; gap: 4px; }
.window-note { font-size: 12px; color: var(--muted); }
.window-note.closed { color: var(--danger); }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; background: var(--panel); border-radius: 8px; padding: 10px 12px; max-height: 160px; }
.send { background: var(--teal-strong); color: #fff; padding: 10px 16px; }
.send-error { font-size: 12px; color: var(--danger); margin-top: 4px; max-width: 420px; }

/* the follow-up, after the 24-hour window */
.follow-up { display: grid; gap: 8px; }
.fu-fields { display: grid; grid-template-columns: 130px 1fr 1fr; gap: 8px; }
.fu-field { display: grid; gap: 2px; font-size: 12px; color: var(--muted); }
.fu-field select, .fu-field input { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px;
  min-width: 0; color: var(--text); }
.fu-preview { background: var(--bubble-out); border-radius: 8px; padding: 8px 10px; box-shadow: var(--shadow); white-space: pre-wrap;
  overflow-wrap: anywhere; }
.fu-preview::before { content: "Preview"; display: block; font-size: 11px; font-weight: 600; color: var(--muted); margin-bottom: 2px; }
.fu-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

/* one status per chat */
.state-label { font-size: 11px; font-weight: 600; border-radius: 10px; padding: 1px 8px; white-space: nowrap; }
.state-label.st-needs { background: var(--green); color: #fff; }
.state-label.st-team { background: var(--info-bg); color: var(--info); }
.state-label.st-bot { background: var(--active); color: var(--muted); }
.state-label.st-stopped { background: var(--active); color: var(--danger); }
.thread-title .state-label { margin-right: 4px; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #111b21; color: #fff; padding: 10px 16px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25); max-width: calc(100% - 32px); z-index: 10; }
.toast.error { background: #b3261e; }
.toast.ok { background: #00715a; }

@media (max-width: 900px) {
  .thread-body.info-open { grid-template-columns: 1fr; }
  .thread-body.info-open .messages { display: none; }
}
@media (max-width: 768px) {
  .topbar { gap: 8px; padding: 8px 12px; }
  .health { display: none; }
  .panel-tab { padding: 16px; }
  .chat-shell { grid-template-columns: 1fr; }
  .chat-shell[data-view="thread"] .chat-list-pane { display: none; }
  .chat-shell[data-view="list"] .thread-pane { display: none; }
  .back { display: inline-flex; }
  .messages { padding: 12px 10px; }
  .bubble { max-width: 88%; }
  .fu-fields { grid-template-columns: 1fr 1fr; }
  .fu-fields .fu-field:first-child { grid-column: 1 / -1; }
  .top-right #sound-toggle, .top-right #password-open { padding: 6px 10px; }
}
