/* ═══════════════════════════════════════════
   DREAM AI — 移动端（竖屏）
   参考：ChatGPT iOS App 界面
   ═══════════════════════════════════════════ */

:root {
  --bg:            #000000;
  --surface:       #2c2c2e;
  --surface-hi:    #3a3a3c;
  --drawer-bg:     #0b0b0b;
  --text:          #ffffff;
  --muted:         #8e8e93;
  --muted-dim:     #6b6b70;
  --blue:          #0a84ff;
  --divider:       rgba(255,255,255,.08);

  --topbar-h:      56px;
  --drawer-w:      83%;

  /* ── 动效令牌 ── */
  --ease-ios:       cubic-bezier(.32, .72, 0, 1);   /* Apple 面板曲线 */
  --ease-out-quint: cubic-bezier(.23, 1, .32, 1);   /* 进场：起步快，尾段收 */
  --ease-out-expo:  cubic-bezier(.19, 1, .22, 1);
  --ease-in-out:    cubic-bezier(.65, 0, .35, 1);   /* 屏内位移 */
  --ease-spring:    cubic-bezier(.34, 1.42, .64, 1);/* 轻回弹，仅用于按压/开关 */

  --dur-micro:      140ms;   /* 按压、颜色 */
  --dur-ui:         220ms;   /* 元素进场 */
  --dur-panel:      320ms;   /* 抽屉、输入框展开 */
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  height: 100%;
  background: var(--bg);
  overscroll-behavior: none;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue",
               "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* 禁止双指缩放 / 双击缩放（与 viewport user-scalable=no 双保险） */
* {
  touch-action: pan-x pan-y;
}

/* 仅输入框与 AI 输出允许选中、允许长按系统菜单 */
textarea,
input {
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}

.msg--ai .bubble {
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}

button, textarea, input {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  outline: none;
}

button { cursor: pointer; -webkit-user-select: none; user-select: none; }
svg { display: block; }

/* ══════════ 主容器 ══════════ */

.app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  z-index: 1;
  overflow: hidden;
  /* translate / 圆角 由 JS 弹簧逐帧写入，这里不给 transition */
  translate: 0 0;
  will-change: translate;
}

/* 兜底：JS 未接管时仍可用 */
body.drawer-open .app {
  translate: var(--drawer-w) 0;
  border-top-left-radius: 14px;
  border-bottom-left-radius: 14px;
}

/* ══════════ 顶栏 ══════════ */

.topbar {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 16px 8px;
  height: calc(var(--topbar-h) + env(safe-area-inset-top, 0px));
}

.circle-btn {
  flex: none;
  width: 40px; height: 40px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  transition: background-color var(--dur-micro) ease,
              scale 340ms var(--ease-spring);
}
.circle-btn:active {
  background: var(--surface-hi);
  scale: .92;
  transition: background-color var(--dur-micro) ease, scale 90ms ease-out;
}
.circle-btn svg { width: 21px; height: 21px; }

.circle-btn--sm { width: 34px; height: 34px; }
.circle-btn--sm svg { width: 18px; height: 18px; }

.ghost-btn {
  flex: none;
  width: 38px; height: 38px;
  display: grid; place-items: center;
  color: var(--muted);
  border-radius: 50%;
  transition: background-color var(--dur-micro) ease,
              scale 340ms var(--ease-spring);
}
.ghost-btn:active { background: rgba(255,255,255,.07); scale: .92; transition: background-color var(--dur-micro) ease, scale 90ms ease-out; }
.ghost-btn svg { width: 23px; height: 23px; }

/* ══════════ 消息流 ══════════ */

