:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --ink: #1d1e20;
  --ink-2: #5b5e63;
  --ink-3: #8c8f94;
  --line: #e7e5e0;
  --accent: #1f7a5c;        /* 收入、主按钮、统计条 */
  --accent-soft: #e3f1ec;
  --track: #efede8;
  --danger: #c0392b;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 4px 16px rgba(0, 0, 0, .05);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181a;
    --surface: #222326;
    --ink: #ececec;
    --ink-2: #a8abb0;
    --ink-3: #7b7e83;
    --line: #313236;
    --accent: #4cc19a;
    --accent-soft: #1d3a31;
    --track: #2c2d31;
    --danger: #ef6b5d;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
.muted { color: var(--ink-2); }
.num { font-variant-numeric: tabular-nums; }

/* ---------- 登录 ---------- */
.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: min(340px, 100%);
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.login-logo {
  width: 56px; height: 56px;
  margin: 0 auto 4px;
  border-radius: 16px;
  display: grid; place-items: center;
  background: var(--accent); color: #fff;
  font-size: 28px; font-weight: 700;
}
.login-card h1 { margin: 0; font-size: 24px; }
.login-card p { margin: 0; }
.login-card input {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  text-align: center;
}
.auth-tabs { margin: 4px 0 0; }
.auth-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.hint { margin: 0; font-size: 12px; color: var(--ink-3); }
.login-card .hint { margin: -6px 0 0; }
.error { color: var(--danger); min-height: 1.5em; font-size: 14px; }

/* ---------- 主界面 ---------- */
#app {
  max-width: 560px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) 16px calc(96px + env(safe-area-inset-bottom));
}
.top {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 12px 0 8px;
}
.month-label {
  flex: 1;
  border: 0; background: none;
  font-size: 20px; font-weight: 650;
  text-align: center;
  padding: 6px;
}
.icon-btn {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  border: 0; border-radius: 10px;
  background: none;
  font-size: 22px; line-height: 1;
  color: var(--ink-2);
  list-style: none;
}
.icon-btn:hover { background: var(--track); }
.icon-btn::-webkit-details-marker { display: none; }

