:root {
  --bg: #f6f1ea;
  --bg-deep: #efe7dd;
  --card: #ffffff;
  --ink: #2c2723;
  --muted: #93887f;
  --accent: #e07a5f;
  --accent-dark: #cf6449;
  --accent-soft: #fdf0e8;
  --border: #eae0d5;
  --radius: 18px;
  --shadow-sm: 0 1px 2px rgba(60, 42, 30, 0.06);
  --shadow-md: 0 4px 16px rgba(60, 42, 30, 0.08);
  --shadow-lg: 0 12px 32px rgba(60, 42, 30, 0.14);
  --danger: #d9534f;
  --ok: #2e7d4f;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background:
    radial-gradient(1200px 500px at 50% -10%, #fbf7f1 0%, transparent 70%),
    var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

::selection { background: rgba(224, 122, 95, 0.25); }

button { font-family: inherit; }
button:focus-visible, input:focus-visible, textarea:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.app {
  max-width: 880px;
  margin: 0 auto;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ---------- 顶栏(毛玻璃) ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 18px;
  background: rgba(255, 255, 255, 0.82);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid rgba(60, 42, 30, 0.07);
}

.avatar {
  position: relative;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: linear-gradient(135deg, #f4a261, #e07a5f);
  color: #fff;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: none;
  box-shadow: 0 0 0 2px #fff, 0 4px 10px rgba(224, 122, 95, 0.35);
}
.avatar::after {
  content: "";
  position: absolute;
  right: 0;
  bottom: 1px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: #58c98b;
  border: 2px solid #fff;
}

.who { flex: 1; min-width: 0; }
.who h1 { font-size: 17px; font-weight: 650; letter-spacing: 0.2px; }
.who p {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge {
  font-size: 11px;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-dark);
  border: 1px solid #f3d5c6;
  flex: none;
  font-weight: 500;
}
.badge.llm { background: #e8f5ec; color: var(--ok); border-color: #c9e5d2; }

.icon-btn {
  border: none;
  background: transparent;
  font-size: 19px;
  cursor: pointer;
  color: var(--muted);
  width: 38px;
  height: 38px;
  border-radius: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s, transform 0.1s;
}
.icon-btn:hover { background: rgba(60, 42, 30, 0.06); color: var(--ink); }
.icon-btn:active { transform: scale(0.94); }
#tts-toggle { opacity: 0.55; }
#tts-toggle.on { opacity: 1; }

/* ---------- 聊天区 ---------- */
.chat {
  flex: 1;
  overflow-y: auto;
  padding: 22px 20px 30px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}

/* 细滚动条 */
.chat::-webkit-scrollbar, .panel::-webkit-scrollbar { width: 6px; }
.chat::-webkit-scrollbar-thumb, .panel::-webkit-scrollbar-thumb {
  background: rgba(60, 42, 30, 0.14);
  border-radius: 99px;
}
.chat::-webkit-scrollbar-thumb:hover, .panel::-webkit-scrollbar-thumb:hover {
  background: rgba(60, 42, 30, 0.25);
}
.chat::-webkit-scrollbar-track, .panel::-webkit-scrollbar-track { background: transparent; }

.empty-hint {
  margin: auto;
  color: var(--muted);
  font-size: 14px;
  text-align: center;
  line-height: 2.1;
  padding: 20px;
}
.empty-hint::before {
  content: "👋";
  display: block;
  font-size: 40px;
  margin-bottom: 10px;
  filter: drop-shadow(0 4px 8px rgba(60, 42, 30, 0.15));
}

.msg {
  max-width: 76%;
  padding: 10px 15px;
  border-radius: var(--radius);
  font-size: 15px;
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: break-word;
  animation: msg-in 0.22s cubic-bezier(0.2, 0.9, 0.3, 1);
  box-shadow: var(--shadow-sm);
}
@keyframes msg-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

.msg.user {
  align-self: flex-end;
  background: linear-gradient(160deg, #e58a6b, #dd6f4f);
  color: #fff;
  border-bottom-right-radius: 6px;
  box-shadow: 0 3px 10px rgba(224, 122, 95, 0.32);
}

.msg.assistant {
  align-self: flex-start;
  background: var(--card);
  border: 1px solid rgba(60, 42, 30, 0.06);
  border-bottom-left-radius: 6px;
  box-shadow: var(--shadow-sm);
}

.msg.assistant.typing {
  display: flex;
  gap: 5px;
  align-items: center;
  padding: 15px 17px;
}
.msg.assistant.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #cfc4ba;
  animation: blink 1.2s infinite;
}
.msg.assistant.typing span:nth-child(2) { animation-delay: 0.2s; }
.msg.assistant.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 60%, 100% { opacity: 0.25; } 30% { opacity: 1; } }

.msg.error {
  align-self: center;
  background: #fdecea;
  color: #b3261e;
  border: 1px solid #f5c6c0;
  font-size: 13px;
  box-shadow: none;
}

/* ---------- 媒体预览条 ---------- */
.img-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 18px 0;
  background: linear-gradient(transparent, rgba(246, 241, 234, 0.6));
}
.img-preview img {
  max-height: 72px;
  max-width: 120px;
  border-radius: 10px;
  border: 1px solid var(--border);
  object-fit: cover;
  box-shadow: var(--shadow-sm);
}
.img-preview .chip {
  font-size: 12px;
  color: var(--accent-dark);
  background: var(--accent-soft);
  border: 1px solid #f3d5c6;
  border-radius: 999px;
  padding: 4px 10px;
  white-space: nowrap;
}
.img-preview button {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 15px;
  cursor: pointer;
  padding: 4px;
  border-radius: 8px;
}
.img-preview button:hover { color: var(--danger); background: rgba(0,0,0,0.04); }

/* ---------- 输入区(悬浮卡片) ---------- */
.composer {
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(transparent, rgba(246, 241, 234, 0.9) 30%);
}

.composer-inner {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--card);
  border: 1px solid rgba(60, 42, 30, 0.08);
  border-radius: 20px;
  padding: 10px 12px 8px;
  box-shadow: var(--shadow-md);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.composer-inner:focus-within {
  border-color: rgba(224, 122, 95, 0.55);
  box-shadow: 0 4px 20px rgba(224, 122, 95, 0.16);
}

#input {
  width: 100%;
  resize: none;
  border: none;
  background: transparent;
  padding: 4px 6px 0;
  font-size: 15px;
  font-family: inherit;
  line-height: 1.55;
  max-height: 140px;
  outline: none;
  color: var(--ink);
}
#input::placeholder { color: #b3a89e; }

.composer-tools {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* 工具按钮:统一幽灵圆角方形 */
#mic-toggle, #vid-toggle, #img-toggle, #web-toggle, #tts-stop {
  width: 38px;
  height: 36px;
  flex: none;
  border: none;
  border-radius: 11px;
  background: transparent;
  font-size: 17px;
  cursor: pointer;
  opacity: 0.72;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, opacity 0.15s, transform 0.1s;
}
#mic-toggle:hover, #vid-toggle:hover, #img-toggle:hover, #web-toggle:hover, #tts-stop:hover {
  background: rgba(60, 42, 30, 0.06);
  opacity: 1;
}
#mic-toggle:active, #vid-toggle:active, #img-toggle:active, #web-toggle:active { transform: scale(0.92); }
#tts-stop { opacity: 1; color: var(--muted); }
#tts-stop:hover { color: var(--danger); }

