:root {
  --canvas: #F5F5F3;
  --surface: #FFFFFF;
  --surface-muted: #FAFAF9;
  --border: #E8E7E4;
  --border-strong: #D6D4D0;
  --text: #171717;
  --text-secondary: #6B6B6B;
  --text-muted: #8F8F8F;
  --sidebar: #181818;
  --sidebar-hover: #232323;
  --sidebar-active: #2A2A2A;
  --sidebar-text: #A3A3A3;
  --accent: #2563EB;
  --accent-hover: #1D4ED8;
  --accent-soft: #EFF6FF;
  --success: #059669;
  --success-soft: #ECFDF5;
  --warning: #B45309;
  --warning-soft: #FFFBEB;
  --danger: #DC2626;
  --danger-soft: #FEF2F2;
  --radius: 8px;
  --radius-card: 12px;
  --shadow: 0 10px 30px rgba(15, 15, 15, .08);
  --font: "Avenir Next", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --mono: "SF Mono", "Menlo", "PingFang SC", monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background: var(--canvas);
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--text-muted); }
.small { font-size: 12px; }
.mono { font-family: var(--mono); }
.nowrap { white-space: nowrap; }

.icon {
  display: inline-block; width: 18px; height: 18px; flex: none;
  background: currentColor;
  -webkit-mask: var(--icon) center / contain no-repeat;
  mask: var(--icon) center / contain no-repeat;
}

/* ── 登录 ── */
.login {
  min-height: 100dvh; display: grid; place-items: center; padding: 24px;
  background: radial-gradient(circle at 20% 10%, #fff 0, var(--canvas) 45%);
}
.login-card {
  width: min(380px, 100%); background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 32px; box-shadow: var(--shadow);
}
.brand-row { display: flex; align-items: center; gap: 10px; margin-bottom: 28px; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 9px; background: var(--accent);
  flex: none; position: relative;
}
.brand-mark::after {
  content: ""; position: absolute; inset: 9px; border: 2px solid #fff;
  border-radius: 4px; border-left-color: transparent;
}
.brand-name { font-weight: 700; font-size: 16px; }
.brand-sub { font-size: 11px; letter-spacing: .14em; color: var(--text-muted); }
.login h1 { margin: 0 0 6px; font-size: 24px; }
.login-hint { margin: 0 0 20px; color: var(--text-secondary); font-size: 13px; line-height: 1.6; }
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; margin-bottom: 6px; font-size: 13px; color: var(--text-secondary); }
.form-error { min-height: 18px; margin: 0 0 12px; color: var(--danger); font-size: 13px; }

