/* ── 基础 ───────────────────────────────────────────────────────────── */
:root {
  --bg: #ffffff;
  --bg2: #f7f8fa;
  --panel: #ffffff;
  --panel2: #f3f4f6;
  --line: #e8eaed;
  --line2: #f1f2f4;
  --text: #1f2329;
  --dim: #6b7280;
  --dim2: #9aa0a6;
  --brand: #4d6bfe;
  --brand-weak: #eef2ff;
  --user-bubble: #e8f0fe;
  --user-bubble-border: #dbe7fd;
  --ok: #12a06a;
  --warn: #b26a00;
  --err: #d93025;
  --r: 16px;
  --sa-b: env(safe-area-inset-bottom, 0px);
  --sa-t: env(safe-area-inset-top, 0px);
}

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

html {
  color-scheme: light;
  -webkit-text-size-adjust: 100%;
}

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

body { overflow: hidden; }

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

button, input, textarea, select {
  font: inherit;
  color: inherit;
}
button { cursor: pointer; -webkit-appearance: none; background: none; border: 0; }
input, textarea, select { -webkit-appearance: none; }

.hidden { display: none !important; }

/* ── 顶栏 ──────────────────────────────────────────────────────────── */
#topbar {
  flex: 0 0 auto;
  padding: calc(var(--sa-t) + 9px) 12px 9px;
  display: flex;
  align-items: center;
  gap: 9px;
  background: rgba(255, 255, 255, .94);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--line2);
  min-height: 54px;
  z-index: 20;
}
#topbar .tb-title { font-weight: 600; font-size: 16px; letter-spacing: .1px; }
#topbar .tb-sub { font-size: 11.5px; color: var(--dim2); margin-top: -3px; }
#topbar .grow { flex: 1; min-width: 0; }
#topbar .tb-title, #topbar .tb-sub { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.avatar {
  width: 36px; height: 36px;
  flex: 0 0 auto;
  border-radius: 50%;
  display: grid; place-items: center;
  font-size: 18px;
  background: var(--bg2);
  border: 1px solid var(--line);
}

.icon-btn {
  flex: 0 0 auto;
  width: 36px; height: 36px;
  display: grid; place-items: center;
  border-radius: 10px;
  background: transparent;
  color: var(--dim);
  font-size: 16px;
  transition: background .15s, color .15s;
}
.icon-btn:active { background: var(--panel2); color: var(--text); }

/* ── 主视图 ────────────────────────────────────────────────────────── */
#view {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.pad { padding: 14px; }
.pad-b { padding-bottom: calc(96px + var(--sa-b)); }

/* ── 底栏 ──────────────────────────────────────────────────────────── */
#tabbar {
  flex: 0 0 auto;
  display: flex;
  padding: 5px 8px calc(5px + var(--sa-b));
  background: rgba(255, 255, 255, .96);
  backdrop-filter: blur(14px);
  border-top: 1px solid var(--line2);
  z-index: 20;
}
#tabbar button {
  flex: 1;
  color: var(--dim2);
  font-size: 10.5px;
  padding: 5px 0 3px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
#tabbar button .i { line-height: 0; opacity: .5; }
#tabbar button .i svg { display: block; }
#tabbar button.on .i { opacity: 1; }
#tabbar button.on { color: var(--brand); }

.icon-btn svg { display: block; }

/* ── 会话列表 ──────────────────────────────────────────────────────── */
.chat-item {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 12px;
  border-radius: var(--r);
  background: #fff;
  border: 1px solid var(--line);
  margin-bottom: 10px;
  transition: background .12s;
}
.chat-item:active { background: var(--bg2); }
.chat-item .ci-main { flex: 1; min-width: 0; }
.chat-item .ci-name { font-weight: 600; display: flex; gap: 6px; align-items: baseline; }
.chat-item .ci-time { font-size: 10.5px; color: var(--dim2); font-weight: 400; }
.chat-item .ci-last {
  font-size: 13px; color: var(--dim); margin-top: 1px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.empty {
  text-align: center; color: var(--dim2); font-size: 13px;
  padding: 64px 24px; line-height: 2;
}
.empty .big { font-size: 40px; display: block; margin-bottom: 8px; opacity: .7; }

/* ── 消息（DeepSeek 风格：TA 是纯文本，我是浅蓝气泡） ─────────────── */
.msgs { padding: 14px 16px 10px; display: flex; flex-direction: column; }

.row { display: flex; margin: 3px 0; }
.row.me { justify-content: flex-end; margin-top: 16px; }
.row.me + .row { margin-top: 12px; }

/* 默认 = 角色说的话：无气泡、整行铺开 */
.bubble {
  max-width: 100%;
  padding: 0;
  border: 0;
  background: none;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 15.5px;
  line-height: 1.78;
  color: var(--text);
  letter-spacing: .1px;
}
.bubble.continued { margin-top: 1px; }

/* 我说的：浅蓝圆角气泡，靠右 */
.row.me .bubble {
  max-width: 78%;
  padding: 9px 14px;
  border-radius: 18px;
  background: var(--user-bubble);
  border: 1px solid var(--user-bubble-border);
  font-size: 15px;
  line-height: 1.7;
}

.bubble strong { font-weight: 600; }
.bubble em { font-style: italic; color: #4b5563; }

.inner-voice {
  margin: 6px 0 2px;
  font-size: 12.5px;
  color: var(--dim2);
  font-style: italic;
  border-left: 2px solid var(--line);
  padding-left: 9px;
  max-width: 90%;
  line-height: 1.7;
}

.typing { display: inline-flex; gap: 4px; align-items: center; padding: 6px 0; }
.typing i {
  width: 6px; height: 6px; border-radius: 50%;
  background: #c7cad1;
  animation: bob 1.1s infinite;
}
.typing i:nth-child(2) { animation-delay: .16s; }
.typing i:nth-child(3) { animation-delay: .32s; }
@keyframes bob { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-4px); opacity: 1; } }