.stream {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* 消息 */
.messages {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 8px 16px 16px;
}
.messages:empty { display: none; }

.msg { display: flex; max-width: 100%; }

.msg--user { justify-content: flex-end; }
.msg--user .bubble {
  background: var(--surface);
  border-radius: 20px;
  padding: 9px 15px;
  max-width: 82%;
  font-size: 16px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg--ai { justify-content: flex-start; }
.msg--ai .bubble {
  max-width: 100%;
  font-size: 16px;
  line-height: 1.55;
  word-break: break-word;
}

/* markdown 基础样式 */
.bubble p { margin: 0 0 10px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 0 0 10px; padding-left: 22px; }
.bubble li { margin: 3px 0; }
.bubble h1, .bubble h2, .bubble h3 { margin: 14px 0 8px; font-weight: 600; line-height: 1.3; }
.bubble h1 { font-size: 20px; }
.bubble h2 { font-size: 18px; }
.bubble h3 { font-size: 16.5px; }
.bubble code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .89em;
  background: rgba(255,255,255,.1);
  padding: 1.5px 5px;
  border-radius: 5px;
}
.bubble pre {
  margin: 0 0 10px;
  padding: 12px 14px;
  border-radius: 12px;
  background: #141416;
  border: 1px solid var(--divider);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.bubble pre code { background: none; padding: 0; font-size: 13.5px; line-height: 1.5; }
.bubble a { color: var(--blue); text-decoration: none; }
.bubble strong { font-weight: 600; }
.bubble hr { border: 0; border-top: 1px solid var(--divider); margin: 12px 0; }
.bubble img { max-width: 100%; border-radius: 14px; margin: 6px 0; display: block; }
.bubble table { border-collapse: collapse; margin: 0 0 10px; font-size: 14.5px; }
.bubble th, .bubble td { border: 1px solid var(--divider); padding: 6px 10px; }

/* 打字光标 */
.caret {
  display: inline-block;
  width: 7px; height: 17px;
  margin-left: 2px;
  vertical-align: -3px;
  background: var(--text);
  border-radius: 1.5px;
  animation: blink 1.05s steps(1, end) infinite;
}
@keyframes blink { 0%,50% { opacity: 1; } 50.01%,100% { opacity: 0; } }

/* 三点等待 */
.typing { display: inline-flex; gap: 5px; align-items: center; height: 22px; }
.typing i {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--muted-dim);
  animation: bounce 1.3s ease-in-out infinite;
}
.typing i:nth-child(2) { animation-delay: .18s; }
.typing i:nth-child(3) { animation-delay: .36s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .45; }
  30%           { transform: translateY(-5px); opacity: 1; }
}

/* ══════════ 底部 ══════════ */

.dock {
  flex: none;
  padding: 0 16px calc(env(safe-area-inset-bottom, 0px) + 10px);
  background: var(--bg);
}

/* 快捷卡片 */
.quick-cards {
  display: flex;
  flex-direction: column;
  padding-bottom: 10px;
  transition: opacity .2s ease, transform .2s ease;
}
.quick-cards[hidden] { display: none; }

.quick-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 2px;
  text-align: left;
  border-radius: 12px;
  transition: background .15s ease;
}
.quick-card:active { background: rgba(255,255,255,.06); }

.qc-icon {
  flex: none;
  width: 22px; height: 22px;
  display: grid; place-items: center;
  color: var(--muted);
}
.qc-icon svg { width: 100%; height: 100%; }
.qc-icon--coursedude { width: 21px; height: 21px; }

.qc-text {
  flex: 1;
  min-width: 0;
  font-size: 16.5px;
  line-height: 1.3;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 输入框：容器高度由 JS 按内容同步，过渡带动 ＋/工具 平滑位移 */
.composer {
  position: relative;
  height: 49px;                /* JS 会按内容改写 */
  background: var(--surface);
  border-radius: 24px;
  transition: height var(--dur-panel) var(--ease-out-quint),
              border-radius var(--dur-panel) var(--ease-out-quint);
}

/* 聚焦：圆角收紧，输入区升到顶部，＋ 与工具沉到底部（参考图 4） */
.composer:focus-within { border-radius: 22px; }

.plus-btn {
  position: absolute;
  left: 6px;
  bottom: 7px;
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--surface-hi);
  color: var(--text);
  transition: background-color var(--dur-micro) ease,
              scale 340ms var(--ease-spring);
}
.plus-btn:active {
  background: #4a4a4d;
  scale: .9;
  transition: background-color var(--dur-micro) ease, scale 90ms ease-out;
}
.plus-btn svg { width: 19px; height: 19px; }

