:root {
  --bg: #f4f6f8;
  --bg-elev: #ffffff;
  --panel: #ffffff;
  --line: #e3e7ec;
  --text: #16191d;
  --muted: #6b7480;
  --brand: #2b6cff;
  --brand-soft: #e7efff;
  --bubble-in: #ffffff;
  --bubble-out: #d8e6ff;
  --ok: #18a058;
  --warn: #e8890c;
  --err: #e0434f;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .08);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --bg-elev: #171b21;
    --panel: #171b21;
    --line: #262c34;
    --text: #e7ecf2;
    --muted: #8a94a3;
    --brand: #4d86ff;
    --brand-soft: #1b2740;
    --bubble-in: #1c222a;
    --bubble-out: #24406e;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; color: inherit; cursor: pointer; }

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  height: 100vh;
}

@supports (height: 100dvh) {
  .app { height: 100dvh; }
}

/* ------------------------------ 顶栏 ------------------------------ */

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(8px + env(safe-area-inset-top)) 10px 8px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--line);
  position: relative;
  z-index: 5;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: transparent;
  font-size: 16px;
  display: grid;
  place-items: center;
}

.icon-btn:active { background: var(--brand-soft); }

.title-wrap { flex: 1; min-width: 0; }
.room-line { display: flex; align-items: center; gap: 8px; }
.room-code {
  font-weight: 700;
  letter-spacing: .06em;
  font-size: 16px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chip {
  border: 1px solid var(--line);
  background: transparent;
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 12px;
  color: var(--muted);
}
.state-line { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.state-line .sep { opacity: .5; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.ok { background: var(--ok); }
.dot.warn { background: var(--warn); }
.dot.err { background: var(--err); }

/* ------------------------------ 聊天区 ------------------------------ */

.chat {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 10px 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
/* 底部留白用真实占位元素，不能用 padding-bottom：
   Chrome 不把 flex 滚动容器的底部 padding 计入 scrollHeight，
   会导致 scrollTop 到底时最后一条消息仍被留白挡在视口外。 */
.chat::after {
  content: '';
  display: block;
  flex: 0 0 auto;
  height: 16px;
}

.empty { text-align: center; color: var(--muted); padding: 40px 20px; }
.empty h1 { font-size: 20px; margin: 0 0 8px; color: var(--text); }
.empty .muted { font-size: 12px; opacity: .8; margin-top: 14px; word-break: break-all; }
.empty.hidden { display: none; }

.row { display: flex; gap: 8px; align-items: flex-start; max-width: 100%; }
.row.mine { flex-direction: row-reverse; }
.row.sys-row { justify-content: center; }
.sys {
  font-size: 12px;
  color: var(--muted);
  background: rgba(127, 140, 160, .12);
  padding: 3px 10px;
  border-radius: 999px;
  max-width: 90%;
  text-align: center;
}

.avatar {
  width: 34px;
  height: 34px;
  flex: 0 0 34px;
  border-radius: 11px;
  background: var(--brand-soft);
  color: var(--brand);
  font-weight: 700;
  display: grid;
  place-items: center;
  font-size: 14px;
}
.avatar.sm { width: 26px; height: 26px; flex-basis: 26px; border-radius: 8px; font-size: 12px; }

.col { display: flex; flex-direction: column; gap: 4px; max-width: min(78%, 520px); min-width: 0; }
.row.mine .col { align-items: flex-end; }
.head { font-size: 12px; color: var(--muted); padding: 0 4px; }
.foot { font-size: 11px; color: var(--muted); padding: 0 4px; opacity: .75; }

.bubble {
  background: var(--bubble-in);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  max-width: 100%;
}
.row.mine .bubble { background: var(--bubble-out); border-color: transparent; }

.bubble.text {
  padding: 9px 12px;
  white-space: pre-wrap;
  word-break: break-word;
  border-radius: var(--radius);
  border-bottom-left-radius: 6px;
}
.row.mine .bubble.text { border-bottom-left-radius: var(--radius); border-bottom-right-radius: 6px; }

/* 文件卡片 */
.bubble.file { width: min(100%, 340px); padding: 0; }
.thumb {
  width: 100%;
  max-height: 240px;
  overflow: hidden;
  background: rgba(0, 0, 0, .04);
  display: grid;
  place-items: center;
  cursor: zoom-in;
}
.thumb img, .thumb video { display: block; width: 100%; max-height: 240px; object-fit: cover; background: #000; }
.thumb.placeholder { height: 120px; font-size: 30px; color: var(--muted); }
.thumb-ph { font-size: 12px; color: var(--muted); padding: 16px; }

.file-info { padding: 8px 10px 4px; }
.file-name { font-size: 14px; word-break: break-all; line-height: 1.35; }
.file-sub { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; font-size: 12px; color: var(--muted); }
.tag {
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 999px;
  background: rgba(127, 140, 160, .16);
  color: var(--muted);
  white-space: nowrap;
}
.tag.ok { background: rgba(24, 160, 88, .15); color: var(--ok); }
.tag.warn { background: rgba(232, 137, 12, .16); color: var(--warn); }

.bar { height: 3px; background: rgba(127, 140, 160, .2); overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--brand); transition: width .25s ease; }
.bar.error i { background: var(--err); }
.bar.wait i { background: var(--warn); }
.bar.hidden { display: none; }

.acts { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 10px; }
.mini {
  border: 1px solid var(--line);
  background: var(--bg-elev);
  border-radius: 9px;
  padding: 5px 10px;
  font-size: 12.5px;
}
.mini.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.mini.warn { color: var(--warn); border-color: currentColor; }
.mini.danger { color: var(--err); border-color: currentColor; }
.mini:active { transform: translateY(1px); }
.hint { font-size: 11.5px; color: var(--muted); padding: 0 10px 8px; min-height: 16px; }

/* ------------------------------ 传输面板 ------------------------------ */

.panel {
  background: var(--panel);
  border-top: 1px solid var(--line);
  max-height: 40vh;
  overflow-y: auto;
  padding: 8px 10px calc(8px);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.panel.hidden { display: none; }
.panel-head { font-size: 12px; color: var(--muted); }

.tf { border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; }
.tf-top { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; }
.tf-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tf-pct { color: var(--brand); font-variant-numeric: tabular-nums; }
.tf-bar { height: 4px; background: rgba(127, 140, 160, .2); border-radius: 4px; overflow: hidden; margin: 6px 0; }
.tf-bar i { display: block; height: 100%; background: var(--brand); transition: width .3s ease; }
.tf-waiting .tf-bar i { background: var(--warn); }
.tf-error .tf-bar i { background: var(--err); }
.tf-sub { display: flex; flex-wrap: wrap; gap: 8px; font-size: 11.5px; color: var(--muted); }
.tf-sub .err { color: var(--err); }

/* ------------------------------ 输入区 ------------------------------ */

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  background: var(--bg-elev);
  border-top: 1px solid var(--line);
}

.round {
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: transparent;
  font-size: 20px;
  display: grid;
  place-items: center;
}

#input {
  flex: 1;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--bg);
  color: var(--text);
  padding: 9px 12px;
  font: inherit;
  resize: none;
  max-height: 120px;
  min-height: 40px;
  outline: none;
}
#input:focus { border-color: var(--brand); }

.send {
  border: none;
  background: var(--brand);
  color: #fff;
  border-radius: 14px;
  padding: 0 16px;
  height: 40px;
  font-weight: 600;
}
.send:active { opacity: .85; }

/* ------------------------------ 弹层 ------------------------------ */

.sheet-wrap, .drawer-wrap {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
}
.sheet-wrap.hidden, .drawer-wrap.hidden { display: none; }
.mask { flex: 1; background: rgba(0, 0, 0, .42); }

.sheet {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg-elev);
  border-radius: 20px 20px 0 0;
  padding: 10px 12px calc(14px + env(safe-area-inset-bottom));
  animation: up .22s ease;
}
@keyframes up { from { transform: translateY(20px); opacity: .6; } }
.sheet-head { text-align: center; color: var(--muted); font-size: 12.5px; padding: 4px 0 10px; }
.sheet-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
  margin-bottom: 8px;
}
.sheet-item b { display: block; font-weight: 600; }
.sheet-item em { display: block; font-style: normal; font-size: 12px; color: var(--muted); margin-top: 2px; }
.sheet-item .si-icon { font-size: 18px; color: var(--brand); width: 24px; text-align: center; }
.sheet-item.danger { justify-content: center; color: var(--muted); }

