/* Hide scrollbars */
* {
  scrollbar-width: none;
}
*::-webkit-scrollbar {
  display: none;
}

:root {
  --bg: #1a1820;
  --bg-1: #222028;
  --bg-2: #25232c;
  --bg-3: #2e2b36;
  --card: rgba(37, 35, 44, 0.72);
  --card-border: rgba(255, 255, 255, 0.06);
  --hair: rgba(255, 255, 255, 0.08);
  --text: #ffcdb2;
  --text-dim: #b5838d;
  --text-faint: #6d6875;
  --accent: #e5989b;
  --accent-soft: rgba(229, 152, 155, 0.16);
  --pos: #ffb4a2;
  --pos-soft: rgba(255, 180, 162, 0.16);
  --neg: #b5838d;
  --neg-soft: rgba(181, 131, 141, 0.16);
  --cash: #ffcdb2;
  --radius: 22px;
  --tab-h: 64px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --shadow: 0 10px 30px rgba(0,0,0,0.45), 0 2px 8px rgba(0,0,0,0.35);
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
    "Helvetica Neue", "Inter", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  font-size: 16px;
  overscroll-behavior-y: none;
  overflow: hidden;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  user-select: none;
  -webkit-user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  width: 100%;
  position: relative;
  overflow: hidden;
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  padding: calc(var(--safe-top) + 10px) 18px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: linear-gradient(to bottom, rgba(26,24,32,0.92), rgba(26,24,32,0.55));
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--hair);
}
.title { display: flex; align-items: center; gap: 10px; }
.title h1 { font-size: 20px; font-weight: 700; letter-spacing: -0.3px; margin: 0; }
.logo-dot {
  width: 30px; height: 30px; border-radius: 9px;
  display: grid; place-items: center;
  background: linear-gradient(150deg, #2a2f3d, #14171f);
  color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--card-border);
}

.undo-btn {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 14px; font-weight: 600;
  color: var(--text-dim);
  background: rgba(255,255,255,0.05);
  border: 0.5px solid var(--hair);
  padding: 8px 12px; border-radius: 999px;
  transition: transform .12s ease, background .2s ease, color .2s ease, opacity .2s ease;
}
.undo-btn:active { transform: scale(0.94); background: rgba(255,255,255,0.10); }
.undo-btn:disabled { opacity: 0.30; }
.undo-btn:not(:disabled) { color: var(--accent); border-color: rgba(229,152,155,0.25); }

/* ---------- View ---------- */
.view {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 16px calc(var(--tab-h) + env(safe-area-inset-bottom, 0px) + 24px);
  animation: fade .28s ease;
}

/* ---------- Pull to refresh ---------- */
.ptr {
  position: fixed;
  top: calc(var(--safe-top) + var(--tab-h) + 10px);
  left: 50%;
  transform: translateX(-50%) translateY(-100%);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 36px;
  color: var(--text-faint);
  font-size: 12px;
  font-weight: 500;
  z-index: 25;
  pointer-events: none;
  transition: transform .15s ease;
  opacity: 0;
}
.ptr.visible {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}
.ptr-spinner {
  width: 18px;
  height: 18px;
}
.ptr-spinner.spinning {
  animation: ptr-spin .8s linear infinite;
}
@keyframes ptr-spin { to { transform: rotate(360deg); } }
@keyframes fade { from { opacity: 0; transform: translateY(6px);} to {opacity:1; transform:none;} }

.section-head {
  display: flex; align-items: baseline; justify-content: space-between;
  margin: 4px 4px 12px;
}
.section-head h2 { margin: 0; font-size: 26px; font-weight: 800; letter-spacing: -0.5px; }
.section-head .sub { color: var(--text-faint); font-size: 13px; font-weight: 500; }

/* ---------- Empty ---------- */
.empty {
  margin: 64px 24px;
  text-align: center;
  color: var(--text-faint);
}
.empty .ico { opacity: 0.35; margin-bottom: 12px; display: inline-flex; }
.empty p { margin: 4px 0; font-size: 15px; }
.empty .hint { font-size: 13px; color: var(--text-faint); }

