/* ============ 基础 ============ */
:root {
  --bg: #f3f5f9;
  --card: #ffffff;
  --line: #e5eaf1;
  --line-strong: #d9e0ea;
  --text: #1c2430;
  --text2: #66738a;
  --text3: #98a2b3;
  --primary: #2b62d9;
  --primary-hover: #2556bd;
  --primary-soft: #eef4ff;
  --green: #0e9f6e;
  --red: #e5484d;
  --red-soft: #fdecec;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 10px 30px rgba(16, 24, 40, .06);
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html {
  -webkit-text-size-adjust: 100%;
  /* 兜底底色:键盘弹出/聚焦导致 Safari 整页重绘时,避免闪出白色 */
  background-color: #eef1f6;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "HarmonyOS Sans SC", "Microsoft YaHei", sans-serif;
  background: linear-gradient(180deg, #f6f8fb, #eef1f6);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

button { font-family: inherit; touch-action: manipulation; }

[hidden] { display: none !important; }

/* ============ 顶栏 ============ */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  /* 用接近实色的底替代半透明+backdrop-filter:
     iOS 在键盘弹出、输入框聚焦重绘时会因 backdrop-filter 出现整页闪白/闪烁 */
  background: #fbfcfe;
  border-bottom: 1px solid var(--line);
  box-shadow: 0 1px 8px rgba(16, 24, 40, .03);
}
.topbar-in {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top));
}
.logo {
  width: 40px;
  height: 40px;
  border-radius: 11px;
  background: linear-gradient(135deg, #4d8bf5, #2b62d9);
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 1px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 10px rgba(43, 98, 217, .32);
  flex: none;
}
.titles h1 { font-size: 16.5px; font-weight: 700; letter-spacing: .5px; }
.titles p { font-size: 12px; color: var(--text3); margin-top: 1px; }

/* ============ 布局 ============ */
.app {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px 14px calc(28px + env(safe-area-inset-bottom));
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 16px;
}

/* ============ 模式切换(分段控件) ============ */
.seg {
  display: flex;
  background: #e8ecf3;
  border-radius: 12px;
  padding: 4px;
  gap: 4px;
  margin-bottom: 18px;
}
.seg-btn {
  flex: 1;
  border: none;
  background: transparent;
  height: 38px;
  border-radius: 9px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text2);
  cursor: pointer;
  transition: all .15s ease;
}
.seg-btn.active {
  background: #fff;
  color: var(--primary);
  font-weight: 600;
  box-shadow: 0 1px 4px rgba(16, 24, 40, .12);
}

/* ============ 表单 ============ */
.field-block { margin-bottom: 15px; min-width: 0; }
.flabel {
  display: block;
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text2);
  margin-bottom: 6px;
}
.hint { font-size: 12px; color: var(--text3); margin-top: 5px; }

/* 成本价+卖价 一行,平台费率+活动费率 一行(手机上也保持 2×2) */
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }

/* 会员类型 */
.type-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.type-item {
  position: relative;
  border: 1.5px solid var(--line-strong);
  border-radius: 12px;
  padding: 9px 6px;
  text-align: center;
  cursor: pointer;
  background: #fff;
  transition: border-color .15s, background .15s, box-shadow .15s;
  user-select: none;
  -webkit-user-select: none;
}
.type-item input { position: absolute; opacity: 0; width: 0; height: 0; }
.type-item .t { display: block; font-weight: 600; font-size: 15px; }
.type-item .c { display: block; font-size: 11px; color: var(--text3); margin-top: 2px; white-space: nowrap; }
.type-item.on {
  border-color: var(--primary);
  background: var(--primary-soft);
  box-shadow: 0 0 0 3px rgba(43, 98, 217, .1);
}
.type-item.on .c { color: #5b83d6; }

/* 输入框 */
.ctl { position: relative; }
.ctl input {
  width: 100%;
  padding: 11px 40px 11px 13px;
  border: 1.5px solid var(--line-strong);
  border-radius: 11px;
  font-size: 16px;              /* 16px 以上可避免 iOS 聚焦自动放大 */
  font-family: inherit;
  color: var(--text);
  background: #fff;
  transition: border-color .15s, box-shadow .15s;
}
.ctl input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(43, 98, 217, .13);
}
.ctl input::placeholder { color: #b7c0cd; font-size: 13px; }
.ctl .unit {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text3);
  font-size: 13px;
  pointer-events: none;
}
input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input[type=number] { -moz-appearance: textfield; appearance: textfield; }

