/* ========== 主题变量 ========== */
:root {
  --bg: #0e0f13;
  --bg-2: #15171e;
  --bg-3: #1c1f28;
  --line: #262a35;
  --fg: #eceef4;
  --fg-2: #a3a8b8;
  --fg-3: #6b7183;
  --accent: #c9a227;
  --accent-2: #8a6d1a;
  --bubble-me: linear-gradient(135deg, #3a3320, #2a2418);
  --bubble-ai: rgba(255, 255, 255, 0.055);
  --danger: #e2564c;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

[data-theme="light"] {
  --bg: #f6f5f2;
  --bg-2: #ffffff;
  --bg-3: #efece6;
  --line: #e2ded6;
  --fg: #1a1a1a;
  --fg-2: #5c5c5c;
  --fg-3: #9a9a9a;
  --accent: #9c7d14;
  --accent-2: #c9a227;
  --bubble-me: linear-gradient(135deg, #f3e6bf, #efe1b4);
  --bubble-ai: rgba(0, 0, 0, 0.04);
  --danger: #c0392b;
}

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

html, body {
  margin: 0; height: 100%;
  background: var(--bg); color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  font-size: 15px; line-height: 1.6;
  overscroll-behavior: none;
}

.hidden { display: none !important; }
.muted { color: var(--fg-2); }
.small { font-size: 12.5px; }
.center { text-align: center; }
.error { color: var(--danger); font-size: 13px; text-align: center; margin: 10px 0 0; }

.view { position: fixed; inset: 0; display: flex; flex-direction: column; }
.overlay { background: var(--bg); z-index: 30; }

/* ========== 按钮 / 输入 ========== */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--bg-3); color: var(--fg);
  padding: 12px 18px; border-radius: 12px; font-size: 15px; cursor: pointer; font-family: inherit;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #1a1408; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.small { padding: 7px 14px; font-size: 13.5px; border-radius: 10px; }
.btn:active { transform: scale(0.98); }

.icon-btn {
  appearance: none; border: 0; background: transparent; color: var(--fg);
  font-size: 24px; line-height: 1; width: 40px; height: 40px; border-radius: 12px; cursor: pointer;
}
.icon-btn:active { background: var(--bg-3); }
.icon-btn.more { font-size: 19px; width: 34px; height: 34px; color: var(--fg-3); flex: 0 0 auto; }

input, textarea {
  width: 100%; appearance: none; font-family: inherit; font-size: 15px;
  background: var(--bg-3); color: var(--fg); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px 14px; outline: none; resize: vertical;
}
input:focus, textarea:focus { border-color: var(--accent-2); }
textarea { line-height: 1.7; }

.stack { display: flex; flex-direction: column; gap: 10px; }

/* ========== 登录 ========== */
.auth { align-items: center; justify-content: center; padding: 24px; }
.auth-card { width: 100%; max-width: 360px; text-align: center; }
.auth-logo { font-size: 42px; color: var(--accent); margin-bottom: 6px; }
.auth-card h1 { font-size: 22px; margin: 0 0 4px; font-weight: 600; letter-spacing: 1px; }
.auth-card .muted { margin: 0 0 22px; font-size: 13.5px; }
.auth-card form { text-align: left; }

/* ========== 顶栏 ========== */
.topbar {
  flex: 0 0 auto; display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 14px 10px;
  border-bottom: 1px solid transparent;
}
.topbar h2 { font-size: 20px; margin: 0; font-weight: 600; flex: 1; }
.chat-top { gap: 10px; }
.chat-title { flex: 1; min-width: 0; }
.chat-title > div:first-child { font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ========== 列表 ========== */
.list { flex: 1; overflow-y: auto; padding: 0 8px calc(76px + var(--safe-b)); -webkit-overflow-scrolling: touch; }

.item {
  display: flex; gap: 12px; align-items: center;
  padding: 12px 10px; border-radius: 14px; cursor: pointer;
}
.item:active { background: var(--bg-2); }
.item .avatar { flex: 0 0 auto; }
.item .meta { flex: 1; min-width: 0; }
.item .name { font-size: 15px; font-weight: 500; display: flex; align-items: center; gap: 6px; }
.item .sub { font-size: 12.5px; color: var(--fg-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.item .time { font-size: 11.5px; color: var(--fg-3); flex: 0 0 auto; }
.badge { background: var(--accent); color: #1a1408; font-size: 10px; padding: 1px 6px; border-radius: 8px; font-weight: 700; }

.avatar {
  width: 46px; height: 46px; border-radius: 50%; overflow: hidden;
  background: var(--bg-3); display: flex; align-items: center; justify-content: center;
  color: var(--fg-2); font-size: 16px; flex: 0 0 auto;
  background-size: cover; background-position: center;
}
.avatar.big { width: 62px; height: 62px; font-size: 20px; }

.empty { text-align: center; padding: 70px 20px; color: var(--fg-2); }
.empty p { margin: 4px 0; }

/* ========== 底部 tab ========== */
.tabbar {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: flex; background: rgba(14, 15, 19, 0.92);
  backdrop-filter: blur(16px); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b); z-index: 20;
}
[data-theme="light"] .tabbar { background: rgba(255, 255, 255, 0.94); }
.tabbar button {
  flex: 1; appearance: none; border: 0; background: transparent; color: var(--fg-3);
  padding: 9px 0 11px; font-size: 11px; font-family: inherit; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
}
.tabbar button.on { color: var(--accent); }
.tabbar .ti { font-size: 19px; line-height: 1; }

/* ========== 我的 ========== */
.me-head { display: flex; gap: 14px; align-items: center; padding: 10px 20px 18px; }
.me-name { font-size: 18px; font-weight: 600; }
.stat-row { display: flex; gap: 10px; padding: 0 14px 18px; }
.stat {
  flex: 1; background: var(--bg-2); border: 1px solid var(--line);
  border-radius: 14px; padding: 12px 10px; text-align: center;
}
.stat b { display: block; font-size: 16px; font-weight: 600; }
.stat span { font-size: 11.5px; color: var(--fg-2); }

.group { margin: 0 14px 14px; background: var(--bg-2); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.row { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); font-size: 14.5px; }
.row:last-child { border-bottom: 0; }
.row.link { cursor: pointer; }
.row.danger { color: var(--danger); justify-content: center; }
.chev { color: var(--fg-3); }

.seg { display: flex; background: var(--bg-3); border-radius: 10px; padding: 2px; }
.seg button {
  appearance: none; border: 0; background: transparent; color: var(--fg-2);
  font-size: 13px; padding: 5px 12px; border-radius: 8px; font-family: inherit; cursor: pointer;
}
.seg button.on { background: var(--accent); color: #1a1408; font-weight: 600; }

/* ========== 聊天 ========== */
.chat-bg {
  position: absolute; inset: 0; z-index: -1;
  background-size: cover; background-position: center top;
  opacity: 0.5;
}
.chat-bg::after {
  content: ''; position: absolute; inset: 0;
  background: linear-gradient(180deg, rgba(14,15,19,0.25) 0%, rgba(14,15,19,0.9) 62%, var(--bg) 100%);
}
[data-theme="light"] .chat-bg::after {
  background: linear-gradient(180deg, rgba(246,245,242,0.25) 0%, rgba(246,245,242,0.92) 62%, var(--bg) 100%);
}

.messages {
  flex: 1; overflow-y: auto; padding: 8px 14px 12px;
  display: flex; flex-direction: column; gap: 10px;
  -webkit-overflow-scrolling: touch;
}
.msg { display: flex; gap: 9px; align-items: flex-start; max-width: 100%; }
.msg.me { flex-direction: row-reverse; }
.msg .avatar { width: 34px; height: 34px; font-size: 13px; margin-top: 2px; }
.bubble {
  position: relative; max-width: 78%;
  padding: 11px 14px; border-radius: 16px;
  white-space: pre-wrap; word-break: break-word;
  font-size: 15px; line-height: 1.72;
  background: var(--bubble-ai);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.06);
}
[data-theme="light"] .bubble { border-color: rgba(0,0,0,0.06); }
.msg.ai .bubble { border-top-left-radius: 5px; }
.msg.me .bubble { background: var(--bubble-me); border-top-right-radius: 5px; }
.bubble.pinned { border: 1px solid var(--accent-2); }
.bubble.pinned::before {
  content: '📌'; position: absolute; top: -8px; left: -6px; font-size: 12px;
}
.msg .t { font-size: 10.5px; color: var(--fg-3); text-align: center; width: 100%; }

.typing { padding: 0 22px 8px; display: flex; gap: 4px; }
.typing span {
  width: 6px; height: 6px; border-radius: 50%; background: var(--fg-3);
  animation: blink 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 60%, 100% { opacity: 0.25; } 30% { opacity: 1; } }

.composer {
  flex: 0 0 auto; display: flex; gap: 9px; align-items: flex-end;
  padding: 9px 12px calc(9px + var(--safe-b));
  background: rgba(14, 15, 19, 0.9); backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
[data-theme="light"] .composer { background: rgba(255,255,255,0.94); }
.composer textarea {
  flex: 1; max-height: 132px; min-height: 42px; padding: 10px 14px;
  border-radius: 21px; resize: none; line-height: 1.5;
}
.send-btn {
  flex: 0 0 auto; width: 42px; height: 42px; border-radius: 50%;
  border: 0; background: var(--accent); color: #1a1408; font-size: 17px;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.send-btn:disabled { opacity: 0.4; }

/* ========== 人设表单 ========== */
.pform { flex: 1; overflow-y: auto; padding: 0 16px calc(30px + var(--safe-b)); }
.cover-wrap { position: relative; margin-bottom: 34px; }
.cover {
  height: 150px; border-radius: 16px; background: var(--bg-3) center/cover no-repeat;
  display: flex; align-items: center; justify-content: center; cursor: pointer;
  border: 1px dashed var(--line);
}
.cover-avatar { position: absolute; left: 16px; bottom: -24px; border: 3px solid var(--bg); cursor: pointer; }
.field { display: block; margin-bottom: 16px; }
.field > span { display: block; font-size: 12.5px; color: var(--fg-2); margin-bottom: 6px; }
.field .count { float: right; font-size: 11.5px; color: var(--fg-3); margin-top: 5px; }
.two { display: flex; gap: 10px; }
.two .field { flex: 1; }

/* 风格模型选择器 */
.style-picker { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.style-card {
  appearance: none; text-align: left; font-family: inherit; cursor: pointer;
  background: var(--bg-3); border: 1.5px solid var(--line); color: var(--fg);
  border-radius: 12px; padding: 10px 12px; display: flex; flex-direction: column; gap: 3px;
}
.style-card b { font-size: 13.5px; font-weight: 600; }
.style-card i { font-style: normal; font-size: 11px; color: var(--fg-3); line-height: 1.45; }
.style-card.on { border-color: var(--accent); background: rgba(201, 162, 39, 0.1); }
.style-card.on b { color: var(--accent); }

/* ========== 底部弹层 ========== */
.sheet-mask {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55);
  z-index: 60; display: flex; align-items: flex-end;
}
.sheet {
  width: 100%; background: var(--bg-2);
  border-radius: 20px 20px 0 0; padding: 10px 10px calc(10px + var(--safe-b));
  animation: up 0.18s ease;
}
@keyframes up { from { transform: translateY(40px); opacity: 0.6; } }
.sheet button.act {
  display: block; width: 100%; text-align: center; appearance: none;
  border: 0; background: var(--bg-3); color: var(--fg); font-family: inherit;
  font-size: 15px; padding: 15px; margin-bottom: 8px; border-radius: 13px; cursor: pointer;
}
.sheet button.act.danger { color: var(--danger); }
.sheet-cancel {
  display: block; width: 100%; text-align: center; appearance: none;
  border: 0; background: transparent; color: var(--fg-2); font-family: inherit;
  font-size: 15px; padding: 13px; cursor: pointer;
}
.sheet .sheet-title { text-align: center; color: var(--fg-3); font-size: 12.5px; padding: 6px 0 12px; }
.sheet .pin-list { max-height: 46vh; overflow-y: auto; margin-bottom: 8px; }
.sheet .pin-item {
  display: flex; gap: 10px; align-items: flex-start; background: var(--bg-3);
  border-radius: 12px; padding: 11px 13px; margin-bottom: 8px; font-size: 13px; line-height: 1.6;
}
.sheet .pin-item .x { color: var(--danger); flex: 0 0 auto; cursor: pointer; padding: 0 4px; }

/* ========== Toast ========== */
.toast {
  position: fixed; left: 50%; bottom: 82px; transform: translateX(-50%);
  background: rgba(0,0,0,0.86); color: #fff; font-size: 13.5px;
  padding: 10px 18px; border-radius: 20px; z-index: 90; max-width: 82vw; text-align: center;
}
