/* Mobile-first layout; uses Telegram theme vars in theme.css */
* {
  box-sizing: border-box;
}
html, body {
  height: 100%;
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--tg-theme-text-color, #111b21);
}
body {
  overflow: hidden;
  overscroll-behavior: none;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-area {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.4rem 0.5rem 0;
  min-height: 40vh;
  max-width: 720px;
  margin: 0 auto;
  background-color: transparent;
  background-image: none;
}


.bubble {
  position: relative;
  z-index: 1;
  max-width: 90%;
  padding: 0.55rem 0.7rem;
  border-radius: 0.9rem;
  line-height: 1.45;
  font-size: 0.92rem;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.bubble--user {
  background: var(--user-bubble, #d9fdd3);
  color: var(--bubble-text, #111b21);
  border-radius: 7.5px 7.5px 0 7.5px;
  position: relative;
  overflow: visible;
  margin-left: auto;
  text-align: left;
}
.bubble--user::after {
  content: "";
  position: absolute;
  bottom: 0;
  right: -8px;
  width: 0;
  height: 0;
  border-left: 8px solid var(--user-bubble, #d9fdd3);
  border-bottom: 8px solid transparent;
}
.bubble--assistant {
  margin-right: auto;
  background: var(--assistant-bubble, #ffffff);
  color: var(--bubble-text, #111b21);
  border-radius: 7.5px 7.5px 7.5px 0;
  position: relative;
  /* Isolate assistant bubble stacking for TG WebView compositing quirks */
  overflow: visible;
  max-height: none;
  isolation: isolate;
  text-align: left;
}
.bubble--assistant::before {
  content: "";
  position: absolute;
  bottom: 0;
  left: -8px;
  width: 0;
  height: 0;
  border-right: 8px solid var(--assistant-bubble, #ffffff);
  border-bottom: 8px solid transparent;
}

.bubble-copy-btn {
  display: block;
  margin-left: auto;
  margin-top: 4px;
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  color: var(--tg-theme-hint-color, #8696a0);
  cursor: pointer;
  opacity: 0.5;
  transition: opacity 0.2s ease;
  border-radius: 6px;
  padding: 0;
}

.bubble--assistant:hover .bubble-copy-btn,
.bubble-copy-btn:focus-visible {
  opacity: 1;
}

.bubble-copy-btn:active {
  opacity: 1;
  background: rgba(0, 0, 0, 0.05);
}

.bubble--assistant .stream-content {
  display: inline;
  color: inherit;
  opacity: 1;
  visibility: visible;
  overflow: visible;
  max-height: none;
  white-space: inherit;
  word-wrap: break-word;
  vertical-align: baseline;
}
.chat-app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  min-height: 0;
  width: 100%;
  position: relative;
  transition: height .15s ease;
}
@supports (height: 100svh) {
  .chat-app {
    height: 100svh;
  }
}
.chat-header {
  flex: 0 0 auto;
}
.chat-messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 12px 12px 31px;
}
.chat-input {
  flex: 0 0 auto;
  position: sticky;
  bottom: 0;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom)) 12px;
  z-index: 10;
}
.input-area {
  display: flex;
  gap: 0.4rem;
  padding: 0.45rem 0.5rem;
  max-width: 720px;
  margin: 0 auto;
  background: transparent;
  border-top: none;
}
/* Input menu wrap */
.input-menu-wrap {
  position: relative;
  flex-shrink: 0;
  align-self: flex-end;
}

/* ⋮ button */
.menu-btn {
  width: 2.4rem;
  height: 2.4rem;
  border: none;
  border-radius: 50%;
  background: var(--menu-btn-bg, #e9edef);
  color: var(--tg-theme-hint-color, #8696a0);
  font-size: 1.3rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  letter-spacing: -1px;
}
.menu-btn:active { opacity: 0.7; }

/* Popup menu - appears ABOVE the button */
.input-menu-popup {
  position: fixed;
  bottom: calc(4.5rem + env(safe-area-inset-bottom, 0px));
  left: 0.5rem;
  min-width: 160px;
  background: var(--tg-theme-secondary-bg-color, #f0f2f5);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.35), 0 2px 8px rgba(0,0,0,0.2);
  overflow: hidden;
  z-index: 9000;
}

.input-menu-item {
  display: block;
  width: 100%;
  padding: 12px 16px;
  text-align: left;
  background: none;
  border: none;
  color: var(--tg-theme-text-color, #e8e8e8);
  font-size: 0.95rem;
  cursor: pointer;
  white-space: nowrap;
}
.input-menu-item:active {
  background: var(--tg-theme-hint-color, #555);
  opacity: 0.8;
}

.input-menu-divider {
  height: 1px;
  background: var(--tg-theme-hint-color, #444);
  margin: 2px 0;
  opacity: 0.4;
}

.input-area__text {
  flex: 1;
  resize: none;
  min-height: 2.4rem;
  max-height: 8rem;  /* ~5 lines, WhatsApp style */
  overflow-y: auto;
  padding: 9px 14px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 1.0625rem;  /* 17px — slightly larger */
  font-weight: 400;
  line-height: 1.4;
  border-radius: 21px;
  border: none;
  background: var(--input-bg, #ffffff);
  color: var(--tg-theme-text-color, #111b21);
  letter-spacing: 0;
}
.input-area__text::placeholder {
  color: var(--tg-theme-hint-color, #8696a0);
}
.btn {
  border: none;
  border-radius: 0.5rem;
  padding: 0.45rem 0.8rem;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  align-self: flex-end;
}
.btn--primary {
  background: #00a884;
  color: #fff;
  border-radius: 50%;
  width: 2.6rem;
  height: 2.6rem;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
}
.btn.btn--primary.send-btn--stop {
  width: auto;
  min-width: 2.6rem;
  height: 2.6rem;
  padding: 0 0.65rem;
  border-radius: 1.3rem;
  background: #00a884;
  color: #fff;
  font-size: 0.78rem;
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: 0.02em;
  box-shadow: 0 2px 8px rgba(0, 168, 132, 0.35);
}
.btn.btn--primary.send-btn--stop:active {
  opacity: 0.92;
  transform: scale(0.98);
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
@media (min-width: 600px) {
  .chat-area { padding-left: 1rem; padding-right: 1rem; }
}

/* ── Mini App 布局优化 ── */
#chat-area {
  min-height: 200px;
  flex: 1;
}

/* ── Splash Screen ── */
.splash-screen {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  background: #111b21;
  transition: opacity 400ms ease;
}
.splash-screen--hidden {
  opacity: 0;
  pointer-events: none;
}
.splash-screen__logo {
  font-size: 3.5rem;
  color: #00a884;
  animation: splash-pulse 1.5s ease-in-out infinite;
}
.splash-screen__name {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: 0.25em;
  color: var(--tg-theme-text-color, #e8e8e8);
}
.splash-screen__bar {
  width: 160px;
  height: 4px;
  border-radius: 999px;
  background: var(--tg-theme-secondary-bg-color, #2a2a2a);
  overflow: hidden;
}
.splash-screen__bar-fill {
  height: 100%;
  border-radius: 999px;
  background: #00a884;
  animation: splash-progress 1.5s ease-out forwards;
}
@keyframes splash-pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50%       { transform: scale(1.15); opacity: 0.8; }
}
@keyframes splash-progress {
  from { width: 0%; }
  to   { width: 100%; }
}

/* ── Typing Indicator ── */
.bubble--typing {
  display: flex;
  align-items: center;
  padding: 0.6rem 0.9rem;
  min-width: 3.5rem;
}
.bubble--typing__dots {
  display: flex;
  gap: 5px;
  align-items: center;
}
.bubble--typing__dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--tg-theme-hint-color, #888);
  animation: typing-bounce 0.9s ease-in-out infinite;
}
.bubble--typing__dot:nth-child(2) { animation-delay: 0.15s; }
.bubble--typing__dot:nth-child(3) { animation-delay: 0.3s; }
@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30%           { transform: translateY(-6px); }
}

/* ── Markdown styles for assistant bubbles ── */
.bubble--md {
  white-space: normal;
}
.bubble--assistant p {
  margin: 0.3em 0;
  white-space: normal;
}
.bubble--assistant p:first-child { margin-top: 0; }
.bubble--assistant p:last-child  { margin-bottom: 0; }
.bubble--assistant strong { font-weight: 700; }
.bubble--assistant em { font-style: italic; }
.bubble--assistant del { text-decoration: line-through; }
.bubble--assistant code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.85em;
  background: rgba(0, 0, 0, 0.2);
  padding: 0.1em 0.3em;
  border-radius: 3px;
}
.bubble--assistant pre {
  background: rgba(0, 0, 0, 0.25);
  border-radius: 6px;
  padding: 0.6em 0.8em;
  overflow-x: auto;
  margin: 0.4em 0;
  white-space: pre;
}
.bubble--assistant pre code {
  background: none;
  padding: 0;
  font-size: 0.82em;
}
.bubble--assistant ul,
.bubble--assistant ol {
  padding-left: 1.4em;
  margin: 0.3em 0;
  white-space: normal;
}
.bubble--assistant li { margin: 0.15em 0; }
.bubble--assistant blockquote {
  border-left: 3px solid var(--tg-theme-hint-color, #666);
  padding-left: 0.7em;
  margin: 0.3em 0;
  opacity: 0.8;
}
.bubble--assistant a {
  color: var(--tg-theme-link-color, var(--tg-theme-button-color, #3390ec));
  text-decoration: none;
}
.bubble--assistant a:hover { text-decoration: underline; }

/* 图片生成结果样式 */
.chat-image {
  max-width: 180px !important;
  max-height: 180px !important;
  width: auto;
  height: auto;
  border-radius: 12px;
  margin: 8px 0 4px 0;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  display: block;
  object-fit: cover;
  transition: opacity 0.2s;
}
.chat-image:hover {
  opacity: 0.9;
}

/* 全屏图片 overlay */
.image-overlay {
  position: fixed;
  top: 0; left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  cursor: pointer;
}
.image-overlay img {
  max-width: 95vw;
  max-height: 95vh;
  object-fit: contain;
  border-radius: 8px;
}

/* ===== Message Overflow Fix ===== */
/* 消息气泡内容溢出修复 */
.bubble,
.bubble--assistant,
.bubble--user,
.message,
.msg-bubble,
.bot-message,
.message-content,
.markdown-body {
  max-width: 100%;
  overflow-wrap: break-word;
  word-break: break-word;
  word-wrap: break-word;
  white-space: normal;
  box-sizing: border-box;
}

/* 代码块溢出处理 */
.bubble pre,
.bubble code,
.bubble--assistant pre,
.bubble--assistant code,
.message pre,
.message code,
.hljs {
  max-width: 100%;
  overflow-x: auto;
  white-space: pre;
  word-break: normal;
  -webkit-overflow-scrolling: touch;
}

/* 表格溢出处理核心修复 */
.table-wrap {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.bubble table,
.bubble--assistant table,
.message table {
  width: max-content;
  max-width: none;
  border-collapse: collapse;
  font-size: 13px;
}

.bubble table td,
.bubble table th,
.bubble--assistant table td,
.bubble--assistant table th,
.message table td,
.message table th {
  white-space: nowrap;
  padding: 4px 8px;
}

/* 窄屏优化 */
@media (max-width: 480px) {
  .bubble table td,
  .bubble table th,
  .bubble--assistant table td,
  .bubble--assistant table th,
  .message table td,
  .message table th {
    font-size: 12px;
    padding: 3px 6px;
  }
}

/* 长URL和英文串处理 */
.bubble a,
.bubble span.long-text,
.bubble--assistant a,
.bubble--assistant span.long-text,
.message a,
.message span.long-text {
  overflow-wrap: anywhere;
  word-break: break-all;
}

/* 图片响应式 */
.bubble img,
.bubble--assistant img,
.message img {
  max-width: 100%;
  height: auto;
  display: block;
}

/* 表格滚动提示样式 */
.table-wrap {
  position: relative;
}

.table-wrap::after {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 16px;
  height: 100%;
  background: linear-gradient(to right, transparent, rgba(0,0,0,0.08));
  pointer-events: none;
  opacity: 0;
  transition: opacity .2s;
}

.table-wrap.is-scrollable::after {
  opacity: 1;
}