/* ---------- Person card ---------- */
.cards { display: flex; flex-direction: column; gap: 12px; }
.person {
  position: relative;
  background: var(--card);
  border: 0.5px solid var(--card-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: transform .18s ease;
}
.person::before {
  content: ""; position: absolute; inset: 0;
  background: radial-gradient(120% 80% at 0% 0%, rgba(229,152,155,0.05), transparent 55%);
  pointer-events: none;
}
.person-head {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: rgba(37, 35, 44, 0.65);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  z-index: 1;
}
.head-info { flex: 1; min-width: 0; }
.person-name { font-size: 16px; font-weight: 600; letter-spacing: -0.2px; }
.person-sub { font-size: 12px; color: var(--text-faint); margin-top: 1px; }
.person-sub .pill { display: inline-flex; gap: 4px; align-items: center; }
.avatar {
  width: 38px; height: 38px; border-radius: 50%;
  display: grid; place-items: center;
  color: #fff; font-weight: 700; font-size: 14px;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.12);
  flex-shrink: 0;
}
.eligible-badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 11px; font-weight: 700;
  color: var(--cash);
  background: rgba(255,205,178,0.12);
  border: 0.5px solid rgba(255,205,178,0.3);
  padding: 3px 8px 3px 6px; border-radius: 999px;
  white-space: nowrap;
  animation: pulse 1.8s ease-in-out infinite;
}
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(255,205,178,0.35);} 50% { box-shadow: 0 0 0 6px rgba(255,205,178,0);} }
.cashout-btn {
  border: 0; border-radius: 10px;
  padding: 7px 12px;
  font-size: 12px; font-weight: 700;
  background: linear-gradient(160deg, #ffcdb2, #ffb4a2);
  color: #1a1820;
  display: inline-flex; align-items: center; gap: 4px;
  box-shadow: 0 4px 12px rgba(255,205,178,0.3);
  transition: transform .14s ease, opacity .2s ease;
  flex-shrink: 0;
  white-space: nowrap;
}
.cashout-btn:active { transform: scale(0.94); }
.cashout-btn.hidden { display: none; }

/* Hero — sandwich: − [number] + */
.hero {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  align-items: center;
  padding: 10px 16px 14px;
  gap: 10px;
}
.hero .pill-btn {
  height: 56px;
  border-radius: 18px;
  font-size: 30px;
}
.net-num {
  font-size: 48px;
  font-weight: 800;
  letter-spacing: -2px;
  text-align: center;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}
.net-num.net-pos { color: var(--pos); }
.net-num.net-neg { color: var(--neg); }

/* Footer — pos/neg breakdown */
.footer {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 5px;
  padding: 0 16px 14px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-faint);
}
.footer .pos-count { color: var(--pos); font-size: 14px; }
.footer .neg-count { color: var(--neg); font-size: 14px; }
.footer .sep { color: var(--text-faint); opacity: 0.5; }

.pill-btn {
  border: 0;
  display: grid;
  place-items: center;
  font-size: 28px;
  font-weight: 400;
  line-height: 1;
  cursor: pointer;
  transition: transform .12s ease, background .2s ease, box-shadow .2s ease;
}
.pill-btn:active { transform: scale(0.92); }
.pill-btn.up {
  background: var(--pos);
  color: #1a1820;
  box-shadow: 0 4px 16px rgba(255,180,162,0.35);
}
.pill-btn.down {
  background: rgba(181,131,141,0.15);
  color: var(--neg);
  box-shadow: 0 3px 10px rgba(181,131,141,0.12);
}

/* number pop animation */
.pop { animation: pop .35s cubic-bezier(.2,1.4,.4,1); }
@keyframes pop { 0% { transform: scale(0.5); opacity: 0.4; } 60% { transform: scale(1.18);} 100% { transform: scale(1);} }

