:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1a1d21;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --green: #15803d;
  --red: #b91c1c;
  --blue: #1d4ed8;
  --blue-bg: #dbeafe;
  --zero: #6b7280;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 2px 8px rgba(0, 0, 0, .04);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --card: #191d24;
    --text: #e8eaee;
    --muted: #9aa3ad;
    --line: #2b313a;
    --accent: #3b82f6;
    --green: #4ade80;
    --red: #f87171;
    --blue: #93c5fd;
    --blue-bg: #1e3a8a66;
    --zero: #9aa3ad;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
body.no-scroll { overflow: hidden; }
#app { max-width: 560px; margin: 0 auto; padding: 0 16px calc(100px + env(safe-area-inset-bottom)); }
a { color: var(--accent); }
button { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; font-weight: 650; }
h3 { font-size: 14px; margin: 22px 0 8px; color: var(--muted); font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.loading { padding: 40px; text-align: center; color: var(--muted); }

/* ---------- top bar */
.top {
  display: flex; align-items: center; gap: 8px;
  padding: calc(12px + env(safe-area-inset-top)) 0 8px;
  position: sticky; top: 0; z-index: 5; background: var(--bg);
}
.top h1 { flex: 1; font-size: 20px; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top > span { width: 40px; flex: none; }
.icon-btn {
  width: 40px; height: 40px; flex: none; display: grid; place-items: center; border-radius: 50%;
  background: var(--card); border: 1px solid var(--line); color: var(--text); text-decoration: none;
  font-size: 22px; line-height: 1; cursor: pointer;
}
.icon-btn:active { transform: scale(.95); }

/* ---------- summary */
.summary { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 6px 0 16px; }
.summary > div { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.summary .lbl { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.summary strong { font-size: 20px; font-variant-numeric: tabular-nums; }
.pos { color: var(--green); }
.neg { color: var(--red); }
.zero { color: var(--zero); }

/* ---------- lists & cards */
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.card {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; text-decoration: none; color: inherit; box-shadow: var(--shadow); cursor: pointer;
}
.card:active { filter: brightness(.96); }
.card .grow { flex: 1; min-width: 0; }
.card .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .sub { font-size: 13px; margin-top: 2px; }
.card .amt { text-align: right; font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; }
.card .amt small { display: block; font-size: 11px; font-weight: 500; color: var(--muted); }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; background: var(--blue-bg); color: var(--blue);
  display: grid; place-items: center; font-weight: 700; flex: none;
}
.k-green { color: var(--green); }
.k-red { color: var(--red); }
.k-blue { color: var(--blue); }
.entry.settled { opacity: .6; }
.entry.settled .name { text-decoration: line-through; }
.group-title { font-size: 13px; margin: 14px 0 6px; }
details summary { cursor: pointer; padding: 10px 0; color: var(--muted); font-size: 14px; font-weight: 600; }
details.archived { margin-top: 18px; }
.empty { text-align: center; padding: 48px 16px; }
.empty p { margin: 4px 0; }
.empty.small { padding: 20px; font-size: 14px; }

/* ---------- friend page */
.balance {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; text-align: center; margin: 6px 0 12px; box-shadow: var(--shadow);
}
.balance .lbl { font-size: 15px; color: var(--muted); }
.balance .big-amt { font-size: 34px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; }
.balance.zero .lbl { font-size: 22px; font-weight: 650; color: var(--green); }
.tag { display: inline-block; font-size: 12px; background: var(--line); color: var(--muted); border-radius: 999px; padding: 2px 10px; margin-top: 8px; }
.btn-row { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.btn-row button {
  padding: 12px 6px; border-radius: 12px; border: 1px solid var(--line); background: var(--card);
  font-weight: 600; font-size: 14px; cursor: pointer; box-shadow: var(--shadow);
}
.btn-row button.accent { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn-row button:disabled { opacity: .45; cursor: default; }
.btn-row button:active:not(:disabled) { transform: scale(.97); }

/* ---------- floating button */
.fab {
  position: fixed; bottom: calc(24px + env(safe-area-inset-bottom)); right: max(16px, calc(50% - 264px));
  padding: 14px 22px; border-radius: 999px; border: none; background: var(--accent); color: var(--accent-text);
  font-weight: 650; font-size: 16px; box-shadow: 0 6px 18px rgba(37, 99, 235, .35); cursor: pointer; z-index: 6;
}
.fab:active { transform: scale(.96); }

/* ---------- bottom sheet */
.sheet-wrap { position: fixed; inset: 0; z-index: 20; }
.sheet-wrap .backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .4); opacity: 0; transition: opacity .18s; }
.sheet-wrap .sheet {
  position: absolute; left: 50%; bottom: 0; transform: translate(-50%, 100%);
  width: 100%; max-width: 560px; max-height: 92vh; overflow: auto;
  background: var(--card); border-radius: 20px 20px 0 0;
  padding: 18px 18px calc(18px + env(safe-area-inset-bottom));
  transition: transform .22s cubic-bezier(.2, .8, .2, 1);
}
.sheet-wrap.open .backdrop { opacity: 1; }
.sheet-wrap.open .sheet { transform: translate(-50%, 0); }
.sheet h2 { font-size: 18px; margin-bottom: 4px; }

/* ---------- forms */
.form label { display: block; font-size: 13px; color: var(--muted); margin: 14px 0 6px; }
.form input {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text); font: inherit; outline: none;
}
.form input:focus { border-color: var(--accent); }
.form input.big { font-size: 24px; font-weight: 650; }
.form .hint { font-size: 13px; margin: 12px 0 0; }
.fixed-friend { padding: 12px 14px; border-radius: 12px; background: var(--bg); font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip { padding: 6px 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--bg); font-size: 14px; cursor: pointer; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; padding: 4px; background: var(--bg); border-radius: 12px; }
.seg.four { grid-auto-flow: row; grid-template-columns: 1fr 1fr; }
.seg button {
  padding: 10px 6px; border-radius: 9px; border: none; background: transparent; font-weight: 600; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.seg button small { font-size: 11px; font-weight: 500; color: var(--muted); }
.seg button.on { background: var(--card); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); color: var(--accent); }
.actions { display: flex; gap: 8px; margin-top: 20px; }
.actions button { flex: 1; padding: 13px 8px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); font-weight: 650; cursor: pointer; }
.actions .primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.actions .ghost { background: transparent; }
.form > .primary {
  width: 100%; padding: 13px; border-radius: 12px; border: none; background: var(--accent); color: var(--accent-text);
  font-weight: 650; margin-top: 18px; cursor: pointer;
}
.danger { color: var(--red) !important; }

/* ---------- menu sheet */
.menu { display: flex; flex-direction: column; gap: 8px; }
.menu h2 { margin-bottom: 8px; }
.menu button { padding: 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); text-align: left; font-weight: 600; cursor: pointer; }
.menu button.ghost { background: transparent; text-align: center; color: var(--muted); }

/* ---------- settings & login */
.card-block { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-top: 12px; box-shadow: var(--shadow); }
.card-block .form label:first-child { margin-top: 0; }
.btn-link, .card-block > button {
  display: block; width: 100%; padding: 12px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text); text-align: center; text-decoration: none; font-weight: 600; cursor: pointer;
}
.login { padding-top: 18vh; text-align: center; }
.login h1 { font-size: 26px; margin-bottom: 20px; }
.login .form { text-align: left; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
.login .form label:first-child { margin-top: 0; }

/* ---------- toast */
#toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: #111827; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: all .2s; z-index: 30; max-width: 90vw;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.err { background: var(--red); }
