/* ===========================================================
   Chat2Me 手机版样式：移动优先、安全区适配、浅色/深色
   =========================================================== */

:root {
  --fs: 16px;
  --bg: #eef0f4;
  --panel: #ffffff;
  --panel-alt: #f5f6f8;
  --border: #dfe3e8;
  --fg: #1f2329;
  --sub: #8b919c;
  --accent: #2f6fed;
  --accent-hover: #2a63d4;
  --accent-fg: #ffffff;
  --chat-bg: #f6f7f9;
  --user-bg: #d6e6ff;
  --user-fg: #12283f;
  --agent-bg: #ffffff;
  --agent-fg: #1f2329;
  --think-bg: #eceef2;
  --think-fg: #6b7280;
  --danger: #d94a4a;
  --shadow: 0 6px 24px rgba(20, 30, 50, .14);
  --radius: 14px;
}

:root[data-theme="dark"] {
  --bg: #16181d;
  --panel: #22252c;
  --panel-alt: #2a2e37;
  --border: #363b46;
  --fg: #e6e8ec;
  --sub: #9aa1ad;
  --accent: #4c8dff;
  --accent-hover: #3f7bea;
  --chat-bg: #16181d;
  --user-bg: #2b4a7d;
  --user-fg: #eaf1ff;
  --agent-bg: #22252c;
  --agent-fg: #e6e8ec;
  --think-bg: #262a33;
  --think-fg: #9aa1ad;
  --danger: #e06a6a;
  --shadow: 0 6px 24px rgba(0, 0, 0, .5);
}

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

/* 关键：HTML 的 hidden 属性靠浏览器默认样式 display:none 生效，
   而作者样式里的 display:flex 会盖过它 —— 于是 .sheet / .drawer
   就算带 hidden 也会铺满整屏，表现为"一片纯色、什么都看不见"。
   这里强制 hidden 优先。 */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--fg);
  font-size: var(--fs);
  line-height: 1.55;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei UI",
               "Hiragino Sans GB", "Source Han Sans SC", "Noto Sans CJK SC", sans-serif;
  overflow: hidden;
}

button, input, textarea, select {
  font-family: inherit;
  font-size: var(--fs);
  color: inherit;
}

/* ------------------------------------------------------------ 主框架 */

#app {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  /* 三级兜底：JS 校正值 → 动态视口单位 → 老浏览器 100% */
  height: var(--app-height, 100dvh);
  min-height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.topbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: calc(env(safe-area-inset-top, 0px) + 8px) 8px 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.topbar-title {
  flex: 1;
  min-width: 0;
  text-align: center;
}

.topbar-title .t1 {
  font-size: calc(var(--fs) + 1px);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-title .t2 {
  font-size: calc(var(--fs) - 3px);
  color: var(--sub);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  flex: none;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  font-size: calc(var(--fs) + 4px);
  line-height: 1;
  cursor: pointer;
}

.icon-btn:active { background: var(--panel-alt); }

/* ------------------------------------------------------------ 消息区 */

.messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px 12px 16px;
  background: var(--chat-bg);
}

.day-sep {
  text-align: center;
  color: var(--sub);
  font-size: calc(var(--fs) - 3px);
  margin: 10px 0 14px;
}

.msg {
  margin-bottom: 14px;
  max-width: 100%;
  animation: fade-in .18s ease;
}

@keyframes fade-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.msg-head {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: calc(var(--fs) - 3px);
  color: var(--sub);
  margin-bottom: 4px;
}

.msg-user .msg-head { justify-content: flex-end; }

.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 7px;
  background: var(--panel-alt);
  font-size: calc(var(--fs) - 3px);
  flex: none;
}

