/* =========================================================================
   B1 财务助手 Mini App — style.css
   完全跟随 Telegram themeParams (var(--tg-theme-*) + fallback), 不自带切换器。
   金额正/负是财务语义色 (Telegram 主题无), 自定义 --num-pos / --num-neg。
   ========================================================================= */

:root {
  /* Telegram 注入 --tg-theme-* CSS 变量; 这里给安全 fallback (浅色) */
  --bg:            var(--tg-theme-bg-color, #ffffff);
  --secondary-bg:  var(--tg-theme-secondary-bg-color, #f1f1f4);
  --text:          var(--tg-theme-text-color, #1c1c1e);
  --hint:          var(--tg-theme-hint-color, #8e8e93);
  --link:          var(--tg-theme-link-color, #2481cc);
  --button:        var(--tg-theme-button-color, #2481cc);
  --button-text:   var(--tg-theme-button-text-color, #ffffff);
  --header-bg:     var(--tg-theme-header-bg-color, var(--secondary-bg));
  --section-bg:    var(--tg-theme-section-bg-color, var(--bg));

  /* 财务正负色 — Telegram 无, 自定义; 暗色下由 [data-scheme] 调亮度 */
  --num-pos: #1f9d55;   /* 收入/正向/已对账 */
  --num-neg: #d64545;   /* 支出/欠款/逾期 */

  /* 用户气泡用按钮色; 助手气泡用 secondary-bg */
  --user-bubble:      var(--button);
  --user-bubble-text: var(--button-text);
  --bot-bubble:       var(--secondary-bg);
  --bot-bubble-text:  var(--text);

  --radius: 14px;
  --gap: 10px;
}

/* 暗色方案下提高正负色亮度 (保证可读对比) */
[data-scheme="dark"] {
  --num-pos: #4ad07d;
  --num-neg: #ff6b6b;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

body {
  display: flex;
  flex-direction: column;
  /* Telegram viewport: 用 100% + 安全区, 键盘弹出由 JS viewportChanged 调整 */
  height: 100vh;
  height: 100dvh;
}

/* ── 顶栏 ── */
.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 14px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--header-bg);
  border-bottom: 1px solid color-mix(in srgb, var(--hint) 22%, transparent);
}
.topbar-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 16px;
}
.topbar-icon { font-size: 18px; }
.reset-btn {
  appearance: none;
  border: none;
  background: color-mix(in srgb, var(--button) 14%, transparent);
  color: var(--link);
  font-size: 13px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
}
.reset-btn:active { opacity: 0.6; }

/* ── 消息区 ── */
.messages {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 12px 6px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  display: flex;
  max-width: 100%;
}
.msg.user { justify-content: flex-end; }
.msg.bot  { justify-content: flex-start; }

.bubble {
  max-width: 86%;
  padding: 9px 13px;
  border-radius: var(--radius);
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg.user .bubble {
  background: var(--user-bubble);
  color: var(--user-bubble-text);
  border-bottom-right-radius: 5px;
  white-space: pre-wrap;
}
.msg.bot .bubble {
  background: var(--bot-bubble);
  color: var(--bot-bubble-text);
  border-bottom-left-radius: 5px;
  /* 助手气泡可放宽 — 财务报表需要宽度 */
  max-width: 94%;
  width: 100%;
}

/* 占位 / 进度 文字 (流式前) */
.placeholder {
  color: var(--hint);
  font-style: italic;
  display: flex;
  align-items: center;
  gap: 7px;
}
.dots span {
  display: inline-block;
  width: 5px; height: 5px;
  margin: 0 1px;
  border-radius: 50%;
  background: var(--hint);
  animation: blink 1.4s infinite both;
}
.dots span:nth-child(2) { animation-delay: 0.2s; }
.dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

/* =========================================================================
   Markdown 渲染 (助手气泡内) — 响应式财务报表
   ========================================================================= */
.md > *:first-child { margin-top: 0; }
.md > *:last-child  { margin-bottom: 0; }

.md h1, .md h2, .md h3, .md h4 {
  margin: 0.7em 0 0.4em;
  line-height: 1.25;
  font-weight: 700;
}
.md h1 { font-size: 1.25em; }
.md h2 { font-size: 1.15em; }
.md h3 { font-size: 1.05em; }
.md h4 { font-size: 1.0em; }

.md p { margin: 0.45em 0; }
.md ul, .md ol { margin: 0.45em 0; padding-left: 1.3em; }
.md li { margin: 0.2em 0; }

.md a { color: var(--link); text-decoration: none; }
.md code {
  background: color-mix(in srgb, var(--hint) 20%, transparent);
  padding: 1px 5px;
  border-radius: 5px;
  font-size: 0.9em;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.md pre {
  background: color-mix(in srgb, var(--hint) 16%, transparent);
  padding: 10px;
  border-radius: 8px;
  overflow-x: auto;
}
.md pre code { background: none; padding: 0; }
.md blockquote {
  margin: 0.5em 0;
  padding: 4px 12px;
  border-left: 3px solid color-mix(in srgb, var(--hint) 45%, transparent);
  color: var(--hint);
}
.md hr {
  border: none;
  border-top: 1px solid color-mix(in srgb, var(--hint) 30%, transparent);
  margin: 0.8em 0;
}

/* ── 表格: 手机财务表核心 — 横向滚动 + sticky 表头 ── */
.md .table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0.55em 0;
  border-radius: 9px;
  border: 1px solid color-mix(in srgb, var(--hint) 22%, transparent);
}
.md table {
  border-collapse: collapse;
  width: 100%;
  min-width: max-content;   /* 列不挤压, 由 wrap 横向滚动 */
  font-size: 0.92em;
}
.md thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--secondary-bg);
  color: var(--text);
  font-weight: 700;
  text-align: left;
  white-space: nowrap;
}
.md th, .md td {
  padding: 7px 11px;
  border-bottom: 1px solid color-mix(in srgb, var(--hint) 18%, transparent);
  white-space: nowrap;
  vertical-align: top;
}
.md tbody tr:last-child td { border-bottom: none; }
.md tbody tr:nth-child(even) td {
  background: color-mix(in srgb, var(--hint) 7%, transparent);
}

/* ── 数字 / 金额高亮 ── */
.num {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}
.num.pos { color: var(--num-pos); font-weight: 600; }
.num.neg { color: var(--num-neg); font-weight: 600; }

/* =========================================================================
   底部输入区
   ========================================================================= */
.composer {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 10px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--header-bg);
  border-top: 1px solid color-mix(in srgb, var(--hint) 22%, transparent);
}
.composer-input {
  flex: 1 1 auto;
  resize: none;
  max-height: 120px;
  border: 1px solid color-mix(in srgb, var(--hint) 32%, transparent);
  border-radius: 18px;
  padding: 9px 14px;
  font-size: 15px;
  line-height: 1.35;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  outline: none;
}
.composer-input::placeholder { color: var(--hint); }
.composer-input:disabled { opacity: 0.55; }

.send-btn {
  flex: 0 0 auto;
  width: 40px; height: 40px;
  border: none;
  border-radius: 50%;
  background: var(--button);
  color: var(--button-text);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
}
.send-btn:disabled { opacity: 0.4; cursor: default; }
.send-btn:not(:disabled):active { transform: scale(0.92); }

/* ── 错误/状态横幅 ── */
.banner {
  flex: 0 0 auto;
  margin: 0 12px 8px;
  padding: 9px 13px;
  border-radius: 10px;
  font-size: 13.5px;
  background: color-mix(in srgb, var(--num-neg) 16%, var(--bg));
  color: var(--num-neg);
  border: 1px solid color-mix(in srgb, var(--num-neg) 35%, transparent);
}
.banner.info {
  background: color-mix(in srgb, var(--link) 14%, var(--bg));
  color: var(--link);
  border-color: color-mix(in srgb, var(--link) 35%, transparent);
}
.hidden { display: none !important; }

/* 空状态欢迎 */
.welcome {
  margin: auto;
  text-align: center;
  color: var(--hint);
  max-width: 320px;
  padding: 24px 12px;
}
.welcome h2 { color: var(--text); font-size: 17px; margin: 0 0 6px; }
.welcome p { margin: 4px 0; font-size: 14px; }
.welcome .examples {
  margin-top: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.welcome .chip {
  padding: 9px 12px;
  border-radius: 12px;
  background: var(--secondary-bg);
  color: var(--text);
  cursor: pointer;
  font-size: 13.5px;
  border: 1px solid color-mix(in srgb, var(--hint) 18%, transparent);
}
.welcome .chip:active { opacity: 0.6; }
