/* 班级助手设计系统（Phase 0）
 *
 * 只提供 token、基础组件与深浅主题，不覆盖页面既有样式：
 * 本文件必须先于页面 <style> 加载，页面规则自然覆盖共享规则。
 * 组件统一使用 ui- 前缀；不使用 !important 解决优先级冲突。
 */

/* ---------- 1. Token ---------- */

:root {
  color-scheme: light;

  --font: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei",
          "Segoe UI", sans-serif;

  --bg: #F6F8FC;
  --surface: #FFFFFF;
  --surface-2: #F1F4F9;
  --border: #E2E8F0;

  --text: #0F172A;
  --text-2: #475569;
  --text-3: #64748B;

  --primary: #3B6FF6;
  --primary-weak: #EAF0FF;
  --success: #16A34A;
  --success-weak: #E8F7EE;
  --success-ink: #15803D;
  --warning: #D97706;
  --warning-weak: #FFF7E6;
  --warning-ink: #B45309;
  --danger: #DC2626;
  --danger-weak: #FDECEC;
  --danger-ink: #B91C1C;
  --primary-ink: #2F5FDD;
  --focus: rgba(59, 111, 246, .4);

  --fs-xs: 12px;
  --fs-sm: 13px;
  --fs-base: 15px;
  --fs-md: 17px;
  --fs-lg: 20px;
  --fs-xl: 24px;

  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 24px;
  --sp-6: 32px;

  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;
  --r-xl: 20px;
  --r-full: 999px;

  --shadow-1: 0 1px 2px rgba(15, 23, 42, .04),
              0 1px 3px rgba(15, 23, 42, .06);
  --shadow-2: 0 8px 24px rgba(15, 23, 42, .08);

  --dur-fast: 120ms;
  --dur: 200ms;
  --dur-slow: 1400ms;
  --ease: cubic-bezier(.2, .7, .2, 1);

  /* 外壳布局 */
  --sidebar-w: 240px;
  --topbar-h: 56px;
  --bottomnav-h: 56px;

  /* 层级 */
  --z-layer: 9998;
  --z-toast: 9999;
  --z-modal: 10000;
}

/* 大屏深色主题：由 <html data-theme="dark"> 启用。
 * .ui-theme-dark 用于 styleguide 的局部深色预览（不改全局主题）。 */
html[data-theme="dark"],
.ui-theme-dark {
  color-scheme: dark;

  --bg: #0B1220;
  --surface: #121B2E;
  --surface-2: rgba(255, 255, 255, .06);
  --border: rgba(148, 163, 184, .22);

  --text: #E8ECF5;
  --text-2: #A9B6CC;
  --text-3: #7C8BA3;

  --primary: #4C8DFF;
  --primary-weak: rgba(76, 141, 255, .16);
  --success: #34D399;
  --success-weak: rgba(52, 211, 153, .14);
  --success-ink: #34D399;
  --warning: #FBBF24;
  --warning-weak: rgba(251, 191, 36, .14);
  --warning-ink: #FBBF24;
  --danger: #F87171;
  --danger-weak: rgba(248, 113, 113, .14);
  --danger-ink: #F87171;
  --primary-ink: #4C8DFF;
  --focus: rgba(76, 141, 255, .5);

  --shadow-1: 0 1px 2px rgba(0, 0, 0, .3),
              0 1px 3px rgba(0, 0, 0, .36);
  --shadow-2: 0 8px 24px rgba(0, 0, 0, .45);
}

.ui-theme-dark {
  background: var(--bg);
  color: var(--text);
}

/* ---------- 2. 基础工具 ---------- */

.ui-icon {
  display: inline-block;
  flex: none;
  vertical-align: -.15em;
}

.ui-muted { color: var(--text-2); }
.ui-hidden { display: none; }

/* 少量静态间距/文字工具，替代重复的内联 style */
.ui-mt-2 { margin-top: var(--sp-2); }
.ui-mt-3 { margin-top: var(--sp-3); }
.ui-mt-4 { margin-top: var(--sp-4); }
.ui-mb-2 { margin-bottom: var(--sp-2); }
.ui-mb-3 { margin-bottom: var(--sp-3); }
.ui-mb-4 { margin-bottom: var(--sp-4); }
.ui-text-xs { font-size: var(--fs-xs); line-height: 1.6; }
.ui-text-sm { font-size: var(--fs-sm); line-height: 1.6; }
.ui-text-left { text-align: left; }
.ui-text-warning { color: var(--warning-ink); }

/* 键盘焦点：自定义按钮/链接也有可见焦点环 */
a:focus-visible,
button:focus-visible,
[role="button"]:focus-visible,
select:focus-visible,
input:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* hidden 属性必须压过组件的 display（如 .ui-btn/.ui-tag 的 inline-flex）。
 * 不用 [hidden]{!important}：大屏页会用内联 display 切换 hidden 元素，
 * 内联样式必须仍能覆盖，否则配对窗无法显示。 */
