/* base.css —— 设计令牌(:root)、reset、栅格、断点、布局
 * 注意：CSP 为 style-src 'self'，所有样式必须放在外部文件，禁止内联 <style> 与 style="" 属性。
 * :root 中的令牌与 docs/frontend-design.md 的"设计令牌表"逐项对应，修改需同步两处。
 */

:root {
  /* ---------- 颜色：品牌 ---------- */
  --color-primary: #2563eb;
  --color-primary-hover: #1d4ed8;
  --color-primary-active: #1e40af;
  --color-primary-soft: #eff6ff;

  /* ---------- 颜色：语义 ---------- */
  --color-success: #16a34a;
  --color-success-soft: #dcfce7;
  --color-warning: #d97706;
  --color-warning-soft: #fef3c7;
  --color-danger: #dc2626;
  --color-danger-soft: #fee2e2;
  --color-info: #0891b2;
  --color-info-soft: #cffafe;

  /* ---------- 颜色：中性 ---------- */
  --color-bg: #f5f7fa;
  --color-surface: #ffffff;
  --color-surface-2: #f1f5f9;
  --color-border: #e2e8f0;
  --color-border-strong: #cbd5e1;
  --color-text: #0f172a;
  --color-text-muted: #64748b;
  --color-text-faint: #94a3b8;

  /* ---------- 颜色：侧边栏 ---------- */
  --color-sidebar-bg: #0f172a;
  --color-sidebar-text: #cbd5e1;
  --color-sidebar-active: #2563eb;

  /* ---------- 字号（大字号用 clamp 自适应）---------- */
  --font-size-xs: 0.75rem;
  --font-size-sm: 0.8125rem;
  --font-size-base: 0.9375rem;
  --font-size-md: 1rem;
  --font-size-lg: clamp(1.125rem, 0.95rem + 0.9vw, 1.25rem);
  --font-size-xl: clamp(1.25rem, 1rem + 1.3vw, 1.5rem);
  --font-size-2xl: clamp(1.5rem, 1.1rem + 2vw, 2rem);

  /* ---------- 字重 ---------- */
  --font-weight-normal: 400;
  --font-weight-medium: 500;
  --font-weight-semibold: 600;
  --font-weight-bold: 700;

  /* ---------- 行高 ---------- */
  --line-height-tight: 1.25;
  --line-height-base: 1.5;
  --line-height-relaxed: 1.7;

  /* ---------- 间距（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: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-full: 999px;

  /* ---------- 阴影 ---------- */
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, 0.16);

  /* ---------- 布局 ---------- */
  --sidebar-width: 184px;
  --header-height: 56px;
  --tap-target: 40px;

  /* ---------- 断点（仅供文档对照，媒体查询仍需写死 px）---------- */
  --bp-sm: 640px;
  --bp-md: 768px;
  --bp-lg: 1024px;

  /* ---------- 过渡 ---------- */
  --transition-fast: 120ms ease;
  --transition-base: 200ms ease;

  /* ---------- 字体族 ---------- */
  --font-sans: system-ui, -apple-system, "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
  --font-mono: ui-monospace, "Cascadia Code", Consolas, "Courier New", monospace;
}

/* ================= reset ================= */
*,
*::before,
*::after {
  box-sizing: border-box;
}

/*
 * hidden 属性必须能压过组件的 display:flex/inline-flex。
 * 浏览器默认的 [hidden]{display:none} 优先级低于类选择器（如 .field{display:flex}），
 * 不加这条的话，用 element.hidden = true 隐藏元素会失效。
 */
[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--font-size-base);
  line-height: var(--line-height-base);
  color: var(--color-text);
  background: var(--color-bg);
  min-height: 100vh;
}

h1, h2, h3, h4, h5, h6, p, ul, ol, figure {
  margin: 0;
}

ul, ol {
  padding: 0;
  list-style: none;
}

a {
  color: var(--color-primary);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

img, svg {
  max-width: 100%;
}

table {
  border-collapse: collapse;
  border-spacing: 0;
}

:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* ================= 布局外壳 ================= */
.app-shell {
  min-height: 100vh;
}

/* 侧边栏（>=1024px 固定左侧 184px） */
.app-sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: var(--sidebar-width);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  padding: var(--space-4) var(--space-3);
  background: var(--color-sidebar-bg);
  color: var(--color-sidebar-text);
  overflow-y: auto;
  z-index: 30;
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: 0 var(--space-2);
  min-height: var(--tap-target);
  color: #fff;
  font-weight: var(--font-weight-semibold);
}

