:root {
  --bg: #f2f3f5;
  --panel: #ffffff;
  --text: #111418;
  --muted: #8a9099;
  --line: rgba(0, 0, 0, .06);
  --me: #b8f3c9;
  --me-text: #0b2a14;
  --peer: #ffffff;
  --peer-text: #14171a;
  --accent: #07c160;
  --shadow: 0 4px 18px rgba(0, 0, 0, .06);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --panel: #161a20;
    --text: #e9edf2;
    --muted: #8d96a3;
    --line: rgba(255, 255, 255, .08);
    --me: #1f6d45;
    --me-text: #eafff2;
    --peer: #1d222a;
    --peer-text: #e9edf2;
    --accent: #34d399;
    --shadow: 0 4px 18px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  overscroll-behavior: none;
}
.hidden { display: none !important; }

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 760px;
  margin: 0 auto;
  background: var(--bg);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}
.avatar { object-fit: cover; 
  width: 42px; height: 42px; border-radius: 50%;
  display: grid; place-items: center;
  background: linear-gradient(135deg, #8de5b3, #4ad295);
  color: #08301b; font-weight: 700; font-size: 18px;
  flex: none;
}
.who { flex: 1; min-width: 0; }
.name { font-weight: 600; font-size: 16px; }
.status { color: var(--muted); font-size: 12px; margin-top: 1px; }
.menu-btn {
  border: 0; background: transparent; color: var(--muted);
  font-size: 22px; padding: 4px 8px; border-radius: 10px; line-height: 1;
}
.menu-btn:active { background: var(--line); }

.chat {
  flex: 1;
  overflow-y: auto;
  padding: 14px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}
.empty { color: var(--muted); text-align: center; margin: auto; font-size: 13px; }

.time-sep { align-self: center; color: var(--muted); font-size: 11px; margin: 6px 0 2px; }

.row { display: flex; align-items: flex-end; gap: 8px; max-width: 100%; }
.row.peer { flex-direction: row; }
.row.me { flex-direction: row-reverse; }

.bubble {
  max-width: min(78%, 520px);
  padding: 9px 13px;
  border-radius: 18px;
  white-space: pre-wrap;
  word-break: break-word;
  box-shadow: var(--shadow);
  position: relative;
  animation: pop .16s ease-out;
}
@keyframes pop { from { transform: translateY(4px); opacity: .4 } to { transform: none; opacity: 1 } }
.row.peer .bubble { background: var(--peer); color: var(--peer-text); border-bottom-left-radius: 6px; }
.row.me .bubble { background: var(--me); color: var(--me-text); border-bottom-right-radius: 6px; }

.typing .bubble { display: inline-flex; gap: 4px; align-items: center; }
.typing .dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: currentColor; opacity: .45;
  animation: blink 1.2s infinite;
}
.typing .dot:nth-child(2) { animation-delay: .15s; }
.typing .dot:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25 } 40% { opacity: .8 } }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid var(--line);
}
#input {
  flex: 1;
  resize: none;
  max-height: 120px;
  border: 0;
  outline: none;
  border-radius: 20px;
  padding: 10px 14px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  line-height: 1.4;
}
.send-btn {
  border: 0;
  width: 42px; height: 42px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: grid; place-items: center;
  flex: none;
  transition: transform .1s ease, opacity .2s ease;
}
.send-btn:active { transform: scale(.94); }
.send-btn:disabled { opacity: .45; }

.login {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  background: var(--bg);
  padding: 24px;
  z-index: 50;
}
.login-card {
  width: min(360px, 100%);
  background: var(--panel);
  border-radius: 22px;
  padding: 30px 24px 24px;
  text-align: center;
  box-shadow: var(--shadow);
}
.login-avatar { object-fit: cover; 
  width: 64px; height: 64px; border-radius: 50%;
  display: grid; place-items: center;
  margin: 0 auto 12px;
  background: linear-gradient(135deg, #8de5b3, #4ad295);
  color: #08301b; font-size: 24px; font-weight: 700;
}
.login-card h1 { font-size: 20px; margin: 0 0 6px; }
.hint { color: var(--muted); font-size: 13px; margin: 0 0 18px; }
#password {
  width: 100%; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
  font: inherit; outline: none;
}
#loginBtn, #closeSheet {
  width: 100%; margin-top: 14px; padding: 12px;
  border: 0; border-radius: 12px;
  background: var(--accent); color: #fff;
  font: inherit; font-weight: 600;
}
.err { color: #e5484d; font-size: 13px; min-height: 18px; margin-top: 8px; }

.sheet {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .35);
  display: grid; place-items: center; padding: 24px; z-index: 40;
}
.sheet-card {
  width: min(420px, 100%); max-height: 70vh; overflow: auto;
  background: var(--panel); border-radius: 20px; padding: 22px;
  box-shadow: var(--shadow);
}
.sheet-card h2 { margin: 0 0 12px; font-size: 16px; }
.memory-text { color: var(--text); white-space: pre-wrap; font-size: 14px; line-height: 1.65; }
