/* ===== 颜色与材质变量（设计.md §0、§1） ===== */
:root {
  --bg: #F2F2F7;
  --card-2: rgba(118, 118, 128, 0.12);
  --separator: rgba(60, 60, 67, 0.14);
  --text: #000000;
  --text-2: rgba(60, 60, 67, 0.6);
  --text-3: rgba(60, 60, 67, 0.3);
  --accent: #007AFF;
  --accent-rgb: 0, 122, 255;
  --loss: #FF3B30;
  --gain: #34C759;
  --warn: #FF9500;
  --loss-rgb: 255, 59, 48;
  --gain-rgb: 52, 199, 89;

  /* 玻璃 */
  --glass: rgba(255, 255, 255, 0.55);
  --glass-strong: rgba(255, 255, 255, 0.72);
  --glass-hi: rgba(255, 255, 255, 0.9);
  --glass-rim-a: rgba(255, 255, 255, 0.95);
  --glass-rim-b: rgba(255, 255, 255, 0.15);
  --glass-shadow: 0 8px 32px rgba(40, 50, 90, 0.10), 0 1px 3px rgba(40, 50, 90, 0.06);
  --glass-blur: blur(24px) saturate(180%);
  --thumb: rgba(255, 255, 255, 0.92);
  --spring: cubic-bezier(0.34, 1.56, 0.64, 1);

  /* 背景光晕 */
  --blob-1: rgba(90, 160, 255, 0.45);
  --blob-2: rgba(80, 220, 200, 0.38);
  --blob-3: rgba(255, 150, 190, 0.38);
  --blob-4: rgba(255, 200, 120, 0.25);

  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card-2: rgba(118, 118, 128, 0.24);
    --separator: rgba(84, 84, 88, 0.45);
    --text: #FFFFFF;
    --text-2: rgba(235, 235, 245, 0.6);
    --text-3: rgba(235, 235, 245, 0.3);
    --accent: #0A84FF;
    --accent-rgb: 10, 132, 255;
    --loss: #FF453A;
    --gain: #30D158;
    --warn: #FF9F0A;
    --loss-rgb: 255, 69, 58;
    --gain-rgb: 48, 209, 88;

    --glass: rgba(44, 44, 48, 0.5);
    --glass-strong: rgba(36, 36, 40, 0.68);
    --glass-hi: rgba(255, 255, 255, 0.14);
    --glass-rim-a: rgba(255, 255, 255, 0.32);
    --glass-rim-b: rgba(255, 255, 255, 0.04);
    --glass-shadow: 0 8px 32px rgba(0, 0, 0, 0.45), 0 1px 3px rgba(0, 0, 0, 0.3);
    --thumb: rgba(120, 120, 128, 0.55);

    --blob-1: rgba(40, 90, 220, 0.42);
    --blob-2: rgba(20, 140, 125, 0.32);
    --blob-3: rgba(170, 50, 110, 0.32);
    --blob-4: rgba(160, 100, 30, 0.18);
  }
}

/* 红涨绿跌：交换 --loss 和 --gain */
html.red-up {
  --loss: #34C759;
  --gain: #FF3B30;
  --loss-rgb: 52, 199, 89;
  --gain-rgb: 255, 59, 48;
}
@media (prefers-color-scheme: dark) {
  html.red-up {
    --loss: #30D158;
    --gain: #FF453A;
    --loss-rgb: 48, 209, 88;
    --gain-rgb: 255, 69, 58;
  }
}

/* ===== 基础 ===== */
/* touch-action 不继承，只写在 body 上时双击空白处仍会放大，所以给所有元素都加上 */
* { box-sizing: border-box; touch-action: manipulation; }
[hidden] { display: none !important; }