.bump { animation: bump .22s ease; }
@keyframes bump { 0% { transform: scale(1);} 50% { transform: scale(0.85);} 100% { transform: scale(1);} }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: var(--tab-h);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  display: flex;
  justify-content: center;
  z-index: 40;
  background: linear-gradient(to top, rgba(26,24,32,0.92), rgba(26,24,32,0.6));
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 0.5px solid var(--hair);
  overflow: hidden;
}
.tab {
  flex: 1; max-width: 33%;
  background: none; border: 0;
  color: var(--text-faint);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; font-size: 11px; font-weight: 500;
  padding-top: 4px;
  padding-bottom: 4px;
  transition: color .2s ease, transform .14s ease;
}
.tab:active { transform: scale(0.92); }
.tab.active { color: var(--accent); }
.tab svg { transition: transform .2s ease; }
.tab.active svg { transform: translateY(-1px) scale(1.05); }

/* ---------- Insights ---------- */
.chart-controls { display: flex; gap: 6px; background: rgba(255,255,255,0.05); border-radius: 12px; padding: 3px; margin: 4px 4px 16px; width: fit-content; border: 0.5px solid var(--hair); }
.seg {
  border: 0; background: none; color: var(--text-dim);
  padding: 7px 14px; font-size: 13px; font-weight: 600; border-radius: 9px;
  transition: all .2s ease;
}
.seg.active { background: var(--bg-3); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,0.3); }

.chart-card, .list-card {
  background: var(--card);
  border: 0.5px solid var(--card-border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 14px;
}
.chart-card h3, .list-card h3 { margin: 0 0 4px; font-size: 15px; font-weight: 700; }
.chart-card .sub { margin: 0 0 12px; color: var(--text-faint); font-size: 12px; }
canvas.chart { width: 100%; height: 200px; display: block; }

.legend { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 10px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-dim); }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

.picker {
  width: 100%; appearance: none; -webkit-appearance: none;
  background: var(--bg-3); color: var(--text);
  border: 0.5px solid var(--hair); border-radius: 12px;
  padding: 10px 12px; font-size: 14px; font-weight: 600;
  margin-bottom: 12px;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-dim) 50%), linear-gradient(135deg, var(--text-dim) 50%, transparent 50%);
  background-position: calc(100% - 16px) center, calc(100% - 11px) center;
  background-size: 5px 5px;
  background-repeat: no-repeat;
  padding-right: 30px;
}

.history-list { display: flex; flex-direction: column; }
.h-item {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 12px;
  padding: 11px 2px; border-bottom: 0.5px solid var(--hair);
}
.h-item:last-child { border-bottom: 0; }
.h-dot { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; font-size: 13px; font-weight: 700; }
.h-dot.pos { background: var(--pos-soft); color: var(--pos); }
.h-dot.neg { background: var(--neg-soft); color: var(--neg); }
.h-dot.cash { background: rgba(255,205,178,0.16); color: var(--cash); }
.h-dot.undo { background: rgba(255,255,255,0.08); color: var(--text-dim); }
.h-main { min-width: 0; }
.h-main .top { font-size: 14px; font-weight: 600; }
.h-main .bot { font-size: 12px; color: var(--text-faint); }
.h-amt { font-size: 14px; font-weight: 700; font-variant-numeric: tabular-nums; }
.h-amt.pos { color: var(--pos); }
.h-amt.neg { color: var(--neg); }
.h-amt.cash { color: var(--cash); }

