/* =============================================================
   BongoCat Design System — Foundation (Phase 3 prototype)
   Single source of truth for tokens + components.
   Purpose: unify front/back office under one visual language.
   Purple is reserved for brand / primary CTA / selected / key
   interaction only. Hierarchy comes from bg/border/spacing,
   not shadows.
   ============================================================= */

/* ---------- 1. Design Tokens ---------- */
:root {
  /* Color — surfaces & text */
  --color-bg: #f6f7f9;
  --color-surface: #ffffff;
  --color-surface-2: #f1f3f6;
  --color-border: #e6e8ec;
  --color-border-strong: #d4d8df;
  --color-text: #1a1c23;
  --color-text-2: #4a4f5c;
  --color-text-muted: #8b909c;

  /* Color — primary (brand, used sparingly) */
  --color-primary: #6c4bd1;
  --color-primary-hover: #5a3cbe;
  --color-primary-soft: #f0ebfb;

  /* Color — semantic status (soft bg + solid text, never loud solids) */
  --color-success: #1a8a5a;
  --color-success-soft: #e7f6ef;
  --color-warning: #b7791f;
  --color-warning-soft: #fdf3e0;
  --color-danger: #c0392f;
  --color-danger-hover: #a93226;
  --color-danger-soft: #fdecea;
  --color-info: #2f6fb0;
  --color-info-soft: #eaf2fb;

  /* Typography scale (no stray 13/17/21/23px) */
  --fs-display: clamp(28px, 4vw, 40px);
  --fs-h1: 24px;      /* page title */
  --fs-h2: 18px;      /* section / card title */
  --fs-h3: 16px;      /* card title */
  --fs-body: 14px;
  --fs-body-lg: 15px;
  --fs-2: 13px;       /* secondary */
  --fs-label: 13px;
  --fs-help: 12px;    /* helper / caption */

  --lh-tight: 1.3;
  --lh-body: 1.6;
  --lh-dense: 1.45;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", Roboto, Helvetica, Arial, sans-serif;

  /* Spacing scale (no 13/17/27/31px) */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 20px;
  --sp-6: 24px;
  --sp-7: 32px;
  --sp-8: 40px;
  --sp-9: 48px;

  /* Radius (3 steps only) */
  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 16px;
  --r-pill: 999px;

  /* Shadow — overlays only */
  --sh-sm: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.04);
  --sh-md: 0 4px 12px rgba(16, 24, 40, 0.10);
  --sh-lg: 0 16px 48px rgba(16, 24, 40, 0.18);

  /* Motion */
  --motion: 180ms cubic-bezier(0.4, 0, 0.2, 1);

  /* Layout */
  --content-max: 1200px;
  --sidebar-w: 232px;
}

/* ---------- 2. Reset / base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font-sans);
  font-size: var(--fs-body);
  line-height: var(--lh-body);
  color: var(--color-text);
  background: var(--color-bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
h1, h2, h3, h4, p { margin: 0; }
a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* ---------- 3. App Shell (back office) ---------- */
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
}
.sidebar {
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  padding: var(--sp-5) var(--sp-3);
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  position: sticky;
  top: 0;
  height: 100vh;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-3) var(--sp-5);
  font-size: var(--fs-h3);
  font-weight: 700;
  color: var(--color-text);
}
.sidebar-brand .dot {
  width: 28px; height: 28px; border-radius: var(--r-sm);
  background: var(--color-primary-soft);
  color: var(--color-primary);
  display: grid; place-items: center; font-weight: 800;
}
.sidebar-section { font-size: var(--fs-help); color: var(--color-text-muted);
  padding: var(--sp-3) var(--sp-3) var(--sp-1); text-transform: uppercase;
  letter-spacing: 0.04em; }
