:root {
  --bg: #0f172a;
  --bg-2: #1e293b;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --line: #e2e8f0;
  --text: #0f172a;
  --text-dim: #64748b;
  --text-inv: #f8fafc;
  --brand: #6366f1;
  --brand-d: #4f46e5;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --amber: #d97706;
  --amber-bg: #fef3c7;
  --radius: 16px;
  --radius-sm: 11px;
  --shadow: 0 1px 3px rgba(15,23,42,.08), 0 6px 24px rgba(15,23,42,.06);
  --shadow-lg: 0 10px 40px rgba(15,23,42,.18);
  --safe-b: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--surface-2);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
button { font-family: inherit; cursor: pointer; }

#app { min-height: 100vh; padding-bottom: calc(150px + var(--safe-b)); }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: linear-gradient(135deg, #4f46e5, #7c3aed);
  color: var(--text-inv);
  padding-top: env(safe-area-inset-top, 0px);
  box-shadow: 0 2px 16px rgba(79,70,229,.3);
}
.topbar-inner {
  max-width: 1100px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px;
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; font-size: 18px; }
.brand-mark {
  display: inline-grid; place-items: center;
  width: 30px; height: 30px; border-radius: 9px;
  background: rgba(255,255,255,.2); font-size: 16px;
}
.week-switch { display: flex; align-items: center; gap: 4px; }
.icon-btn {
  background: rgba(255,255,255,.15); color: #fff; border: none;
  width: 34px; height: 34px; border-radius: 10px; font-size: 20px; line-height: 1;
  display: grid; place-items: center; transition: background .15s;
}
.icon-btn:active { background: rgba(255,255,255,.3); }
.week-label {
  background: rgba(255,255,255,.15); color: #fff; border: none;
  height: 34px; padding: 0 14px; border-radius: 10px; font-weight: 600; font-size: 13px;
  min-width: 120px;
}

/* ---------- View container ---------- */
.view { max-width: 1100px; margin: 0 auto; padding: 18px 16px 0; }
.section-title { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--text-dim); margin: 22px 4px 10px; }
.muted { color: var(--text-dim); }

/* ---------- Summary strip ---------- */
.summary {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px;
  margin-bottom: 6px;
}
.summary .stat {
  background: var(--surface); border-radius: var(--radius-sm); padding: 12px 13px;
  box-shadow: var(--shadow);
}
.stat .stat-label { font-size: 11px; color: var(--text-dim); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.stat .stat-value { font-size: 19px; font-weight: 750; margin-top: 3px; }

/* ---------- Member cards ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
.card {
  background: var(--surface); border-radius: var(--radius); padding: 15px;
  box-shadow: var(--shadow); border: 1px solid var(--line);
  text-align: left; position: relative; overflow: hidden;
  transition: transform .12s ease, box-shadow .12s ease;
}
.card:active { transform: scale(.985); }
.card .avatar {
  width: 40px; height: 40px; border-radius: 12px; color: #fff; font-weight: 700;
  display: grid; place-items: center; font-size: 17px; margin-bottom: 10px;
}
.card .who { display: flex; align-items: center; justify-content: space-between; }
.card .name { font-weight: 700; font-size: 15px; }
.card .role-pill { font-size: 10px; font-weight: 700; padding: 2px 7px; border-radius: 999px; text-transform: uppercase; letter-spacing: .03em; }
.role-parent { background: #ede9fe; color: #6d28d9; }
.role-child { background: #e0f2fe; color: #0369a1; }
.card .balance { font-size: 26px; font-weight: 800; margin-top: 12px; letter-spacing: -.02em; }
.card .balance.neg { color: var(--red); }
.card .subline { font-size: 12px; color: var(--text-dim); margin-top: 4px; }
.card .meter { height: 6px; border-radius: 999px; background: var(--line); margin-top: 11px; overflow: hidden; }
.card .meter > span { display: block; height: 100%; border-radius: 999px; }
.card .disc { display: flex; align-items: center; justify-content: space-between; margin-top: 9px; font-size: 12px; }
.card .disc b { font-size: 13px; }
.card .streak-pill { display: inline-block; margin-top: 10px; font-size: 11px; font-weight: 700; color: var(--amber); background: var(--amber-bg); border-radius: 999px; padding: 4px 9px; white-space: nowrap; }
.card .streak-pill.due { color: var(--green); background: var(--green-bg); }
.chip { font-size: 11px; font-weight: 700; padding: 3px 8px; border-radius: 999px; }
.chip.ok { background: var(--green-bg); color: var(--green); }
.chip.warn { background: var(--amber-bg); color: var(--amber); }
.chip.bad { background: var(--red-bg); color: var(--red); }

/* ---------- Quick actions ---------- */
.quick {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(68px, 1fr)); gap: 10px; margin-top: 4px;
}
.quick button {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-sm);
  padding: 14px 6px; display: flex; flex-direction: column; align-items: center; gap: 6px;
  box-shadow: var(--shadow); font-weight: 600; font-size: 12px; color: var(--text);
  transition: transform .1s;
}
.quick button:active { transform: translateY(1px); }
.quick .qico { font-size: 20px; }

