:root {
  --bg: #f5f5f7;
  --bg-elev: #ffffff;
  --line: #e4e4ea;
  --text: #14161a;
  --text-dim: #8a8f98;
  --accent: #2f6df6;
  --accent-soft: #e8efff;
  --bubble-in: #ffffff;
  --bubble-out: #2f6df6;
  --bubble-out-text: #ffffff;
  --danger: #d64545;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e0f12;
    --bg-elev: #17191e;
    --line: #262a32;
    --text: #eceef2;
    --text-dim: #868d98;
    --accent: #4c86ff;
    --accent-soft: #1b2436;
    --bubble-in: #1d2027;
    --bubble-out: #3b6ef0;
    --bubble-out-text: #ffffff;
  }
}

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

html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro SC", "PingFang SC", "Helvetica Neue", sans-serif;
  overscroll-behavior: none;
}

.screen {
  position: fixed; inset: 0; display: none; flex-direction: column;
  background: var(--bg);
}
.screen.active { display: flex; }

/* ---------------- 图形密码 ---------------- */
.lock-inner {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: calc(var(--safe-top) + 24px) 24px calc(var(--safe-bottom) + 24px);
  gap: 28px;
}
.lock-head { text-align: center; }
.lock-head h1 { font-size: 19px; font-weight: 600; margin: 0 0 6px; letter-spacing: .3px; }
.sub { margin: 0; color: var(--text-dim); font-size: 14px; }

.pad {
  position: relative;
  display: grid;
  grid-template-columns: repeat(4, 64px);
  grid-template-rows: repeat(4, 64px);
  gap: 18px;
  touch-action: none;
}
.pad .dot {
  border-radius: 50%;
  background: var(--line);
  position: relative;
  transition: transform .12s ease, background .12s ease;
}
.pad .dot.on {
  background: var(--accent);
  transform: scale(1.12);
  box-shadow: 0 0 0 6px var(--accent-soft);
}
.pad .dot.bad { background: var(--danger); }
.pad.shake { animation: shake .32s; }
@keyframes shake {
  0%,100% { transform: translateX(0); }
  25% { transform: translateX(-7px); }
  75% { transform: translateX(7px); }
}

.lock-msg { min-height: 22px; font-size: 14px; color: var(--danger); text-align: center; }
.lock-foot { display: flex; gap: 26px; }
.linkbtn {
  background: none; border: 0; color: var(--text-dim);
  font-size: 14px; padding: 8px;
}
.linkbtn:active { color: var(--accent); }

/* ---------------- 顶栏 ---------------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(var(--safe-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg-elev) 86%, transparent);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--line);
  position: relative; z-index: 5;
}
.peer { display: flex; align-items: baseline; gap: 8px; }
.peer #peerName { font-weight: 600; font-size: 16px; }
.status { font-size: 12px; color: var(--text-dim); }
.status.on { color: #2aa56b; }

.iconbtn {
  background: none; border: 0; color: var(--text);
  font-size: 20px; width: 40px; height: 40px; border-radius: 20px;
  display: grid; place-items: center;
}
.iconbtn:active { background: var(--line); }

/* ---------------- 消息列表 ---------------- */
.list {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 16px 14px 12px;
  display: flex; flex-direction: column; gap: 10px;
}
.msg { display: flex; flex-direction: column; max-width: 78%; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.msg.in { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 9px 13px; border-radius: 18px;
  word-break: break-word; white-space: pre-wrap;
  font-size: 15.5px; line-height: 1.4;
}
.msg.in .bubble { background: var(--bubble-in); border-bottom-left-radius: 6px; }
.msg.out .bubble {
  background: var(--bubble-out); color: var(--bubble-out-text);
  border-bottom-right-radius: 6px;
}
.msg .meta {
  font-size: 11px; color: var(--text-dim); margin-top: 4px;
  display: flex; gap: 6px; align-items: center;
}
.msg .meta .read { color: #2aa56b; }

.bubble.media { padding: 4px; background: var(--bubble-in); }
.msg.out .bubble.media { background: var(--bubble-out); }
.bubble.media img { display: block; max-width: 240px; max-height: 320px; border-radius: 15px; }
.bubble.media video { display: block; max-width: 240px; border-radius: 15px; }
.vwrap { position: relative; display: inline-block; }
.vwrap .vph {
  width: 220px; height: 130px; border-radius: 15px;
  background: #000; display: grid; place-items: center; color: #fff; font-size: 26px;
}

.daysep {
  align-self: center; font-size: 11.5px; color: var(--text-dim);
  background: var(--bg-elev); padding: 3px 10px; border-radius: 10px;
  margin: 6px 0;
}

/* ---------------- 输入区 ---------------- */
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 12px calc(var(--safe-bottom) + 8px);
  border-top: 1px solid var(--line);
  background: var(--bg-elev);
}
#input {
  flex: 1; resize: none; border: 0; outline: none;
  background: var(--bg); color: var(--text);
  border-radius: 18px; padding: 9px 14px;
  font: inherit; font-size: 16px; max-height: 120px; line-height: 1.35;
}
.sendbtn {
  border: 0; background: var(--accent); color: #fff;
  font-size: 15px; font-weight: 500;
  border-radius: 18px; padding: 9px 16px; height: 38px;
}
.sendbtn:disabled { opacity: .4; }

