:root {
  --bg: #faf7f2;
  --surface: #ffffff;
  --surface-2: #f3ece3;
  --ink: #2b2622;
  --ink-soft: #6f655c;
  --ink-faint: #a2968a;
  --line: #e8ddd0;
  --accent: #e07a5f;
  --accent-soft: #fbe4dc;
  --accent-ink: #b8512f;
  --sage: #6b8f71;
  --danger: #c0392b;
  --radius: 18px;
  --shadow: 0 2px 14px rgba(70, 50, 30, .07);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --tab-h: 58px;
  --composer-h: 62px;
  --fs: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #191614;
    --surface: #221e1b;
    --surface-2: #2c2622;
    --ink: #f2eae2;
    --ink-soft: #b8aa9c;
    --ink-faint: #857768;
    --line: #3a322c;
    --accent: #e8927a;
    --accent-soft: #3d2a23;
    --accent-ink: #f0a68d;
    --sage: #8fb596;
    --shadow: 0 2px 16px rgba(0, 0, 0, .35);
  }
}

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

html, body {
  margin: 0; padding: 0; height: 100%;
  overflow: hidden;
  background: var(--bg); color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Noto Sans SC", system-ui, sans-serif;
  font-size: var(--fs); line-height: 1.6;
  overscroll-behavior: none;
}

#app {
  display: flex; flex-direction: column;
  height: 100vh;
  height: 100dvh;
  width: 100%;
  max-width: 760px; margin: 0 auto;
  position: relative;
  overflow: hidden;
}

/* ---------- top bar ---------- */
#topbar {
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + var(--safe-t)) 12px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
#titleBox { flex: 1; min-width: 0; text-align: center; }
#roomTitle { margin: 0; font-size: 16px; font-weight: 650; letter-spacing: .2px; }
#roomTag { margin: 0; font-size: 11.5px; color: var(--ink-faint); }

.icon-btn {
  width: 40px; height: 40px; flex: 0 0 40px;
  display: grid; place-items: center;
  border: none; border-radius: 12px; background: transparent;
  color: var(--ink-soft); font-size: 18px; cursor: pointer;
}
.icon-btn:active { background: var(--surface-2); }

/* ---------- messages ---------- */
#messages {
  flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden;
  padding: 14px 14px 6px;
  display: flex; flex-direction: column; gap: 12px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.msg { display: flex; flex-direction: column; max-width: 88%; animation: rise .18s ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 10px 14px; border-radius: 16px;
  white-space: pre-wrap; word-break: break-word;
  font-size: calc(var(--fs) - 0.5px);
}
.msg.user .bubble {
  background: var(--accent); color: #fff;
  border-bottom-right-radius: 6px;
}
.msg.assistant .bubble {
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line);
  border-bottom-left-radius: 6px;
  box-shadow: var(--shadow);
}
.msg.assistant .bubble p { margin: 0 0 .55em; }
.msg.assistant .bubble p:last-child { margin-bottom: 0; }
.msg.assistant .bubble ul, .msg.assistant .bubble ol { margin: .3em 0 .6em; padding-left: 1.3em; }
.msg.assistant .bubble li { margin: .15em 0; }
.msg.assistant .bubble code {
  background: var(--surface-2); padding: 1px 5px; border-radius: 5px;
  font-size: .9em; font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
}
.msg.assistant .bubble pre {
  background: var(--surface-2); padding: 10px 12px; border-radius: 10px;
  overflow-x: auto; margin: .5em 0;
}
.msg.assistant .bubble pre code { background: none; padding: 0; }
.msg.assistant .bubble strong { font-weight: 650; }

.msg .meta {
  font-size: 11px; color: var(--ink-faint);
  margin: 4px 6px 0; display: flex; gap: 10px; align-items: center;
}
.msg-time { font-size: 11px; color: var(--ink-faint); }
.msg .meta button {
  border: none; background: none; color: var(--ink-faint);
  font-size: 11px; cursor: pointer; padding: 2px 0;
}
.msg .meta button:hover { color: var(--accent); }

.typing { display: inline-flex; gap: 4px; align-items: center; height: 20px; }
.typing i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--ink-faint); animation: blink 1.2s infinite;
}
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 60%, 100% { opacity: .25; } 30% { opacity: 1; } }

.empty {
  margin: auto; text-align: center; color: var(--ink-faint);
  font-size: 14px; padding: 30px 20px; line-height: 1.9;
}
.empty b { display: block; color: var(--ink-soft); font-size: 17px; margin-bottom: 6px; font-weight: 600; }