.sys-line {
  text-align: center; font-size: 11.5px; color: var(--dim2);
  margin: 16px 0 8px;
}

.msg-act { display: flex; gap: 6px; margin: 2px 0 0; }

/* ── 输入区（DeepSeek 那种：浅灰圆角框 + 蓝色圆按钮） ───────────── */
.composer {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  padding: 8px 12px calc(10px + var(--sa-b));
  background: linear-gradient(180deg, rgba(255, 255, 255, .5), #fff 40%);
  backdrop-filter: blur(14px);
  border-top: 1px solid var(--line2);
  display: flex;
  gap: 8px;
  align-items: flex-end;
  z-index: 30;
}
.composer textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--line);
  background: var(--bg2);
  border-radius: 20px;
  padding: 9px 15px;
  max-height: 34vh;
  min-height: 40px;
  line-height: 1.55;
  outline: none;
  font-size: 15px;
  color: var(--text);
}
.composer textarea::placeholder { color: var(--dim2); }
.composer textarea:focus { background: #fff; border-color: #cfd6e4; }
.send {
  flex: 0 0 auto;
  width: 38px; height: 38px;
  margin-bottom: 1px;
  border-radius: 50%;
  background: var(--brand);
  color: #fff;
  font-size: 17px;
  display: grid; place-items: center;
  font-weight: 600;
  transition: transform .12s, background .15s;
}
.send:active { transform: scale(.94); }
.send.stop { background: #e5484d; }
.send:disabled { opacity: .4; }

/* ── 卡片 / 表单 ───────────────────────────────────────────────────── */
.card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: 15px;
  margin-bottom: 12px;
  box-shadow: 0 1px 2px rgba(16, 24, 40, .03);
}
.card h3 {
  margin: 0 0 11px;
  font-size: 12px;
  font-weight: 600;
  color: var(--dim2);
  letter-spacing: 1px;
  text-transform: uppercase;
}

.field { margin-bottom: 12px; }
.field > label {
  display: block;
  font-size: 12.5px;
  color: var(--dim);
  margin-bottom: 5px;
}
.field .hint { margin-top: 5px; }
.hint { font-size: 11.5px; color: var(--dim2); line-height: 1.6; }

input[type=text], input[type=password], input[type=number], textarea, select {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg2);
  border-radius: 11px;
  padding: 9px 12px;
  outline: none;
  font-size: 14px;
  line-height: 1.5;
  transition: border-color .15s, box-shadow .15s, background .15s;
}
input:focus, textarea:focus, select:focus {
  background: #fff;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(77, 107, 254, .1);
}
input::placeholder, textarea::placeholder { color: var(--dim2); }
textarea { resize: vertical; min-height: 74px; }

