:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --side: #111b21;
  --side-text: #d1d7db;
  --text: #1c1e21;
  --muted: #667781;
  --line: #e4e6eb;
  --primary: #00a884;
  --primary-d: #008f72;
  --danger: #d93838;
  --warn: #f5a524;
  --in: #ffffff;
  --out: #d9fdd3;
  --human: #e3ecff;
  --chat-bg: #efeae2;
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b141a;
    --panel: #111b21;
    --side: #0b141a;
    --text: #e9edef;
    --muted: #8696a0;
    --line: #233138;
    --in: #202c33;
    --out: #005c4b;
    --human: #1f3a64;
    --chat-bg: #0b141a;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; }
.ok { color: var(--primary); font-size: 14px; }

input, textarea, select {
  width: 100%; font: inherit; color: var(--text); background: var(--panel);
  border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px;
}
textarea { resize: vertical; }
textarea.big { min-height: 320px; font-family: ui-monospace, Consolas, monospace; font-size: 14px; }
label { display: block; margin: 14px 0; font-weight: 600; font-size: 14px; }
label input, label textarea { margin-top: 6px; font-weight: 400; }

.btn { font: inherit; border: 1px solid var(--line); background: var(--panel); color: var(--text); border-radius: 8px; padding: 9px 16px; cursor: pointer; white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover { filter: brightness(0.97); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-d); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.warn { background: var(--warn); border-color: var(--warn); color: #111; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 24px; }
.center { text-align: center; }
.row { display: flex; gap: 12px; align-items: center; margin: 12px 0; flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; text-align: center; display: grid; gap: 12px; }
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; }
.logo { font-size: 40px; }

/* Layout */
.app { display: flex; height: 100vh; }
.sidebar { width: 230px; background: var(--side); color: var(--side-text); display: flex; flex-direction: column; padding: 14px 10px; gap: 4px; flex-shrink: 0; }
.brand { font-weight: 700; font-size: 18px; padding: 6px 10px 16px; color: #fff; }
.nav { display: flex; align-items: center; gap: 10px; background: none; border: 0; color: inherit; font: inherit; text-align: left; padding: 10px 12px; border-radius: 8px; cursor: pointer; }
.nav:hover { background: rgba(255,255,255,.06); }
.nav.active { background: rgba(0,168,132,.18); color: #fff; }
.nav .lbl { flex: 1; }
.badge { background: var(--warn); color: #111; border-radius: 99px; padding: 0 7px; font-size: 12px; }
.sidebar-foot { margin-top: auto; display: grid; gap: 10px; padding: 10px; }
.pill { font-size: 13px; padding: 5px 10px; border-radius: 99px; background: rgba(255,255,255,.08); text-align: center; }
.pill.on { background: rgba(0,168,132,.25); color: #7ff0d0; }
.pill.off { background: rgba(217,56,56,.25); color: #ffb3b3; }
.sidebar .btn.ghost { color: var(--side-text); border-color: rgba(255,255,255,.2); }
.switch { display: flex; gap: 8px; align-items: center; margin: 0; font-weight: 500; cursor: pointer; }
.switch input { width: auto; }
.main { flex: 1; min-width: 0; overflow: auto; }
.page { max-width: 900px; margin: 0 auto; padding: 28px 20px; }
.page h2 { margin-top: 0; }

/* Inbox */
.tab#tab-inbox { height: 100%; }
.inbox { display: flex; height: 100%; }
.convo-list { width: 340px; border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; flex-shrink: 0; }
.list-head { display: flex; gap: 8px; padding: 12px; border-bottom: 1px solid var(--line); }
.list-head select { width: auto; }
#convos { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
#convos li { padding: 12px 14px; border-bottom: 1px solid var(--line); cursor: pointer; display: grid; gap: 2px; }
#convos li:hover { background: rgba(0,0,0,.03); }
#convos li.sel { background: rgba(0,168,132,.10); }
.ci-top { display: flex; justify-content: space-between; gap: 8px; }
.ci-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ci-time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.ci-last { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tag { font-size: 11px; border-radius: 4px; padding: 1px 6px; margin-right: 4px; }
.tag.human { background: var(--warn); color: #111; }
.tag.paused { background: var(--line); color: var(--muted); }

.chat { flex: 1; display: flex; min-width: 0; background: var(--chat-bg); }
.empty { margin: auto; color: var(--muted); }
.chat-box { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.chat-head { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-name { font-weight: 600; }
.chat-actions { margin-left: auto; display: flex; gap: 8px; }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 75%; padding: 7px 10px 5px; border-radius: 8px; white-space: pre-wrap; word-wrap: break-word; box-shadow: 0 1px 0 rgba(0,0,0,.06); }
.msg.user { background: var(--in); align-self: flex-start; }
.msg.assistant { background: var(--out); align-self: flex-end; }
.msg.human { background: var(--human); align-self: flex-end; }
.msg .meta { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; }
.composer { display: flex; gap: 8px; padding: 10px 12px; background: var(--panel); border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { min-height: 42px; max-height: 160px; }
.test { padding: 0; overflow: hidden; display: flex; flex-direction: column; height: 60vh; background: var(--chat-bg); }

/* QR */
#qrArea img { width: 280px; height: 280px; background: #fff; padding: 10px; border-radius: 12px; }
#qrArea .status { font-size: 18px; font-weight: 600; margin: 10px 0; }
.steps { text-align: left; display: inline-block; margin: 16px auto; }
.mobile-only { display: none; }

@media (max-width: 800px) {
  .app { flex-direction: column; height: auto; min-height: 100vh; }
  .sidebar { width: 100%; flex-direction: row; overflow-x: auto; padding: 8px; align-items: center; }
  .brand, .nav .lbl { display: none; }
  .sidebar-foot { margin: 0 0 0 auto; display: flex; padding: 0; align-items: center; }
  .switch span { display: none; }
  .inbox { height: calc(100vh - 60px); }
  .convo-list { width: 100%; border-right: 0; }
  .inbox.open .convo-list { display: none; }
  .inbox:not(.open) .chat { display: none; }
  .mobile-only { display: inline-flex; }
  .grid2 { grid-template-columns: 1fr; }
  .msg { max-width: 88%; }
  .page { padding: 20px 16px; }
}
