:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --line: #e6e9ee;
  --text: #1a1d21;
  --muted: #6b7280;
  --accent: #1f6feb;
  --accent-soft: #eaf1fe;
  --up: #d93a3a;
  --down: #17933f;
  --warn: #b45309;
  --warn-bg: #fef6e7;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 4px 16px rgba(16, 24, 40, .05);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --topbar-h: 52px;
  --tabbar-h: 56px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html {
  -webkit-text-size-adjust: 100%;   /* 微信里横屏/旋转后字号不放大 */
  text-size-adjust: 100%;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.55;
  overflow-x: hidden;               /* 防止某个长数字把整页顶出横向滚动 */
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }

button, input, select, textarea {
  font: inherit;
  color: inherit;
  touch-action: manipulation;       /* 去掉 iOS 300ms 点击延迟 */
}

/* ------------------------------------------------------------------ 布局 */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: rgba(255, 255, 255, .94);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  max-width: 1080px;
  margin: 0 auto;
  padding: 10px 16px;
  min-height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 12px;
}
.brand {
  font-weight: 700;
  font-size: 16px;
  letter-spacing: .2px;
  display: flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
}
.brand .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-soft);
}
.nav {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: none;
  flex: 1;
}
.nav::-webkit-scrollbar { display: none; }
.nav a, .nav button {
  padding: 6px 12px;
  border-radius: 999px;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
}
.nav a.active, .nav button.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}
.who {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 4px;
}
.who b { color: var(--text); font-weight: 600; }

main {
  max-width: 1080px;
  margin: 0 auto;
  padding: 18px 16px 64px;
}

/* 底部标签栏：只在手机显示，微信里单手够得着 */
.tabbar { display: none; }

