:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --line: #e5e7eb;
  --text: #1f2329;
  --muted: #646a73;
  --blue: #3370ff;
  --up: #2ea121;
  --down: #d83931;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: -apple-system, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-bottom: var(--safe-bottom);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: calc(10px + var(--safe-top)) calc(14px + var(--safe-right)) 10px calc(14px + var(--safe-left));
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.sync-status {
  font-size: 12px;
  color: var(--muted);
  max-width: 220px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user {
  font-size: 12px;
  color: var(--muted);
  max-width: 72px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.role-badge {
  font-size: 11px;
  line-height: 1;
  padding: 3px 6px;
  border-radius: 4px;
  color: #3370ff;
  background: rgba(51, 112, 255, 0.1);
  white-space: nowrap;
}

.btn {
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 14px;
  line-height: 1.2;
  cursor: pointer;
  min-height: 36px;
  touch-action: manipulation;
}
.btn:hover { border-color: var(--blue); color: var(--blue); }
.btn:active { background: #f0f4ff; }
.btn:disabled { opacity: .5; cursor: wait; }

main {
  max-width: 1080px;
  margin: 0 auto;
  padding: 14px calc(14px + var(--safe-right)) 28px calc(14px + var(--safe-left));
}

.banner {
  background: #fff3e0;
  color: #b25e00;
  border: 1px solid #ffd591;
  border-radius: 8px;
  padding: 10px 14px;
  margin-bottom: 14px;
  font-size: 13px;
  line-height: 1.45;
}
.hidden { display: none; }

.metric-group { margin-bottom: 18px; }

.group-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin: 0 0 8px;
  padding: 0 2px 0 10px;
  letter-spacing: .5px;
  position: relative;
}
.group-title::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 14px;
  border-radius: 2px;
  background: var(--blue);
}
.metric-group--business .group-title::before { background: var(--up); }

.cards {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 12px 10px;
  cursor: pointer;
  transition: box-shadow .15s, border-color .15s, background .15s, transform .15s;
  min-width: 0;
  -webkit-user-select: none;
  user-select: none;
}
.card:hover { box-shadow: 0 4px 14px rgba(31,35,41,.10); transform: translateY(-1px); }
.card:active { background: #f8faff; }
.card.active {
  border-color: var(--blue);
  box-shadow: 0 0 0 1px var(--blue), 0 4px 14px rgba(51,112,255,.08);
  background: #f8faff;
}
.card .name {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
  line-height: 1.35;
  word-break: break-word;
  padding-right: 26px;
}
.card .value {
  font-size: 22px;
  font-weight: 600;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
  word-break: break-all;
}
.card .delta {
  font-size: 12px;
  margin-top: 4px;
  color: var(--muted);
  line-height: 1.3;
}
.card .delta.up { color: var(--up); }
.card .delta.down { color: var(--down); }
.card .delta span { white-space: nowrap; margin-right: 8px; }
.card .delta span.up { color: var(--up); }
.card .delta span.down { color: var(--down); }
.card .delta span.flat { color: var(--muted); }
.card .parts {
  font-size: 11px;
  color: var(--muted);
  margin-top: 3px;
  line-height: 1.3;
  font-variant-numeric: tabular-nums;
}
.card .tag {
  display: inline-block;
  font-size: 10px;
  line-height: 1;
  padding: 2px 5px;
  border-radius: 4px;
  background: #f0f4ff;
  color: var(--blue);
  vertical-align: 1px;
}
.card .value.muted { color: var(--muted); font-size: 16px; font-weight: 400; }
.card.empty { opacity: .65; }
.card.loading { color: var(--muted); cursor: default; }

.asof {
  font-size: 11px;
  color: var(--muted);
  background: #f2f3f5;
  padding: 2px 8px;
  border-radius: 10px;
  white-space: nowrap;
}

.btn-back {
  padding: 4px 10px;
  font-size: 12px;
  min-height: 28px;
  border-radius: 6px;
  white-space: nowrap;
}

/* 卡片右上角图表入口图标：按图表类型区分（折线/饼图/直方），浅灰常显
   （手机无 hover），hover 才变蓝 */
.card-open {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 30px;
  height: 30px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: rgba(100, 106, 115, 0.55);
  cursor: pointer;
}
.card-open:hover { color: var(--blue); background: rgba(51, 112, 255, 0.08); }

/* 分布卡：环形图 + 图例 */
.dist-body { display: flex; align-items: center; gap: 12px; margin-top: 8px; }
.dist-body .donut { flex-shrink: 0; display: block; }
.dist-legend {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.lg-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  line-height: 1.3;
  min-width: 0;
}
.lg-row i { width: 8px; height: 8px; border-radius: 2px; flex-shrink: 0; }
.lg-row .lg-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
}
.lg-row b { font-weight: 600; font-variant-numeric: tabular-nums; flex-shrink: 0; margin-left: 8px; }

/* 时间范围：连体分段控件（省宽度，保工具行一行） */
.range {
  display: inline-flex;
  flex-shrink: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  overflow: hidden;
}
.range-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 7px 10px;
  font-size: 12.5px;
  line-height: 1.2;
  min-height: 32px;
  cursor: pointer;
  touch-action: manipulation;
}
.range-btn + .range-btn { border-left: 1px solid var(--line); }
.range-btn:hover { color: var(--blue); background: #f8faff; }
.range-btn.active { background: var(--blue); color: #fff; }
.range-btn.active:hover { color: #fff; background: var(--blue); }

/* 多指标对比：入口按钮贴右；进入后控件组（A ⇄ B ✕）原位替换，不另起一行 */
#btn-compare { margin-left: auto; flex-shrink: 0; }
#btn-compare svg { vertical-align: -2px; margin-right: 3px; }
.compare-controls {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 1;
  min-width: 0;
  justify-content: flex-end;
}
.cmp-vs { color: #98a1ab; font-size: 11px; font-weight: 600; flex-shrink: 0; }
/* .hidden 定义在前，同特异性会被上面的 display:flex 盖掉，这里显式压住 */
#compare-controls.hidden { display: none; }
.cmp-select {
  flex: 1 1 0;
  min-width: 56px;
  max-width: 220px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 12.5px;
  min-height: 32px;
}
.cmp-mini {
  flex: none;
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.cmp-mini:hover { background: rgba(100, 106, 115, 0.12); color: var(--text); }

/* ---------- 自定义下拉（替代原生 select）：触发器 + 浮层面板，风格与看板统一 ---------- */
.dd {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 6px 8px 6px 10px;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}
.dd:focus-visible { outline: 2px solid var(--blue); outline-offset: 1px; }
.dd.open { border-color: var(--blue); }
.dd-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12.5px;
}
.dd-caret { flex: none; color: var(--muted); transition: transform 0.15s ease; }
.dd.open .dd-caret { transform: rotate(180deg); color: var(--blue); }
.dd-panel {
  position: absolute;
  top: calc(100% + 6px);
  min-width: 100%;
  max-width: 280px;
  max-height: 248px;
  overflow-y: auto;
  padding: 4px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(31, 35, 41, 0.14);
  z-index: 60;
}
.dd-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 34px;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 12.5px;
  color: var(--text);
  cursor: pointer;
}
.dd-option-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dd-option.hl, .dd-option:hover { background: #f2f6ff; color: var(--blue); }
.dd-option.selected { color: var(--blue); font-weight: 600; }
.dd-option.selected svg { flex: none; color: var(--blue); }
.dd-empty { padding: 12px 10px; color: var(--muted); font-size: 12.5px; text-align: center; }

.chart {
  width: 100%;
  height: 340px;
}

/* ---------- 底部弹层：手机优先的趋势详情 ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 20, 34, 0.45);
  opacity: 0;
  transition: opacity .22s ease;
  z-index: 60;
}
.sheet-backdrop.show { opacity: 1; }

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0 auto;
  width: 100%;
  max-width: 820px;
  max-height: 92vh; /* 不用 dvh：部分内嵌浏览器会持续重排 */
  display: flex;
  flex-direction: column;
  background: var(--card);
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -12px 40px rgba(15, 20, 34, 0.22);
  transform: translateY(105%);
  transition: transform .26s cubic-bezier(.33, 1, .68, 1);
  z-index: 61;
  overflow: hidden;
}
.sheet.open { transform: translateY(0); }
body.sheet-lock { overflow: hidden; }

.sheet-grab {
  display: flex;
  justify-content: center;
  padding: 8px 0 2px;
  cursor: grab;
  touch-action: none;
}
.sheet-grab span {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: rgba(100, 106, 115, 0.3);
}

.sheet-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px 8px 14px;
}
.sheet-title-wrap {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.sheet-title-wrap h2 {
  font-size: 15px;
  margin: 0;
  line-height: 1.35;
  font-weight: 600;
  word-break: break-word;
}
.sheet-close {
  flex: none;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 50%;
  background: rgba(100, 106, 115, 0.12);
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.sheet-close:hover { background: rgba(100, 106, 115, 0.2); color: var(--text); }

.sheet-tools {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 2px 14px 10px;
  border-bottom: 1px solid var(--line);
}

.foot {
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  padding: 12px 14px calc(16px + var(--safe-bottom));
  line-height: 1.4;
}

/* 窄屏手机（含竖屏） */
@media (max-width: 640px) {
  .sync-status { display: none; }

  .brand { font-size: 15px; }

  .btn {
    padding: 8px 12px;
    font-size: 13px;
  }

  .cards {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  /* 分布卡占满整行：环形图+图例横排才放得下完整维度名，避免图例被截断 */
  .card.dist-card { grid-column: 1 / -1; }
  .card.dist-card .dist-body { gap: 16px; }

  .card { padding: 11px 10px 9px; }
  .card .value { font-size: 20px; }
  .card-open { width: 38px; height: 38px; top: 5px; right: 5px; }
  .card .name { padding-right: 34px; }

  .topbar { flex-wrap: wrap; }

  /* 工具行保持一行：范围与对比控件各自紧凑 */
  .range-btn { padding: 8px; min-height: 36px; font-size: 12px; }
  #btn-compare { padding: 8px 10px; }
  .cmp-select { min-height: 36px; font-size: 12px; padding: 6px; }
  .cmp-mini { width: 32px; height: 32px; }

  .sheet { max-height: 90vh; border-radius: 14px 14px 0 0; }
  .sheet .chart { height: 300px; }
  .sheet-title-wrap h2 { font-size: 14px; }
}

/* 超窄屏：指标卡单列更易读 */
@media (max-width: 360px) {
  .cards { grid-template-columns: 1fr; }
  .card .value { font-size: 22px; }
}

/* 平板 / 桌面：多列卡片，图表更高 */
@media (min-width: 641px) {
  .topbar {
    padding: calc(14px + var(--safe-top)) calc(24px + var(--safe-right)) 14px calc(24px + var(--safe-left));
  }

  .brand { font-size: 18px; }

  main {
    padding: 20px calc(24px + var(--safe-right)) 40px calc(24px + var(--safe-left));
  }

  .cards {
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 14px;
    margin-bottom: 22px;
  }

  .card { padding: 14px 16px; }
  .card .name { font-size: 13px; }
  .card .value { font-size: 24px; }

  .sheet { bottom: 24px; border-radius: 16px; }
  .sheet .chart { height: 400px; }
}

/* 横屏手机：图表别占满屏高 */
@media (max-width: 900px) and (orientation: landscape) {
  .sheet .chart { height: 220px; }
  .cards {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

/* 数据集切换器：多数据集部署时出现在标题旁（生产 / 测试）。 */
.ds-switch {
  display: inline-flex;
  gap: 6px;
  margin-left: 10px;
  vertical-align: middle;
}

.ds-chip {
  padding: 2px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--muted);
  font-size: 12px;
  text-decoration: none;
  line-height: 18px;
}

.ds-chip--active {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
}