.ui-btn[hidden],
.ui-tag[hidden],
.ui-chip[hidden],
.ui-badge[hidden],
.ui-check[hidden],
.ui-switch[hidden],
.ui-dot[hidden],
.ui-icon[hidden],
.ui-stack[hidden],
.ui-row[hidden],
.ui-grid[hidden],
.ui-tabs[hidden],
.ui-tab[hidden],
.ui-container[hidden],
.ui-table-wrap[hidden] { display: none; }

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

/* 弹窗打开时锁滚动 */
html.ui-noscroll { overflow: hidden; }

/* ---------- 3. 布局 ---------- */

.ui-container {
  width: 100%;
  max-width: 1120px;
  margin: 0 auto;
  padding-left: var(--sp-4);
  padding-right: var(--sp-4);
}

.ui-stack {
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
  min-width: 0;
}

.ui-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--sp-3);
  min-width: 0;
}

.ui-row--between { justify-content: space-between; }

.ui-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: var(--sp-4);
  min-width: 0;
}

.ui-card {
  min-width: 0;
  padding: var(--sp-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-1);
}

.ui-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  margin-bottom: var(--sp-3);
  min-width: 0;
}

.ui-section-title {
  margin: 0;
  font-size: var(--fs-md);
  font-weight: 600;
  color: var(--text);
}

.ui-toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--sp-2);
}

.ui-empty {
  padding: var(--sp-6) var(--sp-4);
  border: 1px dashed var(--border);
  border-radius: var(--r-lg);
  color: var(--text-3);
  font-size: var(--fs-sm);
  line-height: 1.6;
  text-align: center;
}

.ui-skeleton {
  min-height: 1em;
  border-radius: var(--r-sm);
  background: linear-gradient(90deg,
    var(--surface-2) 25%, var(--border) 37%, var(--surface-2) 63%);
  background-size: 400% 100%;
  animation: ui-skeleton var(--dur-slow) ease infinite;
}

/* ---------- 4. 表单 ---------- */

.ui-field {
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  min-width: 0;
}

.ui-label {
  color: var(--text-2);
  font-size: var(--fs-sm);
  font-weight: 500;
}

.ui-input,
.ui-select,
.ui-textarea {
  width: 100%;
  min-width: 0;
  min-height: 40px;
  padding: 9px 12px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  font: inherit;
  font-size: var(--fs-base);
  line-height: 1.4;
  transition: border-color var(--dur-fast) var(--ease),
              box-shadow var(--dur-fast) var(--ease);
}

.ui-textarea {
  min-height: 96px;
  resize: vertical;
}

.ui-input::placeholder,
.ui-textarea::placeholder { color: var(--text-3); }

.ui-input:focus-visible,
.ui-select:focus-visible,
.ui-textarea:focus-visible {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--focus);
}

.ui-input:disabled,
.ui-select:disabled,
.ui-textarea:disabled {
  opacity: .6;
  cursor: not-allowed;
}

.ui-check {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  min-height: 44px;
  color: var(--text);
  font-size: var(--fs-base);
  cursor: pointer;
}

.ui-check input {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--primary);
}

.ui-switch {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  min-height: 44px;
  color: var(--text);
  font-size: var(--fs-base);
  cursor: pointer;
  user-select: none;
}

.ui-switch input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.ui-switch-track {
  position: relative;
  flex: none;
  width: 42px;
  height: 24px;
  background: var(--border);
  border-radius: var(--r-full);
  transition: background var(--dur-fast) var(--ease);
}

.ui-switch-track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 1px 2px rgba(15, 23, 42, .25);
  transition: transform var(--dur-fast) var(--ease);
}

.ui-switch input:checked + .ui-switch-track { background: var(--primary); }
.ui-switch input:checked + .ui-switch-track::after { transform: translateX(18px); }

.ui-switch input:focus-visible + .ui-switch-track {
  box-shadow: 0 0 0 3px var(--focus);
}

/* ---------- 5. 按钮 ---------- */

.ui-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  min-height: 40px;
  padding: 0 var(--sp-4);
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  font: inherit;
  font-size: var(--fs-base);
  font-weight: 500;
  line-height: 1.2;
  text-align: center;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease),
              border-color var(--dur-fast) var(--ease),
              color var(--dur-fast) var(--ease),
              transform var(--dur-fast) var(--ease),
              box-shadow var(--dur-fast) var(--ease);
}

.ui-btn:focus-visible {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--focus);
}

.ui-btn:active { transform: scale(.98); }

