/* 响应式设计 */

/* 平板设备 */
@media (max-width: 768px) {
  .app {
    height: 100vh;
    /* iOS Safari 视口修复 */
    height: calc(var(--vh, 1vh) * 100);
    min-height: -webkit-fill-available;
    max-width: 100%;
    border-radius: 0;
    box-shadow: none;
  }

  .app-header {
    padding: 1.2rem 1rem;
  }

  .app-header h1 {
    font-size: 1.4rem;
  }

  .device-info {
    font-size: 0.8rem;
    flex-direction: row;
    gap: 1rem;
  }

  /* iOS 12 Safari Flexbox Gap 兼容性修复 */
  @supports not (gap: 1rem) {
    .device-info > *:not(:last-child) {
      margin-right: 1rem;
    }
  }

  .connection-status {
    font-size: 0.75rem;
  }

  .message {
    max-width: 85%;
    margin-bottom: 0.5rem;
    padding: 0 0.25rem;
  }

  .message-content {
    padding: 0.5rem 0.75rem;
    font-size: 0.95rem;
    border-radius: 0.5rem;
  }

  .message-list {
    padding: 0.75rem 0.5rem;
    gap: 0;
    /* 为固定的输入框留出空间，防止最后一条消息被遮挡 */
    padding-bottom: calc(72px + var(--safe-area-inset-bottom, 0));
  }

  .input-container {
    /* 在移动端强制使用fixed定位，彻底解决iOS键盘和滚动问题 */
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    /* 继承最大宽度和居中 */
    max-width: 800px;
    margin: 0 auto;
    /* 基础样式 */
    padding: 8px 12px;
    min-height: 56px;
    padding-bottom: calc(8px + var(--safe-area-inset-bottom));
    z-index: 9999;
  }

  .upload-area {
    padding: 1.5rem 1rem;
  }

  .input-wrapper {
    gap: 8px;
  }

  /* iOS 12 Safari Flexbox Gap 兼容性修复 */
  @supports not (gap: 8px) {
    .input-wrapper > *:not(:last-child) {
      margin-right: 8px;
    }
  }

  .file-button {
    width: 42px;
    height: 42px;
    font-size: 18px;
  }

  .input-field-container {
    min-height: 42px;
  }

  .input-field-container textarea {
    font-size: 16px;
    padding: 11px 13px;
  }
}

/* 手机设备 */
@media (max-width: 480px) {
  .app-header {
    padding: 1rem 0.75rem;
  }

  .app-header h1 {
    font-size: 1.3rem;
    margin-bottom: 0.3rem;
  }

  .device-info {
    font-size: 0.75rem;
    flex-direction: column;
    gap: 0.4rem;
  }

  /* iOS 12 Safari Flexbox Gap 兼容性修复 */
  @supports not (gap: 0.4rem) {
    .device-info > *:not(:last-child) {
      margin-bottom: 0.4rem;
    }
  }

  .connection-status {
    font-size: 0.7rem;
    padding: 0.15rem 0.4rem;
  }

  .message-list {
    padding: 0.75rem 0.5rem;
    gap: 0;
    /* 同步为 fixed 输入框留出的空间 */
    padding-bottom: calc(72px + var(--safe-area-inset-bottom, 0));
  }

  .message {
    max-width: 92%;
    margin-bottom: 0.5rem;
    padding: 0 0.25rem;
  }

  .message-content {
    padding: 0.5rem 0.75rem;
    font-size: 0.95rem;
    border-radius: 0.5rem;
  }

  /* 手机设备上的文本消息优化 */
  .message-content .text-message:not(.markdown-rendered) {
    padding: 0;
    margin: 0;
    font-weight: 400;
    letter-spacing: 0.01em;
  }

  .message-content .text-message.markdown-rendered {
    padding: 0.3rem 24px 0.3rem 0;
    font-weight: 400;
    letter-spacing: 0.01em;
  }

  .message-meta {
    font-size: 0.65rem;
    margin-bottom: 0.3rem;
  }

  .message-time {
    font-size: 0.6rem;
    padding: 0.15rem 0.4rem;
  }

  .input-container {
    padding: 8px 12px;
    min-height: 56px;
    /* iOS 手机特殊处理 */
    padding-bottom: calc(8px + var(--safe-area-inset-bottom));
  }

  .input-wrapper {
    gap: 8px;
  }

  /* iOS 12 Safari Flexbox Gap 兼容性修复 */
  @supports not (gap: 8px) {
    .input-wrapper > *:not(:last-child) {
      margin-right: 8px;
    }
  }

  .file-button {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }

  .input-field-container {
    min-height: 40px;
  }

  .input-field-container textarea {
    font-size: 16px;
    padding: 10px 12px;
  }

  .send-button {
    width: 34px;
    height: 34px;
  }

  .send-icon {
    width: 17px;
    height: 17px;
  }

  .file-upload {
    margin-bottom: 0.75rem;
  }

  .upload-area {
    padding: 0.75rem;
  }

  .upload-prompt {
    gap: 0.25rem;
  }

  /* iOS 12 Safari Flexbox Gap 兼容性修复 */
  @supports not (gap: 0.25rem) {
    /* 假设 .upload-prompt 是 flex-direction: column */
    .upload-prompt > *:not(:last-child) {
      margin-bottom: 0.25rem;
    }
  }

  .upload-icon {
    font-size: 1.2rem;
  }

  .file-info {
    gap: 0.5rem;
  }

  .download-btn {
    padding: 0.4rem 0.6rem;
    font-size: 0.75rem;
  }

  .app-footer {
    padding: 0.5rem;
  }

  .refresh-button {
    padding: 0.4rem 0.8rem;
    font-size: 0.8rem;
  }
}