html {
  background: var(--bg);
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* body 不设底色：底色在 html 上（防启动白屏），body 透明才能露出 .ambient 光晕 */
body {
  margin: 0;
  background: transparent;
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", sans-serif;
  font-size: 17px;
  line-height: 1.3;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior-y: none;
  -webkit-font-smoothing: antialiased;
}

button, input { font: inherit; color: inherit; }
input::placeholder { color: var(--text-3); }

.num, input, .recover-table { font-variant-numeric: tabular-nums; }

/* ===== 背景光晕：固定不动，让玻璃透出颜色 ===== */
.ambient {
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background:
    radial-gradient(60vmax 50vmax at 0% 0%, var(--blob-1), transparent 60%),
    radial-gradient(55vmax 45vmax at 100% 30%, var(--blob-2), transparent 60%),
    radial-gradient(60vmax 50vmax at 10% 85%, var(--blob-3), transparent 60%),
    radial-gradient(45vmax 40vmax at 95% 100%, var(--blob-4), transparent 60%),
    var(--bg);
}

/* 顶部滚动边缘：内容滑到导航栏下方时渐隐模糊，而不是硬边 */
.top-edge {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: calc(env(safe-area-inset-top) + 64px);
  z-index: 4;
  pointer-events: none;
  -webkit-backdrop-filter: blur(12px) saturate(160%);
  backdrop-filter: blur(12px) saturate(160%);
  -webkit-mask-image: linear-gradient(to bottom, #000 45%, transparent);
  mask-image: linear-gradient(to bottom, #000 45%, transparent);
  opacity: 0;
  transition: opacity 200ms;
}
body.scrolled .top-edge { opacity: 1; }

/* ===== 玻璃材质（共用） ===== */
.glass,
.card,
.glass-pill,
.primary-btn,
.sheet,
.action-group,
.action-cancel,
.toast {
  position: relative;
  background: var(--glass);
  -webkit-backdrop-filter: var(--glass-blur);
  backdrop-filter: var(--glass-blur);
  box-shadow: inset 0 1px 0 var(--glass-hi), var(--glass-shadow);
}

/* 边缘高光：渐变描边，左上 / 右下亮，中间淡 */
.card::after,
.glass-pill::after,
.primary-btn::after,
.sheet::after,
.action-group::after,
.action-cancel::after,
.toast::after,
.seg-thumb::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  padding: 1px;
  background: linear-gradient(135deg, var(--glass-rim-a), var(--glass-rim-b) 35%, var(--glass-rim-b) 65%, var(--glass-rim-a));
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask: linear-gradient(#000 0 0) content-box exclude, linear-gradient(#000 0 0);
  pointer-events: none;
  z-index: 3;
}

/* ===== 页面 ===== */
.page {
  min-height: 100vh;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) calc(16px + env(safe-area-inset-right))
           calc(48px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
  max-width: 600px;
  margin: 0 auto;
}

/* 二级页面：从右侧滑入 */
.page-sub {
  position: fixed;
  inset: 0;
  max-width: none;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  transform: translateX(100%);
  transition: transform 380ms cubic-bezier(0.2, 0.9, 0.25, 1);
  z-index: 10;
  visibility: hidden;
  isolation: isolate;
}
.page-sub > .ambient { position: fixed; }
.page-sub.open { transform: none; visibility: visible; }
.page-sub > *:not(.ambient) { max-width: 600px; margin-left: auto; margin-right: auto; }
body.sub-open { overflow: hidden; }

/* ===== 导航栏 / 大标题 ===== */
.navbar {
  position: sticky;
  top: env(safe-area-inset-top);
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
}
.navbar-title { font-weight: 600; }
.navbar-spacer { width: 76px; }

/* 悬浮玻璃胶囊按钮 */
.glass-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 40px;
  min-width: 64px;
  padding: 0 16px;
  border: 0;
  border-radius: 20px;
  color: var(--accent);
  font-size: 17px;
  font-weight: 500;
  cursor: pointer;
  transition: transform 300ms var(--spring);
}
.glass-pill:active { transform: scale(0.94); }
.back-btn { min-width: 76px; padding: 0 14px 0 10px; }

.large-title {
  font-size: 34px;
  font-weight: 700;
  letter-spacing: 0.01em;
  margin: 2px 4px 0;
}

/* ===== 分组与卡片 ===== */
.group { margin-top: 26px; }
.group-title {
  font-size: 13px;
  font-weight: 400;
  color: var(--text-2);
  text-transform: uppercase;
  margin: 0 18px 7px;
}
.footnote {
  font-size: 13px;
  color: var(--text-2);
  margin: 7px 18px 0;
}

.card {
  border-radius: 22px;
  overflow: hidden;
}

.row {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 48px;
  padding: 6px 18px;
}
/* 分隔线从左边开始（不通栏），最后一行没有 */
.row + .row::before,
.row-hint + .row::before {
  content: "";
  position: absolute;
  top: 0;
  left: 18px;
  right: 0;
  height: 0.5px;
  background: var(--separator);
}
.row-label {
  flex-shrink: 0;
  font-size: 17px;
  font-weight: 400;
}
.row-value {
  font-size: 17px;
  font-weight: 600;
  text-align: right;
}
.row-value-muted {
  margin-left: auto;
  color: var(--text-2);
}
.row-hint {
  font-size: 13px;
  color: var(--warn);
  margin: -2px 18px 0;
  padding-bottom: 10px;
}

button.row {
  width: 100%;
  background: none;
  border: 0;
  text-align: left;
  cursor: pointer;
  transition: background 150ms;
}
button.row:active { background: var(--card-2); }

.disclosure, .chevron {
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--text-3);
  border-bottom: 2px solid var(--text-3);
  flex-shrink: 0;
}
.disclosure { transform: rotate(-45deg); margin-left: 4px; }
.chevron { transform: rotate(-45deg); transition: transform 300ms var(--spring); }
.collapsible.open .chevron { transform: rotate(45deg); }

/* ===== 输入框 ===== */
.field {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  min-width: 0;
  flex: 1 1 auto;
  cursor: text;
}
.field input {
  width: 100%;
  min-width: 0;
  border: 0;
  outline: none;
  background: transparent;
  text-align: right;
  font-size: 17px;
  font-weight: 600;
  padding: 7px 8px;
  border-radius: 12px;
  -webkit-appearance: none;
  appearance: none;
  transition: background 150ms;
}
.field input:focus { background: var(--card-2); }
.unit {
  flex-shrink: 0;
  color: var(--text-2);
  font-size: 17px;
  padding-left: 2px;
  min-width: 14px;
}

.pair {
  display: flex;
  align-items: center;
  flex: 1 1 auto;
  min-width: 0;
  max-width: 260px;
}
.pair .field { flex: 1 1 50%; }
.pair-sep {
  width: 0.5px;
  align-self: stretch;
  margin: 6px 8px;
  background: var(--separator);
  flex-shrink: 0;
}

.row.invalid input { color: var(--warn); }

.text-input {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: none;
  background: transparent;
  text-align: right;
  font-size: 17px;
  padding: 7px 8px;
  border-radius: 12px;
}
.text-input:focus { background: var(--card-2); }

/* ===== 分段控件：胶囊槽 + 玻璃透镜滑块 ===== */
.segmented {
  --count: 2;
  --index: 0;
  --press: 1;
  position: relative;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  background: var(--card-2);
  border-radius: 999px;
  padding: 3px;
  min-width: 164px;
}
.segmented button {
  position: relative;
  z-index: 4;
  border: 0;
  background: none;
  font-size: 14px;
  font-weight: 500;
  padding: 6px 14px;
  min-height: 30px;
  border-radius: 999px;
  cursor: pointer;
}
.segmented button[aria-checked="true"] { font-weight: 600; }
.seg-thumb {
  position: absolute;
  top: 3px;
  bottom: 3px;
  left: 3px;
  width: calc((100% - 6px) / var(--count));
  transform: translateX(calc(100% * var(--index))) scale(var(--press));
  background: var(--thumb);
  -webkit-backdrop-filter: blur(8px) saturate(180%);
  backdrop-filter: blur(8px) saturate(180%);
  border-radius: 999px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.06), inset 0 1px 0 var(--glass-hi);
  transition: transform 420ms var(--spring);
}
/* 按住时滑块像透镜一样鼓起来 */
.segmented:active { --press: 1.08; }

