/* ============================================================
   Components · 通用 UI 组件
   ============================================================ */

/* ---------- 按钮 ---------- */
.c-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 40px;
  padding: 0 16px;
  font-size: var(--fs-body);
  font-weight: var(--fw-medium);
  line-height: 1;
  border-radius: var(--radius-pill);
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-primary);
  cursor: pointer;
  white-space: nowrap;
  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);
  user-select: none;
}
.c-btn:active { transform: scale(0.97); }
.c-btn .icon { width: 18px; height: 18px; flex-shrink: 0; }

/* 尺寸 */
.c-btn--sm { height: 32px; padding: 0 12px; font-size: var(--fs-body-sm); }
.c-btn--md { height: 40px; padding: 0 16px; }
.c-btn--lg { height: 48px; padding: 0 20px; font-size: var(--fs-body-lg); }
.c-btn--block { width: 100%; }

/* 变体 */
.c-btn--primary { background: var(--text-primary); color: var(--text-inverse); }
.c-btn--primary:hover { background: var(--text-secondary); color: var(--text-inverse); }
.c-btn--primary:disabled { background: var(--border-strong); color: var(--text-disabled); opacity: 1; }

.c-btn--secondary {
  background: transparent;
  border-color: var(--border-default);
  color: var(--text-primary);
}
.c-btn--secondary:hover { background: var(--bg-hover); border-color: var(--border-strong); }

.c-btn--ghost { background: transparent; color: var(--text-primary); }
.c-btn--ghost:hover { background: var(--bg-hover); }
.c-btn--ghost.is-active { background: var(--bg-active); color: var(--text-primary); }

.c-btn--danger { background: var(--danger); color: #fff; }
.c-btn--danger:hover { background: #DC2626; }

.c-btn--brand { background: var(--brand); color: #fff; }
.c-btn--brand:hover { background: var(--brand-hover); }

/* Icon 按钮 (圆形) */
.c-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--text-primary);
  cursor: pointer;
  border: 0;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.c-icon-btn:hover { background: var(--bg-hover); }
.c-icon-btn .icon { width: 20px; height: 20px; }
.c-icon-btn--sm { width: 28px; height: 28px; }
.c-icon-btn--sm .icon { width: 16px; height: 16px; }
.c-icon-btn--lg { width: 44px; height: 44px; }
.c-icon-btn--lg .icon { width: 22px; height: 22px; }
.c-icon-btn--solid { background: var(--text-primary); color: var(--text-inverse); }
.c-icon-btn--solid:hover { opacity: 0.88; background: var(--text-primary); }

/* ---------- 输入框 ---------- */
.c-input {
  display: block;
  width: 100%;
  height: 44px;
  padding: 0 14px;
  background: var(--bg-input);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: var(--fs-body);
  transition: border-color var(--dur-fast) var(--ease);
  outline: 0;
}
.c-input::placeholder { color: var(--text-tertiary); }
.c-input:focus { border-color: var(--border-strong); }
.c-input.has-error { border-color: var(--danger); }

.c-input--sm { height: 36px; padding: 0 12px; font-size: var(--fs-body-sm); }
.c-input--lg { height: 52px; padding: 0 16px; }

.c-textarea {
  display: block;
  width: 100%;
  min-height: 96px;
  padding: 12px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: var(--fs-body);
  font-family: inherit;
  resize: vertical;
  outline: 0;
  transition: border-color var(--dur-fast) var(--ease);
}
.c-textarea::placeholder { color: var(--text-tertiary); }
.c-textarea:focus { border-color: var(--border-strong); }

.c-select {
  display: block;
  width: 100%;
  height: 44px;
  padding: 0 36px 0 14px;
  background: var(--bg-input);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: var(--fs-body);
  outline: 0;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%238E8E8E' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  cursor: pointer;
}

.c-field { display: flex; flex-direction: column; gap: 6px; }
.c-label {
  font-size: var(--fs-body-sm);
  color: var(--text-secondary);
  font-weight: var(--fw-medium);
}
.c-help { font-size: var(--fs-caption); color: var(--text-tertiary); }
.c-error { font-size: var(--fs-caption); color: var(--danger); }

/* 搜索框 */
.c-search {
  position: relative;
  display: flex;
  align-items: center;
}
.c-search .icon-search {
  position: absolute;
  left: 12px;
  width: 16px;
  height: 16px;
  color: var(--text-tertiary);
  pointer-events: none;
}
.c-search .c-input { padding-left: 36px; }

/* ---------- 卡片 ---------- */
.c-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  padding: 20px;
}
.c-card__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.c-card__title { font-size: var(--fs-h5); font-weight: var(--fw-semibold); }

/* ---------- Badge / Tag ---------- */
.c-badge {
  display: inline-flex;
  align-items: center;
  height: 22px;
  padding: 0 8px;
  font-size: var(--fs-caption);
  font-weight: var(--fw-medium);
  border-radius: var(--radius-pill);
  background: var(--bg-elevated-2);
  color: var(--text-secondary);
}
.c-badge--success { background: var(--success-bg); color: var(--success); }
.c-badge--warning { background: var(--warning-bg); color: var(--warning); }
.c-badge--danger { background: var(--danger-bg); color: var(--danger); }
.c-badge--info { background: var(--info-bg); color: var(--info); }
.c-badge--brand { background: rgba(16, 163, 127, 0.14); color: var(--brand); }
.c-badge .dot {
  display: inline-block;
  width: 6px; height: 6px;
  border-radius: 50%;
  background: currentColor;
  margin-right: 6px;
}

/* ---------- Avatar ---------- */
.c-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  min-width: 32px;
  min-height: 32px;
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--brand);
  color: #fff;
  font-size: var(--fs-body-sm);
  font-weight: var(--fw-semibold);
  flex-shrink: 0;
  overflow: hidden;
  max-width: none;
}
.c-avatar--sm { width: 24px; height: 24px; font-size: 11px; }
.c-avatar--lg { width: 40px; height: 40px; }
.c-avatar--xl { width: 64px; height: 64px; font-size: var(--fs-h3); }
.c-avatar img { width: 100%; height: 100%; object-fit: cover; }