/* ---------- starters ---------- */
.starters {
  display: flex; gap: 8px; padding: 4px 14px 8px;
  overflow-x: auto; scrollbar-width: none;
}
.starters::-webkit-scrollbar { display: none; }
.starters[hidden] { display: none; }
.starter {
  flex: 0 0 auto; padding: 7px 13px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--line);
  color: var(--ink-soft); font-size: 13px; cursor: pointer;
}
.starter:active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-ink); }

/* ---------- composer ---------- */
#composer {
  display: flex; flex-direction: column; gap: 6px;
  width: 100%; min-width: 0;
  padding: 8px 12px calc(8px + var(--safe-b));
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.composer-row { display: flex; align-items: flex-end; gap: 6px; width: 100%; min-width: 0; }
.tool-btn {
  width: 38px; height: 38px; flex: 0 0 38px;
  border: none; border-radius: 12px; background: transparent;
  font-size: 19px; cursor: pointer; display: grid; place-items: center;
  color: var(--ink-soft);
}
.tool-btn:active { background: var(--surface-2); }
.tool-btn.rec { background: var(--accent-soft); animation: pulse 1s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .55; } }

#attachPreview { position: relative; align-self: flex-start; }
#attachPreview img { max-height: 92px; border-radius: 12px; border: 1px solid var(--line); display: block; }

#quoteBar {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 10px; border-left: 3px solid var(--accent);
  background: var(--accent-soft); border-radius: 8px;
}
#quoteBar[hidden] { display: none; }
.quote-inner {
  flex: 1; min-width: 0; font-size: 12.5px; color: var(--ink-soft);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#quoteWho { font-weight: 600; color: var(--accent-ink); margin-right: 4px; }
#btnCancelQuote {
  border: none; background: transparent; color: var(--ink-soft);
  font-size: 14px; cursor: pointer; padding: 2px 4px;
}
.quote-block {
  border-left: 3px solid var(--accent); padding: 4px 8px; margin-bottom: 6px;
  background: rgba(255,255,255,.16); border-radius: 6px;
  font-size: 12.5px; opacity: .92;
}
.quote-block b { font-weight: 600; }
.msg.user .quote-block { background: rgba(255,255,255,.22); color: #fff; border-left-color: rgba(255,255,255,.75); }
#btnRemoveImg {
  position: absolute; top: -6px; right: -6px; width: 22px; height: 22px;
  border-radius: 50%; border: none; background: var(--ink); color: var(--bg);
  font-size: 12px; cursor: pointer; line-height: 1;
}
#input {
  flex: 1; min-width: 0; resize: none; max-height: 132px;
  padding: 10px 14px; border-radius: 20px;
  border: 1px solid var(--line); background: var(--surface);
  color: var(--ink); font: inherit; font-size: calc(var(--fs) - 0.5px); line-height: 1.5;
  outline: none;
}
#input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.send-btn {
  width: 42px; height: 42px; flex: 0 0 42px; border-radius: 50%;
  border: none; background: var(--accent); color: #fff;
  font-size: 20px; font-weight: 700; cursor: pointer;
  display: grid; place-items: center;
  transition: transform .12s, opacity .12s;
}
.send-btn:active { transform: scale(.94); }
.send-btn:disabled { opacity: .35; cursor: default; }

/* ---------- tabbar ---------- */
#tabbar {
  display: flex; width: 100%;
  overflow-x: auto; overflow-y: hidden;
  scrollbar-width: none;
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 94%, transparent);
  backdrop-filter: blur(12px);
  padding-bottom: var(--safe-b);
}
#tabbar::-webkit-scrollbar { display: none; }
.tab {
  flex: 1 0 auto; min-width: 68px; border: none; background: none; cursor: pointer;
  padding: 7px 0 6px; display: flex; flex-direction: column;
  align-items: center; gap: 2px; color: var(--ink-faint);
}
.tab.tab-add { min-width: 60px; opacity: .75; }
.tab span { font-size: 19px; line-height: 1; filter: grayscale(1); opacity: .55; }
.tab em { font-style: normal; font-size: 11px; }
.tab.active { color: var(--accent-ink); }
.tab.active span { filter: none; opacity: 1; }