/* ------------------------------------------------------------------ 卡片 */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 14px;
}
.card h2, .card h3 {
  margin: 0 0 14px;
  font-size: 15px;
  font-weight: 650;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.card h2 .sub, .card h3 .sub {
  font-weight: 400;
  color: var(--muted);
  font-size: 12.5px;
}

/* 步骤序号：让人一眼看清「先填什么、后做什么」 */
.step-no {
  width: 20px;
  height: 20px;
  flex: none;
  border-radius: 6px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* 可折叠卡片（资金与成本）：默认收起，标题行给一句话摘要 */
.card.fold { padding: 0; }
.fold-head {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 15px 16px;
  background: none;
  border: 0;
  text-align: left;
  cursor: pointer;
  font-size: 15px;
  color: var(--text);
}
.fold-head b { font-weight: 650; }
.fold-sum {
  margin-left: auto;
  min-width: 0;
  font-size: 12px;
  font-weight: 400;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.fold-head .chev {
  width: 16px; height: 16px; flex: none;
  fill: none; stroke: var(--muted); stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
  transition: transform .18s ease;
}
.card.fold.open .chev { transform: rotate(90deg); }
/* 展开后摘要让位给真实字段，避免标题行挤成两截 */
.card.fold.open .fold-sum { display: none; }
.fold-body { display: none; padding: 0 16px 16px; }
.card.fold.open .fold-body { display: block; }

/* 主操作区：桌面跟在内容后面，手机吸在标签栏上方，单手就够得着 */
.actionbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 2px 0 20px;
}
.actionbar .btn.primary { min-width: 150px; }
.actionbar-space { display: none; }
.grid { display: grid; gap: 14px; }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.four { grid-template-columns: repeat(4, minmax(0, 1fr)); }

/* ------------------------------------------------------------------ 表单 */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > label {
  font-size: 12.5px;
  color: var(--muted);
  font-weight: 500;
}
.input, select.input, textarea.input {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fff;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
  -webkit-appearance: none;
  appearance: none;
  min-width: 0;
}
select.input {
  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath fill='%236b7280' d='M0 0h10L5 6z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 9px center;
  padding-right: 26px;
}
.input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
textarea.input { min-height: 96px; resize: vertical; line-height: 1.6; }
.hint { font-size: 12px; color: var(--muted); }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }

.btn {
  display: inline-flex;
  white-space: nowrap;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 16px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: #fff;
  cursor: pointer;
  font-weight: 550;
  transition: background .15s, border-color .15s, transform .04s;
}
.btn:hover { background: #fafbfc; }
.btn:active { transform: translateY(1px); }
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn.primary:hover { background: #1a61d0; }
.btn.ghost { border-color: transparent; color: var(--muted); }
.btn.danger { color: var(--up); border-color: #f3d5d5; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.block { width: 100%; }

.seg {
  display: inline-flex;
  background: #f1f3f6;
  border-radius: 10px;
  padding: 3px;
  gap: 3px;
  max-width: 100%;
}
.seg button {
  border: 0;
  background: transparent;
  padding: 6px 14px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--muted);
  font-weight: 550;
  white-space: nowrap;
}
.seg button.active { background: #fff; color: var(--text); box-shadow: 0 1px 3px rgba(16,24,40,.08); }

.switch { display: flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; min-height: 38px; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); flex: none; }

/* ------------------------------------------------------------------ 策略选择 */
.presets { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.preset {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 11px 13px;
  cursor: pointer;
  transition: border-color .15s, background .15s;
  background: #fff;
}
.preset:hover { border-color: #c9d4e6; }
.preset.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.preset b { display: block; font-size: 14px; margin-bottom: 2px; }
.preset:last-child:nth-child(odd) { grid-column: 1 / -1; }
.preset span { font-size: 12px; color: var(--muted); line-height: 1.45; }

/* ------------------------------------------------------------------ 积木条件行 */
.cond-row {
  display: flex;
  gap: 6px;
  align-items: center;
  margin-bottom: 8px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fff;
}
.cond-row .input { padding: 6px 8px; }
.cr-a { display: flex; gap: 6px; flex: 2 1 150px; min-width: 0; }
.cr-a .c-left { flex: 1 1 auto; }
.cr-a .c-left-n { width: 62px; flex: none; }
.cr-b { flex: 1 1 92px; min-width: 0; }
.cr-c { display: flex; gap: 6px; flex: 2 1 170px; min-width: 0; }
.cr-c .c-rtype { width: 66px; flex: none; }
.cr-c .c-val { width: 78px; flex: none; }
.cr-c .c-rind { flex: 1 1 auto; min-width: 0; }
.cr-c .c-rn { width: 56px; flex: none; }
.c-del { flex: none; }

/* 「全部满足 AND」这种小下拉：宽度自适应，右边留出箭头的位置 */
select.logic-select {
  width: auto;
  flex: none;
  padding: 6px 28px 6px 10px;
  font-size: 13px;
}

/* ------------------------------------------------------------------ 指标卡 */
.metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.metric {
  background: #fbfcfd;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 11px 13px;
}
.metric .k { font-size: 12px; color: var(--muted); }
.metric .v { font-size: 20px; font-weight: 680; letter-spacing: -.3px; margin-top: 2px; }
.metric .v.up { color: var(--up); }
.metric .v.down { color: var(--down); }
.metric .x { font-size: 11.5px; color: var(--muted); }

/* ------------------------------------------------------------------ 表格 */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td {
  padding: 8px 10px;
  text-align: right;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
th {
  color: var(--muted);
  font-weight: 600;
  font-size: 12px;
  background: #fbfcfd;
  position: sticky;
  top: 0;
}
th:first-child, td:first-child { text-align: left; }
tbody tr:hover { background: #fafbfd; }
td.num { font-variant-numeric: tabular-nums; }
td.wrap, th.wrap { white-space: normal; min-width: 120px; }

/* 手机上「左右滑动」提示 */
.scroll-hint {
  display: none;
  font-size: 11.5px;
  color: var(--muted);
  margin-top: 6px;
  text-align: right;
}

/* 手机卡片式列表（替代宽表格） */
.list-cards { display: none; }
.lcard {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 11px 12px;
  margin-bottom: 8px;
  background: #fff;
}
.lcard .lc-top {
  display: flex;
  align-items: baseline;
  gap: 8px;
  justify-content: space-between;
}
.lcard .lc-title { font-weight: 650; font-size: 14px; min-width: 0; }
.lcard .lc-arrow { color: var(--muted); font-size: 12px; }
.lcard .lc-mid { display: flex; gap: 10px; align-items: baseline; margin-top: 5px; }
.lcard .lc-big { font-size: 18px; font-weight: 680; letter-spacing: -.3px; }
.lcard .lc-kv {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 14px;
  margin-top: 5px;
  font-size: 12.5px;
  color: var(--muted);
}
.lcard .lc-kv b { color: var(--text); font-weight: 550; font-variant-numeric: tabular-nums; }
.lcard .lc-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 9px;
  padding-top: 9px;
  border-top: 1px dashed var(--line);
  font-size: 12.5px;
  color: var(--muted);
}
.lcard .lc-acts { display: flex; gap: 6px; flex: none; }

/* ------------------------------------------------------------------ 图表 */
.chart-box { position: relative; width: 100%; }
.chart-box canvas { display: block; width: 100%; touch-action: pan-y; }
.legend { display: flex; gap: 16px; font-size: 12.5px; color: var(--muted); margin-bottom: 6px; flex-wrap: wrap; }
.legend i {
  display: inline-block; width: 10px; height: 3px; border-radius: 2px;
  vertical-align: middle; margin-right: 5px;
}

/* ------------------------------------------------------------------ 提示 */
.alert {
  padding: 10px 12px;
  border-radius: 9px;
  font-size: 13px;
  background: var(--warn-bg);
  color: var(--warn);
  border: 1px solid #f5e3c3;
  margin-bottom: 12px;
  word-break: break-word;
}
.alert.error { background: #fdf0f0; color: #b42318; border-color: #f5d3d0; }
.alert.ok { background: #eefaf2; color: #0f7b39; border-color: #cfeeda; }
.alert ul { margin: 6px 0 0; padding-left: 18px; }

.tag {
  display: inline-block;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 11.5px;
  background: #f1f3f6;
  color: var(--muted);
  white-space: nowrap;
}
.tag.green { background: #e9f8ef; color: #0f7b39; }
.tag.red { background: #fdeeee; color: #b42318; }
.tag.blue { background: var(--accent-soft); color: var(--accent); }

.center { text-align: center; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.spacer { flex: 1; }
.hidden { display: none !important; }

/* ------------------------------------------------------------------ 登录 */
.auth-wrap {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px calc(24px + var(--safe-b));
  background:
    radial-gradient(1200px 500px at 50% -10%, #eaf1fe 0%, transparent 60%),
    var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 26px 24px;
  box-shadow: 0 10px 40px rgba(16, 24, 40, .08);
}
.auth-card h1 { font-size: 19px; margin: 0 0 4px; }
.auth-card .sub { color: var(--muted); font-size: 13px; margin-bottom: 18px; }
.auth-tabs { display: flex; gap: 6px; margin-bottom: 16px; }
.auth-tabs button {
  flex: 1; border: 0; background: #f1f3f6; color: var(--muted);
  padding: 8px 4px; border-radius: 8px; cursor: pointer; font-weight: 550; font-size: 13px;
}
.auth-tabs button.active { background: var(--accent-soft); color: var(--accent); }

/* ------------------------------------------------------------------ 回显清单 */
.echo {
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  margin-top: 10px;
}
.echo .line {
  display: flex;
  gap: 10px;
  padding: 8px 12px;
  font-size: 13.5px;
  border-bottom: 1px solid var(--line);
  align-items: baseline;
}
.echo .line:last-child { border-bottom: 0; }
.echo .line .k { color: var(--muted); width: 42px; flex: none; font-size: 12.5px; }
.echo .line .v { flex: 1; word-break: break-word; }
.echo .line.bad { background: #fdf7f7; color: #b42318; }

/* ------------------------------------------------------------------ 页面块 */
.page-head {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}
.page-head h1 { font-size: 20px; margin: 0; letter-spacing: -.3px; }
.page-head .meta { color: var(--muted); font-size: 13px; }

.loading { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.spinner {
  width: 15px; height: 15px; border-radius: 50%;
  border: 2px solid #dfe3e9; border-top-color: var(--accent);
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.kv { display: grid; grid-template-columns: 84px 1fr; gap: 6px 12px; font-size: 13px; }
.kv .k { color: var(--muted); }
.kv .v { word-break: break-all; }

/* ------------------------------------------------------------------ 弹窗（替代 prompt/confirm，微信 iOS 下原生弹窗不可靠） */
.modal-mask {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(16, 24, 40, .45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: fade .16s ease-out;
}
.modal {
  width: 100%;
  max-width: 420px;
  background: #fff;
  border-radius: 16px 16px 0 0;
  padding: 20px 20px calc(18px + var(--safe-b));
  box-shadow: 0 -8px 40px rgba(16, 24, 40, .18);
  animation: rise .2s ease-out;
}
.modal h3 { margin: 0 0 6px; font-size: 16px; }
.modal .sub { color: var(--muted); font-size: 13px; margin-bottom: 14px; line-height: 1.6; }
.modal .acts { display: flex; gap: 10px; margin-top: 16px; }
.modal .acts .btn { flex: 1; min-height: 42px; }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(16px); opacity: .6; } }

/* ================================================================== 手机 */
@media (max-width: 760px) {
  :root { --topbar-h: 48px; }

  main { padding: 12px 12px calc(var(--tabbar-h) + var(--safe-b) + 20px); }
  .card { padding: 14px; border-radius: 11px; margin-bottom: 11px; }
  .card h2 { font-size: 14.5px; margin-bottom: 12px; }

  /* 顶栏只留品牌与账号，导航挪到底部 */
  .topbar-inner { padding: 8px 12px; gap: 8px; }
  .nav.desktop-only { display: none; }
  .who .name { max-width: 84px; overflow: hidden; text-overflow: ellipsis; }
  .brand { font-size: 15px; }

  .tabbar {
    display: flex;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 40;
    background: rgba(255, 255, 255, .97);
    backdrop-filter: saturate(180%) blur(14px);
    -webkit-backdrop-filter: saturate(180%) blur(14px);
    border-top: 1px solid var(--line);
    padding-bottom: var(--safe-b);
  }
  .tabbar a {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    height: var(--tabbar-h);
    color: var(--muted);
    font-size: 11px;
    -webkit-tap-highlight-color: transparent;
  }
  .tabbar a svg { width: 21px; height: 21px; stroke: currentColor; fill: none; stroke-width: 1.7; }
  .tabbar a.active { color: var(--accent); }
  .tabbar a:active { background: #f6f8fb; }

  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .metric { padding: 10px 11px; }
  .metric .v { font-size: 19px; }
  .presets { grid-template-columns: 1fr; }
  .preset { padding: 11px 12px; }
  .grid.two, .grid.three, .grid.four { grid-template-columns: 1fr; }
  /* 策略参数（周期数这类短数字）两列排，省掉一半滚动 */
  #params { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .page-head h1 { font-size: 18px; }
  .page-head .meta { font-size: 12.5px; }

  /* 输入框字号必须 ≥16px，否则 iOS 微信点进去会自动放大整页 */
  .input, select.input, textarea.input { font-size: 16px; padding: 10px 11px; }
  select.input { padding-right: 28px; }   /* 给自绘的三角箭头留位，别压住文字 */
  .btn { min-height: 40px; padding: 9px 15px; }
  .btn.small { min-height: 32px; padding: 6px 10px; font-size: 12.5px; }
  .seg { width: 100%; overflow-x: auto; flex-wrap: nowrap; }
  .seg button { flex: 1 0 auto; padding: 8px 12px; font-size: 13px; }

  /* 积木条件行：拆成三行，否则七个控件挤成一团 */
  .cond-row { display: grid; grid-template-columns: 1fr; gap: 6px; padding: 9px 42px 9px 9px; position: relative; }
  .cr-a { flex: none; }
  .cr-b, .cr-c { flex: none; }
  .cr-c .c-rtype { width: 80px; }
  .c-del {
    position: absolute; top: 5px; right: 5px;
    width: 32px; height: 32px; min-height: 32px; padding: 0;
    font-size: 17px; line-height: 1; color: var(--muted);
  }

  table { font-size: 12.5px; }
  th, td { padding: 7px 9px; }
  .scroll-hint { display: block; }

  /* 宽表格换成卡片列表 */
  .desk-only { display: none !important; }
  .hide-sm { display: none !important; }
  .list-cards { display: block; }

  .chart-tip { font-size: 11.5px !important; }
  .auth-wrap { padding: 18px 14px calc(18px + var(--safe-b)); }
  .auth-card { padding: 22px 18px; border-radius: 14px; }
  .auth-card h1 { font-size: 18px; }
  .kv { grid-template-columns: 76px 1fr; }
  .modal { padding: 18px 16px calc(16px + var(--safe-b)); }

  /* 主操作吸底：放在标签栏正上方，滚到哪儿都能一键开始 */
  .actionbar {
    position: fixed;
    left: 0; right: 0;
    bottom: calc(var(--tabbar-h) + var(--safe-b));
    z-index: 35;
    margin: 0;
    padding: 8px 12px;
    background: rgba(255, 255, 255, .97);
    backdrop-filter: saturate(180%) blur(14px);
    -webkit-backdrop-filter: saturate(180%) blur(14px);
    border-top: 1px solid var(--line);
    box-shadow: 0 -2px 12px rgba(16, 24, 40, .05);
  }
  .actionbar .btn { min-height: 42px; }
  .actionbar .btn.primary { flex: 1 1 auto; min-width: 0; font-size: 15px; }
  .actionbar .btn:not(.primary) { flex: 0 0 auto; }
  .actionbar-space { display: block; height: 64px; }

  .card.fold { padding: 0; }
  .fold-head { padding: 14px; font-size: 14.5px; }
  .fold-body { padding: 0 14px 14px; }
  .fold-sum { max-width: 58%; }
  .step-no { width: 18px; height: 18px; border-radius: 5px; font-size: 11.5px; }
}

/* 超小屏（iPhone SE 之类）再收一档 */
@media (max-width: 360px) {
  .metric .v { font-size: 17px; }
  .tabbar a { font-size: 10px; }
  .auth-tabs button { font-size: 12px; }
}
