/**
 * Matrix Quant — 共享基础样式 v3.0
 * 
 * 模块划分：
 *  1. CSS Reset & Variables
 *  2. 基础排版 & 字体
 *  3. 安全区 & 间距工具
 *  4. 移动端触控优化
 *  5. 滚动体验
 *  6. 通用动画
 *  7. 页面转场
 *  8. 骨架屏 Skeleton
 *  9. 深色模式增强
 *  10. PWA Standalone 适配
 */

/* ══════════════════════════════════════════════════════════
   1. Reset & CSS Custom Properties
   ══════════════════════════════════════════════════════════ */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

:root {
  /* ── 色彩系统 ─────────────────────────── */
  --bg-base:          #0a0a0a;
  --bg-surface:       #121212;
  --bg-surface-2:     #1a1a1a;
  --bg-overlay:       rgba(18,18,18,0.92);

  --border-subtle:    rgba(255,255,255,0.08);
  --border-medium:    rgba(255,255,255,0.12);
  --border-focus:     rgba(255,255,255,0.22);

  --text-primary:     #ededed;
  --text-secondary:   #a1a1aa;
  --text-muted:       #52525b;
  --text-inverse:     #0a0a0a;

  /* ── 品牌强调色 ───────────────────────── */
  --accent-amber:     #f59e0b;
  --accent-blue:      #3b82f6;
  --accent-purple:    #a855f7;
  --accent-emerald:   #10b981;
  --accent-indigo:    #af52de;
  --accent-red:       #ef4444;

  /* Apple System Colors */
  --system-blue:      #007aff;
  --system-green:     #34c759;
  --system-red:       #ff3b30;
  --system-yellow:    #ffcc00;

  /* ── 间距系统（4px 基准）──────────────── */
  --space-1:  4px;
  --space-2:  8px;
  --space-3:  12px;
  --space-4:  16px;
  --space-5:  20px;
  --space-6:  24px;
  --space-8:  32px;
  --space-10: 40px;
  --space-12: 48px;

  /* ── 圆角系统 ─────────────────────────── */
  --radius-sm:  6px;
  --radius-md:  10px;
  --radius-lg:  16px;
  --radius-xl:  20px;
  --radius-2xl: 24px;
  --radius-full: 9999px;

  /* ── 阴影系统 ─────────────────────────── */
  --shadow-sm:   0 1px 3px rgba(0,0,0,0.4);
  --shadow-md:   0 4px 16px rgba(0,0,0,0.5);
  --shadow-lg:   0 8px 32px rgba(0,0,0,0.6);
  --shadow-xl:   0 16px 48px rgba(0,0,0,0.7);

  /* ── 动画时间曲线 ─────────────────────── */
  --ease-standard:   cubic-bezier(0.4, 0, 0.2, 1);
  --ease-decelerate: cubic-bezier(0, 0, 0.2, 1);
  --ease-accelerate: cubic-bezier(0.4, 0, 1, 1);
  --ease-spring:     cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease-ios:        cubic-bezier(0.32, 0.72, 0, 1);

  /* ── Safe Area（iOS 安全区）─────────────── */
  --sat: env(safe-area-inset-top,    0px);
  --sar: env(safe-area-inset-right,  0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left,   0px);

  /* ── 字体栈 ───────────────────────────── */
  --font-sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC",
               "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", sans-serif;
  --font-mono: "SF Mono", "Cascadia Code", "Fira Code", Consolas, "Courier New", monospace;
  --font-display: "SF Pro Display", -apple-system, BlinkMacSystemFont, sans-serif;
}

/* ══════════════════════════════════════════════════════════
   2. 基础排版 & 全局元素
   ══════════════════════════════════════════════════════════ */
html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-behavior: smooth;
  height: 100%;
  /* 防止 iOS 橡皮筋滚动引起的 100vh 问题 */
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch;
}

body {
  background-color: var(--bg-base);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 1rem;
  line-height: 1.5;
  min-height: 100%;
  min-height: 100dvh; /* Dynamic Viewport Height */
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  /* 防止选中时出现蓝色高亮 */
  -webkit-user-select: none;
  user-select: none;
}

/* 允许文本内容选中 */
p, h1, h2, h3, h4, h5, h6,
input, textarea, [contenteditable], .selectable {
  -webkit-user-select: text;
  user-select: text;
}

