/**
 * 移动端响应式样式
 * 支持各种手机品牌和型号的自适应显示
 */

/* ========== 基础viewport优化 ========== */
:root {
  /* 动态viewport高度变量 */
  --vh: 1vh;
  --safe-area-inset-top: env(safe-area-inset-top);
  --safe-area-inset-bottom: env(safe-area-inset-bottom);
  --safe-area-inset-left: env(safe-area-inset-left);
  --safe-area-inset-right: env(safe-area-inset-right);
}

/* ========== 通用移动端优化 ========== */
@media (max-width: 768px) {
  * {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
    -webkit-touch-callout: none;
  }

  html {
    /* 禁止双击缩放 */
    touch-action: manipulation;
    /* 平滑滚动 */
    scroll-behavior: smooth;
    /* 使用动态计算的viewport高度 */
    height: 100vh;
    height: calc(var(--vh, 1vh) * 100);
  }

  body {
    /* 防止横向滚动 */
    overflow-x: hidden;
    /* iOS惯性滚动 */
    -webkit-overflow-scrolling: touch;
    /* 最小高度适配 */
    min-height: 100vh;
    min-height: calc(var(--vh, 1vh) * 100);
    /* 字体平滑渲染 */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }

  /* 适配安全区域（刘海屏、药丸屏等） */
  .navbar {
    padding-top: var(--safe-area-inset-top);
    padding-left: var(--safe-area-inset-left);
    padding-right: var(--safe-area-inset-right);
  }

  .main-content {
    padding-bottom: calc(20px + var(--safe-area-inset-bottom));
    padding-left: calc(20px + var(--safe-area-inset-left));
    padding-right: calc(20px + var(--safe-area-inset-right));
  }

  /* 导航栏移动端优化 */
  .navbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: auto;
    min-height: 50px;
    z-index: 1050;
  }

  /* 侧边栏移动端优化 */
  .sidebar-left {
    width: 80%;
    max-width: 280px;
    transform: translateX(-100%);
    box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  }

  .sidebar-left:not(.collapsed) {
    transform: translateX(0);
  }

  /* 主内容区域 */
  .main-content {
    margin-left: 0 !important;
    padding-top: calc(60px + var(--safe-area-inset-top));
    width: 100%;
  }

  /* 容器优化 */
  .container,
  .container-fluid,
  .container-wrapper {
    padding: 15px;
    margin: 0 auto;
    width: 100%;
  }
}

/* ========== 小屏手机优化 (iPhone SE, 小米9等) ========== */
@media (max-width: 375px) {
  body {
    font-size: 14px;
  }

  .navbar-nav .nav-link {
    font-size: 13px;
    padding: 0.4rem 0.8rem;
  }

  .btn {
    font-size: 13px;
    padding: 0.3rem 0.8rem;
  }

  h1 {
    font-size: 18px;
  }

  h2 {
    font-size: 16px;
  }

  .container,
  .container-fluid,
  .container-wrapper {
    padding: 10px;
  }

  /* 表格优化 */
  table {
    font-size: 12px;
  }

  th, td {
    padding: 6px 4px;
  }
}

/* ========== 中等屏幕手机 (iPhone 12/13/14, 大部分Android) ========== */
@media (min-width: 376px) and (max-width: 428px) {
  body {
    font-size: 15px;
  }

  .navbar-nav .nav-link {
    font-size: 14px;
  }
}

/* ========== 大屏手机 (iPhone Pro Max, 安卓大屏) ========== */
@media (min-width: 429px) and (max-width: 768px) {
  body {
    font-size: 16px;
  }

  .container,
  .container-fluid,
  .container-wrapper {
    padding: 20px;
  }
}

/* ========== 横屏模式优化 ========== */
@media (max-width: 915px) and (orientation: landscape) {
  .navbar {
    min-height: 44px;
  }

  .main-content {
    padding-top: calc(50px + var(--safe-area-inset-top));
  }

  .sidebar-left {
    width: 60%;
    max-width: 320px;
  }

  /* 减少垂直间距 */
  .container,
  .container-fluid,
  .container-wrapper {
    padding-top: 10px;
    padding-bottom: 10px;
  }

  h1 {
    margin-bottom: 10px;
  }
}

/* ========== 触摸优化 ========== */
@media (max-width: 768px) {
  /* 增大可触摸区域 */
  a, button, input, select, textarea {
    min-height: 44px;
    min-width: 44px;
  }

  /* 按钮优化 */
  .btn,
  button {
    padding: 10px 16px;
    font-size: 14px;
    border-radius: 6px;
    cursor: pointer;
    user-select: none;
  }

  /* 链接优化 */
  a {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
  }

  /* 表单控件优化 */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="date"],
  input[type="number"],
  select,
  textarea {
    font-size: 16px; /* 防止iOS自动缩放 */
    padding: 10px 12px;
    border-radius: 6px;
    -webkit-appearance: none;
    appearance: none;
  }

  /* 下拉框优化 */
  select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23333' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    background-size: 12px;
    padding-right: 32px;
  }
}

