:root {
  --ink: #17365d;
  --ink-2: #41546b;
  --muted: #7b8ba0;
  --line: #e3e9f0;
  --line-2: #cbd8e6;
  --bg: #f4f7fb;
  --card: #ffffff;
  --brand: #1f4e78;
  --brand-2: #2f75b5;
  --brand-3: #3b82a0;
  --focus: #fff2cc;
  --focus-line: #e0b84a;
  --today: #fff4d6;
  --ok: #2e7d4f;
  --err: #b3261e;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(23, 54, 93, .06), 0 8px 24px rgba(23, 54, 93, .06);
  --font: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--brand-2); text-decoration: none; }
a:hover { text-decoration: underline; }

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

.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;
}
[hidden] { display: none !important; }

/* ---------- 顶栏 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  background: rgba(255, 255, 255, .94);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  max-width: 1600px; margin: 0 auto; padding: 10px 20px;
  display: flex; align-items: center; gap: 18px; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 9px; flex: 0 0 auto;
  background: linear-gradient(135deg, var(--brand) 0%, var(--brand-2) 100%);
  color: #fff; display: grid; place-items: center;
  font-size: 15px; font-weight: 700; letter-spacing: -1px;
}
.brand-name { font-size: 15px; font-weight: 700; letter-spacing: .2px; white-space: nowrap; }
.brand-sub { font-size: 11px; color: var(--muted); white-space: nowrap; }

.region-tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.region-tabs::-webkit-scrollbar { display: none; }
.region-tab {
  padding: 7px 13px; border-radius: 999px; white-space: nowrap;
  color: var(--ink-2); font-weight: 600; font-size: 13px;
  border: 1px solid transparent;
}
.region-tab:hover { background: #eef4fb; text-decoration: none; }
.region-tab.active { background: var(--brand); color: #fff; }

.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.user-chip { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-2); }
.user-avatar {
  width: 28px; height: 28px; border-radius: 50%; background: #e8f0f9;
  display: grid; place-items: center; font-size: 12px; font-weight: 700; color: var(--brand);
}
.user-name { font-weight: 600; }
.user-role { font-size: 11px; color: var(--muted); }
.logout-form { margin: 0; }

/* ---------- 按钮 / 表单 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 7px 12px; border-radius: 8px; border: 1px solid var(--line-2);
  background: #fff; color: var(--ink); font-size: 13px; font-weight: 600;
  cursor: pointer; white-space: nowrap;
}
.btn:hover { background: #f2f6fb; text-decoration: none; }
.btn-primary { background: var(--brand-2); border-color: var(--brand-2); color: #fff; }
.btn-primary:hover { background: var(--brand); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-sm { padding: 4px 8px; font-size: 12px; border-radius: 6px; }
.btn[disabled], .btn.disabled { opacity: .45; pointer-events: none; }
.btn-danger { color: var(--err); border-color: #f0c9c5; }
.btn-danger:hover { background: #fdf1f0; }

.input, .select, .textarea {
  padding: 8px 10px; border: 1px solid var(--line-2); border-radius: 8px;
  background: #fff; outline: none; min-width: 0;
}
.input:focus, .select:focus, .textarea:focus {
  border-color: var(--brand-2); box-shadow: 0 0 0 3px rgba(47, 117, 181, .12);
}
.textarea { width: 100%; min-height: 120px; resize: vertical; font-family: ui-monospace, Consolas, monospace; font-size: 12px; }
.label { display: block; font-size: 12px; font-weight: 600; color: var(--ink-2); margin-bottom: 5px; }
.field { margin-bottom: 14px; }

/* ---------- 主体 ---------- */
.main { max-width: 1600px; margin: 0 auto; padding: 16px 20px 40px; }