.drawer-body {
  width: min(88vw, 420px);
  background: var(--bg-elev);
  border-left: 1px solid var(--line);
  height: 100%;
  overflow-y: auto;
  padding: calc(10px + env(safe-area-inset-top)) 12px calc(20px + env(safe-area-inset-bottom));
  animation: left .24s ease;
}
@keyframes left { from { transform: translateX(24px); opacity: .6; } }

.tabs { display: flex; gap: 6px; margin-bottom: 12px; }
.tab { flex: 1; border: 1px solid var(--line); background: transparent; border-radius: 10px; padding: 7px 0; font-size: 13px; }
.tab.on { background: var(--brand); border-color: var(--brand); color: #fff; }

.card {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
  margin-bottom: 10px;
  background: var(--panel);
}
.card.tip { background: var(--brand-soft); border-color: transparent; }
.card.danger-card { border-color: color-mix(in srgb, var(--err) 40%, transparent); }
.card.danger-card .label { color: var(--err); }
.label { font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.big-code { font-size: 22px; font-weight: 700; letter-spacing: .1em; margin-bottom: 10px; word-break: break-all; }
.field {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  padding: 9px 10px;
  font: inherit;
  outline: none;
}
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.btn {
  border: 1px solid var(--brand);
  background: var(--brand);
  color: #fff;
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13.5px;
}
.btn.ghost { background: transparent; color: var(--brand); }
.btn.danger { background: transparent; color: var(--err); border-color: currentColor; }
.btn.tiny { padding: 4px 8px; font-size: 12px; }
.list-btn {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  border-bottom: 1px solid var(--line);
  background: transparent;
  padding: 9px 2px;
  font-size: 14px;
}
.list-btn:last-child { border-bottom: none; }

.peer { display: flex; align-items: center; gap: 8px; padding: 7px 0; border-bottom: 1px dashed var(--line); }
.peer:last-child { border-bottom: none; }
.peer-name { flex: 1; }

.usage { height: 8px; background: rgba(127, 140, 160, .2); border-radius: 8px; overflow: hidden; }
.usage i { display: block; height: 100%; background: var(--brand); }

.file-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 6px 8px;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px dashed var(--line);
  font-size: 13px;
}
.file-row .row-btns { grid-column: 1 / -1; margin-top: 0; }
.fr-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fr-size { color: var(--muted); font-size: 12px; }

/* ------------------------------ 预览 ------------------------------ */

.viewer {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(0, 0, 0, .94);
  display: flex;
  align-items: center;
  justify-content: center;
}
.viewer.hidden { display: none; }
.viewer-inner { width: 100%; height: 100%; display: grid; place-items: center; position: relative; }
.viewer-inner img, .viewer-inner video { max-width: 100%; max-height: 100%; }
.viewer-bar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: env(safe-area-inset-bottom);
  padding: 10px 14px;
  color: #eee;
  font-size: 12px;
  background: linear-gradient(transparent, rgba(0, 0, 0, .6));
  text-align: center;
}
.viewer-close {
  position: absolute;
  top: calc(10px + env(safe-area-inset-top));
  right: 12px;
  z-index: 2;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, .16);
  color: #fff;
  font-size: 16px;
}