.composer:focus-within .plus-btn { background: transparent; }
.composer:focus-within .plus-btn:active { background: rgba(255,255,255,.1); }
.composer:focus-within .plus-btn svg { width: 22px; height: 22px; }

#input {
  position: absolute;
  left: 46px;
  right: 112px;
  top: 6px;
  min-height: 37px;
  max-height: 132px;
  padding: 7px 2px 8px;
  font-size: 16px;
  line-height: 1.4;
  resize: none;
  overflow-y: auto;
  color: var(--text);
  caret-color: var(--blue);
  transition: right var(--dur-panel) var(--ease-out-quint);
}
#input::placeholder { color: var(--muted); }
/* 聚焦后工具沉到下方，输入区横向铺满 */
.composer:focus-within #input { right: 6px; }

.composer-tools {
  position: absolute;
  right: 6px;
  bottom: 7px;
  display: flex;
  align-items: center;
  gap: 2px;
}

.tool-btn {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border-radius: 50%;
  color: var(--text);
  transition: background-color var(--dur-micro) ease,
              scale 340ms var(--ease-spring);
}
.tool-btn[hidden] { display: none; }
.tool-btn:active {
  scale: .88;
  transition: background-color var(--dur-micro) ease, scale 90ms ease-out;
}
.tool-btn svg { width: 21px; height: 21px; }

.tool-btn--search { display: none; color: var(--blue); }
.composer:focus-within .tool-btn--search { display: grid; }
.tool-btn--search svg { width: 22px; height: 22px; }

.tool-btn--voice {
  background: var(--blue);
  color: #fff;
  width: 32px; height: 32px;
}
.tool-btn--voice svg { width: 19px; height: 19px; }

.tool-btn--send {
  background: var(--surface-hi);
  color: var(--text);
  width: 32px; height: 32px;
}
.tool-btn--send svg { width: 19px; height: 19px; }
.tool-btn--send.is-ready { background: var(--blue); color: #fff; }

/* ══════════ 抽屉 ══════════ */

.scrim {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(0,0,0,.45);
  opacity: 0;
  pointer-events: none;
  will-change: opacity;
}

.drawer {
  position: fixed;
  top: 0; bottom: 0; left: 0;
  z-index: 25;
  width: var(--drawer-w);
  display: flex;
  flex-direction: column;
  background: var(--drawer-bg);
  padding: calc(env(safe-area-inset-top, 0px) + 6px) 0 calc(env(safe-area-inset-bottom, 0px) + 14px);
  translate: -100% 0;
  will-change: translate;
  /* 垂直滚动交给浏览器，横向手势留给 JS 拖拽 */
  touch-action: pan-y;
}
/* 兜底：JS 未接管时仍可用 */
body.drawer-open .drawer { translate: 0 0; }

.drawer-head {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 16px 14px 20px;
}

.drawer-title {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -.4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.drawer-head-actions {
  flex: none;
  display: flex;
  align-items: center;
  gap: 2px;
}

.recent-label {
  flex: none;
  padding: 4px 22px 8px;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
}

.conv-list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0 10px;
}

.conv-item {
  display: block;
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  text-align: left;
  font-size: 16.5px;
  line-height: 1.35;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background .15s ease;
}
.conv-item:active { background: rgba(255,255,255,.08); }
.conv-item.is-active { background: rgba(255,255,255,.12); }

.conv-empty {
  padding: 10px 12px;
  font-size: 15px;
  color: var(--muted-dim);
}

.drawer-foot {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 14px 18px 0;
}

.new-chat-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 44px;
  padding: 0 22px 0 18px;
  border-radius: 999px;
  background: var(--blue);
  color: #fff;
  font-size: 17px;
  font-weight: 500;
  transition: transform .12s ease, filter .15s ease;
}
.new-chat-btn:active { transform: scale(.96); filter: brightness(.9); }
.new-chat-btn svg { width: 19px; height: 19px; }