/* ========== 表格移动端优化 ========== */
@media (max-width: 768px) {
  .table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -15px;
    padding: 0 15px;
  }

  table {
    min-width: 100%;
    font-size: 13px;
  }

  th, td {
    white-space: nowrap;
    padding: 8px 6px;
  }

  /* 固定第一列 */
  .table-wrapper.sticky-column table th:first-child,
  .table-wrapper.sticky-column table td:first-child {
    position: sticky;
    left: 0;
    background-color: #fff;
    z-index: 1;
  }
}

/* ========== 卡片和面板优化 ========== */
@media (max-width: 768px) {
  .card {
    margin-bottom: 15px;
    border-radius: 8px;
  }

  .card-header {
    padding: 12px 15px;
    font-size: 16px;
  }

  .card-body {
    padding: 15px;
  }
}

/* ========== 工具栏优化 ========== */
@media (max-width: 768px) {
  .toolbar {
    flex-direction: column;
    gap: 10px;
  }

  .toolbar > * {
    width: 100%;
  }

  .toolbar input,
  .toolbar select,
  .toolbar button {
    width: 100%;
  }
}

/* ========== iOS特定优化 ========== */
@supports (-webkit-touch-callout: none) {
  /* iOS设备 */
  body {
    /* 修复iOS 100vh问题 */
    min-height: -webkit-fill-available;
  }

  .navbar {
    /* iOS状态栏适配 */
    padding-top: max(var(--safe-area-inset-top), 0px);
  }

  /* 修复iOS输入框缩放问题 */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  select,
  textarea {
    font-size: 16px !important;
  }
}

/* ========== Android特定优化 ========== */
@media (max-width: 768px) {
  /* Android Chrome地址栏优化 */
  html.android {
    height: 100vh;
  }

  body.android {
    min-height: 100vh;
  }
}

/* ========== 折叠屏手机优化 ========== */
@media (min-width: 769px) and (max-width: 1024px) and (orientation: portrait) {
  /* 折叠屏展开状态 */
  .container,
  .container-fluid,
  .container-wrapper {
    max-width: 768px;
    padding: 24px;
  }

  .sidebar-left {
    width: 240px;
  }
}

/* ========== 加载动画优化 ========== */
@media (max-width: 768px) {
  @keyframes fadeIn {
    from {
      opacity: 0;
      transform: translateY(10px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  .fade-in {
    animation: fadeIn 0.3s ease-out;
  }
}

/* ========== 滚动条优化 ========== */
@media (max-width: 768px) {
  /* 移动端隐藏滚动条但保持滚动功能 */
  ::-webkit-scrollbar {
    width: 4px;
    height: 4px;
  }

  ::-webkit-scrollbar-track {
    background: transparent;
  }

  ::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 2px;
  }

  ::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.3);
  }
}

/* ========== 模态框和弹窗优化 ========== */
@media (max-width: 768px) {
  .modal-dialog {
    margin: 10px;
    max-width: calc(100% - 20px);
  }

  .modal-content {
    border-radius: 12px;
  }

  .alert-popup {
    top: calc(60px + var(--safe-area-inset-top));
    right: 10px;
    left: 10px;
    max-width: none;
  }
}

/* ========== 分页优化 ========== */
@media (max-width: 768px) {
  .pagination {
    flex-wrap: wrap;
    gap: 6px;
  }

  .pagination button {
    min-width: 40px;
    padding: 6px 10px;
    font-size: 13px;
  }
}

/* ========== 聊天气泡优化 ========== */
@media (max-width: 768px) {
  .chat-bubble {
    margin-bottom: 10px;
  }

  .chat-message {
    font-size: 14px;
    padding: 8px 12px;
    max-width: calc(100% - 30px);
  }

  .reply-scroll {
    max-height: 200px;
  }
}

/* ========== 下拉菜单移动端优化 ========== */
@media (max-width: 768px) {
  .dropdown-menu {
    position: fixed !important;
    top: auto !important;
    left: 10px !important;
    right: 10px !important;
    bottom: calc(10px + var(--safe-area-inset-bottom)) !important;
    transform: none !important;
    max-width: none;
    border-radius: 12px;
  }

  .dropdown-item {
    padding: 12px 16px;
    font-size: 15px;
  }
}

/* ========== 打印样式 ========== */
@media print {
  .navbar,
  .sidebar-left,
  .menu-toggle,
  .toolbar button,
  .pagination {
    display: none !important;
  }

  .main-content {
    margin-left: 0 !important;
    padding: 0 !important;
  }

  body {
    background: white !important;
  }
}

/* ========== 深色模式支持 ========== */
@media (max-width: 768px) and (prefers-color-scheme: dark) {
  /* 可选：添加深色模式支持 */
  body {
    background-color: #1c1c1e;
    color: #ffffff;
  }

  .navbar,
  .sidebar-left {
    background-color: rgba(28, 28, 30, 0.9);
    border-color: rgba(255, 255, 255, 0.1);
  }

  .card {
    background-color: #2c2c2e;
    border-color: rgba(255, 255, 255, 0.1);
  }

  table {
    background-color: #2c2c2e;
  }

  th {
    background-color: #3a3a3c;
  }

  tr:hover {
    background-color: #3a3a3c;
  }
}

/* ========== 高对比度模式 ========== */
@media (max-width: 768px) and (prefers-contrast: high) {
  * {
    border-width: 2px !important;
  }

  button, .btn {
    border: 2px solid currentColor !important;
  }
}

/* ========== 减少动画模式 ========== */
@media (max-width: 768px) and (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
