/**
 * 移动端 Apple 风格精致适配 v1.2
 * 全面修复iOS安全区域、触摸滚动、键盘弹出问题
 */

/* ============================================
   全局背景色覆盖 - 确保无黑色区域
   ============================================ */
@media (max-width: 768px) {
  html, body {
    background-color: var(--bg-content) !important;
  }

  #app {
    background-color: var(--bg-content);
  }

  /* ============================================
     主布局 - 使用正常flex，不用fixed定位
     ============================================ */
  .main-app {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 100vh;
    min-height: 100dvh;
    min-height: -webkit-fill-available;
    overflow: hidden;
    background-color: var(--bg-content);
    /* 顶部安全区域padding */
    padding-top: env(safe-area-inset-top);
  }

  .main-content {
    flex: 1 1 0;
    height: 0;
    width: 100%;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
    background-color: var(--bg-content);
  }

  /* ============================================
     顶部栏 - 扩展到状态栏区域
     ============================================ */
  .header {
    /* 负margin让背景延伸到状态栏 */
    margin-top: calc(-1 * env(safe-area-inset-top));
    /* padding补偿 + 内容padding */
    padding-top: calc(env(safe-area-inset-top) + 0.5rem);
    padding-bottom: 0.5rem;
    padding-left: 1rem;
    padding-right: 1rem;
    min-height: 56px;
    height: auto;
    background: var(--bg-glass);
    backdrop-filter: var(--blur-std);
    -webkit-backdrop-filter: var(--blur-std);
    flex-shrink: 0;
  }

  .header-title {
    font-size: 1.1rem;
    font-weight: 600;
  }

  .header-logo {
    width: 24px;
    height: 24px;
  }

  #collapse-toggle {
    display: none !important;
  }

  #menu-btn {
    display: flex !important;
  }

  /* ============================================
     聊天容器
     ============================================ */
  .chat-container {
    flex: 1 1 0;
    height: 0;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
    background-color: var(--bg-content);
    position: relative;
  }

  /* ============================================
     聊天消息区 - iOS触摸滚动
     ============================================ */
  .chat-messages {
    flex: 1 1 0;
    height: 0;
    min-height: 0;
    padding: 1.5rem 1rem;
    gap: 1.5rem;
    max-width: 100%;
    width: 100%;
    display: flex;
    flex-direction: column;
    overflow-y: scroll !important;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y pinch-zoom;
    overscroll-behavior-y: contain;
    scroll-behavior: smooth;
  }

  .message {
    max-width: 92%;
  }

  .message-text {
    padding: 0.75rem 1rem;
    font-size: 0.95rem;
  }

  .message-avatar {
    width: 32px;
    height: 32px;
  }

  /* ============================================
     输入区域 - 底部安全区域适配
     ============================================ */
  .input-container {
    padding: 0.75rem 1rem;
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom));
    background: var(--bg-content) !important;
    flex-shrink: 0;
  }

  /* 键盘弹出时移除底部安全区域 padding */
  body.keyboard-visible .input-container {
    padding-bottom: 0.5rem;
  }

  .input-wrapper {
    border-radius: var(--radius-lg);
    padding: 0.5rem 0.75rem;
    min-height: 48px;
    max-width: 100%;
  }

  #message-input {
    font-size: 1rem; /* 防止 iOS 自动缩放 */
    padding: 0.5rem;
  }

  /* ============================================
     侧边栏抽屉
     ============================================ */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 85%;
    max-width: 300px;
    z-index: 1000;
    transform: translateX(-100%);
    box-shadow: none;
    background: var(--bg-sidebar);
    backdrop-filter: blur(30px);
    -webkit-backdrop-filter: blur(30px);
    transition: transform 0.4s cubic-bezier(0.25, 0.1, 0.25, 1);
    /* 侧边栏也需要顶部安全区域 */
    padding-top: env(safe-area-inset-top);
  }

  .sidebar.open {
    transform: translateX(0);
    box-shadow: 20px 0 50px rgba(0, 0, 0, 0.15);
  }

  .sidebar-overlay.active {
    display: block;
    background: rgba(0, 0, 0, 0.3);
  }

  .sidebar-header {
    padding: 1rem 0.75rem;
  }

  .new-chat-btn {
    width: 100%;
    height: auto;
    padding: 0.7rem 1rem;
    font-size: 0.9rem;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
  }

  .new-chat-btn .icon-svg,
  .new-chat-btn svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
  }

  .new-chat-btn span {
    font-size: 0.9rem;
    white-space: nowrap;
  }

  .sidebar-search {
    padding: 0 0.75rem 0.75rem;
  }

  .sidebar-search input {
    padding: 0.6rem 0.8rem;
    font-size: 0.9rem;
  }

  .sidebar-sessions {
    padding: 0.25rem 0.5rem;
  }

  .session-item {
    padding: 0.7rem 0.8rem;
    font-size: 0.9rem;
  }

  .sidebar-footer {
    padding: 0.75rem;
    /* 底部安全区域 */
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom));
  }

  .clear-history-btn {
    padding: 0.6rem 0.8rem;
    font-size: 0.85rem;
  }

  /* ============================================
     其他组件适配
     ============================================ */
  .toast {
    width: fit-content;
    max-width: 90vw;
    /* 顶部安全区域 */
    top: calc(1.5rem + env(safe-area-inset-top));
  }

  .scroll-bottom-btn {
    bottom: calc(15px + env(safe-area-inset-bottom));
    right: 16px;
    width: 38px;
    height: 38px;
  }

  /* ============================================
     欢迎页面
     ============================================ */
  .welcome-message {
    flex: 1;
    min-height: 0;
    padding-bottom: 0;
    justify-content: center;
  }

  .welcome-logo {
    width: 64px;
    height: 64px;
  }

  .welcome-message h2 {
    font-size: 1.35rem;
  }

  .welcome-message p {
    font-size: 0.95rem;
  }

  /* ============================================
     加载和代码块
     ============================================ */
  .progress-bar {
    width: 180px;
  }

  .code-block {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .code-block pre {
    max-width: 100%;
    font-size: 0.8rem;
  }

  /* ============================================
     超小屏幕适配 (≤360px)
     ============================================ */
  @media (max-width: 360px) {
    .header-title {
      font-size: 1rem;
    }

    .chat-messages {
      padding: 1rem 0.75rem;
    }

    .message {
      max-width: 95%;
    }

    .welcome-logo {
      width: 56px;
      height: 56px;
    }

    .welcome-message h2 {
      font-size: 1.2rem;
    }
  }
}

/* ============================================
   iOS安全区域回退支持
   ============================================ */
@supports (padding-top: env(safe-area-inset-top)) {
  @media (max-width: 768px) {
    .toast-container {
      top: calc(1.5rem + env(safe-area-inset-top));
    }
  }
}