/* ===== 开关 ===== */
.switch {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  width: 58px;
  height: 30px;
  margin: 0;
  border-radius: 999px;
  background: rgba(120, 120, 128, 0.2);
  transition: background 250ms;
  cursor: pointer;
  flex-shrink: 0;
}
.switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 36px;
  height: 26px;
  border-radius: 999px;
  background: #FFFFFF;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.08);
  transition: transform 380ms var(--spring), width 250ms var(--spring), left 250ms var(--spring);
}
.switch:checked { background: var(--gain); }
html.red-up .switch:checked { background: var(--loss); }
.switch:checked::after { transform: translateX(18px); }
/* 按住时圆钮横向拉长 */
.switch:active::after { width: 42px; }
.switch:checked:active::after { left: -4px; }
.switch:focus { outline: none; }

/* ===== 按钮 ===== */
.text-btn {
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 17px;
  padding: 10px 0;
  cursor: pointer;
}
.text-btn.strong { font-weight: 600; }
.text-btn:disabled { color: var(--text-3); cursor: default; }
.text-btn:active:not(:disabled) { opacity: 0.6; }
.row-btn { width: 100%; text-align: left; padding: 0; min-height: 32px; }

/* 主按钮：accent 色调的玻璃 */
.primary-btn {
  display: block;
  width: 100%;
  height: 54px;
  border: 0;
  border-radius: 27px;
  background: linear-gradient(180deg, rgba(var(--accent-rgb), 0.88), rgba(var(--accent-rgb), 0.76));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.45), 0 10px 28px rgba(var(--accent-rgb), 0.35);
  color: #FFFFFF;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 300ms var(--spring);
}
.primary-btn::after { --glass-rim-a: rgba(255, 255, 255, 0.7); --glass-rim-b: rgba(255, 255, 255, 0.08); }
.primary-btn:active { transform: scale(0.96); }

