:root {
  --bg: #f4f5f8;
  --panel: #ffffff;
  --panel-2: #f0f1f5;
  --line: #e3e5eb;
  --text: #171923;
  --muted: #575f70;
  --accent: #6c5cff;
  --accent-2: #ff6b8b;
  --accent-soft: #ecebff;
  --mine: #5847ce;
  --mine-text: #ffffff;
  --theirs: #ffffff;
  --danger: #ad2940;
  --ok: #197042;
  --countdown-alert: #a72036;
  --countdown-bg: #fff0f1;
  --action: #5948d9;
  --destructive: #a7243d;
  --radius: 14px;
  --shadow: 0 8px 30px rgba(20, 20, 40, .12);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --line: #2a2f3a;
  --text: #e8eaf0;
  --muted: #abb2c2;
  --accent: #8b7dff;
  --accent-soft: #262344;
  --mine: #5847ce;
  --countdown-alert: #ffb6c1;
  --countdown-bg: #512333;
  --action: #6551de;
  --destructive: #ad2940;
  --theirs: #232834;
  --ok: #82d5a7;
  --danger: #ffa9b4;
  --shadow: 0 8px 30px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------- 按钮 / 输入 ---------- */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--panel); color: var(--text);
  padding: 10px 16px; border-radius: 10px; font: inherit; font-weight: 500; cursor: pointer;
  transition: transform .06s, background .15s, border-color .15s;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--action); border-color: var(--action); color: #fff; }
.btn.primary:hover { filter: brightness(.94); }
.btn.danger { background: var(--destructive); border-color: var(--destructive); color: #fff; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--text); }
.btn.danger.ghost { color: var(--danger); background: transparent; border-color: transparent; }
.btn.sm { padding: 6px 12px; font-size: 13px; }
.btn.block { width: 100%; }
.icon-btn {
  display: inline-grid; place-items: center; width: 38px; height: 38px; flex: none;
  border: 0; border-radius: 10px; background: transparent; color: var(--text); font-size: 18px; cursor: pointer;
}
.icon-btn:hover { background: var(--panel-2); }
.field {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--panel-2); color: var(--text); font: inherit; outline: none;
}
.field:focus { border-color: var(--accent); }
textarea.field { resize: none; word-break: break-all; font-size: 13px; }

/* ---------- 布局 ---------- */
.app { display: grid; grid-template-columns: 300px 1fr; height: 100dvh; }
.sidebar { display: flex; flex-direction: column; background: var(--panel); border-right: 1px solid var(--line); min-height: 0; }
.main { position: relative; min-width: 0; min-height: 0; display: flex; flex-direction: column; }

.brand { display: flex; align-items: center; justify-content: space-between; padding: 16px; border-bottom: 1px solid var(--line); }
.logo { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; }
.logo small { color: var(--muted); font-weight: 400; font-size: 13px; }

