/**
 * 统一设计系统 - Design System Unified
 * 解决整个平台的对齐、间距、层级、typography 问题
 * Apple Design System + shadcn/ui 统一规范
 */

/* ========== 全局变量系统 ========== */
:root {
  /* Spacing Scale - 统一间距体系 (4px base) */
  --space-0: 0;
  --space-1: 0.25rem;   /* 4px */
  --space-2: 0.5rem;    /* 8px */
  --space-3: 0.75rem;   /* 12px */
  --space-4: 1rem;      /* 16px */
  --space-5: 1.25rem;   /* 20px */
  --space-6: 1.5rem;    /* 24px */
  --space-8: 2rem;      /* 32px */
  --space-10: 2.5rem;   /* 40px */
  --space-12: 3rem;     /* 48px */
  --space-16: 4rem;     /* 64px */

  /* Button Height System - 统一按钮高度 */
  --btn-height-sm: 32px;
  --btn-height-md: 40px;
  --btn-height-lg: 44px;
  --btn-height-xl: 48px;

  /* Input Height System - 统一输入框高度 */
  --input-height-sm: 36px;
  --input-height-md: 40px;
  --input-height-lg: 44px;
  --input-height-xl: 48px;

  /* Border Radius Scale - 统一圆角 */
  --radius-none: 0;
  --radius-sm: 0.25rem;    /* 4px */
  --radius-md: 0.5rem;     /* 8px */
  --radius-lg: 0.75rem;    /* 12px */
  --radius-xl: 1rem;       /* 16px */
  --radius-full: 9999px;

  /* Z-Index Scale - 统一层级 */
  --z-base: 0;
  --z-dropdown: 1000;
  --z-sticky: 1020;
  --z-fixed: 1030;
  --z-modal-backdrop: 1040;
  --z-modal: 1050;
  --z-popover: 1060;
  --z-tooltip: 1070;

  /* Typography Scale - 统一字体 */
  --font-size-xs: 0.75rem;      /* 12px */
  --font-size-sm: 0.875rem;     /* 14px */
  --font-size-base: 1rem;       /* 16px */
  --font-size-lg: 1.125rem;     /* 18px */
  --font-size-xl: 1.25rem;      /* 20px */
  --font-size-2xl: 1.5rem;      /* 24px */
  --font-size-3xl: 1.875rem;    /* 30px */
  --font-size-4xl: 2.25rem;     /* 36px */

  /* Line Height Scale - 统一行高 */
  --leading-none: 1;
  --leading-tight: 1.25;
  --leading-snug: 1.375;
  --leading-normal: 1.5;
  --leading-relaxed: 1.625;
  --leading-loose: 2;

  /* Font Weight - 统一字重 */
  --font-normal: 400;
  --font-medium: 500;
  --font-semibold: 600;
  --font-bold: 700;

  /* Transition - 统一动画 */
  --transition-fast: 150ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-base: 200ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-slow: 300ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* ========== 按钮统一样式 ========== */
button,
.btn,
[class*="shadcn-button"],
[role="button"] {
  /* 完美垂直居中 */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);

  /* 统一高度 */
  height: var(--btn-height-md);
  padding: 0 var(--space-4);

  /* 文字不换行 */
  white-space: nowrap;

  /* 统一字体 */
  font-size: var(--font-size-sm);
  font-weight: var(--font-medium);
  line-height: var(--leading-none);

  /* 统一圆角 */
  border-radius: var(--radius-lg);

  /* 统一过渡 */
  transition: all var(--transition-base);

  /* 禁用用户选择 */
  user-select: none;
  -webkit-user-select: none;

  /* 光标 */
  cursor: pointer;
}

/* 按钮尺寸变体 */
.shadcn-button-sm,
.btn-sm {
  height: var(--btn-height-sm);
  padding: 0 var(--space-3);
  font-size: var(--font-size-xs);
}

.shadcn-button-lg,
.btn-lg {
  height: var(--btn-height-lg);
  padding: 0 var(--space-6);
  font-size: var(--font-size-base);
}

.shadcn-button-xl,
.btn-xl {
  height: var(--btn-height-xl);
  padding: 0 var(--space-8);
  font-size: var(--font-size-lg);
}

/* 按钮内部图标对齐 */
button svg,
.btn svg,
[class*="shadcn-button"] svg {
  flex-shrink: 0;
  width: 1em;
  height: 1em;
}

/* 按钮文字对齐 */
button span,
.btn span,
[class*="shadcn-button"] span {
  display: inline-block;
  line-height: var(--leading-none);
}

/* 按钮禁用状态 */
button:disabled,
.btn:disabled,
[class*="shadcn-button"]:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