.row2 { display: flex; gap: 10px; }
.row2 > * { flex: 1; min-width: 0; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  border-radius: 11px;
  padding: 10px 14px;
  font-size: 14px;
  font-weight: 500;
  transition: background .12s;
}
.btn:active { background: var(--bg2); }
.btn.primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
  font-weight: 600;
}
.btn.primary:active { background: #3f5ae0; }
.btn.ghost { background: var(--bg2); border-color: transparent; }
.btn.danger { color: var(--err); border-color: #f3d4d2; background: #fff; }
.btn.wide { width: 100%; }
.btn.sm { padding: 6px 10px; font-size: 12.5px; border-radius: 9px; }
.btn:disabled { opacity: .45; }

.switch-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 10px 0;
  border-bottom: 1px solid var(--line2);
}
.switch-row:last-child { border-bottom: 0; }
.switch-row .sr-t { font-size: 14.5px; }
.switch-row .sr-d { font-size: 11.5px; color: var(--dim2); margin-top: 1px; line-height: 1.5; }

.sw {
  flex: 0 0 auto;
  width: 44px; height: 26px;
  border-radius: 13px;
  background: #dfe1e6;
  position: relative;
  transition: background .18s;
}
.sw::after {
  content: "";
  position: absolute; top: 2px; left: 2px;
  width: 22px; height: 22px; border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(16, 24, 40, .2);
  transition: transform .18s;
}
.sw.on { background: var(--brand); }
.sw.on::after { transform: translateX(18px); }

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 9px; border-radius: 999px;
  background: var(--brand-weak);
  border: 1px solid #dde5ff;
  font-size: 11px; color: #3f5ae0;
}
.chip.warm { background: #fff1f0; border-color: #ffd8d6; color: #c5342f; }

/* ── 弹层 ──────────────────────────────────────────────────────────── */
.mask {
  position: fixed; inset: 0;
  background: rgba(17, 24, 39, .32);
  z-index: 60;
  display: flex;
  align-items: flex-end;
}
.sheet {
  width: 100%;
  max-height: 88vh;
  background: #fff;
  border-radius: 20px 20px 0 0;
  padding: 8px 16px calc(18px + var(--sa-b));
  overflow-y: auto;
  animation: up .22s ease-out;
  box-shadow: 0 -8px 32px rgba(16, 24, 40, .12);
}
@keyframes up { from { transform: translateY(24px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet-grip {
  width: 36px; height: 4px; border-radius: 3px;
  background: #e3e5e9; margin: 4px auto 12px;
}
.sheet h2 { font-size: 17px; margin: 0 0 3px; font-weight: 600; }
.sheet .sub { font-size: 12.5px; color: var(--dim2); margin-bottom: 14px; }

.toast {
  position: fixed;
  left: 50%; bottom: calc(84px + var(--sa-b));
  transform: translate(-50%, 14px);
  background: rgba(31, 35, 41, .94);
  color: #fff;
  padding: 9px 16px;
  border-radius: 10px;
  font-size: 13px;
  max-width: 84vw;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 90;
  box-shadow: 0 6px 20px rgba(16, 24, 40, .18);
}
.toast.on { opacity: 1; transform: translate(-50%, 0); }

.boot {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  background: #fff;
  color: var(--dim2);
  font-size: 13px;
  z-index: 200;
  transition: opacity .3s;
}
.boot.off { opacity: 0; pointer-events: none; }

/* ── 记忆条目 ──────────────────────────────────────────────────────── */
.mem {
  display: flex; gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line2);
  font-size: 13.5px;
  line-height: 1.65;
}
.mem:last-child { border-bottom: 0; }
.mem .mk {
  flex: 0 0 auto;
  font-size: 10px;
  color: var(--dim2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 1px 5px;
  height: fit-content;
  margin-top: 3px;
  white-space: nowrap;
}
.mem .mt { flex: 1; }
.mem.pinned .mt { color: #9a5b00; }
.mem .mx { flex: 0 0 auto; }

.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat {
  background: var(--bg2); border: 1px solid var(--line2);
  border-radius: 12px; padding: 9px; text-align: center;
}
.stat .v { font-size: 15.5px; font-weight: 600; color: var(--brand); }
.stat .k { font-size: 10.5px; color: var(--dim2); }

.progress {
  height: 6px; border-radius: 4px; background: #eef0f3;
  overflow: hidden; margin-top: 10px;
}
.progress > i { display: block; height: 100%; width: 0; background: var(--brand); transition: width .2s; }

.log {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  color: #4b5563;
  background: var(--bg2);
  border: 1px solid var(--line2);
  border-radius: 10px;
  padding: 9px;
  max-height: 200px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-all;
  margin-top: 8px;
  line-height: 1.6;
}

a { color: var(--brand); text-decoration: none; }

/* ── 滑杆 ──────────────────────────────────────────────────────────── */
input[type=range] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 30px;
  background: transparent;
  padding: 0;
  border: 0;
}
input[type=range]::-webkit-slider-runnable-track {
  height: 5px;
  border-radius: 4px;
  background: #e6e8ec;
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px; height: 18px;
  margin-top: -7px;
  border-radius: 50%;
  background: var(--brand);
  border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(16, 24, 40, .25);
}
input[type=range]::-moz-range-track {
  height: 5px; border-radius: 4px; background: #e6e8ec;
}
input[type=range]::-moz-range-thumb {
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--brand); border: 2px solid #fff;
}

/* ── 文件选择 ──────────────────────────────────────────────────────── */
.filebtn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px dashed #d8dbe2;
  border-radius: 11px;
  padding: 13px;
  color: var(--dim);
  font-size: 13px;
  background: var(--bg2);
}
.filebtn:active { background: #eef0f3; }
.filebtn input { display: none; }
.filebtn.has-file { border-style: solid; color: var(--brand); border-color: #c9d5fe; background: var(--brand-weak); }
