/* ============================================
   全局移动端适配样式 v1.0
   适用：管理端、裁判端、大屏端
   特性：响应式布局、触摸优化、安全区适配、手势支持
   ============================================ */

/* 防止移动端 300ms 点击延迟 */
* {
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

/* 改善触摸滚动 */
html, body {
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
}

/* ===== 1. 全局响应式断点 ===== */
/* 设备分类：
   - 超小屏 ≤ 380px （iPhone SE 一代）
   - 移动端 ≤ 640px （大部分手机竖屏）
   - 平板竖屏 641-1024px
   - 桌面 > 1024px
*/

/* ===== 2. 输入框移动端优化 ===== */
@media (max-width: 768px) {
  /* 防止 iOS Safari 缩放 */
  input, select, textarea {
    font-size: 16px !important;
  }
  /* 数字键盘优化 */
  input[type="number"],
  input[type="tel"] {
    inputmode: numeric;
    -webkit-appearance: none;
    appearance: none;
  }
}

/* ===== 3. 裁判端（judge.html）增强 ===== */
@media (max-width: 640px) {
  /* 数字键盘按钮：增大点击区 */
  .numpad-btn {
    min-height: 56px !important;
    padding: 0.75rem !important;
    font-size: 1.3rem !important;
  }
  /* 提交按钮：固定在底部，方便单手操作 */
  .btn-submit {
    position: sticky;
    bottom: calc(0.5rem + env(safe-area-inset-bottom, 0));
    z-index: 30;
    box-shadow: 0 4px 20px rgba(59, 130, 246, 0.4);
  }
  /* 数字输入框：禁用系统键盘，强制使用自定义数字键盘 */
  .score-input {
    -webkit-user-select: none;
    user-select: none;
    caret-color: transparent;
  }
  /* 列表卡片：增大点击区 */
  .athlete-card {
    min-height: 60px;
    padding: 0.75rem !important;
  }
  /* 当前运动员：突出显示 */
  .athlete-card.current {
    transform: scale(1.02);
  }
  /* 顶部栏：紧凑布局 */
  header.sticky {
    -webkit-backdrop-filter: blur(20px);
  }
}

/* 超小屏（iPhone 5/SE 一代） */
@media (max-width: 380px) {
  .score-input {
    font-size: 1.8rem !important;
    padding: 0.75rem !important;
  }
  .numpad-btn {
    font-size: 1.1rem !important;
    min-height: 48px !important;
  }
  .btn-submit {
    font-size: 0.95rem !important;
    padding: 0.75rem 1rem !important;
  }
}

/* 平板优化（iPad 等） */
@media (min-width: 641px) and (max-width: 1024px) {
  /* 利用更大屏幕展示更多运动员 */
  .athlete-card {
    padding: 1rem !important;
  }
}

/* 横屏适配 */
@media (max-width: 1024px) and (orientation: landscape) {
  /* 顶部栏更紧凑 */
  header {
    height: 48px !important;
  }
  /* 核心录分区高度优化 */
  .glass-card {
    margin-bottom: 0.5rem !important;
  }
}

/* ===== 4. 管理端（admin.html）响应式 ===== */
@media (max-width: 1024px) {
  /* 侧边栏：默认隐藏，变成抽屉式 */
  aside.fixed {
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    box-shadow: 4px 0 20px rgba(0, 0, 0, 0.1);
  }
  aside.fixed.open {
    transform: translateX(0);
  }
  /* 主内容区：全宽 */
  main {
    margin-left: 0 !important;
    padding: 1rem !important;
  }
  /* 顶部导航：增加汉堡菜单按钮 */
  .mobile-menu-btn {
    display: flex !important;
  }
}

@media (min-width: 1025px) {
  .mobile-menu-btn {
    display: none !important;
  }
}

/* 移动端遮罩层 */
@media (max-width: 1024px) {
  .sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 35;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
  }
  .sidebar-overlay.show {
    opacity: 1;
    pointer-events: auto;
  }
}