/* ========== 输入框统一样式 ========== */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="tel"],
input[type="url"],
input[type="search"],
select,
textarea {
  /* 统一高度 */
  height: var(--input-height-md);
  padding: 0 var(--space-3);

  /* 统一字体 */
  font-size: var(--font-size-sm);
  line-height: var(--leading-normal);

  /* 统一圆角 */
  border-radius: var(--radius-lg);

  /* 统一边框 */
  border: 1px solid var(--input-border);

  /* 统一背景 */
  background-color: var(--input-bg);
  color: var(--input-text);

  /* 统一过渡 */
  transition: all var(--transition-base);

  /* 宽度 */
  width: 100%;
  max-width: 100%;
}

/* Textarea 特殊处理 */
textarea {
  height: auto;
  min-height: 80px;
  padding: var(--space-3);
  resize: vertical;
}

/* Select 下拉箭头 */
select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg width='12' height='8' viewBox='0 0 12 8' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1.5L6 6.5L11 1.5' stroke='%23666' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right var(--space-3) center;
  padding-right: var(--space-8);
}

/* Focus 状态 */
input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

/* Placeholder */
input::placeholder,
textarea::placeholder {
  color: var(--input-placeholder);
  opacity: 1;
}

/* ========== Label 统一样式 ========== */
label {
  display: block;
  font-size: var(--font-size-sm);
  font-weight: var(--font-medium);
  color: var(--text-primary);
  margin-bottom: var(--space-2);
  line-height: var(--leading-tight);
}

/* Label 与 Input 对齐 */
.form-group,
.form-field {
  margin-bottom: var(--space-6);
}

.form-group label,
.form-field label {
  margin-bottom: var(--space-2);
}

/* 必填标记对齐 */
label .required,
label .text-red-500 {
  vertical-align: baseline;
  line-height: inherit;
}

/* ========== Checkbox & Radio 统一样式 ========== */
input[type="checkbox"],
input[type="radio"] {
  width: 16px;
  height: 16px;
  margin: 0;
  cursor: pointer;
  flex-shrink: 0;
}

/* Checkbox/Radio 与 Label 对齐 */
label:has(input[type="checkbox"]),
label:has(input[type="radio"]) {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  margin-bottom: 0;
}

/* ========== Switch 开关统一样式 ========== */
.switch-container {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
}

.switch-container label {
  margin: 0;
}

/* ========== Modal/Dialog 统一样式 ========== */
[role="dialog"],
.modal,
.dialog,
[id*="modal"],
[id*="Modal"],
[class*="modal"] {
  /* 统一层级 */
  z-index: var(--z-modal) !important;

  /* 圆角 */
  border-radius: var(--radius-xl);

  /* 背景 */
  background-color: var(--modal-bg);

  /* 阴影 */
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

/* Modal Backdrop */
.modal-backdrop,
.dialog-backdrop,
[class*="backdrop"] {
  z-index: var(--z-modal-backdrop) !important;
  background-color: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
}

/* Modal Header */
.modal-header,
.dialog-header {
  padding: var(--space-6) var(--space-8);
  border-bottom: 1px solid var(--border-light);
}

/* Modal Body */
.modal-body,
.dialog-body {
  padding: var(--space-6) var(--space-8);
  max-height: calc(100vh - 200px);
  overflow-y: auto;
}

/* Modal Footer - 底部按钮区域统一 */
.modal-footer,
.dialog-footer,
[class*="modal"] footer,
[class*="dialog"] footer {
  padding: var(--space-6) var(--space-8);
  border-top: 1px solid var(--border-light);

  /* 按钮布局 */
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

/* Modal Footer 按钮统一高度 */
.modal-footer button,
.dialog-footer button {
  height: var(--btn-height-md);
  min-height: var(--btn-height-md);
}

/* ========== Dropdown/Popover 统一层级 ========== */
[role="menu"],
[role="listbox"],
.dropdown-menu,
.popover,
[class*="dropdown"],
[class*="popover"] {
  z-index: var(--z-popover) !important;
  border-radius: var(--radius-lg);
  background-color: var(--card-bg);
  border: 1px solid var(--border-medium);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* ========== Tooltip 统一层级 ========== */
[role="tooltip"],
.tooltip,
[class*="tooltip"] {
  z-index: var(--z-tooltip) !important;
}

/* ========== Card 统一样式 ========== */
.card,
.shadcn-card,
[class*="card"] {
  background-color: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--radius-xl);
  padding: var(--space-6);
  box-shadow: var(--card-shadow);
  transition: all var(--transition-base);
}

.card:hover,
.shadcn-card:hover {
  border-color: var(--border-medium);
  box-shadow: var(--card-hover-shadow);
}

/* ========== Table 统一样式 ========== */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--font-size-sm);
}