.contact-list { list-style: none; margin: 0; padding: 8px; overflow-y: auto; flex: 1; }
.contact-empty { color: var(--muted); text-align: center; padding: 40px 16px; font-size: 14px; flex: 1; }
.contact { display: flex; align-items: center; gap: 12px; padding: 10px; border-radius: 12px; cursor: pointer; outline: none; }
.contact:hover, .contact:focus-visible { background: var(--panel-2); }
.contact.active { background: var(--accent-soft); }
.contact.closed .c-name { color: var(--muted); }
.avatar {
  width: 40px; height: 40px; flex: none; border-radius: 50%; display: grid; place-items: center;
  color: #fff; font-weight: 600; background: hsl(var(--h, 250) 55% 28%);
}
.c-body { min-width: 0; flex: 1; }
.c-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.c-state { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.unread { background: var(--accent-2); color: #211222; font-size: 12px; font-weight: 600; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; display: grid; place-items: center; }

.side-foot { padding: 12px 16px; border-top: 1px solid var(--line); display: grid; gap: 8px; }
.badge-row { display: flex; flex-wrap: wrap; gap: 6px 10px; font-size: 12px; color: var(--muted); }

/* ---------- 欢迎页 ---------- */
.welcome { margin: auto; max-width: 560px; padding: 32px 24px; text-align: center; overflow-y: auto; }
.welcome h1 { margin: 12px 0 8px; font-size: 28px; }
.lead { color: var(--muted); margin: 0 0 24px; }
.actions { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; margin-bottom: 28px; }
.features { list-style: none; padding: 0; margin: 0 0 20px; display: grid; grid-template-columns: 1fr 1fr; gap: 12px; text-align: left; }
.features li { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; font-size: 13px; color: var(--muted); }
.features b { display: block; color: var(--text); font-size: 14px; margin-bottom: 4px; }
.tip { font-size: 13px; color: var(--muted); }

/* ---------- 聊天 ---------- */
.conv { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.conv-head { display: flex; align-items: center; gap: 6px; padding: 10px 12px; background: var(--panel); border-bottom: 1px solid var(--line); }
.conv-head .back { display: none; font-size: 28px; }
.conv-title { flex: 1; min-width: 0; padding-left: 4px; }
.conv-name { all: unset; cursor: pointer; font-weight: 600; font-size: 16px; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.conv-status { font-size: 12px; color: var(--muted); }
.conv-status.ready { color: var(--ok); }
.ttl { display: flex; align-items: center; gap: 2px; background: var(--panel-2); border-radius: 10px; padding: 0 4px 0 8px; height: 34px; font-size: 13px; }
.ttl select { border: 0; background: transparent; color: var(--text); font: inherit; outline: none; cursor: pointer; padding: 4px; }

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; overscroll-behavior: contain; }
.msg { display: flex; animation: pop .18s ease-out; }
.msg.mine { justify-content: flex-end; }
.bubble { max-width: min(78%, 560px); padding: 8px 12px 6px; border-radius: 16px; background: var(--theirs); border: 1px solid var(--line); box-shadow: 0 1px 2px rgba(0,0,0,.04); }
.mine .bubble { background: var(--mine); color: var(--mine-text); border-color: transparent; border-bottom-right-radius: 6px; }
.theirs .bubble { border-bottom-left-radius: 6px; }
.text { white-space: pre-wrap; word-break: break-word; }
.pic { display: block; max-width: 100%; max-height: 320px; border-radius: 10px; margin: 2px 0 4px; cursor: zoom-in; }
.picture-button { display: block; border: 0; padding: 0; border-radius: 10px; background: transparent; cursor: zoom-in; }
.lightbox { inset: 0; margin: 0; padding: 18px; border: 0; max-width: none; max-height: none; width: 100%; height: 100%; background: rgba(10, 10, 20, .94); cursor: zoom-out; }
.lightbox[open] { display: grid; grid-template-rows: auto minmax(0, 1fr); gap: 12px; }
.lightbox button { justify-self: end; }
.lightbox img { place-self: center; max-width: 100%; max-height: 100%; object-fit: contain; }
.meta { display: flex; gap: 8px; justify-content: flex-end; font-size: 11px; opacity: .72; margin-top: 2px; white-space: nowrap; }
.meta .status.failed { color: #ffd2d4; text-decoration: underline; cursor: pointer; opacity: 1; }
.meta .cd { font-variant-numeric: tabular-nums; font-weight: 600; }
.sys { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel-2); padding: 4px 12px; border-radius: 20px; margin: 6px 0; text-align: center; }
.burning { animation: burn .45s ease-in forwards; }

.conv-notice { display: flex; align-items: center; justify-content: center; gap: 12px; flex-wrap: wrap; padding: 10px 16px; font-size: 13px; color: var(--muted); background: var(--accent-soft); }
.composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); background: var(--panel); border-top: 1px solid var(--line); }
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--line); border-radius: 12px; padding: 9px 12px; max-height: 160px;
  background: var(--panel-2); color: var(--text); font: inherit; outline: none; line-height: 1.45;
}
.composer textarea:focus { border-color: var(--accent); }
.composer .attach { cursor: pointer; }
.composer .send { height: 40px; }
.composer:has(textarea:disabled) .attach { opacity: .4; pointer-events: none; }

/* ---------- 对话框 ---------- */
.dlg { border: 0; padding: 0; border-radius: 18px; background: var(--panel); color: var(--text); box-shadow: var(--shadow); width: min(460px, calc(100vw - 32px)); }
.dlg::backdrop { background: rgba(10, 10, 20, .5); backdrop-filter: blur(3px); }
.dlg-body { padding: 22px; display: grid; gap: 14px; }
.dlg h2 { margin: 0; font-size: 19px; }
.dlg h3 { margin: 0 0 4px; font-size: 15px; }
.dlg p { margin: 0; }
.dlg section { display: grid; gap: 10px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row .btn { flex: 1; }
.safety-code { font-size: 22px; letter-spacing: 2px; text-align: center; background: var(--panel-2); border-radius: 12px; padding: 16px; margin: 0; word-spacing: 6px; }

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); z-index: 99;
  background: #222633; color: #fff; padding: 10px 18px; border-radius: 22px; font-size: 14px; box-shadow: var(--shadow);
  animation: pop .18s ease-out; max-width: calc(100vw - 32px);
}

@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(.98); } }
@keyframes burn {
  40% { filter: blur(1px) brightness(1.4); }
  to { opacity: 0; filter: blur(8px); transform: scale(.92) translateY(-6px); }
}

