:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --panel-2: #f9fafb;
  --line: #e5e7eb;
  --text: #111827;
  --muted: #6b7280;
  --primary: #07c160;
  --primary-ink: #ffffff;
  --accent: #2563eb;
  --danger: #dc2626;
  --warn-bg: #fff7ed;
  --warn-ink: #9a3412;
  --bubble-user: #ffffff;
  --bubble-me: #95ec69;
  --bubble-platform: #e0ecff;
  --sel: #e8f5ee;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.06);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111315; --panel: #1b1e21; --panel-2: #22262a; --line: #2f3439; --text: #e5e7eb; --muted: #9ca3af;
    --bubble-user: #2a2f34; --bubble-me: #2f6b3a; --bubble-platform: #1f3352; --sel: #1f3a2b;
    --warn-bg: #3a2514; --warn-ink: #fdba74;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 14px/1.5 -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.err { color: var(--danger); min-height: 1.5em; }
button { font: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; }
textarea { resize: vertical; width: 100%; }
.btn { border: 1px solid var(--line); background: var(--panel); color: var(--text); border-radius: 6px; padding: 6px 14px; }
.btn:hover { border-color: var(--muted); }
.btn.small { padding: 3px 10px; font-size: 12px; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.link { border: 0; background: none; color: var(--accent); padding: 0 4px; }

/* 登录 */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; background: var(--panel); padding: 28px; border-radius: 12px; box-shadow: var(--shadow); display: grid; gap: 12px; }
.login-card h1 { margin: 0; font-size: 20px; }
.login-card p { margin: 0; }
.login-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }

/* 布局 */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar { height: 48px; flex: none; display: flex; align-items: center; gap: 16px; padding: 0 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { font-weight: 600; white-space: nowrap; }
.sync { flex: 1; min-width: 0; font-size: 12px; color: var(--muted); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.sync .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--primary); margin-right: 6px; }
.sync .dot.warn { background: #f59e0b; }
.sync .dot.stop { background: var(--danger); }
.sync .paused { color: var(--danger); font-weight: 600; }
.steps { margin: 0; padding-left: 20px; display: grid; gap: 14px; }
.steps p { margin: 4px 0; }
.steps pre { margin: 6px 0 0; padding: 8px 10px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; overflow-x: auto; font-size: 12px; white-space: pre; }
.top-actions { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.me { color: var(--muted); font-size: 13px; margin-left: 8px; }
.main { flex: 1; min-height: 0; display: grid; grid-template-columns: 320px 1fr 340px; }

/* 列表 */
.list-pane { display: flex; flex-direction: column; min-height: 0; background: var(--panel); border-right: 1px solid var(--line); }
.tabs { display: flex; border-bottom: 1px solid var(--line); overflow-x: auto; }
.tabs button { flex: 1; border: 0; background: none; padding: 10px 4px; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; font-size: 13px; }
.tabs button.active { color: var(--text); border-bottom-color: var(--primary); font-weight: 600; }
.tabs b { font-weight: 600; color: var(--danger); font-size: 12px; }
.search { padding: 8px; border-bottom: 1px solid var(--line); display: grid; gap: 6px; }
.search input { width: 100%; }
.chip { font-size: 12px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px; padding: 2px 10px; justify-self: start; }
.list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.item { padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; display: grid; gap: 2px; position: relative; }
.item:hover { background: var(--panel-2); }
.item.sel { background: var(--sel); }
.item .row1 { display: flex; justify-content: space-between; gap: 8px; }
.item .name { font-weight: 600; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.item .time { color: var(--muted); font-size: 12px; white-space: nowrap; }
.item .preview { color: var(--muted); font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.item .tags { display: flex; gap: 4px; flex-wrap: wrap; }
.item.unread::before { content: ""; position: absolute; left: 3px; top: 16px; width: 6px; height: 6px; border-radius: 50%; background: var(--danger); }
.tag { font-size: 11px; padding: 0 6px; border-radius: 4px; background: var(--panel-2); border: 1px solid var(--line); color: var(--muted); }
.tag.red { color: var(--danger); border-color: var(--danger); }
.tag.green { color: var(--primary); border-color: var(--primary); }
.tag.blue { color: var(--accent); border-color: var(--accent); }
.more { margin: 8px; }
.list-empty { padding: 24px; text-align: center; color: var(--muted); }

/* 聊天 */
.chat-pane { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.empty { margin: auto; color: var(--muted); }
.chat { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chat-head { display: flex; align-items: center; gap: 12px; padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-title { flex: 1; min-width: 0; }
.chat-title > div { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
#cTitle { font-weight: 600; }
.chat-actions { display: flex; align-items: center; gap: 6px; }
.back, #btnInfo { display: none; }
.state { font-size: 12px; padding: 1px 8px; border-radius: 10px; border: 1px solid var(--line); }
.state.PENDING { color: var(--danger); border-color: var(--danger); }
.state.PROCESSING { color: var(--accent); border-color: var(--accent); }
.state.PROCESSED { color: var(--muted); }
.banner { padding: 6px 16px; font-size: 13px; }
.banner.warn { background: var(--warn-bg); color: var(--warn-ink); }
.thread { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.msg { display: flex; flex-direction: column; max-width: 72%; }
.msg .meta { font-size: 12px; color: var(--muted); margin-bottom: 2px; }
.msg .bubble { padding: 8px 12px; border-radius: 8px; background: var(--bubble-user); box-shadow: 0 1px 1px rgba(0,0,0,.05); white-space: pre-wrap; word-break: break-word; }
.msg.user { align-self: flex-start; }
.msg.merchant { align-self: flex-end; align-items: flex-end; }
.msg.merchant .bubble { background: var(--bubble-me); color: #0b2b12; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .msg.merchant .bubble { color: var(--text); } }
.msg.platform { align-self: flex-start; }
.msg.platform .bubble { background: var(--bubble-platform); }
.msg.system { align-self: center; max-width: 90%; text-align: center; }
.msg.system .bubble { background: none; box-shadow: none; color: var(--muted); font-size: 12px; padding: 0; }
.msg.pending .bubble { opacity: .7; border: 1px dashed var(--muted); }
.msg.failed .bubble { border: 1px solid var(--danger); }
.msg .status { font-size: 12px; color: var(--muted); margin-top: 2px; }
.msg.failed .status { color: var(--danger); }
.imgs { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.imgs img { width: 88px; height: 88px; object-fit: cover; border-radius: 6px; cursor: zoom-in; background: var(--panel-2); border: 1px solid var(--line); }
.day { align-self: center; font-size: 12px; color: var(--muted); }
.composer { flex: none; background: var(--panel); border-top: 1px solid var(--line); padding: 8px 16px 12px; display: grid; gap: 6px; }
.composer.readonly { color: var(--muted); text-align: center; padding: 16px; }
.composer-bar { display: flex; align-items: center; gap: 8px; }
.composer-bar select { max-width: 220px; }
#counter { flex: 1; text-align: right; }
#counter.over { color: var(--danger); }
.attach { display: flex; gap: 6px; flex-wrap: wrap; }
.attach:empty { display: none; }
.thumb { position: relative; }
.thumb img { width: 56px; height: 56px; object-fit: cover; border-radius: 4px; border: 1px solid var(--line); }
.thumb button { position: absolute; top: -6px; right: -6px; width: 18px; height: 18px; border-radius: 50%; border: 0; background: var(--danger); color: #fff; font-size: 12px; line-height: 18px; padding: 0; }
.thumb.uploading img { opacity: .4; }

/* 详情 */
.info-pane { background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; padding: 12px 16px; }
.info-pane h3 { font-size: 13px; margin: 16px 0 8px; color: var(--muted); font-weight: 600; }
.info-pane h3:first-child { margin-top: 4px; }
.kv { display: grid; grid-template-columns: 84px 1fr; gap: 4px 8px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-all; }
.actions { display: grid; gap: 8px; }
.actions .btn { width: 100%; }
.refund-box { border: 1px solid var(--line); border-radius: 8px; padding: 10px; display: grid; gap: 8px; }
.refund-box label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.refund-box .row { display: flex; gap: 8px; }

/* 弹窗 */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: grid; place-items: center; padding: 16px; z-index: 10; }
.modal-card { width: 100%; max-width: 760px; max-height: 86vh; display: flex; flex-direction: column; background: var(--panel); border-radius: 10px; box-shadow: var(--shadow); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.modal-body { padding: 12px 16px; overflow-y: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th, .table td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { color: var(--muted); font-weight: 500; }
.qr-form { display: grid; gap: 8px; margin-bottom: 12px; }
.qr-form .row { display: flex; gap: 8px; }
.qr-form input { flex: 1; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 20; cursor: zoom-out; }
.lightbox img { max-width: 94vw; max-height: 94vh; }
.toast { position: fixed; left: 50%; bottom: 32px; transform: translateX(-50%); background: rgba(17,24,39,.92); color: #fff; padding: 8px 16px; border-radius: 6px; z-index: 30; max-width: 90vw; }
.toast.error { background: var(--danger); }

/* 窄屏：一次只显示一栏 */
@media (max-width: 1100px) {
  .main { grid-template-columns: 300px 1fr; }
  .info-pane { position: fixed; right: 0; top: 48px; bottom: 0; width: min(360px, 100vw); z-index: 5; box-shadow: var(--shadow); transform: translateX(100%); transition: transform .2s; }
  .info-pane.open { transform: none; }
  #btnInfo { display: inline-block; }
}
@media (max-width: 720px) {
  .topbar { gap: 8px; padding: 0 12px; }
  .sync, .me, #btnNotifyPerm, #btnLogs { display: none; }
  .main { grid-template-columns: 1fr; }
  .app.chatting .list-pane { display: none; }
  .app:not(.chatting) .chat-pane { display: none; }
  .back { display: inline-block; }
  .msg { max-width: 88%; }
  .thread { padding: 12px; }
  .composer { padding: 8px 12px 12px; }
  .chat-head { padding: 8px 12px; gap: 8px; }
}
a.btn { display: inline-block; text-decoration: none; }
