/* 中台助手 · 手机优先聊天 UI
 * 设计依据:ui-ux-pro-max —— Apple Native Experience(system 字体栈/语义色/
 * 8pt 间距/10-16px 圆角/150-300ms 过渡/44px 触控目标/safe-area);
 * 配色:深色 #0F0F23 底 + #1B1B30 卡片 + 靛蓝用户气泡;浅色跟随系统。
 * 字体:系统栈(中文回退 Noto Sans SC),不引外部 CDN。
 */

:root {
  color-scheme: light dark;
  --bg: #F8FAFC;
  --card: #FFFFFF;
  --fg: #0F172A;
  --fg-muted: #64748B;
  --border: #E2E8F0;
  --user-bubble: #4F46E5;
  --user-fg: #FFFFFF;
  --ai-bubble: #FFFFFF;
  --danger: #EF4444;
  --ring: #4F46E5;
  --radius: 16px;
  --radius-sm: 10px;
  --motion: 200ms ease-out;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0F0F23;
    --card: #1B1B30;
    --fg: #F8FAFC;
    --fg-muted: #94A3B8;
    --border: #312E81;
    --user-bubble: #4F46E5;
    --user-fg: #FFFFFF;
    --ai-bubble: #1B1B30;
    --danger: #EF4444;
    --ring: #A5B4FC;
  }
}

* { box-sizing: border-box; }
/* hidden 属性优先于任何组件自己的 display(否则 #gate/#app 的 grid 会顶掉它) */
[hidden] { display: none !important; }
html, body { height: 100%; }
body { overflow-x: hidden; } /* 固定定位抽屉等极端情况的横向溢出保险 */
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  background: var(--bg);
  color: var(--fg);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
}
:focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; }

/* ---------- 令牌门 ---------- */
#gate {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  background: var(--bg);
  padding: 16px;
}
.gate-card {
  width: min(360px, 100%);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px 20px;
  display: grid; gap: 12px;
}
.gate-card h1 { margin: 0; font-size: 20px; }
.gate-card p { margin: 0; color: var(--fg-muted); font-size: 13px; }
.gate-card code {
  background: var(--bg);
  border-radius: 4px;
  padding: 1px 4px;
  font-size: 12px;
  word-break: break-all;
}
.gate-card input {
  font: inherit;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--fg);
}
.gate-card button {
  background: var(--user-bubble);
  color: var(--user-fg);
  border-radius: var(--radius-sm);
  padding: 12px;
  min-height: 44px;
}
.gate-error { color: var(--danger); }

/* ---------- 布局 ---------- */
#app {
  height: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr); /* 列轨道硬锁容器宽:行内 min-content 再大也不撑破 */
  grid-template-rows: auto 1fr auto;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.topbar-title {
  flex: 1;
  min-width: 0; /* 允许被压缩:长标题靠 ellipsis,不撑破布局(nowrap + min-width:auto 会顶宽 grid 轨道) */
  text-align: center;
  font-weight: 600;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.icon-btn {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border-radius: var(--radius-sm);
  color: var(--fg);
  transition: background var(--motion);
}
.icon-btn:active { background: var(--border); }
.icon-btn svg { width: 22px; height: 22px; }

/* ---------- 会话抽屉 ---------- */
#drawer-backdrop {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 9;
}
#drawer {
  position: fixed;
  top: 0; bottom: 0; left: 0;
  width: min(300px, 82vw);
  background: var(--card);
  border-right: 1px solid var(--border);
  z-index: 10;
  transform: translateX(-100%);
  visibility: hidden; /* 关态连无障碍树一起藏 */
  transition: transform var(--motion), visibility 0s linear var(--motion);
  display: flex;
  flex-direction: column;
  padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
}
#drawer.open {
  transform: translateX(0);
  visibility: visible;
  transition: transform var(--motion);
}
.drawer-new {
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  text-align: center;
  margin-bottom: 12px;
  transition: background var(--motion);
}
.drawer-new:active { background: var(--border); }
#conv-list {
  list-style: none;
  margin: 0; padding: 0;
  overflow-y: auto;
  flex: 1;
}
#conv-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  cursor: pointer;
  transition: background var(--motion);
}
#conv-list li:active, #conv-list li.active { background: var(--border); }
#conv-list .conv-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 14px;
}
#conv-list .conv-del {
  width: 32px; height: 32px;
  display: grid; place-items: center;
  border-radius: 8px;
  color: var(--fg-muted);
  flex: none;
}
#conv-list .conv-del:active { color: var(--danger); background: var(--bg); }
.drawer-foot {
  padding-top: 8px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--fg-muted);
}

