:root {
  --bg: #f2f3f5;
  --surface: #ffffff;
  --surface-2: #f7f8fa;
  --line: #e7e9ec;
  --text: #16181d;
  --muted: #8b9099;
  --accent: #07c160;
  --accent-soft: rgba(7, 193, 96, .12);
  --accent-ink: #ffffff;
  --danger: #fa5151;
  --warn: #f59a23;
  --info: #3d7bfa;
  --self: #95ec69;
  --self-ink: #10170c;
  --other: #ffffff;
  --chip: #eef0f3;
  --pin: #f6f7f9;
  --shadow: 0 10px 30px rgba(15, 20, 30, .10);
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e0f11;
    --surface: #191a1d;
    --surface-2: #212226;
    --line: #2b2d32;
    --text: #e8e9ec;
    --muted: #8c9098;
    --accent-soft: rgba(7, 193, 96, .18);
    --self: #3aa46c;
    --self-ink: #f2fff6;
    --other: #26272b;
    --chip: #2a2c31;
    --pin: #1f2023;
    --shadow: 0 10px 30px rgba(0, 0, 0, .45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
a { color: var(--info); text-decoration: none; }
#app { height: 100dvh; }

/* ------------------------------------------------------------ layout */

.shell { height: 100dvh; display: flex; }
.pane-list { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--surface); }
.pane-chat { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--bg); }
.wide .pane-list { flex: 0 0 360px; border-right: 1px solid var(--line); }
.wide .pane-chat.empty { align-items: center; justify-content: center; color: var(--muted); }

.topbar {
  padding: calc(var(--safe-t) + 10px) 16px 8px;
  display: flex; align-items: center; gap: 10px;
  background: var(--surface);
}
.topbar h1 { font-size: 20px; margin: 0; font-weight: 650; letter-spacing: .2px; flex: 1; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.status-dot.on { background: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.status-dot.off { background: var(--danger); }
.status-text { font-size: 12px; color: var(--muted); }

.offline-banner {
  margin: 0 12px 8px; padding: 8px 12px; border-radius: 10px;
  background: rgba(250, 81, 81, .1); color: var(--danger); font-size: 13px;
}

.tabbar {
  display: flex; border-top: 1px solid var(--line); background: var(--surface);
  padding-bottom: var(--safe-b);
}
.tabbar button {
  flex: 1; padding: 8px 0 6px; display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--muted); font-size: 11px; position: relative;
}
.tabbar button.active { color: var(--accent); }
.tabbar svg { width: 24px; height: 24px; }
.tabbar .badge { position: absolute; top: 3px; left: calc(50% + 6px); }

.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }

/* ------------------------------------------------------------ list */

