/* home.css - 最终修复版 */

/* ========== 基础样式 ========== */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

html, body {
  height: 100%;
  font-family: sans-serif;
  background: #f1f1f1;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 50px;
  line-height: 50px;
  text-align: center;
  background-color: #5baaff;
  color: white;
  font-size: 18px;
  font-weight: bold;
  z-index: 1000;
  transform: translateZ(0);
  will-change: transform;
}

#messages {
  height: calc(100% - 65px);
  padding: 60px 10px 10px 10px;
  overflow-y: auto;
  overflow-x: hidden;
  font-size: 16px;
  opacity: 0;
  transition: opacity 0.2s ease-in;
  -webkit-overflow-scrolling: touch;
  transform: translateZ(0);
  will-change: scroll-position;
  contain: layout style paint;
}

#messages.visible {
  opacity: 1;
}

.msg {
  display: flex;
  margin: 10px 0;
  align-items: flex-start;
  transform: translateZ(0);
  backface-visibility: hidden;
  contain: layout style;
}

.visitor {
  justify-content: flex-end;
}

.admin {
  justify-content: flex-start;
}

.avatar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  transform: translateZ(0);
}

.avatar svg {
  width: 40px;
  height: 40px;
  display: block;
}

.bubble {
  font-size: 16px;
  padding: 8px 10px;
  border-radius: 6px;
  position: relative;
  display: inline-block;
  max-width: calc(100% - 96px);
  word-break: break-word;
  -webkit-user-select: text;
  -moz-user-select: text;
  -ms-user-select: text;
  user-select: text;
  transform: translateZ(0);
  backface-visibility: hidden;
}

.admin .bubble {
  background-color: #ffffff;
  margin-left: 8px;
}

.visitor .bubble {
  background-color: #95d1a0;
  margin-right: 8px;
}

.admin .bubble::before {
  content: '';
  width: 10px;
  height: 10px;
  background-color: inherit;
  position: absolute;
  left: -3px;
  top: 14px;
  transform: rotate(45deg) translateZ(0);
  backface-visibility: hidden;
}

.visitor .bubble::before {
  content: '';
  width: 10px;
  height: 10px;
  background-color: inherit;
  position: absolute;
  right: -3px;
  top: 14px;
  transform: rotate(45deg) translateZ(0);
  backface-visibility: hidden;
}

#inputArea {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  padding: 5px 10px 20px 12px;
  background: #ededed;
  touch-action: none;
  transform: translateZ(0);
  will-change: transform;
}

#inputArea.keyboard-open {
  padding: 5px 10px 10px 12px;
}

#msgInput {
  caret-color: #2d5bf1;
  color: #000;
  height: 40px;
  flex: 1;
  border: 1px solid #fff;
  border-radius: 5px;
  font-size: 16px;
  outline: none;
  touch-action: none;
  padding: 0 10px;
  transform: translateZ(0);
}

#newMsgNotice {
  position: fixed;
  bottom: 70px;
  left: 50%;
  transform: translateX(-50%) translateZ(0);
  background-color: #007bff;
  color: #fff;
  padding: 6px 12px;
  font-size: 14px;
  border-radius: 20px;
  cursor: pointer;
  z-index: 999;
  display: none;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
  transition: opacity .3s;
  opacity: 0;
  backface-visibility: hidden;
  will-change: opacity, transform;
}

#newMsgNotice[style*="display: block"] {
  opacity: 1;
}

#sendBtn {
  margin-left: 5px;
  min-width: 55px;
  padding: 0 12px;
  border: 1px solid #ededed;
  border-radius: 6px;
  font-size: 14px;
  background: #d3d3d3;
  color: #fff;
  cursor: not-allowed;
  touch-action: none;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  transform: translateZ(0);
  backface-visibility: hidden;
}

#sendBtn.active {
  background: #4396f7;
  cursor: pointer;
}

#sendBtn.loading {
  pointer-events: none;
  opacity: 0.5;
  cursor: not-allowed;
  background: #4396f7 !important;
}

#sendBtn.loading span {
  visibility: hidden;
}

#sendBtn.loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) translateZ(0);
  width: 30px;
  height: 16px;
  background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="30" height="16" viewBox="0 0 30 16"><circle cx="5" cy="8" r="3.5" fill="white"><animate attributeName="opacity" values="0.3;1;0.3" dur="1s" repeatCount="indefinite" begin="0s"/></circle><circle cx="15" cy="8" r="3.5" fill="white"><animate attributeName="opacity" values="0.3;1;0.3" dur="1s" repeatCount="indefinite" begin="0.2s"/></circle><circle cx="25" cy="8" r="3.5" fill="white"><animate attributeName="opacity" values="0.3;1;0.3" dur="1s" repeatCount="indefinite" begin="0.4s"/></circle></svg>') center/contain no-repeat;
}

/* ========== 图片相关 - 关键修复 ========== */

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.fade-in {
  animation: fadeIn 0.3s ease-in forwards;
}

/* 
  核心修复：图片尺寸控制
  - 移除所有 width: 100% 的设置
  - 使用 max-width 限制最大宽度
  - height: auto 保持比例
  - object-fit: contain 确保不拉伸
*/
.bubble img {
  display: block;
  max-width: 100%;
  width: auto !important;
  height: auto !important;
  border-radius: 8px;
  cursor: pointer;
  object-fit: contain;
  transform: translateZ(0);
  backface-visibility: hidden;
  will-change: transform;
}