#web-toggle.on {
  background: #e8f5ec;
  opacity: 1;
  box-shadow: inset 0 0 0 1.5px var(--ok);
}

#mic-toggle.rec {
  background: #fde8e8;
  opacity: 1;
  animation: rec-pulse 1.2s infinite;
}
@keyframes rec-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(192, 57, 43, 0.35); }
  50% { box-shadow: 0 0 0 5px rgba(192, 57, 43, 0); }
}

#send {
  margin-left: auto;
  flex: none;
  min-width: 76px;
  height: 36px;
  border: none;
  border-radius: 11px;
  padding: 0 20px;
  background: linear-gradient(160deg, #e58a6b, #dd6f4f);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 2px;
  cursor: pointer;
  box-shadow: 0 3px 10px rgba(224, 122, 95, 0.35);
  transition: filter 0.15s, transform 0.1s, box-shadow 0.15s;
}
#send:hover { filter: brightness(1.05); box-shadow: 0 4px 14px rgba(224, 122, 95, 0.45); }
#send:active { transform: scale(0.96); }
#send:disabled { opacity: 0.5; cursor: default; box-shadow: none; }
/* 发送中变「⏹ 停止」 */
#send.stop { background: var(--danger); box-shadow: 0 3px 10px rgba(217, 83, 79, 0.35); }
#send.stop:hover { background: #c9302c; }

