/* 记一笔。颜色全走 :root 里的令牌，深色模式只改令牌。 */
:root {
  color-scheme: light dark;
  --bg: #F4F2EC;
  --card: #FFFFFF;
  --sheet: #FAF9F5;
  --chip: #EEEBE3;
  --line: #E8E5DC;
  --ink: #1A1B18;
  --ink-2: #5F6159;
  --ink-3: #8D8F87;
  --accent: #1B7A5E;
  --on-accent: #FFFFFF;
  --accent-soft: #DFEFE8;
  --warn: #A8650A;
  --danger: #B93A26;
  --series: #178561;
  --seg-on: #FFFFFF;
  --scrim: rgba(20, 21, 18, .42);
  --shadow: 0 1px 2px rgba(26, 27, 24, .04), 0 6px 20px rgba(26, 27, 24, .05);
  --r-card: 18px;
  --r-ctl: 12px;
  --t-fast: 120ms;
  --t-sheet: 280ms;
  --ease: cubic-bezier(.2, .8, .2, 1);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0F100E;
    --card: #1B1D1A;
    --sheet: #171916;
    --chip: #272925;
    --line: #2B2D29;
    --ink: #EFF0EB;
    --ink-2: #A6A9A0;
    --ink-3: #74776F;
    --accent: #4CC3A0;
    --on-accent: #072019;
    --accent-soft: #17312A;
    --warn: #E0A040;
    --danger: #F27A64;
    --series: #36A888;
    --seg-on: #3D403A;
    --scrim: rgba(0, 0, 0, .6);
    --shadow: none;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }
html.locked, html.locked body { overflow: hidden; }

body {
  margin: 0;
  min-height: 100dvh;
  padding: var(--safe-t) 0 calc(96px + var(--safe-b));
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

button, input { font: inherit; color: inherit; }
button {
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
input { -webkit-user-select: text; user-select: text; }
h1, h2, h3, p, ul, dl, dd { margin: 0; padding: 0; }
ul { list-style: none; }
svg { display: block; flex: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
.grow { flex: 1; }

#app { max-width: 520px; margin: 0 auto; padding: 0 16px; }

/* ---------- 顶栏 ---------- */
.bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  height: 56px;
  margin: 0 -8px;
  background: var(--bg);
}
.bar-title { padding: 0 4px; font-size: 17px; font-weight: 600; white-space: nowrap; }
.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: var(--r-ctl);
  color: var(--ink-2);
  transition: background var(--t-fast);
}
.icon-btn:active, .text-btn:active { background: var(--chip); }
.text-btn { min-height: 44px; padding: 0 12px; border-radius: var(--r-ctl); font-size: 15px; font-weight: 500; }
.text-btn.danger { color: var(--danger); }

/* 透明的原生日期框盖在文字上：点文字就是点输入框，系统选择器照常弹出 */
.pick { position: relative; cursor: pointer; }
.pick input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  font-size: 16px;
  cursor: pointer;
}
label.bar-title { display: grid; align-items: center; height: 44px; border-radius: var(--r-ctl); }

/* ---------- 卡片与首屏数字 ---------- */
.card { background: var(--card); border-radius: var(--r-card); box-shadow: var(--shadow); }

.hero { padding: 20px 20px 16px; }
.hero .label { font-size: 14px; color: var(--ink-2); }
.figure { margin-top: 2px; font-size: clamp(38px, 12.5vw, 50px); font-weight: 700; line-height: 1.15; letter-spacing: -.02em; }
.figure span { margin-right: 4px; font-size: .52em; font-weight: 600; color: var(--ink-2); letter-spacing: 0; }
.pair { display: flex; gap: 28px; margin-top: 12px; }
.pair dt { font-size: 13px; color: var(--ink-2); }
.pair dd { font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; }
.delta { margin-top: 8px; font-size: 14px; color: var(--ink-2); }
.delta.good { color: var(--accent); }
.delta.bad { color: var(--warn); }

/* 进度条：轨道是填充色的浅一档，整条一眼能看出状态 */
.meter {
  --fill: var(--accent);
  display: block;
  height: 8px;
  overflow: hidden;
  border-radius: 4px;
  background: var(--accent-soft);
  background: color-mix(in srgb, var(--fill) 16%, var(--card));
}
.meter i { display: block; height: 100%; border-radius: 4px; background: var(--fill); }
.meter.warn { --fill: var(--warn); }
.meter.over { --fill: var(--danger); }
.meter.thin { --fill: var(--series); height: 6px; }