/* 表格响应式 */
@media (max-width: 768px) {
  table {
    font-size: 0.875rem;
  }
  .table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  /* 表格行高增大，方便触摸 */
  .table-row {
    min-height: 48px;
  }
}

/* ===== 5. 大屏端（display.html）移动适配 ===== */
@media (max-width: 1024px) {
  /* 大屏端在手机/平板上：保持横屏体验 */
  .display-container {
    transform-origin: top left;
  }
  /* 字体大小：根据屏幕调整 */
  .event-title {
    font-size: clamp(1.2rem, 4vw, 2rem) !important;
  }
  .score-number {
    font-size: clamp(1.5rem, 5vw, 3rem) !important;
  }
  /* 紧凑布局 */
  .top-bar {
    padding: 0.5rem !important;
  }
  .leaderboard-card {
    padding: 0.75rem !important;
  }
}

@media (max-width: 640px) {
  /* 手机端大屏：显示简化版 */
  .full-only {
    display: none !important;
  }
  .mobile-only {
    display: block !important;
  }
  /* 排名列表更紧凑 */
  .ranking-item {
    padding: 0.5rem 0.75rem !important;
  }
}

/* ===== 6. 通用触摸交互优化 ===== */
/* 触摸反馈：所有可点击元素 */
button, [role="button"], .clickable {
  transition: transform 0.15s, opacity 0.15s, background 0.15s;
}
button:active, [role="button"]:active, .clickable:active {
  transform: scale(0.97);
  opacity: 0.85;
}

/* 滑动删除支持 */
.swipe-item {
  position: relative;
  overflow: hidden;
  touch-action: pan-y;
}

/* ===== 7. 安全区适配（iPhone X+ 全面屏） ===== */
.safe-top {
  padding-top: env(safe-area-inset-top, 0);
}
.safe-bottom {
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.safe-left {
  padding-left: env(safe-area-inset-left, 0);
}
.safe-right {
  padding-right: env(safe-area-inset-right, 0);
}

/* 全局应用 */
.page-bg, .dark-bg {
  padding-top: env(safe-area-inset-top, 0);
  padding-bottom: env(safe-area-inset-bottom, 0);
  padding-left: env(safe-area-inset-left, 0);
  padding-right: env(safe-area-inset-right, 0);
}

/* ===== 8. 加载性能优化 ===== */
/* 减少重绘 */
.gpu-accelerated {
  transform: translateZ(0);
  will-change: transform;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}

/* 滚动容器优化 */
.scroll-container {
  -webkit-overflow-scrolling: touch;
  overflow-y: auto;
  overscroll-behavior: contain;
}

/* ===== 9. 暗色模式增强（可选） ===== */
@media (prefers-color-scheme: dark) {
  /* 系统暗色模式偏好（不影响大屏端深色主题） */
}

/* ===== 10. 减少动画（可访问性） ===== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ===== 11. 横竖屏切换监听 ===== */
@media (orientation: portrait) {
  .landscape-only {
    display: none !important;
  }
}
@media (orientation: landscape) {
  .portrait-only {
    display: none !important;
  }
}

/* ===== 12. Toast 移动端优化 ===== */
@media (max-width: 640px) {
  .toast-mobile {
    bottom: calc(1rem + env(safe-area-inset-bottom, 0)) !important;
    left: 1rem !important;
    right: 1rem !important;
    transform: none !important;
    max-width: none !important;
  }
}

/* ===== 13. 模态框移动端优化 ===== */
@media (max-width: 640px) {
  .modal-mobile {
    margin: 0 !important;
    border-radius: 16px 16px 0 0 !important;
    max-height: 90vh !important;
    width: 100% !important;
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    animation: slideUp 0.3s cubic-bezier(0.16, 1, 0.3, 1) !important;
  }
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

/* ===== 14. 数字键盘抖动修复 ===== */
@media (max-width: 640px) {
  /* 防止 iOS 键盘弹起时页面抖动 */
  html, body {
    height: 100%;
    overflow: hidden;
  }
  .scroll-container {
    height: 100%;
  }
}

/* ===== 15. 打印样式（备用） ===== */
@media print {
  .no-print {
    display: none !important;
  }
}