/* 手机窄屏 */
@media (max-width: 640px) {
  .topbar { padding: 10px 12px; }
  .chat { padding: 16px 12px 24px; }
  .msg { max-width: 84%; }
  .composer { padding: 8px 10px calc(10px + env(safe-area-inset-bottom)); }
  .composer-inner { border-radius: 18px; }
  #input { max-height: 110px; }
}

/* ---------- 侧边面板 ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(46, 34, 26, 0.4);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  z-index: 9;
}

.panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(400px, 100vw);
  background: #fbf8f4;
  z-index: 10;
  overflow-y: auto;
  box-shadow: -12px 0 40px rgba(46, 34, 26, 0.18);
}

.hidden { display: none !important; }

.panel-head {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: rgba(251, 248, 244, 0.9);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(60, 42, 30, 0.06);
}
.panel-head h2 { font-size: 16px; font-weight: 650; }

.stats {
  background: var(--card);
  border: 1px solid rgba(60, 42, 30, 0.06);
  border-radius: 14px;
  padding: 12px 14px;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.9;
  box-shadow: var(--shadow-sm);
  margin-top: 12px;
}

.panel section {
  margin-top: 18px;
  background: var(--card);
  border: 1px solid rgba(60, 42, 30, 0.06);
  border-radius: 16px;
  padding: 14px 16px 16px;
  box-shadow: var(--shadow-sm);
}
.panel h3 {
  font-size: 13px;
  color: var(--ink);
  font-weight: 650;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.panel h3::before {
  content: "";
  width: 4px;
  height: 14px;
  border-radius: 2px;
  background: linear-gradient(#f4a261, #e07a5f);
  flex: none;
}

/* 折叠的高级设置 */
details.adv {
  margin-top: 10px;
  border: 1px solid rgba(60, 42, 30, 0.07);
  border-radius: 12px;
  background: #faf6f0;
  overflow: hidden;
}
details.adv summary {
  list-style: none;
  cursor: pointer;
  padding: 10px 12px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 6px;
  user-select: none;
  transition: background 0.15s;
}
details.adv summary:hover { background: rgba(60, 42, 30, 0.04); }
details.adv summary::-webkit-details-marker { display: none; }
details.adv summary::after {
  content: "▾";
  margin-left: auto;
  color: var(--muted);
  font-size: 12px;
  transition: transform 0.18s;
}
details.adv[open] summary::after { transform: rotate(180deg); }
.adv-note {
  font-size: 11px;
  font-weight: 400;
  color: var(--muted);
  margin-left: 2px;
}
details.adv > label,
details.adv > .inline-group,
details.adv > .settings-actions,
details.adv > .hint {
  margin: 8px 12px;
}
details.adv > .hint { display: block; margin-bottom: 12px; }