.page-head { display: flex; align-items: flex-end; gap: 14px; flex-wrap: wrap; margin-bottom: 12px; }
.page-title { font-size: 20px; font-weight: 800; letter-spacing: .2px; margin: 0; }
.page-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.page-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 10px; margin-bottom: 14px; }
.kpi {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px 14px; box-shadow: var(--shadow);
}
.kpi-label { font-size: 11px; color: var(--muted); font-weight: 600; letter-spacing: .3px; }
.kpi-value { font-size: 22px; font-weight: 800; letter-spacing: -.3px; font-variant-numeric: tabular-nums; margin-top: 2px; }
.kpi-value small { font-size: 12px; font-weight: 600; color: var(--muted); margin-left: 3px; }
.kpi-foot { font-size: 11px; color: var(--muted); margin-top: 1px; }
.kpi.accent { background: linear-gradient(180deg, #fff 0%, #f3f8ff 100%); border-color: #d7e5f4; }

.toolbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 9px 12px; margin-bottom: 12px; box-shadow: var(--shadow);
}
.search-wrap { position: relative; }
.search-wrap .input { width: 200px; padding-left: 30px; }
.search-wrap::before {
  content: "⌕"; position: absolute; left: 10px; top: 50%; transform: translateY(-52%);
  color: var(--muted); font-size: 16px; pointer-events: none;
}
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink-2); cursor: pointer; user-select: none; }
.check input { accent-color: var(--brand-2); width: 15px; height: 15px; }
.toolbar-meta { margin-left: auto; font-size: 12px; color: var(--muted); display: flex; gap: 12px; flex-wrap: wrap; }
.view-switch {
  display: inline-flex; gap: 2px; padding: 3px; border: 2px solid #1f4e78;
  border-radius: 10px; background: #fff; box-shadow: 0 1px 5px rgba(31, 78, 120, .18);
}
.view-btn {
  border: 0; background: transparent; color: #1f4e78; cursor: pointer;
  padding: 6px 14px; border-radius: 7px; font-size: 13px; font-weight: 800;
}
.view-btn:hover { background: #eaf1f9; }
.view-btn.active { background: #1f4e78; color: #fff; box-shadow: 0 2px 7px rgba(31, 78, 120, .36); }
.view-btn:focus-visible { outline: 3px solid #c98a1a; outline-offset: 2px; }
.legend { display: inline-flex; align-items: center; gap: 5px; }
.legend-swatch { width: 12px; height: 12px; border-radius: 3px; background: var(--focus); border: 1px solid var(--focus-line); display: inline-block; }
.legend-swatch.ok { background: #e8f4ea; border-color: #9cc4a6; }
.legend-swatch.bad { background: #fdecea; border-color: #d9a19c; }

.alert {
  background: #fff8e6; border: 1px solid #f0d38a; color: #7a5a12;
  padding: 10px 14px; border-radius: 10px; font-size: 13px; margin-bottom: 12px;
}
.alert.error { background: #fdf1f0; border-color: #f0c9c5; color: var(--err); }

/* ---------- 明细矩阵 ---------- */
.matrix-wrap {
  --store-w: 138px;
  --target-w: 76px;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: auto; max-height: calc(100vh - 250px);
  -webkit-overflow-scrolling: touch;
}
table.matrix { border-collapse: separate; border-spacing: 0; font-size: 12px; white-space: nowrap; }
table.matrix th, table.matrix td { border-right: 1px solid #edf1f6; border-bottom: 1px solid #edf1f6; padding: 0 6px; height: 30px; text-align: right; font-variant-numeric: tabular-nums; }
table.matrix thead th { position: sticky; z-index: 5; background: #f8fafd; }
table.matrix thead tr:nth-child(1) th { top: 0; height: 30px; }
table.matrix thead tr:nth-child(2) th { top: 30px; }

table.matrix th.col-store, table.matrix td.col-store { position: sticky; left: 0; z-index: 6; width: var(--store-w); min-width: var(--store-w); text-align: left; background: #fff; }
table.matrix th.col-target { position: sticky; z-index: 6; width: var(--target-w); min-width: var(--target-w); text-align: center; }
table.matrix th.t1 { left: var(--store-w); }
table.matrix th.t2 { left: calc(var(--store-w) + var(--target-w)); }
table.matrix thead th.col-store, table.matrix thead th.col-target { z-index: 8; background: var(--brand); color: #fff; font-weight: 700; font-size: 12px; }
table.matrix td.col-store { font-weight: 700; color: var(--ink); }
table.matrix td.col-store .code { display: block; font-weight: 500; font-size: 10px; color: var(--muted); line-height: 1.1; }
table.matrix td.col-target { text-align: center; background: #eef7ea; font-weight: 700; color: #2f5d33; }
table.matrix td.col-target.focus-target { background: var(--focus); box-shadow: inset 0 0 0 1px var(--focus-line); }

table.matrix thead th.day {
  text-align: center; color: #fff; font-weight: 700; font-size: 11px;
  background: var(--brand-2); border-right: 1px solid rgba(255, 255, 255, .25);
}
table.matrix thead th.day.alt { background: var(--brand-3); }
table.matrix thead th.day.today-day { background: #c98a1a; }
table.matrix thead th.metric { text-align: center; font-weight: 600; font-size: 10px; color: var(--ink-2); background: #eaf3fb; }
table.matrix thead th.metric.m0 { border-left: 1px solid var(--line-2); }
table.matrix thead th.metric.m3 { color: #1f6b46; }

table.matrix tbody tr:nth-child(even) td { background: #fbfcfe; }
table.matrix tbody tr:nth-child(even) td.col-store { background: #f7f9fc; }
table.matrix tbody tr:hover td { background: #f2f7fd; }
table.matrix tbody tr:hover td.col-store { background: #ecf4fd; }
table.matrix td.m0 { border-left: 1px solid var(--line-2); }
table.matrix td.rate { color: #4a5f78; }
table.matrix td.deal { color: #245b3d; font-weight: 600; }
table.matrix td.is-today { background: var(--today) !important; }
table.matrix td.is-latest { color: var(--ink); }
table.matrix td.empty { color: #c2ccd8; }

table.matrix td.rate-ok {
  background: #e8f4ea !important; color: #2f6b3f !important; font-weight: 700;
}
table.matrix td.rate-bad {
  background: #fdecea !important; color: #a33a34 !important; font-weight: 700;
}

/* ---------- 折线图卡片 ---------- */
.trend-view { margin-bottom: 12px; }
.trend-top { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 0 2px; }
.trend-note { font-size: 11px; }
.trend-list {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(480px, 1fr));
  gap: 12px; margin-top: 8px;
}
.trend-card {
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow); padding: 12px 14px 8px; min-width: 0;
}
.trend-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.trend-title { display: flex; align-items: baseline; gap: 7px; min-width: 0; }
.trend-name { font-size: 14px; font-weight: 800; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.trend-code { font-size: 10px; color: var(--muted); white-space: nowrap; }
.trend-facts { display: flex; align-items: baseline; gap: 3px; flex-wrap: wrap; margin-top: 3px; font-size: 11px; color: var(--muted); }
.trend-facts b { color: var(--ink-2); font-weight: 700; }
.trend-sep { color: #c2ccd8; margin: 0 2px; }
.trend-box { width: 100%; overflow: hidden; margin-top: 2px; }
.trend-box svg { display: block; width: 100%; height: auto; }
.trend-empty {
  grid-column: 1 / -1; padding: 40px 16px; text-align: center; color: var(--muted);
  background: #fff; border: 1px dashed var(--line-2); border-radius: var(--radius);
}
.chart-legend { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; font-size: 11px; color: var(--muted); }
.line-key { display: inline-flex; align-items: center; gap: 5px; }
.line-key::before { content: ""; display: inline-block; width: 16px; height: 3px; border-radius: 2px; background: #2f75b5; }
.line-key.deal::before { background: #2e8b57; }
.line-key.weekend::before { width: 12px; height: 12px; border-radius: 2px; background: #edf2f8; border: 1px solid #dbe5f0; }
.chart-foot { font-size: 11px; color: var(--muted); margin-top: 8px; }

.badge {
  display: inline-block; padding: 2px 7px; border-radius: 999px;
  font-size: 10px; font-weight: 700; background: var(--focus); color: #7a5a12;
  border: 1px solid var(--focus-line); white-space: nowrap;
}
.badge.muted { background: #f1f4f8; color: var(--muted); border-color: var(--line); }
.badge-in { background: #fff0b8; color: #8a4b08; border-color: #dfae4a; }
.badge-deal { background: #e9f5ec; color: #2c6640; border-color: #b4d6be; }
.badge-both { background: #f2edfa; color: #5a3f87; border-color: #d3c7e8; }

.empty-state { background: #fff; border: 1px dashed var(--line-2); border-radius: var(--radius); padding: 40px 20px; text-align: center; color: var(--muted); }
.empty-state h2 { color: var(--ink); font-size: 16px; margin: 0 0 6px; }

/* ---------- 登录 / 表单页 ---------- */
.auth-body { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: radial-gradient(1200px 600px at 20% -10%, #e7f1fb 0%, var(--bg) 55%); }
.auth-card { width: 100%; max-width: 380px; background: #fff; border: 1px solid var(--line); border-radius: 16px; box-shadow: 0 20px 50px rgba(23, 54, 93, .12); padding: 26px 24px; }
.auth-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }
.auth-title { font-size: 19px; font-weight: 800; margin: 0 0 4px; }
.auth-sub { font-size: 12px; color: var(--muted); margin: 0 0 18px; }
.auth-card .btn { width: 100%; padding: 10px; }
.auth-foot { margin-top: 16px; font-size: 11px; color: var(--muted); text-align: center; line-height: 1.6; }
.form-error { background: #fdf1f0; border: 1px solid #f0c9c5; color: var(--err); font-size: 12px; padding: 8px 10px; border-radius: 8px; margin-bottom: 12px; }
.flash { background: #eef8f1; border: 1px solid #bfe3cb; color: var(--ok); font-size: 12px; padding: 8px 10px; border-radius: 8px; margin-bottom: 12px; }

/* ---------- 管理 ---------- */
.panel { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; }
.panel h2 { font-size: 15px; margin: 0 0 10px; }
.panel-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.panel-head h2 { margin: 0; }
.panel-head .btn { margin-left: auto; }
table.grid { width: 100%; border-collapse: collapse; font-size: 12px; }
table.grid th, table.grid td { padding: 7px 8px; border-bottom: 1px solid #eef2f7; text-align: left; vertical-align: middle; }
table.grid th { color: var(--muted); font-size: 11px; font-weight: 700; background: #f8fafd; position: sticky; top: 0; }
table.grid td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.grid tr:last-child td { border-bottom: 0; }
.table-scroll { max-height: 420px; overflow: auto; border: 1px solid var(--line); border-radius: 10px; }
.status-ok { color: var(--ok); font-weight: 700; }
.status-err { color: var(--err); font-weight: 700; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, Consolas, monospace; font-size: 11px; }
.inline-form { display: inline; margin: 0; }
.stack { display: grid; gap: 10px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }

/* ---------- 响应式 ---------- */
@media (max-width: 1200px) {
  .kpis { grid-template-columns: repeat(5, minmax(0, 1fr)); }
  .kpi-value { font-size: 19px; }
}

@media (max-width: 900px) {
  .topbar-inner { padding: 8px 12px; gap: 10px; }
  .brand-sub, .user-role { display: none; }
  .brand-name { font-size: 14px; }
  .region-tabs { order: 3; width: 100%; padding-bottom: 2px; }
  .region-tab { padding: 6px 11px; font-size: 12px; }
  .topbar-right { gap: 6px; }
  .main { padding: 12px 12px 32px; }
  .page-head { align-items: flex-start; }
  .page-title { font-size: 17px; }
  .page-actions { margin-left: 0; width: 100%; }
  .page-actions .btn { flex: 0 0 auto; }
  .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
  .kpi { padding: 8px 10px; }
  .kpi-value { font-size: 18px; }
  .kpi:nth-child(4), .kpi:nth-child(5) { grid-column: span 1; }
  .toolbar { padding: 8px 10px; gap: 8px; }
  .search-wrap { flex: 1 1 100%; }
  .search-wrap .input { width: 100%; }
  .toolbar-meta { margin-left: 0; width: 100%; justify-content: space-between; }
  .view-switch { margin-left: auto; padding: 2px; }
  .view-btn { padding: 6px 12px; font-size: 12px; }
  .trend-list { grid-template-columns: 1fr; gap: 10px; }
  .trend-card { padding: 10px 12px 6px; }
  .trend-name { font-size: 13px; }
  .trend-facts { font-size: 10.5px; }
  .matrix-wrap { --store-w: 104px; --target-w: 60px; max-height: calc(100vh - 240px); }
  table.matrix { font-size: 11px; }
  table.matrix thead th.day { font-size: 10px; }
}

@media (max-width: 600px) {
  .matrix-wrap { --store-w: 96px; --target-w: 56px; }
  table.matrix th.col-store, table.matrix td.col-store { padding: 0 4px; }
  table.matrix th, table.matrix td { padding: 0 4px; }
  table.matrix td.col-store .code { font-size: 9px; }
  /* 手机屏窄：只固定门店列，目标列随表格横向滚动，给逐日指标留出空间 */
  table.matrix thead th.col-target { left: auto; }
  table.matrix tbody td.col-target { position: static; }
}

@media (min-width: 901px) {
  .mobile-only { display: none !important; }
}

@media (max-width: 420px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi:nth-child(5) { grid-column: span 2; }
  .chart-legend { gap: 10px; }
}

@media print {
  .topbar, .toolbar, .page-actions, .view-switch { display: none !important; }
  .matrix-wrap { max-height: none; overflow: visible; box-shadow: none; }
  body { background: #fff; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: .01ms !important; animation-duration: .01ms !important; }
}

/* 目标列随横向滚动固定（数据行） */
table.matrix tbody td.col-target {
  position: sticky; z-index: 4; width: var(--target-w); min-width: var(--target-w);
  background: #eef7ea;
}
table.matrix tbody td.t1 { left: var(--store-w); }
table.matrix tbody td.t2 { left: calc(var(--store-w) + var(--target-w)); }
table.matrix tbody tr:nth-child(even) td.col-target { background: #e9f3e5; }
table.matrix tbody tr:hover td.col-target { background: #e4f0de; }
table.matrix tbody tr td.col-target.focus-target { background: var(--focus) !important; }

@media (max-width: 600px) {
  table.matrix tbody td.col-target { position: static; }
  table.matrix thead th.col-target { left: auto; }
}