/* ============ 按钮 ============ */
.btn-row { display: flex; gap: 10px; margin-top: 2px; }
.btn-primary {
  flex: 1;
  height: 46px;
  border: none;
  border-radius: 12px;
  background: linear-gradient(180deg, #3d7bef, #2b62d9);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 4px;
  cursor: pointer;
  box-shadow: 0 6px 16px rgba(43, 98, 217, .25);
  transition: all .15s;
}
.btn-primary:hover { background: linear-gradient(180deg, #3570e2, var(--primary-hover)); }
.btn-primary:active { transform: translateY(1px); box-shadow: 0 3px 8px rgba(43, 98, 217, .25); }
.btn-ghost {
  height: 46px;
  padding: 0 20px;
  border: 1.5px solid var(--line-strong);
  border-radius: 12px;
  background: #fff;
  color: var(--text2);
  font-size: 14px;
  cursor: pointer;
  transition: all .15s;
}
.btn-ghost:hover { background: #f5f7fa; color: var(--text); }
.btn-danger-ghost {
  border: 1px solid #f0c7c7;
  border-radius: 10px;
  background: #fff;
  color: var(--red);
  font-size: 13px;
  padding: 7px 13px;
  cursor: pointer;
  transition: all .15s;
}
.btn-danger-ghost:hover { background: var(--red-soft); }
.btn-danger {
  flex: 1;
  height: 44px;
  border: none;
  border-radius: 12px;
  background: var(--red);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s;
}
.btn-danger:hover { background: #d13a3f; }

/* ============ 错误提示 ============ */
.err {
  margin-top: 14px;
  background: var(--red-soft);
  border: 1px solid #f3c1c1;
  color: #c0392b;
  border-radius: 10px;
  padding: 9px 13px;
  font-size: 13px;
}

/* ============ 结果内容(悬浮窗内) ============ */
.profit-line {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 9px;
  flex-wrap: wrap;
  margin: 4px 0 14px;
}
.profit-label { font-size: 13px; color: var(--text2); }
.profit-value {
  font-size: 30px;
  font-weight: 700;
  color: var(--green);
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.popup.neg .profit-value { color: var(--red); }
.profit-tag {
  font-size: 12px;
  color: var(--text2);
  background: #f5f7fa;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 10px;
  font-variant-numeric: tabular-nums;
}
.breakdown { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.b-item {
  background: #f7f9fc;
  border: 1px solid #e9eef5;
  border-radius: 10px;
  padding: 8px 11px;
}
.b-item span { display: block; font-size: 11px; color: var(--text3); }
.b-item b { font-size: 14.5px; font-variant-numeric: tabular-nums; }
.formula {
  margin-top: 12px;
  font-size: 12px;
  color: var(--text2);
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* ============ 记录 ============ */
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: -18px -18px 0;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}
.card-head h2 { font-size: 15px; font-weight: 700; }
.card-head .stat {
  font-size: 12px;
  color: var(--text3);
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
}
.rec-list { list-style: none; margin-top: 12px; }
.rec {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 11px 8px 11px 12px;
  background: #f8fafc;
  border: 1px solid #eef1f6;
  border-radius: 12px;
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.rec + .rec { margin-top: 8px; }
.rec:hover, .rec:active { background: var(--primary-soft); border-color: #d5e3fa; }
.rec-info { flex: 1; min-width: 0; }
.rec-top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.badge {
  font-size: 11px;
  font-weight: 600;
  color: var(--primary);
  background: var(--primary-soft);
  border: 1px solid #d5e3fa;
  border-radius: 6px;
  padding: 1px 7px;
  flex: none;
}
.rec:hover .badge { background: #fff; }
.rec-price { font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }
.rec-time { font-size: 12px; color: var(--text3); margin-left: auto; font-variant-numeric: tabular-nums; }
.rec-sub {
  font-size: 12px;
  color: var(--text2);
  margin-top: 3px;
  font-variant-numeric: tabular-nums;
}
.rec-side { display: flex; align-items: center; gap: 6px; flex: none; }
.rec-profit {
  font-size: 16px;
  font-weight: 700;
  min-width: 72px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.rec-profit.pos { color: var(--green); }
.rec-profit.neg { color: var(--red); }
.rec-del {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 9px;
  background: transparent;
  color: #b6bfcc;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all .15s;
}
.rec-del:hover, .rec-del:active { background: var(--red-soft); color: var(--red); }

.empty {
  text-align: center;
  padding: 34px 0 26px;
  color: var(--text3);
  font-size: 13px;
}
.empty svg { display: block; margin: 0 auto 10px; }

.foot { text-align: center; font-size: 12px; color: var(--text3); margin-top: 2px; }

/* ============ 弹窗(确认框 / 结果悬浮窗,屏幕居中) ============ */
.modal-mask {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(15, 23, 42, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  opacity: 0;
  visibility: hidden;                 /* 平时不参与合成,避免聚焦输入/键盘弹出时整屏闪动 */
  pointer-events: none;
  transition: opacity .2s ease, visibility 0s linear .2s;
}
.modal-mask.show {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transition: opacity .2s ease;
}

/* 确认框 */
.modal {
  width: min(360px, 100%);
  background: #fff;
  border-radius: 16px;
  padding: 22px 20px 18px;
  box-shadow: 0 20px 50px rgba(15, 23, 42, .28);
  transform: translateY(12px) scale(.96);
  transition: transform .2s ease;
}
.modal-mask.show .modal { transform: none; }
.modal h3 { font-size: 16px; text-align: center; }
.modal-msg {
  margin-top: 8px;
  font-size: 13px;
  color: var(--text2);
  text-align: center;
  line-height: 1.7;
  white-space: pre-line;
  word-break: break-all;
  font-variant-numeric: tabular-nums;
}
.modal-btns { display: flex; gap: 10px; margin-top: 18px; }
.modal-btns .btn-ghost { flex: 1; height: 44px; }

/* 结果悬浮窗 */
.popup {
  width: min(400px, 100%);
  background: #fff;
  border-radius: 18px;
  padding: 16px 18px 18px;
  box-shadow: 0 24px 60px rgba(15, 23, 42, .3);
  transform: translateY(14px) scale(.96);
  transition: transform .22s ease;
}
.modal-mask.show .popup { transform: none; }
.pop-head { display: flex; align-items: center; gap: 8px; }
.pop-head h3 { font-size: 15.5px; flex: 1; text-align: center; }
.pop-close {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 9px;
  background: #f2f4f8;
  color: var(--text2);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background .15s;
  flex: none;
}
.pop-close:hover { background: #e8ecf3; }
.pop-ok {
  width: 100%;
  margin-top: 16px;
  letter-spacing: 2px;
}

/* ============ 移动端适配(iPhone 14 Pro Max 430pt) ============ */
@media (max-width: 599px) {
  .app { padding-left: 12px; padding-right: 12px; }
  .card { padding: 16px 14px; }
  .type-grid { grid-template-columns: repeat(2, 1fr); gap: 9px; }  /* 上面两个、下面两个 */
  .card-head { margin: -16px -14px 0; padding: 13px 14px; }
  .seg-btn { font-size: 13.5px; }
  .rec-profit { min-width: 64px; font-size: 15px; }
}
