:root {
  --bg: #faf6f2;
  --bg2: #f2e9e2;
  --ink: #2b2724;
  --muted: #8c8279;
  --accent: #b5543c;
  --accent-dark: #96432f;
  --accent-soft: #f2ded6;
  --card: #fffdfb;
  --line: #e9ded6;
  --ok: #5c7a5a;
  --radius: 18px;
  --shadow: 0 8px 28px rgba(85, 55, 45, 0.09);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  font-size: 15px;
  line-height: 1.65;
  overscroll-behavior: none;
}

#app { height: 100%; }

.view {
  height: 100%;
  display: flex;
  flex-direction: column;
}
[hidden] { display: none !important; }

/* ---------- 通用控件 ---------- */

.btn {
  border: 1px solid transparent;
  background: transparent;
  color: var(--ink);
  font: inherit;
  cursor: pointer;
  border-radius: 12px;
  padding: 9px 16px;
  transition: 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: scale(0.97); }
.btn.primary {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  letter-spacing: 0.02em;
}
.btn.primary:hover { background: var(--accent-dark); }
.btn.primary:disabled { background: #c9b3aa; cursor: default; }
.btn.ghost {
  border-color: var(--line);
  color: var(--muted);
  font-size: 13px;
  padding: 6px 12px;
  background: var(--card);
}
.btn.icon {
  width: 36px; height: 36px;
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 18px;
  color: var(--muted);
  border-radius: 50%;
}
.btn.icon:hover { background: var(--bg2); }
.btn.block { width: 100%; padding: 13px; font-size: 15px; }
.btn.danger { color: #a33a2c; border-color: #e7c6be; }

.field { display: block; margin-bottom: 14px; }
.field > span {
  display: block;
  font-size: 12.5px;
  color: var(--muted);
  margin-bottom: 6px;
  letter-spacing: 0.04em;
}
.field input, .field textarea, .composer textarea {
  width: 100%;
  font: inherit;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 11px 14px;
  outline: none;
  transition: 0.15s;
}
.field input:focus, .field textarea:focus, .composer textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(181, 84, 60, 0.1);
}
.field textarea { resize: vertical; min-height: 120px; line-height: 1.6; }

.err { color: #a33a2c; font-size: 13px; min-height: 18px; margin: 4px 0 0; }

/* ---------- 登录页 ---------- */

.auth-wrap {
  margin: auto;
  width: min(420px, 100% - 40px);
  padding: 40px 0;
}
.brand { text-align: center; margin-bottom: 32px; }
.brand-mark {
  width: 58px; height: 58px;
  margin: 0 auto 16px;
  border-radius: 18px;
  background: linear-gradient(150deg, #c9674c, #97432f);
  color: #fff;
  font-family: "Songti SC", "Noto Serif SC", serif;
  font-size: 28px;
  display: grid;
  place-items: center;
  box-shadow: 0 10px 24px rgba(150, 67, 47, 0.26);
}
.brand h1 {
  font-family: "Songti SC", "Noto Serif SC", serif;
  font-size: 27px;
  font-weight: 600;
  letter-spacing: 0.16em;
  margin: 0 0 8px;
  text-indent: 0.16em;
}
.brand-sub { color: var(--muted); font-size: 13.5px; margin: 0; letter-spacing: 0.03em; }

.tabs {
  display: flex;
  background: var(--bg2);
  border-radius: 14px;
  padding: 4px;
  margin-bottom: 22px;
}
.tab {
  flex: 1;
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 14px;
  color: var(--muted);
  padding: 9px;
  border-radius: 11px;
  cursor: pointer;
  transition: 0.18s;
}
.tab.active {
  background: var(--card);
  color: var(--ink);
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(85, 55, 45, 0.08);
}
.stack { display: block; }

.foot-note {
  text-align: center;
  color: #b3a89f;
  font-size: 12px;
  margin-top: 28px;
}

.site-foot {
  flex: 0 0 auto;
  text-align: center;
  color: #b8ada4;
  font-size: 11.5px;
  letter-spacing: 0.02em;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  margin-top: 18px;
}
.site-foot a {
  color: inherit;
  text-decoration: none;
}
.site-foot a:hover { color: var(--muted); text-decoration: underline; }
.site-foot .dot { margin: 0 7px; opacity: 0.6; }

/* ---------- 顶栏 ---------- */

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
}
.topbar-title {
  font-family: "Songti SC", "Noto Serif SC", serif;
  font-size: 19px;
  letter-spacing: 0.08em;
}
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.pill {
  font-size: 12px;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 999px;
  padding: 4px 11px;
  white-space: nowrap;
}

/* ---------- 角色列表 ---------- */

.list-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 4px 18px calc(120px + env(safe-area-inset-bottom));
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
}
.char-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
}
.char-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 14px;
  cursor: pointer;
  transition: 0.18s;
  position: relative;
  overflow: hidden;
}
.char-card:hover {
  border-color: #dcc7bd;
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}
.char-card::after {
  content: "";
  position: absolute;
  inset: 0 auto auto 0;
  width: 100%;
  height: 3px;
  background: linear-gradient(90deg, #d0795f, #e8bda9);
  opacity: 0.75;
}
.char-emoji { font-size: 26px; line-height: 1; margin-bottom: 10px; }
.char-name {
  font-family: "Songti SC", "Noto Serif SC", serif;
  font-size: 16.5px;
  font-weight: 600;
  margin-bottom: 3px;
}
.char-tag {
  font-size: 12.5px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.char-meta { margin-top: 10px; font-size: 11.5px; color: #b3a89f; }
.empty { text-align: center; color: var(--muted); padding: 48px 0; font-size: 14px; }

.fab {
  position: fixed;
  right: 22px;
  bottom: calc(26px + env(safe-area-inset-bottom));
  width: 54px; height: 54px;
  border-radius: 50%;
  border: 0;
  background: var(--accent);
  color: #fff;
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 10px 24px rgba(150, 67, 47, 0.34);
  transition: 0.18s;
}
.fab:hover { background: var(--accent-dark); transform: translateY(-2px); }

/* ---------- 聊天 ---------- */

.chat-topbar { border-bottom: 1px solid var(--line); }
.chat-topbar .topbar-title { flex: 1; }
.chat-title { display: flex; align-items: center; gap: 7px; }
.chat-emoji { font-size: 17px; }
.chat-name {
  font-family: "Songti SC", "Noto Serif SC", serif;
  font-size: 17px;
  letter-spacing: 0.06em;
}

.chat-scroll {
  flex: 1 1 auto;
  overflow-y: auto;
  scroll-behavior: smooth;
}
.messages {
  max-width: 760px;
  margin: 0 auto;
  padding: 18px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.msg { display: flex; flex-direction: column; max-width: 86%; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.bot { align-self: flex-start; align-items: flex-start; }

.msg-name {
  font-size: 11.5px;
  color: #a89b91;
  margin: 0 4px 5px;
  letter-spacing: 0.05em;
}
.bubble {
  padding: 11px 15px;
  border-radius: 18px 18px 18px 6px;
  background: var(--card);
  border: 1px solid var(--line);
  white-space: pre-wrap;
  word-break: break-word;
  box-shadow: 0 1px 3px rgba(85, 55, 45, 0.04);
}
.msg.user .bubble {
  background: var(--accent);
  color: #fff;
  border-color: transparent;
  border-radius: 18px 18px 6px 18px;
}
.bubble em {
  font-style: normal;
  color: #9a8a7f;
}
.msg.user .bubble em { color: rgba(255, 255, 255, 0.78); }

.cursor::after {
  content: "▍";
  color: var(--accent);
  animation: blink 1.05s steps(2, start) infinite;
}
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }

.typing { display: inline-flex; gap: 4px; padding: 4px 2px; }
.typing i {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: #c9b3aa;
  animation: bob 1.1s infinite ease-in-out;
}
.typing i:nth-child(2) { animation-delay: 0.15s; }
.typing i:nth-child(3) { animation-delay: 0.3s; }
@keyframes bob { 0%, 80%, 100% { transform: translateY(0); opacity: 0.55; } 40% { transform: translateY(-4px); opacity: 1; } }

.sys-hint {
  align-self: center;
  font-size: 12px;
  color: #b3a89f;
  background: var(--bg2);
  padding: 4px 12px;
  border-radius: 999px;
  max-width: 100%;
  text-align: center;
}

.composer {
  flex: 0 0 auto;
  display: flex;
  gap: 9px;
  align-items: flex-end;
  padding: 10px 14px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--bg);
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
}
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 132px;
  min-height: 44px;
  background: var(--card);
  line-height: 1.5;
}
.btn.send { padding: 11px 20px; height: 44px; }

/* ---------- 弹层 ---------- */

.sheet-mask {
  position: fixed;
  inset: 0;
  background: rgba(43, 39, 36, 0.42);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
  animation: fade 0.16s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.sheet {
  background: var(--bg);
  width: min(560px, 100%);
  max-height: 88vh;
  border-radius: 22px 22px 0 0;
  display: flex;
  flex-direction: column;
  animation: rise 0.2s ease;
  padding-bottom: env(safe-area-inset-bottom);
}
@keyframes rise { from { transform: translateY(24px); opacity: 0.6; } to { transform: none; opacity: 1; } }

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px 8px;
}
.sheet-title {
  font-family: "Songti SC", "Noto Serif SC", serif;
  font-size: 17px;
  letter-spacing: 0.06em;
}
.sheet-body {
  padding: 8px 18px calc(22px + env(safe-area-inset-bottom));
  overflow-y: auto;
}

.menu-item {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: var(--card);
  border: 1px solid var(--line);
  font: inherit;
  color: var(--ink);
  padding: 14px 16px;
  border-radius: 14px;
  margin-bottom: 9px;
  cursor: pointer;
  transition: 0.15s;
}
.menu-item:hover { border-color: #dcc7bd; }
.menu-item small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }

.row { display: flex; gap: 9px; }
.row > * { flex: 1; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 90px;
  transform: translateX(-50%);
  background: rgba(43, 39, 36, 0.9);
  color: #fff;
  font-size: 13.5px;
  padding: 10px 18px;
  border-radius: 999px;
  z-index: 99;
  max-width: 84vw;
  text-align: center;
  animation: fade 0.16s ease;
}

/* ---------- 管理后台 ---------- */

.admin-body {
  max-width: 900px;
  margin: 0 auto;
  padding: 20px 18px 60px;
}
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 11px;
  margin-bottom: 22px;
}
.stat {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px 16px;
}
.stat b { display: block; font-size: 22px; font-weight: 600; font-family: "Songti SC", serif; }
.stat span { font-size: 12px; color: var(--muted); }
.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px;
  margin-bottom: 18px;
}
.panel h2 {
  font-family: "Songti SC", "Noto Serif SC", serif;
  font-size: 16px;
  margin: 0 0 12px;
  letter-spacing: 0.06em;
}
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-weight: 500; font-size: 12px; }
td input[type="number"] {
  width: 72px;
  font: inherit;
  font-size: 13px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px 7px;
  background: var(--bg);
}
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0.06em; }