/* ---------- Settings ---------- */
.settings-group { background: var(--card); border: 0.5px solid var(--card-border); border-radius: var(--radius); margin-bottom: 18px; overflow: hidden; box-shadow: var(--shadow); }
.settings-group .group-title { padding: 16px 16px 6px; font-size: 12px; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.5px; font-weight: 600; }
.row {
  display: flex; align-items: center; gap: 12px;
  padding: 13px 16px; border-top: 0.5px solid var(--hair);
  background: transparent;
}
.row:first-of-type { border-top: 0; }
.row .grow { flex: 1; min-width: 0; }
.row .title { font-size: 15px; font-weight: 500; margin: 0; }
.row .desc { font-size: 12px; color: var(--text-faint); margin-top: 2px; }
.input {
  width: 100%; background: var(--bg-3); color: var(--text);
  border: 0.5px solid var(--hair); border-radius: 12px;
  padding: 11px 14px; font-size: 15px; outline: none;
  margin: 4px 16px 14px; width: calc(100% - 32px);
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.btn {
  border: 0.5px solid var(--hair); background: rgba(255,255,255,0.05);
  color: var(--text); padding: 11px 16px; border-radius: 12px;
  font-size: 14px; font-weight: 600; cursor: pointer;
  transition: transform .12s ease, background .2s ease;
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); color: #1a1408; border-color: transparent; }
.btn.danger { background: var(--neg-soft); color: var(--neg); border-color: rgba(255,69,58,0.25); }
.btn-row { display: flex; gap: 10px; padding: 0 16px 16px; }
.btn-row .btn { flex: 1; }

.person-row { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-top: 0.5px solid var(--hair); }
.person-row:first-of-type { border-top: 0; }
.person-row .avatar { width: 34px; height: 34px; font-size: 13px; }
.person-row .name { flex: 1; font-size: 15px; font-weight: 500; }
.person-row .stats { font-size: 11px; color: var(--text-faint); }
.icon-btn {
  width: 32px; height: 32px; border-radius: 9px;
  background: rgba(255,255,255,0.05); border: 0.5px solid var(--hair);
  display: grid; place-items: center; color: var(--text-dim);
  transition: all .14s ease; cursor: pointer;
}
.icon-btn:active { transform: scale(0.92); }
.icon-btn.danger { color: var(--neg); }

/* ---------- Modal ---------- */
#modalRoot { position: fixed; inset: 0; z-index: 100; display: none; }
#modalRoot.show { display: block; }
.modal-back {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.55);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  animation: fade .2s ease;
}
.modal {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: calc(100% - 48px); max-width: 380px;
  background: var(--bg-2);
  border: 0.5px solid var(--card-border);
  border-radius: 22px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
  padding: 22px 20px 16px;
  animation: rise .3s cubic-bezier(.2,.9,.3,1);
}
@keyframes rise { from { transform: translate(-50%, -42%); opacity: 0;} to { transform: translate(-50%,-50%); opacity: 1;} }
.modal h3 { margin: 0 0 6px; font-size: 18px; font-weight: 700; }
.modal p { margin: 0 0 14px; color: var(--text-dim); font-size: 14px; line-height: 1.45; }
.modal .actions { display: flex; gap: 10px; }
.modal .actions .btn { flex: 1; padding: 12px; }
.modal input { margin-bottom: 18px; }

/* Undo modal */
.undo-list { display: flex; flex-direction: column; gap: 2px; margin-bottom: 14px; max-height: 320px; overflow-y: auto; }
.undo-action {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px;
  padding: 10px 8px; border-radius: 12px;
  border-bottom: 0.5px solid var(--hair);
}
.undo-action:last-child { border-bottom: 0; }
.undo-action.latest { background: rgba(255,255,255,0.03); }
.undo-action .h-dot { width: 26px; height: 26px; font-size: 11px; }
.undo-action .h-main .top { font-size: 13px; }
.undo-action .h-main .bot { font-size: 11px; }
.undo-action-btn {
  border: 0; border-radius: 999px;
  padding: 6px 14px; font-size: 12px; font-weight: 700;
  background: var(--accent); color: #1a1408;
  cursor: pointer; transition: transform .12s ease;
}
.undo-action-btn:active { transform: scale(0.94); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom, 0px) + 18px);
  left: 50%; transform: translate(-50%, 16px);
  background: rgba(34, 32, 40, 0.95);
  border: 0.5px solid var(--card-border);
  color: var(--text);
  padding: 10px 16px; border-radius: 999px;
  font-size: 13px; font-weight: 600;
  box-shadow: var(--shadow);
  opacity: 0; pointer-events: none;
  transition: opacity .2s ease, transform .25s cubic-bezier(.2,.9,.3,1);
  z-index: 200; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* small screens safety */
@media (max-width: 360px) {
  .counter .number { font-size: 26px; min-width: 38px; }
  .btn-step { width: 38px; height: 38px; }
}