/* ---------- 消息区 ---------- */
#msgs {
  overflow-y: auto;
  padding: 16px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
}
.empty {
  margin: auto;
  text-align: center;
  color: var(--fg-muted);
  display: grid;
  gap: 8px;
  justify-items: center;
}
.empty h2 { margin: 0; font-size: 18px; color: var(--fg); }
.empty p { margin: 0; font-size: 14px; }

.msg {
  max-width: 86%;
  padding: 10px 14px;
  border-radius: var(--radius);
  word-break: break-word;
  overflow-wrap: anywhere;
  animation: rise var(--motion);
}
@keyframes rise {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: reduce) {
  .msg { animation: none; }
  #drawer, * { transition: none !important; scroll-behavior: auto !important; }
}
.msg.user {
  align-self: flex-end;
  background: var(--user-bubble);
  color: var(--user-fg);
  border-bottom-right-radius: 6px;
}
.msg.ai {
  align-self: flex-start;
  background: var(--ai-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}
.msg.ai > *:first-child { margin-top: 0; }
.msg.ai > *:last-child { margin-bottom: 0; }
.msg.ai p { margin: 8px 0; }
.msg.ai pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px;
  overflow-x: auto;
  font-size: 13px;
}
.msg.ai code {
  background: var(--bg);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 0.9em;
}
.msg.ai pre code { background: none; padding: 0; }
.msg.ai table { border-collapse: collapse; font-size: 14px; }
.msg.ai th, .msg.ai td { border: 1px solid var(--border); padding: 4px 8px; }
.msg.ai blockquote {
  margin: 8px 0;
  padding: 2px 12px;
  border-left: 3px solid var(--border);
  color: var(--fg-muted);
}
.msg .cursor {
  display: inline-block;
  width: 8px; height: 16px;
  background: currentColor;
  vertical-align: -2px;
  margin-left: 2px;
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.msg.error {
  align-self: center;
  background: none;
  border: 1px dashed var(--danger);
  color: var(--danger);
  font-size: 13px;
  max-width: 92%;
}

/* ---------- 输入区 ---------- */
.composer {
  padding: 8px 12px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg);
}
.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}
#input {
  flex: 1;
  min-width: 0; /* 同理:防止长内容顶宽 composer */
  font: inherit;
  color: var(--fg);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 11px 14px;
  resize: none;
  max-height: 132px;
  line-height: 1.4;
}
#input::placeholder { color: var(--fg-muted); }
.send-btn {
  width: 44px; height: 44px;
  border-radius: 50%;
  display: grid; place-items: center;
  background: var(--user-bubble);
  color: var(--user-fg);
  flex: none;
  transition: opacity var(--motion), transform var(--motion);
}
.send-btn:disabled { opacity: 0.35; }
.send-btn svg { width: 22px; height: 22px; }
.send-btn.streaming { background: var(--danger); }

/* ---- 联网检索来源(sources 事件渲染) ---- */
.sources {
  margin-top: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--card) 55%, transparent);
  border: 1px solid color-mix(in srgb, var(--fg-muted) 25%, transparent);
  font-size: 12.5px;
  line-height: 1.5;
}
.sources-label { color: var(--fg-muted); margin-bottom: 4px; }
.sources ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 3px;
}
.sources a {
  color: var(--fg);
  text-decoration: none;
  word-break: break-all;
}
.sources a:active { opacity: 0.6; }
