:root {
  --brand: #1877f2;
  --brand-dark: #1259b8;
  --brand-soft: #e7f0fd;
  --accent: #f59e0b;
  --accent-soft: #fff4e0;
  --green: #1f9d5a;
  --green-soft: #e3f5ec;
  --red: #d64545;
  --red-soft: #fde8e8;
  --bg: #f0f2f5;
  --panel: #ffffff;
  --line: #e1e5ea;
  --text: #1c1e21;
  --muted: #65676b;
  --bubble-in: #ffffff;
  --bubble-out: #d7e8fd;
  --bubble-bot: #eef1f4;
  --bubble-page: #e9e3fb;
  --chat-bg: #e8edf2;
  --radius: 14px;
  font-family: 'Sarabun', 'Noto Sans Thai', 'Leelawadee UI', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
body { font-size: 14px; -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--brand); }
[hidden] { display: none !important; }
.boot { display: grid; place-items: center; height: 100vh; color: var(--muted); }

.btn { border: 1px solid var(--line); background: #fff; border-radius: 10px; padding: 7px 12px; font-weight: 600; white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover { background: #f7f9fa; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.success { background: var(--green); border-color: var(--green); color: #fff; }
.btn.danger { color: var(--red); border-color: #f1c4c4; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { padding: 4px 9px; font-size: 13px; border-radius: 8px; }
.btn.icon { padding: 6px 8px; }
.btn:disabled { opacity: .5; cursor: default; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field label { font-size: 13px; color: var(--muted); font-weight: 600; }
.field .hint { font-size: 12px; color: var(--muted); }
.input, textarea.input, select.input { width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px; background: #fff; outline: none; }
.input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
textarea.input { resize: vertical; min-height: 70px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.error { color: var(--red); font-size: 13px; min-height: 18px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, Menlo, monospace; font-size: 12px; word-break: break-all; }
.check { display: flex; align-items: center; gap: 8px; margin: 8px 0; cursor: pointer; }
.check input { width: 18px; height: 18px; }
.pill { display: inline-flex; align-items: center; gap: 4px; border-radius: 999px; padding: 2px 9px; font-size: 12px; font-weight: 600; background: var(--bg); color: var(--muted); }
.pill.green { background: var(--green-soft); color: var(--green); }
.pill.blue { background: var(--brand-soft); color: var(--brand); }
.pill.orange { background: var(--accent-soft); color: #b36b00; }
.pill.red { background: var(--red-soft); color: var(--red); }
.pill.purple { background: var(--bubble-page); color: #5b3fb3; }
.tagchip { display: inline-flex; align-items: center; gap: 4px; border-radius: 6px; padding: 1px 7px; font-size: 12px; color: #fff; font-weight: 600; }
.tagchip button { border: 0; background: transparent; color: inherit; padding: 0 0 0 2px; font-size: 13px; line-height: 1; }

/* ---------- login ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background: linear-gradient(160deg, #1877f2 0%, #42a5f5 55%, #90caf9 100%); }
.auth-card { width: 100%; max-width: 380px; background: #fff; border-radius: 20px; padding: 28px 24px; box-shadow: 0 20px 50px rgba(0, 40, 90, .25); }
.auth-card h1 { margin: 0 0 4px; font-size: 22px; }
.auth-card .logo { width: 52px; height: 52px; border-radius: 14px; margin-bottom: 12px; }
.auth-card .btn.primary { width: 100%; padding: 11px; font-size: 15px; justify-content: center; }

/* ---------- layout ---------- */
.shell { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
.topbar { display: flex; align-items: center; gap: 6px; padding: 6px 12px; padding-top: calc(6px + env(safe-area-inset-top, 0px)); background: var(--brand); color: #fff; flex-shrink: 0; overflow-x: auto; }
.topbar .brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 15px; margin-right: 8px; white-space: nowrap; }
.topbar .brand img { width: 26px; height: 26px; border-radius: 7px; }
.topbar .nav { display: flex; gap: 2px; }
.topbar .nav button { border: 0; background: transparent; color: rgba(255,255,255,.85); padding: 6px 10px; border-radius: 8px; font-weight: 600; white-space: nowrap; position: relative; }
.topbar .nav button:hover { background: rgba(255,255,255,.15); }
.topbar .nav button.on { background: rgba(255,255,255,.22); color: #fff; }
.topbar .nav .badge { position: absolute; top: 0; right: 0; }
.topbar .spacer { flex: 1; }
.topbar .who { font-size: 13px; opacity: .9; white-space: nowrap; }
.topbar .btn { background: rgba(255, 255, 255, .15); border-color: transparent; color: #fff; }
.status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: #9be7c4; margin-right: 4px; }
.status-dot.off { background: #ffcf9e; }
.status-dot.down { background: #ff8a8a; }
.badge { background: var(--red); color: #fff; border-radius: 999px; font-size: 11px; padding: 1px 6px; font-weight: 700; min-width: 18px; text-align: center; }
.badge.soft { background: var(--brand-soft); color: var(--brand); }
.main { flex: 1; display: grid; grid-template-columns: 340px 1fr 320px; min-height: 0; }
.main.no-info { grid-template-columns: 340px 1fr; }
.main > * { min-width: 0; }
.page { flex: 1; overflow: auto; padding: 20px; }
.page .inner { max-width: 1000px; margin: 0 auto; }
.page h2 { margin: 0 0 14px; font-size: 20px; }
.page h3 { margin: 18px 0 8px; font-size: 15px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.card.warn { background: var(--accent-soft); border-color: #f6d59a; }
.card.ok { background: var(--green-soft); border-color: #bfe6d0; }
table.tbl { width: 100%; border-collapse: collapse; background: #fff; border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); }
.tbl th, .tbl td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.tbl th { font-size: 12px; color: var(--muted); background: #fafbfc; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl tr.click { cursor: pointer; }
.tbl tr.click:hover td { background: #f7f9fc; }
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 14px; }
.stat { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.stat .n { font-size: 26px; font-weight: 700; }
.stat .l { font-size: 12px; color: var(--muted); }

/* ---------- conversation list ---------- */
.list-pane { display: flex; flex-direction: column; border-right: 1px solid var(--line); background: var(--panel); min-height: 0; }
.chips { display: flex; gap: 6px; padding: 10px 10px 4px; overflow-x: auto; }
.chip { border: 1px solid var(--line); background: #fff; padding: 4px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; color: var(--muted); }
.chip img { width: 18px; height: 18px; border-radius: 50%; }
.chip.on { background: var(--brand); border-color: var(--brand); color: #fff; }
.chip .badge { font-size: 10px; padding: 0 5px; min-width: 16px; }
.tabs { display: flex; gap: 2px; padding: 4px 8px; overflow-x: auto; }
.tab { border: 0; background: transparent; padding: 6px 9px; border-radius: 999px; color: var(--muted); font-weight: 600; white-space: nowrap; font-size: 13px; }
.tab.on { background: var(--brand-soft); color: var(--brand); }
.tab .badge { margin-left: 4px; }
.search { padding: 4px 10px 8px; }
.list { flex: 1; overflow-y: auto; }
.item { display: flex; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; align-items: flex-start; }
.item:hover { background: #f7f9fc; }
.item.on { background: var(--brand-soft); }
.item .body { flex: 1; min-width: 0; }
.item .name { font-weight: 600; display: flex; align-items: center; gap: 6px; }
.item .name span.nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .prev { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }
.item.unread .prev { color: var(--text); font-weight: 600; }
.item .meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; font-size: 11px; color: var(--muted); flex-shrink: 0; }
.item .pagetag { font-size: 11px; color: var(--muted); display: inline-flex; align-items: center; gap: 4px; }
.item .pagetag img { width: 14px; height: 14px; border-radius: 50%; }
.avatar { width: 44px; height: 44px; border-radius: 50%; object-fit: cover; background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; font-weight: 700; flex-shrink: 0; }
.avatar.sm { width: 32px; height: 32px; font-size: 13px; }
.avatar.lg { width: 72px; height: 72px; font-size: 26px; }
.avwrap { position: relative; }
.avwrap .pg { position: absolute; right: -3px; bottom: -3px; width: 18px; height: 18px; border-radius: 50%; border: 2px solid #fff; background: #fff; }
.empty { padding: 40px 16px; text-align: center; color: var(--muted); }
.more { padding: 10px; text-align: center; }

/* ---------- chat ---------- */
.chat { display: flex; flex-direction: column; min-height: 0; background: var(--chat-bg); }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.chat-head .title { font-weight: 700; display: flex; flex-direction: column; min-width: 0; }
.chat-head .title .sub { font-size: 12px; color: var(--muted); font-weight: 500; display: flex; align-items: center; gap: 5px; }
.chat-head .title .sub img { width: 14px; height: 14px; border-radius: 50%; }
.chat-head .spacer { flex: 1; }
.msgs { flex: 1; overflow-y: auto; padding: 14px 14px 6px; display: flex; flex-direction: column; gap: 4px; }
.daysep { align-self: center; background: rgba(0,0,0,.08); color: #333; font-size: 11px; padding: 2px 10px; border-radius: 999px; margin: 8px 0; }
.sysmsg { align-self: center; color: var(--muted); font-size: 12px; margin: 4px 0; background: rgba(255,255,255,.6); padding: 2px 10px; border-radius: 999px; }
.msg { max-width: 72%; display: flex; flex-direction: column; }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.bubble { padding: 8px 12px; border-radius: 16px; background: var(--bubble-in); white-space: pre-wrap; word-break: break-word; box-shadow: 0 1px 1px rgba(0,0,0,.05); line-height: 1.45; }
.msg.in .bubble { border-bottom-left-radius: 4px; }
.msg.out .bubble { background: var(--bubble-out); border-bottom-right-radius: 4px; }
.msg.out.bot .bubble { background: var(--bubble-bot); }
.msg.out.page .bubble { background: var(--bubble-page); }
.msg.failed .bubble { background: var(--red-soft); border: 1px solid #f1c4c4; }
.bubble img.pic { max-width: 260px; max-height: 300px; border-radius: 10px; display: block; cursor: zoom-in; }
.bubble img.sticker { width: 110px; display: block; }
.bubble video, .bubble audio { max-width: 280px; display: block; }
.bubble .att { display: inline-flex; gap: 6px; align-items: center; }
.msg .who { font-size: 11px; color: var(--muted); margin: 3px 4px 0; display: flex; gap: 6px; align-items: center; }
.msg .who .st { color: var(--brand); }
.msg .who .err { color: var(--red); }
.qr-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }
.qr-row span { border: 1px solid var(--brand); color: var(--brand); border-radius: 999px; padding: 1px 9px; font-size: 12px; background: #fff; }
.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 8px 10px; }
.composer .notice { font-size: 12px; color: #b36b00; background: var(--accent-soft); padding: 5px 10px; border-radius: 8px; margin-bottom: 6px; }
.composer .box { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; min-height: 40px; max-height: 160px; resize: none; border: 1px solid var(--line); border-radius: 12px; padding: 9px 11px; outline: none; }
.composer textarea:focus { border-color: var(--brand); }
.qr-pop { position: absolute; bottom: 100%; left: 0; right: 0; max-height: 260px; overflow: auto; background: #fff; border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 -6px 24px rgba(0,0,0,.08); margin-bottom: 6px; z-index: 5; }
.qr-pop .qi { padding: 8px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.qr-pop .qi:hover, .qr-pop .qi.on { background: var(--brand-soft); }
.qr-pop .qi b { display: block; font-size: 13px; }
.qr-pop .qi span { font-size: 12px; color: var(--muted); display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.composer .wrap { position: relative; }
.chat-empty { flex: 1; display: grid; place-items: center; color: var(--muted); }

/* ---------- info pane ---------- */
.info { border-left: 1px solid var(--line); background: var(--panel); overflow-y: auto; padding: 16px; }
.info .head { text-align: center; margin-bottom: 10px; }
.info .head .avatar { margin: 0 auto 8px; }
.info .head .nm { font-weight: 700; font-size: 16px; }
.info h4 { margin: 16px 0 6px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.info .kv { display: grid; grid-template-columns: 90px 1fr; gap: 4px 8px; font-size: 13px; }
.info .kv .k { color: var(--muted); }
.info .tags { display: flex; gap: 5px; flex-wrap: wrap; }
.info .customs .row { margin-bottom: 6px; }
.info .customs .input { padding: 6px 8px; }

/* ---------- modal / toast ---------- */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal { background: #fff; border-radius: 16px; padding: 20px; width: 100%; max-width: 560px; max-height: 92vh; overflow: auto; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal h3 { margin: 0 0 14px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1c1e21; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 100; max-width: 90vw; }
.toast.err { background: var(--red); }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 60; cursor: zoom-out; }
.lightbox img { max-width: 95vw; max-height: 95vh; }

/* ---------- rules ---------- */
.rule { display: flex; gap: 12px; align-items: flex-start; padding: 12px 14px; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 8px; }
.rule .body { flex: 1; min-width: 0; }
.rule .nm { font-weight: 700; }
.rule .pat { font-family: ui-monospace, Menlo, monospace; font-size: 12px; background: var(--bg); padding: 2px 6px; border-radius: 6px; }
.rule .rp { color: var(--muted); font-size: 13px; margin-top: 4px; white-space: pre-wrap; }
.rule.off { opacity: .55; }
.switch { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; font-size: 13px; }
.steps li { margin-bottom: 6px; }

@media (max-width: 960px) {
  .main, .main.no-info { grid-template-columns: 1fr; }
  .main > .list-pane, .main > .chat, .main > .info { display: none; }
  .main[data-mobile="list"] > .list-pane { display: flex; }
  .main[data-mobile="chat"] > .chat { display: flex; }
  .main[data-mobile="info"] > .info { display: block; }
  .msg { max-width: 86%; }
  .grid2 { grid-template-columns: 1fr; }
  .topbar .who { display: none; }
}