/* ------------------------------ 提示 ------------------------------ */

.toast-wrap {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(80px + env(safe-area-inset-bottom));
  z-index: 80;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}
.toast {
  background: rgba(20, 24, 30, .92);
  color: #fff;
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13px;
  max-width: 90vw;
  text-align: center;
  transition: opacity .3s, transform .3s;
  word-break: break-all;
}
.toast.t-ok { background: rgba(24, 160, 88, .94); }
.toast.t-warn { background: rgba(214, 122, 8, .94); }
.toast.t-err { background: rgba(224, 67, 79, .94); }
.toast.out { opacity: 0; transform: translateY(6px); }

/* 横屏 / 大屏微调 */
@media (min-width: 720px) {
  .chat { padding: 16px 20px; }
  .col { max-width: min(62%, 560px); }
}

/* ------------------------------ 二维码邀请 ------------------------------ */

.qr-modal {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: flex;
  align-items: center;
  justify-content: center;
}
.qr-modal.hidden { display: none; }
.qr-card {
  position: relative;
  width: min(86vw, 340px);
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 18px 16px 16px;
  box-shadow: var(--shadow);
  animation: up .22s ease;
  margin: 0 12px;
}
.qr-card-head { text-align: center; font-weight: 600; font-size: 15px; margin-bottom: 14px; }
.qr-box {
  display: grid;
  place-items: center;
  background: #fff;
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 12px;
}
.qr-box svg { width: 100%; height: auto; max-width: 260px; display: block; }
.qr-link {
  font-size: 12px;
  color: var(--muted);
  word-break: break-all;
  text-align: center;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  margin-bottom: 12px;
}
.qr-tip { font-size: 12px; color: var(--muted); text-align: center; margin-top: 10px; }

