/* ============================================
   DSH Mobile - 手机端样式
   移动优先，深色主题，类原生 App 体验
   ============================================ */

:root {
  --bg: #0d1117;
  --bg-elev: #161b22;
  --bg-hover: #21262d;
  --border: #30363d;
  --text: #e6edf3;
  --text-dim: #8b949e;
  --accent: #4d8dff;
  --accent-hover: #6ba3ff;
  --danger: #f85149;
  --success: #3fb950;
  --user-bubble: #1f6feb;
  --user-bubble-text: #ffffff;
  --code-bg: #0a0e14;
  --blockquote-bg: rgba(77, 141, 255, 0.08);
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* 浅色主题 */
html[data-theme="light"] {
  --bg: #f6f8fa;
  --bg-elev: #ffffff;
  --bg-hover: #eaeef2;
  --border: #d0d7de;
  --text: #1f2328;
  --text-dim: #656d76;
  --accent: #0969da;
  --accent-hover: #0550ae;
  --danger: #cf222e;
  --success: #1a7f37;
  --user-bubble: #0969da;
  --user-bubble-text: #ffffff;
  --code-bg: #f6f8fa;
  --blockquote-bg: rgba(9, 105, 218, 0.06);
}

@media (prefers-color-scheme: light) {
  html[data-theme="auto"] {
    --bg: #f6f8fa;
    --bg-elev: #ffffff;
    --bg-hover: #eaeef2;
    --border: #d0d7de;
    --text: #1f2328;
    --text-dim: #656d76;
    --accent: #0969da;
    --accent-hover: #0550ae;
    --danger: #cf222e;
    --success: #1a7f37;
    --user-bubble: #0969da;
    --user-bubble-text: #ffffff;
    --code-bg: #f6f8fa;
    --blockquote-bg: rgba(9, 105, 218, 0.06);
  }
}

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

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}

body {
  display: flex;
  flex-direction: column;
}

/* ---------- 顶部栏 ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  flex: none;
  z-index: 10;
}

.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.app-title {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 0.5px;
}
.app-title em {
  font-style: normal;
  color: var(--accent);
}

.icon-btn {
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--text-dim);
  font-size: 18px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:active {
  background: var(--bg-hover);
}

.balance-badge {
  padding: 5px 10px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--success);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.balance-badge.error {
  color: var(--danger);
}

/* ---------- 消息区 ---------- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

.empty-hint {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--text-dim);
}
.empty-hint.hidden { display: none; }
.empty-hint .logo {
  width: 72px;
  height: 72px;
  border-radius: 22px;
  background: linear-gradient(135deg, #4d8dff, #5ad0fa);
  color: #fff;
  font-size: 40px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8px 24px rgba(77, 141, 255, 0.35);
  margin-bottom: 8px;
}
.empty-hint p { font-size: 15px; }
.empty-hint .sub { font-size: 13px; }

.message {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  animation: msg-in 0.2s ease;
}
@keyframes msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.message .avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
}
.message.user .avatar { background: var(--user-bubble); color: var(--user-bubble-text); }
.message.assistant .avatar { background: linear-gradient(135deg, var(--accent), #5ad0fa); color: var(--user-bubble-text); }

.message .bubble {
  max-width: 78%;
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 15px;
}
.message.user { flex-direction: row-reverse; }
.message.user .bubble {
  background: var(--user-bubble);
  color: var(--user-bubble-text);
  border-bottom-right-radius: 4px;
}
.message.assistant .bubble {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

/* ---------- 输入区 ---------- */
.inputbar {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 10px 12px;
  padding-bottom: calc(10px + var(--safe-bottom));
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
  flex: none;
}

.inputbar textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  padding: 10px 12px;
  font-size: 15px;
  line-height: 1.4;
  max-height: 120px;
  font-family: inherit;
  outline: none;
}
.inputbar textarea:focus {
  border-color: var(--accent);
}
.inputbar textarea::placeholder {
  color: var(--text-dim);
}