.brand__logo {
  width: 28px;
  height: 28px;
  flex: 0 0 auto;
}

.brand__text {
  font-size: var(--font-size-md);
  white-space: nowrap;
}

.app-nav {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  flex: 1 1 auto;
  min-width: 0;
}

.nav-link {
  display: flex;
  align-items: center;
  min-height: var(--tap-target);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  color: var(--color-sidebar-text);
  font-size: var(--font-size-base);
  white-space: nowrap;
  transition: background var(--transition-fast), color var(--transition-fast);
}

.nav-link:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  text-decoration: none;
}

.nav-link[aria-current="page"] {
  background: var(--color-sidebar-active);
  color: #fff;
  font-weight: var(--font-weight-semibold);
}

.app-user {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2);
  border-top: 1px solid rgba(255, 255, 255, 0.12);
  min-width: 0;
}

.app-user__name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: var(--font-size-sm);
}

/* 内容区 */
.app-main {
  margin-left: var(--sidebar-width);
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.app-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4) max(var(--space-6), calc((100% - 1480px) / 2 + var(--space-6)));
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  min-height: var(--header-height);
  position: sticky;
  top: 0;
  z-index: 20;
}

.page-title {
  font-size: var(--font-size-xl);
  font-weight: var(--font-weight-semibold);
  line-height: var(--line-height-tight);
  flex: 1 1 auto;
  min-width: 0;
}

.app-header__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  align-items: center;
}

/* 标题与内容共用 1480px 阅读边界，超宽屏仍能在同一视域完成操作。 */
.page {
  padding: var(--space-6);
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
  width: 100%;
  max-width: 1480px;
  margin-inline: auto;
  min-width: 0;
}

/* ================= 栅格 ================= */
.grid {
  display: grid;
  gap: var(--space-4);
}

.grid--2 { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
.grid--3 { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.grid--4 { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }

.stack {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  align-items: center;
}

/* flex 子项默认 min-width:0，防止长文本/表格把容器撑破 */
.row > *,
.stack > *,
.app-header > * {
  min-width: 0;
}

/* ================= 工具类 ================= */
.muted { color: var(--color-text-muted); }
.faint { color: var(--color-text-faint); }
.mono { font-family: var(--font-mono); font-size: var(--font-size-sm); }
.nowrap { white-space: nowrap; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.text-right { text-align: right; }
.min-w-0 { min-width: 0; }
.hidden { display: none !important; }

.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;
}

/* ================= 断点：<1024px 侧边栏收成顶部横向滚动 tab ================= */
@media (max-width: 1023px) {
  .app-shell {
    display: block;
  }

  .app-sidebar {
    position: sticky;
    inset: auto;
    top: 0;
    width: 100%;
    flex-direction: row;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    overflow-y: visible;
    overflow-x: hidden;
    border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  }

  .brand__text {
    display: none;
  }

  .app-nav {
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    gap: var(--space-1);
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }

  .app-user {
    border-top: 0;
    padding: 0;
    flex: 0 0 auto;
  }

  .app-user__name {
    display: none;
  }

  .app-main {
    margin-left: 0;
  }

  .app-header {
    position: static;
    padding: var(--space-3) var(--space-4);
  }

  .page {
    padding: var(--space-4);
  }
}

@media (max-width: 640px) {
  .app-sidebar { display: grid; grid-template-columns: 1fr auto; }
  .app-nav { grid-column: 1 / -1; grid-row: 2; display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); overflow: visible; }
  .app-sidebar .nav-link { justify-content: center; white-space: normal; padding-inline: var(--space-1); font-size: var(--font-size-xs); }
  .app-user { grid-column: 2; grid-row: 1; }
  .brand__text { display: inline; }
  .page {
    padding: var(--space-3);
  }

  .app-header {
    padding: var(--space-3);
  }

  .grid--2,
  .grid--3,
  .grid--4 {
    grid-template-columns: minmax(0, 1fr);
  }
}