/* ══════════ 动效 ══════════
   原则：只动 transform/opacity 系（translate / scale），
   进场用 ease-out（起步快、尾段收），屏内位移用 ease-in-out，
   按压用「按下快、松开弹」的双向 transition。 */

@keyframes enterUp   { from { opacity: 0; translate: 0 12px; } }
@keyframes enterSoft  { from { opacity: 0; translate: 0 10px; scale: .985; } }
@keyframes popIn      { from { opacity: 0; scale: .8; } }
@keyframes fadeIn     { from { opacity: 0; } }
@keyframes listIn     { from { opacity: 0; translate: -8px 0; } }

/* 消息入场：新消息才播，全量重绘不播 */
.msg.is-enter {
  animation: enterSoft var(--dur-ui) var(--ease-out-quint) backwards;
}

/* 快捷卡片：淡出而非消失，保持占位避免输入框跳动 */
.quick-cards {
  opacity: 1;
  translate: 0 0;
  transition: opacity var(--dur-ui) var(--ease-out-quint),
              translate var(--dur-ui) var(--ease-out-quint);
}
.quick-cards.is-hidden {
  opacity: 0;
  translate: 0 10px;
  pointer-events: none;
}

/* 抽屉会话列表：错峰滑入，间隔 22ms，总时长压在 400ms 内 */
.conv-item { animation: listIn 300ms var(--ease-out-quint) backwards; }
.conv-item:nth-child(1)  { animation-delay: 20ms; }
.conv-item:nth-child(2)  { animation-delay: 42ms; }
.conv-item:nth-child(3)  { animation-delay: 64ms; }
.conv-item:nth-child(4)  { animation-delay: 86ms; }
.conv-item:nth-child(5)  { animation-delay: 108ms; }
.conv-item:nth-child(6)  { animation-delay: 130ms; }
.conv-item:nth-child(7)  { animation-delay: 152ms; }
.conv-item:nth-child(8)  { animation-delay: 174ms; }
.conv-item:nth-child(9)  { animation-delay: 196ms; }
.conv-item:nth-child(10) { animation-delay: 218ms; }
.conv-item:nth-child(n+11) { animation-delay: 240ms; }

.conv-empty { animation: fadeIn var(--dur-ui) var(--ease-out-quint) backwards; }

/* 工具按钮（麦克风 ↔ 发送）切换时弹入 */
.tool-btn:not([hidden]) { animation: popIn 200ms var(--ease-out-quint) backwards; }

/* 抽屉底部按钮入场 */
.new-chat-btn, .drawer-foot .circle-btn {
  animation: enterUp 340ms var(--ease-out-quint) backwards;
  animation-delay: 60ms;
}

/* ══════════ 降级：用户要求减少动态效果 ══════════ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 1ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 1ms !important;
  }
  /* 位移类动画一律停用，保留状态切换本身 */
  .msg.is-enter,
  .conv-item,
  .conv-empty,
  .quick-cards,
  .tool-btn:not([hidden]),
  .new-chat-btn,
  .drawer-foot .circle-btn {
    animation: none !important;
  }
}

/* 仅指针设备做 hover 反馈（触屏点击会误触发 hover） */
@media (hover: hover) and (pointer: fine) {
  .conv-item:hover { background: rgba(255,255,255,.06); }
  .quick-card:hover { background: rgba(255,255,255,.06); }
  .circle-btn:hover { background: var(--surface-hi); }
}

/* ══════════ 安全区（非 iOS 环境兜底） ══════════ */
@supports not (padding: env(safe-area-inset-top)) {
  .topbar { padding-top: 12px; height: 60px; }
}

/* ══════════ 横屏 / 大屏：居中收窄，保持手机观感 ══════════ */
@media (min-width: 600px) {
  body { background: #0a0a0a; }
  .app {
    max-width: 430px;
    margin: 0 auto;
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    border-left: 1px solid var(--divider);
    border-right: 1px solid var(--divider);
  }
  body.drawer-open .app { transform: translateX(-50%); }
}
