:root {
  --bg: #f2f3f5;
  --panel: #ffffff;
  --text: #111827;
  --muted: #8a8f99;
  --blue: #3b82f6;
  --blue-soft: #dbeafe;
  --green: #22c55e;
  --border: #e5e7eb;
  --radius: 16px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, html, body { height: 100%; overflow: hidden; }
body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}
.hidden { display: none !important; }

.login-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(circle at 50% 0%, #e0f2fe 0%, #f2f3f5 55%);
}
.login-card {
  width: 100%;
  max-width: 360px;
  background: rgba(255,255,255,.9);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(255,255,255,.8);
  border-radius: 28px;
  padding: 36px 24px 28px;
  text-align: center;
  box-shadow: 0 18px 50px rgba(15,23,42,.08);
}
.login-avatar, .header-avatar {
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: linear-gradient(145deg, #bfdbfe, #60a5fa);
  color: #fff;
  font-weight: 700;
  text-shadow: 0 1px 2px rgba(0,0,0,.15);
}
.login-avatar {
  width: 84px; height: 84px; margin: 0 auto 14px; font-size: 34px;
  box-shadow: 0 10px 28px rgba(59,130,246,.28);
}
.login-card h1 { margin: 0; font-size: 26px; letter-spacing: 2px; }
.login-sub { margin: 6px 0 24px; color: var(--muted); font-size: 13px; }
.login-card input {
  width: 100%; height: 48px; border-radius: 14px; border: 1px solid var(--border);
  padding: 0 14px; font-size: 16px; outline: none; background: #fff;
}
.login-card input:focus { border-color: #93c5fd; box-shadow: 0 0 0 4px rgba(59,130,246,.12); }
.login-card button {
  width: 100%; height: 48px; margin-top: 12px; border: 0; border-radius: 14px;
  background: var(--blue); color: #fff; font-size: 16px; font-weight: 600;
  cursor: pointer;
}
.login-card button:active { transform: scale(.99); }
.error { min-height: 18px; margin: 10px 0 0; color: #ef4444; font-size: 13px; }

.app { height: 100%; display: flex; flex-direction: column; min-height: 0; max-width: 760px; margin: 0 auto; background: var(--bg); }
.chat-header {
  position: relative; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
  background: rgba(255,255,255,.92);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}
.header-left { display: flex; align-items: center; gap: 10px; }
.header-avatar { width: 40px; height: 40px; font-size: 17px; }
.header-name { font-size: 16px; font-weight: 650; }
.header-status { display: flex; align-items: center; gap: 5px; color: var(--muted); font-size: 11px; margin-top: 2px; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--green); box-shadow: 0 0 0 3px rgba(34,197,94,.12); }
.icon-btn {
  width: 38px; height: 38px; border: 0; border-radius: 12px; background: transparent;
  color: #374151; font-size: 22px; line-height: 1; cursor: pointer;
}
.icon-btn:active { background: #f3f4f6; }
.menu {
  position: absolute; right: 12px; top: calc(58px + env(safe-area-inset-top));
  background: #fff; border: 1px solid var(--border); border-radius: 14px;
  box-shadow: 0 12px 30px rgba(15,23,42,.12); overflow: hidden; min-width: 140px;
  z-index: 30;
}
.menu a.menu-link,
.menu button {
  display: block; width: 100%; padding: 12px 16px; border: 0; background: #fff;
  text-align: left; font-size: 14px; color: #374151; text-decoration: none; cursor: pointer;
  box-sizing: border-box;
}
.menu a.menu-link + a.menu-link,
.menu a.menu-link + button,
.menu button + button,
.menu button + a.menu-link { border-top: 1px solid var(--border); }
.menu a.menu-link:active,
.menu button:active { background: #f9fafb; }

.messages {
  flex: 1; min-height: 0; overflow-y: auto; padding: 16px 12px 12px;
  display: flex; flex-direction: column; gap: 12px;
  scroll-behavior: smooth;
}
.empty-hint { margin: auto; color: #9ca3af; font-size: 13px; }
.msg-row { display: flex; align-items: flex-start; gap: 8px; max-width: 100%; }
.msg-row.user { justify-content: flex-end; }
.msg-avatar {
  width: 32px; height: 32px; flex: 0 0 32px; border-radius: 50%;
  display: grid; place-items: center; font-size: 13px; font-weight: 700; color: #fff;
  background: linear-gradient(145deg, #bfdbfe, #60a5fa);
}
.bubble-wrap { display: flex; flex-direction: column; max-width: 78%; }
.msg-row.user .bubble-wrap { align-items: flex-end; }
.bubble {
  padding: 9px 12px; border-radius: 16px; font-size: 15px; line-height: 1.55;
  white-space: pre-wrap; word-break: break-word;
  background: #fff; border: 1px solid rgba(229,231,235,.7);
  box-shadow: 0 1px 2px rgba(15,23,42,.04);
}
.msg-row.user .bubble {
  background: var(--blue); color: #fff; border-color: transparent;
  border-bottom-right-radius: 5px;
}
.msg-row.assistant .bubble { border-bottom-left-radius: 5px; }
.msg-row.her .msg-avatar { margin-top: 0; align-self: flex-start; }
.msg-row.me .bubble-wrap { align-self: flex-end; }
.msg-time { margin-top: 4px; font-size: 10px; color: #9ca3af; padding: 0 4px; }

.typing { display: inline-flex; gap: 4px; align-items: center; height: 20px; }
.typing span {
  width: 5px; height: 5px; border-radius: 50%; background: #9ca3af;
  animation: blink 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%,80%,100% { opacity:.25; transform: translateY(0); } 40% { opacity:1; transform: translateY(-2px); } }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  background: rgba(255,255,255,.96); border-top: 1px solid var(--border);
}
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border); border-radius: 18px;
  padding: 9px 12px; font-size: 15px; line-height: 1.45; max-height: 110px;
  outline: none; background: #fff; font-family: inherit;
}
.composer textarea:focus { border-color: #93c5fd; }
.send-btn {
  height: 38px; padding: 0 16px; border: 0; border-radius: 19px;
  background: var(--blue); color: #fff; font-size: 14px; font-weight: 600; cursor: pointer;
}
.send-btn:disabled { opacity: .5; }
.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: rgba(17,24,39,.88); color: #fff; font-size: 13px;
  padding: 9px 14px; border-radius: 999px; z-index: 50; max-width: 80vw;
  text-align: center;
}
@media (min-width: 760px) {
  body { background: #e9edf3; }
  .app { box-shadow: 0 0 40px rgba(15,23,42,.08); }
}

/* Memory modal */
.modal {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(17,24,39,.35);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal-card {
  width: min(92vw, 520px); max-height: 82dvh;
  background: #fff; border-radius: 22px; padding: 18px;
  display: flex; flex-direction: column; gap: 12px;
  box-shadow: 0 24px 70px rgba(15,23,42,.22);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; }
.modal-head h2 { margin: 0; font-size: 18px; }
.memory-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; min-height: 120px; }
.memory-empty { color: #9ca3af; font-size: 13px; text-align: center; padding: 24px 0; }
.memory-item {
  background: #f9fafb; border: 1px solid var(--border); border-radius: 12px;
  padding: 10px 12px; font-size: 14px; line-height: 1.5;
}
.memory-item .memory-text { word-break: break-word; }
.memory-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.memory-actions button {
  border: 0; background: transparent; color: #6b7280; font-size: 12px; cursor: pointer; padding: 2px 4px;
}
.memory-actions button.danger { color: #ef4444; }
.memory-add { display: flex; gap: 8px; }
.memory-add input {
  flex: 1; height: 40px; border: 1px solid var(--border); border-radius: 12px;
  padding: 0 12px; font-size: 14px; outline: none;
}
.memory-add input:focus { border-color: #93c5fd; }
.memory-add button {
  height: 40px; padding: 0 14px; border: 0; border-radius: 12px;
  background: var(--blue); color: #fff; font-size: 14px; font-weight: 600; cursor: pointer;
}

.chat-state {
  margin-top: 2px;
  font-size: 11px;
  color: #9ca3af;
  min-height: 14px;
}

.msg-avatar.has-avatar {
  color: transparent;
  background-image: var(--persona-avatar);
  background-size: cover;
  background-position: center;
}

.settings-overlay {
  position: fixed; inset: 0; z-index: 200; background: #eef2f7;
  display: flex; flex-direction: column;
}
.settings-bar {
  height: 46px; flex: 0 0 46px; display: flex; align-items: center; justify-content: space-between;
  padding: 0 14px; background: #fff; border-bottom: 1px solid #e5e7eb; font-size: 14px; font-weight: 600;
}
.settings-bar button {
  border: 0; background: transparent; color: #3b82f6; font-size: 14px; font-weight: 600; padding: 6px;
}
.settings-frame { flex: 1; width: 100%; border: 0; background: #eef2f7; }
.menu button.menu-link { border: 0; }

.header-avatar { cursor: pointer; }
.affection-modal {
  position: fixed; inset: 0; z-index: 300; background: rgba(17,24,39,.45);
  backdrop-filter: blur(6px); display: flex; align-items: center; justify-content: center; padding: 20px;
}
.affection-card {
  width: min(92vw, 380px); position: relative; overflow: hidden; text-align: center;
  background: linear-gradient(160deg, #ffffff 0%, #fff7fb 58%, #eef2ff 100%);
  border-radius: 28px; padding: 28px 24px 22px;
  box-shadow: 0 30px 80px rgba(15,23,42,.28);
}
.affection-card::before {
  content: ''; position: absolute; width: 190px; height: 190px; border-radius: 50%;
  top: -80px; right: -70px; background: radial-gradient(circle, #fbcfe8, transparent 70%); opacity: .8;
}
.affection-close {
  position: absolute; right: 12px; top: 8px; z-index: 2; border: 0; background: transparent;
  color: #9ca3af; font-size: 24px; cursor: pointer;
}
.affection-avatar {
  width: 88px; height: 88px; border-radius: 50%; margin: 0 auto 12px;
  display: grid; place-items: center; color: #fff; font-size: 34px; font-weight: 700;
  background: linear-gradient(145deg, #bfdbfe, #60a5fa); background-size: cover; background-position: center;
  border: 3px solid rgba(255,255,255,.9); box-shadow: 0 12px 30px rgba(59,130,246,.35);
  position: relative; z-index: 1;
}
.affection-card h2 { margin: 0; font-size: 21px; position: relative; z-index: 1; }
.affection-level {
  display: inline-block; margin-top: 8px; padding: 4px 14px; border-radius: 999px;
  font-size: 13px; font-weight: 650; color: #fff; background: #9ca3af;
}
.affection-score { margin-top: 14px; font-size: 34px; font-weight: 800; color: #111827; }
.affection-score span { font-size: 13px; color: #9ca3af; font-weight: 400; margin-left: 2px; }
.affection-track {
  position: relative; height: 10px; margin: 14px 4px 4px; background: #e5e7eb;
  border-radius: 999px; overflow: hidden;
}
.affection-zero {
  position: absolute; left: 49.7%; top: -3px; bottom: -3px; width: 2px;
  background: #fff; box-shadow: 0 0 0 1px #cbd5e1; z-index: 2;
}
.affection-fill { position: absolute; top: 0; bottom: 0; border-radius: 999px; transition: all .4s ease; }
.affection-fill.positive { background: linear-gradient(90deg, #f9a8d4, #ef4444); }
.affection-fill.negative { background: linear-gradient(90deg, #6b7280, #ef4444); }
.affection-scale { display: flex; justify-content: space-between; font-size: 10px; color: #9ca3af; margin: 0 2px; }
.affection-desc { margin: 16px 0 0; font-size: 13px; line-height: 1.6; color: #4b5563; min-height: 42px; }
.affection-hearts { margin-top: 10px; font-size: 20px; letter-spacing: 4px; }

.affection-settlement { margin-top: 10px; font-size: 12px; color: #6b7280; }

.memory-toolbar { display: flex; gap: 8px; margin-bottom: 8px; }
.memory-toolbar input {
  flex: 1; height: 36px; border: 1px solid var(--border); border-radius: 10px;
  padding: 0 10px; font-size: 13px; outline: none; background: #fff;
}
.memory-toolbar input:focus { border-color: #93c5fd; }
.memory-toolbar button {
  height: 36px; padding: 0 12px; border: 0; border-radius: 10px;
  background: #eef2ff; color: #3b5bdb; font-size: 13px; font-weight: 600; cursor: pointer;
}
.memory-summaries {
  max-height: 180px; overflow-y: auto; background: #f8fafc; border: 1px solid var(--border);
  border-radius: 12px; padding: 8px 10px; margin-bottom: 8px;
}
.memory-summary-item { font-size: 12px; line-height: 1.5; color: #4b5563; padding: 6px 0; border-bottom: 1px dashed #e5e7eb; }
.memory-summary-item:last-child { border-bottom: 0; }
.memory-summary-level { display: inline-block; font-size: 10px; color: #3b5bdb; background: #e0e7ff; border-radius: 999px; padding: 1px 6px; margin-right: 6px; }
.memory-item.pinned { border-color: #fbbf24; background: #fffbeb; }
.memory-meta { display: flex; gap: 8px; align-items: center; margin-top: 4px; font-size: 11px; color: #9ca3af; }
.memory-meta .pin-badge { color: #b45309; font-weight: 600; }