.uploadbar {
  position: absolute; left: 0; right: 0; bottom: calc(var(--safe-bottom) + 58px);
  display: flex; align-items: center; gap: 10px;
  padding: 8px 16px; font-size: 12.5px; color: var(--text-dim);
  background: var(--bg-elev); border-top: 1px solid var(--line);
}
.uploadbar.hidden { display: none; }
.progress { flex: 1; height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; }
.progress i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .15s; }

.notifybar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 9px 14px; font-size: 13px; color: var(--text-dim);
  background: var(--accent-soft); border-top: 1px solid var(--line);
}
.notifybar.hidden { display: none; }

/* 对方输入中 */
.typing {
  padding: 6px 16px;
  font-size: 12.5px;
  color: var(--text-dim);
  background: var(--bg-elev);
  border-top: 1px solid var(--line);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  animation: fadeIn .12s ease;
}
.typing.hidden { display: none; }
.typing b { color: var(--text); font-weight: 500; }

/* ---------------- 设置面板 ---------------- */
.sheet {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(0,0,0,.34);
  display: flex; align-items: flex-end;
}
.sheet.hidden { display: none; }
.sheet-body {
  width: 100%; background: var(--bg-elev);
  border-radius: 18px 18px 0 0;
  padding: 18px 18px calc(var(--safe-bottom) + 18px);
}
.sheet-body h3 { margin: 0 0 12px; font-size: 15px; color: var(--text-dim); font-weight: 500; }
.row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 0; border-bottom: 1px solid var(--line); font-size: 15px;
}
.row b { font-weight: 500; color: var(--text-dim); }
.mini {
  border: 1px solid var(--line); background: none; color: var(--accent);
  border-radius: 14px; padding: 5px 12px; font-size: 13px;
}

/* 开关 */
.switch {
  width: 50px; height: 30px; border-radius: 15px;
  border: 0; padding: 0; background: var(--line);
  position: relative; transition: background .18s ease;
  flex: 0 0 auto;
}
.switch i {
  position: absolute; top: 3px; left: 3px;
  width: 24px; height: 24px; border-radius: 12px;
  background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .28);
  transition: transform .18s ease;
}
.switch.on { background: #34c759; }
.switch.on i { transform: translateX(20px); }
.switch:active { opacity: .85; }
.wide {
  width: 100%; margin-top: 14px; border: 0; border-radius: 13px;
  padding: 13px; font-size: 15px; font-weight: 500;
  background: var(--accent); color: #fff;
}
.wide.ghost { background: var(--bg); color: var(--text); border: 1px solid var(--line); }

/* ---------------- 图片查看器（页内） ---------------- */
.viewer {
  position: fixed; inset: 0; z-index: 80;
  background: rgba(0, 0, 0, .95);
  display: flex; align-items: center; justify-content: center;
  padding: calc(var(--safe-top) + 12px) 12px calc(var(--safe-bottom) + 84px);
  animation: fadeIn .16s ease;
}
.viewer.hidden { display: none; }
.viewer img {
  max-width: 100%; max-height: 100%;
  object-fit: contain; border-radius: 8px;
  user-select: none; -webkit-user-select: none;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .5);
}
/* 底部高对比胶囊按钮，任何底图上都清晰可见 */
.viewer-close {
  position: absolute;
  left: 50%; transform: translateX(-50%);
  bottom: calc(var(--safe-bottom) + 24px);
  min-width: 112px; height: 46px; padding: 0 26px;
  border: 0; border-radius: 23px;
  background: rgba(255, 255, 255, .95);
  color: #111317;
  font-size: 15.5px; font-weight: 600; letter-spacing: .5px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .5), 0 0 0 1px rgba(255, 255, 255, .2);
  transition: transform .12s ease, background .12s ease;
}
.viewer-close:active { background: #fff; transform: translateX(-50%) scale(.95); }
@keyframes fadeIn { from { opacity: 0 } to { opacity: 1 } }
body.viewer-open { overflow: hidden; }

/* ---------------- toast ---------------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 90px);
  transform: translate(-50%, 12px);
  background: rgba(20,22,26,.92); color: #fff;
  font-size: 13.5px; padding: 9px 16px; border-radius: 12px;
  opacity: 0; pointer-events: none; transition: .22s; z-index: 60;
  max-width: 82vw; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------------- 桌面浏览器：约束为居中的手机宽列 ---------------- */
@media (min-width: 760px) {
  body { background: #e6e8ee; }
  .screen {
    width: 560px;
    left: 50%; right: auto;
    transform: translateX(-50%);
    box-shadow: 0 0 0 1px var(--line), 0 20px 70px rgba(0, 0, 0, .12);
  }
  .sheet { align-items: flex-end; }
  .sheet-body {
    max-width: 560px;
    margin: 0 auto;
    border-radius: 18px 18px 0 0;
  }
  .viewer img { max-width: min(92vw, 1100px); }
  .bubble.media img { max-width: 320px; max-height: 420px; }
}

@media (min-width: 760px) and (prefers-color-scheme: dark) {
  body { background: #08090b; }
}