.sidebar-nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar-link {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-3); border-radius: var(--r-md);
  color: var(--color-text-2); font-size: var(--fs-body);
  border: 1px solid transparent;
  transition: background var(--motion), color var(--motion);
}
.sidebar-link:hover { background: var(--color-surface-2); color: var(--color-text); }
.sidebar-link.active {
  background: var(--color-primary-soft);
  color: var(--color-primary);
  font-weight: 600;
}
.sidebar-link .ico { width: 18px; text-align: center; opacity: 0.9; }

.content {
  padding: var(--sp-7) var(--sp-7) var(--sp-9);
  max-width: var(--content-max);
  width: 100%;
}

/* ---------- 4. Page header ---------- */
.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--sp-4);
  margin-bottom: var(--sp-6);
  flex-wrap: wrap;
}
.page-header h1 {
  font-size: var(--fs-h1);
  line-height: var(--lh-tight);
  font-weight: 700;
}
.page-header .desc {
  color: var(--color-text-2);
  font-size: var(--fs-body-lg);
  margin-top: var(--sp-2);
  max-width: 60ch;
}
.page-header .actions { display: flex; gap: var(--sp-3); flex-wrap: wrap; }

/* ---------- 5. Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: var(--sp-2);
  font-size: var(--fs-body); font-weight: 600;
  border-radius: var(--r-md);
  border: 1px solid transparent;
  padding: 0 var(--sp-4);
  height: 40px;
  transition: background var(--motion), border-color var(--motion),
    color var(--motion), box-shadow var(--motion);
  white-space: nowrap;
  user-select: none;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-sm { height: 32px; padding: 0 var(--sp-3); font-size: var(--fs-2); }
.btn-lg { height: 44px; padding: 0 var(--sp-5); font-size: var(--fs-body-lg); }
.btn-primary { background: var(--color-primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--color-primary-hover); }
.btn-secondary {
  background: var(--color-surface); color: var(--color-text);
  border-color: var(--color-border-strong);
}
.btn-secondary:hover:not(:disabled) { background: var(--color-surface-2); }
.btn-ghost { background: transparent; color: var(--color-text-2); }
.btn-ghost:hover:not(:disabled) { background: var(--color-surface-2); color: var(--color-text); }
.btn-danger { background: transparent; color: var(--color-danger); }
.btn-danger:hover:not(:disabled) { background: var(--color-danger-soft); }
.btn-danger-solid { background: var(--color-danger); color: #fff; }
.btn-danger-solid:hover:not(:disabled) { background: var(--color-danger-hover); }
.btn-icon {
  width: 36px; height: 36px; padding: 0; border-radius: var(--r-md);
  background: transparent; color: var(--color-text-2);
  border: 1px solid transparent;
}
.btn-icon:hover:not(:disabled) { background: var(--color-surface-2); color: var(--color-text); }
.btn.wide { width: 100%; }

/* ---------- 6. Forms ---------- */
.field { display: flex; flex-direction: column; gap: var(--sp-2); margin-bottom: var(--sp-4); }
.field > label { font-size: var(--fs-label); font-weight: 600; color: var(--color-text); }
.field .help { font-size: var(--fs-help); color: var(--color-text-muted); }
.field .input, .field textarea, .field select {
  width: 100%;
  font-size: var(--fs-body);
  color: var(--color-text);
  background: var(--color-surface);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--r-md);
  padding: 0 var(--sp-3);
  height: 42px;
  transition: border-color var(--motion), box-shadow var(--motion);
}
.field textarea { height: auto; min-height: 120px; padding: var(--sp-3); resize: vertical; line-height: var(--lh-body); }
.field .input:focus, .field textarea:focus, .field select:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-soft);
}
.field.is-error .input,
.field.is-error textarea,
.field.is-error select { border-color: var(--color-danger); }
.field .error-msg { font-size: var(--fs-help); color: var(--color-danger); }
.field.is-disabled .input,
.field.is-disabled textarea { background: var(--color-surface-2); color: var(--color-text-muted); }