.msg-body {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.msg-user .msg-body { align-items: flex-end; }

.bubble {
  max-width: 88%;
  padding: 9px 12px;
  border-radius: var(--radius);
  background: var(--agent-bg);
  color: var(--agent-fg);
  border: 1px solid var(--border);
  overflow-wrap: break-word;
  word-break: break-word;
  white-space: normal;
}

.msg-user .bubble {
  background: var(--user-bg);
  color: var(--user-fg);
  border-color: transparent;
  border-bottom-right-radius: 5px;
}

.msg-agent .bubble { border-bottom-left-radius: 5px; }

.bubble p { margin: 0 0 6px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble .md-gap { height: 6px; }
.bubble .md-h { font-weight: 600; margin-top: 6px; }
.bubble ul { margin: 4px 0; padding-left: 20px; }
.bubble li { margin: 2px 0; }
.bubble code {
  background: var(--panel-alt);
  border-radius: 4px;
  padding: 1px 4px;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: .92em;
}
.bubble pre.code {
  margin: 6px 0;
  padding: 9px 10px;
  background: var(--panel-alt);
  border-radius: 10px;
  overflow-x: auto;
}
.bubble pre.code code { background: none; padding: 0; font-size: .9em; }
.code-lang { display: block; color: var(--sub); font-size: .8em; margin-bottom: 4px; }

.bubble .cursor {
  display: inline-block;
  width: 6px;
  height: 1em;
  vertical-align: -2px;
  background: var(--accent);
  animation: blink 1s steps(2) infinite;
}

@keyframes blink { 50% { opacity: 0; } }

.msg-foot {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 4px;
  font-size: calc(var(--fs) - 4px);
  color: var(--sub);
}

.mini-btn {
  border: 0;
  background: transparent;
  color: var(--sub);
  padding: 2px 4px;
  border-radius: 6px;
  font-size: calc(var(--fs) - 4px);
  cursor: pointer;
}

.mini-btn:active { background: var(--panel-alt); }

.think {
  max-width: 88%;
  margin-bottom: 6px;
  background: var(--think-bg);
  color: var(--think-fg);
  border-radius: 10px;
  font-size: calc(var(--fs) - 2px);
}

.think summary {
  cursor: pointer;
  padding: 6px 10px;
  list-style: none;
  font-style: italic;
}

.think summary::-webkit-details-marker { display: none; }
.think .think-body { padding: 0 10px 8px; white-space: pre-wrap; }

/* 提示卡片（错误 / 引导） */
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin: 12px 4px;
  text-align: left;
}

.card h3 { margin: 0 0 6px; font-size: calc(var(--fs) + 1px); }
.card p { margin: 0 0 10px; color: var(--sub); font-size: calc(var(--fs) - 2px); white-space: pre-wrap; }
.card .row { display: flex; gap: 8px; flex-wrap: wrap; }
.card.error { border-color: var(--danger); }
.card.error h3 { color: var(--danger); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }

.chip {
  border: 1px solid var(--border);
  background: var(--panel-alt);
  border-radius: 999px;
  padding: 7px 13px;
  font-size: calc(var(--fs) - 1px);
  cursor: pointer;
}

.chip:active { background: var(--border); }

/* ------------------------------------------------------------ 输入区 */

.composer {
  flex: none;
  padding: 8px 10px calc(env(safe-area-inset-bottom, 0px) + 8px);
  background: var(--panel);
  border-top: 1px solid var(--border);
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

#input {
  flex: 1;
  min-height: 40px;
  max-height: 34vh;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--panel-alt);
  color: var(--fg);
  resize: none;
  outline: none;
  line-height: 1.4;
  /* 不小于 16px，否则 iOS 聚焦时会自动放大页面 */
  font-size: max(var(--fs), 16px);
}

#input:focus { border-color: var(--accent); }

.send-btn, .stop-btn, .btn {
  flex: none;
  border: 0;
  border-radius: 20px;
  padding: 10px 16px;
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
  cursor: pointer;
}

.send-btn:disabled { opacity: .5; }
.stop-btn { background: var(--danger); }

.btn {
  display: block;
  width: 100%;
  border-radius: 12px;
  padding: 12px 16px;
  text-align: center;
}

.btn.ghost {
  background: var(--panel-alt);
  color: var(--fg);
  border: 1px solid var(--border);
  font-weight: 500;
}

.btn.danger { background: var(--danger); }

/* ------------------------------------------------------------ 抽屉 */

.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .35);
  z-index: 30;
  animation: fade-in .16s ease;
}

.drawer {
  position: fixed;
  z-index: 40;
  left: 0;
  top: 0;
  bottom: 0;
  width: min(84vw, 340px);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
  animation: slide-left .2s ease;
}

@keyframes slide-left { from { transform: translateX(-100%); } to { transform: none; } }

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 12px 10px;
  border-bottom: 1px solid var(--border);
}

.drawer-title { font-weight: 700; }

.drawer-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px;
  padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 20px);
}

.drawer-section { margin-top: 16px; }

.drawer-section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--sub);
  font-size: calc(var(--fs) - 3px);
  font-weight: 600;
  letter-spacing: .08em;
  margin-bottom: 6px;
}

.list { display: flex; flex-direction: column; gap: 2px; }

.list-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 8px;
  border-radius: 10px;
  cursor: pointer;
}