.pending-card { margin: auto; width: min(410px, calc(100% - 32px)); overflow-y: auto; padding: clamp(18px, 4vw, 32px); border: 1px solid var(--line); border-top: 3px solid var(--accent); border-radius: var(--radius); background: var(--panel); }
.pending-card h2 { margin: 0; font-size: 21px; }
.pending-card ol { margin: 18px 0 24px; padding-left: 1.5em; color: var(--muted); }
.pending-card li + li { margin-top: 9px; }
.pending-card li::marker { color: var(--accent); font-weight: 650; }
.pending-card strong { color: var(--text); }
.invite-qr { margin: 0; padding: 12px; display: grid; justify-items: center; gap: 8px; border-radius: var(--radius); background: var(--panel-2); font-size: 13px; }
.invite-qr canvas { width: min(200px, 100%); height: auto; image-rendering: pixelated; }
#copy-feedback { font-size: 13px; color: var(--muted); }

.session-hint { display: flex; align-items: center; gap: 14px; justify-content: space-between; padding: 10px 16px; color: var(--text); background: var(--accent-soft); font-size: 13px; }
.session-hint button { flex: none; }

body { display: flex; flex-direction: column; }
.app { height: auto; flex: 1; min-height: 0; }
.network-notice { padding: 8px 16px; text-align: center; background: var(--panel-2); color: var(--text); border-bottom: 2px solid var(--danger); font-size: 13px; }

.msg.group-prev { margin-top: -4px; }
.mine.group-prev .bubble { border-top-right-radius: 5px; }
.mine.group-next .bubble { border-bottom-right-radius: 5px; }
.theirs.group-prev .bubble { border-top-left-radius: 5px; }
.theirs.group-next .bubble { border-bottom-left-radius: 5px; }
.group-next .time { display: none; }
.time-divider { align-self: center; margin: 12px 0 6px; color: var(--muted); font-size: 12px; }

.conv { position: relative; }
.conv.drop-target::after { content: "松开发送图片"; position: absolute; inset: 6px; display: grid; place-items: center; pointer-events: none; border: 2px dashed var(--accent); border-radius: var(--radius); background: var(--accent-soft); color: var(--text); font-weight: 600; }
.image-placeholder { display: block; padding: 16px; }

.meta { align-items: center; opacity: 1; }
.cd { display: inline-flex; align-items: center; gap: 4px; border-radius: 6px; padding: 1px 4px; }
.cd::before { content: ""; width: 10px; height: 10px; flex: none; border-radius: 50%; background: conic-gradient(currentColor var(--left), transparent 0); box-shadow: inset 0 0 0 1px currentColor; mask-image: radial-gradient(circle, transparent 37%, black 43%); }
.cd.urgent { color: var(--countdown-alert); background: var(--countdown-bg); }

.meta { flex-wrap: wrap; gap: 2px 7px; }
.ttl > span { color: var(--muted); font-size: 12px; }

.setting { display: flex; gap: 18px; align-items: center; justify-content: space-between; padding: 12px 0; border-bottom: 1px solid var(--line); }
.setting small { display: block; color: var(--muted); }
.setting input { width: 20px; height: 20px; accent-color: var(--accent); }
.typing-notice { padding: 4px 16px; font-size: 12px; color: var(--muted); }

