/* ============================================
   全局移动端适配样式 v2.0 — Android Material Design
   适用：管理端、裁判端、大屏端
   特性：底部导航、FAB、底部Sheet模态、Ripple、触摸优化
   ============================================ */

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

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

/* ===== 1. 全局响应式断点 ===== */

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

/* ===== 3. Android Ripple 涟漪效果 ===== */
.ripple {
  position: relative;
  overflow: hidden;
}
.ripple::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 5px;
  height: 5px;
  background: rgba(255, 255, 255, 0.5);
  opacity: 0;
  border-radius: 100%;
  transform: scale(1, 1) translate(-50%, -50%);
  transform-origin: 50% 50%;
  pointer-events: none;
}
.ripple:active::after {
  animation: ripple 0.4s ease-out;
}
@keyframes ripple {
  0% { transform: scale(0, 0) translate(-50%, -50%); opacity: 0.5; }
  100% { transform: scale(40, 40) translate(-50%, -50%); opacity: 0; }
}

/* ===== 4. 管理端 — 侧边抽屉导航（移动端+平板） ===== */
@media (max-width: 1023px) {
  /* 侧边栏改为抽屉式 */
  aside.fixed:not(.always-show) {
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.15);
    z-index: 55;
  }
  aside.fixed.open {
    transform: translateX(0);
  }
  /* 主内容区全宽 */
  main.ml-56 {
    margin-left: 0 !important;
    padding: 12px !important;
  }
  /* 汉堡菜单按钮显示 */
  .mobile-menu-btn {
    display: flex !important;
  }
  /* 底部导航不显示 */
  .bottom-nav {
    display: none !important;
  }
  /* 遮罩层 */
  .sidebar-overlay {
    position: fixed;
    inset: 0;
    top: 56px;
    background: rgba(0, 0, 0, 0.45);
    backdrop-filter: blur(2px);
    z-index: 45;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
  }
  .sidebar-overlay.show {
    opacity: 1;
    pointer-events: auto;
  }
}

/* 底部导航栏 — 不再使用（改为侧边抽屉） */
.bottom-nav {
  display: none !important;
}

/* ===== 5. FAB 浮动操作按钮 ===== */
@media (max-width: 1023px) {
  .fab {
    display: flex !important;
    position: fixed;
    bottom: 20px;
    right: 16px;
    width: 56px;
    height: 56px;
    border-radius: 18px;
    background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
    color: #fff;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 16px rgba(59, 130, 246, 0.4);
    z-index: 45;
    transition: transform 0.2s, box-shadow 0.2s;
  }
  .fab:active {
    transform: scale(0.92);
    box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
  }
}
.fab {
  display: none;
}

/* ===== 6. 底部 Sheet 模态框（安卓风格） ===== */
@media (max-width: 640px) {
  .modal-overlay {
    align-items: flex-end !important;
  }
  .modal-box {
    margin: 0 !important;
    border-radius: 20px 20px 0 0 !important;
    max-height: 92vh !important;
    width: 100% !important;
    max-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;
  }
  /* 模态框顶部拖拽指示器 */
  .modal-box::before {
    content: '';
    display: block;
    width: 36px;
    height: 4px;
    background: #cbd5e1;
    border-radius: 2px;
    margin: 0 auto 12px;
  }
}

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

/* ===== 7. 裁判端增强 ===== */
@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: 56px;
    padding: 0.75rem !important;
  }
  .athlete-card.current {
    transform: scale(1.02);
  }
  header.sticky {
    -webkit-backdrop-filter: blur(20px);
  }
}

/* 超小屏 */
@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;
  }
}

/* 平板 */
@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;
  }
}

/* ===== 8. 表格响应式 ===== */
@media (max-width: 768px) {
  table {
    font-size: 0.875rem;
  }
  .table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .table-row {
    min-height: 48px;
  }
}

/* ===== 9. 大屏端移动适配 ===== */
@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;
  }
}

/* ===== 10. 触摸交互 ===== */
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;
}

/* ===== 11. 安全区适配 ===== */
.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);
}

/* ===== 12. 性能优化 ===== */
.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;
}

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

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

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

/* ===== 16. 打印 ===== */
@media print {
  .no-print { display: none !important; }
  .bottom-nav { display: none !important; }
  .fab { display: none !important; }
}

/* ===== 17. 选择赛事全屏弹窗（v12.0 重构登录流程） ===== */
.selector-overlay {
  background: rgba(15, 23, 42, 0.55);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  animation: selectorFadeIn 0.2s ease-out;
}
.selector-box {
  animation: selectorSlideUp 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}
