:root {
  --bg: #f4f5f7; --panel: #fff; --line: #e4e6eb; --text: #1c1e21; --muted: #6b7280;
  --accent: #2563eb; --shopee: #ee4d2d; --tiktok: #111; --in: #eef0f4; --out: #2563eb; --err: #b91c1c;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #14161a; --panel: #1c1f25; --line: #2c3038; --text: #e8eaed; --muted: #9aa1ac; --in: #2a2e36; --tiktok: #e8eaed; }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif; background: var(--bg); color: var(--text); }
button { font: inherit; cursor: pointer; border: 0; border-radius: 8px; padding: 8px 14px; background: var(--accent); color: #fff; }
button.ghost { background: transparent; color: var(--text); border: 1px solid var(--line); }
button:disabled { opacity: .5; cursor: default; }
input, textarea, select { font: inherit; color: inherit; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; width: 100%; }
textarea { resize: vertical; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

#app { display: grid; grid-template-columns: 200px 340px 1fr; height: 100%; }
#sidebar { background: var(--panel); border-right: 1px solid var(--line); padding: 16px 12px; display: flex; flex-direction: column; gap: 8px; }
.brand { font-weight: 700; font-size: 18px; padding: 0 8px 8px; }
#filters button { display: flex; align-items: center; gap: 8px; width: 100%; text-align: left; background: transparent; color: var(--text); padding: 9px 10px; }
#filters button.active { background: var(--bg); font-weight: 600; }
.badge { margin-left: auto; background: var(--accent); color: #fff; border-radius: 10px; font-size: 11px; padding: 1px 7px; }
.badge:empty { display: none; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot.shopee, .tag.shopee { background: var(--shopee); }
.dot.tiktok, .tag.tiktok { background: var(--tiktok); }
.side-sec { display: flex; flex-direction: column; gap: 6px; padding: 8px 10px; color: var(--muted); }
.chk { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.chk input { width: auto; }
.spacer { flex: 1; }

#list-pane { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tabs button { flex: 1; background: transparent; color: var(--muted); border-radius: 0; padding: 11px 8px; border-bottom: 2px solid transparent; display: flex; justify-content: center; align-items: center; gap: 6px; }
.tabs button.active { color: var(--text); font-weight: 600; border-bottom-color: var(--accent); }
.tabs .badge { margin-left: 0; }
.search { padding: 12px; border-bottom: 1px solid var(--line); }
#conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
#conv-list li { padding: 12px 14px; border-bottom: 1px solid var(--line); cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
#conv-list li:hover { background: var(--bg); }
#conv-list li.sel { background: var(--bg); box-shadow: inset 3px 0 var(--accent); }
#conv-list .name { font-weight: 600; display: flex; align-items: center; gap: 6px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
#conv-list .time { color: var(--muted); font-size: 12px; }
#conv-list .snip { color: var(--muted); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
#conv-list li.unread .snip { color: var(--text); font-weight: 600; }
#conv-list .unread-n { grid-row: 2; grid-column: 2; background: var(--accent); color: #fff; border-radius: 10px; font-size: 11px; padding: 0 7px; align-self: center; justify-self: end; }
#conv-list .empty { padding: 24px; color: var(--muted); text-align: center; cursor: default; display: block; }

#chat-pane { min-width: 0; min-height: 0; display: flex; }
#empty { margin: auto; color: var(--muted); }
#chat { display: flex; flex-direction: column; flex: 1; min-width: 0; min-height: 0; }
#chat[hidden], #empty[hidden], #note-box[hidden], .err[hidden] { display: none; }
#chat header { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 18px; background: var(--panel); border-bottom: 1px solid var(--line); }
.who { font-weight: 700; font-size: 16px; display: flex; gap: 8px; align-items: center; }
.sub { color: var(--muted); font-size: 12px; }
.tag { color: #fff; font-size: 11px; font-weight: 600; padding: 1px 8px; border-radius: 10px; }
.tag.tiktok { color: var(--bg); }
.actions { display: flex; gap: 8px; }
#note-box { padding: 8px 18px; background: #fff8e1; border-bottom: 1px solid var(--line); }
@media (prefers-color-scheme: dark) { #note-box { background: #2b2616; } }
#messages { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 68%; padding: 8px 12px; border-radius: 14px; background: var(--in); white-space: pre-wrap; word-break: break-word; }
.msg.out { align-self: flex-end; background: var(--out); color: #fff; }
.msg .t { display: block; font-size: 11px; opacity: .65; margin-top: 2px; }
.msg img { max-width: 240px; border-radius: 8px; display: block; }
.msg .card { font-style: italic; }
#chat footer { background: var(--panel); border-top: 1px solid var(--line); padding: 10px 18px 14px; }
#quick { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 8px; }
#quick button { background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 14px; padding: 3px 11px; font-size: 12px; white-space: nowrap; }
#composer { display: flex; gap: 10px; align-items: flex-end; }
.ok { color: #15803d; font-size: 13px; }
#pw-msg[hidden] { display: none; }
.err { color: var(--err); margin-top: 6px; font-size: 13px; }

dialog { width: min(640px, 94vw); border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--text); padding: 20px; }
dialog::backdrop { background: rgba(0,0,0,.45); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; }
.dlg-head h2 { margin: 0; font-size: 18px; }
dialog h3 { margin: 18px 0 8px; font-size: 14px; color: var(--muted); }
dialog ul { list-style: none; margin: 0; padding: 0; }
dialog li { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
dialog li .grow { flex: 1; min-width: 0; }
dialog li small { color: var(--muted); display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
dialog li small.bad { color: var(--err); white-space: normal; }
dialog summary { cursor: pointer; color: var(--accent); margin-top: 10px; }
.row { display: flex; gap: 8px; margin: 8px 0; }
#acc-form textarea { font-family: ui-monospace, Consolas, monospace; font-size: 12px; margin-bottom: 8px; }

@media (max-width: 900px) {
  #app { grid-template-columns: 1fr; }
  #sidebar { flex-direction: row; flex-wrap: wrap; align-items: center; padding: 8px; }
  #sidebar .side-sec, .spacer, .brand { display: none; }
  #filters { display: flex; gap: 4px; flex: 1; }
  #filters button { width: auto; }
  #app.in-chat #list-pane, #app.in-chat #sidebar { display: none; }
  #app:not(.in-chat) #chat-pane { display: none; }
  #chat header::before { content: "‹ Back"; cursor: pointer; color: var(--accent); }
}