.bubble img.loading-placeholder {
  pointer-events: none;
}

.bubble img.loaded {
  pointer-events: auto;
  background: transparent;
}

/* PC端图片最大宽度 */
@media (min-width: 768px) {
  .bubble img {
    max-width: 320px;
  }
}

/* ========== 图文混合消息 ========== */
.bubble.with-image-text {
  padding: 0;
  background: none;
  border-radius: 0;
  overflow: visible;
  display: flex;
  flex-direction: column;
}

.bubble.with-image-text::before {
  display: none;
}

.bubble.with-image-text .text-part {
  padding: 8px 10px;
  border-top-left-radius: 6px;
  border-top-right-radius: 6px;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  margin-bottom: 0;
  position: relative;
  transform: translateZ(0);
  backface-visibility: hidden;
}

.bubble.with-image-text img {
  margin: 0;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
  border-bottom-left-radius: 12px;
  border-bottom-right-radius: 12px;
}

.admin .bubble.with-image-text .text-part {
  background-color: #ffffff;
}

.visitor .bubble.with-image-text .text-part {
  background-color: #95d1a0;
}

.admin .bubble.with-image-text .text-part::before {
  content: '';
  width: 10px;
  height: 10px;
  background-color: #ffffff;
  position: absolute;
  left: -3px;
  top: 14px;
  transform: rotate(45deg) translateZ(0);
  backface-visibility: hidden;
}

.visitor .bubble.with-image-text .text-part::before {
  content: '';
  width: 10px;
  height: 10px;
  background-color: #95d1a0;
  position: absolute;
  right: -3px;
  top: 14px;
  transform: rotate(45deg) translateZ(0);
  backface-visibility: hidden;
}

/* ========== 纯图片消息 ========== */
.bubble.pure-image {
  position: relative;
  z-index: 1;
  overflow: visible;
  background: none;
  padding: 0;
  transform: translateZ(0);
  backface-visibility: hidden;
}

.bubble.pure-image img {
  display: block;
  border-radius: 12px;
  border: none;
  background-color: transparent;
  position: relative;
  z-index: 10;
  box-shadow: 0 1px 2px rgba(0,0,0,0.1);
  max-width: 220px;
  width: auto !important;
  height: auto !important;
  object-fit: contain;
  transform: translate3d(0, 0, 0);
  backface-visibility: hidden;
  will-change: auto;
}

@media (min-width: 768px) {
  .bubble.pure-image img {
    max-width: 350px;
  }
}

/* 纯图片的小三角 */
.bubble.pure-image::before {
  display: block;
  content: '';
  position: absolute;
  width: 0;
  height: 0;
  background-color: transparent;
  transform: translateZ(0);
  border: 6px solid transparent;
  top: 14px;
  z-index: 9;
  backface-visibility: hidden;
}

.visitor .bubble.pure-image::before {
  border-left-color: #95d1a0;
  border-right: 0;
  right: -6px;
  left: auto;
}

.admin .bubble.pure-image::before {
  border-right-color: #ffffff;
  border-left: 0;
  left: -6px;
  right: auto;
}

/* ========== 图片模态框 ========== */
#imageModal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.9);
  z-index: 999;
  touch-action: none;
  transform: translateZ(0);
  will-change: opacity;
  backface-visibility: hidden;
}

#modalImage {
  position: absolute;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  transform: translate3d(0, 0, 0);
  will-change: transform, left, top, width, height;
  pointer-events: auto;
  backface-visibility: hidden;
  image-rendering: -webkit-optimize-contrast;
}

@media (min-width: 768px) {
  #modalImage {
    max-width: 90%;
    max-height: 90%;
  }
}

/* ========== Toast提示 ========== */
#apptoastcontainer {
  position: fixed;
  top: 5px;
  left: 50%;
  transform: translateX(-50%) translateZ(0);
  z-index: 10001;
  pointer-events: none;
  backface-visibility: hidden;
}

.app-toast {
  display: flex;
  align-items: center;
  justify-content: center;
  max-width: 90vw;
  min-width: 100px;
  height: 40px;
  padding: 2px 18px;
  border-radius: 8px;
  color: white;
  font-size: 16px;
  text-align: center;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  opacity: 0;
  transform: translateY(-20px) translateZ(0);
  transition: all 0.3s ease;
  pointer-events: auto;
  white-space: nowrap;
  margin: 0 auto;
  overflow: hidden;
  text-overflow: ellipsis;
  backface-visibility: hidden;
}

.app-toast.show {
  opacity: 1;
  transform: translateY(0) translateZ(0);
}

.app-toast.hide {
  opacity: 0;
  transform: translateY(-10px) translateZ(0);
}

.app-toast.success { background: #4caf50; }
.app-toast.error { background: #f44336; }
.app-toast.green { background: #4caf50; }
.app-toast.red { background: #f44336; }
.app-toast.blue { background: #007bff; }
.app-toast.gray { background: #7f8c8d; }
.app-toast.orange { background: #ff9800; }
.app-toast.yellow { background: #ffeb3b; }
.app-toast.purple { background: #9c27b0; }

#messages::-webkit-scrollbar {
  width: 0;
  background: transparent;
}

@supports (-webkit-overflow-scrolling: touch) {
  #messages {
    -webkit-overflow-scrolling: touch;
  }
}

@media (max-width: 767px) {
  * {
    -webkit-tap-highlight-color: transparent;
  }
}