@keyframes selectorFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes selectorSlideUp {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

@media (max-width: 640px) {
  .selector-box {
    border-radius: 0 !important;
    max-width: 100vw !important;
  }
  /* 移动端列表项高度更大，方便点击 */
  .selector-box .grid > button,
  .selector-box [class*="space-y-2"] > button {
    min-height: 64px;
  }
}

/* ============================================
   ===== 18. 平板横屏适配 (Pad Landscape) =====
   适用：iPad / Android Pad 横屏 (宽度 1024px+)
   ============================================ */

/* 平板横屏基础布局（仅作用于 iPad 等平板设备，排除 PC 端和 WebView 1280+ 场景） */
@media (min-width: 768px) and (max-width: 1023px) and (orientation: landscape),
       (min-width: 768px) and (max-width: 1023px) and (orientation: portrait) {

  /* 根字号放大，整体缩放 */
  html {
    font-size: 17px !important;
  }

  /* 头部导航栏加宽 */
  header.sticky {
    height: 56px !important;
    padding: 0 24px !important;
  }
  header h1,
  header [class*="font-bold"] {
    font-size: 18px !important;
  }

  /* 主内容区利用宽屏 */
  main[class*="p-"] {
    padding: 20px !important;
    max-width: 100% !important;
  }

  /* 统计卡片：横屏 4 列 */
  .grid[class*="grid-cols-2"][class*="sm:grid-cols-3"],
  .grid[class*="grid-cols-2"][class*="md:grid-cols-4"] {
    grid-template-columns: repeat(4, 1fr) !important;
    gap: 16px !important;
  }

  /* 卡片内容放大 */
  .stat-card,
  [class*="bg-white rounded"][class*="p-4"] {
    padding: 20px !important;
  }
  .stat-card .text-2xl,
  [class*="bg-white"][class*="rounded"] .text-2xl {
    font-size: 2rem !important;
  }

  /* 表格行高加大，便于触摸 */
  .table-row {
    min-height: 52px !important;
  }
  table {
    font-size: 0.9375rem !important;
  }

  /* 触控目标增大 */
  button:not(.bottom-nav-item):not(.fab),
  [role="button"] {
    min-height: 48px !important;
  }
  .table-row button,
  .table-row a {
    min-height: 40px !important;
    min-width: 40px !important;
    padding: 8px 14px !important;
  }

  /* 输入框放大 */
  input[type="text"],
  input[type="number"],
  input[type="tel"],
  input[type="password"],
  select,
  textarea {
    min-height: 52px !important;
    font-size: 16px !important;
    border-radius: 12px !important;
    padding: 12px 16px !important;
  }

  /* 弹窗宽度优化（横屏不要全宽） */
  .modal-box {
    max-width: 640px !important;
    margin: 0 auto !important;
  }

  /* FAB 按钮放大 */
  .fab {
    width: 64px !important;
    height: 64px !important;
    border-radius: 20px !important;
    bottom: 24px !important;
    right: 24px !important;
  }
  .fab svg {
    width: 28px !important;
    height: 28px !important;
  }
}

/* 平板横屏专用 — 侧边栏恢复固定显示，汉堡隐藏 */
@media (min-width: 1024px) and (orientation: landscape) {

  /* 显示侧边栏（固定） */
  aside.fixed:not(.always-show) {
    display: block !important;
    width: 240px !important;
    transform: translateX(0) !important;
    box-shadow: none !important;
  }
  /* 主内容区留出侧边栏空间 */
  main.ml-56 {
    margin-left: 240px !important;
  }
  /* 底部导航隐藏 */
  .bottom-nav {
    display: none !important;
  }
  /* FAB 隐藏 */
  .fab {
    display: none !important;
  }
  /* 汉堡菜单隐藏 */
  .mobile-menu-btn {
    display: none !important;
  }
  /* 遮罩层隐藏 */
  .sidebar-overlay {
    display: none !important;
  }
}

/* 中等平板横屏 (768-1023px) — 侧边抽屉 + 全宽内容 */
@media (min-width: 768px) and (max-width: 1023px) and (orientation: landscape) {
  /* 主内容区全宽 */
  main[class*="p-"] {
    padding: 16px !important;
  }

  /* 双列布局利用横屏空间 */
  .grid[class*="grid-cols-1"]:not([class*="sm:grid-cols-2"]) {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}

/* ===== 19. 平板竖屏适配 (Pad Portrait) ===== */
@media (min-width: 768px) and (max-width: 1023px) and (orientation: portrait) {
  html {
    font-size: 16px !important;
  }
  /* 统计卡片 3 列 */
  .grid[class*="grid-cols-2"][class*="sm:grid-cols-3"] {
    grid-template-columns: repeat(3, 1fr) !important;
  }
}

/* ===== 20. 平板横竖屏切换过渡 ===== */
@media (min-width: 768px) {
  main,
  aside,
  .bottom-nav,
  .fab,
  .grid,
  .stat-card,
  .modal-box {
    transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  }
}

/* ===== 21. 平板字体清晰度 ===== */
@media (min-width: 768px) and (max-width: 1023px) {
  /* 标题层级 */
  h1 { font-size: 1.5rem !important; }
  h2 { font-size: 1.25rem !important; }
  h3 { font-size: 1.125rem !important; }
  /* 正文 */
  [class*="text-sm"] { font-size: 0.875rem !important; }
  [class*="text-xs"] { font-size: 0.8125rem !important; }
  /* 图标 */
  svg[class*="w-5"], svg[class*="w-6"] {
    width: 24px !important;
    height: 24px !important;
  }
  /* 表格内图标 */
  .table-row svg {
    width: 20px !important;
    height: 20px !important;
  }
}

/* ===== 22. 超宽屏平板 (iPad Pro 12.9 横屏 1366px) ===== */
@media (min-width: 1367px) and (orientation: landscape) {
  /* 侧边栏加宽 */
  aside.fixed:not(.always-show) {
    width: 260px !important;
  }
  main.ml-56 {
    margin-left: 260px !important;
    max-width: calc(100vw - 260px) !important;
  }
  /* 统计卡片可以 5-6 列 */
  .grid[class*="grid-cols-2"][class*="md:grid-cols-4"] {
    grid-template-columns: repeat(5, 1fr) !important;
  }
}

/* ===== 23. 平板横屏模态框优化 ===== */
@media (min-width: 768px) and (orientation: landscape) {
  .modal-overlay {
    align-items: center !important;
  }
  .modal-box {
    border-radius: 20px !important;
    max-height: 85vh !important;
    max-width: 720px !important;
    margin: 0 auto !important;
    animation: modalIn 0.3s cubic-bezier(0.16, 1, 0.3, 1) !important;
  }
  .modal-box::before {
    display: none !important;
  }
}

/* ===== 24. 平板横屏搜索/筛选区 ===== */
@media (min-width: 768px) and (orientation: landscape) {
  /* 搜索+筛选横向排列 */
  .flex[class*="gap-3"][class*="items-center"] {
    flex-wrap: nowrap !important;
  }
  input[placeholder*="搜索"],
  input[placeholder*="筛"] {
    max-width: 300px !important;
  }
  select[class*="text-sm"] {
    max-width: 200px !important;
    font-size: 14px !important;
  }
}

/* ===== 25. 平板横屏表格优化 ===== */
@media (min-width: 768px) and (orientation: landscape) {
  .table-scroll {
    border-radius: 16px;
    overflow: hidden;
  }
  .table-scroll table {
    min-width: auto !important;
  }
  .table-scroll thead th {
    position: sticky;
    top: 0;
    background: #f8fafc;
    z-index: 5;
    padding: 14px 16px !important;
    font-size: 0.875rem !important;
    font-weight: 600 !important;
  }
  .table-row td {
    padding: 12px 16px !important;
  }
  .dark .table-scroll thead th {
    background: #1e293b !important;
  }
}

/* ===== 26. 管理端 Header 精简化（移动端+平板） ===== */

/* <1024px：header 紧凑布局 */
@media (max-width: 1023px) {
  /* header 高度统一 48px */
  header.fixed.top-0 {
    height: 48px !important;
    padding: 0 8px !important;
  }
  /* 防止内容换行 */
  header.fixed.top-0 > div {
    white-space: nowrap;
  }
  /* 覆盖平板横屏的 18px 强制规则，标题保持紧凑 */
  header.fixed.top-0 [class*="font-bold"] {
    font-size: 13px !important;
  }
  /* 比赛切换按钮限制宽度 */
  header.fixed.top-0 .max-w-\[120px\] {
    max-width: 90px !important;
    font-size: 11px !important;
  }
  /* 主题切换按钮缩小 */
  header.fixed.top-0 button[class*="w-9"] {
    width: 36px !important;
    height: 36px !important;
  }
  /* logo 图标缩小 */
  header.fixed.top-0 .w-8 {
    width: 28px !important;
    height: 28px !important;
  }
  header.fixed.top-0 .w-8 svg {
    width: 16px !important;
    height: 16px !important;
  }
}

/* 手机竖屏 (<640px) — 极限紧凑 */
@media (max-width: 639px) {
  header.fixed.top-0 {
    padding: 0 6px !important;
  }
  header.fixed.top-0 .max-w-\[120px\] {
    max-width: 70px !important;
  }
}

/* 平板横屏 (768-1023px landscape) — 覆盖 18px 标题强制 */
@media (min-width: 768px) and (max-width: 1023px) and (orientation: landscape) {
  header.fixed.top-0 {
    height: 48px !important;
  }
  header.fixed.top-0 [class*="font-bold"] {
    font-size: 14px !important;
  }
  /* 比赛切换按钮可以稍宽 */
  header.fixed.top-0 .max-w-\[120px\] {
    max-width: 160px !important;
  }
}