/* ---------- 表格 ---------- */
.c-table-wrap {
  background: var(--bg-elevated);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.c-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-body);
}
.c-table th {
  text-align: left;
  font-size: var(--fs-caption);
  font-weight: var(--fw-semibold);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-default);
  background: transparent;
}
.c-table td {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-subtle);
  color: var(--text-primary);
  vertical-align: middle;
}
.c-table tr:last-child td { border-bottom: 0; }
.c-table tbody tr { transition: background var(--dur-fast) var(--ease); }
.c-table tbody tr:hover { background: var(--bg-hover); }
.c-table .col-actions { text-align: right; white-space: nowrap; }
.c-table .col-actions .c-icon-btn { margin-left: 4px; }

/* ---------- Modal ---------- */
.c-modal-mask {
  position: fixed;
  inset: 0;
  background: var(--bg-overlay);
  z-index: var(--z-modal);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  animation: fade-in var(--dur-base) var(--ease);
}
.c-modal-mask.is-open { display: flex; }

.c-modal {
  width: 100%;
  max-width: 480px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-xl);
  padding: 24px;
  box-shadow: var(--shadow-xl);
  animation: pop-in var(--dur-base) var(--ease);
}
.c-modal--lg { max-width: 720px; }
.c-modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.c-modal__title { font-size: var(--fs-h3); font-weight: var(--fw-semibold); }
.c-modal__body { font-size: var(--fs-body); color: var(--text-secondary); }
.c-modal__footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 24px;
}

/* 移动端改为底部抽屉 */
@media (max-width: 639px) {
  .c-modal-mask { align-items: flex-end; padding: 0; }
  .c-modal {
    max-width: 100%;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    padding: 24px 20px calc(24px + env(safe-area-inset-bottom));
    animation: slide-up var(--dur-base) var(--ease);
  }
  .c-modal::before {
    content: "";
    display: block;
    width: 36px; height: 4px;
    background: var(--border-strong);
    border-radius: 2px;
    margin: -8px auto 16px;
  }
}

/* ---------- Toast ---------- */
.c-toast-container {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0) + 16px);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: var(--z-toast);
  pointer-events: none;
}
.c-toast {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 240px;
  max-width: 360px;
  padding: 12px 16px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-default);
  border-left: 4px solid var(--info);
  border-radius: var(--radius-md);
  font-size: var(--fs-body-sm);
  color: var(--text-primary);
  box-shadow: var(--shadow-md);
  pointer-events: auto;
  animation: slide-down var(--dur-base) var(--ease);
}
.c-toast--success { border-left-color: var(--success); }
.c-toast--warning { border-left-color: var(--warning); }
.c-toast--danger { border-left-color: var(--danger); }

/* ---------- Tabs ---------- */
.c-tabs {
  display: flex;
  border-bottom: 1px solid var(--border-default);
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: none;
}
.c-tabs::-webkit-scrollbar { display: none; }
.c-tab {
  position: relative;
  padding: 12px 16px;
  font-size: var(--fs-body);
  color: var(--text-secondary);
  cursor: pointer;
  white-space: nowrap;
  transition: color var(--dur-fast) var(--ease);
  border: 0;
  background: transparent;
}
.c-tab:hover { color: var(--text-primary); }
.c-tab.is-active { color: var(--text-primary); }
.c-tab.is-active::after {
  content: "";
  position: absolute;
  left: 16px; right: 16px; bottom: -1px;
  height: 2px;
  background: var(--text-primary);
  border-radius: 2px;
}

