:root {
  --bg: #f0f2f5;
  --panel: #ffffff;
  --panel-2: #f7f8fa;
  --line: #e2e5e9;
  --text: #111b21;
  --muted: #667781;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #ccfbf1;
  --danger: #b42318;
  --danger-soft: #fee4e2;
  --warn: #b54708;
  --warn-soft: #fef0c7;
  --ok: #067647;
  --ok-soft: #dcfae6;
  --bubble-in: #ffffff;
  --bubble-raj: #d9fdd3;
  --bubble-team: #dbeafe;
  --chat-bg: #efeae2;
  --shadow: 0 1px 2px rgba(11, 20, 26, .13);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b141a; --panel: #111b21; --panel-2: #182229; --line: #2a3942; --text: #e9edef; --muted: #8696a0;
    --accent: #14b8a6; --accent-ink: #04211e; --accent-soft: #134e4a; --danger: #f97066; --danger-soft: #3d1715;
    --warn: #fdb022; --warn-soft: #3d2a0a; --ok: #47cd89; --ok-soft: #0e2e1f;
    --bubble-in: #202c33; --bubble-raj: #005c4b; --bubble-team: #1e3a5f; --chat-bg: #0b141a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b141a; --panel: #111b21; --panel-2: #182229; --line: #2a3942; --text: #e9edef; --muted: #8696a0;
  --accent: #14b8a6; --accent-ink: #04211e; --accent-soft: #134e4a; --danger: #f97066; --danger-soft: #3d1715;
  --warn: #fdb022; --warn-soft: #3d2a0a; --ok: #47cd89; --ok-soft: #0e2e1f;
  --bubble-in: #202c33; --bubble-raj: #005c4b; --bubble-team: #1e3a5f; --chat-bg: #0b141a;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
.loading { padding: 40px; text-align: center; color: var(--muted); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }

/* buttons and inputs */
.btn {
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 12px;
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; min-height: 36px;
}
.btn:hover { background: var(--panel-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.go { background: var(--ok); color: #fff; border-color: var(--ok); font-weight: 600; }
.btn.stop { background: var(--danger); color: #fff; border-color: var(--danger); font-weight: 600; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.ghost { border-color: transparent; background: transparent; }
input[type=text], input[type=password], input[type=number], select, textarea {
  width: 100%; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 9px 11px;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }

/* login */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login {
  width: 100%; max-width: 380px; background: var(--panel); border-radius: 14px; padding: 28px 24px;
  box-shadow: var(--shadow); border: 1px solid var(--line);
}
.login h1 { font-size: 21px; margin: 10px 0 2px; }
.login .brand { display: flex; align-items: center; gap: 10px; }
.login .brand img { width: 40px; height: 40px; }
.login .btn { width: 100%; justify-content: center; margin-top: 18px; padding: 10px; }
.error { color: var(--danger); font-size: 13.5px; margin-top: 10px; min-height: 1em; }

/* shell */
.shell { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 8px 14px; background: var(--panel);
  border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.topbar .title { display: flex; align-items: center; gap: 8px; font-weight: 700; margin-right: 6px; }
.topbar .title img { width: 28px; height: 28px; }
.tabs { display: flex; gap: 2px; }
.tab { border: 0; background: transparent; padding: 8px 12px; border-radius: 8px; color: var(--muted); font-weight: 600; }
.tab.active { background: var(--accent-soft); color: var(--text); }
.spacer { flex: 1; }
.status { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px;
  font-size: 12.5px; font-weight: 600; background: var(--panel-2); border: 1px solid var(--line); white-space: nowrap;
}
.pill.on { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.pill.off { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.pill.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; display: inline-block; }
.banner { padding: 8px 14px; background: var(--warn-soft); color: var(--warn); font-weight: 600; font-size: 14px; }
.banner.bad { background: var(--danger-soft); color: var(--danger); }
.main { flex: 1; min-height: 0; display: flex; }

/* chats layout */
.chats { flex: 1; display: grid; grid-template-columns: minmax(280px, 380px) 1fr; min-height: 0; }
.list { border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.list-head { padding: 10px; border-bottom: 1px solid var(--line); }
.filters { display: flex; gap: 6px; overflow-x: auto; margin-top: 8px; padding-bottom: 2px; scrollbar-width: none; }
.filters::-webkit-scrollbar { display: none; }
.chip {
  border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 4px 11px;
  font-size: 13px; white-space: nowrap;
}
.chip.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.items { overflow-y: auto; flex: 1; }
.item {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 10px 12px; border-bottom: 1px solid var(--line);
  cursor: pointer; width: 100%; text-align: left; background: transparent; border-left: 0; border-right: 0; border-top: 0;
}
.item:hover { background: var(--panel-2); }
.item.active { background: var(--accent-soft); }
.item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .when { font-size: 12px; color: var(--muted); }
.item .preview { color: var(--muted); font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .tags { display: flex; gap: 4px; justify-content: flex-end; align-items: center; }
.badge { background: var(--accent); color: var(--accent-ink); border-radius: 999px; font-size: 11.5px; font-weight: 700; padding: 1px 7px; }
.tag { font-size: 11px; font-weight: 700; padding: 1px 6px; border-radius: 5px; background: var(--panel-2); color: var(--muted); }
.tag.human { background: var(--danger-soft); color: var(--danger); }
.tag.manual { background: var(--warn-soft); color: var(--warn); }
.tag.ai { background: var(--ok-soft); color: var(--ok); }
.empty { padding: 30px 16px; text-align: center; color: var(--muted); }

/* conversation */
.convo { display: flex; flex-direction: column; min-height: 0; background: var(--chat-bg); }
.convo-head {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--panel);
  border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.convo-head .who { flex: 1; min-width: 150px; }
.convo-head .who .n { font-weight: 700; }
.convo-head .actions { display: flex; gap: 6px; flex-wrap: wrap; }
.back { display: none; }
.card {
  margin: 10px 12px 0; background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--danger);
  border-radius: var(--radius); padding: 10px 12px; box-shadow: var(--shadow); font-size: 14px;
}
.card.answered { border-left-color: var(--warn); }
.card summary { cursor: pointer; font-size: 14.5px; }
.card[open] summary { margin-bottom: 4px; }
.card { max-height: 45vh; overflow-y: auto; }
.card ul { margin: 6px 0; padding-left: 18px; }
.card .q { margin-top: 6px; }
.card .sugg { margin-top: 6px; background: var(--panel-2); border-radius: 8px; padding: 8px; white-space: pre-wrap; }
.messages { flex: 1; overflow-y: auto; padding: 12px 4% 16px; display: flex; flex-direction: column; gap: 4px; }
.day { align-self: center; background: var(--panel); color: var(--muted); font-size: 12px; padding: 3px 10px; border-radius: 8px; margin: 8px 0; box-shadow: var(--shadow); }
.msg { max-width: min(78%, 620px); padding: 6px 9px 4px; border-radius: 9px; box-shadow: var(--shadow); position: relative; }
.msg .text { white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg .meta { display: flex; gap: 6px; justify-content: flex-end; align-items: center; font-size: 11px; color: var(--muted); margin-top: 2px; flex-wrap: wrap; }
.msg .label { font-size: 12px; font-weight: 700; margin-bottom: 1px; }
.msg.in { align-self: flex-start; background: var(--bubble-in); }
.msg.raj { align-self: flex-end; background: var(--bubble-raj); }
.msg.raj .label { color: var(--accent); }
.msg.team { align-self: flex-end; background: var(--bubble-team); }
.msg.notice { opacity: .85; font-style: italic; }
.msg.suppressed { opacity: .55; }
.msg.suppressed .text { text-decoration: line-through; }
.sys { align-self: center; max-width: 88%; background: var(--warn-soft); color: var(--text); font-size: 12.5px; padding: 5px 10px; border-radius: 8px; white-space: pre-wrap; }
.att { margin: 2px 0 4px; }
.att img { max-width: 260px; max-height: 260px; border-radius: 6px; display: block; cursor: zoom-in; }
.att a { font-weight: 600; }
.fb { display: inline-flex; gap: 2px; }
.fb button { border: 0; background: transparent; padding: 0 3px; font-size: 13px; opacity: .55; }
.fb button:hover { opacity: 1; }
.fbline { font-size: 12px; margin-top: 3px; padding-top: 3px; border-top: 1px dashed var(--line); color: var(--muted); }
.en { font-size: 12.5px; color: var(--muted); border-left: 2px solid var(--line); padding-left: 6px; margin-top: 4px; white-space: pre-wrap; }
.composer { display: flex; gap: 8px; padding: 8px 10px; background: var(--panel); border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { min-height: 42px; max-height: 160px; }
.composer .note { flex: 1; color: var(--muted); font-size: 14px; align-self: center; }
.placeholder { flex: 1; display: grid; place-items: center; color: var(--muted); text-align: center; padding: 20px; }

/* side drawer */
.drawer {
  position: fixed; inset: 0 0 0 auto; width: min(420px, 100%); background: var(--panel); border-left: 1px solid var(--line);
  box-shadow: -4px 0 18px rgba(0,0,0,.15); z-index: 20; display: flex; flex-direction: column;
}
.drawer-head { display: flex; align-items: center; padding: 10px 12px; border-bottom: 1px solid var(--line); gap: 8px; }
.drawer-head h2 { font-size: 16px; margin: 0; flex: 1; }
.drawer-body { overflow-y: auto; padding: 12px; }
.kv { display: grid; grid-template-columns: 40% 60%; gap: 4px 8px; font-size: 13.5px; }
.kv .k { color: var(--muted); overflow-wrap: anywhere; }
.kv .v { overflow-wrap: anywhere; }
h4 { margin: 18px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }

/* pages */
.page { flex: 1; overflow-y: auto; padding: 16px; }
.page-inner { max-width: 1100px; margin: 0 auto; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 14px; }
.box { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px; box-shadow: var(--shadow); }
.box h3 { margin: 0 0 10px; font-size: 15px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 14px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.kpi .v { font-size: 24px; font-weight: 700; }
.bars { display: flex; flex-direction: column; gap: 6px; }
.bar { display: grid; grid-template-columns: 120px 1fr 44px; gap: 8px; align-items: center; font-size: 13px; }
.bar .track { background: var(--panel-2); border-radius: 4px; height: 14px; overflow: hidden; }
.bar .fill { background: var(--accent); height: 100%; border-radius: 4px; }
.bar .lbl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar .num { text-align: right; font-variant-numeric: tabular-nums; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: left; padding: 7px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 12.5px; }
.fbitem { border-bottom: 1px solid var(--line); padding: 10px 0; display: grid; gap: 4px; }
.fbitem .quote { background: var(--panel-2); border-radius: 8px; padding: 6px 8px; white-space: pre-wrap; font-size: 13.5px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* lightbox and toast */
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 40; cursor: zoom-out; }
.lightbox img { max-width: 96vw; max-height: 92vh; }
.toast {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--panel);
  padding: 9px 16px; border-radius: 8px; z-index: 50; font-size: 14px; max-width: calc(100% - 32px);
}
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 30; padding: 16px; }
.modal { background: var(--panel); border-radius: 12px; padding: 18px; width: 100%; max-width: 440px; box-shadow: var(--shadow); }
.modal h3 { margin: 0 0 8px; }
.modal .row { justify-content: flex-end; margin-top: 14px; }

/* phones */
@media (max-width: 760px) {
  .topbar { padding: 6px 10px; gap: 6px; }
  .topbar .title span { display: none; }
  .tab { padding: 6px 8px; }
  .chats { grid-template-columns: 1fr; }
  .chats.open .list { display: none; }
  .chats:not(.open) .convo { display: none; }
  .back { display: inline-flex; }
  .msg { max-width: 88%; }
  .messages { padding: 10px 8px 14px; }
  .convo-head .actions .btn { padding: 6px 9px; min-height: 34px; font-size: 13.5px; }
  .bar { grid-template-columns: 90px 1fr 40px; }
  .hide-phone { display: none; }
}