.budget { display: block; width: 100%; margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--line); text-align: left; }
.budget .tip { display: block; margin-top: 10px; font-size: 14px; color: var(--ink-2); }
.budget .tip b { font-weight: 600; color: var(--ink); font-variant-numeric: tabular-nums; }
.budget-set {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: 44px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  font-size: 14px;
  color: var(--accent);
  text-align: left;
}

/* ---------- 一句话记账 ---------- */
.quick { position: relative; margin-top: 12px; }
.quick input {
  width: 100%;
  height: 50px;
  padding: 0 56px 0 16px;
  border: 1px solid transparent;
  border-radius: 14px;
  background: var(--card);
  box-shadow: var(--shadow);
  outline: none;
}
.quick input::placeholder, .note::placeholder, .cat-add input::placeholder, .money-in input::placeholder { color: var(--ink-3); }
.quick input:focus { border-color: var(--accent); }
.quick button {
  position: absolute;
  top: 5px;
  right: 5px;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--on-accent);
}
.quick input:placeholder-shown + button { background: var(--chip); color: var(--ink-3); }

/* ---------- 搜索 ---------- */
.search { position: relative; flex: 1; min-width: 0; margin-right: 8px; }
.search > svg { position: absolute; top: 10px; left: 10px; color: var(--ink-3); pointer-events: none; }
.search input {
  width: 100%;
  height: 44px;
  padding: 0 44px 0 42px;
  border: 1px solid transparent;
  border-radius: var(--r-ctl);
  background: var(--card);
  outline: none;
}
.search input::placeholder { color: var(--ink-3); }
.search input:focus { border-color: var(--accent); }
.search .clear { position: absolute; top: 0; right: 0; display: grid; place-items: center; width: 44px; height: 44px; color: var(--ink-2); }
.search .clear svg { width: 18px; height: 18px; }
.search input:placeholder-shown ~ .clear { display: none; }
.search-sum { margin: 10px 4px -8px; font-size: 14px; color: var(--ink-2); }
.search-sum b { font-weight: 600; color: var(--ink); font-variant-numeric: tabular-nums; }

/* ---------- 列表 ---------- */
.day h3, .group {
  display: flex;
  justify-content: space-between;
  margin: 22px 4px 8px;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
}
.day h3 small { margin-left: 8px; font-size: inherit; }

.rows li + li { border-top: 1px solid var(--line); }
.rows.flat { padding: 0 4px; }
.row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 62px;
  padding: 10px 16px;
  text-align: left;
  transition: background var(--t-fast);
}
button.row:active, label.row:active { background: var(--chip); }
label.row { cursor: pointer; }
.rows li:first-child .row { border-radius: var(--r-card) var(--r-card) 0 0; }
.rows li:last-child .row { border-radius: 0 0 var(--r-card) var(--r-card); }
.rows li:only-child .row { border-radius: var(--r-card); }
.row > svg { color: var(--ink-3); }

.emo {
  display: grid;
  place-items: center;
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--chip);
  font-size: 20px;
  line-height: 1;
}
.what { flex: 1; min-width: 0; }
.what b { display: block; font-weight: 500; }
.what small { display: block; overflow: hidden; font-size: 13px; color: var(--ink-2); text-overflow: ellipsis; white-space: nowrap; }
.amt { flex: none; font-weight: 600; font-variant-numeric: tabular-nums; }
.amt.income { color: var(--accent); }
.val { flex: none; font-size: 15px; color: var(--ink-2); font-variant-numeric: tabular-nums; }

.empty { padding: 56px 24px; text-align: center; color: var(--ink-2); }
.empty b { display: block; margin-bottom: 6px; font-size: 17px; font-weight: 600; color: var(--ink); }
.empty p { font-size: 15px; }

/* ---------- 分段切换 ---------- */
.seg { display: inline-flex; padding: 3px; border-radius: 11px; background: var(--chip); }
.seg button {
  min-width: 52px;
  height: 36px;
  padding: 0 14px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  color: var(--ink-2);
  transition: background var(--t-fast), color var(--t-fast);
}
.seg button.on { background: var(--seg-on); color: var(--ink); box-shadow: 0 1px 2px rgba(0, 0, 0, .1); }
.seg.wide { display: flex; margin: 4px 0 12px; }
.seg.wide button { flex: 1; }