.menu { position: relative; }
.menu-pop {
  position: absolute; right: 0; top: 44px; z-index: 10;
  min-width: 170px;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column;
}
.menu-pop button {
  border: 0; background: none;
  padding: 10px 12px;
  text-align: left;
  border-radius: 8px;
}
.menu-pop button:hover { background: var(--track); }
.menu-me {
  max-width: 240px;
  padding: 8px 12px 10px;
  margin-bottom: 4px;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
  color: var(--ink-3);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* 账本切换按钮 */
.book-bar {
  display: flex; justify-content: center;
  margin: -4px 0 12px;
}
.book-btn {
  max-width: 75%;
  display: inline-flex; align-items: center; gap: 4px;
  padding: 4px 12px;
  border: 0; border-radius: 999px;
  background: var(--track);
  color: var(--ink-2);
  font-size: 14px; font-weight: 550;
}
.book-btn:hover { color: var(--ink); }
#book-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.caret { font-size: 11px; flex: none; }

.summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.tile.hero { grid-column: 1 / -1; padding: 16px 18px; }
.tile.hero .tile-value { font-size: 32px; letter-spacing: -.01em; }
.tile {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px;
  display: flex; flex-direction: column;
  min-width: 0;
}
.tile-label { font-size: 13px; color: var(--ink-2); }
.tile-value {
  font-size: 19px; font-weight: 650;
  font-variant-numeric: tabular-nums;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.tabs {
  display: flex; gap: 4px;
  margin: 16px 0 8px;
  padding: 4px;
  background: var(--track);
  border-radius: 12px;
}
.tabs button {
  flex: 1;
  border: 0; border-radius: 9px;
  padding: 7px;
  background: none;
  color: var(--ink-2);
  font-weight: 550;
}
.tabs button[aria-selected="true"] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
}

/* ---------- 明细 ---------- */
.day { margin-top: 14px; }
.day-head {
  display: flex; justify-content: space-between; align-items: baseline;
  padding: 0 4px 6px;
  font-size: 13px; color: var(--ink-2);
}
.day-list {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.rec {
  width: 100%;
  display: flex; align-items: center; gap: 12px;
  padding: 11px 14px;
  border: 0; background: none;
  text-align: left;
}
.rec + .rec { border-top: 1px solid var(--line); }
.rec:hover { background: var(--bg); }
.rec-icon {
  width: 36px; height: 36px; flex: none;
  display: grid; place-items: center;
  border-radius: 10px;
  background: var(--track);
  font-size: 19px;
}
.rec-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.rec-cat { font-weight: 550; }
.rec-note {
  font-size: 13px; color: var(--ink-3);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.rec-amt { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.rec-amt.income { color: var(--accent); }

.empty {
  text-align: center;
  color: var(--ink-3);
  padding: 56px 16px;
}
.empty-big { font-size: 40px; margin-bottom: 8px; }

/* ---------- 统计 ---------- */
.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-top: 12px;
}
.stat-card h2 {
  margin: 0 0 4px;
  font-size: 15px;
  display: flex; justify-content: space-between; align-items: baseline;
}
.stat-card h2 .num { font-weight: 600; }
.stat-sub { font-size: 13px; color: var(--ink-3); margin: 0 0 10px; }
.bar-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 6px 10px;
  padding: 7px 0;
  font-size: 14px;
}
.bar-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-track {
  grid-column: 1 / -1;
  display: block;
  height: 8px;
  background: var(--track);
  border-radius: 4px;
  overflow: hidden;
}
.bar-fill {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 4px;
  min-width: 2px;
}
.bar-val { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.bar-val small { color: var(--ink-3); margin-left: 6px; display: inline-block; min-width: 3em; }

/* ---------- 悬浮按钮 ---------- */
.fab {
  position: fixed;
  right: max(20px, calc(50% - 260px));
  bottom: calc(24px + env(safe-area-inset-bottom));
  width: 58px; height: 58px;
  border: 0; border-radius: 50%;
  background: var(--accent); color: #fff;
  font-size: 30px; line-height: 1;
  box-shadow: 0 6px 20px rgba(31, 122, 92, .35);
}
.fab:active { transform: scale(.96); }

/* ---------- 编辑弹窗 ---------- */
dialog {
  width: min(520px, 100%);
  max-width: 100%;
  max-height: 92dvh;
  padding: 0;
  border: 0;
  border-radius: 20px;
  background: var(--surface);
  color: var(--ink);
}
dialog::backdrop { background: rgba(0, 0, 0, .35); }
@media (max-width: 600px) {
  dialog {
    margin: auto 0 0;
    border-radius: 20px 20px 0 0;
  }
}
#editor-form {
  padding: 18px 18px calc(18px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 14px;
}
.seg {
  display: flex;
  padding: 4px;
  background: var(--track);
  border-radius: 12px;
}
.seg label { flex: 1; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
  display: block;
  text-align: center;
  padding: 7px;
  border-radius: 9px;
  color: var(--ink-2);
  font-weight: 550;
  cursor: pointer;
}
.seg input:checked + span { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); }

.amount {
  display: flex; align-items: baseline; gap: 6px;
  border-bottom: 2px solid var(--line);
  padding-bottom: 4px;
}
.amount:focus-within { border-color: var(--accent); }
.currency { font-size: 24px; color: var(--ink-2); }
.amount input {
  flex: 1; min-width: 0;
  border: 0; outline: 0; background: none;
  font-size: 34px; font-weight: 650;
  font-variant-numeric: tabular-nums;
}

.cat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
  gap: 6px;
}
.cat-grid label { position: relative; }
.cat-grid input { position: absolute; opacity: 0; pointer-events: none; }
.cat-grid span {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 2px;
  border-radius: 12px;
  border: 1.5px solid transparent;
  font-size: 13px;
  color: var(--ink-2);
  cursor: pointer;
}
.cat-grid span b { font-size: 22px; font-weight: 400; line-height: 1.2; }
.cat-grid input:checked + span {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--ink);
}
.cat-grid input:focus-visible + span { outline: 2px solid var(--accent); }
.cat-add {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 2px;
  border: 1.5px dashed var(--line);
  border-radius: 12px;
  background: none;
  font-size: 13px;
  color: var(--ink-3);
}
.cat-add b { font-size: 22px; font-weight: 400; line-height: 1.2; }
.cat-add:hover { border-color: var(--accent); color: var(--accent); }