/* ===== 结果卡片 ===== */
.loss { color: var(--loss); }
.gain { color: var(--gain); }
.muted { color: var(--text-2); }

.result-card {
  padding: 18px 0 0;
  background: var(--glass-strong);
}
.result-main {
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 0 18px 14px;
  gap: 12px;
}
.result-col { display: flex; flex-direction: column; min-width: 0; }
.result-label, .metric-label { font-size: 13px; color: var(--text-2); }
.result-big {
  font-size: 34px;
  font-weight: 700;
  line-height: 1.15;
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.result-big.small { font-size: 26px; }
.result-big.xsmall { font-size: 20px; }

.result-metrics {
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  padding: 12px 0 16px;
}
.result-metrics::before {
  content: "";
  position: absolute;
  top: 0;
  left: 18px;
  right: 18px;
  height: 0.5px;
  background: var(--separator);
}
.metric {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 0;
}
.metric + .metric { border-left: 0.5px solid var(--separator); }
.metric-value { font-size: 17px; font-weight: 600; white-space: nowrap; }

.num { transition: opacity 120ms; }
.num.flash { opacity: 0.35; transition: none; }

/* ===== 回本对照表 ===== */
.collapse-body { display: none; padding: 0 12px 10px; }
.collapsible.open .collapse-body { display: block; }
.table-current {
  font-size: 13px;
  color: var(--text-2);
  margin: 0 6px 6px;
}
.table-current strong { color: var(--loss); font-weight: 600; }
.recover-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 17px;
}
.recover-table th {
  font-size: 13px;
  font-weight: 400;
  color: var(--text-2);
  text-align: right;
  padding: 6px 14px;
}
.recover-table td {
  text-align: right;
  padding: 9px 14px;
  border-top: 0.5px solid var(--separator);
}
.recover-table td.strong { font-weight: 700; }
.recover-table tr.hl td {
  background: rgba(var(--loss-rgb), 0.14);
  color: var(--loss);
  border-top-color: transparent;
}
.recover-table tr.hl + tr td { border-top-color: transparent; }
.recover-table tr.hl td:first-child { border-radius: 12px 0 0 12px; }
.recover-table tr.hl td:last-child { border-radius: 0 12px 12px 0; }

/* ===== 记录页 ===== */
.records-wrap { padding-top: 8px; }
.swipe-tip { margin: 8px 18px 8px; }
.records-list:empty { display: none; }
.empty {
  text-align: center;
  color: var(--text-2);
  margin: 80px 0 40px;
}
.records-actions {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin-top: 24px;
}