/* ---------- 统计 ---------- */
.chart-card { margin-top: 12px; padding: 16px 16px 10px; }
.chart-read { display: flex; align-items: baseline; gap: 8px; min-height: 28px; }
.chart-read b { font-size: 20px; font-weight: 600; }
.chart-read span { font-size: 13px; color: var(--ink-2); }
.chart { width: 100%; height: auto; margin-top: 6px; border-radius: 6px; touch-action: pan-y; cursor: pointer; }
/* 手指点图表不要焦点框，键盘 Tab 过来才要 */
.chart:focus:not(:focus-visible) { outline: none; }
.chart .col { fill: var(--series); transition: opacity var(--t-fast); }
.chart.has-sel .col:not(.on) { opacity: .32; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { stroke: var(--ink-3); stroke-width: 1; }
.chart .tick { fill: var(--ink-2); font-size: 11px; font-variant-numeric: tabular-nums; }

.rank-body { flex: 1; min-width: 0; }
.rank-top { display: flex; align-items: baseline; gap: 8px; margin-bottom: 7px; }
.rank-top b { font-weight: 500; }
.rank-top small { flex: 1; font-size: 13px; color: var(--ink-2); }

/* ---------- 设置 ---------- */
.stat-pair { display: flex; padding: 18px 20px; }
.stat-pair div { flex: 1; }
.stat-pair b { display: block; font-size: 28px; font-weight: 700; line-height: 1.2; }
.stat-pair span { font-size: 13px; color: var(--ink-2); }
.tip-card { margin-top: 22px; padding: 16px 18px; }
.tip-card b { font-weight: 600; }
.tip-card p { margin-top: 4px; font-size: 14px; color: var(--ink-2); }
.about { margin: 28px 8px 0; font-size: 13px; color: var(--ink-2); text-align: center; }

/* ---------- 底部导航 ---------- */
#tabs {
  position: fixed;
  left: 50%;
  bottom: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  width: 100%;
  max-width: 520px;
  padding: 6px 12px calc(6px + var(--safe-b));
  transform: translateX(-50%);
  border-top: 1px solid var(--line);
  background: var(--card);
}
.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 48px;
  font-size: 11px;
  font-weight: 500;
  color: var(--ink-2);
  transition: color var(--t-fast);
}
.tab.on { color: var(--accent); }
.add {
  display: grid;
  place-items: center;
  width: 62px;
  height: 62px;
  margin: -30px 8px 0;
  border: 4px solid var(--bg);
  border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent);
  transition: transform var(--t-fast) var(--ease);
}
.add:active { transform: scale(.94); }

/* ---------- 弹层 ---------- */
#sheet { position: fixed; inset: 0; z-index: 50; }
.backdrop { position: absolute; inset: 0; background: var(--scrim); opacity: 0; transition: opacity 200ms linear; }
.panel {
  position: absolute;
  left: 50%;
  bottom: 0;
  width: 100%;
  max-width: 520px;
  max-height: calc(100dvh - var(--safe-t) - 28px);
  padding-bottom: var(--safe-b);
  overflow-y: auto;
  overscroll-behavior: contain;
  border-radius: 22px 22px 0 0;
  background: var(--sheet);
  outline: none;
  transform: translate(-50%, 100%);
  /* 收起比展开快，手感才利索 */
  transition: transform 200ms cubic-bezier(.4, 0, 1, 1);
}
#sheet.open .backdrop { opacity: 1; }
#sheet.open .panel { transform: translate(-50%, 0); transition: transform var(--t-sheet) var(--ease); }

.sheet-head {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  min-height: 60px;
  padding: 8px 8px 4px;
}
.sheet-head > :first-child { justify-self: start; }
.sheet-head .end { justify-self: end; }
.sheet-head h2 { font-size: 17px; font-weight: 600; }
.sheet-sub { margin: -6px 0 8px; font-size: 13px; color: var(--ink-2); text-align: center; }

.cat-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 2px 0;
  max-height: calc(100dvh - 440px - var(--safe-t) - var(--safe-b));
  min-height: 150px;
  padding: 4px 8px 8px;
  overflow-y: auto;
}
.cat-grid button { display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 5px 0; font-size: 12px; color: var(--ink-2); }
.cat-grid i {
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: var(--chip);
  font-size: 22px;
  font-style: normal;
  line-height: 1;
  transition: background var(--t-fast), box-shadow var(--t-fast);
}
.cat-grid .on { color: var(--ink); font-weight: 600; }
.cat-grid .on i { background: var(--accent-soft); box-shadow: inset 0 0 0 2px var(--accent); }