/* ── 壳层 ── */
.app { display: flex; min-height: 100dvh; }
.sidebar {
  width: 232px; flex: none; background: var(--sidebar); color: var(--sidebar-text);
  padding: 20px 12px 16px; display: flex; flex-direction: column; gap: 2px;
  position: sticky; top: 0; height: 100dvh; overflow-y: auto; z-index: 20;
}
.sidebar .brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; }
.sidebar .brand-name { color: #fff; }
.sidebar .brand-sub { color: #707070; }
.nav-group { font-size: 11px; letter-spacing: .08em; color: #666; padding: 14px 12px 6px; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: var(--radius);
  color: var(--sidebar-text); font-size: 14px;
}
.nav-item:hover { background: var(--sidebar-hover); color: #E7E7E7; }
.nav-item.active { background: var(--sidebar-active); color: #fff; font-weight: 600; }
.nav-spacer { flex: 1; }
.quick-box {
  background: #1F1F1F; border: 1px solid #2A2A2A; border-radius: 12px;
  padding: 12px; margin: 14px 4px 10px; display: grid; gap: 8px;
}
.quick-title { font-size: 12px; color: #707070; padding-left: 2px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  height: 56px; display: flex; align-items: center; gap: 10px; padding: 0 24px;
  background: var(--canvas); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 14px; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--success); flex: none; }
.status-dot.error { background: var(--danger); }
.top-stats { color: var(--text-secondary); font-size: 13px; }
.menu-btn { display: none; }
.view { padding: 24px 32px 40px; max-width: 1440px; width: 100%; margin: 0 auto; }

/* ── 按钮与表单 ── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  cursor: pointer; white-space: nowrap; font-size: 14px;
}
.btn:hover { background: var(--surface-muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.warning { background: var(--warning-soft); border-color: #FDE68A; color: var(--warning); }
.btn.warning.active { border-color: var(--warning); box-shadow: 0 0 0 3px rgba(180, 83, 9, .12); }
.btn.danger { background: var(--danger-soft); border-color: #FECACA; color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { height: 30px; padding: 0 10px; font-size: 13px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn {
  width: 34px; height: 34px; border-radius: var(--radius); border: 1px solid transparent;
  background: transparent; cursor: pointer; display: inline-grid; place-items: center;
}
.icon-btn:hover { background: #ECEBE8; }
input, select, textarea {
  height: 36px; padding: 0 10px; border: 1px solid var(--border-strong);
  border-radius: var(--radius); background: var(--surface); outline: none; width: 100%;
}
textarea { height: auto; min-height: 76px; padding: 8px 10px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37, 99, 235, .12); }
input[type="checkbox"], input[type="radio"] {
  width: 16px; height: 16px; flex: none; padding: 0; margin: 0;
  accent-color: var(--accent);
}
.picker-row:hover { background: var(--surface-muted); }
.search { max-width: 380px; }

/* ── 页面头部 ── */
.page-head { display: flex; align-items: flex-start; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-title { margin: 0; font-size: 24px; font-weight: 700; }
.page-sub { margin: 4px 0 0; color: var(--text-secondary); font-size: 13px; }
.page-actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; }

/* ── 卡片 / 统计 ── */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-card); padding: 18px; min-width: 0;
}
.card-title { font-size: 15px; font-weight: 600; margin: 0 0 14px; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 20px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-card); padding: 16px 18px; }
.stat-head { display: flex; align-items: center; gap: 8px; color: var(--text-secondary); font-size: 13px; }
.stat-value { font-size: 28px; font-weight: 700; margin-top: 10px; line-height: 1.1; }
.stat-hint { color: var(--text-muted); font-size: 12px; margin-top: 6px; }
.stat.accent .stat-value { color: var(--accent); }
.stat.success .stat-value { color: var(--success); }
.stat.warning .stat-value { color: var(--warning); }
.stat.danger .stat-value { color: var(--danger); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* ── 表格 ── */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid #F0EFEC; font-size: 13px; }
th { color: var(--text-muted); font-weight: 600; background: var(--surface-muted); white-space: nowrap; }
tbody tr:hover { background: #FAFBFC; }
tbody tr.selected { background: var(--accent-soft); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.danger { color: var(--danger); font-weight: 600; }
.row-actions { display: flex; gap: 4px; }
.row-actions .icon-btn { width: 28px; height: 28px; }
.row-actions .icon { width: 16px; height: 16px; }

.cards { display: none; }
.mobile-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-card);
  padding: 14px; margin-bottom: 10px;
}
.mobile-card .line { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; }
.mobile-card .line span:first-child { color: var(--text-muted); }

/* ── 徽章 ── */
.badge {
  display: inline-flex; align-items: center; height: 22px; padding: 0 8px;
  border-radius: 999px; font-size: 12px; border: 1px solid transparent; white-space: nowrap;
}
.badge.success { color: var(--success); background: var(--success-soft); border-color: #A7F3D0; }
.badge.danger { color: var(--danger); background: var(--danger-soft); border-color: #FECACA; }
.badge.warning { color: var(--warning); background: var(--warning-soft); border-color: #FDE68A; }
.badge.accent { color: var(--accent); background: var(--accent-soft); border-color: #BFDBFE; }
.badge.neutral { color: var(--text-secondary); background: #F3F3F1; border-color: var(--border); }

/* ── 筛选 chips ── */
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.chip {
  height: 32px; padding: 0 12px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-size: 13px; color: var(--text-secondary);
  white-space: nowrap;
}
.chip:hover { border-color: var(--border-strong); }
.chip.active { background: var(--accent-soft); border-color: #BFDBFE; color: var(--accent); font-weight: 600; }

/* ── 工具条 / 筛选行 ── */
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar .grow { flex: 1; min-width: 180px; }
.field-inline { display: flex; align-items: center; gap: 6px; color: var(--text-secondary); font-size: 13px; }
.field-inline input, .field-inline select { width: auto; }

/* ── 分页 ── */
.pager { display: flex; align-items: center; justify-content: flex-end; gap: 10px; margin-top: 14px; color: var(--text-secondary); font-size: 13px; }
.pager button { height: 30px; padding: 0 10px; }

/* ── 空状态 / 加载 ── */
.empty { padding: 44px 16px; text-align: center; color: var(--text-muted); }
.empty .icon { width: 34px; height: 34px; margin-bottom: 10px; opacity: .6; }
.skeleton { height: 14px; border-radius: 6px; background: linear-gradient(90deg, #EFEFEC 25%, #F7F7F5 50%, #EFEFEC 75%); background-size: 200% 100%; animation: shimmer 1.2s infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }
.banner { padding: 12px 14px; border-radius: var(--radius); background: var(--danger-soft); color: var(--danger); margin-bottom: 14px; display: flex; gap: 10px; align-items: center; }

/* ── 弹窗 / 抽屉 ── */
.overlay {
  position: fixed; inset: 0; background: rgba(16, 16, 16, .42); z-index: 100;
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.modal {
  background: var(--surface); border-radius: 16px; width: min(560px, 100%);
  max-height: min(86dvh, 900px); display: flex; flex-direction: column; box-shadow: var(--shadow);
}
.modal.wide { width: min(960px, 100%); }
.modal-head { display: flex; align-items: center; gap: 10px; padding: 18px 20px; border-bottom: 1px solid var(--border); }
.modal-title { font-size: 18px; font-weight: 700; margin: 0; }
.modal-head .icon-btn { margin-left: auto; }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }

/* ── 提示 ── */
.toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast {
  background: #181818; color: #fff; padding: 10px 14px; border-radius: 10px;
  font-size: 13px; box-shadow: var(--shadow); max-width: 340px;
}
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }

/* ── 日历 ── */
.calendar-layout { display: grid; grid-template-columns: 360px 1fr; gap: 16px; align-items: start; }
.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.cal-title { font-weight: 600; }
.cal-week { display: grid; grid-template-columns: repeat(7, 1fr); text-align: center; color: var(--text-muted); font-size: 12px; margin-bottom: 6px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.cal-day {
  position: relative; height: 42px; border: 1px solid transparent; background: transparent;
  border-radius: 8px; cursor: pointer; color: var(--text);
}
.cal-day:hover { background: var(--surface-muted); }
.cal-day.outside { color: #C7C5C0; cursor: default; }
.cal-day.active { background: var(--accent-soft); border-color: #BFDBFE; color: var(--accent); font-weight: 600; }
.cal-dots { position: absolute; left: 50%; bottom: 5px; transform: translateX(-50%); display: flex; gap: 3px; }
.cal-dots .dot { width: 4px; height: 4px; border-radius: 50%; display: block; }
.cal-dots .dot.in { background: var(--success); }
.cal-dots .dot.out { background: var(--danger); }
@media (max-width: 960px) {
  .calendar-layout { grid-template-columns: 1fr; }
}

/* ── 单据编辑器明细行 ── */
.items-head, .item-row { display: grid; gap: 8px; align-items: center; }
.items-head {
  grid-template-columns: 2.2fr 70px 80px 90px 95px 1fr 32px;
  color: var(--text-muted); font-size: 12px; padding: 8px 0; border-bottom: 1px solid var(--border);
}
.items-head.in { grid-template-columns: 2.2fr 60px 90px 1fr 32px; }
.item-row { padding: 6px 0; border-bottom: 1px solid #F3F2EF; }
.item-row.out { grid-template-columns: 2.2fr 70px 80px 90px 95px 1fr 32px; }
.item-row.in { grid-template-columns: 2.2fr 60px 90px 1fr 32px; }
.item-row input, .item-row .btn { height: 32px; }
.item-row input.computed { background: var(--surface-muted); color: var(--text-secondary); }
.item-row .btn { justify-content: flex-start; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.modal-foot { flex-wrap: wrap; }
.page-sub { white-space: pre-line; }
@media (max-width: 960px) {
  .items-head { display: none; }
  .item-row, .item-row.out, .item-row.in {
    grid-template-columns: 1fr 1fr; border: 1px solid var(--border);
    border-radius: 10px; padding: 10px; margin-bottom: 8px;
  }
  .item-row > :first-child { grid-column: 1 / -1; }
  .item-row .icon-btn { justify-self: end; }
}

/* ── 响应式 ── */
.tabbar { display: none; }
@media (max-width: 960px) {
  .sidebar {
    position: fixed; left: 0; top: 0; height: 100dvh; transform: translateX(-100%);
    transition: transform .2s ease; box-shadow: var(--shadow);
  }
  .sidebar.open { transform: none; }
  .menu-btn { display: inline-grid; }
  .main { width: 100%; }
  .view { padding: 16px 14px calc(76px + env(safe-area-inset-bottom)); }
  .topbar { padding: 0 14px; }
  .top-stats {
    font-size: 12px; white-space: nowrap; overflow: hidden;
    text-overflow: ellipsis; max-width: 52vw;
  }
  #refresh-hint { display: none; }
  .stats { grid-template-columns: 1fr 1fr; gap: 10px; }
  .stat-value { font-size: 22px; }
  .grid-2 { grid-template-columns: 1fr; }
  .page-actions { width: 100%; }
  .page-actions .btn { flex: 1; }
  .table-wrap { display: none; }
  .cards { display: block; }
  .toolbar .search { max-width: none; }
  .form-grid { grid-template-columns: 1fr; }
  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
    background: rgba(255, 255, 255, .96); border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom);
    backdrop-filter: blur(10px);
  }
  .tabbar a {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px;
    padding: 8px 0 7px; font-size: 11px; color: var(--text-muted);
  }
  .tabbar a.active { color: var(--accent); }
  .tabbar .icon { width: 20px; height: 20px; }
  .modal { border-radius: 16px 16px 0 0; margin-top: auto; max-height: 92dvh; }
  .overlay { align-items: flex-end; padding: 0; }
  .modal.fixed-bottom { width: 100%; }
}