/* 超小屏幕 */
@media (max-width: 320px) {
  .app-header h1 {
    font-size: 1.1rem;
  }

  .message-content {
    padding: 0.625rem 0.75rem;
    font-size: 0.9rem;
  }

  /* 超小屏幕的文本消息优化 */
  .message-content .text-message:not(.markdown-rendered) {
    padding: 0;
    margin: 0;
    font-weight: 400;
    letter-spacing: 0.01em;
  }

  .message-content .text-message.markdown-rendered {
    padding: 0.25rem 20px 0.25rem 0;
    font-weight: 400;
    letter-spacing: 0.01em;
  }

  .file-name {
    font-size: 0.85rem;
  }

  .file-size {
    font-size: 0.75rem;
  }

  .input-field-container textarea {
    font-size: 15px;
    padding: 8px 10px;
  }

  .file-button {
    width: 38px;
    height: 38px;
    font-size: 16px;
  }

  .send-button {
    width: 30px;
    height: 30px;
  }

  .send-icon {
    width: 15px;
    height: 15px;
  }
}

/* 横屏适配 */
@media (max-height: 500px) and (orientation: landscape) {
  .app-header {
    padding: 0.5rem;
  }

  .app-header h1 {
    font-size: 1.1rem;
    margin-bottom: 0.2rem;
  }

  .device-info {
    font-size: 0.7rem;
  }

  .message-list {
    padding: 0.5rem;
  }

  .input-container {
    padding: 0.5rem;
  }

  .app-footer {
    padding: 0.4rem;
  }
}

/* iOS Safari 专用修复 */
@supports (-webkit-touch-callout: none) {
  .app {
    height: -webkit-fill-available;
    min-height: -webkit-fill-available;
  }
}

/* iOS 设备特定修复 */
@media screen and (-webkit-min-device-pixel-ratio: 2) and (orientation: portrait) {
  .app {
    height: calc(var(--vh, 1vh) * 100);
    min-height: calc(var(--vh, 1vh) * 100);
  }
  .input-container {
    padding-bottom: calc(8px + 34px); /* 34px 是 iPhone X 系列的底部安全区域 */
    /* 确保此处的规则不会覆盖 position: fixed */
  }
}

/* iPhone X 及以上设备的安全区域适配 */
@media screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3),
  screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 2),
  screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 3),
  screen and (device-width: 390px) and (device-height: 844px) and (-webkit-device-pixel-ratio: 3),
  screen and (device-width: 428px) and (device-height: 926px) and (-webkit-device-pixel-ratio: 3) {
  .input-container {
    padding-bottom: calc(8px + 34px); /* 34px 是 iPhone X 系列的底部安全区域 */
  }
}

/* iOS 虚拟键盘弹出时的样式调整 */
body.keyboard-open .app {
  height: 100vh;
  height: calc(var(--vh, 1vh) * 100);
}

body.keyboard-open .input-container {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  z-index: 10000;
  /* 确保在虚拟键盘上方 */
  transform: translateY(0);
}

body.keyboard-open .message-list {
  /* 为固定输入框预留更多空间 */
  padding-bottom: 80px;
}

/* 高分辨率屏幕 */
@media (min-width: 1200px) {
  .app {
    max-width: 900px;
  }

  .message {
    max-width: 60%;
  }

  .app-header h1 {
    font-size: 1.6rem;
  }

  .message-content {
    font-size: 1.05rem;
    padding: 0.5rem 0.5rem;
  }

  /* 高分辨率屏幕的文本消息优化 */
  .message-content .text-message:not(.markdown-rendered) {
    padding: 0;
    margin: 0;
    font-weight: 400;
    letter-spacing: 0.01em;
  }

  .message-content .text-message.markdown-rendered {
    padding: 0.375rem 28px 0.375rem 0;
    font-weight: 400;
    letter-spacing: 0.01em;
  }
}

/* 打印样式 */
@media print {
  .app-header,
  .input-container,
  .app-footer {
    display: none;
  }

  .app {
    height: auto;
    box-shadow: none;
  }

  .message-list {
    padding: 0;
  }

  .message-content {
    border: 1px solid #ddd;
    background-color: white !important;
    color: black !important;
  }
}
