/**
 * 易医智能体 - 移动端响应式样式
 * 适配手机（安卓 + iOS）、平板等各种屏幕尺寸
 * 最后更新：2025-12-30
 */

/* ===== 移动端变量 ===== */
:root {
  --mobile-header-height: 56px;
  --mobile-footer-height: auto;
  --mobile-touch-target: 44px; /* iOS和Android最小触摸目标 */
  --mobile-safe-area-bottom: env(safe-area-inset-bottom, 0px); /* iOS刘海屏底部安全区 */
  --mobile-safe-area-top: env(safe-area-inset-top, 0px); /* iOS刘海屏顶部安全区 */
}

/* ===== 平板设备（768px - 1024px）===== */
@media (max-width: 1024px) and (min-width: 769px) {
  .sidebar {
    width: 260px;
    min-width: 260px;
  }

  .insights {
    width: 280px;
  }

  .admin-grid {
    grid-template-columns: 1fr;
  }
}

/* ===== 小平板/大手机横屏（481px - 768px）===== */
@media (max-width: 768px) {
  /* 侧边栏改为抽屉式 */
  .sidebar {
    position: fixed;
    top: 0;
    left: -320px; /* 默认隐藏 */
    bottom: 0;
    width: 300px;
    min-width: 300px;
    z-index: 1000;
    transition: left 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
    box-shadow: 2px 0 8px rgba(0, 0, 0, 0.3);
  }

  .sidebar.open {
    left: 0;
  }

  /* 侧边栏遮罩层 */
  .sidebar-backdrop {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 999;
    opacity: 0;
    transition: opacity 0.3s ease;
  }

  .sidebar-backdrop.show {
    display: block;
    opacity: 1;
  }

  /* 主内容区占满屏幕 */
  .main {
    width: 100%;
  }

  /* 顶部栏添加汉堡菜单 */
  .topbar {
    padding: 12px 14px;
  }

  .menu-toggle {
    display: flex !important; /* 显示汉堡菜单按钮 */
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border);
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.2s ease;
  }

  .menu-toggle:active {
    transform: scale(0.95);
    background: rgba(255, 255, 255, 0.1);
  }

  /* 汉堡图标 */
  .menu-icon {
    width: 20px;
    height: 16px;
    position: relative;
    transform: rotate(0deg);
    transition: 0.3s ease;
  }

  .menu-icon span {
    display: block;
    position: absolute;
    height: 2px;
    width: 100%;
    background: var(--text);
    border-radius: 2px;
    opacity: 1;
    left: 0;
    transform: rotate(0deg);
    transition: 0.25s ease-in-out;
  }

  .menu-icon span:nth-child(1) {
    top: 0px;
  }

  .menu-icon span:nth-child(2) {
    top: 7px;
  }

  .menu-icon span:nth-child(3) {
    top: 14px;
  }

  /* 汉堡菜单展开时的动画 */
  .menu-toggle.active .menu-icon span:nth-child(1) {
    top: 7px;
    transform: rotate(135deg);
  }

  .menu-toggle.active .menu-icon span:nth-child(2) {
    opacity: 0;
    left: -20px;
  }

  .menu-toggle.active .menu-icon span:nth-child(3) {
    top: 7px;
    transform: rotate(-135deg);
  }

  /* 右侧工具栏按钮调整 */
  .right-tools {
    display: flex;
    gap: 8px;
  }

  .right-tools .btn {
    font-size: 13px;
    padding: 8px 10px;
  }

  /* Insights面板在平板上隐藏或改为模态框 */
  .insights {
    position: fixed;
    top: 0;
    right: -340px;
    bottom: 0;
    width: 320px;
    z-index: 1001;
    transition: right 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
    box-shadow: -2px 0 8px rgba(0, 0, 0, 0.3);
  }

  .insights.open {
    right: 0;
  }

  /* 消息气泡调整 */
  .msg {
    max-width: 85%;
    font-size: 14px;
  }

  /* 模态框调整 */
  .modal-body {
    width: 94vw;
    max-height: 90vh;
  }

  /* 管理后台网格 */
  .admin-grid {
    grid-template-columns: 1fr;
  }
}