/* Switch */
.switch { display: inline-flex; align-items: center; gap: var(--sp-3); }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch .track {
  width: 40px; height: 22px; border-radius: var(--r-pill);
  background: var(--color-border-strong); position: relative;
  transition: background var(--motion);
}
.switch .track::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 18px; height: 18px; border-radius: 50%; background: #fff;
  box-shadow: var(--sh-sm); transition: transform var(--motion);
}
.switch input:checked + .track { background: var(--color-primary); }
.switch input:checked + .track::after { transform: translateX(18px); }
.switch input:focus-visible + .track { outline: 2px solid var(--color-primary); outline-offset: 2px; }

/* ---------- 7. Card / List / Table ---------- */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--r-lg);
  padding: var(--sp-5);
}
.list { display: flex; flex-direction: column; gap: var(--sp-3); }
.row {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--r-md);
  padding: var(--sp-4) var(--sp-5);
  display: flex; align-items: center; gap: var(--sp-4);
  transition: border-color var(--motion), background var(--motion);
}
.row:hover { border-color: var(--color-border-strong); }
.row .row-main { flex: 1; min-width: 0; }
.row .row-title { font-size: var(--fs-h3); font-weight: 600; }
.row .row-sub { font-size: var(--fs-2); color: var(--color-text-muted); margin-top: 2px; }
.row .row-actions { display: flex; align-items: center; gap: var(--sp-2); flex-shrink: 0; }

.table { width: 100%; border-collapse: collapse; font-size: var(--fs-body); }
.table th, .table td {
  text-align: left; padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--color-border);
}
.table th { font-size: var(--fs-2); color: var(--color-text-muted); font-weight: 600; }
.table tbody tr:hover { background: var(--color-surface-2); }

/* ---------- 8. Badge / Status ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: var(--fs-help); font-weight: 600;
  padding: 2px var(--sp-2); border-radius: var(--r-pill);
  background: var(--color-surface-2); color: var(--color-text-2);
}
.badge-success { background: var(--color-success-soft); color: var(--color-success); }
.badge-warning { background: var(--color-warning-soft); color: var(--color-warning); }
.badge-danger { background: var(--color-danger-soft); color: var(--color-danger); }
.badge-info { background: var(--color-info-soft); color: var(--color-info); }
.badge-primary { background: var(--color-primary-soft); color: var(--color-primary); }

/* ---------- 9. Toolbar / segmented ---------- */
.toolbar { display: flex; align-items: center; gap: var(--sp-3); margin-bottom: var(--sp-5); flex-wrap: wrap; }
.segmented {
  display: inline-flex; background: var(--color-surface-2);
  border-radius: var(--r-md); padding: 3px;
}
.segmented button {
  border: none; background: transparent; color: var(--color-text-2);
  font-size: var(--fs-2); font-weight: 600; padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-sm); transition: background var(--motion), color var(--motion);
}
.segmented button.active { background: var(--color-surface); color: var(--color-text); box-shadow: var(--sh-sm); }

/* ---------- 10. Modal / Overlay / Drawer ---------- */
/* 默认可见可点；z-index 9500 高于 .page-loading(9000)，防止骨架屏盖住弹窗导致 Promise 挂起。
   .open 仅为兼容/动画预留，新增消费者无需手动加。 */
.overlay {
  position: fixed; inset: 0; background: rgba(16, 24, 40, 0.45);
  display: flex; align-items: center; justify-content: center;
  padding: var(--sp-5); z-index: 9500;
}
.overlay.open { opacity: 1; pointer-events: auto; }
.modal {
  background: var(--color-surface); border-radius: var(--r-lg);
  box-shadow: var(--sh-lg); width: 100%; max-width: 560px;
  padding: var(--sp-6); transform: translateY(8px); transition: transform var(--motion);
  display: flex; flex-direction: column; gap: var(--sp-4);
  max-height: 88vh; overflow: hidden;
}
.modal.wide { max-width: 680px; }
.overlay.open .modal { transform: translateY(0); }
.modal h2, .modal h3 { font-size: var(--fs-h2); margin: 0; }
.modal .modal-body { overflow-y: auto; line-height: 1.8; white-space: pre-wrap; border: 1px solid var(--color-border); border-radius: var(--r-md); padding: var(--sp-4); }
.modal .modal-status { color: var(--color-text-muted); margin: 0; font-size: var(--fs-2); }
.modal p { margin: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: var(--sp-3); margin-top: var(--sp-2); flex-wrap: wrap; }