.ui-btn:disabled,
.ui-btn[aria-disabled="true"] {
  opacity: .55;
  cursor: not-allowed;
  transform: none;
}

.ui-btn--primary {
  color: #fff;
  background: var(--primary);
  border-color: transparent;
}

.ui-btn--secondary {
  color: var(--text);
  background: var(--surface);
  border-color: var(--border);
}

.ui-btn--ghost {
  color: var(--text-2);
  background: transparent;
  border-color: transparent;
}

.ui-btn--danger {
  color: #fff;
  background: var(--danger);
  border-color: transparent;
}

.ui-btn--sm {
  min-height: 32px;
  padding: 0 var(--sp-3);
  font-size: var(--fs-sm);
}

.ui-btn--lg {
  min-height: 48px;
  padding: 0 var(--sp-5);
  font-size: var(--fs-md);
}

.ui-btn--block {
  display: flex;
  width: 100%;
}

@media (hover: hover) {
  .ui-btn--primary:hover { background: #2f5fdd; }
  .ui-btn--secondary:hover { background: var(--surface-2); }
  .ui-btn--ghost:hover { background: var(--surface-2); }
  .ui-btn--danger:hover { background: #c21f1f; }

  html[data-theme="dark"] .ui-btn--primary:hover,
  .ui-theme-dark .ui-btn--primary:hover { background: #3d7cf0; }
  html[data-theme="dark"] .ui-btn--danger:hover,
  .ui-theme-dark .ui-btn--danger:hover { background: #ef5b5b; }
}

@media (max-width: 600px) {
  .ui-btn,
  .ui-btn--sm { min-height: 44px; }
  .ui-input,
  .ui-select { min-height: 44px; }
  .ui-chip,
  .ui-tab { min-height: 44px; }
}

/* ---------- 6. 数据展示 ---------- */

.ui-table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.ui-table {
  width: 100%;
  border-collapse: collapse;
  color: var(--text);
  font-size: var(--fs-sm);
}

.ui-table th,
.ui-table td {
  padding: 10px 8px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: middle;
}

.ui-table th {
  color: var(--text-3);
  font-weight: 500;
  white-space: nowrap;
}

@media (hover: hover) {
  .ui-table tbody tr:hover { background: var(--surface-2); }
}

.ui-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  color: var(--text-2);
  background: var(--surface-2);
  border-radius: var(--r-full);
  font-size: var(--fs-xs);
  line-height: 1.6;
  white-space: nowrap;
}

.ui-tag--success { color: var(--success-ink); background: var(--success-weak); }
.ui-tag--warn    { color: var(--warning-ink); background: var(--warning-weak); }
.ui-tag--danger  { color: var(--danger-ink);  background: var(--danger-weak); }
.ui-tag--info    { color: var(--primary-ink); background: var(--primary-weak); }

.ui-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  min-height: 32px;
  padding: 4px 12px;
  color: var(--text-2);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-full);
  font: inherit;
  font-size: var(--fs-sm);
  line-height: 1.4;
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease),
              color var(--dur-fast) var(--ease),
              border-color var(--dur-fast) var(--ease);
}

.ui-chip:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--focus);
}

.ui-chip.is-on,
.ui-chip[aria-pressed="true"] {
  color: var(--primary);
  background: var(--primary-weak);
  border-color: transparent;
  font-weight: 500;
}

.ui-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  color: #fff;
  background: var(--primary);
  border-radius: var(--r-full);
  font-size: var(--fs-xs);
  font-weight: 600;
  line-height: 1;
}

.ui-badge--danger { background: var(--danger); }
.ui-badge--neutral { color: var(--text-2); background: var(--surface-2); }

.ui-dot {
  display: inline-block;
  flex: none;
  width: 8px;
  height: 8px;
  background: var(--text-3);
  border-radius: 50%;
}

.ui-dot--success { background: var(--success); }
.ui-dot--warn    { background: var(--warning); }
.ui-dot--danger  { background: var(--danger); }
.ui-dot--primary { background: var(--primary); }

/* ---------- 7. 导航 ---------- */

.ui-tabs {
  display: flex;
  gap: var(--sp-1);
  padding: var(--sp-1);
  overflow-x: auto;
  background: var(--surface-2);
  border-radius: var(--r-md);
  -webkit-overflow-scrolling: touch;
}

.ui-tab {
  flex: none;
  min-height: 36px;
  padding: 6px 14px;
  color: var(--text-2);
  background: transparent;
  border: 0;
  border-radius: var(--r-sm);
  font: inherit;
  font-size: var(--fs-sm);
  white-space: nowrap;
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease),
              color var(--dur-fast) var(--ease);
}

.ui-tab:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--focus);
}

.ui-tab.is-on,
.ui-tab[aria-selected="true"] {
  color: var(--text);
  background: var(--surface);
  box-shadow: var(--shadow-1);
  font-weight: 600;
}