/* ===== 手机设备（320px - 480px）===== */
@media (max-width: 480px) {
  /* 全局字体大小调整 */
  html {
    font-size: 15px; /* 稍微放大基础字体 */
  }

  body {
    /* 禁用双击缩放，优化触控体验 */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  /* ===== 侧边栏优化 ===== */
  .sidebar {
    width: 280px;
    min-width: 280px;
    left: -280px;
    padding: 12px;
  }

  .sidebar.open {
    left: 0;
  }

  /* Logo和品牌区域 */
  .brand {
    gap: 10px;
  }

  .logo {
    width: 36px;
    height: 36px;
    font-size: 14px;
  }

  .brand .title {
    font-size: 13px;
  }

  .brand .sub {
    font-size: 11px;
  }

  /* 侧边栏按钮 */
  .sidebar-actions {
    flex-direction: column;
    gap: 8px;
  }

  .sidebar-actions .btn {
    width: 100%;
    min-height: var(--mobile-touch-target);
  }

  /* 会话列表项 */
  .session-item {
    min-height: var(--mobile-touch-target);
    padding: 12px 10px;
  }

  .session-item .t {
    font-size: 14px;
    max-width: 180px;
  }

  .session-item .menu {
    gap: 8px;
  }

  .icon-btn {
    min-width: 36px;
    min-height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* ===== 主内容区 ===== */
  .app {
    flex-direction: column;
  }

  .main {
    flex: 1;
    width: 100%;
  }

  /* 顶部栏 */
  .topbar {
    padding: 10px 12px;
    height: var(--mobile-header-height);
    padding-top: max(10px, var(--mobile-safe-area-top)); /* 适配刘海屏 */
  }

  .session-title .h1 {
    font-size: 16px;
  }

  .session-title .muted {
    font-size: 11px;
  }

  .menu-toggle {
    width: var(--mobile-touch-target);
    height: var(--mobile-touch-target);
    flex-shrink: 0;
  }

  /* 右侧工具栏 - 优化为图标模式 */
  .right-tools {
    gap: 6px;
  }

  .right-tools .btn {
    padding: 0;
    min-width: 38px;
    min-height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
  }

  /* 隐藏按钮文字，只显示图标（如果有） */
  .right-tools .btn.icon-only {
    width: 38px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* ===== 聊天区域 ===== */
  .message-list {
    padding: 12px 10px;
    padding-bottom: 16px;
  }

  /* 消息气泡 */
  .msg {
    max-width: 90%;
    font-size: 15px;
    padding: 12px 14px;
    line-height: 1.6;
  }

  .msg .meta {
    font-size: 10px;
    margin-top: 8px;
  }

  .msg .attach {
    margin-top: 10px;
  }

  .pill {
    font-size: 11px;
    padding: 5px 8px;
  }

  /* ===== 输入区域（Composer）===== */
  .composer {
    padding: 10px 12px;
    padding-bottom: max(10px, var(--mobile-safe-area-bottom)); /* 适配底部安全区 */
    background: rgba(11, 18, 32, 0.95);
  }

  .composer-row {
    gap: 6px;
    flex-wrap: nowrap;
  }

  /* 按钮调整 */
  .composer-row .btn {
    min-width: var(--mobile-touch-target);
    min-height: var(--mobile-touch-target);
    padding: 0 10px;
    font-size: 12px;
    flex-shrink: 0;
    white-space: nowrap;
  }

  /* 文本输入框 */
  textarea {
    min-height: 44px;
    font-size: 15px; /* iOS需要16px+才不会自动缩放 */
    padding: 12px;
    border-radius: 12px;
  }

  /* 附件预览 */
  .attachments {
    gap: 8px;
    margin-bottom: 8px;
  }

  .attachment {
    padding: 6px 8px;
    font-size: 11px;
  }

  .attachment img {
    width: 40px;
    height: 40px;
  }

  .composer-hints {
    font-size: 11px;
    margin-top: 6px;
    padding: 0 2px;
  }

  /* ===== Insights面板（移动端改为底部弹出）===== */
  .insights {
    position: fixed;
    top: auto;
    right: 0;
    bottom: -100%;
    left: 0;
    width: 100%;
    height: 70vh;
    max-height: 70vh;
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: 20px 20px 0 0;
    z-index: 1002;
    transition: bottom 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
    padding-bottom: var(--mobile-safe-area-bottom);
  }

  .insights.open {
    bottom: 0;
  }

  /* Insights关闭手柄 */
  .insights::before {
    content: '';
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 4px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 2px;
  }

  .insights .card {
    margin-top: 20px; /* 为手柄留空间 */
  }

  /* ===== 模态框（Modal）===== */
  .modal-body {
    width: 96vw;
    max-height: 92vh;
    border-radius: 16px;
  }

  .modal-header {
    padding: 10px 12px;
  }

  .modal-title {
    font-size: 16px;
  }

  .modal-content {
    padding: 12px;
    max-height: 80vh;
    font-size: 14px;
  }

  /* ===== 管理后台 ===== */
  .admin {
    padding: 12px;
  }

  .admin-topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }

  .admin-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .list {
    max-height: 400px;
  }

  .list-item {
    padding: 12px 10px;
    flex-direction: column;
    align-items: stretch;
  }

  .list-item .title {
    margin-bottom: 6px;
  }

  /* 表单优化 */
  .form-row {
    grid-template-columns: 1fr;
    gap: 6px;
  }

  input, select {
    min-height: var(--mobile-touch-target);
    font-size: 15px; /* iOS需要16px+才不会缩放 */
    padding: 10px 12px;
  }

  /* ===== 按钮通用优化 ===== */
  .btn {
    min-height: var(--mobile-touch-target);
    font-size: 14px;
    padding: 10px 14px;
    -webkit-tap-highlight-color: transparent;
  }

  .btn:active {
    transform: scale(0.97);
  }

  /* ===== 卡片 ===== */
  .card {
    padding: 12px;
  }

  .card-title {
    font-size: 15px;
  }

  .big {
    font-size: 17px;
  }

  .sub {
    font-size: 11px;
  }

  /* 体质条形图 */
  .bar {
    grid-template-columns: 60px 1fr 35px;
    gap: 8px;
  }

  .bar .label {
    font-size: 11px;
  }

  .bar .track {
    height: 8px;
  }

  .bar .val {
    font-size: 11px;
  }

  /* ===== 横屏适配 ===== */
  @media (orientation: landscape) and (max-height: 500px) {
    .topbar {
      padding: 8px 10px;
      height: 48px;
    }

    .message-list {
      padding: 10px 8px;
    }

    .composer {
      padding: 8px 10px;
    }

    textarea {
      min-height: 38px;
      max-height: 80px;
    }

    .insights {
      height: 85vh;
    }
  }
}

/* ===== 超小屏幕（< 360px）===== */
@media (max-width: 359px) {
  .sidebar {
    width: 260px;
    min-width: 260px;
    left: -260px;
  }

  .msg {
    font-size: 14px;
    padding: 10px 12px;
  }

  .composer-row .btn {
    font-size: 11px;
    padding: 0 8px;
    min-width: 40px;
  }

  .right-tools .btn {
    min-width: 36px;
    min-height: 36px;
  }
}

/* ===== 触摸设备优化 ===== */
@media (hover: none) and (pointer: coarse) {
  /* 所有可点击元素增加触摸反馈 */
  .btn:active,
  .session-item:active,
  .icon-btn:active,
  .list-item:active {
    opacity: 0.7;
  }

  /* 移除hover效果（触摸设备不需要） */
  .btn:hover,
  .icon-btn:hover,
  .session-item:hover {
    background: rgba(255, 255, 255, 0.03);
  }
}

/* ===== iOS特定优化 ===== */
@supports (-webkit-touch-callout: none) {
  /* iOS安全区域适配 */
  body {
    padding-top: var(--mobile-safe-area-top);
    padding-bottom: var(--mobile-safe-area-bottom);
  }

  .topbar {
    padding-top: max(10px, var(--mobile-safe-area-top));
  }

  .composer {
    padding-bottom: max(10px, var(--mobile-safe-area-bottom));
  }

  .sidebar {
    padding-top: max(12px, var(--mobile-safe-area-top));
  }

  /* 禁用iOS文本选择弹出菜单（在某些场景下） */
  .msg.user,
  .msg.assistant {
    -webkit-user-select: text;
    user-select: text;
  }

  /* iOS输入框优化 */
  input, textarea, select {
    -webkit-appearance: none;
    appearance: none;
  }

  textarea {
    font-size: 16px !important; /* iOS防止自动缩放 */
  }
}

/* ===== Android特定优化 ===== */
@media (max-width: 480px) {
  /* Android输入法弹出时调整 */
  .composer {
    position: sticky;
    bottom: 0;
  }

  /* Android Material Design涟漪效果 */
  .btn,
  .session-item,
  .list-item {
    position: relative;
    overflow: hidden;
  }
}

/* ===== PWA状态栏适配（全屏模式）===== */
@media (display-mode: standalone) {
  .topbar {
    padding-top: max(14px, var(--mobile-safe-area-top));
  }

  .sidebar {
    padding-top: max(16px, var(--mobile-safe-area-top));
  }
}

/* ===== 高分辨率屏幕优化 ===== */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  /* 高分屏下边框优化 */
  .border,
  input,
  textarea,
  .btn,
  .card {
    border-width: 0.5px;
  }
}

/* ===== 暗黑模式优化（移动端）===== */
@media (prefers-color-scheme: dark) {
  /* 移动端暗黑模式下优化对比度 */
  .msg {
    border-color: rgba(255, 255, 255, 0.15);
  }

  .composer {
    background: rgba(7, 11, 19, 0.98);
  }
}

/* ===== 辅助功能优化 ===== */
@media (prefers-reduced-motion: reduce) {
  /* 为用户禁用动画 */
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .sidebar,
  .insights,
  .sidebar-backdrop {
    transition: none !important;
  }
}

/* ===== 加载状态优化 ===== */
.loading-skeleton {
  background: linear-gradient(90deg, rgba(255,255,255,0.03) 25%, rgba(255,255,255,0.08) 50%, rgba(255,255,255,0.03) 75%);
  background-size: 200% 100%;
  animation: skeleton-loading 1.5s ease-in-out infinite;
}

@keyframes skeleton-loading {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}