.drawer {
  position: fixed; top: 0; right: 0; bottom: 0;
  width: 460px; max-width: 92vw;
  background: var(--color-surface);
  box-shadow: var(--sh-lg);
  z-index: 9501; display: flex; flex-direction: column;
  transform: translateX(100%); transition: transform var(--motion);
}
.drawer.open { transform: translateX(0); }
.drawer-scrim {
  position: fixed; inset: 0; background: rgba(16, 24, 40, 0.45);
  z-index: 9500;
}
.drawer-scrim.open { opacity: 1; pointer-events: auto; }
.drawer-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--sp-5) var(--sp-6); border-bottom: 1px solid var(--color-border);
}
.drawer-header h3 { font-size: var(--fs-h2); font-weight: 700; }
.drawer-body { padding: var(--sp-6); overflow-y: auto; flex: 1; }
.drawer-section-title {
  font-size: var(--fs-label); font-weight: 700; color: var(--color-text-muted);
  text-transform: uppercase; letter-spacing: 0.04em; margin: var(--sp-6) 0 var(--sp-3);
}
.drawer-section-title:first-child { margin-top: 0; }
.drawer-footer {
  display: flex; justify-content: flex-end; gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-6); border-top: 1px solid var(--color-border);
  background: var(--color-surface);
}

/* ---------- 11. Toast ---------- */
.toast-wrap {
  position: fixed; bottom: var(--sp-6); left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: var(--sp-2); z-index: 200;
}
.toast {
  display: flex; align-items: center; gap: var(--sp-3);
  background: var(--color-text); color: #fff;
  padding: var(--sp-3) var(--sp-4); border-radius: var(--r-md);
  box-shadow: var(--sh-lg); font-size: var(--fs-body);
  animation: toast-in var(--motion);
}
.toast.success { background: var(--color-success); }
.toast.error { background: var(--color-danger); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- 12. States ---------- */
.empty-state {
  text-align: center; padding: var(--sp-9) var(--sp-5);
  color: var(--color-text-muted);
}
.empty-state .ico { font-size: 32px; opacity: 0.5; }
.empty-state p { margin-top: var(--sp-3); font-size: var(--fs-body-lg); }
.loading { padding: var(--sp-9); text-align: center; color: var(--color-text-muted); }
.skeleton {
  background: linear-gradient(90deg, var(--color-surface-2) 25%,
    #e9edf2 37%, var(--color-surface-2) 63%);
  background-size: 400% 100%; animation: sk 1.4s ease infinite;
  border-radius: var(--r-sm); height: 14px;
}
@keyframes sk { from { background-position: 100% 0; } to { background-position: 0 0; } }

/* ---------- 13. Responsive ---------- */
.menu-toggle { display: none; }
@media (max-width: 1024px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; z-index: 90; width: var(--sidebar-w);
    transform: translateX(-100%); transition: transform var(--motion);
  }
  .sidebar.open { transform: translateX(0); }
  .menu-toggle {
    display: inline-flex; align-items: center; justify-content: center;
    width: 40px; height: 40px; border-radius: var(--r-md);
    border: 1px solid var(--color-border); background: var(--color-surface);
  }
  .content { padding: var(--sp-5); }
}
@media (max-width: 720px) {
  .content { padding: var(--sp-4); }
  .drawer { width: 100%; max-width: 100%; }
  .page-header { align-items: stretch; }
  .row { flex-wrap: wrap; }
  .row .row-actions { width: 100%; justify-content: flex-end; }
}