.entry-line { display: flex; align-items: center; gap: 8px; padding: 8px 12px 8px 16px; border-top: 1px solid var(--line); }
.chip {
  display: grid;
  align-items: center;
  flex: none;
  height: 36px;
  padding: 0 12px;
  border-radius: 10px;
  background: var(--chip);
  font-size: 14px;
  font-weight: 500;
}
.note { flex: 1; min-width: 0; height: 44px; padding: 0 4px; border: 0; background: none; outline: none; }
.amount {
  display: flex;
  justify-content: flex-end;
  flex: 0 1 auto;
  max-width: 58%;
  overflow: hidden;
  font-size: 26px;
  font-weight: 700;
  white-space: nowrap;
}
.amount .ph { color: var(--ink-3); }

.keys { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; padding: 6px 8px 10px; }
.key {
  display: grid;
  place-items: center;
  height: 54px;
  border-radius: var(--r-ctl);
  background: var(--card);
  font-size: 23px;
  font-weight: 500;
  transition: background 60ms;
}
.key:active { background: var(--chip); }
.key.fn { color: var(--ink-2); }
.key.small { font-size: 16px; }
.key.ok { background: var(--accent); color: var(--on-accent); font-size: 17px; font-weight: 600; }
.key.ok:active { background: var(--accent); filter: brightness(.9); }

/* ---------- 表单 ---------- */
.form { padding: 4px 20px 20px; }
.form label { display: block; margin-bottom: 8px; font-size: 14px; color: var(--ink-2); }
.money-in { display: flex; align-items: center; gap: 6px; height: 60px; padding: 0 16px; border: 1px solid var(--line); border-radius: 14px; background: var(--card); }
.money-in:focus-within { border-color: var(--accent); }
.money-in span { font-size: 22px; font-weight: 600; color: var(--ink-2); }
.money-in input { flex: 1; min-width: 0; border: 0; background: none; outline: none; font-size: 26px; font-weight: 600; }
.err { font-size: 14px; color: var(--danger); }
.err:not(:empty) { margin-top: 8px; }
.hint { margin: 10px 0 20px; font-size: 14px; color: var(--ink-2); }
.btn { display: block; width: 100%; height: 52px; border-radius: 14px; font-weight: 600; transition: filter var(--t-fast); }
.btn:active { filter: brightness(.92); }
.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.ghost { margin-top: 8px; color: var(--ink-2); }

.cat-add { display: grid; grid-template-columns: 56px 1fr 76px; gap: 8px; padding: 4px 16px 12px; }
.cat-add input { min-width: 0; height: 48px; padding: 0 14px; border: 1px solid var(--line); border-radius: var(--r-ctl); background: var(--card); outline: none; }
.cat-add input:focus { border-color: var(--accent); }
.cat-add input[name="icon"] { padding: 0; text-align: center; }
.cat-add .btn { height: 48px; border-radius: var(--r-ctl); }
.cat-add .err { grid-column: 1 / -1; }

.switch { position: relative; flex: none; width: 50px; height: 30px; border-radius: 15px; background: var(--ink-3); transition: background var(--t-fast); }
.switch::before { content: ""; position: absolute; inset: -7px 0; }
.switch i { position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; transition: transform 160ms var(--ease); }
.switch[aria-checked="true"] { background: var(--accent); }
.switch[aria-checked="true"] i { transform: translateX(20px); }

/* ---------- 提示条 ---------- */
#toast {
  position: fixed;
  top: calc(10px + var(--safe-t));
  left: 50%;
  z-index: 60;
  display: flex;
  align-items: center;
  gap: 4px;
  max-width: calc(100% - 32px);
  min-height: 46px;
  padding: 0 6px 0 18px;
  border-radius: 23px;
  background: var(--ink);
  color: var(--bg);
  font-size: 15px;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -12px);
  transition: opacity 160ms, transform 160ms var(--ease);
}
#toast.show { opacity: 1; pointer-events: auto; transform: translate(-50%, 0); }
#toast span { padding: 10px 12px 10px 0; }
#toast button { flex: none; min-height: 44px; padding: 0 14px; border-radius: 20px; font-weight: 600; color: var(--accent-soft); }

/* 提示条是反色的：深色模式下它是浅底，按钮要换成深绿才看得清 */
@media (prefers-color-scheme: dark) {
  #toast button { color: #12634B; }
}

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