.rec {
  position: relative;
  overflow: hidden;
}
.rec + .rec::before {
  content: "";
  position: absolute;
  top: 0;
  left: 18px;
  right: 0;
  height: 0.5px;
  background: var(--separator);
  z-index: 2;
}
.rec-del {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 88px;
  border: 0;
  background: var(--loss);
  color: #FFFFFF;
  font-size: 17px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 150ms;
}
.rec.swiping .rec-del { opacity: 1; }
html.red-up .rec-del { background: var(--gain); }
.rec-main {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  min-height: 64px;
  padding: 10px 18px;
  border: 0;
  /* 滑动时要盖住后面的红色删除按钮，所以用接近不透明的底色 */
  background: var(--glass-strong);
  -webkit-backdrop-filter: var(--glass-blur);
  backdrop-filter: var(--glass-blur);
  text-align: left;
  cursor: pointer;
  transition: transform 380ms var(--spring);
  touch-action: pan-y;
}
.rec-main.dragging { transition: none; }
.rec-main:active { background: var(--card-2); }
.rec-left { min-width: 0; }
.rec-name {
  font-size: 17px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.rec-sub {
  font-size: 13px;
  color: var(--text-2);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.rec-right { text-align: right; flex-shrink: 0; }
.rec-pct { font-size: 15px; font-weight: 600; }
.rec-ratio { font-size: 13px; color: var(--text-2); margin-top: 2px; }

/* ===== Sheet：悬浮玻璃，不贴边 ===== */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.25);
  z-index: 20;
  opacity: 0;
  transition: opacity 300ms;
}
.sheet-backdrop.show { opacity: 1; }

.sheet {
  position: fixed;
  left: 8px;
  right: 8px;
  bottom: calc(8px + env(safe-area-inset-bottom));
  max-width: 584px;
  margin: 0 auto;
  background: var(--glass-strong);
  border-radius: 28px;
  padding: 10px 16px 20px;
  z-index: 21;
  transform: translateY(calc(100% + 40px));
  transition: transform 420ms cubic-bezier(0.2, 0.9, 0.25, 1);
}
.sheet.show { transform: none; }
.sheet .card { background: var(--glass); box-shadow: inset 0 1px 0 var(--glass-hi); }
.sheet-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  padding: 0 4px;
}
.sheet-title { font-weight: 600; }
.sheet-extra {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 4px;
}
.sheet-extra .footnote { margin: 0 0 0 16px; }

.action-sheet {
  position: fixed;
  left: 8px;
  right: 8px;
  bottom: calc(8px + env(safe-area-inset-bottom));
  max-width: 584px;
  margin: 0 auto;
  z-index: 21;
  transform: translateY(calc(100% + 40px));
  transition: transform 420ms cubic-bezier(0.2, 0.9, 0.25, 1);
}
.action-sheet.show { transform: none; }
.action-group {
  background: var(--glass-strong);
  border-radius: 26px;
  overflow: hidden;
  margin-bottom: 8px;
}
.action-title {
  margin: 0;
  padding: 14px 16px;
  text-align: center;
  font-size: 13px;
  color: var(--text-2);
}
.action-btn {
  display: block;
  width: 100%;
  min-height: 56px;
  border: 0;
  border-top: 0.5px solid var(--separator);
  background: transparent;
  color: var(--accent);
  font-size: 19px;
  cursor: pointer;
}
.action-btn:active { background: var(--card-2); }
.action-btn.danger { color: var(--loss); }
.action-cancel {
  border-top: 0;
  border-radius: 28px;
  background: var(--glass-strong);
  font-weight: 600;
}

/* ===== Toast：玻璃胶囊 ===== */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(40px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px) scale(0.9);
  background: var(--glass-strong);
  color: var(--text);
  font-size: 15px;
  font-weight: 500;
  padding: 11px 20px;
  border-radius: 999px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms, transform 380ms var(--spring);
  z-index: 40;
  white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%, 0) scale(1); }

/* ===== 窄屏 ===== */
@media (max-width: 360px) {
  .result-big { font-size: 28px; }
  .segmented { min-width: 140px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; }
}
