:root {
  --bg: #F5F7FA;
  --surface: #FFFFFF;
  --border: #E6EAF0;
  --border-strong: #D5DCE6;
  --text: #0F172A;
  --text-2: #475569;
  --muted: #64748B;
  --primary: #BE3B31;
  --primary-600: #9F2D24;
  --primary-soft: #FBEDEA;
  --green: #16A34A;
  --green-soft: #E9F9EF;
  --red: #DC2626;
  --red-soft: #FDEEF0;
  --amber: #C47A0B;
  --amber-soft: #FDF3E2;
  --blue: #2563EB;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 1px 3px rgba(16, 24, 40, .06);
  --shadow-hover: 0 8px 24px rgba(16, 24, 40, .10);
  --radius: 14px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: "Inter", "PingFang SC", "Microsoft YaHei", -apple-system, system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  font-size: 14px;
}
a { text-decoration: none; color: inherit; }
button { font-family: inherit; }

/* ============ 布局 ============ */
.app { display: flex; min-height: calc(100vh - 52px); }

/* ============ 侧边栏 ============ */
.sidebar {
  width: 244px;
  flex: 0 0 244px;
  background: #fff;
  border-right: 1px solid var(--border);
  position: sticky;
  top: 52px;
  height: calc(100vh - 52px);
  overflow-y: auto;
  padding: 16px 12px 24px;
}
.sidebar-head {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .5px;
  color: var(--muted);
  padding: 4px 10px 12px;
  text-transform: uppercase;
}
.metric-nav { display: flex; flex-direction: column; gap: 2px; }
.metric-nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  color: var(--text-2);
  font-size: 13.5px;
  border: 1px solid transparent;
  transition: background .12s ease, color .12s ease;
  text-align: left;
  background: transparent;
}
.metric-nav-item:hover { background: #F8FAFC; }
.metric-nav-item.active {
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 600;
}
.metric-nav-item .micon {
  width: 28px; height: 28px;
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  flex: 0 0 28px;
  background: #F1F5F9; color: var(--muted);
}
.metric-nav-item.active .micon { background: var(--primary); color: #fff; }
.metric-nav-item .micon svg { width: 15px; height: 15px; }
.metric-nav-item .mname { flex: 1; line-height: 1.25; }
.metric-nav-item .mcnt {
  font-size: 11px; color: var(--muted);
  background: #F1F5F9; border-radius: 999px; padding: 1px 8px;
}

/* ============ 主区 ============ */
.main { flex: 1; min-width: 0; padding: 24px 28px 80px; max-width: 1400px; }

.metric-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 18px; }
.metric-header .t h1 { font-size: 22px; font-weight: 700; display: flex; align-items: center; gap: 10px; }
.metric-header .t h1 .hicon {
  width: 38px; height: 38px; border-radius: 11px; flex: 0 0 38px;
  background: var(--primary-soft); color: var(--primary);
  display: inline-flex; align-items: center; justify-content: center;
}
.metric-header .t h1 .hicon svg { width: 20px; height: 20px; }
.metric-header .t p { color: var(--muted); font-size: 13px; margin-top: 8px; }
.metric-header .source-link {
  display: inline-flex; align-items: center; gap: 6px;
  margin-top: 10px; font-size: 12.5px; color: var(--blue);
  background: #EAF1FE; border-radius: 8px; padding: 5px 10px;
}
.metric-header .source-link svg { width: 13px; height: 13px; }

.heading-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border-strong);
  background: #fff; color: var(--text-2);
  border-radius: 9px; padding: 8px 14px; font-size: 13px; cursor: pointer;
  font-weight: 500; transition: background .12s, border-color .12s;
}
.btn:hover { background: #F8FAFC; border-color: #C9D1DC; }
.btn svg { width: 15px; height: 15px; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }
.btn-primary:hover { background: var(--primary-600); border-color: var(--primary-600); }
.btn-ghost { background: var(--primary-soft); border-color: transparent; color: var(--primary); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ============ KPI 卡 ============ */
.kpi-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; margin-bottom: 18px; }
.kpi-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow);
}
.kpi-card .k-label { font-size: 12.5px; color: var(--muted); margin-bottom: 8px; }
.kpi-card .k-value { font-size: 24px; font-weight: 700; letter-spacing: -.3px; }
.kpi-card .k-unit { font-size: 12px; color: var(--muted); font-weight: 500; margin-left: 3px; }
.kpi-card .k-delta { margin-top: 8px; font-size: 12px; font-weight: 600; display: flex; align-items: center; gap: 4px; }
.kpi-card .k-delta .up { color: var(--green); }
.kpi-card .k-delta .down { color: var(--red); }
.kpi-card .k-delta .flat { color: var(--muted); }
.kpi-card .k-hint { margin-top: 6px; font-size: 11px; color: #AEB6C2; }

/* ============ 卡片 ============ */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 18px 20px; margin-bottom: 18px;
}
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.card-head h3 { font-size: 15px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.card-head .hint { font-size: 12px; color: var(--muted); }
.chart-wrap { position: relative; height: 340px; }
.chart-wrap canvas { display: block; }

/* ============ 表格 ============ */
.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th, .data-table td {
  padding: 10px 12px; text-align: right; white-space: nowrap;
  border-bottom: 1px solid var(--border);
}
.data-table th:first-child, .data-table td:first-child { text-align: left; }
.data-table th { color: var(--muted); font-weight: 600; font-size: 12px; background: #F8FAFC; }
.data-table td.month-cell { font-weight: 600; color: var(--text); font-variant-numeric: tabular-nums; }
.data-table tr:hover td { background: #FBFCFE; }
.data-table .empty-cell { color: #CBD5E1; }
.row-actions { display: flex; gap: 4px; justify-content: flex-end; }
.row-actions button {
  border: none; background: none; cursor: pointer; padding: 3px 7px;
  border-radius: 6px; font-size: 12px; color: var(--blue); font-weight: 500;
}
.row-actions button:hover { background: #EAF1FE; }
.row-actions button.del { color: var(--red); }
.row-actions button.del:hover { background: var(--red-soft); }

/* ============ 空状态 ============ */
.empty-state {
  text-align: center; padding: 44px 20px; color: var(--muted);
}
.empty-state .eicon {
  width: 56px; height: 56px; margin: 0 auto 14px; border-radius: 16px;
  background: #F1F5F9; color: #94A3B8; display: flex; align-items: center; justify-content: center;
}
.empty-state .eicon svg { width: 26px; height: 26px; }
.empty-state p { font-size: 13px; margin-bottom: 16px; line-height: 1.6; }

/* ============ 模态 ============ */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .4);
  z-index: 2000; display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal {
  background: #fff; border-radius: 16px; box-shadow: 0 16px 48px rgba(16,24,40,.24);
  width: 100%; max-width: 560px; max-height: 90vh; overflow-y: auto;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 22px 0;
}
.modal-header h3 { font-size: 17px; font-weight: 700; }
.modal-body { padding: 16px 22px; }
.modal-footer { padding: 0 22px 20px; display: flex; justify-content: flex-end; gap: 8px; }
.icon-button {
  border: none; background: none; cursor: pointer; color: var(--muted);
  border-radius: 8px; padding: 6px; display: flex;
}
.icon-button:hover { background: #F1F5F9; }
.icon-button svg { width: 18px; height: 18px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field.full { grid-column: 1 / -1; }
.field > span { font-size: 12.5px; font-weight: 600; color: var(--text-2); }
.field input, .field textarea, .field select {
  border: 1.5px solid var(--border-strong); border-radius: 9px;
  padding: 9px 11px; font-size: 13.5px; font-family: inherit; color: var(--text);
  outline: none; background: #fff; width: 100%;
}
.field input:focus, .field textarea:focus, .field select:focus { border-color: var(--primary); }
.field .unit { position: absolute; }
.field-input-wrap { position: relative; display: flex; align-items: center; }
.field-input-wrap input { padding-right: 44px; }
.field-input-wrap .u {
  position: absolute; right: 11px; font-size: 12px; color: var(--muted); pointer-events: none;
}

/* ============ Toast ============ */
.toast {
  position: fixed; bottom: 28px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: #0F172A; color: #fff; padding: 11px 18px; border-radius: 10px;
  font-size: 13px; opacity: 0; pointer-events: none; transition: all .25s ease; z-index: 3000;
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--red); }

/* ============ 权限控制 ============ */
body.perm-read .only-edit, body.perm-read .only-delete { display: none !important; }
body.perm-edit .only-delete { display: none !important; }

@media (max-width: 900px) {
  .app { flex-direction: column; }
  .sidebar { width: 100%; flex: none; position: static; height: auto; border-right: none; border-bottom: 1px solid var(--border); }
  .metric-nav { flex-direction: row; flex-wrap: wrap; }
  .metric-nav-item { flex: 1 1 auto; }
  .main { padding: 16px; }
  .form-grid { grid-template-columns: 1fr; }
}