.send-btn {
  flex: none;
  padding: 10px 18px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: var(--user-bubble-text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.send-btn:active { background: var(--accent-hover); }
.send-btn:disabled { opacity: 0.6; }

/* ---------- 设置面板 ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 90;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}
.overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.settings-panel {
  position: fixed;
  right: 0;
  top: 0;
  bottom: 0;
  width: 82%;
  max-width: 340px;
  background: var(--bg-elev);
  z-index: 100;
  transform: translateX(100%);
  transition: transform 0.28s ease;
  display: flex;
  flex-direction: column;
  padding-bottom: var(--safe-bottom);
  overflow-y: auto;
}
.settings-panel.open { transform: translateX(0); }

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border);
}
.settings-header h2 { font-size: 17px; }

.settings-section {
  padding: 16px;
  border-bottom: 1px solid var(--border);
}
.settings-section h3 {
  font-size: 14px;
  color: var(--text-dim);
  margin-bottom: 8px;
  font-weight: 600;
}
.settings-section .hint {
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 10px;
  line-height: 1.5;
}

.settings-section input,
.settings-section select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  outline: none;
  margin-bottom: 10px;
}
.settings-section input:focus,
.settings-section select:focus { border-color: var(--accent); }

.settings-actions {
  display: flex;
  gap: 10px;
}
.btn {
  flex: 1;
  padding: 10px;
  border: none;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.btn.primary { background: var(--accent);
  color: var(--user-bubble-text); }
.btn.primary:active { background: var(--accent-hover); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }

.footer-note {
  padding: 16px;
  font-size: 12px;
  color: var(--text-dim);
  text-align: center;
  margin-top: auto;
}

/* ---------- 在线状态点 ---------- */
.online-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  margin-left: 4px;
}
.online-dot.on { background: var(--success); box-shadow: 0 0 6px var(--success); }
.online-dot.off { background: var(--danger); box-shadow: 0 0 6px var(--danger); }

/* ---------- 发送/停止按钮切换 ---------- */
.send-btn.stop {
  background: var(--danger);
  font-size: 18px;
  line-height: 1;
  padding: 10px 16px;
}

/* ---------- Markdown 渲染 ---------- */
.md-body {
  font-size: 15px;
  line-height: 1.6;
}
.md-body p { margin: 0 0 8px; }
.md-body p:last-child { margin-bottom: 0; }
.md-body h1, .md-body h2, .md-body h3,
.md-body h4, .md-body h5, .md-body h6 {
  margin: 10px 0 6px;
  font-weight: 700;
  line-height: 1.35;
}
.md-body h1 { font-size: 19px; }
.md-body h2 { font-size: 17px; }
.md-body h3 { font-size: 16px; }
.md-body h4, .md-body h5, .md-body h6 { font-size: 15px; }
.md-body ul, .md-body ol {
  margin: 4px 0 8px;
  padding-left: 20px;
}
.md-body li { margin: 2px 0; }
.md-body blockquote {
  margin: 6px 0;
  padding: 6px 12px;
  border-left: 3px solid var(--accent);
  background: rgba(77, 141, 255, 0.08);
  border-radius: 0 8px 8px 0;
  color: var(--text-dim);
}
.md-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 10px 0;
}
.md-body a {
  color: var(--accent);
  text-decoration: underline;
  word-break: break-all;
}
.md-body strong { font-weight: 700; }
.md-body em { font-style: italic; }
.inline-code {
  background: var(--bg-hover);
  padding: 2px 5px;
  border-radius: 4px;
  font-family: 'SF Mono', Consolas, 'Courier New', monospace;
  font-size: 13px;
}