/* ---------- Transaction list ---------- */
.tx-list { display: flex; flex-direction: column; gap: 1px; background: var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.tx {
  background: var(--surface); padding: 12px 14px; display: flex; align-items: center; gap: 12px;
}
.tx .tx-ico { width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center; font-size: 16px; flex-shrink: 0; }
.tx .tx-main { flex: 1; min-width: 0; }
.tx .tx-title { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx .tx-sub { font-size: 12px; color: var(--text-dim); margin-top: 2px; }
.tx .tx-amt { font-weight: 750; font-size: 15px; white-space: nowrap; }
.tx .tx-amt.pos { color: var(--green); }
.tx .tx-amt.neg { color: var(--red); }
.tx .tx-del { background: none; border: none; color: var(--text-dim); font-size: 16px; padding: 4px 2px 4px 8px; }
.empty { text-align: center; color: var(--text-dim); padding: 36px 16px; font-size: 14px; }
.empty .big { font-size: 32px; margin-bottom: 8px; }

/* ---------- Filters ---------- */
.filterbar { display: flex; gap: 8px; overflow-x: auto; padding: 4px 0 10px; -webkit-overflow-scrolling: touch; }
.filterbar button {
  background: var(--surface); border: 1px solid var(--line); border-radius: 999px;
  padding: 7px 13px; font-size: 13px; font-weight: 600; color: var(--text-dim); white-space: nowrap;
}
.filterbar button.is-active { background: var(--brand); border-color: var(--brand); color: #fff; }

/* ---------- Trends ---------- */
.panel { background: var(--surface); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); border: 1px solid var(--line); margin-bottom: 14px; }
.panel h3 { margin: 0 0 4px; font-size: 15px; }
.panel .sub { font-size: 12px; color: var(--text-dim); margin-bottom: 14px; }
.legend { display: flex; flex-wrap: wrap; gap: 10px 16px; margin-top: 12px; font-size: 12px; }
.legend .li { display: flex; align-items: center; gap: 6px; }
.legend .sw { width: 11px; height: 11px; border-radius: 3px; }
.barrow { display: flex; align-items: center; gap: 10px; margin: 9px 0; font-size: 13px; }
.barrow .lab { width: 96px; flex-shrink: 0; color: var(--text-dim); }
.barrow .track { flex: 1; height: 22px; background: var(--surface-2); border-radius: 7px; overflow: hidden; }
.barrow .fill { height: 100%; border-radius: 7px; }
.barrow .val { width: 64px; text-align: right; font-weight: 700; }

/* ---------- Review table ---------- */
.tablewrap { overflow-x: auto; border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--line); background: var(--surface); }
table.review { border-collapse: collapse; width: 100%; min-width: 560px; font-size: 14px; }
table.review th, table.review td { padding: 12px 14px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.review th { background: var(--surface-2); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--text-dim); position: sticky; top: 0; }
table.review td:first-child, table.review th:first-child { text-align: left; position: sticky; left: 0; background: var(--surface); }
table.review th:first-child { background: var(--surface-2); }
table.review tr:last-child td { border-bottom: none; }
table.review .who { display: flex; align-items: center; gap: 9px; font-weight: 600; }
table.review .dot { width: 26px; height: 26px; border-radius: 8px; color:#fff; display:grid; place-items:center; font-size:12px; font-weight:700; }
table.review .pos { color: var(--green); } table.review .neg { color: var(--red); }
table.review tfoot td { font-weight: 750; background: var(--surface-2); }
.notearea { width: 100%; border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 12px; font: inherit; font-size: 14px; resize: vertical; min-height: 84px; margin-top: 8px; }

/* ---------- Settings ---------- */
.set-row { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 13px 14px; margin-bottom: 10px; box-shadow: var(--shadow); }
.set-row .top { display: flex; align-items: center; gap: 12px; }
.set-row .fields { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.field label { display: block; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; color: var(--text-dim); margin-bottom: 5px; }
.field input, .field select { width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: var(--radius-sm); font: inherit; font-size: 15px; background: var(--surface-2); }
.field input:focus, .field select:focus { outline: 2px solid var(--brand); border-color: var(--brand); background: #fff; }

/* ---------- Buttons ---------- */
.btn { border: none; border-radius: var(--radius-sm); padding: 13px 18px; font-weight: 700; font-size: 15px; transition: transform .1s, filter .1s; }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:active { filter: brightness(.95); }
.btn-ghost { background: var(--surface-2); color: var(--text); border: 1px solid var(--line); }
.btn-danger { background: var(--red-bg); color: var(--red); }
.btn-block { width: 100%; }
.btn-row { display: flex; gap: 10px; margin-top: 8px; }
.btn-row .btn { flex: 1; }

/* ---------- FAB ---------- */
.fab {
  position: fixed; right: 18px; bottom: calc(86px + var(--safe-b)); z-index: 30;
  width: 60px; height: 60px; border-radius: 20px; border: none;
  background: linear-gradient(135deg, #4f46e5, #7c3aed); color: #fff; font-size: 30px; font-weight: 300;
  box-shadow: 0 8px 24px rgba(79,70,229,.45); display: grid; place-items: center;
  transition: transform .12s;
}
.fab:active { transform: scale(.92); }

/* ---------- Fixed meeting bar ---------- */
.meet-bar {
  position: fixed; left: 14px; right: 14px; bottom: calc(68px + var(--safe-b)); z-index: 28;
  padding: 15px; font-size: 16px; display: flex; align-items: center; justify-content: center; gap: 9px;
  box-shadow: 0 8px 24px rgba(79,70,229,.4);
}
.meet-badge { background: rgba(255,255,255,.22); color: #fff; border-radius: 999px; padding: 3px 10px; font-size: 12px; font-weight: 800; white-space: nowrap; }
.meet-badge.over { background: var(--red); }
body.meetbar .fab { bottom: calc(150px + var(--safe-b)); }
body.meetbar #app { padding-bottom: calc(220px + var(--safe-b)); }

/* ---------- Bottom nav ---------- */
.bottomnav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
  background: rgba(255,255,255,.92); backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--line);
  display: grid; grid-template-columns: repeat(5, 1fr);
  padding: 8px 6px calc(8px + var(--safe-b));
}
.nav-btn { background: none; border: none; display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 4px; font-size: 11px; font-weight: 600; color: var(--text-dim); }
.nav-btn .nav-ico { font-size: 20px; filter: grayscale(1) opacity(.6); }
.nav-btn.is-active { color: var(--brand); }
.nav-btn.is-active .nav-ico { filter: none; }

/* ---------- Weekly jobs ---------- */
.job-line { display: flex; align-items: center; gap: 9px; padding: 5px 2px; border-top: 1px solid var(--line); font-size: 13px; font-weight: 600; cursor: pointer; }
.job-line input[type="checkbox"] { width: 18px; height: 18px; flex-shrink: 0; accent-color: var(--green); }
.job-line.done .job-name { text-decoration: line-through; color: var(--text-dim); opacity: .75; }
.jobs-pill { font-size: 11px; font-weight: 700; color: var(--amber); background: var(--amber-bg); border-radius: 999px; padding: 4px 9px; white-space: nowrap; }
.jobs-pill.ok { color: var(--green); background: var(--green-bg); }

/* ---------- Bills ---------- */
.bill-item { border-top: 1px solid var(--line); padding: 9px 0; }
.bill-item:first-child { border-top: 0; }
.bill-row { display: flex; align-items: center; gap: 10px; }
.bill-src { display: inline-flex; align-items: center; gap: 5px; margin: 8px 0 0 36px; font: inherit; font-size: 11.5px; font-weight: 700; color: var(--text-dim); background: var(--surface-2); border: 1px solid var(--line); border-radius: 999px; padding: 5px 11px; cursor: pointer; }
.bill-src.alloc { color: var(--green); background: var(--green-bg); border-color: transparent; }
.bill-row .bill-name { flex: 1; font-weight: 600; font-size: 14px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bill-row input { width: 96px; flex-shrink: 0; padding: 9px 11px; border: 1px solid var(--line); border-radius: var(--radius-sm); font: inherit; font-size: 15px; font-weight: 700; text-align: right; background: var(--surface-2); }
.bill-pct { width: 50px; text-align: right; font-size: 12px; font-weight: 700; color: var(--text-dim); flex-shrink: 0; }

/* ---------- Modal ---------- */
.modal-back { position: fixed; inset: 0; z-index: 50; background: rgba(15,23,42,.45); display: flex; align-items: flex-end; justify-content: center; animation: fade .15s ease; }
.modal {
  background: var(--surface); width: 100%; max-width: 460px;
  border-radius: 22px 22px 0 0; padding: 18px 18px calc(22px + var(--safe-b));
  box-shadow: var(--shadow-lg); animation: slideup .22s cubic-bezier(.2,.8,.2,1);
  max-height: 92vh; overflow-y: auto;
}
.modal h2 { margin: 4px 0 4px; font-size: 19px; }
.modal .modal-sub { color: var(--text-dim); font-size: 13px; margin-bottom: 16px; }
.modal .grabber { width: 38px; height: 4px; background: var(--line); border-radius: 999px; margin: 0 auto 14px; }
.form-grid { display: flex; flex-direction: column; gap: 13px; }
.seg { display: flex; gap: 6px; background: var(--surface-2); padding: 4px; border-radius: var(--radius-sm); }
.seg button { flex: 1; border: none; background: none; padding: 9px; border-radius: 9px; font-weight: 600; font-size: 13px; color: var(--text-dim); }
.seg button.on { background: #fff; color: var(--brand); box-shadow: var(--shadow); }
.who-pick { display: flex; gap: 8px; flex-wrap: wrap; }
.who-pick button { border: 1px solid var(--line); background: var(--surface-2); border-radius: 999px; padding: 8px 12px; font-weight: 600; font-size: 13px; display: flex; align-items: center; gap: 7px; }
.who-pick button.on { border-color: var(--brand); background: #eef2ff; color: var(--brand-d); }
.who-pick .mini { width: 20px; height: 20px; border-radius: 6px; color: #fff; display: grid; place-items: center; font-size: 11px; font-weight: 700; }
.split-list { display: flex; flex-direction: column; gap: 8px; }
.split-row { display: flex; align-items: center; gap: 10px; }
.split-row .nm { flex: 1; display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; }
.split-row input { width: 92px; text-align: right; }
.split-row .cap { font-size: 11px; color: var(--amber); }
.callout { background: var(--amber-bg); color: #92400e; border-radius: var(--radius-sm); padding: 10px 12px; font-size: 12.5px; line-height: 1.4; }
.callout.info { background: #eff6ff; color: #1e40af; }

@keyframes slideup { from { transform: translateY(100%); } to { transform: translateY(0); } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* ---------- Bonus celebration ---------- */
.celebrate {
  position: fixed; inset: 0; z-index: 120; overflow: hidden;
  background: linear-gradient(160deg, #4f46e5, #7c3aed 55%, #db2777);
  display: flex; align-items: center; justify-content: center; padding: 26px; text-align: center; color: #fff;
  animation: fade .2s ease;
}
.celebrate-card { position: relative; z-index: 2; max-width: 340px; }
.celebrate-title { font-size: 26px; font-weight: 800; margin-top: 10px; line-height: 1.15; }
.celebrate-sub { font-size: 15px; opacity: .92; margin-top: 8px; }
.celebrate-amount { font-size: 54px; font-weight: 900; margin: 16px 0 2px; text-shadow: 0 6px 24px rgba(0,0,0,.28); }
.confetti { position: absolute; inset: 0; z-index: 1; pointer-events: none; }
.confetti-piece { position: absolute; top: -24px; width: 10px; height: 15px; border-radius: 2px; opacity: .92; animation: confetti-fall linear infinite; }
@keyframes confetti-fall {
  0% { transform: translateY(-24px) rotateZ(0); }
  100% { transform: translateY(106vh) rotateZ(720deg); }
}
.balloon { position: absolute; bottom: -70px; animation: balloon-rise linear infinite; }
@keyframes balloon-rise {
  0% { transform: translateY(0) translateX(0); }
  50% { transform: translateY(-55vh) translateX(14px); }
  100% { transform: translateY(-118vh) translateX(0); }
}

/* ---------- PIN gate ---------- */
.gate {
  position: fixed; inset: 0; z-index: 100;
  background: linear-gradient(160deg, #4f46e5, #7c3aed 60%, #9333ea);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 24px; color: #fff; text-align: center;
}
.gate .gate-mark { width: 66px; height: 66px; border-radius: 20px; background: rgba(255,255,255,.18); display: grid; place-items: center; font-size: 32px; margin-bottom: 18px; }
.gate h1 { font-size: 24px; margin: 0 0 6px; }
.gate p { margin: 0 0 26px; opacity: .85; font-size: 14px; max-width: 300px; line-height: 1.5; }
.pin-dots { display: flex; gap: 14px; margin-bottom: 26px; height: 18px; }
.pin-dots span { width: 15px; height: 15px; border-radius: 50%; border: 2px solid rgba(255,255,255,.55); transition: background .12s, transform .12s; }
.pin-dots span.on { background: #fff; border-color: #fff; transform: scale(1.08); }
.pin-pad { display: grid; grid-template-columns: repeat(3, 74px); gap: 14px; }
.pin-pad button { height: 74px; border-radius: 50%; border: none; background: rgba(255,255,255,.16); color: #fff; font-size: 28px; font-weight: 600; transition: background .12s, transform .08s; }
.pin-pad button:active { background: rgba(255,255,255,.34); transform: scale(.94); }
.pin-pad button.ghost { background: none; font-size: 15px; font-weight: 600; }
.gate .gate-err { min-height: 20px; margin-top: 18px; color: #fecaca; font-weight: 600; font-size: 14px; }
.gate .gate-busy { margin-top: 18px; font-size: 14px; opacity: .9; }
.gate-spin { width: 38px; height: 38px; border: 3px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- sync pill ---------- */
.sync-pill { display: inline-flex; align-items: center; gap: 6px; font-size: 11px; font-weight: 600; opacity: .9; }
.sync-pill .dot { width: 7px; height: 7px; border-radius: 50%; background: #4ade80; box-shadow: 0 0 0 0 rgba(74,222,128,.6); }
.sync-pill.live .dot { animation: pulse 2s infinite; }
.sync-pill.off .dot { background: #fbbf24; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(74,222,128,.5); } 70% { box-shadow: 0 0 0 6px rgba(74,222,128,0); } 100% { box-shadow: 0 0 0 0 rgba(74,222,128,0); } }

/* ---------- Desktop ---------- */
@media (min-width: 760px) {
  .modal-back { align-items: center; }
  .modal { border-radius: 22px; }
  .summary { grid-template-columns: repeat(3, 1fr); }
  .fab { display: none; }
  #app { padding-bottom: 40px; }
  body.meetbar #app { padding-bottom: 40px; }
  .meet-bar { position: static; margin-top: 14px; width: 100%; }
  .bottomnav {
    position: sticky; top: 0; max-width: 1100px; margin: 0 auto; bottom: auto;
    background: transparent; backdrop-filter: none; border: none;
    grid-template-columns: repeat(5, max-content); gap: 4px; justify-content: center;
    padding: 12px 0 0;
  }
  .nav-btn { flex-direction: row; gap: 7px; padding: 9px 16px; border-radius: 999px; font-size: 14px; }
  .nav-btn.is-active { background: #eef2ff; }
  .nav-btn .nav-ico { font-size: 16px; }
  .topbar { order: -1; }
}