.accounts { display: flex; gap: 8px; padding: 2px 16px 10px; overflow-x: auto; scrollbar-width: none; }
.accounts::-webkit-scrollbar { display: none; }
.chip {
  flex: none; display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 12px; border-radius: 999px; background: var(--chip); font-size: 13px; color: var(--text);
  white-space: nowrap;
}
.chip.active { background: var(--accent); color: var(--accent-ink); }
.chip .status-dot { width: 6px; height: 6px; box-shadow: none; }
.chip.active .status-dot.on { background: #fff; }

.search { padding: 0 16px 10px; display: flex; gap: 8px; }
.search input {
  flex: 1; min-width: 0; border: 0; outline: 0; border-radius: 10px;
  padding: 8px 12px; background: var(--chip);
}
.search button { color: var(--accent); font-size: 14px; flex: none; }

.row {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px; position: relative;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.row:active, .row.selected { background: var(--surface-2); }
.row.pinned { background: var(--pin); }
.row.pinned:active, .row.pinned.selected { background: var(--surface-2); }
.row + .row::before {
  content: ""; position: absolute; top: 0; left: 76px; right: 0; height: 1px; background: var(--line);
}
.row .main { flex: 1; min-width: 0; }
.row .line1, .row .line2 { display: flex; align-items: center; gap: 6px; }
.row .title { flex: 1; min-width: 0; font-size: 16px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .time { font-size: 12px; color: var(--muted); flex: none; }
.row .line2 { margin-top: 2px; font-size: 13px; color: var(--muted); }
.row .preview { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .muted-icon { width: 14px; height: 14px; color: var(--muted); flex: none; }
.acct-tag { font-size: 11px; padding: 0 6px; border-radius: 6px; background: var(--chip); color: var(--muted); flex: none; }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 6px; flex: none; font-weight: 500; }
.tag.warn { background: rgba(245, 154, 35, .15); color: var(--warn); }
.tag.danger { background: rgba(250, 81, 81, .12); color: var(--danger); }
.tag.info { background: rgba(61, 123, 250, .12); color: var(--info); }
.tag.ok { background: var(--accent-soft); color: var(--accent); }
.tag.quiet { background: var(--chip); color: var(--muted); }
.tag.pulse { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .45; } }

.badge {
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--danger); color: #fff;
  font-size: 11px; line-height: 18px; text-align: center; font-weight: 600;
}
.avatar-wrap { position: relative; flex: none; }
.avatar-wrap .badge { position: absolute; top: -6px; right: -8px; border: 2px solid var(--surface); height: 20px; line-height: 16px; }
.avatar {
  width: 48px; height: 48px; border-radius: 10px; flex: none; object-fit: cover; background: var(--chip);
  display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 600; font-size: 18px;
}
.avatar.sm { width: 36px; height: 36px; border-radius: 8px; font-size: 14px; }

.section-title { padding: 14px 16px 6px; font-size: 12px; color: var(--muted); }
.empty-note { padding: 40px 24px; text-align: center; color: var(--muted); font-size: 14px; }
.folded-row .avatar { background: var(--chip); color: var(--muted); }

/* ------------------------------------------------------------ chat */

.chat-head {
  padding: calc(var(--safe-t) + 8px) 8px 8px; display: flex; align-items: center; gap: 4px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.chat-head .titles { flex: 1; min-width: 0; text-align: center; }
.wide .chat-head .titles { text-align: left; padding-left: 8px; }
.chat-head .t1 { font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head .t2 { font-size: 11px; color: var(--muted); }
.icon-btn {
  width: 40px; height: 40px; border-radius: 10px; display: flex; align-items: center; justify-content: center;
  color: var(--text); flex: none; position: relative;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg { width: 24px; height: 24px; }
.icon-btn .dot { position: absolute; top: 8px; right: 8px; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.icon-btn .dot.busy { animation: pulse 1s infinite; }

.messages { flex: 1; overflow-y: auto; padding: 12px 12px 4px; overscroll-behavior: contain; }
.older { text-align: center; padding: 6px 0 12px; font-size: 12px; color: var(--muted); }
.older button { color: var(--info); font-size: 12px; }
.time-sep { text-align: center; font-size: 12px; color: var(--muted); margin: 14px 0 8px; }
.sys { text-align: center; margin: 8px 24px; font-size: 12px; color: var(--muted); }
.sys span { background: rgba(127, 127, 127, .12); padding: 3px 8px; border-radius: 6px; display: inline-block; }

.msg { display: flex; gap: 8px; margin: 6px 0; align-items: flex-start; }
.msg.self { flex-direction: row-reverse; }
.msg .body { max-width: min(76%, 560px); display: flex; flex-direction: column; align-items: flex-start; min-width: 0; }
.msg.self .body { align-items: flex-end; }
.msg .sender { font-size: 12px; color: var(--muted); margin: 0 4px 2px; }
.bubble {
  background: var(--other); color: var(--text); padding: 9px 12px; border-radius: 10px;
  white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere; font-size: 15.5px; position: relative;
  user-select: text; -webkit-user-select: text;
}
.msg.self .bubble { background: var(--self); color: var(--self-ink); }
.bubble.media { padding: 0; background: none !important; overflow: hidden; border-radius: 8px; }
.bubble.media img { display: block; max-width: min(62vw, 300px); max-height: 360px; min-width: 80px; min-height: 60px; border-radius: 8px; background: var(--chip); object-fit: cover; }
.media-ph {
  width: 180px; height: 120px; border-radius: 8px; background: var(--chip); color: var(--muted);
  display: flex; align-items: center; justify-content: center; font-size: 13px; text-align: center; padding: 8px;
}
.card {
  background: var(--other); border-radius: 10px; padding: 10px 12px; width: min(66vw, 280px);
  display: flex; flex-direction: column; gap: 4px; color: var(--text); text-align: left;
}
.card .ct { font-size: 15px; font-weight: 500; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card .cd { font-size: 12.5px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.card .cs { font-size: 11px; color: var(--muted); border-top: 1px solid var(--line); padding-top: 6px; margin-top: 4px; }
.card.file { flex-direction: row; align-items: center; gap: 10px; }
.card.file .ficon {
  width: 40px; height: 48px; border-radius: 6px; background: var(--info); color: #fff; font-size: 11px; font-weight: 700;
  display: flex; align-items: center; justify-content: center; flex: none; text-transform: uppercase;
}
.card.pay { background: #fa9d3b; color: #fff; }
.card.pay.done { background: #f8c88f; }
.card.pay .cd, .card.pay .cs { color: rgba(255, 255, 255, .85); border-color: rgba(255, 255, 255, .25); }
.card.video { width: 200px; height: 130px; align-items: center; justify-content: center; background: #22252b; color: #fff; }
.card.video svg { width: 44px; height: 44px; }
.quote {
  margin-top: 4px; font-size: 12.5px; color: var(--muted); background: rgba(127, 127, 127, .12);
  padding: 5px 8px; border-radius: 6px; max-width: 100%; display: flex; gap: 6px; align-items: center;
}
.quote span { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; white-space: pre-wrap; }
.quote img { width: 36px; height: 36px; object-fit: cover; border-radius: 4px; flex: none; }
.voice-text { font-size: 13px; color: var(--muted); margin-top: 4px; white-space: pre-wrap; }
.msg-status { font-size: 11px; color: var(--muted); margin: 3px 4px 0; }
.msg-status.err { color: var(--danger); }
.pending-mark { width: 14px; height: 14px; border: 2px solid var(--muted); border-top-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; align-self: center; flex: none; }
.fail-mark { width: 18px; height: 18px; border-radius: 50%; background: var(--danger); color: #fff; font-size: 12px; font-weight: 700; display: flex; align-items: center; justify-content: center; align-self: center; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }

.new-pill {
  position: absolute; right: 16px; bottom: 12px; background: var(--surface); color: var(--accent);
  border-radius: 999px; padding: 6px 12px; font-size: 13px; box-shadow: var(--shadow);
}

/* ------------------------------------------------------------ composer */

.composer { background: var(--surface); border-top: 1px solid var(--line); padding: 8px 10px calc(8px + var(--safe-b)); }
.composer .bar { display: flex; align-items: flex-end; gap: 6px; }
.composer textarea {
  flex: 1; min-width: 0; resize: none; border: 0; outline: 0; border-radius: 10px; background: var(--chip);
  padding: 9px 12px; max-height: 140px; min-height: 40px; line-height: 1.4; font-size: 16px;
}
.send-btn {
  height: 40px; padding: 0 16px; border-radius: 10px; background: var(--accent); color: var(--accent-ink);
  font-weight: 600; flex: none;
}
.send-btn:disabled { opacity: .45; }
.attach-row { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.attach-chip {
  display: flex; align-items: center; gap: 6px; background: var(--chip); border-radius: 8px; padding: 4px 4px 4px 8px;
  font-size: 12.5px; max-width: 100%;
}
.attach-chip img { width: 28px; height: 28px; object-fit: cover; border-radius: 4px; }
.attach-chip span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 180px; }
.attach-chip button { width: 22px; height: 22px; border-radius: 50%; color: var(--muted); font-size: 16px; line-height: 1; }
.composer-note { font-size: 12px; color: var(--warn); margin-bottom: 6px; }

/* ------------------------------------------------------------ AI */

.ai-panel { background: var(--surface); border-top: 1px solid var(--line); max-height: 62dvh; display: flex; flex-direction: column; }
.wide .ai-panel { max-height: 55dvh; }
.ai-head { display: flex; align-items: center; gap: 8px; padding: 10px 14px 6px; }
.ai-head h3 { margin: 0; font-size: 15px; flex: 1; }
.ai-body { overflow-y: auto; padding: 0 14px 12px; }
.seg { display: inline-flex; background: var(--chip); border-radius: 9px; padding: 2px; }
.seg button { padding: 4px 12px; border-radius: 7px; font-size: 13px; color: var(--muted); }
.seg button.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .1); }
.field { display: flex; gap: 8px; align-items: center; margin: 8px 0; flex-wrap: wrap; }
.field label { font-size: 13px; color: var(--muted); }
select, .input {
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 9px; padding: 7px 10px; outline: 0; min-width: 0;
}
textarea.input { width: 100%; resize: vertical; min-height: 56px; font-size: 15px; }
.btn {
  height: 38px; padding: 0 16px; border-radius: 10px; background: var(--chip); color: var(--text); font-weight: 500;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
.btn.sm { height: 30px; padding: 0 12px; font-size: 13px; border-radius: 8px; }
.ai-status { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 8px; margin: 6px 0; }
.ai-error { font-size: 13px; color: var(--danger); margin: 6px 0; }
.cli-log {
  font: 11.5px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: #0f1115; color: #c8d0da;
  border-radius: 10px; padding: 10px; white-space: pre-wrap; word-break: break-all; max-height: 200px; overflow-y: auto; margin: 6px 0;
}
.reply-list { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.reply {
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px;
}
.reply p { margin: 0 0 8px; white-space: pre-wrap; word-break: break-word; }
.reply .acts { display: flex; gap: 8px; justify-content: flex-end; }
.reply.sent { opacity: .55; }
.ai-meta { font-size: 12px; color: var(--muted); }

/* ------------------------------------------------------------ todo */

.todo { padding: 12px 16px; border-bottom: 1px solid var(--line); display: flex; gap: 12px; }
.todo .main { flex: 1; min-width: 0; }
.todo .t { font-weight: 500; display: flex; gap: 6px; align-items: center; }
.todo .t b { font-weight: 500; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.todo .d { font-size: 13px; color: var(--muted); margin-top: 3px; }
.todo .ai { font-size: 13px; margin-top: 4px; color: var(--text); background: var(--surface-2); border-radius: 8px; padding: 6px 8px; }
.todo .acts { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }

/* ------------------------------------------------------------ me */

.card-list { margin: 12px; background: var(--surface); border-radius: var(--radius); overflow: hidden; }
.item { display: flex; align-items: center; gap: 12px; padding: 13px 16px; }
.item + .item { border-top: 1px solid var(--line); }
.item .k { flex: 1; }
.item .v { color: var(--muted); font-size: 14px; text-align: right; }
.hint { padding: 4px 28px 12px; font-size: 12.5px; color: var(--muted); }

/* ------------------------------------------------------------ overlays */

.mask { position: fixed; inset: 0; background: rgba(0, 0, 0, .38); z-index: 50; display: flex; align-items: flex-end; justify-content: center; animation: fade .15s; }
.mask.center { align-items: center; padding: 24px; }
@keyframes fade { from { opacity: 0; } }
.sheet {
  background: var(--surface); width: 100%; max-width: 520px; border-radius: 16px 16px 0 0; padding: 8px 0 calc(8px + var(--safe-b));
  animation: up .18s ease-out;
}
@keyframes up { from { transform: translateY(30px); opacity: .6; } }
.sheet .st { text-align: center; font-size: 13px; color: var(--muted); padding: 8px 16px 10px; }
.sheet button.opt { display: block; width: 100%; padding: 15px 16px; font-size: 16px; text-align: center; }
.sheet button.opt + button.opt { border-top: 1px solid var(--line); }
.sheet button.opt.danger { color: var(--danger); }
.sheet .gap { height: 8px; background: var(--bg); }
.dialog { background: var(--surface); border-radius: 16px; width: 100%; max-width: 380px; padding: 20px 20px 14px; box-shadow: var(--shadow); }
.dialog h3 { margin: 0 0 10px; font-size: 17px; text-align: center; }
.dialog .txt { font-size: 14px; color: var(--muted); white-space: pre-wrap; }
.dialog .acts { display: flex; gap: 10px; margin-top: 18px; }
.dialog .acts .btn { flex: 1; }

.viewer { position: fixed; inset: 0; background: #000; z-index: 60; display: flex; align-items: center; justify-content: center; }
.viewer img, .viewer video { max-width: 100vw; max-height: 100dvh; object-fit: contain; }
.viewer .close { position: absolute; top: calc(var(--safe-t) + 10px); right: 12px; color: #fff; width: 40px; height: 40px; font-size: 26px; }
.viewer .loading { color: #aaa; position: absolute; }

.toast {
  position: fixed; left: 50%; top: calc(var(--safe-t) + 16px); transform: translateX(-50%); z-index: 70;
  background: rgba(30, 30, 30, .92); color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px;
  max-width: calc(100vw - 32px); box-shadow: var(--shadow); animation: fade .15s;
}

/* ------------------------------------------------------------ login */

.login { min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.login form { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 14px; }
.login .logo { width: 64px; height: 64px; border-radius: 16px; margin: 0 auto 4px; }
.login h1 { text-align: center; margin: 0 0 8px; font-size: 22px; }
.login input[type=password] { padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); outline: 0; font-size: 16px; }
.login input[type=password]:focus { border-color: var(--accent); }
.login .remember { display: flex; gap: 8px; align-items: center; font-size: 14px; color: var(--muted); }
.login .err { color: var(--danger); font-size: 14px; text-align: center; min-height: 20px; }
.login .btn { height: 46px; font-size: 16px; }

.spinner { width: 18px; height: 18px; border: 2px solid currentColor; border-top-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
.center-fill { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--muted); }