/* ---------- 代码块 ---------- */
.code-block {
  margin: 8px 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--code-bg);
}
.code-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  background: var(--bg-hover);
  border-bottom: 1px solid var(--border);
}
.code-lang {
  font-size: 12px;
  color: var(--text-dim);
  font-family: 'SF Mono', Consolas, monospace;
}
.code-copy {
  padding: 3px 10px;
  font-size: 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
}
.code-copy:active { background: var(--bg-hover); }
.code-block pre {
  padding: 10px 12px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.code-block code {
  font-family: 'SF Mono', Consolas, 'Courier New', monospace;
  font-size: 13px;
  line-height: 1.5;
  color: var(--text);
  white-space: pre;
}

/* ---------- 代码语法高亮 ---------- */
.tk-kw { color: #ff7b72; font-weight: 600; }
.tk-str { color: #a5d6ff; }
.tk-com { color: #8b949e; font-style: italic; }
html[data-theme="light"] .tk-kw { color: #cf222e; }
html[data-theme="light"] .tk-str { color: #0a3069; }
html[data-theme="light"] .tk-com { color: #6e7781; }

/* ---------- 消息操作栏 ---------- */
.msg-actions {
  display: flex;
  gap: 8px;
  margin-top: 6px;
  opacity: 0.6;
}
.message:hover .msg-actions,
.message:active .msg-actions { opacity: 1; }
.msg-action {
  padding: 3px 10px;
  font-size: 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
}
.msg-action:active { background: var(--bg-hover); color: var(--text); }
.message.user .msg-actions { justify-content: flex-end; }
.message.user .msg-action { color: rgba(255, 255, 255, 0.8); border-color: rgba(255, 255, 255, 0.35); }

/* ---------- 分享按钮 ---------- */
.share-btn {
  flex: none;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text-dim);
  font-size: 18px;
  cursor: pointer;
}
.share-btn:active { background: var(--bg-hover); }

/* ---------- 语音输入按钮 ---------- */
.voice-btn {
  flex: none;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text-dim);
  font-size: 18px;
  cursor: pointer;
  transition: all 0.2s ease;
}
.voice-btn:active { background: var(--bg-hover); }
.voice-btn.recording {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
  animation: pulse 1s infinite;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(248, 81, 73, 0.5); }
  50% { box-shadow: 0 0 0 8px rgba(248, 81, 73, 0); }
}

/* ---------- 推理过程折叠块 ---------- */
.reasoning-block {
  margin: 6px 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--bg-hover);
}
.reasoning-header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  font-size: 12px;
  color: var(--text-dim);
  cursor: pointer;
  user-select: none;
}
.reasoning-header .toggle { font-size: 10px; transition: transform 0.2s; }
.reasoning-block.open .reasoning-header .toggle { transform: rotate(90deg); }
.reasoning-header .spinner {
  width: 10px;
  height: 10px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.reasoning-content {
  display: none;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.5;
  max-height: 240px;
  overflow-y: auto;
  white-space: pre-wrap;
}
.reasoning-block.open .reasoning-content { display: block; }

/* ---------- 会话列表面板（左侧滑出） ---------- */
.side-panel {
  position: fixed;
  top: 0;
  bottom: 0;
  width: 82%;
  max-width: 340px;
  background: var(--bg-elev);
  z-index: 100;
  display: flex;
  flex-direction: column;
  transition: transform 0.28s ease;
  overflow-y: auto;
  padding-bottom: var(--safe-bottom);
}
.side-panel.left {
  left: 0;
  transform: translateX(-100%);
}
.side-panel.left.open { transform: translateX(0); }

.session-list {
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.search-box {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}
.search-box input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  outline: none;
}
.search-box input:focus { border-color: var(--accent); }
.search-result-count {
  padding: 6px 14px;
  font-size: 12px;
  color: var(--text-dim);
}
.search-hit {
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 0 4px;
  background: rgba(77, 141, 255, 0.15);
}
.session-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid transparent;
}
.session-item:active { background: var(--bg-hover); }
.session-item.active {
  background: var(--bg-hover);
  border-color: var(--accent);
}
.session-item .session-title {
  flex: 1;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.session-item .session-time {
  font-size: 11px;
  color: var(--text-dim);
  flex: none;
}
.session-item .session-del {
  flex: none;
  width: 26px;
  height: 26px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-dim);
  font-size: 14px;
  cursor: pointer;
}
.session-item .session-del:active { color: var(--danger); background: var(--bg-hover); }

.session-item .session-rename {
  flex: none;
  width: 26px;
  height: 26px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-dim);
  font-size: 13px;
  cursor: pointer;
}
.session-item .session-rename:active { color: var(--accent); background: var(--bg-hover); }

.session-empty {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
}