/* ---------- sheet ---------- */
.sheet {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(20, 15, 10, .42);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .16s ease-out;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.sheet[hidden] { display: none; }
.sheet-inner {
  width: 100%; max-width: 760px; max-height: 90dvh; overflow-y: auto;
  background: var(--bg); border-radius: 22px 22px 0 0;
  padding: 16px 18px calc(24px + var(--safe-b));
  animation: slideUp .2s ease-out;
}
@keyframes slideUp { from { transform: translateY(24px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sheet-head h2 { margin: 0; font-size: 18px; }

.group { padding: 14px 0; border-top: 1px solid var(--line); }
.group:first-of-type { border-top: none; }
.group h3 { margin: 0 0 10px; font-size: 13px; font-weight: 650; color: var(--ink-soft); letter-spacing: .4px; }

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 10px; }
.field > span { font-size: 12.5px; color: var(--ink-soft); }
.field input[type="text"], .field input[type="password"], .field input[type="number"],
.field select, .field textarea, .group > textarea {
  width: 100%; padding: 9px 12px; font: inherit; font-size: 14.5px;
  border: 1px solid var(--line); border-radius: 11px;
  background: var(--surface); color: var(--ink); outline: none;
}
.field input:focus, .field select:focus, .field textarea:focus, .group > textarea:focus {
  border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.field input[type="range"] { width: 100%; accent-color: var(--accent); }

.row { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; margin-bottom: 0; }
.hint { margin: 4px 0 0; font-size: 12px; color: var(--ink-faint); line-height: 1.6; }

.btn {
  padding: 9px 14px; border-radius: 11px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); font: inherit; font-size: 13.5px;
  cursor: pointer;
}
.btn:active { transform: scale(.98); }
.btn.ghost { background: var(--surface); }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 35%, var(--line)); background: transparent; }
.btn.primary { background: var(--accent); color: #fff; border-color: transparent; }
.btn:disabled { opacity: .5; }

.check { display: flex; align-items: center; gap: 8px; font-size: 13.5px; color: var(--ink-soft); margin: 8px 0; }
.check input { width: 17px; height: 17px; accent-color: var(--accent); }
.font-label { font-size: 13px; color: var(--ink-faint); min-width: 46px; text-align: center; }

.msg .bubble img { max-width: 100%; border-radius: 12px; margin-bottom: 6px; display: block; }

.import-item { display: flex; gap: 10px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.import-item:last-child { border-bottom: none; }
.import-item.skip { opacity: .4; }
.import-info { flex: 1; min-width: 0; }
.import-title { font-size: 14px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.import-meta { font-size: 12px; color: var(--ink-faint); }
.import-item select {
  width: 104px; flex: 0 0 104px; padding: 7px 6px;
  border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); font: inherit; font-size: 12.5px;
}

.tune-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 14px 0; }
.tune-btn {
  padding: 14px 10px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); font: inherit; font-size: 14px;
  cursor: pointer;
}
.tune-btn:active { background: var(--accent-soft); border-color: var(--accent); }
.tune-btn:last-child:nth-child(odd) { grid-column: span 2; }

.save-note { text-align: center; font-size: 12px; color: var(--ink-faint); margin: 16px 0 0; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(84px + var(--safe-b));
  transform: translateX(-50%);
  background: var(--ink); color: var(--bg);
  padding: 9px 16px; border-radius: 999px; font-size: 13px;
  z-index: 90; max-width: 86vw; text-align: center;
  animation: fade .15s ease-out;
  box-shadow: 0 6px 22px rgba(0,0,0,.25);
}
.toast[hidden] { display: none; }

/* ---------- 隐私锁 ---------- */
.lock {
  position: fixed; inset: 0; z-index: 100;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.lock[hidden] { display: none; }
.lock-box { width: 100%; max-width: 340px; text-align: center; }
.lock-icon { font-size: 40px; margin-bottom: 8px; }
.lock-box h2 { margin: 0 0 6px; font-size: 20px; }
.lock-box input {
  width: 100%; margin: 16px 0 12px; padding: 12px 14px;
  border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink);
  font: inherit; font-size: 16px; text-align: center; outline: none;
}
.lock-box input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.lock-box .btn { width: 100%; padding: 12px; font-size: 15px; }
.lock-error { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 10px; }
.shake { animation: shake .3s; }
@keyframes shake {
  0%, 100% { transform: none; }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

/* ---------- rooms drawer (desktop hidden default) ---------- */
@media (min-width: 760px) {
  #app { border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
}