a {
  color: inherit;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}

button, [role="button"] {
  cursor: pointer;
  font-family: var(--font-sans);
  -webkit-tap-highlight-color: transparent;
}

img, video, svg {
  max-width: 100%;
  display: block;
}

input, textarea, select {
  font-family: var(--font-sans);
  font-size: 1rem;  /* 防止 iOS 自动缩放 */
  color: var(--text-primary);
  background: transparent;
  border: none;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

/* ── 滚动条样式（仅桌面） ─────────────────────────────── */
@media (pointer: fine) {
  ::-webkit-scrollbar { width: 6px; height: 6px; }
  ::-webkit-scrollbar-track { background: transparent; }
  ::-webkit-scrollbar-thumb {
    background-color: rgba(255,255,255,0.12);
    border-radius: 3px;
    border: 1px solid var(--bg-base);
  }
  ::-webkit-scrollbar-thumb:hover { background-color: rgba(255,255,255,0.2); }
}

/* ══════════════════════════════════════════════════════════
   3. 安全区 & 间距工具类
   ══════════════════════════════════════════════════════════ */

/* Safe Area Padding */
.pt-safe   { padding-top:    env(safe-area-inset-top,    0px); }
.pb-safe   { padding-bottom: env(safe-area-inset-bottom, 0px); }
.pl-safe   { padding-left:   env(safe-area-inset-left,   0px); }
.pr-safe   { padding-right:  env(safe-area-inset-right,  0px); }
.px-safe   { padding-left: env(safe-area-inset-left, 0px); padding-right: env(safe-area-inset-right, 0px); }
.py-safe   { padding-top: env(safe-area-inset-top, 0px); padding-bottom: env(safe-area-inset-bottom, 0px); }

/* Margin Safe Area */
.mb-nav    { margin-bottom: calc(60px + env(safe-area-inset-bottom, 0px)); }

/* ══════════════════════════════════════════════════════════
   4. 移动端触控优化
   ══════════════════════════════════════════════════════════ */

/* 44px 最小触控目标（Apple HIG 标准） */
.touch-target {
  min-height: 44px;
  min-width:  44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 触控高亮反馈 */
.touchable {
  position: relative;
  overflow: hidden;
  transition: opacity 0.15s ease;
}

.touchable:active {
  opacity: 0.7;
}

/* Active 状态缩放（更自然的 iOS 感） */
.press-scale:active {
  transform: scale(0.97);
  transition: transform 0.1s var(--ease-standard);
}

/* 禁用文本选中 */
.no-select {
  -webkit-user-select: none;
  user-select: none;
}

/* 触控区域扩展（不改变视觉大小） */
.hit-expand::before {
  content: '';
  position: absolute;
  inset: -8px;
}

/* 触控卡片 */
.touch-card {
  border-radius: var(--radius-xl);
  transition: transform 0.12s var(--ease-standard), opacity 0.12s ease;
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
}

.touch-card:active {
  transform: scale(0.97);
  opacity: 0.85;
}

@media (hover: hover) and (pointer: fine) {
  .touch-card:active { transform: scale(0.99); }
}

/* ══════════════════════════════════════════════════════════
   5. 滚动体验
   ══════════════════════════════════════════════════════════ */

/* iOS 动量滚动 */
.scroll-momentum {
  -webkit-overflow-scrolling: touch;
  overflow-y: auto;
  overscroll-behavior-y: contain;
}

/* 水平滚动条 */
.scroll-x {
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scroll-snap-type: x mandatory;
  scrollbar-width: none;
}
.scroll-x::-webkit-scrollbar { display: none; }

/* 垂直滚动 */
.scroll-y {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  scrollbar-width: thin;
}

/* Snap 滚动 */
.snap-start  { scroll-snap-align: start; }
.snap-center { scroll-snap-align: center; }

/* ══════════════════════════════════════════════════════════
   6. 通用动画 & Keyframes
   ══════════════════════════════════════════════════════════ */

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes fade-in-up {
  from { opacity: 0; transform: translateY(16px); }
  to   { opacity: 1; transform: translateY(0); }
}

@keyframes fade-in-down {
  from { opacity: 0; transform: translateY(-16px); }
  to   { opacity: 1; transform: translateY(0); }
}

@keyframes slide-in-right {
  from { transform: translateX(100%); }
  to   { transform: translateX(0); }
}

@keyframes slide-in-up {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

@keyframes scale-in {
  from { opacity: 0; transform: scale(0.9); }
  to   { opacity: 1; transform: scale(1); }
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.5; }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@keyframes shimmer {
  0%   { background-position: -200% 0; }
  100% { background-position:  200% 0; }
}

@keyframes liquid-shine {
  to { background-position: -200% center; }
}

/* 工具类 */
.animate-fade-in     { animation: fade-in     0.3s var(--ease-decelerate) forwards; }
.animate-fade-in-up  { animation: fade-in-up  0.4s var(--ease-decelerate) forwards; }
.animate-scale-in    { animation: scale-in    0.35s var(--ease-spring) forwards; }
.animate-pulse       { animation: pulse       2s ease-in-out infinite; }
.animate-spin        { animation: spin        1s linear infinite; }

/* 延迟工具类 */
.delay-100 { animation-delay: 0.1s; }
.delay-200 { animation-delay: 0.2s; }
.delay-300 { animation-delay: 0.3s; }
.delay-400 { animation-delay: 0.4s; }
.delay-500 { animation-delay: 0.5s; }

/* ══════════════════════════════════════════════════════════
   7. 页面转场（View Transitions API）
   ══════════════════════════════════════════════════════════ */

@supports (view-transition-name: auto) {
  @view-transition { navigation: auto; }

  ::view-transition-old(root) {
    animation: 0.25s var(--ease-accelerate) both fade-out;
  }

  ::view-transition-new(root) {
    animation: 0.3s var(--ease-decelerate) both fade-in;
  }

  @keyframes fade-out {
    to { opacity: 0; transform: scale(0.98); }
  }
}

/* ══════════════════════════════════════════════════════════
   8. 骨架屏 Skeleton
   ══════════════════════════════════════════════════════════ */
.skeleton {
  background: linear-gradient(
    90deg,
    rgba(255,255,255,0.04) 25%,
    rgba(255,255,255,0.08) 50%,
    rgba(255,255,255,0.04) 75%
  );
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: var(--radius-sm);
}

.skeleton-text  { height: 14px; border-radius: 4px; }
.skeleton-title { height: 22px; border-radius: 6px; width: 60%; }
.skeleton-block { border-radius: var(--radius-lg); }

/* ══════════════════════════════════════════════════════════
   9. 通用组件样式
   ══════════════════════════════════════════════════════════ */

/* ── 玻璃态卡片 ────────────────────────────────────────── */
.glass-card {
  background: var(--bg-overlay);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
}

/* ── 极简输入框 ────────────────────────────────────────── */
.glass-input {
  background: transparent;
  border: 1px solid var(--border-subtle);
  color: var(--text-primary);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 0.875rem;
  transition: border-color 0.15s ease, background 0.15s ease;
  width: 100%;
  /* 防止 iOS 自动缩放：font-size 必须 >= 16px */
  font-size: max(16px, 0.875rem);
}

.glass-input:focus {
  border-color: var(--border-focus);
  outline: none;
  background: rgba(255,255,255,0.02);
}

.glass-input::placeholder { color: var(--text-muted); }

/* ── 极简按钮 ──────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 0.875rem;
  font-weight: 500;
  border-radius: var(--radius-full);
  padding: 10px 20px;
  border: none;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
  min-height: 44px;
  -webkit-tap-highlight-color: transparent;
}

.btn:active { transform: scale(0.96); }

.btn-primary {
  background: var(--accent-amber);
  color: var(--text-inverse);
}

.btn-ghost {
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--border-subtle);
  color: var(--text-primary);
}

.btn-ghost:hover { background: rgba(255,255,255,0.08); }

/* ── 状态徽章 ──────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 3px 8px;
  border-radius: var(--radius-full);
}

/* ── 分隔线 ────────────────────────────────────────────── */
.divider {
  height: 1px;
  background: var(--border-subtle);
  border: none;
  margin: 0;
}

/* ── 活跃状态指示点 ─────────────────────────────────────── */
.status-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  flex-shrink: 0;
}

.status-dot.active {
  background: var(--accent-emerald);
  box-shadow: 0 0 6px rgba(16,185,129,0.6);
  animation: pulse 2s ease-in-out infinite;
}

/* ── 内联标签导航 ─────────────────────────────────────── */
.tab-list {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  scroll-snap-type: x mandatory;
}

.tab-list::-webkit-scrollbar { display: none; }

.tab-item {
  flex-shrink: 0;
  padding: 6px 14px;
  border-radius: var(--radius-full);
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s ease;
  scroll-snap-align: start;
  min-height: 36px;
  display: flex;
  align-items: center;
  white-space: nowrap;
  border: 1px solid transparent;
}

.tab-item.is-active {
  background: rgba(255,255,255,0.06);
  border-color: var(--border-subtle);
  color: var(--text-primary);
}

/* ══════════════════════════════════════════════════════════
   10. PWA Standalone 模式适配
   ══════════════════════════════════════════════════════════ */

/* Standalone 模式（已安装到主屏幕）*/
@media (display-mode: standalone) {
  body {
    /* 顶部安全区 */
    padding-top: env(safe-area-inset-top, 0px);
    /* 底部导航高度 + 底部安全区 */
    padding-bottom: calc(60px + env(safe-area-inset-bottom, 0px));
  }

  /* 隐藏浏览器级别的"返回"按钮提示（自定义导航栏时需要） */
  .browser-only { display: none !important; }
}

/* Window Controls Overlay（桌面 PWA 自定义标题栏）*/
@media (display-mode: window-controls-overlay) {
  .app-titlebar {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: env(titlebar-area-height, 33px);
    padding-left: env(titlebar-area-x, 0);
    width: env(titlebar-area-width, 100%);
    -webkit-app-region: drag;
    background: var(--bg-base);
    border-bottom: 1px solid var(--border-subtle);
    z-index: 9999;
    display: flex;
    align-items: center;
    padding: 0 16px;
    gap: 8px;
  }

  body { padding-top: env(titlebar-area-height, 33px); }
}

/* ══════════════════════════════════════════════════════════
   11. 移动端响应式增强（≤ 768px）
   ══════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  /* 字体缩放适配 */
  html { font-size: 15px; }

  /* 最小触控高度 */
  button, a, [role="button"] { min-height: 44px; }

  /* 减少过重的视觉效果（节省渲染性能）*/
  .ambient-glow {
    opacity: 0.08 !important;
    filter: blur(80px) !important;
    animation: none !important;
  }

  #ambient-canvas { opacity: 0.08 !important; }

  /* 卡片 Active 态 */
  .spatial-card:active,
  .touch-card:active {
    transform: scale(0.97) !important;
    transition: transform 0.1s ease !important;
  }

  /* 禁用桌面端的 3D tilt 效果 */
  .spatial-card {
    transform: none !important;
    transform-style: flat !important;
  }
}

/* ══════════════════════════════════════════════════════════
   12. 平板端适配（769px – 1023px）
   ══════════════════════════════════════════════════════════ */
@media (min-width: 769px) and (max-width: 1023px) {
  .spatial-card:active { transform: scale(0.99) !important; }
}

/* ══════════════════════════════════════════════════════════
   13. 打印 & 无障碍
   ══════════════════════════════════════════════════════════ */
@media print {
  body { background: white !important; color: black !important; }
  .ambient-glow, #ambient-canvas, #pwa-bottom-nav { display: none !important; }
}

/* 减少动画（无障碍设置）*/
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }

  .ambient-glow, #ambient-canvas { display: none !important; }
}

/* 高对比度 */
@media (prefers-contrast: high) {
  :root {
    --border-subtle: rgba(255,255,255,0.2);
    --text-secondary: #c4c4c4;
    --text-muted: #8a8a8a;
  }
}

/* ══════════════════════════════════════════════════════════
   14. 工具类补充
   ══════════════════════════════════════════════════════════ */

.truncate      { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.line-clamp-2  { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.line-clamp-3  { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

.font-mono { font-family: var(--font-mono); }

.text-gradient-amber {
  background: linear-gradient(110deg, #f59e0b 10%, #fbbf24 50%, #f59e0b 90%);
  background-size: 200% auto;
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: liquid-shine 5s linear infinite;
}

.text-gradient-silver {
  background: linear-gradient(110deg, #ffffff 10%, #a1a1aa 40%, #ffffff 60%, #d4d4d8 90%);
  background-size: 200% auto;
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: liquid-shine 6s linear infinite;
}