/* 添加 / 编辑分类的小表单（编辑器和账本设置共用） */
.cat-form {
  display: flex; flex-direction: column; gap: 8px;
  padding: 10px;
  border: 1px dashed var(--line);
  border-radius: 12px;
}
#cat-new-fields { display: flex; flex-direction: column; gap: 8px; }
.cat-form-row { display: flex; align-items: center; gap: 6px; }
.cat-form-row input {
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
}
.cat-form-row .cat-icon-input { width: 3em; flex: none; text-align: center; font-size: 18px; padding: 6px 4px; }
.cat-form-row .cat-name-input { flex: 1; }
.cat-form-row .btn { flex: none; }
.emoji-row { display: flex; flex-wrap: wrap; gap: 2px; }
.emoji-row button {
  width: 36px; height: 36px;
  padding: 0;
  border: 0; border-radius: 8px;
  background: none;
  font-size: 20px; line-height: 1;
}
.emoji-row button:hover { background: var(--track); }
.cat-msg, .editor-msg { margin: 0; }
.cat-msg:empty, .editor-msg:empty { display: none; }

.row-fields { display: flex; gap: 10px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--ink-2); }
.field.grow { flex: 1; min-width: 0; }
.field input {
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  font-size: 15px;
  color: var(--ink);
  min-width: 0;
}

.actions { display: flex; gap: 8px; align-items: center; }
.spacer { flex: 1; }
.btn {
  padding: 10px 18px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-weight: 550;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); border-color: transparent; background: none; padding-left: 4px; }
.btn:disabled { opacity: .6; cursor: default; }

/* ---------- 账本弹窗 ---------- */
.sheet {
  padding: 18px 18px calc(18px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 14px;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin: -6px -8px -4px 0; }
.sheet-head h2 { margin: 0; font-size: 17px; }
.sheet-sub { margin: 0 0 4px; font-size: 13px; font-weight: 550; color: var(--ink-2); }
.sheet .error { margin: 0; }
.sheet .error.ok { color: var(--accent); }
.sheet-actions { display: flex; gap: 8px; }
.sheet-actions .btn { flex: 1; }

.inline-form { display: flex; gap: 8px; }
.inline-form input {
  flex: 1; min-width: 0;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  font-size: 15px;
  color: var(--ink);
}
.inline-form input[readonly]:not(.code) { border-color: transparent; padding-left: 0; background: none; }
.inline-form .btn { flex: none; padding: 9px 14px; font-size: 15px; }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .06em; }

.badge {
  flex: none;
  padding: 1px 7px;
  border-radius: 6px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11px; font-weight: 600;
}

.book-list { display: flex; flex-direction: column; margin: 0 -6px; }
.book-item {
  width: 100%;
  display: flex; align-items: center; gap: 8px;
  padding: 11px 8px;
  border: 0; border-radius: 10px;
  background: none;
  text-align: left;
}
.book-item:hover { background: var(--track); }
.book-item[aria-current="true"] { font-weight: 600; }
.book-check { width: 1.2em; flex: none; color: var(--accent); }
.book-item-name { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-meta { flex: none; font-size: 13px; font-weight: 400; color: var(--ink-3); }
.book-empty { margin: 4px 8px; font-size: 14px; }

.member-list { list-style: none; margin: 0; padding: 0; }
.member-list li {
  display: flex; align-items: center; gap: 8px;
  min-height: 44px;
  padding: 6px 0;
}
.member-list li + li { border-top: 1px solid var(--line); }
.member-name { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.member-name small { margin-left: 4px; }
.btn.small { padding: 4px 10px; font-size: 13px; border-radius: 9px; }
.btn.link-danger { color: var(--danger); }
.btn.danger-block { width: 100%; color: var(--danger); }

#cat-type-seg { margin-bottom: 4px; }
.cat-li-icon { width: 1.6em; flex: none; text-align: center; font-size: 19px; }
.cat-list li.cat-editing { display: block; padding: 8px 0; }
.cat-add-btn { width: 100%; margin-top: 6px; border-style: dashed; color: var(--ink-2); }

/* ---------- 提示 ---------- */
.toast {
  position: fixed;
  left: 50%; bottom: calc(100px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  background: var(--ink); color: var(--bg);
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 100;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