.list-item:active { background: var(--panel-alt); }
.list-item.active { background: var(--panel-alt); box-shadow: inset 3px 0 0 var(--accent); }
.list-item .li-main { flex: 1; min-width: 0; }
.list-item .li-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: calc(var(--fs) - 1px);
}
.list-item .li-sub {
  color: var(--sub);
  font-size: calc(var(--fs) - 3px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.list-item .li-avatar {
  flex: none;
  width: 30px;
  height: 30px;
  border-radius: 9px;
  background: var(--panel-alt);
  display: flex;
  align-items: center;
  justify-content: center;
}
.empty-hint { color: var(--sub); font-size: calc(var(--fs) - 2px); padding: 8px; }

/* ------------------------------------------------------------ 底部动作面板 */

.actionsheet {
  position: fixed;
  z-index: 50;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--panel);
  border-radius: 16px 16px 0 0;
  padding: 8px 10px calc(env(safe-area-inset-bottom, 0px) + 10px);
  box-shadow: var(--shadow);
  animation: slide-up .2s ease;
}

@keyframes slide-up { from { transform: translateY(100%); } to { transform: none; } }

.actionsheet .action {
  display: block;
  width: 100%;
  border: 0;
  background: transparent;
  text-align: left;
  padding: 13px 12px;
  border-radius: 10px;
  cursor: pointer;
}

.actionsheet .action:active { background: var(--panel-alt); }
.actionsheet .action.danger { color: var(--danger); }
.actionsheet .action.cancel { text-align: center; color: var(--sub); margin-top: 4px; }
.actionsheet-title { padding: 6px 12px 10px; color: var(--sub); font-size: calc(var(--fs) - 3px); }

/* ------------------------------------------------------------ 全屏面板 */

.sheet {
  position: fixed;
  z-index: 60;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  animation: slide-up .2s ease;
}

.sheet-head {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top, 0px) + 8px) 12px 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.sheet-head h2 {
  flex: 1;
  margin: 0;
  font-size: calc(var(--fs) + 1px);
  text-align: center;
}

.link-btn {
  border: 0;
  background: transparent;
  color: var(--accent);
  padding: 8px 4px;
  cursor: pointer;
  font-size: calc(var(--fs) - 1px);
}

.link-btn.strong { font-weight: 600; }
.link-btn.danger { color: var(--danger); }
.link-btn:disabled { color: var(--sub); }

.sheet-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 14px calc(env(safe-area-inset-bottom, 0px) + 28px);
}

.field { margin-bottom: 16px; }

.field label {
  display: block;
  margin-bottom: 6px;
  font-size: calc(var(--fs) - 2px);
  color: var(--sub);
}

.field .hint {
  margin-top: 5px;
  font-size: calc(var(--fs) - 4px);
  color: var(--sub);
}

.field input[type="text"],
.field input[type="password"],
.field input[type="number"],
.field textarea,
.field select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  color: var(--fg);
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

.field textarea { resize: vertical; min-height: 88px; line-height: 1.5; }
.field input:focus, .field textarea:focus, .field select:focus { border-color: var(--accent); }

.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 0;
  border-bottom: 1px solid var(--border);
}

.switch-row:last-child { border-bottom: 0; }
.switch-row .sw-label { font-size: calc(var(--fs) - 1px); }
.switch-row .sw-hint { color: var(--sub); font-size: calc(var(--fs) - 4px); }

.switch {
  flex: none;
  position: relative;
  width: 50px;
  height: 30px;
}

.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; }

.switch .track {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: var(--border);
  transition: background .16s;
}

.switch .track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  transition: transform .16s;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
}

.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(20px); }

.range-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.range-row input[type="range"] { flex: 1; accent-color: var(--accent); }
.range-row .range-val {
  flex: none;
  min-width: 46px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-size: calc(var(--fs) - 2px);
  color: var(--sub);
}

.group {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 16px;
}

.group > h4 {
  margin: 0 0 10px;
  font-size: calc(var(--fs) - 2px);
  color: var(--sub);
  font-weight: 600;
}

details.advanced { margin-bottom: 16px; }

details.advanced > summary {
  cursor: pointer;
  padding: 10px 12px;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: calc(var(--fs) - 1px);
  list-style: none;
}

details.advanced > summary::-webkit-details-marker { display: none; }
details.advanced[open] > summary { border-radius: 10px 10px 0 0; }
details.advanced .adv-body {
  border: 1px solid var(--border);
  border-top: 0;
  border-radius: 0 0 10px 10px;
  padding: 12px;
  background: var(--panel);
}

pre.preview {
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--panel-alt);
  border-radius: 10px;
  padding: 10px;
  font-size: calc(var(--fs) - 3px);
  max-height: 46vh;
  overflow: auto;
}

.about { color: var(--sub); font-size: calc(var(--fs) - 3px); line-height: 1.7; }

/* ------------------------------------------------------------ 提示条 */

.toast {
  position: fixed;
  z-index: 90;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 76px);
  transform: translate(-50%, 12px);
  max-width: 84vw;
  padding: 10px 16px;
  border-radius: 999px;
  background: rgba(20, 22, 26, .92);
  color: #fff;
  font-size: calc(var(--fs) - 2px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s, transform .18s;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* 桌面浏览器预览时限制宽度，方便看手机效果 */
@media (min-width: 700px) {
  #app, .drawer, .actionsheet, .sheet { max-width: 560px; margin: 0 auto; }
  .drawer { left: max(0px, calc(50vw - 280px)); }
  .sheet { left: 50%; right: auto; width: 560px; transform: translateX(-50%); }
  .actionsheet { left: 50%; right: auto; width: 560px; transform: translateX(-50%); }
  body { background: #d9dde3; }
  :root[data-theme="dark"] body { background: #0d0f12; }
}
