:root {
  --bg: #f3f5f4;
  --panel: #ffffff;
  --ink: #17221f;
  --muted: #6b7a76;
  --line: #e2e7e5;
  --brand: #0f5c4d;
  --brand-soft: #e3f1ed;
  --in: #ffffff;
  --out: #d9f2e6;
  --ai: #e8eefc;
  --note: #fff6d6;
  --warn: #fff1e0;
  --warn-ink: #8a4b00;
  --danger: #b42318;
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1513; --panel: #17201d; --ink: #e6eeeb; --muted: #93a39e; --line: #26322e;
    --brand: #3fb594; --brand-soft: #1b2e28; --in: #1e2925; --out: #1d3b30; --ai: #1e2840;
    --note: #3a3320; --warn: #3a2a17; --warn-ink: #ffc580;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--ink); }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 14px; }
button { font: inherit; cursor: pointer; }
input, select, textarea { font: inherit; color: var(--ink); background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; }

.btn { border: 1px solid var(--line); background: var(--panel); color: var(--ink); border-radius: 8px; padding: 8px 14px; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 600; }
.btn.danger { color: var(--danger); }
.icon-btn { border: 0; background: transparent; color: var(--ink); font-size: 18px; width: 38px; height: 38px; border-radius: 8px; }
.icon-btn:hover { background: var(--brand-soft); }
.chip { border: 1px solid var(--line); background: var(--panel); color: var(--ink); border-radius: 999px; padding: 4px 10px; font-size: 13px; }
.chip.active { background: var(--note); border-color: #e6c84a; }

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.logo { display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 8px; background: var(--brand); color: #fff; font-weight: 800; }
.logo.has-img { background: #fff; overflow: hidden; border: 1px solid var(--line, #e5e5e5); }
.logo img { width: 100%; height: 100%; object-fit: contain; display: block; }
.logo-row { display: flex; gap: 14px; align-items: center; margin: 4px 0 14px; }
.logo-row .logo.big { margin: 0; flex: none; }
.logo-row p { margin: 2px 0 8px; }
.logo.big { width: 56px; height: 56px; font-size: 26px; border-radius: 14px; margin: 0 auto 10px; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 340px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 24px; display: grid; gap: 14px; }
.login-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }

/* Layout */
.app { display: grid; grid-template-columns: 340px 1fr; height: 100dvh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.topbar { display: flex; justify-content: space-between; align-items: center; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.top-actions { display: flex; gap: 2px; }
.mode-bar { display: flex; gap: 6px; flex-wrap: wrap; padding: 8px 12px 0; }
.pill { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--brand-soft); color: var(--brand); }
.pill.off { background: var(--warn); color: var(--warn-ink); }
.filters { padding: 8px 12px; display: grid; gap: 8px; border-bottom: 1px solid var(--line); }
.tabs { display: flex; gap: 4px; }
.tab { flex: 1; border: 0; background: transparent; color: var(--muted); padding: 6px; border-radius: 8px; }
.tab.active { background: var(--brand-soft); color: var(--brand); font-weight: 600; }
.filter-row { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.filter-row > * { min-width: 0; font-size: 14px; padding: 6px 8px; }

.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv { display: grid; grid-template-columns: 40px 1fr auto; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; align-items: center; }
.conv:hover { background: var(--bg); }
.conv.active { background: var(--brand-soft); }
.avatar { width: 40px; height: 40px; border-radius: 50%; background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; font-weight: 700; }
.conv-main { min-width: 0; }
.conv-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-last { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-meta { display: grid; justify-items: end; gap: 4px; font-size: 12px; color: var(--muted); }
.badge { background: var(--brand); color: #fff; border-radius: 999px; min-width: 20px; padding: 0 6px; text-align: center; font-size: 12px; font-weight: 700; }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--bg); }
.tag.human { background: var(--warn); color: var(--warn-ink); }
.empty-list { padding: 24px; text-align: center; color: var(--muted); }

/* Chat */
.chat { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.chat .chat-inner { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat.empty .chat-inner { display: none; }
.chat:not(.empty) .empty-state { display: none; }
.empty-state { margin: auto; text-align: center; color: var(--muted); padding: 16px; }
.chat-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-head .back { display: none; }
.who { flex: 1; min-width: 0; }
.who-name { font-weight: 700; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who-sub { font-size: 12px; color: var(--muted); }
.details { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); align-items: end; }
.details label { display: grid; gap: 2px; font-size: 12px; color: var(--muted); }

.switch { display: flex; align-items: center; gap: 6px; cursor: pointer; user-select: none; }
.switch input { display: none; }
.slider { width: 38px; height: 22px; border-radius: 999px; background: var(--line); position: relative; transition: background .15s; }
.slider::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0,0,0,.25); }
.switch input:checked + .slider { background: var(--brand); }
.switch input:checked + .slider::after { transform: translateX(16px); }
.switch-label { font-size: 13px; font-weight: 600; }

.banner { margin: 8px 12px 0; padding: 8px 12px; border-radius: 10px; background: var(--warn); color: var(--warn-ink); font-size: 14px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.banner input { flex: 1; min-width: 120px; padding: 4px 8px; }

.messages { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 78%; padding: 7px 10px 5px; border-radius: var(--radius); white-space: pre-wrap; word-wrap: break-word; box-shadow: 0 1px 1px rgba(0,0,0,.05); }
.msg.in { align-self: flex-start; background: var(--in); border-top-left-radius: 4px; }
.msg.out { align-self: flex-end; background: var(--out); border-top-right-radius: 4px; }
.msg.out.ai { background: var(--ai); }
.msg.note { align-self: center; background: var(--note); max-width: 90%; font-size: 13px; }
.msg.system { background: transparent; box-shadow: none; color: var(--muted); font-size: 12px; text-align: center; }
.msg-meta { font-size: 11px; color: var(--muted); margin-top: 2px; text-align: right; }
.msg.failed .msg-meta { color: var(--danger); }
.day-sep { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); padding: 2px 10px; border-radius: 999px; margin: 6px 0; }
.typing { padding: 0 16px 4px; font-size: 13px; color: var(--muted); font-style: italic; }

.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 8px 12px calc(8px + env(safe-area-inset-bottom)); display: grid; gap: 6px; }
.composer-tools { display: flex; gap: 6px; flex-wrap: wrap; }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; max-height: 160px; }
.composer.note-mode textarea { background: var(--note); }

/* Modal & toast */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: grid; place-items: center; padding: 16px; z-index: 10; }
.modal-card { background: var(--panel); border-radius: 16px; padding: 20px; width: 100%; max-width: 560px; max-height: 90dvh; overflow: auto; display: grid; gap: 12px; }
.modal-card h3 { margin: 0; }
.modal-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.modal-card textarea.kb { min-height: 50dvh; font: 13px/1.5 ui-monospace, Menlo, Consolas, monospace; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.menu-list { display: grid; gap: 6px; }
.menu-list .btn { text-align: left; }
.team-row { display: flex; justify-content: space-between; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--line); }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--panel); padding: 10px 16px; border-radius: 10px; z-index: 20; max-width: calc(100% - 32px); }

/* Mobile: show list OR chat */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .app.show-chat .sidebar { display: none; }
  .app:not(.show-chat) .chat { display: none; }
  .chat-head .back { display: inline-block; }
  .details { grid-template-columns: 1fr 1fr; }
  .details .btn { grid-column: span 2; }
  .msg { max-width: 88%; }
}

/* Sales stages */
.tag.stage { background: var(--brand-soft); color: var(--brand); }
.tag.stage-lost { background: var(--bg); color: var(--muted); text-decoration: line-through; }
/* Usage page */
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.stat { border: 1px solid var(--line); border-radius: 10px; padding: 10px; }
.stat .v { font-size: 22px; font-weight: 700; }
.stat .k { font-size: 12px; color: var(--muted); }
.meter { height: 10px; border-radius: 999px; background: var(--bg); overflow: hidden; border: 1px solid var(--line); }
.meter > span { display: block; height: 100%; background: var(--brand); }
.meter.warn > span { background: #d97706; } .meter.over > span { background: var(--danger); }
.set-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.set-grid label.wide { grid-column: span 2; }
.keybox { font: 12px ui-monospace, Menlo, Consolas, monospace; word-break: break-all; background: var(--bg); padding: 8px; border-radius: 8px; }
.modal-card.wide { max-width: 760px; }
.mini-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.mini-table td, .mini-table th { text-align: left; padding: 4px 6px; border-bottom: 1px solid var(--line); }
@media (max-width: 760px) { .set-grid { grid-template-columns: 1fr; } .set-grid label.wide { grid-column: auto; } }

.details input, .details select { width: 100%; min-width: 0; }