thead th {
  padding: var(--space-3) var(--space-4);
  text-align: left;
  font-weight: var(--font-semibold);
  color: var(--table-header-text);
  background-color: var(--table-header-bg);
  border-bottom: 1px solid var(--table-border);
}

tbody td {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--table-border);
  color: var(--text-secondary);
}

tbody tr:hover {
  background-color: var(--table-row-hover);
}

/* ========== Typography 统一 ========== */
h1, h2, h3, h4, h5, h6 {
  color: var(--text-primary);
  font-weight: var(--font-semibold);
  line-height: var(--leading-tight);
  margin: 0;
}

h1 { font-size: var(--font-size-4xl); }
h2 { font-size: var(--font-size-3xl); }
h3 { font-size: var(--font-size-2xl); }
h4 { font-size: var(--font-size-xl); }
h5 { font-size: var(--font-size-lg); }
h6 { font-size: var(--font-size-base); }

p {
  color: var(--text-secondary);
  font-size: var(--font-size-base);
  line-height: var(--leading-normal);
  margin: 0;
}

/* ========== 中英文混排优化 ========== */
body {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  font-feature-settings: "kern" 1;
}

/* 中英文之间自动添加间距 */
.text-spacing {
  text-spacing: trim-start allow-end ideograph-alpha ideograph-numeric;
}

/* ========== Header/Sidebar 统一层级 ========== */
header,
.header,
.apple-header,
[class*="header"] {
  z-index: var(--z-sticky);
  background-color: var(--nav-bg);
  border-bottom: 1px solid var(--nav-border);
}

aside,
.sidebar,
.apple-sidebar,
[class*="sidebar"] {
  z-index: var(--z-sticky);
  background-color: var(--sidebar-bg);
  border-right: 1px solid var(--sidebar-border);
}

/* ========== Badge 统一样式 ========== */
.badge,
.shadcn-badge,
[class*="badge"] {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 20px;
  padding: 0 var(--space-2);
  font-size: var(--font-size-xs);
  font-weight: var(--font-medium);
  line-height: var(--leading-none);
  border-radius: var(--radius-full);
  white-space: nowrap;
}

/* ========== Code Block 统一样式 ========== */
code {
  font-family: 'SF Mono', Menlo, Monaco, 'Courier New', monospace;
  font-size: 0.875em;
  padding: 0.125em 0.375em;
  background-color: var(--code-bg);
  color: var(--code-text);
  border-radius: var(--radius-sm);
}

pre {
  padding: var(--space-4);
  background-color: var(--code-bg);
  color: var(--code-text);
  border-radius: var(--radius-lg);
  overflow-x: auto;
}

pre code {
  padding: 0;
  background: none;
  border-radius: 0;
}

/* ========== Loading State 统一 ========== */
.loading {
  pointer-events: none;
  opacity: 0.6;
}

.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ========== Responsive Utilities ========== */
@media (max-width: 768px) {
  :root {
    --btn-height-md: 44px;
    --input-height-md: 44px;
  }

  .modal,
  .dialog,
  [role="dialog"] {
    width: 95vw !important;
    max-width: 95vw !important;
  }
}

/* ========== Accessibility ========== */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* 高对比度模式 */
@media (prefers-contrast: high) {
  button,
  input,
  select,
  textarea {
    border-width: 2px;
  }
}

/* Focus Visible - 键盘导航高亮 */
:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 2px;
}

button:focus-visible,
a:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 2px;
}

/* ========== Utility Classes ========== */
.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.inline-flex-center {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.text-ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.text-wrap-balance {
  text-wrap: balance;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

/* ========== 暗色模式 ========== */

@media (prefers-color-scheme: DISABLED-dark) {
    :root {
        --color-bg-primary: #111827;
        --color-bg-secondary: #1f2937;
        --color-bg-tertiary: #374151;
        --color-text-primary: #f3f4f6;
        --color-text-secondary: #9ca3af;
        --color-text-tertiary: #6b7280;
        --color-border-primary: #374151;
        --color-border-secondary: #4b5563;
    }

    body {
        background: var(--color-bg-primary);
        color: var(--color-text-primary);
    }

    .container {
        background: var(--color-bg-primary);
    }

    .card,
    .panel {
        background: var(--color-bg-secondary);
        border-color: var(--color-border-primary);
    }

    h1, h2, h3, h4, h5, h6 {
        color: var(--color-text-primary);
    }

    p, span, div {
        color: var(--color-text-primary);
    }

    input, select, textarea {
        background: var(--color-bg-tertiary);
        border-color: var(--color-border-primary);
        color: var(--color-text-primary);
    }

    input:focus, select:focus, textarea:focus {
        background: var(--color-bg-secondary);
    }
}