.memories { list-style: none; display: flex; flex-direction: column; gap: 8px; }
.memories li {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  background: #faf6f0;
  border: 1px solid rgba(60, 42, 30, 0.05);
  border-radius: 11px;
  padding: 9px 12px;
  font-size: 13.5px;
  line-height: 1.55;
}
.memories li span { flex: 1; }
.memories li button {
  border: none;
  background: transparent;
  color: #b8aba0;
  cursor: pointer;
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 6px;
  flex: none;
  transition: color 0.15s, background 0.15s;
}
.memories li button:hover { color: var(--danger); background: #fdecea; }
.memories li.none { color: var(--muted); justify-content: center; background: transparent; border-style: dashed; }

/* 洞察标签 */
.pf-tag {
  font-style: normal;
  font-size: 10.5px;
  color: #6b9bd0;
  background: #eef3f9;
  border-radius: 999px;
  padding: 1px 7px;
  margin-right: 6px;
  vertical-align: 1px;
}

/* 「她眼中的你」画像 */
.profile-box {
  background: #faf6f0;
  border: 1px solid rgba(60, 42, 30, 0.06);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.8;
  color: var(--ink);
}
.pf-row { display: flex; gap: 8px; }
.pf-label { color: var(--muted); flex: none; min-width: 56px; }
.pf-empty { color: var(--muted); text-align: center; padding: 6px 0; }
.reflect-btn {
  margin-top: 10px;
  border: none;
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  background: #eef3f9;
  color: #4a7bb5;
  transition: background 0.15s, filter 0.15s;
}
.reflect-btn:hover { background: #e3ecf6; }
.reflect-btn:disabled { opacity: 0.55; cursor: wait; }

#memory-form { display: flex; gap: 8px; margin-top: 10px; }
#memory-form input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 12px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
  background: #faf6f0;
  transition: border-color 0.15s, background 0.15s;
}
#memory-form input:focus { border-color: var(--accent); background: #fff; }

#memory-form button,
#save-companion {
  border: none;
  border-radius: 10px;
  padding: 9px 18px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  background: linear-gradient(160deg, #e58a6b, #dd6f4f);
  color: #fff;
  box-shadow: 0 2px 8px rgba(224, 122, 95, 0.3);
  transition: filter 0.15s, transform 0.1s;
}
#memory-form button:hover, #save-companion:hover { filter: brightness(1.05); }
#memory-form button:active, #save-companion:active { transform: scale(0.97); }

.panel label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 10px;
}
.panel label input,
.panel label textarea {
  display: block;
  width: 100%;
  margin-top: 6px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 12px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  background: #faf6f0;
  color: var(--ink);
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
}
.panel label input:focus,
.panel label textarea:focus {
  border-color: var(--accent);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(224, 122, 95, 0.12);
}

#clear-chat {
  border: none;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  background: #fdecea;
  color: #b3261e;
  width: 100%;
  transition: background 0.15s;
}
#clear-chat:hover { background: #f9d9d6; }

/* ---------- 模型设置 ---------- */
.hint { font-weight: 400; font-size: 12px; color: var(--muted); display: inline; }

#set-apikey { font-family: inherit; letter-spacing: 2px; }

.inline-group { display: flex; gap: 12px; }
.inline-group .half { flex: 1; }

#set-temp, #set-tts-speed, #set-tts-pitch {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 5px; border-radius: 3px;
  background: var(--border); outline: none;
  margin: 8px 0 4px;
  cursor: pointer;
}
#set-temp::-webkit-slider-thumb,
#set-tts-speed::-webkit-slider-thumb,
#set-tts-pitch::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--accent); border: none;
  cursor: pointer;
  box-shadow: 0 1px 4px rgba(224, 122, 95, 0.5);
}
#temp-val {
  display: inline-block; font-size: 13px; font-weight: 600;
  color: var(--accent-dark); margin-left: 6px; min-width: 2.4em;
}

.settings-actions { display: flex; gap: 8px; margin-top: 6px; }
.settings-actions button.mini { font-size: 12px; padding: 7px 8px; }

#save-settings,
#clear-api-key,
#tts-test {
  border: none; border-radius: 10px;
  padding: 10px 12px; font-size: 13px; font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: filter 0.15s, transform 0.1s;
}
#save-settings { background: linear-gradient(160deg, #6b9bd0, #5387c2); color: #fff; flex: 1; box-shadow: 0 2px 8px rgba(91, 137, 196, 0.3); }
#save-settings:hover { filter: brightness(1.06); }
#tts-test { background: #eef3f9; color: #4a7bb5; flex: 1; }
#tts-test:hover { background: #e3ecf6; }
#clear-api-key { background: #fdecea; color: #b3261e; }
#clear-api-key:hover { background: #f9d9d6; }
.settings-actions button:active { transform: scale(0.97); }

#clear-emb-key, #clear-web-key, #clear-tts-key {
  background: #f3ede5; color: #8a7d71;
}
#clear-emb-key:hover, #clear-web-key:hover, #clear-tts-key:hover { background: #eadfce; }

#settings-status {
  display: block; font-size: 12px; margin-top: 8px;
  min-height: 1.2em; color: var(--muted);
}

/* ---------- 动效偏好 ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .chat { scroll-behavior: auto; }
}