/* ============================ 激活门禁 ============================ */
.gate {
  position: fixed;
  inset: 0;
  z-index: 900;
  display: grid;
  place-items: center;
  padding: 24px 18px;
  background: var(--bg);
  overflow-y: auto;
}
.gate-card {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 26px 22px 20px;
  box-shadow: var(--shadow);
}
.gate-brand { text-align: center; margin-bottom: 22px; }
.gate-logo {
  width: 52px; height: 52px; margin: 0 auto 12px;
  display: grid; place-items: center;
  font-size: 26px; color: #fff; border-radius: 14px;
  background: linear-gradient(135deg, var(--brand), #6d9bff);
}
.gate-brand h1 { font-size: 19px; margin: 0 0 6px; color: var(--text); }
.gate-brand p { font-size: 13px; color: var(--muted); margin: 0; }
.gate-field { display: block; margin-bottom: 6px; }
.gate-field span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 7px; }
.gate-field input {
  width: 100%;
  box-sizing: border-box;
  font-size: 19px;
  letter-spacing: .16em;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  text-align: center;
  padding: 14px 10px;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition: border-color .15s;
}
.gate-field input:focus { border-color: var(--brand); }
.gate-err {
  min-height: 20px;
  font-size: 12.5px;
  color: var(--err);
  text-align: center;
  margin: 8px 0 4px;
  opacity: 0;
  transition: opacity .15s;
}
.gate-err.on { opacity: 1; }
.gate-btn {
  width: 100%;
  padding: 14px;
  font-size: 15px;
  font-weight: 600;
  color: #fff;
  background: var(--brand);
  border: 0;
  border-radius: 12px;
  cursor: pointer;
  margin-top: 4px;
}
.gate-btn:disabled { opacity: .6; cursor: default; }
.gate-foot {
  display: flex;
  flex-direction: column;
  gap: 4px;
  text-align: center;
  font-size: 11.5px;
  color: var(--muted);
  margin-top: 16px;
}
@keyframes gateShake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-7px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(3px); }
}
.gate-card.shake { animation: gateShake .38s ease-in-out; }

/* 顶栏授权剩余天数 */
.lic-chip {
  background: var(--brand-soft);
  color: var(--brand);
  border-color: transparent;
  font-variant-numeric: tabular-nums;
}
.lic-chip.warn { background: color-mix(in srgb, var(--warn) 16%, transparent); color: var(--warn); }

/* 键值行（授权信息） */
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 5px 0; font-size: 13px; }
.kv-k { color: var(--muted); flex: 0 0 auto; }
.kv-v { color: var(--text); text-align: right; word-break: break-all; font-variant-numeric: tabular-nums; }
.kv-v.warn { color: var(--warn); }