.app { grid-template-columns: 268px minmax(0, 1fr); }
.brand { padding: 22px 18px; }
.logo { letter-spacing: -.04em; font-size: 23px; }
.logo small { letter-spacing: .06em; }
.list-heading { padding: 22px 18px 6px; display: flex; align-items: center; gap: 8px; color: var(--muted); }
.list-heading h2 { font-size: 13px; margin: 0; }
.list-heading span { font-size: 11px; font-variant-numeric: tabular-nums; background: var(--panel-2); padding: 0 6px; border-radius: 4px; }
.contact-list:empty { display: none; }
.contact-empty { text-align: left; padding: 8px 18px; }
.side-foot .btn { text-align: left; }
.welcome { margin: auto; width: min(100%, 740px); max-width: none; text-align: left; padding: clamp(22px, 4vw, 50px); }
.welcome-intro { position: relative; isolation: isolate; padding: clamp(24px, 4vw, 44px); overflow: hidden; background: var(--accent-soft); border-radius: 22px; }
.welcome-intro::after { content: ""; position: absolute; z-index: -1; right: -95px; top: -120px; width: 310px; height: 310px; border: 1px solid var(--accent); border-radius: 50%; box-shadow: 0 0 0 26px var(--accent-soft), 0 0 0 27px var(--accent), 0 0 0 55px var(--accent-soft), 0 0 0 56px var(--accent); opacity: .14; }
.welcome-mark img { display: block; width: 48px; height: 48px; }
.welcome h1 { margin: 22px 0 10px; font-size: clamp(28px, 3.4vw, 40px); line-height: 1.2; letter-spacing: -.045em; }
.lead { margin-bottom: 24px; line-height: 1.75; color: var(--text); }
.actions { justify-content: flex-start; margin: 0; gap: 8px; }
.actions .btn { font-weight: 600; }
.actions .primary span { margin-left: 5px; }
.features { margin: 24px 0 12px; gap: 0 24px; }
.features li { padding: 14px 0; border: 0; border-bottom: 1px solid var(--line); border-radius: 0; background: transparent; }
.features b { margin-bottom: 4px; }
.welcome .tip { margin: 16px 0 0; }
@media (max-width: 760px) {
  body:not(.show-chat) .app:has(#contact-list:empty) { grid-template-rows: auto minmax(0, 1fr) auto; }
  .app:has(#contact-list:empty) .sidebar { display: contents; }
  .app:has(#contact-list:empty) .brand { grid-row: 1; padding: 12px 18px; background: var(--panel); }
  .app:has(#contact-list:empty) :is(#btn-new, .list-heading, .contact-empty, #btn-wipe) { display: none; }
  .app:has(#contact-list:empty) .main { display: flex; grid-row: 2; }
  .app:has(#contact-list:empty) .side-foot { grid-row: 3; padding: 6px 12px max(6px, env(safe-area-inset-bottom)); background: var(--panel); }
  .welcome { padding: 16px; }
  .welcome-intro { padding: 24px 18px; }
  .welcome .features { grid-template-columns: 1fr 1fr; gap: 0 16px; margin-top: 8px; }
  .wide-only { display: none; }
}

@media (max-width: 760px) {
  body { position: fixed; inset: var(--viewport-top, 0px) 0 auto; height: var(--viewport-height, 100dvh); }
  .composer textarea { min-width: 0; font-size: 16px; }
  .dlg .field { font-size: 16px; }
  .conv-head, .composer { flex-shrink: 0; }
}

.setting select { padding: 6px; font: inherit; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); color: var(--text); }
::placeholder { color: var(--muted); opacity: 1; }

.symbols { position: absolute; width: 0; height: 0; overflow: hidden; }
.icon { width: 19px; height: 19px; flex: none; vertical-align: middle; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
#btn-new, .side-foot .btn { display: inline-flex; align-items: center; gap: 6px; }
#btn-new .icon, .side-foot .icon { width: 16px; height: 16px; }
.contact { width: 100%; text-align: left; background: transparent; border: 0; font: inherit; color: var(--text); }
.conv-name { display: flex; gap: 6px; align-items: center; }
#contact-name { text-overflow: ellipsis; white-space: nowrap; overflow: hidden; }
.conv-name .icon { width: 13px; height: 13px; color: var(--muted); }
.conv-status.ready::before { content: ""; display: inline-block; width: 7px; height: 7px; background: var(--ok); border-radius: 50%; margin-right: 5px; }
.status.failed { border: 0; background: transparent; font: inherit; padding: 0; }
:where(button, input, textarea, select, .contact):focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.field:focus-visible, .conv-name:focus-visible, .ttl select:focus-visible, .composer textarea:focus-visible, .contact:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.icon-btn { width: 42px; height: 42px; }
.icon-btn:disabled { opacity: .45; cursor: default; }

.mode-picker { display: flex; padding: 4px; border-radius: 11px; background: var(--panel-2); }
.mode-picker button { flex: 1; border: 0; border-radius: 8px; padding: 8px; background: transparent; color: var(--muted); font: inherit; cursor: pointer; }
.mode-picker button[aria-pressed=true] { background: var(--panel); color: var(--text); font-weight: 600; box-shadow: 0 1px 4px rgba(20,20,40,.12); }

/* ---------- 口令连接 ---------- */
.pass-row { display: flex; gap: 8px; align-items: center; }
.pass-row .field { flex: 1; min-width: 0; }
.pass-row .btn { flex: none; }
.pass-feedback { display: grid; gap: 2px; margin: 0; padding: 8px 10px; border-radius: 9px; background: var(--panel-2); color: var(--muted); border-left: 3px solid var(--line); }
.pass-feedback.good { border-left-color: var(--ok); }
.pass-feedback.fair { border-left-color: var(--accent); }
.pass-feedback.bad { border-left-color: var(--danger); color: var(--danger); }
.pass-norm { color: var(--text); font-weight: 600; word-break: break-all; }
.pairing-spinner {
  width: 40px; height: 40px; margin: 4px auto; border-radius: 50%;
  border: 3px solid var(--accent-soft); border-top-color: var(--accent); animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- 移动端 ---------- */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .main { display: none; }
  body.show-chat .sidebar { display: none; }
  body.show-chat .main { display: flex; }
  .conv-head .back { display: inline-grid; }
  .features { grid-template-columns: 1fr; }
  .ttl { height: auto; flex-direction: column; gap: 0; padding: 2px 5px; }
  .ttl span { font-size: 10px; color: var(--muted); }
  .ttl select { padding: 0; }
  .bubble { max-width: 85%; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