/* ---------- Empty State ---------- */
.c-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 16px;
  text-align: center;
  color: var(--text-tertiary);
}
.c-empty .icon { width: 48px; height: 48px; opacity: 0.5; margin-bottom: 12px; }
.c-empty__title { font-size: var(--fs-body-lg); color: var(--text-secondary); margin-bottom: 4px; }
.c-empty__desc { font-size: var(--fs-body-sm); }

/* ---------- Dropdown ---------- */
.c-menu {
  background: var(--bg-elevated);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  padding: 6px;
  box-shadow: var(--shadow-lg);
  min-width: 180px;
  z-index: var(--z-dropdown);
}
.c-menu__item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-size: var(--fs-body);
  color: var(--text-primary);
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease);
}
.c-menu__item:hover { background: var(--bg-hover); }
.c-menu__item .icon { width: 16px; height: 16px; }
.c-menu__divider { height: 1px; background: var(--border-default); margin: 4px 0; }
.c-menu__item--danger { color: var(--danger); }

/* ---------- Switch ---------- */
.c-switch {
  position: relative;
  display: inline-block;
  width: 38px; height: 22px;
}
.c-switch input { opacity: 0; width: 0; height: 0; }
.c-switch__slider {
  position: absolute; inset: 0;
  background: var(--border-default);
  border-radius: 999px;
  transition: background var(--dur-fast) var(--ease);
  cursor: pointer;
}
.c-switch__slider::before {
  content: "";
  position: absolute;
  left: 3px; top: 3px;
  width: 16px; height: 16px;
  background: #fff;
  border-radius: 50%;
  transition: transform var(--dur-fast) var(--ease);
}
.c-switch input:checked + .c-switch__slider { background: var(--brand); }
.c-switch input:checked + .c-switch__slider::before { transform: translateX(16px); }

/* ---------- Pagination ---------- */
.c-pagination {
  display: flex;
  align-items: center;
  gap: 4px;
  justify-content: flex-end;
  padding: 16px;
}
.c-page-btn {
  min-width: 32px; height: 32px;
  padding: 0 8px;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font-size: var(--fs-body-sm);
  cursor: pointer;
}
.c-page-btn:hover { background: var(--bg-hover); color: var(--text-primary); }
.c-page-btn.is-active { background: var(--bg-active); color: var(--text-primary); }
.c-page-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ---------- 搜索 Modal (顶部居中变体) ---------- */
.c-search-modal-mask {
  position: fixed;
  inset: 0;
  background: var(--bg-overlay);
  z-index: var(--z-modal);
  display: none;
  justify-content: center;
  align-items: flex-start;
  padding: 80px 16px 16px;
  animation: fade-in var(--dur-base) var(--ease);
}
.c-search-modal-mask.is-open { display: flex; }

.c-search-modal {
  width: 100%;
  max-width: 560px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  overflow: hidden;
  animation: pop-in var(--dur-base) var(--ease);
  display: flex;
  flex-direction: column;
  max-height: calc(100dvh - 160px);
}
.c-search-modal__head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border-subtle);
}
.c-search-modal__head .icon-search {
  width: 18px; height: 18px;
  color: var(--text-tertiary);
  flex-shrink: 0;
}
.c-search-modal__input {
  flex: 1;
  height: 36px;
  background: transparent;
  border: 0;
  outline: 0;
  color: var(--text-primary);
  font-size: var(--fs-body);
}
.c-search-modal__input::placeholder { color: var(--text-tertiary); }
.c-search-modal__close {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: transparent;
  color: var(--text-tertiary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border: 0;
}
.c-search-modal__close:hover { background: var(--bg-hover); color: var(--text-primary); }
.c-search-modal__body {
  flex: 1;
  overflow-y: auto;
  padding: 8px 6px 12px;
}
.c-search-modal__group-title {
  padding: 10px 12px 4px;
  font-size: var(--fs-caption);
  color: var(--text-tertiary);
}
.c-search-modal__item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 12px;
  margin: 0 4px;
  border: 0;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--text-primary);
  font: inherit;
  font-size: var(--fs-body);
  text-align: left;
  text-decoration: none;
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease);
}
.c-search-modal__item:hover,
.c-search-modal__item.is-focus {
  background: var(--bg-hover);
  text-decoration: none;
}
.c-search-modal__item .icon { width: 18px; height: 18px; color: var(--text-secondary); flex-shrink: 0; }
.c-search-modal__item__title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.c-search-modal__empty {
  padding: 16px 14px;
  font-size: var(--fs-body-sm);
  color: var(--text-tertiary);
  text-align: center;
}