.ui-bottom-sheet {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: var(--z-modal);
  max-height: 80vh;
  padding: var(--sp-4) var(--sp-4) calc(var(--sp-4) + env(safe-area-inset-bottom));
  overflow-y: auto;
  background: var(--surface);
  border-radius: var(--r-xl) var(--r-xl) 0 0;
  box-shadow: 0 -8px 24px rgba(15, 23, 42, .16);
  transform: translateY(100%);
  transition: transform var(--dur) var(--ease);
}

.ui-bottom-sheet.is-open { transform: none; }

/* ---------- 8. 反馈 ---------- */

#ui-layer {
  position: fixed;
  inset: 0;
  z-index: var(--z-layer);
  pointer-events: none;
}

.ui-toasts {
  position: fixed;
  top: calc(16px + env(safe-area-inset-top));
  left: 50%;
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  width: min(92vw, 420px);
  pointer-events: none;
  transform: translateX(-50%);
}

.ui-toast {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-3);
  padding: 12px 14px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-2);
  font-size: var(--fs-sm);
  line-height: 1.5;
  pointer-events: auto;
  animation: ui-toast-in var(--dur) var(--ease);
}

.ui-toast--success { border-left-color: var(--success); }
.ui-toast--error   { border-left-color: var(--danger); }
.ui-toast--warning { border-left-color: var(--warning); }
.ui-toast--info    { border-left-color: var(--primary); }

.ui-toast-icon {
  flex: none;
  margin-top: 1px;
  color: var(--primary);
}

.ui-toast--success .ui-toast-icon { color: var(--success); }
.ui-toast--error .ui-toast-icon   { color: var(--danger); }
.ui-toast--warning .ui-toast-icon { color: var(--warning); }

.ui-toast-msg {
  flex: 1;
  min-width: 0;
  word-break: break-word;
}

.ui-toast-close,
.ui-modal-close {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  color: var(--text-3);
  background: transparent;
  border: 0;
  border-radius: var(--r-sm);
  cursor: pointer;
}

.ui-toast-close:focus-visible,
.ui-modal-close:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--focus);
}

.ui-toast--out { animation: ui-toast-out var(--dur-fast) var(--ease) forwards; }

.ui-overlay {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--sp-4);
  background: rgba(15, 23, 42, .46);
  pointer-events: auto;
  animation: ui-fade-in var(--dur) var(--ease);
}

.ui-modal {
  width: min(96vw, 440px);
  max-height: 86vh;
  padding: var(--sp-5);
  overflow-y: auto;
  color: var(--text);
  background: var(--surface);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-2);
  outline: none;
  animation: ui-modal-in var(--dur) var(--ease);
}

.ui-modal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--sp-3);
  margin-bottom: var(--sp-3);
}

.ui-modal-title {
  margin: 0;
  font-size: var(--fs-md);
  font-weight: 600;
  line-height: 1.4;
}

.ui-modal-body {
  color: var(--text-2);
  font-size: var(--fs-base);
  line-height: 1.65;
  word-break: break-word;
}

.ui-modal-body p { margin: 0 0 var(--sp-2); }
.ui-modal-body p:last-child { margin-bottom: 0; }

.ui-modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--sp-2);
  margin-top: var(--sp-5);
}

.ui-spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: ui-spin .8s linear infinite;
}

.ui-progress {
  width: 100%;
  height: 8px;
  overflow: hidden;
  background: var(--surface-2);
  border-radius: var(--r-full);
}

.ui-progress > i {
  display: block;
  height: 100%;
  background: var(--primary);
  border-radius: inherit;
  transition: width var(--dur) var(--ease);
}

/* ---------- 9. 动效 ---------- */

@keyframes ui-toast-in {
  from { opacity: 0; transform: translateY(-8px); }
  to   { opacity: 1; transform: translateY(0); }
}

@keyframes ui-toast-out {
  to { opacity: 0; transform: translateY(-8px); }
}

@keyframes ui-fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes ui-modal-in {
  from { opacity: 0; transform: translateY(12px) scale(.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

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

@keyframes ui-skeleton {
  0%   { background-position: 100% 50%; }
  100% { background-position: 0 50%; }
}

@media (max-width: 480px) {
  .ui-modal { padding: var(--sp-4); }
  .ui-modal-actions { flex-direction: column-reverse; }
  .ui-modal-actions > .ui-btn { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  :root,
  html[data-theme="dark"],
  .ui-theme-dark {
    --dur-fast: 0ms;
    --dur: 0ms;
    --dur-slow: 0ms;
  }
  .ui-skeleton { animation: none; }
  .ui-spinner { animation: none; }
  .ui-btn:active { transform: none; }
}