/* 移动端搜索 Modal:贴顶,占满宽度 */
@media (max-width: 639px) {
  .c-search-modal-mask { padding: 0; align-items: stretch; }
  .c-search-modal {
    max-width: 100%;
    max-height: 100dvh;
    border-radius: 0;
    border: 0;
  }
  .c-search-modal__head { padding: max(8px, env(safe-area-inset-top)) 12px 8px; }
}

/* ---------- 抽屉 (右侧滑入,主用于 admin 详情) ---------- */
.c-drawer-mask {
  position: fixed;
  inset: 0;
  background: var(--bg-overlay);
  z-index: var(--z-modal);
  display: none;
  animation: fade-in var(--dur-base) var(--ease);
}
.c-drawer-mask.is-open { display: block; }

.c-drawer {
  position: fixed;
  top: 0; right: 0;
  height: 100dvh;
  width: min(560px, 100%);
  background: var(--bg-canvas);
  border-left: 1px solid var(--border-default);
  box-shadow: var(--shadow-xl);
  display: flex;
  flex-direction: column;
  z-index: calc(var(--z-modal) + 1);
  transform: translateX(100%);
  transition: transform var(--dur-base) var(--ease);
}
.c-drawer-mask.is-open .c-drawer { transform: translateX(0); }
.c-drawer__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}
.c-drawer__title { font-size: var(--fs-h4); font-weight: var(--fw-semibold); }
.c-drawer__sub { font-size: var(--fs-caption); color: var(--text-tertiary); margin-top: 2px; }
.c-drawer__body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}
.c-drawer__footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

/* 抽屉内的展示块 */
.c-drawer-section { margin-bottom: 18px; }
.c-drawer-section__title {
  font-size: var(--fs-caption);
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 8px;
}
.c-drawer-section__body {
  background: var(--bg-elevated);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  font-size: var(--fs-body-sm);
  color: var(--text-primary);
  line-height: var(--lh-normal);
}
.c-drawer-section__body--err {
  border-color: rgba(239, 68, 68, 0.4);
  background: var(--danger-bg);
  color: var(--danger);
  font-family: var(--font-mono);
  font-size: var(--fs-caption);
  white-space: pre-wrap;
  word-break: break-word;
}
.c-drawer-section__body--mono {
  font-family: var(--font-mono);
  font-size: var(--fs-caption);
  white-space: pre-wrap;
  word-break: break-word;
}
.c-drawer-meta-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 16px;
}
.c-drawer-meta-grid > div { display: flex; flex-direction: column; gap: 2px; }
.c-drawer-meta-grid .k { font-size: var(--fs-caption); color: var(--text-tertiary); }
.c-drawer-meta-grid .v { font-size: var(--fs-body-sm); color: var(--text-primary); font-weight: var(--fw-medium); }

@media (max-width: 639px) {
  .c-drawer {
    width: 100%;
    border-left: 0;
  }
}

/* ---------- 浮层菜单 (Popover) 用于 composer 模型选择 ---------- */
.c-popover {
  position: fixed;
  background: var(--bg-elevated);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  padding: 6px;
  box-shadow: var(--shadow-lg);
  min-width: 220px;
  max-width: 320px;
  z-index: var(--z-dropdown);
  display: none;
}
.c-popover.is-open { display: block; }
.c-popover__empty {
  padding: 16px 12px;
  text-align: center;
  font-size: var(--fs-body-sm);
  color: var(--text-secondary);
  line-height: 1.5;
}
.c-popover__empty span {
  display: block;
  margin-top: 4px;
  font-size: var(--fs-caption);
  color: var(--text-tertiary);
}
.c-popover__item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease);
}
.c-popover__item:hover { background: var(--bg-hover); }
.c-popover__item.is-active { background: var(--bg-active); }
.c-popover__item__main { flex: 1; min-width: 0; }
.c-popover__item__title { font-size: var(--fs-body-sm); font-weight: var(--fw-medium); color: var(--text-primary); }
.c-popover__item__desc { font-size: var(--fs-caption); color: var(--text-tertiary); margin-top: 2px; }
.c-popover__item__check {
  width: 16px; height: 16px;
  color: var(--text-primary);
  flex-shrink: 0;
  margin-top: 2px;
  opacity: 0;
}
.c-popover__item.is-active .c-popover__item__check { opacity: 1; }

/* ---------- 动画 ---------- */
@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes pop-in {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: scale(1); }
}
@keyframes slide-up {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}
@keyframes slide-down {
  from { transform: translateY(-12px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
@keyframes pulse-dot {
  0%, 80%, 100% { opacity: 0.3; }
  40% { opacity: 1; }
}
